1
0

systick.c 8.3 KB

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