1
0

systick.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //*****************************************************************************
  2. //
  3. // systick.c - Driver for the SysTick timer in NVIC.
  4. //
  5. // Copyright (c) 2005-2011 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 8264 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 function starts the SysTick counter. If an interrupt handler has been
  41. //! registered, it is called when the SysTick counter rolls over.
  42. //!
  43. //! \note Calling this function causes 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 the reload. Any write to this register clears the SysTick
  48. //! counter to 0 and causes 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 function stops the SysTick counter. If an interrupt handler has been
  67. //! registered, it is not 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 function registers the handler to be called when a SysTick interrupt
  88. //! occurs.
  89. //!
  90. //! \sa IntRegister() for important information about registering interrupt
  91. //! handlers.
  92. //!
  93. //! \return None.
  94. //
  95. //*****************************************************************************
  96. void
  97. SysTickIntRegister(void (*pfnHandler)(void))
  98. {
  99. //
  100. // Register the interrupt handler, returning an error if an error occurs.
  101. //
  102. IntRegister(FAULT_SYSTICK, pfnHandler);
  103. //
  104. // Enable the SysTick interrupt.
  105. //
  106. HWREG(NVIC_ST_CTRL) |= NVIC_ST_CTRL_INTEN;
  107. }
  108. //*****************************************************************************
  109. //
  110. //! Unregisters the interrupt handler for the SysTick interrupt.
  111. //!
  112. //! This function unregisters the handler to be called when a SysTick interrupt
  113. //! occurs.
  114. //!
  115. //! \sa IntRegister() for important information about registering interrupt
  116. //! handlers.
  117. //!
  118. //! \return None.
  119. //
  120. //*****************************************************************************
  121. void
  122. SysTickIntUnregister(void)
  123. {
  124. //
  125. // Disable the SysTick interrupt.
  126. //
  127. HWREG(NVIC_ST_CTRL) &= ~(NVIC_ST_CTRL_INTEN);
  128. //
  129. // Unregister the interrupt handler.
  130. //
  131. IntUnregister(FAULT_SYSTICK);
  132. }
  133. //*****************************************************************************
  134. //
  135. //! Enables the SysTick interrupt.
  136. //!
  137. //! This function enables the SysTick interrupt, allowing it to be
  138. //! reflected to the processor.
  139. //!
  140. //! \note The SysTick interrupt handler is not required to clear the SysTick
  141. //! interrupt source because it is cleared automatically by the NVIC when the
  142. //! interrupt handler is called.
  143. //!
  144. //! \return None.
  145. //
  146. //*****************************************************************************
  147. void
  148. SysTickIntEnable(void)
  149. {
  150. //
  151. // Enable the SysTick interrupt.
  152. //
  153. HWREG(NVIC_ST_CTRL) |= NVIC_ST_CTRL_INTEN;
  154. }
  155. //*****************************************************************************
  156. //
  157. //! Disables the SysTick interrupt.
  158. //!
  159. //! This function disables the SysTick interrupt, preventing it from being
  160. //! reflected to the processor.
  161. //!
  162. //! \return None.
  163. //
  164. //*****************************************************************************
  165. void
  166. SysTickIntDisable(void)
  167. {
  168. //
  169. // Disable the SysTick interrupt.
  170. //
  171. HWREG(NVIC_ST_CTRL) &= ~(NVIC_ST_CTRL_INTEN);
  172. }
  173. //*****************************************************************************
  174. //
  175. //! Sets the period of the SysTick counter.
  176. //!
  177. //! \param ulPeriod is the number of clock ticks in each period of the SysTick
  178. //! counter and must be between 1 and 16,777,216, inclusive.
  179. //!
  180. //! This function sets the rate at which the SysTick counter wraps, which
  181. //! equates to the number of processor clocks between interrupts.
  182. //!
  183. //! \note Calling this function does not cause the SysTick counter to reload
  184. //! immediately. If an immediate reload is required, the \b NVIC_ST_CURRENT
  185. //! register must be written. Any write to this register clears the SysTick
  186. //! counter to 0 and causes a reload with the \e ulPeriod supplied here on
  187. //! the next clock after SysTick is enabled.
  188. //!
  189. //! \return None.
  190. //
  191. //*****************************************************************************
  192. void
  193. SysTickPeriodSet(unsigned long ulPeriod)
  194. {
  195. //
  196. // Check the arguments.
  197. //
  198. ASSERT((ulPeriod > 0) && (ulPeriod <= 16777216));
  199. //
  200. // Set the period of the SysTick counter.
  201. //
  202. HWREG(NVIC_ST_RELOAD) = ulPeriod - 1;
  203. }
  204. //*****************************************************************************
  205. //
  206. //! Gets the period of the SysTick counter.
  207. //!
  208. //! This function returns the rate at which the SysTick counter wraps, which
  209. //! equates to the number of processor clocks between interrupts.
  210. //!
  211. //! \return Returns the period of the SysTick counter.
  212. //
  213. //*****************************************************************************
  214. unsigned long
  215. SysTickPeriodGet(void)
  216. {
  217. //
  218. // Return the period of the SysTick counter.
  219. //
  220. return(HWREG(NVIC_ST_RELOAD) + 1);
  221. }
  222. //*****************************************************************************
  223. //
  224. //! Gets the current value of the SysTick counter.
  225. //!
  226. //! This function returns the current value of the SysTick counter, which is
  227. //! a value between the period - 1 and zero, inclusive.
  228. //!
  229. //! \return Returns the current value of the SysTick counter.
  230. //
  231. //*****************************************************************************
  232. unsigned long
  233. SysTickValueGet(void)
  234. {
  235. //
  236. // Return the current value of the SysTick counter.
  237. //
  238. return(HWREG(NVIC_ST_CURRENT));
  239. }
  240. //*****************************************************************************
  241. //
  242. // Close the Doxygen group.
  243. //! @}
  244. //
  245. //*****************************************************************************