Skip to content

Commit 9ab8831

Browse files
authored
Merge pull request #9218 from jepler/mp3-esp
Enable mp3 output on esp32s3
2 parents 8734076 + 8cbd9d9 commit 9ab8831

File tree

16 files changed

+150
-26
lines changed

16 files changed

+150
-26
lines changed

ports/espressif/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ CFLAGS += \
152152
-DESP_PLATFORM=1 \
153153
-DMBEDTLS_CONFIG_FILE=\"mbedtls/esp_config.h\" \
154154
-DMBEDTLS_PADLOCK_FILE=\"ports/espressif/esp-idf/components/mbedtls/mbedtls/library/padlock.h\" \
155-
-DUNITY_INCLUDE_CONFIG_H -DWITH_POSIX
155+
-DUNITY_INCLUDE_CONFIG_H -DWITH_POSIX \
156+
-DMP3DEC_GENERIC
156157

157158
# Make our canary value match FreeRTOS's
158159
# This define is in FreeRTOS as tskSTACK_FILL_BYTE 0xa5U which we expand out to a full word.
@@ -303,7 +304,9 @@ SRC_C += \
303304
peripherals/i2c.c \
304305
peripherals/$(IDF_TARGET)/pins.c
305306

307+
ifeq ($(CIRCUITPY_SSL),1)
306308
SRC_C += lib/mbedtls_config/crt_bundle.c
309+
endif
307310

