ESPHome 2025.12.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
7namespace esphome {
8namespace remote_receiver {
9
10static const char *const TAG = "remote_receiver.esp32";
11#ifdef USE_ESP32_VARIANT_ESP32H2
12static const uint32_t RMT_CLK_FREQ = 32000000;
13#else
14static const uint32_t RMT_CLK_FREQ = 80000000;
15#endif
16
17static bool IRAM_ATTR HOT rmt_callback(rmt_channel_handle_t channel, const rmt_rx_done_event_data_t *event, void *arg) {
18 RemoteReceiverComponentStore *store = (RemoteReceiverComponentStore *) arg;
19 rmt_rx_done_event_data_t *event_buffer = (rmt_rx_done_event_data_t *) (store->buffer + store->buffer_write);
20 uint32_t event_size = sizeof(rmt_rx_done_event_data_t);
21 uint32_t next_write = store->buffer_write + event_size + event->num_symbols * sizeof(rmt_symbol_word_t);
22 if (next_write + event_size + store->receive_size > store->buffer_size) {
23 next_write = 0;
24 }
25 if (store->buffer_read - next_write < event_size + store->receive_size) {
26 next_write = store->buffer_write;
27 store->overflow = true;
28 }
29 if (event->num_symbols <= store->filter_symbols) {
30 next_write = store->buffer_write;
31 }
32 store->error =
33 rmt_receive(channel, (uint8_t *) store->buffer + next_write + event_size, store->receive_size, &store->config);
34 event_buffer->num_symbols = event->num_symbols;
35 event_buffer->received_symbols = event->received_symbols;
36 store->buffer_write = next_write;
37 return false;
38}
39
41 rmt_rx_channel_config_t channel;
42 memset(&channel, 0, sizeof(channel));
43 channel.clk_src = RMT_CLK_SRC_DEFAULT;
44 channel.resolution_hz = this->clock_resolution_;
45 channel.mem_block_symbols = rmt_symbols_;
46 channel.gpio_num = gpio_num_t(this->pin_->get_pin());
47 channel.intr_priority = 0;
48 channel.flags.invert_in = 0;
49 channel.flags.with_dma = this->with_dma_;
50 channel.flags.io_loop_back = 0;
51 esp_err_t error = rmt_new_rx_channel(&channel, &this->channel_);
52 if (error != ESP_OK) {
53 this->error_code_ = error;
54 if (error == ESP_ERR_NOT_FOUND) {
55 this->error_string_ = "out of RMT symbol memory";
56 } else {
57 this->error_string_ = "in rmt_new_rx_channel";
58 }
59 this->mark_failed();
60 return;
61 }
62 if (this->pin_->get_flags() & gpio::FLAG_PULLUP) {
63 gpio_pullup_en(gpio_num_t(this->pin_->get_pin()));
64 } else {
65 gpio_pullup_dis(gpio_num_t(this->pin_->get_pin()));
66 }
67 error = rmt_enable(this->channel_);
68 if (error != ESP_OK) {
69 this->error_code_ = error;
70 this->error_string_ = "in rmt_enable";
71 this->mark_failed();
72 return;
73 }
74
75 if (this->carrier_frequency_ > 0 && 0 < this->carrier_duty_percent_ && this->carrier_duty_percent_ < 100) {
76 rmt_carrier_config_t carrier;
77 memset(&carrier, 0, sizeof(carrier));
78 carrier.frequency_hz = this->carrier_frequency_;
79 carrier.duty_cycle = (float) this->carrier_duty_percent_ / 100.0f;
80 carrier.flags.polarity_active_low = this->pin_->is_inverted();
81 error = rmt_apply_carrier(this->channel_, &carrier);
82 if (error != ESP_OK) {
83 this->error_code_ = error;
84 this->error_string_ = "in rmt_apply_carrier";
85 this->mark_failed();
86 return;
87 }
88 }
89
90 rmt_rx_event_callbacks_t callbacks;
91 memset(&callbacks, 0, sizeof(callbacks));
92 callbacks.on_recv_done = rmt_callback;
93 error = rmt_rx_register_event_callbacks(this->channel_, &callbacks, &this->store_);
94 if (error != ESP_OK) {
95 this->error_code_ = error;
96 this->error_string_ = "in rmt_rx_register_event_callbacks";
97 this->mark_failed();
98 return;
99 }
100
101 uint32_t event_size = sizeof(rmt_rx_done_event_data_t);
102 uint32_t max_filter_ns = 255u * 1000 / (RMT_CLK_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, "Remote Receiver:");
122 LOG_PIN(" Pin: ", this->pin_);
123 ESP_LOGCONFIG(TAG,
124 " Clock resolution: %" PRIu32 " hz\n"
125 " RMT symbols: %" PRIu32 "\n"
126 " Filter symbols: %" PRIu32 "\n"
127 " Receive symbols: %" PRIu32 "\n"
128 " Tolerance: %" PRIu32 "%s\n"
129 " Carrier frequency: %" PRIu32 " hz\n"
130 " Carrier duty: %u%%\n"
131 " Filter out pulses shorter than: %" PRIu32 " us\n"
132 " Signal is done after %" PRIu32 " us of no changes",
134 this->tolerance_, (this->tolerance_mode_ == remote_base::TOLERANCE_MODE_TIME) ? " us" : "%",
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 remote_receiver
252} // namespace esphome
253
254#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
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
volatile uint32_t * buffer
Stores the time (in micros) that the leading/falling edge happened at.
volatile uint32_t buffer_read
The position last read from.
volatile uint32_t buffer_write
The position last written to.