Skip to content

fix "pulseio.PulseIn maxlen is limited to 128 in esp32" #10397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions ports/espressif/common-hal/pulseio/PulseIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu
}
// We add one to the maxlen version to ensure that two symbols at lease are
// captured because we may skip the first portion of a symbol.
self->raw_symbols_size = MIN(64, maxlen / 2 + 1) * sizeof(rmt_symbol_word_t);
self->raw_symbols = (rmt_symbol_word_t *)m_malloc_without_collect(self->raw_symbols_size);
self->raw_symbols_size = (maxlen / 2 + 1) * sizeof(rmt_symbol_word_t);
// RMT DMA mode cannot access PSRAM -> ensure raw_symbols is in internal ram
self->raw_symbols = (rmt_symbol_word_t *)port_malloc(self->raw_symbols_size, true);
if (self->raw_symbols == NULL) {
m_free(self->buffer);
m_malloc_fail(self->raw_symbols_size);
Expand Down Expand Up @@ -109,17 +110,26 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu
.clk_src = RMT_CLK_SRC_DEFAULT,
// 2 us resolution so we can capture 65ms pulses. The RMT period is only 15 bits.
.resolution_hz = 1000000 / 2,
.mem_block_symbols = SOC_RMT_MEM_WORDS_PER_CHANNEL,
.mem_block_symbols = self->raw_symbols_size,
.flags.with_dma = 1
};
// If we fail here, the buffers allocated above will be garbage collected.
CHECK_ESP_RESULT(rmt_new_rx_channel(&config, &self->channel));
// If we fail here, the self->buffer will be garbage collected.
esp_err_t result = rmt_new_rx_channel(&config, &self->channel);
if (result != ESP_OK) {
port_free(self->raw_symbols);
raise_esp_error(result);
}

rmt_rx_event_callbacks_t rx_callback = {
.on_recv_done = _done_callback
};
rmt_rx_register_event_callbacks(self->channel, &rx_callback, self);
rmt_enable(self->channel);
rmt_receive(self->channel, self->raw_symbols, self->raw_symbols_size, &rx_config);
result = rmt_receive(self->channel, self->raw_symbols, self->raw_symbols_size, &rx_config);
if (result != ESP_OK) {
port_free(self->raw_symbols);
raise_esp_error(result);
}
}

bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t *self) {
Expand Down
10 changes: 5 additions & 5 deletions ports/espressif/supervisor/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,18 +304,18 @@ void port_heap_init(void) {
}

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

void *ptr = NULL;
// Try SPIRAM first when available.
// Try SPIRAM first if available.
#ifdef CONFIG_SPIRAM
ptr = heap_caps_malloc(size, caps | MALLOC_CAP_SPIRAM);
ptr = heap_caps_malloc(size, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
#endif
if (ptr == NULL) {
ptr = heap_caps_malloc(size, caps);
ptr = heap_caps_malloc(size, MALLOC_CAP_8BIT);
}
return ptr;
}
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy