systick.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //*****************************************************************************
  2. //
  3. // systick.c - Driver for the SysTick timer in NVIC.
  4. //
  5. // Copyright (c) 2005-2010 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Texas Instruments (TI) is supplying this software for use solely and
  9. // exclusively on TI's microcontroller products. The software is owned by
  10. // TI and/or its suppliers, and is protected under applicable copyright
  11. // laws. You may not combine this software with "viral" open-source
  12. // software in order to form a larger program.
  13. //
  14. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  15. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  16. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  18. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  19. // DAMAGES, FOR ANY REASON WHATSOEVER.
  20. //
  21. // This is part of revision 6459 of the Stellaris Peripheral Driver Library.
  22. //
  23. //*****************************************************************************
  24. //*****************************************************************************
  25. //
  26. //! \addtogroup systick_api
  27. //! @{
  28. //
  29. //*****************************************************************************
  30. #include "inc/hw_ints.h"
  31. #include "inc/hw_nvic.h"
  32. #include "inc/hw_types.h"
  33. #include "driverlib/debug.h"
  34. #include "driverlib/interrupt.h"
  35. #include "driverlib/systick.h"
  36. //*****************************************************************************
  37. //
  38. //! Enables the SysTick counter.
  39. //!
  40. //! This will start the SysTick counter. If an interrupt handler has been
  41. //! registered, it will be called when the SysTick counter rolls over.
  42. //!
  43. //! \note Calling this function will cause the SysTick counter to (re)commence
  44. //! counting from its current value. The counter is not automatically reloaded
  45. //! with the period as specified in a previous call to SysTickPeriodSet(). If
  46. //! an immediate reload is required, the \b NVIC_ST_CURRENT register must be
  47. //! written to force this. Any write to this register clears the SysTick
  48. //! counter to 0 and will cause a reload with the supplied period on the next
  49. //! clock.
  50. //!
  51. //! \return None.
  52. //
  53. //*****************************************************************************
  54. void
  55. SysTickEnable(void)
  56. {
  57. //
  58. // Enable SysTick.
  59. //
  60. HWREG(NVIC_ST_CTRL) |= NVIC_ST_CTRL_CLK_SRC | NVIC_ST_CTRL_ENABLE;
  61. }
  62. //*****************************************************************************
  63. //
  64. //! Disables the SysTick counter.
  65. //!
  66. //! This will stop the SysTick counter. If an interrupt handler has been
  67. //! registered, it will no longer be called until SysTick is restarted.
  68. //!
  69. //! \return None.
  70. //
  71. //*****************************************************************************
  72. void
  73. SysTickDisable(void)
  74. {
  75. //
  76. // Disable SysTick.
  77. //
  78. HWREG(NVIC_ST_CTRL) &= ~(NVIC_ST_CTRL_ENABLE);
  79. }
  80. //*****************************************************************************
  81. //
  82. //! Registers an interrupt handler for the SysTick interrupt.
  83. //!
  84. //! \param pfnHandler is a pointer to the function to be called when the
  85. //! SysTick interrupt occurs.
  86. //!
  87. //! This sets the handler to be called when a SysTick interrupt occurs.
  88. //!
  89. //! \sa IntRegister() for important information about registering interrupt
  90. //! handlers.
  91. //!
  92. //! \return None.
  93. //
  94. //*****************************************************************************
  95. void
  96. SysTickIntRegister(void (*pfnHandler)(void))
  97. {
  98. //
  99. // Register the interrupt handler, returning an error if an error occurs.
  100. //
  101. IntRegister(FAULT_SYSTICK, pfnHandler);
  102. //
  103. // Enable the SysTick interrupt.
  104. //
  105. HWREG(NVIC_ST_CTRL) |= NVIC_ST_CTRL_INTEN;
  106. }
  107. //*****************************************************************************
  108. //
  109. //! Unregisters the interrupt handler for the SysTick interrupt.
  110. //!
  111. //! This function will clear the handler to be called when a SysTick interrupt
  112. //! occurs.
  113. //!
  114. //! \sa IntRegister() for important information about registering interrupt
  115. //! handlers.
  116. //!
  117. //! \return None.
  118. //
  119. //*****************************************************************************
  120. void
  121. SysTickIntUnregister(void)
  122. {
  123. //
  124. // Disable the SysTick interrupt.
  125. //
  126. HWREG(NVIC_ST_CTRL) &= ~(NVIC_ST_CTRL_INTEN);
  127. //
  128. // Unregister the interrupt handler.
  129. //
  130. IntUnregister(FAULT_SYSTICK);
  131. }
  132. //*****************************************************************************
  133. //
  134. //! Enables the SysTick interrupt.
  135. //!
  136. //! This function will enable the SysTick interrupt, allowing it to be
  137. //! reflected to the processor.
  138. //!
  139. //! \note The SysTick interrupt handler does not need to clear the SysTick
  140. //! interrupt source as this is done automatically by NVIC when the interrupt
  141. //! handler is called.
  142. //!
  143. //! \return None.
  144. //
  145. //*****************************************************************************
  146. void
  147. SysTickIntEnable(void)
  148. {
  149. //
  150. // Enable the SysTick interrupt.
  151. //
  152. HWREG(NVIC_ST_CTRL) |= NVIC_ST_CTRL_INTEN;
  153. }
  154. //*****************************************************************************
  155. //
  156. //! Disables the SysTick interrupt.
  157. //!
  158. //! This function will disable the SysTick interrupt, preventing it from being
  159. //! reflected to the processor.
  160. //!
  161. //! \return None.
  162. //
  163. //*****************************************************************************
  164. void
  165. SysTickIntDisable(void)
  166. {
  167. //
  168. // Disable the SysTick interrupt.
  169. //
  170. HWREG(NVIC_ST_CTRL) &= ~(NVIC_ST_CTRL_INTEN);
  171. }
  172. //*****************************************************************************
  173. //
  174. //! Sets the period of the SysTick counter.
  175. //!
  176. //! \param ulPeriod is the number of clock ticks in each period of the SysTick
  177. //! counter; must be between 1 and 16,777,216, inclusive.
  178. //!
  179. //! This function sets the rate at which the SysTick counter wraps; this
  180. //! equates to the number of processor clocks between interrupts.
  181. //!
  182. //! \note Calling this function does not cause the SysTick counter to reload
  183. //! immediately. If an immediate reload is required, the \b NVIC_ST_CURRENT
  184. //! register must be written. Any write to this register clears the SysTick
  185. //! counter to 0 and will cause a reload with the \e ulPeriod supplied here on
  186. //! the next clock after the SysTick is enabled.
  187. //!
  188. //! \return None.
  189. //
  190. //*****************************************************************************
  191. void
  192. SysTickPeriodSet(unsigned long ulPeriod)
  193. {
  194. //
  195. // Check the arguments.
  196. //
  197. ASSERT((ulPeriod > 0) && (ulPeriod <= 16777216));
  198. //
  199. // Set the period of the SysTick counter.
  200. //
  201. HWREG(NVIC_ST_RELOAD) = ulPeriod - 1;
  202. }
  203. //*****************************************************************************
  204. //
  205. //! Gets the period of the SysTick counter.
  206. //!
  207. //! This function returns the rate at which the SysTick counter wraps; this
  208. //! equates to the number of processor clocks between interrupts.
  209. //!
  210. //! \return Returns the period of the SysTick counter.
  211. //
  212. //*****************************************************************************
  213. unsigned long
  214. SysTickPeriodGet(void)
  215. {
  216. //
  217. // Return the period of the SysTick counter.
  218. //
  219. return(HWREG(NVIC_ST_RELOAD) + 1);
  220. }
  221. //*****************************************************************************
  222. //
  223. //! Gets the current value of the SysTick counter.
  224. //!
  225. //! This function returns the current value of the SysTick counter; this will
  226. //! be a value between the period - 1 and zero, inclusive.
  227. //!
  228. //! \return Returns the current value of the SysTick counter.
  229. //
  230. //*****************************************************************************
  231. unsigned long
  232. SysTickValueGet(void)
  233. {
  234. //
  235. // Return the current value of the SysTick counter.
  236. //
  237. return(HWREG(NVIC_ST_CURRENT));
  238. }
  239. //*****************************************************************************
  240. //
  241. // Close the Doxygen group.
  242. //! @}
  243. //
  244. //*****************************************************************************