]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LM3S811_GCC/hw_include/timer.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / CORTEX_LM3S811_GCC / hw_include / timer.c
1 //*****************************************************************************\r
2 //\r
3 // timer.c - Driver for the timer module.\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 timer_api\r
31 //! @{\r
32 //\r
33 //*****************************************************************************\r
34 \r
35 #include "../hw_ints.h"\r
36 #include "../hw_memmap.h"\r
37 #include "../hw_timer.h"\r
38 #include "../hw_types.h"\r
39 #include "debug.h"\r
40 #include "interrupt.h"\r
41 #include "timer.h"\r
42 \r
43 //*****************************************************************************\r
44 //\r
45 //! Enables the timer(s).\r
46 //!\r
47 //! \param ulBase is the base address of the timer module.\r
48 //! \param ulTimer specifies the timer(s) to enable; must be one of \b TIMER_A,\r
49 //! \b TIMER_B, or \b TIMER_BOTH.\r
50 //!\r
51 //! This will enable operation of the timer module.  The timer must be\r
52 //! configured before it is enabled.\r
53 //!\r
54 //! \return None.\r
55 //\r
56 //*****************************************************************************\r
57 #if defined(GROUP_enable) || defined(BUILD_ALL) || defined(DOXYGEN)\r
58 void\r
59 TimerEnable(unsigned long ulBase, unsigned long ulTimer)\r
60 {\r
61     //\r
62     // Check the arguments.\r
63     //\r
64     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
65            (ulBase == TIMER2_BASE));\r
66     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||\r
67            (ulTimer == TIMER_BOTH));\r
68 \r
69     //\r
70     // Enable the timer(s) module.\r
71     //\r
72     HWREG(ulBase + TIMER_O_CTL) |= ulTimer & (TIMER_CTL_TAEN | TIMER_CTL_TBEN);\r
73 }\r
74 #endif\r
75 \r
76 //*****************************************************************************\r
77 //\r
78 //! Disables the timer(s).\r
79 //!\r
80 //! \param ulBase is the base address of the timer module.\r
81 //! \param ulTimer specifies the timer(s) to disable; must be one of\r
82 //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.\r
83 //!\r
84 //! This will disable operation of the timer module.\r
85 //!\r
86 //! \return None.\r
87 //\r
88 //*****************************************************************************\r
89 #if defined(GROUP_disable) || defined(BUILD_ALL) || defined(DOXYGEN)\r
90 void\r
91 TimerDisable(unsigned long ulBase, unsigned long ulTimer)\r
92 {\r
93     //\r
94     // Check the arguments.\r
95     //\r
96     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
97            (ulBase == TIMER2_BASE));\r
98     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||\r
99            (ulTimer == TIMER_BOTH));\r
100 \r
101     //\r
102     // Disable the timer module.\r
103     //\r
104     HWREG(ulBase + TIMER_O_CTL) &= ~(ulTimer &\r
105                                      (TIMER_CTL_TAEN | TIMER_CTL_TBEN));\r
106 }\r
107 #endif\r
108 \r
109 //*****************************************************************************\r
110 //\r
111 //! Configures the timer(s).\r
112 //!\r
113 //! \param ulBase is the base address of the timer module.\r
114 //! \param ulConfig is the configuration for the timer.\r
115 //!\r
116 //! This function configures the operating mode of the timer(s).  The timer\r
117 //! module is disabled before being configured, and is left in the disabled\r
118 //! state.  The configuration is specified in \e ulConfig as one of the\r
119 //! following values:\r
120 //!\r
121 //! - \b TIMER_CFG_32_BIT_OS - 32-bit one shot timer\r
122 //! - \b TIMER_CFG_32_BIT_PER - 32-bit periodic timer\r
123 //! - \b TIMER_CFG_32_RTC - 32-bit real time clock timer\r
124 //! - \b TIMER_CFG_16_BIT_PAIR - Two 16-bit timers\r
125 //!\r
126 //! When configured for a pair of 16-bit timers, each timer is separately\r
127 //! configured.  The first timer is configured by setting \e ulConfig to\r
128 //! the result of a logical OR operation between one of the following values\r
129 //! and \e ulConfig:\r
130 //!\r
131 //! - \b TIMER_CFG_A_ONE_SHOT - 16-bit one shot timer\r
132 //! - \b TIMER_CFG_A_PERIODIC - 16-bit periodic timer\r
133 //! - \b TIMER_CFG_A_CAP_COUNT - 16-bit edge count capture\r
134 //! - \b TIMER_CFG_A_CAP_TIME - 16-bit edge time capture\r
135 //! - \b TIMER_CFG_A_PWM - 16-bit PWM output\r
136 //!\r
137 //! Similarly, the second timer is configured by setting \e ulConfig to\r
138 //! the result of a logical OR operation between one of the corresponding\r
139 //! \b TIMER_CFG_B_* values and \e ulConfig.\r
140 //!\r
141 //! \return None.\r
142 //\r
143 //*****************************************************************************\r
144 #if defined(GROUP_configure) || defined(BUILD_ALL) || defined(DOXYGEN)\r
145 void\r
146 TimerConfigure(unsigned long ulBase, unsigned long ulConfig)\r
147 {\r
148     //\r
149     // Check the arguments.\r
150     //\r
151     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
152            (ulBase == TIMER2_BASE));\r
153     ASSERT((ulConfig == TIMER_CFG_32_BIT_OS) ||\r
154            (ulConfig == TIMER_CFG_32_BIT_PER) ||\r
155            (ulConfig == TIMER_CFG_32_RTC) ||\r
156            ((ulConfig & 0xff000000) == TIMER_CFG_16_BIT_PAIR));\r
157     ASSERT(((ulConfig & 0xff000000) != TIMER_CFG_16_BIT_PAIR) ||\r
158            ((((ulConfig & 0x000000ff) == TIMER_CFG_A_ONE_SHOT) ||\r
159              ((ulConfig & 0x000000ff) == TIMER_CFG_A_PERIODIC) ||\r
160              ((ulConfig & 0x000000ff) == TIMER_CFG_A_CAP_COUNT) ||\r
161              ((ulConfig & 0x000000ff) == TIMER_CFG_A_CAP_TIME) ||\r
162              ((ulConfig & 0x000000ff) == TIMER_CFG_A_PWM)) &&\r
163             (((ulConfig & 0x0000ff00) == TIMER_CFG_B_ONE_SHOT) ||\r
164              ((ulConfig & 0x0000ff00) == TIMER_CFG_B_PERIODIC) ||\r
165              ((ulConfig & 0x0000ff00) == TIMER_CFG_B_CAP_COUNT) ||\r
166              ((ulConfig & 0x0000ff00) == TIMER_CFG_B_CAP_TIME) ||\r
167              ((ulConfig & 0x0000ff00) == TIMER_CFG_B_PWM))));\r
168 \r
169     //\r
170     // Disable the timers.\r
171     //\r
172     HWREG(ulBase + TIMER_O_CTL) &= ~(TIMER_CTL_TAEN | TIMER_CTL_TBEN);\r
173 \r
174     //\r
175     // Set the global timer configuration.\r
176     //\r
177     HWREG(ulBase + TIMER_O_CFG) = ulConfig >> 24;\r
178 \r
179     //\r
180     // Set the configuration of the A and B timers.  Note that the B timer\r
181     // configuration is ignored by the hardware in 32-bit modes.\r
182     //\r
183     HWREG(ulBase + TIMER_O_TAMR) = ulConfig & 255;\r
184     HWREG(ulBase + TIMER_O_TBMR) = (ulConfig >> 8) & 255;\r
185 }\r
186 #endif\r
187 \r
188 //*****************************************************************************\r
189 //\r
190 //! Controls the output level.\r
191 //!\r
192 //! \param ulBase is the base address of the timer module.\r
193 //! \param ulTimer specifies the timer(s) to adjust; must be one of \b TIMER_A,\r
194 //! \b TIMER_B, or \b TIMER_BOTH.\r
195 //! \param bInvert specifies the output level.\r
196 //!\r
197 //! This function sets the PWM output level for the specified timer.  If the\r
198 //! parameter \e bInvert is \b true, then the timer's output will be made\r
199 //! active low; otherwise, it will be made active high.\r
200 //!\r
201 //! \return None.\r
202 //\r
203 //*****************************************************************************\r
204 #if defined(GROUP_controllevel) || defined(BUILD_ALL) || defined(DOXYGEN)\r
205 void\r
206 TimerControlLevel(unsigned long ulBase, unsigned long ulTimer,\r
207                   tBoolean bInvert)\r
208 {\r
209     //\r
210     // Check the arguments.\r
211     //\r
212     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
213            (ulBase == TIMER2_BASE));\r
214     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||\r
215            (ulTimer == TIMER_BOTH));\r
216 \r
217     //\r
218     // Set the output levels as requested.\r
219     //\r
220     ulTimer &= TIMER_CTL_TAPWML | TIMER_CTL_TBPWML;\r
221     HWREG(ulBase + TIMER_O_CTL) = (bInvert ?\r
222                                    (HWREG(ulBase + TIMER_O_CTL) | ulTimer) :\r
223                                    (HWREG(ulBase + TIMER_O_CTL) & ~(ulTimer)));\r
224 }\r
225 #endif\r
226 \r
227 //*****************************************************************************\r
228 //\r
229 //! Enables or disables the trigger output.\r
230 //!\r
231 //! \param ulBase is the base address of the timer module.\r
232 //! \param ulTimer specifies the timer to adjust; must be one of \b TIMER_A,\r
233 //! \b TIMER_B, or \b TIMER_BOTH.\r
234 //! \param bEnable specifies the desired trigger state.\r
235 //!\r
236 //! This function controls the trigger output for the specified timer.  If the\r
237 //! parameter \e bEnable is \b true, then the timer's output trigger is\r
238 //! enabled; otherwise it is disabled.\r
239 //!\r
240 //! \return None.\r
241 //\r
242 //*****************************************************************************\r
243 #if defined(GROUP_controltrigger) || defined(BUILD_ALL) || defined(DOXYGEN)\r
244 void\r
245 TimerControlTrigger(unsigned long ulBase, unsigned long ulTimer,\r
246                     tBoolean bEnable)\r
247 {\r
248     //\r
249     // Check the arguments.\r
250     //\r
251     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
252            (ulBase == TIMER2_BASE));\r
253     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||\r
254            (ulTimer == TIMER_BOTH));\r
255 \r
256     //\r
257     // Set the trigger output as requested.\r
258     //\r
259     ulTimer &= TIMER_CTL_TAOTE | TIMER_CTL_TBOTE;\r
260     HWREG(ulBase + TIMER_O_CTL) = (bEnable ?\r
261                                    (HWREG(ulBase + TIMER_O_CTL) | ulTimer) :\r
262                                    (HWREG(ulBase + TIMER_O_CTL) & ~(ulTimer)));\r
263 }\r
264 #endif\r
265 \r
266 //*****************************************************************************\r
267 //\r
268 //! Controls the event type.\r
269 //!\r
270 //! \param ulBase is the base address of the timer module.\r
271 //! \param ulTimer specifies the timer(s) to be adjusted; must be one of\r
272 //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.\r
273 //! \param ulEvent specifies the type of event; must be one of\r
274 //! \b TIMER_EVENT_POS_EDGE, \b TIMER_EVENT_NEG_EDGE, or\r
275 //! \b TIMER_EVENT_BOTH_EDGES.\r
276 //!\r
277 //! This function sets the signal edge(s) that will trigger the timer when in\r
278 //! capture mode.\r
279 //!\r
280 //! \return None.\r
281 //\r
282 //*****************************************************************************\r
283 #if defined(GROUP_controlevent) || defined(BUILD_ALL) || defined(DOXYGEN)\r
284 void\r
285 TimerControlEvent(unsigned long ulBase, unsigned long ulTimer,\r
286                   unsigned long ulEvent)\r
287 {\r
288     //\r
289     // Check the arguments.\r
290     //\r
291     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
292            (ulBase == TIMER2_BASE));\r
293     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||\r
294            (ulTimer == TIMER_BOTH));\r
295 \r
296     //\r
297     // Set the event type.\r
298     //\r
299     ulEvent &= ulTimer & (TIMER_CTL_TAEVENT_MSK | TIMER_CTL_TBEVENT_MSK);\r
300     HWREG(ulBase + TIMER_O_CTL) = ((HWREG(ulBase + TIMER_O_CTL) &\r
301                                     ~(TIMER_CTL_TAEVENT_MSK |\r
302                                       TIMER_CTL_TBEVENT_MSK)) | ulEvent);\r
303 }\r
304 #endif\r
305 \r
306 //*****************************************************************************\r
307 //\r
308 //! Controls the stall handling.\r
309 //!\r
310 //! \param ulBase is the base address of the timer module.\r
311 //! \param ulTimer specifies the timer(s) to be adjusted; must be one of\r
312 //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.\r
313 //! \param bStall specifies the response to a stall signal.\r
314 //!\r
315 //! This function controls the stall response for the specified timer.  If the\r
316 //! parameter \e bStall is \b true, then the timer will stop counting if the\r
317 //! processor enters debug mode; otherwise the timer will keep running while in\r
318 //! debug mode.\r
319 //!\r
320 //! \return None.\r
321 //\r
322 //*****************************************************************************\r
323 #if defined(GROUP_controlstall) || defined(BUILD_ALL) || defined(DOXYGEN)\r
324 void\r
325 TimerControlStall(unsigned long ulBase, unsigned long ulTimer,\r
326                   tBoolean  bStall)\r
327 {\r
328     //\r
329     // Check the arguments.\r
330     //\r
331     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
332            (ulBase == TIMER2_BASE));\r
333     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||\r
334            (ulTimer == TIMER_BOTH));\r
335 \r
336     //\r
337     // Set the stall mode.\r
338     //\r
339     ulTimer &= TIMER_CTL_TASTALL | TIMER_CTL_TBSTALL;\r
340     HWREG(ulBase + TIMER_O_CTL) = (bStall ?\r
341                                    (HWREG(ulBase + TIMER_O_CTL) | ulTimer) :\r
342                                    (HWREG(ulBase + TIMER_O_CTL) & ~(ulTimer)));\r
343 }\r
344 #endif\r
345 \r
346 //*****************************************************************************\r
347 //\r
348 //! Enable RTC counting.\r
349 //!\r
350 //! \param ulBase is the base address of the timer module.\r
351 //!\r
352 //! This function causes the timer to start counting when in RTC mode.  If not\r
353 //! configured for RTC mode, this will do nothing.\r
354 //!\r
355 //! \return None.\r
356 //\r
357 //*****************************************************************************\r
358 #if defined(GROUP_rtcenable) || defined(BUILD_ALL) || defined(DOXYGEN)\r
359 void\r
360 TimerRTCEnable(unsigned long ulBase)\r
361 {\r
362     //\r
363     // Check the arguments.\r
364     //\r
365     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
366            (ulBase == TIMER2_BASE));\r
367 \r
368     //\r
369     // Enable RTC counting.\r
370     //\r
371     HWREG(ulBase + TIMER_O_CTL) |= TIMER_CTL_RTCEN;\r
372 }\r
373 #endif\r
374 \r
375 //*****************************************************************************\r
376 //\r
377 //! Disable RTC counting.\r
378 //!\r
379 //! \param ulBase is the base address of the timer module.\r
380 //!\r
381 //! This function causes the timer to stop counting when in RTC mode.\r
382 //!\r
383 //! \return None.\r
384 //\r
385 //*****************************************************************************\r
386 #if defined(GROUP_rtcdisable) || defined(BUILD_ALL) || defined(DOXYGEN)\r
387 void\r
388 TimerRTCDisable(unsigned long ulBase)\r
389 {\r
390     //\r
391     // Check the arguments.\r
392     //\r
393     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
394            (ulBase == TIMER2_BASE));\r
395 \r
396     //\r
397     // Disable RTC counting.\r
398     //\r
399     HWREG(ulBase + TIMER_O_CTL) &= ~(TIMER_CTL_RTCEN);\r
400 }\r
401 #endif\r
402 \r
403 //*****************************************************************************\r
404 //\r
405 //! Set the timer prescale value.\r
406 //!\r
407 //! \param ulBase is the base address of the timer module.\r
408 //! \param ulTimer specifies the timer(s) to adjust; must be one of \b TIMER_A,\r
409 //! \b TIMER_B, or \b TIMER_BOTH.\r
410 //! \param ulValue is the timer prescale value; must be between 0 and 255,\r
411 //! inclusive.\r
412 //!\r
413 //! This function sets the value of the input clock prescaler.  The prescaler\r
414 //! is only operational when in 16-bit mode and is used to extend the range of\r
415 //! the 16-bit timer modes.\r
416 //!\r
417 //! \return None.\r
418 //\r
419 //*****************************************************************************\r
420 #if defined(GROUP_prescaleset) || defined(BUILD_ALL) || defined(DOXYGEN)\r
421 void\r
422 TimerPrescaleSet(unsigned long ulBase, unsigned long ulTimer,\r
423                  unsigned long ulValue)\r
424 {\r
425     //\r
426     // Check the arguments.\r
427     //\r
428     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
429            (ulBase == TIMER2_BASE));\r
430     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||\r
431            (ulTimer == TIMER_BOTH));\r
432     ASSERT(ulValue < 256);\r
433 \r
434     //\r
435     // Set the timer A prescaler if requested.\r
436     //\r
437     if(ulTimer & TIMER_A)\r
438     {\r
439         HWREG(ulBase + TIMER_O_TAPR) = ulValue;\r
440     }\r
441 \r
442     //\r
443     // Set the timer B prescaler if requested.\r
444     //\r
445     if(ulTimer & TIMER_B)\r
446     {\r
447         HWREG(ulBase + TIMER_O_TBPR) = ulValue;\r
448     }\r
449 }\r
450 #endif\r
451 \r
452 //*****************************************************************************\r
453 //\r
454 //! Get the timer prescale value.\r
455 //!\r
456 //! \param ulBase is the base address of the timer module.\r
457 //! \param ulTimer specifies the timer; must be one of \b TIMER_A or\r
458 //! \b TIMER_B.\r
459 //!\r
460 //! This function gets the value of the input clock prescaler.  The prescaler\r
461 //! is only operational when in 16-bit mode and is used to extend the range of\r
462 //! the 16-bit timer modes.\r
463 //!\r
464 //! \return The value of the timer prescaler.\r
465 //\r
466 //*****************************************************************************\r
467 #if defined(GROUP_prescaleget) || defined(BUILD_ALL) || defined(DOXYGEN)\r
468 unsigned long\r
469 TimerPrescaleGet(unsigned long ulBase, unsigned long ulTimer)\r
470 {\r
471     //\r
472     // Check the arguments.\r
473     //\r
474     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
475            (ulBase == TIMER2_BASE));\r
476     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||\r
477            (ulTimer == TIMER_BOTH));\r
478 \r
479     //\r
480     // Return the appropriate prescale value.\r
481     //\r
482     return((ulTimer == TIMER_A) ? HWREG(ulBase + TIMER_O_TAPR) :\r
483            HWREG(ulBase + TIMER_O_TBPR));\r
484 }\r
485 #endif\r
486 \r
487 //*****************************************************************************\r
488 //\r
489 //! Set the timer prescale match value.\r
490 //!\r
491 //! \param ulBase is the base address of the timer module.\r
492 //! \param ulTimer specifies the timer(s) to adjust; must be one of \b TIMER_A,\r
493 //! \b TIMER_B, or \b TIMER_BOTH.\r
494 //! \param ulValue is the timer prescale match value; must be between 0 and\r
495 //! 255, inclusive.\r
496 //!\r
497 //! This function sets the value of the input clock prescaler match value.\r
498 //! When in a 16-bit mode that uses the counter match (edge count or PWM), the\r
499 //! prescale match effectively extends the range of the counter to 24-bits.\r
500 //!\r
501 //! \return None.\r
502 //\r
503 //*****************************************************************************\r
504 #if defined(GROUP_prescalematchset) || defined(BUILD_ALL) || defined(DOXYGEN)\r
505 void\r
506 TimerPrescaleMatchSet(unsigned long ulBase, unsigned long ulTimer,\r
507                       unsigned long ulValue)\r
508 {\r
509     //\r
510     // Check the arguments.\r
511     //\r
512     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
513            (ulBase == TIMER2_BASE));\r
514     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||\r
515            (ulTimer == TIMER_BOTH));\r
516     ASSERT(ulValue < 256);\r
517 \r
518     //\r
519     // Set the timer A prescale match if requested.\r
520     //\r
521     if(ulTimer & TIMER_A)\r
522     {\r
523         HWREG(ulBase + TIMER_O_TAPMR) = ulValue;\r
524     }\r
525 \r
526     //\r
527     // Set the timer B prescale match if requested.\r
528     //\r
529     if(ulTimer & TIMER_B)\r
530     {\r
531         HWREG(ulBase + TIMER_O_TBPMR) = ulValue;\r
532     }\r
533 }\r
534 #endif\r
535 \r
536 //*****************************************************************************\r
537 //\r
538 //! Get the timer prescale match value.\r
539 //!\r
540 //! \param ulBase is the base address of the timer module.\r
541 //! \param ulTimer specifies the timer; must be one of \b TIMER_A or\r
542 //! \b TIMER_B.\r
543 //!\r
544 //! This function gets the value of the input clock prescaler match value.\r
545 //! When in a 16-bit mode that uses the counter match (edge count or PWM), the\r
546 //! prescale match effectively extends the range of the counter to 24-bits.\r
547 //!\r
548 //! \return The value of the timer prescale match.\r
549 //\r
550 //*****************************************************************************\r
551 #if defined(GROUP_prescalematchget) || defined(BUILD_ALL) || defined(DOXYGEN)\r
552 unsigned long\r
553 TimerPrescaleMatchGet(unsigned long ulBase, unsigned long ulTimer)\r
554 {\r
555     //\r
556     // Check the arguments.\r
557     //\r
558     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
559            (ulBase == TIMER2_BASE));\r
560     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||\r
561            (ulTimer == TIMER_BOTH));\r
562 \r
563     //\r
564     // Return the appropriate prescale match value.\r
565     //\r
566     return((ulTimer == TIMER_A) ? HWREG(ulBase + TIMER_O_TAPMR) :\r
567            HWREG(ulBase + TIMER_O_TBPMR));\r
568 }\r
569 #endif\r
570 \r
571 //*****************************************************************************\r
572 //\r
573 //! Sets the timer load value.\r
574 //!\r
575 //! \param ulBase is the base address of the timer module.\r
576 //! \param ulTimer specifies the timer(s) to adjust; must be one of \b TIMER_A,\r
577 //! \b TIMER_B, or \b TIMER_BOTH.  Only \b TIMER_A should be used when the\r
578 //! timer is configured for 32-bit operation.\r
579 //! \param ulValue is the load value.\r
580 //!\r
581 //! This function sets the timer load value; if the timer is running then the\r
582 //! value will be immediately loaded into the timer.\r
583 //!\r
584 //! \return None.\r
585 //\r
586 //*****************************************************************************\r
587 #if defined(GROUP_loadset) || defined(BUILD_ALL) || defined(DOXYGEN)\r
588 void\r
589 TimerLoadSet(unsigned long ulBase, unsigned long ulTimer,\r
590              unsigned long ulValue)\r
591 {\r
592     //\r
593     // Check the arguments.\r
594     //\r
595     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
596            (ulBase == TIMER2_BASE));\r
597     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||\r
598            (ulTimer == TIMER_BOTH));\r
599 \r
600     //\r
601     // Set the timer A load value if requested.\r
602     //\r
603     if(ulTimer & TIMER_A)\r
604     {\r
605         HWREG(ulBase + TIMER_O_TAILR) = ulValue;\r
606     }\r
607 \r
608     //\r
609     // Set the timer B load value if requested.\r
610     //\r
611     if(ulTimer & TIMER_B)\r
612     {\r
613         HWREG(ulBase + TIMER_O_TBILR) = ulValue;\r
614     }\r
615 }\r
616 #endif\r
617 \r
618 //*****************************************************************************\r
619 //\r
620 //! Gets the timer load value.\r
621 //!\r
622 //! \param ulBase is the base address of the timer module.\r
623 //! \param ulTimer specifies the timer; must be one of \b TIMER_A or\r
624 //! \b TIMER_B.  Only \b TIMER_A should be used when the timer is configured\r
625 //! for 32-bit operation.\r
626 //!\r
627 //! This function gets the currently programmed interval load value for the\r
628 //! specified timer.\r
629 //!\r
630 //! \return Returns the load value for the timer.\r
631 //\r
632 //*****************************************************************************\r
633 #if defined(GROUP_loadget) || defined(BUILD_ALL) || defined(DOXYGEN)\r
634 unsigned long\r
635 TimerLoadGet(unsigned long ulBase, unsigned long ulTimer)\r
636 {\r
637     //\r
638     // Check the arguments.\r
639     //\r
640     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
641            (ulBase == TIMER2_BASE));\r
642     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B));\r
643 \r
644     //\r
645     // Return the appropriate load value.\r
646     //\r
647     return((ulTimer == TIMER_A) ? HWREG(ulBase + TIMER_O_TAILR) :\r
648            HWREG(ulBase + TIMER_O_TBILR));\r
649 }\r
650 #endif\r
651 \r
652 //*****************************************************************************\r
653 //\r
654 //! Gets the current timer value.\r
655 //!\r
656 //! \param ulBase is the base address of the timer module.\r
657 //! \param ulTimer specifies the timer; must be one of \b TIMER_A or\r
658 //! \b TIMER_B.  Only \b TIMER_A should be used when the timer is configured\r
659 //! for 32-bit operation.\r
660 //!\r
661 //! This function reads the current value of the specified timer.\r
662 //!\r
663 //! \return Returns the current value of the timer.\r
664 //\r
665 //*****************************************************************************\r
666 #if defined(GROUP_valueget) || defined(BUILD_ALL) || defined(DOXYGEN)\r
667 unsigned long\r
668 TimerValueGet(unsigned long ulBase, unsigned long ulTimer)\r
669 {\r
670     //\r
671     // Check the arguments.\r
672     //\r
673     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
674            (ulBase == TIMER2_BASE));\r
675     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B));\r
676 \r
677     //\r
678     // Return the appropriate timer value.\r
679     //\r
680     return((ulTimer == TIMER_A) ? HWREG(ulBase + TIMER_O_TAR) :\r
681            HWREG(ulBase + TIMER_O_TBR));\r
682 }\r
683 #endif\r
684 \r
685 //*****************************************************************************\r
686 //\r
687 //! Sets the timer match value.\r
688 //!\r
689 //! \param ulBase is the base address of the timer module.\r
690 //! \param ulTimer specifies the timer(s) to adjust; must be one of \b TIMER_A,\r
691 //! \b TIMER_B, or \b TIMER_BOTH.  Only \b TIMER_A should be used when the\r
692 //! timer is configured for 32-bit operation.\r
693 //! \param ulValue is the match value.\r
694 //!\r
695 //! This function sets the match value for a timer.  This is used in capture\r
696 //! count mode to determine when to interrupt the processor and in PWM mode to\r
697 //! determine the duty cycle of the output signal.\r
698 //!\r
699 //! \return None.\r
700 //\r
701 //*****************************************************************************\r
702 #if defined(GROUP_matchset) || defined(BUILD_ALL) || defined(DOXYGEN)\r
703 void\r
704 TimerMatchSet(unsigned long ulBase, unsigned long ulTimer,\r
705               unsigned long ulValue)\r
706 {\r
707     //\r
708     // Check the arguments.\r
709     //\r
710     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
711            (ulBase == TIMER2_BASE));\r
712     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||\r
713            (ulTimer == TIMER_BOTH));\r
714 \r
715     //\r
716     // Set the timer A match value if requested.\r
717     //\r
718     if(ulTimer & TIMER_A)\r
719     {\r
720         HWREG(ulBase + TIMER_O_TAMATCHR) = ulValue;\r
721     }\r
722 \r
723     //\r
724     // Set the timer B match value if requested.\r
725     //\r
726     if(ulTimer & TIMER_B)\r
727     {\r
728         HWREG(ulBase + TIMER_O_TBMATCHR) = ulValue;\r
729     }\r
730 }\r
731 #endif\r
732 \r
733 //*****************************************************************************\r
734 //\r
735 //! Gets the timer match value.\r
736 //!\r
737 //! \param ulBase is the base address of the timer module.\r
738 //! \param ulTimer specifies the timer; must be one of \b TIMER_A or\r
739 //! \b TIMER_B.  Only \b TIMER_A should be used when the timer is configured\r
740 //! for 32-bit operation.\r
741 //!\r
742 //! This function gets the match value for the specified timer.\r
743 //!\r
744 //! \return Returns the match value for the timer.\r
745 //\r
746 //*****************************************************************************\r
747 #if defined(GROUP_matchget) || defined(BUILD_ALL) || defined(DOXYGEN)\r
748 unsigned long\r
749 TimerMatchGet(unsigned long ulBase, unsigned long ulTimer)\r
750 {\r
751     //\r
752     // Check the arguments.\r
753     //\r
754     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
755            (ulBase == TIMER2_BASE));\r
756     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B));\r
757 \r
758     //\r
759     // Return the appropriate match value.\r
760     //\r
761     return((ulTimer == TIMER_A) ? HWREG(ulBase + TIMER_O_TAMATCHR) :\r
762            HWREG(ulBase + TIMER_O_TBMATCHR));\r
763 }\r
764 #endif\r
765 \r
766 //*****************************************************************************\r
767 //\r
768 //! Registers an interrupt handler for the timer interrupt.\r
769 //!\r
770 //! \param ulBase is the base address of the timer module.\r
771 //! \param ulTimer specifies the timer(s); must be one of \b TIMER_A,\r
772 //! \b TIMER_B, or \b TIMER_BOTH.\r
773 //! \param pfnHandler is a pointer to the function to be called when the timer\r
774 //! interrupt occurs.\r
775 //!\r
776 //! This sets the handler to be called when a timer interrupt occurs.  This\r
777 //! will enable the global interrupt in the interrupt controller; specific\r
778 //! timer interrupts must be enabled via TimerIntEnable().  It is the interrupt\r
779 //! handler's responsibility to clear the interrupt source via TimerIntClear().\r
780 //!\r
781 //! \sa IntRegister() for important information about registering interrupt\r
782 //! handlers.\r
783 //!\r
784 //! \return None.\r
785 //\r
786 //*****************************************************************************\r
787 #if defined(GROUP_intregister) || defined(BUILD_ALL) || defined(DOXYGEN)\r
788 void\r
789 TimerIntRegister(unsigned long ulBase, unsigned long ulTimer,\r
790                  void (*pfnHandler)(void))\r
791 {\r
792     //\r
793     // Check the arguments.\r
794     //\r
795     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
796            (ulBase == TIMER2_BASE));\r
797     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||\r
798            (ulTimer == TIMER_BOTH));\r
799 \r
800     //\r
801     // Get the interrupt number for this timer module.\r
802     //\r
803     ulBase = ((ulBase == TIMER0_BASE) ? INT_TIMER0A :\r
804               ((ulBase == TIMER1_BASE) ? INT_TIMER1A : INT_TIMER2A));\r
805 \r
806     //\r
807     // Register an interrupt handler for timer A if requested.\r
808     //\r
809     if(ulTimer & TIMER_A)\r
810     {\r
811         //\r
812         // Register the interrupt handler.\r
813         //\r
814         IntRegister(ulBase, pfnHandler);\r
815 \r
816         //\r
817         // Enable the interrupt.\r
818         //\r
819         IntEnable(ulBase);\r
820     }\r
821 \r
822     //\r
823     // Register an interrupt handler for timer B if requested.\r
824     //\r
825     if(ulTimer & TIMER_B)\r
826     {\r
827         //\r
828         // Register the interrupt handler.\r
829         //\r
830         IntRegister(ulBase + 1, pfnHandler);\r
831 \r
832         //\r
833         // Enable the interrupt.\r
834         //\r
835         IntEnable(ulBase + 1);\r
836     }\r
837 }\r
838 #endif\r
839 \r
840 //*****************************************************************************\r
841 //\r
842 //! Unregisters an interrupt handler for the timer interrupt.\r
843 //!\r
844 //! \param ulBase is the base address of the timer module.\r
845 //! \param ulTimer specifies the timer(s); must be one of \b TIMER_A,\r
846 //! \b TIMER_B, or \b TIMER_BOTH.\r
847 //!\r
848 //! This function will clear the handler to be called when a timer interrupt\r
849 //! occurs.  This will also mask off the interrupt in the interrupt controller\r
850 //! so that the interrupt handler no longer is called.\r
851 //!\r
852 //! \sa IntRegister() for important information about registering interrupt\r
853 //! handlers.\r
854 //!\r
855 //! \return None.\r
856 //\r
857 //*****************************************************************************\r
858 #if defined(GROUP_intunregister) || defined(BUILD_ALL) || defined(DOXYGEN)\r
859 void\r
860 TimerIntUnregister(unsigned long ulBase, unsigned long ulTimer)\r
861 {\r
862     //\r
863     // Check the arguments.\r
864     //\r
865     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
866            (ulBase == TIMER2_BASE));\r
867     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||\r
868            (ulTimer == TIMER_BOTH));\r
869 \r
870     //\r
871     // Get the interrupt number for this timer module.\r
872     //\r
873     ulBase = ((ulBase == TIMER0_BASE) ? INT_TIMER0A :\r
874               ((ulBase == TIMER1_BASE) ? INT_TIMER1A : INT_TIMER2A));\r
875 \r
876     //\r
877     // Unregister the interrupt handler for timer A if requested.\r
878     //\r
879     if(ulTimer & TIMER_A)\r
880     {\r
881         //\r
882         // Disable the interrupt.\r
883         //\r
884         IntDisable(ulBase);\r
885 \r
886         //\r
887         // Unregister the interrupt handler.\r
888         //\r
889         IntUnregister(ulBase);\r
890     }\r
891 \r
892     //\r
893     // Unregister the interrupt handler for timer B if requested.\r
894     //\r
895     if(ulTimer & TIMER_B)\r
896     {\r
897         //\r
898         // Disable the interrupt.\r
899         //\r
900         IntDisable(ulBase + 1);\r
901 \r
902         //\r
903         // Unregister the interrupt handler.\r
904         //\r
905         IntUnregister(ulBase + 1);\r
906     }\r
907 }\r
908 #endif\r
909 \r
910 //*****************************************************************************\r
911 //\r
912 //! Enables individual timer interrupt sources.\r
913 //!\r
914 //! \param ulBase is the base address of the timer module.\r
915 //! \param ulIntFlags is the bit mask of the interrupt sources to be enabled.\r
916 //!\r
917 //! Enables the indicated timer interrupt sources.  Only the sources that are\r
918 //! enabled can be reflected to the processor interrupt; disabled sources have\r
919 //! no effect on the processor.\r
920 //!\r
921 //! The parameter \e ulIntFlags must be the logical OR of any combination of\r
922 //! the following:\r
923 //!\r
924 //! - TIMER_CAPB_EVENT  - Capture B event interrupt\r
925 //! - TIMER_CAPB_MATCH  - Capture B match interrupt\r
926 //! - TIMER_TIMB_TIMEOUT  - Timer B timeout interrupt\r
927 //! - TIMER_RTC_MATCH  - RTC interrupt mask\r
928 //! - TIMER_CAPA_EVENT  - Capture A event interrupt\r
929 //! - TIMER_CAPA_MATCH  - Capture A match interrupt\r
930 //! - TIMER_TIMA_TIMEOUT  - Timer A timeout interrupt\r
931 //!\r
932 //! \return None.\r
933 //\r
934 //*****************************************************************************\r
935 #if defined(GROUP_intenable) || defined(BUILD_ALL) || defined(DOXYGEN)\r
936 void\r
937 TimerIntEnable(unsigned long ulBase, unsigned long ulIntFlags)\r
938 {\r
939     //\r
940     // Check the arguments.\r
941     //\r
942     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
943            (ulBase == TIMER2_BASE));\r
944 \r
945     //\r
946     // Enable the specified interrupts.\r
947     //\r
948     HWREG(ulBase + TIMER_O_IMR) |= ulIntFlags;\r
949 }\r
950 #endif\r
951 \r
952 //*****************************************************************************\r
953 //\r
954 //! Disables individual timer interrupt sources.\r
955 //!\r
956 //! \param ulBase is the base address of the timer module.\r
957 //! \param ulIntFlags is the bit mask of the interrupt sources to be disabled.\r
958 //!\r
959 //! Disables the indicated timer interrupt sources.  Only the sources that are\r
960 //! enabled can be reflected to the processor interrupt; disabled sources have\r
961 //! no effect on the processor.\r
962 //!\r
963 //! The parameter \e ulIntFlags has the same definition as the \e ulIntFlags\r
964 //! parameter to TimerIntEnable().\r
965 //!\r
966 //! \return None.\r
967 //\r
968 //*****************************************************************************\r
969 #if defined(GROUP_intdisable) || defined(BUILD_ALL) || defined(DOXYGEN)\r
970 void\r
971 TimerIntDisable(unsigned long ulBase, unsigned long ulIntFlags)\r
972 {\r
973     //\r
974     // Check the arguments.\r
975     //\r
976     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
977            (ulBase == TIMER2_BASE));\r
978 \r
979     //\r
980     // Disable the specified interrupts.\r
981     //\r
982     HWREG(ulBase + TIMER_O_IMR) &= ~(ulIntFlags);\r
983 }\r
984 #endif\r
985 \r
986 //*****************************************************************************\r
987 //\r
988 //! Gets the current interrupt status.\r
989 //!\r
990 //! \param ulBase is the base address of the timer module.\r
991 //! \param bMasked is false if the raw interrupt status is required and true if\r
992 //! the masked interrupt status is required.\r
993 //!\r
994 //! This returns the interrupt status for the timer module.  Either the raw\r
995 //! interrupt status or the status of interrupts that are allowed to reflect to\r
996 //! the processor can be returned.\r
997 //!\r
998 //! \return The current interrupt status, enumerated as a bit field of\r
999 //! values described in TimerIntEnable().\r
1000 //\r
1001 //*****************************************************************************\r
1002 #if defined(GROUP_intstatus) || defined(BUILD_ALL) || defined(DOXYGEN)\r
1003 unsigned long\r
1004 TimerIntStatus(unsigned long ulBase, tBoolean bMasked)\r
1005 {\r
1006     //\r
1007     // Check the arguments.\r
1008     //\r
1009     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
1010            (ulBase == TIMER2_BASE));\r
1011 \r
1012     //\r
1013     // Return either the interrupt status or the raw interrupt status as\r
1014     // requested.\r
1015     //\r
1016     return(bMasked ? HWREG(ulBase + TIMER_O_MIS) :\r
1017            HWREG(ulBase + TIMER_O_RIS));\r
1018 }\r
1019 #endif\r
1020 \r
1021 //*****************************************************************************\r
1022 //\r
1023 //! Clears timer interrupt sources.\r
1024 //!\r
1025 //! \param ulBase is the base address of the timer module.\r
1026 //! \param ulIntFlags is a bit mask of the interrupt sources to be cleared.\r
1027 //!\r
1028 //! The specified timer interrupt sources are cleared, so that they no longer\r
1029 //! assert.  This must be done in the interrupt handler to keep it from being\r
1030 //! called again immediately upon exit.\r
1031 //!\r
1032 //! The parameter \e ulIntFlags has the same definition as the \e ulIntFlags\r
1033 //! parameter to TimerIntEnable().\r
1034 //!\r
1035 //! \return None.\r
1036 //\r
1037 //*****************************************************************************\r
1038 #if defined(GROUP_intclear) || defined(BUILD_ALL) || defined(DOXYGEN)\r
1039 void\r
1040 TimerIntClear(unsigned long ulBase, unsigned long ulIntFlags)\r
1041 {\r
1042     //\r
1043     // Check the arguments.\r
1044     //\r
1045     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
1046            (ulBase == TIMER2_BASE));\r
1047 \r
1048     //\r
1049     // Clear the requested interrupt sources.\r
1050     //\r
1051     HWREG(ulBase + TIMER_O_ICR) = ulIntFlags;\r
1052 }\r
1053 #endif\r
1054 \r
1055 //*****************************************************************************\r
1056 //\r
1057 //! Puts the timer into its reset state.\r
1058 //!\r
1059 //! \param ulBase is the base address of the timer module.\r
1060 //!\r
1061 //! The specified timer is disabled, and all its interrupts are disabled,\r
1062 //! cleared, and unregistered. Then the timer registers are set to their reset\r
1063 //! value.\r
1064 //!\r
1065 //! \return None.\r
1066 //\r
1067 //*****************************************************************************\r
1068 #if defined(GROUP_quiesce) || defined(BUILD_ALL) || defined(DOXYGEN)\r
1069 void\r
1070 TimerQuiesce(unsigned long ulBase)\r
1071 {\r
1072     //\r
1073     // Check the arguments.\r
1074     //\r
1075     ASSERT((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||\r
1076            (ulBase == TIMER2_BASE));\r
1077 \r
1078     //\r
1079     // Disable the timer.\r
1080     //\r
1081     HWREG(ulBase + TIMER_O_CTL) = TIMER_RV_CTL;\r
1082 \r
1083     //\r
1084     // Disable all the timer interrupts.\r
1085     //\r
1086     HWREG(ulBase + TIMER_O_IMR) = TIMER_RV_IMR;\r
1087 \r
1088     //\r
1089     // Clear all the timer interrupts.\r
1090     //\r
1091     HWREG(ulBase + TIMER_O_ICR) = 0xFFFFFFFF;\r
1092 \r
1093     //\r
1094     // Unregister the interrupt handler.  This also disables interrupts to the\r
1095     // core.\r
1096     //\r
1097     TimerIntUnregister(ulBase, TIMER_BOTH);\r
1098 \r
1099     //\r
1100     // Set all the registers to their reset value.\r
1101     //\r
1102     HWREG(ulBase + TIMER_O_CFG) = TIMER_RV_CFG;\r
1103     HWREG(ulBase + TIMER_O_TAMR) = TIMER_RV_TAMR;\r
1104     HWREG(ulBase + TIMER_O_TBMR) = TIMER_RV_TBMR;\r
1105     HWREG(ulBase + TIMER_O_RIS) = TIMER_RV_RIS;\r
1106     HWREG(ulBase + TIMER_O_MIS) = TIMER_RV_MIS;\r
1107     HWREG(ulBase + TIMER_O_TAILR) = TIMER_RV_TAILR;\r
1108     HWREG(ulBase + TIMER_O_TBILR) = TIMER_RV_TBILR;\r
1109     HWREG(ulBase + TIMER_O_TAMATCHR) = TIMER_RV_TAMATCHR;\r
1110     HWREG(ulBase + TIMER_O_TBMATCHR) = TIMER_RV_TBMATCHR;\r
1111     HWREG(ulBase + TIMER_O_TAPR) = TIMER_RV_TAPR;\r
1112     HWREG(ulBase + TIMER_O_TBPR) = TIMER_RV_TBPR;\r
1113     HWREG(ulBase + TIMER_O_TAPMR) = TIMER_RV_TAPMR;\r
1114     HWREG(ulBase + TIMER_O_TBPMR) = TIMER_RV_TBPMR;\r
1115     HWREG(ulBase + TIMER_O_TAR) = TIMER_RV_TAR;\r
1116     HWREG(ulBase + TIMER_O_TBR) = TIMER_RV_TBR;\r
1117 }\r
1118 #endif\r
1119 \r
1120 //*****************************************************************************\r
1121 //\r
1122 // Close the Doxygen group.\r
1123 //! @}\r
1124 //\r
1125 //*****************************************************************************\r