浏览代码

refactor DAC handling

Philippe G 4 年之前
父节点
当前提交
9fd792cf98

+ 1 - 5
build-scripts/ESP32-A1S-sdkconfig.defaults

@@ -26,15 +26,11 @@ CONFIG_SPKFAULT_GPIO=-1
 CONFIG_SPKFAULT_GPIO_LEVEL=0
 CONFIG_BAT_CHANNEL=-1
 CONFIG_BAT_SCALE="0"
-CONFIG_I2S_BCK_IO=27
-CONFIG_I2S_WS_IO=26
-CONFIG_I2S_DO_IO=25
-CONFIG_I2S_DI_IO=35
 CONFIG_SDIF_NUM=0
 CONFIG_SPDIF_BCK_IO=27
 CONFIG_SPDIF_WS_IO=26
 CONFIG_SPDIF_DO_IO=-1
-
+CONFIG_DAC_CONFIG="model=AC101,bck=27,ws=26,do=25,di=35,sda=33,scl=32"
 CONFIG_IDF_TARGET_ESP32=y
 CONFIG_IDF_TARGET="esp32"
 

+ 2 - 9
build-scripts/SqueezeAmp4MBFlash-sdkconfig.defaults

@@ -12,7 +12,6 @@ CONFIG_DSP_MAX_FFT_SIZE_512=y
 CONFIG_JACK_LOCKED=y
 CONFIG_BAT_LOCKED=y
 CONFIG_I2C_LOCKED=y
-CONFIG_SPDIF_LOCKED=y
 CONFIG_LED_LOCKED=y
 CONFIG_DISPLAY_CONFIG=""
 CONFIG_I2C_CONFIG=""
@@ -30,15 +29,9 @@ CONFIG_SPKFAULT_GPIO_LEVEL=0
 CONFIG_BAT_CHANNEL=7
 CONFIG_BAT_SCALE="20.24"
 CONFIG_I2S_NUM=0
-CONFIG_I2S_BCK_IO=33
-CONFIG_I2S_WS_IO=25
-CONFIG_I2S_DO_IO=32
-CONFIG_I2S_DI_IO=-1
 CONFIG_SDIF_NUM=0
-CONFIG_SPDIF_BCK_IO=33
-CONFIG_SPDIF_WS_IO=25
-CONFIG_SPDIF_DO_IO=15
-
+CONFIG_SPDIF_CONFIG="bck=33,ws=25,do=15"
+CONFIG_DAC_CONFIG="model=TAS57xx,bck=33,ws=25,do=32,sda=27,scl=26,mute=14"
 CONFIG_IDF_TARGET_ESP32=y
 CONFIG_IDF_TARGET="esp32"
 

+ 2 - 8
build-scripts/SqueezeAmp8MBFlash-sdkconfig.defaults

@@ -12,7 +12,6 @@ CONFIG_DSP_MAX_FFT_SIZE_512=y
 CONFIG_JACK_LOCKED=y
 CONFIG_BAT_LOCKED=y
 CONFIG_I2C_LOCKED=y
-CONFIG_SPDIF_LOCKED=y
 CONFIG_LED_LOCKED=y
 CONFIG_DISPLAY_CONFIG=""
 CONFIG_I2C_CONFIG=""
@@ -30,14 +29,9 @@ CONFIG_SPKFAULT_GPIO_LEVEL=0
 CONFIG_BAT_CHANNEL=7
 CONFIG_BAT_SCALE="20.24"
 CONFIG_I2S_NUM=0
-CONFIG_I2S_BCK_IO=33
-CONFIG_I2S_WS_IO=25
-CONFIG_I2S_DO_IO=32
-CONFIG_I2S_DI_IO=-1
 CONFIG_SDIF_NUM=0
-CONFIG_SPDIF_BCK_IO=33
-CONFIG_SPDIF_WS_IO=25
-CONFIG_SPDIF_DO_IO=15
+CONFIG_SPDIF_CONFIG="bck=33,ws=25,do=15"
+CONFIG_DAC_CONFIG="model=TAS57xx,bck=33,ws=25,do=32,sda=27,scl=26,mute=14"
 
 CONFIG_IDF_TARGET_ESP32=y
 CONFIG_IDF_TARGET="esp32"

+ 1 - 1
components/services/led.c

