aboutsummaryrefslogtreecommitdiffstats
path: root/libmaple/stm32f2/include/series/adc.h
blob: 175fe112f15e34623145b4d95d20ca5b4631eca0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/******************************************************************************
 * The MIT License
 *
 * Copyright (c) 2012 LeafLabs, LLC.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *****************************************************************************/

/**
 * @file libmaple/stm32f2/include/series/adc.h
 * @author Marti Bolivar <mbolivar@leaflabs.com>,
 * @brief STM32F2 ADC support.
 */

#ifndef _LIBMAPLE_STM32F2_ADC_H_
#define _LIBMAPLE_STM32F2_ADC_H_

#include <libmaple/libmaple_types.h>

/*
 * Devices
 */

extern const struct adc_dev *ADC1;
extern const struct adc_dev *ADC2;
extern const struct adc_dev *ADC3;

/*
 * Common register map
 */

/** ADC common register map type */
typedef struct adc_common_reg_map {
    __io uint32 CSR;            /**< Common status register */
    __io uint32 CCR;            /**< Common control register */
    __io uint32 CDR;            /**<
                                 * @brief Common regular data register
                                 *        for dual and triple modes */
} adc_common_reg_map;

/*
 * Register map base pointers
 */

/** ADC1 register map base pointer. */
#define ADC1_BASE       ((struct adc_reg_map*)0x40012000)
/** ADC2 register map base pointer. */
#define ADC2_BASE       ((struct adc_reg_map*)0x40012100)
/** ADC3 register map base pointer. */
#define ADC3_BASE       ((struct adc_reg_map*)0x40012200)
/** ADC common register map base pointer. */
#define ADC_COMMON_BASE ((struct adc_common_reg_map*)0x40012300)

/*
 * Register bit definitions
 */

/* Status register */

/** Overrun bit. */
#define ADC_SR_OVR_BIT                  5
/** Overrun. */
#define ADC_SR_OVR                      (1U << ADC_SR_OVR_BIT)

/* Control register 1 */

/** Overrun interrupt enable bit. */
#define ADC_CR1_OVRIE_BIT               26

/** Overrun interrupt error enable. */
#define ADC_CR1_OVRIE                   (1U << ADC_CR1_OVRIE_BIT)
/** Conversion resolution. */
#define ADC_CR1_RES                     (0x3U << 24)
/** Conversion resolution: 12 bit (at least 15 ADCCLK cycles). */
#define ADC_CR1_RES_12BIT               (0x0U << 24)
/** Conversion resolution: 10 bit (at least 13 ADCCLK cycles). */
#define ADC_CR1_RES_10BIT               (0x1U << 24)
/** Conversion resolution: 8 bit (at least 11 ADCCLK cycles). */
#define ADC_CR1_RES_8BIT                (0x2U << 24)
/** Conversion resolution: 6 bit (at least 9 ADCCLK cycles). */
#define ADC_CR1_RES_6BIT                (0x3U << 24)

/* Control register 2 */

#define ADC_CR2_SWSTART_BIT             30
#define ADC_CR2_JSWSTART_BIT            22
#define ADC_CR2_ALIGN_BIT               11
#define ADC_CR2_EOCS_BIT                10
#define ADC_CR2_DDS_BIT                 9
#define ADC_CR2_DMA_BIT                 8
#define ADC_CR2_CONT_BIT                1
#define ADC_CR2_ADON_BIT                0

