SysTick是CM内核独立的定时器,时钟可以用内核内部的,也可以用芯片厂家(ST)的时钟,参考《Cortex-M3权威指南》的第13章:
另外也可以考《STM32F10xxx Cortex-M3 programming manual.pdf》第4章CM3系统外设,从手册来看,ST应该只提供了内核外部时钟,而外部时钟又分成两种:HCLK/8 和 HCLK,
ST官方提供了库直接可以操作SYSTICK,但不同的版本使用起来有些区别:
在V2.0的版本中对SysTick的操作是使用的stm32f10x_systick.c和stm32f10x_systick.h
void SysTick_CLKSourceConfig(u32 SysTick_CLKSource);
void SysTick_SetReload(u32 Reload);
void SysTick_CounterCmd(u32 SysTick_Counter);
void SysTick_ITConfig(FunctionalState NewState);
u32 SysTick_GetCounter(void);
FlagStatus SysTick_GetFlagStatus(u8 SysTick_FLAG);
但是在V3.0中取消了stm32f10x_systick.c和stm32f10x_systick.h文件直接在core_cm3.h文件定义成内联函数
/* ################################## SysTick function ############################################ */
#if (!defined (__Vendor_SysTickConfig)) || (__Vendor_SysTickConfig == 0)
/**
* @brief Initialize and start the SysTick counter and its interrupt.
*
* @param ticks number of ticks between two interrupts
* @return 1 = failed, 0 = successful
*
* Initialise the system tick timer and its interrupt and start the
* system tick timer / counter in free running mode to generate
* periodical interrupts.
*/
static __INLINE uint32_t SysTick_Config(uint32_t ticks)
{
if (ticks > SysTick_LOAD_RELOAD_Msk) return (1); /* Reload value impossible */
SysTick->LOAD = (ticks & SysTick_LOAD_RELOAD_Msk) - 1; /* set reload register */
NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); /* set Priority for Cortex-M0 System Interrupts */
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 */
}
#endif
而且是默认使用的AHB总线的时钟HCLK
宗合上述如果使用V3版本SysTick的话可以做以下操作,在不知道AHB总线的时钟的频下也可以直接算出加载值:
void SysTickInit(u16 Frequency)
{
RCC_ClocksTypeDef RccClocks;
RCC_GetClocksFreq(&RccClocks);
SysTick_Config(RccClocks.HCLK_Frequency / Frequency);
}
//systick.c文件
#include "stm32f10x_rcc.h"
#include "systick.h"
u8 SysTickTimes[SYS_TICK_TIME_SIZE];
void SysTickInit(u16 Frequency)
{
u8 i;
RCC_ClocksTypeDef RccClocks;
for(i = 0; i < SYS_TICK_TIME_SIZE; i++)
SysTickTimes = 0;
RCC_GetClocksFreq(&RccClocks);
SysTick_Config(RccClocks.HCLK_Frequency / Frequency);
}
void SysTick_Handler(void)
{
u8 i;
for(i = 0; i < SYS_TICK_TIME_SIZE; i++)
if(SysTickTimes)
SysTickTimes--;
}
/**
******************************************************************************
* @file systick.h
* @author onsunsl
* @version V0.1
* @date 11-March-2013
* @brief define the zx240160m1a option cmd
******************************************************************************
*/
#ifndef __SYSTICK__H
#define __SYSTICK__H
void SysTickInit(u16 Frequency);
#define SYS_TICK_TIME_SIZE 10
#endif
/******************* (C) COPYRIGHT 2013 LCJCTEC *****END OF FILE***************/