ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
remote_receiver_bk72xx.cpp
Go to the documentation of this file.
1#include "remote_receiver.h"
3#include "esphome/core/log.h"
4
5// clang-tidy cannot parse the Beken SDK headers pulled in via ArduinoPrivate.h
6#if defined(USE_REMOTE_RECEIVER_TIMER_SAMPLING) && !defined(CLANG_TIDY)
7
8// ArduinoPrivate.h = Arduino.h + the BDK SDK headers (bk_timer.h, bk_timer_pub.h, sys_config.h)
9#include <ArduinoPrivate.h>
10
11extern "C" {
12#include "fake_clock_pub.h"
13}
14
15// The FreeRTOS tick runs on BKTIMER0 only with LT_MICROS_HIGH_RES, the default everywhere but on the BK7231Q,
16// which config validation rejects.
17#if !defined(LT_MICROS_HIGH_RES) || !LT_MICROS_HIGH_RES
18#error "remote_receiver timer sampling needs LT_MICROS_HIGH_RES"
19#endif
20
22
23ESPHOME_LOG_TAG(TAG, "remote_receiver");
24
25// BKTIMER0 is the FreeRTOS tick and BKTIMER2 the SDK clock calibration, so the sampler runs on BKTIMER1. On
26// BK7238 remote_transmitter uses it too, which config validation rejects.
27static constexpr uint8_t SAMPLE_TIMER = BKTIMER1;
28
29// Edge times come from the BKTIMER0 counter, which runs at the crystal frequency and wraps every
30// FCLK_DURATION_MS. Elapsed time is accumulated as counter deltas, so an interrupt delayed by up to one wrap
31// costs nothing; a longer hold-off (a flash write) loses whole wraps, and every other clock stops with it.
32static_assert(TIMER_TICKS_PER_US == CFG_XTAL_FREQUENCE / 1000000, "TIMER_TICKS_PER_US must match the crystal");
33static constexpr uint32_t TICK_COUNTER_WRAP = FCLK_DURATION_MS * 1000 * TIMER_TICKS_PER_US;
34
35// The latch completes in a few cycles; this bound only guards against a controller that never
36// finishes the read. Give up and skip the sample rather than return a stale counter.
37static constexpr uint32_t TICK_READ_SPINS = 100;
38
39static inline bool read_tick_counter(uint32_t *value) {
40 REG_WRITE(TIMER0_2_READ_CTL, (BKTIMER0 << TIMER0_2_READ_INDEX_POSI) | TIMER0_2_READ_OP_BIT);
41 for (uint32_t spins = 0; spins < TICK_READ_SPINS && (REG_READ(TIMER0_2_READ_CTL) & TIMER0_2_READ_OP_BIT); spins++) {
42 }
43 if (REG_READ(TIMER0_2_READ_CTL) & TIMER0_2_READ_OP_BIT)
44 return false;
45 *value = REG_READ(TIMER0_2_READ_VALUE);
46 return true;
47}
48
49// The timer handler receives only the channel number, so it reaches the receiver through this pointer.
50static RemoteReceiverComponent *s_receiver = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
51
53 if (s_receiver->store_.timer_isr())
54 s_receiver->enable_loop_soon_any_context();
55}
56
58 // digital_read() applies the pin's inverted: option, so a mark reads true, like the edge path
59 const bool level = this->pin.digital_read();
60 uint32_t counter;
61 if (!this->capture_active) {
62 // idle samples skip the tick counter read; the edge that starts a capture re-syncs it
63 if (level == this->last_level)
64 return false;
65 if (!read_tick_counter(&counter)) {
67 // wake the loop so it reports the failure
68 return true;
69 }
70 this->last_counter = counter;
71 return this->sample(level, 0);
72 }
73 if (!read_tick_counter(&counter)) {
74 // leave last_counter and the level alone, so the next read spans the gap
76 // wake the loop so it reports the failure
77 return true;
78 }
79 uint32_t elapsed = counter - this->last_counter;
80 if (counter < this->last_counter)
81 elapsed += TICK_COUNTER_WRAP;
82 this->last_counter = counter;
83 return this->sample(level, elapsed);
84}
85
87 auto &s = this->store_;
88 this->pin_->setup();
89 s.pin = this->pin_->to_isr();
90 s.capture = s.buffer;
91 s.last_level = s.pin.digital_read();
92 s_receiver = this;
93
94 timer_param_t param{};
95 param.channel = SAMPLE_TIMER;
96 param.div = 1;
97 param.period = s.sample_period_us;
98 param.t_Int_Handler = sample_timer_isr;
99 // CMD_TIMER_INIT_PARAM_US also enables the channel and its interrupt
100 if (sddev_control((char *) TIMER_DEV_NAME, CMD_TIMER_INIT_PARAM_US, &param) != 0) {
101 this->mark_failed(LOG_STR("Timer init failed"));
102 return;
103 }
104 // the timer interrupt wakes the loop when it has a frame
105 this->disable_loop();
106}
107
109 auto &s = this->store_;
110 const int32_t *frame;
111 uint16_t count;
112 bool overflow;
113 bool busy;
114 uint16_t tick_errors;
115 {
116 InterruptLock lock;
117 frame = s.ready;
118 count = s.ready_count;
119 overflow = s.overflow;
120 busy = s.busy;
121 tick_errors = s.tick_read_errors;
122 s.overflow = false;
123 s.busy = false;
124 s.tick_read_errors = 0;
125 }
126 if (overflow) {
127 ESP_LOGW(TAG, "Buffer overflow");
128 }
129 if (busy) {
130 ESP_LOGW(TAG, "Frame dropped, previous frame still being processed");
131 }
132 if (tick_errors != 0) {
133 if (tick_errors == UINT16_MAX) {
134 ESP_LOGW(TAG, "Counter read timed out at least %u times; timings may be wrong", tick_errors);
135 } else {
136 ESP_LOGW(TAG, "Counter read timed out %u times; timings may be wrong", tick_errors);
137 }
138 }
139 if (frame == nullptr) {
140 this->disable_loop();
141 return;
142 }
143 // the interrupt keeps capturing into the other half and publishes it once this one is released
144 this->call_listeners_dumpers_({frame, count});
145 s.ready = nullptr;
146}
147
148} // namespace esphome::remote_receiver
149
150#endif // USE_REMOTE_RECEIVER_TIMER_SAMPLING && !CLANG_TIDY
void mark_failed()
Mark this component as failed.
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
void disable_loop()
Disable this component's loop.
ISRInternalGPIOPin to_isr() const
Definition gpio.cpp:77
unsigned count
Definition gif.cpp:51
ESPHOME_LOG_TAG(TAG, "remote_receiver")
uint32_t value
Definition hal.cpp:59
static void uint32_t
const int32_t *volatile ready
Finished frame for the loop, nullptr once the loop has dispatched it.
bool sample(bool level, uint32_t elapsed)
Takes one sample: the logical pin level (true for a mark) and the ticks since the previous sample.
void count_tick_read_error()
Counts a failed counter read; saturating so a flood cannot wrap the count to zero.
bool timer_isr()
Returns true when it published a frame, an overflow, or a counter read error for the loop.
SemaphoreHandle_t lock