mirror of
https://github.com/goldenfishs/MRobot.git
synced 2025-05-20 00:50:55 +08:00
上传us延时
This commit is contained in:
parent
9f964e1532
commit
ced464290e
114
User/bsp/delay.c
114
User/bsp/delay.c
@ -1,34 +1,118 @@
|
|||||||
/* Includes ----------------------------------------------------------------- */
|
#include "bsp_delay.h"
|
||||||
#include "bsp\delay.h"
|
|
||||||
|
|
||||||
#include <cmsis_os2.h>
|
#include "cmsis_os.h"
|
||||||
#include <main.h>
|
#include "main.h"
|
||||||
|
|
||||||
/* Private define ----------------------------------------------------------- */
|
/* Private define ----------------------------------------------------------- */
|
||||||
/* Private macro ------------------------------------------------------------ */
|
/* Private macro ------------------------------------------------------------ */
|
||||||
/* Private typedef ---------------------------------------------------------- */
|
/* Private typedef ---------------------------------------------------------- */
|
||||||
/* Private variables -------------------------------------------------------- */
|
/* Private variables -------------------------------------------------------- */
|
||||||
|
static uint8_t fac_us = 0;
|
||||||
|
static uint32_t fac_ms = 0;
|
||||||
/* Private function -------------------------------------------------------- */
|
/* Private function -------------------------------------------------------- */
|
||||||
|
static void delay_ticks(uint32_t ticks)
|
||||||
|
{
|
||||||
|
uint32_t told = SysTick->VAL;
|
||||||
|
uint32_t tnow = 0;
|
||||||
|
uint32_t tcnt = 0;
|
||||||
|
uint32_t reload = SysTick->LOAD;
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
tnow = SysTick->VAL;
|
||||||
|
if (tnow != told)
|
||||||
|
{
|
||||||
|
if (tnow < told)
|
||||||
|
{
|
||||||
|
tcnt += told - tnow;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tcnt += reload - tnow + told;
|
||||||
|
}
|
||||||
|
told = tnow;
|
||||||
|
if (tcnt >= ticks)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
/* Exported functions ------------------------------------------------------- */
|
/* Exported functions ------------------------------------------------------- */
|
||||||
int8_t BSP_Delay(uint32_t ms) {
|
|
||||||
uint32_t tick_period = 1000u / osKernelGetTickFreq();
|
|
||||||
uint32_t ticks = ms / tick_period;
|
|
||||||
|
|
||||||
switch (osKernelGetState()) {
|
/**
|
||||||
|
* @brief 毫秒延时函数
|
||||||
|
* @param ms
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
int8_t BSP_Delay(uint32_t ms)
|
||||||
|
{
|
||||||
|
uint32_t tick_period = 1000u / osKernelGetTickFreq();
|
||||||
|
uint32_t ticks = ms / tick_period;
|
||||||
|
|
||||||
|
switch (osKernelGetState())
|
||||||
|
{
|
||||||
case osKernelError:
|
case osKernelError:
|
||||||
case osKernelReserved:
|
case osKernelReserved:
|
||||||
case osKernelLocked:
|
case osKernelLocked:
|
||||||
case osKernelSuspended:
|
case osKernelSuspended:
|
||||||
return BSP_ERR;
|
return BSP_ERR;
|
||||||
|
|
||||||
case osKernelRunning:
|
case osKernelRunning:
|
||||||
osDelay(ticks ? ticks : 1);
|
osDelay(ticks ? ticks : 1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case osKernelInactive:
|
case osKernelInactive:
|
||||||
case osKernelReady:
|
case osKernelReady:
|
||||||
HAL_Delay(ms);
|
HAL_Delay(ms);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return BSP_OK;
|
return BSP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 延时初始化
|
||||||
|
* @param
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
int8_t BSP_Delay_Init(void)
|
||||||
|
{
|
||||||
|
if (SystemCoreClock == 0)
|
||||||
|
{
|
||||||
|
return BSP_ERR;
|
||||||
|
}
|
||||||
|
fac_us = SystemCoreClock / 1000000;
|
||||||
|
fac_ms = SystemCoreClock / 1000;
|
||||||
|
return BSP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 微秒延时,注意:此函数会阻塞当前线程,直到延时结束,并且会被中断打断
|
||||||
|
* @param us
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
int8_t BSP_Delay_us(uint32_t us)
|
||||||
|
{
|
||||||
|
if (fac_us == 0)
|
||||||
|
{
|
||||||
|
return BSP_ERR;
|
||||||
|
}
|
||||||
|
uint32_t ticks = us * fac_us;
|
||||||
|
delay_ticks(ticks);
|
||||||
|
return BSP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 毫秒延时,注意:此函数会阻塞当前线程,直到延时结束,并且会被中断打断
|
||||||
|
* @param ms
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
int8_t BSP_Delay_ms(uint32_t ms)
|
||||||
|
{
|
||||||
|
if (fac_ms == 0)
|
||||||
|
{
|
||||||
|
return BSP_ERR;
|
||||||
|
}
|
||||||
|
uint32_t ticks = ms * fac_ms;
|
||||||
|
delay_ticks(ticks);
|
||||||
|
return BSP_OK;
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,10 @@ extern "C" {
|
|||||||
/* Exported functions prototypes -------------------------------------------- */
|
/* Exported functions prototypes -------------------------------------------- */
|
||||||
int8_t BSP_Delay(uint32_t ms);
|
int8_t BSP_Delay(uint32_t ms);
|
||||||
|
|
||||||
|
int8_t BSP_Delay_Init(void);
|
||||||
|
int8_t BSP_Delay_us(uint32_t us);
|
||||||
|
int8_t BSP_Delay_ms(uint32_t ms);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user