MSPM0G1X0X_G3X0X TI-Driver Library  1.20.01.06
ADC.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2023, 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  *
35  * @brief Analog to Digital Conversion (ADC) Input Driver
36  *
37  * @defgroup ADC Analog to Digital Conversion (ADC)
38  *
39  * @anchor ti_drivers_ADC_Overview
40  * # Overview
41  *
42  * The ADC driver allows you to manage an Analog to Digital peripheral via
43  * simple and portable APIs. This driver supports sampling and converting
44  * raw values into microvolts.
45  *
46  * <hr>
47  * @anchor ti_drivers_ADC_Usage
48  * # Usage
49  *
50  * This documentation provides a basic @ref ti_drivers_ADC_Synopsis
51  * "usage summary" and a set of @ref ti_drivers_ADC_Examples "examples"
52  * in the form of commented code fragments. Detailed descriptions of the
53  * APIs are provided in subsequent sections.
54  *
55  * @anchor ti_drivers_ADC_Synopsis
56  * ## Synopsis
57  * @anchor ti_drivers_ADC_Synopsis_Code
58  * @code
59  * // Import ADC Driver definitions
60  * #include <ti/drivers/ADC.h>
61  *
62  * // Define name for ADC channel index
63  * #define THERMOCOUPLE_OUT 0
64  *
65  * // One-time init of ADC driver
66  * ADC_init();
67  *
68  * // initialize optional ADC parameters
69  * ADC_Params params;
70  * ADC_Params_init(&params);
71  * params.isProtected = true;
72  *
73  * // Open ADC channels for usage
74  * ADC_Handle adcHandle = ADC_open(THERMOCOUPLE_OUT, &params);
75  *
76  * // Sample the analog output from the Thermocouple
77  * ADC_convert(adcHandle, &result);
78  *
79  * // Convert the sample to microvolts
80  * resultUv = ADC_convertToMicroVolts(adcHandle, result);
81  *
82  * ADC_close(adcHandle);
83  * @endcode
84  *
85  * <hr>
86  * @anchor ti_drivers_ADC_Examples
87  * # Examples
88  *
89  * @li @ref ti_drivers_ADC_Examples_open "Opening an ADC instance"
90  * @li @ref ti_drivers_ADC_Examples_convert "Taking an ADC sample"
91  * @li @ref ti_drivers_ADC_Examples_convert_microvolts "Converting a sample to microvolts"
92  * @li @ref ti_drivers_ADC_Examples_convert_chain "Executing multi-channel sampling"
93  *
94  * @anchor ti_drivers_ADC_Examples_open
95  * ## Opening an ADC instance
96  *
97  * @code
98  * ADC_Handle adc;
99  * ADC_Params params;
100  *
101  * ADC_Params_init(&params);
102  *
103  * adc = ADC_open(0, &params);
104  * if (adc == NULL) {
105  * // ADC_open() failed
106  * while (1) {}
107  * }
108  * @endcode
109  *
110  * @anchor ti_drivers_ADC_Examples_convert
111  * ## Taking an ADC sample
112  *
113  * An ADC conversion with an ADC peripheral is started by calling
114  * ADC_convert(). The result value is returned by ADC_convert()
115  * once the conversion is finished.
116  *
117  * @code
118  * int_fast16_t res;
119  * uint_fast16_t adcValue;
120  *
121  * res = ADC_convert(adc, &adcValue);
122  * if (res == ADC_STATUS_SUCCESS)
123  * {
124  * print(adcValue);
125  * }
126  * @endcode
127  *
128  * @anchor ti_drivers_ADC_Examples_convert_microvolts
129  * ## Converting a sample to microvolts
130  *
131  * The result value returned by ADC_convert() is a raw value. The
132  * following uses ADC_convertToMicroVolts() to convert the raw value
133  * into microvolts.
134  * @code
135  * int_fast16_t res;
136  * uint_fast16_t adcValue;
137  * uint32_t adcValueUv;
138  *
139  * res = ADC_convert(adc, &adcValue);
140  * if (res == ADC_STATUS_SUCCESS)
141  * {
142  * adcValueUv = ADC_convertToMicroVolts(adc, adcValue);
143  * }
144  * @endcode
145  *
146  * @anchor ti_drivers_ADC_Examples_convert_chain
147  * ## Executing multi-channel sampling
148  *
149  * ADC_convertChain() provides an optimized way to perform
150  * ADC conversions for several ADC instances.
151  * It takes a list of identically configured ADC instances and returns
152  * a buffer with the corresponding result values once the conversion
153  * is finished. One typical use-case would be reading a group of sensors
154  * that share the sampling duration.
155  *
156  * Should the configuration of the ADC instances differ, the configuration
157  * of the first instance in the list is used to set the parameters of the
158  * entire conversion chain.
159  * @code
160  * ADC_Handle adc[ADC_COUNT];
161  * int_fast16_t res;
162  * uint16_t retValues[ADC_COUNT];
163  * uint8_t i;
164  *
165  * res = ADC_convertChain(adc, retValues, ADC_COUNT);
166  * if (res == ADC_STATUS_SUCCESS)
167  * {
168  * for (i = 0; i < ADC_COUNT; i++) {
169  * print(retValues[i]);
170  * }
171  * }
172  * @endcode
173  *
174  * <hr>
175  ******************************************************************************
176  */
180 /* clang-format off */
181 
182 #ifndef ti_drivers_ADC__include
183 #define ti_drivers_ADC__include
184 
185 #include <stdbool.h>
186 #include <stdint.h>
187 #include <ti/drivers/dpl/SemaphoreP.h>
188 
189 #ifdef __cplusplus
190 extern "C" {
191 #endif
192 
197  #define ADC_convertRawToMicroVolts ADC_convertToMicroVolts
198 
217 #define ADC_CMD_RESERVED (32)
218 
232 #define ADC_STATUS_RESERVED (-32)
233 
242 #define ADC_STATUS_SUCCESS (0)
243 
250 #define ADC_STATUS_ERROR (-1)
251 
259 #define ADC_STATUS_UNDEFINEDCMD (-2)
260 
270 /* Add ADC_CMD_<commands> here */
271 
279 typedef struct ADC_Config_ *ADC_Handle;
280 
289 typedef struct
290 {
291  void *custom;
293  bool isProtected;
300 } ADC_Params;
301 
302 
308 typedef void (*ADC_CloseFxn)(ADC_Handle handle);
309 
315 typedef int_fast16_t (*ADC_ControlFxn)(ADC_Handle handle, uint_fast16_t cmd, void *arg);
316 
322 typedef int_fast16_t (*ADC_ConvertFxn)(ADC_Handle handle, uint16_t *value);
323 
329 typedef int_fast16_t (*ADC_ConvertChainFxn)(ADC_Handle *handleList, uint16_t *dataBuffer, uint8_t channelCount);
330 
336 typedef uint32_t (*ADC_ConvertToMicroVoltsFxn)(ADC_Handle handle, uint16_t adcValue);
337 
343 typedef void (*ADC_InitFxn)(ADC_Handle handle);
344 
350 typedef ADC_Handle (*ADC_OpenFxn)(ADC_Handle handle, ADC_Params *params);
351 
357 typedef struct
358 {
360  ADC_CloseFxn closeFxn;
361 
363  ADC_ControlFxn controlFxn;
364 
366  ADC_ConvertFxn convertFxn;
367 
369  ADC_ConvertChainFxn convertChainFxn;
370 
372  ADC_ConvertToMicroVoltsFxn convertToMicroVolts;
373 
375  ADC_InitFxn initFxn;
376 
378  ADC_OpenFxn openFxn;
379 } ADC_FxnTable;
380 
382 extern const ADC_FxnTable ADCMSPM0_fxnTable;
383 
391 typedef struct ADC_Config_
392 {
396 
398  void *object;
399 
402  void const *hwAttrs;
403 } ADC_Config;
404 
412 extern void ADC_close(ADC_Handle handle);
413 
435 extern int_fast16_t ADC_control(ADC_Handle handle, uint_fast16_t cmd, void *arg);
436 
454 extern int_fast16_t ADC_convert(ADC_Handle handle, uint16_t *value);
455 
476 extern int_fast16_t ADC_convertChain(ADC_Handle *handleList, uint16_t *dataBuffer, uint8_t channelCount);
477 
491 extern uint32_t ADC_convertToMicroVolts(ADC_Handle handle, uint16_t adcValue);
492 
498 extern void ADC_init(void);
499 
517 extern ADC_Handle ADC_open(uint_least8_t index, ADC_Params *params);
518 
528 extern void ADC_Params_init(ADC_Params *params);
529 
530 #ifdef __cplusplus
531 }
532 #endif
533 
534 #endif /* ti_drivers_ADC__include */
535 /* clang-format on */
ADC driver&#39;s custom configuration structure.
Definition: ADC.h:391
ADC_Handle ADC_open(uint_least8_t index, ADC_Params *params)
Function to initialize the ADC peripheral.
ADC Parameters used with ADC_open().
Definition: ADC.h:289
struct ADC_Config_ ADC_Config
ADC driver&#39;s custom configuration structure.
struct ADC_Config_ * ADC_Handle
A handle that is returned from an ADC_open() call.
Definition: ADC.h:279
ADC_FxnTable const * fxnTablePtr
Definition: ADC.h:395
uint32_t ADC_convertToMicroVolts(ADC_Handle handle, uint16_t adcValue)
Function to convert a raw ADC sample into microvolts.
const ADC_FxnTable ADCMSPM0_fxnTable
int_fast16_t ADC_convertChain(ADC_Handle *handleList, uint16_t *dataBuffer, uint8_t channelCount)
Function to perform a multi-channel ADC conversion.
void const * hwAttrs
Definition: ADC.h:402
The definition of an ADC function table that contains the required set of functions to control a spec...
Definition: ADC.h:357
void * object
Definition: ADC.h:398
void ADC_close(ADC_Handle handle)
Function to close an ADC driver instance.
int_fast16_t ADC_convert(ADC_Handle handle, uint16_t *value)
Function to perform an ADC conversion.
void ADC_init(void)
Function to initialize the ADC driver.
void ADC_Params_init(ADC_Params *params)
Initialize an ADC_Params structure to its default values.
bool isProtected
Definition: ADC.h:293
int_fast16_t ADC_control(ADC_Handle handle, uint_fast16_t cmd, void *arg)
Function performs implementation specific features on a driver instance.
void * custom
Definition: ADC.h:291
© Copyright 1995-2023, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale