Showing results for 
Search instead for 
Did you mean: 

I2C communication issue when called from UART Command line

Associate II

Hi There,


we have an IO Expender connected to I2C2, We read the GPIO using function "Read_IOExpendr2_Status();"

also, we have a UART connection for CLI

the function "Read_IOExpendr2_Status();" is at "Application.c" file

the CLI is at "COM_PORT.c" File


when the costumer send any UART message, a function is activated "void executeSerialCommand1(char *uart1RxBuf)"

inside that function there is a call for Read_IOExpendr2_Status();




void executeSerialCommand1(char *uart1RxBuf)






void Read_IOExpendr2_Status(void) 
uint8_t pRegAddress[2] = {INPUT_PORT0,INPUT_PORT1};
pcal9555_readRegister(&hi2c2, (uint16_t)PCAL9555_I2C_ADD_2, pRegAddress,USB_Link_Status );





Declaration of: pcal9555_readRegister



HAL_StatusTypeDef pcal9555_readRegister(I2C_HandleTypeDef *hi2c, uint16_t address, uint8_t *pRegAddress, uint8_t *pData);



Definition of: pcal9555_readRegister



HAL_StatusTypeDef pcal9555_readRegister(I2C_HandleTypeDef *hi2c, uint16_t address, uint8_t *pRegAddress, uint8_t *pData){

//Set the register address in the pointer register
HAL_Status = pcal9555_writeRegister(hi2c, address, pRegAddress, 1);

HAL_Status= HAL_I2C_Master_Receive_IT(hi2c, address, pData, 1);

while (HAL_I2C_GetState(hi2c) != HAL_I2C_STATE_READY){

/* Error_Handler() function is called when Timeout error occurs.
When Acknowledge failure occurs (Slave don't acknowledge its address)
Master restarts communication */
if ((HAL_I2C_GetError(hi2c) != HAL_I2C_ERROR_AF)&&(HAL_I2C_GetError(hi2c) != HAL_I2C_ERROR_NONE))
return HAL_Status;




the result is that the MCU send the IO Expender's address and then SDA and SCL lines set to '1' constant

from debugging, i can see that there is an issue with "pData"


HAL_Status = HAL_I2C_Master_Transmit_IT(hi2c,address, pData, Size);



when the function "Read_IOExpendr2_Status();" is called Outside of "void executeSerialCommand1(char *uart1RxBuf)" the value of pData is 0x200008F8

when the function "Read_IOExpendr2_Status();" is called Inside of "void executeSerialCommand1(char *uart1RxBuf)" the value of pData is 0x20000668


the Program enters a loop 


while (HAL_I2C_GetState(hi2c) != HAL_I2C_STATE_READY)





any suggestions?


thank you






The two functions have different static buffers.

Not sure this can be solved via the description, attach more salient portions of the source.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

thank you, i have updated the post with relative code

Please use this button to properly post source code:



thank you, i didn't find it at first

You need to re-do the indentation!


void Read_IOExpendr2_Status(void) //USB/ETH LINK[1-5] on IOE IOs P.0[0-4]
   uint8_t pRegAddress[2] = {INPUT_PORT0,INPUT_PORT1};
   pcal9555_readRegister( &hi2c2, (uint16_t)PCAL9555_I2C_ADD_2, pRegAddress,USB_Link_Status );


So   pRegAddress  is a local, automatic (or "auto") variable.,valid%20value%20of%20its%20type.

This means that a new instance of that variable is created on the stack each time the function is called.

Therefore the address would be expected to change.


Associate II

hi there,

i have replaced the pRegAddress  to global variable

same results


i have uploaded a more detailed topic with more code and more tests at this link: