rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
hal_adc_lld.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2014 Derek Mulcahy
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17/**
18 * @file ADCv1/hal_adc_lld.h
19 * @brief KINETIS ADC subsystem low level driver header.
20 * @author andreika <prometheus.pcb@gmail.com>
21 *
22 * @addtogroup ADC
23 * @{
24 */
25
26#ifndef HAL_ADC_LLD_H_
27#define HAL_ADC_LLD_H_
28
29#if HAL_USE_ADC || defined(__DOXYGEN__)
30
31#include "fsl_adc12.h"
32
33/*===========================================================================*/
34/* Driver constants. */
35/*===========================================================================*/
36
37/**
38 * @name Absolute Maximum Ratings
39 * @{
40 */
41/**
42 * @brief Minimum ADC clock frequency.
43 */
44#define KINETIS_ADCCLK_MIN 600000
45
46/**
47 * @brief Maximum ADC clock frequency.
48 */
49#define KINETIS_ADCCLK_MAX 36000000
50
51#define ADCx_SC1n_ADCH_DISABLED 31
52
53#define ADC_CR2_SWSTART (0x1U << 30U) /*!<Start Conversion of regular channels =0x40000000 */
54
55#define ADC_NUM_CHANNELS 16
56
57/**
58 * @name Sampling rate settings helper macros
59 * @{
60 */
61#define ADC_SMPR2_SMP_AN0(n) ((n) << 0) /**< @brief AN0 sampling time. */
62#define ADC_SMPR2_SMP_AN1(n) ((n) << 3) /**< @brief AN1 sampling time. */
63#define ADC_SMPR2_SMP_AN2(n) ((n) << 6) /**< @brief AN2 sampling time. */
64#define ADC_SMPR2_SMP_AN3(n) ((n) << 9) /**< @brief AN3 sampling time. */
65#define ADC_SMPR2_SMP_AN4(n) ((n) << 12) /**< @brief AN4 sampling time. */
66#define ADC_SMPR2_SMP_AN5(n) ((n) << 15) /**< @brief AN5 sampling time. */
67#define ADC_SMPR2_SMP_AN6(n) ((n) << 18) /**< @brief AN6 sampling time. */
68#define ADC_SMPR2_SMP_AN7(n) ((n) << 21) /**< @brief AN7 sampling time. */
69#define ADC_SMPR2_SMP_AN8(n) ((n) << 24) /**< @brief AN8 sampling time. */
70#define ADC_SMPR2_SMP_AN9(n) ((n) << 27) /**< @brief AN9 sampling time. */
71
72#define ADC_SMPR1_SMP_AN10(n) ((n) << 0) /**< @brief AN10 sampling time. */
73#define ADC_SMPR1_SMP_AN11(n) ((n) << 3) /**< @brief AN11 sampling time. */
74#define ADC_SMPR1_SMP_AN12(n) ((n) << 6) /**< @brief AN12 sampling time. */
75#define ADC_SMPR1_SMP_AN13(n) ((n) << 9) /**< @brief AN13 sampling time. */
76#define ADC_SMPR1_SMP_AN14(n) ((n) << 12) /**< @brief AN14 sampling time. */
77#define ADC_SMPR1_SMP_AN15(n) ((n) << 15) /**< @brief AN15 sampling time. */
78#define ADC_SMPR1_SMP_SENSOR(n) ((n) << 18) /**< @brief Temperature Sensor
79 sampling time. */
80
81/**
82 * @name Available analog channels
83 * @{
84 */
85#define ADC_CHANNEL_IN0 0 /**< @brief External analog input 0. */
86#define ADC_CHANNEL_IN1 1 /**< @brief External analog input 1. */
87#define ADC_CHANNEL_IN2 2 /**< @brief External analog input 2. */
88#define ADC_CHANNEL_IN3 3 /**< @brief External analog input 3. */
89#define ADC_CHANNEL_IN4 4 /**< @brief External analog input 4. */
90#define ADC_CHANNEL_IN5 5 /**< @brief External analog input 5. */
91#define ADC_CHANNEL_IN6 6 /**< @brief External analog input 6. */
92#define ADC_CHANNEL_IN7 7 /**< @brief External analog input 7. */
93#define ADC_CHANNEL_IN8 8 /**< @brief External analog input 8. */
94#define ADC_CHANNEL_IN9 9 /**< @brief External analog input 9. */
95#define ADC_CHANNEL_IN10 10 /**< @brief External analog input 10. */
96#define ADC_CHANNEL_IN11 11 /**< @brief External analog input 11. */
97#define ADC_CHANNEL_IN12 12 /**< @brief External analog input 12. */
98#define ADC_CHANNEL_IN13 13 /**< @brief External analog input 13. */
99#define ADC_CHANNEL_IN14 14 /**< @brief External analog input 14. */
100#define ADC_CHANNEL_IN15 15 /**< @brief External analog input 15. */
101#if 0
102#define ADC_CHANNEL_SENSOR 16 /**< @brief Internal temperature sensor.
103 @note Available onADC1 only. */
104#define ADC_CHANNEL_VREFINT 17 /**< @brief Internal reference.
105 @note Available onADC1 only. */
106#define ADC_CHANNEL_VBAT 18 /**< @brief VBAT.
107 @note Available onADC1 only. */
108#endif
109
110/**
111 * @name Sampling rates
112 * @{
113 */
114#define ADC_SAMPLE_3 0 /**< @brief 3 cycles sampling time. */
115#define ADC_SAMPLE_15 1 /**< @brief 15 cycles sampling time. */
116#define ADC_SAMPLE_28 2 /**< @brief 28 cycles sampling time. */
117#define ADC_SAMPLE_56 3 /**< @brief 56 cycles sampling time. */
118#define ADC_SAMPLE_84 4 /**< @brief 84 cycles sampling time. */
119#define ADC_SAMPLE_112 5 /**< @brief 112 cycles sampling time. */
120#define ADC_SAMPLE_144 6 /**< @brief 144 cycles sampling time. */
121#define ADC_SAMPLE_480 7 /**< @brief 480 cycles sampling time. */
122
123
124/** @} */
125
126#define ADC_SQR1_NUM_CH(n) 0
127
128/*===========================================================================*/
129/* Driver pre-compile time settings. */
130/*===========================================================================*/
131
132/**
133 * @name Configuration options
134 * @{
135 */
136
137/**
138 * @brief ADC1 driver enable switch.
139 * @details If set to @p TRUE the support for ADC1 is included.
140 * @note The default is @p TRUE.
141 */
142#if !defined(KINETIS_ADC_USE_ADC0) || defined(__DOXYGEN__)
143#define KINETIS_ADC_USE_ADC0 FALSE
144#endif
145
146#if !defined(KINETIS_ADC_USE_ADC1) || defined(__DOXYGEN__)
147#define KINETIS_ADC_USE_ADC1 FALSE
148#endif
149
150#if !defined(KINETIS_ADC_USE_ADC2) || defined(__DOXYGEN__)
151#define KINETIS_ADC_USE_ADC2 FALSE
152#endif
153
154/**
155 * @brief ADC interrupt priority level setting.
156 */
157#if !defined(KINETIS_ADC_IRQ_PRIORITY) || defined(__DOXYGEN__)
158#define KINETIS_ADC_IRQ_PRIORITY 5
159#endif
160
161/** @} */
162
163/*===========================================================================*/
164/* Derived constants and error checks. */
165/*===========================================================================*/
166
167#if KINETIS_ADC_USE_ADC0 && !KINETIS_HAS_ADC0
168#error "ADC1 not present in the selected device"
169#endif
170
171#if KINETIS_ADC_USE_ADC1 && !KINETIS_HAS_ADC1
172#error "ADC2 not present in the selected device"
173#endif
174
175#if KINETIS_ADC_USE_ADC2 && !KINETIS_HAS_ADC2
176#error "ADC3 not present in the selected device"
177#endif
178
179#if !KINETIS_ADC_USE_ADC0 && !KINETIS_ADC_USE_ADC1 && !KINETIS_ADC_USE_ADC2
180#error "ADC driver activated but no ADC peripheral assigned"
181#endif
182
183/*===========================================================================*/
184/* Driver data structures and types. */
185/*===========================================================================*/
186
187/**
188 * @brief ADC sample data type.
189 */
190typedef uint16_t adcsample_t;
191
192/**
193 * @brief Channels number in a conversion group.
194 */
195typedef uint16_t adc_channels_num_t;
196
197/**
198 * @brief Possible ADC failure causes.
199 * @note Error codes are architecture dependent and should not relied
200 * upon.
201 */
202typedef enum {
203 ADC_ERR_DMAFAILURE = 0, /**< DMA operations failure. */
204 ADC_ERR_OVERFLOW = 1 /**< ADC overflow condition. */
206
207/**
208 * @brief ADC error callback type.
209 *
210 * @param[in] adcp pointer to the @p ADCDriver object triggering the
211 * callback
212 * @param[in] err ADC error code
213 */
214typedef void (*adcerrorcallback_t)(ADCDriver *adcp, adcerror_t err);
215
216
217/**
218 * @brief Low level fields of the ADC configuration structure.
219 */
220#define adc_lld_configuration_group_fields \
221 /* [andreika]: these are STM32-compatible fields, we'll convert them into Kinetis ones */ \
222 /* ADC CR1 register initialization data. \
223 NOTE: All the required bits must be defined into this field except \
224 @p ADC_CR1_SCAN that is enforced inside the driver.*/ \
225 uint32_t cr1; \
226 /* ADC CR2 register initialization data. \
227 NOTE: All the required bits must be defined into this field except \
228 @p ADC_CR2_DMA, @p ADC_CR2_CONT and @p ADC_CR2_ADON that are \
229 enforced inside the driver.*/ \
230 uint32_t cr2; \
231 /* ADC SMPR1 register initialization data. \
232 NOTE: In this field must be specified the sample times for channels \
233 10...18.*/ \
234 uint32_t smpr1; \
235 /* ADC SMPR2 register initialization data. \
236 NOTE: In this field must be specified the sample times for channels \
237 0...9.*/ \
238 uint32_t smpr2; \
239 /* ADC watchdog high threshold register. \
240 NOTE: This field defines the high threshold of the analog watchdog.*/ \
241 uint16_t htr; \
242 /* ADC watchdog low threshold register. \
243 NOTE: This field defines the low threshold of the analog watchdog.*/ \
244 uint16_t ltr; \
245 /* ADC SQR1 register initialization data. \
246 NOTE: Conversion group sequence 13...16 + sequence length.*/ \
247 uint32_t sqr1; \
248 /* ADC SQR2 register initialization data. \
249 NOTE: Conversion group sequence 7...12.*/ \
250 uint32_t sqr2; \
251 /* ADC SQR3 register initialization data. \
252 NOTE: Conversion group sequence 1...6.*/ \
253 uint32_t sqr3
254
255#if 0
256 /**
257 * @brief Bitmask of channels for ADC conversion.
258 */
259 uint32_t channel_mask;
260 /**
261 * @brief ADC CFG1 register initialization data.
262 * @note All the required bits must be defined into this field.
263 */
264 uint32_t cfg1;
265 /**
266 * @brief ADC SC3 register initialization data.
267 * @note All the required bits must be defined into this field.
268 */
269 uint32_t sc3;
270#endif
271
272/**
273 * @brief Low level fields of the ADC configuration structure.
274 */
275#define adc_lld_config_fields \
276 /* Perform first time calibration */ \
277 bool calibrate
278
279/**
280 * @brief Low level fields of the ADC driver structure.
281 */
282#define adc_lld_driver_fields \
283 /* Pointer to the ADCx registers block. */ \
284 ADC_TypeDef *adc; \
285 /* Number of samples expected. */ \
286 size_t number_of_samples; \
287 /* Current position in the buffer. */ \
288 size_t current_index; \
289 /* Current channel index into group channel_mask. */ \
290 size_t current_channel; \
291 /* ADC12 driver config. */ \
292 adc12_config_t adc12Cfg; \
293 /* Current channel config struct. */ \
294 adc12_channel_config_t adc12ChannelCfg; \
295 /* Channel group index used to select software or hardware conversion triggering. */ \
296 uint32_t channelGroup; \
297 /* Bitmask of channels for ADC conversion. */ \
298 int8_t channelIndices[ADC_NUM_CHANNELS]
299
300/*===========================================================================*/
301/* Driver macros. */
302/*===========================================================================*/
303
304
305/*===========================================================================*/
306/* External declarations. */
307/*===========================================================================*/
308
309#if KINETIS_ADC_USE_ADC0 && !defined(__DOXYGEN__)
310extern ADCDriver ADCD1;
311#endif
312
313#if KINETIS_ADC_USE_ADC1 && !defined(__DOXYGEN__)
314extern ADCDriver ADCD2;
315#endif
316
317#if KINETIS_ADC_USE_ADC2 && !defined(__DOXYGEN__)
318extern ADCDriver ADCD3;
319#endif
320
321#ifdef __cplusplus
322extern "C" {
323#endif
324 void adc_lld_init(void);
325 void adc_lld_start(ADCDriver *adcp);
326 void adc_lld_stop(ADCDriver *adcp);
327 void adc_lld_start_conversion(ADCDriver *adcp);
328 void adc_lld_stop_conversion(ADCDriver *adcp);
329#ifdef __cplusplus
330}
331#endif
332
333#endif /* HAL_USE_ADC */
334
335#endif /* HAL_ADC_LLD_H_ */
336
337/** @} */
ADCDriver ADCD3
ADC3 driver identifier.
Definition hal_adc_lld.c:54
adcerror_t
Possible ADC failure causes.
void adc_lld_stop_conversion(ADCDriver *adcp)
Stops an ongoing conversion.
void adc_lld_init(void)
Low level ADC driver initialization.
uint32_t sc3
ADC SC3 register initialization data.
void(* adcerrorcallback_t)(ADCDriver *adcp, adcerror_t err)
ADC error callback type.
uint32_t channel_mask
Bitmask of channels for ADC conversion.
ADCDriver ADCD2
ADC2 driver identifier.
Definition hal_adc_lld.c:49
void adc_lld_start_conversion(ADCDriver *adcp)
Starts an ADC conversion.
void adc_lld_stop(ADCDriver *adcp)
Deactivates the ADC peripheral.
uint32_t cfg1
ADC CFG1 register initialization data.
uint16_t adc_channels_num_t
Channels number in a conversion group.
void adc_lld_start(ADCDriver *adcp)
Configures and activates the ADC peripheral.
uint16_t adcsample_t
ADC sample data type.
ADCDriver ADCD1
ADC1 driver identifier.
Definition hal_adc_lld.c:44
@ ADC_ERR_DMAFAILURE
@ ADC_ERR_OVERFLOW