#define ADC_CR2_SWSTART                 (1U << ADC_CR2_SWSTART_BIT)
#define ADC_CR2_EXTEN                   (0x3 << 28)
#define ADC_CR2_EXTEN_DISABLED          (0x0 << 28)
#define ADC_CR2_EXTEN_RISE              (0x1 << 28)
#define ADC_CR2_EXTEN_FALL              (0x2 << 28)
#define ADC_CR2_EXTEN_RISE_FALL         (0x3 << 28)
#define ADC_CR2_EXTSEL                  (0xF << 24)
#define ADC_CR2_EXTSEL_TIM1_CC1         (0x0 << 24)
#define ADC_CR2_EXTSEL_TIM1_CC2         (0x1 << 24)
#define ADC_CR2_EXTSEL_TIM1_CC3         (0x2 << 24)
#define ADC_CR2_EXTSEL_TIM2_CC2         (0x3 << 24)
#define ADC_CR2_EXTSEL_TIM2_CC3         (0x4 << 24)
#define ADC_CR2_EXTSEL_TIM2_CC4         (0x5 << 24)
#define ADC_CR2_EXTSEL_TIM1_TRGO        (0x6 << 24)
#define ADC_CR2_EXTSEL_TIM3_CC1         (0x7 << 24)
#define ADC_CR2_EXTSEL_TIM3_TRGO        (0x8 << 24)
#define ADC_CR2_EXTSEL_TIM4_CC4         (0x9 << 24)
#define ADC_CR2_EXTSEL_TIM5_CC1         (0xA << 24)
#define ADC_CR2_EXTSEL_TIM5_CC2         (0xB << 24)
#define ADC_CR2_EXTSEL_TIM5_CC3         (0xC << 24)
#define ADC_CR2_EXTSEL_TIM8_CC1         (0xD << 24)
#define ADC_CR2_EXTSEL_TIM8_TRGO        (0xE << 24)
#define ADC_CR2_EXTSEL_TIM1_EXTI11      (0xF << 24)
#define ADC_CR2_JSWSTART                (1U << ADC_CR2_JSWSTART_BIT)
#define ADC_CR2_JEXTEN                  (0x3 << 20)
#define ADC_CR2_JEXTEN_DISABLED         (0x0 << 20)
#define ADC_CR2_JEXTEN_RISE             (0x1 << 20)
#define ADC_CR2_JEXTEN_FALL             (0x2 << 20)
#define ADC_CR2_JEXTEN_RISE_FALL        (0x3 << 20)
#define ADC_CR2_JEXTSEL                 (0xF << 16)
#define ADC_CR2_JEXTSEL_TIM1_CC4        (0x0 << 16)
#define ADC_CR2_JEXTSEL_TIM1_TRGO       (0x1 << 16)
#define ADC_CR2_JEXTSEL_TIM2_CC1        (0x2 << 16)
#define ADC_CR2_JEXTSEL_TIM2_TRGO       (0x3 << 16)
#define ADC_CR2_JEXTSEL_TIM3_CC2        (0x4 << 16)
#define ADC_CR2_JEXTSEL_TIM3_CC4        (0x5 << 16)
#define ADC_CR2_JEXTSEL_TIM4_CC1        (0x6 << 16)
#define ADC_CR2_JEXTSEL_TIM4_CC2        (0x7 << 16)
#define ADC_CR2_JEXTSEL_TIM4_CC3        (0x8 << 16)
#define ADC_CR2_JEXTSEL_TIM4_TRGO       (0x9 << 16)
#define ADC_CR2_JEXTSEL_TIM5_CC4        (0xA << 16)
#define ADC_CR2_JEXTSEL_TIM5_TRGO       (0xB << 16)
#define ADC_CR2_JEXTSEL_TIM8_CC2        (0xC << 16)
#define ADC_CR2_JEXTSEL_TIM8_CC3        (0xD << 16)
#define ADC_CR2_JEXTSEL_TIM8_CC4        (0xE << 16)
#define ADC_CR2_JEXTSEL_TIM1_EXTI15     (0xF << 16)
#define ADC_CR2_ALIGN                   (1U << ADC_CR2_ALIGN_BIT)
#define ADC_CR2_ALIGN_RIGHT             (0U << ADC_CR2_ALIGN_BIT)
#define ADC_CR2_ALIGN_LEFT              (1U << ADC_CR2_ALIGN_BIT)
#define ADC_CR2_EOCS                    (1U << ADC_CR2_EOCS_BIT)
#define ADC_CR2_EOCS_SEQUENCE           (0U << ADC_CR2_EOCS_BIT)
#define ADC_CR2_EOCS_CONVERSION         (1U << ADC_CR2_EOCS_BIT)
#define ADC_CR2_DDS                     (1U << ADC_CR2_DDS_BIT)
#define ADC_CR2_DMA                     (1U << ADC_CR2_DMA_BIT)
#define ADC_CR2_CONT                    (1U << ADC_CR2_CONT_BIT)
#define ADC_CR2_ADON                    (1U << ADC_CR2_ADON_BIT)

