1 //*****************************************************************************
2 //
3 // timer.c - Driver for the timer module.
4 //
5 // Copyright (c) 2005-2017 Texas Instruments Incorporated.  All rights reserved.
6 // Software License Agreement
7 //
8 //   Redistribution and use in source and binary forms, with or without
9 //   modification, are permitted provided that the following conditions
10 //   are met:
11 //
12 //   Redistributions of source code must retain the above copyright
13 //   notice, this list of conditions and the following disclaimer.
14 //
15 //   Redistributions in binary form must reproduce the above copyright
16 //   notice, this list of conditions and the following disclaimer in the
17 //   documentation and/or other materials provided with the
18 //   distribution.
19 //
20 //   Neither the name of Texas Instruments Incorporated nor the names of
21 //   its contributors may be used to endorse or promote products derived
22 //   from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 //
36 //*****************************************************************************
37 
38 //*****************************************************************************
39 //
40 //! \addtogroup timer_api
41 //! @{
42 //
43 //*****************************************************************************
44 
45 #include <ti/devices/msp432e4/inc/msp432e411y.h>
46 #include "types.h"
47 #include <stdbool.h>
48 #include <stdint.h>
49 #include "inc/hw_timer.h"
50 #include "inc/hw_sysctl.h"
51 #include "debug.h"
52 #include "interrupt.h"
53 #include "timer.h"
54 
55 //*****************************************************************************
56 //
57 // A mapping of timer base address to interrupt number.
58 //
59 //*****************************************************************************
60 static const uint32_t g_ppui32TimerIntMap[][2] =
61 {
62     { TIMER0_BASE, INT_TIMER0A },
63     { TIMER1_BASE, INT_TIMER1A },
64     { TIMER2_BASE, INT_TIMER2A },
65     { TIMER3_BASE, INT_TIMER3A },
66     { TIMER4_BASE, INT_TIMER4A },
67     { TIMER5_BASE, INT_TIMER5A },
68     { TIMER6_BASE, INT_TIMER6A },
69     { TIMER7_BASE, INT_TIMER7A },
70 };
71 static const uint_fast8_t g_ui8TimerIntMapRows =
72     sizeof(g_ppui32TimerIntMap) / sizeof(g_ppui32TimerIntMap[0]);
73 
74 //*****************************************************************************
75 //
76 //! \internal
77 //! Checks a timer base address.
78 //!
79 //! \param ui32Base is the base address of the timer module.
80 //!
81 //! This function determines if a timer module base address is valid.
82 //!
83 //! \return Returns \b true if the base address is valid and \b false
84 //! otherwise.
85 //
86 //*****************************************************************************
87 #ifdef DEBUG
88 static bool
_TimerBaseValid(uint32_t ui32Base)89 _TimerBaseValid(uint32_t ui32Base)
90 {
91     return ((ui32Base == TIMER0_BASE) || (ui32Base == TIMER1_BASE) ||
92             (ui32Base == TIMER2_BASE) || (ui32Base == TIMER3_BASE) ||
93             (ui32Base == TIMER4_BASE) || (ui32Base == TIMER5_BASE) ||
94             (ui32Base == TIMER6_BASE) || (ui32Base == TIMER7_BASE));
95 }
96 #endif
97 
98 //*****************************************************************************
99 //
100 //! Returns a timer modules interrupt number.
101 //!
102 //! \param ui32Base is the base address of the selected timer.
103 //! \param ui32Timer specifies the timer(s) to enable; must be one of
104 //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
105 //!
106 //! This function returns the interrupt number for a given timer module
107 //! specified by the \e ui32Base and \e ui32Timer parameter.
108 //!
109 //! \return Returns a timer module's interrupt number or 0 if the interrupt
110 //! does not exist.
111 //
112 //*****************************************************************************
113 static uint32_t
_TimerIntNumberGet(uint32_t ui32Base,uint32_t ui32Timer)114 _TimerIntNumberGet(uint32_t ui32Base, uint32_t ui32Timer)
115 {
116     uint32_t ui32Int;
117     uint_fast8_t ui8Idx, ui8Rows;
118     const uint32_t (*ppui32SSIIntMap)[2];
119 
120     //
121     // Default interrupt map.
122     //
123     ppui32SSIIntMap = g_ppui32TimerIntMap;
124     ui8Rows = g_ui8TimerIntMapRows;
125 
126     //
127     // Loop through the table that maps timer base addresses to interrupt
128     // numbers.
129     //
130     for (ui8Idx = 0; ui8Idx < ui8Rows; ui8Idx++)
131     {
132         //
133         // See if this base address matches.
134         //
135         if (ppui32SSIIntMap[ui8Idx][0] == ui32Base)
136         {
137             ui32Int = ppui32SSIIntMap[ui8Idx][1];
138 
139             if (ui32Timer == TIMER_B)
140             {
141                 ui32Int += 1;
142             }
143 
144             //
145             // Return the corresponding interrupt number.
146             //
147             return (ui32Int);
148         }
149     }
150 
151     //
152     // The base address could not be found, so return an error.
153     //
154     return (0);
155 }
156 
157 //*****************************************************************************
158 //
159 //! Enables the timer(s).
160 //!
161 //! \param ui32Base is the base address of the timer module.
162 //! \param ui32Timer specifies the timer(s) to enable; must be one of
163 //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
164 //!
165 //! This function enables operation of the timer module.  The timer must be
166 //! configured before it is enabled.
167 //!
168 //! \return None.
169 //
170 //*****************************************************************************
171 void
TimerEnable(uint32_t ui32Base,uint32_t ui32Timer)172 TimerEnable(uint32_t ui32Base, uint32_t ui32Timer)
173 {
174     //
175     // Check the arguments.
176     //
177     ASSERT(_TimerBaseValid(ui32Base));
178     ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
179            (ui32Timer == TIMER_BOTH));
180 
181     //
182     // Enable the timer(s) module.
183     //
184     HWREG(ui32Base + TIMER_O_CTL) |= ui32Timer & (TIMER_CTL_TAEN |
185                                      TIMER_CTL_TBEN);
186 }
187 
188 //*****************************************************************************
189 //
190 //! Disables the timer(s).
191 //!
192 //! \param ui32Base is the base address of the timer module.
193 //! \param ui32Timer specifies the timer(s) to disable; must be one of
194 //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
195 //!
196 //! This function disables operation of the timer module.
197 //!
198 //! \return None.
199 //
200 //*****************************************************************************
201 void
TimerDisable(uint32_t ui32Base,uint32_t ui32Timer)202 TimerDisable(uint32_t ui32Base, uint32_t ui32Timer)
203 {
204     //
205     // Check the arguments.
206     //
207     ASSERT(_TimerBaseValid(ui32Base));
208     ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
209            (ui32Timer == TIMER_BOTH));
210 
211     //
212     // Disable the timer module.
213     //
214     HWREG(ui32Base + TIMER_O_CTL) &= ~(ui32Timer &
215                                        (TIMER_CTL_TAEN | TIMER_CTL_TBEN));
216 }
217 
218 //*****************************************************************************
219 //
220 //! Configures the timer(s).
221 //!
222 //! \param ui32Base is the base address of the timer module.
223 //! \param ui32Config is the configuration for the timer.
224 //!
225 //! This function configures the operating mode of the timer(s).  The timer
226 //! module is disabled before being configured and is left in the disabled
227 //! state.  The timer can be configured to be a single full-width timer
228 //! by using the \b TIMER_CFG_* values or a pair of half-width timers using the
229 //! \b TIMER_CFG_A_* and \b TIMER_CFG_B_* values passed in the \e ui32Config
230 //! parameter.
231 //!
232 //! The configuration is specified in \e ui32Config as one of the following
233 //! values:
234 //!
235 //! - \b TIMER_CFG_ONE_SHOT - Full-width one-shot timer
236 //! - \b TIMER_CFG_ONE_SHOT_UP - Full-width one-shot timer that counts up
237 //!   instead of down
238 //! - \b TIMER_CFG_PERIODIC - Full-width periodic timer
239 //! - \b TIMER_CFG_PERIODIC_UP - Full-width periodic timer that counts up
240 //!   instead of down
241 //! - \b TIMER_CFG_RTC - Full-width real time clock timer
242 //! - \b TIMER_CFG_SPLIT_PAIR - Two half-width timers
243 //!
244 //! When configured for a pair of half-width timers, each timer is separately
245 //! configured.  The first timer is configured by setting \e ui32Config to
246 //! the result of a logical OR operation between one of the following values
247 //! and \e ui32Config:
248 //!
249 //! - \b TIMER_CFG_A_ONE_SHOT - Half-width one-shot timer
250 //! - \b TIMER_CFG_A_ONE_SHOT_UP - Half-width one-shot timer that counts up
251 //!   instead of down
252 //! - \b TIMER_CFG_A_PERIODIC - Half-width periodic timer
253 //! - \b TIMER_CFG_A_PERIODIC_UP - Half-width periodic timer that counts up
254 //!   instead of down
255 //! - \b TIMER_CFG_A_CAP_COUNT - Half-width edge count capture
256 //! - \b TIMER_CFG_A_CAP_COUNT_UP - Half-width edge count capture that counts
257 //!   up instead of down
258 //! - \b TIMER_CFG_A_CAP_TIME - Half-width edge time capture
259 //! - \b TIMER_CFG_A_CAP_TIME_UP - Half-width edge time capture that counts up
260 //!   instead of down
261 //! - \b TIMER_CFG_A_PWM - Half-width PWM output
262 //!
263 //! One of the following can be combined with the  \b TIMER_CFG_* values to
264 //! enable an action on timer A:
265 //!
266 //! - \b TIMER_CFG_A_ACT_TOINTD - masks the timeout interrupt of timer A.
267 //! - \b TIMER_CFG_A_ACT_NONE - no additional action on timeout of timer A.
268 //! - \b TIMER_CFG_A_ACT_TOGGLE - toggle CCP on timeout of timer A.
269 //! - \b TIMER_CFG_A_ACT_SETTO - set CCP on timeout of timer A.
270 //! - \b TIMER_CFG_A_ACT_CLRTO - clear CCP on timeout of timer A.
271 //! - \b TIMER_CFG_A_ACT_SETTOGTO - set CCP immediately and then toggle it on
272 //!      timeout of timer A.
273 //! - \b TIMER_CFG_A_ACT_CLRTOGTO - clear CCP immediately and then toggle it on
274 //!      timeout of timer A.
275 //! - \b TIMER_CFG_A_ACT_SETCLRTO - set CCP immediately and then clear it on
276 //!      timeout of timer A.
277 //! - \b TIMER_CFG_A_ACT_CLRSETTO - clear CCP immediately and then set it on
278 //!      timeout of timer A.
279 //!
280 //! One of the following can be combined with the \b TIMER_CFG_* values to
281 //! enable an action on timer B:
282 //!
283 //! - \b TIMER_CFG_B_ACT_TOINTD - masks the timeout interrupt of timer B.
284 //! - \b TIMER_CFG_B_ACT_NONE - no additional action on timeout of timer B.
285 //! - \b TIMER_CFG_B_ACT_TOGGLE - toggle CCP on timeout of timer B.
286 //! - \b TIMER_CFG_B_ACT_SETTO - set CCP on timeout of timer B.
287 //! - \b TIMER_CFG_B_ACT_CLRTO - clear CCP on timeout of timer B.
288 //! - \b TIMER_CFG_B_ACT_SETTOGTO - set CCP immediately and then toggle it on
289 //!      timeout of timer B.
290 //! - \b TIMER_CFG_B_ACT_CLRTOGTO - clear CCP immediately and then toggle it on
291 //!      timeout of timer B.
292 //! - \b TIMER_CFG_B_ACT_SETCLRTO - set CCP immediately and then clear it on
293 //!      timeout of timer B.
294 //! - \b TIMER_CFG_B_ACT_CLRSETTO - clear CCP immediately and then set it on
295 //!      timeout of timer B.
296 //!
297 //! Similarly, the second timer is configured by setting \e ui32Config to
298 //! the result of a logical OR operation between one of the corresponding
299 //! \b TIMER_CFG_B_* values and \e ui32Config.
300 //!
301 //! \return None.
302 //
303 //*****************************************************************************
304 void
TimerConfigure(uint32_t ui32Base,uint32_t ui32Config)305 TimerConfigure(uint32_t ui32Base, uint32_t ui32Config)
306 {
307     //
308     // Check the arguments.
309     //
310     ASSERT(_TimerBaseValid(ui32Base));
311     ASSERT((ui32Config == TIMER_CFG_ONE_SHOT) ||
312            (ui32Config == TIMER_CFG_ONE_SHOT_UP) ||
313            (ui32Config == TIMER_CFG_PERIODIC) ||
314            (ui32Config == TIMER_CFG_PERIODIC_UP) ||
315            (ui32Config == TIMER_CFG_RTC) ||
316            ((ui32Config & 0xff000000) == TIMER_CFG_SPLIT_PAIR));
317     ASSERT(((ui32Config & 0xff000000) != TIMER_CFG_SPLIT_PAIR) ||
318            ((((ui32Config & 0x000000ff) == TIMER_CFG_A_ONE_SHOT) ||
319              ((ui32Config & 0x000000ff) == TIMER_CFG_A_ONE_SHOT_UP) ||
320              ((ui32Config & 0x000000ff) == TIMER_CFG_A_PERIODIC) ||
321              ((ui32Config & 0x000000ff) == TIMER_CFG_A_PERIODIC_UP) ||
322              ((ui32Config & 0x000000ff) == TIMER_CFG_A_CAP_COUNT) ||
323              ((ui32Config & 0x000000ff) == TIMER_CFG_A_CAP_COUNT_UP) ||
324              ((ui32Config & 0x000000ff) == TIMER_CFG_A_CAP_TIME) ||
325              ((ui32Config & 0x000000ff) == TIMER_CFG_A_CAP_TIME_UP) ||
326              ((ui32Config & 0x000000ff) == TIMER_CFG_A_PWM)) &&
327             (((ui32Config & 0x0000ff00) == TIMER_CFG_B_ONE_SHOT) ||
328              ((ui32Config & 0x0000ff00) == TIMER_CFG_B_ONE_SHOT_UP) ||
329              ((ui32Config & 0x0000ff00) == TIMER_CFG_B_PERIODIC) ||
330              ((ui32Config & 0x0000ff00) == TIMER_CFG_B_PERIODIC_UP) ||
331              ((ui32Config & 0x0000ff00) == TIMER_CFG_B_CAP_COUNT) ||
332              ((ui32Config & 0x0000ff00) == TIMER_CFG_B_CAP_COUNT_UP) ||
333              ((ui32Config & 0x0000ff00) == TIMER_CFG_B_CAP_TIME) ||
334              ((ui32Config & 0x0000ff00) == TIMER_CFG_B_CAP_TIME_UP) ||
335              ((ui32Config & 0x0000ff00) == TIMER_CFG_B_PWM))));
336 
337     //
338     // Disable the timers.
339     //
340     HWREG(ui32Base + TIMER_O_CTL) &= ~(TIMER_CTL_TAEN | TIMER_CTL_TBEN);
341 
342     //
343     // Set the global timer configuration.
344     //
345     HWREG(ui32Base + TIMER_O_CFG) = ui32Config >> 24;
346 
347     //
348     // Set the configuration of the A and B timers and set the TxPWMIE bit.
349     // Note that the B timer configuration is ignored by the hardware in 32-bit
350     // modes.
351     //
352     HWREG(ui32Base + TIMER_O_TAMR) = (((ui32Config & 0x000f0000) >> 4) |
353                                       (ui32Config & 0xff) |
354                                       TIMER_TAMR_TAPWMIE);
355     HWREG(ui32Base + TIMER_O_TBMR) = (((ui32Config & 0x00f00000) >> 8) |
356                                       ((ui32Config >> 8) & 0xff) |
357                                       TIMER_TBMR_TBPWMIE);
358 }
359 
360 //*****************************************************************************
361 //
362 //! Controls the output level.
363 //!
364 //! \param ui32Base is the base address of the timer module.
365 //! \param ui32Timer specifies the timer(s) to adjust; must be one of
366 //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
367 //! \param bInvert specifies the output level.
368 //!
369 //! This function configures the PWM output level for the specified timer.  If
370 //! the \e bInvert parameter is \b true, then the timer's output is made active
371 //! low; otherwise, it is made active high.
372 //!
373 //! \return None.
374 //
375 //*****************************************************************************
376 void
TimerControlLevel(uint32_t ui32Base,uint32_t ui32Timer,bool bInvert)377 TimerControlLevel(uint32_t ui32Base, uint32_t ui32Timer, bool bInvert)
378 {
379     //
380     // Check the arguments.
381     //
382     ASSERT(_TimerBaseValid(ui32Base));
383     ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
384            (ui32Timer == TIMER_BOTH));
385 
386     //
387     // Set the output levels as requested.
388     //
389     ui32Timer &= TIMER_CTL_TAPWML | TIMER_CTL_TBPWML;
390     HWREG(ui32Base + TIMER_O_CTL) = (bInvert ?
391                                      (HWREG(ui32Base + TIMER_O_CTL) |
392                                       ui32Timer) :
393                                      (HWREG(ui32Base + TIMER_O_CTL) &
394                                       ~(ui32Timer)));
395 }
396 
397 //*****************************************************************************
398 //
399 //! Enables or disables the ADC trigger output.
400 //!
401 //! \param ui32Base is the base address of the timer module.
402 //! \param ui32Timer specifies the timer to adjust; must be one of \b TIMER_A,
403 //! \b TIMER_B, or \b TIMER_BOTH.
404 //! \param bEnable specifies the desired ADC trigger state.
405 //!
406 //! This function controls the ADC trigger output for the specified timer.  If
407 //! the \e bEnable parameter is \b true, then the timer's ADC output trigger is
408 //! enabled; otherwise it is disabled.
409 //!
410 //! \return None.
411 //
412 //*****************************************************************************
413 void
TimerControlTrigger(uint32_t ui32Base,uint32_t ui32Timer,bool bEnable)414 TimerControlTrigger(uint32_t ui32Base, uint32_t ui32Timer,
415                     bool bEnable)
416 {
417     //
418     // Timer time out ADC trigger enable must also be set.
419     //
420     uint32_t ui32Val;
421 
422     //
423     // Check the arguments.
424     //
425     ASSERT(_TimerBaseValid(ui32Base));
426     ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
427            (ui32Timer == TIMER_BOTH));
428 
429     //
430     // Determine which bits to set or clear in GPTMADCEV.
431     //
432     ui32Val = (TIMER_ADCEV_TATOADCEN | TIMER_ADCEV_TBTOADCEN);
433     ui32Val &= ui32Timer;
434 
435     //
436     // Write the GPTM ADC Event register to enable or disable the trigger
437     // to the ADC.
438     //
439     HWREG(ui32Base + TIMER_O_ADCEV) = (bEnable ?
440                                        (HWREG(ui32Base + TIMER_O_ADCEV) |
441                                         ui32Val) :
442                                        (HWREG(ui32Base + TIMER_O_ADCEV) &
443                                         ~(ui32Val)));
444 
445     //
446     // Set the trigger output as requested.
447     // Set the ADC trigger output as requested.
448     //
449     ui32Timer &= TIMER_CTL_TAOTE | TIMER_CTL_TBOTE;
450     HWREG(ui32Base + TIMER_O_CTL) = (bEnable ?
451                                      (HWREG(ui32Base + TIMER_O_CTL) |
452                                       ui32Timer) :
453                                      (HWREG(ui32Base + TIMER_O_CTL) &
454                                       ~(ui32Timer)));
455 }
456 
457 //*****************************************************************************
458 //
459 //! Controls the event type.
460 //!
461 //! \param ui32Base is the base address of the timer module.
462 //! \param ui32Timer specifies the timer(s) to be adjusted; must be one of
463 //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
464 //! \param ui32Event specifies the type of event; must be one of
465 //! \b TIMER_EVENT_POS_EDGE, \b TIMER_EVENT_NEG_EDGE, or
466 //! \b TIMER_EVENT_BOTH_EDGES.
467 //!
468 //! This function configures the signal edge(s) that triggers the timer when
469 //! in capture mode.
470 //!
471 //! \return None.
472 //
473 //*****************************************************************************
474 void
TimerControlEvent(uint32_t ui32Base,uint32_t ui32Timer,uint32_t ui32Event)475 TimerControlEvent(uint32_t ui32Base, uint32_t ui32Timer,
476                   uint32_t ui32Event)
477 {
478     //
479     // Check the arguments.
480     //
481     ASSERT(_TimerBaseValid(ui32Base));
482     ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
483            (ui32Timer == TIMER_BOTH));
484 
485     //
486     // Set the event type.
487     //
488     ui32Timer &= TIMER_CTL_TAEVENT_M | TIMER_CTL_TBEVENT_M;
489     HWREG(ui32Base + TIMER_O_CTL) = ((HWREG(ui32Base + TIMER_O_CTL) &
490                                       ~ui32Timer) | (ui32Event & ui32Timer));
491 }
492 
493 //*****************************************************************************
494 //
495 //! Controls the stall handling.
496 //!
497 //! \param ui32Base is the base address of the timer module.
498 //! \param ui32Timer specifies the timer(s) to be adjusted; must be one of
499 //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
500 //! \param bStall specifies the response to a stall signal.
501 //!
502 //! This function controls the stall response for the specified timer.  If the
503 //! \e bStall parameter is \b true, then the timer stops counting if the
504 //! processor enters debug mode; otherwise the timer keeps running while in
505 //! debug mode.
506 //!
507 //! \return None.
508 //
509 //*****************************************************************************
510 void
TimerControlStall(uint32_t ui32Base,uint32_t ui32Timer,bool bStall)511 TimerControlStall(uint32_t ui32Base, uint32_t ui32Timer,
512                   bool bStall)
513 {
514     //
515     // Check the arguments.
516     //
517     ASSERT(_TimerBaseValid(ui32Base));
518     ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
519            (ui32Timer == TIMER_BOTH));
520 
521     //
522     // Set the stall mode.
523     //
524     ui32Timer &= TIMER_CTL_TASTALL | TIMER_CTL_TBSTALL;
525     HWREG(ui32Base + TIMER_O_CTL) = (bStall ?
526                                      (HWREG(ui32Base + TIMER_O_CTL) |
527                                       ui32Timer) :
528                                      (HWREG(ui32Base + TIMER_O_CTL) &
529                                       ~(ui32Timer)));
530 }
531 
532 //*****************************************************************************
533 //
534 //! Controls the wait on trigger handling.
535 //!
536 //! \param ui32Base is the base address of the timer module.
537 //! \param ui32Timer specifies the timer(s) to be adjusted; must be one of
538 //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
539 //! \param bWait specifies if the timer should wait for a trigger input.
540 //!
541 //! This function controls whether or not a timer waits for a trigger input to
542 //! start counting.  When enabled, the previous timer in the trigger chain must
543 //! count to its timeout in order for this timer to start counting. Refer to
544 //! the TRM/data sheet for a description of the trigger chain.
545 //!
546 //! \note This function should not be used for Timer 0A.
547 //!
548 //! \return None.
549 //
550 //*****************************************************************************
551 void
TimerControlWaitOnTrigger(uint32_t ui32Base,uint32_t ui32Timer,bool bWait)552 TimerControlWaitOnTrigger(uint32_t ui32Base, uint32_t ui32Timer,
553                           bool bWait)
554 {
555     //
556     // Check the arguments.
557     //
558     ASSERT(_TimerBaseValid(ui32Base));
559     ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
560            (ui32Timer == TIMER_BOTH));
561 
562     //
563     // Set the wait on trigger mode for timer A.
564     //
565     if ((ui32Timer & TIMER_A) != 0)
566     {
567         if (bWait)
568         {
569             HWREG(ui32Base + TIMER_O_TAMR) |= TIMER_TAMR_TAWOT;
570         }
571         else
572         {
573             HWREG(ui32Base + TIMER_O_TAMR) &= ~(TIMER_TAMR_TAWOT);
574         }
575     }
576 
577     //
578     // Set the wait on trigger mode for timer B.
579     //
580     if ((ui32Timer & TIMER_B) != 0)
581     {
582         if (bWait)
583         {
584             HWREG(ui32Base + TIMER_O_TBMR) |= TIMER_TBMR_TBWOT;
585         }
586         else
587         {
588             HWREG(ui32Base + TIMER_O_TBMR) &= ~(TIMER_TBMR_TBWOT);
589         }
590     }
591 }
592 
593 //*****************************************************************************
594 //
595 //! Enables RTC counting.
596 //!
597 //! \param ui32Base is the base address of the timer module.
598 //!
599 //! This function causes the timer to start counting when in RTC mode.  If not
600 //! configured for RTC mode, this function does nothing.
601 //!
602 //! \return None.
603 //
604 //*****************************************************************************
605 void
TimerRTCEnable(uint32_t ui32Base)606 TimerRTCEnable(uint32_t ui32Base)
607 {
608     //
609     // Check the arguments.
610     //
611     ASSERT(_TimerBaseValid(ui32Base));
612 
613     //
614     // Enable RTC counting.
615     //
616     HWREG(ui32Base + TIMER_O_CTL) |= TIMER_CTL_RTCEN;
617 }
618 
619 //*****************************************************************************
620 //
621 //! Disables RTC counting.
622 //!
623 //! \param ui32Base is the base address of the timer module.
624 //!
625 //! This function causes the timer to stop counting when in RTC mode.
626 //!
627 //! \return None.
628 //
629 //*****************************************************************************
630 void
TimerRTCDisable(uint32_t ui32Base)631 TimerRTCDisable(uint32_t ui32Base)
632 {
633     //
634     // Check the arguments.
635     //
636     ASSERT(_TimerBaseValid(ui32Base));
637 
638     //
639     // Disable RTC counting.
640     //
641     HWREG(ui32Base + TIMER_O_CTL) &= ~(TIMER_CTL_RTCEN);
642 }
643 
644 //*****************************************************************************
645 //
646 //! Sets the clock source for the specified timer module.
647 //!
648 //! \param ui32Base is the base address of the timer module.
649 //! \param ui32Source is the clock source for the timer module.
650 //!
651 //! This function sets the clock source for both timer A and timer B for the
652 //! given timer module.  The possible clock sources are the system clock
653 //! (\b TIMER_CLOCK_SYSTEM) or the precision internal oscillator
654 //! (\b TIMER_CLOCK_PIOSC).
655 //!
656 //! \return None.
657 //
658 //*****************************************************************************
659 void
TimerClockSourceSet(uint32_t ui32Base,uint32_t ui32Source)660 TimerClockSourceSet(uint32_t ui32Base, uint32_t ui32Source)
661 {
662     //
663     // Check the arguments.
664     //
665     ASSERT(_TimerBaseValid(ui32Base));
666     ASSERT((ui32Source == TIMER_CLOCK_SYSTEM) ||
667            (ui32Source == TIMER_CLOCK_PIOSC));
668 
669     //
670     // Set the timer clock source.
671     //
672     HWREG(ui32Base + TIMER_O_CC) = ui32Source;
673 }
674 
675 //*****************************************************************************
676 //
677 //! Returns the clock source for the specified timer module.
678 //!
679 //! \param ui32Base is the base address of the timer module.
680 //!
681 //! This function returns the clock source for the specified timer module.  The
682 //! possible clock sources are the system clock (\b TIMER_CLOCK_SYSTEM) or
683 //! the precision internal oscillator (\b TIMER_CLOCK_PIOSC).
684 //!
685 //! \return Returns either \b TIMER_CLOCK_SYSTEM or \b TIMER_CLOCK_PIOSC.
686 //
687 //*****************************************************************************
688 uint32_t
TimerClockSourceGet(uint32_t ui32Base)689 TimerClockSourceGet(uint32_t ui32Base)
690 {
691     //
692     // Check the arguments.
693     //
694     ASSERT(_TimerBaseValid(ui32Base));
695 
696     //
697     // Return the timer clock source.
698     //
699     return (HWREG(ui32Base + TIMER_O_CC));
700 }
701 
702 //*****************************************************************************
703 //
704 //! Sets the timer prescale value.
705 //!
706 //! \param ui32Base is the base address of the timer module.
707 //! \param ui32Timer specifies the timer(s) to adjust; must be one of
708 //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
709 //! \param ui32Value is the timer prescale value which must be between 0 and
710 //! 255 (inclusive) for 16/32-bit timers.
711 //!
712 //! This function configures the value of the input clock prescaler.  The
713 //! prescaler is only operational when in half-width mode and is used to extend
714 //! the range of the half-width timer modes.  The prescaler provides the least
715 //! significant bits when counting down in periodic and one-shot modes; in all
716 //! other modes, the prescaler provides the most significant bits.
717 //!
718 //! \return None.
719 //
720 //*****************************************************************************
721 void
TimerPrescaleSet(uint32_t ui32Base,uint32_t ui32Timer,uint32_t ui32Value)722 TimerPrescaleSet(uint32_t ui32Base, uint32_t ui32Timer, uint32_t ui32Value)
723 {
724     //
725     // Check the arguments.
726     //
727     ASSERT(_TimerBaseValid(ui32Base));
728     ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
729            (ui32Timer == TIMER_BOTH));
730     ASSERT(ui32Value < 256);
731 
732     //
733     // Set the timer A prescaler if requested.
734     //
735     if (ui32Timer & TIMER_A)
736     {
737         HWREG(ui32Base + TIMER_O_TAPR) = ui32Value;
738     }
739 
740     //
741     // Set the timer B prescaler if requested.
742     //
743     if (ui32Timer & TIMER_B)
744     {
745         HWREG(ui32Base + TIMER_O_TBPR) = ui32Value;
746     }
747 }
748 
749 //*****************************************************************************
750 //
751 //! Gets the timer prescale value.
752 //!
753 //! \param ui32Base is the base address of the timer module.
754 //! \param ui32Timer specifies the timer; must be one of \b TIMER_A or
755 //! \b TIMER_B.
756 //!
757 //! This function gets the value of the input clock prescaler.  The prescaler
758 //! is only operational when in half-width mode and is used to extend the range
759 //! of the half-width timer modes.  The prescaler provides the least
760 //! significant bits when counting down in periodic and one-shot modes; in all
761 //! other modes, the prescaler provides the most significant bits.
762 //!
763 //! \return The value of the timer prescaler.
764 //
765 //*****************************************************************************
766 uint32_t
TimerPrescaleGet(uint32_t ui32Base,uint32_t ui32Timer)767 TimerPrescaleGet(uint32_t ui32Base, uint32_t ui32Timer)
768 {
769     //
770     // Check the arguments.
771     //
772     ASSERT(_TimerBaseValid(ui32Base));
773     ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
774            (ui32Timer == TIMER_BOTH));
775 
776     //
777     // Return the appropriate prescale value.
778     //
779     return ((ui32Timer == TIMER_A) ? HWREG(ui32Base + TIMER_O_TAPR) :
780             HWREG(ui32Base + TIMER_O_TBPR));
781 }
782 
783 //*****************************************************************************
784 //
785 //! Sets the timer prescale match value.
786 //!
787 //! \param ui32Base is the base address of the timer module.
788 //! \param ui32Timer specifies the timer(s) to adjust; must be one of
789 //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
790 //! \param ui32Value is the timer prescale match value which must be between 0
791 //! and 255 (inclusive) for 16/32-bit timers.
792 //!
793 //! This function configures the value of the input clock prescaler match
794 //! value.  When in a half-width mode that uses the counter match and the
795 //! prescaler, the prescale match effectively extends the range of the match.
796 //! The prescaler provides the least significant bits when counting down in
797 //! periodic and one-shot modes; in all other modes, the prescaler provides the
798 //! most significant bits.
799 //!
800 //! \return None.
801 //
802 //*****************************************************************************
803 void
TimerPrescaleMatchSet(uint32_t ui32Base,uint32_t ui32Timer,uint32_t ui32Value)804 TimerPrescaleMatchSet(uint32_t ui32Base, uint32_t ui32Timer,
805                       uint32_t ui32Value)
806 {
807     //
808     // Check the arguments.
809     //
810     ASSERT(_TimerBaseValid(ui32Base));
811     ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
812            (ui32Timer == TIMER_BOTH));
813     ASSERT(ui32Value < 256);
814 
815     //
816     // Set the timer A prescale match if requested.
817     //
818     if (ui32Timer & TIMER_A)
819     {
820         HWREG(ui32Base + TIMER_O_TAPMR) = ui32Value;
821     }
822 
823     //
824     // Set the timer B prescale match if requested.
825     //
826     if (ui32Timer & TIMER_B)
827     {
828         HWREG(ui32Base + TIMER_O_TBPMR) = ui32Value;
829     }
830 }
831 
832 //*****************************************************************************
833 //
834 //! Gets the timer prescale match value.
835 //!
836 //! \param ui32Base is the base address of the timer module.
837 //! \param ui32Timer specifies the timer; must be one of \b TIMER_A or
838 //! \b TIMER_B.
839 //!
840 //! This function gets the value of the input clock prescaler match value.
841 //! When in a half-width mode that uses the counter match and prescaler, the
842 //! prescale match effectively extends the range of the match.  The prescaler
843 //! provides the least significant bits when counting down in periodic and
844 //! one-shot modes; in all other modes, the prescaler provides the most
845 //! significant bits.
846 //!
847 //! \return The value of the timer prescale match.
848 //
849 //*****************************************************************************
850 uint32_t
TimerPrescaleMatchGet(uint32_t ui32Base,uint32_t ui32Timer)851 TimerPrescaleMatchGet(uint32_t ui32Base, uint32_t ui32Timer)
852 {
853     //
854     // Check the arguments.
855     //
856     ASSERT(_TimerBaseValid(ui32Base));
857     ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
858            (ui32Timer == TIMER_BOTH));
859 
860     //
861     // Return the appropriate prescale match value.
862     //
863     return ((ui32Timer == TIMER_A) ? HWREG(ui32Base + TIMER_O_TAPMR) :
864             HWREG(ui32Base + TIMER_O_TBPMR));
865 }
866 
867 //*****************************************************************************
868 //
869 //! Sets the timer load value.
870 //!
871 //! \param ui32Base is the base address of the timer module.
872 //! \param ui32Timer specifies the timer(s) to adjust; must be one of
873 //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.  Only \b TIMER_A should be used
874 //! when the timer is configured for full-width operation.
875 //! \param ui32Value is the load value.
876 //!
877 //! This function configures the timer load value; if the timer is running then
878 //! the value is immediately loaded into the timer.
879 //!
880 //! \note This function can be used for both full- and half-width modes of
881 //! 16/32-bit timers.
882 //!
883 //! \return None.
884 //
885 //*****************************************************************************
886 void
TimerLoadSet(uint32_t ui32Base,uint32_t ui32Timer,uint32_t ui32Value)887 TimerLoadSet(uint32_t ui32Base, uint32_t ui32Timer, uint32_t ui32Value)
888 {
889     //
890     // Check the arguments.
891     //
892     ASSERT(_TimerBaseValid(ui32Base));
893     ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
894            (ui32Timer == TIMER_BOTH));
895 
896     //
897     // Set the timer A load value if requested.
898     //
899     if (ui32Timer & TIMER_A)
900     {
901         HWREG(ui32Base + TIMER_O_TAILR) = ui32Value;
902     }
903 
904     //
905     // Set the timer B load value if requested.
906     //
907     if (ui32Timer & TIMER_B)
908     {
909         HWREG(ui32Base + TIMER_O_TBILR) = ui32Value;
910     }
911 }
912 
913 //*****************************************************************************
914 //
915 //! Gets the timer load value.
916 //!
917 //! \param ui32Base is the base address of the timer module.
918 //! \param ui32Timer specifies the timer; must be one of \b TIMER_A or
919 //! \b TIMER_B.  Only \b TIMER_A should be used when the timer is configured
920 //! for full-width operation.
921 //!
922 //! This function gets the currently programmed interval load value for the
923 //! specified timer.
924 //!
925 //! \note This function can be used for both full- and half-width modes of
926 //! 16/32-bit timers.
927 //!
928 //! \return Returns the load value for the timer.
929 //
930 //*****************************************************************************
931 uint32_t
TimerLoadGet(uint32_t ui32Base,uint32_t ui32Timer)932 TimerLoadGet(uint32_t ui32Base, uint32_t ui32Timer)
933 {
934     //
935     // Check the arguments.
936     //
937     ASSERT(_TimerBaseValid(ui32Base));
938     ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B));
939 
940     //
941     // Return the appropriate load value.
942     //
943     return ((ui32Timer == TIMER_A) ? HWREG(ui32Base + TIMER_O_TAILR) :
944             HWREG(ui32Base + TIMER_O_TBILR));
945 }
946 
947 //*****************************************************************************
948 //
949 //! Gets the current timer value.
950 //!
951 //! \param ui32Base is the base address of the timer module.
952 //! \param ui32Timer specifies the timer; must be one of \b TIMER_A or
953 //! \b TIMER_B.  Only \b TIMER_A should be used when the timer is configured
954 //! for full-width operation.
955 //!
956 //! This function reads the current value of the specified timer.
957 //!
958 //! \note This function can be used for both full- and half-width modes of
959 //! 16/32-bit timers.
960 //!
961 //! \return Returns the current value of the timer.
962 //
963 //*****************************************************************************
964 uint32_t
TimerValueGet(uint32_t ui32Base,uint32_t ui32Timer)965 TimerValueGet(uint32_t ui32Base, uint32_t ui32Timer)
966 {
967     //
968     // Check the arguments.
969     //
970     ASSERT(_TimerBaseValid(ui32Base));
971     ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B));
972 
973     //
974     // Return the appropriate timer value.
975     //
976     return ((ui32Timer == TIMER_A) ? HWREG(ui32Base + TIMER_O_TAR) :
977             HWREG(ui32Base + TIMER_O_TBR));
978 }
979 
980 //*****************************************************************************
981 //
982 //! Sets the timer match value.
983 //!
984 //! \param ui32Base is the base address of the timer module.
985 //! \param ui32Timer specifies the timer(s) to adjust; must be one of
986 //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.  Only \b TIMER_A should be used
987 //! when the timer is configured for full-width operation.
988 //! \param ui32Value is the match value.
989 //!
990 //! This function configures the match value for a timer.  This value is used
991 //! in capture count mode to determine when to interrupt the processor and in
992 //! PWM mode to determine the duty cycle of the output signal.
993 //! Match interrupts can also be generated in periodic and one-shot modes.
994 //!
995 //! \note This function can be used for both full- and half-width modes of
996 //! 16/32-bit timers.
997 //!
998 //! \return None.
999 //
1000 //*****************************************************************************
1001 void
TimerMatchSet(uint32_t ui32Base,uint32_t ui32Timer,uint32_t ui32Value)1002 TimerMatchSet(uint32_t ui32Base, uint32_t ui32Timer,
1003               uint32_t ui32Value)
1004 {
1005     //
1006     // Check the arguments.
1007     //
1008     ASSERT(_TimerBaseValid(ui32Base));
1009     ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
1010            (ui32Timer == TIMER_BOTH));
1011 
1012     //
1013     // Set the timer A match value if requested.
1014     //
1015     if (ui32Timer & TIMER_A)
1016     {
1017         HWREG(ui32Base + TIMER_O_TAMATCHR) = ui32Value;
1018     }
1019 
1020     //
1021     // Set the timer B match value if requested.
1022     //
1023     if (ui32Timer & TIMER_B)
1024     {
1025         HWREG(ui32Base + TIMER_O_TBMATCHR) = ui32Value;
1026     }
1027 }
1028 
1029 //*****************************************************************************
1030 //
1031 //! Gets the timer match value.
1032 //!
1033 //! \param ui32Base is the base address of the timer module.
1034 //! \param ui32Timer specifies the timer; must be one of \b TIMER_A or
1035 //! \b TIMER_B.  Only \b TIMER_A should be used when the timer is configured
1036 //! for full-width operation.
1037 //!
1038 //! This function gets the match value for the specified timer.
1039 //!
1040 //! \note This function can be used for both full- and half-width modes of
1041 //! 16/32-bit timers.
1042 //!
1043 //! \return Returns the match value for the timer.
1044 //
1045 //*****************************************************************************
1046 uint32_t
TimerMatchGet(uint32_t ui32Base,uint32_t ui32Timer)1047 TimerMatchGet(uint32_t ui32Base, uint32_t ui32Timer)
1048 {
1049     //
1050     // Check the arguments.
1051     //
1052     ASSERT(_TimerBaseValid(ui32Base));
1053     ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B));
1054 
1055     //
1056     // Return the appropriate match value.
1057     //
1058     return ((ui32Timer == TIMER_A) ? HWREG(ui32Base + TIMER_O_TAMATCHR) :
1059             HWREG(ui32Base + TIMER_O_TBMATCHR));
1060 }
1061 
1062 //*****************************************************************************
1063 //
1064 //! Registers an interrupt handler for the timer interrupt.
1065 //!
1066 //! \param ui32Base is the base address of the timer module.
1067 //! \param ui32Timer specifies the timer(s); must be one of \b TIMER_A,
1068 //! \b TIMER_B, or \b TIMER_BOTH.
1069 //! \param pfnHandler is a pointer to the function to be called when the timer
1070 //! interrupt occurs.
1071 //!
1072 //! This function registers the handler to be called when a timer interrupt
1073 //! occurs.  In addition, this function enables the global interrupt in the
1074 //! interrupt controller; specific timer interrupts must be enabled via
1075 //! TimerIntEnable().  It is the interrupt handler's responsibility to clear
1076 //! the interrupt source via TimerIntClear().
1077 //!
1078 //! \sa IntRegister() for important information about registering interrupt
1079 //! handlers.
1080 //!
1081 //! \return None.
1082 //
1083 //*****************************************************************************
1084 void
TimerIntRegister(uint32_t ui32Base,uint32_t ui32Timer,void (* pfnHandler)(void))1085 TimerIntRegister(uint32_t ui32Base, uint32_t ui32Timer,
1086                  void (*pfnHandler)(void))
1087 {
1088     uint32_t ui32Int;
1089 
1090     //
1091     // Check the arguments.
1092     //
1093     ASSERT(_TimerBaseValid(ui32Base));
1094     ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
1095            (ui32Timer == TIMER_BOTH));
1096 
1097     //
1098     // Get the interrupt number for this timer module.
1099     //
1100     ui32Int = _TimerIntNumberGet(ui32Base, ui32Timer);
1101 
1102     ASSERT(ui32Int != 0);
1103 
1104     //
1105     // Register the interrupt handler.
1106     //
1107     IntRegister(ui32Int, pfnHandler);
1108 
1109     //
1110     // Enable the interrupt.
1111     //
1112     IntEnable(ui32Int);
1113 }
1114 
1115 //*****************************************************************************
1116 //
1117 //! Unregisters an interrupt handler for the timer interrupt.
1118 //!
1119 //! \param ui32Base is the base address of the timer module.
1120 //! \param ui32Timer specifies the timer(s); must be one of \b TIMER_A,
1121 //! \b TIMER_B, or \b TIMER_BOTH.
1122 //!
1123 //! This function unregisters the handler to be called when a timer interrupt
1124 //! occurs.  This function also masks off the interrupt in the interrupt
1125 //! controller so that the interrupt handler is no longer called.
1126 //!
1127 //! \sa IntRegister() for important information about registering interrupt
1128 //! handlers.
1129 //!
1130 //! \return None.
1131 //
1132 //*****************************************************************************
1133 void
TimerIntUnregister(uint32_t ui32Base,uint32_t ui32Timer)1134 TimerIntUnregister(uint32_t ui32Base, uint32_t ui32Timer)
1135 {
1136     uint32_t ui32Int;
1137 
1138     //
1139     // Check the arguments.
1140     //
1141     ASSERT(_TimerBaseValid(ui32Base));
1142     ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
1143            (ui32Timer == TIMER_BOTH));
1144 
1145     //
1146     // Get the interrupt number for this timer module.
1147     //
1148     ui32Int = _TimerIntNumberGet(ui32Base, ui32Timer);
1149 
1150     //
1151     // Disable the interrupt.
1152     //
1153     IntDisable(ui32Int);
1154 
1155     //
1156     // Unregister the interrupt handler.
1157     //
1158     IntUnregister(ui32Int);
1159 }
1160 
1161 //*****************************************************************************
1162 //
1163 //! Enables individual timer interrupt sources.
1164 //!
1165 //! \param ui32Base is the base address of the timer module.
1166 //! \param ui32IntFlags is the bit mask of the interrupt sources to be enabled.
1167 //!
1168 //! This function enables the indicated timer interrupt sources.  Only the
1169 //! sources that are enabled can be reflected to the processor interrupt;
1170 //! disabled sources have no effect on the processor.
1171 //!
1172 //! The \e ui32IntFlags parameter must be the logical OR of any combination of
1173 //! the following:
1174 //!
1175 //! - \b TIMER_TIMB_DMA - Timer B uDMA complete
1176 //! - \b TIMER_TIMA_DMA - Timer A uDMA complete
1177 //! - \b TIMER_CAPB_EVENT  - Capture B event interrupt
1178 //! - \b TIMER_CAPB_MATCH  - Capture B match interrupt
1179 //! - \b TIMER_TIMB_TIMEOUT  - Timer B timeout interrupt
1180 //! - \b TIMER_RTC_MATCH  - RTC interrupt mask
1181 //! - \b TIMER_CAPA_EVENT  - Capture A event interrupt
1182 //! - \b TIMER_CAPA_MATCH  - Capture A match interrupt
1183 //! - \b TIMER_TIMA_TIMEOUT  - Timer A timeout interrupt
1184 //!
1185 //! \return None.
1186 //
1187 //*****************************************************************************
1188 void
TimerIntEnable(uint32_t ui32Base,uint32_t ui32IntFlags)1189 TimerIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
1190 {
1191     //
1192     // Check the arguments.
1193     //
1194     ASSERT(_TimerBaseValid(ui32Base));
1195 
1196     //
1197     // Enable the specified interrupts.
1198     //
1199     HWREG(ui32Base + TIMER_O_IMR) |= ui32IntFlags;
1200 }
1201 
1202 //*****************************************************************************
1203 //
1204 //! Disables individual timer interrupt sources.
1205 //!
1206 //! \param ui32Base is the base address of the timer module.
1207 //! \param ui32IntFlags is the bit mask of the interrupt sources to be
1208 //! disabled.
1209 //!
1210 //! This function disables the indicated timer interrupt sources.  Only the
1211 //! sources that are enabled can be reflected to the processor interrupt;
1212 //! disabled sources have no effect on the processor.
1213 //!
1214 //! The \e ui32IntFlags parameter has the same definition as the
1215 //! \e ui32IntFlags parameter to TimerIntEnable().
1216 //!
1217 //! \return None.
1218 //
1219 //*****************************************************************************
1220 void
TimerIntDisable(uint32_t ui32Base,uint32_t ui32IntFlags)1221 TimerIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags)
1222 {
1223     //
1224     // Check the arguments.
1225     //
1226     ASSERT(_TimerBaseValid(ui32Base));
1227 
1228     //
1229     // Disable the specified interrupts.
1230     //
1231     HWREG(ui32Base + TIMER_O_IMR) &= ~(ui32IntFlags);
1232 }
1233 
1234 //*****************************************************************************
1235 //
1236 //! Gets the current interrupt status.
1237 //!
1238 //! \param ui32Base is the base address of the timer module.
1239 //! \param bMasked is false if the raw interrupt status is required and true if
1240 //! the masked interrupt status is required.
1241 //!
1242 //! This function returns the interrupt status for the timer module.  Either
1243 //! the raw interrupt status or the status of interrupts that are allowed to
1244 //! reflect to the processor can be returned.
1245 //!
1246 //! \return The current interrupt status, enumerated as a bit field of
1247 //! values described in TimerIntEnable().
1248 //
1249 //*****************************************************************************
1250 uint32_t
TimerIntStatus(uint32_t ui32Base,bool bMasked)1251 TimerIntStatus(uint32_t ui32Base, bool bMasked)
1252 {
1253     //
1254     // Check the arguments.
1255     //
1256     ASSERT(_TimerBaseValid(ui32Base));
1257 
1258     //
1259     // Return either the interrupt status or the raw interrupt status as
1260     // requested.
1261     //
1262     return (bMasked ? HWREG(ui32Base + TIMER_O_MIS) :
1263             HWREG(ui32Base + TIMER_O_RIS));
1264 }
1265 
1266 //*****************************************************************************
1267 //
1268 //! Clears timer interrupt sources.
1269 //!
1270 //! \param ui32Base is the base address of the timer module.
1271 //! \param ui32IntFlags is a bit mask of the interrupt sources to be cleared.
1272 //!
1273 //! The specified timer interrupt sources are cleared, so that they no longer
1274 //! assert.  This function must be called in the interrupt handler to keep the
1275 //! interrupt from being triggered again immediately upon exit.
1276 //!
1277 //! The \e ui32IntFlags parameter has the same definition as the
1278 //! \e ui32IntFlags parameter to TimerIntEnable().
1279 //!
1280 //! \note Because there is a write buffer in the Cortex-M processor, it may
1281 //! take several clock cycles before the interrupt source is actually cleared.
1282 //! Therefore, it is recommended that the interrupt source be cleared early in
1283 //! the interrupt handler (as opposed to the very last action) to avoid
1284 //! returning from the interrupt handler before the interrupt source is
1285 //! actually cleared.  Failure to do so may result in the interrupt handler
1286 //! being immediately reentered (because the interrupt controller still sees
1287 //! the interrupt source asserted).
1288 //!
1289 //! \return None.
1290 //
1291 //*****************************************************************************
1292 void
TimerIntClear(uint32_t ui32Base,uint32_t ui32IntFlags)1293 TimerIntClear(uint32_t ui32Base, uint32_t ui32IntFlags)
1294 {
1295     //
1296     // Check the arguments.
1297     //
1298     ASSERT(_TimerBaseValid(ui32Base));
1299 
1300     //
1301     // Clear the requested interrupt sources.
1302     //
1303     HWREG(ui32Base + TIMER_O_ICR) = ui32IntFlags;
1304 }
1305 
1306 //*****************************************************************************
1307 //
1308 //! Synchronizes the counters in a set of timers.
1309 //!
1310 //! \param ui32Base is the base address of the timer module.  This parameter
1311 //! must be the base address of Timer0 (in other words, \b TIMER0_BASE).
1312 //! \param ui32Timers is the set of timers to synchronize.
1313 //!
1314 //! This function synchronizes the counters in a specified set of timers.
1315 //! When a timer is running in half-width mode, each half can be included or
1316 //! excluded in the synchronization event.  When a timer is running in
1317 //! full-width mode, only the A timer can be synchronized (specifying the B
1318 //! timer has no effect).
1319 //!
1320 //! The \e ui32Timers parameter is the logical OR of any of the following
1321 //! defines:
1322 //!
1323 //! - \b TIMER_0A_SYNC
1324 //! - \b TIMER_0B_SYNC
1325 //! - \b TIMER_1A_SYNC
1326 //! - \b TIMER_1B_SYNC
1327 //! - \b TIMER_2A_SYNC
1328 //! - \b TIMER_2B_SYNC
1329 //! - \b TIMER_3A_SYNC
1330 //! - \b TIMER_3B_SYNC
1331 //! - \b TIMER_4A_SYNC
1332 //! - \b TIMER_4B_SYNC
1333 //! - \b TIMER_5A_SYNC
1334 //! - \b TIMER_5B_SYNC
1335 //! - \b TIMER_6A_SYNC
1336 //! - \b TIMER_6B_SYNC
1337 //! - \b TIMER_7A_SYNC
1338 //! - \b TIMER_7B_SYNC
1339 //!
1340 //! \return None.
1341 //
1342 //*****************************************************************************
1343 void
TimerSynchronize(uint32_t ui32Base,uint32_t ui32Timers)1344 TimerSynchronize(uint32_t ui32Base, uint32_t ui32Timers)
1345 {
1346     //
1347     // Check the arguments.
1348     //
1349     ASSERT(ui32Base == TIMER0_BASE);
1350 
1351     //
1352     // Synchronize the specified timers.
1353     //
1354     HWREG(ui32Base + TIMER_O_SYNC) = ui32Timers;
1355 }
1356 
1357 //*****************************************************************************
1358 //
1359 //! Enables the events that can cause an ADC trigger event.
1360 //!
1361 //! \param ui32Base is the base address of the timer module.
1362 //! \param ui32ADCEvent is a bit mask of the events that can cause an ADC
1363 //!        trigger event.
1364 //!
1365 //! This function enables the timer events that can cause an ADC trigger event.
1366 //! The ADC trigger events are specified in the \e ui32ADCEvent parameter by
1367 //! passing in the logical OR of any of the following values:
1368 //!
1369 //! - \b TIMER_ADC_MODEMATCH_B - Enables the mode match ADC trigger for timer
1370 //!   B.
1371 //! - \b TIMER_ADC_CAPEVENT_B - Enables the capture event ADC trigger for
1372 //!   timer B.
1373 //! - \b TIMER_ADC_CAPMATCH_B - Enables the capture match ADC trigger for
1374 //!   timer B.
1375 //! - \b TIMER_ADC_TIMEOUT_B - Enables the timeout ADC trigger for timer B.
1376 //! - \b TIMER_ADC_MODEMATCH_A - Enables the mode match ADC trigger for timer
1377 //!   A.
1378 //! - \b TIMER_ADC_RTC_A - Enables the RTC ADC trigger for timer A.
1379 //! - \b TIMER_ADC_CAPEVENT_A - Enables the capture event ADC trigger for
1380 //!   timer A.
1381 //! - \b TIMER_ADC_CAPMATCH_A - Enables the capture match ADC trigger for
1382 //!   timer A.
1383 //! - \b TIMER_ADC_TIMEOUT_A - Enables the timeout ADC trigger for timer A.
1384 //!
1385 //! \return None.
1386 //
1387 //*****************************************************************************
1388 void
TimerADCEventSet(uint32_t ui32Base,uint32_t ui32ADCEvent)1389 TimerADCEventSet(uint32_t ui32Base, uint32_t ui32ADCEvent)
1390 {
1391     //
1392     // Check the arguments.
1393     //
1394     ASSERT(_TimerBaseValid(ui32Base));
1395 
1396     //
1397     // Set the ADC triggers.
1398     //
1399     HWREG(ui32Base + TIMER_O_ADCEV) = ui32ADCEvent;
1400 }
1401 
1402 //*****************************************************************************
1403 //
1404 //! Returns the events that can cause an ADC trigger event.
1405 //!
1406 //! \param ui32Base is the base address of the timer module.
1407 //!
1408 //! This function returns the timer events that can cause an ADC trigger event.
1409 //! The ADC trigger events are the logical OR of any of the following values:
1410 //!
1411 //! - \b TIMER_ADC_MODEMATCH_B - The mode match ADC trigger for timer B is
1412 //!   enabled.
1413 //! - \b TIMER_ADC_CAPEVENT_B - The capture event ADC trigger for timer B is
1414 //!   enabled.
1415 //! - \b TIMER_ADC_CAPMATCH_B - The capture match ADC trigger for timer B is
1416 //!   enabled.
1417 //! - \b TIMER_ADC_TIMEOUT_B - The timeout ADC trigger for timer B is enabled.
1418 //! - \b TIMER_ADC_MODEMATCH_A - The mode match ADC trigger for timer A is
1419 //!   enabled.
1420 //! - \b TIMER_ADC_RTC_A - The RTC ADC trigger for timer A is enabled.
1421 //! - \b TIMER_ADC_CAPEVENT_A - The capture event ADC trigger for timer A is
1422 //!   enabled.
1423 //! - \b TIMER_ADC_CAPMATCH_A - The capture match ADC trigger for timer A is
1424 //!   enabled.
1425 //! - \b TIMER_ADC_TIMEOUT_A - The timeout ADC trigger for timer A is enabled.
1426 //!
1427 //! \return The timer events that trigger the ADC.
1428 //
1429 //*****************************************************************************
1430 uint32_t
TimerADCEventGet(uint32_t ui32Base)1431 TimerADCEventGet(uint32_t ui32Base)
1432 {
1433     //
1434     // Check the arguments.
1435     //
1436     ASSERT(_TimerBaseValid(ui32Base));
1437 
1438     //
1439     // Return the current ADC triggers.
1440     //
1441     return (HWREG(ui32Base + TIMER_O_ADCEV));
1442 }
1443 
1444 //*****************************************************************************
1445 //
1446 //! Enables the events that can trigger a uDMA request.
1447 //!
1448 //! \param ui32Base is the base address of the timer module.
1449 //! \param ui32DMAEvent is a bit mask of the events that can trigger uDMA.
1450 //!
1451 //! This function enables the timer events that can trigger the start of a uDMA
1452 //! sequence.  The uDMA trigger events are specified in the \e ui32DMAEvent
1453 //! parameter by passing in the logical OR of the following values:
1454 //!
1455 //! - \b TIMER_DMA_MODEMATCH_B - The mode match uDMA trigger for timer B is
1456 //!   enabled.
1457 //! - \b TIMER_DMA_CAPEVENT_B - The capture event uDMA trigger for timer B is
1458 //!   enabled.
1459 //! - \b TIMER_DMA_CAPMATCH_B - The capture match uDMA trigger for timer B is
1460 //!   enabled.
1461 //! - \b TIMER_DMA_TIMEOUT_B - The timeout uDMA trigger for timer B is enabled.
1462 //! - \b TIMER_DMA_MODEMATCH_A - The mode match uDMA trigger for timer A is
1463 //!   enabled.
1464 //! - \b TIMER_DMA_RTC_A - The RTC uDMA trigger for timer A is enabled.
1465 //! - \b TIMER_DMA_CAPEVENT_A - The capture event uDMA trigger for timer A is
1466 //!   enabled.
1467 //! - \b TIMER_DMA_CAPMATCH_A - The capture match uDMA trigger for timer A is
1468 //!   enabled.
1469 //! - \b TIMER_DMA_TIMEOUT_A - The timeout uDMA trigger for timer A is enabled.
1470 //!
1471 //! \return None.
1472 //
1473 //*****************************************************************************
1474 void
TimerDMAEventSet(uint32_t ui32Base,uint32_t ui32DMAEvent)1475 TimerDMAEventSet(uint32_t ui32Base, uint32_t ui32DMAEvent)
1476 {
1477     //
1478     // Check the arguments.
1479     //
1480     ASSERT(_TimerBaseValid(ui32Base));
1481 
1482     //
1483     // Set the uDMA triggers.
1484     //
1485     HWREG(ui32Base + TIMER_O_DMAEV) = ui32DMAEvent;
1486 }
1487 
1488 //*****************************************************************************
1489 //
1490 //! Returns the events that can trigger a uDMA request.
1491 //!
1492 //! \param ui32Base is the base address of the timer module.
1493 //!
1494 //! This function returns the timer events that can trigger the start of a uDMA
1495 //! sequence.  The uDMA trigger events are the logical OR of the following
1496 //! values:
1497 //!
1498 //! - \b TIMER_DMA_MODEMATCH_B - Enables the mode match uDMA trigger for timer
1499 //!   B.
1500 //! - \b TIMER_DMA_CAPEVENT_B - Enables the capture event uDMA trigger for
1501 //!   timer B.
1502 //! - \b TIMER_DMA_CAPMATCH_B - Enables the capture match uDMA trigger for
1503 //!   timer B.
1504 //! - \b TIMER_DMA_TIMEOUT_B - Enables the timeout uDMA trigger for timer B.
1505 //! - \b TIMER_DMA_MODEMATCH_A - Enables the mode match uDMA trigger for timer
1506 //!   A.
1507 //! - \b TIMER_DMA_RTC_A - Enables the RTC uDMA trigger for timer A.
1508 //! - \b TIMER_DMA_CAPEVENT_A - Enables the capture event uDMA trigger for
1509 //!   timer A.
1510 //! - \b TIMER_DMA_CAPMATCH_A - Enables the capture match uDMA trigger for
1511 //!   timer A.
1512 //! - \b TIMER_DMA_TIMEOUT_A - Enables the timeout uDMA trigger for timer A.
1513 //!
1514 //! \return The timer events that trigger the uDMA.
1515 //
1516 //*****************************************************************************
1517 uint32_t
TimerDMAEventGet(uint32_t ui32Base)1518 TimerDMAEventGet(uint32_t ui32Base)
1519 {
1520     //
1521     // Check the arguments.
1522     //
1523     ASSERT(_TimerBaseValid(ui32Base));
1524 
1525     //
1526     // Return the current uDMA triggers.
1527     //
1528     return (HWREG(ui32Base + TIMER_O_DMAEV));
1529 }
1530 
1531 //*****************************************************************************
1532 //
1533 //! This function configures the update of timer load and match settings.
1534 //!
1535 //! \param ui32Base is the base address of the timer module.
1536 //! \param ui32Timer specifies the timer(s); must be one of \b TIMER_A,
1537 //! \b TIMER_B, or \b TIMER_BOTH.
1538 //! \param ui32Config is a combination of the updates methods for the timers
1539 //! specified in the \e ui32Timer parameter.
1540 //!
1541 //! This function configures how the timer updates the timer load and match
1542 //! values for the timers.  The \e ui32Timer values can be \b TIMER_A,
1543 //! \b TIMER_B, or \b TIMER_BOTH to apply the settings in \e ui32Config to
1544 //! either timer or both timers.  If the timer is not split then the \b TIMER_A
1545 //! should be used.  The \e ui32Config values affects when the TimerLoadSet()
1546 //! values take effect.
1547 //!
1548 //! - \b TIMER_UP_LOAD_IMMEDIATE is the default mode that causes the
1549 //! TimerLoadSet() to update the timer counter immediately.
1550 //! - \b TIMER_UP_LOAD_TIMEOUT causes the TimerLoadSet() to
1551 //! update the timer when it counts down to zero.
1552 //!
1553 //! Similarly the \e ui32Config value affects when the TimerMatchSet()
1554 //! values take effect.
1555 //!
1556 //! - \b TIMER_UP_MATCH_IMMEDIATE is the default mode that causes the
1557 //! TimerMatchSet() to update the timer match value
1558 //! immediately.
1559 //! - \b TIMER_UP_MATCH_TIMEOUT causes the TimerMatchSet()
1560 //! to update the timer match value when it counts down to zero.
1561 //!
1562 //! \note These settings have no effect if the timer is not in count down mode
1563 //! and are mostly useful when operating in PWM mode to allow for synchronous
1564 //! update of timer match and load values.
1565 //!
1566 //! \return None.
1567 //
1568 //*****************************************************************************
1569 void
TimerUpdateMode(uint32_t ui32Base,uint32_t ui32Timer,uint32_t ui32Config)1570 TimerUpdateMode(uint32_t ui32Base, uint32_t ui32Timer, uint32_t ui32Config)
1571 {
1572     uint32_t ui32Value;
1573 
1574     if ((ui32Timer & TIMER_A) == TIMER_A)
1575     {
1576         ui32Value = HWREG(ui32Base + TIMER_O_TAMR) & ~(0x00000500);
1577         ui32Value |= ui32Config;
1578         HWREG(ui32Base + TIMER_O_TAMR) = ui32Value;
1579     }
1580 
1581     if ((ui32Timer & TIMER_B) == TIMER_B)
1582     {
1583         ui32Value = HWREG(ui32Base + TIMER_O_TBMR) & ~(0x00000500);
1584         ui32Value |= ui32Config;
1585         HWREG(ui32Base + TIMER_O_TBMR) = ui32Value;
1586     }
1587 }
1588 
1589 //*****************************************************************************
1590 //
1591 // Close the Doxygen group.
1592 //! @}
1593 //
1594 //*****************************************************************************
1595