2020-09-21 12:23 AM
I am using STM32L072RB MCU.
I am trying to scan the devices connected to i2c bus (i2c2 and i2c3)
following is my function
#define DEBUG 1
void i2c_scan(void)
{
uint8_t *p_data;
uint16_t addr_scan = 3 ;
printf("I2C3 Address : ");
//scanning i2c devices from address 3 to address 127 in i2c3 of MCU
for(addr_scan=3; addr_scan <=0x7F ; addr_scan++)
{
#if DEBUG
printf("\r\naddr_scan : %d",addr_scan);
#endif
if(HAL_I2C_Master_Receive(&hi2c3,(uint16_t)(addr_scan << 1),p_data,1,500) != HAL_OK)
{
continue ;
}
printf(" 0x%x",(addr_scan << 1));
}
printf("\r\nI2C2 Address : ");
//scanning i2c devices from address 3 to address 127 in i2c2 of MCU
for(addr_scan=3; addr_scan <=0x7F ; addr_scan++)
{
#if DEBUG
printf("\r\naddr_scan : %d",addr_scan);
#endif
if(HAL_I2C_Master_Receive(&hi2c2,(addr_scan << 1),p_data,1,500) != HAL_OK)
{
continue ;
}
printf(" 0x%x ",(addr_scan << 1));
}
}
Issue :
If i use any one of the for loops (scanning only one i2c bus and commenting other for loop),the code works.
but if i run full i2c_scan() function i.e. not commenting any of for loops
code stucks
when i added print statement ,i found first loop stucks at count of 72 ,Even i tried debugging the code ,for loop doesn't increment at first increment itself.
Why is this happening ? and how to resolve it
Solved! Go to Solution.
2020-09-21 06:50 AM
You're storing data at the address of an uninitialized pointer. May or may not be the issue. Where does the code actually get stuck?
2020-09-21 06:50 AM
You're storing data at the address of an uninitialized pointer. May or may not be the issue. Where does the code actually get stuck?
2020-09-21 09:12 PM
in first for loop at value of 72
2020-09-21 09:24 PM
i initialized pointer ,now its working :)