Thanks to visit codestin.com
Credit goes to github.com

Skip to content

esp32: Add I2S peripheral support. #3489

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions ports/esp32/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ SRC_C = \
moduhashlib.c \
espneopixel.c \
machine_hw_spi.c \
machine_i2s.c \
machine_wdt.c \
mpthreadport.c \
$(SRC_MOD)
Expand Down Expand Up @@ -214,6 +215,7 @@ ESPIDF_DRIVER_O = $(addprefix $(ESPCOMP)/driver/,\
timer.o \
spi_master.o \
spi_common.o \
i2s.o \
rtc_module.o \
)

Expand Down
141 changes: 141 additions & 0 deletions ports/esp32/machine_i2s.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Ayke van Laethem
*
* 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 "machine_i2s.h"
#include "py/runtime.h"
#include "py/mperrno.h"

STATIC const machine_i2s_obj_t machine_i2s_obj_0 = {
{ &machine_i2s_type },
.port = I2S_NUM_0,
};

STATIC const machine_i2s_obj_t machine_i2s_obj_1 = {
{ &machine_i2s_type },
.port = I2S_NUM_1,
};

STATIC void machine_i2s_obj_init_helper(const machine_i2s_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_bclk, ARG_ws, ARG_data_out, ARG_data_in, ARG_samplerate, ARG_bitdepth };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_bclk, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_ws, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_data_out, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_data_in, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_samplerate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 48000} },
{ MP_QSTR_bitdepth, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 16} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

i2s_config_t config = {
.mode = I2S_MODE_MASTER | I2S_MODE_TX,
.sample_rate = args[ARG_samplerate].u_int,
.bits_per_sample = args[ARG_bitdepth].u_int,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 8,
.dma_buf_len = 64,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've noticed this must be configurable somehow. For example, for stereo 48kHz 20ms packets (Opus) it needs to be at least 120.

.use_apll = 0,
};
if (i2s_driver_install(self->port, &config, 0, NULL) != ESP_OK) {
mp_raise_ValueError("I2S: failed to enable");
}

i2s_pin_config_t pin_config = {
.bck_io_num = args[ARG_bclk].u_int,
.ws_io_num = args[ARG_ws].u_int,
.data_out_num = args[ARG_data_out].u_int,
.data_in_num = args[ARG_data_in].u_int,
};
if (i2s_set_pin(self->port, &pin_config) != ESP_OK) {
i2s_driver_uninstall(self->port);
mp_raise_ValueError("I2S: failed to set pins in");
}
}

STATIC mp_obj_t machine_i2s_obj_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
machine_i2s_obj_init_helper(args[0], n_args - 1, args + 1, kw_args);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(machine_i2s_init_obj, 1, machine_i2s_obj_init);

STATIC mp_obj_t machine_i2s_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_int_t id = 0;
if (n_args > 0) {
id = mp_obj_get_int(args[0]);
n_args--;
args++;
}
const machine_i2s_obj_t *self;
if (id == 0) {
self = &machine_i2s_obj_0;
} else if (id == 1) {
self = &machine_i2s_obj_1;
} else {
mp_raise_ValueError("invalid I2S peripheral");
}
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
machine_i2s_obj_init_helper(self, n_args, args, &kw_args);
return (machine_i2s_obj_t*)self; // discard const
}

STATIC mp_obj_t machine_i2s_obj_deinit(mp_obj_t self_in) {
const machine_i2s_obj_t *self = MP_OBJ_TO_PTR(self_in);
i2s_driver_uninstall(self->port);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(machine_i2s_deinit_obj, machine_i2s_obj_deinit);

STATIC mp_obj_t machine_i2s_obj_write(mp_obj_t self_in, mp_obj_t buf_in) {
const machine_i2s_obj_t *self = MP_OBJ_TO_PTR(self_in);

mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);

if (i2s_write_bytes(self->port, bufinfo.buf, bufinfo.len, portMAX_DELAY) == ESP_FAIL) {
mp_raise_OSError(MP_EIO);
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(machine_i2s_write_obj, machine_i2s_obj_write);

STATIC const mp_rom_map_elem_t machine_i2s_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_i2s_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_i2s_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&machine_i2s_write_obj) },
};

STATIC MP_DEFINE_CONST_DICT(machine_i2s_locals_dict, machine_i2s_locals_dict_table);

const mp_obj_type_t machine_i2s_type = {
{ &mp_type_type },
.name = MP_QSTR_I2S,
.make_new = machine_i2s_make_new,
.locals_dict = (mp_obj_dict_t*)&machine_i2s_locals_dict,
};
40 changes: 40 additions & 0 deletions ports/esp32/machine_i2s.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Ayke van Laethem
*
* 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.
*/

#ifndef MICROPY_INCLUDED_ESP32_MACHINE_I2S_H
#define MICROPY_INCLUDED_ESP32_MACHINE_I2S_H

#include "py/obj.h"
#include "driver/i2s.h"

typedef struct {
mp_obj_base_t base;
i2s_port_t port;
} machine_i2s_obj_t;

extern const mp_obj_type_t machine_i2s_type;

#endif // MICROPY_INCLUDED_ESP32_MACHINE_I2S_H
1 change: 1 addition & 0 deletions ports/esp32/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
{ MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) },
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
{ MP_ROM_QSTR(MP_QSTR_I2S), MP_ROM_PTR(&machine_i2s_type) },
{ MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) },
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) },
Expand Down
1 change: 1 addition & 0 deletions ports/esp32/modmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern const mp_obj_type_t machine_adc_type;
extern const mp_obj_type_t machine_dac_type;
extern const mp_obj_type_t machine_pwm_type;
extern const mp_obj_type_t machine_hw_spi_type;
extern const mp_obj_type_t machine_i2s_type;
extern const mp_obj_type_t machine_uart_type;

void machine_pins_init(void);
Expand Down