ChibiOS/HAL  6.1.0
hal_adc_lld.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
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 hal_adc_lld.h
19  * @brief PLATFORM ADC subsystem low level driver header.
20  *
21  * @addtogroup ADC
22  * @{
23  */
24 
25 #ifndef HAL_ADC_LLD_H
26 #define HAL_ADC_LLD_H
27 
28 #if (HAL_USE_ADC == TRUE) || defined(__DOXYGEN__)
29 
30 /*===========================================================================*/
31 /* Driver constants. */
32 /*===========================================================================*/
33 
34 /*===========================================================================*/
35 /* Driver pre-compile time settings. */
36 /*===========================================================================*/
37 
38 /**
39  * @name PLATFORM configuration options
40  * @{
41  */
42 /**
43  * @brief ADC1 driver enable switch.
44  * @details If set to @p TRUE the support for ADC1 is included.
45  * @note The default is @p FALSE.
46  */
47 #if !defined(PLATFORM_ADC_USE_ADC1) || defined(__DOXYGEN__)
48 #define PLATFORM_ADC_USE_ADC1 FALSE
49 #endif
50 /** @} */
51 
52 /*===========================================================================*/
53 /* Derived constants and error checks. */
54 /*===========================================================================*/
55 
56 /*===========================================================================*/
57 /* Driver data structures and types. */
58 /*===========================================================================*/
59 
60 /**
61  * @brief ADC sample data type.
62  */
63 typedef uint16_t adcsample_t;
64 
65 /**
66  * @brief Channels number in a conversion group.
67  */
68 typedef uint16_t adc_channels_num_t;
69 
70 /**
71  * @brief Possible ADC failure causes.
72  * @note Error codes are architecture dependent and should not relied
73  * upon.
74  */
75 typedef enum {
76  ADC_ERR_DMAFAILURE = 0, /**< DMA operations failure. */
77  ADC_ERR_OVERFLOW = 1, /**< ADC overflow condition. */
78  ADC_ERR_AWD = 2 /**< Analog watchdog triggered. */
79 } adcerror_t;
80 
81 /**
82  * @brief Type of a structure representing an ADC driver.
83  */
84 typedef struct ADCDriver ADCDriver;
85 
86 /**
87  * @brief ADC notification callback type.
88  *
89  * @param[in] adcp pointer to the @p ADCDriver object triggering the
90  * callback
91  * @param[in] buffer pointer to the most recent samples data
92  * @param[in] n number of buffer rows available starting from @p buffer
93  */
94 typedef void (*adccallback_t)(ADCDriver *adcp, adcsample_t *buffer, size_t n);
95 
96 /**
97  * @brief ADC error callback type.
98  *
99  * @param[in] adcp pointer to the @p ADCDriver object triggering the
100  * callback
101  * @param[in] err ADC error code
102  */
103 typedef void (*adcerrorcallback_t)(ADCDriver *adcp, adcerror_t err);
104 
105 /**
106  * @brief Conversion group configuration structure.
107  * @details This implementation-dependent structure describes a conversion
108  * operation.
109  * @note The use of this configuration structure requires knowledge of
110  * PLATFORM ADC cell registers interface, please refer to the PLATFORM
111  * reference manual for details.
112  */
113 typedef struct {
114  /**
115  * @brief Enables the circular buffer mode for the group.
116  */
117  bool circular;
118  /**
119  * @brief Number of the analog channels belonging to the conversion group.
120  */
122  /**
123  * @brief Callback function associated to the group or @p NULL.
124  */
126  /**
127  * @brief Error callback or @p NULL.
128  */
130  /* End of the mandatory fields.*/
132 
133 /**
134  * @brief Driver configuration structure.
135  * @note It could be empty on some architectures.
136  */
137 typedef struct {
138  uint32_t dummy;
139 } ADCConfig;
140 
141 /**
142  * @brief Structure representing an ADC driver.
143  */
144 struct ADCDriver {
145  /**
146  * @brief Driver state.
147  */
149  /**
150  * @brief Current configuration data.
151  */
153  /**
154  * @brief Current samples buffer pointer or @p NULL.
155  */
157  /**
158  * @brief Current samples buffer depth or @p 0.
159  */
160  size_t depth;
161  /**
162  * @brief Current conversion group pointer or @p NULL.
163  */
165 #if (ADC_USE_WAIT == TRUE) || defined(__DOXYGEN__)
166  /**
167  * @brief Waiting thread.
168  */
170 #endif
171 #if (ADC_USE_MUTUAL_EXCLUSION == TRUE) || defined(__DOXYGEN__)
172  /**
173  * @brief Mutex protecting the peripheral.
174  */
176 #endif
177 #if defined(ADC_DRIVER_EXT_FIELDS)
178  ADC_DRIVER_EXT_FIELDS
179 #endif
180  /* End of the mandatory fields.*/
181 };
182 
183 /*===========================================================================*/
184 /* Driver macros. */
185 /*===========================================================================*/
186 
187 /*===========================================================================*/
188 /* External declarations. */
189 /*===========================================================================*/
190 
191 #if (PLATFORM_ADC_USE_ADC1 == TRUE) && !defined(__DOXYGEN__)
192 extern ADCDriver ADCD1;
193 #endif
194 
195 #ifdef __cplusplus
196 extern "C" {
197 #endif
198  void adc_lld_init(void);
199  void adc_lld_start(ADCDriver *adcp);
200  void adc_lld_stop(ADCDriver *adcp);
203 #ifdef __cplusplus
204 }
205 #endif
206 
207 #endif /* HAL_USE_ADC == TRUE */
208 
209 #endif /* HAL_ADC_LLD_H */
210 
211 /** @} */
adcstate_t
Driver state machine possible states.
Definition: hal_adc.h:70
const ADCConversionGroup * grpp
Current conversion group pointer or NULL.
Definition: hal_adc_lld.h:164
adcerror_t
Possible ADC failure causes.
Definition: hal_adc_lld.h:75
void adc_lld_start(ADCDriver *adcp)
Configures and activates the ADC peripheral.
Definition: hal_adc_lld.c:80
adccallback_t end_cb
Callback function associated to the group or NULL.
Definition: hal_adc_lld.h:125
thread_reference_t thread
Waiting thread.
Definition: hal_adc_lld.h:169
Conversion group configuration structure.
Definition: hal_adc_lld.h:113
adcerrorcallback_t error_cb
Error callback or NULL.
Definition: hal_adc_lld.h:129
adc_channels_num_t num_channels
Number of the analog channels belonging to the conversion group.
Definition: hal_adc_lld.h:121
uint16_t adc_channels_num_t
Channels number in a conversion group.
Definition: hal_adc_lld.h:68
void(* adccallback_t)(ADCDriver *adcp, adcsample_t *buffer, size_t n)
ADC notification callback type.
Definition: hal_adc_lld.h:94
void adc_lld_stop_conversion(ADCDriver *adcp)
Stops an ongoing conversion.
Definition: hal_adc_lld.c:134
Structure representing an ADC driver.
Definition: hal_adc_lld.h:144
bool circular
Enables the circular buffer mode for the group.
Definition: hal_adc_lld.h:117
void(* adcerrorcallback_t)(ADCDriver *adcp, adcerror_t err)
ADC error callback type.
Definition: hal_adc_lld.h:103
adcstate_t state
Driver state.
Definition: hal_adc_lld.h:148
void * thread_reference_t
Type of a thread reference.
Definition: osal.h:180
uint16_t adcsample_t
ADC sample data type.
Definition: hal_adc_lld.h:63
mutex_t mutex
Mutex protecting the peripheral.
Definition: hal_adc_lld.h:175
void adc_lld_stop(ADCDriver *adcp)
Deactivates the ADC peripheral.
Definition: hal_adc_lld.c:101
ADCDriver ADCD1
ADC1 driver identifier.
Definition: hal_adc_lld.c:41
void adc_lld_init(void)
Low level ADC driver initialization.
Definition: hal_adc_lld.c:65
size_t depth
Current samples buffer depth or 0.
Definition: hal_adc_lld.h:160
Driver configuration structure.
Definition: hal_adc_lld.h:137
uint32_t mutex_t
Type of a mutex.
Definition: osal.h:223
adcsample_t * samples
Current samples buffer pointer or NULL.
Definition: hal_adc_lld.h:156
void adc_lld_start_conversion(ADCDriver *adcp)
Starts an ADC conversion.
Definition: hal_adc_lld.c:122
const ADCConfig * config
Current configuration data.
Definition: hal_adc_lld.h:152