1 /******************************************************************************
2 * Filename: adc.h
3 *
4 * Description: Prototypes and defines for the ADC API.
5 *
6 * Copyright (c) 2022 Texas Instruments Incorporated
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 *
14 * 2) Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 *
18 * 3) Neither the name of the copyright holder nor the names of its
19 * contributors may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 ******************************************************************************/
35
36 #ifndef __ADC_H__
37 #define __ADC_H__
38
39 //*****************************************************************************
40 //
41 //! \addtogroup peripheral_group
42 //! @{
43 //! \addtogroup adc_api
44 //! @{
45 //
46 //*****************************************************************************
47
48 #include <stdint.h>
49 #include "../inc/hw_adc.h"
50 #include "../inc/hw_fcfg.h"
51 #include "../inc/hw_memmap.h"
52 #include "../inc/hw_sys0.h"
53 #include "../inc/hw_types.h"
54
55 //*****************************************************************************
56 //
57 // If building with a C++ compiler, make all of the definitions in this header
58 // have a C binding.
59 //
60 //*****************************************************************************
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64
65 //*****************************************************************************
66 //
67 // Values
68 //
69 //*****************************************************************************
70
71 //! \brief Set ADC clock to system clock divided by 1
72 #define ADC_CLOCK_DIVIDER_1 ADC_CTL0_SCLKDIV_DIV_BY_1
73
74 //! \brief Set ADC clock to system clock divided by 2
75 #define ADC_CLOCK_DIVIDER_2 ADC_CTL0_SCLKDIV_DIV_BY_2
76
77 //! \brief Set ADC clock to system clock divided by 4
78 #define ADC_CLOCK_DIVIDER_4 ADC_CTL0_SCLKDIV_DIV_BY_4
79
80 //! \brief Set ADC clock to system clock divided by 8
81 #define ADC_CLOCK_DIVIDER_8 ADC_CTL0_SCLKDIV_DIV_BY_8
82
83 //! \brief Set ADC clock to system clock divided by 16
84 #define ADC_CLOCK_DIVIDER_16 ADC_CTL0_SCLKDIV_DIV_BY_16
85
86 //! \brief Set ADC clock to system clock divided by 24
87 #define ADC_CLOCK_DIVIDER_24 ADC_CTL0_SCLKDIV_DIV_BY_24
88
89 //! \brief Set ADC clock to system clock divided by 32
90 #define ADC_CLOCK_DIVIDER_32 ADC_CTL0_SCLKDIV_DIV_BY_32
91
92 //! \brief Set ADC clock to system clock divided by 48
93 #define ADC_CLOCK_DIVIDER_48 ADC_CTL0_SCLKDIV_DIV_BY_48
94
95 //! \brief Set resolution of ADC conversion to 12 bit (unsigned)
96 #define ADC_RESOLUTION_12_BIT ADC_CTL2_RES_BIT_12
97
98 //! \brief Set resolution of ADC conversion to 10 bit (unsigned)
99 #define ADC_RESOLUTION_10_BIT ADC_CTL2_RES_BIT_10
100
101 //! \brief Set resolution of ADC conversion to 8 bit (unsigned)
102 #define ADC_RESOLUTION_8_BIT ADC_CTL2_RES_BIT_8
103
104 //! \brief Set ADC reference to internal fixed 1.4V
105 #define ADC_FIXED_REFERENCE_1V4 0
106
107 //! \brief Set ADC reference to internal fixed 2.5V
108 #define ADC_FIXED_REFERENCE_2V5 1
109
110 //! \brief Set ADC reference to external analog pin A1
111 #define ADC_EXTERNAL_REFERENCE 2
112
113 //! \brief Set ADC reference to VDDS
114 #define ADC_VDDS_REFERENCE 3
115
116 //! \brief Set ADC conversion sequence to repeat control registers defined by start and stop address,
117 //! as set by \ref ADCSetMemctlRange
118 #define ADC_SEQUENCE_REPEATSEQUENCE ADC_CTL1_CONSEQ_REPEATSEQUENCE
119
120 //! \brief Set ADC conversion sequence to repeat control register defined by start as set by \ref ADCSetMemctlRange
121 #define ADC_SEQUENCE_REPEATSINGLE ADC_CTL1_CONSEQ_REPEATSINGLE
122
123 //! \brief Set ADC conversion sequence to a single pass of control registers defined by start and stop address,
124 //! as set by \ref ADCSetMemctlRange
125 #define ADC_SEQUENCE_SEQUENCE ADC_CTL1_CONSEQ_SEQUENCE
126
127 //! \brief Set ADC conversion sequence to do a single conversion of control register defined by start,
128 //! as set by \ref ADCSetMemctlRange
129 #define ADC_SEQUENCE_SINGLE ADC_CTL1_CONSEQ_SINGLE
130
131 //! \brief Result ready in memory result register 23
132 #define ADC_INT_MEMRES_23 ADC_IMASK0_MEMRESIFG23
133
134 //! \brief Result ready in memory result register 22
135 #define ADC_INT_MEMRES_22 ADC_IMASK0_MEMRESIFG22
136
137 //! \brief Result ready in memory result register 21
138 #define ADC_INT_MEMRES_21 ADC_IMASK0_MEMRESIFG21
139
140 //! \brief Result ready in memory result register 20
141 #define ADC_INT_MEMRES_20 ADC_IMASK0_MEMRESIFG20
142
143 //! \brief Result ready in memory result register 19
144 #define ADC_INT_MEMRES_19 ADC_IMASK0_MEMRESIFG19
145
146 //! \brief Result ready in memory result register 18
147 #define ADC_INT_MEMRES_18 ADC_IMASK0_MEMRESIFG18
148
149 //! \brief Result ready in memory result register 17
150 #define ADC_INT_MEMRES_17 ADC_IMASK0_MEMRESIFG17
151
152 //! \brief Result ready in memory result register 16
153 #define ADC_INT_MEMRES_16 ADC_IMASK0_MEMRESIFG16
154
155 //! \brief Result ready in memory result register 15
156 #define ADC_INT_MEMRES_15 ADC_IMASK0_MEMRESIFG15
157
158 //! \brief Result ready in memory result register 14
159 #define ADC_INT_MEMRES_14 ADC_IMASK0_MEMRESIFG14
160
161 //! \brief Result ready in memory result register 13
162 #define ADC_INT_MEMRES_13 ADC_IMASK0_MEMRESIFG13
163
164 //! \brief Result ready in memory result register 12
165 #define ADC_INT_MEMRES_12 ADC_IMASK0_MEMRESIFG12
166
167 //! \brief Result ready in memory result register 11
168 #define ADC_INT_MEMRES_11 ADC_IMASK0_MEMRESIFG11
169
170 //! \brief Result ready in memory result register 10
171 #define ADC_INT_MEMRES_10 ADC_IMASK0_MEMRESIFG10
172
173 //! \brief Result ready in memory result register 9
174 #define ADC_INT_MEMRES_09 ADC_IMASK0_MEMRESIFG9
175
176 //! \brief Result ready in memory result register 8
177 #define ADC_INT_MEMRES_08 ADC_IMASK0_MEMRESIFG8
178
179 //! \brief Result ready in memory result register 7
180 #define ADC_INT_MEMRES_07 ADC_IMASK0_MEMRESIFG7
181
182 //! \brief Result ready in memory result register 6
183 #define ADC_INT_MEMRES_06 ADC_IMASK0_MEMRESIFG6
184
185 //! \brief Result ready in memory result register 5
186 #define ADC_INT_MEMRES_05 ADC_IMASK0_MEMRESIFG5
187
188 //! \brief Result ready in memory result register 4
189 #define ADC_INT_MEMRES_04 ADC_IMASK0_MEMRESIFG4
190
191 //! \brief Result ready in memory result register 3
192 #define ADC_INT_MEMRES_03 ADC_IMASK0_MEMRESIFG3
193
194 //! \brief Result ready in memory result register 2
195 #define ADC_INT_MEMRES_02 ADC_IMASK0_MEMRESIFG2
196
197 //! \brief Result ready in memory result register 1
198 #define ADC_INT_MEMRES_01 ADC_IMASK0_MEMRESIFG1
199
200 //! \brief Result ready in memory result register 0
201 #define ADC_INT_MEMRES_00 ADC_IMASK0_MEMRESIFG0
202
203 //! \brief Ad-Hoc single conversion done
204 #define ADC_INT_ASCDONE ADC_IMASK0_ASCDONE
205
206 //! \brief Conversion underflow
207 #define ADC_INT_UVIFG ADC_IMASK0_UVIFG
208
209 //! \brief DMA transaction done
210 #define ADC_INT_DMADONE ADC_IMASK0_DMADONE
211
212 //! \brief ADC result is inside window comparator range
213 #define ADC_INT_INIFG ADC_IMASK0_INIFG
214
215 //! \brief ADC result is below window comparator range
216 #define ADC_INT_LOWIFG ADC_IMASK0_LOWIFG
217
218 //! \brief ADC result is above window comparator range
219 #define ADC_INT_HIGHIFG ADC_IMASK0_HIGHIFG
220
221 //! \brief Sequence conversion timeout overflow
222 #define ADC_INT_TOVIFG ADC_IMASK0_TOVIFG
223
224 //! \brief Conversion overflow
225 #define ADC_INT_OVIFG ADC_IMASK0_OVIFG
226
227 //*****************************************************************************
228 //
229 // API Functions and prototypes
230 //
231 //*****************************************************************************
232
233 //*****************************************************************************
234 //
235 //! \brief Sets the clock-divider value, and sample duration
236 //!
237 //! This function sets the clock divider, which determines the ADC clock,
238 //! derived from the system clock, and sets the duration of a sample in
239 //! ADC-clock cycles
240 //!
241 //! \param clkDiv is the clock divider value
242 //! - \ref ADC_CLOCK_DIVIDER_1
243 //! - \ref ADC_CLOCK_DIVIDER_2
244 //! - \ref ADC_CLOCK_DIVIDER_4
245 //! - \ref ADC_CLOCK_DIVIDER_8
246 //! - \ref ADC_CLOCK_DIVIDER_16
247 //! - \ref ADC_CLOCK_DIVIDER_24
248 //! - \ref ADC_CLOCK_DIVIDER_32
249 //! - \ref ADC_CLOCK_DIVIDER_48
250 //!
251 //! \param clkCycles is the duration of a sample, in ADC-clock cycles.
252 //! Valid range of input is [0, 1023]
253 //!
254 //! \note
255 //! The numerical value of clkDiv is not the actual divider value.
256 //! See the list of possible arguments and which divider value they represent.
257 //!
258 //! \note
259 //! The minimum sampling time for the ADC is 250 ns. The clock-divider and sample
260 //! duration must be set accordingly to maintain this requirement.
261 //!
262 //! \return None
263 //
264 //*****************************************************************************
265 extern void ADCSetSampleDuration(uint32_t clkDiv, uint16_t clkCycles);
266
267 //*****************************************************************************
268 //
269 //! \brief Sets the ADC bit resolution
270 //!
271 //! This function sets the resolution of the ADC conversion.
272 //!
273 //! \param resolution Bit resolution to be used in conversion
274 //! - \ref ADC_RESOLUTION_12_BIT
275 //! - \ref ADC_RESOLUTION_10_BIT
276 //! - \ref ADC_RESOLUTION_8_BIT
277 //!
278 //! \note
279 //! The resolution will affect how long a conversion will take.
280 //! - 12 bit: 14 clock cycles
281 //! - 10 bit: 12 clock cycles
282 //! - 8 bit: 9 clock cycles
283 //!
284 //! \return None
285 //
286 //*****************************************************************************
287 extern void ADCSetResolution(uint32_t resolution);
288
289 //*****************************************************************************
290 //
291 //! \brief Sets the ADC reference source and input channel
292 //!
293 //! This function sets the ADC reference and input channel. The control register
294 //! index that the settings are applied to must also be passed as a parameter
295 //!
296 //! \param reference Reference source used in conversion
297 //! - \ref ADC_FIXED_REFERENCE_1V4
298 //! - \ref ADC_FIXED_REFERENCE_2V5
299 //! - \ref ADC_VDDS_REFERENCE
300 //! - \ref ADC_EXTERNAL_REFERENCE
301 //!
302 //! \param channel Internal channels that can be muxed to ADC.
303 //! Channels 0-11 correspond to analog pins 0-11. See device data for more information.
304 //!
305 //! \param index Index of which control register to write to. See device data for valid indexes.
306 //!
307 //! \return None
308 //
309 //*****************************************************************************
310 extern void ADCSetInput(uint32_t reference, uint8_t channel, uint32_t index);
311
312 //*****************************************************************************
313 //
314 //! \brief Set start and stop control registers
315 //!
316 //! This function selects which control registers should be selected for
317 //! a conversion. Valid indexes are [0, 3]. For a single conversion, start and stop
318 //! should be set to the same.
319 //!
320 //! \param start the index of first control register used in sequence
321 //! \param stop the index of last control register used in sequence
322 //!
323 //! \return None
324 //
325 //*****************************************************************************
326 extern void ADCSetMemctlRange(uint32_t start, uint32_t stop);
327
328 //*****************************************************************************
329 //
330 //! \brief Set conversion sequence
331 //!
332 //! This function sets the sequence for ADC conversions. The actual sequence is
333 //! defined by \ref ADCSetMemctlRange. For a single conversion, start and stop
334 //! should be set to the same.
335 //!
336 //! \param sequence
337 //! - \ref ADC_SEQUENCE_REPEATSEQUENCE
338 //! - \ref ADC_SEQUENCE_REPEATSINGLE
339 //! - \ref ADC_SEQUENCE_SEQUENCE
340 //! - \ref ADC_SEQUENCE_SINGLE
341 //!
342 //! \return None
343 //
344 //*****************************************************************************
345 extern void ADCSetSequence(uint32_t sequence);
346
347 //*****************************************************************************
348 //
349 //! \brief Triggers an ADC conversion
350 //!
351 //! This function manually triggers an ADC conversion sequence, based on the
352 //! settings in the control registers in the start and stop range. See
353 //! \ref ADCSetMemctlRange and \ref ADCSetSequence
354 //!
355 //! \note It takes a minimum of 9 system-clock cycles for the BUSY-bit
356 //! in the STATUS register to go high after calling this function.
357 //!
358 //! \return None
359 //
360 //*****************************************************************************
361 extern void ADCManualTrigger(void);
362
363 //*****************************************************************************
364 //
365 //! \brief Read conversion result from ADC
366 //!
367 //! This function blocks until a conversion is done, and returns data
368 //! from the given memory register. The index corresponds to the selected
369 //! control register used for the conversion
370 //!
371 //! \param index Index of which memory result register to read from
372 //!
373 //! \return Raw ADC conversion result
374 //
375 //*****************************************************************************
ADCReadResult(uint32_t index)376 __STATIC_INLINE uint32_t ADCReadResult(uint32_t index)
377 {
378
379 while (HWREG(ADC_BASE + ADC_O_STA) & ADC_STA_BUSY_ACTIVE) {}
380
381 /* Return data from result register */
382 return HWREG(ADC_BASE + ADC_O_MEMRES0 + (4 * index));
383 }
384
385 //*****************************************************************************
386 //
387 //! \brief Check if ADC is busy
388 //!
389 //! This function returns whether the ADC is busy or not.
390 //!
391 //! \return ADC Busy status
392 //! true: ADC sampling or conversion is in progress.
393 //! false: No ADC sampling or conversion in progress.
394 //!
395 //! \note It takes a minimum of 9 system-clock cycles between writing to the
396 //! START-bit, and the BUSY-bit in the STATUS register going high
397 //
398 //*****************************************************************************
ADCIsBusy(void)399 __STATIC_INLINE bool ADCIsBusy(void)
400 {
401
402 if (HWREG(ADC_BASE + ADC_O_STA) & ADC_STA_BUSY_ACTIVE)
403 {
404 return true;
405 }
406 else
407 {
408 return false;
409 }
410 }
411
412 //*****************************************************************************
413 //
414 //! \brief Read conversion result from ADC
415 //!
416 //! This function returns data from the given memory register without blocking.
417 //! The index corresponds to the selected control register used for the conversion
418 //!
419 //! \param index Index of which memory result register to read from
420 //!
421 //! \return Raw ADC conversion result
422 //
423 //*****************************************************************************
ADCReadResultNonBlocking(uint32_t index)424 __STATIC_INLINE uint32_t ADCReadResultNonBlocking(uint32_t index)
425 {
426 /* Return data from result register */
427 return HWREG(ADC_BASE + ADC_O_MEMRES0 + (4 * index));
428 }
429
430 //*****************************************************************************
431 //
432 //! \brief Convert ADC code to microvolts
433 //!
434 //! This function converts an adjusted ADC code to microvolts. Function arguments
435 //! also include bit resolution and reference voltage
436 //!
437 //! \param adcCode Raw adjusted adc code
438 //! \param bitResolution Bit resolution used in conversion
439 //! \param referenceVoltageMicroVolt Reference voltage (microvolts)
440 //!
441 //! \return ADC result in microvolts
442 //
443 //*****************************************************************************
444 extern uint32_t ADCValueToMicrovolts(uint32_t adcCode, uint32_t bitResolution, uint32_t referenceVoltageMicroVolt);
445
446 //*****************************************************************************
447 //
448 //! \brief Enables individual ADC interrupt sources.
449 //!
450 //! This function enables the indicated ADC interrupt sources.
451 //!
452 //! \param intFlags is the bit mask of the interrupt sources to be enabled.
453 //! The parameter is the bitwise OR of any of the following:
454 //! - ADC_INT_MEMRES_N (\ref ADC_INT_MEMRES_00, \ref ADC_INT_MEMRES_01, etc)
455 //! - \ref ADC_INT_ASCDONE
456 //! - \ref ADC_INT_UVIFG
457 //! - \ref ADC_INT_DMADONE
458 //! - \ref ADC_INT_INIFG
459 //! - \ref ADC_INT_LOWIFG
460 //! - \ref ADC_INT_HIGHIFG
461 //! - \ref ADC_INT_TOVIFG
462 //! - \ref ADC_INT_OVIFG
463 //!
464 //! \return None
465 //
466 //*****************************************************************************
ADCEnableInterrupt(uint32_t intFlags)467 __STATIC_INLINE void ADCEnableInterrupt(uint32_t intFlags)
468 {
469 // Enable the specified interrupts.
470 HWREG(ADC_BASE + ADC_O_IMASK0) |= intFlags;
471 }
472
473 //*****************************************************************************
474 //
475 //! \brief Disables individual ADC interrupt sources.
476 //!
477 //! This function disables the indicated ADC interrupt sources.
478 //!
479 //! \param intFlags is the bit mask of the interrupt sources to be disabled.
480 //! The parameter is the bitwise OR of any of the following:
481 //! - ADC_INT_MEMRES_N (\ref ADC_INT_MEMRES_00, \ref ADC_INT_MEMRES_01, etc)
482 //! - \ref ADC_INT_ASCDONE
483 //! - \ref ADC_INT_UVIFG
484 //! - \ref ADC_INT_DMADONE
485 //! - \ref ADC_INT_INIFG
486 //! - \ref ADC_INT_LOWIFG
487 //! - \ref ADC_INT_HIGHIFG
488 //! - \ref ADC_INT_TOVIFG
489 //! - \ref ADC_INT_OVIFG
490 //!
491 //! \return None
492 //
493 //*****************************************************************************
ADCDisableInterrupt(uint32_t intFlags)494 __STATIC_INLINE void ADCDisableInterrupt(uint32_t intFlags)
495 {
496 // Disable the specified interrupts.
497 HWREG(ADC_BASE + ADC_O_IMASK0) &= ~(intFlags);
498 }
499
500 //*****************************************************************************
501 //
502 //! \brief Gets the current raw interrupt status.
503 //!
504 //! This function returns the raw interrupt status for the ADC
505 //!
506 //! \return Returns the current interrupt status, enumerated as a bit field of:
507 //! - ADC_INT_MEMRES_N (\ref ADC_INT_MEMRES_00, \ref ADC_INT_MEMRES_01, etc)
508 //! - \ref ADC_INT_ASCDONE
509 //! - \ref ADC_INT_UVIFG
510 //! - \ref ADC_INT_DMADONE
511 //! - \ref ADC_INT_INIFG
512 //! - \ref ADC_INT_LOWIFG
513 //! - \ref ADC_INT_HIGHIFG
514 //! - \ref ADC_INT_TOVIFG
515 //! - \ref ADC_INT_OVIFG
516 //
517 //*****************************************************************************
ADCRawInterruptStatus(void)518 __STATIC_INLINE uint32_t ADCRawInterruptStatus(void)
519 {
520 return (HWREG(ADC_BASE + ADC_O_RIS0));
521 }
522
523 //*****************************************************************************
524 //
525 //! \brief Gets the current masked interrupt status.
526 //!
527 //! This function returns the masked interrupt status for the ADC
528 //!
529 //! \return Returns the current interrupt status, enumerated as a bit field of:
530 //! - ADC_INT_MEMRES_N (\ref ADC_INT_MEMRES_00, \ref ADC_INT_MEMRES_01, etc)
531 //! - \ref ADC_INT_ASCDONE
532 //! - \ref ADC_INT_UVIFG
533 //! - \ref ADC_INT_DMADONE
534 //! - \ref ADC_INT_INIFG
535 //! - \ref ADC_INT_LOWIFG
536 //! - \ref ADC_INT_HIGHIFG
537 //! - \ref ADC_INT_TOVIFG
538 //! - \ref ADC_INT_OVIFG
539 //
540 //*****************************************************************************
ADCMaskedInterruptStatus(void)541 __STATIC_INLINE uint32_t ADCMaskedInterruptStatus(void)
542 {
543 return (HWREG(ADC_BASE + ADC_O_MIS0));
544 }
545
546 //*****************************************************************************
547 //
548 //! \brief Clears ADC interrupt sources.
549 //!
550 //! The specified ADC interrupt sources are cleared, so that they no longer
551 //! assert. This function must be called in the interrupt handler to keep the
552 //! interrupt from being recognized again immediately upon exit.
553 //!
554 //! \note Due to write buffers and synchronizers in the system it may take several
555 //! clock cycles from a register write clearing an event in a module and until the
556 //! event is actually cleared in the NVIC of the system CPU. It is recommended to
557 //! clear the event source early in the interrupt service routine (ISR) to allow
558 //! the event clear to propagate to the NVIC before returning from the ISR.
559 //! At the same time, an early event clear allows new events of the same type to be
560 //! pended instead of ignored if the event is cleared later in the ISR.
561 //! It is the responsibility of the programmer to make sure that enough time has passed
562 //! before returning from the ISR to avoid false re-triggering of the cleared event.
563 //! A simple, although not necessarily optimal, way of clearing an event before
564 //! returning from the ISR is:
565 //! -# Write to clear event (interrupt source). (buffered write)
566 //! -# Dummy read from the event source module. (making sure the write has propagated)
567 //! -# Wait two system CPU clock cycles (user code or two NOPs). (allowing cleared event to propagate through any
568 //! synchronizers)
569 //!
570 //! \param intFlags is a bit mask of the interrupt sources to be cleared.
571 //! - ADC_INT_MEMRES_N (\ref ADC_INT_MEMRES_00, \ref ADC_INT_MEMRES_01, etc)
572 //! - \ref ADC_INT_ASCDONE
573 //! - \ref ADC_INT_UVIFG
574 //! - \ref ADC_INT_DMADONE
575 //! - \ref ADC_INT_INIFG
576 //! - \ref ADC_INT_LOWIFG
577 //! - \ref ADC_INT_HIGHIFG
578 //! - \ref ADC_INT_TOVIFG
579 //! - \ref ADC_INT_OVIFG
580 //!
581 //! \return None
582 //
583 //*****************************************************************************
ADCClearInterrupt(uint32_t intFlags)584 __STATIC_INLINE void ADCClearInterrupt(uint32_t intFlags)
585 {
586 // Clear the requested interrupt sources
587 HWREG(ADC_BASE + ADC_O_ICLR0) = intFlags;
588 }
589
590 //*****************************************************************************
591 //
592 //! \brief Enable DMA trigger for data transfer.
593 //!
594 //! This function enables DMA trigger for data transfer. DMAEN bit is cleared by hardware
595 //! based on DMA done signal at the end of data transfer. Software has to re-enable DMAEN
596 //! bit for ADC to generate DMA triggers.
597 //!
598 //! \return None
599 //
600 //*****************************************************************************
ADCEnableDMATrigger(void)601 __STATIC_INLINE void ADCEnableDMATrigger(void)
602 {
603 HWREG(ADC_BASE + ADC_O_CTL2) |= ADC_CTL2_DMAEN;
604 }
605
606 //*****************************************************************************
607 //
608 //! \brief Enables individual ADC interrupt sources for DMA Trigger Event Publisher (INT_EVENT2).
609 //!
610 //! This function enables the indicated ADC interrupt sources (INT_EVENT2).
611 //!
612 //! \param intFlags is the bit mask of the interrupt sources to be enabled.
613 //! The parameter is the bitwise OR of any of the following:
614 //! - ADC_INT_MEMRES_N (\ref ADC_INT_MEMRES_00, \ref ADC_INT_MEMRES_01, etc)
615 //!
616 //! \return None
617 //
618 //*****************************************************************************
ADCEnableDMAInterrupt(uint32_t intFlags)619 __STATIC_INLINE void ADCEnableDMAInterrupt(uint32_t intFlags)
620 {
621 // Enable the specified interrupts.
622 HWREG(ADC_BASE + ADC_O_IMASK2) |= intFlags;
623 }
624
625 //*****************************************************************************
626 //
627 //! \brief Disables individual ADC interrupt sources for DMA Trigger Event Publisher (INT_EVENT2).
628 //!
629 //! This function disables the indicated ADC interrupt sources (INT_EVENT2).
630 //!
631 //! \param intFlags is the bit mask of the interrupt sources to be disabled.
632 //! The parameter is the bitwise OR of any of the following:
633 //! - ADC_INT_MEMRES_N (\ref ADC_INT_MEMRES_00, \ref ADC_INT_MEMRES_01, etc)
634 //!
635 //! \return None
636 //
637 //*****************************************************************************
ADCDisableDMAInterrupt(uint32_t intFlags)638 __STATIC_INLINE void ADCDisableDMAInterrupt(uint32_t intFlags)
639 {
640 // Disable the specified interrupts.
641 HWREG(ADC_BASE + ADC_O_IMASK2) &= ~(intFlags);
642 }
643
644 //*****************************************************************************
645 //
646 //! \brief Gets the current raw interrupt status for DMA Trigger Event Publisher (INT_EVENT2).
647 //!
648 //! This function returns the raw interrupt status for the ADC (INT_EVENT2).
649 //!
650 //! \return Returns the current interrupt status, enumerated as a bit field of:
651 //! - ADC_INT_MEMRES_N (\ref ADC_INT_MEMRES_00, \ref ADC_INT_MEMRES_01, etc)
652 //
653 //*****************************************************************************
ADCRawDMAInterruptStatus(void)654 __STATIC_INLINE uint32_t ADCRawDMAInterruptStatus(void)
655 {
656 return (HWREG(ADC_BASE + ADC_O_RIS2));
657 }
658
659 //*****************************************************************************
660 //
661 //! \brief Gets the current masked interrupt status for DMA Trigger Event Publisher (INT_EVENT2).
662 //!
663 //! This function returns the masked interrupt status for the ADC (INT_EVENT2).
664 //!
665 //! \return Returns the current interrupt status, enumerated as a bit field of:
666 //! - ADC_INT_MEMRES_N (\ref ADC_INT_MEMRES_00, \ref ADC_INT_MEMRES_01, etc)
667 //
668 //*****************************************************************************
ADCMaskedDMAInterruptStatus(void)669 __STATIC_INLINE uint32_t ADCMaskedDMAInterruptStatus(void)
670 {
671 return (HWREG(ADC_BASE + ADC_O_MIS2));
672 }
673
674 //*****************************************************************************
675 //
676 //! \brief Clears ADC interrupt sources for DMA Trigger Event Publisher (INT_EVENT2).
677 //!
678 //! The specified ADC interrupt sources are cleared, so that they no longer
679 //! assert. This function must be called in the interrupt handler to keep the
680 //! interrupt from being recognized again immediately upon exit.
681 //!
682 //! \note Due to write buffers and synchronizers in the system it may take several
683 //! clock cycles from a register write clearing an event in a module and until the
684 //! event is actually cleared in the NVIC of the system CPU. It is recommended to
685 //! clear the event source early in the interrupt service routine (ISR) to allow
686 //! the event clear to propagate to the NVIC before returning from the ISR.
687 //! At the same time, an early event clear allows new events of the same type to be
688 //! pended instead of ignored if the event is cleared later in the ISR.
689 //! It is the responsibility of the programmer to make sure that enough time has passed
690 //! before returning from the ISR to avoid false re-triggering of the cleared event.
691 //! A simple, although not necessarily optimal, way of clearing an event before
692 //! returning from the ISR is:
693 //! -# Write to clear event (interrupt source). (buffered write)
694 //! -# Dummy read from the event source module. (making sure the write has propagated)
695 //! -# Wait two system CPU clock cycles (user code or two NOPs). (allowing cleared event to propagate through any
696 //! synchronizers)
697 //!
698 //! \param intFlags is a bit mask of the interrupt sources to be cleared.
699 //! - ADC_INT_MEMRES_N (\ref ADC_INT_MEMRES_00, \ref ADC_INT_MEMRES_01, etc)
700 //!
701 //! \return None
702 //
703 //*****************************************************************************
ADCClearDMAInterrupt(uint32_t intFlags)704 __STATIC_INLINE void ADCClearDMAInterrupt(uint32_t intFlags)
705 {
706 // Clear the requested interrupt sources
707 HWREG(ADC_BASE + ADC_O_ICLR2) = intFlags;
708 }
709
710 //*****************************************************************************
711 //
712 //! \brief Returns ADC gain value for given reference
713 //!
714 //! This returns a gain value that should be passed to \ref ADCAdjustValueForGain.
715 //! The gain is dependant on the reference source
716 //!
717 //! \param reference reference source used in conversion
718 //! - \ref ADC_FIXED_REFERENCE_1V4
719 //! - \ref ADC_FIXED_REFERENCE_2V5
720 //! - \ref ADC_VDDS_REFERENCE
721 //! - \ref ADC_EXTERNAL_REFERENCE
722 //!
723 //! \return Gain value
724 //
725 //*****************************************************************************
726 extern uint16_t ADCGetAdjustmentGain(uint32_t reference);
727
728 //*****************************************************************************
729 //
730 //! \brief Write correct offset value to ADC-peripheral trim register
731 //!
732 //! The ADC peripheral relies on an offset trim value in \ref SYS0_O_TMUTE2. This value
733 //! needs to be set depending on which reference source is used in the conversion
734 //!
735 //! \param reference reference source used in conversion
736 //! - \ref ADC_FIXED_REFERENCE_1V4
737 //! - \ref ADC_FIXED_REFERENCE_2V5
738 //! - \ref ADC_VDDS_REFERENCE
739 //! - \ref ADC_EXTERNAL_REFERENCE
740 //
741 //*****************************************************************************
742 extern void ADCSetAdjustmentOffset(uint32_t reference);
743
744 //*****************************************************************************
745 //
746 //! \brief Performs ADC value gain adjustment.
747 //!
748 //! This function takes a measured ADC value and compensates for the internal gain
749 //! in the ADC.
750 //!
751 //! \param adcValue
752 //! ADC unadjusted value
753 //! \param bitResolution
754 //! ADC bit resolution
755 //! \param gain
756 //! Gain adjustment value provided by \ref ADCGetAdjustmentGain()
757 //!
758 //! \return
759 //! ADC adjusted value
760 //
761 //*****************************************************************************
762 extern uint32_t ADCAdjustValueForGain(uint32_t adcValue, uint32_t bitResolution, uint16_t gain);
763
764 //*****************************************************************************
765 //
766 // Mark the end of the C bindings section for C++ compilers.
767 //
768 //*****************************************************************************
769 #ifdef __cplusplus
770 }
771 #endif
772
773 //*****************************************************************************
774 //
775 //! Close the Doxygen group.
776 //! @}
777 //! @}
778 //
779 //*****************************************************************************
780
781 #endif // __ADC_H__
782