This post is targeted at engineers, hobbyists, students, and engineers and provides two complete practical implementations, one based on HAL drivers and the second based on both HAL and LL drivers, for sampling a signal at regular intervals.
The following equipment is used
- NUCLEO-L476RG Board and an analog device like ADXL345 accelerometer
- Eclipse with the necessary packages for Nucleo boards installed
- OpenOCD or STLink USB Driver - STM32CubeMX
STM’s Application Note AN3116 explains the different operating modes but forgets to give any practical examples.
The ADC can perform a single conversion, or continuously convert values. The conversion can be performed on a single channel or multiple channels.
Each of the four modes can be triggered by an external trigger such as a timer. In any mode the sample time per channel can be specified as number of ADC clock steps. Interrupts can be generated to signal the completion of a conversion.
I focus here on the multiple channels mode which is referred to as scan configuration.
The ADXL345 accelerometer is used to feed signals into the ADC. Upon expiry of the timer (TIM6) period an interrupt is triggered that initiates the ADC. During the conversion the DMA copies the samples into the cyclic buffer.
Two implementations are proposed one based on HAL drivers exclusively and the other based on mixed HAL and LL drivers. The latter one allows us to gain a factor 2 in size code.
Jean-Christophe Toussaint full professor @ phelma