cancel
Showing results for 
Search instead for 
Did you mean: 

program stucks when scanning 2 i2c (hi2c2 and hi2c3) using for loop

SA.17
Associate III

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

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

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?

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

3 REPLIES 3
TDK
Guru

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?

If you feel a post has answered your question, please click "Accept as Solution".
SA.17
Associate III

in first for loop at value of 72

SA.17
Associate III

i initialized pointer ,now its working :)