generated from hulk/gd32e23x_template_cmake_vscode
release dev branch
This commit is contained in:
167
Src/systick.c
167
Src/systick.c
@@ -1,83 +1,118 @@
|
||||
/*!
|
||||
\file systick.c
|
||||
\brief the systick configuration file
|
||||
|
||||
\version 2025-02-10, V2.4.0, demo for GD32E23x
|
||||
*/
|
||||
|
||||
/*
|
||||
Copyright (c) 2025, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* ************************************************************************
|
||||
*
|
||||
* @file systick.c
|
||||
* @author GD32
|
||||
* @brief 通过 SysTick 定时器进行微秒级别和毫秒级别的延时函数
|
||||
*
|
||||
* ************************************************************************
|
||||
* @copyright Copyright (c) 2024 GD32
|
||||
* ************************************************************************
|
||||
*/
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
|
||||
volatile static uint32_t delay;
|
||||
volatile static uint32_t delay_count = 0;
|
||||
|
||||
/*!
|
||||
\brief configure systick
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
/**
|
||||
* ************************************************************************
|
||||
* @brief 配置 SysTick 定时器
|
||||
*
|
||||
*
|
||||
* ************************************************************************
|
||||
*/
|
||||
void systick_config(void)
|
||||
{
|
||||
/* setup systick timer for 1000Hz interrupts */
|
||||
if (SysTick_Config(SystemCoreClock / 1000U)){
|
||||
/* capture error */
|
||||
while (1){
|
||||
}
|
||||
}
|
||||
/* configure the systick handler priority */
|
||||
//设置了 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 a time in milliseconds
|
||||
\param[in] count: count in milliseconds
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
/**
|
||||
* ************************************************************************
|
||||
* @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;
|
||||
|
||||
while(0U != delay){
|
||||
}
|
||||
delay_count = count; // 设置延时计数
|
||||
while (delay_count != 0U);
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief delay decrement
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
/**
|
||||
* ************************************************************************
|
||||
* @brief 每个 SysTick 中断调用时,减少延时计数
|
||||
*
|
||||
* @param[in] void
|
||||
*
|
||||
* ************************************************************************
|
||||
*/
|
||||
void delay_decrement(void)
|
||||
{
|
||||
if (0U != delay){
|
||||
delay--;
|
||||
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++);
|
||||
// }
|
||||
// }
|
Reference in New Issue
Block a user