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 * ======== ADC.c ========
34 */
35
36 #include <stdbool.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39
40 #include <ti/drivers/ADC.h>
41 #include <ti/drivers/dpl/HwiP.h>
42
43 extern const ADC_Config ADC_config[];
44 extern const uint_least8_t ADC_count;
45
46 /* Default ADC parameters structure */
47 const ADC_Params ADC_defaultParams = {
48 .custom = NULL,
49 .isProtected = true
50 };
51
52 static bool isInitialized = false;
53
54 /*
55 * ======== ADC_close ========
56 */
ADC_close(ADC_Handle handle)57 void ADC_close(ADC_Handle handle)
58 {
59 ((ADC_FxnTable *)handle->fxnTablePtr)->closeFxn(handle);
60 }
61
62 /*
63 * ======== ADC_control ========
64 */
ADC_control(ADC_Handle handle,uint_fast16_t cmd,void * arg)65 int_fast16_t ADC_control(ADC_Handle handle, uint_fast16_t cmd, void *arg)
66 {
67 return (((ADC_FxnTable *)handle->fxnTablePtr)->controlFxn(handle, cmd, arg));
68 }
69
70 /*
71 * ======== ADC_convert ========
72 */
ADC_convert(ADC_Handle handle,uint16_t * value)73 int_fast16_t ADC_convert(ADC_Handle handle, uint16_t *value)
74 {
75 return (handle->fxnTablePtr->convertFxn(handle, value));
76 }
77
78 /*
79 * ======== ADC_convertToMicroVolts ========
80 */
ADC_convertToMicroVolts(ADC_Handle handle,uint16_t adcValue)81 uint32_t ADC_convertToMicroVolts(ADC_Handle handle, uint16_t adcValue)
82 {
83 return (handle->fxnTablePtr->convertToMicroVolts(handle, adcValue));
84 }
85
86 /*
87 * ======== ADC_init ========
88 */
ADC_init(void)89 void ADC_init(void)
90 {
91 uint_least8_t i;
92 uint_fast32_t key;
93
94 key = HwiP_disable();
95
96 if (!isInitialized) {
97 isInitialized = (bool) true;
98
99 /* Call each driver's init function */
100 for (i = 0; i < ADC_count; i++) {
101 ADC_config[i].fxnTablePtr->initFxn((ADC_Handle)&(ADC_config[i]));
102 }
103 }
104
105 HwiP_restore(key);
106 }
107
108 /*
109 * ======== ADC_open ========
110 */
ADC_open(uint_least8_t index,ADC_Params * params)111 ADC_Handle ADC_open(uint_least8_t index, ADC_Params *params)
112 {
113 ADC_Handle handle = NULL;
114
115 if (isInitialized && (index < ADC_count)) {
116 /* If params are NULL use defaults */
117 if (params == NULL) {
118 params = (ADC_Params *) &ADC_defaultParams;
119 }
120
121 /* Get handle for this driver instance */
122 handle = (ADC_Handle) &(ADC_config[index]);
123 handle = handle->fxnTablePtr->openFxn(handle, params);
124 }
125
126 return (handle);
127 }
128
129 /*
130 * ======== ADC_Params_init ========
131 */
ADC_Params_init(ADC_Params * params)132 void ADC_Params_init(ADC_Params *params)
133 {
134 *params = ADC_defaultParams;
135 }
136