cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_I2C_Mem_Write Stuck

uveuvenouve
Associate III

Hi everyone,

 

i am working on a STM32C011F6P6

i am trying to apply some I2C setting to other chip

normal writing value to corresponding address
but seems the HAL_I2C_Mem_Write function is stucked

these are the read and write function

HAL_StatusTypeDef i2c_write_reg(uint8_t reg, uint8_t send_data ) {
    	return HAL_I2C_Mem_Write(&hi2c1, SLAVE_ADDRESS<<1 , reg, I2C_MEMADD_SIZE_8BIT ,&send_data, 1 , HAL_MAX_DELAY);

}

uint8_t i2c_read_reg(uint8_t reg) {
    uint8_t temp;

    if (HAL_I2C_Mem_Read(&hi2c1, SLAVE_ADDRESS << 1, reg, I2C_MEMADD_SIZE_8BIT, &temp, 1, HAL_MAX_DELAY) != HAL_OK) {
        // Handle error here
        return 0; // Or any default value you prefer
    }

    return temp; // Success, return the read value
}

 these are the code execute

HAL_StatusTypeDef stat2;
	stat2 = i2c_write_reg(0x25,0x70);
	if(stat2!=HAL_OK){
		printf("HAL not ok\r\n");
	}
	stat2 =i2c_write_reg(0x26,0x07);
	if(stat2!=HAL_OK){
		printf("HAL not ok\r\n");
	}

	uint8_t x ;

	x = i2c_read_reg(0x25) ;
	printf("0x250x%02x\r\n",x);
	x = i2c_read_reg(0x26) ;
	printf("0x26:0x%02x\r\n",x);



i already have pull up resistor on board

any solution or any thing i can do to identify the real issue?

6 REPLIES 6
RhSilicon
Lead

any solution or any thing i can do to identify the real issue?

I don't know if it can help in your case, but I found these videos about advanced debugging:

STM32CubeIDE Advanced Debug Features
https://youtube.com/playlist?list=PLnMKNibPkDnEDEsV7IBXNvg7oNn3MfRd6

TDK
Guru

Always use HAL_I2C_IsDeviceReady first to verify the device is responding before using other functions.

My guess is the bus is stuck to due debugging/previous activity on bus. In which case you need to toggle SCL 9 times at startup before initializing it as I2C. Or power cycle everything.

If HAL_I2C_IsDeviceReady does not pass for this chip, then problem could be somewhere else.

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

just found is my slave device have not powered at the first place

now it can return hal_err

thinking why it is not working

Foued_KH
ST Employee

Hello @uveuvenouve , 

I suggest you use an oscilloscope to check SDA and SCL behavior or try to debug and set breakpoints .

Foued

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

If your slave device is not powered, it's not likely to respond to commands. It will also generally pull the SDA and SCL lines down and prevent you from using them effectively.

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

just found i may have messed up the slave id setting
i thought setting slave id in i2c mean the target slave device

but seems that is stating own slave id
and i have 2 devices connected to the target slave device
so i think they some how conflicted by falsely setting all 3 devices have same slave id