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