ESPHome 2026.2.4
Loading...
Searching...
No Matches
remote_receiver_esp32.cpp
Go to the documentation of this file.
1#include "remote_receiver.h"
2#include "esphome/core/log.h"
3
4#ifdef USE_ESP32
5#include <soc/soc_caps.h>
6#if SOC_RMT_SUPPORTED
7#include <driver/gpio.h>
8#include <esp_clk_tree.h>
9
11
12static const char *const TAG = "remote_receiver.esp32";
13
14static bool IRAM_ATTR HOT rmt_callback(rmt_channel_handle_t channel, const rmt_rx_done_event_data_t *event, void *arg) {
15 RemoteReceiverComponentStore *store = (RemoteReceiverComponentStore *) arg;
16 rmt_rx_done_event_data_t *event_buffer = (rmt_rx_done_event_data_t *) (store->buffer + store->buffer_write);
17 uint32_t event_size = sizeof(rmt_rx_done_event_data_t);
18 uint32_t next_write = store->buffer_write + event_size + event->num_symbols * sizeof(rmt_symbol_word_t);
19 if (next_write + event_size + store->receive_size > store->buffer_size) {
20 next_write = 0;
21 }
22 if (store->buffer_read - next_write < event_size + store->receive_size) {
23 next_write = store->buffer_write;
24 store->overflow = true;
25 }
26 if (event->num_symbols <= store->filter_symbols) {
27 next_write = store->buffer_write;
28 }
29 store->error =
30 rmt_receive(channel, (uint8_t *) store->buffer + next_write + event_size, store->receive_size, &store->config);
31 event_buffer->num_symbols = event->num_symbols;
32 event_buffer->received_symbols = event->received_symbols;
33 store->buffer_write = next_write;
34 return false;
35}
36
38 rmt_rx_channel_config_t channel;
39 memset(&channel, 0, sizeof(channel));
40 channel.clk_src = RMT_CLK_SRC_DEFAULT;
41 channel.resolution_hz = this->clock_resolution_;
42 channel.mem_block_symbols = rmt_symbols_;
43 channel.gpio_num = gpio_num_t(this->pin_->get_pin());
44 channel.intr_priority = 0;
45 channel.flags.invert_in = 0;
46 channel.flags.with_dma = this->with_dma_;
47 channel.flags.io_loop_back = 0;
48 esp_err_t error = rmt_new_rx_channel(&channel, &this->channel_);
49 if (error != ESP_OK) {
50 this->error_code_ = error;
51 if (error == ESP_ERR_NOT_FOUND) {
52 this->error_string_ = "out of RMT symbol memory";
53 } else {
54 this->error_string_ = "in rmt_new_rx_channel";
55 }
56 this->mark_failed();
57 return;
58 }
59 if (this->pin_->get_flags() & gpio::FLAG_PULLUP) {
60 gpio_pullup_en(gpio_num_t(this->pin_->get_pin()));
61 } else {
62 gpio_pullup_dis(gpio_num_t(this->pin_->get_pin()));
63 }
64 error = rmt_enable(this->channel_);
65 if (error != ESP_OK) {
66 this->error_code_ = error;
67 this->error_string_ = "in rmt_enable";
68 this->mark_failed();
69 return;
70 }
71
72 if (this->carrier_frequency_ > 0 && 0 < this->carrier_duty_percent_ && this->carrier_duty_percent_ < 100) {
73 rmt_carrier_config_t carrier;
74 memset(&carrier, 0, sizeof(carrier));
75 carrier.frequency_hz = this->carrier_frequency_;
76 carrier.duty_cycle = (float) this->carrier_duty_percent_ / 100.0f;
77 carrier.flags.polarity_active_low = this->pin_->is_inverted();
78 error = rmt_apply_carrier(this->channel_, &carrier);
79 if (error != ESP_OK) {
80 this->error_code_ = error;
81 this->error_string_ = "in rmt_apply_carrier";
82 this->mark_failed();
83 return;
84 }
85 }
86
87 rmt_rx_event_callbacks_t callbacks;
88 memset(&callbacks, 0, sizeof(callbacks));
89 callbacks.on_recv_done = rmt_callback;
90 error = rmt_rx_register_event_callbacks(this->channel_, &callbacks, &this->store_);
91 if (error != ESP_OK) {
92 this->error_code_ = error;
93 this->error_string_ = "in rmt_rx_register_event_callbacks";
94 this->mark_failed();
95 return;
96 }
97
98 uint32_t event_size = sizeof(rmt_rx_done_event_data_t);
99 uint32_t rmt_freq;
100 esp_clk_tree_src_get_freq_hz((soc_module_clk_t) RMT_CLK_SRC_DEFAULT, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED,
101 &rmt_freq);
102 uint32_t max_filter_ns = UINT8_MAX * 1000u / (rmt_freq / 1000000);
103 memset(&this->store_.config, 0, sizeof(this->store_.config));
104 this->store_.config.signal_range_min_ns = std::min(this->filter_us_ * 1000, max_filter_ns);
105 this->store_.config.signal_range_max_ns = this->idle_us_ * 1000;
107 this->store_.receive_size = this->receive_symbols_ * sizeof(rmt_symbol_word_t);
108 this->store_.buffer_size = std::max((event_size + this->store_.receive_size) * 2, this->buffer_size_);
109 this->store_.buffer = new uint8_t[this->buffer_size_];
110 error = rmt_receive(this->channel_, (uint8_t *) this->store_.buffer + event_size, this->store_.receive_size,
111 &this->store_.config);
112 if (error != ESP_OK) {
113 this->error_code_ = error;
114 this->error_string_ = "in rmt_receive";
115 this->mark_failed();
116 return;
117 }
118}
119
121 ESP_LOGCONFIG(TAG,
122 "Remote Receiver:\n"
123 " Clock resolution: %" PRIu32 " hz\n"
124 " RMT symbols: %" PRIu32 "\n"
125 " Filter symbols: %" PRIu32 "\n"
126 " Receive symbols: %" PRIu32 "\n"
127 " Tolerance: %" PRIu32 "%s\n"
128 " Carrier frequency: %" PRIu32 " hz\n"
129 " Carrier duty: %u%%\n"
130 " Filter out pulses shorter than: %" PRIu32 " us\n"
131 " Signal is done after %" PRIu32 " us of no changes",
133 this->tolerance_, (this->tolerance_mode_ == remote_base::TOLERANCE_MODE_TIME) ? " us" : "%",
135 LOG_PIN(" Pin: ", this->pin_);
136 if (this->is_failed()) {
137 ESP_LOGE(TAG, "Configuring RMT driver failed: %s (%s)", esp_err_to_name(this->error_code_),
138 this->error_string_.c_str());
139 }
140}
141
143 if (this->store_.error != ESP_OK) {
144 ESP_LOGE(TAG, "Receive error");
145 this->error_code_ = this->store_.error;
146 this->error_string_ = "in rmt_callback";
147 this->mark_failed();
148 }
149 if (this->store_.overflow) {
150 ESP_LOGW(TAG, "Buffer overflow");
151 this->store_.overflow = false;
152 }
153 uint32_t buffer_write = this->store_.buffer_write;
154 while (this->store_.buffer_read != buffer_write) {
155 rmt_rx_done_event_data_t *event = (rmt_rx_done_event_data_t *) (this->store_.buffer + this->store_.buffer_read);
156 uint32_t event_size = sizeof(rmt_rx_done_event_data_t);
157 uint32_t next_read = this->store_.buffer_read + event_size + event->num_symbols * sizeof(rmt_symbol_word_t);
158 if (next_read + event_size + this->store_.receive_size > this->store_.buffer_size) {
159 next_read = 0;
160 }
161 this->decode_rmt_(event->received_symbols, event->num_symbols);
162 this->store_.buffer_read = next_read;
163
164 if (!this->temp_.empty()) {
166 }
167 }
168}
169
170void RemoteReceiverComponent::decode_rmt_(rmt_symbol_word_t *item, size_t item_count) {
171 bool prev_level = false;
172 bool idle_level = false;
173 uint32_t prev_length = 0;
174 this->temp_.clear();
175 int32_t multiplier = this->pin_->is_inverted() ? -1 : 1;
176 uint32_t filter_ticks = this->from_microseconds_(this->filter_us_);
177
178 ESP_LOGVV(TAG, "START:");
179 for (size_t i = 0; i < item_count; i++) {
180 if (item[i].level0) {
181 ESP_LOGVV(TAG, "%zu A: ON %" PRIu32 "us (%u ticks)", i, this->to_microseconds_(item[i].duration0),
182 item[i].duration0);
183 } else {
184 ESP_LOGVV(TAG, "%zu A: OFF %" PRIu32 "us (%u ticks)", i, this->to_microseconds_(item[i].duration0),
185 item[i].duration0);
186 }
187 if (item[i].level1) {
188 ESP_LOGVV(TAG, "%zu B: ON %" PRIu32 "us (%u ticks)", i, this->to_microseconds_(item[i].duration1),
189 item[i].duration1);
190 } else {
191 ESP_LOGVV(TAG, "%zu B: OFF %" PRIu32 "us (%u ticks)", i, this->to_microseconds_(item[i].duration1),
192 item[i].duration1);
193 }
194 }
195 ESP_LOGVV(TAG, "\n");
196
197 this->temp_.reserve(item_count * 2); // each RMT item has 2 pulses
198 for (size_t i = 0; i < item_count; i++) {
199 if (item[i].duration0 == 0u) {
200 // EOF, sometimes garbage follows, break early
201 break;
202 } else if ((bool(item[i].level0) == prev_level) || (item[i].duration0 < filter_ticks)) {
203 prev_length += item[i].duration0;
204 } else {
205 if (prev_length >= filter_ticks) {
206 if (prev_level) {
207 this->temp_.push_back(this->to_microseconds_(prev_length) * multiplier);
208 } else {
209 this->temp_.push_back(-int32_t(this->to_microseconds_(prev_length)) * multiplier);
210 }
211 }
212 prev_level = bool(item[i].level0);
213 prev_length = item[i].duration0;
214 }
215 idle_level = !bool(item[i].level0);
216
217 if (item[i].duration1 == 0u) {
218 // EOF, sometimes garbage follows, break early
219 break;
220 } else if ((bool(item[i].level1) == prev_level) || (item[i].duration1 < filter_ticks)) {
221 prev_length += item[i].duration1;
222 } else {
223 if (prev_length >= filter_ticks) {
224 if (prev_level) {
225 this->temp_.push_back(this->to_microseconds_(prev_length) * multiplier);
226 } else {
227 this->temp_.push_back(-int32_t(this->to_microseconds_(prev_length)) * multiplier);
228 }
229 }
230 prev_level = bool(item[i].level1);
231 prev_length = item[i].duration1;
232 }
233 idle_level = !bool(item[i].level1);
234 }
235 if (prev_length >= filter_ticks && prev_level != idle_level) {
236 if (prev_level) {
237 this->temp_.push_back(this->to_microseconds_(prev_length) * multiplier);
238 } else {
239 this->temp_.push_back(-int32_t(this->to_microseconds_(prev_length)) * multiplier);
240 }
241 }
242 if (!this->temp_.empty()) {
243 if (idle_level) {
244 this->temp_.push_back(this->idle_us_ * multiplier);
245 } else {
246 this->temp_.push_back(-int32_t(this->idle_us_) * multiplier);
247 }
248 }
249}
250
251} // namespace esphome::remote_receiver
252
253#endif // SOC_RMT_SUPPORTED
254#endif // USE_ESP32
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
virtual gpio::Flags get_flags() const =0
Retrieve GPIO pin flags.
virtual uint8_t get_pin() const =0
virtual bool is_inverted() const =0
uint32_t to_microseconds_(uint32_t ticks)
uint32_t from_microseconds_(uint32_t us)
void decode_rmt_(rmt_symbol_word_t *item, size_t item_count)
@ FLAG_PULLUP
Definition gpio.h:30
FLAG_HAS_TRANSITION float
uint32_t buffer_read
The position last read from.
volatile int32_t * buffer
Stores pulse durations in microseconds as signed integers.
volatile uint32_t buffer_write
The position last written to.