HF-Core Platform 0.1.0-dev
Hardware-Agnostic Handler Layer & RTOS Utilities for HardFOC
Loading...
Searching...
No Matches
HandlerCommon.h
Go to the documentation of this file.
1
14#pragma once
15
16#include <cstdarg>
17#include <cstdint>
18#include <cstdio>
19
20#include "Logger.h"
21
22#if defined(ESP_PLATFORM)
23#include "esp_rom_sys.h"
24#include "freertos/FreeRTOS.h"
25#include "freertos/task.h"
26#endif
27
28namespace handler_utils {
29
44inline void RouteLogToLogger(int level, const char* tag,
45 const char* format, va_list args) noexcept {
46 char buf[256];
47 vsnprintf(buf, sizeof(buf), format, args);
48 auto& log = Logger::GetInstance();
49 switch (level) {
50 case 0: log.Error(tag, "%s", buf); break;
51 case 1: log.Warn(tag, "%s", buf); break;
52 case 2: log.Info(tag, "%s", buf); break;
53 default: log.Debug(tag, "%s", buf); break;
54 }
55}
56
65inline void DelayMs(uint32_t ms) noexcept {
66#if defined(ESP_PLATFORM)
67 vTaskDelay(pdMS_TO_TICKS(ms));
68#else
69 volatile uint32_t count = ms * 10000;
70 while (count--) { __asm__ volatile(""); }
71#endif
72}
73
83inline void DelayUs(uint32_t us) noexcept {
84#if defined(ESP_PLATFORM)
85 if (us >= 1000) {
86 vTaskDelay(pdMS_TO_TICKS(us / 1000));
87 } else {
88 esp_rom_delay_us(us);
89 }
90#else
91 volatile uint32_t count = us * 10;
92 while (count--) { __asm__ volatile(""); }
93#endif
94}
95
96} // namespace handler_utils
Advanced logging system with formatting capabilities.
static Logger & GetInstance() noexcept
Get singleton instance.
Definition Logger.cpp:61
Definition HandlerCommon.h:28
void RouteLogToLogger(int level, const char *tag, const char *format, va_list args) noexcept
Route a driver log message to the Logger singleton.
Definition HandlerCommon.h:44
void DelayUs(uint32_t us) noexcept
Microsecond delay with automatic fallback to RTOS delay for large values.
Definition HandlerCommon.h:83
void DelayMs(uint32_t ms) noexcept
RTOS-aware millisecond delay.
Definition HandlerCommon.h:65