systick.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //###########################################################################
  2. //
  3. // FILE: systick.c
  4. //
  5. // TITLE: Stellaris style wrapper driver for C28x CPU Timer 0.
  6. //
  7. //###########################################################################
  8. // $TI Release: F2837xD Support Library v3.05.00.00 $
  9. // $Release Date: Tue Jun 26 03:15:23 CDT 2018 $
  10. // $Copyright:
  11. // Copyright (C) 2013-2018 Texas Instruments Incorporated - http://www.ti.com/
  12. //
  13. // Redistribution and use in source and binary forms, with or without
  14. // modification, are permitted provided that the following conditions
  15. // are met:
  16. //
  17. // Redistributions of source code must retain the above copyright
  18. // notice, this list of conditions and the following disclaimer.
  19. //
  20. // Redistributions in binary form must reproduce the above copyright
  21. // notice, this list of conditions and the following disclaimer in the
  22. // documentation and/or other materials provided with the
  23. // distribution.
  24. //
  25. // Neither the name of Texas Instruments Incorporated nor the names of
  26. // its contributors may be used to endorse or promote products derived
  27. // from this software without specific prior written permission.
  28. //
  29. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. // $
  41. //###########################################################################
  42. //*****************************************************************************
  43. //
  44. //! \addtogroup systick_api
  45. //! @{
  46. //
  47. //*****************************************************************************
  48. #include "F28x_Project.h"
  49. #include "inc/hw_ints.h"
  50. #include "inc/hw_types.h"
  51. #include "driverlib/debug.h"
  52. #include "driverlib/interrupt.h"
  53. #include "driverlib/systick.h"
  54. //*****************************************************************************
  55. //
  56. //! Initializes the Timer0 Module to act as a system tick
  57. //!
  58. //! \return None.
  59. //
  60. //*****************************************************************************
  61. void
  62. SysTickInit(void)
  63. {
  64. // CPU Timer 0
  65. // Initialize timer period to maximum:
  66. CpuTimer0Regs.PRD.all = 0xFFFFFFFF;
  67. // Initialize pre-scale counter to divide by 1 (SYSCLKOUT):
  68. CpuTimer0Regs.TPR.all = 0;
  69. CpuTimer0Regs.TPRH.all = 0;
  70. // Make sure timer is stopped:
  71. CpuTimer0Regs.TCR.bit.TSS = 1;
  72. // Reload all counter register with period value:
  73. CpuTimer0Regs.TCR.bit.TRB = 1;
  74. }
  75. //*****************************************************************************
  76. //
  77. //! Enables the SysTick counter.
  78. //!
  79. //! This will start the SysTick counter. If an interrupt handler has been
  80. //! registered, it will be called when the SysTick counter rolls over.
  81. //!
  82. //! \note Calling this function will cause the SysTick counter to (re)commence
  83. //! counting from its current value. The counter is not automatically reloaded
  84. //! with the period as specified in a previous call to SysTickPeriodSet(). If
  85. //! an immediate reload is required, the \b NVIC_ST_CURRENT register must be
  86. //! written to force this. Any write to this register clears the SysTick
  87. //! counter to 0 and will cause a reload with the supplied period on the next
  88. //! clock.
  89. //!
  90. //! \return None.
  91. //
  92. //*****************************************************************************
  93. void
  94. SysTickEnable(void)
  95. {
  96. //
  97. // Enable SysTick.
  98. //
  99. CpuTimer0Regs.TCR.bit.TRB = 1;
  100. CpuTimer0Regs.TCR.bit.TSS = 0;
  101. }
  102. //*****************************************************************************
  103. //
  104. //! Disables the SysTick counter.
  105. //!
  106. //! This will stop the SysTick counter. If an interrupt handler has been
  107. //! registered, it will no longer be called until SysTick is restarted.
  108. //!
  109. //! \return None.
  110. //
  111. //*****************************************************************************
  112. void
  113. SysTickDisable(void)
  114. {
  115. //
  116. // Disable SysTick.
  117. //
  118. StopCpuTimer0();
  119. }
  120. //*****************************************************************************
  121. //
  122. //! Registers an interrupt handler for the SysTick interrupt.
  123. //!
  124. //! \param pfnHandler is a pointer to the function to be called when the
  125. //! SysTick interrupt occurs.
  126. //!
  127. //! This sets the handler to be called when a SysTick interrupt occurs.
  128. //!
  129. //! \sa IntRegister() for important information about registering interrupt
  130. //! handlers.
  131. //!
  132. //! \return None.
  133. //
  134. //*****************************************************************************
  135. void
  136. SysTickIntRegister(void (*pfnHandler)(void))
  137. {
  138. //
  139. // Register the interrupt handler, returning an error if an error occurs.
  140. //
  141. IntRegister(INT_TINT0, pfnHandler);
  142. //
  143. // Enable the SysTick interrupt.
  144. //
  145. IntEnable(INT_TINT0);
  146. }
  147. //*****************************************************************************
  148. //
  149. //! Unregisters the interrupt handler for the SysTick interrupt.
  150. //!
  151. //! This function will clear the handler to be called when a SysTick interrupt
  152. //! occurs.
  153. //!
  154. //! \sa IntRegister() for important information about registering interrupt
  155. //! handlers.
  156. //!
  157. //! \return None.
  158. //
  159. //*****************************************************************************
  160. void
  161. SysTickIntUnregister(void)
  162. {
  163. //
  164. // Disable the SysTick interrupt.
  165. //
  166. IntDisable(INT_TINT0);
  167. //
  168. // Unregister the interrupt handler.
  169. //
  170. IntUnregister(INT_TINT0);
  171. }
  172. //*****************************************************************************
  173. //
  174. //! Enables the SysTick interrupt.
  175. //!
  176. //! This function will enable the SysTick interrupt, allowing it to be
  177. //! reflected to the processor.
  178. //!
  179. //! \note The SysTick interrupt handler does not need to clear the SysTick
  180. //! interrupt source as this is done automatically by NVIC when the interrupt
  181. //! handler is called.
  182. //!
  183. //! \return None.
  184. //
  185. //*****************************************************************************
  186. void
  187. SysTickIntEnable(void)
  188. {
  189. //
  190. // Enable the SysTick interrupt.
  191. //
  192. CpuTimer0Regs.TCR.bit.TIE = 1;
  193. IntEnable(INT_TINT0);
  194. }
  195. //*****************************************************************************
  196. //
  197. //! Disables the SysTick interrupt.
  198. //!
  199. //! This function will disable the SysTick interrupt, preventing it from being
  200. //! reflected to the processor.
  201. //!
  202. //! \return None.
  203. //
  204. //*****************************************************************************
  205. void
  206. SysTickIntDisable(void)
  207. {
  208. //
  209. // Disable the SysTick interrupt.
  210. //
  211. CpuTimer0Regs.TCR.bit.TIE = 0;
  212. IntDisable(INT_TINT0);
  213. }
  214. //*****************************************************************************
  215. //
  216. //! Sets the period of the SysTick counter.
  217. //!
  218. //! \param ui32Period is the number of clock ticks in each period of the SysTick
  219. //! counter; must be between 1 and 16,777,216, inclusive.
  220. //!
  221. //! This function sets the rate at which the SysTick counter wraps; this
  222. //! equates to the number of processor clocks between interrupts.
  223. //!
  224. //! \note Calling this function does not cause the SysTick counter to reload
  225. //! immediately. If an immediate reload is required, the \b NVIC_ST_CURRENT
  226. //! register must be written. Any write to this register clears the SysTick
  227. //! counter to 0 and will cause a reload with the \e ui32Period supplied here on
  228. //! the next clock after the SysTick is enabled.
  229. //!
  230. //! \return None.
  231. //
  232. //*****************************************************************************
  233. void
  234. SysTickPeriodSet(uint32_t ui32Period)
  235. {
  236. //
  237. // Check the arguments.
  238. //
  239. ASSERT((ui32Period > 0) && (ui32Period <= 16777216));
  240. //
  241. // Set the period of the SysTick counter.
  242. //
  243. CpuTimer0Regs.PRD.all = ui32Period;
  244. }
  245. //*****************************************************************************
  246. //
  247. //! Gets the period of the SysTick counter.
  248. //!
  249. //! This function returns the rate at which the SysTick counter wraps; this
  250. //! equates to the number of processor clocks between interrupts.
  251. //!
  252. //! \return Returns the period of the SysTick counter.
  253. //
  254. //*****************************************************************************
  255. uint32_t
  256. SysTickPeriodGet(void)
  257. {
  258. //
  259. // Return the period of the SysTick counter.
  260. //
  261. return(CpuTimer0Regs.PRD.all);
  262. }
  263. //*****************************************************************************
  264. //
  265. //! Gets the current value of the SysTick counter.
  266. //!
  267. //! This function returns the current value of the SysTick counter; this will
  268. //! be a value between the period - 1 and zero, inclusive.
  269. //!
  270. //! \return Returns the current value of the SysTick counter.
  271. //
  272. //*****************************************************************************
  273. uint32_t
  274. SysTickValueGet(void)
  275. {
  276. //
  277. // Return the current value of the SysTick counter.
  278. //
  279. return(CpuTimer0Regs.TIM.all);
  280. }
  281. //*****************************************************************************
  282. //
  283. // Close the Doxygen group.
  284. //! @}
  285. //
  286. //*****************************************************************************