2024-06-03 04:27 AM - edited 2024-06-03 05:57 AM
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
2024-06-03 04:38 AM
The two functions have different static buffers.
Not sure this can be solved via the description, attach more salient portions of the source.
2024-06-03 05:39 AM
thank you, i have updated the post with relative code
2024-06-03 05:47 AM - edited 2024-06-03 05:47 AM
2024-06-03 05:56 AM
thank you, i didn't find it at first
2024-06-03 06:06 AM - edited 2024-06-03 06:28 AM
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.
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.
2024-06-26 06:24 AM
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