systick.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*!
  2. \file systick.c
  3. \brief the systick configuration file
  4. \version 2019-02-19, V1.0.0, firmware for GD32E23x
  5. \version 2020-12-12, V1.1.0, firmware for GD32E23x
  6. */
  7. /*
  8. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  9. Redistribution and use in source and binary forms, with or without modification,
  10. are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright notice, this
  12. list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holder nor the names of its contributors
  17. may be used to endorse or promote products derived from this software without
  18. specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. OF SUCH DAMAGE.
  29. */
  30. #include "systick.h"
  31. #include "FreeRTOS.h"
  32. #include "gd32e23x.h"
  33. #include "task.h"
  34. volatile static uint32_t delay;
  35. static uint8_t fac_us = 0; // us延时倍乘数
  36. static uint16_t fac_ms = 0; // ms延时倍乘数,在ucos下,代表每个节拍的ms数
  37. /*!
  38. \brief configure systick
  39. \param[in] none
  40. \param[out] none
  41. \retval none
  42. */
  43. void systick_config(void)
  44. {
  45. // SysTick_Config(SystemCoreClock / configTICK_RATE_HZ);
  46. /* Stop and reset the SysTick. */
  47. SysTick->CTRL = 0UL;
  48. SysTick->VAL = 0UL;
  49. /* Configure SysTick to interrupt at the requested rate. */
  50. SysTick->LOAD = (configCPU_CLOCK_HZ / configTICK_RATE_HZ) - 1UL;
  51. SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
  52. /* configure the systick handler priority */
  53. NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);
  54. fac_us = SystemCoreClock / 1000000;
  55. fac_ms = 1000 / configTICK_RATE_HZ;
  56. //NVIC_EnableIRQ(SysTick_IRQn);
  57. nvic_irq_enable(SysTick_IRQn,0);
  58. //__set_PRIMASK(0);//开启总中断
  59. //__asm volatile ( " cpsie i " ::: "memory" );
  60. }
  61. /*!
  62. \brief delay a time in milliseconds
  63. \param[in] count: count in milliseconds
  64. \param[out] none
  65. \retval none
  66. */
  67. void delay_ms(uint32_t count, uint8_t sync)
  68. {
  69. if (sync == 1)
  70. {
  71. delay = count;
  72. while (0U != delay)
  73. {
  74. }
  75. }
  76. else
  77. {
  78. if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) // 系统已经运行
  79. {
  80. vTaskDelay(pdMS_TO_TICKS(count));
  81. }
  82. else
  83. {
  84. delay = count;
  85. while (0U != delay)
  86. {
  87. }
  88. }
  89. }
  90. }
  91. /*!
  92. \brief delay decrement
  93. \param[in] none
  94. \param[out] none
  95. \retval none
  96. */
  97. void delay_decrement(void)
  98. {
  99. if (0U != delay)
  100. {
  101. delay--;
  102. }
  103. }
  104. // 延时nus
  105. // nus:要延时的us数.
  106. // nus:0~204522252(最大值即2^32/fac_us@fac_us=168)
  107. void delay_us(uint32_t nus)
  108. {
  109. uint32_t ticks;
  110. uint32_t told, tnow, tcnt = 0;
  111. uint32_t reload = SysTick->LOAD; // LOAD的值
  112. ticks = nus * fac_us; // 需要的节拍数
  113. told = SysTick->VAL; // 刚进入时的计数器值
  114. while (1)
  115. {
  116. tnow = SysTick->VAL;
  117. if (tnow != told)
  118. {
  119. if (tnow < told)
  120. tcnt += told - tnow; // 这里注意一下SYSTICK是一个递减的计数器就可以了.
  121. else
  122. tcnt += reload - tnow + told;
  123. told = tnow;
  124. if (tcnt >= ticks)
  125. break; // 时间超过/等于要延迟的时间,则退出.
  126. }
  127. };
  128. }
  129. // 延时nms,不会引起任务调度
  130. // nms:要延时的ms数
  131. void delay_xms(uint32_t nms)
  132. {
  133. uint32_t i;
  134. for (i = 0; i < nms; i++)
  135. delay_us(1000);
  136. }