ADC
DMA transfers are recommended for the ADC in STM32CubeMX. In polling mode, threads reading different channels of the same ADC at the same time may get wrong data.
DMA Mode Configuration Requirements
- Configure the ADC conversion sequence (Rank).
- Enable Continuous Conversion Mode and DMA Continuous Requests.
- Set DMA to Circular mode.
Polling Mode Configuration Requirements
- The number of conversion channels must be 1 (i.e., only one Rank).
- Disable Continuous Conversion Mode.
Example
The code generator will read the enabled channels for each ADC peripheral and their order in continuous conversion mode to generate the following code:
// Create the ADC object
static STM32ADC adc3(&hadc3, adc3_buf, {ADC_CHANNEL_8}, 3.3);
// One reference per channel, named <ADC instance>_<channel>
static auto& adc3_adc_channel_8 = adc3.GetChannel(0);
UNUSED(adc3_adc_channel_8);
In polling mode, all enabled channels are recognized; in DMA mode, only channels with a configured Rank are recognized. In DMA mode the channel references follow the Ranks, GetChannel(i) being Rank i+1; when a channel is configured in several Ranks, the later references carry a Rank suffix, for example adc3_adc_channel_8_rank12. With XRobot integration (--xrobot), each channel reference is registered under the same name as LibXR::ADC.
STM32ADC contains several channel objects, each derived from LibXR::ADC and obtained with GetChannel(i).
Configuration File
After the code is generated, an ADC configuration section will appear in the User/libxr_config.yaml file, formatted as follows:
ADC:
adcX:
buffer_size: 32
dma_section: ''
vref: 3.3
buffer_size: Buffer bytes per channel. The generateduint16_tbuffer holdsbuffer_size / 2 × channel countelements (16 for the single channel above).dma_section: The memory section where the DMA buffer is located.vref: The reference voltage for the ADC, in volts.
After editing the file, regenerate the code with the commands in Regenerating the Code.