systick.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*!
  2. \file systick.c
  3. \brief the systick configuration file
  4. \version 2024-02-22, V2.1.0, firmware for GD32E23x
  5. */
  6. /*
  7. Copyright (c) 2024, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #include "gd32e23x.h"
  30. #include "systick.h"
  31. //volatile static uint32_t delay;
  32. ///*!
  33. // \brief configure systick
  34. // \param[in] none
  35. // \param[out] none
  36. // \retval none
  37. //*/
  38. //void systick_config(void)
  39. //{
  40. // /* setup systick timer for 1000Hz interrupts */
  41. //
  42. // if(SysTick_Config(SystemCoreClock / 1000U)){
  43. // /* capture error */
  44. // while(1){
  45. // }
  46. // }
  47. // /* configure the systick handler priority */
  48. // NVIC_SetPriority(SysTick_IRQn, 0x00U);
  49. //}
  50. ///*!
  51. // \brief delay a time in milliseconds
  52. // \param[in] count: count in milliseconds
  53. // \param[out] none
  54. // \retval none
  55. //*/
  56. //void delay_1ms(uint32_t count)
  57. //{
  58. // delay = count;
  59. // while(0U != delay){
  60. // }
  61. //}
  62. ///*!
  63. // \brief delay decrement
  64. // \param[in] none
  65. // \param[out] none
  66. // \retval none
  67. //*/
  68. //void delay_decrement(void)
  69. //{
  70. // if(0U != delay){
  71. // delay--;
  72. // }
  73. //}
  74. //volatile static uint32_t count_1us = 0;
  75. //volatile static uint32_t count_1ms = 0;
  76. //void systick_config(void)
  77. //{
  78. // systick_clksource_set(SYSTICK_CLKSOURCE_HCLK_DIV8);
  79. // nvic_irq_enable(SysTick_IRQn,10);
  80. // count_1us = (uint32_t)(SystemCoreClock/8000000);
  81. // count_1ms = (uint32_t)count_1us*1000;
  82. //}
  83. //void delay_us(uint32_t us)
  84. //{
  85. // uint32_t ctl;
  86. //
  87. // /* reload the count value */
  88. // SysTick->LOAD = (uint32_t)(us * count_1us);
  89. // /* clear the current count value */
  90. // SysTick->VAL = 0x0000U;
  91. // /* enable the systick timer */
  92. // SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
  93. // /* wait for the COUNTFLAG flag set */
  94. // do{
  95. // ctl = SysTick->CTRL;
  96. // }while((ctl&SysTick_CTRL_ENABLE_Msk)&&!(ctl & SysTick_CTRL_COUNTFLAG_Msk));
  97. // /* disable the systick timer */
  98. // SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  99. // /* clear the current count value */
  100. // SysTick->VAL = 0x0000U;
  101. //}
  102. //void delay_ms(uint32_t ms)
  103. //{
  104. // uint32_t ctl;
  105. //
  106. // /* reload the count value */
  107. // SysTick->LOAD = (uint32_t)(ms * count_1ms);
  108. // /* clear the current count value */
  109. // SysTick->VAL = 0x0000U;
  110. // /* enable the systick timer */
  111. // SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
  112. // /* wait for the COUNTFLAG flag set */
  113. // do{
  114. // ctl = SysTick->CTRL;
  115. // }while((ctl&SysTick_CTRL_ENABLE_Msk)&&!(ctl & SysTick_CTRL_COUNTFLAG_Msk));
  116. // /* disable the systick timer */
  117. // SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  118. // /* clear the current count value */
  119. // SysTick->VAL = 0x0000U;
  120. //}
  121. static __IO unsigned int s_iTimDelayCnt = 0;
  122. static void TimDelayDec(void); //延时计数
  123. static void TimDelayDec(void)
  124. {
  125. if(s_iTimDelayCnt != 0) //延时计数器的数值不为0
  126. {
  127. s_iTimDelayCnt--; //延时计数器的数值减1
  128. }
  129. }
  130. void systick_config( void )
  131. {
  132. if (SysTick_Config(SystemCoreClock / 1000)) //配置系统滴答定时器1ms中断一次
  133. {
  134. while(1) //错误发生的情况下,进入死循环
  135. {
  136. }
  137. }
  138. }
  139. void SysTick_Handler(void)
  140. {
  141. TimDelayDec(); //延时计数函数
  142. }
  143. void delay_ms(uint32_t ms)
  144. {
  145. s_iTimDelayCnt = ms; //将延时计数器s_iTimDelayCnt的数值赋为nms
  146. while(s_iTimDelayCnt != 0) //延时计数器的数值为0时,表示延时了nms,跳出while语句
  147. {
  148. }
  149. }
  150. void delay_ns(__IO unsigned int nus)
  151. {
  152. unsigned int s_iTimCnt = nus; //定义一个变量s_iTimCnt作为延时计数器,赋值为nus
  153. unsigned short i; //定义一个变量作为循环计数器
  154. while(s_iTimCnt != 0) //延时计数器s_iTimCnt的值不为0
  155. {
  156. for(i = 0; i < 3; i++) //空循环,产生延时功能
  157. {
  158. }
  159. s_iTimCnt--; //成功延时1us,变量s_iTimCnt减1
  160. }
  161. }