ADC.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2019, 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  *
90  * @anchor ti_drivers_ADC_Examples_open
91  * ## Opening an ADC instance
92  *
93  * @code
94  * ADC_Handle adc;
95  * ADC_Params params;
96  *
97  * ADC_Params_init(&params);
98  *
99  * adc = ADC_open(0, &params);
100  * if (adc == NULL) {
101  * // ADC_open() failed
102  * while (1) {}
103  * }
104  * @endcode
105  *
106  * @anchor ti_drivers_ADC_Examples_convert
107  * ## Taking an ADC sample
108  *
109  * An ADC conversion with an ADC peripheral is started by calling
110  * ADC_convert(). The result value is returned by ADC_convert()
111  * once the conversion is finished.
112  *
113  * @code
114  * int_fast16_t res;
115  * uint_fast16_t adcValue;
116  *
117  * res = ADC_convert(adc, &adcValue);
118  * if (res == ADC_STATUS_SUCCESS)
119  * {
120  * print(adcValue);
121  * }
122  * @endcode
123  *
124  * @anchor ti_drivers_ADC_Examples_convert_microvolts
125  * ## Converting a sample to microvolts
126  *
127  * The result value returned by ADC_convert() is a raw value. The
128  * following uses ADC_convertToMicroVolts() to convert the raw value
129  * into microvolts.
130  * @code
131  * int_fast16_t res;
132  * uint_fast16_t adcValue;
133  * uint32_t adcValueUv;
134  *
135  * res = ADC_convert(adc, &adcValue);
136  * if (res == ADC_STATUS_SUCCESS)
137  * {
138  * adcValueUv = ADC_convertToMicroVolts(adc, adcValue);
139  * }
140  * @endcode
141  *
142  * <hr>
143  * @anchor ti_drivers_ADC_Configuration
144  * # Configuration
145  *
146  * Refer to the @ref driver_configuration "Driver's Configuration" section
147  * for driver configuration information.
148  * <hr>
149  ******************************************************************************
150  */
151 
152 #ifndef ti_drivers_ADC__include
153 #define ti_drivers_ADC__include
154 
155 #include <stdbool.h>
156 #include <stdint.h>
157 
158 #ifdef __cplusplus
159 extern "C" {
160 #endif
161 
166 #define ADC_convertRawToMicroVolts ADC_convertToMicroVolts
167 
186 #define ADC_CMD_RESERVED (32)
187 
201 #define ADC_STATUS_RESERVED (-32)
202 
211 #define ADC_STATUS_SUCCESS (0)
212 
219 #define ADC_STATUS_ERROR (-1)
220 
228 #define ADC_STATUS_UNDEFINEDCMD (-2)
229 
239 /* Add ADC_CMD_<commands> here */
240 
248 typedef struct ADC_Config_ *ADC_Handle;
249 
258 typedef struct {
259  void *custom;
261  bool isProtected;
268 } ADC_Params;
269 
275 typedef void (*ADC_CloseFxn) (ADC_Handle handle);
276 
282 typedef int_fast16_t (*ADC_ControlFxn) (ADC_Handle handle, uint_fast16_t cmd,
283  void *arg);
284 
290 typedef int_fast16_t (*ADC_ConvertFxn) (ADC_Handle handle, uint16_t *value);
291 
297 typedef uint32_t (*ADC_ConvertToMicroVoltsFxn) (ADC_Handle handle,
298  uint16_t adcValue);
299 
305 typedef void (*ADC_InitFxn) (ADC_Handle handle);
306 
312 typedef ADC_Handle (*ADC_OpenFxn) (ADC_Handle handle, ADC_Params *params);
313 
319 typedef struct {
321  ADC_CloseFxn closeFxn;
322 
324  ADC_ControlFxn controlFxn;
325 
327  ADC_ConvertFxn convertFxn;
328 
330  ADC_ConvertToMicroVoltsFxn convertToMicroVolts;
331 
333  ADC_InitFxn initFxn;
334 
336  ADC_OpenFxn openFxn;
337 } ADC_FxnTable;
338 
346 typedef struct ADC_Config_ {
350 
352  void *object;
353 
356  void const *hwAttrs;
357 } ADC_Config;
358 
366 extern void ADC_close(ADC_Handle handle);
367 
389 extern int_fast16_t ADC_control(ADC_Handle handle, uint_fast16_t cmd,
390  void *arg);
391 
409 extern int_fast16_t ADC_convert(ADC_Handle handle, uint16_t *value);
410 
424 extern uint32_t ADC_convertToMicroVolts(ADC_Handle handle,
425  uint16_t adcValue);
426 
432 extern void ADC_init(void);
433 
451 extern ADC_Handle ADC_open(uint_least8_t index, ADC_Params *params);
452 
462 extern void ADC_Params_init(ADC_Params *params);
463 
464 #ifdef __cplusplus
465 }
466 #endif
467 
468 #endif /* ti_drivers_ADC__include */
ADC driver&#39;s custom configuration structure.
Definition: ADC.h:346
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:258
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:349
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:356
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:319
void * object
Definition: ADC.h:352
struct ADC_Config_ * ADC_Handle
A handle that is returned from an ADC_open() call.
Definition: ADC.h:248
bool isProtected
Definition: ADC.h:261
void ADC_close(ADC_Handle handle)
Function to close an ADC driver instance.
void * custom
Definition: ADC.h:259
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale