The system employs a Hybrid Event-Driven Architecture, strictly separating Hard Real-Time acquisition (handled by hardware interrupts) from Soft Real-Time signal processing (handled by FreeRTOS tasks).
The signal path is designed to minimize CPU intervention using DMA and Hardware Triggers.
-
Acquisition Stage (Hard Real-Time)
- Source:
TIM2(1kHz Hardware Metronome). - Trigger:
TIM2_TRGOevent fires on Update (1ms). - Conversion:
ADC1converts analog signal (PA0) to 12-bit int. - Transport:
ADC_IRQHandlercaptures data immediately upon End of Conversion (EOC). - Output: Raw data pushed to
ecgQueue(FreeRTOS Queue).
- Source:
-
Processing Stage (Soft Real-Time)
- Task:
Algorithm_Task(Priority: High). - Input: Blocks on
ecgQueuereceive. - Filtering: Recursive FIR (Moving Average) to reject 60Hz noise.
- Analysis: Pan-Tompkins Algorithm (Squaring -> Integration -> Adaptive Thresholding).
- Output: Updates global
HeartRatestate andFilterOutdisplay buffer.
- Task:
-
Visualization Stage (Background)
- Task:
GUI_Task(Priority: Normal). - Mechanism: Zero-Copy Bare-Metal DMA.
- Synchronization: Acquires
lcdSpiSemaphorebefore drawing. - Action: Configures
DMA2_Stream3to blast pixel data to ILI9341 via SPI1.
- Task:
Safety-critical systems require robust protection against Race Conditions.
The ILI9341 display shares the SPI bus. To prevent the Algorithm task or other peripherals from interrupting a massive DMA transfer, we implement a Binary Semaphore.
- Take:
GUI_Tasktakes the semaphore before starting a frame. - Give:
DMA2_Stream3_IRQHandlergives the semaphore back only when the Transfer Complete (TC) flag is set AND the SPI Busy (BSY) flag is cleared.
A software watchdog monitors the time delta between detected R-peaks.
- Condition:
(CurrentTime - LastBeatTime) > 3000ms. - Action: Force
BPM = 0. - Rationale: Prevents "Stale Data" hazards where the screen displays a healthy heart rate despite patient cardiac arrest or lead detachment.
To ensure acquisition fidelity, priorities are tiered:
| IRQ | Priority | Rationale |
|---|---|---|
| TIM2_IRQn | 0 (Highest) | Metronome cannot jitter. |
| ADC_IRQn | 1 | Data capture must happen immediately. |
| DMA2_Stream3 | 5 | Graphics completion is less critical than acquisition. |
| SysTick | 15 (Lowest) | OS Scheduler yields to hardware. |
- ADC Buffer:
uint16_t(Single sample depth, ISR-managed). - Filter Buffer:
int16_t[FILTER_SIZE](Circular buffer for FIR). - Display Buffer:
uint16_t[320](One scanline, DMA-accessible SRAM).