app_adc.h
Go to the documentation of this file.
1 /**
2 ****************************************************************************************
3 *
4 * @file app_adc.h
5 * @author BLE Driver Team
6 * @brief Header file containing functions prototypes of ADC app library.
7 *
8 ****************************************************************************************
9 * @attention
10 #####Copyright (c) 2019 GOODIX
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 * Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 * Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 * Neither the name of GOODIX nor the names of its contributors may be used
21 to endorse or promote products derived from this software without
22 specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 POSSIBILITY OF SUCH DAMAGE.
35 ****************************************************************************************
36 */
37
38 /** @addtogroup PERIPHERAL Peripheral Driver
39 * @{
40 */
41
42 /** @addtogroup APP_DRIVER APP DRIVER
43 * @{
44 */
45
46 /** @defgroup APP_ADC ADC
47 * @brief ADC APP module driver.
48 * @{
49 */
50
51
52 #ifndef _APP_ADC_H_
53 #define _APP_ADC_H_
54
55 #include "grx_hal.h"
56 #include "app_io.h"
57 #include "app_dma.h"
58 #include "app_drv_error.h"
59 #include "app_drv_config.h"
60
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64
65 #ifdef HAL_ADC_MODULE_ENABLED
66
67 /** @addtogroup APP_ADC_ENUM Enumerations
68 * @{
69 */
70
71 /**
72 * @brief ADC event Enumerations definition
73 */
74 typedef enum
75 {
76 APP_ADC_EVT_CONV_CPLT, /**< Conversion completed by ADC peripheral. */
77 APP_ADC_EVT_ERROR, /**< Error reported by ADC peripheral. */
78 } app_adc_evt_type_t;
79
80 /**@brief App adc state types. */
81 typedef enum
82 {
83 APP_ADC_INVALID = 0,
84 APP_ADC_ACTIVITY,
85 #ifdef APP_DRIVER_WAKEUP_CALL_FUN
86 APP_ADC_SLEEP,
87 #endif
88 } app_adc_state_t;
89
90 /**@brief App adc dma state types. */
91 typedef enum
92 {
93 APP_ADC_DMA_INVALID = 0,
94 APP_ADC_DMA_ACTIVITY,
95 } app_adc_dma_state_t;
96
97 /** @} */
98
99
100 /** @addtogroup APP_ADC_STRUCTURES Structures
101 * @{
102 */
103
104 /**
105 * @brief ADC pins Structures
106 */
107 typedef struct
108 {
109 app_io_type_t type; /**< Specifies the type of ADC IO. */
110 app_io_mux_t mux; /**< Specifies the Peripheral to be connected to the selected pins. */
111 uint32_t pin; /**< Specifies the IO pins to be configured.
112 This parameter can be any value of @ref GR5xxx_pins. */
113 } app_adc_pin_t;
114
115 /**
116 * @brief ADC pins config Structures
117 */
118 typedef struct
119 {
120 app_adc_pin_t channel_p; /**< Set the configuration of ADC pin. */
121 app_adc_pin_t channel_n; /**< Set the configuration of ADC pin. */
122 app_adc_pin_t extern_ref; /**< Set the configuration of extern reference pin. */
123 } app_adc_pin_cfg_t;
124
125 /**
126 * @brief ADC DMA configuration Structures
127 */
128 typedef struct
129 {
130 dma_regs_t * dma_instance; /**< Specifies the dma instance of ADC. */
131 dma_channel_t dma_channel; /**< Specifies the dma channel of ADC. */
132 } app_adc_dma_cfg_t;
133
134 /**
135 * @brief ADC event structure definition
136 */
137 typedef struct
138 {
139 app_adc_evt_type_t type; /**< Type of event. */
140 #ifdef APP_ADC_GPADC_ENABLE
141 uint32_t error_code; /**< ADC Error code. */
142 #endif
143 } app_adc_evt_t;
144
145 /** @} */
146
147 /** @addtogroup APP_ADC_TYPEDEFS Type definitions
148 * @{
149 */
150 /**
151 * @brief ADC event callback definition
152 */
153 typedef void (*app_adc_evt_handler_t)(app_adc_evt_t *p_evt);
154
155 /** @} */
156
157 /** @addtogroup APP_ADC_STRUCTURES Structures
158 * @{
159 */
160
161 /**
162 * @brief ADC sample-node definition
163 */
164 typedef struct link_node
165 {
166 uint32_t channel; /**< Which channel to be sample; This parameter can be any value of ADC_INPUT_SOURCE:ADC_INPUT_SRC_IO0~ADC_INPUT_SRC_REF*/
167 uint16_t *p_buf; /**< Buffer pointer of current channel sample codes. Note:pointer must be 4-byte aligned as it is filled in datas by DMA */
168 uint32_t len; /**< Sample len codes on current channel. */
169 struct link_node *next; /**< Point to the next sample node. */
170 } app_adc_sample_node_t;
171
172 /**
173 * @brief ADC device structure definition
174 */
175 typedef struct
176 {
177 app_adc_evt_handler_t evt_handler; /**< ADC event callback definition. */
178 adc_handle_t handle; /**< ADC handle definition. */
179 dma_id_t dma_id; /**< DMA id definition . */
180 app_adc_state_t adc_state; /**< ADC state types. */
181 app_adc_dma_state_t adc_dma_state; /**< ADC dma state types. */
182 app_adc_sample_node_t *p_current_sample_node; /**< ADC sample-node definition. */
183 uint32_t multi_channel; /**< multi channel definition. */
184 } adc_env_t;
185
186 /**
187 * @brief ADC parameters structure definition
188 */
189 typedef struct
190 {
191 app_adc_pin_cfg_t pin_cfg; /**< the pin configuration information for the specified ADC module. */
192 app_adc_dma_cfg_t dma_cfg; /**< ADC DMA configuration. */
193 adc_init_t init; /**< ADC configuration parameters. */
194 adc_env_t adc_env; /**< ADC device structure definition. */
195 } app_adc_params_t;
196
197 /** @} */
198
199 /* Exported functions --------------------------------------------------------*/
200 /** @addtogroup APP_ADC_DRIVER_FUNCTIONS Functions
201 * @{
202 */
203
204 /**
205 ****************************************************************************************
206 * @brief Initialize the APP ADC DRIVER according to the specified parameters
207 * in the app_adc_params_t and app_adc_evt_handler_t.
208 * @note If DMA mode is set, you can use blocking mode. Conversely, if blocking mode
209 * is set, you can't use DMA mode.
210 *
211 * @param[in] p_params: Pointer to app_adc_params_t parameter which contains the
212 * configuration information for the specified ADC module.
213 * @param[in] evt_handler: ADC user callback function.
214 *
215 * @return Result of initialization.
216 ****************************************************************************************
217 */
218 uint16_t app_adc_init(app_adc_params_t *p_params, app_adc_evt_handler_t evt_handler);
219
220 /**
221 ****************************************************************************************
222 * @brief De-initialize the APP ADC DRIVER peripheral.
223 *
224 * @return Result of De-initialization.
225 ****************************************************************************************
226 */
227 uint16_t app_adc_deinit(void);
228
229 /**
230 ****************************************************************************************
231 * @brief Polling for conversion.
232 *
233 * @param[in] p_data: Pointer to data buffer which to storage ADC conversion results.
234 * @param[in] length: Length of data buffer.
235 * @param[in] timeout : Timeout duration
236 *
237 * @return Result of operation.
238 ****************************************************************************************
239 */
240 uint16_t app_adc_conversion_sync(uint16_t *p_data, uint32_t length, uint32_t timeout);
241
242 /**
243 ****************************************************************************************
244 * @brief Async for conversion.
245 * Note: SNSADC uses DMA for asynchronous transmission; GPADC uses interrupts for asynchronous transmission.
246 *
247 * @param[in] p_data: Pointer to data buffer which to storage ADC conversion results.
248 * @param[in] length: Length of data buffer, ranging between 0 and 4095.
249 * Note: Length must be aligned on a four-byte boundary.
250 *
251 * @return Result of operation.
252 ****************************************************************************************
253 */
254 uint16_t app_adc_conversion_async(uint16_t *p_data, uint32_t length);
255
256 /**
257 ****************************************************************************************
258 * @brief Convert the ADC conversion results to a voltage value(internal reference).
259 *
260 * @param[in] inbuf: Pointer to data buffer which storage ADC conversion results.
261 * @param[out] outbuf: Pointer to data buffer which to storage voltage results.
262 * @param[in] buflen: Length of data buffer.
263 *
264 * @return Result of operation.
265 ****************************************************************************************
266 */
267 uint16_t app_adc_voltage_intern(uint16_t *inbuf, double *outbuf, uint32_t buflen);
268
269 #ifdef APP_ADC_SNSADC_ENABLE
270 /**
271 ****************************************************************************************
272 * @brief Convert the ADC conversion results to a voltage value(external reference).
273 *
274 * @param[in] ref: slope of ADC.
275 * @param[in] inbuf: Pointer to data buffer which storage ADC conversion results.
276 * @param[out] outbuf: Pointer to data buffer which to storage voltage results.
277 * @param[in] buflen: Length of data buffer.
278 *
279 * @return Result of operation.
280 ****************************************************************************************
281 */
282 uint16_t app_adc_voltage_extern(double ref, uint16_t *inbuf, double *outbuf, uint32_t buflen);
283 #endif
284
285 #ifdef APP_ADC_VBAT_TEMP_CONV_ENABLE
286 /**
287 ****************************************************************************************
288 * @brief Convert the ADC conversion results to temperature value.
289 *
290 * @param[in] inbuf: Pointer to data buffer which storage ADC conversion results.
291 * @param[out] outbuf: Pointer to data buffer which to storage voltage results.
292 * @param[in] buflen: Length of data buffer.
293 *
294 * @return Result of operation.
295 ****************************************************************************************
296 */
297 uint16_t app_adc_temperature_conv(uint16_t *inbuf, double *outbuf, uint32_t buflen);
298
299 /**
300 ****************************************************************************************
301 * @brief Convert the ADC conversion results to battery value.
302 *
303 * @param[in] inbuf: Pointer to data buffer which storage ADC codes.
304 * @param[out] outbuf: Pointer to data buffer which to storage conversion results.
305 * @param[in] buflen: Length of data buffer.
306 *
307 * @return Result of operation.
308 ****************************************************************************************
309 */
310 uint16_t app_adc_vbat_conv(uint16_t *inbuf, double *outbuf, uint32_t buflen);
311 #endif
312
313 /**
314 ****************************************************************************************
315 * @brief Return the ADC handle.
316 *
317 * @return Pointer to the ADC handle.
318 ****************************************************************************************
319 */
320 adc_handle_t *app_adc_get_handle(void);
321
322 /**
323 ****************************************************************************************
324 * @brief Async for multi channels conversion; evt_handler in app_adc_init will callback when all channels finish.
325 * Note: SNSADC uses DMA for asynchronous transmission; GPADC uses interrupts for asynchronous transmission.
326 *
327 * @param[in] p_begin_node: Pointer to the multi sample channels list node.
328 * @param[in] total_nodes: total sample channels.
329 *
330 * @return Result of operation.
331 ****************************************************************************************
332 */
333 uint16_t app_adc_multi_channel_conversion_async(app_adc_sample_node_t *p_begin_node, uint32_t total_nodes);
334
335 #ifdef APP_ADC_CLOCK_START_ENABLE
336 /**
337 ****************************************************************************************
338 * @brief Clear the ADC FIFO, filter the first two incorrect code values , and start the ADC clock.
339 *
340 * @return Result of operation.
341 ****************************************************************************************
342 */
343 uint16_t app_adc_clock_start(void);
344 #endif
345
346 #ifdef APP_ADC_GET_AVG_ENABLE
347 /**
348 ****************************************************************************************
349 * @brief Get the ADC sampling code by polling, and convert sampling code of the specified length to the average voltage.
350 *
351 * @param[out] avg_voltage: Pointer to data which to storage average voltage results.
352 * @param[in] length: Length of the ADC sample values to be averaged.
353 *
354 * @return Result of operation.
355 ****************************************************************************************
356 */
357 uint16_t app_adc_get_avg_voltage(float *avg_voltage, uint32_t length);
358
359 #endif
360
361
362 /** @} */
363
364 #endif
365
366 #ifdef __cplusplus
367 }
368 #endif
369
370 #endif
371
372 /** @} */
373
374 /** @} */
375
376 /** @} */
377
app_adc_vbat_conv
uint16_t app_adc_vbat_conv(uint16_t *inbuf, double *outbuf, uint32_t buflen)
Convert the ADC conversion results to battery value.
APP_ADC_INVALID
@ APP_ADC_INVALID
Definition: app_adc.h:83
app_adc_evt_handler_t
void(* app_adc_evt_handler_t)(app_adc_evt_t *p_evt)
ADC event callback definition.
Definition: app_adc.h:153
APP_ADC_EVT_CONV_CPLT
@ APP_ADC_EVT_CONV_CPLT
Definition: app_adc.h:76
app_adc_pin_t::mux
app_io_mux_t mux
Definition: app_adc.h:110
app_adc_dma_cfg_t::dma_channel
dma_channel_t dma_channel
Definition: app_adc.h:131
app_adc_multi_channel_conversion_async
uint16_t app_adc_multi_channel_conversion_async(app_adc_sample_node_t *p_begin_node, uint32_t total_nodes)
Async for multi channels conversion; evt_handler in app_adc_init will callback when all channels fini...
app_adc_params_t::adc_env
adc_env_t adc_env
Definition: app_adc.h:194
adc_env_t::adc_state
app_adc_state_t adc_state
Definition: app_adc.h:180
APP_ADC_DMA_INVALID
@ APP_ADC_DMA_INVALID
Definition: app_adc.h:93
adc_env_t
ADC device structure definition.
Definition: app_adc.h:176
app_adc_evt_t::type
app_adc_evt_type_t type
Definition: app_adc.h:139
app_io_type_t
app_io_type_t
GPIO type Enumerations definition.
Definition: app_io.h:142
adc_env_t::adc_dma_state
app_adc_dma_state_t adc_dma_state
Definition: app_adc.h:181
app_adc_params_t
ADC parameters structure definition.
Definition: app_adc.h:190
app_adc_pin_t::pin
uint32_t pin
Definition: app_adc.h:111
app_adc_dma_cfg_t
ADC DMA configuration Structures.
Definition: app_adc.h:129
APP_ADC_DMA_ACTIVITY
@ APP_ADC_DMA_ACTIVITY
Definition: app_adc.h:94
app_adc_state_t
app_adc_state_t
App adc state types.
Definition: app_adc.h:82
app_adc_params_t::pin_cfg
app_adc_pin_cfg_t pin_cfg
Definition: app_adc.h:191
_ll_adc_init
LL ADC init Structure definition.
Definition: ll_adc.h:77
app_io.h
Header file containing functions prototypes of GPIO app library.
APP_ADC_EVT_ERROR
@ APP_ADC_EVT_ERROR
Definition: app_adc.h:77
app_adc_evt_t
ADC event structure definition.
Definition: app_adc.h:138
app_adc_evt_type_t
app_adc_evt_type_t
ADC event Enumerations definition.
Definition: app_adc.h:75
app_adc_voltage_intern
uint16_t app_adc_voltage_intern(uint16_t *inbuf, double *outbuf, uint32_t buflen)
Convert the ADC conversion results to a voltage value(internal reference).
adc_env_t::evt_handler
app_adc_evt_handler_t evt_handler
Definition: app_adc.h:177
grx_hal.h
This file contains all the functions prototypes for the HAL module driver.
app_adc_sample_node_t
struct link_node app_adc_sample_node_t
ADC sample-node definition.
app_adc_conversion_sync
uint16_t app_adc_conversion_sync(uint16_t *p_data, uint32_t length, uint32_t timeout)
Polling for conversion.
_adc_handle
ADC handle Structure definition.
Definition: hal_adc.h:112
app_adc_pin_t::type
app_io_type_t type
Definition: app_adc.h:109
app_adc_pin_cfg_t::extern_ref
app_adc_pin_t extern_ref
Definition: app_adc.h:122
link_node::next
struct link_node * next
Definition: app_adc.h:169
app_adc_params_t::init
adc_init_t init
Definition: app_adc.h:193
link_node::len
uint32_t len
Definition: app_adc.h:168
link_node::p_buf
uint16_t * p_buf
Definition: app_adc.h:167
APP_ADC_ACTIVITY
@ APP_ADC_ACTIVITY
Definition: app_adc.h:84
app_dma.h
Header file containing functions prototypes of DMA app library.
app_adc_deinit
uint16_t app_adc_deinit(void)
De-initialize the APP ADC DRIVER peripheral.
link_node
ADC sample-node definition.
Definition: app_adc.h:165
link_node::channel
uint32_t channel
Definition: app_adc.h:166
app_adc_pin_cfg_t::channel_n
app_adc_pin_t channel_n
Definition: app_adc.h:121
app_adc_get_handle
adc_handle_t * app_adc_get_handle(void)
Return the ADC handle.
app_io_mux_t
app_io_mux_t
GPIO mux Enumerations definition.
Definition: app_io.h:248
adc_env_t::handle
adc_handle_t handle
Definition: app_adc.h:178
adc_env_t::multi_channel
uint32_t multi_channel
Definition: app_adc.h:183
app_adc_conversion_async
uint16_t app_adc_conversion_async(uint16_t *p_data, uint32_t length)
Async for conversion. Note: SNSADC uses DMA for asynchronous transmission; GPADC uses interrupts for ...
app_adc_pin_cfg_t::channel_p
app_adc_pin_t channel_p
Definition: app_adc.h:120
adc_env_t::dma_id
dma_id_t dma_id
Definition: app_adc.h:179
app_adc_temperature_conv
uint16_t app_adc_temperature_conv(uint16_t *inbuf, double *outbuf, uint32_t buflen)
Convert the ADC conversion results to temperature value.
app_drv_error.h
Header file of app driver error code.
app_drv_config.h
Header file of app driver config code.
app_adc_dma_cfg_t::dma_instance
dma_regs_t * dma_instance
Definition: app_adc.h:130
app_adc_dma_state_t
app_adc_dma_state_t
App adc dma state types.
Definition: app_adc.h:92
dma_id_t
int16_t dma_id_t
DMA id definition.
Definition: app_dma.h:98
app_adc_pin_cfg_t
ADC pins config Structures.
Definition: app_adc.h:119
dma_channel_t
dma_channel_t
HAL DMA Channel Enumerations definition.
Definition: hal_dma.h:95
adc_env_t::p_current_sample_node
app_adc_sample_node_t * p_current_sample_node
Definition: app_adc.h:182
app_adc_params_t::dma_cfg
app_adc_dma_cfg_t dma_cfg
Definition: app_adc.h:192
app_adc_pin_t
ADC pins Structures.
Definition: app_adc.h:108
app_adc_init
uint16_t app_adc_init(app_adc_params_t *p_params, app_adc_evt_handler_t evt_handler)
Initialize the APP ADC DRIVER according to the specified parameters in the app_adc_params_t and app_a...