ESPHome
2026.12.0-dev
Loading...
Searching...
No Matches
esphome
components
libretiny
hal.h
Go to the documentation of this file.
1
#pragma once
2
3
#ifdef USE_LIBRETINY
4
5
#include <sys/cdefs.h>
6
#include <cstdint>
7
8
// For the inline millis() fast paths (xTaskGetTickCount, portTICK_PERIOD_MS).
9
#include <FreeRTOS.h>
10
#include <task.h>
11
12
#include "
esphome/core/main_task.h
"
13
#include "
esphome/core/time_64.h
"
14
15
// IRAM_ATTR places a function in executable RAM so it is callable from an
16
// ISR even while flash is busy (XIP stall, OTA, logger flash write). All
17
// LibreTiny families that need it give each function its own input section,
18
// .sram.text.<n>, so --gc-sections drops unused ones; how *(.sram.text*) is
19
// routed into RAM differs per family:
20
// RTL8720C: stock linker consumes *(.sram.text*) into .ram.code_text.
21
// RTL8710B: patch_linker.py.script injects *(.sram.text*) at the
22
// top of .ram_image2.data (which IS in ltchiptool's
23
// sections_ram). The stock linker has KEEP(*(.image2.ram.text*))
24
// in .ram_image2.text but that output section is NOT in
25
// ltchiptool's AmebaZ elf2bin sections_ram list, so code routed
26
// there is dropped from the flashed binary.
27
// LN882H: patch_linker.py.script injects *(.sram.text*) into
28
// .flash_copysection (> RAM0 AT> FLASH), after KEEP(*(.vectors))
29
// so the Cortex-M4 vector table stays 512-byte-aligned for VTOR.
30
//
31
// BK72xx (all variants) are left as a no-op: their SDK wraps flash
32
// operations in GLOBAL_INT_DISABLE() which masks FIQ + IRQ at the CPU for
33
// the duration of every write, so no ISR fires while flash is stalled and
34
// the race IRAM_ATTR guards against cannot occur. The trade-off is that
35
// interrupts are delayed (not dropped) by up to ~20 ms during a sector
36
// erase, but that is an SDK-level choice and cannot be changed from this
37
// layer.
38
#if defined(USE_BK72XX)
39
#define IRAM_ATTR
40
#else
41
#define IRAM_ATTR __attribute__((noinline, section(".sram.text." __XSTRING(__COUNTER__))))
42
#endif
43
#define PROGMEM
44
45
#ifdef USE_BK72XX
46
// Declared in the Beken FreeRTOS port (portmacro.h) and built in ARM mode so
47
// it is callable from Thumb code via interworking. The MRS CPSR instruction
48
// is ARM-only and user code here may be built in Thumb, so in_isr_context()
49
// defers to this port helper on BK72xx instead of reading CPSR inline.
50
// NOLINTNEXTLINE(readability-redundant-declaration)
51
extern
"C"
uint32_t
platform_is_in_interrupt_context
(
void
);
52
#endif
53
54
// Forward decls from Arduino's <Arduino.h> for the inline wrappers below.
55
// NOLINTBEGIN(google-runtime-int,readability-identifier-naming,readability-redundant-declaration)
56
extern
"C"
void
yield
(
void
);
57
extern
"C"
void
delay
(
unsigned
long
ms);
58
extern
"C"
unsigned
long
micros
(
void
);
59
extern
"C"
unsigned
long
millis
(
void
);
60
extern
"C"
void
delayMicroseconds
(
unsigned
int
us);
61
// NOLINTEND(google-runtime-int,readability-identifier-naming,readability-redundant-declaration)
62
63
// Forward decls from libretiny's <lt_api.h> family for the inline arch_*
64
// wrappers below. Pulling the full header would drag in the rest of the
65
// LibreTiny C API.
66
// NOLINTBEGIN(readability-redundant-declaration)
67
extern
"C"
void
lt_wdt_feed
(
void
);
68
extern
"C"
uint32_t
lt_cpu_get_cycle_count
(
void
);
69
extern
"C"
uint32_t
lt_cpu_get_freq
(
void
);
70
// NOLINTEND(readability-redundant-declaration)
71
72
namespace
esphome::libretiny
{}
73
74
namespace
esphome
{
75
77
__attribute__
((always_inline))
inline
bool
in_isr_context() {
78
#if defined(USE_BK72XX)
79
// BK72xx is ARM968E-S (ARM9); see extern declaration above.
80
return
platform_is_in_interrupt_context
() != 0;
81
#else
82
// Cortex-M (AmebaZ, AmebaZ2, LN882H). IPSR is the active exception number;
83
// non-zero means we're in a handler.
84
uint32_t
ipsr;
85
__asm__
volatile
(
"mrs %0, ipsr"
:
"=r"
(ipsr));
86
return
ipsr != 0;
87
#endif
88
}
89
91
__attribute__
((always_inline))
inline
bool
is_main_loop_thread() {
92
return
xTaskGetCurrentTaskHandle() ==
esphome_main_task_handle
;
93
}
94
__attribute__
((always_inline))
inline
void
yield
() {
::yield
(); }
95
#if defined(USE_LIBRETINY_VARIANT_RTL8710B)
96
// Already sleeps to a deadline (rtl8710b_micros.c)
97
__attribute__
((always_inline))
inline
void
delay
(
uint32_t
ms) {
::delay
(ms); }
98
#else
99
// vTaskDelay() counts the partial tick, so one more tick makes the wait at least ms
100
__attribute__
((always_inline))
inline
void
delay
(
uint32_t
ms) {
101
vTaskDelay(ms == 0 ? 0 : (ms + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS + 1);
102
}
103
#endif
104
__attribute__
((always_inline))
inline
uint32_t
micros
() {
return
static_cast<
uint32_t
>
(
::micros
()); }
105
106
// Per-variant millis() fast path — matches MillisInternal::get().
107
#if defined(USE_LIBRETINY_VARIANT_RTL8710B)
108
// The RTL8710B tick is 976.6 us; millis() comes from the system timer instead (rtl8710b_micros.c)
109
__attribute__
((always_inline))
inline
uint32_t
millis
() {
return
static_cast<
uint32_t
>
(
::millis
()); }
110
#elif defined(USE_RTL87XX) || defined(USE_LN882X)
111
static_assert
(configTICK_RATE_HZ == 1000,
"millis() fast path requires 1 kHz FreeRTOS tick"
);
112
__attribute__
((always_inline))
inline
uint32_t
millis
() {
113
// xTaskGetTickCountFromISR is mandatory in interrupt context per the FreeRTOS API contract.
114
return
in_isr_context() ? xTaskGetTickCountFromISR() : xTaskGetTickCount();
115
}
116
#elif defined(USE_BK72XX)
117
static_assert
(configTICK_RATE_HZ == 500,
"BK72xx millis() fast path assumes 500 Hz FreeRTOS tick"
);
118
__attribute__
((always_inline))
inline
uint32_t
millis
() {
return
xTaskGetTickCount() * portTICK_PERIOD_MS; }
119
#else
120
__attribute__
((always_inline))
inline
uint32_t
millis
() {
return
static_cast<
uint32_t
>
(
::millis
()); }
121
#endif
122
__attribute__
((always_inline))
inline
uint64_t
millis_64
() {
return
Millis64Impl::compute(
millis
()); }
123
124
// NOLINTNEXTLINE(readability-identifier-naming)
125
__attribute__
((always_inline))
inline
void
delayMicroseconds
(
uint32_t
us) {
::delayMicroseconds
(us); }
126
__attribute__
((hot, always_inline))
inline
void
arch_feed_wdt
() {
lt_wdt_feed
(); }
127
__attribute__
((always_inline))
inline
uint32_t
arch_get_cpu_cycle_count
() {
return
lt_cpu_get_cycle_count
(); }
128
__attribute__
((always_inline))
inline
uint32_t
arch_get_cpu_freq_hz
() {
return
lt_cpu_get_freq
(); }
129
130
void
arch_init
();
131
132
}
// namespace esphome
133
134
#endif
// USE_LIBRETINY
__attribute__
struct @67::@68 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition
main_task.h:45
millis
unsigned long millis(void)
delay
void delay(unsigned long ms)
micros
unsigned long micros(void)
yield
void yield(void)
platform_is_in_interrupt_context
uint32_t platform_is_in_interrupt_context(void)
lt_cpu_get_freq
uint32_t lt_cpu_get_freq(void)
delayMicroseconds
void delayMicroseconds(unsigned int us)
lt_cpu_get_cycle_count
uint32_t lt_cpu_get_cycle_count(void)
lt_wdt_feed
void lt_wdt_feed(void)
esphome_main_task_handle
TaskHandle_t esphome_main_task_handle
Main loop task handle and wake helpers — shared between wake.h (C++) and lwip_fast_select....
Definition
main_task.c:4
main_task.h
esphome::libretiny
Definition
atexit_stubs.cpp:14
esphome
Definition
a01nyub.cpp:7
esphome::arch_get_cpu_cycle_count
uint32_t arch_get_cpu_cycle_count()
Definition
hal.cpp:71
esphome::arch_init
void arch_init()
Definition
hal.cpp:44
esphome::delayMicroseconds
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition
hal.cpp:48
esphome::arch_get_cpu_freq_hz
uint32_t arch_get_cpu_freq_hz()
Definition
hal.cpp:57
esphome::millis_64
uint64_t millis_64()
Definition
hal.cpp:29
esphome::micros
uint32_t IRAM_ATTR HOT micros()
Definition
hal.cpp:43
esphome::arch_feed_wdt
void arch_feed_wdt()
Definition
hal.cpp:133
esphome::delay
void HOT delay(uint32_t ms)
Definition
hal.cpp:78
esphome::millis
uint32_t IRAM_ATTR HOT millis()
Definition
hal.cpp:25
uint32_t
static void uint32_t
Definition
crash_handler.cpp:179
time_64.h
Generated by
1.12.0