esp32s2: add I2SOut

This commit is contained in:
Jeff Epler 2020-10-30 10:05:16 -05:00
parent 2cd377f1a7
commit a7542598a0
13 changed files with 545 additions and 1 deletions

View File

@ -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 \

View File

@ -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

View File

@ -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

231
ports/esp32s2/i2s_common.c Normal file
View File

@ -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));
}
}

View File

@ -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);

View File

@ -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

View File

@ -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();

View File

@ -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;
}
}

View File

@ -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