308311
SRC_C += $(wildcard common-hal/espidf/*.c)
309312

ports/espressif/common-hal/audiobusio/I2SOut.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void common_hal_audiobusio_i2sout_construct(audiobusio_i2sout_obj_t *self,
5353

5454
i2s_std_config_t i2s_config = {
5555
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(48000),
56-
.slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO),
56+
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO),
5757
.gpio_cfg = {
5858
.mclk = main_clock != NULL ? main_clock->number : I2S_GPIO_UNUSED,
5959
.bclk = bit_clock->number,

ports/espressif/common-hal/audiobusio/__init__.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@
3535

3636
#include "shared-module/audiocore/__init__.h"
3737

38-
#define CIRCUITPY_BUFFER_COUNT 3
39-
#define CIRCUITPY_BUFFER_SIZE 1023
40-
#define CIRCUITPY_OUTPUT_SLOTS 2
38+
// The maximum DMA buffer size (in bytes)
39+
#define I2S_DMA_BUFFER_MAX_SIZE 4092
40+
// The number of DMA buffers to allocate
41+
#define CIRCUITPY_BUFFER_COUNT (3)
42+
// The maximum DMA buffer size in frames (at stereo 16-bit)
43+
#define CIRCUITPY_BUFFER_SIZE (I2S_DMA_BUFFER_MAX_SIZE / 4)
44+
// The number of output channels is fixed at 2
45+
#define CIRCUITPY_OUTPUT_SLOTS (2)
4146

4247
static void i2s_fill_buffer(i2s_t *self) {
4348
if (self->next_buffer_size == 0) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2024 Jeff Epler for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdlib.h>
28+
#include <string.h>
29+
#include "py/mpprint.h"
30+
#include "esp_heap_caps.h"
31+
#include "shared-module/audiomp3/__init__.h"
32+
#include "supervisor/port_heap.h"
33+
34+
void *mp3_alloc(size_t sz) {
35+
void *ptr = heap_caps_malloc(sz, MALLOC_CAP_8BIT);
36+
if (ptr) {
37+
memset(ptr, 0, sz);
38+
}
39+
return ptr;
40+
}
41+
42+
void mp3_free(void *ptr) {
43+
heap_caps_free(ptr);
44+
}

ports/espressif/common-hal/socketpool/Socket.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
#include "py/mperrno.h"
3232
#include "py/runtime.h"
3333
#include "shared-bindings/socketpool/SocketPool.h"
34+
#if CIRCUITPY_SSL
3435
#include "shared-bindings/ssl/SSLSocket.h"
3536
#include "shared-module/ssl/SSLSocket.h"
37+
#endif
3638
#include "supervisor/port.h"
3739
#include "supervisor/shared/tick.h"
3840
#include "supervisor/workflow.h"
@@ -362,12 +364,14 @@ size_t common_hal_socketpool_socket_bind(socketpool_socket_obj_t *self,
362364
}
363365

364366
void socketpool_socket_close(socketpool_socket_obj_t *self) {
367+
#if CIRCUITPY_SSL
365368
if (self->ssl_socket) {
366369
ssl_sslsocket_obj_t *ssl_socket = self->ssl_socket;
367370
self->ssl_socket = NULL;
368371
common_hal_ssl_sslsocket_close(ssl_socket);
369372
return;
370373
}
374+
#endif
371375
self->connected = false;
372376
int fd = self->num;
373377
// Ignore bogus/closed sockets

ports/espressif/mpconfigport.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,11 @@
7474
#define CIRCUITPY_I2C_ALLOW_INTERNAL_PULL_UP (0)
7575
#endif
7676

77+
// Protect the background queue with a lock because both cores may modify it.
78+
#include "freertos/FreeRTOS.h"
79+
#include "freertos/task.h"
80+
extern portMUX_TYPE background_task_mutex;
81+
#define CALLBACK_CRITICAL_BEGIN (taskENTER_CRITICAL(&background_task_mutex))
82+
#define CALLBACK_CRITICAL_END (taskEXIT_CRITICAL(&background_task_mutex))
83+
7784
#endif // MICROPY_INCLUDED_ESPRESSIF_MPCONFIGPORT_H

ports/espressif/mpconfigport.mk

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ CIRCUITPY_ANALOGBUFIO ?= 1
2222
CIRCUITPY_AUDIOBUSIO ?= 1
2323
CIRCUITPY_AUDIOBUSIO_PDMIN ?= 0
2424
CIRCUITPY_AUDIOIO ?= 0
25-
CIRCUITPY_AUDIOMP3 ?= 0
2625
CIRCUITPY_BLEIO_HCI = 0
2726
CIRCUITPY_CANIO ?= 1
2827
CIRCUITPY_COUNTIO ?= 1
@@ -143,12 +142,17 @@ endif
143142
ifeq ($(CIRCUITPY_ESP_FLASH_SIZE),4MB)
144143
CIRCUITPY_BITMAPFILTER ?= 0
145144
OPTIMIZATION_FLAGS ?= -Os
145+
# Until the 4MB C6 partition table is updated, disable mp3 on the 4MB C6 parts
146+
ifeq ($(IDF_TARGET),esp32c6)
147+
CIRCUITPY_AUDIOMP3 ?= 0
148+
endif
146149
endif
147150

148-
# No room for dualbank on boards with 2MB flash
151+
# No room for dualbank or mp3 on boards with 2MB flash
149152
ifeq ($(CIRCUITPY_ESP_FLASH_SIZE),2MB)
150153
CIRCUITPY_BITMAPFILTER ?= 0
151154
CIRCUITPY_DUALBANK = 0
155+
CIRCUITPY_AUDIOMP3 = 0
152156
endif
153157

154158
# Modules dependent on other modules
@@ -178,3 +182,6 @@ USB_NUM_IN_ENDPOINTS = 5
178182

179183
# Usually lots of flash space available
180184
CIRCUITPY_MESSAGE_COMPRESSION_LEVEL ?= 1
185+
186+
CIRCUITPY_AUDIOMP3 ?= 1
187+
CIRCUITPY_AUDIOMP3_USE_PORT_ALLOCATOR ?= 1

ports/espressif/supervisor/port.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,3 +522,5 @@ extern void app_main(void);
522522
void app_main(void) {
523523
main();
524524
}
525+
526+
portMUX_TYPE background_task_mutex = portMUX_INITIALIZER_UNLOCKED;

py/circuitpy_defns.mk

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,11 @@ SRC_MOD += $(addprefix lib/mp3/src/, \
782782
subband.c \
783783
trigtabs.c \
784784
)
785-
$(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)'
785+
$(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
786+
ifeq ($(CIRCUITPY_AUDIOMP3_USE_PORT_ALLOCATOR),1)
787+
SRC_COMMON_HAL_ALL += \
788+
audiomp3/__init__.c
789+
endif
786790
endif
787791

788792
ifeq ($(CIRCUITPY_GIFIO),1)

0 commit comments

Comments
 (0)