timer.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  1. //*****************************************************************************
  2. //
  3. // timer.c - Driver for the timer module.
  4. //
  5. // Copyright (c) 2005-2011 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 8049 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. // A mapping of timer base address to interupt number.
  40. //
  41. //*****************************************************************************
  42. static const unsigned long g_ppulTimerIntMap[][2] =
  43. {
  44. { TIMER0_BASE, INT_TIMER0A },
  45. { TIMER1_BASE, INT_TIMER1A },
  46. { TIMER2_BASE, INT_TIMER2A },
  47. { TIMER3_BASE, INT_TIMER3A },
  48. { TIMER4_BASE, INT_TIMER4A },
  49. { TIMER5_BASE, INT_TIMER5A },
  50. { WTIMER0_BASE, INT_WTIMER0A },
  51. { WTIMER1_BASE, INT_WTIMER1A },
  52. { WTIMER2_BASE, INT_WTIMER2A },
  53. { WTIMER3_BASE, INT_WTIMER3A },
  54. { WTIMER4_BASE, INT_WTIMER4A },
  55. { WTIMER5_BASE, INT_WTIMER5A },
  56. };
  57. //*****************************************************************************
  58. //
  59. //! \internal
  60. //! Checks a timer base address.
  61. //!
  62. //! \param ulBase is the base address of the timer module.
  63. //!
  64. //! This function determines if a timer module base address is valid.
  65. //!
  66. //! \return Returns \b true if the base address is valid and \b false
  67. //! otherwise.
  68. //
  69. //*****************************************************************************
  70. #ifdef DEBUG
  71. static tBoolean
  72. TimerBaseValid(unsigned long ulBase)
  73. {
  74. return((ulBase == TIMER0_BASE) || (ulBase == TIMER1_BASE) ||
  75. (ulBase == TIMER2_BASE) || (ulBase == TIMER3_BASE) ||
  76. (ulBase == TIMER4_BASE) || (ulBase == TIMER5_BASE) ||
  77. (ulBase == WTIMER0_BASE) || (ulBase == WTIMER1_BASE) ||
  78. (ulBase == WTIMER2_BASE) || (ulBase == WTIMER3_BASE) ||
  79. (ulBase == WTIMER4_BASE) || (ulBase == WTIMER5_BASE));
  80. }
  81. #endif
  82. //*****************************************************************************
  83. //
  84. //! \internal
  85. //! Gets the timer interrupt number.
  86. //!
  87. //! \param ulBase is the base address of the timer module.
  88. //!
  89. //! Given a timer base address, returns the corresponding interrupt number.
  90. //!
  91. //! \return Returns a timer interrupt number, or -1 if \e ulBase is invalid.
  92. //
  93. //*****************************************************************************
  94. static long
  95. TimerIntNumberGet(unsigned long ulBase)
  96. {
  97. unsigned long ulIdx;
  98. //
  99. // Loop through the table that maps timer base addresses to interrupt
  100. // numbers.
  101. //
  102. for(ulIdx = 0; ulIdx < (sizeof(g_ppulTimerIntMap) /
  103. sizeof(g_ppulTimerIntMap[0])); ulIdx++)
  104. {
  105. //
  106. // See if this base address matches.
  107. //
  108. if(g_ppulTimerIntMap[ulIdx][0] == ulBase)
  109. {
  110. //
  111. // Return the corresponding interrupt number.
  112. //
  113. return(g_ppulTimerIntMap[ulIdx][1]);
  114. }
  115. }
  116. //
  117. // The base address could not be found, so return an error.
  118. //
  119. return(-1);
  120. }
  121. //*****************************************************************************
  122. //
  123. //! Enables the timer(s).
  124. //!
  125. //! \param ulBase is the base address of the timer module.
  126. //! \param ulTimer specifies the timer(s) to enable; must be one of \b TIMER_A,
  127. //! \b TIMER_B, or \b TIMER_BOTH.
  128. //!
  129. //! This function enables operation of the timer module. The timer must be
  130. //! configured before it is enabled.
  131. //!
  132. //! \return None.
  133. //
  134. //*****************************************************************************
  135. void
  136. TimerEnable(unsigned long ulBase, unsigned long ulTimer)
  137. {
  138. //
  139. // Check the arguments.
  140. //
  141. ASSERT(TimerBaseValid(ulBase));
  142. ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
  143. (ulTimer == TIMER_BOTH));
  144. //
  145. // Enable the timer(s) module.
  146. //
  147. HWREG(ulBase + TIMER_O_CTL) |= ulTimer & (TIMER_CTL_TAEN | TIMER_CTL_TBEN);
  148. }
  149. //*****************************************************************************
  150. //
  151. //! Disables the timer(s).
  152. //!
  153. //! \param ulBase is the base address of the timer module.
  154. //! \param ulTimer specifies the timer(s) to disable; must be one of
  155. //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
  156. //!
  157. //! This function disables operation of the timer module.
  158. //!
  159. //! \return None.
  160. //
  161. //*****************************************************************************
  162. void
  163. TimerDisable(unsigned long ulBase, unsigned long ulTimer)
  164. {
  165. //
  166. // Check the arguments.
  167. //
  168. ASSERT(TimerBaseValid(ulBase));
  169. ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
  170. (ulTimer == TIMER_BOTH));
  171. //
  172. // Disable the timer module.
  173. //
  174. HWREG(ulBase + TIMER_O_CTL) &= ~(ulTimer &
  175. (TIMER_CTL_TAEN | TIMER_CTL_TBEN));
  176. }
  177. //*****************************************************************************
  178. //
  179. //! Configures the timer(s).
  180. //!
  181. //! \param ulBase is the base address of the timer module.
  182. //! \param ulConfig is the configuration for the timer.
  183. //!
  184. //! This function configures the operating mode of the timer(s). The timer
  185. //! module is disabled before being configured, and is left in the disabled
  186. //! state. There are two types of timers; a 16/32-bit variety and a 32/64-bit
  187. //! variety. The 16/32-bit variety is comprised of two 16-bit timers that can
  188. //! operate independently or be concatenated to form a 32-bit timer.
  189. //! Similarly, the 32/64-bit variety is comprised of two 32-bit timers that can
  190. //! operate independently or be concatenated to form a 64-bit timer.
  191. //!
  192. //! The configuration is specified in \e ulConfig as one of the following
  193. //! values:
  194. //!
  195. //! - \b TIMER_CFG_ONE_SHOT - Full-width one-shot timer
  196. //! - \b TIMER_CFG_ONE_SHOT_UP - Full-width one-shot timer that counts up
  197. //! instead of down (not available on all parts)
  198. //! - \b TIMER_CFG_PERIODIC - Full-width periodic timer
  199. //! - \b TIMER_CFG_PERIODIC_UP - Full-width periodic timer that counts up
  200. //! instead of down (not available on all parts)
  201. //! - \b TIMER_CFG_RTC - Full-width real time clock timer
  202. //! - \b TIMER_CFG_SPLIT_PAIR - Two half-width timers
  203. //!
  204. //! When configured for a pair of half-width timers, each timer is separately
  205. //! configured. The first timer is configured by setting \e ulConfig to
  206. //! the result of a logical OR operation between one of the following values
  207. //! and \e ulConfig:
  208. //!
  209. //! - \b TIMER_CFG_A_ONE_SHOT - Half-width one-shot timer
  210. //! - \b TIMER_CFG_A_ONE_SHOT_UP - Half-width one-shot timer that counts up
  211. //! instead of down (not available on all parts)
  212. //! - \b TIMER_CFG_A_PERIODIC - Half-width periodic timer
  213. //! - \b TIMER_CFG_A_PERIODIC_UP - Half-width periodic timer that counts up
  214. //! instead of down (not available on all parts)
  215. //! - \b TIMER_CFG_A_CAP_COUNT - Half-width edge count capture
  216. //! - \b TIMER_CFG_A_CAP_COUNT_UP - Half-width edge count capture that counts
  217. //! up instead of down (not available on all parts)
  218. //! - \b TIMER_CFG_A_CAP_TIME - Half-width edge time capture
  219. //! - \b TIMER_CFG_A_CAP_TIME_UP - Half-width edge time capture that counts up
  220. //! instead of down (not available on all parts)
  221. //! - \b TIMER_CFG_A_PWM - Half-width PWM output
  222. //!
  223. //! Similarly, the second timer is configured by setting \e ulConfig to
  224. //! the result of a logical OR operation between one of the corresponding
  225. //! \b TIMER_CFG_B_* values and \e ulConfig.
  226. //!
  227. //! \return None.
  228. //
  229. //*****************************************************************************
  230. void
  231. TimerConfigure(unsigned long ulBase, unsigned long ulConfig)
  232. {
  233. //
  234. // Check the arguments.
  235. //
  236. ASSERT(TimerBaseValid(ulBase));
  237. ASSERT((ulConfig == TIMER_CFG_ONE_SHOT) ||
  238. (ulConfig == TIMER_CFG_ONE_SHOT_UP) ||
  239. (ulConfig == TIMER_CFG_PERIODIC) ||
  240. (ulConfig == TIMER_CFG_PERIODIC_UP) ||
  241. (ulConfig == TIMER_CFG_RTC) ||
  242. ((ulConfig & 0xff000000) == TIMER_CFG_SPLIT_PAIR));
  243. ASSERT(((ulConfig & 0xff000000) != TIMER_CFG_SPLIT_PAIR) ||
  244. ((((ulConfig & 0x000000ff) == TIMER_CFG_A_ONE_SHOT) ||
  245. ((ulConfig & 0x000000ff) == TIMER_CFG_A_ONE_SHOT_UP) ||
  246. ((ulConfig & 0x000000ff) == TIMER_CFG_A_PERIODIC) ||
  247. ((ulConfig & 0x000000ff) == TIMER_CFG_A_PERIODIC_UP) ||
  248. ((ulConfig & 0x000000ff) == TIMER_CFG_A_CAP_COUNT) ||
  249. ((ulConfig & 0x000000ff) == TIMER_CFG_A_CAP_TIME) ||
  250. ((ulConfig & 0x000000ff) == TIMER_CFG_A_PWM)) &&
  251. (((ulConfig & 0x0000ff00) == TIMER_CFG_B_ONE_SHOT) ||
  252. ((ulConfig & 0x0000ff00) == TIMER_CFG_B_ONE_SHOT_UP) ||
  253. ((ulConfig & 0x0000ff00) == TIMER_CFG_B_PERIODIC) ||
  254. ((ulConfig & 0x0000ff00) == TIMER_CFG_B_PERIODIC_UP) ||
  255. ((ulConfig & 0x0000ff00) == TIMER_CFG_B_CAP_COUNT) ||
  256. ((ulConfig & 0x0000ff00) == TIMER_CFG_B_CAP_COUNT_UP) ||
  257. ((ulConfig & 0x0000ff00) == TIMER_CFG_B_CAP_TIME) ||
  258. ((ulConfig & 0x0000ff00) == TIMER_CFG_B_CAP_TIME_UP) ||
  259. ((ulConfig & 0x0000ff00) == TIMER_CFG_B_PWM))));
  260. //
  261. // Disable the timers.
  262. //
  263. HWREG(ulBase + TIMER_O_CTL) &= ~(TIMER_CTL_TAEN | TIMER_CTL_TBEN);
  264. //
  265. // Set the global timer configuration.
  266. //
  267. HWREG(ulBase + TIMER_O_CFG) = ulConfig >> 24;
  268. //
  269. // Set the configuration of the A and B timers. Note that the B timer
  270. // configuration is ignored by the hardware in 32-bit modes.
  271. //
  272. HWREG(ulBase + TIMER_O_TAMR) = (ulConfig & 255) | TIMER_TAMR_TAPWMIE;
  273. HWREG(ulBase + TIMER_O_TBMR) =
  274. ((ulConfig >> 8) & 255) | TIMER_TBMR_TBPWMIE;
  275. }
  276. //*****************************************************************************
  277. //
  278. //! Controls the output level.
  279. //!
  280. //! \param ulBase is the base address of the timer module.
  281. //! \param ulTimer specifies the timer(s) to adjust; must be one of \b TIMER_A,
  282. //! \b TIMER_B, or \b TIMER_BOTH.
  283. //! \param bInvert specifies the output level.
  284. //!
  285. //! This function sets the PWM output level for the specified timer. If the
  286. //! \e bInvert parameter is \b true, then the timer's output is made active
  287. //! low; otherwise, it is made active high.
  288. //!
  289. //! \return None.
  290. //
  291. //*****************************************************************************
  292. void
  293. TimerControlLevel(unsigned long ulBase, unsigned long ulTimer,
  294. tBoolean bInvert)
  295. {
  296. //
  297. // Check the arguments.
  298. //
  299. ASSERT(TimerBaseValid(ulBase));
  300. ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
  301. (ulTimer == TIMER_BOTH));
  302. //
  303. // Set the output levels as requested.
  304. //
  305. ulTimer &= TIMER_CTL_TAPWML | TIMER_CTL_TBPWML;
  306. HWREG(ulBase + TIMER_O_CTL) = (bInvert ?
  307. (HWREG(ulBase + TIMER_O_CTL) | ulTimer) :
  308. (HWREG(ulBase + TIMER_O_CTL) & ~(ulTimer)));
  309. }
  310. //*****************************************************************************
  311. //
  312. //! Enables or disables the trigger output.
  313. //!
  314. //! \param ulBase is the base address of the timer module.
  315. //! \param ulTimer specifies the timer to adjust; must be one of \b TIMER_A,
  316. //! \b TIMER_B, or \b TIMER_BOTH.
  317. //! \param bEnable specifies the desired trigger state.
  318. //!
  319. //! This function controls the trigger output for the specified timer. If the
  320. //! \e bEnable parameter is \b true, then the timer's output trigger is
  321. //! enabled; otherwise it is disabled.
  322. //!
  323. //! \return None.
  324. //
  325. //*****************************************************************************
  326. void
  327. TimerControlTrigger(unsigned long ulBase, unsigned long ulTimer,
  328. tBoolean bEnable)
  329. {
  330. //
  331. // Check the arguments.
  332. //
  333. ASSERT(TimerBaseValid(ulBase));
  334. ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
  335. (ulTimer == TIMER_BOTH));
  336. //
  337. // Set the trigger output as requested.
  338. //
  339. ulTimer &= TIMER_CTL_TAOTE | TIMER_CTL_TBOTE;
  340. HWREG(ulBase + TIMER_O_CTL) = (bEnable ?
  341. (HWREG(ulBase + TIMER_O_CTL) | ulTimer) :
  342. (HWREG(ulBase + TIMER_O_CTL) & ~(ulTimer)));
  343. }
  344. //*****************************************************************************
  345. //
  346. //! Controls the event type.
  347. //!
  348. //! \param ulBase is the base address of the timer module.
  349. //! \param ulTimer specifies the timer(s) to be adjusted; must be one of
  350. //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
  351. //! \param ulEvent specifies the type of event; must be one of
  352. //! \b TIMER_EVENT_POS_EDGE, \b TIMER_EVENT_NEG_EDGE, or
  353. //! \b TIMER_EVENT_BOTH_EDGES.
  354. //!
  355. //! This function sets the signal edge(s) that triggers the timer when in
  356. //! capture mode.
  357. //!
  358. //! \return None.
  359. //
  360. //*****************************************************************************
  361. void
  362. TimerControlEvent(unsigned long ulBase, unsigned long ulTimer,
  363. unsigned long ulEvent)
  364. {
  365. //
  366. // Check the arguments.
  367. //
  368. ASSERT(TimerBaseValid(ulBase));
  369. ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
  370. (ulTimer == TIMER_BOTH));
  371. //
  372. // Set the event type.
  373. //
  374. ulTimer &= TIMER_CTL_TAEVENT_M | TIMER_CTL_TBEVENT_M;
  375. HWREG(ulBase + TIMER_O_CTL) = ((HWREG(ulBase + TIMER_O_CTL) & ~ulTimer) |
  376. (ulEvent & ulTimer));
  377. }
  378. //*****************************************************************************
  379. //
  380. //! Controls the stall handling.
  381. //!
  382. //! \param ulBase is the base address of the timer module.
  383. //! \param ulTimer specifies the timer(s) to be adjusted; must be one of
  384. //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
  385. //! \param bStall specifies the response to a stall signal.
  386. //!
  387. //! This function controls the stall response for the specified timer. If the
  388. //! \e bStall parameter is \b true, then the timer stops counting if the
  389. //! processor enters debug mode; otherwise the timer keeps running while in
  390. //! debug mode.
  391. //!
  392. //! \return None.
  393. //
  394. //*****************************************************************************
  395. void
  396. TimerControlStall(unsigned long ulBase, unsigned long ulTimer,
  397. tBoolean bStall)
  398. {
  399. //
  400. // Check the arguments.
  401. //
  402. ASSERT(TimerBaseValid(ulBase));
  403. ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
  404. (ulTimer == TIMER_BOTH));
  405. //
  406. // Set the stall mode.
  407. //
  408. ulTimer &= TIMER_CTL_TASTALL | TIMER_CTL_TBSTALL;
  409. HWREG(ulBase + TIMER_O_CTL) = (bStall ?
  410. (HWREG(ulBase + TIMER_O_CTL) | ulTimer) :
  411. (HWREG(ulBase + TIMER_O_CTL) & ~(ulTimer)));
  412. }
  413. //*****************************************************************************
  414. //
  415. //! Controls the wait on trigger handling.
  416. //!
  417. //! \param ulBase is the base address of the timer module.
  418. //! \param ulTimer specifies the timer(s) to be adjusted; must be one of
  419. //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
  420. //! \param bWait specifies if the timer should wait for a trigger input.
  421. //!
  422. //! This function controls whether or not a timer waits for a trigger input to
  423. //! start counting. When enabled, the previous timer in the trigger chain must
  424. //! count to its timeout in order for this timer to start counting. Refer to
  425. //! the part's data sheet for a description of the trigger chain.
  426. //!
  427. //! \note This functionality is not available on all parts.
  428. //!
  429. //! \return None.
  430. //
  431. //*****************************************************************************
  432. void
  433. TimerControlWaitOnTrigger(unsigned long ulBase, unsigned long ulTimer,
  434. tBoolean bWait)
  435. {
  436. //
  437. // Check the arguments.
  438. //
  439. ASSERT(TimerBaseValid(ulBase));
  440. ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
  441. (ulTimer == TIMER_BOTH));
  442. //
  443. // Set the wait on trigger mode for timer A.
  444. //
  445. if((ulTimer & TIMER_A) != 0)
  446. {
  447. if(bWait)
  448. {
  449. HWREG(ulBase + TIMER_O_TAMR) |= TIMER_TAMR_TAWOT;
  450. }
  451. else
  452. {
  453. HWREG(ulBase + TIMER_O_TAMR) &= ~(TIMER_TAMR_TAWOT);
  454. }
  455. }
  456. //
  457. // Set the wait on trigger mode for timer B.
  458. //
  459. if((ulTimer & TIMER_B) != 0)
  460. {
  461. if(bWait)
  462. {
  463. HWREG(ulBase + TIMER_O_TBMR) |= TIMER_TBMR_TBWOT;
  464. }
  465. else
  466. {
  467. HWREG(ulBase + TIMER_O_TBMR) &= ~(TIMER_TBMR_TBWOT);
  468. }
  469. }
  470. }
  471. //*****************************************************************************
  472. //
  473. //! Enable RTC counting.
  474. //!
  475. //! \param ulBase is the base address of the timer module.
  476. //!
  477. //! This function causes the timer to start counting when in RTC mode. If not
  478. //! configured for RTC mode, this function does nothing.
  479. //!
  480. //! \return None.
  481. //
  482. //*****************************************************************************
  483. void
  484. TimerRTCEnable(unsigned long ulBase)
  485. {
  486. //
  487. // Check the arguments.
  488. //
  489. ASSERT(TimerBaseValid(ulBase));
  490. //
  491. // Enable RTC counting.
  492. //
  493. HWREG(ulBase + TIMER_O_CTL) |= TIMER_CTL_RTCEN;
  494. }
  495. //*****************************************************************************
  496. //
  497. //! Disable RTC counting.
  498. //!
  499. //! \param ulBase is the base address of the timer module.
  500. //!
  501. //! This function causes the timer to stop counting when in RTC mode.
  502. //!
  503. //! \return None.
  504. //
  505. //*****************************************************************************
  506. void
  507. TimerRTCDisable(unsigned long ulBase)
  508. {
  509. //
  510. // Check the arguments.
  511. //
  512. ASSERT(TimerBaseValid(ulBase));
  513. //
  514. // Disable RTC counting.
  515. //
  516. HWREG(ulBase + TIMER_O_CTL) &= ~(TIMER_CTL_RTCEN);
  517. }
  518. //*****************************************************************************
  519. //
  520. //! Set the timer prescale value.
  521. //!
  522. //! \param ulBase is the base address of the timer module.
  523. //! \param ulTimer specifies the timer(s) to adjust; must be one of \b TIMER_A,
  524. //! \b TIMER_B, or \b TIMER_BOTH.
  525. //! \param ulValue is the timer prescale value which must be between 0 and 255
  526. //! (inclusive) for 16/32-bit timers and between 0 and 65535 (inclusive) for
  527. //! 32/64-bit timers.
  528. //!
  529. //! This function sets the value of the input clock prescaler. The prescaler
  530. //! is only operational when in half-width mode and is used to extend the range
  531. //! of the half-width timer modes.
  532. //!
  533. //! \note The availability of the prescaler varies with the Stellaris part and
  534. //! timer mode in use. Please consult the datasheet for the part you are using
  535. //! to determine whether this support is available.
  536. //!
  537. //! \return None.
  538. //
  539. //*****************************************************************************
  540. void
  541. TimerPrescaleSet(unsigned long ulBase, unsigned long ulTimer,
  542. unsigned long ulValue)
  543. {
  544. //
  545. // Check the arguments.
  546. //
  547. ASSERT(TimerBaseValid(ulBase));
  548. ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
  549. (ulTimer == TIMER_BOTH));
  550. ASSERT(ulValue < 256);
  551. //
  552. // Set the timer A prescaler if requested.
  553. //
  554. if(ulTimer & TIMER_A)
  555. {
  556. HWREG(ulBase + TIMER_O_TAPR) = ulValue;
  557. }
  558. //
  559. // Set the timer B prescaler if requested.
  560. //
  561. if(ulTimer & TIMER_B)
  562. {
  563. HWREG(ulBase + TIMER_O_TBPR) = ulValue;
  564. }
  565. }
  566. //*****************************************************************************
  567. //
  568. //! Get the timer prescale value.
  569. //!
  570. //! \param ulBase is the base address of the timer module.
  571. //! \param ulTimer specifies the timer; must be one of \b TIMER_A or
  572. //! \b TIMER_B.
  573. //!
  574. //! This function gets the value of the input clock prescaler. The prescaler
  575. //! is only operational when in half-width mode and is used to extend the range
  576. //! of the half-width timer modes.
  577. //!
  578. //! \note The availability of the prescaler varies with the Stellaris part and
  579. //! timer mode in use. Please consult the datasheet for the part you are using
  580. //! to determine whether this support is available.
  581. //!
  582. //! \return The value of the timer prescaler.
  583. //
  584. //*****************************************************************************
  585. unsigned long
  586. TimerPrescaleGet(unsigned long ulBase, unsigned long ulTimer)
  587. {
  588. //
  589. // Check the arguments.
  590. //
  591. ASSERT(TimerBaseValid(ulBase));
  592. ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
  593. (ulTimer == TIMER_BOTH));
  594. //
  595. // Return the appropriate prescale value.
  596. //
  597. return((ulTimer == TIMER_A) ? HWREG(ulBase + TIMER_O_TAPR) :
  598. HWREG(ulBase + TIMER_O_TBPR));
  599. }
  600. //*****************************************************************************
  601. //
  602. //! Set the timer prescale match value.
  603. //!
  604. //! \param ulBase is the base address of the timer module.
  605. //! \param ulTimer specifies the timer(s) to adjust; must be one of \b TIMER_A,
  606. //! \b TIMER_B, or \b TIMER_BOTH.
  607. //! \param ulValue is the timer prescale match value which must be between 0
  608. //! and 255 (inclusive) for 16/32-bit timers and between 0 and 65535
  609. //! (inclusive) for 32/64-bit timers.
  610. //!
  611. //! This function sets the value of the input clock prescaler match value.
  612. //! When in a half-width mode that uses the counter match and the prescaler,
  613. //! the prescale match effectively extends the range of the match.
  614. //!
  615. //! \note The availability of the prescaler match varies with the Stellaris
  616. //! part and timer mode in use. Please consult the datasheet for the part you
  617. //! are using to determine whether this support is available.
  618. //!
  619. //! \return None.
  620. //
  621. //*****************************************************************************
  622. void
  623. TimerPrescaleMatchSet(unsigned long ulBase, unsigned long ulTimer,
  624. unsigned long ulValue)
  625. {
  626. //
  627. // Check the arguments.
  628. //
  629. ASSERT(TimerBaseValid(ulBase));
  630. ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
  631. (ulTimer == TIMER_BOTH));
  632. ASSERT(ulValue < 256);
  633. //
  634. // Set the timer A prescale match if requested.
  635. //
  636. if(ulTimer & TIMER_A)
  637. {
  638. HWREG(ulBase + TIMER_O_TAPMR) = ulValue;
  639. }
  640. //
  641. // Set the timer B prescale match if requested.
  642. //
  643. if(ulTimer & TIMER_B)
  644. {
  645. HWREG(ulBase + TIMER_O_TBPMR) = ulValue;
  646. }
  647. }
  648. //*****************************************************************************
  649. //
  650. //! Get the timer prescale match value.
  651. //!
  652. //! \param ulBase is the base address of the timer module.
  653. //! \param ulTimer specifies the timer; must be one of \b TIMER_A or
  654. //! \b TIMER_B.
  655. //!
  656. //! This function gets the value of the input clock prescaler match value.
  657. //! When in a half-width mode that uses the counter match and prescaler, the
  658. //! prescale match effectively extends the range of the match.
  659. //!
  660. //! \note The availability of the prescaler match varies with the Stellaris
  661. //! part and timer mode in use. Please consult the datasheet for the part you
  662. //! are using to determine whether this support is available.
  663. //!
  664. //! \return The value of the timer prescale match.
  665. //
  666. //*****************************************************************************
  667. unsigned long
  668. TimerPrescaleMatchGet(unsigned long ulBase, unsigned long ulTimer)
  669. {
  670. //
  671. // Check the arguments.
  672. //
  673. ASSERT(TimerBaseValid(ulBase));
  674. ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
  675. (ulTimer == TIMER_BOTH));
  676. //
  677. // Return the appropriate prescale match value.
  678. //
  679. return((ulTimer == TIMER_A) ? HWREG(ulBase + TIMER_O_TAPMR) :
  680. HWREG(ulBase + TIMER_O_TBPMR));
  681. }
  682. //*****************************************************************************
  683. //
  684. //! Sets the timer load value.
  685. //!
  686. //! \param ulBase is the base address of the timer module.
  687. //! \param ulTimer specifies the timer(s) to adjust; must be one of \b TIMER_A,
  688. //! \b TIMER_B, or \b TIMER_BOTH. Only \b TIMER_A should be used when the
  689. //! timer is configured for full-width operation.
  690. //! \param ulValue is the load value.
  691. //!
  692. //! This function sets the timer load value; if the timer is running then the
  693. //! value is immediately loaded into the timer.
  694. //!
  695. //! \note This function can be used for both full- and half-width modes of
  696. //! 16/32-bit timers, and for half-width modes of 32/64-bit timers. Use
  697. //! TimerLoadSet64() for full-width modes of 32/64-bit timers.
  698. //!
  699. //! \return None.
  700. //
  701. //*****************************************************************************
  702. void
  703. TimerLoadSet(unsigned long ulBase, unsigned long ulTimer,
  704. unsigned long ulValue)
  705. {
  706. //
  707. // Check the arguments.
  708. //
  709. ASSERT(TimerBaseValid(ulBase));
  710. ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
  711. (ulTimer == TIMER_BOTH));
  712. //
  713. // Set the timer A load value if requested.
  714. //
  715. if(ulTimer & TIMER_A)
  716. {
  717. HWREG(ulBase + TIMER_O_TAILR) = ulValue;
  718. }
  719. //
  720. // Set the timer B load value if requested.
  721. //
  722. if(ulTimer & TIMER_B)
  723. {
  724. HWREG(ulBase + TIMER_O_TBILR) = ulValue;
  725. }
  726. }
  727. //*****************************************************************************
  728. //
  729. //! Gets the timer load value.
  730. //!
  731. //! \param ulBase is the base address of the timer module.
  732. //! \param ulTimer specifies the timer; must be one of \b TIMER_A or
  733. //! \b TIMER_B. Only \b TIMER_A should be used when the timer is configured
  734. //! for full-width operation.
  735. //!
  736. //! This function gets the currently programmed interval load value for the
  737. //! specified timer.
  738. //!
  739. //! \note This function can be used for both full- and half-width modes of
  740. //! 16/32-bit timers, and for half-width modes of 32/64-bit timers. Use
  741. //! TimerLoadGet64() for full-width modes of 32/64-bit timers.
  742. //!
  743. //! \return Returns the load value for the timer.
  744. //
  745. //*****************************************************************************
  746. unsigned long
  747. TimerLoadGet(unsigned long ulBase, unsigned long ulTimer)
  748. {
  749. //
  750. // Check the arguments.
  751. //
  752. ASSERT(TimerBaseValid(ulBase));
  753. ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B));
  754. //
  755. // Return the appropriate load value.
  756. //
  757. return((ulTimer == TIMER_A) ? HWREG(ulBase + TIMER_O_TAILR) :
  758. HWREG(ulBase + TIMER_O_TBILR));
  759. }
  760. //*****************************************************************************
  761. //
  762. //! Sets the timer load value for a 64-bit timer.
  763. //!
  764. //! \param ulBase is the base address of the timer module.
  765. //! \param ullValue is the load value.
  766. //!
  767. //! This function sets the timer load value for a 64-bit timer; if the timer is
  768. //! running then the value is immediately loaded into the timer.
  769. //!
  770. //! \return None.
  771. //
  772. //*****************************************************************************
  773. void
  774. TimerLoadSet64(unsigned long ulBase, unsigned long long ullValue)
  775. {
  776. //
  777. // Check the arguments.
  778. //
  779. ASSERT(TimerBaseValid(ulBase));
  780. //
  781. // Set the timer load value. The upper 32-bits must be written before the
  782. // lower 32-bits in order to adhere to the hardware interlocks on the
  783. // 64-bit value.
  784. //
  785. HWREG(ulBase + TIMER_O_TBILR) = ullValue >> 32;
  786. HWREG(ulBase + TIMER_O_TAILR) = ullValue & 0xffffffff;
  787. }
  788. //*****************************************************************************
  789. //
  790. //! Gets the timer load value for a 64-bit timer.
  791. //!
  792. //! \param ulBase is the base address of the timer module.
  793. //!
  794. //! This function gets the currently programmed interval load value for the
  795. //! specified 64-bit timer.
  796. //!
  797. //! \return Returns the load value for the timer.
  798. //
  799. //*****************************************************************************
  800. unsigned long long
  801. TimerLoadGet64(unsigned long ulBase)
  802. {
  803. unsigned long ulHigh1, ulHigh2, ulLow;
  804. //
  805. // Check the arguments.
  806. //
  807. ASSERT(TimerBaseValid(ulBase));
  808. //
  809. // Read the 64-bit load value. A read of the low 32-bits is performed
  810. // between two reads of the upper 32-bits; if the upper 32-bit values match
  811. // then the 64-bit value is consistent. If they do not match, then the
  812. // read is performed again until they do match (it should never execute the
  813. // loop body more than twice).
  814. //
  815. do
  816. {
  817. ulHigh1 = HWREG(ulBase + TIMER_O_TBILR);
  818. ulLow = HWREG(ulBase + TIMER_O_TAILR);
  819. ulHigh2 = HWREG(ulBase + TIMER_O_TBILR);
  820. }
  821. while(ulHigh1 != ulHigh2);
  822. //
  823. // Return the load value.
  824. //
  825. return(((unsigned long long)ulHigh1 << 32) | (unsigned long long)ulLow);
  826. }
  827. //*****************************************************************************
  828. //
  829. //! Gets the current timer value.
  830. //!
  831. //! \param ulBase is the base address of the timer module.
  832. //! \param ulTimer specifies the timer; must be one of \b TIMER_A or
  833. //! \b TIMER_B. Only \b TIMER_A should be used when the timer is configured
  834. //! for full-width operation.
  835. //!
  836. //! This function reads the current value of the specified timer.
  837. //!
  838. //! \note This function can be used for both full- and half-width modes of
  839. //! 16/32-bit timers, and for half-width modes of 32/64-bit timers. Use
  840. //! TimerValueGet64() for full-width modes of 32/64-bit timers.
  841. //!
  842. //! \return Returns the current value of the timer.
  843. //
  844. //*****************************************************************************
  845. unsigned long
  846. TimerValueGet(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. //
  854. // Return the appropriate timer value.
  855. //
  856. return((ulTimer == TIMER_A) ? HWREG(ulBase + TIMER_O_TAR) :
  857. HWREG(ulBase + TIMER_O_TBR));
  858. }
  859. //*****************************************************************************
  860. //
  861. //! Gets the current 64-bit timer value.
  862. //!
  863. //! \param ulBase is the base address of the timer module.
  864. //!
  865. //! This function reads the current value of the specified timer.
  866. //!
  867. //! \return Returns the current value of the timer.
  868. //
  869. //*****************************************************************************
  870. unsigned long long
  871. TimerValueGet64(unsigned long ulBase)
  872. {
  873. unsigned long ulHigh1, ulHigh2, ulLow;
  874. //
  875. // Check the arguments.
  876. //
  877. ASSERT(TimerBaseValid(ulBase));
  878. //
  879. // Read the 64-bit timer value. A read of the low 32-bits is performed
  880. // between two reads of the upper 32-bits; if the upper 32-bit values match
  881. // then the 64-bit value is consistent. If they do not match, then the
  882. // read is performed again until they do match (it should never execute the
  883. // loop body more than twice).
  884. //
  885. do
  886. {
  887. ulHigh1 = HWREG(ulBase + TIMER_O_TBR);
  888. ulLow = HWREG(ulBase + TIMER_O_TAR);
  889. ulHigh2 = HWREG(ulBase + TIMER_O_TBR);
  890. }
  891. while(ulHigh1 != ulHigh2);
  892. //
  893. // Return the timer value.
  894. //
  895. return(((unsigned long long)ulHigh1 << 32) | (unsigned long long)ulLow);
  896. }
  897. //*****************************************************************************
  898. //
  899. //! Sets the timer match value.
  900. //!
  901. //! \param ulBase is the base address of the timer module.
  902. //! \param ulTimer specifies the timer(s) to adjust; must be one of \b TIMER_A,
  903. //! \b TIMER_B, or \b TIMER_BOTH. Only \b TIMER_A should be used when the
  904. //! timer is configured for full-width operation.
  905. //! \param ulValue is the match value.
  906. //!
  907. //! This function sets the match value for a timer. This value is used in
  908. //! capture count mode to determine when to interrupt the processor and in PWM
  909. //! mode to determine the duty cycle of the output signal.
  910. //!
  911. //! \note This function can be used for both full- and half-width modes of
  912. //! 16/32-bit timers, and for half-width modes of 32/64-bit timers. Use
  913. //! TimerMatchSet64() for full-width modes of 32/64-bit timers.
  914. //!
  915. //! \return None.
  916. //
  917. //*****************************************************************************
  918. void
  919. TimerMatchSet(unsigned long ulBase, unsigned long ulTimer,
  920. unsigned long ulValue)
  921. {
  922. //
  923. // Check the arguments.
  924. //
  925. ASSERT(TimerBaseValid(ulBase));
  926. ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
  927. (ulTimer == TIMER_BOTH));
  928. //
  929. // Set the timer A match value if requested.
  930. //
  931. if(ulTimer & TIMER_A)
  932. {
  933. HWREG(ulBase + TIMER_O_TAMATCHR) = ulValue;
  934. }
  935. //
  936. // Set the timer B match value if requested.
  937. //
  938. if(ulTimer & TIMER_B)
  939. {
  940. HWREG(ulBase + TIMER_O_TBMATCHR) = ulValue;
  941. }
  942. }
  943. //*****************************************************************************
  944. //
  945. //! Gets the timer match value.
  946. //!
  947. //! \param ulBase is the base address of the timer module.
  948. //! \param ulTimer specifies the timer; must be one of \b TIMER_A or
  949. //! \b TIMER_B. Only \b TIMER_A should be used when the timer is configured
  950. //! for full-width operation.
  951. //!
  952. //! This function gets the match value for the specified timer.
  953. //!
  954. //! \note This function can be used for both full- and half-width modes of
  955. //! 16/32-bit timers, and for half-width modes of 32/64-bit timers. Use
  956. //! TimerMatchGet64() for full-width modes of 32/64-bit timers.
  957. //!
  958. //! \return Returns the match value for the timer.
  959. //
  960. //*****************************************************************************
  961. unsigned long
  962. TimerMatchGet(unsigned long ulBase, unsigned long ulTimer)
  963. {
  964. //
  965. // Check the arguments.
  966. //
  967. ASSERT(TimerBaseValid(ulBase));
  968. ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B));
  969. //
  970. // Return the appropriate match value.
  971. //
  972. return((ulTimer == TIMER_A) ? HWREG(ulBase + TIMER_O_TAMATCHR) :
  973. HWREG(ulBase + TIMER_O_TBMATCHR));
  974. }
  975. //*****************************************************************************
  976. //
  977. //! Sets the timer match value for a 64-bit timer.
  978. //!
  979. //! \param ulBase is the base address of the timer module.
  980. //! \param ullValue is the match value.
  981. //!
  982. //! This function sets the match value for a timer. This value is used in
  983. //! capture count mode to determine when to interrupt the processor and in PWM
  984. //! mode to determine the duty cycle of the output signal.
  985. //!
  986. //! \return None.
  987. //
  988. //*****************************************************************************
  989. void
  990. TimerMatchSet64(unsigned long ulBase, unsigned long long ullValue)
  991. {
  992. //
  993. // Check the arguments.
  994. //
  995. ASSERT(TimerBaseValid(ulBase));
  996. //
  997. // Set the timer match value. The upper 32-bits must be written before the
  998. // lower 32-bits in order to adhere to the hardware interlocks on the
  999. // 64-bit value.
  1000. //
  1001. HWREG(ulBase + TIMER_O_TBMATCHR) = ullValue >> 32;
  1002. HWREG(ulBase + TIMER_O_TAMATCHR) = ullValue & 0xffffffff;
  1003. }
  1004. //*****************************************************************************
  1005. //
  1006. //! Gets the timer match value for a 64-bit timer.
  1007. //!
  1008. //! \param ulBase is the base address of the timer module.
  1009. //!
  1010. //! This function gets the match value for the specified timer.
  1011. //!
  1012. //! \return Returns the match value for the timer.
  1013. //
  1014. //*****************************************************************************
  1015. unsigned long long
  1016. TimerMatchGet64(unsigned long ulBase)
  1017. {
  1018. unsigned long ulHigh1, ulHigh2, ulLow;
  1019. //
  1020. // Check the arguments.
  1021. //
  1022. ASSERT(TimerBaseValid(ulBase));
  1023. //
  1024. // Read the 64-bit match value. A read of the low 32-bits is performed
  1025. // between two reads of the upper 32-bits; if the upper 32-bit values match
  1026. // then the 64-bit value is consistent. If they do not match, then the
  1027. // read is performed again until they do match (it should never execute the
  1028. // loop body more than twice).
  1029. //
  1030. do
  1031. {
  1032. ulHigh1 = HWREG(ulBase + TIMER_O_TBMATCHR);
  1033. ulLow = HWREG(ulBase + TIMER_O_TAMATCHR);
  1034. ulHigh2 = HWREG(ulBase + TIMER_O_TBMATCHR);
  1035. }
  1036. while(ulHigh1 != ulHigh2);
  1037. //
  1038. // Return the match value.
  1039. //
  1040. return(((unsigned long long)ulHigh1 << 32) | (unsigned long long)ulLow);
  1041. }
  1042. //*****************************************************************************
  1043. //
  1044. //! Registers an interrupt handler for the timer interrupt.
  1045. //!
  1046. //! \param ulBase is the base address of the timer module.
  1047. //! \param ulTimer specifies the timer(s); must be one of \b TIMER_A,
  1048. //! \b TIMER_B, or \b TIMER_BOTH.
  1049. //! \param pfnHandler is a pointer to the function to be called when the timer
  1050. //! interrupt occurs.
  1051. //!
  1052. //! This function sets the handler to be called when a timer interrupt occurs.
  1053. //! In addition, this function enables the global interrupt in the interrupt
  1054. //! controller; specific timer interrupts must be enabled via TimerIntEnable().
  1055. //! It is the interrupt handler's responsibility to clear the interrupt source
  1056. //! via TimerIntClear().
  1057. //!
  1058. //! \sa IntRegister() for important information about registering interrupt
  1059. //! handlers.
  1060. //!
  1061. //! \return None.
  1062. //
  1063. //*****************************************************************************
  1064. void
  1065. TimerIntRegister(unsigned long ulBase, unsigned long ulTimer,
  1066. void (*pfnHandler)(void))
  1067. {
  1068. //
  1069. // Check the arguments.
  1070. //
  1071. ASSERT(TimerBaseValid(ulBase));
  1072. ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
  1073. (ulTimer == TIMER_BOTH));
  1074. //
  1075. // Get the interrupt number for this timer module.
  1076. //
  1077. ulBase = TimerIntNumberGet(ulBase);
  1078. //
  1079. // Register an interrupt handler for timer A if requested.
  1080. //
  1081. if(ulTimer & TIMER_A)
  1082. {
  1083. //
  1084. // Register the interrupt handler.
  1085. //
  1086. IntRegister(ulBase, pfnHandler);
  1087. //
  1088. // Enable the interrupt.
  1089. //
  1090. IntEnable(ulBase);
  1091. }
  1092. //
  1093. // Register an interrupt handler for timer B if requested.
  1094. //
  1095. if(ulTimer & TIMER_B)
  1096. {
  1097. //
  1098. // Register the interrupt handler.
  1099. //
  1100. IntRegister(ulBase + 1, pfnHandler);
  1101. //
  1102. // Enable the interrupt.
  1103. //
  1104. IntEnable(ulBase + 1);
  1105. }
  1106. }
  1107. //*****************************************************************************
  1108. //
  1109. //! Unregisters an interrupt handler for the timer interrupt.
  1110. //!
  1111. //! \param ulBase is the base address of the timer module.
  1112. //! \param ulTimer specifies the timer(s); must be one of \b TIMER_A,
  1113. //! \b TIMER_B, or \b TIMER_BOTH.
  1114. //!
  1115. //! This function clears the handler to be called when a timer interrupt
  1116. //! occurs. This function also masks off the interrupt in the interrupt
  1117. //! controller so that the interrupt handler no longer is called.
  1118. //!
  1119. //! \sa IntRegister() for important information about registering interrupt
  1120. //! handlers.
  1121. //!
  1122. //! \return None.
  1123. //
  1124. //*****************************************************************************
  1125. void
  1126. TimerIntUnregister(unsigned long ulBase, unsigned long ulTimer)
  1127. {
  1128. //
  1129. // Check the arguments.
  1130. //
  1131. ASSERT(TimerBaseValid(ulBase));
  1132. ASSERT((ulTimer == TIMER_A) || (ulTimer == TIMER_B) ||
  1133. (ulTimer == TIMER_BOTH));
  1134. //
  1135. // Get the interrupt number for this timer module.
  1136. //
  1137. ulBase = TimerIntNumberGet(ulBase);
  1138. //
  1139. // Unregister the interrupt handler for timer A if requested.
  1140. //
  1141. if(ulTimer & TIMER_A)
  1142. {
  1143. //
  1144. // Disable the interrupt.
  1145. //
  1146. IntDisable(ulBase);
  1147. //
  1148. // Unregister the interrupt handler.
  1149. //
  1150. IntUnregister(ulBase);
  1151. }
  1152. //
  1153. // Unregister the interrupt handler for timer B if requested.
  1154. //
  1155. if(ulTimer & TIMER_B)
  1156. {
  1157. //
  1158. // Disable the interrupt.
  1159. //
  1160. IntDisable(ulBase + 1);
  1161. //
  1162. // Unregister the interrupt handler.
  1163. //
  1164. IntUnregister(ulBase + 1);
  1165. }
  1166. }
  1167. //*****************************************************************************
  1168. //
  1169. //! Enables individual timer interrupt sources.
  1170. //!
  1171. //! \param ulBase is the base address of the timer module.
  1172. //! \param ulIntFlags is the bit mask of the interrupt sources to be enabled.
  1173. //!
  1174. //! Enables the indicated timer interrupt sources. Only the sources that are
  1175. //! enabled can be reflected to the processor interrupt; disabled sources have
  1176. //! no effect on the processor.
  1177. //!
  1178. //! The \e ulIntFlags parameter must be the logical OR of any combination of
  1179. //! the following:
  1180. //!
  1181. //! - \b TIMER_CAPB_EVENT - Capture B event interrupt
  1182. //! - \b TIMER_CAPB_MATCH - Capture B match interrupt
  1183. //! - \b TIMER_TIMB_TIMEOUT - Timer B timeout interrupt
  1184. //! - \b TIMER_RTC_MATCH - RTC interrupt mask
  1185. //! - \b TIMER_CAPA_EVENT - Capture A event interrupt
  1186. //! - \b TIMER_CAPA_MATCH - Capture A match interrupt
  1187. //! - \b TIMER_TIMA_TIMEOUT - Timer A timeout interrupt
  1188. //!
  1189. //! \return None.
  1190. //
  1191. //*****************************************************************************
  1192. void
  1193. TimerIntEnable(unsigned long ulBase, unsigned long ulIntFlags)
  1194. {
  1195. //
  1196. // Check the arguments.
  1197. //
  1198. ASSERT(TimerBaseValid(ulBase));
  1199. //
  1200. // Enable the specified interrupts.
  1201. //
  1202. HWREG(ulBase + TIMER_O_IMR) |= ulIntFlags;
  1203. }
  1204. //*****************************************************************************
  1205. //
  1206. //! Disables individual timer interrupt sources.
  1207. //!
  1208. //! \param ulBase is the base address of the timer module.
  1209. //! \param ulIntFlags is the bit mask of the interrupt sources to be disabled.
  1210. //!
  1211. //! Disables the indicated timer interrupt sources. Only the sources that are
  1212. //! enabled can be reflected to the processor interrupt; disabled sources have
  1213. //! no effect on the processor.
  1214. //!
  1215. //! The \e ulIntFlags parameter has the same definition as the \e ulIntFlags
  1216. //! parameter to TimerIntEnable().
  1217. //!
  1218. //! \return None.
  1219. //
  1220. //*****************************************************************************
  1221. void
  1222. TimerIntDisable(unsigned long ulBase, unsigned long ulIntFlags)
  1223. {
  1224. //
  1225. // Check the arguments.
  1226. //
  1227. ASSERT(TimerBaseValid(ulBase));
  1228. //
  1229. // Disable the specified interrupts.
  1230. //
  1231. HWREG(ulBase + TIMER_O_IMR) &= ~(ulIntFlags);
  1232. }
  1233. //*****************************************************************************
  1234. //
  1235. //! Gets the current interrupt status.
  1236. //!
  1237. //! \param ulBase is the base address of the timer module.
  1238. //! \param bMasked is false if the raw interrupt status is required and true if
  1239. //! the masked interrupt status is required.
  1240. //!
  1241. //! This function returns the interrupt status for the timer module. Either
  1242. //! the raw interrupt status or the status of interrupts that are allowed to
  1243. //! reflect to the processor can be returned.
  1244. //!
  1245. //! \return The current interrupt status, enumerated as a bit field of
  1246. //! values described in TimerIntEnable().
  1247. //
  1248. //*****************************************************************************
  1249. unsigned long
  1250. TimerIntStatus(unsigned long ulBase, tBoolean bMasked)
  1251. {
  1252. //
  1253. // Check the arguments.
  1254. //
  1255. ASSERT(TimerBaseValid(ulBase));
  1256. //
  1257. // Return either the interrupt status or the raw interrupt status as
  1258. // requested.
  1259. //
  1260. return(bMasked ? HWREG(ulBase + TIMER_O_MIS) :
  1261. HWREG(ulBase + TIMER_O_RIS));
  1262. }
  1263. //*****************************************************************************
  1264. //
  1265. //! Clears timer interrupt sources.
  1266. //!
  1267. //! \param ulBase is the base address of the timer module.
  1268. //! \param ulIntFlags is a bit mask of the interrupt sources to be cleared.
  1269. //!
  1270. //! The specified timer interrupt sources are cleared, so that they no longer
  1271. //! assert. This function must be called in the interrupt handler to keep the
  1272. //! interrupt from being triggered again immediately upon exit.
  1273. //!
  1274. //! The \e ulIntFlags parameter has the same definition as the \e ulIntFlags
  1275. //! parameter to TimerIntEnable().
  1276. //!
  1277. //! \note Because there is a write buffer in the Cortex-M3 processor, it may
  1278. //! take several clock cycles before the interrupt source is actually cleared.
  1279. //! Therefore, it is recommended that the interrupt source be cleared early in
  1280. //! the interrupt handler (as opposed to the very last action) to avoid
  1281. //! returning from the interrupt handler before the interrupt source is
  1282. //! actually cleared. Failure to do so may result in the interrupt handler
  1283. //! being immediately reentered (because the interrupt controller still sees
  1284. //! the interrupt source asserted).
  1285. //!
  1286. //! \return None.
  1287. //
  1288. //*****************************************************************************
  1289. void
  1290. TimerIntClear(unsigned long ulBase, unsigned long ulIntFlags)
  1291. {
  1292. //
  1293. // Check the arguments.
  1294. //
  1295. ASSERT(TimerBaseValid(ulBase));
  1296. //
  1297. // Clear the requested interrupt sources.
  1298. //
  1299. HWREG(ulBase + TIMER_O_ICR) = ulIntFlags;
  1300. }
  1301. //*****************************************************************************
  1302. //
  1303. //! Synchronizes the counters in a set of timers.
  1304. //!
  1305. //! \param ulBase is the base address of the timer module. This must be the
  1306. //! base address of Timer0 (in other words, \b TIMER0_BASE).
  1307. //! \param ulTimers is the set of timers to synchronize.
  1308. //!
  1309. //! This function will synchronize the counters in a specified set of timers.
  1310. //! When a timer is running in half-width mode, each half can be included or
  1311. //! excluded in the synchronization event. When a timer is running in
  1312. //! full-width mode, only the A timer can be synchronized (specifying the B
  1313. //! timer has no effect).
  1314. //!
  1315. //! The \e ulTimers parameter is the logical OR of any of the following
  1316. //! defines:
  1317. //!
  1318. //! - \b TIMER_0A_SYNC
  1319. //! - \b TIMER_0B_SYNC
  1320. //! - \b TIMER_1A_SYNC
  1321. //! - \b TIMER_1B_SYNC
  1322. //! - \b TIMER_2A_SYNC
  1323. //! - \b TIMER_2B_SYNC
  1324. //! - \b TIMER_3A_SYNC
  1325. //! - \b TIMER_3B_SYNC
  1326. //! - \b TIMER_4A_SYNC
  1327. //! - \b TIMER_4B_SYNC
  1328. //! - \b TIMER_5A_SYNC
  1329. //! - \b TIMER_5B_SYNC
  1330. //! - \b WTIMER_0A_SYNC
  1331. //! - \b WTIMER_0B_SYNC
  1332. //! - \b WTIMER_1A_SYNC
  1333. //! - \b WTIMER_1B_SYNC
  1334. //! - \b WTIMER_2A_SYNC
  1335. //! - \b WTIMER_2B_SYNC
  1336. //! - \b WTIMER_3A_SYNC
  1337. //! - \b WTIMER_3B_SYNC
  1338. //! - \b WTIMER_4A_SYNC
  1339. //! - \b WTIMER_4B_SYNC
  1340. //! - \b WTIMER_5A_SYNC
  1341. //! - \b WTIMER_5B_SYNC
  1342. //!
  1343. //! \note This functionality is not available on all parts.
  1344. //!
  1345. //! \return None.
  1346. //
  1347. //*****************************************************************************
  1348. void
  1349. TimerSynchronize(unsigned long ulBase, unsigned long ulTimers)
  1350. {
  1351. //
  1352. // Check the arguments.
  1353. //
  1354. ASSERT(ulBase == TIMER0_BASE);
  1355. //
  1356. // Synchronize the specified timers.
  1357. //
  1358. HWREG(ulBase + TIMER_O_SYNC) = ulTimers;
  1359. }
  1360. //*****************************************************************************
  1361. //
  1362. // Puts the timer into its reset state.
  1363. //
  1364. // \param ulBase is the base address of the timer module.
  1365. //
  1366. // The specified timer is disabled, and all its interrupts are disabled,
  1367. // cleared, and unregistered. Then the timer registers are set to their reset
  1368. // value.
  1369. //
  1370. // \return None.
  1371. //
  1372. //*****************************************************************************
  1373. #ifndef DEPRECATED
  1374. void
  1375. TimerQuiesce(unsigned long ulBase)
  1376. {
  1377. //
  1378. // Check the arguments.
  1379. //
  1380. ASSERT(TimerBaseValid(ulBase));
  1381. //
  1382. // Disable the timer.
  1383. //
  1384. HWREG(ulBase + TIMER_O_CTL) = TIMER_RV_CTL;
  1385. //
  1386. // Disable all the timer interrupts.
  1387. //
  1388. HWREG(ulBase + TIMER_O_IMR) = TIMER_RV_IMR;
  1389. //
  1390. // Clear all the timer interrupts.
  1391. //
  1392. HWREG(ulBase + TIMER_O_ICR) = 0xFFFFFFFF;
  1393. //
  1394. // Unregister the interrupt handler. This also disables interrupts to the
  1395. // core.
  1396. //
  1397. TimerIntUnregister(ulBase, TIMER_BOTH);
  1398. //
  1399. // Set all the registers to their reset value.
  1400. //
  1401. HWREG(ulBase + TIMER_O_CFG) = TIMER_RV_CFG;
  1402. HWREG(ulBase + TIMER_O_TAMR) = TIMER_RV_TAMR;
  1403. HWREG(ulBase + TIMER_O_TBMR) = TIMER_RV_TBMR;
  1404. HWREG(ulBase + TIMER_O_RIS) = TIMER_RV_RIS;
  1405. HWREG(ulBase + TIMER_O_MIS) = TIMER_RV_MIS;
  1406. HWREG(ulBase + TIMER_O_TAILR) = TIMER_RV_TAILR;
  1407. HWREG(ulBase + TIMER_O_TBILR) = TIMER_RV_TBILR;
  1408. HWREG(ulBase + TIMER_O_TAMATCHR) = TIMER_RV_TAMATCHR;
  1409. HWREG(ulBase + TIMER_O_TBMATCHR) = TIMER_RV_TBMATCHR;
  1410. HWREG(ulBase + TIMER_O_TAPR) = TIMER_RV_TAPR;
  1411. HWREG(ulBase + TIMER_O_TBPR) = TIMER_RV_TBPR;
  1412. HWREG(ulBase + TIMER_O_TAPMR) = TIMER_RV_TAPMR;
  1413. HWREG(ulBase + TIMER_O_TBPMR) = TIMER_RV_TBPMR;
  1414. HWREG(ulBase + TIMER_O_TAR) = TIMER_RV_TAR;
  1415. HWREG(ulBase + TIMER_O_TBR) = TIMER_RV_TBR;
  1416. }
  1417. #endif // DEPRECATED
  1418. //*****************************************************************************
  1419. //
  1420. // Close the Doxygen group.
  1421. //! @}
  1422. //
  1423. //*****************************************************************************