@@ -168,7 +168,7 @@ int led_allocate(void) {
  */
 bool led_config(int idx, gpio_num_t gpio, int onstate, int pwm) {
 	if (gpio < 0) {
-		ESP_LOGW(TAG,"LED GPIO not configured");
+		ESP_LOGW(TAG,"LED GPIO -1 ignored");
 		return false;
 	}
 	

+ 13 - 19
components/squeezelite/a1s/ac101.c → components/squeezelite/ac101/ac101.c

@@ -33,7 +33,7 @@
 #include "adac.h"
 #include "ac101.h"
 
-const static char TAG[] = "AC101";
+static const char TAG[] = "AC101";
 
 #define SPKOUT_EN ((1 << 9) | (1 << 11) | (1 << 7) | (1 << 5))
 #define EAROUT_EN ((1 << 11) | (1 << 12) | (1 << 13))
@@ -48,14 +48,14 @@ const static char TAG[] = "AC101";
         return b;\
     }
 	
-static bool init(int i2c_port_num, int i2s_num, i2s_config_t *config);
+static bool init(char *config, int i2c_port_num);
 static void deinit(void);
 static void speaker(bool active);
 static void headset(bool active);
 static void volume(unsigned left, unsigned right);
 static void power(adac_power_e mode);
 
-struct adac_s dac_a1s = { init, deinit, power, speaker, headset, volume };
+const struct adac_s dac_ac101 = { "AC101", init, deinit, power, speaker, headset, volume };
 
 static esp_err_t i2c_write_reg(uint8_t reg, uint16_t val);
 static uint16_t i2c_read_reg(uint8_t reg);
@@ -70,21 +70,24 @@ static int i2c_port;
 /****************************************************************************************
  * init
  */
-static bool init(int i2c_port_num, int i2s_num, i2s_config_t *i2s_config) {	 
+static bool init(char *config, int i2c_port_num) {	 
 	esp_err_t res = ESP_OK;
+	char *p;
 	
-	i2c_port = i2c_port_num;
-
 	// configure i2c
 	i2c_config_t i2c_config = {
 			.mode = I2C_MODE_MASTER,
-			.sda_io_num = 33,
+			.sda_io_num = -1,
 			.sda_pullup_en = GPIO_PULLUP_ENABLE,
-			.scl_io_num = 32,
+			.scl_io_num = -1,
 			.scl_pullup_en = GPIO_PULLUP_ENABLE,
 			.master.clk_speed = 250000,
 		};
-		
+	
+	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);
+	
+	i2c_port = i2c_port_num;
 	i2c_param_config(i2c_port, &i2c_config);
 	i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false);
 	
@@ -96,8 +99,6 @@ static bool init(int i2c_port_num, int i2s_num, i2s_config_t *i2s_config) {
 		return 0;		
 	}
 	
-	ESP_LOGI(TAG, "AC101 DAC using I2C sda:%u, scl:%u", i2c_config.sda_io_num, i2c_config.scl_io_num);
-	
 	res = i2c_write_reg(CHIP_AUDIO_RS, 0x123);
 	// huh?
 	vTaskDelay(100 / portTICK_PERIOD_MS); 
@@ -140,13 +141,6 @@ static bool init(int i2c_port_num, int i2s_num, i2s_config_t *i2s_config) {
 	i2c_write_reg(OMIXER_SR, 		BIN(0000,0101,0000,1010));	// source=DAC(R/L) and LINEIN(R/L)
 #endif	
 	
-	// configure I2S pins & install driver	
-	i2s_pin_config_t i2s_pin_config = (i2s_pin_config_t) { 	.bck_io_num = CONFIG_I2S_BCK_IO, .ws_io_num = CONFIG_I2S_WS_IO, 
-															.data_out_num = CONFIG_I2S_DO_IO, .data_in_num = CONFIG_I2S_DI_IO
-								};
-	res |= i2s_driver_install(i2s_num, i2s_config, 0, NULL);
-	res |= i2s_set_pin(i2s_num, &i2s_pin_config);
-	
 	// enable earphone & speaker
 	i2c_write_reg(SPKOUT_CTRL, 0x0220);
 	i2c_write_reg(HPOUT_CTRL, 0xf801);
@@ -155,7 +149,7 @@ static bool init(int i2c_port_num, int i2s_num, i2s_config_t *i2s_config) {
 	ac101_set_spk_volume(100);
 	ac101_set_earph_volume(100);
 	
-	ESP_LOGI(TAG, "DAC using I2S bck:%d, ws:%d, do:%d", i2s_pin_config.bck_io_num, i2s_pin_config.ws_io_num, i2s_pin_config.data_out_num);
+	ESP_LOGI(TAG, "AC101 uses I2C sda:%d, scl:%d", i2c_config.sda_io_num, i2c_config.scl_io_num);
 
 	return (res == ESP_OK);
 }	

+ 0 - 0
components/squeezelite/a1s/ac101.h → components/squeezelite/ac101/ac101.h


+ 5 - 4
components/squeezelite/adac.h

@@ -15,7 +15,8 @@
 typedef enum { ADAC_ON = 0, ADAC_STANDBY, ADAC_OFF } adac_power_e;
 
 struct adac_s {
-	bool (*init)(int i2c_port_num, int i2s_num, i2s_config_t *config);
+	char *model;
+	bool (*init)(char *config, int i2c_port_num);
 	void (*deinit)(void);
 	void (*power)(adac_power_e mode);
 	void (*speaker)(bool active);
@@ -23,6 +24,6 @@ struct adac_s {
 	void (*volume)(unsigned left, unsigned right);
 };
 
-extern struct adac_s dac_tas57xx;
-extern struct adac_s dac_a1s;
-extern struct adac_s dac_external;
+extern const struct adac_s dac_tas57xx;
+extern const struct adac_s dac_ac101;
+extern const struct adac_s dac_external;

+ 2 - 2
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 a1s external
-COMPONENT_ADD_INCLUDEDIRS := . ./tas57xx ./a1s
+COMPONENT_SRCDIRS := . tas57xx ac101 external
+COMPONENT_ADD_INCLUDEDIRS := . ./tas57xx ./ac101
 COMPONENT_EMBED_FILES := vu.data
 

+ 6 - 30
components/squeezelite/external/dac_external.c

@@ -16,42 +16,18 @@
 #include "config.h"
 #include "adac.h"
 
-static bool init(int i2c_port_num, int i2s_num, i2s_config_t *config);
+static const char TAG[] = "DAC external";
+
+static bool init(char *config, int i2c_port_num);
 static void deinit(void) { };
 static void speaker(bool active) { };
 static void headset(bool active) { } ;
 static void volume(unsigned left, unsigned right) { };
 static void power(adac_power_e mode) { };
 
-struct adac_s dac_external = { init, deinit, power, speaker, headset, volume };
-
-static char TAG[] = "DAC external";
-
-static bool init(int i2c_port_num, int i2s_num, i2s_config_t *config) { 
-	i2s_pin_config_t i2s_pin_config = (i2s_pin_config_t) { 	.bck_io_num = CONFIG_I2S_BCK_IO, .ws_io_num = CONFIG_I2S_WS_IO, 
-															.data_out_num = CONFIG_I2S_DO_IO, .data_in_num = CONFIG_I2S_DI_IO };
-	char *nvs_item = config_alloc_get(NVS_TYPE_STR, "dac_config");
-	
-	if (nvs_item) {
-		char *p;
-		if ((p = strcasestr(nvs_item, "bck")) != NULL) i2s_pin_config.bck_io_num = atoi(strchr(p, '=') + 1);
-		if ((p = strcasestr(nvs_item, "ws")) != NULL) i2s_pin_config.ws_io_num = atoi(strchr(p, '=') + 1);
-		if ((p = strcasestr(nvs_item, "do")) != NULL) i2s_pin_config.data_out_num = atoi(strchr(p, '=') + 1);
-		free(nvs_item);
-	} 
-	
-	if (i2s_pin_config.bck_io_num != -1 && i2s_pin_config.ws_io_num != -1 && i2s_pin_config.data_out_num != -1) {
-		i2s_driver_install(i2s_num, config, 0, NULL);
-		i2s_set_pin(i2s_num, &i2s_pin_config);
-
-		ESP_LOGI(TAG, "External DAC using I2S bck:%u, ws:%u, do:%u", i2s_pin_config.bck_io_num, i2s_pin_config.ws_io_num, i2s_pin_config.data_out_num);
+const struct adac_s dac_external = { "i2s", init, deinit, power, speaker, headset, volume };
 
-		return true;
-	} else {
-		ESP_LOGI(TAG, "Cannot initialize I2S for DAC bck:%d ws:%d do:%d", i2s_pin_config.bck_io_num, 
-																		   i2s_pin_config.ws_io_num, 
-																		   i2s_pin_config.data_out_num);
-		return false;
-	}
+static bool init(char *config, int i2c_port_num) { 
+	return true;	
 }
 

+ 59 - 33
components/squeezelite/output_i2s.c

@@ -77,8 +77,8 @@ extern struct buffer *streambuf;
 extern struct buffer *outputbuf;
 extern u8_t *silencebuf;
 
-// by default no DAC selected
-struct adac_s *adac = &dac_external;
+const struct adac_s *dac_set[] = { &dac_tas57xx, &dac_ac101, NULL };
+const struct adac_s *adac = &dac_external;
 
 static log_level loglevel;
 
@@ -145,8 +145,9 @@ static void set_amp_gpio(int gpio, char *value) {
  */
 void output_init_i2s(log_level level, char *device, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned idle) {
 	loglevel = level;
-	char *p;
-
+	char *p, *dac_config, *spdif_config;
+	esp_err_t res;
+	
 	p = config_alloc_get_default(NVS_TYPE_STR, "jack_mutes_amp", "n", 0);
 	jack_mutes_amp = (strcmp(p,"1") == 0 ||strcasecmp(p,"y") == 0);
 	free(p);
@@ -182,7 +183,17 @@ void output_init_i2s(log_level level, char *device, unsigned output_buf_size, ch
 	}
 		
 	running = true;
+	i2s_pin_config_t i2s_pin_config = {	.bck_io_num = -1, .ws_io_num = -1, .data_out_num = -1, .data_in_num = -1 }; 				
 
+	// get SPDIF configuration from NVS or compile
+#ifdef CONFIG_SPDIF_CONFIG
+	spdif_config = strdup(CONFIG_SPDIF_CONFIG);
+#else
+	spdif_config = config_alloc_get(NVS_TYPE_STR, "spdif_config");
+	if (!spdif_config) spdif_config = strdup("bck=" STR(CONFIG_SPDIF_BCK_IO) ",ws=" STR(CONFIG_SPDIF_WS_IO) ",do=" STR(CONFIG_SPDIF_DO_IO));
+#endif		
+	if ((p = strcasestr(spdif_config, "do")) != NULL) i2s_pin_config.data_out_num = atoi(strchr(p, '=') + 1);
+	
 	// common I2S initialization
 	i2s_config.mode = I2S_MODE_MASTER | I2S_MODE_TX;
 	i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;
@@ -191,21 +202,13 @@ void output_init_i2s(log_level level, char *device, unsigned output_buf_size, ch
 	i2s_config.tx_desc_auto_clear = true;		
 	i2s_config.use_apll = true;
 	i2s_config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1; //Interrupt level 1
-
+	
 	if (strcasestr(device, "spdif")) {
 		spdif = true;	
-		i2s_pin_config_t i2s_pin_config = (i2s_pin_config_t) { .bck_io_num = CONFIG_SPDIF_BCK_IO, .ws_io_num = CONFIG_SPDIF_WS_IO, 
-															  .data_out_num = CONFIG_SPDIF_DO_IO, .data_in_num = -1 };
-#ifndef CONFIG_SPDIF_LOCKED															  
-		char *nvs_item = config_alloc_get(NVS_TYPE_STR, "spdif_config");
-		if (nvs_item) {
-			if ((p = strcasestr(nvs_item, "bck")) != NULL) i2s_pin_config.bck_io_num = atoi(strchr(p, '=') + 1);
-			if ((p = strcasestr(nvs_item, "ws")) != NULL) i2s_pin_config.ws_io_num = atoi(strchr(p, '=') + 1);
-			if ((p = strcasestr(nvs_item, "do")) != NULL) i2s_pin_config.data_out_num = atoi(strchr(p, '=') + 1);
-			free(nvs_item);
-		} 
-#endif		
-		
+
+		if ((p = strcasestr(spdif_config, "bck")) != NULL) i2s_pin_config.bck_io_num = atoi(strchr(p, '=') + 1);
+		if ((p = strcasestr(spdif_config, "ws")) != NULL) i2s_pin_config.ws_io_num = atoi(strchr(p, '=') + 1);
+			
 		if (i2s_pin_config.bck_io_num == -1 || i2s_pin_config.ws_io_num == -1 || i2s_pin_config.data_out_num == -1) {
 			LOG_WARN("Cannot initialize I2S for SPDIF bck:%d ws:%d do:%d", i2s_pin_config.bck_io_num, 
 																		   i2s_pin_config.ws_io_num, 
@@ -223,30 +226,53 @@ void output_init_i2s(log_level level, char *device, unsigned output_buf_size, ch
 		   audio frame. So the real depth is true frames is (LEN * COUNT / 2)
 		*/   
 		dma_buf_frames = DMA_BUF_COUNT * DMA_BUF_LEN / 2;	
-		i2s_driver_install(CONFIG_I2S_NUM, &i2s_config, 0, NULL);
-		i2s_set_pin(CONFIG_I2S_NUM, &i2s_pin_config);
+		res = i2s_driver_install(CONFIG_I2S_NUM, &i2s_config, 0, NULL);
+		res |= i2s_set_pin(CONFIG_I2S_NUM, &i2s_pin_config);
 		LOG_INFO("SPDIF using I2S bck:%u, ws:%u, do:%u", i2s_pin_config.bck_io_num, i2s_pin_config.ws_io_num, i2s_pin_config.data_out_num);
 	} else {
-#if CONFIG_SPDIF_DO_IO != -1
-		gpio_pad_select_gpio(CONFIG_SPDIF_DO_IO);
-		gpio_set_direction(CONFIG_SPDIF_DO_IO, GPIO_MODE_OUTPUT);
-		gpio_set_level(CONFIG_SPDIF_DO_IO, 0);
-#endif
-
+		// turn off SPDIF if configured
+		if (i2s_pin_config.data_out_num >= 0) {
+			gpio_pad_select_gpio(i2s_pin_config.data_out_num);
+			gpio_set_direction(i2s_pin_config.data_out_num, GPIO_MODE_OUTPUT);
+			gpio_set_level(i2s_pin_config.data_out_num, 0);
+		}	
+		
+#ifdef CONFIG_DAC_CONFIG
+		dac_config = strdup(CONFIG_DAC_CONFIG);
+#else
+		dac_config = config_alloc_get(NVS_TYPE_STR, "dac_config");
+		if (!dac_config) dac_config = strdup("model=i2s,bck=" STR(CONFIG_I2S_BCK_IO) ",ws=" STR(CONFIG_I2S_WS_IO) ",do=" STR(CONFIG_I2S_DO_IO) ",sda=" STR(CONFIG_I2C_SDA) ",scl=" STR(CONFIG_I2C_SCL));
+#endif	
+		char model[32] = "i2s";
+		if ((p = strcasestr(dac_config, "model")) != NULL) sscanf(p, "%*[^=]=%31[^,]", model);
+		
+		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) ? ESP_OK : ESP_FAIL;
+		
+		if ((p = strcasestr(dac_config, "bck")) != NULL) i2s_pin_config.bck_io_num = atoi(strchr(p, '=') + 1);
+		if ((p = strcasestr(dac_config, "ws")) != NULL) i2s_pin_config.ws_io_num = atoi(strchr(p, '=') + 1);
+		if ((p = strcasestr(dac_config, "do")) != NULL) i2s_pin_config.data_out_num = atoi(strchr(p, '=') + 1);
+		free(dac_config);
+		
 		i2s_config.sample_rate = output.current_sample_rate;
 		i2s_config.bits_per_sample = bytes_per_frame * 8 / 2;
 		// Counted in frames (but i2s allocates a buffer <= 4092 bytes)
 		i2s_config.dma_buf_len = DMA_BUF_LEN;	
 		i2s_config.dma_buf_count = DMA_BUF_COUNT;
-		dma_buf_frames = DMA_BUF_COUNT * DMA_BUF_LEN;	
+		dma_buf_frames = DMA_BUF_COUNT * DMA_BUF_LEN;			
 		
-		// finally let DAC driver initialize I2C and I2S
-		if (dac_tas57xx.init(I2C_PORT, CONFIG_I2S_NUM, &i2s_config)) adac = &dac_tas57xx;
-		else if (dac_a1s.init(I2C_PORT, CONFIG_I2S_NUM, &i2s_config)) adac = &dac_a1s;
-		else if (!dac_external.init(I2C_PORT, CONFIG_I2S_NUM, &i2s_config)) {
-			LOG_WARN("DAC not configured and SPDIF not enabled, I2S will not continue");
-			return;
-		}
+		res |= i2s_driver_install(CONFIG_I2S_NUM, &i2s_config, 0, NULL);
+		res |= i2s_set_pin(CONFIG_I2S_NUM, &i2s_pin_config);
+	
+		LOG_INFO("%s DAC using I2S bck:%d, ws:%d, do:%d", model, i2s_pin_config.bck_io_num, 
+							i2s_pin_config.ws_io_num, i2s_pin_config.data_out_num);
+	}	
+	
+	free(spdif_config);
+	
+	if (res != ESP_OK) {
+		LOG_WARN("no DAC configured");
+		return;
 	}	
 
 	LOG_INFO("Initializing I2S mode %s with rate: %d, bits per sample: %d, buffer frames: %d, number of buffers: %d ", 

+ 38 - 40
components/squeezelite/tas57xx/dac_57xx.c

@@ -9,28 +9,28 @@
  *
  */
  
-#include "squeezelite.h" 
+#include <string.h>
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
 #include "driver/i2s.h"
 #include "driver/i2c.h"
 #include "driver/gpio.h"
+#include "esp_log.h"
 #include "adac.h"
 
-// this is the only hard-wired thing
-#define VOLUME_GPIO	14	
-
 #define TAS575x 0x98
 #define TAS578x	0x90
 
-static bool init(int i2c_port_num, int i2s_num, i2s_config_t *config);
+static const char TAG[] = "TAS575x/8x";
+
+static bool init(char *config, int i2c_port_num);
 static void deinit(void);
 static void speaker(bool active);
 static void headset(bool active);
 static void volume(unsigned left, unsigned right);
 static void power(adac_power_e mode);
 
-struct adac_s dac_tas57xx = { init, deinit, power, speaker, headset, volume };
+const struct adac_s dac_tas57xx = { "TAS57xx", init, deinit, power, speaker, headset, volume };
 
 struct tas57xx_cmd_s {
 	uint8_t reg;
@@ -59,9 +59,9 @@ static const struct tas57xx_cmd_s tas57xx_cmd[] = {
 	{ 0x56, 0x00 },	// TAS57_ANALOGUE_ON
 };
 
-static log_level loglevel = lINFO;
-static u8_t tas57_addr;
+static uint8_t tas57_addr;
 static int i2c_port;
+static int mute_gpio = -1;
 
 static void dac_cmd(dac_cmd_e cmd, ...);
 static int tas57_detect(void);
@@ -69,19 +69,24 @@ static int tas57_detect(void);
 /****************************************************************************************
  * init
  */
-static bool init(int i2c_port_num, int i2s_num, i2s_config_t *i2s_config)	{	 
+static bool init(char *config, int i2c_port_num)	{	 
 	i2c_port = i2c_port_num;
-		
+	char *p;	
+	
 	// configure i2c
 	i2c_config_t i2c_config = {
 			.mode = I2C_MODE_MASTER,
-			.sda_io_num = 27,
+			.sda_io_num = -1,
 			.sda_pullup_en = GPIO_PULLUP_ENABLE,
-			.scl_io_num = 26,
+			.scl_io_num = -1,
 			.scl_pullup_en = GPIO_PULLUP_ENABLE,
-			.master.clk_speed = 100000,
+			.master.clk_speed = 250000,
 		};
-		
+
+	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);
+	if ((p = strcasestr(config, "mute")) != NULL) mute_gpio = atoi(strchr(p, '=') + 1);
+	
 	i2c_param_config(i2c_port, &i2c_config);
 	i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false);
 		
@@ -89,13 +94,11 @@ static bool init(int i2c_port_num, int i2s_num, i2s_config_t *i2s_config)	{
 	tas57_addr = tas57_detect();
 	
 	if (!tas57_addr) {
-		LOG_WARN("No TAS57xx detected");
+		ESP_LOGW(TAG, "No TAS57xx detected");
 		i2c_driver_delete(i2c_port);
-		return 0;
+		return false;
 	}
-	
-	LOG_INFO("TAS57xx DAC using I2C sda:%u, scl:%u", i2c_config.sda_io_num, i2c_config.scl_io_num);
-	
+
 	i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
 	
 	for (int i = 0; tas57xx_init_sequence[i].reg != 0xff; i++) {
@@ -103,30 +106,25 @@ static bool init(int i2c_port_num, int i2s_num, i2s_config_t *i2s_config)	{
 		i2c_master_write_byte(i2c_cmd, tas57_addr | I2C_MASTER_WRITE, I2C_MASTER_NACK);
 		i2c_master_write_byte(i2c_cmd, tas57xx_init_sequence[i].reg, I2C_MASTER_NACK);
 		i2c_master_write_byte(i2c_cmd, tas57xx_init_sequence[i].value, I2C_MASTER_NACK);
-
-		LOG_DEBUG("i2c write %x at %u", tas57xx_init_sequence[i].reg, tas57xx_init_sequence[i].value);
+		ESP_LOGD(TAG, "i2c write %x at %u", tas57xx_init_sequence[i].reg, tas57xx_init_sequence[i].value);
 	}
 
 	i2c_master_stop(i2c_cmd);	
 	esp_err_t res = i2c_master_cmd_begin(i2c_port, i2c_cmd, 500 / portTICK_RATE_MS);
     i2c_cmd_link_delete(i2c_cmd);
 
-	// configure I2S pins & install driver	
-	i2s_pin_config_t i2s_pin_config = (i2s_pin_config_t) { 	.bck_io_num = CONFIG_I2S_BCK_IO, .ws_io_num = CONFIG_I2S_WS_IO, 
-														.data_out_num = CONFIG_I2S_DO_IO, .data_in_num = CONFIG_I2S_DI_IO,
-								};
-	res |= i2s_driver_install(i2s_num, i2s_config, 0, NULL);
-	res |= i2s_set_pin(i2s_num, &i2s_pin_config);
-	LOG_INFO("DAC using I2S bck:%d, ws:%d, do:%d", i2s_pin_config.bck_io_num, i2s_pin_config.ws_io_num, i2s_pin_config.data_out_num);
+	ESP_LOGI(TAG, "TAS57xx uses I2C sda:%d, scl:%d and mute: %d", i2c_config.sda_io_num, i2c_config.scl_io_num, mute_gpio);
 	
 	if (res == ESP_OK) {
-		// init volume & mute
-		gpio_pad_select_gpio(VOLUME_GPIO);
-		gpio_set_direction(VOLUME_GPIO, GPIO_MODE_OUTPUT);
-		gpio_set_level(VOLUME_GPIO, 0);
+		if (mute_gpio >= 0) {
+			// init volume & mute
+			gpio_pad_select_gpio(mute_gpio);
+			gpio_set_direction(mute_gpio, GPIO_MODE_OUTPUT);
+			gpio_set_level(mute_gpio, 0);
+		}	
 		return true;
 	} else {
-		LOG_ERROR("could not intialize TAS57xx %d", res);
+		ESP_LOGE(TAG, "could not intialize TAS57xx %d", res);
 		return false;
 	}	
 }	
@@ -142,8 +140,8 @@ static void deinit(void)	{
  * change volume
  */
 static void volume(unsigned left, unsigned right) {
-	LOG_INFO("TAS57xx volume (L:%u R:%u)", left, right);
-	gpio_set_level(VOLUME_GPIO, left || right);
+	ESP_LOGI(TAG, "TAS57xx volume (L:%u R:%u)", left, right);
+	if (mute_gpio >= 0) gpio_set_level(mute_gpio, left || right);
 } 
 
 /****************************************************************************************
@@ -161,7 +159,7 @@ static void power(adac_power_e mode) {
 		dac_cmd(TAS57_DOWN);
 		break;				
 	default:
-		LOG_WARN("unknown DAC command");
+		ESP_LOGW(TAG, "unknown DAC command");
 		break;
 	}
 }
@@ -192,7 +190,7 @@ void dac_cmd(dac_cmd_e cmd, ...) {
 
 	switch(cmd) {
 	case TAS57_VOLUME:
-		LOG_ERROR("DAC volume not handled yet");
+		ESP_LOGE(TAG, "DAC volume not handled yet");
 		break;
 	default:
 		i2c_master_start(i2c_cmd);
@@ -206,7 +204,7 @@ void dac_cmd(dac_cmd_e cmd, ...) {
     i2c_cmd_link_delete(i2c_cmd);
 	
 	if (ret != ESP_OK) {
-		LOG_ERROR("could not intialize TAS57xx %d", ret);
+		ESP_LOGE(TAG, "could not intialize TAS57xx %d", ret);
 	}
 
 	va_end(args);
@@ -216,7 +214,7 @@ void dac_cmd(dac_cmd_e cmd, ...) {
  * TAS57 detection
  */
 static int tas57_detect(void) {
-	u8_t data, addr[] = {TAS578x, TAS575x};
+	uint8_t data, addr[] = {TAS578x, TAS575x};
 	int ret;
 	
 	for (int i = 0; i < sizeof(addr); i++) {
@@ -235,7 +233,7 @@ static int tas57_detect(void) {
 		i2c_cmd_link_delete(i2c_cmd);	
 		
 		if (ret == ESP_OK) {
-			LOG_INFO("Detected TAS @0x%x", addr[i]);
+			ESP_LOGI(TAG, "Detected TAS @0x%x", addr[i]);
 			return addr[i];
 		}	
 	}	

+ 54 - 36
main/Kconfig.projbuild

@@ -33,7 +33,14 @@ menu "Squeezelite-ESP32"
 		bool				
 	config SPKFAULT_LOCKED
 		bool				
-    menu "Audio Output"
+	config DAC_CONFIG
+		string 
+		default "model=TAS57xx,bck=33,ws=25,do=32,sda=27,scl=26,mute=14" if SQUEEZEAMP
+		default "model=AC101,bck=27,ws=26,do=25,di=35,sda=33,scl=32" if A1S
+	config SPDIF_CONFIG		
+		string
+		default "bck=33,ws=25,do=15" if SQUEEZEAMP
+	menu "Audio Output"
 		choice OUTPUT_TYPE
 			prompt "Output Type"
 	        default BASIC_I2C_BT
@@ -44,7 +51,6 @@ menu "Squeezelite-ESP32"
 				select JACK_LOCKED
 				select BAT_LOCKED
 				select I2C_LOCKED
-				select SPDIF_LOCKED
 				select LED_LOCKED
 				select SPKFAULT_LOCKED
 			config A1S
@@ -54,38 +60,52 @@ menu "Squeezelite-ESP32"
 	            bool "Generic I2S & Bluetooth"
 	    endchoice
 	  	
-		menu "DAC I2S settings" 
+		menu "DAC settings" 
 			visible if BASIC_I2C_BT
-			config I2S_NUM         
-		        int "I2S channel (0 or 1). "
-		        default 0
-		        help
-		            I2S dma channel to use.  
-		    config I2S_BCK_IO         
-		        int "I2S Bit clock GPIO number. "
-				default 33 if !A1S
-		        default 27 if A1S
-		        help
-		            I2S Bit Clock gpio pin to use.  
-		    config I2S_WS_IO         
-		        int "I2S Word Select GPIO number. "
-		        default 25 if !A1S
-				default 26 if A1S
-		        help
-		            I2S Word Select gpio pin to use.
-		    config I2S_DO_IO         
-		        int "I2S Data Output GPIO number. "
-		        default 32 if !A1S
-				default 25 if A1S
-		        help
-		            I2S data output gpio pin to use.
-			config I2S_DI_IO         
-		        int "I2S Data Input GPIO number. "
-		        default -1 if !A1S
-				default 35 if A1S
-		        help
-		            I2S data input gpio pin to use (not used mostly, leave it to -1).					
-					
+			menu "I2S settings"
+				config I2S_NUM         
+					int "I2S channel (0 or 1). "
+					default 0
+					help
+						I2S dma channel to use.  
+				config I2S_BCK_IO         
+					int "I2S Bit clock GPIO number. "
+					default 33 
+					help
+						I2S Bit Clock gpio pin to use.  
+				config I2S_WS_IO         
+					int "I2S Word Select GPIO number. "
+					default 25 
+					help
+						I2S Word Select gpio pin to use.
+				config I2S_DO_IO         
+					int "I2S Data Output GPIO number. "
+					default 32
+					help
+						I2S data output gpio pin to use.
+				config I2S_DI_IO         
+					int "I2S Data Input GPIO number. "
+					default -1 if !A1S
+					help
+						I2S data input gpio pin to use (not used mostly, leave it to -1).					
+			endmenu
+			menu "I2C settings"	
+				config I2C_SDA
+					int "I2C SDA GPIO number for DAC control. "
+					default -1
+					help
+						I2C data gpio pin to use with DAC (not used mostly, leave it to -1).										
+				config I2C_SCL
+					int "I2C SCL GPIO number for DAC control. "
+					default -1
+					help
+						I2C clock gpio pin to use with DAC (not used mostly, leave it to -1).															
+			endmenu		
+			config MUTE_GPIO
+				int "GPIO for muting DAC"
+				default -1 
+				help
+					GPIO used to mute DAC (not used mostly, leave it to -1).															
 		endmenu
 		
 		menu "SPDIF settings" 
@@ -107,9 +127,7 @@ menu "Squeezelite-ESP32"
 		            Must be set as SPDIF re-uses I2S but only needs DO (recommendation: set it to I2S Word select value)
 		    config SPDIF_DO_IO         
 		        int "SPDIF Data I/O GPIO number"
-		        default 15 if SQUEEZEAMP
-				default I2S_DO_IO if !A1S
-				default -1 if A1S
+		        default -1
 				help
 		            I2S data output IO use to simulate SPDIF
 		endmenu