cancel
Showing results for 
Search instead for 
Did you mean: 

Terminate an incomming UART packet by recognising a terminating ASCII character (using UART interrupts)

Greg Horler
Associate III
Posted on June 27, 2018 at 19:11

Hi, I'm new to this community and I hope you can help with my first question.

After a lot of effort in trying to get arbtrary length packet reception over UART working on an STM32F3 processor i've changed tack and I'm trying to get the UART to terminate when a specific ASCII character code is recognised. I have the bare bones of a solution, but the last piece in the jigsaw illudes me. On receiving the character I need to fool the processor into thinking that the RxBUFFer is full, in whuch case it will trigger the HAL-RxCpltCallBack function triggered by UART_Receive_IT via the HAL_UART_IRQHandler.

Can anyone advise me, by example how to achieve this please. Code snippets for the ISR and RxCpltCallBack are copied below.

Thanks in advance.

void USART2_IRQHandler(void)

{

++IntCount;

/* USER CODE END USART2_IRQn 0 */

HAL_UART_IRQHandler(&huart2);

/* USER CODE BEGIN USART2_IRQn 1 */

//test if the latest byte received in RxBuf == the terminating character code

if(RxBuf[IntCount-1] == 0x7e){

// If terminating, need to fool system into thinking that RxBuff is full

// thus forcing the system to reset and fill up the RxBuf from RxBuf[0]?

// Also need to know how many bytes in RxBuf are valid,

// Currently using IntCount but isn't this already done lower down?

__nop();

}

/* USER CODE END USART2_IRQn 1 */

}

/* USER CODE BEGIN 4 */

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

HAL_UART_Receive_IT(&huart2, RxBuf, RxBufLen);

IntCount = 0;

__nop();

}

/* USER CODE END 4 */
GDH
2 REPLIES 2
AvaTar
Lead
Posted on June 28, 2018 at 07:14

I can't recall any STM32 MCU which has an UART FIFO. In other words, the depth is 1, just the RX register. ST uses to prefer DMA.

'Packets' are solely the interpretation of your code. Just pick up each character from the interrupt code.

For robustness, I would check for the start character (if there is any), too.

I don't use Cube/HAL. What are this '_nop()' calls supposed for ?

Posted on June 28, 2018 at 08:20

The L4+ and H7 have FIFO on the USART

The mechanics of the USART and IRQ into a buffer can be done easily by discarding the HAL/Cube UART_Receive_IT() paradigm.

https://community.st.com/0D50X00009XkVy3SAF

 
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..