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