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