/* Common status register */

#define ADC_CSR_OVR3_BIT                21
#define ADC_CSR_STRT3_BIT               20
#define ADC_CSR_JSTRT3_BIT              19
#define ADC_CSR_JEOC3_BIT               18
#define ADC_CSR_EOC3_BIT                17
#define ADC_CSR_AWD3_BIT                16
#define ADC_CSR_OVR2_BIT                13
#define ADC_CSR_STRT2_BIT               12
#define ADC_CSR_JSTRT2_BIT              11
#define ADC_CSR_JEOC2_BIT               10
#define ADC_CSR_EOC2_BIT                9
#define ADC_CSR_AWD2_BIT                8
#define ADC_CSR_OVR1_BIT                5
#define ADC_CSR_STRT1_BIT               4
#define ADC_CSR_JSTRT1_BIT              3
#define ADC_CSR_JEOC1_BIT               2
#define ADC_CSR_EOC1_BIT                1
#define ADC_CSR_AWD1_BIT                0

#define ADC_CSR_OVR3                    (1U << ADC_CSR_OVR3_BIT)
#define ADC_CSR_STRT3                   (1U << ADC_CSR_STRT3_BIT)
#define ADC_CSR_JSTRT3                  (1U << ADC_CSR_JSTRT3_BIT)
#define ADC_CSR_JEOC3                   (1U << ADC_CSR_JEOC3_BIT)
#define ADC_CSR_EOC3                    (1U << ADC_CSR_EOC3_BIT)
#define ADC_CSR_AWD3                    (1U << ADC_CSR_AWD3_BIT)
#define ADC_CSR_OVR2                    (1U << ADC_CSR_OVR2_BIT)
#define ADC_CSR_STRT2                   (1U << ADC_CSR_STRT2_BIT)
#define ADC_CSR_JSTRT2                  (1U << ADC_CSR_JSTRT2_BIT)
#define ADC_CSR_JEOC2                   (1U << ADC_CSR_JEOC2_BIT)
#define ADC_CSR_EOC2                    (1U << ADC_CSR_EOC2_BIT)
#define ADC_CSR_AWD2                    (1U << ADC_CSR_AWD2_BIT)
#define ADC_CSR_OVR1                    (1U << ADC_CSR_OVR1_BIT)
#define ADC_CSR_STRT1                   (1U << ADC_CSR_STRT1_BIT)
#define ADC_CSR_JSTRT1                  (1U << ADC_CSR_JSTRT1_BIT)
#define ADC_CSR_JEOC1                   (1U << ADC_CSR_JEOC1_BIT)
#define ADC_CSR_EOC1                    (1U << ADC_CSR_EOC1_BIT)
#define ADC_CSR_AWD1                    (1U << ADC_CSR_AWD1_BIT)

/* Common control register */

#define ADC_CCR_TSVREFE_BIT             23
#define ADC_CCR_VBATE_BIT               22
#define ADC_CCR_DDS_BIT                 13

