timer.c 32 KB

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