Skip to main content
Associate II
June 3, 2024
Question

I2C communication issue when called from UART Command line

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

 

 

 

 

    This topic has been closed for replies.

    6 replies

    Tesla DeLorean
    Guru
    June 3, 2024

    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    GalBTMAuthor
    Associate II
    June 3, 2024

    thank you, i have updated the post with relative code

    Andrew Neil
    Super User
    June 3, 2024
    Please use this button to properly post source code:
     
    AndrewNeil_1-1717418821674.png

     

     

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    GalBTMAuthor
    Associate II
    June 26, 2024

    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