px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_adc.h
1#ifndef __PX_ADC_H__
2#define __PX_ADC_H__
3/* =============================================================================
4 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
5 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
6 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
7 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
8 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
9
10 Copyright (c) 2018 Pieter Conradie <https://piconomix.com>
11
12 License: MIT
13 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
14
15 Title: px_adc.h : ADC peripheral driver
16 Author(s): Pieter Conradie
17 Creation Date: 2018-11-04
18
19============================================================================= */
20
21/**
22 * @ingroup STM32
23 * @defgroup STM32_ADC px_adc.h : ADC peripheral driver
24 *
25 * Driver to perform analogue measurements.
26 *
27 * File(s):
28 * - arch/arm/stm32/inc/px_adc.h
29 * - arch/arm/stm32/inc/px_adc_cfg_template.h
30 * - arch/arm/stm32/src/px_adc.c
31 *
32 * The driver must be configured by supplying a project specific "px_adc_cfg.h".
33 * "px_adc_cfg_template.h" can be copied, renamed and modified to supply
34 * compile time options.
35 *
36 * @par Example:
37 * @include arch/arm/stm32/test/px_adc_test.c
38 *
39 * @{
40 */
41
42/* _____STANDARD INCLUDES____________________________________________________ */
43
44/* _____PROJECT INCLUDES_____________________________________________________ */
45#include "px_defs.h"
46
47// Include project specific configuration. See "px_adc_cfg_template.h"
48#include "px_adc_cfg.h"
49
50// Check that all project specific options have been specified in "px_adc_cfg.h"
51#if ( !defined(PX_ADC_CFG_ADC1_EN ) \
52 || !defined(PX_ADC_CFG_CLK ) \
53 || !defined(PX_ADC_CFG_SAMPLE_TIME ) \
54 || !defined(PX_ADC_CFG_RES ) \
55 || !defined(PX_ADC_CFG_OVERSAMPLING ) \
56 || !defined(PX_ADC_CFG_OVERSAMPLING_SHIFT) )
57#error "One or more options not defined in 'px_adc_cfg.h'"
58#endif
59#if (PX_ADC_CFG_ADC1_EN > 1)
60#error "PX_ADC_CFG_ADCx_EN must be 0 or 1"
61#endif
62
63/// Number of enabled peripherals
64#define PX_ADC_CFG_PER_COUNT (PX_ADC_CFG_ADC1_EN)
65#if (PX_ADC_CFG_PER_COUNT == 0)
66#error "No peripherals enabled"
67#endif
68
69#ifdef __cplusplus
70extern "C" {
71#endif
72/* _____DEFINITIONS__________________________________________________________ */
73/// Specify ADC peripheral
74typedef enum
75{
76 PX_ADC_NR_1 = 1,
78
79/// Specify ADC channel
80typedef enum
81{
82 PX_ADC_CH0 = 0, ///< ADC Channel 0
83 PX_ADC_CH1 = 1, ///< ADC Channel 1
84 PX_ADC_CH2 = 2, ///< ADC Channel 2
85 PX_ADC_CH3 = 3, ///< ADC Channel 3
86 PX_ADC_CH4 = 4, ///< ADC Channel 4
87 PX_ADC_CH5 = 5, ///< ADC Channel 5
88 PX_ADC_CH6 = 6, ///< ADC Channel 6
89 PX_ADC_CH7 = 7, ///< ADC Channel 7
90 PX_ADC_CH8 = 8, ///< ADC Channel 8
91 PX_ADC_CH9 = 9, ///< ADC Channel 9
92 PX_ADC_CH10 = 10, ///< ADC Channel 10
93 PX_ADC_CH11 = 11, ///< ADC Channel 11
94 PX_ADC_CH12 = 12, ///< ADC Channel 12
95 PX_ADC_CH13 = 13, ///< ADC Channel 13
96 PX_ADC_CH14 = 14, ///< ADC Channel 14
97 PX_ADC_CH15 = 15, ///< ADC Channel 15
98 PX_ADC_CH16 = 16,
99 PX_ADC_CH17 = 17, ///< Internal Voltage Reference (Vrefint)
100 PX_ADC_CH18 = 18, ///< Temperature sensor (Tsense)
102
103/// Specify ADC peripheral clock source
104typedef enum
105{
106 PX_ADC_CFG_CLK_PCLK = 0, ///< Peripheral Clock
107 PX_ADC_CFG_CLK_PCLK_DIV2 = 1, ///< Peripheral Clock / 2
108 PX_ADC_CFG_CLK_PCLK_DIV4 = 2, ///< Peripheral Clock / 4
110
111/// Specify ADC sampling time
112typedef enum
113{
114 PX_ADC_CFG_SAMPLE_TIME_1_5 = 0, ///< 1.5 ADC clock cycles
115 PX_ADC_CFG_SAMPLE_TIME_3_5 = 1, ///< 3.5 ADC clock cycles
116 PX_ADC_CFG_SAMPLE_TIME_7_5 = 2, ///< 7.5 ADC clock cycles
117 PX_ADC_CFG_SAMPLE_TIME_12_5 = 3, ///< 12.5 ADC clock cycles
118 PX_ADC_CFG_SAMPLE_TIME_19_5 = 4, ///< 19.5 ADC clock cycles
119 PX_ADC_CFG_SAMPLE_TIME_39_5 = 5, ///< 39.5 ADC clock cycles
120 PX_ADC_CFG_SAMPLE_TIME_79_5 = 6, ///< 79.5 ADC clock cycles
121 PX_ADC_CFG_SAMPLE_TIME_160_5 = 7, ///< 160.5 ADC clock cycles
123
124/// Specify ADC resolution
125typedef enum
126{
127 PX_ADC_CFG_RES_12_BITS = 0, ///< 12-bit resolution
128 PX_ADC_CFG_RES_10_BITS = 1, ///< 10-bit resolution
129 PX_ADC_CFG_RES_8_BITS = 2, ///< 8-bit resolution
130 PX_ADC_CFG_RES_6_BITS = 3, ///< 6-bit resolution
132
133/// Specify ADC oversampling
134typedef enum
135{
136 PX_ADC_CFG_OVERSAMPLING_NONE = 0, ///< No oversampling
137 PX_ADC_CFG_OVERSAMPLING_2 = 1, ///< 2 x oversampling
138 PX_ADC_CFG_OVERSAMPLING_4 = 2, ///< 4 x oversampling
139 PX_ADC_CFG_OVERSAMPLING_8 = 3, ///< 8 x oversampling
140 PX_ADC_CFG_OVERSAMPLING_16 = 4, ///< 16 x oversampling
141 PX_ADC_CFG_OVERSAMPLING_32 = 5, ///< 32 x oversampling
142 PX_ADC_CFG_OVERSAMPLING_64 = 6, ///< 64 x oversampling
143 PX_ADC_CFG_OVERSAMPLING_128 = 7, ///< 128 x oversampling
144 PX_ADC_CFG_OVERSAMPLING_256 = 8, ///< 256 x oversampling
146
147/// Specify ADC oversampling
148typedef enum
149{
152 PX_ADC_CFG_OVERSAMPLING_SHIFT_2 = 2, ///< Shift 2-bits
153 PX_ADC_CFG_OVERSAMPLING_SHIFT_3 = 3, ///< Shift 3-bits
154 PX_ADC_CFG_OVERSAMPLING_SHIFT_4 = 4, ///< Shift 4-bits
155 PX_ADC_CFG_OVERSAMPLING_SHIFT_5 = 5, ///< Shift 5-bits
156 PX_ADC_CFG_OVERSAMPLING_SHIFT_6 = 6, ///< Shift 6-bits
157 PX_ADC_CFG_OVERSAMPLING_SHIFT_7 = 7, ///< Shift 7-bits
158 PX_ADC_CFG_OVERSAMPLING_SHIFT_8 = 8, ///< Shift 8-bits
160
161/// Define ADC handle
162typedef struct
163{
164 struct px_adc_per_s * adc_per; ///< ADC peripheral data
166
167/* _____TYPE DEFINITIONS_____________________________________________________ */
168
169/* _____GLOBAL VARIABLES_____________________________________________________ */
170
171/* _____GLOBAL FUNCTION DECLARATIONS_________________________________________ */
172/**
173 * Initialise ADC driver.
174 */
175void px_adc_init(void);
176
177/**
178 * Open ADC peripheral using predefined (default) parameters.
179 *
180 * @param handle Pointer to handle data structure
181 * @param adc_nr ADC peripheral number
182 *
183 * @retval false Error: peripheral was not opened
184 * @retval true Success: peripheral was opened
185 */
186bool px_adc_open(px_adc_handle_t * handle,
187 px_adc_nr_t adc_nr);
188
189/**
190 * Close specified device.
191 *
192 * @param handle Pointer to handle data structure
193 *
194 * @retval true Success
195 * @retval false Specified device was already closed (or not opened)
196 */
197bool px_adc_close(px_adc_handle_t * handle);
198
199/**
200 * Perform a single measurement on the specified ADC channel
201 *
202 * @param handle Pointer to handle data structure
203 * @param ch Specified ADC channel
204 *
205 * @return uint16_t Raw ADC value
206 */
207uint16_t px_adc_sample(px_adc_handle_t * handle,
208 px_adc_ch_t ch);
209
210/* _____MACROS_______________________________________________________________ */
211/// Convert ADC channel number into bit mask
212#define PX_ADC_CH_TO_BITMASK(ch) ((uint32_t)1 << (ch))
213
214#ifdef __cplusplus
215}
216#endif
217
218/// @}
219#endif
struct px_adc_per_s * adc_per
ADC peripheral data.
Definition: px_adc.h:164
px_adc_cfg_sample_time_t
Specify ADC sampling time.
Definition: px_adc.h:113
px_adc_cfg_oversampling_shift_t
Specify ADC oversampling.
Definition: px_adc.h:149
px_adc_cfg_oversampling_t
Specify ADC oversampling.
Definition: px_adc.h:135
void px_adc_init(void)
Initialise ADC driver.
Definition: px_adc.c:128
px_adc_nr_t
Specify ADC peripheral.
Definition: px_adc.h:75
px_adc_cfg_res_t
Specify ADC resolution.
Definition: px_adc.h:126
px_adc_ch_t
Specify ADC channel.
Definition: px_adc.h:81
bool px_adc_close(px_adc_handle_t *handle)
Close specified device.
Definition: px_adc.c:181
bool px_adc_open(px_adc_handle_t *handle, px_adc_nr_t adc_nr)
Open ADC peripheral using predefined (default) parameters.
Definition: px_adc.c:136
px_adc_cfg_clk_t
Specify ADC peripheral clock source.
Definition: px_adc.h:105
uint16_t px_adc_sample(px_adc_handle_t *handle, px_adc_ch_t ch)
Perform a single measurement on the specified ADC channel.
Definition: px_adc.c:235
@ PX_ADC_CFG_SAMPLE_TIME_160_5
160.5 ADC clock cycles
Definition: px_adc.h:121
@ PX_ADC_CFG_SAMPLE_TIME_3_5
3.5 ADC clock cycles
Definition: px_adc.h:115
@ PX_ADC_CFG_SAMPLE_TIME_7_5
7.5 ADC clock cycles
Definition: px_adc.h:116
@ PX_ADC_CFG_SAMPLE_TIME_1_5
1.5 ADC clock cycles
Definition: px_adc.h:114
@ PX_ADC_CFG_SAMPLE_TIME_39_5
39.5 ADC clock cycles
Definition: px_adc.h:119
@ PX_ADC_CFG_SAMPLE_TIME_19_5
19.5 ADC clock cycles
Definition: px_adc.h:118
@ PX_ADC_CFG_SAMPLE_TIME_79_5
79.5 ADC clock cycles
Definition: px_adc.h:120
@ PX_ADC_CFG_SAMPLE_TIME_12_5
12.5 ADC clock cycles
Definition: px_adc.h:117
@ PX_ADC_CFG_OVERSAMPLING_SHIFT_4
Shift 4-bits.
Definition: px_adc.h:154
@ PX_ADC_CFG_OVERSAMPLING_SHIFT_1
Shift 1-bit.
Definition: px_adc.h:151
@ PX_ADC_CFG_OVERSAMPLING_SHIFT_8
Shift 8-bits.
Definition: px_adc.h:158
@ PX_ADC_CFG_OVERSAMPLING_SHIFT_7
Shift 7-bits.
Definition: px_adc.h:157
@ PX_ADC_CFG_OVERSAMPLING_SHIFT_2
Shift 2-bits.
Definition: px_adc.h:152
@ PX_ADC_CFG_OVERSAMPLING_SHIFT_6
Shift 6-bits.
Definition: px_adc.h:156
@ PX_ADC_CFG_OVERSAMPLING_SHIFT_5
Shift 5-bits.
Definition: px_adc.h:155
@ PX_ADC_CFG_OVERSAMPLING_SHIFT_NONE
No shift.
Definition: px_adc.h:150
@ PX_ADC_CFG_OVERSAMPLING_SHIFT_3
Shift 3-bits.
Definition: px_adc.h:153
@ PX_ADC_CFG_OVERSAMPLING_64
64 x oversampling
Definition: px_adc.h:142
@ PX_ADC_CFG_OVERSAMPLING_4
4 x oversampling
Definition: px_adc.h:138
@ PX_ADC_CFG_OVERSAMPLING_8
8 x oversampling
Definition: px_adc.h:139
@ PX_ADC_CFG_OVERSAMPLING_16
16 x oversampling
Definition: px_adc.h:140
@ PX_ADC_CFG_OVERSAMPLING_2
2 x oversampling
Definition: px_adc.h:137
@ PX_ADC_CFG_OVERSAMPLING_32
32 x oversampling
Definition: px_adc.h:141
@ PX_ADC_CFG_OVERSAMPLING_256
256 x oversampling
Definition: px_adc.h:144
@ PX_ADC_CFG_OVERSAMPLING_128
128 x oversampling
Definition: px_adc.h:143
@ PX_ADC_CFG_OVERSAMPLING_NONE
No oversampling.
Definition: px_adc.h:136
@ PX_ADC_CFG_RES_12_BITS
12-bit resolution
Definition: px_adc.h:127
@ PX_ADC_CFG_RES_6_BITS
6-bit resolution
Definition: px_adc.h:130
@ PX_ADC_CFG_RES_10_BITS
10-bit resolution
Definition: px_adc.h:128
@ PX_ADC_CFG_RES_8_BITS
8-bit resolution
Definition: px_adc.h:129
@ PX_ADC_CH0
ADC Channel 0.
Definition: px_adc.h:82
@ PX_ADC_CH5
ADC Channel 5.
Definition: px_adc.h:87
@ PX_ADC_CH15
ADC Channel 15.
Definition: px_adc.h:97
@ PX_ADC_CH18
Temperature sensor (Tsense)
Definition: px_adc.h:100
@ PX_ADC_CH3
ADC Channel 3.
Definition: px_adc.h:85
@ PX_ADC_CH8
ADC Channel 8.
Definition: px_adc.h:90
@ PX_ADC_CH1
ADC Channel 1.
Definition: px_adc.h:83
@ PX_ADC_CH12
ADC Channel 12.
Definition: px_adc.h:94
@ PX_ADC_CH10
ADC Channel 10.
Definition: px_adc.h:92
@ PX_ADC_CH17
Internal Voltage Reference (Vrefint)
Definition: px_adc.h:99
@ PX_ADC_CH2
ADC Channel 2.
Definition: px_adc.h:84
@ PX_ADC_CH13
ADC Channel 13.
Definition: px_adc.h:95
@ PX_ADC_CH11
ADC Channel 11.
Definition: px_adc.h:93
@ PX_ADC_CH6
ADC Channel 6.
Definition: px_adc.h:88
@ PX_ADC_CH7
ADC Channel 7.
Definition: px_adc.h:89
@ PX_ADC_CH4
ADC Channel 4.
Definition: px_adc.h:86
@ PX_ADC_CH9
ADC Channel 9.
Definition: px_adc.h:91
@ PX_ADC_CH14
ADC Channel 14.
Definition: px_adc.h:96
@ PX_ADC_CFG_CLK_PCLK_DIV2
Peripheral Clock / 2.
Definition: px_adc.h:107
@ PX_ADC_CFG_CLK_PCLK
Peripheral Clock.
Definition: px_adc.h:106
@ PX_ADC_CFG_CLK_PCLK_DIV4
Peripheral Clock / 4.
Definition: px_adc.h:108
Define ADC handle.
Definition: px_adc.h:163