STM32 - Analogread

Some tips about analoRead on STM32DUINO.

Pin Map:
analogread sequentially scans through PinMap_ADC to find the pin’s ADC information (eg. F405).
The further the pin is in the list, the more time it takes.
You can redefine PinMap_ADC in your sketch, keeping only the analog pins you need(analoRead, current sense, MagneticSensorAnalog, …).

const PinMap PinMap_ADC = {
{PC_5, ADC1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 15, 0)}, // ADC1_IN15
{PA_3, ADC1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 3, 0)}, // ADC1_IN3
{PC_0, ADC1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 10, 0)}, // ADC1_IN10
{PC_1, ADC1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 11, 0)}, // ADC1_IN11
};

Analogread Execution went from 17us to 14us for PA3, and from 22us to 14us for PC5 :scream:

Sampling Time:
STM32DUINO uses ADC_SAMPLINGTIME as default sampling time in analogread for pin channels, and ADC_SAMPLINGTIME_INTERNAL for internal channels (Voltage reference, internal temperature, …).
On STM32F405, ADC_SAMPLETIME_15CYCLES for pin channels, and ADC_SAMPLETIME_480CYCLES for internal channels.

By using this build flag, analogread is using the lowest sampling time, which is ADC_SAMPLETIME_3CYCLES on F405

-DADC_SAMPLINGTIME0

This only saved 0.1us, but it’s worth knowing how to change the sampling time