之前没有在意,在demo程序的开始都有这样一个函数
systick_config();
以为是设置系统时钟啥的。
今天看了下,做下标记
这个函数对整个程序的作用
1:提供一个函数函数(接口);
2:在有RTOS系统的程序中用做调度的最小周期;
(如果还有其他啥作用我没说到请拍砖上来。。。)
对应这个systick.c有几点说的
1:volatile static uint32_t delay; 这个定义很经典,具体说是volatile很经典;
2: /* setup systick timer for 1000Hz interrupts */ 这句注释很经典,这里这个if (SysTick_Config(SystemCoreClock / 1000U))
语句设置了tick的周期或者说频率,你想这个sys tick系统定时器的中断频率是1000Hz(也就是没秒中断1000次),那么你需要设置什么样的值给对应的sys tick寄存器呢,小伙子,只用用系统时钟乘以1/1000就可以了。系统时钟那是相当快的,这里比如说是8M的时钟,就是说1s可以震荡8百万次,你想做个定时器1000次震荡一次,那么我用8百万除以1000,等于8000,你系统时钟震荡到8000次的时候,我这里会产生一个中断,有这个中断就是说嘛现在够1/1000秒了,也是1ms(我怎么感觉我在说废话。。。)还有3往下看
#include "gd32e10x.h"
#include "systick.h"
volatile static uint32_t delay;
/*!
\brief configure systick
\param[in] none
\param[out] none
\retval none
*/
void systick_config(void)
{
/* setup systick timer for 1000Hz interrupts */
if (SysTick_Config(SystemCoreClock / 1000U)){
/* capture error */
while (1){
}
}
/* configure the systick handler priority */
NVIC_SetPriority(SysTick_IRQn, 0x00U);
}
/*!
\brief delay a time in milliseconds
\param[in] count: count in milliseconds
\param[out] none
\retval none
*/
void delay_1ms(uint32_t count)
{
delay = count;
while(0U != delay){
}
}
/*!
\brief delay decrement
\param[in] none
\param[out] none
\retval none
*/
void delay_decrement(void)
{
if (0U != delay){
delay--;
}
}
3:其实下面的注释/* set reload register */也说了这是个reload寄存器,什么意思呢,设置这个数值,超过这个数值就要做下某些动作,做什么动作呢?产生中断和数值重置。
__STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)
{
if ((ticks - 1) > SysTick_LOAD_RELOAD_Msk) return (1); /* Reload value impossible */
SysTick->LOAD = ticks - 1; /* set reload register */
NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); /* set Priority for Systick Interrupt */
SysTick->VAL = 0; /* Load the SysTick Counter Value */
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_TICKINT_Msk |
SysTick_CTRL_ENABLE_Msk; /* Enable SysTick IRQ and SysTick Timer */
return (0); /* Function successful */
}
另外就是你找Systick_BASE的地址(基地址在0xE000E000),在GD32 的SPEC中你找不到,为什么呢?
/* Memory mapping of Cortex-M4 Hardware */
#define SCS_BASE (0xE000E000UL) /*!< System Control Space Base Address */
#define ITM_BASE (0xE0000000UL) /*!< ITM Base Address */
#define DWT_BASE (0xE0001000UL) /*!< DWT Base Address */
#define TPI_BASE (0xE0040000UL) /*!< TPI Base Address */
#define CoreDebug_BASE (0xE000EDF0UL) /*!< Core Debug Base Address */
#define SysTick_BASE (SCS_BASE + 0x0010UL) /*!< SysTick Base Address */
#define NVIC_BASE (SCS_BASE + 0x0100UL) /*!< NVIC Base Address */
#define SCB_BASE (SCS_BASE + 0x0D00UL) /*!< System Control Block Base Address */
#define SCnSCB ((SCnSCB_Type *) SCS_BASE ) /*!< System control Register not in SCB */
#define SCB ((SCB_Type *) SCB_BASE ) /*!< SCB configuration struct */
#define SysTick ((SysTick_Type *) SysTick_BASE ) /*!< SysTick configuration struct */
这是个Cortex-M4的内核相关的寄存器
下面的我找到一份文档中对这个寄存器的官方说法叫。。。。内核外设寄存器。。。(这都内核集成了,你叫内核外设寄存器???what's your problem?)
总之这个Systick系统定时器实在我感觉没什么太大用途。。。哈哈