1 //*****************************************************************************
2 //
3 // adc.c - Driver for the ADC.
4 //
5 // Copyright (c) 2005-2012 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 9453 of the Stellaris Peripheral Driver Library.
37 //
38 //*****************************************************************************
39
40 //*****************************************************************************
41 //
42 //! \addtogroup adc_api
43 //! @{
44 //
45 //*****************************************************************************
46
47 #include "inc/hw_adc.h"
48 #include "inc/hw_ints.h"
49 #include "inc/hw_memmap.h"
50 #include "inc/hw_types.h"
51 #include "inc/hw_sysctl.h"
52 #include "driverlib/adc.h"
53 #include "driverlib/debug.h"
54 #include "driverlib/interrupt.h"
55
56 //*****************************************************************************
57 //
58 // These defines are used by the ADC driver to simplify access to the ADC
59 // sequencer's registers.
60 //
61 //*****************************************************************************
62 #define ADC_SEQ (ADC_O_SSMUX0)
63 #define ADC_SEQ_STEP (ADC_O_SSMUX1 - ADC_O_SSMUX0)
64 #define ADC_SSMUX (ADC_O_SSMUX0 - ADC_O_SSMUX0)
65 #define ADC_SSEMUX (ADC_O_SSEMUX0 - ADC_O_SSMUX0)
66 #define ADC_SSCTL (ADC_O_SSCTL0 - ADC_O_SSMUX0)
67 #define ADC_SSFIFO (ADC_O_SSFIFO0 - ADC_O_SSMUX0)
68 #define ADC_SSFSTAT (ADC_O_SSFSTAT0 - ADC_O_SSMUX0)
69 #define ADC_SSOP (ADC_O_SSOP0 - ADC_O_SSMUX0)
70 #define ADC_SSDC (ADC_O_SSDC0 - ADC_O_SSMUX0)
71
72 //*****************************************************************************
73 //
74 // The currently configured software oversampling factor for each of the ADC
75 // sequencers.
76 //
77 //*****************************************************************************
78 static unsigned char g_pucOversampleFactor[3];
79
80 //*****************************************************************************
81 //
82 //! Registers an interrupt handler for an ADC interrupt.
83 //!
84 //! \param ulBase is the base address of the ADC module.
85 //! \param ulSequenceNum is the sample sequence number.
86 //! \param pfnHandler is a pointer to the function to be called when the
87 //! ADC sample sequence interrupt occurs.
88 //!
89 //! This function sets the handler to be called when a sample sequence
90 //! interrupt occurs. This function enables the global interrupt in the
91 //! interrupt controller; the sequence interrupt must be enabled with
92 //! ADCIntEnable(). It is the interrupt handler's responsibility to clear the
93 //! interrupt source via ADCIntClear().
94 //!
95 //! \sa IntRegister() for important information about registering interrupt
96 //! handlers.
97 //!
98 //! \return None.
99 //
100 //*****************************************************************************
101 void
ADCIntRegister(unsigned long ulBase,unsigned long ulSequenceNum,void (* pfnHandler)(void))102 ADCIntRegister(unsigned long ulBase, unsigned long ulSequenceNum,
103 void (*pfnHandler)(void))
104 {
105 unsigned long ulInt;
106
107 //
108 // Check the arguments.
109 //
110 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
111 ASSERT(ulSequenceNum < 4);
112
113 //
114 // Determine the interrupt to register based on the sequence number.
115 //
116 ulInt = ((ulBase == ADC0_BASE) ? (INT_ADC0SS0 + ulSequenceNum) :
117 (INT_ADC1SS0 + ulSequenceNum));
118
119 //
120 // Register the interrupt handler.
121 //
122 IntRegister(ulInt, pfnHandler);
123
124 //
125 // Enable the timer interrupt.
126 //
127 IntEnable(ulInt);
128 }
129
130 //*****************************************************************************
131 //
132 //! Unregisters the interrupt handler for an ADC interrupt.
133 //!
134 //! \param ulBase is the base address of the ADC module.
135 //! \param ulSequenceNum is the sample sequence number.
136 //!
137 //! This function unregisters the interrupt handler. This function disables
138 //! the global interrupt in the interrupt controller; the sequence interrupt
139 //! must be disabled via ADCIntDisable().
140 //!
141 //! \sa IntRegister() for important information about registering interrupt
142 //! handlers.
143 //!
144 //! \return None.
145 //
146 //*****************************************************************************
147 void
ADCIntUnregister(unsigned long ulBase,unsigned long ulSequenceNum)148 ADCIntUnregister(unsigned long ulBase, unsigned long ulSequenceNum)
149 {
150 unsigned long ulInt;
151
152 //
153 // Check the arguments.
154 //
155 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
156 ASSERT(ulSequenceNum < 4);
157
158 //
159 // Determine the interrupt to unregister based on the sequence number.
160 //
161 ulInt = ((ulBase == ADC0_BASE) ? (INT_ADC0SS0 + ulSequenceNum) :
162 (INT_ADC1SS0 + ulSequenceNum));
163
164 //
165 // Disable the interrupt.
166 //
167 IntDisable(ulInt);
168
169 //
170 // Unregister the interrupt handler.
171 //
172 IntUnregister(ulInt);
173 }
174
175 //*****************************************************************************
176 //
177 //! Disables a sample sequence interrupt.
178 //!
179 //! \param ulBase is the base address of the ADC module.
180 //! \param ulSequenceNum is the sample sequence number.
181 //!
182 //! This function disables the requested sample sequence interrupt.
183 //!
184 //! \return None.
185 //
186 //*****************************************************************************
187 void
ADCIntDisable(unsigned long ulBase,unsigned long ulSequenceNum)188 ADCIntDisable(unsigned long ulBase, unsigned long ulSequenceNum)
189 {
190 //
191 // Check the arguments.
192 //
193 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
194 ASSERT(ulSequenceNum < 4);
195
196 //
197 // Disable this sample sequence interrupt.
198 //
199 HWREG(ulBase + ADC_O_IM) &= ~(1 << ulSequenceNum);
200 }
201
202 //*****************************************************************************
203 //
204 //! Enables a sample sequence interrupt.
205 //!
206 //! \param ulBase is the base address of the ADC module.
207 //! \param ulSequenceNum is the sample sequence number.
208 //!
209 //! This function enables the requested sample sequence interrupt. Any
210 //! outstanding interrupts are cleared before enabling the sample sequence
211 //! interrupt.
212 //!
213 //! \return None.
214 //
215 //*****************************************************************************
216 void
ADCIntEnable(unsigned long ulBase,unsigned long ulSequenceNum)217 ADCIntEnable(unsigned long ulBase, unsigned long ulSequenceNum)
218 {
219 //
220 // Check the arguments.
221 //
222 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
223 ASSERT(ulSequenceNum < 4);
224
225 //
226 // Clear any outstanding interrupts on this sample sequence.
227 //
228 HWREG(ulBase + ADC_O_ISC) = 1 << ulSequenceNum;
229
230 //
231 // Enable this sample sequence interrupt.
232 //
233 HWREG(ulBase + ADC_O_IM) |= 1 << ulSequenceNum;
234 }
235
236 //*****************************************************************************
237 //
238 //! Gets the current interrupt status.
239 //!
240 //! \param ulBase is the base address of the ADC module.
241 //! \param ulSequenceNum is the sample sequence number.
242 //! \param bMasked is false if the raw interrupt status is required and true if
243 //! the masked interrupt status is required.
244 //!
245 //! This function returns the interrupt status for the specified sample
246 //! sequence. Either the raw interrupt status or the status of interrupts that
247 //! are allowed to reflect to the processor can be returned.
248 //!
249 //! \return The current raw or masked interrupt status.
250 //
251 //*****************************************************************************
252 unsigned long
ADCIntStatus(unsigned long ulBase,unsigned long ulSequenceNum,tBoolean bMasked)253 ADCIntStatus(unsigned long ulBase, unsigned long ulSequenceNum,
254 tBoolean bMasked)
255 {
256 unsigned long ulTemp;
257
258 //
259 // Check the arguments.
260 //
261 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
262 ASSERT(ulSequenceNum < 4);
263
264 //
265 // Return either the interrupt status or the raw interrupt status as
266 // requested.
267 //
268 if(bMasked)
269 {
270 ulTemp = HWREG(ulBase + ADC_O_ISC) & (0x10001 << ulSequenceNum);
271 }
272 else
273 {
274 ulTemp = HWREG(ulBase + ADC_O_RIS) & (0x10000 | (1 << ulSequenceNum));
275
276 //
277 // If the digital comparator status bit is set, reflect it to the
278 // appropriate sequence bit.
279 //
280 if(ulTemp & 0x10000)
281 {
282 ulTemp |= 0xF0000;
283 ulTemp &= ~(0x10000 << ulSequenceNum);
284 }
285 }
286
287 //
288 // Return the interrupt status
289 //
290 return(ulTemp);
291 }
292
293 //*****************************************************************************
294 //
295 //! Clears sample sequence interrupt source.
296 //!
297 //! \param ulBase is the base address of the ADC module.
298 //! \param ulSequenceNum is the sample sequence number.
299 //!
300 //! The specified sample sequence interrupt is cleared, so that it no longer
301 //! asserts. This function must be called in the interrupt handler to keep
302 //! the interrupt from being triggered again immediately upon exit.
303 //!
304 //! \note Because there is a write buffer in the Cortex-M processor, it may
305 //! take several clock cycles before the interrupt source is actually cleared.
306 //! Therefore, it is recommended that the interrupt source be cleared early in
307 //! the interrupt handler (as opposed to the very last action) to avoid
308 //! returning from the interrupt handler before the interrupt source is
309 //! actually cleared. Failure to do so may result in the interrupt handler
310 //! being immediately reentered (because the interrupt controller still sees
311 //! the interrupt source asserted).
312 //!
313 //! \return None.
314 //
315 //*****************************************************************************
316 void
ADCIntClear(unsigned long ulBase,unsigned long ulSequenceNum)317 ADCIntClear(unsigned long ulBase, unsigned long ulSequenceNum)
318 {
319 //
320 // Check the arguments.
321 //
322 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
323 ASSERT(ulSequenceNum < 4);
324
325 //
326 // Clear the interrupt.
327 //
328 HWREG(ulBase + ADC_O_ISC) = 1 << ulSequenceNum;
329 }
330
331 //*****************************************************************************
332 //
333 //! Enables a sample sequence.
334 //!
335 //! \param ulBase is the base address of the ADC module.
336 //! \param ulSequenceNum is the sample sequence number.
337 //!
338 //! Allows the specified sample sequence to be captured when its trigger is
339 //! detected. A sample sequence must be configured before it is enabled.
340 //!
341 //! \return None.
342 //
343 //*****************************************************************************
344 void
ADCSequenceEnable(unsigned long ulBase,unsigned long ulSequenceNum)345 ADCSequenceEnable(unsigned long ulBase, unsigned long ulSequenceNum)
346 {
347 //
348 // Check the arguments.
349 //
350 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
351 ASSERT(ulSequenceNum < 4);
352
353 //
354 // Enable the specified sequence.
355 //
356 HWREG(ulBase + ADC_O_ACTSS) |= 1 << ulSequenceNum;
357 }
358
359 //*****************************************************************************
360 //
361 //! Disables a sample sequence.
362 //!
363 //! \param ulBase is the base address of the ADC module.
364 //! \param ulSequenceNum is the sample sequence number.
365 //!
366 //! Prevents the specified sample sequence from being captured when its trigger
367 //! is detected. A sample sequence should be disabled before it is configured.
368 //!
369 //! \return None.
370 //
371 //*****************************************************************************
372 void
ADCSequenceDisable(unsigned long ulBase,unsigned long ulSequenceNum)373 ADCSequenceDisable(unsigned long ulBase, unsigned long ulSequenceNum)
374 {
375 //
376 // Check the arguments.
377 //
378 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
379 ASSERT(ulSequenceNum < 4);
380
381 //
382 // Disable the specified sequences.
383 //
384 HWREG(ulBase + ADC_O_ACTSS) &= ~(1 << ulSequenceNum);
385 }
386
387 //*****************************************************************************
388 //
389 //! Configures the trigger source and priority of a sample sequence.
390 //!
391 //! \param ulBase is the base address of the ADC module.
392 //! \param ulSequenceNum is the sample sequence number.
393 //! \param ulTrigger is the trigger source that initiates the sample sequence;
394 //! must be one of the \b ADC_TRIGGER_* values.
395 //! \param ulPriority is the relative priority of the sample sequence with
396 //! respect to the other sample sequences.
397 //!
398 //! This function configures the initiation criteria for a sample sequence.
399 //! Valid sample sequencers range from zero to three; sequencer zero captures
400 //! up to eight samples, sequencers one and two capture up to four samples,
401 //! and sequencer three captures a single sample. The trigger condition and
402 //! priority (with respect to other sample sequencer execution) are set.
403 //!
404 //! The \e ulTrigger parameter can take on the following values:
405 //!
406 //! - \b ADC_TRIGGER_PROCESSOR - A trigger generated by the processor, via the
407 //! ADCProcessorTrigger() function.
408 //! - \b ADC_TRIGGER_COMP0 - A trigger generated by the first analog
409 //! comparator; configured with ComparatorConfigure().
410 //! - \b ADC_TRIGGER_COMP1 - A trigger generated by the second analog
411 //! comparator; configured with ComparatorConfigure().
412 //! - \b ADC_TRIGGER_COMP2 - A trigger generated by the third analog
413 //! comparator; configured with ComparatorConfigure().
414 //! - \b ADC_TRIGGER_EXTERNAL - A trigger generated by an input from the Port
415 //! B4 pin. Note that some microcontrollers can
416 //! select from any GPIO using the
417 //! GPIOADCTriggerEnable() function.
418 //! - \b ADC_TRIGGER_TIMER - A trigger generated by a timer; configured with
419 //! TimerControlTrigger().
420 //! - \b ADC_TRIGGER_PWM0 - A trigger generated by the first PWM generator;
421 //! configured with PWMGenIntTrigEnable().
422 //! - \b ADC_TRIGGER_PWM1 - A trigger generated by the second PWM generator;
423 //! configured with PWMGenIntTrigEnable().
424 //! - \b ADC_TRIGGER_PWM2 - A trigger generated by the third PWM generator;
425 //! configured with PWMGenIntTrigEnable().
426 //! - \b ADC_TRIGGER_PWM3 - A trigger generated by the fourth PWM generator;
427 //! configured with PWMGenIntTrigEnable().
428 //! - \b ADC_TRIGGER_ALWAYS - A trigger that is always asserted, causing the
429 //! sample sequence to capture repeatedly (so long as
430 //! there is not a higher priority source active).
431 //!
432 //! Note that not all trigger sources are available on all Stellaris family
433 //! members; consult the data sheet for the device in question to determine the
434 //! availability of triggers.
435 //!
436 //! The \e ulPriority parameter is a value between 0 and 3, where 0 represents
437 //! the highest priority and 3 the lowest. Note that when programming the
438 //! priority among a set of sample sequences, each must have unique priority;
439 //! it is up to the caller to guarantee the uniqueness of the priorities.
440 //!
441 //! \return None.
442 //
443 //*****************************************************************************
444 void
ADCSequenceConfigure(unsigned long ulBase,unsigned long ulSequenceNum,unsigned long ulTrigger,unsigned long ulPriority)445 ADCSequenceConfigure(unsigned long ulBase, unsigned long ulSequenceNum,
446 unsigned long ulTrigger, unsigned long ulPriority)
447 {
448 //
449 // Check the arugments.
450 //
451 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
452 ASSERT(ulSequenceNum < 4);
453 ASSERT((ulTrigger == ADC_TRIGGER_PROCESSOR) ||
454 (ulTrigger == ADC_TRIGGER_COMP0) ||
455 (ulTrigger == ADC_TRIGGER_COMP1) ||
456 (ulTrigger == ADC_TRIGGER_COMP2) ||
457 (ulTrigger == ADC_TRIGGER_EXTERNAL) ||
458 (ulTrigger == ADC_TRIGGER_TIMER) ||
459 (ulTrigger == ADC_TRIGGER_PWM0) ||
460 (ulTrigger == ADC_TRIGGER_PWM1) ||
461 (ulTrigger == ADC_TRIGGER_PWM2) ||
462 (ulTrigger == ADC_TRIGGER_PWM3) ||
463 (ulTrigger == ADC_TRIGGER_ALWAYS));
464 ASSERT(ulPriority < 4);
465
466 //
467 // Compute the shift for the bits that control this sample sequence.
468 //
469 ulSequenceNum *= 4;
470
471 //
472 // Set the trigger event for this sample sequence.
473 //
474 HWREG(ulBase + ADC_O_EMUX) = ((HWREG(ulBase + ADC_O_EMUX) &
475 ~(0xf << ulSequenceNum)) |
476 ((ulTrigger & 0xf) << ulSequenceNum));
477
478 //
479 // Set the priority for this sample sequence.
480 //
481 HWREG(ulBase + ADC_O_SSPRI) = ((HWREG(ulBase + ADC_O_SSPRI) &
482 ~(0xf << ulSequenceNum)) |
483 ((ulPriority & 0x3) << ulSequenceNum));
484 }
485
486 //*****************************************************************************
487 //
488 //! Configure a step of the sample sequencer.
489 //!
490 //! \param ulBase is the base address of the ADC module.
491 //! \param ulSequenceNum is the sample sequence number.
492 //! \param ulStep is the step to be configured.
493 //! \param ulConfig is the configuration of this step; must be a logical OR of
494 //! \b ADC_CTL_TS, \b ADC_CTL_IE, \b ADC_CTL_END, \b ADC_CTL_D, one of the
495 //! input channel selects (\b ADC_CTL_CH0 through \b ADC_CTL_CH23), and one of
496 //! the digital comparator selects (\b ADC_CTL_CMP0 through \b ADC_CTL_CMP7).
497 //!
498 //! This function configures the ADC for one step of a sample sequence. The
499 //! ADC can be configured for single-ended or differential operation
500 //! (the \b ADC_CTL_D bit selects differential operation when set), the
501 //! channel to be sampled can be chosen (the \b ADC_CTL_CH0 through
502 //! \b ADC_CTL_CH23 values), and the internal temperature sensor can be
503 //! selected (the \b ADC_CTL_TS bit). Additionally, this step can be defined
504 //! as the last in the sequence (the \b ADC_CTL_END bit) and it can be
505 //! configured to cause an interrupt when the step is complete (the
506 //! \b ADC_CTL_IE bit). If the digital comparators are present on the device,
507 //! this step may also be configured to send the ADC sample to the selected
508 //! comparator using \b ADC_CTL_CMP0 through \b ADC_CTL_CMP7. The configuration
509 //! is used by the ADC at the appropriate time when the trigger for
510 //! this sequence occurs.
511 //!
512 //! \note If the Digital Comparator is present and enabled using the
513 //! \b ADC_CTL_CMP0 through \b ADC_CTL_CMP7 selects, the ADC sample is NOT
514 //! written into the ADC sequence data FIFO.
515 //!
516 //! The \e ulStep parameter determines the order in which the samples are
517 //! captured by the ADC when the trigger occurs. It can range from zero to
518 //! seven for the first sample sequencer, from zero to three for the second and
519 //! third sample sequencer, and can only be zero for the fourth sample
520 //! sequencer.
521 //!
522 //! Differential mode only works with adjacent channel pairs (for example, 0
523 //! and 1). The channel select must be the number of the channel pair to
524 //! sample (for example, \b ADC_CTL_CH0 for 0 and 1, or \b ADC_CTL_CH1 for 2
525 //! and 3) or undefined results are returned by the ADC. Additionally, if
526 //! differential mode is selected when the temperature sensor is being sampled,
527 //! undefined results are returned by the ADC.
528 //!
529 //! It is the responsibility of the caller to ensure that a valid configuration
530 //! is specified; this function does not check the validity of the specified
531 //! configuration.
532 //!
533 //! \return None.
534 //
535 //*****************************************************************************
536 void
ADCSequenceStepConfigure(unsigned long ulBase,unsigned long ulSequenceNum,unsigned long ulStep,unsigned long ulConfig)537 ADCSequenceStepConfigure(unsigned long ulBase, unsigned long ulSequenceNum,
538 unsigned long ulStep, unsigned long ulConfig)
539 {
540 unsigned long ulTemp;
541
542 //
543 // Check the arguments.
544 //
545 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
546 ASSERT(ulSequenceNum < 4);
547 ASSERT(((ulSequenceNum == 0) && (ulStep < 8)) ||
548 ((ulSequenceNum == 1) && (ulStep < 4)) ||
549 ((ulSequenceNum == 2) && (ulStep < 4)) ||
550 ((ulSequenceNum == 3) && (ulStep < 1)));
551
552 //
553 // Get the offset of the sequence to be configured.
554 //
555 ulBase += ADC_SEQ + (ADC_SEQ_STEP * ulSequenceNum);
556
557 //
558 // Compute the shift for the bits that control this step.
559 //
560 ulStep *= 4;
561
562 //
563 // Set the analog mux value for this step.
564 //
565 HWREG(ulBase + ADC_SSMUX) = ((HWREG(ulBase + ADC_SSMUX) &
566 ~(0x0000000f << ulStep)) |
567 ((ulConfig & 0x0f) << ulStep));
568
569 //
570 // Set the upper bits of the analog mux value for this step.
571 //
572 HWREG(ulBase + ADC_SSEMUX) = ((HWREG(ulBase + ADC_SSEMUX) &
573 ~(0x0000000f << ulStep)) |
574 (((ulConfig & 0xf00) >> 8) << ulStep));
575
576 //
577 // Set the control value for this step.
578 //
579 HWREG(ulBase + ADC_SSCTL) = ((HWREG(ulBase + ADC_SSCTL) &
580 ~(0x0000000f << ulStep)) |
581 (((ulConfig & 0xf0) >> 4) << ulStep));
582
583 //
584 // Enable digital comparator if specified in the ulConfig bit-fields.
585 //
586 if(ulConfig & 0x000F0000)
587 {
588 //
589 // Program the comparator for the specified step.
590 //
591 ulTemp = HWREG(ulBase + ADC_SSDC);
592 ulTemp &= ~(0xF << ulStep);
593 ulTemp |= (((ulConfig & 0x00070000) >> 16) << ulStep);
594 HWREG(ulBase + ADC_SSDC) = ulTemp;
595
596 //
597 // Enable the comparator.
598 //
599 ulTemp = HWREG(ulBase + ADC_SSOP);
600 ulTemp |= (1 << ulStep);
601 HWREG(ulBase + ADC_SSOP) = ulTemp;
602 }
603
604 //
605 // Disable digital comparator if not specified.
606 //
607 else
608 {
609 ulTemp = HWREG(ulBase + ADC_SSOP);
610 ulTemp &= ~(1 << ulStep);
611 HWREG(ulBase + ADC_SSOP) = ulTemp;
612 }
613 }
614
615 //*****************************************************************************
616 //
617 //! Determines if a sample sequence overflow occurred.
618 //!
619 //! \param ulBase is the base address of the ADC module.
620 //! \param ulSequenceNum is the sample sequence number.
621 //!
622 //! This function determines if a sample sequence overflow has occurred.
623 //! Overflow happens if the captured samples are not read from the FIFO before
624 //! the next trigger occurs.
625 //!
626 //! \return Returns zero if there was not an overflow, and non-zero if there
627 //! was.
628 //
629 //*****************************************************************************
630 long
ADCSequenceOverflow(unsigned long ulBase,unsigned long ulSequenceNum)631 ADCSequenceOverflow(unsigned long ulBase, unsigned long ulSequenceNum)
632 {
633 //
634 // Check the arguments.
635 //
636 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
637 ASSERT(ulSequenceNum < 4);
638
639 //
640 // Determine if there was an overflow on this sequence.
641 //
642 return(HWREG(ulBase + ADC_O_OSTAT) & (1 << ulSequenceNum));
643 }
644
645 //*****************************************************************************
646 //
647 //! Clears the overflow condition on a sample sequence.
648 //!
649 //! \param ulBase is the base address of the ADC module.
650 //! \param ulSequenceNum is the sample sequence number.
651 //!
652 //! This function clears an overflow condition on one of the sample sequences.
653 //! The overflow condition must be cleared in order to detect a subsequent
654 //! overflow condition (it otherwise causes no harm).
655 //!
656 //! \return None.
657 //
658 //*****************************************************************************
659 void
ADCSequenceOverflowClear(unsigned long ulBase,unsigned long ulSequenceNum)660 ADCSequenceOverflowClear(unsigned long ulBase, unsigned long ulSequenceNum)
661 {
662 //
663 // Check the arguments.
664 //
665 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
666 ASSERT(ulSequenceNum < 4);
667
668 //
669 // Clear the overflow condition for this sequence.
670 //
671 HWREG(ulBase + ADC_O_OSTAT) = 1 << ulSequenceNum;
672 }
673
674 //*****************************************************************************
675 //
676 //! Determines if a sample sequence underflow occurred.
677 //!
678 //! \param ulBase is the base address of the ADC module.
679 //! \param ulSequenceNum is the sample sequence number.
680 //!
681 //! This function determines if a sample sequence underflow has occurred.
682 //! Underflow happens if too many samples are read from the FIFO.
683 //!
684 //! \return Returns zero if there was not an underflow, and non-zero if there
685 //! was.
686 //
687 //*****************************************************************************
688 long
ADCSequenceUnderflow(unsigned long ulBase,unsigned long ulSequenceNum)689 ADCSequenceUnderflow(unsigned long ulBase, unsigned long ulSequenceNum)
690 {
691 //
692 // Check the arguments.
693 //
694 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
695 ASSERT(ulSequenceNum < 4);
696
697 //
698 // Determine if there was an underflow on this sequence.
699 //
700 return(HWREG(ulBase + ADC_O_USTAT) & (1 << ulSequenceNum));
701 }
702
703 //*****************************************************************************
704 //
705 //! Clears the underflow condition on a sample sequence.
706 //!
707 //! \param ulBase is the base address of the ADC module.
708 //! \param ulSequenceNum is the sample sequence number.
709 //!
710 //! This function clears an underflow condition on one of the sample sequencers.
711 //! The underflow condition must be cleared in order to detect a subsequent
712 //! underflow condition (it otherwise causes no harm).
713 //!
714 //! \return None.
715 //
716 //*****************************************************************************
717 void
ADCSequenceUnderflowClear(unsigned long ulBase,unsigned long ulSequenceNum)718 ADCSequenceUnderflowClear(unsigned long ulBase, unsigned long ulSequenceNum)
719 {
720 //
721 // Check the arguments.
722 //
723 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
724 ASSERT(ulSequenceNum < 4);
725
726 //
727 // Clear the underflow condition for this sequence.
728 //
729 HWREG(ulBase + ADC_O_USTAT) = 1 << ulSequenceNum;
730 }
731
732 //*****************************************************************************
733 //
734 //! Gets the captured data for a sample sequence.
735 //!
736 //! \param ulBase is the base address of the ADC module.
737 //! \param ulSequenceNum is the sample sequence number.
738 //! \param pulBuffer is the address where the data is stored.
739 //!
740 //! This function copies data from the specified sample sequencer output FIFO
741 //! to a memory resident buffer. The number of samples available in the
742 //! hardware FIFO are copied into the buffer, which is assumed to be large
743 //! enough to hold that many samples. This function only returns the samples
744 //! that are presently available, which may not be the entire sample sequence
745 //! if it is in the process of being executed.
746 //!
747 //! \return Returns the number of samples copied to the buffer.
748 //
749 //*****************************************************************************
750 long
ADCSequenceDataGet(unsigned long ulBase,unsigned long ulSequenceNum,unsigned long * pulBuffer)751 ADCSequenceDataGet(unsigned long ulBase, unsigned long ulSequenceNum,
752 unsigned long *pulBuffer)
753 {
754 unsigned long ulCount;
755
756 //
757 // Check the arguments.
758 //
759 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
760 ASSERT(ulSequenceNum < 4);
761
762 //
763 // Get the offset of the sequence to be read.
764 //
765 ulBase += ADC_SEQ + (ADC_SEQ_STEP * ulSequenceNum);
766
767 //
768 // Read samples from the FIFO until it is empty.
769 //
770 ulCount = 0;
771 while(!(HWREG(ulBase + ADC_SSFSTAT) & ADC_SSFSTAT0_EMPTY) && (ulCount < 8))
772 {
773 //
774 // Read the FIFO and copy it to the destination.
775 //
776 *pulBuffer++ = HWREG(ulBase + ADC_SSFIFO);
777
778 //
779 // Increment the count of samples read.
780 //
781 ulCount++;
782 }
783
784 //
785 // Return the number of samples read.
786 //
787 return(ulCount);
788 }
789
790 //*****************************************************************************
791 //
792 //! Causes a processor trigger for a sample sequence.
793 //!
794 //! \param ulBase is the base address of the ADC module.
795 //! \param ulSequenceNum is the sample sequence number, with
796 //! \b ADC_TRIGGER_WAIT or \b ADC_TRIGGER_SIGNAL optionally ORed into it.
797 //!
798 //! This function triggers a processor-initiated sample sequence if the sample
799 //! sequence trigger is configured to \b ADC_TRIGGER_PROCESSOR. If
800 //! \b ADC_TRIGGER_WAIT is ORed into the sequence number, the
801 //! processor-initiated trigger is delayed until a later processor-initiated
802 //! trigger to a different ADC module that specifies \b ADC_TRIGGER_SIGNAL,
803 //! allowing multiple ADCs to start from a processor-initiated trigger in a
804 //! synchronous manner.
805 //!
806 //! \return None.
807 //
808 //*****************************************************************************
809 void
ADCProcessorTrigger(unsigned long ulBase,unsigned long ulSequenceNum)810 ADCProcessorTrigger(unsigned long ulBase, unsigned long ulSequenceNum)
811 {
812 //
813 // Check the arguments.
814 //
815 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
816 ASSERT((ulSequenceNum & 0xf) < 4);
817
818 //
819 // Generate a processor trigger for this sample sequence.
820 //
821 HWREG(ulBase + ADC_O_PSSI) |= ((ulSequenceNum & 0xffff0000) |
822 (1 << (ulSequenceNum & 0xf)));
823 }
824
825 //*****************************************************************************
826 //
827 //! Configures the software oversampling factor of the ADC.
828 //!
829 //! \param ulBase is the base address of the ADC module.
830 //! \param ulSequenceNum is the sample sequence number.
831 //! \param ulFactor is the number of samples to be averaged.
832 //!
833 //! This function configures the software oversampling for the ADC, which can
834 //! be used to provide better resolution on the sampled data. Oversampling is
835 //! accomplished by averaging multiple samples from the same analog input.
836 //! Three different oversampling rates are supported; 2x, 4x, and 8x.
837 //!
838 //! Oversampling is only supported on the sample sequencers that are more than
839 //! one sample in depth (that is, the fourth sample sequencer is not
840 //! supported). Oversampling by 2x (for example) divides the depth of the
841 //! sample sequencer by two; so 2x oversampling on the first sample sequencer
842 //! can only provide four samples per trigger. This also means that 8x
843 //! oversampling is only available on the first sample sequencer.
844 //!
845 //! \return None.
846 //
847 //*****************************************************************************
848 void
ADCSoftwareOversampleConfigure(unsigned long ulBase,unsigned long ulSequenceNum,unsigned long ulFactor)849 ADCSoftwareOversampleConfigure(unsigned long ulBase,
850 unsigned long ulSequenceNum,
851 unsigned long ulFactor)
852 {
853 unsigned long ulValue;
854
855 //
856 // Check the arguments.
857 //
858 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
859 ASSERT(ulSequenceNum < 3);
860 ASSERT(((ulFactor == 2) || (ulFactor == 4) || (ulFactor == 8)) &&
861 ((ulSequenceNum == 0) || (ulFactor != 8)));
862
863 //
864 // Convert the oversampling factor to a shift factor.
865 //
866 for(ulValue = 0, ulFactor >>= 1; ulFactor; ulValue++, ulFactor >>= 1)
867 {
868 }
869
870 //
871 // Save the shift factor.
872 //
873 g_pucOversampleFactor[ulSequenceNum] = ulValue;
874 }
875
876 //*****************************************************************************
877 //
878 //! Configures a step of the software oversampled sequencer.
879 //!
880 //! \param ulBase is the base address of the ADC module.
881 //! \param ulSequenceNum is the sample sequence number.
882 //! \param ulStep is the step to be configured.
883 //! \param ulConfig is the configuration of this step.
884 //!
885 //! This function configures a step of the sample sequencer when using the
886 //! software oversampling feature. The number of steps available depends on
887 //! the oversampling factor set by ADCSoftwareOversampleConfigure(). The value
888 //! of \e ulConfig is the same as defined for ADCSequenceStepConfigure().
889 //!
890 //! \return None.
891 //
892 //*****************************************************************************
893 void
ADCSoftwareOversampleStepConfigure(unsigned long ulBase,unsigned long ulSequenceNum,unsigned long ulStep,unsigned long ulConfig)894 ADCSoftwareOversampleStepConfigure(unsigned long ulBase,
895 unsigned long ulSequenceNum,
896 unsigned long ulStep,
897 unsigned long ulConfig)
898 {
899 //
900 // Check the arguments.
901 //
902 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
903 ASSERT(ulSequenceNum < 3);
904 ASSERT(((ulSequenceNum == 0) &&
905 (ulStep < (8UL >> g_pucOversampleFactor[ulSequenceNum]))) ||
906 (ulStep < (4UL >> g_pucOversampleFactor[ulSequenceNum])));
907
908 //
909 // Get the offset of the sequence to be configured.
910 //
911 ulBase += ADC_SEQ + (ADC_SEQ_STEP * ulSequenceNum);
912
913 //
914 // Compute the shift for the bits that control this step.
915 //
916 ulStep *= 4 << g_pucOversampleFactor[ulSequenceNum];
917
918 //
919 // Loop through the hardware steps that make up this step of the software
920 // oversampled sequence.
921 //
922 for(ulSequenceNum = 1 << g_pucOversampleFactor[ulSequenceNum];
923 ulSequenceNum; ulSequenceNum--)
924 {
925 //
926 // Set the analog mux value for this step.
927 //
928 HWREG(ulBase + ADC_SSMUX) = ((HWREG(ulBase + ADC_SSMUX) &
929 ~(0x0000000f << ulStep)) |
930 ((ulConfig & 0x0f) << ulStep));
931
932 //
933 // Set the upper bits of the analog mux value for this step.
934 //
935 HWREG(ulBase + ADC_SSEMUX) = ((HWREG(ulBase + ADC_SSEMUX) &
936 ~(0x0000000f << ulStep)) |
937 (((ulConfig & 0xf00) >> 8) << ulStep));
938
939 //
940 // Set the control value for this step.
941 //
942 HWREG(ulBase + ADC_SSCTL) = ((HWREG(ulBase + ADC_SSCTL) &
943 ~(0x0000000f << ulStep)) |
944 (((ulConfig & 0xf0) >> 4) << ulStep));
945 if(ulSequenceNum != 1)
946 {
947 HWREG(ulBase + ADC_SSCTL) &= ~((ADC_SSCTL0_IE0 |
948 ADC_SSCTL0_END0) << ulStep);
949 }
950
951 //
952 // Go to the next hardware step.
953 //
954 ulStep += 4;
955 }
956 }
957
958 //*****************************************************************************
959 //
960 //! Gets the captured data for a sample sequence using software oversampling.
961 //!
962 //! \param ulBase is the base address of the ADC module.
963 //! \param ulSequenceNum is the sample sequence number.
964 //! \param pulBuffer is the address where the data is stored.
965 //! \param ulCount is the number of samples to be read.
966 //!
967 //! This function copies data from the specified sample sequence output FIFO to
968 //! a memory resident buffer with software oversampling applied. The requested
969 //! number of samples are copied into the data buffer; if there are not enough
970 //! samples in the hardware FIFO to satisfy this many oversampled data items,
971 //! then incorrect results are returned. It is the caller's responsibility to
972 //! read only the samples that are available and wait until enough data is
973 //! available, for example as a result of receiving an interrupt.
974 //!
975 //! \return None.
976 //
977 //*****************************************************************************
978 void
ADCSoftwareOversampleDataGet(unsigned long ulBase,unsigned long ulSequenceNum,unsigned long * pulBuffer,unsigned long ulCount)979 ADCSoftwareOversampleDataGet(unsigned long ulBase, unsigned long ulSequenceNum,
980 unsigned long *pulBuffer, unsigned long ulCount)
981 {
982 unsigned long ulIdx, ulAccum;
983
984 //
985 // Check the arguments.
986 //
987 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
988 ASSERT(ulSequenceNum < 3);
989 ASSERT(((ulSequenceNum == 0) &&
990 (ulCount < (8UL >> g_pucOversampleFactor[ulSequenceNum]))) ||
991 (ulCount < (4UL >> g_pucOversampleFactor[ulSequenceNum])));
992
993 //
994 // Get the offset of the sequence to be read.
995 //
996 ulBase += ADC_SEQ + (ADC_SEQ_STEP * ulSequenceNum);
997
998 //
999 // Read the samples from the FIFO until it is empty.
1000 //
1001 while(ulCount--)
1002 {
1003 //
1004 // Compute the sum of the samples.
1005 //
1006 ulAccum = 0;
1007 for(ulIdx = 1 << g_pucOversampleFactor[ulSequenceNum]; ulIdx; ulIdx--)
1008 {
1009 //
1010 // Read the FIFO and add it to the accumulator.
1011 //
1012 ulAccum += HWREG(ulBase + ADC_SSFIFO);
1013 }
1014
1015 //
1016 // Write the averaged sample to the output buffer.
1017 //
1018 *pulBuffer++ = ulAccum >> g_pucOversampleFactor[ulSequenceNum];
1019 }
1020 }
1021
1022 //*****************************************************************************
1023 //
1024 //! Configures the hardware oversampling factor of the ADC.
1025 //!
1026 //! \param ulBase is the base address of the ADC module.
1027 //! \param ulFactor is the number of samples to be averaged.
1028 //!
1029 //! This function configures the hardware oversampling for the ADC, which can
1030 //! be used to provide better resolution on the sampled data. Oversampling is
1031 //! accomplished by averaging multiple samples from the same analog input. Six
1032 //! different oversampling rates are supported; 2x, 4x, 8x, 16x, 32x, and 64x.
1033 //! Specifying an oversampling factor of zero disables hardware
1034 //! oversampling.
1035 //!
1036 //! Hardware oversampling applies uniformly to all sample sequencers. It does
1037 //! not reduce the depth of the sample sequencers like the software
1038 //! oversampling APIs; each sample written into the sample sequencer FIFO is a
1039 //! fully oversampled analog input reading.
1040 //!
1041 //! Enabling hardware averaging increases the precision of the ADC at the cost
1042 //! of throughput. For example, enabling 4x oversampling reduces the
1043 //! throughput of a 250 K samples/second ADC to 62.5 K samples/second.
1044 //!
1045 //! \return None.
1046 //
1047 //*****************************************************************************
1048 void
ADCHardwareOversampleConfigure(unsigned long ulBase,unsigned long ulFactor)1049 ADCHardwareOversampleConfigure(unsigned long ulBase, unsigned long ulFactor)
1050 {
1051 unsigned long ulValue;
1052
1053 //
1054 // Check the arguments.
1055 //
1056 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
1057 ASSERT(((ulFactor == 0) || (ulFactor == 2) || (ulFactor == 4) ||
1058 (ulFactor == 8) || (ulFactor == 16) || (ulFactor == 32) ||
1059 (ulFactor == 64)));
1060
1061 //
1062 // Convert the oversampling factor to a shift factor.
1063 //
1064 for(ulValue = 0, ulFactor >>= 1; ulFactor; ulValue++, ulFactor >>= 1)
1065 {
1066 }
1067
1068 //
1069 // Write the shift factor to the ADC to configure the hardware oversampler.
1070 //
1071 HWREG(ulBase + ADC_O_SAC) = ulValue;
1072 }
1073
1074 //*****************************************************************************
1075 //
1076 //! Configures an ADC digital comparator.
1077 //!
1078 //! \param ulBase is the base address of the ADC module.
1079 //! \param ulComp is the index of the comparator to configure.
1080 //! \param ulConfig is the configuration of the comparator.
1081 //!
1082 //! This function configures a comparator. The \e ulConfig parameter is
1083 //! the result of a logical OR operation between the \b ADC_COMP_TRIG_xxx, and
1084 //! \b ADC_COMP_INT_xxx values.
1085 //!
1086 //! The \b ADC_COMP_TRIG_xxx term can take on the following values:
1087 //!
1088 //! - \b ADC_COMP_TRIG_NONE to never trigger PWM fault condition.
1089 //! - \b ADC_COMP_TRIG_LOW_ALWAYS to always trigger PWM fault condition when
1090 //! ADC output is in the low-band.
1091 //! - \b ADC_COMP_TRIG_LOW_ONCE to trigger PWM fault condition once when ADC
1092 //! output transitions into the low-band.
1093 //! - \b ADC_COMP_TRIG_LOW_HALWAYS to always trigger PWM fault condition when
1094 //! ADC output is in the low-band only if ADC output has been in the high-band
1095 //! since the last trigger output.
1096 //! - \b ADC_COMP_TRIG_LOW_HONCE to trigger PWM fault condition once when ADC
1097 //! output transitions into low-band only if ADC output has been in the
1098 //! high-band since the last trigger output.
1099 //! - \b ADC_COMP_TRIG_MID_ALWAYS to always trigger PWM fault condition when
1100 //! ADC output is in the mid-band.
1101 //! - \b ADC_COMP_TRIG_MID_ONCE to trigger PWM fault condition once when ADC
1102 //! output transitions into the mid-band.
1103 //! - \b ADC_COMP_TRIG_HIGH_ALWAYS to always trigger PWM fault condition when
1104 //! ADC output is in the high-band.
1105 //! - \b ADC_COMP_TRIG_HIGH_ONCE to trigger PWM fault condition once when ADC
1106 //! output transitions into the high-band.
1107 //! - \b ADC_COMP_TRIG_HIGH_HALWAYS to always trigger PWM fault condition when
1108 //! ADC output is in the high-band only if ADC output has been in the low-band
1109 //! since the last trigger output.
1110 //! - \b ADC_COMP_TRIG_HIGH_HONCE to trigger PWM fault condition once when ADC
1111 //! output transitions into high-band only if ADC output has been in the
1112 //! low-band since the last trigger output.
1113 //!
1114 //! The \b ADC_COMP_INT_xxx term can take on the following values:
1115 //!
1116 //! - \b ADC_COMP_INT_NONE to never generate ADC interrupt.
1117 //! - \b ADC_COMP_INT_LOW_ALWAYS to always generate ADC interrupt when ADC
1118 //! output is in the low-band.
1119 //! - \b ADC_COMP_INT_LOW_ONCE to generate ADC interrupt once when ADC output
1120 //! transitions into the low-band.
1121 //! - \b ADC_COMP_INT_LOW_HALWAYS to always generate ADC interrupt when ADC
1122 //! output is in the low-band only if ADC output has been in the high-band
1123 //! since the last trigger output.
1124 //! - \b ADC_COMP_INT_LOW_HONCE to generate ADC interrupt once when ADC output
1125 //! transitions into low-band only if ADC output has been in the high-band
1126 //! since the last trigger output.
1127 //! - \b ADC_COMP_INT_MID_ALWAYS to always generate ADC interrupt when ADC
1128 //! output is in the mid-band.
1129 //! - \b ADC_COMP_INT_MID_ONCE to generate ADC interrupt once when ADC output
1130 //! transitions into the mid-band.
1131 //! - \b ADC_COMP_INT_HIGH_ALWAYS to always generate ADC interrupt when ADC
1132 //! output is in the high-band.
1133 //! - \b ADC_COMP_INT_HIGH_ONCE to generate ADC interrupt once when ADC output
1134 //! transitions into the high-band.
1135 //! - \b ADC_COMP_INT_HIGH_HALWAYS to always generate ADC interrupt when ADC
1136 //! output is in the high-band only if ADC output has been in the low-band
1137 //! since the last trigger output.
1138 //! - \b ADC_COMP_INT_HIGH_HONCE to generate ADC interrupt once when ADC output
1139 //! transitions into high-band only if ADC output has been in the low-band
1140 //! since the last trigger output.
1141 //!
1142 //! \return None.
1143 //
1144 //*****************************************************************************
1145 void
ADCComparatorConfigure(unsigned long ulBase,unsigned long ulComp,unsigned long ulConfig)1146 ADCComparatorConfigure(unsigned long ulBase, unsigned long ulComp,
1147 unsigned long ulConfig)
1148 {
1149 //
1150 // Check the arguments.
1151 //
1152 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
1153 ASSERT(ulComp < 8);
1154
1155 //
1156 // Save the new setting.
1157 //
1158 HWREG(ulBase + ADC_O_DCCTL0 + (ulComp * 4)) = ulConfig;
1159 }
1160
1161 //*****************************************************************************
1162 //
1163 //! Defines the ADC digital comparator regions.
1164 //!
1165 //! \param ulBase is the base address of the ADC module.
1166 //! \param ulComp is the index of the comparator to configure.
1167 //! \param ulLowRef is the reference point for the low/mid band threshold.
1168 //! \param ulHighRef is the reference point for the mid/high band threshold.
1169 //!
1170 //! The ADC digital comparator operation is based on three ADC value regions:
1171 //! - \b low-band is defined as any ADC value less than or equal to the
1172 //! \e ulLowRef value.
1173 //! - \b mid-band is defined as any ADC value greater than the \e ulLowRef
1174 //! value but less than or equal to the \e ulHighRef value.
1175 //! - \b high-band is defined as any ADC value greater than the \e ulHighRef
1176 //! value.
1177 //!
1178 //! \return None.
1179 //
1180 //*****************************************************************************
1181 void
ADCComparatorRegionSet(unsigned long ulBase,unsigned long ulComp,unsigned long ulLowRef,unsigned long ulHighRef)1182 ADCComparatorRegionSet(unsigned long ulBase, unsigned long ulComp,
1183 unsigned long ulLowRef, unsigned long ulHighRef)
1184 {
1185 //
1186 // Check the arguments.
1187 //
1188 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
1189 ASSERT(ulComp < 8);
1190 ASSERT((ulLowRef < 1024) && (ulLowRef <= ulHighRef));
1191 ASSERT(ulHighRef < 1024);
1192
1193 //
1194 // Save the new region settings.
1195 //
1196 HWREG(ulBase + ADC_O_DCCMP0 + (ulComp * 4)) = (ulHighRef << 16) | ulLowRef;
1197 }
1198
1199 //*****************************************************************************
1200 //
1201 //! Resets the current ADC digital comparator conditions.
1202 //!
1203 //! \param ulBase is the base address of the ADC module.
1204 //! \param ulComp is the index of the comparator.
1205 //! \param bTrigger is the flag to indicate reset of Trigger conditions.
1206 //! \param bInterrupt is the flag to indicate reset of Interrupt conditions.
1207 //!
1208 //! Because the digital comparator uses current and previous ADC values, this
1209 //! function allows the comparator to be reset to its initial
1210 //! value to prevent stale data from being used when a sequence is enabled.
1211 //!
1212 //! \return None.
1213 //
1214 //*****************************************************************************
1215 void
ADCComparatorReset(unsigned long ulBase,unsigned long ulComp,tBoolean bTrigger,tBoolean bInterrupt)1216 ADCComparatorReset(unsigned long ulBase, unsigned long ulComp,
1217 tBoolean bTrigger, tBoolean bInterrupt)
1218 {
1219 unsigned long ulTemp = 0;
1220
1221 //
1222 // Check the arguments.
1223 //
1224 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
1225 ASSERT(ulComp < 8);
1226
1227 //
1228 // Set the appropriate bits to reset the trigger and/or interrupt
1229 // comparator conditions.
1230 //
1231 if(bTrigger)
1232 {
1233 ulTemp |= (1 << (16 + ulComp));
1234 }
1235 if(bInterrupt)
1236 {
1237 ulTemp |= (1 << ulComp);
1238 }
1239
1240 HWREG(ulBase + ADC_O_DCRIC) = ulTemp;
1241 }
1242
1243 //*****************************************************************************
1244 //
1245 //! Disables a sample sequence comparator interrupt.
1246 //!
1247 //! \param ulBase is the base address of the ADC module.
1248 //! \param ulSequenceNum is the sample sequence number.
1249 //!
1250 //! This function disables the requested sample sequence comparator interrupt.
1251 //!
1252 //! \return None.
1253 //
1254 //*****************************************************************************
1255 void
ADCComparatorIntDisable(unsigned long ulBase,unsigned long ulSequenceNum)1256 ADCComparatorIntDisable(unsigned long ulBase, unsigned long ulSequenceNum)
1257 {
1258 //
1259 // Check the arguments.
1260 //
1261 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
1262 ASSERT(ulSequenceNum < 4);
1263
1264 //
1265 // Disable this sample sequence comparator interrupt.
1266 //
1267 HWREG(ulBase + ADC_O_IM) &= ~(0x10000 << ulSequenceNum);
1268 }
1269
1270 //*****************************************************************************
1271 //
1272 //! Enables a sample sequence comparator interrupt.
1273 //!
1274 //! \param ulBase is the base address of the ADC module.
1275 //! \param ulSequenceNum is the sample sequence number.
1276 //!
1277 //! This function enables the requested sample sequence comparator interrupt.
1278 //!
1279 //! \return None.
1280 //
1281 //*****************************************************************************
1282 void
ADCComparatorIntEnable(unsigned long ulBase,unsigned long ulSequenceNum)1283 ADCComparatorIntEnable(unsigned long ulBase, unsigned long ulSequenceNum)
1284 {
1285 //
1286 // Check the arguments.
1287 //
1288 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
1289 ASSERT(ulSequenceNum < 4);
1290
1291 //
1292 // Enable this sample sequence interrupt.
1293 //
1294 HWREG(ulBase + ADC_O_IM) |= 0x10000 << ulSequenceNum;
1295 }
1296
1297 //*****************************************************************************
1298 //
1299 //! Gets the current comparator interrupt status.
1300 //!
1301 //! \param ulBase is the base address of the ADC module.
1302 //!
1303 //! This function returns the digital comparator interrupt status bits. This
1304 //! status is sequence agnostic.
1305 //!
1306 //! \return The current comparator interrupt status.
1307 //
1308 //*****************************************************************************
1309 unsigned long
ADCComparatorIntStatus(unsigned long ulBase)1310 ADCComparatorIntStatus(unsigned long ulBase)
1311 {
1312 //
1313 // Check the arguments.
1314 //
1315 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
1316
1317 //
1318 // Return the digital comparator interrupt status.
1319 //
1320 return(HWREG(ulBase + ADC_O_DCISC));
1321 }
1322
1323 //*****************************************************************************
1324 //
1325 //! Clears sample sequence comparator interrupt source.
1326 //!
1327 //! \param ulBase is the base address of the ADC module.
1328 //! \param ulStatus is the bit-mapped interrupts status to clear.
1329 //!
1330 //! The specified interrupt status is cleared.
1331 //!
1332 //! \return None.
1333 //
1334 //*****************************************************************************
1335 void
ADCComparatorIntClear(unsigned long ulBase,unsigned long ulStatus)1336 ADCComparatorIntClear(unsigned long ulBase, unsigned long ulStatus)
1337 {
1338 //
1339 // Check the arguments.
1340 //
1341 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
1342
1343 //
1344 // Clear the interrupt.
1345 //
1346 HWREG(ulBase + ADC_O_DCISC) = ulStatus;
1347 }
1348
1349 //*****************************************************************************
1350 //
1351 //! Selects the ADC reference.
1352 //!
1353 //! \param ulBase is the base address of the ADC module.
1354 //! \param ulRef is the reference to use.
1355 //!
1356 //! The ADC reference is set as specified by \e ulRef. It must be one of
1357 //! \b ADC_REF_INT, \b ADC_REF_EXT_3V, or \b ADC_REF_EXT_1V for internal or
1358 //! external reference. If \b ADC_REF_INT is chosen, then an internal 3V
1359 //! reference is used and no external reference is needed. If
1360 //! \b ADC_REF_EXT_3V is chosen, then a 3V reference must be supplied to the
1361 //! AVREF pin. If \b ADC_REF_EXT_1V is chosen, then a 1V external reference
1362 //! must be supplied to the AVREF pin.
1363 //!
1364 //! \note The ADC reference can only be selected on parts that have an external
1365 //! reference. Consult the data sheet for your part to determine if there is
1366 //! an external reference.
1367 //!
1368 //! \return None.
1369 //
1370 //*****************************************************************************
1371 void
ADCReferenceSet(unsigned long ulBase,unsigned long ulRef)1372 ADCReferenceSet(unsigned long ulBase, unsigned long ulRef)
1373 {
1374 //
1375 // Check the arguments.
1376 //
1377 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
1378 ASSERT((ulRef == ADC_REF_INT) || (ulRef == ADC_REF_EXT_3V) ||
1379 (ulRef == ADC_REF_EXT_1V));
1380
1381 //
1382 // Set the reference.
1383 //
1384 HWREG(ulBase + ADC_O_CTL) = (HWREG(ulBase + ADC_O_CTL) & ~ADC_CTL_VREF_M) |
1385 ulRef;
1386 }
1387
1388 //*****************************************************************************
1389 //
1390 //! Returns the current setting of the ADC reference.
1391 //!
1392 //! \param ulBase is the base address of the ADC module.
1393 //!
1394 //! Returns the value of the ADC reference setting. The returned value is one
1395 //! of \b ADC_REF_INT, \b ADC_REF_EXT_3V, or \b ADC_REF_EXT_1V.
1396 //!
1397 //! \note The value returned by this function is only meaningful if used on a
1398 //! part that is capable of using an external reference. Consult the data
1399 //! sheet for your part to determine if it has an external reference input.
1400 //!
1401 //! \return The current setting of the ADC reference.
1402 //
1403 //*****************************************************************************
1404 unsigned long
ADCReferenceGet(unsigned long ulBase)1405 ADCReferenceGet(unsigned long ulBase)
1406 {
1407 //
1408 // Check the arguments.
1409 //
1410 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
1411
1412 //
1413 // Return the value of the reference.
1414 //
1415 return(HWREG(ulBase + ADC_O_CTL) & ADC_CTL_VREF_M);
1416 }
1417
1418 //*****************************************************************************
1419 //
1420 //! Selects the ADC resolution.
1421 //!
1422 //! \param ulBase is the base address of the ADC module.
1423 //! \param ulResolution is the ADC bit resolution.
1424 //!
1425 //! The ADC resolution is set as specified by \e ulResolution. It must be one
1426 //! of \b ADC_RES_12BIT or \b ADC_RES_10BIT.
1427 //!
1428 //! \note The ADC resolution can only be set on parts that are capable of
1429 //! changing ADC resolution mode. Consult the data sheet for your part to
1430 //! determine if it is capable of operating in more than one resolution mode.
1431 //!
1432 //! \return None.
1433 //
1434 //*****************************************************************************
1435 void
ADCResolutionSet(unsigned long ulBase,unsigned long ulResolution)1436 ADCResolutionSet(unsigned long ulBase, unsigned long ulResolution)
1437 {
1438 //
1439 // Check the arguments.
1440 //
1441 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
1442 ASSERT((ulResolution == ADC_RES_10BIT) || (ulResolution == ADC_RES_12BIT));
1443
1444 //
1445 // Set the resolution.
1446 //
1447 HWREG(ulBase + ADC_O_CTL) = (HWREG(ulBase + ADC_O_CTL) & ~ADC_CTL_RES) |
1448 ulResolution;
1449 }
1450
1451 //*****************************************************************************
1452 //
1453 //! Gets the setting of ADC resolution.
1454 //!
1455 //! \param ulBase is the base address of the ADC module.
1456 //!
1457 //! The ADC resolution is returned as one of \b ADC_RES_12BIT or
1458 //! \b ADC_RES_10BIT.
1459 //!
1460 //! \note The value returned by this function is only meaningful if used on a
1461 //! part that is capable of changing ADC resolution mode. Consult the
1462 //! data sheet for your part to determine if it is capable of operating in
1463 //! more than one resolution mode.
1464 //!
1465 //! \return The current setting of the ADC resolution.
1466 //
1467 //*****************************************************************************
1468 unsigned long
ADCResolutionGet(unsigned long ulBase)1469 ADCResolutionGet(unsigned long ulBase)
1470 {
1471 //
1472 // Check the arguments.
1473 //
1474 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
1475
1476 //
1477 // Get the resolution and return it to the caller.
1478 //
1479 return(HWREG(ulBase + ADC_O_CTL) & ADC_CTL_RES);
1480 }
1481
1482 //*****************************************************************************
1483 //
1484 //! Sets the phase delay between a trigger and the start of a sequence.
1485 //!
1486 //! \param ulBase is the base address of the ADC module.
1487 //! \param ulPhase is the phase delay, specified as one of \b ADC_PHASE_0,
1488 //! \b ADC_PHASE_22_5, \b ADC_PHASE_45, \b ADC_PHASE_67_5, \b ADC_PHASE_90,
1489 //! \b ADC_PHASE_112_5, \b ADC_PHASE_135, \b ADC_PHASE_157_5, \b ADC_PHASE_180,
1490 //! \b ADC_PHASE_202_5, \b ADC_PHASE_225, \b ADC_PHASE_247_5, \b ADC_PHASE_270,
1491 //! \b ADC_PHASE_292_5, \b ADC_PHASE_315, or \b ADC_PHASE_337_5.
1492 //!
1493 //! This function sets the phase delay between the detection of an ADC trigger
1494 //! event and the start of the sample sequence. By selecting a different phase
1495 //! delay for a pair of ADC modules (such as \b ADC_PHASE_0 and
1496 //! \b ADC_PHASE_180) and having each ADC module sample the same analog input,
1497 //! it is possible to increase the sampling rate of the analog input (with
1498 //! samples N, N+2, N+4, and so on, coming from the first ADC and samples N+1,
1499 //! N+3, N+5, and so on, coming from the second ADC). The ADC module has a
1500 //! single phase delay that is applied to all sample sequences within that
1501 //! module.
1502 //!
1503 //! \note This capability is not available on all parts.
1504 //!
1505 //! \return None.
1506 //
1507 //*****************************************************************************
1508 void
ADCPhaseDelaySet(unsigned long ulBase,unsigned long ulPhase)1509 ADCPhaseDelaySet(unsigned long ulBase, unsigned long ulPhase)
1510 {
1511 //
1512 // Check the arguments.
1513 //
1514 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
1515 ASSERT((ulPhase == ADC_PHASE_0) || (ulPhase == ADC_PHASE_22_5) ||
1516 (ulPhase == ADC_PHASE_45) || (ulPhase == ADC_PHASE_67_5) ||
1517 (ulPhase == ADC_PHASE_90) || (ulPhase == ADC_PHASE_112_5) ||
1518 (ulPhase == ADC_PHASE_135) || (ulPhase == ADC_PHASE_157_5) ||
1519 (ulPhase == ADC_PHASE_180) || (ulPhase == ADC_PHASE_202_5) ||
1520 (ulPhase == ADC_PHASE_225) || (ulPhase == ADC_PHASE_247_5) ||
1521 (ulPhase == ADC_PHASE_270) || (ulPhase == ADC_PHASE_292_5) ||
1522 (ulPhase == ADC_PHASE_315) || (ulPhase == ADC_PHASE_337_5));
1523
1524 //
1525 // Set the phase delay.
1526 //
1527 HWREG(ulBase + ADC_O_SPC) = ulPhase;
1528 }
1529
1530 //*****************************************************************************
1531 //
1532 //! Gets the phase delay between a trigger and the start of a sequence.
1533 //!
1534 //! \param ulBase is the base address of the ADC module.
1535 //!
1536 //! This function gets the current phase delay between the detection of an ADC
1537 //! trigger event and the start of the sample sequence.
1538 //!
1539 //! \return Returns the phase delay, specified as one of \b ADC_PHASE_0,
1540 //! \b ADC_PHASE_22_5, \b ADC_PHASE_45, \b ADC_PHASE_67_5, \b ADC_PHASE_90,
1541 //! \b ADC_PHASE_112_5, \b ADC_PHASE_135, \b ADC_PHASE_157_5, \b ADC_PHASE_180,
1542 //! \b ADC_PHASE_202_5, \b ADC_PHASE_225, \b ADC_PHASE_247_5, \b ADC_PHASE_270,
1543 //! \b ADC_PHASE_292_5, \b ADC_PHASE_315, or \b ADC_PHASE_337_5.
1544 //
1545 //*****************************************************************************
1546 unsigned long
ADCPhaseDelayGet(unsigned long ulBase)1547 ADCPhaseDelayGet(unsigned long ulBase)
1548 {
1549 //
1550 // Check the arguments.
1551 //
1552 ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
1553
1554 //
1555 // Return the phase delay.
1556 //
1557 return(HWREG(ulBase + ADC_O_SPC));
1558 }
1559
1560 //*****************************************************************************
1561 //
1562 // Close the Doxygen group.
1563 //! @}
1564 //
1565 //*****************************************************************************
1566