cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F051K8, I2C and UART Communication Issue

Shirish
Associate II

Hi,

I am trying to receive voltage from an external ADC, MCP3421 using I2C. These values are then transmitted over USART to be viewed by the user ranging from (-2.048 to 2.048). The MCU I am using is STM32F051K8.

The problem that I am facing is that the program runs fine for 4-5 hours but then returns garbage value on the uart. eg. 262.7 or 568.7. If I reset the communication it starts working fine again.

I am using FreeRTOS where one task gets the value from the ADC and stores it in a variable that is the passed over uart to be viewed. And another task waits for user to send a string so that the uart (HAL_UART_Receive_IT(&huart2, (uint8_t *)rxBuf, 2);) can start transmitting.

Code using TrueStudio Atollic.

a snippet of my code:

if(strcmp(rxBuf,"y#")==0){

         sprintf_(txBuf, "%2.4f\n",sendvar);

         HAL_UART_Transmit_IT(&huart2, (uint8_t *) txBuf,8);

         }

Thanking you in advance.

3 REPLIES 3
S.Ma
Principal

Check your time mechanics.

What triggers the I2C transaction? How often?

How long the I2C transaction takes (min-max)

How often the variable passed to the USART is updated? Do you have a flag to tell the variable is "new" and as long as this flag is not cleared by RS232 to tell I2C that it is relinquished for next I2C transaction (no details on how these works using the RTOS)

How long does it take to transmit the string by USART?

When the communication is reset, is USART or I2C or both reset?

Make sure your output buffer is large enough, and not blowing up. Use the length returned by sprintf() to control the length of data sent, and also sanity check that it hasn't overrun the buffer.

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

I wonder how well FreeRTOS works with interrupt-driven commands. If your program timing allows it, you might try switching to using HAL_UART_Transmit or HAL_UART_Transmit_DMA instead.

Might also want to ensure the previous transmission is complete before attempting a new one.

Definitely check the return value from the HAL function to make sure it’s HAL_OK and not an error code.

Where is the “garbage value�? being introduced? Might be that the HAL_UART function is just passing along whatever it’s passed and the error is upstream somewhere.

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