Search code examples
stm32i2c

I2c communication stm32f3 how can i solve?


i want to read touch activity from the touchscreen.If i touch, i want to led blink. Some my definitions in below code but generally i want to get my activity with i2c

Some definitions:

    uint8_t deviceaddr;
static uint32_t touch_i2c_read(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t *pBuffer, uint16_t len);
static const int I2C_TIMEOUT = 65536;
unsigned char i2c_buffer[256];
uint32_t res;

This is my i2c read code:

    static uint32_t touch_i2c_read(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t *pBuffer, uint16_t len)
{ //uint8_t deviceaddr ,0x00,(uint8_t *)&buf, sizeof(buf)

  uint32_t timeout = I2C_TIMEOUT;
  while (I2C_GetFlagStatus(I2C1, I2C_ISR_BUSY) != RESET)
  {
    if ((timeout--) == 0)
      return 0;
  }
  I2C_TransferHandling(I2C1, DeviceAddr << 1, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);

  /* !!! Wait until TXIS flag is set !!! */
  timeout = I2C_TIMEOUT;
  while (I2C_GetFlagStatus(I2C1, I2C_ISR_TXIS) == RESET)
  {
    if ((timeout--) == 0)
      return 0;
  }
}

This is my settings

void configure_interrupt_pins()
{

  GPIO_InitTypeDef GPIO_InitStruct;
  EXTI_InitTypeDef EXTI_InitStruct;
  NVIC_InitTypeDef NVIC_InitStruct;

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOB, &GPIO_InitStruct);

  /*ENABLE CLOCK FOR GPIOX*/
  RCC_APB1PeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  // ENABLE CLOCK FOR SYSCFG
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  //SET PIN AS INPUT
  // init_pin(EX_NCVIC_PORT, EX_NCVIC_Pin, GPIO_MODE_INPUT, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_UP);
  //TELL THE SYSTEM THAT YOU WILL USE PXX FOR EXTI_LineX
  SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOx, EXTI_PinSourcex);

  //CONFIGIRATION of exti

  EXTI_InitStruct.EXTI_Line = EXTI_Linex;                     //pxx connect to line x
  EXTI_InitStruct.EXTI_LineCmd = ENABLE;                      //enable interrupt
  EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;            //interrupt mode
  EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising_Falling; //triggers on rising and failing edge
  EXTI_Init(&EXTI_InitStruct);                                //add to exti

  //CONFIGURATION of nvic

  NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;
  NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x00;
  NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x00;
  NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStruct);
}

This is my interrupt

  void EXTI0_IRQHandler(void)
{
  if (EXTI_GetITStatus((EXTI_Line0) != RESET))
  {
    res = touch_i2c_read(0x42, 0x00, i2c_buffer, 22);
    printf("deneme");
    if (!res)
    {
      GPIO_SetBits(GPIOE, GPIO_Pin_13);
    }

    else
    {
      GPIO_SetBits(GPIOE, GPIO_Pin_13);
    }
    EXTI_ClearITPendingBit(EXTI_Line0);
  }
}

But my code not working. Stm32 dont understand touch activity how can i solve this.

Edit i change line 1 every external interupts but i have res value 0 how can i fix this it must be different 0


Solution

  • I solved this error. I hadn't any signal pb7 and pb6 so i changed codes as below:

        // enable APB1 peripheral clock for I2C1
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
      // enable clock for SCL and SDA pins
      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
    

    And then,

    Device adress of the FT6236 was mising. it is not include at datasheet so i used debugging. I found the device adress which is the (0x32). And than my code working succesfully.