From b8f3d231de27029e117a40764146e5d1bb8c36ee Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 25 Apr 2024 09:10:54 -0500 Subject: [PATCH 01/15] espressif: fix build error when WIFI && !SSL --- ports/espressif/Makefile | 2 ++ ports/espressif/common-hal/socketpool/Socket.c | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/ports/espressif/Makefile b/ports/espressif/Makefile index 9eed90b71af5..45c4133cb0a4 100644 --- a/ports/espressif/Makefile +++ b/ports/espressif/Makefile @@ -303,7 +303,9 @@ SRC_C += \ peripherals/i2c.c \ peripherals/$(IDF_TARGET)/pins.c +ifeq ($(CIRCUITPY_SSL),1) SRC_C += lib/mbedtls_config/crt_bundle.c +endif SRC_C += $(wildcard common-hal/espidf/*.c) diff --git a/ports/espressif/common-hal/socketpool/Socket.c b/ports/espressif/common-hal/socketpool/Socket.c index 942abbdb3ae5..35351a7ccaad 100644 --- a/ports/espressif/common-hal/socketpool/Socket.c +++ b/ports/espressif/common-hal/socketpool/Socket.c @@ -31,8 +31,10 @@ #include "py/mperrno.h" #include "py/runtime.h" #include "shared-bindings/socketpool/SocketPool.h" +#if CIRCUITPY_SSL #include "shared-bindings/ssl/SSLSocket.h" #include "shared-module/ssl/SSLSocket.h" +#endif #include "supervisor/port.h" #include "supervisor/shared/tick.h" #include "supervisor/workflow.h" @@ -359,12 +361,14 @@ size_t common_hal_socketpool_socket_bind(socketpool_socket_obj_t *self, } void socketpool_socket_close(socketpool_socket_obj_t *self) { + #if CIRCUITPY_SSL if (self->ssl_socket) { ssl_sslsocket_obj_t *ssl_socket = self->ssl_socket; self->ssl_socket = NULL; common_hal_ssl_sslsocket_close(ssl_socket); return; } + #endif self->connected = false; int fd = self->num; // Ignore bogus/closed sockets From 91646290e5250d09b565f4f8e36d59a6c87aa3a4 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 30 Apr 2024 10:14:07 -0500 Subject: [PATCH 02/15] espressif: Fix queueing background tasks from other core --- ports/espressif/mpconfigport.h | 7 +++++++ ports/espressif/supervisor/port.c | 2 ++ 2 files changed, 9 insertions(+) diff --git a/ports/espressif/mpconfigport.h b/ports/espressif/mpconfigport.h index 1a97d1b6ded3..e85242dfd290 100644 --- a/ports/espressif/mpconfigport.h +++ b/ports/espressif/mpconfigport.h @@ -74,4 +74,11 @@ #define CIRCUITPY_I2C_ALLOW_INTERNAL_PULL_UP (0) #endif +// Protect the background queue with a lock because both cores may modify it. +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +extern portMUX_TYPE background_task_mutex; +#define CALLBACK_CRITICAL_BEGIN (taskENTER_CRITICAL(&background_task_mutex)) +#define CALLBACK_CRITICAL_END (taskEXIT_CRITICAL(&background_task_mutex)) + #endif // MICROPY_INCLUDED_ESPRESSIF_MPCONFIGPORT_H diff --git a/ports/espressif/supervisor/port.c b/ports/espressif/supervisor/port.c index 7aaf1332d782..da8d10afc147 100644 --- a/ports/espressif/supervisor/port.c +++ b/ports/espressif/supervisor/port.c @@ -522,3 +522,5 @@ extern void app_main(void); void app_main(void) { main(); } + +portMUX_TYPE background_task_mutex = portMUX_INITIALIZER_UNLOCKED; From 08a440d19b8285134102134d2832fdd92bef0179 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 30 Apr 2024 10:51:42 -0500 Subject: [PATCH 03/15] espressif: Don't hold interrupts disabled a long time .. just to prevent background tasks from running --- shared-module/audiomp3/MP3Decoder.c | 8 ++++---- supervisor/background_callback.h | 4 ++-- supervisor/shared/background_callback.c | 19 ++++++++++++------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/shared-module/audiomp3/MP3Decoder.c b/shared-module/audiomp3/MP3Decoder.c index 51cb37d91600..08349bde70ba 100644 --- a/shared-module/audiomp3/MP3Decoder.c +++ b/shared-module/audiomp3/MP3Decoder.c @@ -226,7 +226,7 @@ void common_hal_audiomp3_mp3file_construct(audiomp3_mp3file_obj_t *self, } void common_hal_audiomp3_mp3file_set_file(audiomp3_mp3file_obj_t *self, pyb_file_obj_t *file) { - background_callback_begin_critical_section(); + background_callback_prevent(); self->file = file; f_lseek(&self->file->fp, 0); @@ -244,7 +244,7 @@ void common_hal_audiomp3_mp3file_set_file(audiomp3_mp3file_obj_t *self, pyb_file memset(self->buffers[1], 0, MAX_BUFFER_LEN); MP3FrameInfo fi; bool result = mp3file_get_next_frame_info(self, &fi); - background_callback_end_critical_section(); + background_callback_allow(); if (!result) { mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("Failed to parse MP3 file")); @@ -296,7 +296,7 @@ void audiomp3_mp3file_reset_buffer(audiomp3_mp3file_obj_t *self, } // We don't reset the buffer index in case we're looping and we have an odd number of buffer // loads - background_callback_begin_critical_section(); + background_callback_prevent(); f_lseek(&self->file->fp, 0); self->inbuf_offset = self->inbuf_length; self->eof = 0; @@ -305,7 +305,7 @@ void audiomp3_mp3file_reset_buffer(audiomp3_mp3file_obj_t *self, mp3file_update_inbuf_half(self); mp3file_skip_id3v2(self); mp3file_find_sync_word(self); - background_callback_end_critical_section(); + background_callback_allow(); } audioio_get_buffer_result_t audiomp3_mp3file_get_buffer(audiomp3_mp3file_obj_t *self, diff --git a/supervisor/background_callback.h b/supervisor/background_callback.h index 36b0017f0cae..4be62c081eb2 100644 --- a/supervisor/background_callback.h +++ b/supervisor/background_callback.h @@ -89,8 +89,8 @@ void background_callback_reset(void); * bracket the section of code where this is the case. These calls nest, and * begins must be balanced with ends. */ -void background_callback_begin_critical_section(void); -void background_callback_end_critical_section(void); +void background_callback_prevent(void); +void background_callback_allow(void); /* * Background callbacks may stop objects from being collected diff --git a/supervisor/shared/background_callback.c b/supervisor/shared/background_callback.c index 776435ac5570..a8c0f071faf9 100644 --- a/supervisor/shared/background_callback.c +++ b/supervisor/shared/background_callback.c @@ -76,18 +76,19 @@ inline bool background_callback_pending(void) { return callback_head != NULL; } -static bool in_background_callback; +static int background_prevention_count; + void PLACE_IN_ITCM(background_callback_run_all)() { port_background_task(); if (!background_callback_pending()) { return; } CALLBACK_CRITICAL_BEGIN; - if (in_background_callback) { + if (background_prevention_count) { CALLBACK_CRITICAL_END; return; } - in_background_callback = true; + ++background_prevention_count; background_callback_t *cb = (background_callback_t *)callback_head; callback_head = NULL; callback_tail = NULL; @@ -104,15 +105,19 @@ void PLACE_IN_ITCM(background_callback_run_all)() { CALLBACK_CRITICAL_BEGIN; cb = next; } - in_background_callback = false; + --background_prevention_count; CALLBACK_CRITICAL_END; } -void background_callback_begin_critical_section() { +void background_callback_prevent() { CALLBACK_CRITICAL_BEGIN; + ++background_prevention_count; + CALLBACK_CRITICAL_END; } -void background_callback_end_critical_section() { +void background_callback_allow() { + CALLBACK_CRITICAL_BEGIN; + --background_prevention_count; CALLBACK_CRITICAL_END; } @@ -146,7 +151,7 @@ void background_callback_reset() { } callback_head = new_head; callback_tail = new_tail; - in_background_callback = false; + background_prevention_count = 0; CALLBACK_CRITICAL_END; } From 50a971ea802704d2e17355e546b2a4833a6870d3 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 1 May 2024 10:06:48 -0500 Subject: [PATCH 04/15] espressif: enable MP3Decoder on esp32s3 --- ports/espressif/Makefile | 3 ++- ports/espressif/mpconfigport.mk | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/ports/espressif/Makefile b/ports/espressif/Makefile index 45c4133cb0a4..8fb21c953f4e 100644 --- a/ports/espressif/Makefile +++ b/ports/espressif/Makefile @@ -152,7 +152,8 @@ CFLAGS += \ -DESP_PLATFORM=1 \ -DMBEDTLS_CONFIG_FILE=\"mbedtls/esp_config.h\" \ -DMBEDTLS_PADLOCK_FILE=\"ports/espressif/esp-idf/components/mbedtls/mbedtls/library/padlock.h\" \ - -DUNITY_INCLUDE_CONFIG_H -DWITH_POSIX + -DUNITY_INCLUDE_CONFIG_H -DWITH_POSIX \ + -DMP3DEC_GENERIC # Make our canary value match FreeRTOS's # This define is in FreeRTOS as tskSTACK_FILL_BYTE 0xa5U which we expand out to a full word. diff --git a/ports/espressif/mpconfigport.mk b/ports/espressif/mpconfigport.mk index 1b67a8db5830..0b7df3266343 100644 --- a/ports/espressif/mpconfigport.mk +++ b/ports/espressif/mpconfigport.mk @@ -22,7 +22,6 @@ CIRCUITPY_ANALOGBUFIO ?= 1 CIRCUITPY_AUDIOBUSIO ?= 1 CIRCUITPY_AUDIOBUSIO_PDMIN ?= 0 CIRCUITPY_AUDIOIO ?= 0 -CIRCUITPY_AUDIOMP3 ?= 0 CIRCUITPY_BLEIO_HCI = 0 CIRCUITPY_CANIO ?= 1 CIRCUITPY_COUNTIO ?= 1 @@ -136,6 +135,7 @@ CIRCUITPY_BLEIO ?= 0 else CIRCUITPY_BLEIO ?= 1 endif +CIRCUITPY_AUDIOMP3 ?= 1 endif @@ -178,3 +178,5 @@ USB_NUM_IN_ENDPOINTS = 5 # Usually lots of flash space available CIRCUITPY_MESSAGE_COMPRESSION_LEVEL ?= 1 + +CIRCUITPY_AUDIOMP3 ?= 0 From 6237a14abcdad91370443f431efa79006e49efef Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 1 May 2024 13:48:59 -0500 Subject: [PATCH 05/15] espressif: i2sout: Comment on these constants better --- ports/espressif/common-hal/audiobusio/__init__.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ports/espressif/common-hal/audiobusio/__init__.c b/ports/espressif/common-hal/audiobusio/__init__.c index 9ebf325b9798..cc7768758735 100644 --- a/ports/espressif/common-hal/audiobusio/__init__.c +++ b/ports/espressif/common-hal/audiobusio/__init__.c @@ -35,9 +35,14 @@ #include "shared-module/audiocore/__init__.h" -#define CIRCUITPY_BUFFER_COUNT 3 -#define CIRCUITPY_BUFFER_SIZE 1023 -#define CIRCUITPY_OUTPUT_SLOTS 2 +// The maximum DMA buffer size (in bytes) +#define I2S_DMA_BUFFER_MAX_SIZE 4092 +// The number of DMA buffers to allocate +#define CIRCUITPY_BUFFER_COUNT (3) +// The maximum DMA buffer size in frames (at stereo 16-bit) +#define CIRCUITPY_BUFFER_SIZE (I2S_DMA_BUFFER_MAX_SIZE / 4) +// The number of output channels is fixed at 2 +#define CIRCUITPY_OUTPUT_SLOTS (2) static void i2s_fill_buffer(i2s_t *self) { if (self->next_buffer_size == 0) { From 6136c33a8722bc0e175699f5e3082df263cd9cdc Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 1 May 2024 14:02:33 -0500 Subject: [PATCH 06/15] espressif: Use Philips data format for i2s it's the standard i2s format, and what's needed for max98357 breakout. --- ports/espressif/common-hal/audiobusio/I2SOut.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/espressif/common-hal/audiobusio/I2SOut.c b/ports/espressif/common-hal/audiobusio/I2SOut.c index 59e8604fb7d7..54a4d243fd9a 100644 --- a/ports/espressif/common-hal/audiobusio/I2SOut.c +++ b/ports/espressif/common-hal/audiobusio/I2SOut.c @@ -53,7 +53,7 @@ void common_hal_audiobusio_i2sout_construct(audiobusio_i2sout_obj_t *self, i2s_std_config_t i2s_config = { .clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(48000), - .slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO), + .slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO), .gpio_cfg = { .mclk = main_clock != NULL ? main_clock->number : I2S_GPIO_UNUSED, .bclk = bit_clock->number, From a8170f9930b0a3db389c88cf7bb054edfbbdb894 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 1 May 2024 10:00:54 -0500 Subject: [PATCH 07/15] MP3Decoder: ensure object uses finalizer .. so that the underlying allocations will be freed. This is not important now but will be if the underlying allocator is changed to something else like `port_malloc` in the future. --- shared-bindings/audiomp3/MP3Decoder.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/shared-bindings/audiomp3/MP3Decoder.c b/shared-bindings/audiomp3/MP3Decoder.c index 3cb1ac4a687b..18d17e1d2eed 100644 --- a/shared-bindings/audiomp3/MP3Decoder.c +++ b/shared-bindings/audiomp3/MP3Decoder.c @@ -94,7 +94,9 @@ STATIC mp_obj_t audiomp3_mp3file_make_new(const mp_obj_type_t *type, size_t n_ar arg = mp_call_function_2(MP_OBJ_FROM_PTR(&mp_builtin_open_obj), arg, MP_ROM_QSTR(MP_QSTR_rb)); } - audiomp3_mp3file_obj_t *self = mp_obj_malloc(audiomp3_mp3file_obj_t, &audiomp3_mp3file_type); + audiomp3_mp3file_obj_t *self = m_new_obj_with_finaliser(audiomp3_mp3file_obj_t); + self->base.type = &audiomp3_mp3file_type; + if (!mp_obj_is_type(arg, &mp_type_fileio)) { mp_raise_TypeError(MP_ERROR_TEXT("file must be a file opened in byte mode")); } @@ -260,6 +262,7 @@ STATIC const mp_rom_map_elem_t audiomp3_mp3file_locals_dict_table[] = { // Methods { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&audiomp3_mp3file_open_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audiomp3_mp3file_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&audiomp3_mp3file_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&audiomp3_mp3file___exit___obj) }, From 7af2a13245ef9540fcffb77d15e23f9bbfaf6648 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 30 Apr 2024 10:52:24 -0500 Subject: [PATCH 08/15] MP3Decoder: Allow port to override the allocator .. espressif will be able to put the mp3 data in faster RAM this way. --- py/circuitpy_defns.mk | 6 ++++- shared-module/audiomp3/__init__.c | 38 +++++++++++++++++++++++++++++++ shared-module/audiomp3/__init__.h | 8 ++++--- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 36af49a1a794..e084adb1f294 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -782,7 +782,11 @@ SRC_MOD += $(addprefix lib/mp3/src/, \ subband.c \ trigtabs.c \ ) -$(BUILD)/lib/mp3/src/buffers.o: CFLAGS += -include "py/misc.h" -D'MPDEC_ALLOCATOR(x)=m_malloc(x)' -D'MPDEC_FREE(x)=m_free(x)' +$(BUILD)/lib/mp3/src/buffers.o: CFLAGS += -include "shared-module/audiomp3/__init__.h" -D'MPDEC_ALLOCATOR(x)=mp3_alloc(x)' -D'MPDEC_FREE(x)=mp3_free(x)' +ifeq ($(CIRCUITPY_AUDIOMP3_USE_PORT_ALLOCATOR),1) +SRC_COMMON_HAL_ALL += \ + audiomp3/__init__.c +endif endif ifeq ($(CIRCUITPY_GIFIO),1) diff --git a/shared-module/audiomp3/__init__.c b/shared-module/audiomp3/__init__.c index e69de29bb2d1..5141f4ab05b4 100644 --- a/shared-module/audiomp3/__init__.c +++ b/shared-module/audiomp3/__init__.c @@ -0,0 +1,38 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Jeff Epler for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include "py/mpconfig.h" +#include "py/misc.h" +#include "shared-module/audiomp3/__init__.h" + +MP_WEAK void *mp3_alloc(size_t sz) { + return m_malloc_maybe(sz); +} + +MP_WEAK void mp3_free(void *ptr) { + m_free(ptr); +} diff --git a/shared-module/audiomp3/__init__.h b/shared-module/audiomp3/__init__.h index e7b1f3aab534..6d8a8514ed24 100644 --- a/shared-module/audiomp3/__init__.h +++ b/shared-module/audiomp3/__init__.h @@ -24,7 +24,9 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_SHARED_MODULE_AUDIOMP3__INIT__H -#define MICROPY_INCLUDED_SHARED_MODULE_AUDIOMP3__INIT__H +#pragma once -#endif // MICROPY_INCLUDED_SHARED_MODULE_AUDIOMP3__INIT__H +#include + +extern void *mp3_alloc(size_t sz); +extern void mp3_free(void *ptr); From aee66b246240631acb0b50abd824c5d848db1c9f Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 1 May 2024 10:11:25 -0500 Subject: [PATCH 09/15] espressif: use custom allocator for MP3Decoder data --- .../espressif/common-hal/audiomp3/__init__.c | 42 +++++++++++++++++++ ports/espressif/mpconfigport.mk | 2 + 2 files changed, 44 insertions(+) create mode 100644 ports/espressif/common-hal/audiomp3/__init__.c diff --git a/ports/espressif/common-hal/audiomp3/__init__.c b/ports/espressif/common-hal/audiomp3/__init__.c new file mode 100644 index 000000000000..688c7cefb1d6 --- /dev/null +++ b/ports/espressif/common-hal/audiomp3/__init__.c @@ -0,0 +1,42 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Jeff Epler for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include "py/mpprint.h" +#include "esp_heap_caps.h" +#include "shared-module/audiomp3/__init__.h" +#include "supervisor/port_heap.h" + +void *mp3_alloc(size_t sz) { + void *ptr = heap_caps_malloc(sz, MALLOC_CAP_8BIT); + mp_printf(&mp_plat_print, "alloc(%d) -> %ptr\n", (int)sz, ptr); + return ptr; +} + +void mp3_free(void *ptr) { + mp_printf(&mp_plat_print, "free(%p)\n", ptr); + heap_caps_free(ptr); +} diff --git a/ports/espressif/mpconfigport.mk b/ports/espressif/mpconfigport.mk index 0b7df3266343..d98603768cdb 100644 --- a/ports/espressif/mpconfigport.mk +++ b/ports/espressif/mpconfigport.mk @@ -179,4 +179,6 @@ USB_NUM_IN_ENDPOINTS = 5 # Usually lots of flash space available CIRCUITPY_MESSAGE_COMPRESSION_LEVEL ?= 1 +# Don't enable mp3 by default but DO allocate it to on-chip RAM if enabled CIRCUITPY_AUDIOMP3 ?= 0 +CIRCUITPY_AUDIOMP3_USE_PORT_ALLOCATOR ?= 1 From 334287b57880ce92f38e3f7370c5e15822bb7ecf Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 1 May 2024 13:48:20 -0500 Subject: [PATCH 10/15] MP3Decoder: update mp3 submodule These changes have been requested upstream at https://github.com/adafruit/Adafruit_MP3/pull/21 --- lib/mp3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mp3 b/lib/mp3 index 7a5de1ad777e..aac02afd9f24 160000 --- a/lib/mp3 +++ b/lib/mp3 @@ -1 +1 @@ -Subproject commit 7a5de1ad777e95b0f4fab7bbd35678c7d319b1b5 +Subproject commit aac02afd9f24d2ee930f650156654ab9211a306a From 34a46de3eb5c2938a1f528274a2d83bc730ff776 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 1 May 2024 13:49:55 -0500 Subject: [PATCH 11/15] MP3Decoder: Request defined behavior for signed integer shifts gcc -fsanitize=undefined reports diagnostics like these: ``` ../../src/bitstream.c:93:36: runtime error: left shift of 177 by 24 places cannot be represented in type 'int' ../../src/imdct.c:86:53: runtime error: left shift of negative value -937 ``` -fwrapv provides implementation-defined behavior that matches the expectations of two's complement arithmetic. --- py/circuitpy_defns.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index e084adb1f294..d01869e54426 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -782,7 +782,7 @@ SRC_MOD += $(addprefix lib/mp3/src/, \ subband.c \ trigtabs.c \ ) -$(BUILD)/lib/mp3/src/buffers.o: CFLAGS += -include "shared-module/audiomp3/__init__.h" -D'MPDEC_ALLOCATOR(x)=mp3_alloc(x)' -D'MPDEC_FREE(x)=mp3_free(x)' +$(BUILD)/lib/mp3/src/buffers.o: CFLAGS += -include "shared-module/audiomp3/__init__.h" -D'MPDEC_ALLOCATOR(x)=mp3_alloc(x)' -D'MPDEC_FREE(x)=mp3_free(x)' -fwrapv ifeq ($(CIRCUITPY_AUDIOMP3_USE_PORT_ALLOCATOR),1) SRC_COMMON_HAL_ALL += \ audiomp3/__init__.c From c7807cf95b3c13bc2846786abaea9a63a6c85a7e Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 1 May 2024 14:11:45 -0500 Subject: [PATCH 12/15] MP3Decoder: Need to zero out storage --- ports/espressif/common-hal/audiomp3/__init__.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ports/espressif/common-hal/audiomp3/__init__.c b/ports/espressif/common-hal/audiomp3/__init__.c index 688c7cefb1d6..994da3d21ad0 100644 --- a/ports/espressif/common-hal/audiomp3/__init__.c +++ b/ports/espressif/common-hal/audiomp3/__init__.c @@ -25,6 +25,7 @@ */ #include +#include #include "py/mpprint.h" #include "esp_heap_caps.h" #include "shared-module/audiomp3/__init__.h" @@ -32,11 +33,12 @@ void *mp3_alloc(size_t sz) { void *ptr = heap_caps_malloc(sz, MALLOC_CAP_8BIT); - mp_printf(&mp_plat_print, "alloc(%d) -> %ptr\n", (int)sz, ptr); + if (ptr) { + memset(ptr, 0, sz); + } return ptr; } void mp3_free(void *ptr) { - mp_printf(&mp_plat_print, "free(%p)\n", ptr); heap_caps_free(ptr); } From d68efb803defbc5325787f527e6fac20c43f971e Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 1 May 2024 20:20:48 -0500 Subject: [PATCH 13/15] try enabling mp3 on any espressif chip with at least 4MB flash --- ports/espressif/mpconfigport.mk | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ports/espressif/mpconfigport.mk b/ports/espressif/mpconfigport.mk index d98603768cdb..e95602bd4921 100644 --- a/ports/espressif/mpconfigport.mk +++ b/ports/espressif/mpconfigport.mk @@ -135,7 +135,6 @@ CIRCUITPY_BLEIO ?= 0 else CIRCUITPY_BLEIO ?= 1 endif -CIRCUITPY_AUDIOMP3 ?= 1 endif @@ -145,10 +144,11 @@ CIRCUITPY_BITMAPFILTER ?= 0 OPTIMIZATION_FLAGS ?= -Os endif -# No room for dualbank on boards with 2MB flash +# No room for dualbank or mp3 on boards with 2MB flash ifeq ($(CIRCUITPY_ESP_FLASH_SIZE),2MB) CIRCUITPY_BITMAPFILTER ?= 0 CIRCUITPY_DUALBANK = 0 +CIRCUITPY_AUDIOMP3 = 0 endif # Modules dependent on other modules @@ -180,5 +180,5 @@ USB_NUM_IN_ENDPOINTS = 5 CIRCUITPY_MESSAGE_COMPRESSION_LEVEL ?= 1 # Don't enable mp3 by default but DO allocate it to on-chip RAM if enabled -CIRCUITPY_AUDIOMP3 ?= 0 +CIRCUITPY_AUDIOMP3 ?= 1 CIRCUITPY_AUDIOMP3_USE_PORT_ALLOCATOR ?= 1 From 4f020a2fd10c927268f0dc820b196badbf50635c Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 1 May 2024 20:39:00 -0500 Subject: [PATCH 14/15] disable on 4MB C6, for now --- ports/espressif/mpconfigport.mk | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/espressif/mpconfigport.mk b/ports/espressif/mpconfigport.mk index e95602bd4921..780a8ecbdfb2 100644 --- a/ports/espressif/mpconfigport.mk +++ b/ports/espressif/mpconfigport.mk @@ -142,6 +142,10 @@ endif ifeq ($(CIRCUITPY_ESP_FLASH_SIZE),4MB) CIRCUITPY_BITMAPFILTER ?= 0 OPTIMIZATION_FLAGS ?= -Os +# Until the 4MB C6 partition table is updated, disable mp3 on the 4MB C6 parts +ifeq ($(IDF_TARGET),esp32c6) +CIRCUITPY_AUDIOMP3 ?= 0 +endif endif # No room for dualbank or mp3 on boards with 2MB flash From 8cbd9d996ce74c18a9ea142dda6cd8b4772f1380 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 2 May 2024 14:36:04 -0500 Subject: [PATCH 15/15] Update ports/espressif/mpconfigport.mk Co-authored-by: Scott Shawcroft --- ports/espressif/mpconfigport.mk | 1 - 1 file changed, 1 deletion(-) diff --git a/ports/espressif/mpconfigport.mk b/ports/espressif/mpconfigport.mk index 780a8ecbdfb2..445c5c69d6a4 100644 --- a/ports/espressif/mpconfigport.mk +++ b/ports/espressif/mpconfigport.mk @@ -183,6 +183,5 @@ USB_NUM_IN_ENDPOINTS = 5 # Usually lots of flash space available CIRCUITPY_MESSAGE_COMPRESSION_LEVEL ?= 1 -# Don't enable mp3 by default but DO allocate it to on-chip RAM if enabled CIRCUITPY_AUDIOMP3 ?= 1 CIRCUITPY_AUDIOMP3_USE_PORT_ALLOCATOR ?= 1