Skip to content
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

External I2S pin configuration (AUD-1392) #360

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 23 additions & 4 deletions components/audio_board/include/board_pins_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#include "driver/i2c.h"
#include "driver/i2s.h"
#include "driver/gpio.h"
#include "driver/ledc.h"
#include "driver/spi_common.h"
#include "driver/spi_master.h"
#include "driver/spi_slave.h"
Expand Down Expand Up @@ -72,20 +74,37 @@ esp_err_t get_i2s_pins(i2s_port_t port, i2s_pin_config_t *i2s_config);
esp_err_t get_spi_pins(spi_bus_config_t *spi_config, spi_device_interface_config_t *spi_device_interface_config);

/**
* @brief Set i2s mclk output pin
* @brief Enables i2s mclk output pin
*
* @note GPIO1 and GPIO3 default are UART pins.
*
* @param i2s_num i2s port index
* @param gpio_num gpio number index, only support GPIO0, GPIO1 and GPIO3.
* @param i2s_num i2s port index
* @param i2s_config i2s configuration
* @param mclk_gpio_num mclk gpio number index (only support GPIO0, GPIO1 and GPIO3 with I2S peripheral, others are supported via ledc component)

* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
* - ESP_ERR_INVALID_STATE Driver state error
* - ESP_ERR_ADF_NOT_SUPPORT Not support
*/
esp_err_t i2s_mclk_gpio_select(i2s_port_t i2s_num, gpio_num_t gpio_num);
esp_err_t i2s_mclk_gpio_enable(i2s_port_t i2s_num, i2s_config_t i2s_config, gpio_num_t mclk_gpio_num);

/**
* @brief Disables i2s mclk output pin
*
* @note GPIO1 and GPIO3 default are UART pins.
*
* @param i2s_num i2s port index
* @param mclk_gpio_num MCLK gpio number index

* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
* - ESP_ERR_INVALID_STATE Driver state error
* - ESP_ERR_ADF_NOT_SUPPORT Not support
*/
esp_err_t i2s_mclk_gpio_disable(i2s_port_t i2s_num, gpio_num_t mclk_gpio_num);

