1. 修改日志增加兼容

2. 修改定时器支持外部设置定时器数量
This commit is contained in:
coffee 2025-08-06 14:37:42 +08:00
parent a6a8151af3
commit cec303972c
4 changed files with 176 additions and 49 deletions

View File

@ -17,14 +17,18 @@
// 当前检查栈数量的线程总数 // 当前检查栈数量的线程总数
#define USE_CHECK_STACK_NUM (1) #define USE_CHECK_STACK_NUM (1)
#if USE_RTOS
#define USE_SYS_CHECK_STACK (1)
#define USE_SYS_CHECK_HEAP (1)
#define USE_RTOS_LOG_LOCK (1)
#else
// 使用系统的栈检查 // 使用系统的栈检查
#define USE_SYS_CHECK_STACK (0) #define USE_SYS_CHECK_STACK (0)
// 使用系统的堆检查 // 使用系统的堆检查
#define USE_SYS_CHECK_HEAP (0) #define USE_SYS_CHECK_HEAP (0)
// 使用RTOS的日志锁 // 使用RTOS的日志锁
#define USE_RTOS_LOG_LOCK (0) #define USE_RTOS_LOG_LOCK (0)
#endif
// 标准日志输出 // 标准日志输出
#define _LogDExtLevel(level, format, ...) HDLogOut(1, level, __FILE__, __func__, __LINE__, format, ##__VA_ARGS__) #define _LogDExtLevel(level, format, ...) HDLogOut(1, level, __FILE__, __func__, __LINE__, format, ##__VA_ARGS__)
@ -64,6 +68,23 @@
#define LogHexData(data, len) #define LogHexData(data, len)
#endif #endif
// 仅用于兼容旧版, 实际不输出数据
#define qDebugWin(A, B)
#define OutputFunInfo
#define DSDebug(...)
#define KEYDebug(...)
#define RTADebug(...)
#define CloudDebug(...)
#define FPGADebug(...)
#define TVDebug(...)
#define qDebugData(...)
#define qDebugBuffer(...)
#if 1
#define qDebug(...)
#else
#define qDebug printf
#endif
#define InitLogData() HDLogInit(NULL)
enum eLogLevel { enum eLogLevel {
kLogLevelSwitch = 0, ///< 日志开关 kLogLevelSwitch = 0, ///< 日志开关

View File

@ -16,6 +16,10 @@
#define HTIMER_MAX 10 #define HTIMER_MAX 10
#endif #endif
#ifndef HTIMER_REGISTER_MAX
#define HTIMER_REGISTER_MAX (1)
#endif
#ifndef HTIMER_INVALID #ifndef HTIMER_INVALID
#define HTIMER_INVALID -1 #define HTIMER_INVALID -1
#endif #endif
@ -32,25 +36,49 @@ typedef enum
kHTimerLoop, ///< 循环执行 kHTimerLoop, ///< 循环执行
} eHTimerFlags; } eHTimerFlags;
typedef void (*HTimerCallType)(); typedef void (*HTimerCallType)();
typedef struct HTimerInfo {
uint32_t flags : 1; ///< 定时器标志, eTimerFlags
uint32_t enable : 1; ///< 定时器使能
uint32_t curr : 1; ///< 当前回调者
uint32_t duration : 29; ///< 定时触发时长, 毫秒为计数单元
uint32_t lastTime; ///< 上次触发时间
HTimerCallType call; ///< 定时触发函数
} HTimerInfo;
///< 初始化毫秒定时器, 需要传递获取毫秒的函数 ///< 初始化毫秒定时器, 需要传递获取毫秒的函数
void HTimerInitMs(uint32_t (*func)(void)); void HTimerInitMs(uint32_t (*func)(void));
///< 获取毫秒 ///< 获取毫秒
uint32_t HTimerGetMs(); uint32_t HTimerGetMs();
/**
* @brief
* @param id ID
* @param info ,
* @param infoLen
* @return HTIMER_INVALID , ID
*/
int16_t HTimerRegisterTimerInfo(uint8_t id, HTimerInfo *info, uint16_t infoLen);
///< 移除定时器信息
void HTimerRemoveRegister(uint8_t id);
///< 运行定时器可执行任务, 需要在主循环中调用 ///< 运行定时器可执行任务, 需要在主循环中调用
void HTimerRun(); void HTimerRun(uint8_t id);
///< 添加一个定时任务, 添加失败返回 HTIMER_INVALID, ms不超过24比特 ///< 添加一个定时任务, 添加失败返回 HTIMER_INVALID, ms不超过24比特
int16_t HTimerAdd(uint32_t ms, HTimerCallType call, eHTimerFlags flags); int16_t HTimerAdd(uint8_t id, uint32_t ms, HTimerCallType call, eHTimerFlags flags);
///< 移除一个定时任务 ///< 移除一个定时任务
void HTimerRemove(int16_t index); void HTimerRemove(uint8_t id, int16_t index);
///< 在定时器回调任务中获取当前定时器ID
uint8_t HTimerGetCurrentId();
///< 在定时器回调任务中获取当前定时器调用者索引, 不存在返回 HTIMER_INVALID ///< 在定时器回调任务中获取当前定时器调用者索引, 不存在返回 HTIMER_INVALID
int16_t HTimerGetCurrentCaller(); int16_t HTimerGetCurrentCaller(uint8_t id);
#endif //__H_TIMER_H__ #endif //__H_TIMER_H__

