Skip to main content
SA.17
Associate III
September 21, 2020
Solved

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

  • September 21, 2020
  • 3 replies
  • 1078 views

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

This topic has been closed for replies.
Best answer by TDK

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?

3 replies

TDK
TDKBest answer
September 21, 2020

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
SA.17Author
Associate III
September 22, 2020

in first for loop at value of 72