Open Source Firmware Engineering Certification (OSFEC.017) • Super-Fundamentals
A microcontroller sampling a sensor continuously must preserve both the timing and integrity of its measurements. Direct memory access (DMA) can transfer ADC results into RAM while the processor handles other work. However, a stream can silently lose samples when firmware cannot process buffers before the hardware reuses them.
Key objectives
- Explain timer-triggered ADC sampling and circular DMA.
- Calculate throughput, RAM allocation and the processing deadline.
- Distinguish ADC overrun from software buffer overwrite and explain ownership.
- Design a validation procedure with measurable pass criteria.
1. Make the sampling interval predictable
A hardware timer can trigger each ADC conversion at a defined interval. The ADC digitizes the input, and DMA transfers the result from a peripheral register to memory. In circular mode, DMA returns to the beginning of its allocated buffer after reaching the end. The STMicroelectronics ADC tutorial documents DMA configuration. Select an ADC conversion time short enough for the trigger interval, including acquisition and conversion. For analog accuracy, review OSFTC.018; for transfer fundamentals, review OSFEC.006.
2. Calculate a ping-pong buffer deadline
Assume one channel, 20,000 samples per second, and 16-bit storage per sample. Throughput is 40,000 bytes per second (40 kB/s). Allocate 1,024 samples in two halves of 512. Total memory consumption is 2,048 bytes, excluding metadata. Each half fills in 512 / 20,000 = 25.6 ms. DMA alternates halves; after one half completes, the processor has at most 25.6 ms before DMA begins rewriting it. A measured worst-case 8 ms processing interval leaves 17.6 ms nominal margin. Include interrupt latency, scheduling delay, cache maintenance and processing in the deadline.
3. Protect buffer ownership
A half-transfer interrupt announces that the first half is ready; a transfer-complete interrupt announces the second. Keep callbacks short: record the completed region and sequence number, then notify a worker. The worker must release or copy the data before DMA returns. A queue of pointers alone does not extend sample lifetime. If processing stalls, record the lost block, reject invalid data and recover according to application policy. A larger buffer provides more time but increases latency and memory demand.
ADC peripheral overrun occurs when an unread conversion result is displaced or otherwise cannot be delivered in time; exact behavior depends on device configuration. Software buffer overwrite is different: DMA may have transferred successfully, but the processor missed its ownership deadline. Track both errors separately. On processors with noncoherent data caches, use a suitable noncacheable DMA region or the platform’s prescribed cache-maintenance and alignment rules. Volatile alone cannot guarantee cache coherency or safe synchronization. See Zephyr ring buffer concurrency documentation and OSFEC.016 on interrupt timing.
4. Simulated engineering lab
Use an authorized low-voltage development board and a known in-range signal. Configure the timer, ADC and DMA using the exact device manual. Record expected sample rate, half-buffer size, sequence numbers, ADC and DMA error flags, and worst-case worker completion time. First verify a stable stream, then inject a controlled 30 ms worker delay. At 20 kS/s, 30 ms exceeds the 25.6 ms ownership budget: the test should detect a missed deadline or invalid block, even if no ADC overrun is raised. Resume normal scheduling and confirm the recovery policy works. Follow the hardware-in-the-loop testing lesson to document reproducible results.
Knowledge check
How many bytes does each sample occupy? Two. How much time is available per half? 25.6 ms. Does a successful DMA interrupt guarantee that software retained every sample? No; ownership can expire before consumption.
Elementary conclusion
Timer-triggered sampling controls when data arrives. DMA controls how data moves. Buffer ownership and deadlines determine whether the application keeps valid measurements.
Primary references: ST ADC measurement recommendations; ST ADC transfer and overrun definitions; Zephyr ring buffer documentation linked above.
Editor’s Note: All numerical scenarios are instructional calculations, not measured hardware results. Cover photograph: Frans (fotoopa), Nucleo-H755ZI-Q board, CC BY 2.0, resized for editorial use; not the experimental setup.
We volunteer daily to ensure the credibility of the information on this platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiative, please scan the QR code below.


Leave a Reply