View File

@ -201,10 +201,15 @@ void HDLogInit(uint32_t (*getTime)())
HBitSet(sLogItem, kLogLevelError, 1); HBitSet(sLogItem, kLogLevelError, 1);
HBitSet(sLogItem, kLogLevelHex, 0); HBitSet(sLogItem, kLogLevelHex, 0);
HBitSet(sLogItem, kLogLevelStack, 1); HBitSet(sLogItem, kLogLevelStack, 1);
HBitSet(sLogItem, kLogLevelHeap, 0); #ifdef USE_RTOS
HBitSet(sLogItem, kLogLevelHeap, 1);
#endif
#if USE_RTOS_LOG_LOCK #if USE_RTOS_LOG_LOCK
sLogMutex = xSemaphoreCreateMutex(); if (sLogMutex == NULL)
{
sLogMutex = xSemaphoreCreateMutex();
}
#endif #endif
} }

View File

@ -10,56 +10,71 @@ static uint32_t (*GetCurrentMs)(void);
#define CHECK_VALUE (0x0FFFFFFF) #define CHECK_VALUE (0x0FFFFFFF)
struct Timer {
uint32_t flags : 1; ///< 定时器标志, eTimerFlags struct TimeRegisterInfo {
uint32_t enable : 1; ///< 定时器使能 uint16_t enable : 1; ///< 定时器使能
uint32_t curr : 1; ///< 当前回调者 uint16_t curr : 1; ///< 当前定时器
uint32_t duration : 29; ///< 定时触发时长, 毫秒为计数单元 uint16_t len; ///< 定时器个数
uint32_t lastTime; ///< 上次触发时间 HTimerInfo *timers; ///< 定时器
HTimerCallType call; ///< 定时触发函数
}; };
static struct TimeRegisterInfo sTimeRegisters[HTIMER_REGISTER_MAX];
static struct Timer timers[HTIMER_MAX];
static void CallTimer(int16_t index) { static void CallTimer(uint8_t id, int16_t index) {
if (index < 0 || index >= HTIMER_MAX) { if (id >= HTIMER_REGISTER_MAX) {
LogD("error index[%d]", index); LogD("error id[%d]", id);
return ; return ;
} }
timers[index].curr = 1; if (index < 0 || index >= sTimeRegisters[id].len) {
if (timers[index].flags == kHTimerOnce) { LogD("error index[%d], len[%d]", index, sTimeRegisters[id].len);
timers[index].enable = 0; return ;
} }
timers[index].call(); sTimeRegisters[id].curr = 1;
timers[index].curr = 0; sTimeRegisters[id].timers[index].curr = 1;
timers[index].lastTime = GetCurrentMs(); if (sTimeRegisters[id].timers[index].flags == kHTimerOnce) {
sTimeRegisters[id].timers[index].enable = 0;
}
sTimeRegisters[id].timers[index].call();
sTimeRegisters[id].curr = 0;
sTimeRegisters[id].timers[index].curr = 0;
sTimeRegisters[id].timers[index].lastTime = GetCurrentMs();
} }
static int16_t AddTimerData(uint32_t duration, HTimerCallType call, eHTimerFlags flags) { static int16_t AddTimerData(uint8_t id, uint32_t duration, HTimerCallType call, eHTimerFlags flags) {
if (!GetCurrentMs) { if (!GetCurrentMs) {
LogD("GetCurrentMs not init"); LogD("GetCurrentMs not init");
return HTIMER_INVALID; return HTIMER_INVALID;
} }
if (id >= HTIMER_REGISTER_MAX) {
LogD("error id[%d]", id);
return HTIMER_INVALID;
}
if (sTimeRegisters[id].enable == 0) {
LogD("not enable, id[%d]", id);
return HTIMER_INVALID;
}
if ((duration & ~CHECK_VALUE) != 0) { if ((duration & ~CHECK_VALUE) != 0) {
LogD("duration overflow, duration[%d]", duration); LogD("duration overflow, duration[%d]", duration);
return HTIMER_INVALID; return HTIMER_INVALID;
} }
for (uint16_t i = 0; i < HTIMER_MAX; ++i) { for (uint16_t i = 0; i < sTimeRegisters[id].len; ++i) {
if (timers[i].enable == 1) { if (sTimeRegisters[id].timers[i].enable == 1) {
continue; continue;
} }
timers[i].enable = 1; sTimeRegisters[id].timers[i].enable = 1;
timers[i].duration = duration; sTimeRegisters[id].timers[i].duration = duration;
timers[i].lastTime = GetCurrentMs(); sTimeRegisters[id].timers[i].lastTime = GetCurrentMs();
timers[i].call = call; sTimeRegisters[id].timers[i].call = call;
timers[i].flags = flags; sTimeRegisters[id].timers[i].flags = flags;
return i; return i;
} }
@ -79,11 +94,46 @@ uint32_t HTimerGetMs() {
return GetCurrentMs(); return GetCurrentMs();
} }
void HTimerRun() { int16_t HTimerRegisterTimerInfo(uint8_t id, HTimerInfo *info, uint16_t infoLen) {
if (id >= HTIMER_REGISTER_MAX) {
LogD("error id[%d], max[%d]", id, HTIMER_REGISTER_MAX);
return HTIMER_INVALID;
}
if (sTimeRegisters[id].enable == 1) {
LogD("id[%d] already register", id);
return HTIMER_INVALID;
}
sTimeRegisters[id].timers = info;
sTimeRegisters[id].len = infoLen;
sTimeRegisters[id].enable = 1;
return 0;
}
void HTimerRemoveRegister(uint8_t id) {
if (id >= HTIMER_REGISTER_MAX) {
return ;
}
sTimeRegisters[id].enable = 0;
sTimeRegisters[id].len = 0;
sTimeRegisters[id].timers = 0;
}
void HTimerRun(uint8_t id) {
if (!GetCurrentMs) { if (!GetCurrentMs) {
return ; return ;
} }
if (id >= HTIMER_REGISTER_MAX) {
return ;
}
if (sTimeRegisters[id].enable == 0) {
return ;
}
// 没必要每次都扫描, 当时间更新时再检查 // 没必要每次都扫描, 当时间更新时再检查
static uint32_t lastMs = 0; static uint32_t lastMs = 0;
if (lastMs == GetCurrentMs()) { if (lastMs == GetCurrentMs()) {
@ -91,46 +141,69 @@ void HTimerRun() {
} }
lastMs = GetCurrentMs(); lastMs = GetCurrentMs();
for (uint16_t i = 0; i < HTIMER_MAX; ++i) { for (uint16_t i = 0; i < sTimeRegisters[id].len; ++i) {
if (timers[i].enable == 0) { if (sTimeRegisters[id].timers[i].enable == 0) {
continue; continue;
} }
// 这里每次都获取最新时间是因为执行任务期间可能会导致时间变化 // 这里每次都获取最新时间是因为执行任务期间可能会导致时间变化
uint32_t diff = GetCurrentMs() - timers[i].lastTime; uint32_t diff = GetCurrentMs() - sTimeRegisters[id].timers[i].lastTime;
uint32_t timeDuration = timers[i].duration; uint32_t timeDuration = sTimeRegisters[id].timers[i].duration;
if (diff < timeDuration) { if (diff < timeDuration) {
continue; continue;
} }
CallTimer(i); CallTimer(id, i);
} }
} }
int16_t HTimerAdd(uint32_t ms, HTimerCallType call, eHTimerFlags flags) { int16_t HTimerAdd(uint8_t id, uint32_t ms, HTimerCallType call, eHTimerFlags flags) {
#ifdef HTIMER_CHECK_INVALID_LOOP #ifdef HTIMER_CHECK_INVALID_LOOP
int16_t result = AddTimerData(ms, call, flags); int16_t result = AddTimerData(id, ms, call, flags);
if (result == HTIMER_INVALID) { if (result == HTIMER_INVALID) {
while (1); while (1);
} }
return result; return result;
#else #else
return AddTimerData(ms, call, flags); return AddTimerData(id, ms, call, flags);
#endif #endif
} }
void HTimerRemove(int16_t index) { void HTimerRemove(uint8_t id, int16_t index) {
if (id >= HTIMER_REGISTER_MAX) {
LogD("error id[%d]", id);
return ;
}
if (index < 0 || index >= HTIMER_MAX) { if (index < 0 || index >= HTIMER_MAX) {
LogD("error index[%d]", index); LogD("error index[%d]", index);
return ; return ;
} }
timers[index].enable = 0; sTimeRegisters[id].timers[index].enable = 0;
} }
int16_t HTimerGetCurrentCaller() { uint8_t HTimerGetCurrentId() {
for (uint16_t i = 0; i < HTIMER_MAX; ++i) { for (uint8_t i = 0; i < HTIMER_REGISTER_MAX; ++i) {
if (timers[i].curr == 1) { if (sTimeRegisters[i].curr == 1) {
return i;
}
}
return HTIMER_INVALID;
}
int16_t HTimerGetCurrentCaller(uint8_t id) {
if (id >= HTIMER_REGISTER_MAX) {
return HTIMER_INVALID;
}
if (sTimeRegisters[id].curr == 0) {
return HTIMER_INVALID;
}
for (uint16_t i = 0; i < sTimeRegisters[id].len; ++i) {
if (sTimeRegisters[id].timers[i].curr == 1) {
return i; return i;
} }
} }