timer_if.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Driverlib includes
  2. #include <inc/hw_types.h>
  3. #include <inc/hw_memmap.h>
  4. #include <inc/hw_ints.h>
  5. #include <driverlib/debug.h>
  6. #include <driverlib/interrupt.h>
  7. #include <driverlib/timer.h>
  8. #include <driverlib/rom.h>
  9. #include <driverlib/rom_map.h>
  10. #include <driverlib/prcm.h>
  11. // TI-RTOS includes
  12. #if defined(USE_TIRTOS) || defined(USE_FREERTOS) || defined(SL_PLATFORM_MULTI_THREADED)
  13. #include <stdlib.h>
  14. #include "osi.h"
  15. #endif
  16. #include "timer_if.h"
  17. static unsigned char
  18. GetPeripheralIntNum(unsigned long ulBase, unsigned long ulTimer)
  19. {
  20. if(ulTimer == TIMER_A)
  21. {
  22. switch(ulBase)
  23. {
  24. case TIMERA0_BASE:
  25. return INT_TIMERA0A;
  26. case TIMERA1_BASE:
  27. return INT_TIMERA1A;
  28. case TIMERA2_BASE:
  29. return INT_TIMERA2A;
  30. case TIMERA3_BASE:
  31. return INT_TIMERA3A;
  32. default:
  33. return INT_TIMERA0A;
  34. }
  35. }
  36. else if(ulTimer == TIMER_B)
  37. {
  38. switch(ulBase)
  39. {
  40. case TIMERA0_BASE:
  41. return INT_TIMERA0B;
  42. case TIMERA1_BASE:
  43. return INT_TIMERA1B;
  44. case TIMERA2_BASE:
  45. return INT_TIMERA2B;
  46. case TIMERA3_BASE:
  47. return INT_TIMERA3B;
  48. default:
  49. return INT_TIMERA0B;
  50. }
  51. }
  52. else
  53. {
  54. return INT_TIMERA0A;
  55. }
  56. }
  57. //*****************************************************************************
  58. //
  59. //! Initializing the Timer
  60. //!
  61. //! \param ePeripheral is the peripheral which need to be initialized.
  62. //! \param ulBase is the base address for the timer.
  63. //! \param ulConfig is the configuration for the timer.
  64. //! \param ulTimer selects amoung the TIMER_A or TIMER_B or TIMER_BOTH.
  65. //! \param ulValue is the timer prescale value which must be between 0 and
  66. //! 255 (inclusive) for 16/32-bit timers and between 0 and 65535 (inclusive)
  67. //! for 32/64-bit timers.
  68. //! This function
  69. //! 1. Enables and reset the peripheral for the timer.
  70. //! 2. Configures and set the prescale value for the timer.
  71. //!
  72. //! \return none
  73. //
  74. //*****************************************************************************
  75. void Timer_IF_Init( unsigned long ePeripheral, unsigned long ulBase, unsigned
  76. long ulConfig, unsigned long ulTimer, unsigned long ulValue)
  77. {
  78. //
  79. // Initialize GPT A0 (in 32 bit mode) as periodic down counter.
  80. //
  81. MAP_PRCMPeripheralClkEnable(ePeripheral, PRCM_RUN_MODE_CLK);
  82. MAP_PRCMPeripheralReset(ePeripheral);
  83. MAP_TimerConfigure(ulBase,ulConfig);
  84. MAP_TimerPrescaleSet(ulBase,ulTimer,ulValue);
  85. }
  86. //*****************************************************************************
  87. //
  88. //! setting up the timer
  89. //!
  90. //! \param ulBase is the base address for the timer.
  91. //! \param ulTimer selects between the TIMER_A or TIMER_B or TIMER_BOTH.
  92. //! \param TimerBaseIntHandler is the pointer to the function that handles the
  93. //! interrupt for the Timer
  94. //!
  95. //! This function
  96. //! 1. Register the function handler for the timer interrupt.
  97. //! 2. enables the timer interrupt.
  98. //!
  99. //! \return none
  100. //
  101. //*****************************************************************************
  102. void Timer_IF_IntSetup(unsigned long ulBase, unsigned long ulTimer,
  103. void (*TimerBaseIntHandler)(void))
  104. {
  105. //
  106. // Setup the interrupts for the timer timeouts.
  107. //
  108. #if defined(USE_TIRTOS) || defined(USE_FREERTOS) || defined(SL_PLATFORM_MULTI_THREADED)
  109. // USE_TIRTOS: if app uses TI-RTOS (either networking/non-networking)
  110. // USE_FREERTOS: if app uses Free-RTOS (either networking/non-networking)
  111. // SL_PLATFORM_MULTI_THREADED: if app uses any OS + networking(simplelink)
  112. if(ulTimer == TIMER_BOTH)
  113. {
  114. osi_InterruptRegister(GetPeripheralIntNum(ulBase, TIMER_A),
  115. TimerBaseIntHandler, INT_PRIORITY_LVL_1);
  116. osi_InterruptRegister(GetPeripheralIntNum(ulBase, TIMER_B),
  117. TimerBaseIntHandler, INT_PRIORITY_LVL_1);
  118. }
  119. else
  120. {
  121. osi_InterruptRegister(GetPeripheralIntNum(ulBase, ulTimer),
  122. TimerBaseIntHandler, INT_PRIORITY_LVL_1);
  123. }
  124. #else
  125. MAP_IntPrioritySet(GetPeripheralIntNum(ulBase, ulTimer), INT_PRIORITY_LVL_1);
  126. MAP_TimerIntRegister(ulBase, ulTimer, TimerBaseIntHandler);
  127. #endif
  128. if(ulTimer == TIMER_BOTH)
  129. {
  130. MAP_TimerIntEnable(ulBase, TIMER_TIMA_TIMEOUT|TIMER_TIMB_TIMEOUT);
  131. }
  132. else
  133. {
  134. MAP_TimerIntEnable(ulBase, ((ulTimer == TIMER_A) ? TIMER_TIMA_TIMEOUT :
  135. TIMER_TIMB_TIMEOUT));
  136. }
  137. }
  138. //*****************************************************************************
  139. //
  140. //! clears the timer interrupt
  141. //!
  142. //! \param ulBase is the base address for the timer.
  143. //!
  144. //! This function
  145. //! 1. clears the interrupt with given base.
  146. //!
  147. //! \return none
  148. //
  149. //*****************************************************************************
  150. void Timer_IF_InterruptClear(unsigned long ulBase)
  151. {
  152. unsigned long ulInts;
  153. ulInts = MAP_TimerIntStatus(ulBase, true);
  154. //
  155. // Clear the timer interrupt.
  156. //
  157. MAP_TimerIntClear(ulBase, ulInts);
  158. }
  159. //*****************************************************************************
  160. //
  161. //! starts the timer
  162. //!
  163. //! \param ulBase is the base address for the timer.
  164. //! \param ulTimer selects amoung the TIMER_A or TIMER_B or TIMER_BOTH.
  165. //! \param ulValue is the time delay in mSec after that run out,
  166. //! timer gives an interrupt.
  167. //!
  168. //! This function
  169. //! 1. Load the Timer with the specified value.
  170. //! 2. enables the timer.
  171. //!
  172. //! \return none
  173. //!
  174. //! \Note- HW Timer runs on 80MHz clock
  175. //
  176. //*****************************************************************************
  177. void Timer_IF_Start(unsigned long ulBase, unsigned long ulTimer,
  178. unsigned long ulValue)
  179. {
  180. MAP_TimerLoadSet(ulBase,ulTimer,MILLISECONDS_TO_TICKS(ulValue));
  181. //
  182. // Enable the GPT
  183. //
  184. MAP_TimerEnable(ulBase,ulTimer);
  185. }
  186. //*****************************************************************************
  187. //
  188. //! disable the timer
  189. //!
  190. //! \param ulBase is the base address for the timer.
  191. //! \param ulTimer selects amoung the TIMER_A or TIMER_B or TIMER_BOTH.
  192. //!
  193. //! This function
  194. //! 1. disables the interupt.
  195. //!
  196. //! \return none
  197. //
  198. //*****************************************************************************
  199. void Timer_IF_Stop(unsigned long ulBase, unsigned long ulTimer)
  200. {
  201. //
  202. // Disable the GPT
  203. //
  204. MAP_TimerDisable(ulBase,ulTimer);
  205. }
  206. //*****************************************************************************
  207. //
  208. //! De-Initialize the timer
  209. //!
  210. //! \param uiGPTBaseAddr
  211. //! \param ulTimer
  212. //!
  213. //! This function
  214. //! 1. disable the timer interrupts
  215. //! 2. unregister the timer interrupt
  216. //!
  217. //! \return None.
  218. //
  219. //*****************************************************************************
  220. void Timer_IF_DeInit(unsigned long ulBase,unsigned long ulTimer)
  221. {
  222. //
  223. // Disable the timer interrupt
  224. //
  225. MAP_TimerIntDisable(ulBase,TIMER_TIMA_TIMEOUT|TIMER_TIMB_TIMEOUT);
  226. //
  227. // Unregister the timer interrupt
  228. //
  229. MAP_TimerIntUnregister(ulBase,ulTimer);
  230. }
  231. //*****************************************************************************
  232. //
  233. //! starts the timer
  234. //!
  235. //! \param ulBase is the base address for the timer.
  236. //! \param ulTimer selects between the TIMER A and TIMER B.
  237. //! \param ulValue is timer reload value (mSec) after which the timer will run out and gives an interrupt.
  238. //!
  239. //! This function
  240. //! 1. Reload the Timer with the specified value.
  241. //!
  242. //! \return none
  243. //
  244. //*****************************************************************************
  245. void Timer_IF_ReLoad(unsigned long ulBase, unsigned long ulTimer,
  246. unsigned long ulValue)
  247. {
  248. MAP_TimerLoadSet(ulBase,ulTimer,MILLISECONDS_TO_TICKS(ulValue));
  249. }
  250. //*****************************************************************************
  251. //
  252. //! starts the timer
  253. //!
  254. //! \param ulBase is the base address for the timer.
  255. //! \param ulTimer selects amoung the TIMER_A or TIMER_B or TIMER_BOTH.
  256. //!
  257. //! This function
  258. //! 1. returns the timer value.
  259. //!
  260. //! \return Timer Value.
  261. //
  262. //*****************************************************************************
  263. unsigned int Timer_IF_GetCount(unsigned long ulBase, unsigned long ulTimer)
  264. {
  265. unsigned long ulCounter;
  266. ulCounter = MAP_TimerValueGet(ulBase, ulTimer);
  267. return 0xFFFFFFFF - ulCounter;
  268. }
  269. //*****************************************************************************
  270. //
  271. // Close the Doxygen group.
  272. //! @}
  273. //
  274. //*****************************************************************************