ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
remote_receiver_rmt.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 esp_err_t error = rmt_new_rx_channel(&channel, &this->channel_);
48 if (error != ESP_OK) {
49 this->error_code_ = error;
50 if (error == ESP_ERR_NOT_FOUND) {
51 this->error_string_ = "out of RMT symbol memory";
52 } else {
53 this->error_string_ = "in rmt_new_rx_channel";
54 }
55 this->mark_failed();
56 return;
57 }
58 if (this->pin_->get_flags() & gpio::FLAG_PULLUP) {
59 gpio_pullup_en(gpio_num_t(this->pin_->get_pin()));
60 } else {
61 gpio_pullup_dis(gpio_num_t(this->pin_->get_pin()));
62 }
63 error = rmt_enable(this->channel_);
64 if (error != ESP_OK) {
65 this->error_code_ = error;
66 this->error_string_ = "in rmt_enable";
67 this->mark_failed();
68 return;
69 }
70
71 if (this->carrier_frequency_ > 0 && 0 < this->carrier_duty_percent_ && this->carrier_duty_percent_ < 100) {
72 rmt_carrier_config_t carrier;
73 memset(&carrier, 0, sizeof(carrier));
74 carrier.frequency_hz = this->carrier_frequency_;
75 carrier.duty_cycle = (float) this->carrier_duty_percent_ / 100.0f;
76 carrier.flags.polarity_active_low = this->pin_->is_inverted();
77 error = rmt_apply_carrier(this->channel_, &carrier);
78 if (error != ESP_OK) {
79 this->error_code_ = error;
80 this->error_string_ = "in rmt_apply_carrier";
81 this->mark_failed();
82 return;
83 }
84 }
85
86 rmt_rx_event_callbacks_t callbacks;
87 memset(&callbacks, 0, sizeof(callbacks));
88 callbacks.on_recv_done = rmt_callback;
89 error = rmt_rx_register_event_callbacks(this->channel_, &callbacks, &this->store_);
90 if (error != ESP_OK) {
91 this->error_code_ = error;
92 this->error_string_ = "in rmt_rx_register_event_callbacks";
93 this->mark_failed();
94 return;
95 }
96
97 uint32_t event_size = sizeof(rmt_rx_done_event_data_t);
98 uint32_t rmt_freq;
99 esp_clk_tree_src_get_freq_hz((soc_module_clk_t) RMT_CLK_SRC_DEFAULT, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED,
100 &rmt_freq);
101 uint32_t max_filter_ns = UINT8_MAX * 1000u / (rmt_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->store_.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,
121 "Remote Receiver:\n"
122 " Clock resolution: %" PRIu32 " hz\n"
123 " RMT symbols: %" PRIu32 "\n"
124 " Filter symbols: %" PRIu32 "\n"
125 " Receive symbols: %" PRIu32 "\n"
126 " Tolerance: %" PRIu32 "%s\n"
127 " Carrier frequency: %" PRIu32 " hz\n"
128 " Carrier duty: %u%%\n"
129 " Filter out pulses shorter than: %" PRIu32 " us\n"
130 " Signal is done after %" PRIu32 " us of no changes",
132 this->tolerance_, (this->tolerance_mode_ == remote_base::TOLERANCE_MODE_TIME) ? " us" : "%",
134 LOG_PIN(" Pin: ", this->pin_);
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 // SOC_RMT_SUPPORTED
253#endif // USE_ESP32
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
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
static void uint32_t
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.