cancel
Showing results for 
Search instead for 
Did you mean: 

I2C communication issue when called from UART Command line

GalBTM
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();

CODE: (COM_PORT)

 

 

void executeSerialCommand1(char *uart1RxBuf)
{
Read_IOExpendr2_Status();
}

 

 

CODE: (MAIN)

 

 

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))
{
Error_Handler();
}
}
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)

 

 

MCU: STM32F072CBU6

 

any suggestions?

 

thank you

 

 

 

 

6 REPLIES 6

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:
 
AndrewNeil_1-1717418821674.png

 

 

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.

https://en.wikipedia.org/wiki/Automatic_variable#:~:text=C%2C%20C%2B%2B&text=(Called%20automatic%20variables.),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.

 

GalBTM
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:

https://community.st.com/t5/stm32-mcus-products/i2c-issue-when-called-from-external-file/td-p/689715