A drop-in solution for STM32F4 + HAL that receives variable-length UART data reliably
Here’s a drop-in solution for STM32F4 + HAL that receives variable-length UART data reliably. I’m giving you two production-proven patterns: Delimiter framing (e.g., \n ) using an ISR ring buffer (simple, great for text/AT commands) DMA circular + IDLE line (best for bursts/binary streams; minimal CPU) Both work on F4 (F401/F405/F407/F411, etc.) with CubeMX/HAL. A) Delimiter framing ( \n ) with ISR ring buffer 1) CubeMX setup Enable USARTx (e.g., USART1 @ 115200 8N1) Enable RXNE interrupt Configure GPIO pins (Tx/Rx AF) NVIC: set a sensible priority (e.g., preempt 5, sub 0 if using FreeRTOS) 2) Ring buffer + IRQ handler // uart_rx_isr_rb.c # include "main.h" # include <string.h> # include <stdarg.h> # define RB_SIZE 512 typedef struct { volatile uint8_t buf[RB_SIZE]; volatile uint16_t head, tail; // modulo RB_SIZE } ringbuf_t ; extern UART_HandleTypeDef huart1; // adjust to your instance static ringbuf_t r...