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