Summary
This project implements embedded firmware for an H₂S (hydrogen sulfide) 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 linear calibration, and reports concentration in ppm over a UART ASCII interface.
Key Specifications
| Parameter | Value |
|---|---|
| MCU | STM32G431KB (UFQFPN-32) |
| Core | ARM Cortex-M4F, 170 MHz |
| Flash / RAM | 128 KB / 32 KB |
| Target gas | H₂S (Hydrogen Sulfide) |
| Measurement range | 0 – 25 ppm |
| ADC resolution | 12-bit differential, 8× oversampling |
| UART baud rate | 115200, 8N1 |
| Firmware size | ~96 KB flash, ~7 KB RAM |
Design Goals
- Accuracy — VREFINT-compensated ADC readings, hardware oversampling to reduce noise floor, two-point calibration to account for sensor-to-sensor variation.
- Reliability — RTIC’s static task model prevents stack overflow and priority inversion; Embassy async drivers avoid blocking the executor.
- Simplicity — ASCII UART protocol with human-readable commands; external controller drives the calibration procedure.
- Size efficiency —
opt-level = "z"+ LTO; no serde, no telemetry watch channel; 512-byte heap only for format strings.
Signal Chain
H₂S Sensor (electrochemical, current output ~nA–µA)
│
▼
TIA (Transimpedance Amplifier) → 0–2 V voltage
│
▼
AD5545 DAC
├── ChA: Gain control (scales TIA feedback, default 100%)
└── ChB: Offset cancellation (subtracts sensor baseline)
│
▼
ADC1 (differential PA0⁺ / PA1⁻, 12-bit, 8× hardware oversampling)
│
▼
Firmware (CalibrationState → ppm conversion)
│
▼
UART → Host (PC / MCU)
Workspace Structure
gas-sensor-fw/
├── bsp/ Board Support Package — ADC, DAC, SHT45, board init
├── protocol/ ASCII command definitions (Command parser)
├── main-app/ RTIC application — 5 tasks, calibration logic, stability monitor
└── _legacy_src/ Reference — original monolithic firmware