You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
4.3 KiB
136 lines
4.3 KiB
#include "FreeRTOS.h"
|
|
#include "task.h"
|
|
#include "main.h"
|
|
#include "cmsis_os.h"
|
|
#include "tim.h"
|
|
|
|
extern void delay_us(uint16_t nus);
|
|
|
|
// HAL_RCC_GetSysClockFreq(void)
|
|
|
|
typedef enum{
|
|
DELAY_DEFAULT,
|
|
DELAY_SYSTICK,
|
|
DELAY_SYSTICK_COMPLEX,
|
|
DELAY_TIMER,
|
|
|
|
}DELAY_US_TypeDef;
|
|
// SystemCoreClock
|
|
|
|
TIM_HandleTypeDef *Delay_TIM_hanle = NULL;
|
|
|
|
#define DELAY_US_TYPE DELAY_DEFAULT
|
|
|
|
|
|
void delay_us(uint16_t nus)
|
|
{
|
|
#if DELAY_US_TYPE == DELAY_DEFAULT
|
|
uint32_t cnt = nus * (SystemCoreClock/8000000);
|
|
while(cnt--);
|
|
#endif
|
|
|
|
#if DELAY_US_TYPE == DELAY_TIMER
|
|
|
|
__HAL_TIM_SET_COUNTER(&htim10, 0); // set the counter value a 0
|
|
__HAL_TIM_ENABLE(&htim10); // start the counter
|
|
// wait for the counter to reach the us input in the parameter
|
|
while (__HAL_TIM_GET_COUNTER(&htim10) < nus);
|
|
__HAL_TIM_DISABLE(&htim10); // stop the counter
|
|
|
|
#endif
|
|
|
|
#if DELAY_US_TYPE == DELAY_SYSTICK
|
|
SysTick->LOAD = (nus * (SystemCoreClock/8000000))-1;
|
|
SysTick->VAL = 0;
|
|
SysTick->CTRL = 0;
|
|
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
|
|
while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk));
|
|
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
|
|
|
|
#endif
|
|
|
|
#if DELAY_US_TYPE == DELAY_SYSTICK_COMPLEX
|
|
|
|
// 暂停系统,实现微秒延时
|
|
// 保存sysTick的状态
|
|
uint32_t val_temp=SysTick->VAL;
|
|
uint32_t load_temp=SysTick->LOAD;
|
|
uint16_t ctrl_temp=SysTick->CTRL;
|
|
|
|
/*SysTick状态重新设置,用于延时(系统暂停)*/
|
|
// SysTick->LOAD = 9 * us; // 设置定时器重装值,72MHz 8分频后9HMz,即一微秒记9个数
|
|
SysTick->LOAD = (nus * HAL_RCC_GetSysClockFreq()/1000000) - 1; // 设置定时器重装值,168MHz 8分频后21HMz,即一微秒记21个数
|
|
SysTick->VAL = 0x00; // 清空当前计数值,清空后会自动装入重载值
|
|
SysTick->CTRL = 0x00000001; // 位2设置为0,8分频,启动定时器
|
|
while (!(SysTick->CTRL & 0x00010000));// 等待计数到0
|
|
|
|
/*延时结束后恢复之前的状态,系统继续运行*/
|
|
SysTick->LOAD = load_temp;
|
|
SysTick->VAL = val_temp;
|
|
SysTick->CTRL = ctrl_temp;
|
|
//SysTick->CTRL = 0x00000000; // 关闭定时器
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
// #if DELAY_US_TYPE == DELAY_TIMER
|
|
// void delay_us(uint16_t nus)
|
|
// {
|
|
// __HAL_TIM_SET_COUNTER(&htim10, 0); // set the counter value a 0
|
|
// __HAL_TIM_ENABLE(&htim10); // start the counter
|
|
// // wait for the counter to reach the us input in the parameter
|
|
// while (__HAL_TIM_GET_COUNTER(&htim10) < nus);
|
|
// __HAL_TIM_DISABLE(&htim10); // stop the counter
|
|
// }
|
|
// #endif
|
|
|
|
// #if DELAY_US_TYPE == DELAY_SYSTICK_COMPLEX
|
|
// void delay_us(uint16_t nus)
|
|
// {
|
|
// // // 暂停系统,实现微秒延时
|
|
// // // 保存sysTick的状态
|
|
// // uint32_t val_temp=SysTick->VAL;
|
|
// // uint32_t load_temp=SysTick->LOAD;
|
|
// // uint16_t ctrl_temp=SysTick->CTRL;
|
|
|
|
// // /*SysTick状态重新设置,用于延时(系统暂停)*/
|
|
// // SysTick->LOAD = 9 * us; // 设置定时器重装值,72MHz 8分频后9HMz,即一微秒记9个数
|
|
// // SysTick->VAL = 0x00; // 清空当前计数值,清空后会自动装入重载值
|
|
// // SysTick->CTRL = 0x00000001; // 位2设置为0,8分频,启动定时器
|
|
// // while (!(SysTick->CTRL & 0x00010000));// 等待计数到0
|
|
|
|
// // /*延时结束后恢复之前的状态,系统继续运行*/
|
|
// // SysTick->LOAD = load_temp;
|
|
// // SysTick->VAL = val_temp;
|
|
// // SysTick->CTRL = ctrl_temp;
|
|
// // //SysTick->CTRL = 0x00000000; // 关闭定时器
|
|
// }
|
|
|
|
|
|
// #endif
|
|
|
|
// #if DELAY_US_TYPE == DELAY_TIMER
|
|
// void delay_us(uint16_t nus)
|
|
// {
|
|
// // // 暂停系统,实现微秒延时
|
|
// // // 保存sysTick的状态
|
|
// // uint32_t val_temp=SysTick->VAL;
|
|
// // uint32_t load_temp=SysTick->LOAD;
|
|
// // uint16_t ctrl_temp=SysTick->CTRL;
|
|
|
|
// // /*SysTick状态重新设置,用于延时(系统暂停)*/
|
|
// // SysTick->LOAD = 9 * us; // 设置定时器重装值,72MHz 8分频后9HMz,即一微秒记9个数
|
|
// // SysTick->VAL = 0x00; // 清空当前计数值,清空后会自动装入重载值
|
|
// // SysTick->CTRL = 0x00000001; // 位2设置为0,8分频,启动定时器
|
|
// // while (!(SysTick->CTRL & 0x00010000));// 等待计数到0
|
|
|
|
// // /*延时结束后恢复之前的状态,系统继续运行*/
|
|
// // SysTick->LOAD = load_temp;
|
|
// // SysTick->VAL = val_temp;
|
|
// // SysTick->CTRL = ctrl_temp;
|
|
// // //SysTick->CTRL = 0x00000000; // 关闭定时器
|
|
// }
|
|
|
|
|
|
// #endif
|