lpc_systick.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**********************************************************************
  2. * $Id$ lpc_systick.c 2011-06-02
  3. *//**
  4. * @file lpc_systick.c
  5. * @brief Contains all functions support for SysTick firmware library
  6. * on LPC
  7. * @version 1.0
  8. * @date 02. June. 2011
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2011, NXP Semiconductor
  12. * All rights reserved.
  13. *
  14. ***********************************************************************
  15. * Software that is described herein is for illustrative purposes only
  16. * which provides customers with programming information regarding the
  17. * products. This software is supplied "AS IS" without any warranties.
  18. * NXP Semiconductors assumes no responsibility or liability for the
  19. * use of the software, conveys no license or title under any patent,
  20. * copyright, or mask work right to the product. NXP Semiconductors
  21. * reserves the right to make changes in the software without
  22. * notification. NXP Semiconductors also make no representation or
  23. * warranty that such application will be suitable for the specified
  24. * use without further testing or modification.
  25. * Permission to use, copy, modify, and distribute this software and its
  26. * documentation is hereby granted, under NXP Semiconductors'
  27. * relevant copyright in the software, without fee, provided that it
  28. * is used in conjunction with NXP Semiconductors microcontrollers. This
  29. * copyright, permission, and disclaimer notice must appear in all copies of
  30. * this code.
  31. **********************************************************************/
  32. /* Peripheral group ----------------------------------------------------------- */
  33. /** @addtogroup SYSTICK
  34. * @{
  35. */
  36. #ifdef __BUILD_WITH_EXAMPLE__
  37. #include "lpc_libcfg.h"
  38. #else
  39. #include "lpc_libcfg_default.h"
  40. #endif /* __BUILD_WITH_EXAMPLE__ */
  41. #ifdef _SYSTICK
  42. /* Includes ------------------------------------------------------------------- */
  43. #include "lpc_systick.h"
  44. #include "lpc_clkpwr.h"
  45. /* Public Functions ----------------------------------------------------------- */
  46. /** @addtogroup SYSTICK_Public_Functions
  47. * @{
  48. */
  49. /*********************************************************************//**
  50. * @brief Initial System Tick with using internal CPU clock source
  51. * @param[in] time time interval(ms)
  52. * @return None
  53. * Note: time interval parameter should be less than:
  54. * 1/cclk * (2^24) * 1000 (ms)
  55. * In this case, with cclk = 96Mhz, time interval value < 174ms
  56. **********************************************************************/
  57. void SYSTICK_InternalInit(uint32_t time)
  58. {
  59. uint32_t cclk;
  60. float maxtime;
  61. cclk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_CPU);
  62. /* With internal CPU clock frequency for LPC178X is 'SystemCoreClock'
  63. * And limit 24 bit for RELOAD value
  64. * So the maximum time can be set:
  65. * 1/SystemCoreClock * (2^24) * 1000 (ms)
  66. */
  67. //check time value is available or not
  68. maxtime = (1<<24)/(cclk / 1000) ;
  69. if(time > maxtime)
  70. {
  71. //Error loop
  72. while(1);
  73. }
  74. else
  75. {
  76. //Select CPU clock is System Tick clock source
  77. SysTick->CTRL |= ST_CTRL_CLKSOURCE;
  78. /* Set RELOAD value
  79. * RELOAD = (SystemCoreClock/1000) * time - 1
  80. * with time base is millisecond
  81. */
  82. SysTick->LOAD = (cclk/1000)*time - 1;
  83. }
  84. }
  85. /*********************************************************************//**
  86. * @brief Initial System Tick with using external clock source
  87. * @param[in] freq external clock frequency(Hz)
  88. * @param[in] time time interval(ms)
  89. * @return None
  90. **********************************************************************/
  91. void SYSTICK_ExternalInit(uint32_t freq, uint32_t time)
  92. {
  93. float maxtime;
  94. /* With external clock frequency for LPC178X is 'freq'
  95. * And limit 24 bit for RELOAD value
  96. * So the maximum time can be set:
  97. * 1/freq * (2^24) * 1000 (ms)
  98. */
  99. //check time value is available or not
  100. maxtime = (1<<24)/(freq / 1000) ;
  101. if (time>maxtime)
  102. {
  103. //Error Loop
  104. while(1);
  105. }
  106. else
  107. {
  108. //Select external clock is System Tick clock source
  109. SysTick->CTRL &= ~ ST_CTRL_CLKSOURCE;
  110. /* Set RELOAD value
  111. * RELOAD = (freq/1000) * time - 1
  112. * with time base is millisecond
  113. */
  114. maxtime = (freq/1000)*time - 1;
  115. SysTick->LOAD = (freq/1000)*time - 1;
  116. }
  117. }
  118. /*********************************************************************//**
  119. * @brief Enable/disable System Tick counter
  120. * @param[in] NewState System Tick counter status, should be:
  121. * - ENABLE
  122. * - DISABLE
  123. * @return None
  124. **********************************************************************/
  125. void SYSTICK_Cmd(FunctionalState NewState)
  126. {
  127. if(NewState == ENABLE)
  128. //Enable System Tick counter
  129. SysTick->CTRL |= ST_CTRL_ENABLE;
  130. else
  131. //Disable System Tick counter
  132. SysTick->CTRL &= ~ST_CTRL_ENABLE;
  133. }
  134. /*********************************************************************//**
  135. * @brief Enable/disable System Tick interrupt
  136. * @param[in] NewState System Tick interrupt status, should be:
  137. * - ENABLE
  138. * - DISABLE
  139. * @return None
  140. **********************************************************************/
  141. void SYSTICK_IntCmd(FunctionalState NewState)
  142. {
  143. if(NewState == ENABLE)
  144. //Enable System Tick counter
  145. SysTick->CTRL |= ST_CTRL_TICKINT;
  146. else
  147. //Disable System Tick counter
  148. SysTick->CTRL &= ~ST_CTRL_TICKINT;
  149. }
  150. /*********************************************************************//**
  151. * @brief Get current value of System Tick counter
  152. * @param[in] None
  153. * @return current value of System Tick counter
  154. **********************************************************************/
  155. uint32_t SYSTICK_GetCurrentValue(void)
  156. {
  157. return (SysTick->VAL);
  158. }
  159. /*********************************************************************//**
  160. * @brief Clear Counter flag
  161. * @param[in] None
  162. * @return None
  163. **********************************************************************/
  164. void SYSTICK_ClearCounterFlag(void)
  165. {
  166. SysTick->CTRL &= ~ST_CTRL_COUNTFLAG;
  167. }
  168. /**
  169. * @}
  170. */
  171. #endif /*_SYSTICK*/
  172. /**
  173. * @}
  174. */
  175. /* --------------------------------- End Of File ------------------------------ */