1 //*****************************************************************************
2 //
3 // adc.c - Driver for the ADC.
4 //
5 // Copyright (c) 2005-2020 Texas Instruments Incorporated. All rights reserved.
6 // Software License Agreement
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright
16 // notice, this list of conditions and the following disclaimer in the
17 // documentation and/or other materials provided with the
18 // distribution.
19 //
20 // Neither the name of Texas Instruments Incorporated nor the names of
21 // its contributors may be used to endorse or promote products derived
22 // from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 //
36 // This is part of revision 2.2.0.295 of the Tiva Peripheral Driver Library.
37 //
38 //*****************************************************************************
39
40 //*****************************************************************************
41 //
42 //! \addtogroup adc_api
43 //! @{
44 //
45 //*****************************************************************************
46
47 #include <stdbool.h>
48 #include <stdint.h>
49 #include "inc/hw_adc.h"
50 #include "inc/hw_ints.h"
51 #include "inc/hw_memmap.h"
52 #include "inc/hw_types.h"
53 #include "inc/hw_sysctl.h"
54 #include "driverlib/adc.h"
55 #include "driverlib/debug.h"
56 #include "driverlib/interrupt.h"
57
58 //*****************************************************************************
59 //
60 // These defines are used by the ADC driver to simplify access to the ADC
61 // sequencer's registers.
62 //
63 //*****************************************************************************
64 #define ADC_SEQ (ADC_O_SSMUX0)
65 #define ADC_SEQ_STEP (ADC_O_SSMUX1 - ADC_O_SSMUX0)
66 #define ADC_SSMUX (ADC_O_SSMUX0 - ADC_O_SSMUX0)
67 #define ADC_SSEMUX (ADC_O_SSEMUX0 - ADC_O_SSMUX0)
68 #define ADC_SSCTL (ADC_O_SSCTL0 - ADC_O_SSMUX0)
69 #define ADC_SSFIFO (ADC_O_SSFIFO0 - ADC_O_SSMUX0)
70 #define ADC_SSFSTAT (ADC_O_SSFSTAT0 - ADC_O_SSMUX0)
71 #define ADC_SSOP (ADC_O_SSOP0 - ADC_O_SSMUX0)
72 #define ADC_SSDC (ADC_O_SSDC0 - ADC_O_SSMUX0)
73 #define ADC_SSTSH (ADC_O_SSTSH0 - ADC_O_SSMUX0)
74
75 //*****************************************************************************
76 //
77 // The currently configured software oversampling factor for each of the ADC
78 // sequencers.
79 //
80 //*****************************************************************************
81 static uint8_t g_pui8OversampleFactor[2][3];
82
83 //*****************************************************************************
84 //
85 //! Returns the interrupt number for a given ADC base address and sequence
86 //! number.
87 //!
88 //! \param ui32Base is the base address of the ADC module.
89 //! \param ui32SequenceNum is the sample sequence number.
90 //!
91 //! This function returns the interrupt number for the ADC module and sequence
92 //! number provided in the \e ui32Base and \e ui32SequenceNum parameters.
93 //!
94 //! \return Returns the ADC sequence interrupt number or 0 if the interrupt
95 //! does not exist.
96 //
97 //*****************************************************************************
98 static uint_fast8_t
_ADCIntNumberGet(uint32_t ui32Base,uint32_t ui32SequenceNum)99 _ADCIntNumberGet(uint32_t ui32Base, uint32_t ui32SequenceNum)
100 {
101 uint_fast8_t ui8Int;
102
103 //
104 // Determine the interrupt to register based on the sequence number.
105 //
106 if(CLASS_IS_TM4C123)
107 {
108 ui8Int = ((ui32Base == ADC0_BASE) ?
109 (INT_ADC0SS0_TM4C123 + ui32SequenceNum) :
110 (INT_ADC1SS0_TM4C123 + ui32SequenceNum));
111 }
112 else if(CLASS_IS_TM4C129)
113 {
114 ui8Int = ((ui32Base == ADC0_BASE) ?
115 (INT_ADC0SS0_TM4C129 + ui32SequenceNum) :
116 (INT_ADC1SS0_TM4C129 + ui32SequenceNum));
117 }
118 else
119 {
120 ui8Int = 0;
121 }
122
123 return(ui8Int);
124 }
125
126 //*****************************************************************************
127 //
128 //! Registers an interrupt handler for an ADC interrupt.
129 //!
130 //! \param ui32Base is the base address of the ADC module.
131 //! \param ui32SequenceNum is the sample sequence number.
132 //! \param pfnHandler is a pointer to the function to be called when the
133 //! ADC sample sequence interrupt occurs.
134 //!
135 //! This function sets the handler to be called when a sample sequence
136 //! interrupt occurs. This function enables the global interrupt in the
137 //! interrupt controller; the sequence interrupt must be enabled with
138 //! ADCIntEnable(). It is the interrupt handler's responsibility to clear the
139 //! interrupt source via ADCIntClear().
140 //!
141 //! \sa IntRegister() for important information about registering interrupt
142 //! handlers.
143 //!
144 //! \return None.
145 //
146 //*****************************************************************************
147 void
ADCIntRegister(uint32_t ui32Base,uint32_t ui32SequenceNum,void (* pfnHandler)(void))148 ADCIntRegister(uint32_t ui32Base, uint32_t ui32SequenceNum,
149 void (*pfnHandler)(void))
150 {
151 uint_fast8_t ui8Int;
152
153 //
154 // Check the arguments.
155 //
156 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
157 ASSERT(ui32SequenceNum < 4);
158
159 //
160 // Determine the interrupt to register based on the sequence number.
161 //
162 ui8Int = _ADCIntNumberGet(ui32Base, ui32SequenceNum);
163 ASSERT(ui8Int != 0);
164
165 //
166 // Register the interrupt handler.
167 //
168 IntRegister(ui8Int, pfnHandler);
169
170 //
171 // Enable the timer interrupt.
172 //
173 IntEnable(ui8Int);
174 }
175
176 //*****************************************************************************
177 //
178 //! Unregisters the interrupt handler for an ADC interrupt.
179 //!
180 //! \param ui32Base is the base address of the ADC module.
181 //! \param ui32SequenceNum is the sample sequence number.
182 //!
183 //! This function unregisters the interrupt handler. This function disables
184 //! the global interrupt in the interrupt controller; the sequence interrupt
185 //! must be disabled via ADCIntDisable().
186 //!
187 //! \sa IntRegister() for important information about registering interrupt
188 //! handlers.
189 //!
190 //! \return None.
191 //
192 //*****************************************************************************
193 void
ADCIntUnregister(uint32_t ui32Base,uint32_t ui32SequenceNum)194 ADCIntUnregister(uint32_t ui32Base, uint32_t ui32SequenceNum)
195 {
196 uint_fast8_t ui8Int;
197
198 //
199 // Check the arguments.
200 //
201 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
202 ASSERT(ui32SequenceNum < 4);
203
204 //
205 // Determine the interrupt to unregister based on the sequence number.
206 //
207 ui8Int = _ADCIntNumberGet(ui32Base, ui32SequenceNum);
208 ASSERT(ui8Int != 0);
209
210 //
211 // Disable the interrupt.
212 //
213 IntDisable(ui8Int);
214
215 //
216 // Unregister the interrupt handler.
217 //
218 IntUnregister(ui8Int);
219 }
220
221 //*****************************************************************************
222 //
223 //! Disables a sample sequence interrupt.
224 //!
225 //! \param ui32Base is the base address of the ADC module.
226 //! \param ui32SequenceNum is the sample sequence number.
227 //!
228 //! This function disables the requested sample sequence interrupt.
229 //!
230 //! \return None.
231 //
232 //*****************************************************************************
233 void
ADCIntDisable(uint32_t ui32Base,uint32_t ui32SequenceNum)234 ADCIntDisable(uint32_t ui32Base, uint32_t ui32SequenceNum)
235 {
236 //
237 // Check the arguments.
238 //
239 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
240 ASSERT(ui32SequenceNum < 4);
241
242 //
243 // Disable this sample sequence interrupt.
244 //
245 HWREG(ui32Base + ADC_O_IM) &= ~(1 << ui32SequenceNum);
246 }
247
248 //*****************************************************************************
249 //
250 //! Enables a sample sequence interrupt.
251 //!
252 //! \param ui32Base is the base address of the ADC module.
253 //! \param ui32SequenceNum is the sample sequence number.
254 //!
255 //! This function enables the requested sample sequence interrupt. Any
256 //! outstanding interrupts are cleared before enabling the sample sequence
257 //! interrupt.
258 //!
259 //! \return None.
260 //
261 //*****************************************************************************
262 void
ADCIntEnable(uint32_t ui32Base,uint32_t ui32SequenceNum)263 ADCIntEnable(uint32_t ui32Base, uint32_t ui32SequenceNum)
264 {
265 //
266 // Check the arguments.
267 //
268 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
269 ASSERT(ui32SequenceNum < 4);
270
271 //
272 // Clear any outstanding interrupts on this sample sequence.
273 //
274 HWREG(ui32Base + ADC_O_ISC) = 1 << ui32SequenceNum;
275
276 //
277 // Enable this sample sequence interrupt.
278 //
279 HWREG(ui32Base + ADC_O_IM) |= 1 << ui32SequenceNum;
280 }
281
282 //*****************************************************************************
283 //
284 //! Gets the current interrupt status.
285 //!
286 //! \param ui32Base is the base address of the ADC module.
287 //! \param ui32SequenceNum is the sample sequence number.
288 //! \param bMasked is false if the raw interrupt status is required and true if
289 //! the masked interrupt status is required.
290 //!
291 //! This function returns the interrupt status for the specified sample
292 //! sequence. Either the raw interrupt status or the status of interrupts that
293 //! are allowed to reflect to the processor can be returned.
294 //!
295 //! \return The current raw or masked interrupt status.
296 //
297 //*****************************************************************************
298 uint32_t
ADCIntStatus(uint32_t ui32Base,uint32_t ui32SequenceNum,bool bMasked)299 ADCIntStatus(uint32_t ui32Base, uint32_t ui32SequenceNum, bool bMasked)
300 {
301 uint32_t ui32Temp;
302
303 //
304 // Check the arguments.
305 //
306 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
307 ASSERT(ui32SequenceNum < 4);
308
309 //
310 // Return either the interrupt status or the raw interrupt status as
311 // requested.
312 //
313 if(bMasked)
314 {
315 ui32Temp = HWREG(ui32Base + ADC_O_ISC) & (0x10001 << ui32SequenceNum);
316 }
317 else
318 {
319 ui32Temp = (HWREG(ui32Base + ADC_O_RIS) &
320 (0x10000 | (1 << ui32SequenceNum)));
321
322 //
323 // If the digital comparator status bit is set, reflect it to the
324 // appropriate sequence bit.
325 //
326 if(ui32Temp & 0x10000)
327 {
328 ui32Temp |= 0xF0000;
329 ui32Temp &= ~(0x10000 << ui32SequenceNum);
330 }
331 }
332
333 //
334 // Return the interrupt status
335 //
336 return(ui32Temp);
337 }
338
339 //*****************************************************************************
340 //
341 //! Clears sample sequence interrupt source.
342 //!
343 //! \param ui32Base is the base address of the ADC module.
344 //! \param ui32SequenceNum is the sample sequence number.
345 //!
346 //! The specified sample sequence interrupt is cleared, so that it no longer
347 //! asserts. This function must be called in the interrupt handler to keep
348 //! the interrupt from being triggered again immediately upon exit.
349 //!
350 //! \note Because there is a write buffer in the Cortex-M processor, it may
351 //! take several clock cycles before the interrupt source is actually cleared.
352 //! Therefore, it is recommended that the interrupt source be cleared early in
353 //! the interrupt handler (as opposed to the very last action) to avoid
354 //! returning from the interrupt handler before the interrupt source is
355 //! actually cleared. Failure to do so may result in the interrupt handler
356 //! being immediately reentered (because the interrupt controller still sees
357 //! the interrupt source asserted).
358 //!
359 //! \return None.
360 //
361 //*****************************************************************************
362 void
ADCIntClear(uint32_t ui32Base,uint32_t ui32SequenceNum)363 ADCIntClear(uint32_t ui32Base, uint32_t ui32SequenceNum)
364 {
365 //
366 // Check the arguments.
367 //
368 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
369 ASSERT(ui32SequenceNum < 4);
370
371 //
372 // Clear the interrupt.
373 //
374 HWREG(ui32Base + ADC_O_ISC) = 1 << ui32SequenceNum;
375 }
376
377 //*****************************************************************************
378 //
379 //! Enables a sample sequence.
380 //!
381 //! \param ui32Base is the base address of the ADC module.
382 //! \param ui32SequenceNum is the sample sequence number.
383 //!
384 //! Allows the specified sample sequence to be captured when its trigger is
385 //! detected. A sample sequence must be configured before it is enabled.
386 //!
387 //! \return None.
388 //
389 //*****************************************************************************
390 void
ADCSequenceEnable(uint32_t ui32Base,uint32_t ui32SequenceNum)391 ADCSequenceEnable(uint32_t ui32Base, uint32_t ui32SequenceNum)
392 {
393 //
394 // Check the arguments.
395 //
396 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
397 ASSERT(ui32SequenceNum < 4);
398
399 //
400 // Enable the specified sequence.
401 //
402 HWREG(ui32Base + ADC_O_ACTSS) |= 1 << ui32SequenceNum;
403 }
404
405 //*****************************************************************************
406 //
407 //! Disables a sample sequence.
408 //!
409 //! \param ui32Base is the base address of the ADC module.
410 //! \param ui32SequenceNum is the sample sequence number.
411 //!
412 //! Prevents the specified sample sequence from being captured when its trigger
413 //! is detected. A sample sequence must be disabled before it is configured.
414 //!
415 //! \return None.
416 //
417 //*****************************************************************************
418 void
ADCSequenceDisable(uint32_t ui32Base,uint32_t ui32SequenceNum)419 ADCSequenceDisable(uint32_t ui32Base, uint32_t ui32SequenceNum)
420 {
421 //
422 // Check the arguments.
423 //
424 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
425 ASSERT(ui32SequenceNum < 4);
426
427 //
428 // Disable the specified sequences.
429 //
430 HWREG(ui32Base + ADC_O_ACTSS) &= ~(1 << ui32SequenceNum);
431 }
432
433 //*****************************************************************************
434 //
435 //! Configures the trigger source and priority of a sample sequence.
436 //!
437 //! \param ui32Base is the base address of the ADC module.
438 //! \param ui32SequenceNum is the sample sequence number.
439 //! \param ui32Trigger is the trigger source that initiates the sample
440 //! sequence; must be one of the \b ADC_TRIGGER_* values.
441 //! \param ui32Priority is the relative priority of the sample sequence with
442 //! respect to the other sample sequences.
443 //!
444 //! This function configures the initiation criteria for a sample sequence.
445 //! Valid sample sequencers range from zero to three; sequencer zero captures
446 //! up to eight samples, sequencers one and two capture up to four samples,
447 //! and sequencer three captures a single sample. The trigger condition and
448 //! priority (with respect to other sample sequencer execution) are set.
449 //!
450 //! The \e ui32Trigger parameter can take on the following values:
451 //!
452 //! - \b ADC_TRIGGER_PROCESSOR - A trigger generated by the processor, via the
453 //! ADCProcessorTrigger() function.
454 //! - \b ADC_TRIGGER_COMP0 - A trigger generated by the first analog
455 //! comparator; configured with ComparatorConfigure().
456 //! - \b ADC_TRIGGER_COMP1 - A trigger generated by the second analog
457 //! comparator; configured with ComparatorConfigure().
458 //! - \b ADC_TRIGGER_COMP2 - A trigger generated by the third analog
459 //! comparator; configured with ComparatorConfigure().
460 //! - \b ADC_TRIGGER_EXTERNAL - A trigger generated by an input from the Port
461 //! B4 pin. Note that some microcontrollers can
462 //! select from any GPIO using the
463 //! GPIOADCTriggerEnable() function.
464 //! - \b ADC_TRIGGER_TIMER - A trigger generated by a timer; configured with
465 //! TimerControlTrigger().
466 //! - \b ADC_TRIGGER_PWM0 - A trigger generated by the first PWM generator;
467 //! configured with PWMGenIntTrigEnable().
468 //! - \b ADC_TRIGGER_PWM1 - A trigger generated by the second PWM generator;
469 //! configured with PWMGenIntTrigEnable().
470 //! - \b ADC_TRIGGER_PWM2 - A trigger generated by the third PWM generator;
471 //! configured with PWMGenIntTrigEnable().
472 //! - \b ADC_TRIGGER_PWM3 - A trigger generated by the fourth PWM generator;
473 //! configured with PWMGenIntTrigEnable().
474 //! - \b ADC_TRIGGER_ALWAYS - A trigger that is always asserted, causing the
475 //! sample sequence to capture repeatedly (so long as
476 //! there is not a higher priority source active).
477 //!
478 //! When \b ADC_TRIGGER_PWM0, \b ADC_TRIGGER_PWM1, \b ADC_TRIGGER_PWM2 or
479 //! \b ADC_TRIGGER_PWM3 is specified, one of the following should be ORed into
480 //! \e ui32Trigger to select the PWM module from which the triggers will be
481 //! routed for this sequence:
482 //!
483 //! - \b ADC_TRIGGER_PWM_MOD0 - Selects PWM module 0 as the source of the
484 //! PWM0 to PWM3 triggers for this sequence.
485 //! - \b ADC_TRIGGER_PWM_MOD1 - Selects PWM module 1 as the source of the
486 //! PWM0 to PWM3 triggers for this sequence.
487 //!
488 //! Note that not all trigger sources are available on all Tiva family
489 //! members; consult the data sheet for the device in question to determine the
490 //! availability of triggers.
491 //!
492 //! The \e ui32Priority parameter is a value between 0 and 3, where 0
493 //! represents the highest priority and 3 the lowest. Note that when
494 //! programming the priority among a set of sample sequences, each must have
495 //! unique priority; it is up to the caller to guarantee the uniqueness of the
496 //! priorities.
497 //!
498 //! \return None.
499 //
500 //*****************************************************************************
501 void
ADCSequenceConfigure(uint32_t ui32Base,uint32_t ui32SequenceNum,uint32_t ui32Trigger,uint32_t ui32Priority)502 ADCSequenceConfigure(uint32_t ui32Base, uint32_t ui32SequenceNum,
503 uint32_t ui32Trigger, uint32_t ui32Priority)
504 {
505 uint32_t ui32Gen;
506
507 //
508 // Check the arugments.
509 //
510 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
511 ASSERT(ui32SequenceNum < 4);
512 ASSERT(((ui32Trigger & 0xF) == ADC_TRIGGER_PROCESSOR) ||
513 ((ui32Trigger & 0xF) == ADC_TRIGGER_COMP0) ||
514 ((ui32Trigger & 0xF) == ADC_TRIGGER_COMP1) ||
515 ((ui32Trigger & 0xF) == ADC_TRIGGER_COMP2) ||
516 ((ui32Trigger & 0xF) == ADC_TRIGGER_EXTERNAL) ||
517 ((ui32Trigger & 0xF) == ADC_TRIGGER_TIMER) ||
518 ((ui32Trigger & 0xF) == ADC_TRIGGER_PWM0) ||
519 ((ui32Trigger & 0xF) == ADC_TRIGGER_PWM1) ||
520 ((ui32Trigger & 0xF) == ADC_TRIGGER_PWM2) ||
521 ((ui32Trigger & 0xF) == ADC_TRIGGER_PWM3) ||
522 ((ui32Trigger & 0xF) == ADC_TRIGGER_ALWAYS) ||
523 ((ui32Trigger & 0x30) == ADC_TRIGGER_PWM_MOD0) ||
524 ((ui32Trigger & 0x30) == ADC_TRIGGER_PWM_MOD1));
525 ASSERT(ui32Priority < 4);
526
527 //
528 // Compute the shift for the bits that control this sample sequence.
529 //
530 ui32SequenceNum *= 4;
531
532 //
533 // Set the trigger event for this sample sequence.
534 //
535 HWREG(ui32Base + ADC_O_EMUX) = ((HWREG(ui32Base + ADC_O_EMUX) &
536 ~(0xf << ui32SequenceNum)) |
537 ((ui32Trigger & 0xf) << ui32SequenceNum));
538
539 //
540 // Set the priority for this sample sequence.
541 //
542 HWREG(ui32Base + ADC_O_SSPRI) = ((HWREG(ui32Base + ADC_O_SSPRI) &
543 ~(0xf << ui32SequenceNum)) |
544 ((ui32Priority & 0x3) <<
545 ui32SequenceNum));
546
547 //
548 // Set the source PWM module for this sequence's PWM triggers.
549 //
550 ui32Gen = ui32Trigger & 0x0f;
551 if(ui32Gen >= ADC_TRIGGER_PWM0 && ui32Gen <= ADC_TRIGGER_PWM3)
552 {
553 //
554 // Set the shift for the module and generator
555 //
556 ui32Gen = (ui32Gen - ADC_TRIGGER_PWM0) * 8;
557
558 HWREG(ui32Base + ADC_O_TSSEL) = ((HWREG(ui32Base + ADC_O_TSSEL) &
559 ~(0x30 << ui32Gen)) |
560 ((ui32Trigger & 0x30) << ui32Gen));
561 }
562 }
563
564 //*****************************************************************************
565 //
566 //! Configure a step of the sample sequencer.
567 //!
568 //! \param ui32Base is the base address of the ADC module.
569 //! \param ui32SequenceNum is the sample sequence number.
570 //! \param ui32Step is the step to be configured.
571 //! \param ui32Config is the configuration of this step; is a logical OR
572 //! of \b ADC_CTL_TS, \b ADC_CTL_IE, \b ADC_CTL_END, \b ADC_CTL_D, one of the
573 //! input channel selects (\b ADC_CTL_CH0 through \b ADC_CTL_CH23), and one of
574 //! the digital comparator selects (\b ADC_CTL_CMP0 through \b ADC_CTL_CMP7).
575 //! On some parts the sample and hold time can be increased by including a
576 //! logical OR of one of \b ADC_CTL_SHOLD_4, \b ADC_CTL_SHOLD_8,
577 //! \b ADC_CTL_SHOLD_16, \b ADC_CTL_SHOLD_32, \b ADC_CTL_SHOLD_64,
578 //! \b ADC_CTL_SHOLD_128 or \b ADC_CTL_SHOLD_256. The default sample time is 4
579 //! ADC clocks.
580 //!
581 //! This function configures the ADC for one step of a sample sequence. The
582 //! ADC can be configured for single-ended or differential operation (the
583 //! \b ADC_CTL_D bit selects differential operation when set), the channel to
584 //! be sampled can be chosen (the \b ADC_CTL_CH0 through \b ADC_CTL_CH23
585 //! values), and the internal temperature sensor can be selected (the
586 //! \b ADC_CTL_TS bit). Additionally, this step can be defined as the last in
587 //! the sequence (the \b ADC_CTL_END bit) and it can be configured to cause an
588 //! interrupt when the step is complete (the \b ADC_CTL_IE bit). If the
589 //! digital comparators are present on the device, this step may also be
590 //! configured to send the ADC sample to the selected comparator using
591 //! \b ADC_CTL_CMP0 through \b ADC_CTL_CMP7. The configuration is used by the
592 //! ADC at the appropriate time when the trigger for this sequence occurs.
593 //!
594 //! \note If the Digital Comparator is present and enabled using the
595 //! \b ADC_CTL_CMP0 through \b ADC_CTL_CMP7 selects, the ADC sample is NOT
596 //! written into the ADC sequence data FIFO.
597 //!
598 //! The \e ui32Step parameter determines the order in which the samples are
599 //! captured by the ADC when the trigger occurs. It can range from zero to
600 //! seven for the first sample sequencer, from zero to three for the second and
601 //! third sample sequencer, and can only be zero for the fourth sample
602 //! sequencer.
603 //!
604 //! Differential mode only works with adjacent channel pairs (for example, 0
605 //! and 1). The channel select must be the number of the channel pair to
606 //! sample (for example, \b ADC_CTL_CH0 for 0 and 1, or \b ADC_CTL_CH1 for 2
607 //! and 3) or undefined results are returned by the ADC. Additionally, if
608 //! differential mode is selected when the temperature sensor is being sampled,
609 //! undefined results are returned by the ADC.
610 //!
611 //! It is the responsibility of the caller to ensure that a valid configuration
612 //! is specified; this function does not check the validity of the specified
613 //! configuration.
614 //!
615 //! \return None.
616 //
617 //*****************************************************************************
618 void
ADCSequenceStepConfigure(uint32_t ui32Base,uint32_t ui32SequenceNum,uint32_t ui32Step,uint32_t ui32Config)619 ADCSequenceStepConfigure(uint32_t ui32Base, uint32_t ui32SequenceNum,
620 uint32_t ui32Step, uint32_t ui32Config)
621 {
622 uint32_t ui32Temp;
623
624 //
625 // Check the arguments.
626 //
627 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
628 ASSERT(ui32SequenceNum < 4);
629 ASSERT(((ui32SequenceNum == 0) && (ui32Step < 8)) ||
630 ((ui32SequenceNum == 1) && (ui32Step < 4)) ||
631 ((ui32SequenceNum == 2) && (ui32Step < 4)) ||
632 ((ui32SequenceNum == 3) && (ui32Step < 1)));
633
634 //
635 // Get the offset of the sequence to be configured.
636 //
637 ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
638
639 //
640 // Compute the shift for the bits that control this step.
641 //
642 ui32Step *= 4;
643
644 //
645 // Set the analog mux value for this step.
646 //
647 HWREG(ui32Base + ADC_SSMUX) = ((HWREG(ui32Base + ADC_SSMUX) &
648 ~(0x0000000f << ui32Step)) |
649 ((ui32Config & 0x0f) << ui32Step));
650
651 //
652 // Set the upper bits of the analog mux value for this step.
653 //
654 HWREG(ui32Base + ADC_SSEMUX) = ((HWREG(ui32Base + ADC_SSEMUX) &
655 ~(0x0000000f << ui32Step)) |
656 (((ui32Config & 0xf00) >> 8) << ui32Step));
657
658 //
659 // Set the control value for this step.
660 //
661 HWREG(ui32Base + ADC_SSCTL) = ((HWREG(ui32Base + ADC_SSCTL) &
662 ~(0x0000000f << ui32Step)) |
663 (((ui32Config & 0xf0) >> 4) << ui32Step));
664
665 //
666 // Set the sample and hold time for this step. This is not available on
667 // all devices, however on devices that do not support this feature these
668 // reserved bits are ignored on write access.
669 //
670 HWREG(ui32Base + ADC_SSTSH) = ((HWREG(ui32Base + ADC_SSTSH) &
671 ~(0x0000000f << ui32Step)) |
672 (((ui32Config & 0xf00000) >> 20) << ui32Step));
673
674 //
675 // Enable digital comparator if specified in the ui32Config bit-fields.
676 //
677 if(ui32Config & 0x000F0000)
678 {
679 //
680 // Program the comparator for the specified step.
681 //
682 ui32Temp = HWREG(ui32Base + ADC_SSDC);
683 ui32Temp &= ~(0xF << ui32Step);
684 ui32Temp |= (((ui32Config & 0x00070000) >> 16) << ui32Step);
685 HWREG(ui32Base + ADC_SSDC) = ui32Temp;
686
687 //
688 // Enable the comparator.
689 //
690 HWREG(ui32Base + ADC_SSOP) |= (1 << ui32Step);
691 }
692
693 //
694 // Disable digital comparator if not specified.
695 //
696 else
697 {
698 HWREG(ui32Base + ADC_SSOP) &= ~(1 << ui32Step);
699 }
700 }
701
702 //*****************************************************************************
703 //
704 //! Determines if a sample sequence overflow occurred.
705 //!
706 //! \param ui32Base is the base address of the ADC module.
707 //! \param ui32SequenceNum is the sample sequence number.
708 //!
709 //! This function determines if a sample sequence overflow has occurred.
710 //! Overflow happens if the captured samples are not read from the FIFO before
711 //! the next trigger occurs.
712 //!
713 //! \return Returns zero if there was not an overflow, and non-zero if there
714 //! was.
715 //
716 //*****************************************************************************
717 int32_t
ADCSequenceOverflow(uint32_t ui32Base,uint32_t ui32SequenceNum)718 ADCSequenceOverflow(uint32_t ui32Base, uint32_t ui32SequenceNum)
719 {
720 //
721 // Check the arguments.
722 //
723 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
724 ASSERT(ui32SequenceNum < 4);
725
726 //
727 // Determine if there was an overflow on this sequence.
728 //
729 return(HWREG(ui32Base + ADC_O_OSTAT) & (1 << ui32SequenceNum));
730 }
731
732 //*****************************************************************************
733 //
734 //! Clears the overflow condition on a sample sequence.
735 //!
736 //! \param ui32Base is the base address of the ADC module.
737 //! \param ui32SequenceNum is the sample sequence number.
738 //!
739 //! This function clears an overflow condition on one of the sample sequences.
740 //! The overflow condition must be cleared in order to detect a subsequent
741 //! overflow condition (it otherwise causes no harm).
742 //!
743 //! \return None.
744 //
745 //*****************************************************************************
746 void
ADCSequenceOverflowClear(uint32_t ui32Base,uint32_t ui32SequenceNum)747 ADCSequenceOverflowClear(uint32_t ui32Base, uint32_t ui32SequenceNum)
748 {
749 //
750 // Check the arguments.
751 //
752 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
753 ASSERT(ui32SequenceNum < 4);
754
755 //
756 // Clear the overflow condition for this sequence.
757 //
758 HWREG(ui32Base + ADC_O_OSTAT) = 1 << ui32SequenceNum;
759 }
760
761 //*****************************************************************************
762 //
763 //! Determines if a sample sequence underflow occurred.
764 //!
765 //! \param ui32Base is the base address of the ADC module.
766 //! \param ui32SequenceNum is the sample sequence number.
767 //!
768 //! This function determines if a sample sequence underflow has occurred.
769 //! Underflow happens if too many samples are read from the FIFO.
770 //!
771 //! \return Returns zero if there was not an underflow, and non-zero if there
772 //! was.
773 //
774 //*****************************************************************************
775 int32_t
ADCSequenceUnderflow(uint32_t ui32Base,uint32_t ui32SequenceNum)776 ADCSequenceUnderflow(uint32_t ui32Base, uint32_t ui32SequenceNum)
777 {
778 //
779 // Check the arguments.
780 //
781 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
782 ASSERT(ui32SequenceNum < 4);
783
784 //
785 // Determine if there was an underflow on this sequence.
786 //
787 return(HWREG(ui32Base + ADC_O_USTAT) & (1 << ui32SequenceNum));
788 }
789
790 //*****************************************************************************
791 //
792 //! Clears the underflow condition on a sample sequence.
793 //!
794 //! \param ui32Base is the base address of the ADC module.
795 //! \param ui32SequenceNum is the sample sequence number.
796 //!
797 //! This function clears an underflow condition on one of the sample
798 //! sequencers. The underflow condition must be cleared in order to detect a
799 //! subsequent underflow condition (it otherwise causes no harm).
800 //!
801 //! \return None.
802 //
803 //*****************************************************************************
804 void
ADCSequenceUnderflowClear(uint32_t ui32Base,uint32_t ui32SequenceNum)805 ADCSequenceUnderflowClear(uint32_t ui32Base, uint32_t ui32SequenceNum)
806 {
807 //
808 // Check the arguments.
809 //
810 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
811 ASSERT(ui32SequenceNum < 4);
812
813 //
814 // Clear the underflow condition for this sequence.
815 //
816 HWREG(ui32Base + ADC_O_USTAT) = 1 << ui32SequenceNum;
817 }
818
819 //*****************************************************************************
820 //
821 //! Gets the captured data for a sample sequence.
822 //!
823 //! \param ui32Base is the base address of the ADC module.
824 //! \param ui32SequenceNum is the sample sequence number.
825 //! \param pui32Buffer is the address where the data is stored.
826 //!
827 //! This function copies data from the specified sample sequencer output FIFO
828 //! to a memory resident buffer. The number of samples available in the
829 //! hardware FIFO are copied into the buffer, which is assumed to be large
830 //! enough to hold that many samples. This function only returns the samples
831 //! that are presently available, which may not be the entire sample sequence
832 //! if it is in the process of being executed.
833 //!
834 //! \return Returns the number of samples copied to the buffer.
835 //
836 //*****************************************************************************
837 int32_t
ADCSequenceDataGet(uint32_t ui32Base,uint32_t ui32SequenceNum,uint32_t * pui32Buffer)838 ADCSequenceDataGet(uint32_t ui32Base, uint32_t ui32SequenceNum,
839 uint32_t *pui32Buffer)
840 {
841 uint32_t ui32Count;
842
843 //
844 // Check the arguments.
845 //
846 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
847 ASSERT(ui32SequenceNum < 4);
848
849 //
850 // Get the offset of the sequence to be read.
851 //
852 ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
853
854 //
855 // Read samples from the FIFO until it is empty.
856 //
857 ui32Count = 0;
858 while(!(HWREG(ui32Base + ADC_SSFSTAT) & ADC_SSFSTAT0_EMPTY) &&
859 (ui32Count < 8))
860 {
861 //
862 // Read the FIFO and copy it to the destination.
863 //
864 *pui32Buffer++ = HWREG(ui32Base + ADC_SSFIFO);
865
866 //
867 // Increment the count of samples read.
868 //
869 ui32Count++;
870 }
871
872 //
873 // Return the number of samples read.
874 //
875 return(ui32Count);
876 }
877
878 //*****************************************************************************
879 //
880 //! Causes a processor trigger for a sample sequence.
881 //!
882 //! \param ui32Base is the base address of the ADC module.
883 //! \param ui32SequenceNum is the sample sequence number, with
884 //! \b ADC_TRIGGER_WAIT or \b ADC_TRIGGER_SIGNAL optionally ORed into it.
885 //!
886 //! This function triggers a processor-initiated sample sequence if the sample
887 //! sequence trigger is configured to \b ADC_TRIGGER_PROCESSOR. If
888 //! \b ADC_TRIGGER_WAIT is ORed into the sequence number, the
889 //! processor-initiated trigger is delayed until a later processor-initiated
890 //! trigger to a different ADC module that specifies \b ADC_TRIGGER_SIGNAL,
891 //! allowing multiple ADCs to start from a processor-initiated trigger in a
892 //! synchronous manner.
893 //!
894 //! \return None.
895 //
896 //*****************************************************************************
897 void
ADCProcessorTrigger(uint32_t ui32Base,uint32_t ui32SequenceNum)898 ADCProcessorTrigger(uint32_t ui32Base, uint32_t ui32SequenceNum)
899 {
900 //
901 // Check the arguments.
902 //
903 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
904 ASSERT(ui32SequenceNum < 4);
905
906 //
907 // Generate a processor trigger for this sample sequence.
908 //
909 HWREG(ui32Base + ADC_O_PSSI) |= ((ui32SequenceNum & 0xffff0000) |
910 (1 << (ui32SequenceNum & 0xf)));
911 }
912
913 //*****************************************************************************
914 //
915 //! Configures the software oversampling factor of the ADC.
916 //!
917 //! \param ui32Base is the base address of the ADC module.
918 //! \param ui32SequenceNum is the sample sequence number.
919 //! \param ui32Factor is the number of samples to be averaged.
920 //!
921 //! This function configures the software oversampling for the ADC, which can
922 //! be used to provide better resolution on the sampled data. Oversampling is
923 //! accomplished by averaging multiple samples from the same analog input.
924 //! Three different oversampling rates are supported; 2x, 4x, and 8x.
925 //!
926 //! Oversampling is only supported on the sample sequencers that are more than
927 //! one sample in depth (that is, the fourth sample sequencer is not
928 //! supported). Oversampling by 2x (for example) divides the depth of the
929 //! sample sequencer by two; so 2x oversampling on the first sample sequencer
930 //! can only provide four samples per trigger. This also means that 8x
931 //! oversampling is only available on the first sample sequencer.
932 //!
933 //! \return None.
934 //
935 //*****************************************************************************
936 void
ADCSoftwareOversampleConfigure(uint32_t ui32Base,uint32_t ui32SequenceNum,uint32_t ui32Factor)937 ADCSoftwareOversampleConfigure(uint32_t ui32Base, uint32_t ui32SequenceNum,
938 uint32_t ui32Factor)
939 {
940 uint32_t ui32Value;
941 uint32_t ui32ADCInst;
942
943 //
944 // Check the arguments.
945 //
946 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
947 ASSERT(ui32SequenceNum < 3);
948 ASSERT(((ui32Factor == 2) || (ui32Factor == 4) || (ui32Factor == 8)) &&
949 ((ui32SequenceNum == 0) || (ui32Factor != 8)));
950
951 //
952 // Convert the oversampling factor to a shift factor.
953 //
954 for(ui32Value = 0, ui32Factor >>= 1; ui32Factor;
955 ui32Value++, ui32Factor >>= 1)
956 {
957 }
958
959 //
960 // Evaluate the ADC Instance.
961 //
962 if(ui32Base == ADC0_BASE)
963 {
964 ui32ADCInst = 0;
965 }
966 else
967 {
968 ui32ADCInst = 1;
969 }
970
971 //
972 // Save the shift factor.
973 //
974 g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum] = ui32Value;
975 }
976
977 //*****************************************************************************
978 //
979 //! Configures a step of the software oversampled sequencer.
980 //!
981 //! \param ui32Base is the base address of the ADC module.
982 //! \param ui32SequenceNum is the sample sequence number.
983 //! \param ui32Step is the step to be configured.
984 //! \param ui32Config is the configuration of this step.
985 //!
986 //! This function configures a step of the sample sequencer when using the
987 //! software oversampling feature. The number of steps available depends on
988 //! the oversampling factor set by ADCSoftwareOversampleConfigure(). The value
989 //! of \e ui32Config is the same as defined for ADCSequenceStepConfigure().
990 //!
991 //! \return None.
992 //
993 //*****************************************************************************
994 void
ADCSoftwareOversampleStepConfigure(uint32_t ui32Base,uint32_t ui32SequenceNum,uint32_t ui32Step,uint32_t ui32Config)995 ADCSoftwareOversampleStepConfigure(uint32_t ui32Base, uint32_t ui32SequenceNum,
996 uint32_t ui32Step, uint32_t ui32Config)
997 {
998 uint32_t ui32ADCInst;
999
1000 //
1001 // Evaluate the ADC Instance.
1002 //
1003 if(ui32Base == ADC0_BASE)
1004 {
1005 ui32ADCInst = 0;
1006 }
1007 else
1008 {
1009 ui32ADCInst = 1;
1010 }
1011
1012 //
1013 // Check the arguments.
1014 //
1015 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1016 ASSERT(ui32SequenceNum < 3);
1017 ASSERT(((ui32SequenceNum == 0) &&
1018 (ui32Step <
1019 (8 >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum]))) ||
1020 (ui32Step <
1021 (4 >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum])));
1022
1023 //
1024 // Get the offset of the sequence to be configured.
1025 //
1026 ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
1027
1028 //
1029 // Compute the shift for the bits that control this step.
1030 //
1031 ui32Step *= 4 << g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum];
1032
1033 //
1034 // Loop through the hardware steps that make up this step of the software
1035 // oversampled sequence.
1036 //
1037 for(ui32SequenceNum =
1038 (1 << g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum]);
1039 ui32SequenceNum; ui32SequenceNum--)
1040 {
1041 //
1042 // Set the analog mux value for this step.
1043 //
1044 HWREG(ui32Base + ADC_SSMUX) = ((HWREG(ui32Base + ADC_SSMUX) &
1045 ~(0x0000000f << ui32Step)) |
1046 ((ui32Config & 0x0f) << ui32Step));
1047
1048 //
1049 // Set the upper bits of the analog mux value for this step.
1050 //
1051 HWREG(ui32Base + ADC_SSEMUX) = ((HWREG(ui32Base + ADC_SSEMUX) &
1052 ~(0x0000000f << ui32Step)) |
1053 (((ui32Config & 0xf00) >> 8) <<
1054 ui32Step));
1055
1056 //
1057 // Set the control value for this step.
1058 //
1059 HWREG(ui32Base + ADC_SSCTL) = ((HWREG(ui32Base + ADC_SSCTL) &
1060 ~(0x0000000f << ui32Step)) |
1061 (((ui32Config & 0xf0) >> 4) <<
1062 ui32Step));
1063 if(ui32SequenceNum != 1)
1064 {
1065 HWREG(ui32Base + ADC_SSCTL) &= ~((ADC_SSCTL0_IE0 |
1066 ADC_SSCTL0_END0) << ui32Step);
1067 }
1068
1069 //
1070 // Go to the next hardware step.
1071 //
1072 ui32Step += 4;
1073 }
1074 }
1075
1076 //*****************************************************************************
1077 //
1078 //! Gets the captured data for a sample sequence using software oversampling.
1079 //!
1080 //! \param ui32Base is the base address of the ADC module.
1081 //! \param ui32SequenceNum is the sample sequence number.
1082 //! \param pui32Buffer is the address where the data is stored.
1083 //! \param ui32Count is the number of samples to be read.
1084 //!
1085 //! This function copies data from the specified sample sequence output FIFO to
1086 //! a memory resident buffer with software oversampling applied. The requested
1087 //! number of samples are copied into the data buffer; if there are not enough
1088 //! samples in the hardware FIFO to satisfy this many oversampled data items,
1089 //! then incorrect results are returned. It is the caller's responsibility to
1090 //! read only the samples that are available and wait until enough data is
1091 //! available, for example as a result of receiving an interrupt.
1092 //!
1093 //! \return None.
1094 //
1095 //*****************************************************************************
1096 void
ADCSoftwareOversampleDataGet(uint32_t ui32Base,uint32_t ui32SequenceNum,uint32_t * pui32Buffer,uint32_t ui32Count)1097 ADCSoftwareOversampleDataGet(uint32_t ui32Base, uint32_t ui32SequenceNum,
1098 uint32_t *pui32Buffer, uint32_t ui32Count)
1099 {
1100 uint32_t ui32Idx, ui32Accum;
1101 uint32_t ui32ADCInst;
1102
1103 //
1104 // Evaluate the ADC Instance.
1105 //
1106 if(ui32Base == ADC0_BASE)
1107 {
1108 ui32ADCInst = 0;
1109 }
1110 else
1111 {
1112 ui32ADCInst = 1;
1113 }
1114
1115
1116 //
1117 // Check the arguments.
1118 //
1119 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1120 ASSERT(ui32SequenceNum < 3);
1121 ASSERT(((ui32SequenceNum == 0) &&
1122 (ui32Count <
1123 (8 >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum]))) ||
1124 (ui32Count <
1125 (4 >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum])));
1126
1127 //
1128 // Get the offset of the sequence to be read.
1129 //
1130 ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
1131
1132 //
1133 // Read the samples from the FIFO until it is empty.
1134 //
1135 while(ui32Count--)
1136 {
1137 //
1138 // Compute the sum of the samples.
1139 //
1140 ui32Accum = 0;
1141 for(ui32Idx = 1 << g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum];
1142 ui32Idx; ui32Idx--)
1143 {
1144 //
1145 // Read the FIFO and add it to the accumulator.
1146 //
1147 ui32Accum += HWREG(ui32Base + ADC_SSFIFO);
1148 }
1149
1150 //
1151 // Write the averaged sample to the output buffer.
1152 //
1153 *pui32Buffer++ =
1154 ui32Accum >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum];
1155 }
1156 }
1157
1158 //*****************************************************************************
1159 //
1160 //! Configures the hardware oversampling factor of the ADC.
1161 //!
1162 //! \param ui32Base is the base address of the ADC module.
1163 //! \param ui32Factor is the number of samples to be averaged.
1164 //!
1165 //! This function configures the hardware oversampling for the ADC, which can
1166 //! be used to provide better resolution on the sampled data. Oversampling is
1167 //! accomplished by averaging multiple samples from the same analog input. Six
1168 //! different oversampling rates are supported; 2x, 4x, 8x, 16x, 32x, and 64x.
1169 //! Specifying an oversampling factor of zero disables hardware
1170 //! oversampling.
1171 //!
1172 //! Hardware oversampling applies uniformly to all sample sequencers. It does
1173 //! not reduce the depth of the sample sequencers like the software
1174 //! oversampling APIs; each sample written into the sample sequencer FIFO is a
1175 //! fully oversampled analog input reading.
1176 //!
1177 //! Enabling hardware averaging increases the precision of the ADC at the cost
1178 //! of throughput. For example, enabling 4x oversampling reduces the
1179 //! throughput of a 250 k samples/second ADC to 62.5 k samples/second.
1180 //!
1181 //! \return None.
1182 //
1183 //*****************************************************************************
1184 void
ADCHardwareOversampleConfigure(uint32_t ui32Base,uint32_t ui32Factor)1185 ADCHardwareOversampleConfigure(uint32_t ui32Base, uint32_t ui32Factor)
1186 {
1187 uint32_t ui32Value;
1188
1189 //
1190 // Check the arguments.
1191 //
1192 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1193 ASSERT(((ui32Factor == 0) || (ui32Factor == 2) || (ui32Factor == 4) ||
1194 (ui32Factor == 8) || (ui32Factor == 16) || (ui32Factor == 32) ||
1195 (ui32Factor == 64)));
1196
1197 //
1198 // Convert the oversampling factor to a shift factor.
1199 //
1200 for(ui32Value = 0, ui32Factor >>= 1; ui32Factor;
1201 ui32Value++, ui32Factor >>= 1)
1202 {
1203 }
1204
1205 //
1206 // Write the shift factor to the ADC to configure the hardware oversampler.
1207 //
1208 HWREG(ui32Base + ADC_O_SAC) = ui32Value;
1209 }
1210
1211 //*****************************************************************************
1212 //
1213 //! Configures an ADC digital comparator.
1214 //!
1215 //! \param ui32Base is the base address of the ADC module.
1216 //! \param ui32Comp is the index of the comparator to configure.
1217 //! \param ui32Config is the configuration of the comparator.
1218 //!
1219 //! This function configures a comparator. The \e ui32Config parameter is
1220 //! the result of a logical OR operation between the \b ADC_COMP_TRIG_xxx, and
1221 //! \b ADC_COMP_INT_xxx values.
1222 //!
1223 //! The \b ADC_COMP_TRIG_xxx term can take on the following values:
1224 //!
1225 //! - \b ADC_COMP_TRIG_NONE to never trigger PWM fault condition.
1226 //! - \b ADC_COMP_TRIG_LOW_ALWAYS to always trigger PWM fault condition when
1227 //! ADC output is in the low-band.
1228 //! - \b ADC_COMP_TRIG_LOW_ONCE to trigger PWM fault condition once when ADC
1229 //! output transitions into the low-band.
1230 //! - \b ADC_COMP_TRIG_LOW_HALWAYS to always trigger PWM fault condition when
1231 //! ADC output is in the low-band only if ADC output has been in the high-band
1232 //! since the last trigger output.
1233 //! - \b ADC_COMP_TRIG_LOW_HONCE to trigger PWM fault condition once when ADC
1234 //! output transitions into low-band only if ADC output has been in the
1235 //! high-band since the last trigger output.
1236 //! - \b ADC_COMP_TRIG_MID_ALWAYS to always trigger PWM fault condition when
1237 //! ADC output is in the mid-band.
1238 //! - \b ADC_COMP_TRIG_MID_ONCE to trigger PWM fault condition once when ADC
1239 //! output transitions into the mid-band.
1240 //! - \b ADC_COMP_TRIG_HIGH_ALWAYS to always trigger PWM fault condition when
1241 //! ADC output is in the high-band.
1242 //! - \b ADC_COMP_TRIG_HIGH_ONCE to trigger PWM fault condition once when ADC
1243 //! output transitions into the high-band.
1244 //! - \b ADC_COMP_TRIG_HIGH_HALWAYS to always trigger PWM fault condition when
1245 //! ADC output is in the high-band only if ADC output has been in the low-band
1246 //! since the last trigger output.
1247 //! - \b ADC_COMP_TRIG_HIGH_HONCE to trigger PWM fault condition once when ADC
1248 //! output transitions into high-band only if ADC output has been in the
1249 //! low-band since the last trigger output.
1250 //!
1251 //! The \b ADC_COMP_INT_xxx term can take on the following values:
1252 //!
1253 //! - \b ADC_COMP_INT_NONE to never generate ADC interrupt.
1254 //! - \b ADC_COMP_INT_LOW_ALWAYS to always generate ADC interrupt when ADC
1255 //! output is in the low-band.
1256 //! - \b ADC_COMP_INT_LOW_ONCE to generate ADC interrupt once when ADC output
1257 //! transitions into the low-band.
1258 //! - \b ADC_COMP_INT_LOW_HALWAYS to always generate ADC interrupt when ADC
1259 //! output is in the low-band only if ADC output has been in the high-band
1260 //! since the last trigger output.
1261 //! - \b ADC_COMP_INT_LOW_HONCE to generate ADC interrupt once when ADC output
1262 //! transitions into low-band only if ADC output has been in the high-band
1263 //! since the last trigger output.
1264 //! - \b ADC_COMP_INT_MID_ALWAYS to always generate ADC interrupt when ADC
1265 //! output is in the mid-band.
1266 //! - \b ADC_COMP_INT_MID_ONCE to generate ADC interrupt once when ADC output
1267 //! transitions into the mid-band.
1268 //! - \b ADC_COMP_INT_HIGH_ALWAYS to always generate ADC interrupt when ADC
1269 //! output is in the high-band.
1270 //! - \b ADC_COMP_INT_HIGH_ONCE to generate ADC interrupt once when ADC output
1271 //! transitions into the high-band.
1272 //! - \b ADC_COMP_INT_HIGH_HALWAYS to always generate ADC interrupt when ADC
1273 //! output is in the high-band only if ADC output has been in the low-band
1274 //! since the last trigger output.
1275 //! - \b ADC_COMP_INT_HIGH_HONCE to generate ADC interrupt once when ADC output
1276 //! transitions into high-band only if ADC output has been in the low-band
1277 //! since the last trigger output.
1278 //!
1279 //! \return None.
1280 //
1281 //*****************************************************************************
1282 void
ADCComparatorConfigure(uint32_t ui32Base,uint32_t ui32Comp,uint32_t ui32Config)1283 ADCComparatorConfigure(uint32_t ui32Base, uint32_t ui32Comp,
1284 uint32_t ui32Config)
1285 {
1286 //
1287 // Check the arguments.
1288 //
1289 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1290 ASSERT(ui32Comp < 8);
1291
1292 //
1293 // Save the new setting.
1294 //
1295 HWREG(ui32Base + ADC_O_DCCTL0 + (ui32Comp * 4)) = ui32Config;
1296 }
1297
1298 //*****************************************************************************
1299 //
1300 //! Defines the ADC digital comparator regions.
1301 //!
1302 //! \param ui32Base is the base address of the ADC module.
1303 //! \param ui32Comp is the index of the comparator to configure.
1304 //! \param ui32LowRef is the reference point for the low/mid band threshold.
1305 //! \param ui32HighRef is the reference point for the mid/high band threshold.
1306 //!
1307 //! The ADC digital comparator operation is based on three ADC value regions:
1308 //! - \b low-band is defined as any ADC value less than or equal to the
1309 //! \e ui32LowRef value.
1310 //! - \b mid-band is defined as any ADC value greater than the \e ui32LowRef
1311 //! value but less than or equal to the \e ui32HighRef value.
1312 //! - \b high-band is defined as any ADC value greater than the \e ui32HighRef
1313 //! value.
1314 //!
1315 //! \return None.
1316 //
1317 //*****************************************************************************
1318 void
ADCComparatorRegionSet(uint32_t ui32Base,uint32_t ui32Comp,uint32_t ui32LowRef,uint32_t ui32HighRef)1319 ADCComparatorRegionSet(uint32_t ui32Base, uint32_t ui32Comp,
1320 uint32_t ui32LowRef, uint32_t ui32HighRef)
1321 {
1322 //
1323 // Check the arguments.
1324 //
1325 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1326 ASSERT(ui32Comp < 8);
1327 ASSERT((ui32LowRef < 4096) && (ui32LowRef <= ui32HighRef));
1328 ASSERT(ui32HighRef < 4096);
1329
1330 //
1331 // Save the new region settings.
1332 //
1333 HWREG(ui32Base + ADC_O_DCCMP0 + (ui32Comp * 4)) = ((ui32HighRef << 16) |
1334 ui32LowRef);
1335 }
1336
1337 //*****************************************************************************
1338 //
1339 //! Resets the current ADC digital comparator conditions.
1340 //!
1341 //! \param ui32Base is the base address of the ADC module.
1342 //! \param ui32Comp is the index of the comparator.
1343 //! \param bTrigger is the flag to indicate reset of Trigger conditions.
1344 //! \param bInterrupt is the flag to indicate reset of Interrupt conditions.
1345 //!
1346 //! Because the digital comparator uses current and previous ADC values, this
1347 //! function allows the comparator to be reset to its initial
1348 //! value to prevent stale data from being used when a sequence is enabled.
1349 //!
1350 //! \return None.
1351 //
1352 //*****************************************************************************
1353 void
ADCComparatorReset(uint32_t ui32Base,uint32_t ui32Comp,bool bTrigger,bool bInterrupt)1354 ADCComparatorReset(uint32_t ui32Base, uint32_t ui32Comp, bool bTrigger,
1355 bool bInterrupt)
1356 {
1357 uint32_t ui32Temp;
1358
1359 //
1360 // Check the arguments.
1361 //
1362 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1363 ASSERT(ui32Comp < 8);
1364
1365 //
1366 // Set the appropriate bits to reset the trigger and/or interrupt
1367 // comparator conditions.
1368 //
1369 ui32Temp = 0;
1370 if(bTrigger)
1371 {
1372 ui32Temp |= (1 << (16 + ui32Comp));
1373 }
1374 if(bInterrupt)
1375 {
1376 ui32Temp |= (1 << ui32Comp);
1377 }
1378
1379 HWREG(ui32Base + ADC_O_DCRIC) = ui32Temp;
1380 }
1381
1382 //*****************************************************************************
1383 //
1384 //! Disables a sample sequence comparator interrupt.
1385 //!
1386 //! \param ui32Base is the base address of the ADC module.
1387 //! \param ui32SequenceNum is the sample sequence number.
1388 //!
1389 //! This function disables the requested sample sequence comparator interrupt.
1390 //!
1391 //! \return None.
1392 //
1393 //*****************************************************************************
1394 void
ADCComparatorIntDisable(uint32_t ui32Base,uint32_t ui32SequenceNum)1395 ADCComparatorIntDisable(uint32_t ui32Base, uint32_t ui32SequenceNum)
1396 {
1397 //
1398 // Check the arguments.
1399 //
1400 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1401 ASSERT(ui32SequenceNum < 4);
1402
1403 //
1404 // Disable this sample sequence comparator interrupt.
1405 //
1406 HWREG(ui32Base + ADC_O_IM) &= ~(0x10000 << ui32SequenceNum);
1407 }
1408
1409 //*****************************************************************************
1410 //
1411 //! Enables a sample sequence comparator interrupt.
1412 //!
1413 //! \param ui32Base is the base address of the ADC module.
1414 //! \param ui32SequenceNum is the sample sequence number.
1415 //!
1416 //! This function enables the requested sample sequence comparator interrupt.
1417 //!
1418 //! \return None.
1419 //
1420 //*****************************************************************************
1421 void
ADCComparatorIntEnable(uint32_t ui32Base,uint32_t ui32SequenceNum)1422 ADCComparatorIntEnable(uint32_t ui32Base, uint32_t ui32SequenceNum)
1423 {
1424 //
1425 // Check the arguments.
1426 //
1427 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1428 ASSERT(ui32SequenceNum < 4);
1429
1430 //
1431 // Enable this sample sequence interrupt.
1432 //
1433 HWREG(ui32Base + ADC_O_IM) |= 0x10000 << ui32SequenceNum;
1434 }
1435
1436 //*****************************************************************************
1437 //
1438 //! Gets the current comparator interrupt status.
1439 //!
1440 //! \param ui32Base is the base address of the ADC module.
1441 //!
1442 //! This function returns the digital comparator interrupt status bits. This
1443 //! status is sequence agnostic.
1444 //!
1445 //! \return The current comparator interrupt status.
1446 //
1447 //*****************************************************************************
1448 uint32_t
ADCComparatorIntStatus(uint32_t ui32Base)1449 ADCComparatorIntStatus(uint32_t ui32Base)
1450 {
1451 //
1452 // Check the arguments.
1453 //
1454 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1455
1456 //
1457 // Return the digital comparator interrupt status.
1458 //
1459 return(HWREG(ui32Base + ADC_O_DCISC));
1460 }
1461
1462 //*****************************************************************************
1463 //
1464 //! Clears sample sequence comparator interrupt source.
1465 //!
1466 //! \param ui32Base is the base address of the ADC module.
1467 //! \param ui32Status is the bit-mapped interrupts status to clear.
1468 //!
1469 //! The specified interrupt status is cleared.
1470 //!
1471 //! \return None.
1472 //
1473 //*****************************************************************************
1474 void
ADCComparatorIntClear(uint32_t ui32Base,uint32_t ui32Status)1475 ADCComparatorIntClear(uint32_t ui32Base, uint32_t ui32Status)
1476 {
1477 //
1478 // Check the arguments.
1479 //
1480 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1481
1482 //
1483 // Clear the interrupt.
1484 //
1485 HWREG(ui32Base + ADC_O_DCISC) = ui32Status;
1486 }
1487
1488 //*****************************************************************************
1489 //
1490 //! Disables ADC interrupt sources.
1491 //!
1492 //! \param ui32Base is the base address of the ADC module.
1493 //! \param ui32IntFlags is the bit mask of the interrupt sources to disable.
1494 //!
1495 //! This function disables the indicated ADC interrupt sources. Only the
1496 //! sources that are enabled can be reflected to the processor interrupt;
1497 //! disabled sources have no effect on the processor.
1498 //!
1499 //! The \e ui32IntFlags parameter is the logical OR of any of the following:
1500 //!
1501 //! - \b ADC_INT_SS0 - interrupt due to ADC sample sequence 0.
1502 //! - \b ADC_INT_SS1 - interrupt due to ADC sample sequence 1.
1503 //! - \b ADC_INT_SS2 - interrupt due to ADC sample sequence 2.
1504 //! - \b ADC_INT_SS3 - interrupt due to ADC sample sequence 3.
1505 //! - \b ADC_INT_DMA_SS0 - interrupt due to DMA on ADC sample sequence 0.
1506 //! - \b ADC_INT_DMA_SS1 - interrupt due to DMA on ADC sample sequence 1.
1507 //! - \b ADC_INT_DMA_SS2 - interrupt due to DMA on ADC sample sequence 2.
1508 //! - \b ADC_INT_DMA_SS3 - interrupt due to DMA on ADC sample sequence 3.
1509 //! - \b ADC_INT_DCON_SS0 - interrupt due to digital comparator on ADC sample
1510 //! sequence 0.
1511 //! - \b ADC_INT_DCON_SS1 - interrupt due to digital comparator on ADC sample
1512 //! sequence 1.
1513 //! - \b ADC_INT_DCON_SS2 - interrupt due to digital comparator on ADC sample
1514 //! sequence 2.
1515 //! - \b ADC_INT_DCON_SS3 - interrupt due to digital comparator on ADC sample
1516 //! sequence 3.
1517 //!
1518 //! \return None.
1519 //
1520 //*****************************************************************************
1521 void
ADCIntDisableEx(uint32_t ui32Base,uint32_t ui32IntFlags)1522 ADCIntDisableEx(uint32_t ui32Base, uint32_t ui32IntFlags)
1523 {
1524 //
1525 // Check the arguments.
1526 //
1527 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1528
1529 //
1530 // Disable the requested interrupts.
1531 //
1532 HWREG(ui32Base + ADC_O_IM) &= ~ui32IntFlags;
1533 }
1534
1535 //*****************************************************************************
1536 //
1537 //! Enables ADC interrupt sources.
1538 //!
1539 //! \param ui32Base is the base address of the ADC module.
1540 //! \param ui32IntFlags is the bit mask of the interrupt sources to disable.
1541 //!
1542 //! This function enables the indicated ADC interrupt sources. Only the
1543 //! sources that are enabled can be reflected to the processor interrupt;
1544 //! disabled sources have no effect on the processor.
1545 //!
1546 //! The \e ui32IntFlags parameter is the logical OR of any of the following:
1547 //!
1548 //! - \b ADC_INT_SS0 - interrupt due to ADC sample sequence 0.
1549 //! - \b ADC_INT_SS1 - interrupt due to ADC sample sequence 1.
1550 //! - \b ADC_INT_SS2 - interrupt due to ADC sample sequence 2.
1551 //! - \b ADC_INT_SS3 - interrupt due to ADC sample sequence 3.
1552 //! - \b ADC_INT_DMA_SS0 - interrupt due to DMA on ADC sample sequence 0.
1553 //! - \b ADC_INT_DMA_SS1 - interrupt due to DMA on ADC sample sequence 1.
1554 //! - \b ADC_INT_DMA_SS2 - interrupt due to DMA on ADC sample sequence 2.
1555 //! - \b ADC_INT_DMA_SS3 - interrupt due to DMA on ADC sample sequence 3.
1556 //! - \b ADC_INT_DCON_SS0 - interrupt due to digital comparator on ADC sample
1557 //! sequence 0.
1558 //! - \b ADC_INT_DCON_SS1 - interrupt due to digital comparator on ADC sample
1559 //! sequence 1.
1560 //! - \b ADC_INT_DCON_SS2 - interrupt due to digital comparator on ADC sample
1561 //! sequence 2.
1562 //! - \b ADC_INT_DCON_SS3 - interrupt due to digital comparator on ADC sample
1563 //! sequence 3.
1564 //!
1565 //! \return None.
1566 //
1567 //*****************************************************************************
1568 void
ADCIntEnableEx(uint32_t ui32Base,uint32_t ui32IntFlags)1569 ADCIntEnableEx(uint32_t ui32Base, uint32_t ui32IntFlags)
1570 {
1571 //
1572 // Check the arguments.
1573 //
1574 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1575
1576 //
1577 // Enable the requested interrupts.
1578 //
1579 HWREG(ui32Base + ADC_O_IM) |= ui32IntFlags;
1580 }
1581
1582 //*****************************************************************************
1583 //
1584 //! Gets interrupt status for the specified ADC module.
1585 //!
1586 //! \param ui32Base is the base address of the ADC module.
1587 //! \param bMasked specifies whether masked or raw interrupt status is
1588 //! returned.
1589 //!
1590 //! If \e bMasked is set as \b true, then the masked interrupt status is
1591 //! returned; otherwise, the raw interrupt status is returned.
1592 //!
1593 //! \return Returns the current interrupt status for the specified ADC module.
1594 //! The value returned is the logical OR of the \b ADC_INT_* values that are
1595 //! currently active.
1596 //
1597 //*****************************************************************************
1598 uint32_t
ADCIntStatusEx(uint32_t ui32Base,bool bMasked)1599 ADCIntStatusEx(uint32_t ui32Base, bool bMasked)
1600 {
1601 uint32_t ui32Temp;
1602
1603 //
1604 // Check the arguments.
1605 //
1606 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1607
1608 //
1609 // Return either the masked interrupt status or the raw interrupt status as
1610 // requested.
1611 //
1612 if(bMasked)
1613 {
1614 ui32Temp = HWREG(ui32Base + ADC_O_ISC);
1615 }
1616 else
1617 {
1618 //
1619 // Read the Raw interrupt status to see if a digital comparator
1620 // interrupt is active.
1621 //
1622 ui32Temp = HWREG(ui32Base + ADC_O_RIS);
1623
1624 //
1625 // Since, the raw interrupt status only indicates that any one of the
1626 // digital comparators caused an interrupt, if the raw interrupt status
1627 // is set then the return value is modified to indicate that all sample
1628 // sequences have a pending digital comparator interrupt.
1629 // This is exactly how the hardware works so the return code is
1630 // modified to match this behavior.
1631 //
1632 if(ui32Temp & ADC_RIS_INRDC)
1633 {
1634 ui32Temp |= (ADC_INT_DCON_SS3 | ADC_INT_DCON_SS2 |
1635 ADC_INT_DCON_SS1 | ADC_INT_DCON_SS0);
1636 }
1637 }
1638 return(ui32Temp);
1639 }
1640
1641 //*****************************************************************************
1642 //
1643 //! Clears the specified ADC interrupt sources.
1644 //!
1645 //! \param ui32Base is the base address of the ADC port.
1646 //! \param ui32IntFlags is the bit mask of the interrupt sources to disable.
1647 //!
1648 //! Clears the interrupt for the specified interrupt source(s).
1649 //!
1650 //! The \e ui32IntFlags parameter is the logical OR of the \b ADC_INT_* values.
1651 //! See the ADCIntEnableEx() function for the list of possible \b ADC_INT*
1652 //! values.
1653 //!
1654 //! \note Because there is a write buffer in the Cortex-M processor, it may
1655 //! take several clock cycles before the interrupt source is actually cleared.
1656 //! Therefore, it is recommended that the interrupt source be cleared early in
1657 //! the interrupt handler (as opposed to the very last action) to avoid
1658 //! returning from the interrupt handler before the interrupt source is
1659 //! actually cleared. Failure to do so may result in the interrupt handler
1660 //! being immediately reentered (because the interrupt controller still sees
1661 //! the interrupt source asserted).
1662 //!
1663 //! \return None.
1664 //
1665 //*****************************************************************************
1666 void
ADCIntClearEx(uint32_t ui32Base,uint32_t ui32IntFlags)1667 ADCIntClearEx(uint32_t ui32Base, uint32_t ui32IntFlags)
1668 {
1669 //
1670 // Note: The interrupt bits are "W1C" so we DO NOT use a logical OR
1671 // here to clear the requested bits. Doing so would clear all outstanding
1672 // interrupts rather than just those which the caller has specified.
1673 //
1674 HWREG(ui32Base + ADC_O_ISC) = ui32IntFlags;
1675 }
1676
1677 //*****************************************************************************
1678 //
1679 //! Selects the ADC reference.
1680 //!
1681 //! \param ui32Base is the base address of the ADC module.
1682 //! \param ui32Ref is the reference to use.
1683 //!
1684 //! The ADC reference is set as specified by \e ui32Ref. It must be one of
1685 //! \b ADC_REF_INT, or \b ADC_REF_EXT_3V for internal or external reference
1686 //! If \b ADC_REF_INT is chosen, then an internal 3V reference is used and
1687 //! no external reference is needed. If \b ADC_REF_EXT_3V is chosen, then
1688 //! a 3V reference must be supplied to the AVREF pin.
1689 //!
1690 //! \note The ADC reference can only be selected on parts that have an external
1691 //! reference. Consult the data sheet for your part to determine if there is
1692 //! an external reference.
1693 //!
1694 //! \return None.
1695 //
1696 //*****************************************************************************
1697 void
ADCReferenceSet(uint32_t ui32Base,uint32_t ui32Ref)1698 ADCReferenceSet(uint32_t ui32Base, uint32_t ui32Ref)
1699 {
1700 //
1701 // Check the arguments.
1702 //
1703 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1704 ASSERT((ui32Ref == ADC_REF_INT) || (ui32Ref == ADC_REF_EXT_3V));
1705
1706 //
1707 // Set the reference.
1708 //
1709 HWREG(ui32Base + ADC_O_CTL) =
1710 (HWREG(ui32Base + ADC_O_CTL) & ~ADC_CTL_VREF_M) | ui32Ref;
1711 }
1712
1713 //*****************************************************************************
1714 //
1715 //! Returns the current setting of the ADC reference.
1716 //!
1717 //! \param ui32Base is the base address of the ADC module.
1718 //!
1719 //! Returns the value of the ADC reference setting. The returned value is one
1720 //! of \b ADC_REF_INT, or \b ADC_REF_EXT_3V.
1721 //!
1722 //! \note The value returned by this function is only meaningful if used on a
1723 //! part that is capable of using an external reference. Consult the data
1724 //! sheet for your part to determine if it has an external reference input.
1725 //!
1726 //! \return The current setting of the ADC reference.
1727 //
1728 //*****************************************************************************
1729 uint32_t
ADCReferenceGet(uint32_t ui32Base)1730 ADCReferenceGet(uint32_t ui32Base)
1731 {
1732 //
1733 // Check the arguments.
1734 //
1735 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1736
1737 //
1738 // Return the value of the reference.
1739 //
1740 return(HWREG(ui32Base + ADC_O_CTL) & ADC_CTL_VREF_M);
1741 }
1742
1743 //*****************************************************************************
1744 //
1745 //! Sets the phase delay between a trigger and the start of a sequence.
1746 //!
1747 //! \param ui32Base is the base address of the ADC module.
1748 //! \param ui32Phase is the phase delay, specified as one of \b ADC_PHASE_0,
1749 //! \b ADC_PHASE_22_5, \b ADC_PHASE_45, \b ADC_PHASE_67_5, \b ADC_PHASE_90,
1750 //! \b ADC_PHASE_112_5, \b ADC_PHASE_135, \b ADC_PHASE_157_5, \b ADC_PHASE_180,
1751 //! \b ADC_PHASE_202_5, \b ADC_PHASE_225, \b ADC_PHASE_247_5, \b ADC_PHASE_270,
1752 //! \b ADC_PHASE_292_5, \b ADC_PHASE_315, or \b ADC_PHASE_337_5.
1753 //!
1754 //! This function sets the phase delay between the detection of an ADC trigger
1755 //! event and the start of the sample sequence. By selecting a different phase
1756 //! delay for a pair of ADC modules (such as \b ADC_PHASE_0 and
1757 //! \b ADC_PHASE_180) and having each ADC module sample the same analog input,
1758 //! it is possible to increase the sampling rate of the analog input (with
1759 //! samples N, N+2, N+4, and so on, coming from the first ADC and samples N+1,
1760 //! N+3, N+5, and so on, coming from the second ADC). The ADC module has a
1761 //! single phase delay that is applied to all sample sequences within that
1762 //! module.
1763 //!
1764 //! \note This capability is not available on all parts.
1765 //!
1766 //! \return None.
1767 //
1768 //*****************************************************************************
1769 void
ADCPhaseDelaySet(uint32_t ui32Base,uint32_t ui32Phase)1770 ADCPhaseDelaySet(uint32_t ui32Base, uint32_t ui32Phase)
1771 {
1772 //
1773 // Check the arguments.
1774 //
1775 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1776 ASSERT((ui32Phase == ADC_PHASE_0) || (ui32Phase == ADC_PHASE_22_5) ||
1777 (ui32Phase == ADC_PHASE_45) || (ui32Phase == ADC_PHASE_67_5) ||
1778 (ui32Phase == ADC_PHASE_90) || (ui32Phase == ADC_PHASE_112_5) ||
1779 (ui32Phase == ADC_PHASE_135) || (ui32Phase == ADC_PHASE_157_5) ||
1780 (ui32Phase == ADC_PHASE_180) || (ui32Phase == ADC_PHASE_202_5) ||
1781 (ui32Phase == ADC_PHASE_225) || (ui32Phase == ADC_PHASE_247_5) ||
1782 (ui32Phase == ADC_PHASE_270) || (ui32Phase == ADC_PHASE_292_5) ||
1783 (ui32Phase == ADC_PHASE_315) || (ui32Phase == ADC_PHASE_337_5));
1784
1785 //
1786 // Set the phase delay.
1787 //
1788 HWREG(ui32Base + ADC_O_SPC) = ui32Phase;
1789 }
1790
1791 //*****************************************************************************
1792 //
1793 //! Gets the phase delay between a trigger and the start of a sequence.
1794 //!
1795 //! \param ui32Base is the base address of the ADC module.
1796 //!
1797 //! This function gets the current phase delay between the detection of an ADC
1798 //! trigger event and the start of the sample sequence.
1799 //!
1800 //! \return Returns the phase delay, specified as one of \b ADC_PHASE_0,
1801 //! \b ADC_PHASE_22_5, \b ADC_PHASE_45, \b ADC_PHASE_67_5, \b ADC_PHASE_90,
1802 //! \b ADC_PHASE_112_5, \b ADC_PHASE_135, \b ADC_PHASE_157_5, \b ADC_PHASE_180,
1803 //! \b ADC_PHASE_202_5, \b ADC_PHASE_225, \b ADC_PHASE_247_5, \b ADC_PHASE_270,
1804 //! \b ADC_PHASE_292_5, \b ADC_PHASE_315, or \b ADC_PHASE_337_5.
1805 //
1806 //*****************************************************************************
1807 uint32_t
ADCPhaseDelayGet(uint32_t ui32Base)1808 ADCPhaseDelayGet(uint32_t ui32Base)
1809 {
1810 //
1811 // Check the arguments.
1812 //
1813 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1814
1815 //
1816 // Return the phase delay.
1817 //
1818 return(HWREG(ui32Base + ADC_O_SPC));
1819 }
1820
1821 //*****************************************************************************
1822 //
1823 //! Enables DMA for sample sequencers.
1824 //!
1825 //! \param ui32Base is the base address of the ADC module.
1826 //! \param ui32SequenceNum is the sample sequence number.
1827 //!
1828 //! Allows DMA requests to be generated based on the FIFO level of the sample
1829 //! sequencer.
1830 //!
1831 //! \return None.
1832 //
1833 //*****************************************************************************
1834 void
ADCSequenceDMAEnable(uint32_t ui32Base,uint32_t ui32SequenceNum)1835 ADCSequenceDMAEnable(uint32_t ui32Base, uint32_t ui32SequenceNum)
1836 {
1837 //
1838 // Check the arguments.
1839 //
1840 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1841 ASSERT(ui32SequenceNum < 4);
1842
1843 //
1844 // Enable the DMA on the specified sequencer.
1845 //
1846 HWREG(ui32Base + ADC_O_ACTSS) |= 0x100 << ui32SequenceNum;
1847 }
1848
1849 //*****************************************************************************
1850 //
1851 //! Disables DMA for sample sequencers.
1852 //!
1853 //! \param ui32Base is the base address of the ADC module.
1854 //! \param ui32SequenceNum is the sample sequence number.
1855 //!
1856 //! Prevents the specified sample sequencer from generating DMA requests.
1857 //!
1858 //! \return None.
1859 //
1860 //*****************************************************************************
1861 void
ADCSequenceDMADisable(uint32_t ui32Base,uint32_t ui32SequenceNum)1862 ADCSequenceDMADisable(uint32_t ui32Base, uint32_t ui32SequenceNum)
1863 {
1864 //
1865 // Check the arguments.
1866 //
1867 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1868 ASSERT(ui32SequenceNum < 4);
1869
1870 //
1871 // Disable the DMA on the specified sequencer.
1872 //
1873 HWREG(ui32Base + ADC_O_ACTSS) &= ~(0x100 << ui32SequenceNum);
1874 }
1875
1876 //*****************************************************************************
1877 //
1878 //! Determines whether the ADC is busy or not.
1879 //!
1880 //! \param ui32Base is the base address of the ADC.
1881 //!
1882 //! This function allows the caller to determine whether or not the ADC is
1883 //! currently sampling . If \b false is returned, then the ADC is not
1884 //! sampling data.
1885 //!
1886 //! Use this function to detect that the ADC is finished sampling data before
1887 //! putting the device into deep sleep. Before using this function, it is
1888 //! highly recommended that the event trigger is changed to
1889 //! \b ADC_TRIGGER_NEVER on all enabled sequencers to prevent the ADC from
1890 //! starting after checking the busy status.
1891 //!
1892 //! \return Returns \b true if the ADC is sampling or \b false if all
1893 //! samples are complete.
1894 //
1895 //*****************************************************************************
1896 bool
ADCBusy(uint32_t ui32Base)1897 ADCBusy(uint32_t ui32Base)
1898 {
1899 //
1900 // Check the argument.
1901 //
1902 ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1903
1904 //
1905 // Determine if the ADC is busy.
1906 //
1907 return((HWREG(ui32Base + ADC_O_ACTSS) & ADC_ACTSS_BUSY) ? true : false);
1908 }
1909
1910 //*****************************************************************************
1911 //
1912 //! Sets the clock configuration for the ADC.
1913 //!
1914 //! \param ui32Base is the base address of the ADC to configure, which must
1915 //! always be \b ADC0_BASE.
1916 //! \param ui32Config is a combination of the \b ADC_CLOCK_SRC_ and
1917 //! \b ADC_CLOCK_RATE_* values used to configure the ADC clock input.
1918 //! \param ui32ClockDiv is the input clock divider for the clock selected by
1919 //! the \b ADC_CLOCK_SRC value.
1920 //!
1921 //! This function is used to configure the input clock to the ADC modules. The
1922 //! clock configuration is shared across ADC units so \e ui32Base must
1923 //! always be \b ADC0_BASE. The \e ui32Config value is logical OR of one
1924 //! of the \b ADC_CLOCK_RATE_ and one of the \b ADC_CLOCK_SRC_ values defined
1925 //! below. The \b ADC_CLOCK_SRC_* values determine the input clock for the ADC.
1926 //! Not all values are available on all devices so check the device data sheet
1927 //! to determine value configuration options. Regardless of the source, the
1928 //! final frequency for TM4C123x devices must be 16 MHz and for TM4C129x parts
1929 //! after dividing must be between 16 and 32 MHz.
1930 //!
1931 //! \note For TM4C123x devices, if the PLL is enabled, the PLL/25 is used as
1932 //! the ADC clock unless ADC_CLOCK_SRC_PIOSC is specified. If the PLL is
1933 //! disabled, the MOSC is used as the clock source unless ADC_CLOCK_SRC_PIOSC
1934 //! is specified.
1935 //!
1936 //! - \b ADC_CLOCK_SRC_PLL - The main PLL output (TM4x129 class only).
1937 //! - \b ADC_CLOCK_SRC_PIOSC - The internal PIOSC at 16 MHz.
1938 //! - \b ADC_CLOCK_SRC_ALTCLK - The output of the ALTCLK in the system control
1939 //! module (TM4x129 class only).
1940 //! - \b ADC_CLOCK_SRC_MOSC - The external MOSC (TM4x129 class only).
1941 //!
1942 //! \b ADC_CLOCK_RATE values control how often samples are provided back to the
1943 //! application. The values are the following:
1944 //!
1945 //! - \b ADC_CLOCK_RATE_FULL - All samples.
1946 //! - \b ADC_CLOCK_RATE_HALF - Every other sample.
1947 //! - \b ADC_CLOCK_RATE_QUARTER - Every fourth sample.
1948 //! - \b ADC_CLOCK_RATE_EIGHTH - Every either sample.
1949 //!
1950 //! The \e ui32ClockDiv parameter allows for dividing a higher frequency down
1951 //! into the valid range for the ADCs. This parameter is typically only used
1952 //! \b ADC_CLOCK_SRC_PLL option because it is the only clock value that can be
1953 //! with the in the correct range to use the divider. The actual value ranges
1954 //! from 1 to 64.
1955 //!
1956 //! \b Example: ADC Clock Configurations
1957 //!
1958 //! \verbatim
1959 //!
1960 //! //
1961 //! // Configure the ADC to use PIOSC divided by one (16 MHz) and sample at
1962 //! // half the rate.
1963 //! //
1964 //! ADCClockConfigSet(ADC0_BASE, ADC_CLOCK_SRC_PIOSC | ADC_CLOCK_RATE_HALF, 1);
1965 //!
1966 //! ...
1967 //!
1968 //! //
1969 //! // Configure the ADC to use PLL at 480 MHz divided by 24 to get an ADC
1970 //! // clock of 20 MHz.
1971 //! //
1972 //! ADCClockConfigSet(ADC0_BASE, ADC_CLOCK_SRC_PLL | ADC_CLOCK_RATE_FULL, 24);
1973 //! \endverbatim
1974 //!
1975 //! \return None.
1976 //
1977 //*****************************************************************************
1978 void
ADCClockConfigSet(uint32_t ui32Base,uint32_t ui32Config,uint32_t ui32ClockDiv)1979 ADCClockConfigSet(uint32_t ui32Base, uint32_t ui32Config,
1980 uint32_t ui32ClockDiv)
1981 {
1982 //
1983 // Check the argument.
1984 //
1985 ASSERT(ui32Base == ADC0_BASE);
1986 ASSERT((ui32ClockDiv - 1) <= (ADC_CC_CLKDIV_M >> ADC_CC_CLKDIV_S));
1987
1988 //
1989 // A rate must be supplied.
1990 //
1991 ASSERT((ui32Config & ADC_CLOCK_RATE_FULL) != 0);
1992
1993 //
1994 // Write the sample conversion rate.
1995 //
1996 HWREG(ui32Base + ADC_O_PC) = (ui32Config >> 4) & ADC_PC_SR_M;
1997
1998 //
1999 // Write the clock select and divider.
2000 //
2001 HWREG(ui32Base + ADC_O_CC) = (ui32Config & ADC_CC_CS_M) |
2002 (((ui32ClockDiv - 1) << ADC_CC_CLKDIV_S)) ;
2003 }
2004
2005 //*****************************************************************************
2006 //
2007 //! Returns the clock configuration for the ADC.
2008 //!
2009 //! \param ui32Base is the base address of the ADC to configure, which must
2010 //! always be \b ADC0_BASE.
2011 //! \param pui32ClockDiv is a pointer to the input clock divider for the clock
2012 //! selected by the \b ADC_CLOCK_SRC in use by the ADCs.
2013 //!
2014 //! This function returns the ADC clock configuration and the clock divider for
2015 //! the ADCs.
2016 //!
2017 //! \b Example: Read the current ADC clock configuration.
2018 //!
2019 //! \verbatim
2020 //! uint32_t ui32Config, ui32ClockDiv;
2021 //!
2022 //! //
2023 //! // Read the current ADC clock configuration.
2024 //! //
2025 //! ui32Config = ADCClockConfigGet(ADC0_BASE, &ui32ClockDiv);
2026 //! \endverbatim
2027 //!
2028 //! \return The current clock configuration of the ADC defined as a combination
2029 //! of one of \b ADC_CLOCK_SRC_PLL, \b ADC_CLOCK_SRC_PIOSC,
2030 //! \b ADC_CLOCK_SRC_MOSC, or \b ADC_CLOCK_SRC_ALTCLK logical ORed with one of
2031 //! \b ADC_CLOCK_RATE_FULL, \b ADC_CLOCK_RATE_HALF, \b ADC_CLOCK_RATE_QUARTER,
2032 //! or \b ADC_CLOCK_RATE_EIGHTH. See ADCClockConfigSet() for more information
2033 //! on these values.
2034 //
2035 //*****************************************************************************
2036 uint32_t
ADCClockConfigGet(uint32_t ui32Base,uint32_t * pui32ClockDiv)2037 ADCClockConfigGet(uint32_t ui32Base, uint32_t *pui32ClockDiv)
2038 {
2039 uint32_t ui32Config;
2040
2041 //
2042 // Check the argument.
2043 //
2044 ASSERT(ui32Base == ADC0_BASE);
2045
2046 //
2047 // Read the current configuration.
2048 //
2049 ui32Config = HWREG(ADC0_BASE + ADC_O_CC);
2050
2051 //
2052 // If the clock divider was requested provide the current value.
2053 //
2054 if(pui32ClockDiv)
2055 {
2056 *pui32ClockDiv =
2057 ((ui32Config & ADC_CC_CLKDIV_M) >> ADC_CC_CLKDIV_S) + 1;
2058 }
2059
2060 //
2061 // Clear out the divider bits.
2062 //
2063 ui32Config &= ~ADC_CC_CLKDIV_M;
2064
2065 //
2066 // Add in the sample interval to the configuration.
2067 //
2068 ui32Config |= (HWREG(ADC0_BASE + ADC_O_PC) & ADC_PC_SR_M) << 4;
2069
2070 return(ui32Config);
2071 }
2072
2073 //*****************************************************************************
2074 //
2075 // Close the Doxygen group.
2076 //! @}
2077 //
2078 //*****************************************************************************
2079