cancel
Showing results for 
Search instead for 
Did you mean: 

I2C address scan not working for some I2C slave devices

Pranay Dhuri
Associate II
Posted on June 13, 2018 at 15:36

Hi,

I am running code to scan available devices on I2C bus by using HAL_I2C_Master_Transmit() .

scan is responding for RTC+EEPROM module and listing I2C addresses perfeclty.

Now I have I2C based optical switches on bus,but it is not being listed in scan, receiving HAL_TIMEOUT /HAL_BUSY flags. 

same Optical switch is working well with Arduino.

following is my I2C initialization:

static void MX_I2C3_Init(void)

{

hi2c3.Instance = I2C3;

hi2c3.Init.ClockSpeed = 100000;

hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;

hi2c3.Init.OwnAddress1 = 0;

hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;

hi2c3.Init.OwnAddress2 = 0;

hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;

hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

if (HAL_I2C_Init(&hi2c3) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

}

// MSP init:

void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)

{

GPIO_InitTypeDef GPIO_InitStruct;

if(hi2c->Instance==I2C3)

{

/* USER CODE BEGIN I2C3_MspInit 0 */

__HAL_RCC_I2C3_CLK_ENABLE();

/* USER CODE END I2C3_MspInit 0 */

/**I2C3 GPIO Configuration

PC9 ------> I2C3_SDA

PA8 ------> I2C3_SCL

*/

GPIO_InitStruct.Pin = GPIO_PIN_9;

GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;

GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_8;

GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;

GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* Peripheral clock enable */

/* USER CODE BEGIN I2C3_MspInit 1 */

/* USER CODE END I2C3_MspInit 1 */

}

}

 I am using STM32F207ZG nulcleo board for development and STM32Cube_FW_F2_V1.6.0 SDK.

Seeking help in resolving this issue...

Thanks.

4 REPLIES 4
T J
Lead
Posted on June 14, 2018 at 01:10

I cannot simulate your issue, but please consider;

How long is the data trace on your board ?  no more than 100mm I hope.

You may need to lower the pullup value, maybe 1k pullups on SDA and SCLK

You may need to lower the clocking rate.

You may have a bug with the addressing of the errant parts.

john doe
Lead
Posted on June 14, 2018 at 01:17

not sure why you'd use master_transmit to scan the bus

here's my i2c bus scan code:

printf('Scanning I2C bus:\r\n');

HAL_StatusTypeDef result;

uint8_t i;

for (i=1; i<128; i++)

{

  /*

   * the HAL wants a left aligned i2c address

   * &hi2c1 is the handle

   * (uint16_t)(i<<1) is the i2c address left aligned

   * retries 2

   * timeout 2

   */

  result = HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t)(i<<1), 2, 2);

  if (result != HAL_OK) // HAL_ERROR or HAL_BUSY or HAL_TIMEOUT

  {

          printf('.'); // No ACK received at that address

  }

  if (result == HAL_OK)

  {

          printf('0x%X ', i); // Received an ACK at that address

  }

}

printf('\r\n');
Posted on June 14, 2018 at 07:07

Previously I tried with 

HAL_I2C_IsDeviceReady(), but same results were coming. In Description I found that 

HAL_I2C_IsDeviceReady() is only for memory devices so I moved for master_transmit.

Posted on June 14, 2018 at 07:23

I am having STM32F2 nucleo board, SDA- SCL lines are having 2.2k pullups....

Clock is at 100kHz but still not getting desired output with STM controller..