1 //*****************************************************************************
2 //
3 // comp.c - Driver for the analog comparator.
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 comp_api
43 //! @{
44 //
45 //*****************************************************************************
46 
47 #include "inc/hw_comp.h"
48 #include "inc/hw_ints.h"
49 #include "inc/hw_memmap.h"
50 #include "inc/hw_types.h"
51 #include "driverlib/comp.h"
52 #include "driverlib/debug.h"
53 #include "driverlib/interrupt.h"
54 
55 //*****************************************************************************
56 //
57 //! Configures a comparator.
58 //!
59 //! \param ulBase is the base address of the comparator module.
60 //! \param ulComp is the index of the comparator to configure.
61 //! \param ulConfig is the configuration of the comparator.
62 //!
63 //! This function configures a comparator.  The \e ulConfig parameter is the
64 //! result of a logical OR operation between the \b COMP_TRIG_xxx,
65 //! \b COMP_INT_xxx, \b COMP_ASRCP_xxx, and \b COMP_OUTPUT_xxx values.
66 //!
67 //! The \b COMP_TRIG_xxx term can take on the following values:
68 //!
69 //! - \b COMP_TRIG_NONE to have no trigger to the ADC.
70 //! - \b COMP_TRIG_HIGH to trigger the ADC when the comparator output is high.
71 //! - \b COMP_TRIG_LOW to trigger the ADC when the comparator output is low.
72 //! - \b COMP_TRIG_FALL to trigger the ADC when the comparator output goes low.
73 //! - \b COMP_TRIG_RISE to trigger the ADC when the comparator output goes
74 //! high.
75 //! - \b COMP_TRIG_BOTH to trigger the ADC when the comparator output goes low
76 //! or high.
77 //!
78 //! The \b COMP_INT_xxx term can take on the following values:
79 //!
80 //! - \b COMP_INT_HIGH to generate an interrupt when the comparator output is
81 //! high.
82 //! - \b COMP_INT_LOW to generate an interrupt when the comparator output is
83 //! low.
84 //! - \b COMP_INT_FALL to generate an interrupt when the comparator output goes
85 //! low.
86 //! - \b COMP_INT_RISE to generate an interrupt when the comparator output goes
87 //! high.
88 //! - \b COMP_INT_BOTH to generate an interrupt when the comparator output goes
89 //! low or high.
90 //!
91 //! The \b COMP_ASRCP_xxx term can take on the following values:
92 //!
93 //! - \b COMP_ASRCP_PIN to use the dedicated Comp+ pin as the reference
94 //! voltage.
95 //! - \b COMP_ASRCP_PIN0 to use the Comp0+ pin as the reference voltage (this
96 //! the same as \b COMP_ASRCP_PIN for the comparator 0).
97 //! - \b COMP_ASRCP_REF to use the internally generated voltage as the
98 //! reference voltage.
99 //!
100 //! The \b COMP_OUTPUT_xxx term can take on the following values:
101 //!
102 //! - \b COMP_OUTPUT_NORMAL to enable a non-inverted output from the comparator
103 //! to a device pin.
104 //! - \b COMP_OUTPUT_INVERT to enable an inverted output from the comparator to
105 //! a device pin.
106 //! - \b COMP_OUTPUT_NONE is deprecated and behaves the same as
107 //! \b COMP_OUTPUT_NORMAL.
108 //!
109 //! \return None.
110 //
111 //*****************************************************************************
112 void
ComparatorConfigure(unsigned long ulBase,unsigned long ulComp,unsigned long ulConfig)113 ComparatorConfigure(unsigned long ulBase, unsigned long ulComp,
114                     unsigned long ulConfig)
115 {
116     //
117     // Check the arguments.
118     //
119     ASSERT(ulBase == COMP_BASE);
120     ASSERT(ulComp < 3);
121 
122     //
123     // Configure this comparator.
124     //
125     HWREG(ulBase + (ulComp * 0x20) + COMP_O_ACCTL0) = ulConfig;
126 }
127 
128 //*****************************************************************************
129 //
130 //! Sets the internal reference voltage.
131 //!
132 //! \param ulBase is the base address of the comparator module.
133 //! \param ulRef is the desired reference voltage.
134 //!
135 //! This function sets the internal reference voltage value.  The voltage is
136 //! specified as one of the following values:
137 //!
138 //! - \b COMP_REF_OFF to turn off the reference voltage
139 //! - \b COMP_REF_0V to set the reference voltage to 0 V
140 //! - \b COMP_REF_0_1375V to set the reference voltage to 0.1375 V
141 //! - \b COMP_REF_0_275V to set the reference voltage to 0.275 V
142 //! - \b COMP_REF_0_4125V to set the reference voltage to 0.4125 V
143 //! - \b COMP_REF_0_55V to set the reference voltage to 0.55 V
144 //! - \b COMP_REF_0_6875V to set the reference voltage to 0.6875 V
145 //! - \b COMP_REF_0_825V to set the reference voltage to 0.825 V
146 //! - \b COMP_REF_0_928125V to set the reference voltage to 0.928125 V
147 //! - \b COMP_REF_0_9625V to set the reference voltage to 0.9625 V
148 //! - \b COMP_REF_1_03125V to set the reference voltage to 1.03125 V
149 //! - \b COMP_REF_1_134375V to set the reference voltage to 1.134375 V
150 //! - \b COMP_REF_1_1V to set the reference voltage to 1.1 V
151 //! - \b COMP_REF_1_2375V to set the reference voltage to 1.2375 V
152 //! - \b COMP_REF_1_340625V to set the reference voltage to 1.340625 V
153 //! - \b COMP_REF_1_375V to set the reference voltage to 1.375 V
154 //! - \b COMP_REF_1_44375V to set the reference voltage to 1.44375 V
155 //! - \b COMP_REF_1_5125V to set the reference voltage to 1.5125 V
156 //! - \b COMP_REF_1_546875V to set the reference voltage to 1.546875 V
157 //! - \b COMP_REF_1_65V to set the reference voltage to 1.65 V
158 //! - \b COMP_REF_1_753125V to set the reference voltage to 1.753125 V
159 //! - \b COMP_REF_1_7875V to set the reference voltage to 1.7875 V
160 //! - \b COMP_REF_1_85625V to set the reference voltage to 1.85625 V
161 //! - \b COMP_REF_1_925V to set the reference voltage to 1.925 V
162 //! - \b COMP_REF_1_959375V to set the reference voltage to 1.959375 V
163 //! - \b COMP_REF_2_0625V to set the reference voltage to 2.0625 V
164 //! - \b COMP_REF_2_165625V to set the reference voltage to 2.165625 V
165 //! - \b COMP_REF_2_26875V to set the reference voltage to 2.26875 V
166 //! - \b COMP_REF_2_371875V to set the reference voltage to 2.371875 V
167 //!
168 //! \return None.
169 //
170 //*****************************************************************************
171 void
ComparatorRefSet(unsigned long ulBase,unsigned long ulRef)172 ComparatorRefSet(unsigned long ulBase, unsigned long ulRef)
173 {
174     //
175     // Check the arguments.
176     //
177     ASSERT(ulBase == COMP_BASE);
178 
179     //
180     // Set the voltage reference voltage as requested.
181     //
182     HWREG(ulBase + COMP_O_ACREFCTL) = ulRef;
183 }
184 
185 //*****************************************************************************
186 //
187 //! Gets the current comparator output value.
188 //!
189 //! \param ulBase is the base address of the comparator module.
190 //! \param ulComp is the index of the comparator.
191 //!
192 //! This function retrieves the current value of the comparator output.
193 //!
194 //! \return Returns \b true if the comparator output is high and \b false if
195 //! the comparator output is low.
196 //
197 //*****************************************************************************
198 tBoolean
ComparatorValueGet(unsigned long ulBase,unsigned long ulComp)199 ComparatorValueGet(unsigned long ulBase, unsigned long ulComp)
200 {
201     //
202     // Check the arguments.
203     //
204     ASSERT(ulBase == COMP_BASE);
205     ASSERT(ulComp < 3);
206 
207     //
208     // Return the appropriate value based on the comparator's present output
209     // value.
210     //
211     if(HWREG(ulBase + (ulComp * 0x20) + COMP_O_ACSTAT0) & COMP_ACSTAT0_OVAL)
212     {
213         return(true);
214     }
215     else
216     {
217         return(false);
218     }
219 }
220 
221 //*****************************************************************************
222 //
223 //! Registers an interrupt handler for the comparator interrupt.
224 //!
225 //! \param ulBase is the base address of the comparator module.
226 //! \param ulComp is the index of the comparator.
227 //! \param pfnHandler is a pointer to the function to be called when the
228 //! comparator interrupt occurs.
229 //!
230 //! This function sets the handler to be called when the comparator interrupt occurs
231 //! and enables the interrupt in the interrupt controller.  It is the interrupt
232 //! handler's responsibility to clear the interrupt source via
233 //! ComparatorIntClear().
234 //!
235 //! \sa IntRegister() for important information about registering interrupt
236 //! handlers.
237 //!
238 //! \return None.
239 //
240 //*****************************************************************************
241 void
ComparatorIntRegister(unsigned long ulBase,unsigned long ulComp,void (* pfnHandler)(void))242 ComparatorIntRegister(unsigned long ulBase, unsigned long ulComp,
243                       void (*pfnHandler)(void))
244 {
245     //
246     // Check the arguments.
247     //
248     ASSERT(ulBase == COMP_BASE);
249     ASSERT(ulComp < 3);
250 
251     //
252     // Register the interrupt handler, returning an error if an error occurs.
253     //
254     IntRegister(INT_COMP0 + ulComp, pfnHandler);
255 
256     //
257     // Enable the interrupt in the interrupt controller.
258     //
259     IntEnable(INT_COMP0 + ulComp);
260 
261     //
262     // Enable the comparator interrupt.
263     //
264     HWREG(ulBase + COMP_O_ACINTEN) |= 1 << ulComp;
265 }
266 
267 //*****************************************************************************
268 //
269 //! Unregisters an interrupt handler for a comparator interrupt.
270 //!
271 //! \param ulBase is the base address of the comparator module.
272 //! \param ulComp is the index of the comparator.
273 //!
274 //! This function clears the handler to be called when a comparator interrupt
275 //! occurs.  This function also masks off the interrupt in the interrupt controller
276 //! so that the interrupt handler no longer is called.
277 //!
278 //! \sa IntRegister() for important information about registering interrupt
279 //! handlers.
280 //!
281 //! \return None.
282 //
283 //*****************************************************************************
284 void
ComparatorIntUnregister(unsigned long ulBase,unsigned long ulComp)285 ComparatorIntUnregister(unsigned long ulBase, unsigned long ulComp)
286 {
287     //
288     // Check the arguments.
289     //
290     ASSERT(ulBase == COMP_BASE);
291     ASSERT(ulComp < 3);
292 
293     //
294     // Disable the comparator interrupt.
295     //
296     HWREG(ulBase + COMP_O_ACINTEN) &= ~(1 << ulComp);
297 
298     //
299     // Disable the interrupt in the interrupt controller.
300     //
301     IntDisable(INT_COMP0 + ulComp);
302 
303     //
304     // Unregister the interrupt handler.
305     //
306     IntUnregister(INT_COMP0 + ulComp);
307 }
308 
309 //*****************************************************************************
310 //
311 //! Enables the comparator interrupt.
312 //!
313 //! \param ulBase is the base address of the comparator module.
314 //! \param ulComp is the index of the comparator.
315 //!
316 //! This function enables generation of an interrupt from the specified
317 //! comparator.  Only enabled comparator interrupts can be reflected
318 //! to the processor.
319 //!
320 //! \return None.
321 //
322 //*****************************************************************************
323 void
ComparatorIntEnable(unsigned long ulBase,unsigned long ulComp)324 ComparatorIntEnable(unsigned long ulBase, unsigned long ulComp)
325 {
326     //
327     // Check the arguments.
328     //
329     ASSERT(ulBase == COMP_BASE);
330     ASSERT(ulComp < 3);
331 
332     //
333     // Enable the comparator interrupt.
334     //
335     HWREG(ulBase + COMP_O_ACINTEN) |= 1 << ulComp;
336 }
337 
338 //*****************************************************************************
339 //
340 //! Disables the comparator interrupt.
341 //!
342 //! \param ulBase is the base address of the comparator module.
343 //! \param ulComp is the index of the comparator.
344 //!
345 //! This function disables generation of an interrupt from the specified
346 //! comparator.  Only enabled comparator interrupts can be reflected
347 //! to the processor.
348 //!
349 //! \return None.
350 //
351 //*****************************************************************************
352 void
ComparatorIntDisable(unsigned long ulBase,unsigned long ulComp)353 ComparatorIntDisable(unsigned long ulBase, unsigned long ulComp)
354 {
355     //
356     // Check the arguments.
357     //
358     ASSERT(ulBase == COMP_BASE);
359     ASSERT(ulComp < 3);
360 
361     //
362     // Disable the comparator interrupt.
363     //
364     HWREG(ulBase + COMP_O_ACINTEN) &= ~(1 << ulComp);
365 }
366 
367 //*****************************************************************************
368 //
369 //! Gets the current interrupt status.
370 //!
371 //! \param ulBase is the base address of the comparator module.
372 //! \param ulComp is the index of the comparator.
373 //! \param bMasked is \b false if the raw interrupt status is required and
374 //! \b true if the masked interrupt status is required.
375 //!
376 //! This function returns the interrupt status for the comparator.  Either the raw or
377 //! the masked interrupt status can be returned.
378 //!
379 //! \return \b true if the interrupt is asserted and \b false if it is not
380 //! asserted.
381 //
382 //*****************************************************************************
383 tBoolean
ComparatorIntStatus(unsigned long ulBase,unsigned long ulComp,tBoolean bMasked)384 ComparatorIntStatus(unsigned long ulBase, unsigned long ulComp,
385                     tBoolean bMasked)
386 {
387     //
388     // Check the arguments.
389     //
390     ASSERT(ulBase == COMP_BASE);
391     ASSERT(ulComp < 3);
392 
393     //
394     // Return either the interrupt status or the raw interrupt status as
395     // requested.
396     //
397     if(bMasked)
398     {
399         return(((HWREG(ulBase + COMP_O_ACMIS) >> ulComp) & 1) ? true : false);
400     }
401     else
402     {
403         return(((HWREG(ulBase + COMP_O_ACRIS) >> ulComp) & 1) ? true : false);
404     }
405 }
406 
407 //*****************************************************************************
408 //
409 //! Clears a comparator interrupt.
410 //!
411 //! \param ulBase is the base address of the comparator module.
412 //! \param ulComp is the index of the comparator.
413 //!
414 //! The comparator interrupt is cleared, so that it no longer asserts.  This
415 //! fucntion must be called in the interrupt handler to keep the handler from
416 //! being called again immediately upon exit.  Note that for a level-triggered
417 //! interrupt, the interrupt cannot be cleared until it stops asserting.
418 //!
419 //! \note Because there is a write buffer in the Cortex-M processor, it may
420 //! take several clock cycles before the interrupt source is actually cleared.
421 //! Therefore, it is recommended that the interrupt source be cleared early in
422 //! the interrupt handler (as opposed to the very last action) to avoid
423 //! returning from the interrupt handler before the interrupt source is
424 //! actually cleared.  Failure to do so may result in the interrupt handler
425 //! being immediately reentered (because the interrupt controller still sees
426 //! the interrupt source asserted).
427 //!
428 //! \return None.
429 //
430 //*****************************************************************************
431 void
ComparatorIntClear(unsigned long ulBase,unsigned long ulComp)432 ComparatorIntClear(unsigned long ulBase, unsigned long ulComp)
433 {
434     //
435     // Check the arguments.
436     //
437     ASSERT(ulBase == COMP_BASE);
438     ASSERT(ulComp < 3);
439 
440     //
441     // Clear the interrupt.
442     //
443     HWREG(ulBase + COMP_O_ACMIS) = 1 << ulComp;
444 }
445 
446 //*****************************************************************************
447 //
448 // Close the Doxygen group.
449 //! @}
450 //
451 //*****************************************************************************
452