diff --git a/include/HDLog.h b/include/HDLog.h index a82c639..fc4b5b7 100644 --- a/include/HDLog.h +++ b/include/HDLog.h @@ -17,14 +17,18 @@ // 当前检查栈数量的线程总数 #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_HEAP (0) - // 使用RTOS的日志锁 #define USE_RTOS_LOG_LOCK (0) +#endif // 标准日志输出 #define _LogDExtLevel(level, format, ...) HDLogOut(1, level, __FILE__, __func__, __LINE__, format, ##__VA_ARGS__) @@ -64,6 +68,23 @@ #define LogHexData(data, len) #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 { kLogLevelSwitch = 0, ///< 日志开关 diff --git a/include/HTimer.h b/include/HTimer.h index 23c11bd..c7021ee 100644 --- a/include/HTimer.h +++ b/include/HTimer.h @@ -16,6 +16,10 @@ #define HTIMER_MAX 10 #endif +#ifndef HTIMER_REGISTER_MAX +#define HTIMER_REGISTER_MAX (1) +#endif + #ifndef HTIMER_INVALID #define HTIMER_INVALID -1 #endif @@ -32,25 +36,49 @@ typedef enum kHTimerLoop, ///< 循环执行 } eHTimerFlags; - 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)); ///< 获取毫秒 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比特 -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 -int16_t HTimerGetCurrentCaller(); +int16_t HTimerGetCurrentCaller(uint8_t id); #endif //__H_TIMER_H__ diff --git a/src/HDLog.c b/src/HDLog.c index 156185d..2f167a8 100644 --- a/src/HDLog.c +++ b/src/HDLog.c @@ -201,10 +201,15 @@ void HDLogInit(uint32_t (*getTime)()) HBitSet(sLogItem, kLogLevelError, 1); HBitSet(sLogItem, kLogLevelHex, 0); HBitSet(sLogItem, kLogLevelStack, 1); - HBitSet(sLogItem, kLogLevelHeap, 0); +#ifdef USE_RTOS + HBitSet(sLogItem, kLogLevelHeap, 1); +#endif #if USE_RTOS_LOG_LOCK - sLogMutex = xSemaphoreCreateMutex(); + if (sLogMutex == NULL) + { + sLogMutex = xSemaphoreCreateMutex(); + } #endif } diff --git a/src/HTimer.c b/src/HTimer.c index a1d9677..1f48572 100644 --- a/src/HTimer.c +++ b/src/HTimer.c @@ -10,56 +10,71 @@ static uint32_t (*GetCurrentMs)(void); #define CHECK_VALUE (0x0FFFFFFF) -struct Timer { - uint32_t flags : 1; ///< 定时器标志, eTimerFlags - uint32_t enable : 1; ///< 定时器使能 - uint32_t curr : 1; ///< 当前回调者 - uint32_t duration : 29; ///< 定时触发时长, 毫秒为计数单元 - uint32_t lastTime; ///< 上次触发时间 - HTimerCallType call; ///< 定时触发函数 + +struct TimeRegisterInfo { + uint16_t enable : 1; ///< 定时器使能 + uint16_t curr : 1; ///< 当前定时器 + uint16_t len; ///< 定时器个数 + HTimerInfo *timers; ///< 定时器 }; - -static struct Timer timers[HTIMER_MAX]; +static struct TimeRegisterInfo sTimeRegisters[HTIMER_REGISTER_MAX]; -static void CallTimer(int16_t index) { - if (index < 0 || index >= HTIMER_MAX) { - LogD("error index[%d]", index); +static void CallTimer(uint8_t id, int16_t index) { + if (id >= HTIMER_REGISTER_MAX) { + LogD("error id[%d]", id); return ; } - timers[index].curr = 1; - if (timers[index].flags == kHTimerOnce) { - timers[index].enable = 0; + if (index < 0 || index >= sTimeRegisters[id].len) { + LogD("error index[%d], len[%d]", index, sTimeRegisters[id].len); + return ; } - timers[index].call(); - timers[index].curr = 0; - timers[index].lastTime = GetCurrentMs(); + sTimeRegisters[id].curr = 1; + sTimeRegisters[id].timers[index].curr = 1; + 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) { LogD("GetCurrentMs not init"); 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) { LogD("duration overflow, duration[%d]", duration); return HTIMER_INVALID; } - for (uint16_t i = 0; i < HTIMER_MAX; ++i) { - if (timers[i].enable == 1) { + for (uint16_t i = 0; i < sTimeRegisters[id].len; ++i) { + if (sTimeRegisters[id].timers[i].enable == 1) { continue; } - timers[i].enable = 1; - timers[i].duration = duration; - timers[i].lastTime = GetCurrentMs(); - timers[i].call = call; - timers[i].flags = flags; + sTimeRegisters[id].timers[i].enable = 1; + sTimeRegisters[id].timers[i].duration = duration; + sTimeRegisters[id].timers[i].lastTime = GetCurrentMs(); + sTimeRegisters[id].timers[i].call = call; + sTimeRegisters[id].timers[i].flags = flags; return i; } @@ -79,11 +94,46 @@ uint32_t HTimerGetMs() { 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) { return ; } + if (id >= HTIMER_REGISTER_MAX) { + return ; + } + + if (sTimeRegisters[id].enable == 0) { + return ; + } + // 没必要每次都扫描, 当时间更新时再检查 static uint32_t lastMs = 0; if (lastMs == GetCurrentMs()) { @@ -91,46 +141,69 @@ void HTimerRun() { } lastMs = GetCurrentMs(); - for (uint16_t i = 0; i < HTIMER_MAX; ++i) { - if (timers[i].enable == 0) { + for (uint16_t i = 0; i < sTimeRegisters[id].len; ++i) { + if (sTimeRegisters[id].timers[i].enable == 0) { continue; } // 这里每次都获取最新时间是因为执行任务期间可能会导致时间变化 - uint32_t diff = GetCurrentMs() - timers[i].lastTime; - uint32_t timeDuration = timers[i].duration; + uint32_t diff = GetCurrentMs() - sTimeRegisters[id].timers[i].lastTime; + uint32_t timeDuration = sTimeRegisters[id].timers[i].duration; if (diff < timeDuration) { 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 - int16_t result = AddTimerData(ms, call, flags); + int16_t result = AddTimerData(id, ms, call, flags); if (result == HTIMER_INVALID) { while (1); } return result; #else - return AddTimerData(ms, call, flags); + return AddTimerData(id, ms, call, flags); #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) { LogD("error index[%d]", index); return ; } - timers[index].enable = 0; + sTimeRegisters[id].timers[index].enable = 0; } -int16_t HTimerGetCurrentCaller() { - for (uint16_t i = 0; i < HTIMER_MAX; ++i) { - if (timers[i].curr == 1) { +uint8_t HTimerGetCurrentId() { + for (uint8_t i = 0; i < HTIMER_REGISTER_MAX; ++i) { + 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; } }