ADC.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2020, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!****************************************************************************
33  * @file ADC.h
34  * @brief Analog to Digital Conversion (ADC) Input Driver
35  *
36  * @anchor ti_drivers_ADC_Overview
37  * # Overview
38  *
39  * The ADC driver allows you to manage an Analog to Digital peripheral via
40  * simple and portable APIs. This driver supports sampling and converting
41  * raw values into microvolts.
42  *
43  * <hr>
44  * @anchor ti_drivers_ADC_Usage
45  * # Usage
46  *
47  * This documentation provides a basic @ref ti_drivers_ADC_Synopsis
48  * "usage summary" and a set of @ref ti_drivers_ADC_Examples "examples"
49  * in the form of commented code fragments. Detailed descriptions of the
50  * APIs are provided in subsequent sections.
51  *
52  * @anchor ti_drivers_ADC_Synopsis
53  * ## Synopsis
54  * @anchor ti_drivers_ADC_Synopsis_Code
55  * @code
56  * // Import ADC Driver definitions
57  * #include <ti/drivers/ADC.h>
58  *
59  * // Define name for ADC channel index
60  * #define THERMOCOUPLE_OUT 0
61  *
62  * // One-time init of ADC driver
63  * ADC_init();
64  *
65  * // initialize optional ADC parameters
66  * ADC_Params params;
67  * ADC_Params_init(&params);
68  * params.isProtected = true;
69  *
70  * // Open ADC channels for usage
71  * ADC_Handle adcHandle = ADC_open(THERMOCOUPLE_OUT, &params);
72  *
73  * // Sample the analog output from the Thermocouple
74  * ADC_convert(adcHandle, &result);
75  *
76  * // Convert the sample to microvolts
77  * resultUv = ADC_convertToMicroVolts(adcHandle, result);
78  *
79  * ADC_close(adcHandle);
80  * @endcode
81  *
82  * <hr>
83  * @anchor ti_drivers_ADC_Examples
84  * # Examples
85  *
86  * @li @ref ti_drivers_ADC_Examples_open "Opening an ADC instance"
87  * @li @ref ti_drivers_ADC_Examples_convert "Taking an ADC sample"
88  * @li @ref ti_drivers_ADC_Examples_convert_microvolts "Converting a sample to microvolts"
89  * @li @ref ti_drivers_ADC_Examples_convert_chain "Executing multi-channel sampling"
90  *
91  * @anchor ti_drivers_ADC_Examples_open
92  * ## Opening an ADC instance
93  *
94  * @code
95  * ADC_Handle adc;
96  * ADC_Params params;
97  *
98  * ADC_Params_init(&params);
99  *
100  * adc = ADC_open(0, &params);
101  * if (adc == NULL) {
102  * // ADC_open() failed
103  * while (1) {}
104  * }
105  * @endcode
106  *
107  * @anchor ti_drivers_ADC_Examples_convert
108  * ## Taking an ADC sample
109  *
110  * An ADC conversion with an ADC peripheral is started by calling
111  * ADC_convert(). The result value is returned by ADC_convert()
112  * once the conversion is finished.
113  *
114  * @code
115  * int_fast16_t res;
116  * uint_fast16_t adcValue;
117  *
118  * res = ADC_convert(adc, &adcValue);
119  * if (res == ADC_STATUS_SUCCESS)
120  * {
121  * print(adcValue);
122  * }
123  * @endcode
124  *
125  * @anchor ti_drivers_ADC_Examples_convert_microvolts
126  * ## Converting a sample to microvolts
127  *
128  * The result value returned by ADC_convert() is a raw value. The
129  * following uses ADC_convertToMicroVolts() to convert the raw value
130  * into microvolts.
131  * @code
132  * int_fast16_t res;
133  * uint_fast16_t adcValue;
134  * uint32_t adcValueUv;
135  *
136  * res = ADC_convert(adc, &adcValue);
137  * if (res == ADC_STATUS_SUCCESS)
138  * {
139  * adcValueUv = ADC_convertToMicroVolts(adc, adcValue);
140  * }
141  * @endcode
142  *
143  * @anchor ti_drivers_ADC_Examples_convert_chain
144  * ## Executing multi-channel sampling
145  *
146  * ADC_convertChain() provides an optimized way to perform
147  * ADC conversions for several ADC instances (multi-channel sampling).
148  * It takes a list of identically configured ADC instances and returns
149  * a buffer with the corresponding result values once the conversion
150  * is finished. One typical use-case would be reading a group of sensors
151  * that share the sampling duration.
152  *
153  * Should the configuration of the ADC instances differ, the configuration
154  * of the first instance in the list is used to set the parameters of the
155  * entire conversion chain.
156  * @code
157  * ADC_Handle adc[ADC_COUNT];
158  * int_fast16_t res;
159  * uint16_t retValues[ADC_COUNT];
160  * uint8_t i;
161  *
162  * res = ADC_convertChain(adc, retValues, ADC_COUNT);
163  * if (res == ADC_STATUS_SUCCESS)
164  * {
165  * for (i = 0; i < ADC_COUNT; i++) {
166  * print(retValues[i]);
167  * }
168  * }
169  * @endcode
170  *
171  * <hr>
172  * @anchor ti_drivers_ADC_Configuration
173  * # Configuration
174  *
175  * Refer to the @ref driver_configuration "Driver's Configuration" section
176  * for driver configuration information.
177  * <hr>
178  ******************************************************************************
179  */
180 
181 #ifndef ti_drivers_ADC__include
182 #define ti_drivers_ADC__include
183 
184 #include <stdbool.h>
185 #include <stdint.h>
186 
187 #ifdef __cplusplus
188 extern "C" {
189 #endif
190 
195 #define ADC_convertRawToMicroVolts ADC_convertToMicroVolts
196 
215 #define ADC_CMD_RESERVED (32)
216 
230 #define ADC_STATUS_RESERVED (-32)
231 
240 #define ADC_STATUS_SUCCESS (0)
241 
248 #define ADC_STATUS_ERROR (-1)
249 
257 #define ADC_STATUS_UNDEFINEDCMD (-2)
258 
268 /* Add ADC_CMD_<commands> here */
269 
277 typedef struct ADC_Config_ *ADC_Handle;
278 
287 typedef struct {
288  void *custom;
290  bool isProtected;
297 } ADC_Params;
298 
304 typedef void (*ADC_CloseFxn) (ADC_Handle handle);
305 
311 typedef int_fast16_t (*ADC_ControlFxn) (ADC_Handle handle, uint_fast16_t cmd,
312  void *arg);
313 
319 typedef int_fast16_t (*ADC_ConvertFxn) (ADC_Handle handle, uint16_t *value);
320 
326 typedef int_fast16_t (*ADC_ConvertChainFxn) (ADC_Handle *handleList,
327  uint16_t *dataBuffer, uint8_t channelCount);
328 
334 typedef uint32_t (*ADC_ConvertToMicroVoltsFxn) (ADC_Handle handle,
335  uint16_t adcValue);
336 
342 typedef void (*ADC_InitFxn) (ADC_Handle handle);
343 
349 typedef ADC_Handle (*ADC_OpenFxn) (ADC_Handle handle, ADC_Params *params);
350 
356 typedef struct {
358  ADC_CloseFxn closeFxn;
359 
361  ADC_ControlFxn controlFxn;
362 
364  ADC_ConvertFxn convertFxn;
365 
367  ADC_ConvertChainFxn convertChainFxn;
368 
370  ADC_ConvertToMicroVoltsFxn convertToMicroVolts;
371 
373  ADC_InitFxn initFxn;
374 
376  ADC_OpenFxn openFxn;
377 } ADC_FxnTable;
378 
386 typedef struct ADC_Config_ {
390 
392  void *object;
393 
396  void const *hwAttrs;
397 } ADC_Config;
398 
406 extern void ADC_close(ADC_Handle handle);
407 
429 extern int_fast16_t ADC_control(ADC_Handle handle, uint_fast16_t cmd,
430  void *arg);
431 
449 extern int_fast16_t ADC_convert(ADC_Handle handle, uint16_t *value);
450 
471 extern int_fast16_t ADC_convertChain(ADC_Handle *handleList,
472  uint16_t *dataBuffer, uint8_t channelCount);
473 
487 extern uint32_t ADC_convertToMicroVolts(ADC_Handle handle,
488  uint16_t adcValue);
489 
495 extern void ADC_init(void);
496 
514 extern ADC_Handle ADC_open(uint_least8_t index, ADC_Params *params);
515 
525 extern void ADC_Params_init(ADC_Params *params);
526 
527 #ifdef __cplusplus
528 }
529 #endif
530 
531 #endif /* ti_drivers_ADC__include */
ADC driver&#39;s custom configuration structure.
Definition: ADC.h:386
ADC_Params params
Definition: Driver_Init.h:11
int_fast16_t ADC_convert(ADC_Handle handle, uint16_t *value)
Function to perform an ADC conversion.
ADC Parameters used with ADC_open().
Definition: ADC.h:287
void ADC_init(void)
Function to initialize the ADC driver.
uint32_t ADC_convertToMicroVolts(ADC_Handle handle, uint16_t adcValue)
Function to convert a raw ADC sample into microvolts.
void ADC_Params_init(ADC_Params *params)
Initialize an ADC_Params structure to its default values.
ADC_FxnTable const * fxnTablePtr
Definition: ADC.h:389
int_fast16_t ADC_control(ADC_Handle handle, uint_fast16_t cmd, void *arg)
Function performs implementation specific features on a driver instance.
ADC_Handle ADC_open(uint_least8_t index, ADC_Params *params)
Function to initialize the ADC peripheral.
void const * hwAttrs
Definition: ADC.h:396
struct ADC_Config_ ADC_Config
ADC driver&#39;s custom configuration structure.
The definition of an ADC function table that contains the required set of functions to control a spec...
Definition: ADC.h:356
void * object
Definition: ADC.h:392
struct ADC_Config_ * ADC_Handle
A handle that is returned from an ADC_open() call.
Definition: ADC.h:277
bool isProtected
Definition: ADC.h:290
void ADC_close(ADC_Handle handle)
Function to close an ADC driver instance.
int_fast16_t ADC_convertChain(ADC_Handle *handleList, uint16_t *dataBuffer, uint8_t channelCount)
Function to perform a multi-channel ADC conversion.
void * custom
Definition: ADC.h:288
© Copyright 1995-2021, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale