Hello everyone.
In this article, we will try to receive LL UART communication using interrupts.
What is LL? If you are wondering what LL is, please refer to the article HAL and LL.
The following is the development environment at the time of posting.
PC: Windows 10 OS
IDE: STM32CubeIDE Version1.5.0
Configurator: STM32CubeMX Version6.1.0
Board: STM32Nucleo-F401RE
Receiving with interrupts
Sending
Polling
Interrupt
DMA
However, for UART reception, we chose to use interrupt for the following reasons.
Receiving by polling is not practical because it is easy to miss and it affects other processes.
Receiving by DMA is not practical because of the complexity of error handling.
Project to be used
Use the project created in UART with STM32 LL Send (DMA).
We will add the receiving process to this project, and use DMA for sending.
Communication specifications
Send 5 bytes from the PC
Receiving process
When 5 bytes are received without error, the following is returned.
Data received: CR LF
Coding
main.c
Add one line LL_USART_EnableIT_RXNE() at the end of MX_USART2_UART_Init() function.
EnableIT means enable interrupt.
RXNE means Rx Not Empty, which means the receive is not empty.
When data is received, an interrupt will be triggered.
// Enable transmit complete interrupt
LL_DMA_EnableIT_TC(DMA1, LL_DMA_STREAM_6);
LL_USART_EnableIT_RXNE(USART2); // Add this one line
Around the front of the main() function
/* USER CODE BEGIN 0 */
#define _CR 0x0d
#define _LF 0x0a
uint8_t buffer[] = "Data received.\r\n";
int recvComplete;
void DMA_TransmitData();
void DMA_TransmitData()
{
LL_DMA_DisableStream(DMA1, LL_DMA_STREAM_6);
LL_DMA_SetDataLength(DMA1, LL_DMA_STREAM_6, sizeof(buffer));
LL_DMA_EnableStream(DMA1, LL_DMA_STREAM_6);
LL_USART_EnableDMAReq_TX(USART2);
}
/* USER CODE END 0 */
In the while() loop
while (1)
{
if (recvComplete)
{
recvComplete = 0;
DMA_TransmitData();
}
}
stm32f4xx_it.c
This file contains the interrupt handler.
Write USART2_IRQHandler() and the following code before it.
extern int recvComplete;
void USART2_IRQHandler(void)
{
static uint8_t buff[128];
static uint8_t counter = 0;
/* USER CODE BEGIN USART2_IRQn 0 */
if (LL_USART_IsActiveFlag_ORE(USART2)) // Over run error
{
LL_USART_ReceiveData9(USART2); // read away
counter = 0;
}
else if (LL_USART_IsActiveFlag_PE(USART2)) // Parity error
{
LL_USART_ReceiveData9(USART2); // read and discard
counter = 0;
}
} else if (LL_USART_IsActiveFlag_FE(USART2)) // Framing error
{
LL_USART_ReceiveData9(USART2); // read away
counter = 0;
}
else if (LL_USART_IsActiveFlag_RXNE(USART2))
{
buff[counter++] = (uint8_t)LL_USART_ReceiveData9(USART2);
if (4 < counter)
{
counter = 0;
recvComplete = 1;
}
// receive processing
}
/* USER CODE END USART2_IRQn 0 */
/* USER CODE BEGIN USART2_IRQn 1 */ /* USER CODE END USART2_IRQn 1
/* USER CODE END USART2_IRQn 1 */ /* USER CODE
/* USART2_IRQn 1 */ /* USER CODE}
Program outline
USART2_IRQHandler() is an interrupt handler for USART2.
If it detects an overrun, parity or framing error, it will read and discard the data register.
Otherwise, if data is received, it increments counter.
When 5 bytes are received, set recvComplete to 1.
When recvComplete is set to 1 in the while() loop, reply using DMA.
The following is a summary of each error.
Overrun error.
An error in which the next data comes in before the data taken into the receive buffer is read out.
Parity error.
Parity mismatch.
An error that occurs when the parity is an odd number even though it is an even number, or when, for example, noise causes the data bit to flip one bit.
Framing error.
An error in which the stop bit is not in place.
This can be caused by formatting differences.
Teram Term Setup
The image is omitted because it has been shown many times.
Set up in the following way.
COM port : Select Serial in New Connection and select the port of STLink Virtual COM.
From Settings - Serial Port
Speed : 115200
Data : 7bit
Parity : even
Stop bit : 1bit
Flow control : none
Check the Local Echo checkbox in the terminal settings.
Build and run it
Let's build and run the program.
Try to send 5 bytes by pressing a key with Tera Term from your PC.
If you change the parity from even to odd, it will detect the parity error and reject it.
How did you like it?
Did it work well?
Thank you for your time.





