systick.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 count_1us = 0;
  32. volatile static uint32_t count_1ms = 0;
  33. void systick_config(void)
  34. {
  35. systick_clksource_set(SYSTICK_CLKSOURCE_HCLK_DIV8);
  36. count_1us = (uint32_t)(SystemCoreClock/8000000);
  37. count_1ms = (uint32_t)count_1us*1000;
  38. }
  39. void delay_us(uint32_t us)
  40. {
  41. uint32_t ctl;
  42. /* reload the count value */
  43. SysTick->LOAD = (uint32_t)(us * count_1us);
  44. /* clear the current count value */
  45. SysTick->VAL = 0x0000U;
  46. /* enable the systick timer */
  47. SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
  48. /* wait for the COUNTFLAG flag set */
  49. do{
  50. ctl = SysTick->CTRL;
  51. }while((ctl&SysTick_CTRL_ENABLE_Msk)&&!(ctl & SysTick_CTRL_COUNTFLAG_Msk));
  52. /* disable the systick timer */
  53. SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  54. /* clear the current count value */
  55. SysTick->VAL = 0x0000U;
  56. }
  57. //void delay_ms(uint32_t ms)
  58. //{
  59. // uint32_t ctl;
  60. //
  61. // /* reload the count value */
  62. // SysTick->LOAD = (uint32_t)(ms * count_1ms);
  63. // /* clear the current count value */
  64. // SysTick->VAL = 0x0000U;
  65. // /* enable the systick timer */
  66. // SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
  67. // /* wait for the COUNTFLAG flag set */
  68. // do{
  69. // ctl = SysTick->CTRL;
  70. // }while((ctl&SysTick_CTRL_ENABLE_Msk)&&!(ctl & SysTick_CTRL_COUNTFLAG_Msk));
  71. // /* disable the systick timer */
  72. // SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  73. // /* clear the current count value */
  74. // SysTick->VAL = 0x0000U;
  75. //}
  76. static __IO unsigned int s_iTimDelayCnt = 0;
  77. static void TimDelayDec(void);
  78. static void TimDelayDec(void)
  79. {
  80. if(s_iTimDelayCnt != 0)
  81. {
  82. s_iTimDelayCnt--;
  83. }
  84. }
  85. void SysTick_Handler(void)
  86. {
  87. TimDelayDec();
  88. }
  89. void InitSysTick(void)
  90. {
  91. if (SysTick_Config(SystemCoreClock / 1000))
  92. {
  93. while(1)
  94. {
  95. }
  96. }
  97. }
  98. void DelayNms(__IO unsigned int nms)
  99. {
  100. s_iTimDelayCnt = nms;
  101. while(s_iTimDelayCnt != 0)
  102. {
  103. }
  104. }
  105. void DelayNus(__IO unsigned int nus)
  106. {
  107. unsigned int s_iTimCnt = nus;
  108. unsigned short i;
  109. while(s_iTimCnt != 0)
  110. {
  111. for(i = 0; i < 3; i++)
  112. {
  113. }
  114. s_iTimCnt--;
  115. }
  116. }