/**
* @brief Get the gpio number for sdcard interrupt
Expand Down
21 changes: 13 additions & 8 deletions components/audio_board/lyrat_mini_v1_1/board_pins_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,33 +85,33 @@ esp_err_t get_spi_pins(spi_bus_config_t *spi_config, spi_device_interface_config
return ESP_OK;
}

esp_err_t i2s_mclk_gpio_select(i2s_port_t i2s_num, gpio_num_t gpio_num)
esp_err_t i2s_mclk_gpio_enable(i2s_port_t i2s_num, gpio_num_t mclk_gpio_num)
{
if (i2s_num >= I2S_NUM_MAX) {
ESP_LOGE(TAG, "Does not support i2s number(%d)", i2s_num);
return ESP_ERR_INVALID_ARG;
}
if (gpio_num != GPIO_NUM_0 && gpio_num != GPIO_NUM_1 && gpio_num != GPIO_NUM_3) {
ESP_LOGE(TAG, "Only support GPIO0/GPIO1/GPIO3, gpio_num:%d", gpio_num);
if (mclk_gpio_num != GPIO_NUM_0 && mclk_gpio_num != GPIO_NUM_1 && mclk_gpio_num != GPIO_NUM_3) {
ESP_LOGE(TAG, "Only support GPIO0/GPIO1/GPIO3, gpio_num:%d", mclk_gpio_num);
return ESP_ERR_INVALID_ARG;
}
ESP_LOGI(TAG, "I2S%d, MCLK output by GPIO%d", i2s_num, gpio_num);
ESP_LOGI(TAG, "I2S%d, MCLK output by GPIO%d", i2s_num, mclk_gpio_num);
if (i2s_num == I2S_NUM_0) {
if (gpio_num == GPIO_NUM_0) {
if (mclk_gpio_num == GPIO_NUM_0) {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
WRITE_PERI_REG(PIN_CTRL, 0xFFF0);
} else if (gpio_num == GPIO_NUM_1) {
} else if (mclk_gpio_num == GPIO_NUM_1) {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD_CLK_OUT3);
WRITE_PERI_REG(PIN_CTRL, 0xF0F0);
} else {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD_CLK_OUT2);
WRITE_PERI_REG(PIN_CTRL, 0xFF00);
}
} else if (i2s_num == I2S_NUM_1) {
if (gpio_num == GPIO_NUM_0) {
if (mclk_gpio_num == GPIO_NUM_0) {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
WRITE_PERI_REG(PIN_CTRL, 0xFFFF);
} else if (gpio_num == GPIO_NUM_1) {
} else if (mclk_gpio_num == GPIO_NUM_1) {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD_CLK_OUT3);
WRITE_PERI_REG(PIN_CTRL, 0xF0FF);
} else {
Expand All @@ -122,6 +122,11 @@ esp_err_t i2s_mclk_gpio_select(i2s_port_t i2s_num, gpio_num_t gpio_num)
return ESP_OK;
}

esp_err_t i2s_mclk_gpio_disable(i2s_port_t i2s_num, gpio_num_t mclk_gpio_num)
{
return ESP_OK;
}

// sdcard

int8_t get_sdcard_intr_gpio(void)
Expand Down
21 changes: 13 additions & 8 deletions components/audio_board/lyrat_v4_2/board_pins_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,33 @@ esp_err_t get_spi_pins(spi_bus_config_t *spi_config, spi_device_interface_config
return ESP_OK;
}

esp_err_t i2s_mclk_gpio_select(i2s_port_t i2s_num, gpio_num_t gpio_num)
esp_err_t i2s_mclk_gpio_enable(i2s_port_t i2s_num, gpio_num_t mclk_gpio_num)
{
if (i2s_num >= I2S_NUM_MAX) {
ESP_LOGE(TAG, "Does not support i2s number(%d)", i2s_num);
return ESP_ERR_INVALID_ARG;
}
if (gpio_num != GPIO_NUM_0 && gpio_num != GPIO_NUM_1 && gpio_num != GPIO_NUM_3) {
ESP_LOGE(TAG, "Only support GPIO0/GPIO1/GPIO3, gpio_num:%d", gpio_num);
if (mclk_gpio_num != GPIO_NUM_0 && mclk_gpio_num != GPIO_NUM_1 && mclk_gpio_num != GPIO_NUM_3) {
ESP_LOGE(TAG, "Only support GPIO0/GPIO1/GPIO3, gpio_num:%d", mclk_gpio_num);
return ESP_ERR_INVALID_ARG;
}
ESP_LOGI(TAG, "I2S%d, MCLK output by GPIO%d", i2s_num, gpio_num);
ESP_LOGI(TAG, "I2S%d, MCLK output by GPIO%d", i2s_num, mclk_gpio_num);
if (i2s_num == I2S_NUM_0) {
if (gpio_num == GPIO_NUM_0) {
if (mclk_gpio_num == GPIO_NUM_0) {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
WRITE_PERI_REG(PIN_CTRL, 0xFFF0);
} else if (gpio_num == GPIO_NUM_1) {
} else if (mclk_gpio_num == GPIO_NUM_1) {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD_CLK_OUT3);
WRITE_PERI_REG(PIN_CTRL, 0xF0F0);
} else {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD_CLK_OUT2);
WRITE_PERI_REG(PIN_CTRL, 0xFF00);
}
} else if (i2s_num == I2S_NUM_1) {
if (gpio_num == GPIO_NUM_0) {
if (mclk_gpio_num == GPIO_NUM_0) {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
WRITE_PERI_REG(PIN_CTRL, 0xFFFF);
} else if (gpio_num == GPIO_NUM_1) {
} else if (mclk_gpio_num == GPIO_NUM_1) {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD_CLK_OUT3);
WRITE_PERI_REG(PIN_CTRL, 0xF0FF);
} else {
Expand All @@ -116,6 +116,11 @@ esp_err_t i2s_mclk_gpio_select(i2s_port_t i2s_num, gpio_num_t gpio_num)
return ESP_OK;
}

esp_err_t i2s_mclk_gpio_disable(i2s_port_t i2s_num, gpio_num_t mclk_gpio_num)
{
return ESP_OK;
}

// sdcard

int8_t get_sdcard_intr_gpio(void)
Expand Down
21 changes: 13 additions & 8 deletions components/audio_board/lyrat_v4_3/board_pins_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,33 @@ esp_err_t get_spi_pins(spi_bus_config_t *spi_config, spi_device_interface_config
return ESP_OK;
}

esp_err_t i2s_mclk_gpio_select(i2s_port_t i2s_num, gpio_num_t gpio_num)
esp_err_t i2s_mclk_gpio_enable(i2s_port_t i2s_num, gpio_num_t mclk_gpio_num)
{
if (i2s_num >= I2S_NUM_MAX) {
ESP_LOGE(TAG, "Does not support i2s number(%d)", i2s_num);
return ESP_ERR_INVALID_ARG;
}
if (gpio_num != GPIO_NUM_0 && gpio_num != GPIO_NUM_1 && gpio_num != GPIO_NUM_3) {
ESP_LOGE(TAG, "Only support GPIO0/GPIO1/GPIO3, gpio_num:%d", gpio_num);
if (mclk_gpio_num != GPIO_NUM_0 && mclk_gpio_num != GPIO_NUM_1 && mclk_gpio_num != GPIO_NUM_3) {
ESP_LOGE(TAG, "Only support GPIO0/GPIO1/GPIO3, gpio_num:%d", mclk_gpio_num);
return ESP_ERR_INVALID_ARG;
}
ESP_LOGI(TAG, "I2S%d, MCLK output by GPIO%d", i2s_num, gpio_num);
ESP_LOGI(TAG, "I2S%d, MCLK output by GPIO%d", i2s_num, mclk_gpio_num);
if (i2s_num == I2S_NUM_0) {
if (gpio_num == GPIO_NUM_0) {
if (mclk_gpio_num == GPIO_NUM_0) {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
WRITE_PERI_REG(PIN_CTRL, 0xFFF0);
} else if (gpio_num == GPIO_NUM_1) {
} else if (mclk_gpio_num == GPIO_NUM_1) {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD_CLK_OUT3);
WRITE_PERI_REG(PIN_CTRL, 0xF0F0);
} else {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD_CLK_OUT2);
WRITE_PERI_REG(PIN_CTRL, 0xFF00);
}
} else if (i2s_num == I2S_NUM_1) {
if (gpio_num == GPIO_NUM_0) {
if (mclk_gpio_num == GPIO_NUM_0) {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
WRITE_PERI_REG(PIN_CTRL, 0xFFFF);
} else if (gpio_num == GPIO_NUM_1) {
} else if (mclk_gpio_num == GPIO_NUM_1) {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD_CLK_OUT3);
WRITE_PERI_REG(PIN_CTRL, 0xF0FF);
} else {
Expand All @@ -116,6 +116,11 @@ esp_err_t i2s_mclk_gpio_select(i2s_port_t i2s_num, gpio_num_t gpio_num)
return ESP_OK;
}

esp_err_t i2s_mclk_gpio_disable(i2s_port_t i2s_num, gpio_num_t mclk_gpio_num)
{
return ESP_OK;
}

// sdcard

int8_t get_sdcard_intr_gpio(void)
Expand Down
7 changes: 6 additions & 1 deletion components/audio_board/lyratd_msc_v2_1/board_pins_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,16 @@ esp_err_t get_spi_pins(spi_bus_config_t *spi_config, spi_device_interface_config
return ESP_OK;
}

esp_err_t i2s_mclk_gpio_select(i2s_port_t i2s_num, gpio_num_t gpio_num)
esp_err_t i2s_mclk_gpio_enable(i2s_port_t i2s_num, gpio_num_t mclk_gpio_num)
{
return ESP_ERR_ADF_NOT_SUPPORT;
}

esp_err_t i2s_mclk_gpio_disable(i2s_port_t i2s_num, gpio_num_t mclk_gpio_num)
{
return ESP_ERR_ADF_NOT_SUPPORT;
}

// sdcard

int8_t get_sdcard_intr_gpio(void)
Expand Down
7 changes: 6 additions & 1 deletion components/audio_board/lyratd_msc_v2_2/board_pins_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,16 @@ esp_err_t get_spi_pins(spi_bus_config_t *spi_config, spi_device_interface_config
return ESP_OK;
}

esp_err_t i2s_mclk_gpio_select(i2s_port_t i2s_num, gpio_num_t gpio_num)
esp_err_t i2s_mclk_gpio_enable(i2s_port_t i2s_num, gpio_num_t mclk_gpio_num)
{
return ESP_ERR_ADF_NOT_SUPPORT;
}

esp_err_t i2s_mclk_gpio_disable(i2s_port_t i2s_num, gpio_num_t mclk_gpio_num)
{
return ESP_ERR_ADF_NOT_SUPPORT;
}

// sdcard

int8_t get_sdcard_intr_gpio(void)
Expand Down
6 changes: 4 additions & 2 deletions components/audio_hal/test/test_audio_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
#include "zl38063.h"


#define TEST_I2S_NUM I2S_NUM_0
#define TEST_I2S_NUM I2S_NUM_0
#define TEST_I2S_MCLK_GPIO GPIO_NUM_0

static const char *TAG = "TEST_AUDIO_HAL";

Expand All @@ -59,11 +60,12 @@ static void i2s_init()
i2s_pin_config_t i2s_pin_cfg = {0};
get_i2s_pins(TEST_I2S_NUM, &i2s_pin_cfg);
i2s_set_pin(TEST_I2S_NUM, &i2s_pin_cfg);
i2s_mclk_gpio_select(TEST_I2S_NUM, GPIO_NUM_0);
i2s_mclk_gpio_enable(TEST_I2S_NUM, TEST_I2S_MCLK_GPIO);
}

static void i2s_deinit()
{
i2s_mclk_gpio_disable(TEST_I2S_NUM, TEST_I2S_MCLK_GPIO);
i2s_driver_uninstall(TEST_I2S_NUM);
}

Expand Down
14 changes: 10 additions & 4 deletions components/audio_stream/i2s_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ static esp_err_t _i2s_open(audio_element_handle_t self)
static esp_err_t _i2s_destroy(audio_element_handle_t self)
{
i2s_stream_t *i2s = (i2s_stream_t *)audio_element_getdata(self);
i2s_mclk_gpio_disable(i2s->config.i2s_port, i2s->config.mclk_gpio_num);
if (i2s->uninstall_drv) {
i2s_driver_uninstall(i2s->config.i2s_port);
}
Expand Down Expand Up @@ -296,6 +297,7 @@ esp_err_t i2s_stream_set_clk(audio_element_handle_t i2s_stream, int rate, int bi
i2s_info.sample_rates = rate;
audio_element_setinfo(i2s_stream, &i2s_info);

i2s_mclk_gpio_enable(i2s->config.i2s_port, i2s->config.i2s_config, i2s->config.mclk_gpio_num);
if (i2s_set_clk(i2s->config.i2s_port, rate, bits, ch) == ESP_FAIL) {
ESP_LOGE(TAG, "i2s_set_clk failed, type = %d,port:%d", i2s->config.type, i2s->config.i2s_port);
err = ESP_FAIL;
Expand Down Expand Up @@ -383,11 +385,15 @@ audio_element_handle_t i2s_stream_init(i2s_stream_cfg_t *config)
} else
#endif
{
i2s_pin_config_t i2s_pin_cfg = {0};
get_i2s_pins(i2s->config.i2s_port, &i2s_pin_cfg);
i2s_set_pin(i2s->config.i2s_port, &i2s_pin_cfg);
if (i2s->config.i2s_pin_config.bck_io_num == GPIO_NUM_NC &&
i2s->config.i2s_pin_config.ws_io_num == GPIO_NUM_NC &&
i2s->config.i2s_pin_config.data_out_num == GPIO_NUM_NC &&
i2s->config.i2s_pin_config.data_in_num == GPIO_NUM_NC) {
get_i2s_pins(i2s->config.i2s_port, &i2s->config.i2s_pin_config);
}
i2s_set_pin(i2s->config.i2s_port, &i2s->config.i2s_pin_config);
}
i2s_mclk_gpio_select(i2s->config.i2s_port, GPIO_NUM_0);
i2s_mclk_gpio_enable(i2s->config.i2s_port, i2s->config.i2s_config, i2s->config.mclk_gpio_num);

return el;
}
10 changes: 10 additions & 0 deletions components/audio_stream/include/i2s_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#ifndef _I2S_STREAM_WRITER_H_
#define _I2S_STREAM_WRITER_H_

#include "driver/gpio.h"
#include "driver/i2s.h"
#include "audio_common.h"
#include "audio_error.h"
Expand All @@ -41,6 +42,8 @@ typedef struct {
audio_stream_type_t type; /*!< Type of stream */
i2s_config_t i2s_config; /*!< I2S driver configurations */
i2s_port_t i2s_port; /*!< I2S driver hardware port */
i2s_pin_config_t i2s_pin_config; /*!< I2S pin configurations */
gpio_num_t mclk_gpio_num; /*!< MCLK gpio number */
bool use_alc; /*!< It is a flag for ALC. If use ALC, the value is true. Or the value is false */
int volume; /*!< The volume of audio input data will be set. */
int out_rb_size; /*!< Size of output ringbuffer */
Expand Down Expand Up @@ -76,6 +79,13 @@ typedef struct {
.tx_desc_auto_clear = true, \
}, \
.i2s_port = 0, \
.i2s_pin_config = { \
.bck_io_num = GPIO_NUM_NC, \
.ws_io_num = GPIO_NUM_NC, \
.data_out_num = GPIO_NUM_NC, \
.data_in_num = GPIO_NUM_NC, \
}, \
.mclk_gpio_num = GPIO_NUM_0, \
.use_alc = false, \
.volume = 0, \
.multi_out_num = 0, \
Expand Down
Loading