timer.c 37 KB

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