#define ADC_CCR_TSVREFE                       (1U << ADC_CCR_TSVREFE_BIT)
#define ADC_CCR_VBATE                         (1U << ADC_CCR_VBATE_BIT)
#define ADC_CCR_ADCPRE                        (0x3 << 16)
#define ADC_CCR_ADCPRE_PCLK2_DIV_2            (0x0 << 16)
#define ADC_CCR_ADCPRE_PCLK2_DIV_4            (0x1 << 16)
#define ADC_CCR_ADCPRE_PCLK2_DIV_6            (0x2 << 16)
#define ADC_CCR_ADCPRE_PCLK2_DIV_8            (0x3 << 16)
#define ADC_CCR_DMA                           (0x3 << 14)
#define ADC_CCR_DMA_DIS                       (0x0 << 14)
#define ADC_CCR_DMA_MODE_1                    (0x1 << 14)
#define ADC_CCR_DMA_MODE_2                    (0x2 << 14)
#define ADC_CCR_DMA_MODE_3                    (0x3 << 14)
#define ADC_CCR_DDS                           (1U << ADC_CCR_DDS_BIT)
#define ADC_CCR_DELAY                         (0xF << 8)
#define ADC_CCR_DELAY_5                       (0x0 << 8)
#define ADC_CCR_DELAY_6                       (0x1 << 8)
#define ADC_CCR_DELAY_7                       (0x2 << 8)
#define ADC_CCR_DELAY_8                       (0x3 << 8)
#define ADC_CCR_DELAY_9                       (0x4 << 8)
#define ADC_CCR_DELAY_10                      (0x5 << 8)
#define ADC_CCR_DELAY_11                      (0x6 << 8)
#define ADC_CCR_DELAY_12                      (0x7 << 8)
#define ADC_CCR_DELAY_13                      (0x8 << 8)
#define ADC_CCR_DELAY_14                      (0x9 << 8)
#define ADC_CCR_DELAY_15                      (0xA << 8)
#define ADC_CCR_DELAY_16                      (0xB << 8)
#define ADC_CCR_DELAY_17                      (0xC << 8)
#define ADC_CCR_DELAY_18                      (0xD << 8)
#define ADC_CCR_DELAY_19                      (0xE << 8)
#define ADC_CCR_DELAY_20                      (0xF << 8)
/** Multi ADC mode selection. */
#define ADC_CCR_MULTI                         0x1F
/** All ADCs independent. */
#define ADC_CCR_MULTI_INDEPENDENT             0x0
/** Dual mode: combined regular simultaneous/injected simultaneous. */
#define ADC_CCR_MULTI_DUAL_REG_SIM_INJ_SIM    0x1
/** Dual mode: combined regular simultaneous/alternate trigger. */
#define ADC_CCR_MULTI_DUAL_REG_SIM_ALT_TRIG   0x2
/** Dual mode: injected simultaneous mode only. */
#define ADC_CCR_MULTI_DUAL_INJ_SIM            0x5
/** Dual mode: regular simultaneous mode only. */
#define ADC_CCR_MULTI_DUAL_REG_SIM            0x6
/** Dual mode: interleaved mode only. */
#define ADC_CCR_MULTI_DUAL_INTER              0x7
/** Dual mode: alternate trigger mode only. */
#define ADC_CCR_MULTI_DUAL_ALT_TRIG           0x9
/** Triple mode: combined regular simultaneous/injected simultaneous. */
#define ADC_CCR_MULTI_TRIPLE_REG_SIM_INJ_SIM  0x10
/** Triple mode: combined regular simultaneous/alternate trigger. */
#define ADC_CCR_MULTI_TRIPLE_REG_SIM_ALT_TRIG 0x11
/** Triple mode: injected simultaneous mode only. */
#define ADC_CCR_MULTI_TRIPLE_INJ_SIM          0x12
/** Triple mode: regular simultaneous mode only. */
#define ADC_CCR_MULTI_TRIPLE_REG_SIM          0x15
/** Triple mode: interleaved mode only. */
#define ADC_CCR_MULTI_TRIPLE_INTER            0x17
/** Triple mode: alternate trigger mode only. */
#define ADC_CCR_MULTI_TRIPLE_ALT_TRIG         0x19

