]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_LM3S811_IAR/LuminaryCode/comp.c
UpdUpdate IAR projects to use Embedded Workbench V5.11.
[freertos] / Demo / CORTEX_LM3S811_IAR / LuminaryCode / comp.c
1 //*****************************************************************************\r
2 //\r
3 // comp.c - Driver for the analog comparator.\r
4 //\r
5 // Copyright (c) 2005,2006 Luminary Micro, Inc.  All rights reserved.\r
6 //\r
7 // Software License Agreement\r
8 //\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
11 //\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
17 //\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
23 //\r
24 // This is part of revision 991 of the Stellaris Driver Library.\r
25 //\r
26 //*****************************************************************************\r
27 \r
28 //*****************************************************************************\r
29 //\r
30 //! \addtogroup comp_api\r
31 //! @{\r
32 //\r
33 //*****************************************************************************\r
34 \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
39 #include "comp.h"\r
40 #include "debug.h"\r
41 #include "interrupt.h"\r
42 \r
43 //*****************************************************************************\r
44 //\r
45 //! Configures a comparator.\r
46 //!\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
50 //!\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
54 //!\r
55 //! The \b COMP_TRIG_xxx term can take on the following values:\r
56 //!\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
62 //! high.\r
63 //! - \b COMP_TRIG_BOTH to trigger the ADC when the comparator output goes low\r
64 //! or high.\r
65 //!\r
66 //! The \b COMP_INT_xxx term can take on the following values:\r
67 //!\r
68 //! - \b COMP_INT_HIGH to generate an interrupt when the comparator output is\r
69 //! high.\r
70 //! - \b COMP_INT_LOW to generate an interrupt when the comparator output is\r
71 //! low.\r
72 //! - \b COMP_INT_FALL to generate an interrupt when the comparator output goes\r
73 //! low.\r
74 //! - \b COMP_INT_RISE to generate an interrupt when the comparator output goes\r
75 //! high.\r
76 //! - \b COMP_INT_BOTH to generate an interrupt when the comparator output goes\r
77 //! low or high.\r
78 //!\r
79 //! The \b COMP_ASRCP_xxx term can take on the following values:\r
80 //!\r
81 //! - \b COMP_ASRCP_PIN to use the dedicated Comp+ pin as the reference\r
82 //! voltage.\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
87 //!\r
88 //! The \b COMP_OUTPUT_xxx term can take on the following values:\r
89 //!\r
90 //! - \b COMP_OUTPUT_NONE to disable the output from the comparator to a device\r
91 //! pin.\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
95 //! a device pin.\r
96 //!\r
97 //! \return None.\r
98 //\r
99 //*****************************************************************************\r
100 #if defined(GROUP_configure) || defined(BUILD_ALL) || defined(DOXYGEN)\r
101 void\r
102 ComparatorConfigure(unsigned long ulBase, unsigned long ulComp,\r
103                     unsigned long ulConfig)\r
104 {\r
105     //\r
106     // Check the arguments.\r
107     //\r
108     ASSERT(ulBase == COMP_BASE);\r
109     ASSERT(ulComp < 3);\r
110 \r
111     //\r
112     // Configure this comparator.\r
113     //\r
114     HWREG(ulBase + (ulComp * 0x20) + COMP_O_ACCTL0) = ulConfig;\r
115 }\r
116 #endif\r
117 \r
118 //*****************************************************************************\r
119 //\r
120 //! Sets the internal reference voltage.\r
121 //!\r
122 //! \param ulBase is the base address of the comparator module.\r
123 //! \param ulRef is the desired reference voltage.\r
124 //!\r
125 //! This function will set the internal reference voltage value.  The voltage\r
126 //! is specified as one of the following values:\r
127 //!\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
157 //!\r
158 //! \return None.\r
159 //\r
160 //*****************************************************************************\r
161 #if defined(GROUP_refset) || defined(BUILD_ALL) || defined(DOXYGEN)\r
162 void\r
163 ComparatorRefSet(unsigned long ulBase, unsigned long ulRef)\r
164 {\r
165     //\r
166     // Check the arguments.\r
167     //\r
168     ASSERT(ulBase == COMP_BASE);\r
169 \r
170     //\r
171     // Set the voltage reference voltage as requested.\r
172     //\r
173     HWREG(ulBase + COMP_O_REFCTL) = ulRef;\r
174 }\r
175 #endif\r
176 \r
177 //*****************************************************************************\r
178 //\r
179 //! Gets the current comparator output value.\r
180 //!\r
181 //! \param ulBase is the base address of the comparator module.\r
182 //! \param ulComp is the index of the comparator.\r
183 //!\r
184 //! This function retrieves the current value of the comparator output.\r
185 //!\r
186 //! \return Returns \b true if the comparator output is high and \b false if\r
187 //! the comparator output is low.\r
188 //\r
189 //*****************************************************************************\r
190 #if defined(GROUP_valueget) || defined(BUILD_ALL) || defined(DOXYGEN)\r
191 tBoolean\r
192 ComparatorValueGet(unsigned long ulBase, unsigned long ulComp)\r
193 {\r
194     //\r
195     // Check the arguments.\r
196     //\r
197     ASSERT(ulBase == COMP_BASE);\r
198     ASSERT(ulComp < 3);\r
199 \r
200     //\r
201     // Return the appropriate value based on the comparator's present output\r
202     // value.\r
203     //\r
204     if(HWREG(ulBase + (ulComp * 0x20) + COMP_O_ACSTAT0) & COMP_ACSTAT_OVAL)\r
205     {\r
206         return(true);\r
207     }\r
208     else\r
209     {\r
210         return(false);\r
211     }\r
212 }\r
213 #endif\r
214 \r
215 //*****************************************************************************\r
216 //\r
217 //! Registers an interrupt handler for the comparator interrupt.\r
218 //!\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
223 //!\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
228 //!\r
229 //! \sa IntRegister() for important information about registering interrupt\r
230 //! handlers.\r
231 //!\r
232 //! \return None.\r
233 //\r
234 //*****************************************************************************\r
235 #if defined(GROUP_intregister) || defined(BUILD_ALL) || defined(DOXYGEN)\r
236 void\r
237 ComparatorIntRegister(unsigned long ulBase, unsigned long ulComp,\r
238                       void (*pfnHandler)(void))\r
239 {\r
240     //\r
241     // Check the arguments.\r
242     //\r
243     ASSERT(ulBase == COMP_BASE);\r
244     ASSERT(ulComp < 3);\r
245 \r
246     //\r
247     // Register the interrupt handler, returning an error if an error occurs.\r
248     //\r
249     IntRegister(INT_COMP0 + ulComp, pfnHandler);\r
250 \r
251     //\r
252     // Enable the interrupt in the interrupt controller.\r
253     //\r
254     IntEnable(INT_COMP0 + ulComp);\r
255 \r
256     //\r
257     // Enable the comparator interrupt.\r
258     //\r
259     HWREG(ulBase + COMP_O_INTEN) |= 1 << ulComp;\r
260 }\r
261 #endif\r
262 \r
263 //*****************************************************************************\r
264 //\r
265 //! Unregisters an interrupt handler for a comparator interrupt.\r
266 //!\r
267 //! \param ulBase is the base address of the comparator module.\r
268 //! \param ulComp is the index of the comparator.\r
269 //!\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
273 //!\r
274 //! \sa IntRegister() for important information about registering interrupt\r
275 //! handlers.\r
276 //!\r
277 //! \return None.\r
278 //\r
279 //*****************************************************************************\r
280 #if defined(GROUP_intunregister) || defined(BUILD_ALL) || defined(DOXYGEN)\r
281 void\r
282 ComparatorIntUnregister(unsigned long ulBase, unsigned long ulComp)\r
283 {\r
284     //\r
285     // Check the arguments.\r
286     //\r
287     ASSERT(ulBase == COMP_BASE);\r
288     ASSERT(ulComp < 3);\r
289 \r
290     //\r
291     // Disable the comparator interrupt.\r
292     //\r
293     HWREG(ulBase + COMP_O_INTEN) &= ~(1 << ulComp);\r
294 \r
295     //\r
296     // Disable the interrupt in the interrupt controller.\r
297     //\r
298     IntDisable(INT_COMP0 + ulComp);\r
299 \r
300     //\r
301     // Unregister the interrupt handler.\r
302     //\r
303     IntUnregister(INT_COMP0 + ulComp);\r
304 }\r
305 #endif\r
306 \r
307 //*****************************************************************************\r
308 //\r
309 //! Enables the comparator interrupt.\r
310 //!\r
311 //! \param ulBase is the base address of the comparator module.\r
312 //! \param ulComp is the index of the comparator.\r
313 //!\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
317 //!\r
318 //! \return None.\r
319 //\r
320 //*****************************************************************************\r
321 #if defined(GROUP_intenable) || defined(BUILD_ALL) || defined(DOXYGEN)\r
322 void\r
323 ComparatorIntEnable(unsigned long ulBase, unsigned long ulComp)\r
324 {\r
325     //\r
326     // Check the arguments.\r
327     //\r
328     ASSERT(ulBase == COMP_BASE);\r
329     ASSERT(ulComp < 3);\r
330 \r
331     //\r
332     // Enable the comparator interrupt.\r
333     //\r
334     HWREG(ulBase + COMP_O_INTEN) |= 1 << ulComp;\r
335 }\r
336 #endif\r
337 \r
338 //*****************************************************************************\r
339 //\r
340 //! Disables the comparator interrupt.\r
341 //!\r
342 //! \param ulBase is the base address of the comparator module.\r
343 //! \param ulComp is the index of the comparator.\r
344 //!\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
348 //!\r
349 //! \return None.\r
350 //\r
351 //*****************************************************************************\r
352 #if defined(GROUP_intdisable) || defined(BUILD_ALL) || defined(DOXYGEN)\r
353 void\r
354 ComparatorIntDisable(unsigned long ulBase, unsigned long ulComp)\r
355 {\r
356     //\r
357     // Check the arguments.\r
358     //\r
359     ASSERT(ulBase == COMP_BASE);\r
360     ASSERT(ulComp < 3);\r
361 \r
362     //\r
363     // Disable the comparator interrupt.\r
364     //\r
365     HWREG(ulBase + COMP_O_INTEN) &= ~(1 << ulComp);\r
366 }\r
367 #endif\r
368 \r
369 //*****************************************************************************\r
370 //\r
371 //! Gets the current interrupt status.\r
372 //!\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
377 //!\r
378 //! This returns the interrupt status for the comparator.  Either the raw or\r
379 //! the masked interrupt status can be returned.\r
380 //!\r
381 //! \return \b true if the interrupt is asserted and \b false if it is not\r
382 //! asserted.\r
383 //\r
384 //*****************************************************************************\r
385 #if defined(GROUP_intstatus) || defined(BUILD_ALL) || defined(DOXYGEN)\r
386 tBoolean\r
387 ComparatorIntStatus(unsigned long ulBase, unsigned long ulComp,\r
388                     tBoolean bMasked)\r
389 {\r
390     //\r
391     // Check the arguments.\r
392     //\r
393     ASSERT(ulBase == COMP_BASE);\r
394     ASSERT(ulComp < 3);\r
395 \r
396     //\r
397     // Return either the interrupt status or the raw interrupt status as\r
398     // requested.\r
399     //\r
400     if(bMasked)\r
401     {\r
402         return(((HWREG(ulBase + COMP_O_MIS) >> ulComp) & 1) ? true : false);\r
403     }\r
404     else\r
405     {\r
406         return(((HWREG(ulBase + COMP_O_RIS) >> ulComp) & 1) ? true : false);\r
407     }\r
408 }\r
409 #endif\r
410 \r
411 //*****************************************************************************\r
412 //\r
413 //! Clears a comparator interrupt.\r
414 //!\r
415 //! \param ulBase is the base address of the comparator module.\r
416 //! \param ulComp is the index of the comparator.\r
417 //!\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
422 //!\r
423 //! \return None.\r
424 //\r
425 //*****************************************************************************\r
426 #if defined(GROUP_intclear) || defined(BUILD_ALL) || defined(DOXYGEN)\r
427 void\r
428 ComparatorIntClear(unsigned long ulBase, unsigned long ulComp)\r
429 {\r
430     //\r
431     // Check the arguments.\r
432     //\r
433     ASSERT(ulBase == COMP_BASE);\r
434     ASSERT(ulComp < 3);\r
435 \r
436     //\r
437     // Clear the interrupt.\r
438     //\r
439     HWREG(ulBase + COMP_O_MIS) = 1 << ulComp;\r
440 }\r
441 #endif\r
442 \r
443 //*****************************************************************************\r
444 //\r
445 // Close the Doxygen group.\r
446 //! @}\r
447 //\r
448 //*****************************************************************************\r