The next example receive a string of character using interrupts, in this case we define a message as any string of characters ending with a ‘\r'
character, we configure the interrupt reception to be triggered by one character using HAL_UART_Receive_IT
function, then in the callback each byte received is placed in a buffer until the '\r'
, after that we set the flag indicating a message has arrived.
#include "bsp.h"
#include <string.h>
uint8_t RxBuffer[ 10 ]; /*buffer for message received*/
uint8_t RxByte; /*temporary reception buffer*/
__IO ITStatus stat = RESET; /*flag to indicate a message has been received*/
UART_HandleTypeDef UartHandle; /*uart handler*/
int main( void )
{
GPIO_InitTypeDef GPIO_InitStruct; /*gpio init structure*/
HAL_Init(); /*init cube library*/
__HAL_RCC_GPIOC_CLK_ENABLE(); /*enable clock on port C*/
/*config pins 0, 1 y 2 from port C as output push-pull*/
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
/*set configuration*/
HAL_GPIO_Init( GPIOC, &GPIO_InitStruct );
/*uart configuration options for module USART2, 9600 baudrate,
8bits, 1 stop bit, no parity, no flow control, and 8 bit lenght */
UartHandle.Instance = USART2;
UartHandle.Init.BaudRate = 9600;
UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
UartHandle.Init.StopBits = UART_STOPBITS_1;
UartHandle.Init.Parity = UART_PARITY_NONE;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.Mode = UART_MODE_TX_RX;
UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
/*init uart2 with previous paramters*/
HAL_UART_Init( &UartHandle );
/*Set reception through interrupt of one character*/
HAL_UART_Receive_IT( &UartHandle, &RxByte, 1 );
while(1)
{
if( stat == SET ) /*a message has been recieved*/
{
stat = RESET; /*clear reception flag*/
if( memcmp("one\r", RxBuffer, sizeof("one\r")-1) == 0 )
{
/* toggle C0 if it was "one" string */
HAL_GPIO_TogglePin( GPIOC, GPIO_PIN_0 );
}
else if( memcmp("two\r", RxBuffer, sizeof("two\r")-1) == 0 )
{
/* toggle C1 if it was "two" string */
HAL_GPIO_TogglePin( GPIOC, GPIO_PIN_1 );
}
else if( memcmp("three\r", RxBuffer, sizeof("three\r")-1) == 0 )
{
/* toggle C2 if it was "three" string */
HAL_GPIO_TogglePin( GPIOC, GPIO_PIN_2 );
}
}
}
}
/*This function is called everytime a new byte is received, the code in it stores the bytes
in the buffer RxBuffer and detects when the '\r' signaling the end of a string*/
void HAL_UART_RxCpltCallback( UART_HandleTypeDef *huart )
{
static uint32_t i = 0;
RxBuffer[i] = RxByte; /*store data*/
i++; /*increase index pointer*/
/*look for the end of the string*/
if( RxBuffer[i-1] == '\r' )
{
stat = SET; /*set flag to indicate reception complete*/
RxBuffer[i] = '\0'; /*set end of the string*/
i = 0; /*restart buffer index*/
}
/*Set reception through interrupt of one character again*/
HAL_UART_Receive_IT( &UartHandle, &RxByte, 1 );
}
msps.c
/*The function is called from HAL_UART_Init at the very beginning
and allows us to set the pins to be used as serial port*/
void HAL_UART_MspInit( UART_HandleTypeDef *huart )
{
GPIO_InitTypeDef GPIO_InitStruct; /*gpios init structure*/
__HAL_RCC_USART2_CLK_ENABLE(); /*enable usart2 clock*/
__HAL_RCC_GPIOA_CLK_ENABLE(); /*eneable port A clock */
/*set pin 2(tx) and pin 3(rx) from port A in altern mode usart2
the altern mode info can be found in device datasheet*/
GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_USART2;
/*apply configuration configuracion*/
HAL_GPIO_Init( GPIOA, &GPIO_InitStruct );
/*enable UART vector interrupt with priority of 1*/
HAL_NVIC_SetPriority( USART2_LPUART2_IRQn, 1, 0 );
HAL_NVIC_EnableIRQ( USART2_LPUART2_IRQn );
}
ints.c
extern UART_HandleTypeDef UartHandle;
/*Declare interrupt service rutine as it is declare in startup_stm32g0b1xx.s file,
the program will jump here every time a byte has been transmitted*/
void USART2_LPUART2_IRQHandler( void )/*vector de interrupcion usart2*/
{
/*HAL library fucntion that attend interrupts on UART module*/
HAL_UART_IRQHandler( &UartHandle );
}