1 //*****************************************************************************
\r
3 // comp.c - Driver for the analog comparator.
\r
5 // Copyright (c) 2005,2006 Luminary Micro, Inc. All rights reserved.
\r
7 // Software License Agreement
\r
9 // Luminary Micro, Inc. (LMI) is supplying this software for use solely and
\r
10 // exclusively on LMI's Stellaris Family of microcontroller products.
\r
12 // The software is owned by LMI and/or its suppliers, and is protected under
\r
13 // applicable copyright laws. All rights are reserved. Any use in violation
\r
14 // of the foregoing restrictions may subject the user to criminal sanctions
\r
15 // under applicable laws, as well as to civil liability for the breach of the
\r
16 // terms and conditions of this license.
\r
18 // THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
\r
19 // OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
\r
20 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
\r
21 // LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
\r
22 // CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
\r
24 // This is part of revision 991 of the Stellaris Driver Library.
\r
26 //*****************************************************************************
\r
28 //*****************************************************************************
\r
30 //! \addtogroup comp_api
\r
33 //*****************************************************************************
\r
35 #include "../hw_comp.h"
\r
36 #include "../hw_ints.h"
\r
37 #include "../hw_memmap.h"
\r
38 #include "../hw_types.h"
\r
41 #include "interrupt.h"
\r
43 //*****************************************************************************
\r
45 //! Configures a comparator.
\r
47 //! \param ulBase is the base address of the comparator module.
\r
48 //! \param ulComp is the index of the comparator to configure.
\r
49 //! \param ulConfig is the configuration of the comparator.
\r
51 //! This function will configure a comparator. The \e ulConfig parameter is
\r
52 //! the result of a logical OR operation between the \b COMP_TRIG_xxx,
\r
53 //! \b COMP_INT_xxx, \b COMP_ASRCP_xxx, and \b COMP_OUTPUT_xxx values.
\r
55 //! The \b COMP_TRIG_xxx term can take on the following values:
\r
57 //! - \b COMP_TRIG_NONE to have no trigger to the ADC.
\r
58 //! - \b COMP_TRIG_HIGH to trigger the ADC when the comparator output is high.
\r
59 //! - \b COMP_TRIG_LOW to trigger the ADC when the comparator output is low.
\r
60 //! - \b COMP_TRIG_FALL to trigger the ADC when the comparator output goes low.
\r
61 //! - \b COMP_TRIG_RISE to trigger the ADC when the comparator output goes
\r
63 //! - \b COMP_TRIG_BOTH to trigger the ADC when the comparator output goes low
\r
66 //! The \b COMP_INT_xxx term can take on the following values:
\r
68 //! - \b COMP_INT_HIGH to generate an interrupt when the comparator output is
\r
70 //! - \b COMP_INT_LOW to generate an interrupt when the comparator output is
\r
72 //! - \b COMP_INT_FALL to generate an interrupt when the comparator output goes
\r
74 //! - \b COMP_INT_RISE to generate an interrupt when the comparator output goes
\r
76 //! - \b COMP_INT_BOTH to generate an interrupt when the comparator output goes
\r
79 //! The \b COMP_ASRCP_xxx term can take on the following values:
\r
81 //! - \b COMP_ASRCP_PIN to use the dedicated Comp+ pin as the reference
\r
83 //! - \b COMP_ASRCP_PIN0 to use the Comp0+ pin as the reference voltage (this
\r
84 //! the same as \b COMP_ASRCP_PIN for the comparator 0).
\r
85 //! - \b COMP_ASRCP_REF to use the internally generated voltage as the
\r
86 //! reference voltage.
\r
88 //! The \b COMP_OUTPUT_xxx term can take on the following values:
\r
90 //! - \b COMP_OUTPUT_NONE to disable the output from the comparator to a device
\r
92 //! - \b COMP_OUTPUT_NORMAL to enable a non-inverted output from the comparator
\r
93 //! to a device pin.
\r
94 //! - \b COMP_OUTPUT_INVERT to enable an inverted output from the comparator to
\r
99 //*****************************************************************************
\r
100 #if defined(GROUP_configure) || defined(BUILD_ALL) || defined(DOXYGEN)
\r
102 ComparatorConfigure(unsigned long ulBase, unsigned long ulComp,
\r
103 unsigned long ulConfig)
\r
106 // Check the arguments.
\r
108 ASSERT(ulBase == COMP_BASE);
\r
109 ASSERT(ulComp < 3);
\r
112 // Configure this comparator.
\r
114 HWREG(ulBase + (ulComp * 0x20) + COMP_O_ACCTL0) = ulConfig;
\r
118 //*****************************************************************************
\r
120 //! Sets the internal reference voltage.
\r
122 //! \param ulBase is the base address of the comparator module.
\r
123 //! \param ulRef is the desired reference voltage.
\r
125 //! This function will set the internal reference voltage value. The voltage
\r
126 //! is specified as one of the following values:
\r
128 //! - \b COMP_REF_OFF to turn off the reference voltage
\r
129 //! - \b COMP_REF_0V to set the reference voltage to 0 V
\r
130 //! - \b COMP_REF_0_1375V to set the reference voltage to 0.1375 V
\r
131 //! - \b COMP_REF_0_275V to set the reference voltage to 0.275 V
\r
132 //! - \b COMP_REF_0_4125V to set the reference voltage to 0.4125 V
\r
133 //! - \b COMP_REF_0_55V to set the reference voltage to 0.55 V
\r
134 //! - \b COMP_REF_0_6875V to set the reference voltage to 0.6875 V
\r
135 //! - \b COMP_REF_0_825V to set the reference voltage to 0.825 V
\r
136 //! - \b COMP_REF_0_928125V to set the reference voltage to 0.928125 V
\r
137 //! - \b COMP_REF_0_9625V to set the reference voltage to 0.9625 V
\r
138 //! - \b COMP_REF_1_03125V to set the reference voltage to 1.03125 V
\r
139 //! - \b COMP_REF_1_134375V to set the reference voltage to 1.134375 V
\r
140 //! - \b COMP_REF_1_1V to set the reference voltage to 1.1 V
\r
141 //! - \b COMP_REF_1_2375V to set the reference voltage to 1.2375 V
\r
142 //! - \b COMP_REF_1_340625V to set the reference voltage to 1.340625 V
\r
143 //! - \b COMP_REF_1_375V to set the reference voltage to 1.375 V
\r
144 //! - \b COMP_REF_1_44375V to set the reference voltage to 1.44375 V
\r
145 //! - \b COMP_REF_1_5125V to set the reference voltage to 1.5125 V
\r
146 //! - \b COMP_REF_1_546875V to set the reference voltage to 1.546875 V
\r
147 //! - \b COMP_REF_1_65V to set the reference voltage to 1.65 V
\r
148 //! - \b COMP_REF_1_753125V to set the reference voltage to 1.753125 V
\r
149 //! - \b COMP_REF_1_7875V to set the reference voltage to 1.7875 V
\r
150 //! - \b COMP_REF_1_85625V to set the reference voltage to 1.85625 V
\r
151 //! - \b COMP_REF_1_925V to set the reference voltage to 1.925 V
\r
152 //! - \b COMP_REF_1_959375V to set the reference voltage to 1.959375 V
\r
153 //! - \b COMP_REF_2_0625V to set the reference voltage to 2.0625 V
\r
154 //! - \b COMP_REF_2_165625V to set the reference voltage to 2.165625 V
\r
155 //! - \b COMP_REF_2_26875V to set the reference voltage to 2.26875 V
\r
156 //! - \b COMP_REF_2_371875V to set the reference voltage to 2.371875 V
\r
160 //*****************************************************************************
\r
161 #if defined(GROUP_refset) || defined(BUILD_ALL) || defined(DOXYGEN)
\r
163 ComparatorRefSet(unsigned long ulBase, unsigned long ulRef)
\r
166 // Check the arguments.
\r
168 ASSERT(ulBase == COMP_BASE);
\r
171 // Set the voltage reference voltage as requested.
\r
173 HWREG(ulBase + COMP_O_REFCTL) = ulRef;
\r
177 //*****************************************************************************
\r
179 //! Gets the current comparator output value.
\r
181 //! \param ulBase is the base address of the comparator module.
\r
182 //! \param ulComp is the index of the comparator.
\r
184 //! This function retrieves the current value of the comparator output.
\r
186 //! \return Returns \b true if the comparator output is high and \b false if
\r
187 //! the comparator output is low.
\r
189 //*****************************************************************************
\r
190 #if defined(GROUP_valueget) || defined(BUILD_ALL) || defined(DOXYGEN)
\r
192 ComparatorValueGet(unsigned long ulBase, unsigned long ulComp)
\r
195 // Check the arguments.
\r
197 ASSERT(ulBase == COMP_BASE);
\r
198 ASSERT(ulComp < 3);
\r
201 // Return the appropriate value based on the comparator's present output
\r
204 if(HWREG(ulBase + (ulComp * 0x20) + COMP_O_ACSTAT0) & COMP_ACSTAT_OVAL)
\r
215 //*****************************************************************************
\r
217 //! Registers an interrupt handler for the comparator interrupt.
\r
219 //! \param ulBase is the base address of the comparator module.
\r
220 //! \param ulComp is the index of the comparator.
\r
221 //! \param pfnHandler is a pointer to the function to be called when the
\r
222 //! comparator interrupt occurs.
\r
224 //! This sets the handler to be called when the comparator interrupt occurs.
\r
225 //! This will enable the interrupt in the interrupt controller; it is the
\r
226 //! interrupt-handler's responsibility to clear the interrupt source via
\r
227 //! ComparatorIntClear().
\r
229 //! \sa IntRegister() for important information about registering interrupt
\r
234 //*****************************************************************************
\r
235 #if defined(GROUP_intregister) || defined(BUILD_ALL) || defined(DOXYGEN)
\r
237 ComparatorIntRegister(unsigned long ulBase, unsigned long ulComp,
\r
238 void (*pfnHandler)(void))
\r
241 // Check the arguments.
\r
243 ASSERT(ulBase == COMP_BASE);
\r
244 ASSERT(ulComp < 3);
\r
247 // Register the interrupt handler, returning an error if an error occurs.
\r
249 IntRegister(INT_COMP0 + ulComp, pfnHandler);
\r
252 // Enable the interrupt in the interrupt controller.
\r
254 IntEnable(INT_COMP0 + ulComp);
\r
257 // Enable the comparator interrupt.
\r
259 HWREG(ulBase + COMP_O_INTEN) |= 1 << ulComp;
\r
263 //*****************************************************************************
\r
265 //! Unregisters an interrupt handler for a comparator interrupt.
\r
267 //! \param ulBase is the base address of the comparator module.
\r
268 //! \param ulComp is the index of the comparator.
\r
270 //! This function will clear the handler to be called when a comparator
\r
271 //! interrupt occurs. This will also mask off the interrupt in the interrupt
\r
272 //! controller so that the interrupt handler no longer is called.
\r
274 //! \sa IntRegister() for important information about registering interrupt
\r
279 //*****************************************************************************
\r
280 #if defined(GROUP_intunregister) || defined(BUILD_ALL) || defined(DOXYGEN)
\r
282 ComparatorIntUnregister(unsigned long ulBase, unsigned long ulComp)
\r
285 // Check the arguments.
\r
287 ASSERT(ulBase == COMP_BASE);
\r
288 ASSERT(ulComp < 3);
\r
291 // Disable the comparator interrupt.
\r
293 HWREG(ulBase + COMP_O_INTEN) &= ~(1 << ulComp);
\r
296 // Disable the interrupt in the interrupt controller.
\r
298 IntDisable(INT_COMP0 + ulComp);
\r
301 // Unregister the interrupt handler.
\r
303 IntUnregister(INT_COMP0 + ulComp);
\r
307 //*****************************************************************************
\r
309 //! Enables the comparator interrupt.
\r
311 //! \param ulBase is the base address of the comparator module.
\r
312 //! \param ulComp is the index of the comparator.
\r
314 //! This function enables generation of an interrupt from the specified
\r
315 //! comparator. Only comparators whose interrupts are enabled can be reflected
\r
316 //! to the processor.
\r
320 //*****************************************************************************
\r
321 #if defined(GROUP_intenable) || defined(BUILD_ALL) || defined(DOXYGEN)
\r
323 ComparatorIntEnable(unsigned long ulBase, unsigned long ulComp)
\r
326 // Check the arguments.
\r
328 ASSERT(ulBase == COMP_BASE);
\r
329 ASSERT(ulComp < 3);
\r
332 // Enable the comparator interrupt.
\r
334 HWREG(ulBase + COMP_O_INTEN) |= 1 << ulComp;
\r
338 //*****************************************************************************
\r
340 //! Disables the comparator interrupt.
\r
342 //! \param ulBase is the base address of the comparator module.
\r
343 //! \param ulComp is the index of the comparator.
\r
345 //! This function disables generation of an interrupt from the specified
\r
346 //! comparator. Only comparators whose interrupts are enabled can be reflected
\r
347 //! to the processor.
\r
351 //*****************************************************************************
\r
352 #if defined(GROUP_intdisable) || defined(BUILD_ALL) || defined(DOXYGEN)
\r
354 ComparatorIntDisable(unsigned long ulBase, unsigned long ulComp)
\r
357 // Check the arguments.
\r
359 ASSERT(ulBase == COMP_BASE);
\r
360 ASSERT(ulComp < 3);
\r
363 // Disable the comparator interrupt.
\r
365 HWREG(ulBase + COMP_O_INTEN) &= ~(1 << ulComp);
\r
369 //*****************************************************************************
\r
371 //! Gets the current interrupt status.
\r
373 //! \param ulBase is the base address of the comparator module.
\r
374 //! \param ulComp is the index of the comparator.
\r
375 //! \param bMasked is \b false if the raw interrupt status is required and
\r
376 //! \b true if the masked interrupt status is required.
\r
378 //! This returns the interrupt status for the comparator. Either the raw or
\r
379 //! the masked interrupt status can be returned.
\r
381 //! \return \b true if the interrupt is asserted and \b false if it is not
\r
384 //*****************************************************************************
\r
385 #if defined(GROUP_intstatus) || defined(BUILD_ALL) || defined(DOXYGEN)
\r
387 ComparatorIntStatus(unsigned long ulBase, unsigned long ulComp,
\r
391 // Check the arguments.
\r
393 ASSERT(ulBase == COMP_BASE);
\r
394 ASSERT(ulComp < 3);
\r
397 // Return either the interrupt status or the raw interrupt status as
\r
402 return(((HWREG(ulBase + COMP_O_MIS) >> ulComp) & 1) ? true : false);
\r
406 return(((HWREG(ulBase + COMP_O_RIS) >> ulComp) & 1) ? true : false);
\r
411 //*****************************************************************************
\r
413 //! Clears a comparator interrupt.
\r
415 //! \param ulBase is the base address of the comparator module.
\r
416 //! \param ulComp is the index of the comparator.
\r
418 //! The comparator interrupt is cleared, so that it no longer asserts. This
\r
419 //! must be done in the interrupt handler to keep it from being called again
\r
420 //! immediately upon exit. Note that for a level triggered interrupt, the
\r
421 //! interrupt cannot be cleared until it stops asserting.
\r
425 //*****************************************************************************
\r
426 #if defined(GROUP_intclear) || defined(BUILD_ALL) || defined(DOXYGEN)
\r
428 ComparatorIntClear(unsigned long ulBase, unsigned long ulComp)
\r
431 // Check the arguments.
\r
433 ASSERT(ulBase == COMP_BASE);
\r
434 ASSERT(ulComp < 3);
\r
437 // Clear the interrupt.
\r
439 HWREG(ulBase + COMP_O_MIS) = 1 << ulComp;
\r
443 //*****************************************************************************
\r
445 // Close the Doxygen group.
\r
448 //*****************************************************************************
\r