| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- /*!
- \file systick.c
- \brief the systick configuration file
- \version 2019-02-19, V1.0.0, firmware for GD32E23x
- \version 2020-12-12, V1.1.0, firmware for GD32E23x
- */
- /*
- Copyright (c) 2020, GigaDevice Semiconductor Inc.
- Redistribution and use in source and binary forms, with or without modification,
- are permitted provided that the following conditions are met:
- 1. Redistributions of source code must retain the above copyright notice, this
- list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
- 3. Neither the name of the copyright holder nor the names of its contributors
- may be used to endorse or promote products derived from this software without
- specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
- OF SUCH DAMAGE.
- */
- #include "systick.h"
- #include "FreeRTOS.h"
- #include "gd32e23x.h"
- #include "task.h"
- volatile static uint32_t delay;
- static uint8_t fac_us = 0; // us延时倍乘数
- static uint16_t fac_ms = 0; // ms延时倍乘数,在ucos下,代表每个节拍的ms数
- /*!
- \brief configure systick
- \param[in] none
- \param[out] none
- \retval none
- */
- void systick_config(void)
- {
- // SysTick_Config(SystemCoreClock / configTICK_RATE_HZ);
- /* Stop and reset the SysTick. */
- SysTick->CTRL = 0UL;
- SysTick->VAL = 0UL;
- /* Configure SysTick to interrupt at the requested rate. */
- SysTick->LOAD = (configCPU_CLOCK_HZ / configTICK_RATE_HZ) - 1UL;
- SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
- /* configure the systick handler priority */
- NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);
- fac_us = SystemCoreClock / 1000000;
- fac_ms = 1000 / configTICK_RATE_HZ;
- //NVIC_EnableIRQ(SysTick_IRQn);
- nvic_irq_enable(SysTick_IRQn,0);
- //__set_PRIMASK(0);//开启总中断
- //__asm volatile ( " cpsie i " ::: "memory" );
- }
- /*!
- \brief delay a time in milliseconds
- \param[in] count: count in milliseconds
- \param[out] none
- \retval none
- */
- void delay_ms(uint32_t count, uint8_t sync)
- {
- if (sync == 1)
- {
- delay = count;
- while (0U != delay)
- {
- }
- }
- else
- {
- if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) // 系统已经运行
- {
- vTaskDelay(pdMS_TO_TICKS(count));
- }
- else
- {
- delay = count;
- while (0U != delay)
- {
- }
- }
- }
- }
- /*!
- \brief delay decrement
- \param[in] none
- \param[out] none
- \retval none
- */
- void delay_decrement(void)
- {
- if (0U != delay)
- {
- delay--;
- }
- }
- // 延时nus
- // nus:要延时的us数.
- // nus:0~204522252(最大值即2^32/fac_us@fac_us=168)
- void delay_us(uint32_t nus)
- {
- uint32_t ticks;
- uint32_t told, tnow, tcnt = 0;
- uint32_t reload = SysTick->LOAD; // LOAD的值
- ticks = nus * fac_us; // 需要的节拍数
- told = SysTick->VAL; // 刚进入时的计数器值
- while (1)
- {
- tnow = SysTick->VAL;
- if (tnow != told)
- {
- if (tnow < told)
- tcnt += told - tnow; // 这里注意一下SYSTICK是一个递减的计数器就可以了.
- else
- tcnt += reload - tnow + told;
- told = tnow;
- if (tcnt >= ticks)
- break; // 时间超过/等于要延迟的时间,则退出.
- }
- };
- }
- // 延时nms,不会引起任务调度
- // nms:要延时的ms数
- void delay_xms(uint32_t nms)
- {
- uint32_t i;
- for (i = 0; i < nms; i++)
- delay_us(1000);
- }
|