ESPHome 2025.9.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 rmt_rx_event_callbacks_t callbacks;
76 memset(&callbacks, 0, sizeof(callbacks));
77 callbacks.on_recv_done = rmt_callback;
78 error = rmt_rx_register_event_callbacks(this->channel_, &callbacks, &this->store_);
79 if (error != ESP_OK) {
80 this->error_code_ = error;
81 this->error_string_ = "in rmt_rx_register_event_callbacks";
82 this->mark_failed();
83 return;
84 }
85
86 uint32_t event_size = sizeof(rmt_rx_done_event_data_t);
87 uint32_t max_filter_ns = 255u * 1000 / (RMT_CLK_FREQ / 1000000);
88 memset(&this->store_.config, 0, sizeof(this->store_.config));
89 this->store_.config.signal_range_min_ns = std::min(this->filter_us_ * 1000, max_filter_ns);
90 this->store_.config.signal_range_max_ns = this->idle_us_ * 1000;
92 this->store_.receive_size = this->receive_symbols_ * sizeof(rmt_symbol_word_t);
93 this->store_.buffer_size = std::max((event_size + this->store_.receive_size) * 2, this->buffer_size_);
94 this->store_.buffer = new uint8_t[this->buffer_size_];
95 error = rmt_receive(this->channel_, (uint8_t *) this->store_.buffer + event_size, this->store_.receive_size,
96 &this->store_.config);
97 if (error != ESP_OK) {
98 this->error_code_ = error;
99 this->error_string_ = "in rmt_receive";
100 this->mark_failed();
101 return;
102 }
103}
104
106 ESP_LOGCONFIG(TAG, "Remote Receiver:");
107 LOG_PIN(" Pin: ", this->pin_);
108 ESP_LOGCONFIG(TAG,
109 " Clock resolution: %" PRIu32 " hz\n"
110 " RMT symbols: %" PRIu32 "\n"
111 " Filter symbols: %" PRIu32 "\n"
112 " Receive symbols: %" PRIu32 "\n"
113 " Tolerance: %" PRIu32 "%s\n"
114 " Filter out pulses shorter than: %" PRIu32 " us\n"
115 " Signal is done after %" PRIu32 " us of no changes",
117 this->tolerance_, (this->tolerance_mode_ == remote_base::TOLERANCE_MODE_TIME) ? " us" : "%",
118 this->filter_us_, this->idle_us_);
119 if (this->is_failed()) {
120 ESP_LOGE(TAG, "Configuring RMT driver failed: %s (%s)", esp_err_to_name(this->error_code_),
121 this->error_string_.c_str());
122 }
123}
124
126 if (this->store_.error != ESP_OK) {
127 ESP_LOGE(TAG, "Receive error");
128 this->error_code_ = this->store_.error;
129 this->error_string_ = "in rmt_callback";
130 this->mark_failed();
131 }
132 if (this->store_.overflow) {
133 ESP_LOGW(TAG, "Buffer overflow");
134 this->store_.overflow = false;
135 }
136 uint32_t buffer_write = this->store_.buffer_write;
137 while (this->store_.buffer_read != buffer_write) {
138 rmt_rx_done_event_data_t *event = (rmt_rx_done_event_data_t *) (this->store_.buffer + this->store_.buffer_read);
139 uint32_t event_size = sizeof(rmt_rx_done_event_data_t);
140 uint32_t next_read = this->store_.buffer_read + event_size + event->num_symbols * sizeof(rmt_symbol_word_t);
141 if (next_read + event_size + this->store_.receive_size > this->store_.buffer_size) {
142 next_read = 0;
143 }
144 this->decode_rmt_(event->received_symbols, event->num_symbols);
145 this->store_.buffer_read = next_read;
146
147 if (!this->temp_.empty()) {
149 }
150 }
151}
152
153void RemoteReceiverComponent::decode_rmt_(rmt_symbol_word_t *item, size_t item_count) {
154 bool prev_level = false;
155 bool idle_level = false;
156 uint32_t prev_length = 0;
157 this->temp_.clear();
158 int32_t multiplier = this->pin_->is_inverted() ? -1 : 1;
159 uint32_t filter_ticks = this->from_microseconds_(this->filter_us_);
160
161 ESP_LOGVV(TAG, "START:");
162 for (size_t i = 0; i < item_count; i++) {
163 if (item[i].level0) {
164 ESP_LOGVV(TAG, "%zu A: ON %" PRIu32 "us (%u ticks)", i, this->to_microseconds_(item[i].duration0),
165 item[i].duration0);
166 } else {
167 ESP_LOGVV(TAG, "%zu A: OFF %" PRIu32 "us (%u ticks)", i, this->to_microseconds_(item[i].duration0),
168 item[i].duration0);
169 }
170 if (item[i].level1) {
171 ESP_LOGVV(TAG, "%zu B: ON %" PRIu32 "us (%u ticks)", i, this->to_microseconds_(item[i].duration1),
172 item[i].duration1);
173 } else {
174 ESP_LOGVV(TAG, "%zu B: OFF %" PRIu32 "us (%u ticks)", i, this->to_microseconds_(item[i].duration1),
175 item[i].duration1);
176 }
177 }
178 ESP_LOGVV(TAG, "\n");
179
180 this->temp_.reserve(item_count * 2); // each RMT item has 2 pulses
181 for (size_t i = 0; i < item_count; i++) {
182 if (item[i].duration0 == 0u) {
183 // EOF, sometimes garbage follows, break early
184 break;
185 } else if ((bool(item[i].level0) == prev_level) || (item[i].duration0 < filter_ticks)) {
186 prev_length += item[i].duration0;
187 } else {
188 if (prev_length >= filter_ticks) {
189 if (prev_level) {
190 this->temp_.push_back(this->to_microseconds_(prev_length) * multiplier);
191 } else {
192 this->temp_.push_back(-int32_t(this->to_microseconds_(prev_length)) * multiplier);
193 }
194 }
195 prev_level = bool(item[i].level0);
196 prev_length = item[i].duration0;
197 }
198 idle_level = !bool(item[i].level0);
199
200 if (item[i].duration1 == 0u) {
201 // EOF, sometimes garbage follows, break early
202 break;
203 } else if ((bool(item[i].level1) == prev_level) || (item[i].duration1 < filter_ticks)) {
204 prev_length += item[i].duration1;
205 } else {
206 if (prev_length >= filter_ticks) {
207 if (prev_level) {
208 this->temp_.push_back(this->to_microseconds_(prev_length) * multiplier);
209 } else {
210 this->temp_.push_back(-int32_t(this->to_microseconds_(prev_length)) * multiplier);
211 }
212 }
213 prev_level = bool(item[i].level1);
214 prev_length = item[i].duration1;
215 }
216 idle_level = !bool(item[i].level1);
217 }
218 if (prev_length >= filter_ticks && prev_level != idle_level) {
219 if (prev_level) {
220 this->temp_.push_back(this->to_microseconds_(prev_length) * multiplier);
221 } else {
222 this->temp_.push_back(-int32_t(this->to_microseconds_(prev_length)) * multiplier);
223 }
224 }
225 if (!this->temp_.empty()) {
226 if (idle_level) {
227 this->temp_.push_back(this->idle_us_ * multiplier);
228 } else {
229 this->temp_.push_back(-int32_t(this->idle_us_) * multiplier);
230 }
231 }
232}
233
234} // namespace remote_receiver
235} // namespace esphome
236
237#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
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.