/** * ************************************************************************ * * @file systick.c * @author GD32 * @brief 通过 SysTick 定时器进行微秒级别和毫秒级别的延时函数 * * ************************************************************************ * @copyright Copyright (c) 2024 GD32 * ************************************************************************ */ #include "gd32e23x.h" #include "systick.h" volatile static uint32_t delay_count = 0; /** * ************************************************************************ * @brief 配置 SysTick 定时器 * * * ************************************************************************ */ void systick_config(void) { //设置了 SysTick 定时器的时钟源为 HCLK systick_clksource_set(SYSTICK_CLKSOURCE_HCLK); // 配置SysTick为1ms周期中断 // 注意:SysTick_Config会自动设置时钟源为HCLK,所以需要使用SystemCoreClock/1000 SysTick_Config(SystemCoreClock / 1000U); // 1ms中断 NVIC_SetPriority(SysTick_IRQn, 0x00U); } /** * ************************************************************************ * @brief delay_ms 毫秒延时函数 * * @param[in] count 毫秒值 * * ************************************************************************ */ void delay_10us(uint32_t count) { // 基于系统时钟的简单循环延时 // 这是一个粗略的估计,实际延时可能有偏差 实测10.2us uint32_t loops_per_10us = SystemCoreClock / 1700000; // 粗略估计,每10微秒的循环次数 for(uint32_t i = 0; i < count; i++) { for(volatile uint32_t j = 0; j < loops_per_10us; j++); } } /** * ************************************************************************ * @brief delay_ms 毫秒延时函数 * * @param[in] count 毫秒值 * * ************************************************************************ */ void delay_ms(uint32_t count) { delay_count = count; // 设置延时计数 while (delay_count != 0U); } /** * ************************************************************************ * @brief 每个 SysTick 中断调用时,减少延时计数 * * @param[in] void * * ************************************************************************ */ void delay_decrement(void) { if (delay_count != 0U) { delay_count--; } } // /** // * ************************************************************************ // * @brief delay_ms_safe 毫秒延时函数(不干扰SysTick中断) // * @details 使用简单循环实现延时,不会重新配置SysTick // * @param[in] count 毫秒值 // * ************************************************************************ // */ // void delay_ms_safe(uint32_t count) // { // // 基于系统时钟的简单循环延时 // // 这是一个粗略的估计,实际延时可能有偏差 // uint32_t loops_per_ms = SystemCoreClock / 14000; // 粗略估计 // for(uint32_t i = 0; i < count; i++) { // for(volatile uint32_t j = 0; j < loops_per_ms; j++); // } // } // /** // * ************************************************************************ // * @brief delay_us_safe 微秒延时函数(不干扰SysTick中断) // * @details 使用简单循环实现延时,不会重新配置SysTick // * @param[in] count 微秒值 // * ************************************************************************ // */ // void delay_us_safe(uint32_t count) // { // // 基于系统时钟的简单循环延时 // // 这是一个粗略的估计,实际延时可能有偏差 // uint32_t loops_per_us = SystemCoreClock / 22000000; // 粗略估计,每微秒的循环次数 // for(uint32_t i = 0; i < count; i++) { // for(volatile uint32_t j = 0; j < loops_per_us; j++); // } // }