1 /*
2  *  Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *
8  *    Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  *    Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the
14  *    distribution.
15  *
16  *    Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 //*****************************************************************************
34 //
35 //  timer.c
36 //
37 //  Driver for the timer module.
38 //
39 //*****************************************************************************
40 
41 //*****************************************************************************
42 //
43 //! \addtogroup GPT_General_Purpose_Timer_api
44 //! @{
45 //
46 //*****************************************************************************
47 
48 #include "inc/hw_ints.h"
49 #include "inc/hw_memmap.h"
50 #include "inc/hw_timer.h"
51 #include "inc/hw_types.h"
52 #include "debug.h"
53 #include "interrupt.h"
54 #include "timer.h"
55 
56 
57 //*****************************************************************************
58 //
59 //! \internal
60 //! Checks a timer base address.
61 //!
62 //! \param ulBase is the base address of the timer module.
63 //!
64 //! This function determines if a timer module base address is valid.
65 //!
66 //! \return Returns \b true if the base address is valid and \b false
67 //! otherwise.
68 //
69 //*****************************************************************************
70 #ifdef DEBUG
71 static tBoolean
TimerBaseValid(unsigned long ulBase)72 TimerBaseValid(unsigned long ulBase)
73 {
74     return((ulBase == TIMERA0_BASE) || (ulBase == TIMERA1_BASE) ||
75            (ulBase == TIMERA2_BASE) || (ulBase == TIMERA3_BASE));
76 }
77 #endif
78 
79 //*****************************************************************************
80 //
81 //! Enables the timer(s).
82 //!
83 //! \param ulBase is the base address of the timer module.
84 //! \param ulTimer specifies the timer(s) to enable; must be one of \b TIMER_A,
85 //! \b TIMER_B, or \b TIMER_BOTH.
86 //!
87 //! This function enables operation of the timer module.  The timer must be
88 //! configured before it is enabled.
89 //!
90 //! \return None.
91 //
92 //*****************************************************************************
93 void
TimerEnable(unsigned long ulBase,unsigned long ulTimer)94 TimerEnable(unsigned long ulBase, unsigned long ulTimer)
95 {
96     //
97     // Check the arguments.
98     //
99     ASSERT(TimerBaseValid(ulBase));
100     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
101            (ulTimer == TIMER_BOTH));
102 
103     //
104     // Enable the timer(s) module.
105     //
106     HWREG(ulBase + TIMER_O_CTL) |= ulTimer & (TIMER_CTL_TAEN | TIMER_CTL_TBEN);
107 }
108 
109 //*****************************************************************************
110 //
111 //! Disables the timer(s).
112 //!
113 //! \param ulBase is the base address of the timer module.
114 //! \param ulTimer specifies the timer(s) to disable; must be one of
115 //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
116 //!
117 //! This function disables operation of the timer module.
118 //!
119 //! \return None.
120 //
121 //*****************************************************************************
122 void
TimerDisable(unsigned long ulBase,unsigned long ulTimer)123 TimerDisable(unsigned long ulBase, unsigned long ulTimer)
124 {
125     //
126     // Check the arguments.
127     //
128     ASSERT(TimerBaseValid(ulBase));
129     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
130            (ulTimer == TIMER_BOTH));
131 
132     //
133     // Disable the timer module.
134     //
135     HWREG(ulBase + TIMER_O_CTL) &= ~(ulTimer &
136                                      (TIMER_CTL_TAEN | TIMER_CTL_TBEN));
137 }
138 
139 //*****************************************************************************
140 //
141 //! Configures the timer(s).
142 //!
143 //! \param ulBase is the base address of the timer module.
144 //! \param ulConfig is the configuration for the timer.
145 //!
146 //! This function configures the operating mode of the timer(s).  The timer
147 //! module is disabled before being configured, and is left in the disabled
148 //! state. The 16/32-bit timer is comprised of two 16-bit timers that can
149 //! operate independently or be concatenated to form a 32-bit timer.
150 //!
151 //! The configuration is specified in \e ulConfig as one of the following
152 //! values:
153 //!
154 //! - \b TIMER_CFG_ONE_SHOT - Full-width one-shot timer
155 //! - \b TIMER_CFG_ONE_SHOT_UP - Full-width one-shot timer that counts up
156 //!   instead of down (not available on all parts)
157 //! - \b TIMER_CFG_PERIODIC - Full-width periodic timer
158 //! - \b TIMER_CFG_PERIODIC_UP - Full-width periodic timer that counts up
159 //!   instead of down (not available on all parts)
160 //! - \b TIMER_CFG_SPLIT_PAIR - Two half-width timers
161 //!
162 //! When configured for a pair of half-width timers, each timer is separately
163 //! configured.  The first timer is configured by setting \e ulConfig to
164 //! the result of a logical OR operation between one of the following values
165 //! and \e ulConfig:
166 //!
167 //! - \b TIMER_CFG_A_ONE_SHOT - Half-width one-shot timer
168 //! - \b TIMER_CFG_A_ONE_SHOT_UP - Half-width one-shot timer that counts up
169 //!   instead of down (not available on all parts)
170 //! - \b TIMER_CFG_A_PERIODIC - Half-width periodic timer
171 //! - \b TIMER_CFG_A_PERIODIC_UP - Half-width periodic timer that counts up
172 //!   instead of down (not available on all parts)
173 //! - \b TIMER_CFG_A_CAP_COUNT - Half-width edge count capture
174 //! - \b TIMER_CFG_A_CAP_TIME - Half-width edge time capture
175 //! - \b TIMER_CFG_A_PWM - Half-width PWM output
176 //!
177 //! Similarly, the second timer is configured by setting \e ulConfig to
178 //! the result of a logical OR operation between one of the corresponding
179 //! \b TIMER_CFG_B_* values and \e ulConfig.
180 //!
181 //! \return None.
182 //
183 //*****************************************************************************
184 void
TimerConfigure(unsigned long ulBase,unsigned long ulConfig)185 TimerConfigure(unsigned long ulBase, unsigned long ulConfig)
186 {
187 
188     ASSERT( (ulConfig == TIMER_CFG_ONE_SHOT) ||
189             (ulConfig == TIMER_CFG_ONE_SHOT_UP) ||
190             (ulConfig == TIMER_CFG_PERIODIC) ||
191             (ulConfig == TIMER_CFG_PERIODIC_UP) ||
192             (((ulConfig & 0xff000000) == TIMER_CFG_SPLIT_PAIR) &&
193             ((((ulConfig & 0x000000ff) == TIMER_CFG_A_ONE_SHOT) ||
194             ((ulConfig & 0x000000ff) == TIMER_CFG_A_ONE_SHOT_UP) ||
195             ((ulConfig & 0x000000ff) == TIMER_CFG_A_PERIODIC) ||
196             ((ulConfig & 0x000000ff) == TIMER_CFG_A_PERIODIC_UP) ||
197             ((ulConfig & 0x000000ff) == TIMER_CFG_A_CAP_COUNT) ||
198             ((ulConfig & 0x000000ff) == TIMER_CFG_A_CAP_TIME) ||
199             ((ulConfig & 0x000000ff) == TIMER_CFG_A_PWM)) ||
200             (((ulConfig & 0x0000ff00) == TIMER_CFG_B_ONE_SHOT) ||
201             ((ulConfig & 0x0000ff00) == TIMER_CFG_B_ONE_SHOT_UP) ||
202             ((ulConfig & 0x0000ff00) == TIMER_CFG_B_PERIODIC) ||
203             ((ulConfig & 0x0000ff00) == TIMER_CFG_B_PERIODIC_UP) ||
204             ((ulConfig & 0x0000ff00) == TIMER_CFG_B_CAP_COUNT) ||
205             ((ulConfig & 0x0000ff00) == TIMER_CFG_B_CAP_TIME) ||
206             ((ulConfig & 0x0000ff00) == TIMER_CFG_B_PWM)))));
207 
208 
209     //
210     // Enable CCP to IO path
211     //
212     HWREG(0x440260B0) = 0xFF;
213 
214     //
215     // Disable the timers.
216     //
217     HWREG(ulBase + TIMER_O_CTL) &= ~(TIMER_CTL_TAEN | TIMER_CTL_TBEN);
218 
219     //
220     // Set the global timer configuration.
221     //
222     HWREG(ulBase + TIMER_O_CFG) = ulConfig >> 24;
223 
224     //
225     // Set the configuration of the A and B timers.  Note that the B timer
226     // configuration is ignored by the hardware in 32-bit modes.
227     //
228     HWREG(ulBase + TIMER_O_TAMR) = ulConfig & 255;
229     HWREG(ulBase + TIMER_O_TBMR) = (ulConfig >> 8) & 255;
230 }
231 
232 //*****************************************************************************
233 //
234 //! Controls the output level.
235 //!
236 //! \param ulBase is the base address of the timer module.
237 //! \param ulTimer specifies the timer(s) to adjust; must be one of \b TIMER_A,
238 //! \b TIMER_B, or \b TIMER_BOTH.
239 //! \param bInvert specifies the output level.
240 //!
241 //! This function sets the PWM output level for the specified timer.  If the
242 //! \e bInvert parameter is \b true, then the timer's output is made active
243 //! low; otherwise, it is made active high.
244 //!
245 //! \return None.
246 //
247 //*****************************************************************************
248 void
TimerControlLevel(unsigned long ulBase,unsigned long ulTimer,tBoolean bInvert)249 TimerControlLevel(unsigned long ulBase, unsigned long ulTimer,
250                   tBoolean bInvert)
251 {
252     //
253     // Check the arguments.
254     //
255     ASSERT(TimerBaseValid(ulBase));
256     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
257            (ulTimer == TIMER_BOTH));
258 
259     //
260     // Set the output levels as requested.
261     //
262     ulTimer &= TIMER_CTL_TAPWML | TIMER_CTL_TBPWML;
263     HWREG(ulBase + TIMER_O_CTL) = (bInvert ?
264                                    (HWREG(ulBase + TIMER_O_CTL) | ulTimer) :
265                                    (HWREG(ulBase + TIMER_O_CTL) & ~(ulTimer)));
266 }
267 
268 //*****************************************************************************
269 //
270 //! Controls the event type.
271 //!
272 //! \param ulBase is the base address of the timer module.
273 //! \param ulTimer specifies the timer(s) to be adjusted; must be one of
274 //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
275 //! \param ulEvent specifies the type of event; must be one of
276 //! \b TIMER_EVENT_POS_EDGE, \b TIMER_EVENT_NEG_EDGE, or
277 //! \b TIMER_EVENT_BOTH_EDGES.
278 //!
279 //! This function sets the signal edge(s) that triggers the timer when in
280 //! capture mode.
281 //!
282 //! \return None.
283 //
284 //*****************************************************************************
285 void
TimerControlEvent(unsigned long ulBase,unsigned long ulTimer,unsigned long ulEvent)286 TimerControlEvent(unsigned long ulBase, unsigned long ulTimer,
287                   unsigned long ulEvent)
288 {
289     //
290     // Check the arguments.
291     //
292     ASSERT(TimerBaseValid(ulBase));
293     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
294            (ulTimer == TIMER_BOTH));
295 
296     //
297     // Set the event type.
298     //
299     ulEvent &= ulTimer & (TIMER_CTL_TAEVENT_M | TIMER_CTL_TBEVENT_M);
300     HWREG(ulBase + TIMER_O_CTL) = ((HWREG(ulBase + TIMER_O_CTL) &
301                                     ~(TIMER_CTL_TAEVENT_M |
302                                       TIMER_CTL_TBEVENT_M)) | ulEvent);
303 }
304 
305 //*****************************************************************************
306 //
307 //! Controls the stall handling.
308 //!
309 //! \param ulBase is the base address of the timer module.
310 //! \param ulTimer specifies the timer(s) to be adjusted; must be one of
311 //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
312 //! \param bStall specifies the response to a stall signal.
313 //!
314 //! This function controls the stall response for the specified timer.  If the
315 //! \e bStall parameter is \b true, then the timer stops counting if the
316 //! processor enters debug mode; otherwise the timer keeps running while in
317 //! debug mode.
318 //!
319 //! \return None.
320 //
321 //*****************************************************************************
322 void
TimerControlStall(unsigned long ulBase,unsigned long ulTimer,tBoolean bStall)323 TimerControlStall(unsigned long ulBase, unsigned long ulTimer,
324                   tBoolean  bStall)
325 {
326     //
327     // Check the arguments.
328     //
329     ASSERT(TimerBaseValid(ulBase));
330     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
331            (ulTimer == TIMER_BOTH));
332 
333     //
334     // Set the stall mode.
335     //
336     ulTimer &= TIMER_CTL_TASTALL | TIMER_CTL_TBSTALL;
337     HWREG(ulBase + TIMER_O_CTL) = (bStall ?
338                                    (HWREG(ulBase + TIMER_O_CTL) | ulTimer) :
339                                    (HWREG(ulBase + TIMER_O_CTL) & ~(ulTimer)));
340 }
341 
342 //*****************************************************************************
343 //
344 //! Set the timer prescale value.
345 //!
346 //! \param ulBase is the base address of the timer module.
347 //! \param ulTimer specifies the timer(s) to adjust; must be one of \b TIMER_A,
348 //! \b TIMER_B, or \b TIMER_BOTH.
349 //! \param ulValue is the timer prescale value which must be between 0 and 255
350 //! (inclusive) for 16/32-bit timers.
351 //!
352 //! This function sets the value of the input clock prescaler.  The prescaler
353 //! is only operational when in half-width mode and is used to extend the range
354 //! of the half-width timer modes.
355 //!
356 //! \return None.
357 //
358 //*****************************************************************************
359 void
TimerPrescaleSet(unsigned long ulBase,unsigned long ulTimer,unsigned long ulValue)360 TimerPrescaleSet(unsigned long ulBase, unsigned long ulTimer,
361                  unsigned long ulValue)
362 {
363     //
364     // Check the arguments.
365     //
366     ASSERT(TimerBaseValid(ulBase));
367     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
368            (ulTimer == TIMER_BOTH));
369     ASSERT(ulValue < 256);
370 
371     //
372     // Set the timer A prescaler if requested.
373     //
374     if(ulTimer & TIMER_A)
375     {
376         HWREG(ulBase + TIMER_O_TAPR) = ulValue;
377     }
378 
379     //
380     // Set the timer B prescaler if requested.
381     //
382     if(ulTimer & TIMER_B)
383     {
384         HWREG(ulBase + TIMER_O_TBPR) = ulValue;
385     }
386 }
387 
388 
389 //*****************************************************************************
390 //
391 //! Get the timer prescale value.
392 //!
393 //! \param ulBase is the base address of the timer module.
394 //! \param ulTimer specifies the timer; must be one of \b TIMER_A or
395 //! \b TIMER_B.
396 //!
397 //! This function gets the value of the input clock prescaler.  The prescaler
398 //! is only operational when in half-width mode and is used to extend the range
399 //! of the half-width timer modes.
400 //!
401 //! \return The value of the timer prescaler.
402 //
403 //*****************************************************************************
404 
405 unsigned long
TimerPrescaleGet(unsigned long ulBase,unsigned long ulTimer)406 TimerPrescaleGet(unsigned long ulBase, unsigned long ulTimer)
407 {
408     //
409     // Check the arguments.
410     //
411     ASSERT(TimerBaseValid(ulBase));
412     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
413            (ulTimer == TIMER_BOTH));
414 
415     //
416     // Return the appropriate prescale value.
417     //
418     return((ulTimer == TIMER_A) ? HWREG(ulBase + TIMER_O_TAPR) :
419            HWREG(ulBase + TIMER_O_TBPR));
420 }
421 
422 //*****************************************************************************
423 //
424 //! Set the timer prescale match value.
425 //!
426 //! \param ulBase is the base address of the timer module.
427 //! \param ulTimer specifies the timer(s) to adjust; must be one of \b TIMER_A,
428 //! \b TIMER_B, or \b TIMER_BOTH.
429 //! \param ulValue is the timer prescale match value which must be between 0
430 //! and 255 (inclusive) for 16/32-bit timers.
431 //!
432 //! This function sets the value of the input clock prescaler match value.
433 //! When in a half-width mode that uses the counter match and the prescaler,
434 //! the prescale match effectively extends the range of the match.
435 //!
436 //! \note The availability of the prescaler match varies with the
437 //! part and timer mode in use.  Please consult the datasheet for the part you
438 //! are using to determine whether this support is available.
439 //!
440 //! \return None.
441 //
442 //*****************************************************************************
443 void
TimerPrescaleMatchSet(unsigned long ulBase,unsigned long ulTimer,unsigned long ulValue)444 TimerPrescaleMatchSet(unsigned long ulBase, unsigned long ulTimer,
445                       unsigned long ulValue)
446 {
447     //
448     // Check the arguments.
449     //
450     ASSERT(TimerBaseValid(ulBase));
451     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
452            (ulTimer == TIMER_BOTH));
453     ASSERT(ulValue < 256);
454 
455     //
456     // Set the timer A prescale match if requested.
457     //
458     if(ulTimer & TIMER_A)
459     {
460         HWREG(ulBase + TIMER_O_TAPMR) = ulValue;
461     }
462 
463     //
464     // Set the timer B prescale match if requested.
465     //
466     if(ulTimer & TIMER_B)
467     {
468         HWREG(ulBase + TIMER_O_TBPMR) = ulValue;
469     }
470 }
471 
472 //*****************************************************************************
473 //
474 //! Get the timer prescale match value.
475 //!
476 //! \param ulBase is the base address of the timer module.
477 //! \param ulTimer specifies the timer; must be one of \b TIMER_A or
478 //! \b TIMER_B.
479 //!
480 //! This function gets the value of the input clock prescaler match value.
481 //! When in a half-width mode that uses the counter match and prescaler, the
482 //! prescale match effectively extends the range of the match.
483 //!
484 //! \note The availability of the prescaler match varies with the
485 //! part and timer mode in use.  Please consult the datasheet for the part you
486 //! are using to determine whether this support is available.
487 //!
488 //! \return The value of the timer prescale match.
489 //
490 //*****************************************************************************
491 unsigned long
TimerPrescaleMatchGet(unsigned long ulBase,unsigned long ulTimer)492 TimerPrescaleMatchGet(unsigned long ulBase, unsigned long ulTimer)
493 {
494     //
495     // Check the arguments.
496     //
497     ASSERT(TimerBaseValid(ulBase));
498     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
499            (ulTimer == TIMER_BOTH));
500 
501     //
502     // Return the appropriate prescale match value.
503     //
504     return((ulTimer == TIMER_A) ? HWREG(ulBase + TIMER_O_TAPMR) :
505            HWREG(ulBase + TIMER_O_TBPMR));
506 }
507 
508 //*****************************************************************************
509 //
510 //! Sets the timer load value.
511 //!
512 //! \param ulBase is the base address of the timer module.
513 //! \param ulTimer specifies the timer(s) to adjust; must be one of \b TIMER_A,
514 //! \b TIMER_B, or \b TIMER_BOTH.  Only \b TIMER_A should be used when the
515 //! timer is configured for full-width operation.
516 //! \param ulValue is the load value.
517 //!
518 //! This function sets the timer load value; if the timer is running then the
519 //! value is immediately loaded into the timer.
520 //!
521 //! \note This function can be used for both full- and half-width modes of
522 //! 16/32-bit timers.
523 //!
524 //! \return None.
525 //
526 //*****************************************************************************
527 void
TimerLoadSet(unsigned long ulBase,unsigned long ulTimer,unsigned long ulValue)528 TimerLoadSet(unsigned long ulBase, unsigned long ulTimer,
529              unsigned long ulValue)
530 {
531     //
532     // Check the arguments.
533     //
534     ASSERT(TimerBaseValid(ulBase));
535     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
536            (ulTimer == TIMER_BOTH));
537 
538     //
539     // Set the timer A load value if requested.
540     //
541     if(ulTimer & TIMER_A)
542     {
543         HWREG(ulBase + TIMER_O_TAILR) = ulValue;
544     }
545 
546     //
547     // Set the timer B load value if requested.
548     //
549     if(ulTimer & TIMER_B)
550     {
551         HWREG(ulBase + TIMER_O_TBILR) = ulValue;
552     }
553 }
554 
555 //*****************************************************************************
556 //
557 //! Gets the timer load value.
558 //!
559 //! \param ulBase is the base address of the timer module.
560 //! \param ulTimer specifies the timer; must be one of \b TIMER_A or
561 //! \b TIMER_B.  Only \b TIMER_A should be used when the timer is configured
562 //! for full-width operation.
563 //!
564 //! This function gets the currently programmed interval load value for the
565 //! specified timer.
566 //!
567 //! \note This function can be used for both full- and half-width modes of
568 //! 16/32-bit timers.
569 //!
570 //! \return Returns the load value for the timer.
571 //
572 //*****************************************************************************
573 unsigned long
TimerLoadGet(unsigned long ulBase,unsigned long ulTimer)574 TimerLoadGet(unsigned long ulBase, unsigned long ulTimer)
575 {
576     //
577     // Check the arguments.
578     //
579     ASSERT(TimerBaseValid(ulBase));
580     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B));
581 
582     //
583     // Return the appropriate load value.
584     //
585     return((ulTimer == TIMER_A) ? HWREG(ulBase + TIMER_O_TAILR) :
586            HWREG(ulBase + TIMER_O_TBILR));
587 }
588 
589 //*****************************************************************************
590 //
591 //! Gets the current timer value.
592 //!
593 //! \param ulBase is the base address of the timer module.
594 //! \param ulTimer specifies the timer; must be one of \b TIMER_A or
595 //! \b TIMER_B.  Only \b TIMER_A should be used when the timer is configured
596 //! for 32-bit operation.
597 //!
598 //! This function reads the current value of the specified timer.
599 //!
600 //! \return Returns the current value of the timer.
601 //
602 //*****************************************************************************
603 unsigned long
TimerValueGet(unsigned long ulBase,unsigned long ulTimer)604 TimerValueGet(unsigned long ulBase, unsigned long ulTimer)
605 {
606     //
607     // Check the arguments.
608     //
609     ASSERT(TimerBaseValid(ulBase));
610     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B));
611 
612     //
613     // Return the appropriate timer value.
614     //
615     return((ulTimer == TIMER_A) ? HWREG(ulBase + TIMER_O_TAR) :
616            HWREG(ulBase + TIMER_O_TBR));
617 }
618 
619 //*****************************************************************************
620 //
621 //! Sets the current timer value.
622 //!
623 //! \param ulBase is the base address of the timer module.
624 //! \param ulTimer specifies the timer; must be one of \b TIMER_A or
625 //! \b TIMER_B.  Only \b TIMER_A should be used when the timer is configured
626 //! for 32-bit operation.
627 //! \param ulValue is the new value of the timer to be set.
628 //!
629 //! This function sets the current value of the specified timer.
630 //!
631 //! \return None.
632 //
633 //*****************************************************************************
634 void
TimerValueSet(unsigned long ulBase,unsigned long ulTimer,unsigned long ulValue)635 TimerValueSet(unsigned long ulBase, unsigned long ulTimer,
636               unsigned long ulValue)
637 {
638     //
639     // Check the arguments.
640     //
641     ASSERT(TimerBaseValid(ulBase));
642     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B));
643 
644     //
645     // Set the appropriate timer value.
646     //
647     if(ulTimer == TIMER_A)
648     {
649       HWREG(ulBase + TIMER_O_TAV) = ulValue;
650     }
651     else
652     {
653       HWREG(ulBase + TIMER_O_TBV) = ulValue;
654     }
655 }
656 
657 
658 //*****************************************************************************
659 //
660 //! Sets the timer match value.
661 //!
662 //! \param ulBase is the base address of the timer module.
663 //! \param ulTimer specifies the timer(s) to adjust; must be one of \b TIMER_A,
664 //! \b TIMER_B, or \b TIMER_BOTH.  Only \b TIMER_A should be used when the
665 //! timer is configured for 32-bit operation.
666 //! \param ulValue is the match value.
667 //!
668 //! This function sets the match value for a timer.  This is used in capture
669 //! count mode to determine when to interrupt the processor and in PWM mode to
670 //! determine the duty cycle of the output signal.
671 //!
672 //! \return None.
673 //
674 //*****************************************************************************
675 void
TimerMatchSet(unsigned long ulBase,unsigned long ulTimer,unsigned long ulValue)676 TimerMatchSet(unsigned long ulBase, unsigned long ulTimer,
677               unsigned long ulValue)
678 {
679     //
680     // Check the arguments.
681     //
682     ASSERT(TimerBaseValid(ulBase));
683     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
684            (ulTimer == TIMER_BOTH));
685 
686     //
687     // Set the timer A match value if requested.
688     //
689     if(ulTimer & TIMER_A)
690     {
691         HWREG(ulBase + TIMER_O_TAMATCHR) = ulValue;
692     }
693 
694     //
695     // Set the timer B match value if requested.
696     //
697     if(ulTimer & TIMER_B)
698     {
699         HWREG(ulBase + TIMER_O_TBMATCHR) = ulValue;
700     }
701 }
702 
703 //*****************************************************************************
704 //
705 //! Gets the timer match value.
706 //!
707 //! \param ulBase is the base address of the timer module.
708 //! \param ulTimer specifies the timer; must be one of \b TIMER_A or
709 //! \b TIMER_B.  Only \b TIMER_A should be used when the timer is configured
710 //! for 32-bit operation.
711 //!
712 //! This function gets the match value for the specified timer.
713 //!
714 //! \return Returns the match value for the timer.
715 //
716 //********************************************************************************
717 unsigned long
TimerMatchGet(unsigned long ulBase,unsigned long ulTimer)718 TimerMatchGet(unsigned long ulBase, unsigned long ulTimer)
719 {
720     //
721     // Check the arguments.
722     //
723     ASSERT(TimerBaseValid(ulBase));
724     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B));
725 
726     //
727     // Return the appropriate match value.
728     //
729     return((ulTimer == TIMER_A) ? HWREG(ulBase + TIMER_O_TAMATCHR) :
730            HWREG(ulBase + TIMER_O_TBMATCHR));
731 }
732 
733 
734 //*****************************************************************************
735 //
736 //! Registers an interrupt handler for the timer interrupt.
737 //!
738 //! \param ulBase is the base address of the timer module.
739 //! \param ulTimer specifies the timer(s); must be one of \b TIMER_A,
740 //! \b TIMER_B, or \b TIMER_BOTH.
741 //! \param pfnHandler is a pointer to the function to be called when the timer
742 //! interrupt occurs.
743 //!
744 //! This function sets the handler to be called when a timer interrupt occurs.
745 //! In addition, this function enables the global interrupt in the interrupt
746 //! controller; specific timer interrupts must be enabled via TimerIntEnable().
747 //! It is the interrupt handler's responsibility to clear the interrupt source
748 //! via TimerIntClear().
749 //!
750 //! \sa IntRegister() for important information about registering interrupt
751 //! handlers.
752 //!
753 //! \return None.
754 //
755 //*****************************************************************************
756 void
TimerIntRegister(unsigned long ulBase,unsigned long ulTimer,void (* pfnHandler)(void))757 TimerIntRegister(unsigned long ulBase, unsigned long ulTimer,
758                  void (*pfnHandler)(void))
759 {
760     //
761     // Check the arguments.
762     //
763     ASSERT(TimerBaseValid(ulBase));
764     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
765            (ulTimer == TIMER_BOTH));
766 
767     ulBase = ((ulBase == TIMERA0_BASE) ? INT_TIMERA0A :
768               ((ulBase == TIMERA1_BASE) ? INT_TIMERA1A :
769                ((ulBase == TIMERA2_BASE) ? INT_TIMERA2A : INT_TIMERA3A)));
770 
771     //
772     // Register an interrupt handler for timer A if requested.
773     //
774     if(ulTimer & TIMER_A)
775     {
776         //
777         // Register the interrupt handler.
778         //
779         IntRegister(ulBase, pfnHandler);
780 
781         //
782         // Enable the interrupt.
783         //
784         IntEnable(ulBase);
785     }
786 
787     //
788     // Register an interrupt handler for timer B if requested.
789     //
790     if(ulTimer & TIMER_B)
791     {
792         //
793         // Register the interrupt handler.
794         //
795         IntRegister(ulBase + 1, pfnHandler);
796 
797         //
798         // Enable the interrupt.
799         //
800         IntEnable(ulBase + 1);
801     }
802 }
803 
804 //*****************************************************************************
805 //
806 //! Unregisters an interrupt handler for the timer interrupt.
807 //!
808 //! \param ulBase is the base address of the timer module.
809 //! \param ulTimer specifies the timer(s); must be one of \b TIMER_A,
810 //! \b TIMER_B, or \b TIMER_BOTH.
811 //!
812 //! This function clears the handler to be called when a timer interrupt
813 //! occurs.  This function also masks off the interrupt in the interrupt
814 //! controller so that the interrupt handler no longer is called.
815 //!
816 //! \sa IntRegister() for important information about registering interrupt
817 //! handlers.
818 //!
819 //! \return None.
820 //
821 //*****************************************************************************
822 void
TimerIntUnregister(unsigned long ulBase,unsigned long ulTimer)823 TimerIntUnregister(unsigned long ulBase, unsigned long ulTimer)
824 {
825     //
826     // Check the arguments.
827     //
828     ASSERT(TimerBaseValid(ulBase));
829     ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
830            (ulTimer == TIMER_BOTH));
831 
832     //
833     // Get the interrupt number for this timer module.
834     //
835 
836     ulBase = ((ulBase == TIMERA0_BASE) ? INT_TIMERA0A :
837               ((ulBase == TIMERA1_BASE) ? INT_TIMERA1A :
838                ((ulBase == TIMERA2_BASE) ? INT_TIMERA2A : INT_TIMERA3A)));
839 
840 
841 
842     //
843     // Unregister the interrupt handler for timer A if requested.
844     //
845     if(ulTimer & TIMER_A)
846     {
847         //
848         // Disable the interrupt.
849         //
850         IntDisable(ulBase);
851 
852         //
853         // Unregister the interrupt handler.
854         //
855         IntUnregister(ulBase);
856     }
857 
858     //
859     // Unregister the interrupt handler for timer B if requested.
860     //
861     if(ulTimer & TIMER_B)
862     {
863         //
864         // Disable the interrupt.
865         //
866         IntDisable(ulBase + 1);
867 
868         //
869         // Unregister the interrupt handler.
870         //
871         IntUnregister(ulBase + 1);
872     }
873 }
874 
875 //*****************************************************************************
876 //
877 //! Enables individual timer interrupt sources.
878 //!
879 //! \param ulBase is the base address of the timer module.
880 //! \param ulIntFlags is the bit mask of the interrupt sources to be enabled.
881 //!
882 //! Enables the indicated timer interrupt sources.  Only the sources that are
883 //! enabled can be reflected to the processor interrupt; disabled sources have
884 //! no effect on the processor.
885 //!
886 //! The \e ulIntFlags parameter must be the logical OR of any combination of
887 //! the following:
888 //!
889 //! - \b TIMER_CAPB_EVENT  - Capture B event interrupt
890 //! - \b TIMER_CAPB_MATCH  - Capture B match interrupt
891 //! - \b TIMER_TIMB_TIMEOUT  - Timer B timeout interrupt
892 //! - \b TIMER_CAPA_EVENT  - Capture A event interrupt
893 //! - \b TIMER_CAPA_MATCH  - Capture A match interrupt
894 //! - \b TIMER_TIMA_TIMEOUT  - Timer A timeout interrupt
895 //!
896 //! \return None.
897 //
898 //*****************************************************************************
899 void
TimerIntEnable(unsigned long ulBase,unsigned long ulIntFlags)900 TimerIntEnable(unsigned long ulBase, unsigned long ulIntFlags)
901 {
902     //
903     // Check the arguments.
904     //
905     ASSERT(TimerBaseValid(ulBase));
906 
907     //
908     // Enable the specified interrupts.
909     //
910     HWREG(ulBase + TIMER_O_IMR) |= ulIntFlags;
911 }
912 
913 //*****************************************************************************
914 //
915 //! Disables individual timer interrupt sources.
916 //!
917 //! \param ulBase is the base address of the timer module.
918 //! \param ulIntFlags is the bit mask of the interrupt sources to be disabled.
919 //!
920 //! Disables the indicated timer interrupt sources.  Only the sources that are
921 //! enabled can be reflected to the processor interrupt; disabled sources have
922 //! no effect on the processor.
923 //!
924 //! The \e ulIntFlags parameter has the same definition as the \e ulIntFlags
925 //! parameter to TimerIntEnable().
926 //!
927 //! \return None.
928 //
929 //*****************************************************************************
930 void
TimerIntDisable(unsigned long ulBase,unsigned long ulIntFlags)931 TimerIntDisable(unsigned long ulBase, unsigned long ulIntFlags)
932 {
933     //
934     // Check the arguments.
935     //
936     ASSERT(TimerBaseValid(ulBase));
937 
938     //
939     // Disable the specified interrupts.
940     //
941     HWREG(ulBase + TIMER_O_IMR) &= ~(ulIntFlags);
942 }
943 
944 //*****************************************************************************
945 //
946 //! Gets the current interrupt status.
947 //!
948 //! \param ulBase is the base address of the timer module.
949 //! \param bMasked is false if the raw interrupt status is required and true if
950 //! the masked interrupt status is required.
951 //!
952 //! This function returns the interrupt status for the timer module.  Either
953 //! the raw interrupt status or the status of interrupts that are allowed to
954 //! reflect to the processor can be returned.
955 //!
956 //! \return The current interrupt status, enumerated as a bit field of
957 //! values described in TimerIntEnable().
958 //
959 //*****************************************************************************
960 unsigned long
TimerIntStatus(unsigned long ulBase,tBoolean bMasked)961 TimerIntStatus(unsigned long ulBase, tBoolean bMasked)
962 {
963     //
964     // Check the arguments.
965     //
966     ASSERT(TimerBaseValid(ulBase));
967 
968     //
969     // Return either the interrupt status or the raw interrupt status as
970     // requested.
971     //
972     return(bMasked ? HWREG(ulBase + TIMER_O_MIS) :
973            HWREG(ulBase + TIMER_O_RIS));
974 }
975 
976 //*****************************************************************************
977 //
978 //! Clears timer interrupt sources.
979 //!
980 //! \param ulBase is the base address of the timer module.
981 //! \param ulIntFlags is a bit mask of the interrupt sources to be cleared.
982 //!
983 //! The specified timer interrupt sources are cleared, so that they no longer
984 //! assert.  This function must be called in the interrupt handler to keep the
985 //! interrupt from being triggered again immediately upon exit.
986 //!
987 //! The \e ulIntFlags parameter has the same definition as the \e ulIntFlags
988 //! parameter to TimerIntEnable().
989 //!
990 //! \note Because there is a write buffer in the Cortex-M3 processor, it may
991 //! take several clock cycles before the interrupt source is actually cleared.
992 //! Therefore, it is recommended that the interrupt source be cleared early in
993 //! the interrupt handler (as opposed to the very last action) to avoid
994 //! returning from the interrupt handler before the interrupt source is
995 //! actually cleared.  Failure to do so may result in the interrupt handler
996 //! being immediately reentered (because the interrupt controller still sees
997 //! the interrupt source asserted).
998 //!
999 //! \return None.
1000 //
1001 //*****************************************************************************
1002 void
TimerIntClear(unsigned long ulBase,unsigned long ulIntFlags)1003 TimerIntClear(unsigned long ulBase, unsigned long ulIntFlags)
1004 {
1005     //
1006     // Check the arguments.
1007     //
1008     ASSERT(TimerBaseValid(ulBase));
1009 
1010     //
1011     // Clear the requested interrupt sources.
1012     //
1013     HWREG(ulBase + TIMER_O_ICR) = ulIntFlags;
1014 }
1015 
1016 //*****************************************************************************
1017 //
1018 //! Enables the events that can trigger a DMA request.
1019 //!
1020 //! \param ulBase is the base address of the timer module.
1021 //! \param ulDMAEvent is a bit mask of the events that can trigger DMA.
1022 //!
1023 //! This function enables the timer events that can trigger the start of a DMA
1024 //! sequence.  The DMA trigger events are specified in the \e ui32DMAEvent
1025 //! parameter by passing in the logical OR of the following values:
1026 //!
1027 //! - \b TIMER_DMA_MODEMATCH_B - The mode match DMA trigger for timer B is
1028 //!   enabled.
1029 //! - \b TIMER_DMA_CAPEVENT_B - The capture event DMA trigger for timer B is
1030 //!   enabled.
1031 //! - \b TIMER_DMA_CAPMATCH_B - The capture match DMA trigger for timer B is
1032 //!   enabled.
1033 //! - \b TIMER_DMA_TIMEOUT_B - The timeout DMA trigger for timer B is enabled.
1034 //! - \b TIMER_DMA_MODEMATCH_A - The mode match DMA trigger for timer A is
1035 //!   enabled.
1036 //! - \b TIMER_DMA_CAPEVENT_A - The capture event DMA trigger for timer A is
1037 //!   enabled.
1038 //! - \b TIMER_DMA_CAPMATCH_A - The capture match DMA trigger for timer A is
1039 //!   enabled.
1040 //! - \b TIMER_DMA_TIMEOUT_A - The timeout DMA trigger for timer A is enabled.
1041 //!
1042 //! \return None.
1043 //
1044 //*****************************************************************************
1045 void
TimerDMAEventSet(unsigned long ulBase,unsigned long ulDMAEvent)1046 TimerDMAEventSet(unsigned long ulBase, unsigned long ulDMAEvent)
1047 {
1048     //
1049     // Check the arguments.
1050     //
1051     ASSERT(TimerBaseValid(ulBase));
1052 
1053     //
1054     // Set the DMA triggers.
1055     //
1056     HWREG(ulBase + TIMER_O_DMAEV) = ulDMAEvent;
1057 }
1058 
1059 //*****************************************************************************
1060 //
1061 //! Returns the events that can trigger a DMA request.
1062 //!
1063 //! \param ulBase is the base address of the timer module.
1064 //!
1065 //! This function returns the timer events that can trigger the start of a DMA
1066 //! sequence.  The DMA trigger events are the logical OR of the following
1067 //! values:
1068 //!
1069 //! - \b TIMER_DMA_MODEMATCH_B - Enables the mode match DMA trigger for timer
1070 //!   B.
1071 //! - \b TIMER_DMA_CAPEVENT_B - Enables the capture event DMA trigger for
1072 //!   timer B.
1073 //! - \b TIMER_DMA_CAPMATCH_B - Enables the capture match DMA trigger for
1074 //!   timer B.
1075 //! - \b TIMER_DMA_TIMEOUT_B - Enables the timeout DMA trigger for timer B.
1076 //! - \b TIMER_DMA_MODEMATCH_A - Enables the mode match DMA trigger for timer
1077 //!   A.
1078 //! - \b TIMER_DMA_CAPEVENT_A - Enables the capture event DMA trigger for
1079 //!   timer A.
1080 //! - \b TIMER_DMA_CAPMATCH_A - Enables the capture match DMA trigger for
1081 //!   timer A.
1082 //! - \b TIMER_DMA_TIMEOUT_A - Enables the timeout DMA trigger for timer A.
1083 //!
1084 //! \return The timer events that trigger the uDMA.
1085 //
1086 //*****************************************************************************
1087 unsigned long
TimerDMAEventGet(unsigned long ulBase)1088 TimerDMAEventGet(unsigned long ulBase)
1089 {
1090     //
1091     // Check the arguments.
1092     //
1093     ASSERT(TimerBaseValid(ulBase));
1094 
1095     //
1096     // Return the current DMA triggers.
1097     //
1098     return(HWREG(ulBase + TIMER_O_DMAEV));
1099 }
1100 //*****************************************************************************
1101 //
1102 // Close the Doxygen group.
1103 //! @}
1104 //
1105 //*****************************************************************************
1106