/STM32-UART-Protocol-Parser-with-CRC16

Interrupt-driven dual UART parser for STM32 with CRC16/CMS verification using state machine and circular buffer.

Primary LanguageCMIT LicenseMIT

STM32 UART Protocol Parser with CRC16/CMS

An interrupt-driven, dual-UART message parser for STM32 (tested on NUCLEO-G431RB) that:

  • Uses a state machine to validate incoming frames (ID โ†’ LEN โ†’ DATA โ†’ CRC)
  • Buffers data in a lock-free circular buffer for non-blocking reception
  • Verifies message integrity using CRC16/CMS (poly 0x8005, init 0xFFFF, xorOut 0x0000)
  • Sends a fixed 6-byte response on valid CRC

๐Ÿ“น Demo Video

Demo on YouTube


โœจ Features

  • Non-blocking UART RX using HAL HAL_UART_Receive_IT and per-UART circular buffers
  • Deterministic parsing with strict ID โ†” LEN mapping
  • Table-driven CRC16/CMS calculation for efficiency
  • Works with two UART interfaces independently

๐Ÿ“ฆ Repository Structure

. โ”œโ”€ circular_message.c / .h # Circular buffer implementation
โ”œโ”€ message_state.c / .h # State machine parser & response builder
โ”œโ”€ crc16_cms.c / .h # CRC16/CMS calculation
โ”œโ”€ main.c # UART init, interrupt handling, main loop
โ”œโ”€ STM32-UART-Protocol-Parser-with-CRC16.ioc # STM32CubeMX configuration
โ”œโ”€ PROTOCOL.md # On-wire protocol specification
โ”œโ”€ Message Examples.txt # Example request/response frames
โ”œโ”€ LICENSE
โ””โ”€ .gitignore


๐Ÿ”Œ Protocol Overview

Frame format (big-endian CRC): [ ID(1) | LEN(1) | DATA(LEN-4) | CRC_HI(1) | CRC_LO(1) ]

markdown Kopyala Dรผzenle

  • Allowed IDs: 0x55, 0x66, 0x77, 0x88, 0x99
  • LEN includes all fields; DATA length = LEN โˆ’ 4
  • CRC16/CMS is computed over ID..last DATA byte
  • ID โ†” LEN mapping: 0x55 โ†’ 0xFF 0x66 โ†’ 0x7F 0x77 โ†’ 0x06 0x88 โ†’ 0x07 0x99 โ†’ 0x05

yaml Kopyala Dรผzenle

See PROTOCOL.md for full details.


๐Ÿงช Example

From Message Examples.txt:

Request: 77 06 88 BD 9F CC

makefile Kopyala Dรผzenle Response: 33 05 F1 33 E9

yaml Kopyala Dรผzenle (Last two bytes are CRC16/CMS over [ID..OUT3])


๐Ÿ› ๏ธ Build & Run (STM32CubeIDE)

  1. Open STM32-UART-Protocol-Parser-with-CRC16.ioc in STM32CubeIDE
  2. Generate code and ensure Src/ and Inc/ contain the provided .c and .h files
  3. Connect NUCLEO-G431RB via USB
  4. Flash the program to the board
  5. Open HTerm / Serial Monitor @ 115200-8N1 and send example frames in HEX mode

๐Ÿ“œ License

This project is released under the MIT License. See LICENSE.


๐Ÿ™Œ Credits

Developed by Erkin CoลŸkun Ruhi