Design an interrupt-driven UART protocol for ARM Cortex-M with a packet protocol using start byte 0xAA, length, payload, and CRC8 checksum. The ISR should build packets byte by byte and only set a flag for the main loop when a valid checksum-verified packet is complete.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Designing interrupt-driven UART with packet parsing
The UART ISR uses a state machine tracking start byte, length, payload, and CRC8 (poly 0x07) to build packets byte-by-byte, computing CRC over length and payload only. On a valid CRC match, it sets a packet-ready flag for the main loop to process without blocking. The start byte (0xAA) triggers reception, and the ISR resets on errors, keeping minimal processing inside the interrupt.
A simple way to frame it is as a byte‑oriented state machine that runs in the receive interrupt and advances through the fields of your framing:
Wait for the fixed start byte 0xAA.
Take the next byte as a length N.
Collect N payload bytes.
Take the next byte as an 8‑bit checksum.
The interrupt routine keeps a small amount of state in static/volatile variables, updates a running CRC as bytes arrive, and, when the expected number of bytes have been seen and the CRC matches, flips a flag for the foreground code. All of the heavier processing happens in the main loop.
One possible implementation in C for a Cortex‑M might look like this:
c
#include <stdint.h>
#include <stdbool.h>
#define START_BYTE 0xAA
#define MAX_PAYLOAD 64
typedef enum {
RX_WAIT_START,
RX_WAIT_LEN,
RX_WAIT_PAYLOAD,
RX_WAIT_CRC,
} rx_state_t;
static volatile rx_state_t rx_state = RX_WAIT_START;
static volatile uint8_t rx_len = 0;
static volatile uint8_t rx_count = 0;
static volatile uint8_t rx_crc = 0;
static uint8_t rx_buf[MAX_PAYLOAD];
static volatile bool packet_ready = false;
/* Simple CRC-8 with polynomial 0x07, initial value 0x00 */
static inline uint8_t crc8_update(uint8_t crc, uint8_t data)
{
crc ^= data;
for (uint8_t i = 0; i < 8; i++) {
if (crc & 0x80)
crc = (crc << 1) ^ 0x07;
else
crc <<= 1;
}
return crc;
}
void USARTx_IRQHandler(void)
{
if (USARTx->ISR & USART_ISR_RXNE) {
uint8_t b = USARTx->RDR; // read received byte, clears RXNE
switch (rx_state) {
case RX_WAIT_START:
if (b == START_BYTE) {
rx_state = RX_WAIT_LEN;
rx_crc = 0; // reset CRC for new packet
}
break;
case RX_WAIT_LEN:
if (b > MAX_PAYLOAD) { // reject oversize payloads
rx_state = RX_WAIT_START;
} else {
rx_len = b;
rx_count = 0;
rx_crc = crc8_update(rx_crc, b);
rx_state = (rx_len == 0) ? RX_WAIT_CRC : RX_WAIT_PAYLOAD;
}
break;
case RX_WAIT_PAYLOAD:
rx_buf[rx_count++] = b;
rx_crc = crc8_update(rx_crc, b);
if (rx_count >= rx_len) {
rx_state = RX_WAIT_CRC;
}
break;
case RX_WAIT_CRC:
if (rx_crc == b) {
// checksum OK
packet_ready = true;
// you may also want to copy rx_len somewhere for the main loop
}
// in either case, reset for next packet
rx_state = RX_WAIT_START;
break;
}
}
// handle other USART interrupts as needed…
}
And in your main loop you poll packet_ready and, when true, clear it and process rx_buf[0..rx_len-1]:
c
int main(void)
{
uart_init_with_rxne_interrupt();
for (;;) {
if (packet_ready) {
__disable_irq();
packet_ready = false;
uint8_t len = rx_len;
// optionally copy payload to a safer buffer here
__enable_irq();
process_packet(rx_buf, len);
}
// other background tasks…
}
}
A few points worth noting:
The ISR does the minimum: it only parses and verifies the frame, and signals completion via a flag. It does not call any lengthy processing routines.
The CRC is computed over the length and payload bytes; the start byte is used only for framing.
Any error (unexpected start byte, length too large, bad CRC) drops back to the RX_WAIT_START state to resynchronize.
Use volatile on state variables shared between ISR and main. Protect any multi‑byte accesses in the main loop if you copy data out of the buffer.
Depending on your environment you can adapt this to use the vendor HAL callback (HAL_UART_RxCpltCallback) instead of the raw IRQ handler, but the underlying state machine approach is the same.