cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 UART NOT WORKING CORRECTLY

AlexanderKibarov
Associate

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);

}

 

 

6 REPLIES 6
Andrew Neil
Evangelist III

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

 

You're right. I fixed it. 

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

Andrew Neil
Evangelist III

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


@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

Hello, thank you for your response. I did as you suggested. The code is as follows:

 

void UART4_IRQHandler(void)
{
  /* USER CODE BEGIN UART4_IRQn 0 */
	HAL_UART_Receive_IT(&huart4, main_buffer, 5);
	HAL_UART_Receive_IT(&huart4, main_buffer1, 8);
  /* USER CODE END UART4_IRQn 0 */
  HAL_UART_IRQHandler(&huart4);
  /* USER CODE BEGIN UART4_IRQn 1 */

  /* USER CODE END UART4_IRQn 1 */
}

 

Yes, the interrupt works, but there are some problems. Firstly, I am using the UART connection on the STM32F413ZH board and trying to send and receive data from a Nextion screen. There is no problem when I only send data, and there is no problem when I only receive data. However, when both are done simultaneously, problems start to occur. From the Nextion screen, the first 5 bytes received are a name, followed by an 8-byte number. When I implemented your suggestion, the data transmission part improved, but as shown in the attached image, the 5-byte data is not received correctly, and the 8-byte data is not received at all.

Ekran görüntüsü 2024-07-20 133955.png

Another solution I can think of is to directly read either 13 bytes at once or read one byte at a time and save it into a 13-byte buffer. However, when I do this, I encounter the following result.

Ekran görüntüsü 2024-07-20 134911.png

As you can see, the received data is still corrupted. However, if I turn off data transmission, then the first received data is correct, but data reception will stop. Here's the code where I have disabled data transmission and the image of the received data:

Main.c:

 

#include "main.h"


#include "kibar_nextion.h"

#include <string.h>


uint8_t main_buffer[13];


int main(void)

{
__HAL_UART_ENABLE_IT(&huart4, UART_IT_RXNE);

while (1)

{

}

}

 

 

stm32f4xx_it.c:

 

#include "main.h"

#include "stm32f4xx_it.h"


extern UART_HandleTypeDef huart4;

extern uint8_t main_buffer[13];


void UART4_IRQHandler(void)

{

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

HAL_UART_IRQHandler(&huart4);

}

 

 

Ekran görüntüsü 2024-07-20 135840.png

 

I believe I have explained my issue in more detail this time. Thank you for your help if you can assist me.