hal_timer.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /**************************************************************************************************
  2. Filename: hal_timer.c
  3. Revised: $Date: 2009-03-12 16:25:22 -0700 (Thu, 12 Mar 2009) $
  4. Revision: $Revision: 19404 $
  5. Description: This file contains the interface to the Timer Service.
  6. Copyright 2006-2007 Texas Instruments Incorporated. All rights reserved.
  7. IMPORTANT: Your use of this Software is limited to those specific rights
  8. granted under the terms of a software license agreement between the user
  9. who downloaded the software, his/her employer (which must be your employer)
  10. and Texas Instruments Incorporated (the "License"). You may not use this
  11. Software unless you agree to abide by the terms of the License. The License
  12. limits your use, and you acknowledge, that the Software may not be modified,
  13. copied or distributed unless embedded on a Texas Instruments microcontroller
  14. or used solely and exclusively in conjunction with a Texas Instruments radio
  15. frequency transceiver, which is integrated into your product. Other than for
  16. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  17. works of, modify, distribute, perform, display or sell this Software and/or
  18. its documentation for any purpose.
  19. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  20. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  21. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  22. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  23. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  24. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  25. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  26. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  27. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  28. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  29. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  30. Should you have any questions regarding your right to use this Software,
  31. contact Texas Instruments Incorporated at www.TI.com.
  32. **************************************************************************************************/
  33. /*********************************************************************
  34. NOTE: The following mapping is done between the logical timer
  35. names defined in HAL_TIMER.H and the physical HW timer.
  36. HAL_TIMER_0 --> HW Timer 3 (8-bits)
  37. HAL_TIMER_2 --> HW Timer 4 (8-bits)
  38. HAL_TIMER_3 --> HW Timer 1 (16-bits)
  39. NOTE: The timer code assumes only one channel, CHANNEL 0, is used
  40. for each timer. There is currently no support for other
  41. channels.
  42. NOTE: Only Output Compare Mode is supported. There is no provision
  43. to support Input Capture Mode.
  44. NOTE: There is no support to map the output of the timers to a
  45. physical I/O pin
  46. *********************************************************************/
  47. /*********************************************************************
  48. * INCLUDES
  49. */
  50. #include "hal_mcu.h"
  51. #include "hal_defs.h"
  52. #include "hal_types.h"
  53. #include "hal_timer.h"
  54. /*********************************************************************
  55. * MACROS
  56. */
  57. /*********************************************************************
  58. * CONSTANTS
  59. */
  60. #define HW_TIMER_1 0x00
  61. #define HW_TIMER_3 0x01
  62. #define HW_TIMER_4 0x02
  63. #define HW_TIMER_INVALID 0x03
  64. #define HW_TIMER_MAX 0x03
  65. #define IEN1_T1IE 0x02 /* Timer1 Interrupt Enable */
  66. #define IEN1_T3IE 0x08 /* Timer3 Interrupt Enable */
  67. #define IEN1_T4IE 0x10 /* Timer4 Interrupt Enable */
  68. #define T1CTL_CH2IF 0x80
  69. #define T1CTL_CH1IF 0x40
  70. #define T1CTL_CH0IF 0x20
  71. #define T1CTL_OVFIF 0x10
  72. #define TIMIF_T1OVFIM 0x40
  73. #define TIMIF_T4CH1IF 0x20
  74. #define TIMIF_T4CH0IF 0x10
  75. #define TIMIF_T4OVFIF 0x08
  76. #define TIMIF_T3CH1IF 0x04
  77. #define TIMIF_T3CH0IF 0x02
  78. #define TIMIF_T3OVFIF 0x01
  79. #define T34CTL_OVFIM 0x80
  80. #define T134CCTL_IM 0x40 /* Interrupt Mask */
  81. #define T134CCTL_CMP_BITS 0x38 /* Bits[5:3] == CMP[2:0] */
  82. #define T134CCTL_MODE 0x04 /* Capture(0)/Compare(1) mode */
  83. #define T134CCTL_CAP_BITS 0x03 /* Bits[1:0] == CAP[1:0] */
  84. #define T134CCTL_CMP_OC 0x18 /* Set output on compare, clear at 0 */
  85. #define T134CCTL_CAP_RE 0x01 /* Set input capture on rising edge */
  86. /* Timer clock pre-scaler definitions for 16bit timer1 */
  87. #define HAL_TIMER1_16_TC_DIV1 0x00 /* No clock pre-scaling */
  88. #define HAL_TIMER1_16_TC_DIV8 0x04 /* Clock pre-scaled by 8 */
  89. #define HAL_TIMER1_16_TC_DIV32 0x08 /* Clock pre-scaled by 32 */
  90. #define HAL_TIMER1_16_TC_DIV128 0x0c /* Clock pre-scaled by 128 */
  91. #define HAL_TIMER1_16_TC_BITS 0x0c /* Bits 3:2 */
  92. /* Timer clock pre-scaler definitions for 8bit timer3 and timer4 */
  93. #define HAL_TIMER34_8_TC_DIV1 0x00 /* No clock pre-scaling */
  94. #define HAL_TIMER34_8_TC_DIV2 0x20 /* Clock pre-scaled by 2 */
  95. #define HAL_TIMER34_8_TC_DIV4 0x40 /* Clock pre-scaled by 4 */
  96. #define HAL_TIMER34_8_TC_DIV8 0x60 /* Clock pre-scaled by 8 */
  97. #define HAL_TIMER34_8_TC_DIV16 0x80 /* Clock pre-scaled by 16 */
  98. #define HAL_TIMER34_8_TC_DIV32 0xA0 /* Clock pre-scaled by 32 */
  99. #define HAL_TIMER34_8_TC_DIV64 0xC0 /* Clock pre-scaled by 64 */
  100. #define HAL_TIMER34_8_TC_DIV128 0xE0 /* Clock pre-scaled by 128 */
  101. #define HAL_TIMER34_8_TC_BITS 0xE0 /* Bits 7:5 */
  102. /* Operation Mode definitions */
  103. #define HAL_TIMER1_OPMODE_STOP 0x00 /* Free Running Mode, Count from 0 to Max */
  104. #define HAL_TIMER1_OPMODE_FREERUN 0x01 /* Free Running Mode, Count from 0 to Max */
  105. #define HAL_TIMER1_OPMODE_MODULO 0x02 /* Modulo Mode, Count from 0 to CompareValue */
  106. #define HAL_TIMER1_OPMODE_BITS 0x03 /* Bits 1:0 */
  107. #define HAL_TIMER34_START 0x10 /* Timer3 and Timer4 have separate Start bit */
  108. #define HAL_TIMER34_OPMODE_FREERUN 0x00 /* Free Running Mode, Count from 0 to Max */
  109. #define HAL_TIMER34_OPMODE_MODULO 0x02 /* Modulo Mode, Count from 0 to CompareValue */
  110. #define HAL_TIMER34_OPMODE_BITS 0x03 /* Bits 1:0 */
  111. #define HAL_TIMER_MODE_STOP 0x03
  112. /* Prescale settings */
  113. #define HAL_TIMER1_16_PRESCALE HAL_TIMER1_16_TC_DIV128
  114. #define HAL_TIMER1_16_PRESCALE_VAL 128
  115. #define HAL_TIMER3_8_PRESCALE HAL_TIMER34_8_TC_DIV128
  116. #define HAL_TIMER3_8_PRESCALE_VAL 128
  117. #define HAL_TIMER4_8_PRESCALE HAL_TIMER34_8_TC_DIV128
  118. #define HAL_TIMER4_8_PRESCALE_VAL 128
  119. /* Clock settings */
  120. #define HAL_TIMER_16MHZ 16
  121. #define HAL_TIMER_32MHZ 32
  122. /* Default all timers to use channel 0 */
  123. #define TCHN_T1CCTL &(X_T1CCTL0)
  124. #define TCHN_T1CCL &(X_T1CC0L)
  125. #define TCHN_T1CCH &(X_T1CC0H)
  126. #define TCNH_T1OVF &(X_TIMIF)
  127. #define TCHN_T1OVFBIT TIMIF_T1OVFIM
  128. #define TCHN_T1INTBIT IEN1_T1IE
  129. #define TCHN_T3CCTL &(X_T3CCTL0)
  130. #define TCHN_T3CCL &(X_T3CC0)
  131. #define TCHN_T3CCH &(X_T3CC0)
  132. #define TCNH_T3OVF &(X_T3CTL)
  133. #define TCHN_T3OVFBIT T34CTL_OVFIM
  134. #define TCHN_T3INTBIT IEN1_T3IE
  135. #define TCHN_T4CCTL &(X_T4CCTL0)
  136. #define TCHN_T4CCL &(X_T4CC0)
  137. #define TCHN_T4CCH &(X_T4CC0)
  138. #define TCNH_T4OVF &(X_T4CTL)
  139. #define TCHN_T4OVFBIT T34CTL_OVFIM
  140. #define TCHN_T4INTBIT IEN1_T4IE
  141. /*********************************************************************
  142. * TYPEDEFS
  143. */
  144. typedef struct
  145. {
  146. bool configured;
  147. bool intEnable;
  148. uint8 opMode;
  149. uint8 channel;
  150. uint8 channelMode;
  151. uint8 prescale;
  152. uint8 prescaleVal;
  153. uint8 clock;
  154. halTimerCBack_t callBackFunc;
  155. } halTimerSettings_t;
  156. typedef struct
  157. {
  158. uint8 volatile XDATA *TxCCTL;
  159. uint8 volatile XDATA *TxCCH;
  160. uint8 volatile XDATA *TxCCL;
  161. uint8 volatile XDATA *TxOVF;
  162. uint8 ovfbit;
  163. uint8 intbit;
  164. } halTimerChannel_t;
  165. /*********************************************************************
  166. * GLOBAL VARIABLES
  167. */
  168. static halTimerSettings_t halTimerRecord[HW_TIMER_MAX];
  169. static halTimerChannel_t halTimerChannel[HW_TIMER_MAX];
  170. /*********************************************************************
  171. * FUNCTIONS - External
  172. */
  173. /*********************************************************************
  174. * FUNCTIONS - Local
  175. */
  176. uint8 halTimerSetCount (uint8 cc2430id, uint32 timePerTick);
  177. uint8 halTimerSetPrescale (uint8 cc2430id, uint8 prescale);
  178. uint8 halTimerSetOpMode (uint8 cc2430id, uint8 opMode);
  179. uint8 halTimerSetChannelMode (uint8 cc2430id, uint8 channelMode);
  180. void halTimerSendCallBack (uint8 timerId, uint8 channel, uint8 channelMode);
  181. uint8 halTimerRemap (uint8 timerId);
  182. void halProcessTimer1 (void);
  183. void halProcessTimer3 (void);
  184. void halProcessTimer4 (void);
  185. /*********************************************************************
  186. * FUNCTIONS - API
  187. */
  188. /*********************************************************************
  189. * @fn HalTimerInit
  190. *
  191. * @brief Initialize Timer Service
  192. *
  193. * @param None
  194. *
  195. * @return None
  196. */
  197. void HalTimerInit (void)
  198. {
  199. T1CCTL0 = 0; /* Make sure interrupts are disabled */
  200. T1CCTL1 = 0; /* Make sure interrupts are disabled */
  201. T1CCTL2 = 0; /* Make sure interrupts are disabled */
  202. T3CCTL0 = 0; /* Make sure interrupts are disabled */
  203. T3CCTL1 = 0; /* Make sure interrupts are disabled */
  204. T4CCTL0 = 0; /* Make sure interrupts are disabled */
  205. T4CCTL1 = 0; /* Make sure interrupts are disabled */
  206. /* Setup prescale & clock for timer0 */
  207. halTimerRecord[HW_TIMER_1].prescale = HAL_TIMER1_16_PRESCALE;
  208. halTimerRecord[HW_TIMER_1].clock = HAL_TIMER_32MHZ;
  209. halTimerRecord[HW_TIMER_1].prescaleVal = HAL_TIMER1_16_PRESCALE_VAL;
  210. /* Setup prescale & clock for timer2 */
  211. halTimerRecord[HW_TIMER_3].prescale = HAL_TIMER3_8_PRESCALE;
  212. halTimerRecord[HW_TIMER_3].clock = HAL_TIMER_32MHZ;
  213. halTimerRecord[HW_TIMER_3].prescaleVal = HAL_TIMER3_8_PRESCALE_VAL;
  214. /* Setup prescale & clock for timer3 */
  215. halTimerRecord[HW_TIMER_4].prescale = HAL_TIMER4_8_PRESCALE;
  216. halTimerRecord[HW_TIMER_4].clock = HAL_TIMER_32MHZ;
  217. halTimerRecord[HW_TIMER_4].prescaleVal = HAL_TIMER4_8_PRESCALE_VAL;
  218. /* Setup Timer1 Channel structure */
  219. halTimerChannel[HW_TIMER_1].TxCCTL = TCHN_T1CCTL;
  220. halTimerChannel[HW_TIMER_1].TxCCL = TCHN_T1CCL;
  221. halTimerChannel[HW_TIMER_1].TxCCH = TCHN_T1CCH;
  222. halTimerChannel[HW_TIMER_1].TxOVF = TCNH_T1OVF;
  223. halTimerChannel[HW_TIMER_1].ovfbit = TCHN_T1OVFBIT;
  224. halTimerChannel[HW_TIMER_1].intbit = TCHN_T1INTBIT;
  225. /* Setup Timer3 Channel structure */
  226. halTimerChannel[HW_TIMER_3].TxCCTL = TCHN_T3CCTL;
  227. halTimerChannel[HW_TIMER_3].TxCCL = TCHN_T3CCL;
  228. halTimerChannel[HW_TIMER_3].TxCCH = TCHN_T3CCH;
  229. halTimerChannel[HW_TIMER_3].TxOVF = TCNH_T3OVF;
  230. halTimerChannel[HW_TIMER_3].ovfbit = TCHN_T3OVFBIT;
  231. halTimerChannel[HW_TIMER_3].intbit = TCHN_T3INTBIT;
  232. /* Setup Timer4 Channel structure */
  233. halTimerChannel[HW_TIMER_4].TxCCTL = TCHN_T4CCTL;
  234. halTimerChannel[HW_TIMER_4].TxCCL = TCHN_T4CCL;
  235. halTimerChannel[HW_TIMER_4].TxCCH = TCHN_T4CCH;
  236. halTimerChannel[HW_TIMER_4].TxOVF = TCNH_T4OVF;
  237. halTimerChannel[HW_TIMER_4].ovfbit = TCHN_T4OVFBIT;
  238. halTimerChannel[HW_TIMER_4].intbit = TCHN_T4INTBIT;
  239. }
  240. /***************************************************************************************************
  241. * @fn HalTimerConfig
  242. *
  243. * @brief Configure the Timer Serivce
  244. *
  245. * @param timerId - Id of the timer
  246. * opMode - Operation mode
  247. * channel - Channel where the counter operates on
  248. * channelMode - Mode of that channel
  249. * prescale - Prescale of the clock
  250. * cBack - Pointer to the callback function
  251. *
  252. * @return Status of the configuration
  253. ***************************************************************************************************/
  254. uint8 HalTimerConfig (uint8 timerId, uint8 opMode, uint8 channel, uint8 channelMode,
  255. bool intEnable, halTimerCBack_t cBack)
  256. {
  257. uint8 hwtimerid;
  258. hwtimerid = halTimerRemap (timerId);
  259. if ((opMode & HAL_TIMER_MODE_MASK) && (timerId < HAL_TIMER_MAX) &&
  260. (channelMode & HAL_TIMER_CHANNEL_MASK) && (channel & HAL_TIMER_CHANNEL_MASK))
  261. {
  262. halTimerRecord[hwtimerid].configured = TRUE;
  263. halTimerRecord[hwtimerid].opMode = opMode;
  264. halTimerRecord[hwtimerid].channel = channel;
  265. halTimerRecord[hwtimerid].channelMode = channelMode;
  266. halTimerRecord[hwtimerid].intEnable = intEnable;
  267. halTimerRecord[hwtimerid].callBackFunc = cBack;
  268. }
  269. else
  270. {
  271. return HAL_TIMER_PARAMS_ERROR;
  272. }
  273. return HAL_TIMER_OK;
  274. }
  275. /***************************************************************************************************
  276. * @fn HalTimerStart
  277. *
  278. * @brief Start the Timer Service
  279. *
  280. * @param timerId - ID of the timer
  281. * timerPerTick - number of micro sec per tick, (ticks x prescale) / clock = usec/tick
  282. *
  283. * @return Status - OK or Not OK
  284. ***************************************************************************************************/
  285. uint8 HalTimerStart (uint8 timerId, uint32 timePerTick)
  286. {
  287. uint8 hwtimerid;
  288. hwtimerid = halTimerRemap (timerId);
  289. if (halTimerRecord[hwtimerid].configured)
  290. {
  291. halTimerSetCount (hwtimerid, timePerTick);
  292. halTimerSetPrescale (hwtimerid, halTimerRecord[hwtimerid].prescale);
  293. halTimerSetOpMode (hwtimerid, halTimerRecord[hwtimerid].opMode);
  294. halTimerSetChannelMode (hwtimerid, halTimerRecord[hwtimerid].channelMode);
  295. if (hwtimerid == HW_TIMER_3)
  296. {
  297. T3CTL |= HAL_TIMER34_START;
  298. }
  299. if (hwtimerid == HW_TIMER_4)
  300. {
  301. T4CTL |= HAL_TIMER34_START;
  302. }
  303. HalTimerInterruptEnable (hwtimerid, halTimerRecord[hwtimerid].channelMode,
  304. halTimerRecord[hwtimerid].intEnable);
  305. }
  306. else
  307. {
  308. return HAL_TIMER_NOT_CONFIGURED;
  309. }
  310. return HAL_TIMER_OK;
  311. }
  312. /***************************************************************************************************
  313. * @fn HalTimerTick
  314. *
  315. * @brief Check the counter for expired counter.
  316. *
  317. * @param None
  318. *
  319. * @return None
  320. ***************************************************************************************************/
  321. void HalTimerTick (void)
  322. {
  323. if (!halTimerRecord[HW_TIMER_1].intEnable)
  324. {
  325. halProcessTimer1 ();
  326. }
  327. if (!halTimerRecord[HW_TIMER_3].intEnable)
  328. {
  329. halProcessTimer3 ();
  330. }
  331. if (!halTimerRecord[HW_TIMER_4].intEnable)
  332. {
  333. halProcessTimer4 ();
  334. }
  335. }
  336. /***************************************************************************************************
  337. * @fn HalTimerStop
  338. *
  339. * @brief Stop the Timer Service
  340. *
  341. * @param timerId - ID of the timer
  342. *
  343. * @return Status - OK or Not OK
  344. ***************************************************************************************************/
  345. uint8 HalTimerStop (uint8 timerId)
  346. {
  347. uint8 hwtimerid;
  348. hwtimerid = halTimerRemap (timerId);
  349. switch (hwtimerid)
  350. {
  351. case HW_TIMER_1:
  352. halTimerSetOpMode(HW_TIMER_1, HAL_TIMER_MODE_STOP);
  353. break;
  354. case HW_TIMER_3:
  355. T3CTL &= ~(HAL_TIMER34_START);
  356. break;
  357. case HW_TIMER_4:
  358. T4CTL &= ~(HAL_TIMER34_START);
  359. break;
  360. default:
  361. return HAL_TIMER_INVALID_ID;
  362. }
  363. return HAL_TIMER_OK;
  364. }
  365. /***************************************************************************************************
  366. * @fn halTimerSetCount
  367. *
  368. * @brief Stop the Timer Service
  369. *
  370. * @param hwtimerid - ID of the timer
  371. * timerPerTick - Number micro sec per ticks
  372. *
  373. * @return Status - OK or Not OK
  374. ***************************************************************************************************/
  375. uint8 halTimerSetCount (uint8 hwtimerid, uint32 timePerTick)
  376. {
  377. uint16 count;
  378. uint8 high, low;
  379. /* Load count = ((sec/tick) x clock) / prescale */
  380. count = (uint16)((timePerTick * halTimerRecord[hwtimerid].clock) / halTimerRecord[hwtimerid].prescaleVal);
  381. high = (uint8) (count >> 8);
  382. low = (uint8) count;
  383. *(halTimerChannel[hwtimerid].TxCCH) = high;
  384. *(halTimerChannel[hwtimerid].TxCCL) = low;
  385. return HAL_TIMER_OK;
  386. }
  387. /***************************************************************************************************
  388. * @fn halTimerSetPrescale
  389. *
  390. * @brief Stop the Timer Service
  391. *
  392. * @param hwtimerid - ID of the timer
  393. * prescale - Prescale of the clock
  394. *
  395. * @return Status - OK or Not OK
  396. ***************************************************************************************************/
  397. uint8 halTimerSetPrescale (uint8 hwtimerid, uint8 prescale)
  398. {
  399. switch (hwtimerid)
  400. {
  401. case HW_TIMER_1:
  402. T1CTL &= ~(HAL_TIMER1_16_TC_BITS);
  403. T1CTL |= prescale;
  404. break;
  405. case HW_TIMER_3:
  406. T3CTL &= ~(HAL_TIMER34_8_TC_BITS);
  407. T3CTL |= prescale;
  408. break;
  409. case HW_TIMER_4:
  410. T4CTL &= ~(HAL_TIMER34_8_TC_BITS);
  411. T4CTL |= prescale;
  412. break;
  413. default:
  414. return HAL_TIMER_INVALID_ID;
  415. }
  416. return HAL_TIMER_OK;
  417. }
  418. /***************************************************************************************************
  419. * @fn halTimerSetOpMode
  420. *
  421. * @brief Setup operate modes
  422. *
  423. * @param hwtimerid - ID of the timer
  424. * opMode - operation mode of the timer
  425. *
  426. * @return Status - OK or Not OK
  427. ***************************************************************************************************/
  428. uint8 halTimerSetOpMode (uint8 hwtimerid, uint8 opMode)
  429. {
  430. /* Load Waveform Generation Mode */
  431. switch (opMode)
  432. {
  433. case HAL_TIMER_MODE_NORMAL:
  434. switch (hwtimerid)
  435. {
  436. case HW_TIMER_1:
  437. T1CTL &= ~(HAL_TIMER1_OPMODE_BITS);
  438. T1CTL |= HAL_TIMER1_OPMODE_FREERUN;
  439. break;
  440. case HW_TIMER_3:
  441. T3CTL &= ~(HAL_TIMER34_OPMODE_BITS);
  442. T3CTL |= HAL_TIMER34_OPMODE_FREERUN;
  443. break;
  444. case HW_TIMER_4:
  445. T4CTL &= ~(HAL_TIMER34_OPMODE_BITS);
  446. T4CTL |= HAL_TIMER34_OPMODE_FREERUN;
  447. break;
  448. default:
  449. return HAL_TIMER_INVALID_ID;
  450. }
  451. break;
  452. case HAL_TIMER_MODE_CTC:
  453. switch (hwtimerid)
  454. {
  455. case HW_TIMER_1:
  456. T1CTL &= ~(HAL_TIMER1_OPMODE_BITS);
  457. T1CTL |= HAL_TIMER1_OPMODE_MODULO;
  458. break;
  459. case HW_TIMER_3:
  460. T3CTL &= ~(HAL_TIMER34_OPMODE_BITS);
  461. T3CTL |= HAL_TIMER34_OPMODE_MODULO;
  462. break;
  463. case HW_TIMER_4:
  464. T4CTL &= ~(HAL_TIMER34_OPMODE_BITS);
  465. T4CTL |= HAL_TIMER34_OPMODE_MODULO;
  466. break;
  467. default:
  468. return HAL_TIMER_INVALID_ID;
  469. }
  470. break;
  471. case HAL_TIMER_MODE_STOP:
  472. if (hwtimerid == HW_TIMER_1)
  473. {
  474. T1CTL &= ~(HAL_TIMER1_OPMODE_BITS);
  475. T1CTL |= HAL_TIMER1_OPMODE_STOP;
  476. }
  477. break;
  478. default:
  479. return HAL_TIMER_INVALID_OP_MODE;
  480. }
  481. return HAL_TIMER_OK;
  482. }
  483. /***************************************************************************************************
  484. * @fn halTimerSetChannelMode
  485. *
  486. * @brief Setup channel modes. Currently, only output compare mode is supported. Input capture
  487. * mode is NOT supported. Additionally, mapping timer channel inputs/outputs to I/O pins
  488. * is NOT supported.
  489. *
  490. * @param hwtimerid - ID of the timer
  491. * channelMode - channel mode of the timer
  492. *
  493. * @return Status - OK or Not OK
  494. ***************************************************************************************************/
  495. uint8 halTimerSetChannelMode (uint8 hwtimerid, uint8 channelMode)
  496. {
  497. switch (channelMode)
  498. {
  499. case HAL_TIMER_CH_MODE_OUTPUT_COMPARE:
  500. *(halTimerChannel[hwtimerid].TxCCTL) &= ~(T134CCTL_CMP_BITS);
  501. *(halTimerChannel[hwtimerid].TxCCTL) |= (T134CCTL_CMP_OC | T134CCTL_MODE);
  502. break;
  503. case HAL_TIMER_CH_MODE_INPUT_CAPTURE: /* Not Supported */
  504. /*
  505. *(halTimerChannel[hwtimerid].TxCCTL) &= ~(T134CCTL_CAP_BITS | T134CCTL_MODE);
  506. *(halTimerChannel[hwtimerid].TxCCTL) |= T134CCTL_CAP_RE;
  507. */
  508. break;
  509. default:
  510. return HAL_TIMER_INVALID_CH_MODE;
  511. }
  512. return HAL_TIMER_OK;
  513. }
  514. /***************************************************************************************************
  515. * @fn HalTimerInterruptEnable
  516. *
  517. * @brief Setup operate modes
  518. *
  519. * @param hwtimerid - ID of the timer
  520. * channelMode - channel mode
  521. * enable - TRUE or FALSE
  522. *
  523. * @return Status - OK or Not OK
  524. ***************************************************************************************************/
  525. uint8 HalTimerInterruptEnable (uint8 hwtimerid, uint8 channelMode, bool enable)
  526. {
  527. switch (channelMode)
  528. {
  529. case HAL_TIMER_CH_MODE_OVERFLOW:
  530. if (enable)
  531. {
  532. *(halTimerChannel[hwtimerid].TxOVF) |= halTimerChannel[hwtimerid].ovfbit;
  533. }
  534. else
  535. {
  536. *(halTimerChannel[hwtimerid].TxOVF) &= ((halTimerChannel[hwtimerid].ovfbit) ^ 0xFF);
  537. }
  538. break;
  539. case HAL_TIMER_CH_MODE_OUTPUT_COMPARE:
  540. case HAL_TIMER_CH_MODE_INPUT_CAPTURE:
  541. if (enable)
  542. {
  543. *(halTimerChannel[hwtimerid].TxCCTL) |= T134CCTL_IM;
  544. }
  545. else
  546. {
  547. *(halTimerChannel[hwtimerid].TxCCTL) &= ~(T134CCTL_IM);
  548. }
  549. break;
  550. default:
  551. return HAL_TIMER_INVALID_CH_MODE;
  552. }
  553. if (halTimerRecord[hwtimerid].intEnable)
  554. {
  555. IEN1 |= halTimerChannel[hwtimerid].intbit;
  556. }
  557. else
  558. {
  559. IEN1 &= ((halTimerChannel[hwtimerid].intbit) ^ 0xFF);
  560. }
  561. return HAL_TIMER_OK;
  562. }
  563. /***************************************************************************************************
  564. * @fn halTimerSendCallBack
  565. *
  566. * @brief Send Callback back to the caller
  567. *
  568. * @param timerId - ID of the timer
  569. * channel - channel where the interrupt occurs
  570. * channelMode - channel mode
  571. *
  572. *
  573. * @return None
  574. ***************************************************************************************************/
  575. void halTimerSendCallBack (uint8 timerId, uint8 channel, uint8 channelMode)
  576. {
  577. uint8 hwtimerid;
  578. hwtimerid = halTimerRemap (timerId);
  579. if (halTimerRecord[hwtimerid].callBackFunc)
  580. (halTimerRecord[hwtimerid].callBackFunc) (timerId, channel, channelMode);
  581. }
  582. /***************************************************************************************************
  583. * @fn halTimerRemap
  584. *
  585. * @brief Maps API HAL_TIMER_ID to HW Timer ID.
  586. * HAL_TIMER_0 --> HW Timer 3 8bit
  587. * HAL_TIMER_2 --> HW Timer 4 8bit
  588. * HAL_TIMER_3 --> HW Timer 1 16bit
  589. *
  590. * @param timerId - ID of the timer
  591. *
  592. * @return HW timer ID
  593. ***************************************************************************************************/
  594. uint8 halTimerRemap (uint8 timerId)
  595. {
  596. switch (timerId)
  597. {
  598. case HAL_TIMER_0:
  599. return HW_TIMER_3;
  600. case HAL_TIMER_2:
  601. return HW_TIMER_4;
  602. case HAL_TIMER_3:
  603. return HW_TIMER_1;
  604. default:
  605. return HW_TIMER_INVALID;
  606. }
  607. }
  608. /***************************************************************************************************
  609. * @fn halProcessTimer1
  610. *
  611. * @brief Processes Timer 1 Events.
  612. *
  613. * @param
  614. *
  615. * @return
  616. ***************************************************************************************************/
  617. void halProcessTimer1 (void)
  618. {
  619. if (halTimerRecord[halTimerRemap(HAL_TIMER_3)].channelMode == HAL_TIMER_CH_MODE_OUTPUT_COMPARE)
  620. {
  621. if (T1CTL & T1CTL_CH0IF)
  622. {
  623. T1CTL &= ~(T1CTL_CH0IF);
  624. halTimerSendCallBack (HAL_TIMER_3, HAL_TIMER_CHANNEL_A, HAL_TIMER_CH_MODE_OUTPUT_COMPARE);
  625. }
  626. if (T1CTL & T1CTL_CH1IF)
  627. {
  628. T1CTL &= ~(T1CTL_CH1IF);
  629. halTimerSendCallBack (HAL_TIMER_3, HAL_TIMER_CHANNEL_B, HAL_TIMER_CH_MODE_OUTPUT_COMPARE);
  630. }
  631. if (T1CTL & T1CTL_CH2IF)
  632. {
  633. T1CTL &= ~(T1CTL_CH2IF);
  634. halTimerSendCallBack (HAL_TIMER_3, HAL_TIMER_CHANNEL_C, HAL_TIMER_CH_MODE_OUTPUT_COMPARE);
  635. }
  636. }
  637. else if (halTimerRecord[halTimerRemap(HAL_TIMER_3)].channelMode == HAL_TIMER_CH_MODE_OVERFLOW)
  638. {
  639. if (T1CTL & T1CTL_OVFIF)
  640. {
  641. T1CTL &= ~(T1CTL_OVFIF);
  642. halTimerSendCallBack (HAL_TIMER_3, HAL_TIMER_CHANNEL_SINGLE, HAL_TIMER_CH_MODE_OVERFLOW);
  643. }
  644. }
  645. }
  646. /***************************************************************************************************
  647. * @fn halProcessTimer3
  648. *
  649. * @brief Processes Timer 3 Events.
  650. *
  651. * @param
  652. *
  653. * @return
  654. ***************************************************************************************************/
  655. void halProcessTimer3 (void)
  656. {
  657. if (halTimerRecord[halTimerRemap(HAL_TIMER_0)].channelMode == HAL_TIMER_CH_MODE_OUTPUT_COMPARE)
  658. {
  659. if (TIMIF & TIMIF_T3CH0IF)
  660. {
  661. TIMIF &= ~(TIMIF_T3CH0IF);
  662. halTimerSendCallBack (HAL_TIMER_0, HAL_TIMER_CHANNEL_A, HAL_TIMER_CH_MODE_OUTPUT_COMPARE);
  663. }
  664. if (TIMIF & TIMIF_T3CH1IF)
  665. {
  666. TIMIF &= ~(TIMIF_T3CH1IF);
  667. halTimerSendCallBack (HAL_TIMER_0, HAL_TIMER_CHANNEL_B, HAL_TIMER_CH_MODE_OUTPUT_COMPARE);
  668. }
  669. }
  670. else if (halTimerRecord[halTimerRemap(HAL_TIMER_0)].channelMode == HAL_TIMER_CH_MODE_OVERFLOW)
  671. {
  672. if (TIMIF & TIMIF_T3OVFIF)
  673. {
  674. TIMIF &= ~(TIMIF_T3OVFIF);
  675. halTimerSendCallBack (HAL_TIMER_0, HAL_TIMER_CHANNEL_SINGLE, HAL_TIMER_CH_MODE_OVERFLOW);
  676. }
  677. }
  678. }
  679. /***************************************************************************************************
  680. * @fn halProcessTimer4
  681. *
  682. * @brief Processes Timer 4 Events.
  683. *
  684. * @param
  685. *
  686. * @return
  687. ***************************************************************************************************/
  688. void halProcessTimer4 (void)
  689. {
  690. if (halTimerRecord[halTimerRemap(HAL_TIMER_2)].channelMode == HAL_TIMER_CH_MODE_OUTPUT_COMPARE)
  691. {
  692. if (TIMIF & TIMIF_T4CH0IF)
  693. {
  694. TIMIF &= ~(TIMIF_T4CH0IF);
  695. halTimerSendCallBack (HAL_TIMER_2, HAL_TIMER_CHANNEL_A, HAL_TIMER_CH_MODE_OUTPUT_COMPARE);
  696. }
  697. if (TIMIF & TIMIF_T4CH1IF)
  698. {
  699. TIMIF &= ~(TIMIF_T4CH1IF);
  700. halTimerSendCallBack (HAL_TIMER_2, HAL_TIMER_CHANNEL_B, HAL_TIMER_CH_MODE_OUTPUT_COMPARE);
  701. }
  702. }
  703. else if (halTimerRecord[halTimerRemap(HAL_TIMER_2)].channelMode == HAL_TIMER_CH_MODE_OVERFLOW)
  704. if (TIMIF & TIMIF_T4OVFIF)
  705. {
  706. TIMIF &= ~(TIMIF_T4OVFIF);
  707. halTimerSendCallBack (HAL_TIMER_2, HAL_TIMER_CHANNEL_SINGLE, HAL_TIMER_CH_MODE_OVERFLOW);
  708. }
  709. }
  710. /***************************************************************************************************
  711. * INTERRUPT SERVICE ROUTINE
  712. ***************************************************************************************************/
  713. /**************************************************************************************************
  714. * @fn halTimer1Isr
  715. *
  716. * @brief Timer 1 ISR
  717. *
  718. * @param
  719. *
  720. * @return
  721. **************************************************************************************************/
  722. HAL_ISR_FUNCTION( halTimer1Isr, T1_VECTOR )
  723. {
  724. halProcessTimer1 ();
  725. }
  726. /**************************************************************************************************
  727. * @fn halTimer3Isr
  728. *
  729. * @brief Timer 3 ISR
  730. *
  731. * @param
  732. *
  733. * @return
  734. **************************************************************************************************/
  735. HAL_ISR_FUNCTION( halTimer3Isr, T3_VECTOR )
  736. {
  737. halProcessTimer3 ();
  738. }
  739. /**************************************************************************************************
  740. * @fn halTimer4Isr
  741. *
  742. * @brief Timer 4 ISR
  743. *
  744. * @param
  745. *
  746. * @return
  747. **************************************************************************************************/
  748. HAL_ISR_FUNCTION( halTimer4Isr, T4_VECTOR )
  749. {
  750. halProcessTimer4 ();
  751. }
  752. /***************************************************************************************************
  753. ***************************************************************************************************/