Summary
This project implements embedded firmware for an electrochemical gas sensor running on an STM32G431KB microcontroller. The firmware is written in Rust using the RTIC 2.1 real-time framework with the Embassy HAL for async peripheral drivers.
The system reads sensor current via a Transimpedance Amplifier (TIA), digitises the signal with a 12-bit differential ADC, applies two-point digital calibration via the AD5545 multiplying DAC, and reports concentration in ppm over a UART JSON interface.
The key design contribution is replacing mechanical calibration potentiometers with a digitally-controlled analogue conditioning stage (AD5545 MDAC), while keeping the calibrated analogue output fully backward-compatible with the downstream host instrument.
Key Specifications
| Parameter | Value |
|---|---|
| MCU | STM32G431KB (UFQFPN-32) |
| Core | ARM Cortex-M4F, 170 MHz |
| Flash / RAM | 128 KB / 32 KB |
| ADC | 12-bit differential, 8× oversampling |
| DAC | AD5545 16-bit dual MDAC (SPI) |
| UART baud rate | 9600, 8N1 |
| Firmware size | ~96 KB flash, ~7 KB RAM |
Design Goals
- Backward compatibility — the calibrated analogue output must remain hardware-compatible with the existing host gas analyser; software-only correction is not viable.
- Accuracy — VREFINT-compensated ADC readings, 8× hardware oversampling, and a simultaneous closed-form calibration solution to eliminate DAC coupling error.
- Reliability — RTIC’s static task model prevents stack overflow and priority inversion; an IWDG watchdog guards against firmware lockup.
- Digital calibration — zero and span points are stored in flash and applied as analogue offsets via the MDAC, replacing mechanical potentiometers.
Signal Chain
Electrochemical sensor (target analyte, current output ~nA–µA)
│
▼
TIA (Transimpedance Amplifier) 0–2 V voltage
│
▼
AD5545 Dual 16-bit MDAC
├── ChA: Gain control (scales sensitivity)
└── ChB: Offset cancel (subtracts sensor baseline in hardware)
│
▼
ADC1 (differential PA0⁺ / PA1⁻, 12-bit, 8× oversampling)
│
▼
STM32G431KB firmware
├── Calibration state machine
├── PPM conversion
└── UART JSON output → host instrument
Workspace Structure
fw/
├── bsp/ Board Support Package — ADC, DAC, SHT45, board init
├── protocol/ JSON command codec (serde_json)
└── main-app/ RTIC application — tasks, calibration logic, stability monitor