ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
rtl8710b_micros.c
Go to the documentation of this file.
1// RTL8710B: the FreeRTOS tick is 1/1024 s, so millis(), micros() and delay() use the 32.768 kHz system timer instead
2// and delayMicroseconds() the DWT cycle counter. Linked with -Wl,--wrap (RTL8710B_TIME_WRAPS in __init__.py).
3
4#ifdef USE_LIBRETINY_VARIANT_RTL8710B
5
6#include <stdint.h>
7#include <FreeRTOS.h>
8#include <task.h>
9
11__attribute__((long_call)) uint32_t SYSTIMER_TickGet(void); // mask ROM, rtl8710b_delay.h
12
13#define DEMCR (*(volatile uint32_t *) 0xE000EDFC)
14#define DEMCR_TRCENA (1UL << 24)
15#define DWT_CTRL (*(volatile uint32_t *) 0xE0001000)
16#define DWT_CYCCNT (*(volatile uint32_t *) 0xE0001004)
17// Interrupt handlers call these, so they live in RAM like IRAM_ATTR (hal.h)
18#define RTL8710B_RAM_TEXT __attribute__((noinline, section(".sram.text")))
19
20// Extends the 32-bit timer, which wraps every 36.4 hours; the main loop calls millis() far more often
21static inline __attribute__((always_inline)) uint64_t timer_ticks(void) {
22 static uint64_t ticks;
23 uint32_t primask;
24 __asm volatile("mrs %0, primask\n cpsid i" : "=r"(primask)::"memory");
25 ticks += (uint32_t) (SYSTIMER_TickGet() - (uint32_t) ticks);
26 const uint64_t now = ticks;
27 __asm volatile("msr primask, %0" ::"r"(primask) : "memory");
28 return now;
29}
30
31RTL8710B_RAM_TEXT unsigned long __wrap_micros(void) { // NOLINT(readability-identifier-naming)
32 // 1000000 / 32768 = 15625 / 512
33 return (uint32_t) ((timer_ticks() * 15625) >> 9);
34}
35
36RTL8710B_RAM_TEXT unsigned long __wrap_millis(void) { // NOLINT(readability-identifier-naming)
37 // 1000 / 32768 = 125 / 4096
38 return (uint32_t) ((timer_ticks() * 125) >> 12);
39}
40
41void __wrap_delay(uint32_t ms) { // NOLINT(readability-identifier-naming)
42 if (ms == 0) {
43 vTaskDelay(0);
44 return;
45 }
46 // vTaskDelay() counts the partial tick, so sleep to a deadline; +1 covers the elapsed part of the current count
47 const uint64_t deadline = timer_ticks() + ((uint64_t) ms * 32768 + 999) / 1000 + 1;
48 // Sleeps are capped at 1024 s so a delay longer than the 36.4 hour timer wrap still sees every wrap
49 for (uint64_t now = timer_ticks(); now < deadline; now = timer_ticks()) {
50 const uint64_t ticks = (deadline - now + 31) / 32;
51 vTaskDelay((TickType_t) (ticks < 0x100000 ? ticks : 0x100000));
52 }
53}
54
55RTL8710B_RAM_TEXT void __wrap_delayMicroseconds(unsigned int us) { // NOLINT(readability-identifier-naming)
56 DEMCR |= DEMCR_TRCENA;
57 DWT_CTRL |= 1;
58 uint32_t start = DWT_CYCCNT;
59 // Rounded up: the BW12 runs at 62.5 MHz
60 uint64_t cycles = (uint64_t) us * ((SystemCoreClock + 999999) / 1000000);
61 // Steps of at most 2^31 cycles keep the 32-bit difference from wrapping
62 while (cycles > 0) {
63 const uint32_t step = cycles > 0x80000000UL ? 0x80000000UL : (uint32_t) cycles;
64 while (DWT_CYCCNT - start < step) {
65 }
66 start += step;
67 cycles -= step;
68 }
69}
70
71#endif // USE_LIBRETINY_VARIANT_RTL8710B
struct @66::@67 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:45
static void uint32_t
void __wrap_delay(uint32_t ms)
RTL8710B_RAM_TEXT unsigned long __wrap_millis(void)
uint32_t SystemCoreClock
RTL8710B_RAM_TEXT void __wrap_delayMicroseconds(unsigned int us)
RTL8710B_RAM_TEXT unsigned long __wrap_micros(void)