1 //*****************************************************************************
2 //
3 // adc.c - Driver for the ADC.
4 //
5 // Copyright (c) 2005-2017 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.1.4.178 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; must be 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 //!
576 //! This function configures the ADC for one step of a sample sequence.  The
577 //! ADC can be configured for single-ended or differential operation (the
578 //! \b ADC_CTL_D bit selects differential operation when set), the channel to
579 //! be sampled can be chosen (the \b ADC_CTL_CH0 through \b ADC_CTL_CH23
580 //! values), and the internal temperature sensor can be selected (the
581 //! \b ADC_CTL_TS bit).  Additionally, this step can be defined as the last in
582 //! the sequence (the \b ADC_CTL_END bit) and it can be configured to cause an
583 //! interrupt when the step is complete (the \b ADC_CTL_IE bit).  If the
584 //! digital comparators are present on the device, this step may also be
585 //! configured to send the ADC sample to the selected comparator using
586 //! \b ADC_CTL_CMP0 through \b ADC_CTL_CMP7.  The configuration is used by the
587 //! ADC at the appropriate time when the trigger for this sequence occurs.
588 //!
589 //! \note If the Digital Comparator is present and enabled using the
590 //! \b ADC_CTL_CMP0 through \b ADC_CTL_CMP7 selects, the ADC sample is NOT
591 //! written into the ADC sequence data FIFO.
592 //!
593 //! The \e ui32Step parameter determines the order in which the samples are
594 //! captured by the ADC when the trigger occurs.  It can range from zero to
595 //! seven for the first sample sequencer, from zero to three for the second and
596 //! third sample sequencer, and can only be zero for the fourth sample
597 //! sequencer.
598 //!
599 //! Differential mode only works with adjacent channel pairs (for example, 0
600 //! and 1).  The channel select must be the number of the channel pair to
601 //! sample (for example, \b ADC_CTL_CH0 for 0 and 1, or \b ADC_CTL_CH1 for 2
602 //! and 3) or undefined results are returned by the ADC.  Additionally, if
603 //! differential mode is selected when the temperature sensor is being sampled,
604 //! undefined results are returned by the ADC.
605 //!
606 //! It is the responsibility of the caller to ensure that a valid configuration
607 //! is specified; this function does not check the validity of the specified
608 //! configuration.
609 //!
610 //! \return None.
611 //
612 //*****************************************************************************
613 void
ADCSequenceStepConfigure(uint32_t ui32Base,uint32_t ui32SequenceNum,uint32_t ui32Step,uint32_t ui32Config)614 ADCSequenceStepConfigure(uint32_t ui32Base, uint32_t ui32SequenceNum,
615                          uint32_t ui32Step, uint32_t ui32Config)
616 {
617     uint32_t ui32Temp;
618 
619     //
620     // Check the arguments.
621     //
622     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
623     ASSERT(ui32SequenceNum < 4);
624     ASSERT(((ui32SequenceNum == 0) && (ui32Step < 8)) ||
625            ((ui32SequenceNum == 1) && (ui32Step < 4)) ||
626            ((ui32SequenceNum == 2) && (ui32Step < 4)) ||
627            ((ui32SequenceNum == 3) && (ui32Step < 1)));
628 
629     //
630     // Get the offset of the sequence to be configured.
631     //
632     ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
633 
634     //
635     // Compute the shift for the bits that control this step.
636     //
637     ui32Step *= 4;
638 
639     //
640     // Set the analog mux value for this step.
641     //
642     HWREG(ui32Base + ADC_SSMUX) = ((HWREG(ui32Base + ADC_SSMUX) &
643                                     ~(0x0000000f << ui32Step)) |
644                                    ((ui32Config & 0x0f) << ui32Step));
645 
646     //
647     // Set the upper bits of the analog mux value for this step.
648     //
649     HWREG(ui32Base + ADC_SSEMUX) = ((HWREG(ui32Base + ADC_SSEMUX) &
650                                      ~(0x0000000f << ui32Step)) |
651                                     (((ui32Config & 0xf00) >> 8) << ui32Step));
652 
653     //
654     // Set the control value for this step.
655     //
656     HWREG(ui32Base + ADC_SSCTL) = ((HWREG(ui32Base + ADC_SSCTL) &
657                                     ~(0x0000000f << ui32Step)) |
658                                    (((ui32Config & 0xf0) >> 4) << ui32Step));
659 
660     //
661     // Set the sample and hold time for this step.  This is not available on
662     // all devices, however on devices that do not support this feature these
663     // reserved bits are ignored on write access.
664     //
665     HWREG(ui32Base + ADC_SSTSH) = ((HWREG(ui32Base + ADC_SSTSH) &
666                                     ~(0x0000000f << ui32Step)) |
667                                 (((ui32Config & 0xf00000) >> 20) << ui32Step));
668 
669     //
670     // Enable digital comparator if specified in the ui32Config bit-fields.
671     //
672     if(ui32Config & 0x000F0000)
673     {
674         //
675         // Program the comparator for the specified step.
676         //
677         ui32Temp = HWREG(ui32Base + ADC_SSDC);
678         ui32Temp &= ~(0xF << ui32Step);
679         ui32Temp |= (((ui32Config & 0x00070000) >> 16) << ui32Step);
680         HWREG(ui32Base + ADC_SSDC) = ui32Temp;
681 
682         //
683         // Enable the comparator.
684         //
685         HWREG(ui32Base + ADC_SSOP) |= (1 << ui32Step);
686     }
687 
688     //
689     // Disable digital comparator if not specified.
690     //
691     else
692     {
693         HWREG(ui32Base + ADC_SSOP) &= ~(1 << ui32Step);
694     }
695 }
696 
697 //*****************************************************************************
698 //
699 //! Determines if a sample sequence overflow occurred.
700 //!
701 //! \param ui32Base is the base address of the ADC module.
702 //! \param ui32SequenceNum is the sample sequence number.
703 //!
704 //! This function determines if a sample sequence overflow has occurred.
705 //! Overflow happens if the captured samples are not read from the FIFO before
706 //! the next trigger occurs.
707 //!
708 //! \return Returns zero if there was not an overflow, and non-zero if there
709 //! was.
710 //
711 //*****************************************************************************
712 int32_t
ADCSequenceOverflow(uint32_t ui32Base,uint32_t ui32SequenceNum)713 ADCSequenceOverflow(uint32_t ui32Base, uint32_t ui32SequenceNum)
714 {
715     //
716     // Check the arguments.
717     //
718     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
719     ASSERT(ui32SequenceNum < 4);
720 
721     //
722     // Determine if there was an overflow on this sequence.
723     //
724     return(HWREG(ui32Base + ADC_O_OSTAT) & (1 << ui32SequenceNum));
725 }
726 
727 //*****************************************************************************
728 //
729 //! Clears the overflow condition on a sample sequence.
730 //!
731 //! \param ui32Base is the base address of the ADC module.
732 //! \param ui32SequenceNum is the sample sequence number.
733 //!
734 //! This function clears an overflow condition on one of the sample sequences.
735 //! The overflow condition must be cleared in order to detect a subsequent
736 //! overflow condition (it otherwise causes no harm).
737 //!
738 //! \return None.
739 //
740 //*****************************************************************************
741 void
ADCSequenceOverflowClear(uint32_t ui32Base,uint32_t ui32SequenceNum)742 ADCSequenceOverflowClear(uint32_t ui32Base, uint32_t ui32SequenceNum)
743 {
744     //
745     // Check the arguments.
746     //
747     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
748     ASSERT(ui32SequenceNum < 4);
749 
750     //
751     // Clear the overflow condition for this sequence.
752     //
753     HWREG(ui32Base + ADC_O_OSTAT) = 1 << ui32SequenceNum;
754 }
755 
756 //*****************************************************************************
757 //
758 //! Determines if a sample sequence underflow occurred.
759 //!
760 //! \param ui32Base is the base address of the ADC module.
761 //! \param ui32SequenceNum is the sample sequence number.
762 //!
763 //! This function determines if a sample sequence underflow has occurred.
764 //! Underflow happens if too many samples are read from the FIFO.
765 //!
766 //! \return Returns zero if there was not an underflow, and non-zero if there
767 //! was.
768 //
769 //*****************************************************************************
770 int32_t
ADCSequenceUnderflow(uint32_t ui32Base,uint32_t ui32SequenceNum)771 ADCSequenceUnderflow(uint32_t ui32Base, uint32_t ui32SequenceNum)
772 {
773     //
774     // Check the arguments.
775     //
776     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
777     ASSERT(ui32SequenceNum < 4);
778 
779     //
780     // Determine if there was an underflow on this sequence.
781     //
782     return(HWREG(ui32Base + ADC_O_USTAT) & (1 << ui32SequenceNum));
783 }
784 
785 //*****************************************************************************
786 //
787 //! Clears the underflow condition on a sample sequence.
788 //!
789 //! \param ui32Base is the base address of the ADC module.
790 //! \param ui32SequenceNum is the sample sequence number.
791 //!
792 //! This function clears an underflow condition on one of the sample
793 //! sequencers.  The underflow condition must be cleared in order to detect a
794 //! subsequent underflow condition (it otherwise causes no harm).
795 //!
796 //! \return None.
797 //
798 //*****************************************************************************
799 void
ADCSequenceUnderflowClear(uint32_t ui32Base,uint32_t ui32SequenceNum)800 ADCSequenceUnderflowClear(uint32_t ui32Base, uint32_t ui32SequenceNum)
801 {
802     //
803     // Check the arguments.
804     //
805     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
806     ASSERT(ui32SequenceNum < 4);
807 
808     //
809     // Clear the underflow condition for this sequence.
810     //
811     HWREG(ui32Base + ADC_O_USTAT) = 1 << ui32SequenceNum;
812 }
813 
814 //*****************************************************************************
815 //
816 //! Gets the captured data for a sample sequence.
817 //!
818 //! \param ui32Base is the base address of the ADC module.
819 //! \param ui32SequenceNum is the sample sequence number.
820 //! \param pui32Buffer is the address where the data is stored.
821 //!
822 //! This function copies data from the specified sample sequencer output FIFO
823 //! to a memory resident buffer.  The number of samples available in the
824 //! hardware FIFO are copied into the buffer, which is assumed to be large
825 //! enough to hold that many samples.  This function only returns the samples
826 //! that are presently available, which may not be the entire sample sequence
827 //! if it is in the process of being executed.
828 //!
829 //! \return Returns the number of samples copied to the buffer.
830 //
831 //*****************************************************************************
832 int32_t
ADCSequenceDataGet(uint32_t ui32Base,uint32_t ui32SequenceNum,uint32_t * pui32Buffer)833 ADCSequenceDataGet(uint32_t ui32Base, uint32_t ui32SequenceNum,
834                    uint32_t *pui32Buffer)
835 {
836     uint32_t ui32Count;
837 
838     //
839     // Check the arguments.
840     //
841     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
842     ASSERT(ui32SequenceNum < 4);
843 
844     //
845     // Get the offset of the sequence to be read.
846     //
847     ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
848 
849     //
850     // Read samples from the FIFO until it is empty.
851     //
852     ui32Count = 0;
853     while(!(HWREG(ui32Base + ADC_SSFSTAT) & ADC_SSFSTAT0_EMPTY) &&
854           (ui32Count < 8))
855     {
856         //
857         // Read the FIFO and copy it to the destination.
858         //
859         *pui32Buffer++ = HWREG(ui32Base + ADC_SSFIFO);
860 
861         //
862         // Increment the count of samples read.
863         //
864         ui32Count++;
865     }
866 
867     //
868     // Return the number of samples read.
869     //
870     return(ui32Count);
871 }
872 
873 //*****************************************************************************
874 //
875 //! Causes a processor trigger for a sample sequence.
876 //!
877 //! \param ui32Base is the base address of the ADC module.
878 //! \param ui32SequenceNum is the sample sequence number, with
879 //! \b ADC_TRIGGER_WAIT or \b ADC_TRIGGER_SIGNAL optionally ORed into it.
880 //!
881 //! This function triggers a processor-initiated sample sequence if the sample
882 //! sequence trigger is configured to \b ADC_TRIGGER_PROCESSOR.  If
883 //! \b ADC_TRIGGER_WAIT is ORed into the sequence number, the
884 //! processor-initiated trigger is delayed until a later processor-initiated
885 //! trigger to a different ADC module that specifies \b ADC_TRIGGER_SIGNAL,
886 //! allowing multiple ADCs to start from a processor-initiated trigger in a
887 //! synchronous manner.
888 //!
889 //! \return None.
890 //
891 //*****************************************************************************
892 void
ADCProcessorTrigger(uint32_t ui32Base,uint32_t ui32SequenceNum)893 ADCProcessorTrigger(uint32_t ui32Base, uint32_t ui32SequenceNum)
894 {
895     //
896     // Check the arguments.
897     //
898     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
899     ASSERT(ui32SequenceNum < 4);
900 
901     //
902     // Generate a processor trigger for this sample sequence.
903     //
904     HWREG(ui32Base + ADC_O_PSSI) |= ((ui32SequenceNum & 0xffff0000) |
905                                      (1 << (ui32SequenceNum & 0xf)));
906 }
907 
908 //*****************************************************************************
909 //
910 //! Configures the software oversampling factor of the ADC.
911 //!
912 //! \param ui32Base is the base address of the ADC module.
913 //! \param ui32SequenceNum is the sample sequence number.
914 //! \param ui32Factor is the number of samples to be averaged.
915 //!
916 //! This function configures the software oversampling for the ADC, which can
917 //! be used to provide better resolution on the sampled data.  Oversampling is
918 //! accomplished by averaging multiple samples from the same analog input.
919 //! Three different oversampling rates are supported; 2x, 4x, and 8x.
920 //!
921 //! Oversampling is only supported on the sample sequencers that are more than
922 //! one sample in depth (that is, the fourth sample sequencer is not
923 //! supported).  Oversampling by 2x (for example) divides the depth of the
924 //! sample sequencer by two; so 2x oversampling on the first sample sequencer
925 //! can only provide four samples per trigger.  This also means that 8x
926 //! oversampling is only available on the first sample sequencer.
927 //!
928 //! \return None.
929 //
930 //*****************************************************************************
931 void
ADCSoftwareOversampleConfigure(uint32_t ui32Base,uint32_t ui32SequenceNum,uint32_t ui32Factor)932 ADCSoftwareOversampleConfigure(uint32_t ui32Base, uint32_t ui32SequenceNum,
933                                uint32_t ui32Factor)
934 {
935     uint32_t ui32Value;
936     uint32_t ui32ADCInst;
937 
938     //
939     // Check the arguments.
940     //
941     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
942     ASSERT(ui32SequenceNum < 3);
943     ASSERT(((ui32Factor == 2) || (ui32Factor == 4) || (ui32Factor == 8)) &&
944            ((ui32SequenceNum == 0) || (ui32Factor != 8)));
945 
946     //
947     // Convert the oversampling factor to a shift factor.
948     //
949     for(ui32Value = 0, ui32Factor >>= 1; ui32Factor;
950         ui32Value++, ui32Factor >>= 1)
951     {
952     }
953 
954     //
955     // Evaluate the ADC Instance.
956     //
957     if(ui32Base == ADC0_BASE)
958     {
959         ui32ADCInst = 0;
960     }
961     else
962     {
963         ui32ADCInst = 1;
964     }
965 
966     //
967     // Save the shift factor.
968     //
969     g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum] = ui32Value;
970 }
971 
972 //*****************************************************************************
973 //
974 //! Configures a step of the software oversampled sequencer.
975 //!
976 //! \param ui32Base is the base address of the ADC module.
977 //! \param ui32SequenceNum is the sample sequence number.
978 //! \param ui32Step is the step to be configured.
979 //! \param ui32Config is the configuration of this step.
980 //!
981 //! This function configures a step of the sample sequencer when using the
982 //! software oversampling feature.  The number of steps available depends on
983 //! the oversampling factor set by ADCSoftwareOversampleConfigure().  The value
984 //! of \e ui32Config is the same as defined for ADCSequenceStepConfigure().
985 //!
986 //! \return None.
987 //
988 //*****************************************************************************
989 void
ADCSoftwareOversampleStepConfigure(uint32_t ui32Base,uint32_t ui32SequenceNum,uint32_t ui32Step,uint32_t ui32Config)990 ADCSoftwareOversampleStepConfigure(uint32_t ui32Base, uint32_t ui32SequenceNum,
991                                    uint32_t ui32Step, uint32_t ui32Config)
992 {
993     uint32_t ui32ADCInst;
994 
995     //
996     // Evaluate the ADC Instance.
997     //
998     if(ui32Base == ADC0_BASE)
999     {
1000         ui32ADCInst = 0;
1001     }
1002     else
1003     {
1004         ui32ADCInst = 1;
1005     }
1006 
1007     //
1008     // Check the arguments.
1009     //
1010     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1011     ASSERT(ui32SequenceNum < 3);
1012     ASSERT(((ui32SequenceNum == 0) &&
1013             (ui32Step <
1014             (8 >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum]))) ||
1015            (ui32Step <
1016            (4 >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum])));
1017 
1018     //
1019     // Get the offset of the sequence to be configured.
1020     //
1021     ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
1022 
1023     //
1024     // Compute the shift for the bits that control this step.
1025     //
1026     ui32Step *= 4 << g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum];
1027 
1028     //
1029     // Loop through the hardware steps that make up this step of the software
1030     // oversampled sequence.
1031     //
1032     for(ui32SequenceNum =
1033         (1 << g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum]);
1034         ui32SequenceNum; ui32SequenceNum--)
1035     {
1036         //
1037         // Set the analog mux value for this step.
1038         //
1039         HWREG(ui32Base + ADC_SSMUX) = ((HWREG(ui32Base + ADC_SSMUX) &
1040                                         ~(0x0000000f << ui32Step)) |
1041                                        ((ui32Config & 0x0f) << ui32Step));
1042 
1043         //
1044         // Set the upper bits of the analog mux value for this step.
1045         //
1046         HWREG(ui32Base + ADC_SSEMUX) = ((HWREG(ui32Base + ADC_SSEMUX) &
1047                                          ~(0x0000000f << ui32Step)) |
1048                                         (((ui32Config & 0xf00) >> 8) <<
1049                                          ui32Step));
1050 
1051         //
1052         // Set the control value for this step.
1053         //
1054         HWREG(ui32Base + ADC_SSCTL) = ((HWREG(ui32Base + ADC_SSCTL) &
1055                                         ~(0x0000000f << ui32Step)) |
1056                                        (((ui32Config & 0xf0) >> 4) <<
1057                                         ui32Step));
1058         if(ui32SequenceNum != 1)
1059         {
1060             HWREG(ui32Base + ADC_SSCTL) &= ~((ADC_SSCTL0_IE0 |
1061                                               ADC_SSCTL0_END0) << ui32Step);
1062         }
1063 
1064         //
1065         // Go to the next hardware step.
1066         //
1067         ui32Step += 4;
1068     }
1069 }
1070 
1071 //*****************************************************************************
1072 //
1073 //! Gets the captured data for a sample sequence using software oversampling.
1074 //!
1075 //! \param ui32Base is the base address of the ADC module.
1076 //! \param ui32SequenceNum is the sample sequence number.
1077 //! \param pui32Buffer is the address where the data is stored.
1078 //! \param ui32Count is the number of samples to be read.
1079 //!
1080 //! This function copies data from the specified sample sequence output FIFO to
1081 //! a memory resident buffer with software oversampling applied.  The requested
1082 //! number of samples are copied into the data buffer; if there are not enough
1083 //! samples in the hardware FIFO to satisfy this many oversampled data items,
1084 //! then incorrect results are returned.  It is the caller's responsibility to
1085 //! read only the samples that are available and wait until enough data is
1086 //! available, for example as a result of receiving an interrupt.
1087 //!
1088 //! \return None.
1089 //
1090 //*****************************************************************************
1091 void
ADCSoftwareOversampleDataGet(uint32_t ui32Base,uint32_t ui32SequenceNum,uint32_t * pui32Buffer,uint32_t ui32Count)1092 ADCSoftwareOversampleDataGet(uint32_t ui32Base, uint32_t ui32SequenceNum,
1093                              uint32_t *pui32Buffer, uint32_t ui32Count)
1094 {
1095     uint32_t ui32Idx, ui32Accum;
1096     uint32_t ui32ADCInst;
1097 
1098     //
1099     // Evaluate the ADC Instance.
1100     //
1101     if(ui32Base == ADC0_BASE)
1102     {
1103         ui32ADCInst = 0;
1104     }
1105     else
1106     {
1107         ui32ADCInst = 1;
1108     }
1109 
1110 
1111     //
1112     // Check the arguments.
1113     //
1114     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1115     ASSERT(ui32SequenceNum < 3);
1116     ASSERT(((ui32SequenceNum == 0) &&
1117             (ui32Count <
1118             (8 >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum]))) ||
1119             (ui32Count <
1120             (4 >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum])));
1121 
1122     //
1123     // Get the offset of the sequence to be read.
1124     //
1125     ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
1126 
1127     //
1128     // Read the samples from the FIFO until it is empty.
1129     //
1130     while(ui32Count--)
1131     {
1132         //
1133         // Compute the sum of the samples.
1134         //
1135         ui32Accum = 0;
1136         for(ui32Idx = 1 << g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum];
1137             ui32Idx; ui32Idx--)
1138         {
1139             //
1140             // Read the FIFO and add it to the accumulator.
1141             //
1142             ui32Accum += HWREG(ui32Base + ADC_SSFIFO);
1143         }
1144 
1145         //
1146         // Write the averaged sample to the output buffer.
1147         //
1148         *pui32Buffer++ =
1149             ui32Accum >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum];
1150     }
1151 }
1152 
1153 //*****************************************************************************
1154 //
1155 //! Configures the hardware oversampling factor of the ADC.
1156 //!
1157 //! \param ui32Base is the base address of the ADC module.
1158 //! \param ui32Factor is the number of samples to be averaged.
1159 //!
1160 //! This function configures the hardware oversampling for the ADC, which can
1161 //! be used to provide better resolution on the sampled data.  Oversampling is
1162 //! accomplished by averaging multiple samples from the same analog input.  Six
1163 //! different oversampling rates are supported; 2x, 4x, 8x, 16x, 32x, and 64x.
1164 //! Specifying an oversampling factor of zero disables hardware
1165 //! oversampling.
1166 //!
1167 //! Hardware oversampling applies uniformly to all sample sequencers.  It does
1168 //! not reduce the depth of the sample sequencers like the software
1169 //! oversampling APIs; each sample written into the sample sequencer FIFO is a
1170 //! fully oversampled analog input reading.
1171 //!
1172 //! Enabling hardware averaging increases the precision of the ADC at the cost
1173 //! of throughput.  For example, enabling 4x oversampling reduces the
1174 //! throughput of a 250 k samples/second ADC to 62.5 k samples/second.
1175 //!
1176 //! \return None.
1177 //
1178 //*****************************************************************************
1179 void
ADCHardwareOversampleConfigure(uint32_t ui32Base,uint32_t ui32Factor)1180 ADCHardwareOversampleConfigure(uint32_t ui32Base, uint32_t ui32Factor)
1181 {
1182     uint32_t ui32Value;
1183 
1184     //
1185     // Check the arguments.
1186     //
1187     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1188     ASSERT(((ui32Factor == 0) || (ui32Factor == 2) || (ui32Factor == 4) ||
1189             (ui32Factor == 8) || (ui32Factor == 16) || (ui32Factor == 32) ||
1190             (ui32Factor == 64)));
1191 
1192     //
1193     // Convert the oversampling factor to a shift factor.
1194     //
1195     for(ui32Value = 0, ui32Factor >>= 1; ui32Factor;
1196         ui32Value++, ui32Factor >>= 1)
1197     {
1198     }
1199 
1200     //
1201     // Write the shift factor to the ADC to configure the hardware oversampler.
1202     //
1203     HWREG(ui32Base + ADC_O_SAC) = ui32Value;
1204 }
1205 
1206 //*****************************************************************************
1207 //
1208 //! Configures an ADC digital comparator.
1209 //!
1210 //! \param ui32Base is the base address of the ADC module.
1211 //! \param ui32Comp is the index of the comparator to configure.
1212 //! \param ui32Config is the configuration of the comparator.
1213 //!
1214 //! This function configures a comparator.  The \e ui32Config parameter is
1215 //! the result of a logical OR operation between the \b ADC_COMP_TRIG_xxx, and
1216 //! \b ADC_COMP_INT_xxx values.
1217 //!
1218 //! The \b ADC_COMP_TRIG_xxx term can take on the following values:
1219 //!
1220 //! - \b ADC_COMP_TRIG_NONE to never trigger PWM fault condition.
1221 //! - \b ADC_COMP_TRIG_LOW_ALWAYS to always trigger PWM fault condition when
1222 //! ADC output is in the low-band.
1223 //! - \b ADC_COMP_TRIG_LOW_ONCE to trigger PWM fault condition once when ADC
1224 //! output transitions into the low-band.
1225 //! - \b ADC_COMP_TRIG_LOW_HALWAYS to always trigger PWM fault condition when
1226 //! ADC output is in the low-band only if ADC output has been in the high-band
1227 //! since the last trigger output.
1228 //! - \b ADC_COMP_TRIG_LOW_HONCE to trigger PWM fault condition once when ADC
1229 //! output transitions into low-band only if ADC output has been in the
1230 //! high-band since the last trigger output.
1231 //! - \b ADC_COMP_TRIG_MID_ALWAYS to always trigger PWM fault condition when
1232 //! ADC output is in the mid-band.
1233 //! - \b ADC_COMP_TRIG_MID_ONCE to trigger PWM fault condition once when ADC
1234 //! output transitions into the mid-band.
1235 //! - \b ADC_COMP_TRIG_HIGH_ALWAYS to always trigger PWM fault condition when
1236 //! ADC output is in the high-band.
1237 //! - \b ADC_COMP_TRIG_HIGH_ONCE to trigger PWM fault condition once when ADC
1238 //! output transitions into the high-band.
1239 //! - \b ADC_COMP_TRIG_HIGH_HALWAYS to always trigger PWM fault condition when
1240 //! ADC output is in the high-band only if ADC output has been in the low-band
1241 //! since the last trigger output.
1242 //! - \b ADC_COMP_TRIG_HIGH_HONCE to trigger PWM fault condition once when ADC
1243 //! output transitions into high-band only if ADC output has been in the
1244 //! low-band since the last trigger output.
1245 //!
1246 //! The \b ADC_COMP_INT_xxx term can take on the following values:
1247 //!
1248 //! - \b ADC_COMP_INT_NONE to never generate ADC interrupt.
1249 //! - \b ADC_COMP_INT_LOW_ALWAYS to always generate ADC interrupt when ADC
1250 //! output is in the low-band.
1251 //! - \b ADC_COMP_INT_LOW_ONCE to generate ADC interrupt once when ADC output
1252 //! transitions into the low-band.
1253 //! - \b ADC_COMP_INT_LOW_HALWAYS to always generate ADC interrupt when ADC
1254 //! output is in the low-band only if ADC output has been in the high-band
1255 //! since the last trigger output.
1256 //! - \b ADC_COMP_INT_LOW_HONCE to generate ADC interrupt once when ADC output
1257 //! transitions into low-band only if ADC output has been in the high-band
1258 //! since the last trigger output.
1259 //! - \b ADC_COMP_INT_MID_ALWAYS to always generate ADC interrupt when ADC
1260 //! output is in the mid-band.
1261 //! - \b ADC_COMP_INT_MID_ONCE to generate ADC interrupt once when ADC output
1262 //! transitions into the mid-band.
1263 //! - \b ADC_COMP_INT_HIGH_ALWAYS to always generate ADC interrupt when ADC
1264 //! output is in the high-band.
1265 //! - \b ADC_COMP_INT_HIGH_ONCE to generate ADC interrupt once when ADC output
1266 //! transitions into the high-band.
1267 //! - \b ADC_COMP_INT_HIGH_HALWAYS to always generate ADC interrupt when ADC
1268 //! output is in the high-band only if ADC output has been in the low-band
1269 //! since the last trigger output.
1270 //! - \b ADC_COMP_INT_HIGH_HONCE to generate ADC interrupt once when ADC output
1271 //! transitions into high-band only if ADC output has been in the low-band
1272 //! since the last trigger output.
1273 //!
1274 //! \return None.
1275 //
1276 //*****************************************************************************
1277 void
ADCComparatorConfigure(uint32_t ui32Base,uint32_t ui32Comp,uint32_t ui32Config)1278 ADCComparatorConfigure(uint32_t ui32Base, uint32_t ui32Comp,
1279                        uint32_t ui32Config)
1280 {
1281     //
1282     // Check the arguments.
1283     //
1284     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1285     ASSERT(ui32Comp < 8);
1286 
1287     //
1288     // Save the new setting.
1289     //
1290     HWREG(ui32Base + ADC_O_DCCTL0 + (ui32Comp * 4)) = ui32Config;
1291 }
1292 
1293 //*****************************************************************************
1294 //
1295 //! Defines the ADC digital comparator regions.
1296 //!
1297 //! \param ui32Base is the base address of the ADC module.
1298 //! \param ui32Comp is the index of the comparator to configure.
1299 //! \param ui32LowRef is the reference point for the low/mid band threshold.
1300 //! \param ui32HighRef is the reference point for the mid/high band threshold.
1301 //!
1302 //! The ADC digital comparator operation is based on three ADC value regions:
1303 //! - \b low-band is defined as any ADC value less than or equal to the
1304 //!   \e ui32LowRef value.
1305 //! - \b mid-band is defined as any ADC value greater than the \e ui32LowRef
1306 //!   value but less than or equal to the \e ui32HighRef value.
1307 //! - \b high-band is defined as any ADC value greater than the \e ui32HighRef
1308 //!   value.
1309 //!
1310 //! \return None.
1311 //
1312 //*****************************************************************************
1313 void
ADCComparatorRegionSet(uint32_t ui32Base,uint32_t ui32Comp,uint32_t ui32LowRef,uint32_t ui32HighRef)1314 ADCComparatorRegionSet(uint32_t ui32Base, uint32_t ui32Comp,
1315                        uint32_t ui32LowRef, uint32_t ui32HighRef)
1316 {
1317     //
1318     // Check the arguments.
1319     //
1320     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1321     ASSERT(ui32Comp < 8);
1322     ASSERT((ui32LowRef < 4096) && (ui32LowRef <= ui32HighRef));
1323     ASSERT(ui32HighRef < 4096);
1324 
1325     //
1326     // Save the new region settings.
1327     //
1328     HWREG(ui32Base + ADC_O_DCCMP0 + (ui32Comp * 4)) = ((ui32HighRef << 16) |
1329                                                        ui32LowRef);
1330 }
1331 
1332 //*****************************************************************************
1333 //
1334 //! Resets the current ADC digital comparator conditions.
1335 //!
1336 //! \param ui32Base is the base address of the ADC module.
1337 //! \param ui32Comp is the index of the comparator.
1338 //! \param bTrigger is the flag to indicate reset of Trigger conditions.
1339 //! \param bInterrupt is the flag to indicate reset of Interrupt conditions.
1340 //!
1341 //! Because the digital comparator uses current and previous ADC values, this
1342 //! function allows the comparator to be reset to its initial
1343 //! value to prevent stale data from being used when a sequence is enabled.
1344 //!
1345 //! \return None.
1346 //
1347 //*****************************************************************************
1348 void
ADCComparatorReset(uint32_t ui32Base,uint32_t ui32Comp,bool bTrigger,bool bInterrupt)1349 ADCComparatorReset(uint32_t ui32Base, uint32_t ui32Comp, bool bTrigger,
1350                    bool bInterrupt)
1351 {
1352     uint32_t ui32Temp;
1353 
1354     //
1355     // Check the arguments.
1356     //
1357     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1358     ASSERT(ui32Comp < 8);
1359 
1360     //
1361     // Set the appropriate bits to reset the trigger and/or interrupt
1362     // comparator conditions.
1363     //
1364     ui32Temp = 0;
1365     if(bTrigger)
1366     {
1367         ui32Temp |= (1 << (16 + ui32Comp));
1368     }
1369     if(bInterrupt)
1370     {
1371         ui32Temp |= (1 << ui32Comp);
1372     }
1373 
1374     HWREG(ui32Base + ADC_O_DCRIC) = ui32Temp;
1375 }
1376 
1377 //*****************************************************************************
1378 //
1379 //! Disables a sample sequence comparator interrupt.
1380 //!
1381 //! \param ui32Base is the base address of the ADC module.
1382 //! \param ui32SequenceNum is the sample sequence number.
1383 //!
1384 //! This function disables the requested sample sequence comparator interrupt.
1385 //!
1386 //! \return None.
1387 //
1388 //*****************************************************************************
1389 void
ADCComparatorIntDisable(uint32_t ui32Base,uint32_t ui32SequenceNum)1390 ADCComparatorIntDisable(uint32_t ui32Base, uint32_t ui32SequenceNum)
1391 {
1392     //
1393     // Check the arguments.
1394     //
1395     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1396     ASSERT(ui32SequenceNum < 4);
1397 
1398     //
1399     // Disable this sample sequence comparator interrupt.
1400     //
1401     HWREG(ui32Base + ADC_O_IM) &= ~(0x10000 << ui32SequenceNum);
1402 }
1403 
1404 //*****************************************************************************
1405 //
1406 //! Enables a sample sequence comparator interrupt.
1407 //!
1408 //! \param ui32Base is the base address of the ADC module.
1409 //! \param ui32SequenceNum is the sample sequence number.
1410 //!
1411 //! This function enables the requested sample sequence comparator interrupt.
1412 //!
1413 //! \return None.
1414 //
1415 //*****************************************************************************
1416 void
ADCComparatorIntEnable(uint32_t ui32Base,uint32_t ui32SequenceNum)1417 ADCComparatorIntEnable(uint32_t ui32Base, uint32_t ui32SequenceNum)
1418 {
1419     //
1420     // Check the arguments.
1421     //
1422     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1423     ASSERT(ui32SequenceNum < 4);
1424 
1425     //
1426     // Enable this sample sequence interrupt.
1427     //
1428     HWREG(ui32Base + ADC_O_IM) |= 0x10000 << ui32SequenceNum;
1429 }
1430 
1431 //*****************************************************************************
1432 //
1433 //! Gets the current comparator interrupt status.
1434 //!
1435 //! \param ui32Base is the base address of the ADC module.
1436 //!
1437 //! This function returns the digital comparator interrupt status bits.  This
1438 //! status is sequence agnostic.
1439 //!
1440 //! \return The current comparator interrupt status.
1441 //
1442 //*****************************************************************************
1443 uint32_t
ADCComparatorIntStatus(uint32_t ui32Base)1444 ADCComparatorIntStatus(uint32_t ui32Base)
1445 {
1446     //
1447     // Check the arguments.
1448     //
1449     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1450 
1451     //
1452     // Return the digital comparator interrupt status.
1453     //
1454     return(HWREG(ui32Base + ADC_O_DCISC));
1455 }
1456 
1457 //*****************************************************************************
1458 //
1459 //! Clears sample sequence comparator interrupt source.
1460 //!
1461 //! \param ui32Base is the base address of the ADC module.
1462 //! \param ui32Status is the bit-mapped interrupts status to clear.
1463 //!
1464 //! The specified interrupt status is cleared.
1465 //!
1466 //! \return None.
1467 //
1468 //*****************************************************************************
1469 void
ADCComparatorIntClear(uint32_t ui32Base,uint32_t ui32Status)1470 ADCComparatorIntClear(uint32_t ui32Base, uint32_t ui32Status)
1471 {
1472     //
1473     // Check the arguments.
1474     //
1475     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1476 
1477     //
1478     // Clear the interrupt.
1479     //
1480     HWREG(ui32Base + ADC_O_DCISC) = ui32Status;
1481 }
1482 
1483 //*****************************************************************************
1484 //
1485 //! Disables ADC interrupt sources.
1486 //!
1487 //! \param ui32Base is the base address of the ADC module.
1488 //! \param ui32IntFlags is the bit mask of the interrupt sources to disable.
1489 //!
1490 //! This function disables the indicated ADC interrupt sources.  Only the
1491 //! sources that are enabled can be reflected to the processor interrupt;
1492 //! disabled sources have no effect on the processor.
1493 //!
1494 //! The \e ui32IntFlags parameter is the logical OR of any of the following:
1495 //!
1496 //! - \b ADC_INT_SS0 - interrupt due to ADC sample sequence 0.
1497 //! - \b ADC_INT_SS1 - interrupt due to ADC sample sequence 1.
1498 //! - \b ADC_INT_SS2 - interrupt due to ADC sample sequence 2.
1499 //! - \b ADC_INT_SS3 - interrupt due to ADC sample sequence 3.
1500 //! - \b ADC_INT_DMA_SS0 - interrupt due to DMA on ADC sample sequence 0.
1501 //! - \b ADC_INT_DMA_SS1 - interrupt due to DMA on ADC sample sequence 1.
1502 //! - \b ADC_INT_DMA_SS2 - interrupt due to DMA on ADC sample sequence 2.
1503 //! - \b ADC_INT_DMA_SS3 - interrupt due to DMA on ADC sample sequence 3.
1504 //! - \b ADC_INT_DCON_SS0 - interrupt due to digital comparator on ADC sample
1505 //!   sequence 0.
1506 //! - \b ADC_INT_DCON_SS1 - interrupt due to digital comparator on ADC sample
1507 //!   sequence 1.
1508 //! - \b ADC_INT_DCON_SS2 - interrupt due to digital comparator on ADC sample
1509 //!   sequence 2.
1510 //! - \b ADC_INT_DCON_SS3 - interrupt due to digital comparator on ADC sample
1511 //!   sequence 3.
1512 //!
1513 //! \return None.
1514 //
1515 //*****************************************************************************
1516 void
ADCIntDisableEx(uint32_t ui32Base,uint32_t ui32IntFlags)1517 ADCIntDisableEx(uint32_t ui32Base, uint32_t ui32IntFlags)
1518 {
1519     //
1520     // Check the arguments.
1521     //
1522     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1523 
1524     //
1525     // Disable the requested interrupts.
1526     //
1527     HWREG(ui32Base + ADC_O_IM) &= ~ui32IntFlags;
1528 }
1529 
1530 //*****************************************************************************
1531 //
1532 //! Enables ADC interrupt sources.
1533 //!
1534 //! \param ui32Base is the base address of the ADC module.
1535 //! \param ui32IntFlags is the bit mask of the interrupt sources to disable.
1536 //!
1537 //! This function enables the indicated ADC interrupt sources.  Only the
1538 //! sources that are enabled can be reflected to the processor interrupt;
1539 //! disabled sources have no effect on the processor.
1540 //!
1541 //! The \e ui32IntFlags parameter is the logical OR of any of the following:
1542 //!
1543 //! - \b ADC_INT_SS0 - interrupt due to ADC sample sequence 0.
1544 //! - \b ADC_INT_SS1 - interrupt due to ADC sample sequence 1.
1545 //! - \b ADC_INT_SS2 - interrupt due to ADC sample sequence 2.
1546 //! - \b ADC_INT_SS3 - interrupt due to ADC sample sequence 3.
1547 //! - \b ADC_INT_DMA_SS0 - interrupt due to DMA on ADC sample sequence 0.
1548 //! - \b ADC_INT_DMA_SS1 - interrupt due to DMA on ADC sample sequence 1.
1549 //! - \b ADC_INT_DMA_SS2 - interrupt due to DMA on ADC sample sequence 2.
1550 //! - \b ADC_INT_DMA_SS3 - interrupt due to DMA on ADC sample sequence 3.
1551 //! - \b ADC_INT_DCON_SS0 - interrupt due to digital comparator on ADC sample
1552 //!   sequence 0.
1553 //! - \b ADC_INT_DCON_SS1 - interrupt due to digital comparator on ADC sample
1554 //!   sequence 1.
1555 //! - \b ADC_INT_DCON_SS2 - interrupt due to digital comparator on ADC sample
1556 //!   sequence 2.
1557 //! - \b ADC_INT_DCON_SS3 - interrupt due to digital comparator on ADC sample
1558 //!   sequence 3.
1559 //!
1560 //! \return None.
1561 //
1562 //*****************************************************************************
1563 void
ADCIntEnableEx(uint32_t ui32Base,uint32_t ui32IntFlags)1564 ADCIntEnableEx(uint32_t ui32Base, uint32_t ui32IntFlags)
1565 {
1566     //
1567     // Check the arguments.
1568     //
1569     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1570 
1571     //
1572     // Enable the requested interrupts.
1573     //
1574     HWREG(ui32Base + ADC_O_IM) |= ui32IntFlags;
1575 }
1576 
1577 //*****************************************************************************
1578 //
1579 //! Gets interrupt status for the specified ADC module.
1580 //!
1581 //! \param ui32Base is the base address of the ADC module.
1582 //! \param bMasked specifies whether masked or raw interrupt status is
1583 //! returned.
1584 //!
1585 //! If \e bMasked is set as \b true, then the masked interrupt status is
1586 //! returned; otherwise, the raw interrupt status is returned.
1587 //!
1588 //! \return Returns the current interrupt status for the specified ADC module.
1589 //! The value returned is the logical OR of the \b ADC_INT_* values that are
1590 //! currently active.
1591 //
1592 //*****************************************************************************
1593 uint32_t
ADCIntStatusEx(uint32_t ui32Base,bool bMasked)1594 ADCIntStatusEx(uint32_t ui32Base, bool bMasked)
1595 {
1596     uint32_t ui32Temp;
1597 
1598     //
1599     // Check the arguments.
1600     //
1601     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1602 
1603     //
1604     // Return either the masked interrupt status or the raw interrupt status as
1605     // requested.
1606     //
1607     if(bMasked)
1608     {
1609         ui32Temp = HWREG(ui32Base + ADC_O_ISC);
1610     }
1611     else
1612     {
1613         //
1614         // Read the Raw interrupt status to see if a digital comparator
1615         // interrupt is active.
1616         //
1617         ui32Temp = HWREG(ui32Base + ADC_O_RIS);
1618 
1619         //
1620         // Since, the raw interrupt status only indicates that any one of the
1621         // digital comparators caused an interrupt, if the raw interrupt status
1622         // is set then the return value is modified to indicate that all sample
1623         // sequences have a pending digital comparator interrupt.
1624         // This is exactly how the hardware works so the return code is
1625         // modified to match this behavior.
1626         //
1627         if(ui32Temp & ADC_RIS_INRDC)
1628         {
1629             ui32Temp |= (ADC_INT_DCON_SS3 | ADC_INT_DCON_SS2 |
1630                          ADC_INT_DCON_SS1 | ADC_INT_DCON_SS0);
1631         }
1632     }
1633     return(ui32Temp);
1634 }
1635 
1636 //*****************************************************************************
1637 //
1638 //! Clears the specified ADC interrupt sources.
1639 //!
1640 //! \param ui32Base is the base address of the ADC port.
1641 //! \param ui32IntFlags is the bit mask of the interrupt sources to disable.
1642 //!
1643 //! Clears the interrupt for the specified interrupt source(s).
1644 //!
1645 //! The \e ui32IntFlags parameter is the logical OR of the \b ADC_INT_* values.
1646 //! See the ADCIntEnableEx() function for the list of possible \b ADC_INT*
1647 //! values.
1648 //!
1649 //! \note Because there is a write buffer in the Cortex-M processor, it may
1650 //! take several clock cycles before the interrupt source is actually cleared.
1651 //! Therefore, it is recommended that the interrupt source be cleared early in
1652 //! the interrupt handler (as opposed to the very last action) to avoid
1653 //! returning from the interrupt handler before the interrupt source is
1654 //! actually cleared.  Failure to do so may result in the interrupt handler
1655 //! being immediately reentered (because the interrupt controller still sees
1656 //! the interrupt source asserted).
1657 //!
1658 //! \return None.
1659 //
1660 //*****************************************************************************
1661 void
ADCIntClearEx(uint32_t ui32Base,uint32_t ui32IntFlags)1662 ADCIntClearEx(uint32_t ui32Base, uint32_t ui32IntFlags)
1663 {
1664     //
1665     // Note: The interrupt bits are "W1C" so we DO NOT use a logical OR
1666     // here to clear the requested bits. Doing so would clear all outstanding
1667     // interrupts rather than just those which the caller has specified.
1668     //
1669     HWREG(ui32Base + ADC_O_ISC) = ui32IntFlags;
1670 }
1671 
1672 //*****************************************************************************
1673 //
1674 //! Selects the ADC reference.
1675 //!
1676 //! \param ui32Base is the base address of the ADC module.
1677 //! \param ui32Ref is the reference to use.
1678 //!
1679 //! The ADC reference is set as specified by \e ui32Ref.  It must be one of
1680 //! \b ADC_REF_INT, or \b ADC_REF_EXT_3V for internal or external reference
1681 //! If \b ADC_REF_INT is chosen, then an internal 3V reference is used and
1682 //! no external reference is needed.  If \b ADC_REF_EXT_3V is chosen, then
1683 //! a 3V reference must be supplied to the AVREF pin.
1684 //!
1685 //! \note The ADC reference can only be selected on parts that have an external
1686 //! reference.  Consult the data sheet for your part to determine if there is
1687 //! an external reference.
1688 //!
1689 //! \return None.
1690 //
1691 //*****************************************************************************
1692 void
ADCReferenceSet(uint32_t ui32Base,uint32_t ui32Ref)1693 ADCReferenceSet(uint32_t ui32Base, uint32_t ui32Ref)
1694 {
1695     //
1696     // Check the arguments.
1697     //
1698     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1699     ASSERT((ui32Ref == ADC_REF_INT) || (ui32Ref == ADC_REF_EXT_3V));
1700 
1701     //
1702     // Set the reference.
1703     //
1704     HWREG(ui32Base + ADC_O_CTL) =
1705         (HWREG(ui32Base + ADC_O_CTL) & ~ADC_CTL_VREF_M) | ui32Ref;
1706 }
1707 
1708 //*****************************************************************************
1709 //
1710 //! Returns the current setting of the ADC reference.
1711 //!
1712 //! \param ui32Base is the base address of the ADC module.
1713 //!
1714 //! Returns the value of the ADC reference setting.  The returned value is one
1715 //! of \b ADC_REF_INT, or \b ADC_REF_EXT_3V.
1716 //!
1717 //! \note The value returned by this function is only meaningful if used on a
1718 //! part that is capable of using an external reference.  Consult the data
1719 //! sheet for your part to determine if it has an external reference input.
1720 //!
1721 //! \return The current setting of the ADC reference.
1722 //
1723 //*****************************************************************************
1724 uint32_t
ADCReferenceGet(uint32_t ui32Base)1725 ADCReferenceGet(uint32_t ui32Base)
1726 {
1727     //
1728     // Check the arguments.
1729     //
1730     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1731 
1732     //
1733     // Return the value of the reference.
1734     //
1735     return(HWREG(ui32Base + ADC_O_CTL) & ADC_CTL_VREF_M);
1736 }
1737 
1738 //*****************************************************************************
1739 //
1740 //! Sets the phase delay between a trigger and the start of a sequence.
1741 //!
1742 //! \param ui32Base is the base address of the ADC module.
1743 //! \param ui32Phase is the phase delay, specified as one of \b ADC_PHASE_0,
1744 //! \b ADC_PHASE_22_5, \b ADC_PHASE_45, \b ADC_PHASE_67_5, \b ADC_PHASE_90,
1745 //! \b ADC_PHASE_112_5, \b ADC_PHASE_135, \b ADC_PHASE_157_5, \b ADC_PHASE_180,
1746 //! \b ADC_PHASE_202_5, \b ADC_PHASE_225, \b ADC_PHASE_247_5, \b ADC_PHASE_270,
1747 //! \b ADC_PHASE_292_5, \b ADC_PHASE_315, or \b ADC_PHASE_337_5.
1748 //!
1749 //! This function sets the phase delay between the detection of an ADC trigger
1750 //! event and the start of the sample sequence.  By selecting a different phase
1751 //! delay for a pair of ADC modules (such as \b ADC_PHASE_0 and
1752 //! \b ADC_PHASE_180) and having each ADC module sample the same analog input,
1753 //! it is possible to increase the sampling rate of the analog input (with
1754 //! samples N, N+2, N+4, and so on, coming from the first ADC and samples N+1,
1755 //! N+3, N+5, and so on, coming from the second ADC).  The ADC module has a
1756 //! single phase delay that is applied to all sample sequences within that
1757 //! module.
1758 //!
1759 //! \note This capability is not available on all parts.
1760 //!
1761 //! \return None.
1762 //
1763 //*****************************************************************************
1764 void
ADCPhaseDelaySet(uint32_t ui32Base,uint32_t ui32Phase)1765 ADCPhaseDelaySet(uint32_t ui32Base, uint32_t ui32Phase)
1766 {
1767     //
1768     // Check the arguments.
1769     //
1770     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1771     ASSERT((ui32Phase == ADC_PHASE_0) || (ui32Phase == ADC_PHASE_22_5) ||
1772            (ui32Phase == ADC_PHASE_45) || (ui32Phase == ADC_PHASE_67_5) ||
1773            (ui32Phase == ADC_PHASE_90) || (ui32Phase == ADC_PHASE_112_5) ||
1774            (ui32Phase == ADC_PHASE_135) || (ui32Phase == ADC_PHASE_157_5) ||
1775            (ui32Phase == ADC_PHASE_180) || (ui32Phase == ADC_PHASE_202_5) ||
1776            (ui32Phase == ADC_PHASE_225) || (ui32Phase == ADC_PHASE_247_5) ||
1777            (ui32Phase == ADC_PHASE_270) || (ui32Phase == ADC_PHASE_292_5) ||
1778            (ui32Phase == ADC_PHASE_315) || (ui32Phase == ADC_PHASE_337_5));
1779 
1780     //
1781     // Set the phase delay.
1782     //
1783     HWREG(ui32Base + ADC_O_SPC) = ui32Phase;
1784 }
1785 
1786 //*****************************************************************************
1787 //
1788 //! Gets the phase delay between a trigger and the start of a sequence.
1789 //!
1790 //! \param ui32Base is the base address of the ADC module.
1791 //!
1792 //! This function gets the current phase delay between the detection of an ADC
1793 //! trigger event and the start of the sample sequence.
1794 //!
1795 //! \return Returns the phase delay, specified as one of \b ADC_PHASE_0,
1796 //! \b ADC_PHASE_22_5, \b ADC_PHASE_45, \b ADC_PHASE_67_5, \b ADC_PHASE_90,
1797 //! \b ADC_PHASE_112_5, \b ADC_PHASE_135, \b ADC_PHASE_157_5, \b ADC_PHASE_180,
1798 //! \b ADC_PHASE_202_5, \b ADC_PHASE_225, \b ADC_PHASE_247_5, \b ADC_PHASE_270,
1799 //! \b ADC_PHASE_292_5, \b ADC_PHASE_315, or \b ADC_PHASE_337_5.
1800 //
1801 //*****************************************************************************
1802 uint32_t
ADCPhaseDelayGet(uint32_t ui32Base)1803 ADCPhaseDelayGet(uint32_t ui32Base)
1804 {
1805     //
1806     // Check the arguments.
1807     //
1808     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1809 
1810     //
1811     // Return the phase delay.
1812     //
1813     return(HWREG(ui32Base + ADC_O_SPC));
1814 }
1815 
1816 //*****************************************************************************
1817 //
1818 //! Enables DMA for sample sequencers.
1819 //!
1820 //! \param ui32Base is the base address of the ADC module.
1821 //! \param ui32SequenceNum is the sample sequence number.
1822 //!
1823 //! Allows DMA requests to be generated based on the FIFO level of the sample
1824 //! sequencer.
1825 //!
1826 //! \return None.
1827 //
1828 //*****************************************************************************
1829 void
ADCSequenceDMAEnable(uint32_t ui32Base,uint32_t ui32SequenceNum)1830 ADCSequenceDMAEnable(uint32_t ui32Base, uint32_t ui32SequenceNum)
1831 {
1832     //
1833     // Check the arguments.
1834     //
1835     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1836     ASSERT(ui32SequenceNum < 4);
1837 
1838     //
1839     // Enable the DMA on the specified sequencer.
1840     //
1841     HWREG(ui32Base + ADC_O_ACTSS) |= 0x100 << ui32SequenceNum;
1842 }
1843 
1844 //*****************************************************************************
1845 //
1846 //! Disables DMA for sample sequencers.
1847 //!
1848 //! \param ui32Base is the base address of the ADC module.
1849 //! \param ui32SequenceNum is the sample sequence number.
1850 //!
1851 //! Prevents the specified sample sequencer from generating DMA requests.
1852 //!
1853 //! \return None.
1854 //
1855 //*****************************************************************************
1856 void
ADCSequenceDMADisable(uint32_t ui32Base,uint32_t ui32SequenceNum)1857 ADCSequenceDMADisable(uint32_t ui32Base, uint32_t ui32SequenceNum)
1858 {
1859     //
1860     // Check the arguments.
1861     //
1862     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1863     ASSERT(ui32SequenceNum < 4);
1864 
1865     //
1866     // Disable the DMA on the specified sequencer.
1867     //
1868     HWREG(ui32Base + ADC_O_ACTSS) &= ~(0x100 << ui32SequenceNum);
1869 }
1870 
1871 //*****************************************************************************
1872 //
1873 //! Determines whether the ADC is busy or not.
1874 //!
1875 //! \param ui32Base is the base address of the ADC.
1876 //!
1877 //! This function allows the caller to determine whether or not the ADC is
1878 //! currently sampling .  If \b false is returned, then the ADC is not
1879 //! sampling data.
1880 //!
1881 //! Use this function to detect that the ADC is finished sampling data before
1882 //! putting the device into deep sleep.  Before using this function, it is
1883 //! highly recommended that the event trigger is changed to
1884 //! \b ADC_TRIGGER_NEVER on all enabled sequencers to prevent the ADC from
1885 //! starting after checking the busy status.
1886 //!
1887 //! \return Returns \b true if the ADC is sampling or \b false if all
1888 //! samples are complete.
1889 //
1890 //*****************************************************************************
1891 bool
ADCBusy(uint32_t ui32Base)1892 ADCBusy(uint32_t ui32Base)
1893 {
1894     //
1895     // Check the argument.
1896     //
1897     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1898 
1899     //
1900     // Determine if the ADC is busy.
1901     //
1902     return((HWREG(ui32Base + ADC_O_ACTSS) & ADC_ACTSS_BUSY) ? true : false);
1903 }
1904 
1905 //*****************************************************************************
1906 //
1907 //! Sets the clock configuration for the ADC.
1908 //!
1909 //! \param ui32Base is the base address of the ADC to configure, which must
1910 //! always be \b ADC0_BASE.
1911 //! \param ui32Config is a combination of the \b ADC_CLOCK_SRC_ and
1912 //! \b ADC_CLOCK_RATE_* values used to configure the ADC clock input.
1913 //! \param ui32ClockDiv is the input clock divider for the clock selected by
1914 //! the \b ADC_CLOCK_SRC value.
1915 //!
1916 //! This function is used to configure the input clock to the ADC modules.  The
1917 //! clock configuration is shared across ADC units so \e ui32Base must
1918 //! always be \b ADC0_BASE.  The \e ui32Config value is logical OR of one
1919 //! of the \b ADC_CLOCK_RATE_ and one of the \b ADC_CLOCK_SRC_ values defined
1920 //! below. The \b ADC_CLOCK_SRC_* values determine the input clock for the ADC.
1921 //! Not all values are available on all devices so check the device data sheet
1922 //! to determine value configuration options.  Regardless of the source, the
1923 //! final frequency for TM4C123x devices must be 16 MHz and for TM4C129x parts
1924 //! after dividing must be between 16 and 32 MHz.
1925 //!
1926 //! \note For TM4C123x devices, if the PLL is enabled, the PLL/25 is used as
1927 //! the ADC clock unless ADC_CLOCK_SRC_PIOSC is specified.  If the PLL is
1928 //! disabled, the MOSC is used as the clock source unless ADC_CLOCK_SRC_PIOSC
1929 //! is specified.
1930 //!
1931 //! - \b ADC_CLOCK_SRC_PLL - The main PLL output (TM4x129 class only).
1932 //! - \b ADC_CLOCK_SRC_PIOSC - The internal PIOSC at 16 MHz.
1933 //! - \b ADC_CLOCK_SRC_ALTCLK - The output of the ALTCLK in the system control
1934 //!   module (TM4x129 class only).
1935 //! - \b ADC_CLOCK_SRC_MOSC - The external MOSC (TM4x129 class only).
1936 //!
1937 //! \b ADC_CLOCK_RATE values control how often samples are provided back to the
1938 //! application.  The values are the following:
1939 //!
1940 //! - \b ADC_CLOCK_RATE_FULL - All samples.
1941 //! - \b ADC_CLOCK_RATE_HALF - Every other sample.
1942 //! - \b ADC_CLOCK_RATE_QUARTER - Every fourth sample.
1943 //! - \b ADC_CLOCK_RATE_EIGHTH - Every either sample.
1944 //!
1945 //! The \e ui32ClockDiv parameter allows for dividing a higher frequency down
1946 //! into the valid range for the ADCs.  This parameter is typically only used
1947 //! \b ADC_CLOCK_SRC_PLL option because it is the only clock value that can be
1948 //! with the in the correct range to use the divider.  The actual value ranges
1949 //! from 1 to 64.
1950 //!
1951 //! \b Example: ADC Clock Configurations
1952 //!
1953 //! \verbatim
1954 //!
1955 //! //
1956 //! // Configure the ADC to use PIOSC divided by one (16 MHz) and sample at
1957 //! // half the rate.
1958 //! //
1959 //! ADCClockConfigSet(ADC0_BASE, ADC_CLOCK_SRC_PIOSC | ADC_CLOCK_RATE_HALF, 1);
1960 //!
1961 //! ...
1962 //!
1963 //! //
1964 //! // Configure the ADC to use PLL at 480 MHz divided by 24 to get an ADC
1965 //! // clock of 20 MHz.
1966 //! //
1967 //! ADCClockConfigSet(ADC0_BASE, ADC_CLOCK_SRC_PLL | ADC_CLOCK_RATE_FULL, 24);
1968 //! \endverbatim
1969 //!
1970 //! \return None.
1971 //
1972 //*****************************************************************************
1973 void
ADCClockConfigSet(uint32_t ui32Base,uint32_t ui32Config,uint32_t ui32ClockDiv)1974 ADCClockConfigSet(uint32_t ui32Base, uint32_t ui32Config,
1975                   uint32_t ui32ClockDiv)
1976 {
1977     //
1978     // Check the argument.
1979     //
1980     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
1981     ASSERT((ui32ClockDiv - 1) <= (ADC_CC_CLKDIV_M >> ADC_CC_CLKDIV_S));
1982 
1983     //
1984     // A rate must be supplied.
1985     //
1986     ASSERT((ui32Config & ADC_CLOCK_RATE_FULL) != 0);
1987 
1988     //
1989     // Write the sample conversion rate.
1990     //
1991     HWREG(ui32Base + ADC_O_PC) = (ui32Config >> 4) & ADC_PC_SR_M;
1992 
1993     //
1994     // Write the clock select and divider.
1995     //
1996     HWREG(ui32Base + ADC_O_CC) = (ui32Config & ADC_CC_CS_M) |
1997                                  (((ui32ClockDiv - 1) << ADC_CC_CLKDIV_S)) ;
1998 }
1999 
2000 //*****************************************************************************
2001 //
2002 //! Returns the clock configuration for the ADC.
2003 //!
2004 //! \param ui32Base is the base address of the ADC to configure, which must
2005 //! always be \b ADC0_BASE.
2006 //! \param pui32ClockDiv is a pointer to the input clock divider for the clock
2007 //! selected by the \b ADC_CLOCK_SRC in use by the ADCs.
2008 //!
2009 //! This function returns the ADC clock configuration and the clock divider for
2010 //! the ADCs.
2011 //!
2012 //! \b Example: Read the current ADC clock configuration.
2013 //!
2014 //! \verbatim
2015 //! uint32_t ui32Config, ui32ClockDiv;
2016 //!
2017 //! //
2018 //! // Read the current ADC clock configuration.
2019 //! //
2020 //! ui32Config = ADCClockConfigGet(ADC0_BASE, &ui32ClockDiv);
2021 //! \endverbatim
2022 //!
2023 //! \return The current clock configuration of the ADC defined as a combination
2024 //! of one of \b ADC_CLOCK_SRC_PLL, \b ADC_CLOCK_SRC_PIOSC,
2025 //! \b ADC_CLOCK_SRC_MOSC, or \b ADC_CLOCK_SRC_ALTCLK logical ORed with one of
2026 //! \b ADC_CLOCK_RATE_FULL, \b ADC_CLOCK_RATE_HALF, \b ADC_CLOCK_RATE_QUARTER,
2027 //! or \b ADC_CLOCK_RATE_EIGHTH.  See ADCClockConfigSet() for more information
2028 //! on these values.
2029 //
2030 //*****************************************************************************
2031 uint32_t
ADCClockConfigGet(uint32_t ui32Base,uint32_t * pui32ClockDiv)2032 ADCClockConfigGet(uint32_t ui32Base, uint32_t *pui32ClockDiv)
2033 {
2034     uint32_t ui32Config;
2035 
2036     //
2037     // Check the argument.
2038     //
2039     ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
2040 
2041     //
2042     // Read the current configuration.
2043     //
2044     ui32Config = HWREG(ui32Base + ADC_O_CC);
2045 
2046     //
2047     // If the clock divider was requested provide the current value.
2048     //
2049     if(pui32ClockDiv)
2050     {
2051         *pui32ClockDiv =
2052                     ((ui32Config & ADC_CC_CLKDIV_M) >> ADC_CC_CLKDIV_S) + 1;
2053     }
2054 
2055     //
2056     // Clear out the divider bits.
2057     //
2058     ui32Config &= ~ADC_CC_CLKDIV_M;
2059 
2060     //
2061     // Add in the sample interval to the configuration.
2062     //
2063     ui32Config |= (HWREG(ui32Base + ADC_O_PC) & ADC_PC_SR_M) << 4;
2064 
2065     return(ui32Config);
2066 }
2067 
2068 //*****************************************************************************
2069 //
2070 // Close the Doxygen group.
2071 //! @}
2072 //
2073 //*****************************************************************************
2074