Files
BR_YKC/Core/User/Os/os_timer.c
2026-05-21 10:01:28 +08:00

148 lines
3.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
******************************************************************************
* @file user\os\os_timer.c
* @author luhuaishuai
* @version v0.1
* @date 2026-1-12
* @brief 软件定时器管理
******************************************************************************
*/
/* Includes -------------------------------------------------------------------*/
#include "os_timer.h"
#include "task_ykc.h"
/* variables ------------------------------------------------------------------*/
TimerHandle_t YkcTimerHandle = NULL;
/* code -----------------------------------------------------------------------*/
/**
* @brief 软件定时器初始化
* @note 需要在 FreeRTOS 启动前调用
* @param none
* @retval none
*/
void SwTimer_Init(void)
{
SwTimer_YkcTimer_Init();
}
/**
* @brief 创建软件定时器
* @param name 定时器名称
* @param period_ms 定时周期(ms)
* @param auto_reload true:周期模式 false:单次模式
* @param callback 定时器回调函数
* @retval TimerHandle_t 定时器句柄NULL 表示创建失败
*/
TimerHandle_t SwTimer_Create(const char *name,
uint32_t period_ms,
bool auto_reload,
SwTimerCallback_t callback)
{
UBaseType_t reload = auto_reload ? pdTRUE : pdFALSE;
TimerHandle_t xTimer = xTimerCreate(name,
pdMS_TO_TICKS(period_ms),
reload,
NULL,
callback);
if (xTimer == NULL)
{
printf("SwTimer_Create Failed: %s\r\n", name);
}
return xTimer;
}
/**
* @brief 启动软件定时器
* @param xTimer 定时器句柄
* @retval none
*/
void SwTimer_Start(TimerHandle_t xTimer)
{
if (xTimer != NULL)
{
if (xTimerStart(xTimer, 0) != pdPASS)
{
printf("SwTimer_Start Failed\r\n");
}
}
}
/**
* @brief 停止软件定时器
* @param xTimer 定时器句柄
* @retval none
*/
void SwTimer_Stop(TimerHandle_t xTimer)
{
if (xTimer != NULL)
{
if (xTimerStop(xTimer, 0) != pdPASS)
{
printf("SwTimer_Stop Failed\r\n");
}
}
}
/**
* @brief 复位软件定时器
* @param xTimer 定时器句柄
* @retval none
*/
void SwTimer_Reset(TimerHandle_t xTimer)
{
if (xTimer != NULL)
{
if (xTimerReset(xTimer, 0) != pdPASS)
{
printf("SwTimer_Reset Failed\r\n");
}
}
}
/**
* @brief 创建充电桩状态检查定时器
* @note 500ms 周期,自动重载,遍历检查 1-6 号桩状态
* @param none
* @retval none
*/
void SwTimer_YkcTimer_Init(void)
{
YkcTimerHandle = SwTimer_Create("YkcTimer",
2500,
true,
ykc_timer_callback_function);
if (YkcTimerHandle != NULL)
{
SwTimer_Start(YkcTimerHandle);
printf("YkcTimer_Init\r\n");
}
else
{
printf("YkcTimer_Init Failed\r\n");
}
}
/**
* @brief 修改定时器周期
* @param xTimer 定时器句柄
* @param period_ms 新周期(ms)
* @retval none
*/
void SwTimer_ChangePeriod(TimerHandle_t xTimer, uint32_t period_ms)
{
if (xTimer != NULL)
{
if (xTimerChangePeriod(xTimer, pdMS_TO_TICKS(period_ms), 0) != pdPASS)
{
printf("SwTimer_ChangePeriod Failed\r\n");
}
}
}