timer.c 35 KB

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