CC13xx Driver Library
aux_adc.c
Go to the documentation of this file.
1 /******************************************************************************
2 * Filename: aux_adc.c
3 * Revised: 2015-10-14 18:23:25 +0200 (Wed, 14 Oct 2015)
4 * Revision: 44751
5 *
6 * Description: Driver for the AUX Time to Digital Converter interface.
7 *
8 * Copyright (c) 2015, Texas Instruments Incorporated
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 * 1) Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 *
17 * 2) Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 *
21 * 3) Neither the name of the ORGANIZATION nor the names of its contributors may
22 * be used to endorse or promote products derived from this software without
23 * specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 *
37 ******************************************************************************/
38 
39 #include <driverlib/aux_adc.h>
40 #include <inc/hw_memmap.h>
41 #include <inc/hw_aux_wuc.h>
42 #include <inc/hw_fcfg1.h>
43 #include <driverlib/adi.h>
44 #include <driverlib/event.h>
45 
46 //*****************************************************************************
47 //
48 // Handle support for DriverLib in ROM:
49 // This section will undo prototype renaming made in the header file
50 //
51 //*****************************************************************************
52 #if !defined(DOXYGEN)
53  #undef AUXADCDisable
54  #define AUXADCDisable NOROM_AUXADCDisable
55  #undef AUXADCEnableAsync
56  #define AUXADCEnableAsync NOROM_AUXADCEnableAsync
57  #undef AUXADCEnableSync
58  #define AUXADCEnableSync NOROM_AUXADCEnableSync
59  #undef AUXADCDisableInputScaling
60  #define AUXADCDisableInputScaling NOROM_AUXADCDisableInputScaling
61  #undef AUXADCFlushFifo
62  #define AUXADCFlushFifo NOROM_AUXADCFlushFifo
63  #undef AUXADCReadFifo
64  #define AUXADCReadFifo NOROM_AUXADCReadFifo
65  #undef AUXADCPopFifo
66  #define AUXADCPopFifo NOROM_AUXADCPopFifo
67  #undef AUXADCGetAdjustmentGain
68  #define AUXADCGetAdjustmentGain NOROM_AUXADCGetAdjustmentGain
69  #undef AUXADCGetAdjustmentOffset
70  #define AUXADCGetAdjustmentOffset NOROM_AUXADCGetAdjustmentOffset
71  #undef AUXADCValueToMicrovolts
72  #define AUXADCValueToMicrovolts NOROM_AUXADCValueToMicrovolts
73  #undef AUXADCMicrovoltsToValue
74  #define AUXADCMicrovoltsToValue NOROM_AUXADCMicrovoltsToValue
75  #undef AUXADCAdjustValueForGainAndOffset
76  #define AUXADCAdjustValueForGainAndOffset NOROM_AUXADCAdjustValueForGainAndOffset
77  #undef AUXADCUnadjustValueForGainAndOffset
78  #define AUXADCUnadjustValueForGainAndOffset NOROM_AUXADCUnadjustValueForGainAndOffset
79 #endif
80 
81 //*****************************************************************************
82 //
84 //
85 //*****************************************************************************
86 void
88 {
89  // Disable the ADC reference
91 
92  // Assert reset and disable the ADC
94 
95  // Ensure that scaling is enabled by default before next use of the ADC
97 
98  // Disable the ADC clock (no need to wait since IOB_WUC_ADCCLKCTL_ACK goes low immediately)
99  HWREG(AUX_WUC_BASE + AUX_WUC_O_ADCCLKCTL) = 0;
100 
101  // Disable the ADC data interface
102  HWREG(AUX_ANAIF_BASE + AUX_ANAIF_O_ADCCTL) = 0;
103 }
104 
105 //*****************************************************************************
106 //
107 // Enables the ADC for asynchronous operation
108 //
109 //*****************************************************************************
110 void
111 AUXADCEnableAsync(uint32_t refSource, uint32_t trigger)
112 {
113  // Enable the ADC reference, with the following options:
114  // - SRC: Set when using relative reference
115  // - REF_ON_IDLE: Always cleared since there is no idle state in asynchronous operation
117 
118  // Enable the ADC clock
121 
122  // Enable the ADC data interface
123  if (trigger == AUXADC_TRIGGER_MANUAL) {
124  // Manual trigger: No need to configure event routing from GPT
126  } else {
127  // GPT trigger: Configure event routing via MCU_EV to the AUX domain
128  HWREG(EVENT_BASE + EVENT_O_AUXSEL0) = trigger;
130  }
131 
132  // Configure the ADC
134 
135  // Release reset and enable the ADC
137 }
138 
139 //*****************************************************************************
140 //
141 // Enables the ADC for synchronous operation
142 //
143 //*****************************************************************************
144 void
145 AUXADCEnableSync(uint32_t refSource, uint32_t sampleTime, uint32_t trigger)
146 {
147  // Enable the ADC reference, with the following options:
148  // - SRC: Set when using relative reference
149  // - REF_ON_IDLE: Set when using fixed reference and sample time < 21.3 us
150  uint8_t adcref0 = refSource | ADI_4_AUX_ADCREF0_EN_M;
151  if (!refSource && (sampleTime < AUXADC_SAMPLE_TIME_21P3_US)) {
153  }
155 
156  // Enable the ADC clock
159 
160  // Enable the ADC data interface
161  if (trigger == AUXADC_TRIGGER_MANUAL) {
162  // Manual trigger: No need to configure event routing from GPT
164  } else {
165  // GPT trigger: Configure event routing via MCU_EV to the AUX domain
166  HWREG(EVENT_BASE + EVENT_O_AUXSEL0) = trigger;
168  }
169 
170  // Configure the ADC
172 
173  // Release reset and enable the ADC
175 }
176 
177 //*****************************************************************************
178 //
179 // Disables scaling of the ADC input
180 //
181 //*****************************************************************************
182 void
184 {
186 }
187 
188 //*****************************************************************************
189 //
190 // Flushes the ADC FIFO
191 //
192 //*****************************************************************************
193 void
195 {
196  HWREGBITW(AUX_ANAIF_BASE + AUX_ANAIF_O_ADCCTL, 1) = 1; // CMD: EN(1) -> FLUSH(3)
197  HWREGBITW(AUX_ANAIF_BASE + AUX_ANAIF_O_ADCCTL, 1) = 0; // CMD: FLUSH(3) -> EN(1)
198 }
199 
200 //*****************************************************************************
201 //
202 // Waits for and returns the first sample in the ADC FIFO
203 //
204 //*****************************************************************************
205 uint32_t
207 
208  // Wait until there is at least one sample in the FIFO
210 
211  // Return the first sample from the FIFO
212  return HWREG(AUX_ANAIF_BASE + AUX_ANAIF_O_ADCFIFO);
213 }
214 
215 //*****************************************************************************
216 //
217 // Returns the first sample in the ADC FIFO, without waiting
218 //
219 //*****************************************************************************
220 uint32_t
222 
223  // Return the first sample from the FIFO. If the FIFO is empty, this
224  // generates ADC FIFO underflow
225  return HWREG(AUX_ANAIF_BASE + AUX_ANAIF_O_ADCFIFO);
226 }
227 
228 //*****************************************************************************
229 //
230 // Returns the gain value used when adjusting for ADC gain/offset
231 //
232 //*****************************************************************************
233 int32_t
234 AUXADCGetAdjustmentGain(uint32_t refSource)
235 {
236  int32_t gain;
237  if (refSource == AUXADC_REF_FIXED) {
238  // AUXADC_REF_FIXED ==> ABS_GAIN
240  } else {
241  // AUXADC_REF_VDDS_REL ==> REL_GAIN
243  }
244  return gain;
245 }
246 
247 //*****************************************************************************
248 //
249 // Returns the offset value used when adjusting for ADC gain/offset
250 //
251 //*****************************************************************************
252 int32_t
253 AUXADCGetAdjustmentOffset(uint32_t refSource)
254 {
255  int8_t offset;
256  if ( refSource == AUXADC_REF_FIXED ) {
257  // AUXADC_REF_FIXED ==> ABS_OFFSET
259  } else {
260  // AUXADC_REF_VDDS_REL ==> REL_OFFSET
262  }
263  return offset;
264 }
265 
266 //*****************************************************************************
267 //
268 // Converts an "ideal" ADC value to microvolts
269 //
270 //*****************************************************************************
271 int32_t
272 AUXADCValueToMicrovolts(int32_t fixedRefVoltage, int32_t adcValue)
273 {
274  // Chop off 4 bits during calculations to avoid 32-bit overflow
275  fixedRefVoltage >>= 4;
276  return (((adcValue * fixedRefVoltage) + 2047) / 4095) << 4;
277 }
278 
279 //*****************************************************************************
280 //
281 // Converts a number of microvolts to corresponding "ideal" ADC value
282 //
283 //*****************************************************************************
284 int32_t
285 AUXADCMicrovoltsToValue(int32_t fixedRefVoltage, int32_t microvolts)
286 {
287  // Chop off 4 bits during calculations to avoid 32-bit overflow
288  fixedRefVoltage >>= 4;
289  microvolts >>= 4;
290  return ((microvolts * 4095) + (fixedRefVoltage / 2)) / fixedRefVoltage;
291 }
292 
293 //*****************************************************************************
294 //
295 // Performs ADC value gain and offset adjustment
296 //
297 //*****************************************************************************
298 int32_t
299 AUXADCAdjustValueForGainAndOffset(int32_t adcValue, int32_t gain, int32_t offset)
300 {
301  // Apply gain and offset adjustment
302  adcValue = (((adcValue + offset) * gain) + 16384) / 32768;
303 
304  // Saturate
305  if (adcValue < 0) {
306  return 0;
307  } else if (adcValue > 4095) {
308  return 4095;
309  } else {
310  return adcValue;
311  }
312 }
313 
314 //*****************************************************************************
315 //
316 // Performs the inverse of the ADC value gain and offset adjustment
317 //
318 //*****************************************************************************
319 int32_t
320 AUXADCUnadjustValueForGainAndOffset(int32_t adcValue, int32_t gain, int32_t offset)
321 {
322  // Apply inverse gain and offset adjustment
323  adcValue = (((adcValue * 32768) + (gain / 2)) / gain) - offset;
324 
325  // Saturate
326  if (adcValue < 0) {
327  return 0;
328  } else if (adcValue > 4095) {
329  return 4095;
330  } else {
331  return adcValue;
332  }
333 }
static void ADI8BitsClear(uint32_t ui32Base, uint32_t ui32Reg, uint8_t ui8Val)
Clear specific bits in an 8 bit ADI register.
Definition: adi.h:604
int32_t AUXADCAdjustValueForGainAndOffset(int32_t adcValue, int32_t gain, int32_t offset)
Performs ADC value gain and offset adjustment.
Definition: aux_adc.c:299
int32_t AUXADCGetAdjustmentGain(uint32_t refSource)
Returns the gain value used when adjusting for ADC gain/offset.
Definition: aux_adc.c:234
void AUXADCEnableAsync(uint32_t refSource, uint32_t trigger)
Enables the ADC for asynchronous operation.
Definition: aux_adc.c:111
void AUXADCDisable(void)
Disables the ADC.
Definition: aux_adc.c:87
int32_t AUXADCMicrovoltsToValue(int32_t fixedRefVoltage, int32_t microvolts)
Converts a number of microvolts to corresponding "adjusted" ADC value.
Definition: aux_adc.c:285
int32_t AUXADCUnadjustValueForGainAndOffset(int32_t adcValue, int32_t gain, int32_t offset)
Performs the inverse of the ADC value gain and offset adjustment.
Definition: aux_adc.c:320
uint32_t AUXADCPopFifo(void)
Returns the first sample in the ADC FIFO, without waiting.
Definition: aux_adc.c:221
void AUXADCEnableSync(uint32_t refSource, uint32_t sampleTime, uint32_t trigger)
Enables the ADC for synchronous operation.
Definition: aux_adc.c:145
#define AUXADC_SAMPLE_TIME_21P3_US
Definition: aux_adc.h:144
void AUXADCDisableInputScaling(void)
Disables scaling of the ADC input.
Definition: aux_adc.c:183
int32_t AUXADCValueToMicrovolts(int32_t fixedRefVoltage, int32_t adcValue)
Converts an "adjusted" ADC value to microvolts.
Definition: aux_adc.c:272
int32_t AUXADCGetAdjustmentOffset(uint32_t refSource)
Returns the offset value used when adjusting for ADC gain/offset.
Definition: aux_adc.c:253
static void ADI8BitsSet(uint32_t ui32Base, uint32_t ui32Reg, uint8_t ui8Val)
Set specific bits in a single 8 bit ADI register.
Definition: adi.h:433
#define AUXADC_TRIGGER_MANUAL
Definition: aux_adc.h:126
void AUXADCFlushFifo(void)
Flushes the ADC FIFO.
Definition: aux_adc.c:194
uint32_t AUXADCReadFifo(void)
Waits for and returns the first sample in the ADC FIFO.
Definition: aux_adc.c:206
#define AUXADC_REF_FIXED
Definition: aux_adc.h:107