systick.c 8.5 KB

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