2024-07-20 01:40 AM - last edited on 2024-07-20 06:59 AM by Tesla DeLorean
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);
}
Solved! Go to Solution.
2024-07-22 11:45 AM
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.
2024-07-20 01:50 AM
Please see the posting tips for how to properly post source code; also, other details needed:
2024-07-20 02:04 AM
You're right. I fixed it.
2024-07-20 02:10 AM
And the other things:
Remember, we know nothing about your project other than what you clearly describe in your posts
2024-07-20 02:15 AM - edited 2024-07-20 02:19 AM
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 ...
2024-07-20 02:18 AM
@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
2024-07-20 04:03 AM
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.
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.
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);
}
I believe I have explained my issue in more detail this time. Thank you for your help if you can assist me.
2024-07-20 06:54 AM
You can't queue in this fashion, the functions return immediately, you can only have one operation "in-flight" at a time.
HAL_UART_Receive_IT(&huart4, main_buffer, 5);
HAL_UART_Receive_IT(&huart4, main_buffer1, 8);
You're expected to manage the queuing, or scatter-gather behaviour
You call HAL_UART_IRQHandler(&huart4); to allow HAL to execute the callbacks, and it's in the callbacks that you manager your buffers and lists, and light-off the next HAL_UART_Receive_IT()
You don't have to embrace the call-back paradigm, but you have to chose one method or another
2024-07-22 01:57 AM - edited 2024-07-22 02:02 AM
@AlexanderKibarov wrote:I did as you suggested.
No, that's not what I suggested.
Did you look at the HAL documentation?
Did you take a look at the examples?
https://wiki.st.com/stm32mcu/wiki/Getting_started_with_UART
https://www.youtube.com/@stmicroelectronics/search?query=stm32f4%20uart%20interrupt
2024-07-22 11:45 AM
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.