How can I filter received data from UART to act when it reads one, or multiple specific characters?
..
..
Hello @Birdym
No big added value compared to already suggested method. Just to mention another option could be to use the Character Match feature of your UART instance. Goal is to raise a specific flag in ISR (the CMF flag), when received character (8 bits) value corresponds to the value stored in ADD field of the CR2 register. Could work for example in case you wait for a specific character as a "start" sequence for something to do further.
No specific HAL API exists for this feature, but using LL services, and interrupt mode, this could be something like :
main() :
/* UART instance initialisation as usual : baudrate, parity ...*/
...
/* Disable USART */
LL_USART_Disable(USARTx_INSTANCE);
/* Set value of expected character for matching */
LL_USART_ConfigNodeAddress(USARTx_INSTANCE , LL_USART_ADDRESS_DETECT_7B, 0x31);
/* Enable USART */
LL_USART_Enable(USARTx_INSTANCE);
/* Enable RXNE and Error interrupts */
LL_USART_EnableIT_RXNE(USARTx_INSTANCE);
/* Enable the UART Character Match interrupt */
LL_USART_EnableIT_CM(USARTx_INSTANCE);
LL_USART_EnableIT_ERROR(USARTx_INSTANCE);USART IRQ Handler :
/* Check RXNE flag value in ISR register */
if(LL_USART_IsActiveFlag_RXNE(USARTx_INSTANCE))
{
/* Read Received character. RXNE flag is cleared by reading of RDR register */
received_char = LL_USART_ReceiveData8(USARTx_INSTANCE);
}
/* Check CMF flag value in ISR register */
if(LL_USART_IsActiveFlag_CM(USARTx_INSTANCE))
{
/* Clear CMF flag */
LL_USART_ClearFlag_CM(USARTx_INSTANCE);
<<<==== Specific treatment in case of expected specific char value could be added here
}Please note that even if received character does not match the expected specific value, it will be stored in RDR and RXNE flag will raise. So it must ne read (and stored if to be used later) in order to prevent Overrun errors.
Hope this helps.
Guenael
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.