Files
GD32E50X_BOOT/GD32E50X_BOOT/sys/systick.cpp
T
cbb 747fc56886 feat(boot): 添加SPI OTA功能并更新配置
- 添加SpiOta.h和SpiOta.cpp文件以实现SPI OTA功能
- 在项目配置中添加SPI_I2S外设组件支持
- 设置外部晶振值为8MHz (HXTAL_VALUE=8000000U)
- 调整ROM0大小从512KB减小到2KB以适应引导程序需求
- 移除评估板相关文件和看门狗驱动文件以优化存储空间
2026-05-25 01:33:00 +08:00

165 lines
3.6 KiB
C++

#include "systick.h"
#include "gd32e50x.h"
/**
* SysTick 驱动模块
* -----------------
* 提供 Bootloader 中最基础的延时与软复位能力:
* - SysTickTimer::init(): 配置 1ms 系统节拍
* - SysTickTimer::delayUs()/delayMs()/delaySeconds(): 简单阻塞延时
* - SysTickTimer::softReset(): 触发 MCU 软复位
*/
volatile uint32_t SysTickTimer::s_delayCountdown = 0U;
/**
* @brief 配置 SysTick 为 1ms 周期中断。
*
* @return 无。
*/
void SysTickTimer::init()
{
/** 将 SysTick 配置为 1ms 一次中断。 */
if (SysTick_Config(SystemCoreClock / 1000U)) {
/** 配置失败通常意味着时钟参数异常,直接停机等待。 */
while (1) {
}
}
/** 设为最高优先级,保证延时基准尽量稳定。 */
NVIC_SetPriority(SysTick_IRQn, 0x00U);
}
/**
* @brief 执行微秒级粗略阻塞延时。
*
* @param count 延时微秒数。
* @return 无。
*/
void SysTickTimer::delayUs(uint32_t count)
{
/**
* 这里使用空指令忙等实现粗略微秒延时。
* 由于循环/指令开销存在,精度不会像硬件定时器那样严格。
*/
uint32_t cycles = (SystemCoreClock / 4000000UL) * count;
for (uint32_t i = 0; i < cycles; ++i) {
__NOP();
}
}
/**
* @brief 执行毫秒级阻塞延时。
*
* @param count 延时毫秒数。
* @return 无。
*/
void SysTickTimer::delayMs(uint32_t count)
{
/** 设置倒计时,由 SysTick_Handler 每毫秒递减。 */
s_delayCountdown = count;
while (s_delayCountdown != 0U) {
}
}
/**
* @brief 执行秒级阻塞延时。
*
* @param count 延时秒数。
* @return 无。
*/
void SysTickTimer::delaySeconds(uint32_t count)
{
/** 基于 1ms 延时拼出秒级延时。 */
while (count--) {
delayMs(1000U);
}
}
/**
* @brief 递减毫秒倒计时。
*
* @return 无。
*/
void SysTickTimer::decrement()
{
/** 供 SysTick 中断调用,防止无符号下溢。 */
if (s_delayCountdown != 0U) {
s_delayCountdown--;
}
}
/**
* @brief SysTick 中断实际处理逻辑。
*
* @return 无。
*/
void SysTickTimer::onInterrupt()
{
/** 每次滴答中断只做极简工作,降低中断负担。 */
decrement();
}
/**
* @brief 触发 MCU 软件复位。
*
* @return 无。
*/
void SysTickTimer::softReset()
{
/** 通过 CMSIS 接口触发系统软复位。 */
NVIC_SystemReset();
/** 理论上不会执行到这里,保留死循环作为保险。 */
while (1) {
}
}
/**
* @brief Configure SysTick via the timer helper.
* @return none
*/
void systick_config(void) { SysTickTimer::init(); }
/**
* @brief Delay for microseconds via the timer helper.
* @param count Delay time in microseconds.
* @return none
*/
void delay_1us(uint32_t count) { SysTickTimer::delayUs(count); }
/**
* @brief Delay for milliseconds via the timer helper.
* @param count Delay time in milliseconds.
* @return none
*/
void delay_1ms(uint32_t count) { SysTickTimer::delayMs(count); }
/**
* @brief Delay for seconds via the timer helper.
* @param count Delay time in seconds.
* @return none
*/
void delay_1s(uint32_t count) { SysTickTimer::delaySeconds(count); }
/**
* @brief Decrement the global millisecond delay counter.
* @return none
*/
void delay_decrement(void) { SysTickTimer::decrement(); }
/**
* @brief Trigger a software reset via the timer helper.
* @return none
*/
void system_soft_reset(void) { SysTickTimer::softReset(); }
/**
* @brief SysTick interrupt service routine.
* @return none
*/
extern "C" void SysTick_Handler(void)
{
SysTickTimer::onInterrupt();
}