From 0821551a2fd7d7e7d8f393d2cacb4f2b0449477e Mon Sep 17 00:00:00 2001 From: Wizmo2 Date: Mon, 5 Apr 2021 14:34:38 -0400 Subject: [PATCH 1/3] Add support for WM8978 i2c dac --- components/squeezelite/CMakeLists.txt | 2 +- components/squeezelite/adac.h | 1 + components/squeezelite/component.mk | 2 +- components/squeezelite/output_i2s.c | 2 +- components/squeezelite/wm8978/wm8978.c | 204 +++++++++++++++++++++++++ 5 files changed, 208 insertions(+), 3 deletions(-) create mode 100644 components/squeezelite/wm8978/wm8978.c diff --git a/components/squeezelite/CMakeLists.txt b/components/squeezelite/CMakeLists.txt index ce1d38f0..2af25280 100644 --- a/components/squeezelite/CMakeLists.txt +++ b/components/squeezelite/CMakeLists.txt @@ -1,4 +1,4 @@ -idf_component_register( SRC_DIRS . external ac101 tas57xx +idf_component_register( SRC_DIRS . external ac101 tas57xx wm8978 INCLUDE_DIRS . ac101 PRIV_REQUIRES codecs diff --git a/components/squeezelite/adac.h b/components/squeezelite/adac.h index 369b0071..1168c316 100644 --- a/components/squeezelite/adac.h +++ b/components/squeezelite/adac.h @@ -27,4 +27,5 @@ struct adac_s { extern const struct adac_s dac_tas57xx; extern const struct adac_s dac_tas5713; extern const struct adac_s dac_ac101; +extern const struct adac_s dac_wm8978; extern const struct adac_s dac_external; diff --git a/components/squeezelite/component.mk b/components/squeezelite/component.mk index 718baf19..9e896be7 100644 --- a/components/squeezelite/component.mk +++ b/components/squeezelite/component.mk @@ -20,7 +20,7 @@ CFLAGS += -O3 -DLINKALL -DLOOPBACK -DNO_FAAD -DRESAMPLE16 -DEMBEDDED -DTREMOR_ON # -I$(COMPONENT_PATH)/../codecs/inc/faad2 -COMPONENT_SRCDIRS := . tas57xx ac101 external +COMPONENT_SRCDIRS := . tas57xx ac101 external wm8978 COMPONENT_ADD_INCLUDEDIRS := . ./tas57xx ./ac101 COMPONENT_EMBED_FILES := vu.data diff --git a/components/squeezelite/output_i2s.c b/components/squeezelite/output_i2s.c index d554a3f6..60fd0f90 100644 --- a/components/squeezelite/output_i2s.c +++ b/components/squeezelite/output_i2s.c @@ -79,7 +79,7 @@ extern struct buffer *streambuf; extern struct buffer *outputbuf; extern u8_t *silencebuf; -const struct adac_s *dac_set[] = { &dac_tas57xx, &dac_tas5713, &dac_ac101, NULL }; +const struct adac_s *dac_set[] = { &dac_tas57xx, &dac_tas5713, &dac_ac101, &dac_wm8978, NULL }; const struct adac_s *adac = &dac_external; static log_level loglevel; diff --git a/components/squeezelite/wm8978/wm8978.c b/components/squeezelite/wm8978/wm8978.c new file mode 100644 index 00000000..5a139a84 --- /dev/null +++ b/components/squeezelite/wm8978/wm8978.c @@ -0,0 +1,204 @@ +/* + * Squeezelite for esp32 + * + * (c) Wizmo 2021 + * Sebastien 2019 + * Philippe G. 2019, philippe_44@outlook.com + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + * + */ + +#include +#include +#include +#include "driver/i2c.h" +#include "esp_log.h" +#include "cJSON.h" +#include "platform_config.h" +#include "adac.h" + +static const char TAG[] = "WM8978"; + +static void deinit(void) { } +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 i2c_json_execute(char *set); +static esp_err_t i2c_write_reg(uint8_t reg, uint16_t val); +static uint16_t i2c_read_reg(uint8_t reg); + +const struct adac_s dac_wm8978 = { "WM8978", init, deinit, power, speaker, headset, volume }; +static int i2c_port, i2c_addr; +static cJSON *i2c_json; +// default dac_controlset +static char* default_controlset = "{\"init\": [ {\"reg\":0,\"val\":0},{\"reg\":4,\"val\":16},{\"reg\":6,\"val\":0},{\"reg\":10,\"val\":8},{\"reg\":43,\"val\":16},{\"reg\":49,\"val\":102} ], \"poweron\": [ {\"reg\":1,\"val\":11},{\"reg\":2,\"val\":384},{\"reg\":3,\"val\":111} ], \"poweroff\": [ {\"reg\":1,\"val\":0},{\"reg\":2,\"val\":0},{\"reg\":3,\"val\":0} ]}"; +/* Note: Device supports headset and speaker volume/mute, but jack +// insertion is conrolled internaly and not connected gpio on t-audio to monitor. +// (currently jack_handler mutes headset at all times) +// "headseton": [ {"reg":52,"val":57},{"reg":53,"val":319} ], "headsetoff": [ {"reg":52,"val":121},{"reg":53,"val":383} ], "speakeron": [ {"reg":54,"val":57},{"reg":55,"val":319} ], "speakeroff": [ {"reg":54,"val":121},{"reg":55,"val":383} ]}"; +*/ + +// initiation table for non-readbale 9-bit i2c registers +static uint16_t WM8978_REGVAL_TBL[58] = + { + 0X0000, 0X0000, 0X0000, 0X0000, 0X0050, 0X0000, 0X0140, 0X0000, + 0X0000, 0X0000, 0X0000, 0X00FF, 0X00FF, 0X0000, 0X0100, 0X00FF, + 0X00FF, 0X0000, 0X012C, 0X002C, 0X002C, 0X002C, 0X002C, 0X0000, + 0X0032, 0X0000, 0X0000, 0X0000, 0X0000, 0X0000, 0X0000, 0X0000, + 0X0038, 0X000B, 0X0032, 0X0000, 0X0008, 0X000C, 0X0093, 0X00E9, + 0X0000, 0X0000, 0X0000, 0X0000, 0X0003, 0X0010, 0X0010, 0X0100, + 0X0100, 0X0002, 0X0001, 0X0001, 0X0039, 0X0039, 0X0039, 0X0039, + 0X0001, 0X0001}; + + +/**************************************************************************************** + * init + */ +static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config) { + char *p; + i2c_port = i2c_port_num; + + // configure i2c + i2c_config_t i2c_config = { + .mode = I2C_MODE_MASTER, + .sda_io_num = -1, + .sda_pullup_en = GPIO_PULLUP_ENABLE, + .scl_io_num = -1, + .scl_pullup_en = GPIO_PULLUP_ENABLE, + .master.clk_speed = 250000, + }; + + if ((p = strcasestr(config, "i2c")) != NULL) i2c_addr = atoi(strchr(p, '=') + 1); + if ((p = strcasestr(config, "sda")) != NULL) i2c_config.sda_io_num = atoi(strchr(p, '=') + 1); + if ((p = strcasestr(config, "scl")) != NULL) i2c_config.scl_io_num = atoi(strchr(p, '=') + 1); + + p = config_alloc_get_str("dac_controlset", CONFIG_DAC_CONTROLSET, default_controlset); + i2c_json = cJSON_Parse(p); + + if (!i2c_addr || !i2c_json || i2c_config.sda_io_num == -1 || i2c_config.scl_io_num == -1) { + if (p) free(p); + ESP_LOGW(TAG, "No i2c controlset found"); + return true; + } + + ESP_LOGI(TAG, "WM8978 uses IC2 @%d with sda:%d, scl:%d", i2c_addr, i2c_config.sda_io_num, i2c_config.scl_io_num); + + // we have an I2C configured + i2c_param_config(i2c_port, &i2c_config); + i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false); + + if (!i2c_json_execute("init")) { + ESP_LOGE(TAG, "could not intialize DAC"); + return false; + } + + // Configure system clk to GPIO0 for DAC MCLK input + ESP_LOGI(TAG, "Configuring MCLK on pin:%d", 0); + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1); + REG_WRITE(PIN_CTRL, 0xFFFFFFF0); + + return true; +} + +/**************************************************************************************** + * power + */ +static void power(adac_power_e mode) { + if (mode == ADAC_STANDBY || mode == ADAC_OFF) i2c_json_execute("poweroff"); + else i2c_json_execute("poweron"); +} + +/**************************************************************************************** + * speaker + */ +static void speaker(bool active) { + if (active) i2c_json_execute("speakeroff"); + else i2c_json_execute("speakeron"); +} + +/**************************************************************************************** + * headset + */ +static void headset(bool active) { + if (active) i2c_json_execute("headsetoff"); + else i2c_json_execute("headseton"); +} + +/**************************************************************************************** + * + */ +bool i2c_json_execute(char *set) { + cJSON *json_set = cJSON_GetObjectItemCaseSensitive(i2c_json, set); + cJSON *item; + + if (!json_set) return true; + + cJSON_ArrayForEach(item, json_set) + { + cJSON *reg = cJSON_GetObjectItemCaseSensitive(item, "reg"); + cJSON *val = cJSON_GetObjectItemCaseSensitive(item, "val"); + cJSON *mode = cJSON_GetObjectItemCaseSensitive(item, "mode"); + + if (!reg || !val) continue; + + if (!mode) { + i2c_write_reg(reg->valueint, val->valueint); + } else if (!strcasecmp(mode->valuestring, "or")) { + uint16_t data = i2c_read_reg(reg->valueint); + data |= (uint16_t) val->valueint; + i2c_write_reg(reg->valueint, data); + } else if (!strcasecmp(mode->valuestring, "and")) { + uint16_t data = i2c_read_reg(reg->valueint); + data &= (uint16_t) val->valueint; + i2c_write_reg(reg->valueint, data); + } + } + + return true; +} + +/**************************************************************************************** + * + */ +static esp_err_t i2c_write_reg(uint8_t reg, uint16_t val) { + esp_err_t ret; + uint8_t msb; + uint8_t lsb; + msb = (reg << 1) | ((val >> 8) & 0X01); + lsb = val & 0XFF; + + i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + i2c_master_start(cmd); + + i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_WRITE, I2C_MASTER_NACK); + i2c_master_write_byte(cmd, msb, I2C_MASTER_NACK); + i2c_master_write_byte(cmd, lsb, I2C_MASTER_NACK); + + i2c_master_stop(cmd); + ret = i2c_master_cmd_begin(i2c_port, cmd, 100 / portTICK_RATE_MS); + i2c_cmd_link_delete(cmd); + + if (ret != ESP_OK) { + ESP_LOGW(TAG, "I2C write failed"); + } else { + // update local register + WM8978_REGVAL_TBL[reg] = val; + ESP_LOGD(TAG, "I2C Write OK %d=%d (bytes %d %d)", reg, val, msb, lsb); + } + + return ret; +} + +/**************************************************************************************** + * Return local register value + */ +static uint16_t i2c_read_reg(uint8_t reg) { + return WM8978_REGVAL_TBL[reg]; +} + + From c311faa90f21024404ba877200d8d4bb91f97d6c Mon Sep 17 00:00:00 2001 From: Wizmo2 Date: Mon, 5 Apr 2021 21:39:16 -0400 Subject: [PATCH 2/3] Config changes for WM8978 support --- components/platform_console/cmd_config.c | 2 +- components/wifi-manager/webapp/mock/commands.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/components/platform_console/cmd_config.c b/components/platform_console/cmd_config.c index f3834832..5760647a 100644 --- a/components/platform_console/cmd_config.c +++ b/components/platform_console/cmd_config.c @@ -774,7 +774,7 @@ static char * get_log_level_options(const char * longname){ return options; } static void register_i2s_config(void){ - i2s_args.model_name = arg_str1(NULL,"model_name","TAS57xx|TAS5713|AC101|I2S","DAC Model Name"); + i2s_args.model_name = arg_str1(NULL,"model_name","TAS57xx|TAS5713|AC101|WM8978|I2S","DAC Model Name"); i2s_args.clear = arg_lit0(NULL, "clear", "Clear configuration"); i2s_args.clock = arg_int1(NULL,"clock","","Clock GPIO. e.g. 33"); i2s_args.wordselect = arg_int1(NULL,"wordselect","","Word Select GPIO. e.g. 25"); diff --git a/components/wifi-manager/webapp/mock/commands.json b/components/wifi-manager/webapp/mock/commands.json index b34e51a4..35cb07a3 100644 --- a/components/wifi-manager/webapp/mock/commands.json +++ b/components/wifi-manager/webapp/mock/commands.json @@ -124,7 +124,7 @@ "help": "DAC Options", "hascb": true, "argtable": [{ - "datatype": "TAS57xx|TAS5713|AC101|I2S", + "datatype": "TAS57xx|TAS5713|AC101|WM8978|I2S", "glossary": "DAC Model Name", "longopts": "model_name", "checkbox": false, @@ -202,7 +202,7 @@ "mincount": 0, "maxcount": 1 }], - "hint": " --model_name=TAS57xx|TAS5713|AC101|I2S --clock= --wordselect= --data= [--mute_gpio=] [--mute_level] [--dac_sda=] [--dac_scl=] [--dac_i2c=] [--clear]", + "hint": " --model_name=TAS57xx|TAS5713|AC101|WM8978|I2S --clock= --wordselect= --data= [--mute_gpio=] [--mute_level] [--dac_sda=] [--dac_scl=] [--dac_i2c=] [--clear]", "name": "cfg-hw-dac" }, { "help": "SPDIF Options", From 4d40355d5c76f11580697aafdf31114b802055c2 Mon Sep 17 00:00:00 2001 From: wizmo2 Date: Tue, 6 Apr 2021 11:14:21 -0400 Subject: [PATCH 3/3] Update wm8978.c --- components/squeezelite/wm8978/wm8978.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/squeezelite/wm8978/wm8978.c b/components/squeezelite/wm8978/wm8978.c index 5a139a84..9c5c92bb 100644 --- a/components/squeezelite/wm8978/wm8978.c +++ b/components/squeezelite/wm8978/wm8978.c @@ -117,16 +117,16 @@ static void power(adac_power_e mode) { * speaker */ static void speaker(bool active) { - if (active) i2c_json_execute("speakeroff"); - else i2c_json_execute("speakeron"); + if (active) i2c_json_execute("speakeron"); + else i2c_json_execute("speakeroff"); } /**************************************************************************************** * headset */ static void headset(bool active) { - if (active) i2c_json_execute("headsetoff"); - else i2c_json_execute("headseton"); + if (active) i2c_json_execute("headseton"); + else i2c_json_execute("headsetoff"); } /****************************************************************************************