CC13xx Driver Library
timer.c
Go to the documentation of this file.
1 /******************************************************************************
2 * Filename: timer.c
3 * Revised: 2015-09-09 14:37:44 +0200 (Wed, 09 Sep 2015)
4 * Revision: 44542
5 *
6 * Description: Driver for the General Purpose Timer
7 *
8 * Copyright (c) 2015, Texas Instruments Incorporated
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 * 1) Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 *
17 * 2) Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 *
21 * 3) Neither the name of the ORGANIZATION nor the names of its contributors may
22 * be used to endorse or promote products derived from this software without
23 * specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 *
37 ******************************************************************************/
38 
39 #include <driverlib/timer.h>
40 
41 //*****************************************************************************
42 //
43 // Handle support for DriverLib in ROM:
44 // This section will undo prototype renaming made in the header file
45 //
46 //*****************************************************************************
47 #if !defined(DOXYGEN)
48  #undef TimerConfigure
49  #define TimerConfigure NOROM_TimerConfigure
50  #undef TimerLevelControl
51  #define TimerLevelControl NOROM_TimerLevelControl
52  #undef TimerStallControl
53  #define TimerStallControl NOROM_TimerStallControl
54  #undef TimerWaitOnTriggerControl
55  #define TimerWaitOnTriggerControl NOROM_TimerWaitOnTriggerControl
56  #undef TimerIntRegister
57  #define TimerIntRegister NOROM_TimerIntRegister
58  #undef TimerIntUnregister
59  #define TimerIntUnregister NOROM_TimerIntUnregister
60  #undef TimerMatchUpdateMode
61  #define TimerMatchUpdateMode NOROM_TimerMatchUpdateMode
62  #undef TimerIntervalLoadMode
63  #define TimerIntervalLoadMode NOROM_TimerIntervalLoadMode
64 #endif
65 
66 //*****************************************************************************
67 //
76 //
77 //*****************************************************************************
78 static uint32_t
79 TimerIntNumberGet(uint32_t ui32Base)
80 {
81  uint32_t ui32Int;
82 
83  //
84  // Loop through the table that maps timer base addresses to interrupt
85  // numbers.
86  //
87  switch(ui32Base)
88  {
89  case GPT0_BASE :
90  ui32Int = INT_TIMER0A;
91  break;
92  case GPT1_BASE :
93  ui32Int = INT_TIMER1A;
94  break;
95  case GPT2_BASE :
96  ui32Int = INT_TIMER2A;
97  break;
98  case GPT3_BASE :
99  ui32Int = INT_TIMER3A;
100  break;
101  default :
102  ui32Int = 0x0;
103  }
104 
105  //
106  // Return the interrupt number or (-1) if not base address is not matched.
107  //
108  return (ui32Int);
109 }
110 
111 //*****************************************************************************
112 //
114 //
115 //*****************************************************************************
116 void
117 TimerConfigure(uint32_t ui32Base, uint32_t ui32Config)
118 {
119  //
120  // Check the arguments.
121  //
122  ASSERT(TimerBaseValid(ui32Base));
123  ASSERT((ui32Config == TIMER_CFG_ONE_SHOT) ||
124  (ui32Config == TIMER_CFG_ONE_SHOT_UP) ||
125  (ui32Config == TIMER_CFG_PERIODIC) ||
126  (ui32Config == TIMER_CFG_PERIODIC_UP) ||
127  (ui32Config == TIMER_CFG_RTC) ||
128  ((ui32Config & 0xFF000000) == TIMER_CFG_SPLIT_PAIR));
129  ASSERT(((ui32Config & 0xFF000000) != TIMER_CFG_SPLIT_PAIR) ||
130  ((((ui32Config & 0x000000FF) == TIMER_CFG_A_ONE_SHOT) ||
131  ((ui32Config & 0x000000FF) == TIMER_CFG_A_ONE_SHOT_UP) ||
132  ((ui32Config & 0x000000FF) == TIMER_CFG_A_PERIODIC) ||
133  ((ui32Config & 0x000000FF) == TIMER_CFG_A_PERIODIC_UP) ||
134  ((ui32Config & 0x000000FF) == TIMER_CFG_A_CAP_COUNT) ||
135  ((ui32Config & 0x000000FF) == TIMER_CFG_A_CAP_COUNT_UP) ||
136  ((ui32Config & 0x000000FF) == TIMER_CFG_A_CAP_TIME) ||
137  ((ui32Config & 0x000000FF) == TIMER_CFG_A_CAP_TIME_UP) ||
138  ((ui32Config & 0x000000FF) == TIMER_CFG_A_PWM)) &&
139  (((ui32Config & 0x0000FF00) == TIMER_CFG_B_ONE_SHOT) ||
140  ((ui32Config & 0x0000FF00) == TIMER_CFG_B_ONE_SHOT_UP) ||
141  ((ui32Config & 0x0000FF00) == TIMER_CFG_B_PERIODIC) ||
142  ((ui32Config & 0x0000FF00) == TIMER_CFG_B_PERIODIC_UP) ||
143  ((ui32Config & 0x0000FF00) == TIMER_CFG_B_CAP_COUNT) ||
144  ((ui32Config & 0x0000FF00) == TIMER_CFG_B_CAP_COUNT_UP) ||
145  ((ui32Config & 0x0000FF00) == TIMER_CFG_B_CAP_TIME) ||
146  ((ui32Config & 0x0000FF00) == TIMER_CFG_B_CAP_TIME_UP) ||
147  ((ui32Config & 0x0000FF00) == TIMER_CFG_B_PWM))));
148 
149  //
150  // Disable the timers.
151  //
152  HWREG(ui32Base + GPT_O_CTL) &= ~(GPT_CTL_TAEN | GPT_CTL_TBEN);
153 
154  //
155  // Set the global timer configuration.
156  //
157  HWREG(ui32Base + GPT_O_CFG) = ui32Config >> 24;
158 
159  //
160  // Set the configuration of the A and B timers. Note that the B timer
161  // configuration is ignored by the hardware in 32-bit modes.
162  //
163  HWREG(ui32Base + GPT_O_TAMR) = (ui32Config & 0xFF) | GPT_TAMR_TAPWMIE;
164  HWREG(ui32Base + GPT_O_TBMR) =
165  ((ui32Config >> 8) & 0xFF) | GPT_TBMR_TBPWMIE;
166 }
167 
168 //*****************************************************************************
169 //
171 //
172 //*****************************************************************************
173 void
174 TimerLevelControl(uint32_t ui32Base, uint32_t ui32Timer, bool bInvert)
175 {
176  //
177  // Check the arguments.
178  //
179  ASSERT(TimerBaseValid(ui32Base));
180  ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
181  (ui32Timer == TIMER_BOTH));
182 
183  //
184  // Set the output levels as requested.
185  //
186  ui32Timer &= GPT_CTL_TAPWML | GPT_CTL_TBPWML;
187  HWREG(ui32Base + GPT_O_CTL) = (bInvert ?
188  (HWREG(ui32Base + GPT_O_CTL) | ui32Timer) :
189  (HWREG(ui32Base + GPT_O_CTL) &
190  ~(ui32Timer)));
191 }
192 
193 //*****************************************************************************
194 //
196 //
197 //*****************************************************************************
198 void
199 TimerStallControl(uint32_t ui32Base, uint32_t ui32Timer, bool bStall)
200 {
201  //
202  // Check the arguments.
203  //
204  ASSERT(TimerBaseValid(ui32Base));
205  ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
206  (ui32Timer == TIMER_BOTH));
207 
208  //
209  // Set the stall mode.
210  //
211  ui32Timer &= GPT_CTL_TASTALL | GPT_CTL_TBSTALL;
212  HWREG(ui32Base + GPT_O_CTL) = (bStall ?
213  (HWREG(ui32Base + GPT_O_CTL) | ui32Timer) :
214  (HWREG(ui32Base + GPT_O_CTL) & ~(ui32Timer)));
215 }
216 
217 //*****************************************************************************
218 //
220 //
221 //*****************************************************************************
222 void
223 TimerWaitOnTriggerControl(uint32_t ui32Base, uint32_t ui32Timer, bool bWait)
224 {
225  //
226  // Check the arguments.
227  //
228  ASSERT(TimerBaseValid(ui32Base));
229  ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
230  (ui32Timer == TIMER_BOTH));
231 
232  //
233  // Set the wait on trigger mode for timer A.
234  //
235  if(ui32Timer & TIMER_A)
236  {
237  if(bWait)
238  {
239  HWREG(ui32Base + GPT_O_TAMR) |= GPT_TAMR_TAWOT;
240  }
241  else
242  {
243  HWREG(ui32Base + GPT_O_TAMR) &= ~(GPT_TAMR_TAWOT);
244  }
245  }
246 
247  //
248  // Set the wait on trigger mode for timer B.
249  //
250  if(ui32Timer & TIMER_B)
251  {
252  if(bWait)
253  {
254  HWREG(ui32Base + GPT_O_TBMR) |= GPT_TBMR_TBWOT;
255  }
256  else
257  {
258  HWREG(ui32Base + GPT_O_TBMR) &= ~(GPT_TBMR_TBWOT);
259  }
260  }
261 }
262 
263 //*****************************************************************************
264 //
266 //
267 //*****************************************************************************
268 void
269 TimerIntRegister(uint32_t ui32Base, uint32_t ui32Timer, void (*pfnHandler)(void))
270 {
271  uint32_t ui32Int;
272 
273  //
274  // Check the arguments.
275  //
276  ASSERT(TimerBaseValid(ui32Base));
277  ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
278  (ui32Timer == TIMER_BOTH));
279 
280  //
281  // Get the interrupt number for this timer module.
282  //
283  ui32Int = TimerIntNumberGet(ui32Base);
284 
285  //
286  // Register an interrupt handler for timer A if requested.
287  //
288  if(ui32Timer & TIMER_A)
289  {
290  //
291  // Register the interrupt handler.
292  //
293  IntRegister(ui32Int, pfnHandler);
294 
295  //
296  // Enable the interrupt.
297  //
298  IntEnable(ui32Int);
299  }
300 
301  //
302  // Register an interrupt handler for timer B if requested.
303  //
304  if(ui32Timer & TIMER_B)
305  {
306  //
307  // Register the interrupt handler.
308  //
309  IntRegister(ui32Int + 1, pfnHandler);
310 
311  //
312  // Enable the interrupt.
313  //
314  IntEnable(ui32Int + 1);
315  }
316 }
317 
318 //*****************************************************************************
319 //
321 //
322 //*****************************************************************************
323 void
324 TimerIntUnregister(uint32_t ui32Base, uint32_t ui32Timer)
325 {
326  uint32_t ui32Int;
327 
328  //
329  // Check the arguments.
330  //
331  ASSERT(TimerBaseValid(ui32Base));
332  ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
333  (ui32Timer == TIMER_BOTH));
334 
335  //
336  // Get the interrupt number for this timer module.
337  //
338  ui32Int = TimerIntNumberGet(ui32Base);
339 
340  //
341  // Unregister the interrupt handler for timer A if requested.
342  //
343  if(ui32Timer & TIMER_A)
344  {
345  //
346  // Disable the interrupt.
347  //
348  IntDisable(ui32Int);
349 
350  //
351  // Unregister the interrupt handler.
352  //
353  IntUnregister(ui32Int);
354  }
355 
356  //
357  // Unregister the interrupt handler for timer B if requested.
358  //
359  if(ui32Timer & TIMER_B)
360  {
361  //
362  // Disable the interrupt.
363  //
364  IntDisable(ui32Int + 1);
365 
366  //
367  // Unregister the interrupt handler.
368  //
369  IntUnregister(ui32Int + 1);
370  }
371 }
372 
373 //*****************************************************************************
374 //
375 // Sets the Match Register Update mode
376 //
377 //*****************************************************************************
378 void
379 TimerMatchUpdateMode(uint32_t ui32Base, uint32_t ui32Timer, uint32_t ui32Mode)
380 {
381  // Check the arguments
382  ASSERT(TimerBaseValid(ui32Base));
383  ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) || (ui32Timer == TIMER_BOTH));
384  ASSERT((ui32Mode == TIMER_MATCHUPDATE_NEXTCYCLE) || (ui32Mode == TIMER_MATCHUPDATE_TIMEOUT));
385 
386  // Set mode for timer A
387  if(ui32Timer & TIMER_A)
388  {
389  if(ui32Mode == TIMER_MATCHUPDATE_NEXTCYCLE)
390  {
391  HWREG(ui32Base + GPT_O_TAMR) &= ~(GPT_TAMR_TAMRSU);
392  }
393  else
394  {
395  HWREG(ui32Base + GPT_O_TAMR) |= GPT_TAMR_TAMRSU;
396  }
397  }
398 
399  // Set mode for timer B
400  if(ui32Timer & TIMER_B)
401  {
402  if(ui32Mode == TIMER_MATCHUPDATE_NEXTCYCLE)
403  {
404  HWREG(ui32Base + GPT_O_TBMR) &= ~(GPT_TBMR_TBMRSU);
405  }
406  else
407  {
408  HWREG(ui32Base + GPT_O_TBMR) |= GPT_TBMR_TBMRSU;
409  }
410  }
411 }
412 
413 //*****************************************************************************
414 //
415 // Sets the Interval Load mode
416 //
417 //*****************************************************************************
418 void
419 TimerIntervalLoadMode(uint32_t ui32Base, uint32_t ui32Timer, uint32_t ui32Mode)
420 {
421  // Check the arguments
422  ASSERT(TimerBaseValid(ui32Base));
423  ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) || (ui32Timer == TIMER_BOTH));
424  ASSERT((ui32Mode == TIMER_INTERVALLOAD_NEXTCYCLE) || (ui32Mode == TIMER_INTERVALLOAD_TIMEOUT));
425 
426  // Set mode for timer A
427  if(ui32Timer & TIMER_A)
428  {
429  if(ui32Mode == TIMER_INTERVALLOAD_NEXTCYCLE)
430  {
431  HWREG(ui32Base + GPT_O_TAMR) &= ~(GPT_TAMR_TAILD);
432  }
433  else
434  {
435  HWREG(ui32Base + GPT_O_TAMR) |= GPT_TAMR_TAILD;
436  }
437  }
438 
439  // Set mode for timer B
440  if(ui32Timer & TIMER_B)
441  {
442  if(ui32Mode == TIMER_INTERVALLOAD_NEXTCYCLE)
443  {
444  HWREG(ui32Base + GPT_O_TBMR) &= ~(GPT_TBMR_TBILD);
445  }
446  else
447  {
448  HWREG(ui32Base + GPT_O_TBMR) |= GPT_TBMR_TBILD;
449  }
450  }
451 }
static uint32_t TimerIntNumberGet(uint32_t ui32Base)
Gets the timer interrupt number.
Definition: timer.c:79
#define TIMER_CFG_SPLIT_PAIR
Definition: timer.h:103
void TimerWaitOnTriggerControl(uint32_t ui32Base, uint32_t ui32Timer, bool bWait)
Controls the wait on trigger handling.
Definition: timer.c:223
#define TIMER_CFG_A_PWM
Definition: timer.h:112
#define TIMER_CFG_B_ONE_SHOT
Definition: timer.h:113
#define TIMER_CFG_A_ONE_SHOT_UP
Definition: timer.h:105
#define TIMER_CFG_A_CAP_TIME_UP
Definition: timer.h:111
#define TIMER_CFG_PERIODIC_UP
Definition: timer.h:101
void TimerStallControl(uint32_t ui32Base, uint32_t ui32Timer, bool bStall)
Controls the stall handling.
Definition: timer.c:199
void TimerMatchUpdateMode(uint32_t ui32Base, uint32_t ui32Timer, uint32_t ui32Mode)
Sets the Match Register Update mode.
Definition: timer.c:379
#define TIMER_CFG_ONE_SHOT
Definition: timer.h:98
#define TIMER_MATCHUPDATE_TIMEOUT
Definition: timer.h:181
#define TIMER_CFG_A_CAP_COUNT_UP
Definition: timer.h:109
void TimerIntRegister(uint32_t ui32Base, uint32_t ui32Timer, void(*pfnHandler)(void))
Registers an interrupt handler for the timer interrupt.
Definition: timer.c:269
#define TIMER_CFG_B_ONE_SHOT_UP
Definition: timer.h:114
#define TIMER_CFG_B_PWM
Definition: timer.h:121
#define TIMER_CFG_B_CAP_TIME
Definition: timer.h:119
void TimerConfigure(uint32_t ui32Base, uint32_t ui32Config)
Configures the timer(s)
Definition: timer.c:117
#define TIMER_CFG_ONE_SHOT_UP
Definition: timer.h:99
#define ASSERT(expr)
Definition: debug.h:74
#define TIMER_B
Definition: timer.h:158
#define TIMER_CFG_A_PERIODIC
Definition: timer.h:106
#define TIMER_CFG_B_CAP_TIME_UP
Definition: timer.h:120
void TimerIntUnregister(uint32_t ui32Base, uint32_t ui32Timer)
Unregisters an interrupt handler for the timer interrupt.
Definition: timer.c:324
void TimerLevelControl(uint32_t ui32Base, uint32_t ui32Timer, bool bInvert)
Controls the output level.
Definition: timer.c:174
#define TIMER_CFG_B_PERIODIC
Definition: timer.h:115
void TimerIntervalLoadMode(uint32_t ui32Base, uint32_t ui32Timer, uint32_t ui32Mode)
Sets the Interval Load mode.
Definition: timer.c:419
#define TIMER_CFG_B_CAP_COUNT
Definition: timer.h:117
#define TIMER_CFG_B_CAP_COUNT_UP
Definition: timer.h:118
#define TIMER_A
Definition: timer.h:157
void IntUnregister(uint32_t ui32Interrupt)
Unregisters the function to be called when an interrupt occurs.
Definition: interrupt.c:200
#define TIMER_INTERVALLOAD_TIMEOUT
Definition: timer.h:189
#define TIMER_CFG_PERIODIC
Definition: timer.h:100
#define TIMER_CFG_A_ONE_SHOT
Definition: timer.h:104
#define TIMER_CFG_A_CAP_COUNT
Definition: timer.h:108
#define TIMER_CFG_A_CAP_TIME
Definition: timer.h:110
#define TIMER_CFG_RTC
Definition: timer.h:102
#define TIMER_INTERVALLOAD_NEXTCYCLE
Definition: timer.h:188
#define TIMER_CFG_A_PERIODIC_UP
Definition: timer.h:107
#define TIMER_BOTH
Definition: timer.h:159
#define TIMER_MATCHUPDATE_NEXTCYCLE
Definition: timer.h:180
void IntDisable(uint32_t ui32Interrupt)
Disables an interrupt.
Definition: interrupt.c:378
void IntRegister(uint32_t ui32Interrupt, void(*pfnHandler)(void))
Registers a function to be called when an interrupt occurs.
Definition: interrupt.c:152
void IntEnable(uint32_t ui32Interrupt)
Enables an interrupt.
Definition: interrupt.c:318
#define TIMER_CFG_B_PERIODIC_UP
Definition: timer.h:116