ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
beken_irda.cpp
Go to the documentation of this file.
1#include "beken_irda.h"
2
3#include <array>
4
7#include "esphome/core/log.h"
8
9// clang-tidy cannot parse the Beken SDK headers pulled in via ArduinoPrivate.h
10#if defined(USE_BK72XX) && !defined(CLANG_TIDY)
11
12// ArduinoPrivate.h = Arduino.h + the BDK SDK headers (arm_arch.h, intc_pub.h)
13#include <ArduinoPrivate.h>
14
15extern "C" {
16#include "intc_pub.h"
17#include "irda_pub.h"
18// Implemented by the BDK but not declared in this SDK's intc_pub.h.
19void intc_service_change_handler(unsigned char int_num, void (*isr)());
20}
21
23
24ESPHOME_LOG_TAG(TAG, "beken_irda");
25
26// Legacy IRDA block registers, from the BDK's driver/irda/irda.h, which is not on the include path.
27static constexpr uint32_t IRDA_CTRL_REG = 0x00802400;
28static constexpr uint32_t IRDA_INT_REG = 0x00802408;
29static constexpr uint32_t IRDA_RX_FIFO_REG = 0x0080240C;
30
31static constexpr uint32_t IRDA_END_INT = 1u << 0;
32static constexpr uint32_t IRDA_REPEAT_INT = 1u << 2;
33static constexpr uint32_t IRDA_FIFO_WREN_INT = 1u << 3;
34static constexpr uint32_t IRDA_PARITY_ERR_INT = 1u << 4;
35static constexpr uint32_t IRDA_STOP_ERR_INT = 1u << 5;
36// The BDK ignores all three; parity and stop errors mark a bad frame, FIFO_WREN is only counted
37static constexpr uint32_t IRDA_FRAME_ERROR_INTS = IRDA_PARITY_ERR_INT | IRDA_STOP_ERR_INT;
38static constexpr uint32_t IRDA_ERROR_INTS = IRDA_FIFO_WREN_INT | IRDA_FRAME_ERROR_INTS;
39
41// Header mark and space, 32 bits of mark and space, and the final mark
42static constexpr size_t NEC_FRAME_DURATIONS = 2 + 64 + 1;
43
44// The counters are read and reset once per loop, so a flood between passes must not wrap them
45static inline void saturating_increment(volatile uint8_t &value) {
46 if (value != UINT8_MAX)
47 value = value + 1;
48}
49
50// The ISR runs without an instance, so it reaches the component through this pointer.
51static BekenIrdaComponent *s_irda = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
52
54
56 uint32_t status = REG_READ(IRDA_INT_REG);
57 REG_WRITE(IRDA_INT_REG, status);
58
59 if (status & IRDA_END_INT) {
60 const uint32_t code = REG_READ(IRDA_RX_FIFO_REG);
61 const uint8_t command = code >> 16;
62 const uint8_t inverse = code >> 24;
63 // a frame the hardware flagged as corrupt is dropped even when the command check passes
64 if ((status & IRDA_FRAME_ERROR_INTS) == 0 && (command ^ inverse) == 0xFF) {
65 if (this->raw_ready_)
66 saturating_increment(this->dropped_count_);
67 this->last_code_ = code;
68 this->raw_ready_ = true;
69 this->repeats_valid_ = true;
70 // a repeat still pending belongs to the previous code
71 this->repeat_ready_ = false;
72 } else {
73 saturating_increment(this->error_count_);
74 // counted once
75 status &= ~IRDA_ERROR_INTS;
76 // the repeats that follow belong to the rejected frame; a code still pending is dispatched
77 this->repeat_ready_ = false;
78 this->repeats_valid_ = false;
79 }
80 } else if ((status & IRDA_REPEAT_INT) && (status & IRDA_FRAME_ERROR_INTS) == 0 && this->repeats_valid_) {
81 this->repeat_ready_ = true;
82 }
83 if (status & IRDA_ERROR_INTS)
84 saturating_increment(this->error_count_);
86}
87
89 // Rebuild the NEC frame from the decoded address and command so the remote_base decoders and dumpers
90 // can process it.
91 std::array<int32_t, NEC_FRAME_DURATIONS> frame;
92 size_t pos = 0;
93 frame[pos++] = NEC::HEADER_HIGH_US;
94 frame[pos++] = -NEC::HEADER_LOW_US;
95 for (uint32_t mask = 1; mask != 0; mask <<= 1) {
96 frame[pos++] = NEC::BIT_HIGH_US;
97 frame[pos++] = (code & mask) ? -NEC::BIT_ONE_LOW_US : -NEC::BIT_ZERO_LOW_US;
98 }
99 frame[pos] = NEC::BIT_HIGH_US;
100 this->call_listeners_dumpers_(frame);
101}
102
104 // remote_base's NEC decoder ignores standalone repeat frames, so they go out raw for on_raw listeners
105 static constexpr int32_t REPEAT_FRAME[] = {NEC::HEADER_HIGH_US, -NEC::REPEAT_HEADER_LOW_US, NEC::BIT_HIGH_US};
106 this->call_listeners_dumpers_(REPEAT_FRAME);
107}
108
110 s_irda = this;
111 // irda_init() normally registers the IRDA interrupt during the BDK driver init. Registering again with the
112 // same priority then does nothing, and the change call replaces that handler in place; without it, the
113 // register call installs ours and the change call, which would otherwise do nothing, keeps it.
114 intc_service_register(IRQ_IRDA, PRI_IRQ_IRDA, irda_isr);
116 // sets up P26 as the NEC input and enables the IRDA clock and interrupts
117 Irda_init_app();
118 if (REG_READ(IRDA_CTRL_REG) == 0) {
119 this->mark_failed(LOG_STR("IRDA controller did not initialize"));
120 return;
121 }
122 // the interrupt wakes the loop
123 this->disable_loop();
124}
125
127 // Read the validated code together with clearing the flags, so a frame that arrives in between cannot
128 // change what is dispatched.
129 uint32_t code;
130 uint8_t errors;
131 uint8_t dropped;
132 bool have_code;
133 bool have_repeat;
134 {
136 have_code = this->raw_ready_;
137 have_repeat = this->repeat_ready_;
138 code = this->last_code_;
139 errors = this->error_count_;
140 dropped = this->dropped_count_;
141 this->raw_ready_ = false;
142 this->repeat_ready_ = false;
143 this->error_count_ = 0;
144 this->dropped_count_ = 0;
145 }
146 // at debug level so a noisy remote does not spam the log
147 if (errors != 0) {
148 ESP_LOGD(TAG, "%u decode errors", errors);
149 }
150 if (dropped != 0) {
151 ESP_LOGW(TAG, "%u codes dropped, the loop did not keep up", dropped);
152 }
153 if (!have_code && !have_repeat) {
154 this->disable_loop();
155 return;
156 }
157 if (have_code)
158 this->dispatch_full_(code);
159 if (have_repeat)
160 this->dispatch_repeat_();
161}
162
164 ESP_LOGCONFIG(TAG, "Beken IRDA receiver (NEC):");
165 LOG_PIN(" Pin: ", this->pin_);
166}
167
168} // namespace esphome::beken_irda
169
170#endif // USE_BK72XX && !CLANG_TIDY
void intc_service_change_handler(unsigned char int_num, void(*isr)())
uint8_t status
Definition bl0942.h:8
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.
Helper class to disable interrupts.
Definition helpers.h:2449
volatile bool repeats_valid_
Repeats belong to the last frame the ISR read, so they are dropped after a rejected one.
Definition beken_irda.h:32
static constexpr int32_t HEADER_HIGH_US
static constexpr int32_t REPEAT_HEADER_LOW_US
static constexpr int32_t BIT_ZERO_LOW_US
static constexpr int32_t BIT_HIGH_US
static constexpr int32_t HEADER_LOW_US
static constexpr int32_t BIT_ONE_LOW_US
ESPHOME_LOG_TAG(TAG, "beken_irda")
uint32_t value
Definition hal.cpp:59
size_t size_t pos
Definition helpers.h:1493
static void uint32_t
SemaphoreHandle_t lock