timer.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. //*****************************************************************************
  2. //
  3. // timer.c - Driver for the timer module.
  4. //
  5. // Copyright (c) 2005-2017 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions
  10. // are met:
  11. //
  12. // Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. //
  15. // Redistributions in binary form must reproduce the above copyright
  16. // notice, this list of conditions and the following disclaimer in the
  17. // documentation and/or other materials provided with the
  18. // distribution.
  19. //
  20. // Neither the name of Texas Instruments Incorporated nor the names of
  21. // its contributors may be used to endorse or promote products derived
  22. // from this software without specific prior written permission.
  23. //
  24. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //*****************************************************************************
  37. //*****************************************************************************
  38. //
  39. //! \addtogroup timer_api
  40. //! @{
  41. //
  42. //*****************************************************************************
  43. #include <ti/devices/msp432e4/inc/msp432e411y.h>
  44. #include "types.h"
  45. #include <stdbool.h>
  46. #include <stdint.h>
  47. #include "inc/hw_timer.h"
  48. #include "inc/hw_sysctl.h"
  49. #include "debug.h"
  50. #include "interrupt.h"
  51. #include "timer.h"
  52. //*****************************************************************************
  53. //
  54. // A mapping of timer base address to interrupt number.
  55. //
  56. //*****************************************************************************
  57. static const uint32_t g_ppui32TimerIntMap[][2] =
  58. {
  59. { TIMER0_BASE, INT_TIMER0A },
  60. { TIMER1_BASE, INT_TIMER1A },
  61. { TIMER2_BASE, INT_TIMER2A },
  62. { TIMER3_BASE, INT_TIMER3A },
  63. { TIMER4_BASE, INT_TIMER4A },
  64. { TIMER5_BASE, INT_TIMER5A },
  65. { TIMER6_BASE, INT_TIMER6A },
  66. { TIMER7_BASE, INT_TIMER7A },
  67. };
  68. static const uint_fast8_t g_ui8TimerIntMapRows =
  69. sizeof(g_ppui32TimerIntMap) / sizeof(g_ppui32TimerIntMap[0]);
  70. //*****************************************************************************
  71. //
  72. //! \internal
  73. //! Checks a timer base address.
  74. //!
  75. //! \param ui32Base is the base address of the timer module.
  76. //!
  77. //! This function determines if a timer module base address is valid.
  78. //!
  79. //! \return Returns \b true if the base address is valid and \b false
  80. //! otherwise.
  81. //
  82. //*****************************************************************************
  83. #ifdef DEBUG
  84. static bool
  85. _TimerBaseValid(uint32_t ui32Base)
  86. {
  87. return ((ui32Base == TIMER0_BASE) || (ui32Base == TIMER1_BASE) ||
  88. (ui32Base == TIMER2_BASE) || (ui32Base == TIMER3_BASE) ||
  89. (ui32Base == TIMER4_BASE) || (ui32Base == TIMER5_BASE) ||
  90. (ui32Base == TIMER6_BASE) || (ui32Base == TIMER7_BASE));
  91. }
  92. #endif
  93. //*****************************************************************************
  94. //
  95. //! Returns a timer modules interrupt number.
  96. //!
  97. //! \param ui32Base is the base address of the selected timer.
  98. //! \param ui32Timer specifies the timer(s) to enable; must be one of
  99. //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
  100. //!
  101. //! This function returns the interrupt number for a given timer module
  102. //! specified by the \e ui32Base and \e ui32Timer parameter.
  103. //!
  104. //! \return Returns a timer module's interrupt number or 0 if the interrupt
  105. //! does not exist.
  106. //
  107. //*****************************************************************************
  108. static uint32_t
  109. _TimerIntNumberGet(uint32_t ui32Base, uint32_t ui32Timer)
  110. {
  111. uint32_t ui32Int;
  112. uint_fast8_t ui8Idx, ui8Rows;
  113. const uint32_t (*ppui32SSIIntMap)[2];
  114. //
  115. // Default interrupt map.
  116. //
  117. ppui32SSIIntMap = g_ppui32TimerIntMap;
  118. ui8Rows = g_ui8TimerIntMapRows;
  119. //
  120. // Loop through the table that maps timer base addresses to interrupt
  121. // numbers.
  122. //
  123. for (ui8Idx = 0; ui8Idx < ui8Rows; ui8Idx++)
  124. {
  125. //
  126. // See if this base address matches.
  127. //
  128. if (ppui32SSIIntMap[ui8Idx][0] == ui32Base)
  129. {
  130. ui32Int = ppui32SSIIntMap[ui8Idx][1];
  131. if (ui32Timer == TIMER_B)
  132. {
  133. ui32Int += 1;
  134. }
  135. //
  136. // Return the corresponding interrupt number.
  137. //
  138. return (ui32Int);
  139. }
  140. }
  141. //
  142. // The base address could not be found, so return an error.
  143. //
  144. return (0);
  145. }
  146. //*****************************************************************************
  147. //
  148. //! Enables the timer(s).
  149. //!
  150. //! \param ui32Base is the base address of the timer module.
  151. //! \param ui32Timer specifies the timer(s) to enable; must be one of
  152. //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
  153. //!
  154. //! This function enables operation of the timer module. The timer must be
  155. //! configured before it is enabled.
  156. //!
  157. //! \return None.
  158. //
  159. //*****************************************************************************
  160. void
  161. TimerEnable(uint32_t ui32Base, uint32_t ui32Timer)
  162. {
  163. //
  164. // Check the arguments.
  165. //
  166. ASSERT(_TimerBaseValid(ui32Base));
  167. ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
  168. (ui32Timer == TIMER_BOTH));
  169. //
  170. // Enable the timer(s) module.
  171. //
  172. HWREG(ui32Base + TIMER_O_CTL) |= ui32Timer & (TIMER_CTL_TAEN |
  173. TIMER_CTL_TBEN);
  174. }
  175. //*****************************************************************************
  176. //
  177. //! Disables the timer(s).
  178. //!
  179. //! \param ui32Base is the base address of the timer module.
  180. //! \param ui32Timer specifies the timer(s) to disable; must be one of
  181. //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
  182. //!
  183. //! This function disables operation of the timer module.
  184. //!
  185. //! \return None.
  186. //
  187. //*****************************************************************************
  188. void
  189. TimerDisable(uint32_t ui32Base, uint32_t ui32Timer)
  190. {
  191. //
  192. // Check the arguments.
  193. //
  194. ASSERT(_TimerBaseValid(ui32Base));
  195. ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
  196. (ui32Timer == TIMER_BOTH));
  197. //
  198. // Disable the timer module.
  199. //
  200. HWREG(ui32Base + TIMER_O_CTL) &= ~(ui32Timer &
  201. (TIMER_CTL_TAEN | TIMER_CTL_TBEN));
  202. }
  203. //*****************************************************************************
  204. //
  205. //! Configures the timer(s).
  206. //!
  207. //! \param ui32Base is the base address of the timer module.
  208. //! \param ui32Config is the configuration for the timer.
  209. //!
  210. //! This function configures the operating mode of the timer(s). The timer
  211. //! module is disabled before being configured and is left in the disabled
  212. //! state. The timer can be configured to be a single full-width timer
  213. //! by using the \b TIMER_CFG_* values or a pair of half-width timers using the
  214. //! \b TIMER_CFG_A_* and \b TIMER_CFG_B_* values passed in the \e ui32Config
  215. //! parameter.
  216. //!
  217. //! The configuration is specified in \e ui32Config as one of the following
  218. //! values:
  219. //!
  220. //! - \b TIMER_CFG_ONE_SHOT - Full-width one-shot timer
  221. //! - \b TIMER_CFG_ONE_SHOT_UP - Full-width one-shot timer that counts up
  222. //! instead of down
  223. //! - \b TIMER_CFG_PERIODIC - Full-width periodic timer
  224. //! - \b TIMER_CFG_PERIODIC_UP - Full-width periodic timer that counts up
  225. //! instead of down
  226. //! - \b TIMER_CFG_RTC - Full-width real time clock timer
  227. //! - \b TIMER_CFG_SPLIT_PAIR - Two half-width timers
  228. //!
  229. //! When configured for a pair of half-width timers, each timer is separately
  230. //! configured. The first timer is configured by setting \e ui32Config to
  231. //! the result of a logical OR operation between one of the following values
  232. //! and \e ui32Config:
  233. //!
  234. //! - \b TIMER_CFG_A_ONE_SHOT - Half-width one-shot timer
  235. //! - \b TIMER_CFG_A_ONE_SHOT_UP - Half-width one-shot timer that counts up
  236. //! instead of down
  237. //! - \b TIMER_CFG_A_PERIODIC - Half-width periodic timer
  238. //! - \b TIMER_CFG_A_PERIODIC_UP - Half-width periodic timer that counts up
  239. //! instead of down
  240. //! - \b TIMER_CFG_A_CAP_COUNT - Half-width edge count capture
  241. //! - \b TIMER_CFG_A_CAP_COUNT_UP - Half-width edge count capture that counts
  242. //! up instead of down
  243. //! - \b TIMER_CFG_A_CAP_TIME - Half-width edge time capture
  244. //! - \b TIMER_CFG_A_CAP_TIME_UP - Half-width edge time capture that counts up
  245. //! instead of down
  246. //! - \b TIMER_CFG_A_PWM - Half-width PWM output
  247. //!
  248. //! One of the following can be combined with the \b TIMER_CFG_* values to
  249. //! enable an action on timer A:
  250. //!
  251. //! - \b TIMER_CFG_A_ACT_TOINTD - masks the timeout interrupt of timer A.
  252. //! - \b TIMER_CFG_A_ACT_NONE - no additional action on timeout of timer A.
  253. //! - \b TIMER_CFG_A_ACT_TOGGLE - toggle CCP on timeout of timer A.
  254. //! - \b TIMER_CFG_A_ACT_SETTO - set CCP on timeout of timer A.
  255. //! - \b TIMER_CFG_A_ACT_CLRTO - clear CCP on timeout of timer A.
  256. //! - \b TIMER_CFG_A_ACT_SETTOGTO - set CCP immediately and then toggle it on
  257. //! timeout of timer A.
  258. //! - \b TIMER_CFG_A_ACT_CLRTOGTO - clear CCP immediately and then toggle it on
  259. //! timeout of timer A.
  260. //! - \b TIMER_CFG_A_ACT_SETCLRTO - set CCP immediately and then clear it on
  261. //! timeout of timer A.
  262. //! - \b TIMER_CFG_A_ACT_CLRSETTO - clear CCP immediately and then set it on
  263. //! timeout of timer A.
  264. //!
  265. //! One of the following can be combined with the \b TIMER_CFG_* values to
  266. //! enable an action on timer B:
  267. //!
  268. //! - \b TIMER_CFG_B_ACT_TOINTD - masks the timeout interrupt of timer B.
  269. //! - \b TIMER_CFG_B_ACT_NONE - no additional action on timeout of timer B.
  270. //! - \b TIMER_CFG_B_ACT_TOGGLE - toggle CCP on timeout of timer B.
  271. //! - \b TIMER_CFG_B_ACT_SETTO - set CCP on timeout of timer B.
  272. //! - \b TIMER_CFG_B_ACT_CLRTO - clear CCP on timeout of timer B.
  273. //! - \b TIMER_CFG_B_ACT_SETTOGTO - set CCP immediately and then toggle it on
  274. //! timeout of timer B.
  275. //! - \b TIMER_CFG_B_ACT_CLRTOGTO - clear CCP immediately and then toggle it on
  276. //! timeout of timer B.
  277. //! - \b TIMER_CFG_B_ACT_SETCLRTO - set CCP immediately and then clear it on
  278. //! timeout of timer B.
  279. //! - \b TIMER_CFG_B_ACT_CLRSETTO - clear CCP immediately and then set it on
  280. //! timeout of timer B.
  281. //!
  282. //! Similarly, the second timer is configured by setting \e ui32Config to
  283. //! the result of a logical OR operation between one of the corresponding
  284. //! \b TIMER_CFG_B_* values and \e ui32Config.
  285. //!
  286. //! \return None.
  287. //
  288. //*****************************************************************************
  289. void
  290. TimerConfigure(uint32_t ui32Base, uint32_t ui32Config)
  291. {
  292. //
  293. // Check the arguments.
  294. //
  295. ASSERT(_TimerBaseValid(ui32Base));
  296. ASSERT((ui32Config == TIMER_CFG_ONE_SHOT) ||
  297. (ui32Config == TIMER_CFG_ONE_SHOT_UP) ||
  298. (ui32Config == TIMER_CFG_PERIODIC) ||
  299. (ui32Config == TIMER_CFG_PERIODIC_UP) ||
  300. (ui32Config == TIMER_CFG_RTC) ||
  301. ((ui32Config & 0xff000000) == TIMER_CFG_SPLIT_PAIR));
  302. ASSERT(((ui32Config & 0xff000000) != TIMER_CFG_SPLIT_PAIR) ||
  303. ((((ui32Config & 0x000000ff) == TIMER_CFG_A_ONE_SHOT) ||
  304. ((ui32Config & 0x000000ff) == TIMER_CFG_A_ONE_SHOT_UP) ||
  305. ((ui32Config & 0x000000ff) == TIMER_CFG_A_PERIODIC) ||
  306. ((ui32Config & 0x000000ff) == TIMER_CFG_A_PERIODIC_UP) ||
  307. ((ui32Config & 0x000000ff) == TIMER_CFG_A_CAP_COUNT) ||
  308. ((ui32Config & 0x000000ff) == TIMER_CFG_A_CAP_COUNT_UP) ||
  309. ((ui32Config & 0x000000ff) == TIMER_CFG_A_CAP_TIME) ||
  310. ((ui32Config & 0x000000ff) == TIMER_CFG_A_CAP_TIME_UP) ||
  311. ((ui32Config & 0x000000ff) == TIMER_CFG_A_PWM)) &&
  312. (((ui32Config & 0x0000ff00) == TIMER_CFG_B_ONE_SHOT) ||
  313. ((ui32Config & 0x0000ff00) == TIMER_CFG_B_ONE_SHOT_UP) ||
  314. ((ui32Config & 0x0000ff00) == TIMER_CFG_B_PERIODIC) ||
  315. ((ui32Config & 0x0000ff00) == TIMER_CFG_B_PERIODIC_UP) ||
  316. ((ui32Config & 0x0000ff00) == TIMER_CFG_B_CAP_COUNT) ||
  317. ((ui32Config & 0x0000ff00) == TIMER_CFG_B_CAP_COUNT_UP) ||
  318. ((ui32Config & 0x0000ff00) == TIMER_CFG_B_CAP_TIME) ||
  319. ((ui32Config & 0x0000ff00) == TIMER_CFG_B_CAP_TIME_UP) ||
  320. ((ui32Config & 0x0000ff00) == TIMER_CFG_B_PWM))));
  321. //
  322. // Disable the timers.
  323. //
  324. HWREG(ui32Base + TIMER_O_CTL) &= ~(TIMER_CTL_TAEN | TIMER_CTL_TBEN);
  325. //
  326. // Set the global timer configuration.
  327. //
  328. HWREG(ui32Base + TIMER_O_CFG) = ui32Config >> 24;
  329. //
  330. // Set the configuration of the A and B timers and set the TxPWMIE bit.
  331. // Note that the B timer configuration is ignored by the hardware in 32-bit
  332. // modes.
  333. //
  334. HWREG(ui32Base + TIMER_O_TAMR) = (((ui32Config & 0x000f0000) >> 4) |
  335. (ui32Config & 0xff) |
  336. TIMER_TAMR_TAPWMIE);
  337. HWREG(ui32Base + TIMER_O_TBMR) = (((ui32Config & 0x00f00000) >> 8) |
  338. ((ui32Config >> 8) & 0xff) |
  339. TIMER_TBMR_TBPWMIE);
  340. }
  341. //*****************************************************************************
  342. //
  343. //! Controls the output level.
  344. //!
  345. //! \param ui32Base is the base address of the timer module.
  346. //! \param ui32Timer specifies the timer(s) to adjust; must be one of
  347. //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
  348. //! \param bInvert specifies the output level.
  349. //!
  350. //! This function configures the PWM output level for the specified timer. If
  351. //! the \e bInvert parameter is \b true, then the timer's output is made active
  352. //! low; otherwise, it is made active high.
  353. //!
  354. //! \return None.
  355. //
  356. //*****************************************************************************
  357. void
  358. TimerControlLevel(uint32_t ui32Base, uint32_t ui32Timer, bool bInvert)
  359. {
  360. //
  361. // Check the arguments.
  362. //
  363. ASSERT(_TimerBaseValid(ui32Base));
  364. ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
  365. (ui32Timer == TIMER_BOTH));
  366. //
  367. // Set the output levels as requested.
  368. //
  369. ui32Timer &= TIMER_CTL_TAPWML | TIMER_CTL_TBPWML;
  370. HWREG(ui32Base + TIMER_O_CTL) = (bInvert ?
  371. (HWREG(ui32Base + TIMER_O_CTL) |
  372. ui32Timer) :
  373. (HWREG(ui32Base + TIMER_O_CTL) &
  374. ~(ui32Timer)));
  375. }
  376. //*****************************************************************************
  377. //
  378. //! Enables or disables the ADC trigger output.
  379. //!
  380. //! \param ui32Base is the base address of the timer module.
  381. //! \param ui32Timer specifies the timer to adjust; must be one of \b TIMER_A,
  382. //! \b TIMER_B, or \b TIMER_BOTH.
  383. //! \param bEnable specifies the desired ADC trigger state.
  384. //!
  385. //! This function controls the ADC trigger output for the specified timer. If
  386. //! the \e bEnable parameter is \b true, then the timer's ADC output trigger is
  387. //! enabled; otherwise it is disabled.
  388. //!
  389. //! \return None.
  390. //
  391. //*****************************************************************************
  392. void
  393. TimerControlTrigger(uint32_t ui32Base, uint32_t ui32Timer,
  394. bool bEnable)
  395. {
  396. //
  397. // Timer time out ADC trigger enable must also be set.
  398. //
  399. uint32_t ui32Val;
  400. //
  401. // Check the arguments.
  402. //
  403. ASSERT(_TimerBaseValid(ui32Base));
  404. ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
  405. (ui32Timer == TIMER_BOTH));
  406. //
  407. // Determine which bits to set or clear in GPTMADCEV.
  408. //
  409. ui32Val = (TIMER_ADCEV_TATOADCEN | TIMER_ADCEV_TBTOADCEN);
  410. ui32Val &= ui32Timer;
  411. //
  412. // Write the GPTM ADC Event register to enable or disable the trigger
  413. // to the ADC.
  414. //
  415. HWREG(ui32Base + TIMER_O_ADCEV) = (bEnable ?
  416. (HWREG(ui32Base + TIMER_O_ADCEV) |
  417. ui32Val) :
  418. (HWREG(ui32Base + TIMER_O_ADCEV) &
  419. ~(ui32Val)));
  420. //
  421. // Set the trigger output as requested.
  422. // Set the ADC trigger output as requested.
  423. //
  424. ui32Timer &= TIMER_CTL_TAOTE | TIMER_CTL_TBOTE;
  425. HWREG(ui32Base + TIMER_O_CTL) = (bEnable ?
  426. (HWREG(ui32Base + TIMER_O_CTL) |
  427. ui32Timer) :
  428. (HWREG(ui32Base + TIMER_O_CTL) &
  429. ~(ui32Timer)));
  430. }
  431. //*****************************************************************************
  432. //
  433. //! Controls the event type.
  434. //!
  435. //! \param ui32Base is the base address of the timer module.
  436. //! \param ui32Timer specifies the timer(s) to be adjusted; must be one of
  437. //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
  438. //! \param ui32Event specifies the type of event; must be one of
  439. //! \b TIMER_EVENT_POS_EDGE, \b TIMER_EVENT_NEG_EDGE, or
  440. //! \b TIMER_EVENT_BOTH_EDGES.
  441. //!
  442. //! This function configures the signal edge(s) that triggers the timer when
  443. //! in capture mode.
  444. //!
  445. //! \return None.
  446. //
  447. //*****************************************************************************
  448. void
  449. TimerControlEvent(uint32_t ui32Base, uint32_t ui32Timer,
  450. uint32_t ui32Event)
  451. {
  452. //
  453. // Check the arguments.
  454. //
  455. ASSERT(_TimerBaseValid(ui32Base));
  456. ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
  457. (ui32Timer == TIMER_BOTH));
  458. //
  459. // Set the event type.
  460. //
  461. ui32Timer &= TIMER_CTL_TAEVENT_M | TIMER_CTL_TBEVENT_M;
  462. HWREG(ui32Base + TIMER_O_CTL) = ((HWREG(ui32Base + TIMER_O_CTL) &
  463. ~ui32Timer) | (ui32Event & ui32Timer));
  464. }
  465. //*****************************************************************************
  466. //
  467. //! Controls the stall handling.
  468. //!
  469. //! \param ui32Base is the base address of the timer module.
  470. //! \param ui32Timer specifies the timer(s) to be adjusted; must be one of
  471. //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
  472. //! \param bStall specifies the response to a stall signal.
  473. //!
  474. //! This function controls the stall response for the specified timer. If the
  475. //! \e bStall parameter is \b true, then the timer stops counting if the
  476. //! processor enters debug mode; otherwise the timer keeps running while in
  477. //! debug mode.
  478. //!
  479. //! \return None.
  480. //
  481. //*****************************************************************************
  482. void
  483. TimerControlStall(uint32_t ui32Base, uint32_t ui32Timer,
  484. bool bStall)
  485. {
  486. //
  487. // Check the arguments.
  488. //
  489. ASSERT(_TimerBaseValid(ui32Base));
  490. ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
  491. (ui32Timer == TIMER_BOTH));
  492. //
  493. // Set the stall mode.
  494. //
  495. ui32Timer &= TIMER_CTL_TASTALL | TIMER_CTL_TBSTALL;
  496. HWREG(ui32Base + TIMER_O_CTL) = (bStall ?
  497. (HWREG(ui32Base + TIMER_O_CTL) |
  498. ui32Timer) :
  499. (HWREG(ui32Base + TIMER_O_CTL) &
  500. ~(ui32Timer)));
  501. }
  502. //*****************************************************************************
  503. //
  504. //! Controls the wait on trigger handling.
  505. //!
  506. //! \param ui32Base is the base address of the timer module.
  507. //! \param ui32Timer specifies the timer(s) to be adjusted; must be one of
  508. //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
  509. //! \param bWait specifies if the timer should wait for a trigger input.
  510. //!
  511. //! This function controls whether or not a timer waits for a trigger input to
  512. //! start counting. When enabled, the previous timer in the trigger chain must
  513. //! count to its timeout in order for this timer to start counting. Refer to
  514. //! the TRM/data sheet for a description of the trigger chain.
  515. //!
  516. //! \note This function should not be used for Timer 0A.
  517. //!
  518. //! \return None.
  519. //
  520. //*****************************************************************************
  521. void
  522. TimerControlWaitOnTrigger(uint32_t ui32Base, uint32_t ui32Timer,
  523. bool bWait)
  524. {
  525. //
  526. // Check the arguments.
  527. //
  528. ASSERT(_TimerBaseValid(ui32Base));
  529. ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
  530. (ui32Timer == TIMER_BOTH));
  531. //
  532. // Set the wait on trigger mode for timer A.
  533. //
  534. if ((ui32Timer & TIMER_A) != 0)
  535. {
  536. if (bWait)
  537. {
  538. HWREG(ui32Base + TIMER_O_TAMR) |= TIMER_TAMR_TAWOT;
  539. }
  540. else
  541. {
  542. HWREG(ui32Base + TIMER_O_TAMR) &= ~(TIMER_TAMR_TAWOT);
  543. }
  544. }
  545. //
  546. // Set the wait on trigger mode for timer B.
  547. //
  548. if ((ui32Timer & TIMER_B) != 0)
  549. {
  550. if (bWait)
  551. {
  552. HWREG(ui32Base + TIMER_O_TBMR) |= TIMER_TBMR_TBWOT;
  553. }
  554. else
  555. {
  556. HWREG(ui32Base + TIMER_O_TBMR) &= ~(TIMER_TBMR_TBWOT);
  557. }
  558. }
  559. }
  560. //*****************************************************************************
  561. //
  562. //! Enables RTC counting.
  563. //!
  564. //! \param ui32Base is the base address of the timer module.
  565. //!
  566. //! This function causes the timer to start counting when in RTC mode. If not
  567. //! configured for RTC mode, this function does nothing.
  568. //!
  569. //! \return None.
  570. //
  571. //*****************************************************************************
  572. void
  573. TimerRTCEnable(uint32_t ui32Base)
  574. {
  575. //
  576. // Check the arguments.
  577. //
  578. ASSERT(_TimerBaseValid(ui32Base));
  579. //
  580. // Enable RTC counting.
  581. //
  582. HWREG(ui32Base + TIMER_O_CTL) |= TIMER_CTL_RTCEN;
  583. }
  584. //*****************************************************************************
  585. //
  586. //! Disables RTC counting.
  587. //!
  588. //! \param ui32Base is the base address of the timer module.
  589. //!
  590. //! This function causes the timer to stop counting when in RTC mode.
  591. //!
  592. //! \return None.
  593. //
  594. //*****************************************************************************
  595. void
  596. TimerRTCDisable(uint32_t ui32Base)
  597. {
  598. //
  599. // Check the arguments.
  600. //
  601. ASSERT(_TimerBaseValid(ui32Base));
  602. //
  603. // Disable RTC counting.
  604. //
  605. HWREG(ui32Base + TIMER_O_CTL) &= ~(TIMER_CTL_RTCEN);
  606. }
  607. //*****************************************************************************
  608. //
  609. //! Sets the clock source for the specified timer module.
  610. //!
  611. //! \param ui32Base is the base address of the timer module.
  612. //! \param ui32Source is the clock source for the timer module.
  613. //!
  614. //! This function sets the clock source for both timer A and timer B for the
  615. //! given timer module. The possible clock sources are the system clock
  616. //! (\b TIMER_CLOCK_SYSTEM) or the precision internal oscillator
  617. //! (\b TIMER_CLOCK_PIOSC).
  618. //!
  619. //! \return None.
  620. //
  621. //*****************************************************************************
  622. void
  623. TimerClockSourceSet(uint32_t ui32Base, uint32_t ui32Source)
  624. {
  625. //
  626. // Check the arguments.
  627. //
  628. ASSERT(_TimerBaseValid(ui32Base));
  629. ASSERT((ui32Source == TIMER_CLOCK_SYSTEM) ||
  630. (ui32Source == TIMER_CLOCK_PIOSC));
  631. //
  632. // Set the timer clock source.
  633. //
  634. HWREG(ui32Base + TIMER_O_CC) = ui32Source;
  635. }
  636. //*****************************************************************************
  637. //
  638. //! Returns the clock source for the specified timer module.
  639. //!
  640. //! \param ui32Base is the base address of the timer module.
  641. //!
  642. //! This function returns the clock source for the specified timer module. The
  643. //! possible clock sources are the system clock (\b TIMER_CLOCK_SYSTEM) or
  644. //! the precision internal oscillator (\b TIMER_CLOCK_PIOSC).
  645. //!
  646. //! \return Returns either \b TIMER_CLOCK_SYSTEM or \b TIMER_CLOCK_PIOSC.
  647. //
  648. //*****************************************************************************
  649. uint32_t
  650. TimerClockSourceGet(uint32_t ui32Base)
  651. {
  652. //
  653. // Check the arguments.
  654. //
  655. ASSERT(_TimerBaseValid(ui32Base));
  656. //
  657. // Return the timer clock source.
  658. //
  659. return (HWREG(ui32Base + TIMER_O_CC));
  660. }
  661. //*****************************************************************************
  662. //
  663. //! Sets the timer prescale value.
  664. //!
  665. //! \param ui32Base is the base address of the timer module.
  666. //! \param ui32Timer specifies the timer(s) to adjust; must be one of
  667. //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
  668. //! \param ui32Value is the timer prescale value which must be between 0 and
  669. //! 255 (inclusive) for 16/32-bit timers.
  670. //!
  671. //! This function configures the value of the input clock prescaler. The
  672. //! prescaler is only operational when in half-width mode and is used to extend
  673. //! the range of the half-width timer modes. The prescaler provides the least
  674. //! significant bits when counting down in periodic and one-shot modes; in all
  675. //! other modes, the prescaler provides the most significant bits.
  676. //!
  677. //! \return None.
  678. //
  679. //*****************************************************************************
  680. void
  681. TimerPrescaleSet(uint32_t ui32Base, uint32_t ui32Timer, uint32_t ui32Value)
  682. {
  683. //
  684. // Check the arguments.
  685. //
  686. ASSERT(_TimerBaseValid(ui32Base));
  687. ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
  688. (ui32Timer == TIMER_BOTH));
  689. ASSERT(ui32Value < 256);
  690. //
  691. // Set the timer A prescaler if requested.
  692. //
  693. if (ui32Timer & TIMER_A)
  694. {
  695. HWREG(ui32Base + TIMER_O_TAPR) = ui32Value;
  696. }
  697. //
  698. // Set the timer B prescaler if requested.
  699. //
  700. if (ui32Timer & TIMER_B)
  701. {
  702. HWREG(ui32Base + TIMER_O_TBPR) = ui32Value;
  703. }
  704. }
  705. //*****************************************************************************
  706. //
  707. //! Gets the timer prescale value.
  708. //!
  709. //! \param ui32Base is the base address of the timer module.
  710. //! \param ui32Timer specifies the timer; must be one of \b TIMER_A or
  711. //! \b TIMER_B.
  712. //!
  713. //! This function gets the value of the input clock prescaler. The prescaler
  714. //! is only operational when in half-width mode and is used to extend the range
  715. //! of the half-width timer modes. The prescaler provides the least
  716. //! significant bits when counting down in periodic and one-shot modes; in all
  717. //! other modes, the prescaler provides the most significant bits.
  718. //!
  719. //! \return The value of the timer prescaler.
  720. //
  721. //*****************************************************************************
  722. uint32_t
  723. TimerPrescaleGet(uint32_t ui32Base, uint32_t ui32Timer)
  724. {
  725. //
  726. // Check the arguments.
  727. //
  728. ASSERT(_TimerBaseValid(ui32Base));
  729. ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
  730. (ui32Timer == TIMER_BOTH));
  731. //
  732. // Return the appropriate prescale value.
  733. //
  734. return ((ui32Timer == TIMER_A) ? HWREG(ui32Base + TIMER_O_TAPR) :
  735. HWREG(ui32Base + TIMER_O_TBPR));
  736. }
  737. //*****************************************************************************
  738. //
  739. //! Sets the timer prescale match value.
  740. //!
  741. //! \param ui32Base is the base address of the timer module.
  742. //! \param ui32Timer specifies the timer(s) to adjust; must be one of
  743. //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH.
  744. //! \param ui32Value is the timer prescale match value which must be between 0
  745. //! and 255 (inclusive) for 16/32-bit timers.
  746. //!
  747. //! This function configures the value of the input clock prescaler match
  748. //! value. When in a half-width mode that uses the counter match and the
  749. //! prescaler, the prescale match effectively extends the range of the match.
  750. //! The prescaler provides the least significant bits when counting down in
  751. //! periodic and one-shot modes; in all other modes, the prescaler provides the
  752. //! most significant bits.
  753. //!
  754. //! \return None.
  755. //
  756. //*****************************************************************************
  757. void
  758. TimerPrescaleMatchSet(uint32_t ui32Base, uint32_t ui32Timer,
  759. uint32_t ui32Value)
  760. {
  761. //
  762. // Check the arguments.
  763. //
  764. ASSERT(_TimerBaseValid(ui32Base));
  765. ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
  766. (ui32Timer == TIMER_BOTH));
  767. ASSERT(ui32Value < 256);
  768. //
  769. // Set the timer A prescale match if requested.
  770. //
  771. if (ui32Timer & TIMER_A)
  772. {
  773. HWREG(ui32Base + TIMER_O_TAPMR) = ui32Value;
  774. }
  775. //
  776. // Set the timer B prescale match if requested.
  777. //
  778. if (ui32Timer & TIMER_B)
  779. {
  780. HWREG(ui32Base + TIMER_O_TBPMR) = ui32Value;
  781. }
  782. }
  783. //*****************************************************************************
  784. //
  785. //! Gets the timer prescale match value.
  786. //!
  787. //! \param ui32Base is the base address of the timer module.
  788. //! \param ui32Timer specifies the timer; must be one of \b TIMER_A or
  789. //! \b TIMER_B.
  790. //!
  791. //! This function gets the value of the input clock prescaler match value.
  792. //! When in a half-width mode that uses the counter match and prescaler, the
  793. //! prescale match effectively extends the range of the match. The prescaler
  794. //! provides the least significant bits when counting down in periodic and
  795. //! one-shot modes; in all other modes, the prescaler provides the most
  796. //! significant bits.
  797. //!
  798. //! \return The value of the timer prescale match.
  799. //
  800. //*****************************************************************************
  801. uint32_t
  802. TimerPrescaleMatchGet(uint32_t ui32Base, uint32_t ui32Timer)
  803. {
  804. //
  805. // Check the arguments.
  806. //
  807. ASSERT(_TimerBaseValid(ui32Base));
  808. ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
  809. (ui32Timer == TIMER_BOTH));
  810. //
  811. // Return the appropriate prescale match value.
  812. //
  813. return ((ui32Timer == TIMER_A) ? HWREG(ui32Base + TIMER_O_TAPMR) :
  814. HWREG(ui32Base + TIMER_O_TBPMR));
  815. }
  816. //*****************************************************************************
  817. //
  818. //! Sets the timer load value.
  819. //!
  820. //! \param ui32Base is the base address of the timer module.
  821. //! \param ui32Timer specifies the timer(s) to adjust; must be one of
  822. //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH. Only \b TIMER_A should be used
  823. //! when the timer is configured for full-width operation.
  824. //! \param ui32Value is the load value.
  825. //!
  826. //! This function configures the timer load value; if the timer is running then
  827. //! the value is immediately loaded into the timer.
  828. //!
  829. //! \note This function can be used for both full- and half-width modes of
  830. //! 16/32-bit timers.
  831. //!
  832. //! \return None.
  833. //
  834. //*****************************************************************************
  835. void
  836. TimerLoadSet(uint32_t ui32Base, uint32_t ui32Timer, uint32_t ui32Value)
  837. {
  838. //
  839. // Check the arguments.
  840. //
  841. ASSERT(_TimerBaseValid(ui32Base));
  842. ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
  843. (ui32Timer == TIMER_BOTH));
  844. //
  845. // Set the timer A load value if requested.
  846. //
  847. if (ui32Timer & TIMER_A)
  848. {
  849. HWREG(ui32Base + TIMER_O_TAILR) = ui32Value;
  850. }
  851. //
  852. // Set the timer B load value if requested.
  853. //
  854. if (ui32Timer & TIMER_B)
  855. {
  856. HWREG(ui32Base + TIMER_O_TBILR) = ui32Value;
  857. }
  858. }
  859. //*****************************************************************************
  860. //
  861. //! Gets the timer load value.
  862. //!
  863. //! \param ui32Base is the base address of the timer module.
  864. //! \param ui32Timer specifies the timer; must be one of \b TIMER_A or
  865. //! \b TIMER_B. Only \b TIMER_A should be used when the timer is configured
  866. //! for full-width operation.
  867. //!
  868. //! This function gets the currently programmed interval load value for the
  869. //! specified timer.
  870. //!
  871. //! \note This function can be used for both full- and half-width modes of
  872. //! 16/32-bit timers.
  873. //!
  874. //! \return Returns the load value for the timer.
  875. //
  876. //*****************************************************************************
  877. uint32_t
  878. TimerLoadGet(uint32_t ui32Base, uint32_t ui32Timer)
  879. {
  880. //
  881. // Check the arguments.
  882. //
  883. ASSERT(_TimerBaseValid(ui32Base));
  884. ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B));
  885. //
  886. // Return the appropriate load value.
  887. //
  888. return ((ui32Timer == TIMER_A) ? HWREG(ui32Base + TIMER_O_TAILR) :
  889. HWREG(ui32Base + TIMER_O_TBILR));
  890. }
  891. //*****************************************************************************
  892. //
  893. //! Gets the current timer value.
  894. //!
  895. //! \param ui32Base is the base address of the timer module.
  896. //! \param ui32Timer specifies the timer; must be one of \b TIMER_A or
  897. //! \b TIMER_B. Only \b TIMER_A should be used when the timer is configured
  898. //! for full-width operation.
  899. //!
  900. //! This function reads the current value of the specified timer.
  901. //!
  902. //! \note This function can be used for both full- and half-width modes of
  903. //! 16/32-bit timers.
  904. //!
  905. //! \return Returns the current value of the timer.
  906. //
  907. //*****************************************************************************
  908. uint32_t
  909. TimerValueGet(uint32_t ui32Base, uint32_t ui32Timer)
  910. {
  911. //
  912. // Check the arguments.
  913. //
  914. ASSERT(_TimerBaseValid(ui32Base));
  915. ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B));
  916. //
  917. // Return the appropriate timer value.
  918. //
  919. return ((ui32Timer == TIMER_A) ? HWREG(ui32Base + TIMER_O_TAR) :
  920. HWREG(ui32Base + TIMER_O_TBR));
  921. }
  922. //*****************************************************************************
  923. //
  924. //! Sets the timer match value.
  925. //!
  926. //! \param ui32Base is the base address of the timer module.
  927. //! \param ui32Timer specifies the timer(s) to adjust; must be one of
  928. //! \b TIMER_A, \b TIMER_B, or \b TIMER_BOTH. Only \b TIMER_A should be used
  929. //! when the timer is configured for full-width operation.
  930. //! \param ui32Value is the match value.
  931. //!
  932. //! This function configures the match value for a timer. This value is used
  933. //! in capture count mode to determine when to interrupt the processor and in
  934. //! PWM mode to determine the duty cycle of the output signal.
  935. //! Match interrupts can also be generated in periodic and one-shot modes.
  936. //!
  937. //! \note This function can be used for both full- and half-width modes of
  938. //! 16/32-bit timers.
  939. //!
  940. //! \return None.
  941. //
  942. //*****************************************************************************
  943. void
  944. TimerMatchSet(uint32_t ui32Base, uint32_t ui32Timer,
  945. uint32_t ui32Value)
  946. {
  947. //
  948. // Check the arguments.
  949. //
  950. ASSERT(_TimerBaseValid(ui32Base));
  951. ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
  952. (ui32Timer == TIMER_BOTH));
  953. //
  954. // Set the timer A match value if requested.
  955. //
  956. if (ui32Timer & TIMER_A)
  957. {
  958. HWREG(ui32Base + TIMER_O_TAMATCHR) = ui32Value;
  959. }
  960. //
  961. // Set the timer B match value if requested.
  962. //
  963. if (ui32Timer & TIMER_B)
  964. {
  965. HWREG(ui32Base + TIMER_O_TBMATCHR) = ui32Value;
  966. }
  967. }
  968. //*****************************************************************************
  969. //
  970. //! Gets the timer match value.
  971. //!
  972. //! \param ui32Base is the base address of the timer module.
  973. //! \param ui32Timer specifies the timer; must be one of \b TIMER_A or
  974. //! \b TIMER_B. Only \b TIMER_A should be used when the timer is configured
  975. //! for full-width operation.
  976. //!
  977. //! This function gets the match value for the specified timer.
  978. //!
  979. //! \note This function can be used for both full- and half-width modes of
  980. //! 16/32-bit timers.
  981. //!
  982. //! \return Returns the match value for the timer.
  983. //
  984. //*****************************************************************************
  985. uint32_t
  986. TimerMatchGet(uint32_t ui32Base, uint32_t ui32Timer)
  987. {
  988. //
  989. // Check the arguments.
  990. //
  991. ASSERT(_TimerBaseValid(ui32Base));
  992. ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B));
  993. //
  994. // Return the appropriate match value.
  995. //
  996. return ((ui32Timer == TIMER_A) ? HWREG(ui32Base + TIMER_O_TAMATCHR) :
  997. HWREG(ui32Base + TIMER_O_TBMATCHR));
  998. }
  999. //*****************************************************************************
  1000. //
  1001. //! Registers an interrupt handler for the timer interrupt.
  1002. //!
  1003. //! \param ui32Base is the base address of the timer module.
  1004. //! \param ui32Timer specifies the timer(s); must be one of \b TIMER_A,
  1005. //! \b TIMER_B, or \b TIMER_BOTH.
  1006. //! \param pfnHandler is a pointer to the function to be called when the timer
  1007. //! interrupt occurs.
  1008. //!
  1009. //! This function registers the handler to be called when a timer interrupt
  1010. //! occurs. In addition, this function enables the global interrupt in the
  1011. //! interrupt controller; specific timer interrupts must be enabled via
  1012. //! TimerIntEnable(). It is the interrupt handler's responsibility to clear
  1013. //! the interrupt source via TimerIntClear().
  1014. //!
  1015. //! \sa IntRegister() for important information about registering interrupt
  1016. //! handlers.
  1017. //!
  1018. //! \return None.
  1019. //
  1020. //*****************************************************************************
  1021. void
  1022. TimerIntRegister(uint32_t ui32Base, uint32_t ui32Timer,
  1023. void (*pfnHandler)(void))
  1024. {
  1025. uint32_t ui32Int;
  1026. //
  1027. // Check the arguments.
  1028. //
  1029. ASSERT(_TimerBaseValid(ui32Base));
  1030. ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
  1031. (ui32Timer == TIMER_BOTH));
  1032. //
  1033. // Get the interrupt number for this timer module.
  1034. //
  1035. ui32Int = _TimerIntNumberGet(ui32Base, ui32Timer);
  1036. ASSERT(ui32Int != 0);
  1037. //
  1038. // Register the interrupt handler.
  1039. //
  1040. IntRegister(ui32Int, pfnHandler);
  1041. //
  1042. // Enable the interrupt.
  1043. //
  1044. IntEnable(ui32Int);
  1045. }
  1046. //*****************************************************************************
  1047. //
  1048. //! Unregisters an interrupt handler for the timer interrupt.
  1049. //!
  1050. //! \param ui32Base is the base address of the timer module.
  1051. //! \param ui32Timer specifies the timer(s); must be one of \b TIMER_A,
  1052. //! \b TIMER_B, or \b TIMER_BOTH.
  1053. //!
  1054. //! This function unregisters the handler to be called when a timer interrupt
  1055. //! occurs. This function also masks off the interrupt in the interrupt
  1056. //! controller so that the interrupt handler is no longer called.
  1057. //!
  1058. //! \sa IntRegister() for important information about registering interrupt
  1059. //! handlers.
  1060. //!
  1061. //! \return None.
  1062. //
  1063. //*****************************************************************************
  1064. void
  1065. TimerIntUnregister(uint32_t ui32Base, uint32_t ui32Timer)
  1066. {
  1067. uint32_t ui32Int;
  1068. //
  1069. // Check the arguments.
  1070. //
  1071. ASSERT(_TimerBaseValid(ui32Base));
  1072. ASSERT((ui32Timer == TIMER_A) || (ui32Timer == TIMER_B) ||
  1073. (ui32Timer == TIMER_BOTH));
  1074. //
  1075. // Get the interrupt number for this timer module.
  1076. //
  1077. ui32Int = _TimerIntNumberGet(ui32Base, ui32Timer);
  1078. //
  1079. // Disable the interrupt.
  1080. //
  1081. IntDisable(ui32Int);
  1082. //
  1083. // Unregister the interrupt handler.
  1084. //
  1085. IntUnregister(ui32Int);
  1086. }
  1087. //*****************************************************************************
  1088. //
  1089. //! Enables individual timer interrupt sources.
  1090. //!
  1091. //! \param ui32Base is the base address of the timer module.
  1092. //! \param ui32IntFlags is the bit mask of the interrupt sources to be enabled.
  1093. //!
  1094. //! This function enables the indicated timer interrupt sources. Only the
  1095. //! sources that are enabled can be reflected to the processor interrupt;
  1096. //! disabled sources have no effect on the processor.
  1097. //!
  1098. //! The \e ui32IntFlags parameter must be the logical OR of any combination of
  1099. //! the following:
  1100. //!
  1101. //! - \b TIMER_TIMB_DMA - Timer B uDMA complete
  1102. //! - \b TIMER_TIMA_DMA - Timer A uDMA complete
  1103. //! - \b TIMER_CAPB_EVENT - Capture B event interrupt
  1104. //! - \b TIMER_CAPB_MATCH - Capture B match interrupt
  1105. //! - \b TIMER_TIMB_TIMEOUT - Timer B timeout interrupt
  1106. //! - \b TIMER_RTC_MATCH - RTC interrupt mask
  1107. //! - \b TIMER_CAPA_EVENT - Capture A event interrupt
  1108. //! - \b TIMER_CAPA_MATCH - Capture A match interrupt
  1109. //! - \b TIMER_TIMA_TIMEOUT - Timer A timeout interrupt
  1110. //!
  1111. //! \return None.
  1112. //
  1113. //*****************************************************************************
  1114. void
  1115. TimerIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
  1116. {
  1117. //
  1118. // Check the arguments.
  1119. //
  1120. ASSERT(_TimerBaseValid(ui32Base));
  1121. //
  1122. // Enable the specified interrupts.
  1123. //
  1124. HWREG(ui32Base + TIMER_O_IMR) |= ui32IntFlags;
  1125. }
  1126. //*****************************************************************************
  1127. //
  1128. //! Disables individual timer interrupt sources.
  1129. //!
  1130. //! \param ui32Base is the base address of the timer module.
  1131. //! \param ui32IntFlags is the bit mask of the interrupt sources to be
  1132. //! disabled.
  1133. //!
  1134. //! This function disables the indicated timer interrupt sources. Only the
  1135. //! sources that are enabled can be reflected to the processor interrupt;
  1136. //! disabled sources have no effect on the processor.
  1137. //!
  1138. //! The \e ui32IntFlags parameter has the same definition as the
  1139. //! \e ui32IntFlags parameter to TimerIntEnable().
  1140. //!
  1141. //! \return None.
  1142. //
  1143. //*****************************************************************************
  1144. void
  1145. TimerIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags)
  1146. {
  1147. //
  1148. // Check the arguments.
  1149. //
  1150. ASSERT(_TimerBaseValid(ui32Base));
  1151. //
  1152. // Disable the specified interrupts.
  1153. //
  1154. HWREG(ui32Base + TIMER_O_IMR) &= ~(ui32IntFlags);
  1155. }
  1156. //*****************************************************************************
  1157. //
  1158. //! Gets the current interrupt status.
  1159. //!
  1160. //! \param ui32Base is the base address of the timer module.
  1161. //! \param bMasked is false if the raw interrupt status is required and true if
  1162. //! the masked interrupt status is required.
  1163. //!
  1164. //! This function returns the interrupt status for the timer module. Either
  1165. //! the raw interrupt status or the status of interrupts that are allowed to
  1166. //! reflect to the processor can be returned.
  1167. //!
  1168. //! \return The current interrupt status, enumerated as a bit field of
  1169. //! values described in TimerIntEnable().
  1170. //
  1171. //*****************************************************************************
  1172. uint32_t
  1173. TimerIntStatus(uint32_t ui32Base, bool bMasked)
  1174. {
  1175. //
  1176. // Check the arguments.
  1177. //
  1178. ASSERT(_TimerBaseValid(ui32Base));
  1179. //
  1180. // Return either the interrupt status or the raw interrupt status as
  1181. // requested.
  1182. //
  1183. return (bMasked ? HWREG(ui32Base + TIMER_O_MIS) :
  1184. HWREG(ui32Base + TIMER_O_RIS));
  1185. }
  1186. //*****************************************************************************
  1187. //
  1188. //! Clears timer interrupt sources.
  1189. //!
  1190. //! \param ui32Base is the base address of the timer module.
  1191. //! \param ui32IntFlags is a bit mask of the interrupt sources to be cleared.
  1192. //!
  1193. //! The specified timer interrupt sources are cleared, so that they no longer
  1194. //! assert. This function must be called in the interrupt handler to keep the
  1195. //! interrupt from being triggered again immediately upon exit.
  1196. //!
  1197. //! The \e ui32IntFlags parameter has the same definition as the
  1198. //! \e ui32IntFlags parameter to TimerIntEnable().
  1199. //!
  1200. //! \note Because there is a write buffer in the Cortex-M processor, it may
  1201. //! take several clock cycles before the interrupt source is actually cleared.
  1202. //! Therefore, it is recommended that the interrupt source be cleared early in
  1203. //! the interrupt handler (as opposed to the very last action) to avoid
  1204. //! returning from the interrupt handler before the interrupt source is
  1205. //! actually cleared. Failure to do so may result in the interrupt handler
  1206. //! being immediately reentered (because the interrupt controller still sees
  1207. //! the interrupt source asserted).
  1208. //!
  1209. //! \return None.
  1210. //
  1211. //*****************************************************************************
  1212. void
  1213. TimerIntClear(uint32_t ui32Base, uint32_t ui32IntFlags)
  1214. {
  1215. //
  1216. // Check the arguments.
  1217. //
  1218. ASSERT(_TimerBaseValid(ui32Base));
  1219. //
  1220. // Clear the requested interrupt sources.
  1221. //
  1222. HWREG(ui32Base + TIMER_O_ICR) = ui32IntFlags;
  1223. }
  1224. //*****************************************************************************
  1225. //
  1226. //! Synchronizes the counters in a set of timers.
  1227. //!
  1228. //! \param ui32Base is the base address of the timer module. This parameter
  1229. //! must be the base address of Timer0 (in other words, \b TIMER0_BASE).
  1230. //! \param ui32Timers is the set of timers to synchronize.
  1231. //!
  1232. //! This function synchronizes the counters in a specified set of timers.
  1233. //! When a timer is running in half-width mode, each half can be included or
  1234. //! excluded in the synchronization event. When a timer is running in
  1235. //! full-width mode, only the A timer can be synchronized (specifying the B
  1236. //! timer has no effect).
  1237. //!
  1238. //! The \e ui32Timers parameter is the logical OR of any of the following
  1239. //! defines:
  1240. //!
  1241. //! - \b TIMER_0A_SYNC
  1242. //! - \b TIMER_0B_SYNC
  1243. //! - \b TIMER_1A_SYNC
  1244. //! - \b TIMER_1B_SYNC
  1245. //! - \b TIMER_2A_SYNC
  1246. //! - \b TIMER_2B_SYNC
  1247. //! - \b TIMER_3A_SYNC
  1248. //! - \b TIMER_3B_SYNC
  1249. //! - \b TIMER_4A_SYNC
  1250. //! - \b TIMER_4B_SYNC
  1251. //! - \b TIMER_5A_SYNC
  1252. //! - \b TIMER_5B_SYNC
  1253. //! - \b TIMER_6A_SYNC
  1254. //! - \b TIMER_6B_SYNC
  1255. //! - \b TIMER_7A_SYNC
  1256. //! - \b TIMER_7B_SYNC
  1257. //!
  1258. //! \return None.
  1259. //
  1260. //*****************************************************************************
  1261. void
  1262. TimerSynchronize(uint32_t ui32Base, uint32_t ui32Timers)
  1263. {
  1264. //
  1265. // Check the arguments.
  1266. //
  1267. ASSERT(ui32Base == TIMER0_BASE);
  1268. //
  1269. // Synchronize the specified timers.
  1270. //
  1271. HWREG(ui32Base + TIMER_O_SYNC) = ui32Timers;
  1272. }
  1273. //*****************************************************************************
  1274. //
  1275. //! Enables the events that can cause an ADC trigger event.
  1276. //!
  1277. //! \param ui32Base is the base address of the timer module.
  1278. //! \param ui32ADCEvent is a bit mask of the events that can cause an ADC
  1279. //! trigger event.
  1280. //!
  1281. //! This function enables the timer events that can cause an ADC trigger event.
  1282. //! The ADC trigger events are specified in the \e ui32ADCEvent parameter by
  1283. //! passing in the logical OR of any of the following values:
  1284. //!
  1285. //! - \b TIMER_ADC_MODEMATCH_B - Enables the mode match ADC trigger for timer
  1286. //! B.
  1287. //! - \b TIMER_ADC_CAPEVENT_B - Enables the capture event ADC trigger for
  1288. //! timer B.
  1289. //! - \b TIMER_ADC_CAPMATCH_B - Enables the capture match ADC trigger for
  1290. //! timer B.
  1291. //! - \b TIMER_ADC_TIMEOUT_B - Enables the timeout ADC trigger for timer B.
  1292. //! - \b TIMER_ADC_MODEMATCH_A - Enables the mode match ADC trigger for timer
  1293. //! A.
  1294. //! - \b TIMER_ADC_RTC_A - Enables the RTC ADC trigger for timer A.
  1295. //! - \b TIMER_ADC_CAPEVENT_A - Enables the capture event ADC trigger for
  1296. //! timer A.
  1297. //! - \b TIMER_ADC_CAPMATCH_A - Enables the capture match ADC trigger for
  1298. //! timer A.
  1299. //! - \b TIMER_ADC_TIMEOUT_A - Enables the timeout ADC trigger for timer A.
  1300. //!
  1301. //! \return None.
  1302. //
  1303. //*****************************************************************************
  1304. void
  1305. TimerADCEventSet(uint32_t ui32Base, uint32_t ui32ADCEvent)
  1306. {
  1307. //
  1308. // Check the arguments.
  1309. //
  1310. ASSERT(_TimerBaseValid(ui32Base));
  1311. //
  1312. // Set the ADC triggers.
  1313. //
  1314. HWREG(ui32Base + TIMER_O_ADCEV) = ui32ADCEvent;
  1315. }
  1316. //*****************************************************************************
  1317. //
  1318. //! Returns the events that can cause an ADC trigger event.
  1319. //!
  1320. //! \param ui32Base is the base address of the timer module.
  1321. //!
  1322. //! This function returns the timer events that can cause an ADC trigger event.
  1323. //! The ADC trigger events are the logical OR of any of the following values:
  1324. //!
  1325. //! - \b TIMER_ADC_MODEMATCH_B - The mode match ADC trigger for timer B is
  1326. //! enabled.
  1327. //! - \b TIMER_ADC_CAPEVENT_B - The capture event ADC trigger for timer B is
  1328. //! enabled.
  1329. //! - \b TIMER_ADC_CAPMATCH_B - The capture match ADC trigger for timer B is
  1330. //! enabled.
  1331. //! - \b TIMER_ADC_TIMEOUT_B - The timeout ADC trigger for timer B is enabled.
  1332. //! - \b TIMER_ADC_MODEMATCH_A - The mode match ADC trigger for timer A is
  1333. //! enabled.
  1334. //! - \b TIMER_ADC_RTC_A - The RTC ADC trigger for timer A is enabled.
  1335. //! - \b TIMER_ADC_CAPEVENT_A - The capture event ADC trigger for timer A is
  1336. //! enabled.
  1337. //! - \b TIMER_ADC_CAPMATCH_A - The capture match ADC trigger for timer A is
  1338. //! enabled.
  1339. //! - \b TIMER_ADC_TIMEOUT_A - The timeout ADC trigger for timer A is enabled.
  1340. //!
  1341. //! \return The timer events that trigger the ADC.
  1342. //
  1343. //*****************************************************************************
  1344. uint32_t
  1345. TimerADCEventGet(uint32_t ui32Base)
  1346. {
  1347. //
  1348. // Check the arguments.
  1349. //
  1350. ASSERT(_TimerBaseValid(ui32Base));
  1351. //
  1352. // Return the current ADC triggers.
  1353. //
  1354. return (HWREG(ui32Base + TIMER_O_ADCEV));
  1355. }
  1356. //*****************************************************************************
  1357. //
  1358. //! Enables the events that can trigger a uDMA request.
  1359. //!
  1360. //! \param ui32Base is the base address of the timer module.
  1361. //! \param ui32DMAEvent is a bit mask of the events that can trigger uDMA.
  1362. //!
  1363. //! This function enables the timer events that can trigger the start of a uDMA
  1364. //! sequence. The uDMA trigger events are specified in the \e ui32DMAEvent
  1365. //! parameter by passing in the logical OR of the following values:
  1366. //!
  1367. //! - \b TIMER_DMA_MODEMATCH_B - The mode match uDMA trigger for timer B is
  1368. //! enabled.
  1369. //! - \b TIMER_DMA_CAPEVENT_B - The capture event uDMA trigger for timer B is
  1370. //! enabled.
  1371. //! - \b TIMER_DMA_CAPMATCH_B - The capture match uDMA trigger for timer B is
  1372. //! enabled.
  1373. //! - \b TIMER_DMA_TIMEOUT_B - The timeout uDMA trigger for timer B is enabled.
  1374. //! - \b TIMER_DMA_MODEMATCH_A - The mode match uDMA trigger for timer A is
  1375. //! enabled.
  1376. //! - \b TIMER_DMA_RTC_A - The RTC uDMA trigger for timer A is enabled.
  1377. //! - \b TIMER_DMA_CAPEVENT_A - The capture event uDMA trigger for timer A is
  1378. //! enabled.
  1379. //! - \b TIMER_DMA_CAPMATCH_A - The capture match uDMA trigger for timer A is
  1380. //! enabled.
  1381. //! - \b TIMER_DMA_TIMEOUT_A - The timeout uDMA trigger for timer A is enabled.
  1382. //!
  1383. //! \return None.
  1384. //
  1385. //*****************************************************************************
  1386. void
  1387. TimerDMAEventSet(uint32_t ui32Base, uint32_t ui32DMAEvent)
  1388. {
  1389. //
  1390. // Check the arguments.
  1391. //
  1392. ASSERT(_TimerBaseValid(ui32Base));
  1393. //
  1394. // Set the uDMA triggers.
  1395. //
  1396. HWREG(ui32Base + TIMER_O_DMAEV) = ui32DMAEvent;
  1397. }
  1398. //*****************************************************************************
  1399. //
  1400. //! Returns the events that can trigger a uDMA request.
  1401. //!
  1402. //! \param ui32Base is the base address of the timer module.
  1403. //!
  1404. //! This function returns the timer events that can trigger the start of a uDMA
  1405. //! sequence. The uDMA trigger events are the logical OR of the following
  1406. //! values:
  1407. //!
  1408. //! - \b TIMER_DMA_MODEMATCH_B - Enables the mode match uDMA trigger for timer
  1409. //! B.
  1410. //! - \b TIMER_DMA_CAPEVENT_B - Enables the capture event uDMA trigger for
  1411. //! timer B.
  1412. //! - \b TIMER_DMA_CAPMATCH_B - Enables the capture match uDMA trigger for
  1413. //! timer B.
  1414. //! - \b TIMER_DMA_TIMEOUT_B - Enables the timeout uDMA trigger for timer B.
  1415. //! - \b TIMER_DMA_MODEMATCH_A - Enables the mode match uDMA trigger for timer
  1416. //! A.
  1417. //! - \b TIMER_DMA_RTC_A - Enables the RTC uDMA trigger for timer A.
  1418. //! - \b TIMER_DMA_CAPEVENT_A - Enables the capture event uDMA trigger for
  1419. //! timer A.
  1420. //! - \b TIMER_DMA_CAPMATCH_A - Enables the capture match uDMA trigger for
  1421. //! timer A.
  1422. //! - \b TIMER_DMA_TIMEOUT_A - Enables the timeout uDMA trigger for timer A.
  1423. //!
  1424. //! \return The timer events that trigger the uDMA.
  1425. //
  1426. //*****************************************************************************
  1427. uint32_t
  1428. TimerDMAEventGet(uint32_t ui32Base)
  1429. {
  1430. //
  1431. // Check the arguments.
  1432. //
  1433. ASSERT(_TimerBaseValid(ui32Base));
  1434. //
  1435. // Return the current uDMA triggers.
  1436. //
  1437. return (HWREG(ui32Base + TIMER_O_DMAEV));
  1438. }
  1439. //*****************************************************************************
  1440. //
  1441. //! This function configures the update of timer load and match settings.
  1442. //!
  1443. //! \param ui32Base is the base address of the timer module.
  1444. //! \param ui32Timer specifies the timer(s); must be one of \b TIMER_A,
  1445. //! \b TIMER_B, or \b TIMER_BOTH.
  1446. //! \param ui32Config is a combination of the updates methods for the timers
  1447. //! specified in the \e ui32Timer parameter.
  1448. //!
  1449. //! This function configures how the timer updates the timer load and match
  1450. //! values for the timers. The \e ui32Timer values can be \b TIMER_A,
  1451. //! \b TIMER_B, or \b TIMER_BOTH to apply the settings in \e ui32Config to
  1452. //! either timer or both timers. If the timer is not split then the \b TIMER_A
  1453. //! should be used. The \e ui32Config values affects when the TimerLoadSet()
  1454. //! values take effect.
  1455. //!
  1456. //! - \b TIMER_UP_LOAD_IMMEDIATE is the default mode that causes the
  1457. //! TimerLoadSet() to update the timer counter immediately.
  1458. //! - \b TIMER_UP_LOAD_TIMEOUT causes the TimerLoadSet() to
  1459. //! update the timer when it counts down to zero.
  1460. //!
  1461. //! Similarly the \e ui32Config value affects when the TimerMatchSet()
  1462. //! values take effect.
  1463. //!
  1464. //! - \b TIMER_UP_MATCH_IMMEDIATE is the default mode that causes the
  1465. //! TimerMatchSet() to update the timer match value
  1466. //! immediately.
  1467. //! - \b TIMER_UP_MATCH_TIMEOUT causes the TimerMatchSet()
  1468. //! to update the timer match value when it counts down to zero.
  1469. //!
  1470. //! \note These settings have no effect if the timer is not in count down mode
  1471. //! and are mostly useful when operating in PWM mode to allow for synchronous
  1472. //! update of timer match and load values.
  1473. //!
  1474. //! \return None.
  1475. //
  1476. //*****************************************************************************
  1477. void
  1478. TimerUpdateMode(uint32_t ui32Base, uint32_t ui32Timer, uint32_t ui32Config)
  1479. {
  1480. uint32_t ui32Value;
  1481. if ((ui32Timer & TIMER_A) == TIMER_A)
  1482. {
  1483. ui32Value = HWREG(ui32Base + TIMER_O_TAMR) & ~(0x00000500);
  1484. ui32Value |= ui32Config;
  1485. HWREG(ui32Base + TIMER_O_TAMR) = ui32Value;
  1486. }
  1487. if ((ui32Timer & TIMER_B) == TIMER_B)
  1488. {
  1489. ui32Value = HWREG(ui32Base + TIMER_O_TBMR) & ~(0x00000500);
  1490. ui32Value |= ui32Config;
  1491. HWREG(ui32Base + TIMER_O_TBMR) = ui32Value;
  1492. }
  1493. }
  1494. //*****************************************************************************
  1495. //
  1496. // Close the Doxygen group.
  1497. //! @}
  1498. //
  1499. //*****************************************************************************