libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
f1/adc.h
Go to the documentation of this file.
1/** @defgroup adc_defines ADC Defines
2
3@brief <b>Defined Constants and Types for the STM32F1xx Analog to Digital
4Converters</b>
5
6@ingroup STM32F1xx_defines
7
8@version 1.0.0
9
10@author @htmlonly &copy; @endhtmlonly 2009
11Edward Cheeseman <evbuilder@users.sourceforge.net>
12
13@date 18 August 2012
14
15LGPL License Terms @ref lgpl_license
16 */
17/*
18 * This file is part of the libopencm3 project.
19 *
20 * Copyright (C) 2009 Edward Cheeseman <evbuilder@users.sourceforge.net>
21 *
22 * This library is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU Lesser General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
26 *
27 * This library is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU Lesser General Public License for more details.
31 *
32 * You should have received a copy of the GNU Lesser General Public License
33 * along with this library. If not, see <http://www.gnu.org/licenses/>.
34 */
35
36/**@{*/
37
38#ifndef LIBOPENCM3_ADC_H
39#define LIBOPENCM3_ADC_H
40
42
43/* --- Convenience macros -------------------------------------------------- */
44
45/* ADC injected channel data offset register x (ADC_JOFRx) (x=1..4) */
46#define ADC_JOFR1(block) MMIO32((block) + 0x14)
47#define ADC_JOFR2(block) MMIO32((block) + 0x18)
48#define ADC_JOFR3(block) MMIO32((block) + 0x1c)
49#define ADC_JOFR4(block) MMIO32((block) + 0x20)
50
51/* ADC watchdog high threshold register (ADC_HTR) */
52#define ADC_HTR(block) MMIO32((block) + 0x24)
53
54/* ADC watchdog low threshold register (ADC_LTR) */
55#define ADC_LTR(block) MMIO32((block) + 0x28)
56
57/* ADC regular sequence register 1 (ADC_SQR1) */
58#define ADC_SQR1(block) MMIO32((block) + 0x2c)
59
60/* ADC regular sequence register 2 (ADC_SQR2) */
61#define ADC_SQR2(block) MMIO32((block) + 0x30)
62
63/* ADC regular sequence register 3 (ADC_SQR3) */
64#define ADC_SQR3(block) MMIO32((block) + 0x34)
65
66/* ADC injected sequence register (ADC_JSQR) */
67#define ADC_JSQR(block) MMIO32((block) + 0x38)
68
69/* ADC injected data register x (ADC_JDRx) (x=1..4) */
70#define ADC_JDR1(block) MMIO32((block) + 0x3c)
71#define ADC_JDR2(block) MMIO32((block) + 0x40)
72#define ADC_JDR3(block) MMIO32((block) + 0x44)
73#define ADC_JDR4(block) MMIO32((block) + 0x48)
74
75/* ADC regular data register (ADC_DR) */
76#define ADC_DR(block) MMIO32((block) + 0x4c)
77
78
79/* --- ADC_CR1 values ------------------------------------------------------ */
80
81/* Note: Bits [21:20] are reserved, and must be kept at reset value. */
82
83/* DUALMOD[3:0]: Dual mode selection. (ADC1 only) */
84/* Legend:
85 * IND: Independent mode.
86 * CRSISM: Combined regular simultaneous + injected simultaneous mode.
87 * CRSATM: Combined regular simultaneous + alternate trigger mode.
88 * CISFIM: Combined injected simultaneous + fast interleaved mode.
89 * CISSIM: Combined injected simultaneous + slow interleaved mode.
90 * ISM: Injected simultaneous mode only.
91 * RSM: Regular simultaneous mode only.
92 * FIM: Fast interleaved mode only.
93 * SIM: Slow interleaved mode only.
94 * ATM: Alternate trigger mode only.
95 */
96/****************************************************************************/
97/* ADC_CR1 DUALMOD[3:0] ADC Mode Selection */
98/** @defgroup adc_cr1_dualmod ADC Mode Selection
99@ingroup adc_defines
100
101@{*/
102/** Independent (non-dual) mode */
103#define ADC_CR1_DUALMOD_IND (0x0 << 16)
104/** Combined regular simultaneous + injected simultaneous mode. */
105#define ADC_CR1_DUALMOD_CRSISM (0x1 << 16)
106/** Combined regular simultaneous + alternate trigger mode. */
107#define ADC_CR1_DUALMOD_CRSATM (0x2 << 16)
108/** Combined injected simultaneous + fast interleaved mode. */
109#define ADC_CR1_DUALMOD_CISFIM (0x3 << 16)
110/** Combined injected simultaneous + slow interleaved mode. */
111#define ADC_CR1_DUALMOD_CISSIM (0x4 << 16)
112/** Injected simultaneous mode only. */
113#define ADC_CR1_DUALMOD_ISM (0x5 << 16)
114/** Regular simultaneous mode only. */
115#define ADC_CR1_DUALMOD_RSM (0x6 << 16)
116/** Fast interleaved mode only. */
117#define ADC_CR1_DUALMOD_FIM (0x7 << 16)
118/** Slow interleaved mode only. */
119#define ADC_CR1_DUALMOD_SIM (0x8 << 16)
120/** Alternate trigger mode only. */
121#define ADC_CR1_DUALMOD_ATM (0x9 << 16)
122/**@}*/
123#define ADC_CR1_DUALMOD_MASK (0xF << 16)
124#define ADC_CR1_DUALMOD_SHIFT 16
125
126#define ADC_CR1_AWDCH_MAX 17
127
128/* --- ADC_CR2 values ------------------------------------------------------ */
129
130/* TSVREFE: */ /** Temperature sensor and V_REFINT enable. (ADC1 only!) */
131#define ADC_CR2_TSVREFE (1 << 23)
132
133/* SWSTART: */ /** Start conversion of regular channels. */
134#define ADC_CR2_SWSTART (1 << 22)
135
136/* JSWSTART: */ /** Start conversion of injected channels. */
137#define ADC_CR2_JSWSTART (1 << 21)
138
139/* EXTTRIG: */ /** External trigger conversion mode for regular channels. */
140#define ADC_CR2_EXTTRIG (1 << 20)
141
142/* EXTSEL[2:0]: External event select for regular group. */
143/* The following are only valid for ADC1 and ADC2. */
144/****************************************************************************/
145/* ADC_CR2 EXTSEL[2:0] ADC Trigger Identifier for ADC1 and ADC2 */
146/** @defgroup adc_trigger_regular_12 ADC Trigger Identifier for ADC1 and ADC2
147@ingroup adc_defines
148
149@{*/
150/** Timer 1 Compare Output 1 */
151#define ADC_CR2_EXTSEL_TIM1_CC1 (0x0 << 17)
152/** Timer 1 Compare Output 2 */
153#define ADC_CR2_EXTSEL_TIM1_CC2 (0x1 << 17)
154/** Timer 1 Compare Output 3 */
155#define ADC_CR2_EXTSEL_TIM1_CC3 (0x2 << 17)
156/** Timer 2 Compare Output 2 */
157#define ADC_CR2_EXTSEL_TIM2_CC2 (0x3 << 17)
158/** Timer 3 Trigger Output */
159#define ADC_CR2_EXTSEL_TIM3_TRGO (0x4 << 17)
160/** Timer 4 Compare Output 4 */
161#define ADC_CR2_EXTSEL_TIM4_CC4 (0x5 << 17)
162/** External Interrupt 11 */
163#define ADC_CR2_EXTSEL_EXTI11 (0x6 << 17)
164/** Software Trigger */
165#define ADC_CR2_EXTSEL_SWSTART (0x7 << 17)
166/**@}*/
167
168/* The following are only valid for ADC3 */
169/****************************************************************************/
170/* ADC_CR2 EXTSEL[2:0] ADC Trigger Identifier for ADC3 */
171/** @defgroup adc_trigger_regular_3 ADC Trigger Identifier for ADC3
172@ingroup adc_defines
173
174@{*/
175/** Timer 2 Compare Output 1 */
176#define ADC_CR2_EXTSEL_TIM3_CC1 (0x0 << 17)
177/** Timer 2 Compare Output 3 */
178#define ADC_CR2_EXTSEL_TIM2_CC3 (0x1 << 17)
179/** Timer 1 Compare Output 3 */
180#define ADC_CR2_EXTSEL_TIM1_CC3 (0x2 << 17)
181/** Timer 8 Compare Output 1 */
182#define ADC_CR2_EXTSEL_TIM8_CC1 (0x3 << 17)
183/** Timer 8 Trigger Output */
184#define ADC_CR2_EXTSEL_TIM8_TRGO (0x4 << 17)
185/** Timer 5 Compare Output 1 */
186#define ADC_CR2_EXTSEL_TIM5_CC1 (0x5 << 17)
187/** Timer 5 Compare Output 3 */
188#define ADC_CR2_EXTSEL_TIM5_CC3 (0x6 << 17)
189/**@}*/
190
191#define ADC_CR2_EXTSEL_MASK (0x7 << 17)
192#define ADC_CR2_EXTSEL_SHIFT 17
193
194/* Note: Bit 16 is reserved, must be kept at reset value. */
195
196/* JEXTTRIG: External trigger conversion mode for injected channels. */
197#define ADC_CR2_JEXTTRIG (1 << 15)
198
199/* JEXTSEL[2:0]: External event selection for injected group. */
200/* The following are only valid for ADC1 and ADC2. */
201/****************************************************************************/
202/* ADC_CR2 JEXTSEL[2:0] ADC Injected Trigger Identifier for ADC1 and ADC2 */
203/** @defgroup adc_trigger_injected_12 ADC Injected Trigger Identifier for ADC1
204and ADC2
205@ingroup adc_defines
206
207@{*/
208/** Timer 1 Trigger Output */
209#define ADC_CR2_JEXTSEL_TIM1_TRGO (0x0 << 12)
210/** Timer 1 Compare Output 4 */
211#define ADC_CR2_JEXTSEL_TIM1_CC4 (0x1 << 12)
212/** Timer 2 Trigger Output */
213#define ADC_CR2_JEXTSEL_TIM2_TRGO (0x2 << 12)
214/** Timer 2 Compare Output 1 */
215#define ADC_CR2_JEXTSEL_TIM2_CC1 (0x3 << 12)
216/** Timer 3 Compare Output 4 */
217#define ADC_CR2_JEXTSEL_TIM3_CC4 (0x4 << 12)
218/** Timer 4 Trigger Output */
219#define ADC_CR2_JEXTSEL_TIM4_TRGO (0x5 << 12)
220/** External Interrupt 15 */
221#define ADC_CR2_JEXTSEL_EXTI15 (0x6 << 12)
222/** Injected Software Trigger */
223#define ADC_CR2_JEXTSEL_JSWSTART (0x7 << 12) /* Software start. */
224/**@}*/
225
226/* The following are the different meanings for ADC3 only. */
227/****************************************************************************/
228/* ADC_CR2 JEXTSEL[2:0] ADC Injected Trigger Identifier for ADC3 */
229/** @defgroup adc_trigger_injected_3 ADC Injected Trigger Identifier for ADC3
230@ingroup adc_defines
231
232@{*/
233/** Timer 1 Trigger Output */
234#define ADC_CR2_JEXTSEL_TIM1_TRGO (0x0 << 12)
235/** Timer 1 Compare Output 4 */
236#define ADC_CR2_JEXTSEL_TIM1_CC4 (0x1 << 12)
237/** Timer 4 Compare Output 3 */
238#define ADC_CR2_JEXTSEL_TIM4_CC3 (0x2 << 12)
239/** Timer 8 Compare Output 2 */
240#define ADC_CR2_JEXTSEL_TIM8_CC2 (0x3 << 12)
241/** Timer 8 Compare Output 4 */
242#define ADC_CR2_JEXTSEL_TIM8_CC4 (0x4 << 12)
243/** Timer 5 Trigger Output */
244#define ADC_CR2_JEXTSEL_TIM5_TRGO (0x5 << 12)
245/** Timer 5 Compare Output 4 */
246#define ADC_CR2_JEXTSEL_TIM5_CC4 (0x6 << 12)
247/** Injected Software Trigger */
248#define ADC_CR2_JEXTSEL_JSWSTART (0x7 << 12) /* Software start. */
249/**@}*/
250
251#define ADC_CR2_JEXTSEL_MASK (0x7 << 12)
252#define ADC_CR2_JEXTSEL_SHIFT 12
253
254/* ALIGN: Data alignment. */
255#define ADC_CR2_ALIGN_RIGHT (0 << 11)
256#define ADC_CR2_ALIGN_LEFT (1 << 11)
257#define ADC_CR2_ALIGN (1 << 11)
258
259/* Note: Bits [10:9] are reserved and must be kept at reset value. */
260
261/* DMA: Direct memory access mode. (ADC1 and ADC3 only!) */
262#define ADC_CR2_DMA (1 << 8)
263
264/* Note: Bits [7:4] are reserved and must be kept at reset value. */
265
266/* RSTCAL: Reset calibration. */
267#define ADC_CR2_RSTCAL (1 << 3)
268
269/* CAL: A/D Calibration. */
270#define ADC_CR2_CAL (1 << 2)
271
272/* CONT: Continuous conversion. */
273#define ADC_CR2_CONT (1 << 1)
274
275/* ADON: A/D converter On/Off. */
276/* Note: If any other bit in this register apart from ADON is changed at the
277 * same time, then conversion is not triggered. This is to prevent triggering
278 * an erroneous conversion.
279 * Conclusion: Must be separately written.
280 */
281#define ADC_CR2_ADON (1 << 0)
282
283/* --- ADC_SMPR1 values ---------------------------------------------------- */
284#define ADC_SMPR1_SMP17_LSB 21
285#define ADC_SMPR1_SMP16_LSB 18
286#define ADC_SMPR1_SMP15_LSB 15
287#define ADC_SMPR1_SMP14_LSB 12
288#define ADC_SMPR1_SMP13_LSB 9
289#define ADC_SMPR1_SMP12_LSB 6
290#define ADC_SMPR1_SMP11_LSB 3
291#define ADC_SMPR1_SMP10_LSB 0
292#define ADC_SMPR1_SMP17_MSK (0x7 << ADC_SMPR1_SMP17_LSB)
293#define ADC_SMPR1_SMP16_MSK (0x7 << ADC_SMPR1_SMP16_LSB)
294#define ADC_SMPR1_SMP15_MSK (0x7 << ADC_SMPR1_SMP15_LSB)
295#define ADC_SMPR1_SMP14_MSK (0x7 << ADC_SMPR1_SMP14_LSB)
296#define ADC_SMPR1_SMP13_MSK (0x7 << ADC_SMPR1_SMP13_LSB)
297#define ADC_SMPR1_SMP12_MSK (0x7 << ADC_SMPR1_SMP12_LSB)
298#define ADC_SMPR1_SMP11_MSK (0x7 << ADC_SMPR1_SMP11_LSB)
299#define ADC_SMPR1_SMP10_MSK (0x7 << ADC_SMPR1_SMP10_LSB)
300
301/* --- ADC_SMPR2 values ---------------------------------------------------- */
302
303#define ADC_SMPR2_SMP9_LSB 27
304#define ADC_SMPR2_SMP8_LSB 24
305#define ADC_SMPR2_SMP7_LSB 21
306#define ADC_SMPR2_SMP6_LSB 18
307#define ADC_SMPR2_SMP5_LSB 15
308#define ADC_SMPR2_SMP4_LSB 12
309#define ADC_SMPR2_SMP3_LSB 9
310#define ADC_SMPR2_SMP2_LSB 6
311#define ADC_SMPR2_SMP1_LSB 3
312#define ADC_SMPR2_SMP0_LSB 0
313#define ADC_SMPR2_SMP9_MSK (0x7 << ADC_SMPR2_SMP9_LSB)
314#define ADC_SMPR2_SMP8_MSK (0x7 << ADC_SMPR2_SMP8_LSB)
315#define ADC_SMPR2_SMP7_MSK (0x7 << ADC_SMPR2_SMP7_LSB)
316#define ADC_SMPR2_SMP6_MSK (0x7 << ADC_SMPR2_SMP6_LSB)
317#define ADC_SMPR2_SMP5_MSK (0x7 << ADC_SMPR2_SMP5_LSB)
318#define ADC_SMPR2_SMP4_MSK (0x7 << ADC_SMPR2_SMP4_LSB)
319#define ADC_SMPR2_SMP3_MSK (0x7 << ADC_SMPR2_SMP3_LSB)
320#define ADC_SMPR2_SMP2_MSK (0x7 << ADC_SMPR2_SMP2_LSB)
321#define ADC_SMPR2_SMP1_MSK (0x7 << ADC_SMPR2_SMP1_LSB)
322#define ADC_SMPR2_SMP0_MSK (0x7 << ADC_SMPR2_SMP0_LSB)
323
324/* --- ADC_SMPRx values --------------------------------------------------- */
325/****************************************************************************/
326/* ADC_SMPRG ADC Sample Time Selection for Channels */
327/** @defgroup adc_sample_rg ADC Sample Time Selection for All Channels
328@ingroup adc_defines
329
330@{*/
331#define ADC_SMPR_SMP_1DOT5CYC 0x0
332#define ADC_SMPR_SMP_7DOT5CYC 0x1
333#define ADC_SMPR_SMP_13DOT5CYC 0x2
334#define ADC_SMPR_SMP_28DOT5CYC 0x3
335#define ADC_SMPR_SMP_41DOT5CYC 0x4
336#define ADC_SMPR_SMP_55DOT5CYC 0x5
337#define ADC_SMPR_SMP_71DOT5CYC 0x6
338#define ADC_SMPR_SMP_239DOT5CYC 0x7
339/**@}*/
340
341
342/* --- ADC_SQR1 values ----------------------------------------------------- */
343
344#define ADC_SQR_MAX_CHANNELS_REGULAR 16
345
346#define ADC_SQR1_SQ16_LSB 15
347#define ADC_SQR1_SQ15_LSB 10
348#define ADC_SQR1_SQ14_LSB 5
349#define ADC_SQR1_SQ13_LSB 0
350#define ADC_SQR1_L_MSK (0xf << ADC_SQR1_L_LSB)
351#define ADC_SQR1_SQ16_MSK (0x1f << ADC_SQR1_SQ16_LSB)
352#define ADC_SQR1_SQ15_MSK (0x1f << ADC_SQR1_SQ15_LSB)
353#define ADC_SQR1_SQ14_MSK (0x1f << ADC_SQR1_SQ14_LSB)
354#define ADC_SQR1_SQ13_MSK (0x1f << ADC_SQR1_SQ13_LSB)
355
356/* --- ADC_SQR2 values ----------------------------------------------------- */
357
358#define ADC_SQR2_SQ12_LSB 25
359#define ADC_SQR2_SQ11_LSB 20
360#define ADC_SQR2_SQ10_LSB 15
361#define ADC_SQR2_SQ9_LSB 10
362#define ADC_SQR2_SQ8_LSB 5
363#define ADC_SQR2_SQ7_LSB 0
364#define ADC_SQR2_SQ12_MSK (0x1f << ADC_SQR2_SQ12_LSB)
365#define ADC_SQR2_SQ11_MSK (0x1f << ADC_SQR2_SQ11_LSB)
366#define ADC_SQR2_SQ10_MSK (0x1f << ADC_SQR2_SQ10_LSB)
367#define ADC_SQR2_SQ9_MSK (0x1f << ADC_SQR2_SQ9_LSB)
368#define ADC_SQR2_SQ8_MSK (0x1f << ADC_SQR2_SQ8_LSB)
369#define ADC_SQR2_SQ7_MSK (0x1f << ADC_SQR2_SQ7_LSB)
370
371/* --- ADC_SQR3 values ----------------------------------------------------- */
372
373#define ADC_SQR3_SQ6_LSB 25
374#define ADC_SQR3_SQ5_LSB 20
375#define ADC_SQR3_SQ4_LSB 15
376#define ADC_SQR3_SQ3_LSB 10
377#define ADC_SQR3_SQ2_LSB 5
378#define ADC_SQR3_SQ1_LSB 0
379#define ADC_SQR3_SQ6_MSK (0x1f << ADC_SQR3_SQ6_LSB)
380#define ADC_SQR3_SQ5_MSK (0x1f << ADC_SQR3_SQ5_LSB)
381#define ADC_SQR3_SQ4_MSK (0x1f << ADC_SQR3_SQ4_LSB)
382#define ADC_SQR3_SQ3_MSK (0x1f << ADC_SQR3_SQ3_LSB)
383#define ADC_SQR3_SQ2_MSK (0x1f << ADC_SQR3_SQ2_LSB)
384#define ADC_SQR3_SQ1_MSK (0x1f << ADC_SQR3_SQ1_LSB)
385
386/* --- ADC_JDRx, ADC_DR values --------------------------------------------- */
387
388#define ADC_JDATA_LSB 0
389#define ADC_DATA_LSB 0
390#define ADC_ADC2DATA_LSB 16 /* ADC1 only (dual mode) */
391#define ADC_JDATA_MSK (0xffff << ADC_JDATA_LSB)
392#define ADC_DATA_MSK (0xffff << ADC_DA)
393#define ADC_ADC2DATA_MSK (0xffff << ADC_ADC2DATA_LSB)
394 /* ADC1 only (dual mode) */
395
396/** @defgroup adc_channel ADC Channel Numbers
397 * @ingroup adc_defines
398 *
399 *@{*/
400#define ADC_CHANNEL_TEMP 16
401#define ADC_CHANNEL_VREF 17
402/**@}*/
403
404/* --- Function prototypes ------------------------------------------------- */
405
407
408void adc_start_conversion_direct(uint32_t adc);
409void adc_set_dual_mode(uint32_t mode);
412void adc_enable_external_trigger_regular(uint32_t adc, uint32_t trigger);
413void adc_enable_external_trigger_injected(uint32_t adc, uint32_t trigger);
414void adc_reset_calibration(uint32_t adc);
415void adc_calibration(uint32_t adc)
416 LIBOPENCM3_DEPRECATED("see adc_calibrate/_async");
417void adc_calibrate_async(uint32_t adc);
418bool adc_is_calibrating(uint32_t adc);
419void adc_calibrate(uint32_t adc);
420
422
423#endif
424/**@}*/
425
#define END_DECLS
Definition: common.h:34
#define LIBOPENCM3_DEPRECATED(x)
Definition: common.h:46
#define BEGIN_DECLS
Definition: common.h:33
void adc_disable_temperature_sensor(void)
ADC Disable The Temperature Sensor.
Definition: adc.c:198
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) LIBOPENCM3_DEPRECATED("see adc_calibrate/_async")
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_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_enable_temperature_sensor(void)
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