Skip to main content
Graduate
July 20, 2024
Solved

STM32F4 UART NOT WORKING CORRECTLY

  • July 20, 2024
  • 2 replies
  • 2572 views

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.
    Best answer by AlexanderKibarov

    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.

    2 replies

    Super User
    July 20, 2024

    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

     

    Graduate
    July 20, 2024

    You're right. I fixed it. 

    Super User
    July 20, 2024

    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

    Super User
    July 20, 2024

    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 ...

    Super User
    July 20, 2024

    @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