Skip to main content
AlexanderKibarov
Associate III
Jul 20, 2024 8:40 AM UTC
Solved

STM32F4 UART NOT WORKING CORRECTLY

In the code I have written here, I am trying to send data from UART4 within the main function and simultaneously receive data through interrupts if data arrives. However, the data transmission part works for a very short time and then stops, and the data received from the interrupt is corrupted. I am providing the code from the main.c and stm32f4xx_it.c files below. Can you help me?

main.c:

 

#include "main.h"
#include "kibar_nextion.h"
#include <string.h>

uint8_t main_buffer[5];
uint8_t main_buffer1[8];

int main(void)
{
__HAL_UART_ENABLE_IT(&huart4, UART_IT_RXNE);

uint8_t last_data[] = {255, 255, 255};

while (1)
{
 for(int i = 0; i < 360; i++) {
 HAL_UART_Transmit(&huart4, (uint8_t*)"main_screen.z0.val=", 19, 100);

 HAL_UART_Transmit(&huart4, (uint8_t*)kibar_return_array_char_from_32_t(i), 
 kibar_size_integer_data_32_t(i), 100);

 HAL_UART_Transmit(&huart4, last_data, 3, 100);
 }
}
}

 

stm32f4xx_it.c:

 

#include "main.h"
#include "stm32f4xx_it.h"

extern UART_HandleTypeDef huart4;
extern uint8_t main_buffer[5];
extern uint8_t main_buffer1[8];

void UART4_IRQHandler(void)
{
 HAL_UART_Receive(&huart4, main_buffer, 5, 100);

 HAL_UART_Receive(&huart4, main_buffer1, 8, 100);

 HAL_UART_IRQHandler(&huart4);
}

 

 

 

This topic has been closed for replies.
AlexanderKibarov
AlexanderKibarovBest answer
Associate III
Jul 22, 2024 6:45 PM UTC

Hello Sir, In a way that I didn't understand, when I put this code:

HAL_UART_Receive_IT(&huart4, main_buffer, 13, 100);

under this code:

HAL_UART_IRQHandler(&huart4);

everything got better. Thank You.

9 replies

Andrew Neil
Super User
Jul 20, 2024 8:50 AM UTC

Please see the posting tips for how to properly post source code; also, other details needed:

https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

 

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.
AlexanderKibarov
Associate III
Jul 20, 2024 9:04 AM UTC

You're right. I fixed it. 

Andrew Neil
Super User
Jul 20, 2024 9:10 AM UTC

And the other things:

  • details of what hardware you're using
  • details of how you're connecting to the UART, providing input, and observing output
  • What testing/debugging/investigation you have done so far
  • etc

Remember, we know nothing about your project other than what you clearly describe in your posts

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.
Andrew Neil
Super User
Jul 20, 2024 9:15 AM UTC

This is not right!

 

void UART4_IRQHandler(void)
{
 HAL_UART_Receive(&huart4, main_buffer, 5, 100);

 HAL_UART_Receive(&huart4, main_buffer1, 8, 100);

 HAL_UART_IRQHandler(&huart4);
}

 

HAL_UART_Transmit is a blocking receive; ie,  it stops and waits for characters to arrive.

You use this when doing non-interrupt receiving - it's not going to work within the ISR!

HAL_UART_Receive_IT is for interrupt-driven receive.

Please take a look at the HAL documentation, and the examples available in Cube ...

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.
Andrew Neil
Super User
Jul 20, 2024 9:18 AM UTC

@Andrew Neil wrote:

HAL_UART_Receive_IT is for interrupt-driven receive.

Please take a look at the HAL documentation, and the examples available in Cube ...


As @TDK said in your other thread:

https://community.st.com/t5/stm32-mcus-embedded-software/doesn-t-work-uart-interrupt/td-p/699762

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.