summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2020-10-30 10:05:16 -0500
committerJeff Epler <jepler@gmail.com>2020-12-29 14:46:38 -0600
commita7542598a0ce1fbd7722312dd863b1d40b0c2837 (patch)
tree68c59920a804cb616564434d959155baee37deb8
parent2cd377f1a7a91a46d154f4da9dbf50a747da9b41 (diff)
esp32s2: add I2SOut
-rw-r--r--ports/esp32s2/Makefile1
-rw-r--r--ports/esp32s2/common-hal/audiobusio/I2SOut.c126
-rw-r--r--ports/esp32s2/common-hal/audiobusio/I2SOut.h45
-rw-r--r--ports/esp32s2/common-hal/audiobusio/PDMIn.c0
-rw-r--r--ports/esp32s2/common-hal/audiobusio/PDMIn.h0
-rw-r--r--ports/esp32s2/common-hal/audiobusio/__init__.c0
-rw-r--r--ports/esp32s2/common-hal/audiobusio/__init__.h0
-rw-r--r--ports/esp32s2/i2s_common.c231
-rw-r--r--ports/esp32s2/i2s_common.h61
-rw-r--r--ports/esp32s2/mpconfigport.mk6
-rw-r--r--ports/esp32s2/supervisor/port.c8
-rw-r--r--shared-module/audiocore/__init__.c60
-rw-r--r--shared-module/audiocore/__init__.h8
13 files changed, 545 insertions, 1 deletions
diff --git a/ports/esp32s2/Makefile b/ports/esp32s2/Makefile
index e08b8a792..c1e20d17a 100644
--- a/ports/esp32s2/Makefile
+++ b/ports/esp32s2/Makefile
@@ -175,6 +175,7 @@ CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_ESP32S2 -DCFG_TUSB_OS=OPT_OS_FREERTOS -DCFG_TUD
SRC_C += \
background.c \
fatfs_port.c \
+ i2s_common.c \
mphalport.c \
bindings/espidf/__init__.c \
boards/$(BOARD)/board.c \
diff --git a/ports/esp32s2/common-hal/audiobusio/I2SOut.c b/ports/esp32s2/common-hal/audiobusio/I2SOut.c
new file mode 100644
index 000000000..be5b04fa9
--- /dev/null
+++ b/ports/esp32s2/common-hal/audiobusio/I2SOut.c
@@ -0,0 +1,126 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 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 <stdint.h>
+#include <string.h>
+
+#include "mpconfigport.h"
+
+#include "bindings/espidf/__init__.h"
+
+// Some boards don't implement I2SOut, so suppress any routines from here.
+#if CIRCUITPY_AUDIOBUSIO_I2SOUT
+
+#include "extmod/vfs_fat.h"
+#include "py/gc.h"
+#include "py/mperrno.h"
+#include "py/runtime.h"
+#include "common-hal/audiobusio/I2SOut.h"
+#include "shared-bindings/audiobusio/I2SOut.h"
+#include "shared-bindings/audiocore/RawSample.h"
+#include "shared-bindings/microcontroller/Pin.h"
+#include "supervisor/shared/translate.h"
+
+#include "driver/i2s.h"
+
+// Caller validates that pins are free.
+void common_hal_audiobusio_i2sout_construct(audiobusio_i2sout_obj_t* self,
+ const mcu_pin_obj_t* bit_clock, const mcu_pin_obj_t* word_select,
+ const mcu_pin_obj_t* data, bool left_justified) {
+
+ port_i2s_allocate_init(&self->peripheral, left_justified);
+
+ i2s_pin_config_t i2s_pin_config = {
+ .bck_io_num = bit_clock->number,
+ .ws_io_num = word_select->number,
+ .data_out_num = data->number,
+ .data_in_num = I2S_PIN_NO_CHANGE,
+ };
+ ESP_CALL_RAISE(i2s_set_pin(self->peripheral.instance, &i2s_pin_config));
+ self->bit_clock = bit_clock;
+ self->word_select = word_select;
+ self->data = data;
+}
+
+bool common_hal_audiobusio_i2sout_deinited(audiobusio_i2sout_obj_t* self) {
+ return self->peripheral.instance == -1;
+}
+
+void common_hal_audiobusio_i2sout_deinit(audiobusio_i2sout_obj_t* self) {
+ if (common_hal_audiobusio_i2sout_deinited(self)) {
+ return;
+ }
+
+ if (self->bit_clock) {
+ reset_pin_number(self->bit_clock->number);
+ }
+ self->bit_clock = NULL;
+
+ if (self->word_select) {
+ reset_pin_number(self->word_select->number);
+ }
+ self->word_select = NULL;
+
+ if (self->data) {
+ reset_pin_number(self->data->number);
+ }
+ self->data = NULL;
+
+ if (self->peripheral.instance >= 0) {
+ port_i2s_reset_instance(self->peripheral.instance);
+ }
+ self->peripheral.instance = -1;
+}
+
+void common_hal_audiobusio_i2sout_play(audiobusio_i2sout_obj_t* self,
+ mp_obj_t sample, bool loop) {
+ if (common_hal_audiobusio_i2sout_get_playing(self)) {
+ common_hal_audiobusio_i2sout_stop(self);
+ }
+ port_i2s_play(&self->peripheral, sample, loop);
+}
+
+void common_hal_audiobusio_i2sout_pause(audiobusio_i2sout_obj_t* self) {
+ port_i2s_pause(&self->peripheral);
+}
+
+void common_hal_audiobusio_i2sout_resume(audiobusio_i2sout_obj_t* self) {
+ port_i2s_resume(&self->peripheral);
+}
+
+bool common_hal_audiobusio_i2sout_get_paused(audiobusio_i2sout_obj_t* self) {
+ return port_i2s_paused(&self->peripheral);
+}
+
+void common_hal_audiobusio_i2sout_stop(audiobusio_i2sout_obj_t* self) {
+ port_i2s_stop(&self->peripheral);
+}
+
+bool common_hal_audiobusio_i2sout_get_playing(audiobusio_i2sout_obj_t* self) {
+ return port_i2s_playing(&self->peripheral);
+}
+
+#endif // CIRCUITPY_AUDIOBUSIO_I2SOUT
diff --git a/ports/esp32s2/common-hal/audiobusio/I2SOut.h b/ports/esp32s2/common-hal/audiobusio/I2SOut.h
new file mode 100644
index 000000000..90266342c
--- /dev/null
+++ b/ports/esp32s2/common-hal/audiobusio/I2SOut.h
@@ -0,0 +1,45 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 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.
+ */
+
+#pragma once
+
+#include "supervisor/background_callback.h"
+#include "common-hal/microcontroller/Pin.h"
+
+#include "i2s_common.h"
+
+// Some boards don't implement I2SOut, so suppress any routines from here.
+#if CIRCUITPY_AUDIOBUSIO_I2SOUT
+
+typedef struct {
+ mp_obj_base_t base;
+ i2s_t peripheral;
+ const mcu_pin_obj_t *bit_clock;
+ const mcu_pin_obj_t *word_select;
+ const mcu_pin_obj_t *data;
+} audiobusio_i2sout_obj_t;
+
+#endif
diff --git a/ports/esp32s2/common-hal/audiobusio/PDMIn.c b/ports/esp32s2/common-hal/audiobusio/PDMIn.c
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/ports/esp32s2/common-hal/audiobusio/PDMIn.c
diff --git a/ports/esp32s2/common-hal/audiobusio/PDMIn.h b/ports/esp32s2/common-hal/audiobusio/PDMIn.h
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/ports/esp32s2/common-hal/audiobusio/PDMIn.h
diff --git a/ports/esp32s2/common-hal/audiobusio/__init__.c b/ports/esp32s2/common-hal/audiobusio/__init__.c
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/ports/esp32s2/common-hal/audiobusio/__init__.c
diff --git a/ports/esp32s2/common-hal/audiobusio/__init__.h b/ports/esp32s2/common-hal/audiobusio/__init__.h
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/ports/esp32s2/common-hal/audiobusio/__init__.h
diff --git a/ports/esp32s2/i2s_common.c b/ports/esp32s2/i2s_common.c
new file mode 100644
index 000000000..1933665a3
--- /dev/null
+++ b/ports/esp32s2/i2s_common.c
@@ -0,0 +1,231 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 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 "py/runtime.h"
+
+#include "i2s_common.h"
+#include "bindings/espidf/__init__.h"
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+
+#include "shared-module/audiocore/__init__.h"
+
+#define I2S_QUEUE_SIZE (3)
+
+static i2s_t *i2s_instance[I2S_NUM_MAX];
+static QueueHandle_t i2s_queues[I2S_NUM_MAX];
+static TaskHandle_t i2s_tasks[I2S_NUM_MAX];
+
+static int8_t port_i2s_allocate(void) {
+#if defined(I2S_NUM_1)
+ if(!i2s_instance[1]) return 1;
+#endif
+ if(!i2s_instance[0]) return 0;
+
+ mp_raise_RuntimeError(translate("Peripheral in use"));
+}
+
+void port_i2s_reset_instance(int i) {
+ assert(i >= 0 && i < I2S_NUM_MAX);
+ if (i2s_tasks[i]) {
+ vTaskDelete(i2s_tasks[i]);
+ }
+ i2s_tasks[i] = NULL;
+
+ (void)i2s_driver_uninstall(i);
+ i2s_instance[i] = NULL;
+}
+
+void i2s_reset(void) {
+ for (int i=0; i < I2S_NUM_MAX; i++) {
+ port_i2s_reset_instance(i);
+ }
+}
+
+static void i2s_fill_buffer(i2s_t *self) {
+ if (self->instance < 0 || self->instance >= I2S_NUM_MAX) {
+ return;
+ }
+ if (self->paused || !self->sample) {
+ i2s_zero_dma_buffer(self->instance);
+ return;
+ }
+ while (!self->stopping) {
+ if (self->sample_data == self->sample_end) {
+ uint32_t sample_buffer_length;
+ audioio_get_buffer_result_t get_buffer_result =
+ audiosample_get_buffer(self->sample, false, 0,
+ &self->sample_data, &sample_buffer_length);
+ self->sample_end = self->sample_data + sample_buffer_length;
+ if (get_buffer_result == GET_BUFFER_DONE) {
+ if (self->loop) {
+ audiosample_reset_buffer(self->sample, false, 0);
+ } else {
+ self->stopping = true;
+ break;
+ }
+ }
+ if (get_buffer_result == GET_BUFFER_ERROR || sample_buffer_length == 0) {
+ self->stopping = true;
+ break;
+ }
+ }
+ size_t bytes_written = 0;
+ size_t bytecount = self->sample_end - self->sample_data;
+ if (self->samples_signed && self->channel_count == 2) {
+ if (self->bytes_per_sample == 2) {
+ ESP_CALL_RAISE(i2s_write(self->instance, self->sample_data, bytecount, &bytes_written, 0));
+ } else {
+ ESP_CALL_RAISE(i2s_write_expand(self->instance, self->sample_data, bytecount, 8, 16, &bytes_written, 0));
+ }
+ } else {
+#define STACK_BUFFER_SIZE (64)
+ size_t bytes_per_frame = self->channel_count * self->bytes_per_sample;
+ size_t framecount = MIN(STACK_BUFFER_SIZE / bytes_per_frame, bytecount);
+ int16_t signed_samples[STACK_BUFFER_SIZE / sizeof(int16_t)];
+ if (self->samples_signed) {
+ assert(self->channel_count == 1);
+ if (self->bytes_per_sample == 1) {
+ audiosample_convert_s8m_s16s(signed_samples, (int8_t*)(void*)self->sample_data, framecount);
+ } else {
+ audiosample_convert_s16m_s16s(signed_samples, (int16_t*)(void*)self->sample_data, framecount);
+ }
+ } else {
+ if (self->channel_count == 1) {
+ if (self->bytes_per_sample == 1) {
+ audiosample_convert_u8m_s16s(signed_samples, (uint8_t*)(void*)self->sample_data, framecount);
+ } else {
+ audiosample_convert_u16m_s16s(signed_samples, (uint16_t*)(void*)self->sample_data, framecount);
+ }
+ } else {
+ if (self->bytes_per_sample == 1) {
+ audiosample_convert_u8s_s16s(signed_samples, (uint8_t*)(void*)self->sample_data, framecount);
+ } else {
+ audiosample_convert_u16s_s16s(signed_samples, (uint16_t*)(void*)self->sample_data, framecount);
+ }
+ }
+ }
+ size_t expanded_bytes_written = 0;
+ ESP_CALL_RAISE(i2s_write(self->instance, signed_samples, 4*framecount, &expanded_bytes_written, 0));
+ assert(expanded_bytes_written % 4 == 0);
+ bytes_written = expanded_bytes_written / 4 * bytes_per_frame;
+ }
+ self->sample_data += bytes_written;
+ // We have filled the DMA buffer
+ if (!bytes_written) {
+ break;
+ }
+ }
+}
+
+static void i2s_callback_fun(void *self_in) {
+ i2s_t *self = self_in;
+ i2s_fill_buffer(self);
+}
+
+static void i2s_event_task(void *self_in) {
+ i2s_t *self = self_in;
+ while(true) {
+ i2s_event_type_t event;
+ BaseType_t result = xQueueReceive(i2s_queues[self->instance], &event, portMAX_DELAY);
+ if (result && event == I2S_EVENT_TX_DONE) {
+ background_callback_add(&self->callback, i2s_callback_fun, self_in);
+ }
+ }
+}
+
+void port_i2s_allocate_init(i2s_t *self, bool left_justified) {
+ self->instance = port_i2s_allocate();
+
+ i2s_config_t i2s_config = {
+ .mode = I2S_MODE_MASTER | I2S_MODE_TX,
+ .sample_rate = 44100,
+ .bits_per_sample = 16,
+ .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
+ .communication_format = left_justified ? I2S_COMM_FORMAT_STAND_I2S : I2S_COMM_FORMAT_STAND_I2S,
+ .dma_buf_count = 2,
+ .dma_buf_len = 128, // in _frames_, so 128 is 512 bytes per dma buf
+ .use_apll = false,
+ };
+ ESP_CALL_RAISE(i2s_driver_install(self->instance, &i2s_config, I2S_QUEUE_SIZE, &i2s_queues[self->instance]));
+
+ if (!xTaskCreate(i2s_event_task, "I2S_task", 3 * configMINIMAL_STACK_SIZE, self, CONFIG_PTHREAD_TASK_PRIO_DEFAULT, &i2s_tasks[self->instance])) {
+ mp_raise_OSError_msg(translate("xTaskCreate failed"));
+ }
+ i2s_instance[self->instance] = self;
+
+}
+
+
+void port_i2s_play(i2s_t *self, mp_obj_t sample, bool loop) {
+ self->sample = sample;
+ self->bytes_per_sample = audiosample_bits_per_sample(sample) / 8;
+ self->channel_count = audiosample_channel_count(sample);
+ bool single_buffer;
+ bool samples_signed;
+ uint32_t max_buffer_length;
+ uint8_t spacing;
+ audiosample_get_buffer_structure(sample, false, &single_buffer, &samples_signed,
+ &max_buffer_length, &spacing);
+ self->samples_signed = samples_signed;
+ self->loop = loop;
+ self->playing = true;
+ self->paused = false;
+ self->stopping = false;
+ // We always output stereo so output twice as many bits.
+ // uint16_t bits_per_sample_output = bits_per_sample * 2;
+ ESP_CALL_RAISE(i2s_set_sample_rates(self->instance, audiosample_sample_rate(sample)));
+ i2s_fill_buffer(self);
+}
+
+bool port_i2s_playing(i2s_t *self) {
+ return self->playing && !self->stopping;
+}
+
+bool port_i2s_paused(i2s_t *self) {
+ return self->paused;
+}
+
+void port_i2s_stop(i2s_t *self) {
+ self->sample = NULL;
+ self->paused = false;
+ self->playing = false;
+ self->stopping = false;
+}
+
+void port_i2s_pause(i2s_t *self) {
+ if (!self->paused) {
+ self->paused = true;
+ ESP_CALL_RAISE(i2s_stop(self->instance));
+ }
+}
+
+void port_i2s_resume(i2s_t *self) {
+ if (self->paused) {
+ self->paused = false;
+ ESP_CALL_RAISE(i2s_start(self->instance));
+ }
+}
diff --git a/ports/esp32s2/i2s_common.h b/ports/esp32s2/i2s_common.h
new file mode 100644
index 000000000..7709735da
--- /dev/null
+++ b/ports/esp32s2/i2s_common.h
@@ -0,0 +1,61 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 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.
+ */
+
+#pragma once
+
+#include "py/obj.h"
+
+#include "supervisor/background_callback.h"
+
+#include "driver/i2s.h"
+
+typedef struct {
+ mp_obj_t *sample;
+ bool left_justified;
+ bool loop;
+ bool paused;
+ bool playing;
+ bool stopping;
+ bool samples_signed;
+ int8_t bytes_per_sample;
+ int8_t channel_count;
+ int8_t instance;
+ uint16_t buffer_length;
+ uint8_t *sample_data, *sample_end;
+ i2s_config_t i2s_config;
+ background_callback_t callback;
+} i2s_t;
+
+
+void port_i2s_allocate_init(i2s_t *self, bool left_justified);
+void port_i2s_reset_instance(int i);
+void i2s_reset(void);
+void port_i2s_play(i2s_t *self, mp_obj_t sample, bool loop);
+void port_i2s_stop(i2s_t *self);
+bool port_i2s_playing(i2s_t *self);
+bool port_i2s_paused(i2s_t *self);
+void port_i2s_pause(i2s_t *self);
+void port_i2s_resume(i2s_t *self);
diff --git a/ports/esp32s2/mpconfigport.mk b/ports/esp32s2/mpconfigport.mk
index 2ad8afb51..562c60998 100644
--- a/ports/esp32s2/mpconfigport.mk
+++ b/ports/esp32s2/mpconfigport.mk
@@ -15,7 +15,11 @@ LONGINT_IMPL = MPZ
# These modules are implemented in ports/<port>/common-hal:
CIRCUITPY_FULL_BUILD = 1
CIRCUITPY_ALARM = 1
-CIRCUITPY_AUDIOBUSIO = 0
+CIRCUITPY_AUDIOCORE = 1
+CIRCUITPY_AUDIOMP3 = 0
+CIRCUITPY_AUDIOBUSIO = 1
+CIRCUITPY_AUDIOBUSIO_PDMIN = 0
+CIRCUITPY_AUDIOBUSIO_I2SOUT = 1
CIRCUITPY_AUDIOIO = 0
CIRCUITPY_CANIO = 1
CIRCUITPY_COUNTIO = 1
diff --git a/ports/esp32s2/supervisor/port.c b/ports/esp32s2/supervisor/port.c
index ff35845f7..75492492e 100644
--- a/ports/esp32s2/supervisor/port.c
+++ b/ports/esp32s2/supervisor/port.c
@@ -61,6 +61,10 @@
#include "components/soc/soc/esp32s2/include/soc/cache_memory.h"
#include "components/soc/soc/esp32s2/include/soc/rtc_cntl_reg.h"
+#if CIRCUITPY_AUDIOBUSIO
+#include "i2s_common.h"
+#endif
+
#define HEAP_SIZE (48 * 1024)
uint32_t* heap;
@@ -153,6 +157,10 @@ void reset_port(void) {
ps2_reset();
#endif
+#if CIRCUITPY_AUDIOBUSIO
+ i2s_reset();
+#endif
+
#if CIRCUITPY_PULSEIO
esp32s2_peripherals_rmt_reset();
pulsein_reset();
diff --git a/shared-module/audiocore/__init__.c b/shared-module/audiocore/__init__.c
index ac9d41b60..7bdfe460c 100644
--- a/shared-module/audiocore/__init__.c
+++ b/shared-module/audiocore/__init__.c
@@ -70,3 +70,63 @@ void audiosample_get_buffer_structure(mp_obj_t sample_obj, bool single_channel,
proto->get_buffer_structure(MP_OBJ_TO_PTR(sample_obj), single_channel, single_buffer,
samples_signed, max_buffer_length, spacing);
}
+
+void audiosample_convert_u8m_s16s(int16_t *buffer_out, const uint8_t *buffer_in, size_t nframes) {
+ for(;nframes--;) {
+ int16_t sample = (*buffer_in++ - 0x80) << 8;
+ *buffer_out++ = sample;
+ *buffer_out++ = sample;
+ }
+}
+
+
+void audiosample_convert_u8s_s16s(int16_t *buffer_out, const uint8_t *buffer_in, size_t nframes) {
+ size_t nsamples = 2*nframes;
+ for(;nsamples--;) {
+ int16_t sample = (*buffer_in++ - 0x80) << 8;
+ *buffer_out++ = sample;
+ }
+}
+
+void audiosample_convert_s8m_s16s(int16_t *buffer_out, const int8_t *buffer_in, size_t nframes) {
+ for(;nframes--;) {
+ int16_t sample = (*buffer_in++) << 8;
+ *buffer_out++ = sample;
+ *buffer_out++ = sample;
+ }
+}
+
+
+void audiosample_convert_s8s_s16s(int16_t *buffer_out, const int8_t *buffer_in, size_t nframes) {
+ size_t nsamples = 2*nframes;
+ for(;nsamples--;) {
+ int16_t sample = (*buffer_in++) << 8;
+ *buffer_out++ = sample;
+ }
+}
+
+
+void audiosample_convert_u16m_s16s(int16_t *buffer_out, const uint16_t *buffer_in, size_t nframes) {
+ for(;nframes--;) {
+ int16_t sample = *buffer_in++ - 0x8000;
+ *buffer_out++ = sample;
+ *buffer_out++ = sample;
+ }
+}
+
+
+void audiosample_convert_u16s_s16s(int16_t *buffer_out, const uint16_t *buffer_in, size_t nframes) {
+ size_t nsamples = 2*nframes;
+ for(;nsamples--;) {
+ int16_t sample = *buffer_in++ - 0x8000;
+ *buffer_out++ = sample;
+ }
+}
+
+void audiosample_convert_s16m_s16s(int16_t *buffer_out, const int16_t *buffer_in, size_t nframes) {
+ for(;nframes--;) {
+ int16_t sample = *buffer_in++;
+ *buffer_out++ = sample;
+ *buffer_out++ = sample;
+ }
+}
diff --git a/shared-module/audiocore/__init__.h b/shared-module/audiocore/__init__.h
index 7a56454b8..ec2cf1cfa 100644
--- a/shared-module/audiocore/__init__.h
+++ b/shared-module/audiocore/__init__.h
@@ -74,4 +74,12 @@ void audiosample_get_buffer_structure(mp_obj_t sample_obj, bool single_channel,
bool* single_buffer, bool* samples_signed,
uint32_t* max_buffer_length, uint8_t* spacing);
+void audiosample_convert_u8m_s16s(int16_t *buffer_out, const uint8_t *buffer_in, size_t nframes);
+void audiosample_convert_u8s_s16s(int16_t *buffer_out, const uint8_t *buffer_in, size_t nframes);
+void audiosample_convert_s8m_s16s(int16_t *buffer_out, const int8_t *buffer_in, size_t nframes);
+void audiosample_convert_s8s_s16s(int16_t *buffer_out, const int8_t *buffer_in, size_t nframes);
+void audiosample_convert_u16m_s16s(int16_t *buffer_out, const uint16_t *buffer_in, size_t nframes);
+void audiosample_convert_u16s_s16s(int16_t *buffer_out, const uint16_t *buffer_in, size_t nframes);
+void audiosample_convert_s16m_s16s(int16_t *buffer_out, const int16_t *buffer_in, size_t nframes);
+
#endif // MICROPY_INCLUDED_SHARED_MODULE_AUDIOCORE__INIT__H