14 static PinName g_current_pin =
NC;
16 #ifndef ADC_SAMPLINGTIME
17 #if defined(ADC_SAMPLETIME_8CYCLES_5)
18 #define ADC_SAMPLINGTIME ADC_SAMPLETIME_8CYCLES_5;
19 #elif defined(ADC_SAMPLETIME_12CYCLES)
20 #define ADC_SAMPLINGTIME ADC_SAMPLETIME_12CYCLES;
21 #elif defined(ADC_SAMPLETIME_12CYCLES_5)
22 #define ADC_SAMPLINGTIME ADC_SAMPLETIME_12CYCLES_5;
23 #elif defined(ADC_SAMPLETIME_13CYCLES_5)
24 #define ADC_SAMPLINGTIME ADC_SAMPLETIME_13CYCLES_5;
25 #elif defined(ADC_SAMPLETIME_15CYCLES)
26 #define ADC_SAMPLINGTIME ADC_SAMPLETIME_15CYCLES;
27 #elif defined(ADC_SAMPLETIME_16CYCLES)
28 #define ADC_SAMPLINGTIME ADC_SAMPLETIME_16CYCLES;
29 #elif defined(ADC_SAMPLETIME_19CYCLES_5)
30 #define ADC_SAMPLINGTIME ADC_SAMPLETIME_19CYCLES_5;
34 #if defined(ADC_VER_V5_V90) && !defined(ADC3_SAMPLINGTIME)
35 #define ADC3_SAMPLINGTIME ADC3_SAMPLETIME_24CYCLES_5;
38 #if defined(ADC4_SAMPLETIME_19CYCLES_5) && !defined(ADC4_SAMPLINGTIME)
39 #define ADC4_SAMPLINGTIME ADC4_SAMPLETIME_19CYCLES_5;
49 #ifndef ADC_SAMPLINGTIME_INTERNAL
50 #if defined(ADC_SAMPLETIME_480CYCLES)
51 #define ADC_SAMPLINGTIME_INTERNAL ADC_SAMPLETIME_480CYCLES
52 #elif defined(ADC_SAMPLETIME_384CYCLES)
53 #define ADC_SAMPLINGTIME_INTERNAL ADC_SAMPLETIME_384CYCLES
54 #elif defined(ADC_SAMPLETIME_810CYCLES_5)
55 #define ADC_SAMPLINGTIME_INTERNAL ADC_SAMPLETIME_810CYCLES_5
56 #elif defined(ADC_SAMPLETIME_814CYCLES)
57 #define ADC_SAMPLINGTIME_INTERNAL ADC_SAMPLETIME_814CYCLES
58 #elif defined(ADC_SAMPLETIME_640CYCLES_5)
59 #define ADC_SAMPLINGTIME_INTERNAL ADC_SAMPLETIME_640CYCLES_5
60 #elif defined(ADC_SAMPLETIME_601CYCLES_5)
61 #define ADC_SAMPLINGTIME_INTERNAL ADC_SAMPLETIME_601CYCLES_5
62 #elif defined(ADC_SAMPLETIME_247CYCLES_5)
63 #define ADC_SAMPLINGTIME_INTERNAL ADC_SAMPLETIME_247CYCLES_5
64 #elif defined(ADC_SAMPLETIME_239CYCLES_5)
65 #define ADC_SAMPLINGTIME_INTERNAL ADC_SAMPLETIME_239CYCLES_5
66 #elif defined(ADC_SAMPLETIME_160CYCLES_5)
67 #define ADC_SAMPLINGTIME_INTERNAL ADC_SAMPLETIME_160CYCLES_5
69 #error "ADC sampling time could not be defined for internal channels!"
75 #ifdef ADC_CLOCK_SYNC_PCLK_DIV4
76 #define ADC_CLOCK_DIV ADC_CLOCK_SYNC_PCLK_DIV4
77 #elif ADC_CLOCK_SYNC_PCLK_DIV2
78 #define ADC_CLOCK_DIV ADC_CLOCK_SYNC_PCLK_DIV2
79 #elif defined(ADC_CLOCK_ASYNC_DIV4)
80 #define ADC_CLOCK_DIV ADC_CLOCK_ASYNC_DIV4
84 #ifndef ADC_REGULAR_RANK_1
85 #define ADC_REGULAR_RANK_1 1
89 bool adc_setup_read(PinName pin, uint32_t resolution) {
91 ADC_ChannelConfTypeDef AdcChannelConf = {};
92 uint32_t samplingTime = ADC_SAMPLINGTIME;
96 if ((pin & PADC_BASE) && (pin < ANA_START)) {
97 #if defined(STM32H7xx) || defined(STM32MP1xx)
99 AdcHandle.Instance = ADC3;
101 AdcHandle.Instance = ADC2;
104 AdcHandle.Instance = ADC1;
105 #if defined(ADC5) && defined(ADC_CHANNEL_TEMPSENSOR_ADC5)
106 if (pin == PADC_TEMP_ADC5) {
107 AdcHandle.Instance = ADC5;
111 channel = get_adc_internal_channel(pin);
112 samplingTime = ADC_SAMPLINGTIME_INTERNAL;
114 AdcHandle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC);
115 channel = get_adc_channel(pin, &bank);
116 #if defined(ADC_VER_V5_V90)
117 if (AdcHandle.Instance == ADC3) {
118 samplingTime = ADC3_SAMPLINGTIME;
121 #if defined(ADC4_SAMPLINGTIME)
122 if (AdcHandle.Instance == ADC4) {
123 samplingTime = ADC4_SAMPLINGTIME;
128 if (AdcHandle.Instance == NP) {
133 AdcHandle.Init.ClockPrescaler = ADC_CLOCK_DIV;
135 #ifdef ADC_RESOLUTION_12B
136 switch (resolution) {
137 #ifdef ADC_RESOLUTION_6B
139 AdcHandle.Init.Resolution = ADC_RESOLUTION_6B;
143 AdcHandle.Init.Resolution = ADC_RESOLUTION_8B;
146 AdcHandle.Init.Resolution = ADC_RESOLUTION_10B;
150 AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
152 #ifdef ADC_RESOLUTION_14B
154 AdcHandle.Init.Resolution = ADC_RESOLUTION_14B;
157 #ifdef ADC_RESOLUTION_16B
159 AdcHandle.Init.Resolution = ADC_RESOLUTION_16B;
166 #ifdef ADC_DATAALIGN_RIGHT
167 AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
169 #ifdef ADC_SCAN_SEQ_FIXED
170 AdcHandle.Init.ScanConvMode = ADC_SCAN_SEQ_FIXED;
172 AdcHandle.Init.ScanConvMode = DISABLE;
174 #ifdef ADC_EOC_SINGLE_CONV
175 AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
177 #if !defined(STM32F1xx) && !defined(STM32F2xx) && !defined(STM32F4xx) &&
178 !defined(STM32F7xx) && !defined(ADC1_V2_5)
179 AdcHandle.Init.LowPowerAutoWait = DISABLE;
181 #if !defined(STM32F1xx) && !defined(STM32F2xx) && !defined(STM32F3xx) &&
182 !defined(STM32F4xx) && !defined(STM32F7xx) && !defined(STM32G4xx) &&
183 !defined(STM32H7xx) && !defined(STM32L4xx) && !defined(STM32L5xx) &&
184 !defined(STM32MP1xx) && !defined(STM32WBxx) || defined(ADC_SUPPORT_2_5_MSPS)
185 AdcHandle.Init.LowPowerAutoPowerOff = DISABLE;
187 #ifdef ADC_CHANNELS_BANK_B
188 AdcHandle.Init.ChannelsBank = bank;
189 #elif defined(ADC_CHANNELS_BANK_A)
190 AdcHandle.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
192 AdcHandle.Init.ContinuousConvMode = DISABLE;
193 #if !defined(STM32F0xx) && !defined(STM32L0xx)
194 AdcHandle.Init.NbrOfConversion = 1;
196 AdcHandle.Init.DiscontinuousConvMode = DISABLE;
197 #if !defined(STM32C0xx) && !defined(STM32F0xx) && !defined(STM32G0xx) &&
198 !defined(STM32L0xx) && !defined(STM32WLxx) && !defined(ADC_SUPPORT_2_5_MSPS)
199 AdcHandle.Init.NbrOfDiscConversion = 0;
201 AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
202 #if !defined(STM32F1xx) && !defined(ADC1_V2_5)
203 AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
205 #if !defined(STM32F1xx) && !defined(STM32H7xx) && !defined(STM32MP1xx) &&
207 AdcHandle.Init.DMAContinuousRequests = DISABLE;
209 #ifdef ADC_CONVERSIONDATA_DR
210 AdcHandle.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
212 #ifdef ADC_OVR_DATA_OVERWRITTEN
213 AdcHandle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
215 #ifdef ADC_LEFTBITSHIFT_NONE
216 AdcHandle.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
219 #if defined(STM32F0xx)
220 AdcHandle.Init.SamplingTimeCommon = samplingTime;
222 #if defined(STM32C0xx) || defined(STM32G0xx) || defined(STM32U5xx) ||
223 defined(STM32WLxx) || defined(ADC_SUPPORT_2_5_MSPS)
224 AdcHandle.Init.SamplingTimeCommon1 = samplingTime;
225 AdcHandle.Init.SamplingTimeCommon2 = samplingTime;
227 #if defined(STM32L0xx)
228 AdcHandle.Init.LowPowerFrequencyMode = DISABLE;
229 AdcHandle.Init.SamplingTime = samplingTime;
231 #if !defined(STM32F0xx) && !defined(STM32F1xx) && !defined(STM32F2xx) &&
232 !defined(STM32F3xx) && !defined(STM32F4xx) && !defined(STM32F7xx) &&
233 !defined(STM32L1xx) && !defined(ADC_SUPPORT_2_5_MSPS)
234 AdcHandle.Init.OversamplingMode = DISABLE;
238 #if defined(ADC_CFGR_DFSDMCFG) && defined(DFSDM1_Channel0)
239 AdcHandle.Init.DFSDMConfig = ADC_DFSDM_MODE_DISABLE;
241 #ifdef ADC_TRIGGER_FREQ_HIGH
242 AdcHandle.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
244 #ifdef ADC_VREF_PPROT_NONE
245 AdcHandle.Init.VrefProtection = ADC_VREF_PPROT_NONE;
248 AdcHandle.State = HAL_ADC_STATE_RESET;
249 AdcHandle.DMA_Handle = NULL;
250 AdcHandle.Lock = HAL_UNLOCKED;
255 if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
259 AdcChannelConf.Channel = channel;
261 #if defined(STM32G4xx) || defined(STM32L4xx) || defined(STM32L5xx) ||
263 if (!IS_ADC_CHANNEL(&AdcHandle, AdcChannelConf.Channel)) {
265 if (!IS_ADC_CHANNEL(AdcChannelConf.Channel)) {
269 #if defined(ADC_SCAN_SEQ_FIXED) && defined(ADC_RANK_CHANNEL_NUMBER)
270 AdcChannelConf.Rank = ADC_RANK_CHANNEL_NUMBER;
274 #if !defined(STM32L0xx)
275 #if !defined(STM32G0xx)
276 AdcChannelConf.SamplingTime = samplingTime;
278 AdcChannelConf.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
281 #if defined(ADC_DIFFERENTIAL_ENDED) && !defined(ADC1_V2_5)
282 AdcChannelConf.SingleDiff = ADC_SINGLE_ENDED;
283 AdcChannelConf.OffsetNumber = ADC_OFFSET_NONE;
285 #if !defined(STM32C0xx) && !defined(STM32F0xx) && !defined(STM32F1xx) &&
286 !defined(STM32F2xx) && !defined(STM32G0xx) && !defined(STM32L0xx) &&
287 !defined(STM32L1xx) && !defined(STM32WBxx) && !defined(STM32WLxx) &&
289 AdcChannelConf.Offset = 0;
291 #if defined (STM32H7xx) || defined(STM32MP1xx)
292 AdcChannelConf.OffsetRightShift = DISABLE;
293 AdcChannelConf.OffsetSignedSaturation = DISABLE;
297 if (HAL_ADC_ConfigChannel(&AdcHandle, &AdcChannelConf) != HAL_OK) {
302 #if defined(ADC_CR_ADCAL) || defined(ADC_CR2_RSTCAL)
304 #if defined(ADC_CALIB_OFFSET)
305 if (HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED) != HAL_OK)
306 #elif defined(ADC_SINGLE_ENDED) && !defined(ADC1_V2_5)
307 if (HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED) != HAL_OK)
309 if (HAL_ADCEx_Calibration_Start(&AdcHandle) != HAL_OK)
318 if (HAL_ADC_Start(&AdcHandle) != HAL_OK) {