timer.c 49 KB

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