systick.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //*****************************************************************************
  2. //
  3. // systick.c - Driver for the SysTick timer in NVIC.
  4. //
  5. // Copyright (c) 2005-2020 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. // This is part of revision 2.2.0.295 of the Tiva Peripheral Driver Library.
  37. //
  38. //*****************************************************************************
  39. //*****************************************************************************
  40. //
  41. //! \addtogroup systick_api
  42. //! @{
  43. //
  44. //*****************************************************************************
  45. #include <stdbool.h>
  46. #include <stdint.h>
  47. #include "inc/hw_ints.h"
  48. #include "inc/hw_nvic.h"
  49. #include "inc/hw_types.h"
  50. #include "driverlib/debug.h"
  51. #include "driverlib/interrupt.h"
  52. #include "driverlib/systick.h"
  53. //*****************************************************************************
  54. //
  55. //! Enables the SysTick counter.
  56. //!
  57. //! This function starts the SysTick counter. If an interrupt handler has been
  58. //! registered, it is called when the SysTick counter rolls over.
  59. //!
  60. //! \note Calling this function causes the SysTick counter to (re)commence
  61. //! counting from its current value. The counter is not automatically reloaded
  62. //! with the period as specified in a previous call to SysTickPeriodSet(). If
  63. //! an immediate reload is required, the \b NVIC_ST_CURRENT register must be
  64. //! written to force the reload. Any write to this register clears the SysTick
  65. //! counter to 0 and causes a reload with the supplied period on the next
  66. //! clock.
  67. //!
  68. //! \return None.
  69. //
  70. //*****************************************************************************
  71. void
  72. SysTickEnable(void)
  73. {
  74. //
  75. // Enable SysTick.
  76. //
  77. HWREG(NVIC_ST_CTRL) |= NVIC_ST_CTRL_CLK_SRC | NVIC_ST_CTRL_ENABLE;
  78. }
  79. //*****************************************************************************
  80. //
  81. //! Disables the SysTick counter.
  82. //!
  83. //! This function stops the SysTick counter. If an interrupt handler has been
  84. //! registered, it is not called until SysTick is restarted.
  85. //!
  86. //! \return None.
  87. //
  88. //*****************************************************************************
  89. void
  90. SysTickDisable(void)
  91. {
  92. //
  93. // Disable SysTick.
  94. //
  95. HWREG(NVIC_ST_CTRL) &= ~(NVIC_ST_CTRL_ENABLE);
  96. }
  97. //*****************************************************************************
  98. //
  99. //! Registers an interrupt handler for the SysTick interrupt.
  100. //!
  101. //! \param pfnHandler is a pointer to the function to be called when the
  102. //! SysTick interrupt occurs.
  103. //!
  104. //! This function registers the handler to be called when a SysTick interrupt
  105. //! occurs.
  106. //!
  107. //! \sa IntRegister() for important information about registering interrupt
  108. //! handlers.
  109. //!
  110. //! \return None.
  111. //
  112. //*****************************************************************************
  113. void
  114. SysTickIntRegister(void (*pfnHandler)(void))
  115. {
  116. //
  117. // Register the interrupt handler, returning an error if an error occurs.
  118. //
  119. IntRegister(FAULT_SYSTICK, pfnHandler);
  120. //
  121. // Enable the SysTick interrupt.
  122. //
  123. HWREG(NVIC_ST_CTRL) |= NVIC_ST_CTRL_INTEN;
  124. }
  125. //*****************************************************************************
  126. //
  127. //! Unregisters the interrupt handler for the SysTick interrupt.
  128. //!
  129. //! This function unregisters the handler to be called when a SysTick interrupt
  130. //! occurs.
  131. //!
  132. //! \sa IntRegister() for important information about registering interrupt
  133. //! handlers.
  134. //!
  135. //! \return None.
  136. //
  137. //*****************************************************************************
  138. void
  139. SysTickIntUnregister(void)
  140. {
  141. //
  142. // Disable the SysTick interrupt.
  143. //
  144. HWREG(NVIC_ST_CTRL) &= ~(NVIC_ST_CTRL_INTEN);
  145. //
  146. // Unregister the interrupt handler.
  147. //
  148. IntUnregister(FAULT_SYSTICK);
  149. }
  150. //*****************************************************************************
  151. //
  152. //! Enables the SysTick interrupt.
  153. //!
  154. //! This function enables the SysTick interrupt, allowing it to be
  155. //! reflected to the processor.
  156. //!
  157. //! \note The SysTick interrupt handler is not required to clear the SysTick
  158. //! interrupt source because it is cleared automatically by the NVIC when the
  159. //! interrupt handler is called.
  160. //!
  161. //! \return None.
  162. //
  163. //*****************************************************************************
  164. void
  165. SysTickIntEnable(void)
  166. {
  167. //
  168. // Enable the SysTick interrupt.
  169. //
  170. HWREG(NVIC_ST_CTRL) |= NVIC_ST_CTRL_INTEN;
  171. }
  172. //*****************************************************************************
  173. //
  174. //! Disables the SysTick interrupt.
  175. //!
  176. //! This function disables the SysTick interrupt, preventing it from being
  177. //! reflected to the processor.
  178. //!
  179. //! \return None.
  180. //
  181. //*****************************************************************************
  182. void
  183. SysTickIntDisable(void)
  184. {
  185. //
  186. // Disable the SysTick interrupt.
  187. //
  188. HWREG(NVIC_ST_CTRL) &= ~(NVIC_ST_CTRL_INTEN);
  189. }
  190. //*****************************************************************************
  191. //
  192. //! Sets the period of the SysTick counter.
  193. //!
  194. //! \param ui32Period is the number of clock ticks in each period of the
  195. //! SysTick counter and must be between 1 and 16,777,216, inclusive.
  196. //!
  197. //! This function sets the rate at which the SysTick counter wraps, which
  198. //! equates to the number of processor clocks between interrupts.
  199. //!
  200. //! \note Calling this function does not cause the SysTick counter to reload
  201. //! immediately. If an immediate reload is required, the \b NVIC_ST_CURRENT
  202. //! register must be written. Any write to this register clears the SysTick
  203. //! counter to 0 and causes a reload with the \e ui32Period supplied here on
  204. //! the next clock after SysTick is enabled.
  205. //!
  206. //! \return None.
  207. //
  208. //*****************************************************************************
  209. void
  210. SysTickPeriodSet(uint32_t ui32Period)
  211. {
  212. //
  213. // Check the arguments.
  214. //
  215. ASSERT((ui32Period > 0) && (ui32Period <= 16777216));
  216. //
  217. // Set the period of the SysTick counter.
  218. //
  219. HWREG(NVIC_ST_RELOAD) = ui32Period - 1;
  220. }
  221. //*****************************************************************************
  222. //
  223. //! Gets the period of the SysTick counter.
  224. //!
  225. //! This function returns the rate at which the SysTick counter wraps, which
  226. //! equates to the number of processor clocks between interrupts.
  227. //!
  228. //! \return Returns the period of the SysTick counter.
  229. //
  230. //*****************************************************************************
  231. uint32_t
  232. SysTickPeriodGet(void)
  233. {
  234. //
  235. // Return the period of the SysTick counter.
  236. //
  237. return(HWREG(NVIC_ST_RELOAD) + 1);
  238. }
  239. //*****************************************************************************
  240. //
  241. //! Gets the current value of the SysTick counter.
  242. //!
  243. //! This function returns the current value of the SysTick counter, which is
  244. //! a value between the period - 1 and zero, inclusive.
  245. //!
  246. //! \return Returns the current value of the SysTick counter.
  247. //
  248. //*****************************************************************************
  249. uint32_t
  250. SysTickValueGet(void)
  251. {
  252. //
  253. // Return the current value of the SysTick counter.
  254. //
  255. return(HWREG(NVIC_ST_CURRENT));
  256. }
  257. //*****************************************************************************
  258. //
  259. // Close the Doxygen group.
  260. //! @}
  261. //
  262. //*****************************************************************************