// 通过直接计算校准RTC时钟(使用0.01ms周期的systick测量再修正RTC预分频系数)。 void RTC_Calibrate(void) { unsigned int systick_sta; unsigned int rtctick_lmt; unsigned int prescaler = 0; // 在使用systick测量RTC tick的周期长度之前,初始化systick NVIC_SetPriority(SysTick_IRQn, 0); SysTick_Init(); // 开始rtcalarm计数并使能rtcalarm中断 SysTick_Enable(); // 先设置默认的RTC预分频系数(rtc tike = ~1ms) RTC_SetPrescaler(DEF_RTC_PRESCALER); /* Enable RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); // 测量RTC单周期长度持续~1秒 rtctick_lmt = systick+1000/SYSTICK_PERIOD; // 记录测量前的systick值 systick_sta = systick; // 测量1000个周期的rtc tick(~1s) while(systick < rtctick_lmt); prescaler = (DEF_RTC_PRESCALER * 1000)/((systick-systick_sta)/100); // printf("prescaler = %d.\r\n", prescaler); // 重新设置RTC预分频系数 RTC_SetPrescaler(prescaler); /* Enable RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); // 校准完RTC时钟后,停止rtcalarm计数并禁止rtcalarm中断 SysTick_Disable(); // printf("RTC Clock calibrated.\r\n"); }
/* Sets up system hardware */ static void prvSetupHardware(void) { /* LED0 is used for the link status, on = PHY cable detected */ Board_Init(); /* Initial LED state is off to show an unconnected cable state */ Board_LED_Set(0, false); /* Setup a 1mS sysTick for the primary time base */ SysTick_Enable(1); }
// ************** SysTick_Init **************************** // - Initializes the Systick timer // ******************************************************** // Input: none // Output: none // ******************************************************** void SysTick_Init(){ // disable systick while it is being initialized SysTick_Disable(); // set period to maximum value SysTick_SetPeriod(0xFFFFFF); // reset counter value SysTick_Reset(); // set interrupt priority SysTick_SetInterruptPriority(); // enable systick SysTick_Enable(); }