From e78e5a4df79d35aeadf217f1f64b0b1a1f9b204e Mon Sep 17 00:00:00 2001 From: philippe44 Date: Mon, 15 May 2023 15:29:16 +0200 Subject: [PATCH] prepare 4.4 migration + esp32s3 + mck on 0,1,3 --- build-scripts/I2S-4MFlash-sdkconfig.defaults | 1 + build-scripts/Muse-sdkconfig.defaults | 1 + build-scripts/SqueezeAmp-sdkconfig.defaults | 1 + components/platform_console/cmd_ota.c | 1 - components/platform_console/cmd_system.c | 5 +- components/squeezelite/ac101/ac101.c | 4 +- components/squeezelite/adac.h | 2 +- components/squeezelite/adac_core.c | 10 +--- .../squeezelite/external/dac_external.c | 10 ++-- components/squeezelite/output_i2s.c | 49 ++++++++++++++++--- components/squeezelite/slimproto.c | 2 +- components/squeezelite/squeezelite.h | 3 +- components/squeezelite/tas57xx/dac_5713.c | 4 +- components/squeezelite/tas57xx/dac_57xx.c | 4 +- components/squeezelite/wm8978/wm8978.c | 13 ++--- components/wifi-manager/network_status.c | 4 ++ main/Kconfig.projbuild | 5 ++ 17 files changed, 75 insertions(+), 44 deletions(-) diff --git a/build-scripts/I2S-4MFlash-sdkconfig.defaults b/build-scripts/I2S-4MFlash-sdkconfig.defaults index 7e6a9138..20e876ba 100644 --- a/build-scripts/I2S-4MFlash-sdkconfig.defaults +++ b/build-scripts/I2S-4MFlash-sdkconfig.defaults @@ -216,6 +216,7 @@ CONFIG_TARGET="" # I2S settings # CONFIG_I2S_NUM=0 +CONFIG_I2S_MCK_IO=-1 CONFIG_I2S_BCK_IO=-1 CONFIG_I2S_WS_IO=-1 CONFIG_I2S_DO_IO=-1 diff --git a/build-scripts/Muse-sdkconfig.defaults b/build-scripts/Muse-sdkconfig.defaults index d001b283..76bad083 100644 --- a/build-scripts/Muse-sdkconfig.defaults +++ b/build-scripts/Muse-sdkconfig.defaults @@ -217,6 +217,7 @@ CONFIG_TARGET="muse" # I2S settings # CONFIG_I2S_NUM=0 +CONFIG_I2S_MCK_IO=-1 CONFIG_I2S_BCK_IO=-1 CONFIG_I2S_WS_IO=-1 CONFIG_I2S_DO_IO=-1 diff --git a/build-scripts/SqueezeAmp-sdkconfig.defaults b/build-scripts/SqueezeAmp-sdkconfig.defaults index 2f37e09a..1b5fdd5d 100644 --- a/build-scripts/SqueezeAmp-sdkconfig.defaults +++ b/build-scripts/SqueezeAmp-sdkconfig.defaults @@ -209,6 +209,7 @@ CONFIG_TARGET="squeezeamp" # I2S settings # CONFIG_I2S_NUM=0 +CONFIG_I2S_MCK_IO=-1 CONFIG_I2S_BCK_IO=-1 CONFIG_I2S_WS_IO=-1 CONFIG_I2S_DO_IO=-1 diff --git a/components/platform_console/cmd_ota.c b/components/platform_console/cmd_ota.c index 634f120f..ed711d59 100644 --- a/components/platform_console/cmd_ota.c +++ b/components/platform_console/cmd_ota.c @@ -23,7 +23,6 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "soc/rtc_cntl_reg.h" -#include "esp32/rom/uart.h" #include "sdkconfig.h" #include "platform_console.h" #include "messaging.h" diff --git a/components/platform_console/cmd_system.c b/components/platform_console/cmd_system.c index 3d6cfb4a..45df5192 100644 --- a/components/platform_console/cmd_system.c +++ b/components/platform_console/cmd_system.c @@ -20,7 +20,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "soc/rtc_cntl_reg.h" -#include "esp32/rom/uart.h" +#include "esp_rom_uart.h" #include "cmd_system.h" #include "sdkconfig.h" #include "esp_partition.h" @@ -28,7 +28,6 @@ #include "platform_esp32.h" #include "platform_config.h" #include "esp_sleep.h" -#include "driver/uart.h" // for the uart driver access #include "messaging.h" #include "platform_console.h" #include "tools.h" @@ -791,7 +790,7 @@ static int light_sleep(int argc, char **argv) ESP_ERROR_CHECK( esp_sleep_enable_uart_wakeup(CONFIG_ESP_CONSOLE_UART_NUM) ); } fflush(stdout); - uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM); + esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM); esp_light_sleep_start(); esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause(); const char *cause_str; diff --git a/components/squeezelite/ac101/ac101.c b/components/squeezelite/ac101/ac101.c index 51944998..bc64378f 100644 --- a/components/squeezelite/ac101/ac101.c +++ b/components/squeezelite/ac101/ac101.c @@ -48,7 +48,7 @@ static const char TAG[] = "AC101"; return b;\ } -static bool init(char *config, int i2c_port, i2s_config_t *i2s_config); +static bool init(char *config, int i2c_port, i2s_config_t *i2s_config, bool *mck); static void speaker(bool active); static void headset(bool active); static bool volume(unsigned left, unsigned right); @@ -64,7 +64,7 @@ static void ac101_set_spk_volume(uint8_t volume); /**************************************************************************************** * init */ -static bool init(char *config, int i2c_port, i2s_config_t *i2s_config) { +static bool init(char *config, int i2c_port, i2s_config_t *i2s_config, bool *mck) { adac_init(config, i2c_port); if (adac_read_word(AC101_ADDR, CHIP_AUDIO_RS) == 0xffff) { ESP_LOGW(TAG, "No AC101 detected"); diff --git a/components/squeezelite/adac.h b/components/squeezelite/adac.h index 5a3a8cf6..1ab7790e 100644 --- a/components/squeezelite/adac.h +++ b/components/squeezelite/adac.h @@ -17,7 +17,7 @@ typedef enum { ADAC_ON = 0, ADAC_STANDBY, ADAC_OFF } adac_power_e; struct adac_s { char *model; - bool (*init)(char *config, int i2c_port_num, i2s_config_t *i2s_config); + bool (*init)(char *config, int i2c_port_num, i2s_config_t *i2s_config, bool *mck); void (*deinit)(void); void (*power)(adac_power_e mode); void (*speaker)(bool active); diff --git a/components/squeezelite/adac_core.c b/components/squeezelite/adac_core.c index 27db6f58..ea72450c 100644 --- a/components/squeezelite/adac_core.c +++ b/components/squeezelite/adac_core.c @@ -29,16 +29,8 @@ static int i2c_port = -1; * init */ int adac_init(char *config, int i2c_port_num) { - char *p; int i2c_addr = 0; - - // some crappy codecs require MCLK to work - if ((p = strcasestr(config, "mck")) != NULL) { - ESP_LOGI(TAG, "Configuring MCLK on GPIO0"); - PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1); - REG_WRITE(PIN_CTRL, 0xFFFFFFF0); - } - + i2c_port = i2c_port_num; // configure i2c diff --git a/components/squeezelite/external/dac_external.c b/components/squeezelite/external/dac_external.c index d16c6d32..fd72c00c 100644 --- a/components/squeezelite/external/dac_external.c +++ b/components/squeezelite/external/dac_external.c @@ -24,7 +24,7 @@ static void speaker(bool active); static void headset(bool active); static bool volume(unsigned left, unsigned right) { return false; } static void power(adac_power_e mode); -static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config); +static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config, bool *mck); static bool i2c_json_execute(char *set); @@ -56,7 +56,7 @@ static struct { /**************************************************************************************** * init */ -static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config) { +static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config, bool *mck) { char *p; i2c_addr = adac_init(config, i2c_port_num); @@ -71,11 +71,7 @@ static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config) { int i; sscanf(p, "%*[^=]=%31[^,]", model); for (i = 0; *model && ((p = codecs[i].controlset) != NULL) && strcasecmp(codecs[i].model, model); i++); - if (p && codecs[i].mclk) { - ESP_LOGI(TAG, "Configuring MCLK on GPIO0"); - PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1); - REG_WRITE(PIN_CTRL, 0xFFFFFFF0); - } + if (p) *mck = codecs[i].mclk; } i2c_json = cJSON_Parse(p); diff --git a/components/squeezelite/output_i2s.c b/components/squeezelite/output_i2s.c index 9f06952d..f5dad4b4 100644 --- a/components/squeezelite/output_i2s.c +++ b/components/squeezelite/output_i2s.c @@ -190,10 +190,14 @@ static void set_amp_gpio(int gpio, char *value) { * Set pin from config string */ static void set_i2s_pin(char *config, i2s_pin_config_t *pin_config) { - pin_config->bck_io_num = pin_config->ws_io_num = pin_config->data_out_num = pin_config->data_in_num = -1; + pin_config->bck_io_num = pin_config->ws_io_num = pin_config->data_out_num = pin_config->data_in_num = -1; PARSE_PARAM(config, "bck", '=', pin_config->bck_io_num); PARSE_PARAM(config, "ws", '=', pin_config->ws_io_num); PARSE_PARAM(config, "do", '=', pin_config->data_out_num); +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0) + pin_config->mck_io_num = strcasestr(config, "mck") ? 0 : -1; + PARSE_PARAM(config, "mck", '=', pin_config->mck_io_num); +#endif } /**************************************************************************************** @@ -234,14 +238,19 @@ void output_init_i2s(log_level level, char *device, unsigned output_buf_size, ch ",ws=" STR(CONFIG_SPDIF_WS_IO) ",do=" STR(CONFIG_SPDIF_DO_IO)); char *dac_config = config_alloc_get_str("dac_config", CONFIG_DAC_CONFIG, "model=i2s,bck=" STR(CONFIG_I2S_BCK_IO) - ",ws=" STR(CONFIG_I2S_WS_IO) ",do=" STR(CONFIG_I2S_DO_IO) + ",ws=" STR(CONFIG_I2S_WS_IO) ",do=" STR(CONFIG_I2S_DO_IO) ",mck=" STR(CONFIG_I2S_MCK_IO) ",sda=" STR(CONFIG_I2C_SDA) ",scl=" STR(CONFIG_I2C_SCL) ",mute=" STR(CONFIG_MUTE_GPIO)); - i2s_pin_config_t i2s_dac_pin, i2s_spdif_pin; + i2s_pin_config_t i2s_dac_pin, i2s_spdif_pin; set_i2s_pin(spdif_config, &i2s_spdif_pin); set_i2s_pin(dac_config, &i2s_dac_pin); - + + if (i2s_dac_pin.data_out_num == -1 && i2s_spdif_pin.data_out_num == -1) { + LOG_WARN("DAC and SPDIF not configured, NOT launching i2s thread"); + return; + } + /* BEWARE: i2s.c must be patched otherwise L/R are swapped in 32 bits mode */ // common I2S initialization @@ -250,7 +259,9 @@ void output_init_i2s(log_level level, char *device, unsigned output_buf_size, ch i2s_config.communication_format = I2S_COMM_FORMAT_STAND_I2S; // in case of overflow, do not replay old buffer i2s_config.tx_desc_auto_clear = true; - i2s_config.use_apll = true; +#ifndef CONFIG_IDF_TARGET_ESP32S3 + i2s_config.use_apll = true; +#endif i2s_config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1; //Interrupt level 1 if (strcasestr(device, "spdif")) { @@ -303,12 +314,36 @@ void output_init_i2s(log_level level, char *device, unsigned output_buf_size, ch if ((p = strchr(mute, ':')) != NULL) mute_control.active = atoi(p + 1); } + bool mck_required = false; for (int i = 0; adac == &dac_external && dac_set[i]; i++) if (strcasestr(dac_set[i]->model, model)) adac = dac_set[i]; - res = adac->init(dac_config, I2C_PORT, &i2s_config) ? ESP_OK : ESP_FAIL; + res = adac->init(dac_config, I2C_PORT, &i2s_config, &mck_required) ? ESP_OK : ESP_FAIL; + +#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 4, 0) + int mck_io_num = strcasestr(dac_config, "mck") || mck_required ? 0 : -1; + PARSE_PARAM(dac_config, "mck", '=', mck_io_num); + LOG_INFO("configuring MCLK on GPIO %d", mck_io_num); + + if (mck_io_num == GPIO_NUM_0) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1); + WRITE_PERI_REG(PIN_CTRL, CONFIG_I2S_NUM == I2S_NUM_0 ? 0xFFF0 : 0xFFFF); + } else if (mck_io_num == GPIO_NUM_1) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD_CLK_OUT3); + WRITE_PERI_REG(PIN_CTRL, CONFIG_I2S_NUM == I2S_NUM_0 ? 0xF0F0 : 0xF0FF); + } else if (mck_io_num == GPIO_NUM_2) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD_CLK_OUT2); + WRITE_PERI_REG(PIN_CTRL, CONFIG_I2S_NUM == I2S_NUM_0 ? 0xFF00 : 0xFF0F); + } else { + LOG_WARN("invalid MCK gpio %d", mck_io_num); + } +#else + if (mck_required && i2s_dac_pin.mck_io_num == -1) i2s_dac_pin.mck_io_num = 0; + LOG_INFO("configuring MCLK on GPIO %d", i2s_dac_pin.mck_io_num); +#endif + res |= i2s_driver_install(CONFIG_I2S_NUM, &i2s_config, 0, NULL); res |= i2s_set_pin(CONFIG_I2S_NUM, &i2s_dac_pin); - + if (res == ESP_OK && mute_control.gpio >= 0) { gpio_pad_select_gpio(mute_control.gpio); gpio_set_direction(mute_control.gpio, GPIO_MODE_OUTPUT); diff --git a/components/squeezelite/slimproto.c b/components/squeezelite/slimproto.c index 146ac381..7a70614d 100644 --- a/components/squeezelite/slimproto.c +++ b/components/squeezelite/slimproto.c @@ -99,7 +99,7 @@ static char *new_server_cap; static char player_name[PLAYER_NAME_LEN + 1] = ""; static const char *name_file = NULL; -void send_packet(u8_t *packet, size_t len) { +void slimproto_send_packet(u8_t *packet, size_t len) { u8_t *ptr = packet; unsigned try = 0; ssize_t n; diff --git a/components/squeezelite/squeezelite.h b/components/squeezelite/squeezelite.h index 6703b794..60ac4f64 100644 --- a/components/squeezelite/squeezelite.h +++ b/components/squeezelite/squeezelite.h @@ -559,7 +559,8 @@ void buf_destroy(struct buffer *buf); void slimproto(log_level level, char *server, u8_t mac[6], const char *name, const char *namefile, const char *modelname, int maxSampleRate); void slimproto_stop(void); void wake_controller(void); -void send_packet(u8_t *packet, size_t len); +void slimproto_send_packet(u8_t *packet, size_t len); +#define send_packet(p, s) slimproto_send_packet(p,s) // stream.c typedef enum { STOPPED = 0, DISCONNECT, STREAMING_WAIT, diff --git a/components/squeezelite/tas57xx/dac_5713.c b/components/squeezelite/tas57xx/dac_5713.c index 7bc77f9d..1a45efac 100644 --- a/components/squeezelite/tas57xx/dac_5713.c +++ b/components/squeezelite/tas57xx/dac_5713.c @@ -39,7 +39,7 @@ static const char TAG[] = "TAS5713"; -static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config); +static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config, bool *mck); static void speaker(bool active) { }; static void headset(bool active) { } ; static bool volume(unsigned left, unsigned right); @@ -65,7 +65,7 @@ typedef enum { /**************************************************************************************** * init */ -static bool init(char *config, int i2c_port, i2s_config_t *i2s_config) { +static bool init(char *config, int i2c_port, i2s_config_t *i2s_config, bool *mck) { /* find if there is a tas5713 attached. Reg 0 should read non-zero but not 255 if so */ adac_init(config, i2c_port); if (adac_read_byte(TAS5713, 0x00) == 255) { diff --git a/components/squeezelite/tas57xx/dac_57xx.c b/components/squeezelite/tas57xx/dac_57xx.c index c689a0d6..45b073c8 100644 --- a/components/squeezelite/tas57xx/dac_57xx.c +++ b/components/squeezelite/tas57xx/dac_57xx.c @@ -23,7 +23,7 @@ static const char TAG[] = "TAS575x/8x"; -static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config); +static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config, bool *mck); static void speaker(bool active); static void headset(bool active); static bool volume(unsigned left, unsigned right); @@ -71,7 +71,7 @@ static int tas57_detect(void); /**************************************************************************************** * init */ -static bool init(char *config, int i2c_port, i2s_config_t *i2s_config) { +static bool init(char *config, int i2c_port, i2s_config_t *i2s_config, bool *mck) { // find which TAS we are using (if any) tas57_addr = adac_init(config, i2c_port); if (!tas57_addr) tas57_addr = tas57_detect(); diff --git a/components/squeezelite/wm8978/wm8978.c b/components/squeezelite/wm8978/wm8978.c index fd6b12f8..09621964 100644 --- a/components/squeezelite/wm8978/wm8978.c +++ b/components/squeezelite/wm8978/wm8978.c @@ -23,7 +23,7 @@ static void speaker(bool active) { } static void headset(bool active) { } static bool volume(unsigned left, unsigned right) { return false; } static void power(adac_power_e mode); -static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config); +static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config, bool *mck); static esp_err_t i2c_write_shadow(uint8_t reg, uint16_t val); static uint16_t i2c_read_shadow(uint8_t reg); @@ -47,12 +47,14 @@ static uint16_t WM8978_REGVAL_TBL[58] = { /**************************************************************************************** * init */ -static bool init(char *config, int i2c_port, i2s_config_t *i2s_config) { +static bool init(char *config, int i2c_port, i2s_config_t *i2s_config, bool *mck) { WM8978 = adac_init(config, i2c_port); if (!WM8978) WM8978 = 0x1a; ESP_LOGI(TAG, "WM8978 detected @%d", WM8978); - + + *mck = true; + // init sequence i2c_write_shadow(0, 0); i2c_write_shadow(4, 16); @@ -61,11 +63,6 @@ static bool init(char *config, int i2c_port, i2s_config_t *i2s_config) { i2c_write_shadow(43, 16); i2c_write_shadow(49, 102); - // Configure system clk to GPIO0 for DAC MCLK input - ESP_LOGI(TAG, "Configuring MCLK on GPIO0"); - PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1); - REG_WRITE(PIN_CTRL, 0xFFFFFFF0); - return true; } diff --git a/components/wifi-manager/network_status.c b/components/wifi-manager/network_status.c index 3080eb7f..cbc9d936 100644 --- a/components/wifi-manager/network_status.c +++ b/components/wifi-manager/network_status.c @@ -4,7 +4,9 @@ #include "network_status.h" #include +#ifdef BT_ENABLED #include "bt_app_core.h" +#endif #include "esp_log.h" #include "lwip/inet.h" #include "monitor.h" @@ -264,8 +266,10 @@ cJSON* network_status_get_basic_info(cJSON** old) { *old = network_status_update_float(old, "Voltage", battery_value_svc()); *old = network_update_cjson_number(old, "disconnect_count", nm->num_disconnect); *old = network_status_update_float(old, "avg_conn_time", nm->num_disconnect > 0 ? (nm->total_connected_time / nm->num_disconnect) : 0); +#ifdef BT_ENABLED *old = network_update_cjson_number(old, "bt_status", bt_app_source_get_a2d_state()); *old = network_update_cjson_number(old, "bt_sub_status", bt_app_source_get_media_state()); +#endif #if DEPTH == 16 *old = network_update_cjson_number(old, "depth", 16); #elif DEPTH == 32 diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index 4c7c1ef4..92f0f9c2 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -202,6 +202,11 @@ menu "Squeezelite-ESP32" default -1 help I2S data input gpio pin to use (not used mostly, leave it to -1). + config I2S_MCK_IO + int "I2S Master Clock GPIO number" + default -1 + help + I2S data output gpio pin to use (not used mostly, leave it to -1). endmenu menu "I2C settings" config I2C_SDA