ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
hal.cpp
Go to the documentation of this file.
1#ifdef USE_LIBRETINY
2
3#include "core.h"
4#include "crash_handler.h"
5#include "esphome/core/hal.h"
6#include "preferences.h"
7
8#include <FreeRTOS.h>
9#include <task.h>
10
11// Empty libretiny namespace block to satisfy ci-custom's lint_namespace check.
12// HAL functions live in namespace esphome (root) — they are not part of the
13// libretiny component's API.
14namespace esphome::libretiny {} // namespace esphome::libretiny
15
16#if defined(USE_LN882X) && !defined(CLANG_TIDY) // clang-tidy runs without the PlatformIO flags
17// Fail the build if the -fshort-enums swap in __init__.py stops working.
19static_assert(sizeof(Ln882xEnumSizeCheck) == 1, "LN882x must build with -fshort-enums to match the SDK");
20#endif
21
22namespace esphome {
23
24// yield(), delay(), micros(), millis(), millis_64(), delayMicroseconds(),
25// arch_feed_wdt(), arch_get_cpu_cycle_count(), arch_get_cpu_freq_hz()
26// inlined in components/libretiny/hal.h.
27
28void arch_init() {
30 lt_wdt_enable(10000L);
31#ifdef USE_LIBRETINY_CRASH_HANDLER_VECTOR_PATCH
33#endif
34#ifdef USE_BK72XX
35 // BK72xx SDK creates the main Arduino task at priority 3, which is lower than
36 // all WiFi (4-5), LwIP (4), and TCP/IP (7) tasks. This causes ~100ms loop
37 // stalls whenever WiFi background processing runs, because the main task
38 // cannot resume until every higher-priority task finishes.
39 //
40 // By contrast, RTL87xx creates the main task at osPriorityRealtime (highest).
41 //
42 // Raise to priority 6: above WiFi/LwIP tasks (4-5) so they don't preempt the
43 // main loop, but below the TCP/IP thread (7) so packet processing keeps priority.
44 // This is safe because ESPHome yields voluntarily via wakeable_delay() and
45 // the Arduino mainTask yield() after each loop() iteration.
46 static constexpr UBaseType_t MAIN_TASK_PRIORITY = 6;
47 static_assert(MAIN_TASK_PRIORITY < configMAX_PRIORITIES, "MAIN_TASK_PRIORITY must be less than configMAX_PRIORITIES");
48 vTaskPrioritySet(nullptr, MAIN_TASK_PRIORITY);
49#endif
50#if LT_GPIO_RECOVER
51 lt_gpio_recover();
52#endif
53}
54
55void arch_restart() {
56 lt_reboot();
57 while (true) {
58 }
59}
60
61} // namespace esphome
62
63#endif // USE_LIBRETINY
Ln882xEnumSizeCheck
Definition hal.cpp:18
@ LN882X_ENUM_SIZE_CHECK
Definition hal.cpp:18
lt_wdt_enable(CRASH_REBOOT_WDT_MS)
void crash_handler_install()
Put the crash recorder in front of the SDK's fault handlers.
void arch_init()
Definition hal.cpp:44
void arch_restart()
Definition hal.cpp:36