Skip to content

Commit dae4962

Browse files
authored
Merge pull request #10397 from Sola85/espressif-rmt-use-dma
fix "pulseio.PulseIn maxlen is limited to 128 in esp32"
2 parents 5dd6010 + f999afd commit dae4962

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

ports/espressif/common-hal/pulseio/PulseIn.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu
8080
}
8181
// We add one to the maxlen version to ensure that two symbols at lease are
8282
// captured because we may skip the first portion of a symbol.
83-
self->raw_symbols_size = MIN(64, maxlen / 2 + 1) * sizeof(rmt_symbol_word_t);
84-
self->raw_symbols = (rmt_symbol_word_t *)m_malloc_without_collect(self->raw_symbols_size);
83+
self->raw_symbols_size = (maxlen / 2 + 1) * sizeof(rmt_symbol_word_t);
84+
// RMT DMA mode cannot access PSRAM -> ensure raw_symbols is in internal ram
85+
self->raw_symbols = (rmt_symbol_word_t *)port_malloc(self->raw_symbols_size, true);
8586
if (self->raw_symbols == NULL) {
8687
m_free(self->buffer);
8788
m_malloc_fail(self->raw_symbols_size);
@@ -109,17 +110,26 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu
109110
.clk_src = RMT_CLK_SRC_DEFAULT,
110111
// 2 us resolution so we can capture 65ms pulses. The RMT period is only 15 bits.
111112
.resolution_hz = 1000000 / 2,
112-
.mem_block_symbols = SOC_RMT_MEM_WORDS_PER_CHANNEL,
113+
.mem_block_symbols = self->raw_symbols_size,
114+
.flags.with_dma = 1
113115
};
114-
// If we fail here, the buffers allocated above will be garbage collected.
115-
CHECK_ESP_RESULT(rmt_new_rx_channel(&config, &self->channel));
116+
// If we fail here, the self->buffer will be garbage collected.
117+
esp_err_t result = rmt_new_rx_channel(&config, &self->channel);
118+
if (result != ESP_OK) {
119+
port_free(self->raw_symbols);
120+
raise_esp_error(result);
121+
}
116122

117123
rmt_rx_event_callbacks_t rx_callback = {
118124
.on_recv_done = _done_callback
119125
};
120126
rmt_rx_register_event_callbacks(self->channel, &rx_callback, self);
121127
rmt_enable(self->channel);
122-
rmt_receive(self->channel, self->raw_symbols, self->raw_symbols_size, &rx_config);
128+
result = rmt_receive(self->channel, self->raw_symbols, self->raw_symbols_size, &rx_config);
129+
if (result != ESP_OK) {
130+
port_free(self->raw_symbols);
131+
raise_esp_error(result);
132+
}
123133
}
124134

125135
bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t *self) {

ports/espressif/supervisor/port.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,18 +304,18 @@ void port_heap_init(void) {
304304
}
305305

306306
void *port_malloc(size_t size, bool dma_capable) {
307-
size_t caps = MALLOC_CAP_8BIT;
308307
if (dma_capable) {
309-
caps |= MALLOC_CAP_DMA;
308+
// SPIRAM is not DMA-capable, so don't bother to ask for it.
309+
return heap_caps_malloc(size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA);
310310
}
311311

312312
void *ptr = NULL;
313-
// Try SPIRAM first when available.
313+
// Try SPIRAM first if available.
314314
#ifdef CONFIG_SPIRAM
315-
ptr = heap_caps_malloc(size, caps | MALLOC_CAP_SPIRAM);
315+
ptr = heap_caps_malloc(size, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
316316
#endif
317317
if (ptr == NULL) {
318-
ptr = heap_caps_malloc(size, caps);
318+
ptr = heap_caps_malloc(size, MALLOC_CAP_8BIT);
319319
}
320320
return ptr;
321321
}

0 commit comments

Comments
 (0)