stm32_ota/SYSTEM/SYSTICK/systick.c

170 lines
5.1 KiB
C
Raw Normal View History

2023-12-15 06:28:06 +00:00
#include "systick.h"
#include "sys.h"
#include "sysport.h"
#include "watchdog.h"
/*********FreeRTOS<4F><53><EFBFBD>ⲿ<EFBFBD><E2B2BF><EFBFBD><EFBFBD>******** */
#include "FreeRTOS.h" //FreeRTOSʹ<53><CAB9>
#include "task.h"
/*****************************************/
/********************************************************************************
* @file systick.c
* @author <EFBFBD>̳ϿƼ<EFBFBD> Mr.Wang
* @version V1.0.0
* @date 11-Dec-2018
* @brief STM32ϵͳ<EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪOS<EFBFBD>ʱ<EFBFBD>ӻ<EFBFBD>׼
******************************************************************************
* ˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳ<EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>10ms<EFBFBD>ж<EFBFBD>һ<EFBFBD>Σ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱС<EFBFBD><EFBFBD>10msʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>FreeRTOS<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*******************************************************************************/
/*****************************************
*<EFBFBD><EFBFBD><EFBFBD>ⲿ<EFBFBD><EFBFBD><EFBFBD>õĺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>򳣱<EFBFBD><EFBFBD><EFBFBD>
****************************************/
/*****************************************
*<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD>ʹ<EFBFBD>õij<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
****************************************/
static u8 facUs=0; //us<75><73>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
static u16 facMs=0; //ms<6D><73>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><>FreeRTOS<4F><53>,<2C><><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ms<6D><73>
/**************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD> void SysTick_Handler(void)
* <EFBFBD>ⲿ<EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD> xPortSysTickHandler
* <EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD> ϵͳ<EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>жϴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* ˵ <EFBFBD><EFBFBD><EFBFBD><EFBFBD> SysTick_Init<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ1/configTICK_RATE_HZ<EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>һ<EFBFBD><EFBFBD> FreeRTOS<EFBFBD><EFBFBD>configTICK_RATE_HZ = 100<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>SysTick 10ms<EFBFBD>ж<EFBFBD>һ<EFBFBD><EFBFBD>
*************************************************************************************************/
void SysTick_Handler(void)
{
static uint32_t counter = 0 ;
if( counter % 50 == 0 ) //500ms<6D><73>תһ<D7AA><D2BB>LED<45><44>ƽ
Data_Led_Reverse() ;
}
/**************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD> void SysTick_Init(void)
* <EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD> <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ϵͳ<EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>
* ˵ <EFBFBD><EFBFBD><EFBFBD><EFBFBD> SYSTICK<EFBFBD><EFBFBD>ʱ<EFBFBD>ӹ̶<EFBFBD>ΪAHBʱ<EFBFBD>ӣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SYSTICKʱ<EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD>ΪAHB/8
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>˼<EFBFBD><EFBFBD><EFBFBD>FreeRTOS<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD>SYSTICK<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>Ƶ<EFBFBD>ʸ<EFBFBD>ΪAHB<EFBFBD><EFBFBD>Ƶ<EFBFBD>ʣ<EFBFBD>
* SYSCLK:ϵͳʱ<EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD>
* ÿ1/configTICK_RATE_HZ<EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>һ<EFBFBD><EFBFBD> FreeRTOS<EFBFBD><EFBFBD>configTICK_RATE_HZ = 100<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>SysTick 10ms<EFBFBD>ж<EFBFBD>һ<EFBFBD><EFBFBD>
*************************************************************************************************/
void SysTick_Init(void)
{
u32 reload;
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); //ѡ<><D1A1><EFBFBD>ⲿʱ<E2B2BF><CAB1> HCLK
facUs = SystemCoreClock/1000000; //<2F><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>ʹ<EFBFBD><CAB9>OS,fac_us<75><73><EFBFBD><EFBFBD>Ҫʹ<D2AA><CAB9>,1us<75><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ۼӵĴ<D3B5><C4B4><EFBFBD>
reload = SystemCoreClock/1000000; //ÿ<><C3BF><EFBFBD>ӵļ<D3B5><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><>λΪM
reload *= 1000000/TICK_RATE_HZ; //<2F><><EFBFBD><EFBFBD>TICK_RATE_HZ<48><EFBFBD><E8B6A8><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
//reloadΪ24λ<34>Ĵ<EFBFBD><C4B4><EFBFBD>,<2C><><EFBFBD><EFBFBD>ֵ:16777216,<2C><>72M<32><4D><><D4BC>0.233s<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
facMs = 1000/TICK_RATE_HZ; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ٵ<EFBFBD>λ
//SCB->SHP[11]=2;//<2F><><EFBFBD><EFBFBD>SYSTICK<43><4B><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>Ϊ1<CEAA><31>ע<EFBFBD><D7A2>SYSTICK<43><4B><EFBFBD><EFBFBD>ϵͳ<CFB5><EFBFBD><ECB3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD>SCB<43><42><EFBFBD><EFBFBD><EFBFBD>á<EFBFBD>
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; //<2F><><EFBFBD><EFBFBD>SYSTICK<43>ж<EFBFBD>
SysTick->LOAD = reload; //ÿ1/TICK_RATE_HZ<48><5A><EFBFBD>ж<EFBFBD>һ<EFBFBD><D2BB>
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; //<2F><><EFBFBD><EFBFBD>SYSTICK
}
/**************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD> void Delay_Us(u32 nus)
* <EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD> <EFBFBD><EFBFBD>ʱnus
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param nus Ҫ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>us<EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD>ϵͳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* ˵ <EFBFBD><EFBFBD><EFBFBD><EFBFBD> nus:0~59652323(<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>2^32/fac_us@fac_us=72) 59652323
*************************************************************************************************/
void Delay_Us(u32 nus)
{
if( nus > 59652323 )
{
nus = 59652323 ;
SysErr("") ;
}
u32 ticks;
u32 told, tnow, tcnt = 0 ;
u32 reload = SysTick->LOAD; //LOAD<41><44>ֵ
ticks = nus*facUs; //<2F><>Ҫ<EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD>
told = SysTick->VAL; //<2F>ս<EFBFBD><D5BD><EFBFBD>ʱ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ֵ
while(1)
{
tnow = SysTick->VAL;
if( tnow!=told )
{
if( tnow<told )
tcnt+=told-tnow ; //<2F><><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>һ<EFBFBD><D2BB>SYSTICK<43><4B>һ<EFBFBD><D2BB><EFBFBD>ݼ<EFBFBD><DDBC>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϳ<EFBFBD><CDBF><EFBFBD><EFBFBD><EFBFBD>.
else
tcnt+=reload-tnow+told ;
told = tnow;
if( tcnt>=ticks )
break; //ʱ<><EFBFBD><E4B3AC>/<2F><><EFBFBD><EFBFBD>Ҫ<EFBFBD>ӳٵ<D3B3>ʱ<EFBFBD><CAB1>,<2C><><EFBFBD>˳<EFBFBD>.
}
}
}
/**************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD> void Delay_Ms(u32 nms)
* <EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD> <EFBFBD><EFBFBD>ʱnms
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param nms Ҫ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ms<EFBFBD><EFBFBD>
*************************************************************************************************/
void Delay_Ms(u32 nms)
{
for(u32 i =0; i< nms; i++)
{
Delay_Us(1000) ; //ϵͳû<CDB3><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD>us<75><73>ʱ<EFBFBD><CAB1>ϵͳ<CFB5><CDB3>ʼ<EFBFBD><CABC><EFBFBD>к<EFBFBD><D0BA><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>ʽ<EFBFBD><CABD>ʱ
Watchdog_Feed() ; //<2F><><EFBFBD>Ź<EFBFBD>ι<EFBFBD><CEB9>
}
}
/**************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD> void Delay_Ms_StopScheduler(u32 nms)
* <EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD> <EFBFBD><EFBFBD>ʱnms nms<EFBFBD><EFBFBD>ΧΪ0~59652
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param nms Ҫ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ms<EFBFBD><EFBFBD>
* ˵ <EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD>ʱnms,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*************************************************************************************************/
void Delay_Ms_StopScheduler(u32 nms) //<2F>ж<EFBFBD>ϵͳ<CFB5><CDB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
u32 i ;
for(i=0; i<nms; i++)
Delay_Us(1000) ;
}