/* Common regular data register for dual and triple modes */

#define ADC_CDR_DATA2                   0xFFFF0000
#define ADC_CDR_DATA1                   0xFFFF

/*
 * Other types
 */

/**
 * @brief STM32F2 external event selectors for regular group
 *        conversion.
 * @see adc_set_extsel()
 */
typedef enum adc_extsel_event {
    ADC_EXT_EV_TIM1_CC1    = ADC_CR2_EXTSEL_TIM1_CC1,
    ADC_EXT_EV_TIM1_CC2    = ADC_CR2_EXTSEL_TIM1_CC2,
    ADC_EXT_EV_TIM1_CC3    = ADC_CR2_EXTSEL_TIM1_CC3,
    ADC_EXT_EV_TIM2_CC2    = ADC_CR2_EXTSEL_TIM2_CC2,
    ADC_EXT_EV_TIM2_CC3    = ADC_CR2_EXTSEL_TIM2_CC3,
    ADC_EXT_EV_TIM2_CC4    = ADC_CR2_EXTSEL_TIM2_CC4,
    ADC_EXT_EV_TIM1_TRGO   = ADC_CR2_EXTSEL_TIM1_TRGO,
    ADC_EXT_EV_TIM3_CC1    = ADC_CR2_EXTSEL_TIM3_CC1,
    ADC_EXT_EV_TIM3_TRGO   = ADC_CR2_EXTSEL_TIM3_TRGO,
    ADC_EXT_EV_TIM4_CC4    = ADC_CR2_EXTSEL_TIM4_CC4,
    ADC_EXT_EV_TIM5_CC1    = ADC_CR2_EXTSEL_TIM5_CC1,
    ADC_EXT_EV_TIM5_CC2    = ADC_CR2_EXTSEL_TIM5_CC2,
    ADC_EXT_EV_TIM5_CC3    = ADC_CR2_EXTSEL_TIM5_CC3,
    ADC_EXT_EV_TIM8_CC1    = ADC_CR2_EXTSEL_TIM8_CC1,
    ADC_EXT_EV_TIM8_TRGO   = ADC_CR2_EXTSEL_TIM8_TRGO,
    ADC_EXT_EV_TIM1_EXTI11 = ADC_CR2_EXTSEL_TIM1_EXTI11,
} adc_extsel_event;

/**
 * @brief STM32F2 sample times, in ADC clock cycles.
 */
typedef enum adc_smp_rate {
    ADC_SMPR_3,                 /**< 3 ADC cycles */
    ADC_SMPR_15,                /**< 15 ADC cycles */
    ADC_SMPR_28,                /**< 28 ADC cycles */
    ADC_SMPR_56,                /**< 56 ADC cycles */
    ADC_SMPR_84,                /**< 84 ADC cycles */
    ADC_SMPR_112,               /**< 112 ADC cycles */
    ADC_SMPR_144,               /**< 144 ADC cycles */
    ADC_SMPR_480,               /**< 480 ADC cycles */
} adc_smp_rate;

/**
 * @brief STM32F2 ADC prescalers, as divisors of PCLK2.
 */
typedef enum adc_prescaler {
    /** PCLK2 divided by 2 */
    ADC_PRE_PCLK2_DIV_2 = ADC_CCR_ADCPRE_PCLK2_DIV_2,
    /** PCLK2 divided by 4 */
    ADC_PRE_PCLK2_DIV_4 = ADC_CCR_ADCPRE_PCLK2_DIV_4,
    /** PCLK2 divided by 6 */
    ADC_PRE_PCLK2_DIV_6 = ADC_CCR_ADCPRE_PCLK2_DIV_6,
    /** PCLK2 divided by 8 */
    ADC_PRE_PCLK2_DIV_8 = ADC_CCR_ADCPRE_PCLK2_DIV_8,
} adc_prescaler;

#endif