libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
adc.c
Go to the documentation of this file.
1/** @addtogroup adc_file ADC peripheral API
2@ingroup peripheral_apis
3
4@version 1.0.0
5
6@author @htmlonly © @endhtmlonly 2009
7Edward Cheeseman <evbuilder@users.sourceforge.net>
8@author @htmlonly &copy; @endhtmlonly 2012
9Ken Sarkies <ksarkies@internode.on.net>
10
11@date 18 August 2012
12
13This library supports the A/D Converter Control System in the STM32F1xx series
14of ARM Cortex Microcontrollers by ST Microelectronics.
15
16Devices can have up to three A/D converters each with their own set of
17registers. However all the A/D converters share a common clock which is
18prescaled from the APB2 clock by default by a minimum factor of 2 to a maximum
19of 8.
20
21Each A/D converter has up to 18 channels:
22@li On ADC1 the analog channels 16 and 17 are internally connected to the
23temperature
24sensor and V<sub>REFINT</sub>, respectively.
25@li On ADC2 the analog channels 16 and 17 are internally connected to
26V<sub>SS</sub>.
27@li On ADC3 the analog channels 9, 14, 15, 16 and 17 are internally connected
28to V<sub>SS</sub>.
29
30The conversions can occur as a one-off conversion whereby the process stops
31once conversion is complete. The conversions can also be continuous wherein a
32new conversion starts immediately the previous conversion has ended.
33
34Conversion can occur as a single channel conversion or a scan of a group of
35channels in either continuous or one-off mode. If more than one channel is
36converted in a scan group, DMA must be used to transfer the data as there is
37only one result register available. An interrupt can be set to occur at the end
38of conversion, which occurs after all channels have been scanned.
39
40A discontinuous mode allows a subgroup of group of a channels to be converted
41in bursts of a given length.
42
43Injected conversions allow a second group of channels to be converted
44separately from the regular group. An interrupt can be set to occur at the end
45of conversion, which occurs after all channels have been scanned.
46
47@section adc_api_ex Basic ADC Handling API.
48
49Example 1: Simple single channel conversion polled. Enable the peripheral clock
50and ADC, reset ADC and set the prescaler divider. Set dual mode to independent
51(default). Enable triggering for a software trigger.
52
53@code
54 rcc_periph_clock_enable(RCC_ADC1);
55 adc_power_off(ADC1);
56 rcc_periph_reset_pulse(RST_ADC1);
57 rcc_set_adcpre(RCC_CFGR_ADCPRE_PCLK2_DIV2);
58 adc_set_dual_mode(ADC_CR1_DUALMOD_IND);
59 adc_disable_scan_mode(ADC1);
60 adc_set_single_conversion_mode(ADC1);
61 adc_set_sample_time(ADC1, ADC_CHANNEL0, ADC_SMPR_SMP_1DOT5CYC);
62 adc_enable_external_trigger_regular(ADC1, ADC_CR2_EXTSEL_SWSTART);
63 adc_power_on(ADC1);
64 adc_reset_calibration(ADC1);
65 adc_calibrate(ADC1);
66 adc_start_conversion_regular(ADC1);
67 while (! adc_eoc(ADC1));
68 reg16 = adc_read_regular(ADC1);
69@endcode
70
71LGPL License Terms @ref lgpl_license
72 */
73/*
74 * This file is part of the libopencm3 project.
75 *
76 * Copyright (C) 2009 Edward Cheeseman <evbuilder@users.sourceforge.net>
77 *
78 * This library is free software: you can redistribute it and/or modify
79 * it under the terms of the GNU Lesser General Public License as published by
80 * the Free Software Foundation, either version 3 of the License, or
81 * (at your option) any later version.
82 *
83 * This library is distributed in the hope that it will be useful,
84 * but WITHOUT ANY WARRANTY; without even the implied warranty of
85 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
86 * GNU Lesser General Public License for more details.
87 *
88 * You should have received a copy of the GNU Lesser General Public License
89 * along with this library. If not, see <http://www.gnu.org/licenses/>.
90 */
91
92/**@{*/
93
95
96/*---------------------------------------------------------------------------*/
97/** @brief ADC Power On
98
99If the ADC is in power-down mode then it is powered up. The application needs
100to wait a time of about 3 microseconds for stabilization before using the ADC.
101If the ADC is already on this function call has no effect.
102 * NOTE Common with F37x
103
104@param[in] adc Unsigned int32. ADC block register address base @ref adc_reg_base
105*/
106
107void adc_power_on(uint32_t adc)
108{
109 if (!(ADC_CR2(adc) & ADC_CR2_ADON)) {
110 ADC_CR2(adc) |= ADC_CR2_ADON;
111 }
112}
113
114/*---------------------------------------------------------------------------*/
115/** @brief ADC Start a Conversion Without Trigger
116
117This initiates a conversion by software without a trigger. The ADC needs to be
118powered on before this is called, otherwise this function has no effect.
119
120Note that this is not available in other STM32F families. To ensure code
121compatibility, enable triggering and use a software trigger source @see
122adc_start_conversion_regular.
123
124@param[in] adc Unsigned int32. ADC block register address base @ref adc_reg_base
125*/
126
128{
129 if (ADC_CR2(adc) & ADC_CR2_ADON) {
130 ADC_CR2(adc) |= ADC_CR2_ADON;
131 }
132}
133
134/*---------------------------------------------------------------------------*/
135/** @brief ADC Set Dual A/D Mode
136
137The dual mode uses ADC1 as master and ADC2 in a slave arrangement. This setting
138is applied to ADC1 only. Start of conversion when triggered can cause
139simultaneous conversion with ADC2, or alternate conversion. Regular and
140injected conversions can be configured, each one being separately simultaneous
141or alternate.
142
143Fast interleaved mode starts ADC1 immediately on trigger, and ADC2 seven clock
144cycles later.
145
146Slow interleaved mode starts ADC1 immediately on trigger, and ADC2 fourteen
147clock cycles later, followed by ADC1 fourteen cycles later again. This can only
148be used on a single channel.
149
150Alternate trigger mode must occur on an injected channel group, and alternates
151between the ADCs on each trigger.
152
153Note that sampling must not overlap between ADCs on the same channel.
154
155Dual A/D converter modes possible:
156
157@li IND: Independent mode.
158@li CRSISM: Combined regular simultaneous + injected simultaneous mode.
159@li CRSATM: Combined regular simultaneous + alternate trigger mode.
160@li CISFIM: Combined injected simultaneous + fast interleaved mode.
161@li CISSIM: Combined injected simultaneous + slow interleaved mode.
162@li ISM: Injected simultaneous mode only.
163@li RSM: Regular simultaneous mode only.
164@li FIM: Fast interleaved mode only.
165@li SIM: Slow interleaved mode only.
166@li ATM: Alternate trigger mode only.
167
168@param[in] mode Unsigned int32. Dual mode selection from @ref adc_cr1_dualmod
169*/
170
171void adc_set_dual_mode(uint32_t mode)
172{
173 ADC1_CR1 |= mode;
174}
175
176
177
178/*---------------------------------------------------------------------------*/
179/** @brief ADC Enable The Temperature Sensor
180
181This enables both the sensor and the reference voltage measurements on channels
18216 and 17.
183
184*/
185
187{
189}
190
191/*---------------------------------------------------------------------------*/
192/** @brief ADC Disable The Temperature Sensor
193
194Disabling this will reduce power consumption from the sensor and the reference
195voltage measurements.
196*/
197
199{
200 ADC_CR2(ADC1) &= ~ADC_CR2_TSVREFE;
201}
202
203
204/*---------------------------------------------------------------------------*/
205/** @brief ADC Enable an External Trigger for Regular Channels
206
207This enables an external trigger for set of defined regular channels.
208
209For ADC1 and ADC2
210@li Timer 1 CC1 event
211@li Timer 1 CC2 event
212@li Timer 1 CC3 event
213@li Timer 2 CC2 event
214@li Timer 3 TRGO event
215@li Timer 4 CC4 event
216@li EXTI (TIM8_TRGO is also possible on some devices, see datasheet)
217@li Software Start
218
219For ADC3
220@li Timer 3 CC1 event
221@li Timer 2 CC3 event
222@li Timer 1 CC3 event
223@li Timer 8 CC1 event
224@li Timer 8 TRGO event
225@li Timer 5 CC1 event
226@li Timer 5 CC3 event
227@li Software Start
228
229@param[in] adc Unsigned int32. ADC block register address base @ref
230adc_reg_base.
231@param[in] trigger Unsigned int8. Trigger identifier @ref adc_trigger_regular_12
232for ADC1 and ADC2, or @ref adc_trigger_regular_3 for ADC3.
233*/
234
235void adc_enable_external_trigger_regular(uint32_t adc, uint32_t trigger)
236{
237 uint32_t reg32;
238
239 reg32 = (ADC_CR2(adc) & ~(ADC_CR2_EXTSEL_MASK));
240 reg32 |= (trigger);
241 ADC_CR2(adc) = reg32;
242 ADC_CR2(adc) |= ADC_CR2_EXTTRIG;
243}
244
245/*---------------------------------------------------------------------------*/
246/** @brief ADC Disable an External Trigger for Regular Channels
247
248@param[in] adc Unsigned int32. ADC block register address base @ref
249adc_reg_base.
250*/
251
253{
254 ADC_CR2(adc) &= ~ADC_CR2_EXTTRIG;
255}
256
257/*---------------------------------------------------------------------------*/
258/** @brief ADC Enable an External Trigger for Injected Channels
259
260This enables an external trigger for set of defined injected channels.
261
262For ADC1 and ADC2
263@li Timer 1 TRGO event
264@li Timer 1 CC4 event
265@li Timer 2 TRGO event
266@li Timer 2 CC1 event
267@li Timer 3 CC4 event
268@li Timer 4 TRGO event
269@li EXTI (TIM8 CC4 is also possible on some devices, see datasheet)
270@li Software Start
271
272For ADC3
273@li Timer 1 TRGO event
274@li Timer 1 CC4 event
275@li Timer 4 CC3 event
276@li Timer 8 CC2 event
277@li Timer 8 CC4 event
278@li Timer 5 TRGO event
279@li Timer 5 CC4 event
280@li Software Start
281
282@param[in] adc Unsigned int32. ADC block register address base @ref
283adc_reg_base.
284@param[in] trigger Unsigned int8. Trigger identifier @ref
285adc_trigger_injected_12 for ADC1 and ADC2, or @ref adc_trigger_injected_3 for
286ADC3.
287*/
288void adc_enable_external_trigger_injected(uint32_t adc, uint32_t trigger)
289{
290 uint32_t reg32;
291
292 reg32 = (ADC_CR2(adc) & ~(ADC_CR2_JEXTSEL_MASK)); /* Clear bits [12:14]
293 */
294 reg32 |= (trigger);
295 ADC_CR2(adc) = reg32;
297}
298
299/*---------------------------------------------------------------------------*/
300/** @brief ADC Disable an External Trigger for Injected Channels
301
302@param[in] adc Unsigned int32. ADC block register address base @ref
303adc_reg_base.
304*/
305
307{
308 ADC_CR2(adc) &= ~ADC_CR2_JEXTTRIG;
309}
310
311/*---------------------------------------------------------------------------*/
312/** @brief ADC Initialize Calibration Registers
313
314This resets the calibration registers. It is not clear if this is required to be
315done before every calibration operation.
316
317@param[in] adc Unsigned int32. ADC block register address base @ref
318adc_reg_base.
319*/
320
321void adc_reset_calibration(uint32_t adc)
322{
323 ADC_CR2(adc) |= ADC_CR2_RSTCAL;
324 while (ADC_CR2(adc) & ADC_CR2_RSTCAL);
325}
326
327/*---------------------------------------------------------------------------*/
328/** @brief ADC Calibration
329@deprecated replaced by adc_calibrate/_async/_is_calibrating
330The calibration data for the ADC is recomputed. The hardware clears the
331calibration status flag when calibration is complete. This function does not
332return until this happens and the ADC is ready for use.
333
334The ADC must have been powered down for at least 2 ADC clock cycles, then
335powered on. before calibration starts
336
337@param[in] adc Unsigned int32. ADC block register address base @ref
338adc_reg_base.
339*/
340
341void adc_calibration(uint32_t adc)
342{
343 ADC_CR2(adc) |= ADC_CR2_CAL;
344 while (ADC_CR2(adc) & ADC_CR2_CAL);
345}
346
347/**
348 * Start the ADC calibration and immediately return.
349 * @sa adc_calibrate
350 * @sa adc_is_calibrate
351 * @param adc ADC Block register address base @ref adc_reg_base
352 */
353void adc_calibrate_async(uint32_t adc)
354{
355 ADC_CR2(adc) |= ADC_CR2_CAL;
356}
357
358/**
359 * Is the ADC Calibrating?
360 * @param adc ADC Block register address base @ref adc_reg_base
361 * @return true if the adc is currently calibrating
362 */
363bool adc_is_calibrating(uint32_t adc)
364{
365 return (ADC_CR2(adc) & ADC_CR2_CAL);
366}
367
368/**
369 * Start ADC calibration and wait for it to finish.
370 * The ADC must have been powered down for at least 2 ADC clock cycles, then
371 * powered on before calibration starts
372 * @param adc ADC Block register address base @ref adc_reg_base
373 */
374void adc_calibrate(uint32_t adc)
375{
377 while (adc_is_calibrating(adc));
378}
379
380
381/*---------------------------------------------------------------------------*/
382/** @brief ADC Set the Sample Time for a Single Channel
383
384The sampling time can be selected in ADC clock cycles from 1.5 to 239.5.
385
386@param[in] adc Unsigned int32. ADC block register address base @ref
387adc_reg_base.
388@param[in] channel Unsigned int8. ADC Channel integer 0..18 or from @ref
389adc_channel.
390@param[in] time Unsigned int8. Sampling time selection from @ref adc_sample_rg.
391 * * NOTE Common with f2 and f37x and f4
392*/
393
394void adc_set_sample_time(uint32_t adc, uint8_t channel, uint8_t time)
395{
396 uint32_t reg32;
397
398 if (channel < 10) {
399 reg32 = ADC_SMPR2(adc);
400 reg32 &= ~(0x7 << (channel * 3));
401 reg32 |= (time << (channel * 3));
402 ADC_SMPR2(adc) = reg32;
403 } else {
404 reg32 = ADC_SMPR1(adc);
405 reg32 &= ~(0x7 << ((channel - 10) * 3));
406 reg32 |= (time << ((channel - 10) * 3));
407 ADC_SMPR1(adc) = reg32;
408 }
409}
410
411/*---------------------------------------------------------------------------*/
412/** @brief ADC Set the Sample Time for All Channels
413
414The sampling time can be selected in ADC clock cycles from 1.5 to 239.5, same
415for all channels.
416
417@param[in] adc Unsigned int32. ADC block register address base @ref
418adc_reg_base.
419@param[in] time Unsigned int8. Sampling time selection from @ref adc_sample_rg.
420 * * NOTE Common with f2 and f37x and f4
421*/
422
423void adc_set_sample_time_on_all_channels(uint32_t adc, uint8_t time)
424{
425 uint8_t i;
426 uint32_t reg32 = 0;
427
428 for (i = 0; i <= 9; i++) {
429 reg32 |= (time << (i * 3));
430 }
431 ADC_SMPR2(adc) = reg32;
432
433 for (i = 10; i <= 17; i++) {
434 reg32 |= (time << ((i - 10) * 3));
435 }
436 ADC_SMPR1(adc) = reg32;
437}
438
439
440/*---------------------------------------------------------------------------*/
441
442/**@}*/
443
#define ADC_CR2_EXTTRIG
External trigger conversion mode for regular channels.
Definition: f1/adc.h:140
#define ADC_CR2_CAL
Definition: f1/adc.h:270
#define ADC1_CR1
Definition: adc_common_v1.h:67
#define ADC_CR2_JEXTSEL_MASK
Definition: f1/adc.h:251
#define ADC_SMPR1(block)
Definition: adc_common_v1.h:61
#define ADC_CR2(block)
Definition: adc_common_v1.h:58
#define ADC_CR2_RSTCAL
Definition: f1/adc.h:267
#define ADC_CR2_ADON
Definition: f1/adc.h:281
#define ADC_CR2_JEXTTRIG
Definition: f1/adc.h:197
#define ADC_SMPR2(block)
Definition: adc_common_v1.h:64
#define ADC_CR2_EXTSEL_MASK
Definition: f1/adc.h:191
#define ADC_CR2_TSVREFE
Temperature sensor and V_REFINT enable.
Definition: f1/adc.h:131
void adc_set_sample_time_on_all_channels(uint32_t adc, uint8_t time)
ADC Set the Sample Time for All Channels.
Definition: adc.c:423
void adc_set_sample_time(uint32_t adc, uint8_t channel, uint8_t time)
ADC Set the Sample Time for a Single Channel.
Definition: adc.c:394
void adc_calibrate_async(uint32_t adc)
Start the ADC calibration and immediately return.
Definition: adc.c:353
bool adc_is_calibrating(uint32_t adc)
Is the ADC Calibrating?
Definition: adc.c:363
void adc_calibration(uint32_t adc)
ADC Calibration.
Definition: adc.c:341
void adc_start_conversion_direct(uint32_t adc)
ADC Start a Conversion Without Trigger.
Definition: adc.c:127
void adc_power_on(uint32_t adc)
ADC Power On.
Definition: adc.c:107
void adc_reset_calibration(uint32_t adc)
ADC Initialize Calibration Registers.
Definition: adc.c:321
void adc_enable_external_trigger_injected(uint32_t adc, uint32_t trigger)
ADC Enable an External Trigger for Injected Channels.
Definition: adc.c:288
void adc_disable_external_trigger_injected(uint32_t adc)
ADC Disable an External Trigger for Injected Channels.
Definition: adc.c:306
void adc_enable_temperature_sensor()
ADC Enable The Temperature Sensor.
Definition: adc.c:186
void adc_enable_external_trigger_regular(uint32_t adc, uint32_t trigger)
ADC Enable an External Trigger for Regular Channels.
Definition: adc.c:235
void adc_set_dual_mode(uint32_t mode)
ADC Set Dual A/D Mode.
Definition: adc.c:171
void adc_calibrate(uint32_t adc)
Start ADC calibration and wait for it to finish.
Definition: adc.c:374
void adc_disable_temperature_sensor()
ADC Disable The Temperature Sensor.
Definition: adc.c:198
void adc_disable_external_trigger_regular(uint32_t adc)
ADC Disable an External Trigger for Regular Channels.
Definition: adc.c:252
#define ADC1
Definition: adc_common_v1.h:46