cancel
Showing results for 
Search instead for 
Did you mean: 

[SOLVED] --- BSP_I2C_Read() hang my STM32 app ---

I am developping a touch screen user interface to control an audio preampl and dac.

I have no problems with I2C bus to set the registers of CT7302PL and ES9018K2M but if I try to read a single register the application hang.

This is my I2C configuration.

//######################################################################################
//# DAC SECTION
//######################################################################################
void SetReg_ES9018K2M()
{
    unsigned char reg7, reg11;
 
    BSP_I2C_Write(0x90, 8, 0b10011001);  // GPIO1 & GPIO2 as INPUT based on reg#21
 
    switch (inputsel)
    {
	    case 0:
		    //write_ext_eeprom( 1,0b10001100); old ?
	    	BSP_I2C_Write(0x90,  1, 0b10000100);
	    	BSP_I2C_Write(0x90, 21, 0b00000000);
	    	BSP_I2C_Write(0x90, 11, 0b00000000);
		    break;
 
	    case 1:
	    	BSP_I2C_Write(0x90,  1, 0b10000001);
	    	BSP_I2C_Write(0x90, 21, 0b01010000);
	    	BSP_I2C_Write(0x90, 11, 0b00110000); // 3
	    	break;
 
	    case 2:
	    	BSP_I2C_Write(0x90,  1, 0b10000001);
	    	BSP_I2C_Write(0x90, 21, 0b01010000);
	    	BSP_I2C_Write(0x90, 11, 0b01000000);  // 4
	    	break;
    }
 
    reg11 = BSP_I2C_Read(0x90, 11);
 
}
 
 
 
void BSP_I2C_Init()
{
#define I2C_ADDRESS        0x3c
#define I2C_TIMING      0x00B1112E
 
  hi2c1.Instance = I2C1;
  hi2c1.Init.Timing = I2C_TIMING;
  hi2c1.Init.OwnAddress1 = I2C_ADDRESS;
  hi2c1.Init.OwnAddress1 = 0x78; // an unused address
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
 
  I2Cx_Init(&hi2c1);
}

11 REPLIES 11

Which STM32?

I2C is relatively tricky to get right. Observe the I2C bus, and try to match the "hanging" with events on the bus.

You might want to start with bit-banging it.

JW

I am using the STM32F746G-DISCO

The app hang and stop at -"1-" only if I use touch screen otherwise all is ok

unsigned char BSP_I2C_Read(uint8_t Addr, uint8_t Reg)
{
  uint8_t Value;
 
  BSP_LCD_DisplayStringAt(20, 170, "-1-", LEFT_MODE);
 
  I2Cx_ReadMultiple(&hi2c1, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, (uint8_t*)&Value, 1);
 
  BSP_LCD_DisplayStringAt(20, 170, "-2-", LEFT_MODE);
 
  return Value;
}

Well, then there might be some problem in I2Cx_ReadMultiple(). Debug it as usually: instrument functions it's using, observe variables it is using, observe content of relevant peripheral (maily I2C presumably) registers, observe the bus...

JW

I have tried to use the TS_IO_Read(0x90, 11); instead of the BSP_I2C_Read(0x90, 11); because this does not hang the app but it return always 0.

The app lock here at "-114-" on HAL_StatusTypeDef HAL_I2C_Mem_Read()

BSP_LCD_DisplayStringAt(20, 170, "-114-", 0x03);
 
 
   /* Send Slave Address and Memory Address */
 
   if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
 
   {
 
     /* Process Unlocked */
 
     __HAL_UNLOCK(hi2c);
 
     return HAL_ERROR;
 
   }
 
 
   BSP_LCD_DisplayStringAt(20, 170, "-115-", 0x03);

Yes, adding the debug BSP_LCD_DisplayStrinAt() function sometimes the code work but after some seconds hang in this call.

if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
    {
      /* Process Unlocked */
      __HAL_UNLOCK(hi2c);
      return HAL_ERROR;
    }

-

-