|
@@ -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;
|
|
|
|
|
@@ -93,7 +93,10 @@ static size_t dma_buf_frames;
|
|
|
static pthread_t thread;
|
|
|
static TaskHandle_t stats_task;
|
|
|
static bool stats;
|
|
|
-static int amp_gpio = -1;
|
|
|
+static struct {
|
|
|
+ int gpio, active;
|
|
|
+} amp_control = { -1, 1 },
|
|
|
+ mute_control = { CONFIG_MUTE_GPIO, CONFIG_MUTE_GPIO_LEVEL };
|
|
|
|
|
|
DECLARE_ALL_MIN_MAX;
|
|
|
|
|
@@ -129,14 +132,17 @@ static void jack_handler(bool inserted) {
|
|
|
* amp GPIO
|
|
|
*/
|
|
|
static void set_amp_gpio(int gpio, char *value) {
|
|
|
+ char *p;
|
|
|
+
|
|
|
if (!strcasecmp(value, "amp")) {
|
|
|
- amp_gpio = gpio;
|
|
|
+ amp_control.gpio = gpio;
|
|
|
+ if ((p = strchr(value, ':')) != NULL) amp_control.active = atoi(p + 1);
|
|
|
|
|
|
- gpio_pad_select_gpio(amp_gpio);
|
|
|
- gpio_set_direction(amp_gpio, GPIO_MODE_OUTPUT);
|
|
|
- gpio_set_level(amp_gpio, 0);
|
|
|
+ gpio_pad_select_gpio(amp_control.gpio);
|
|
|
+ gpio_set_direction(amp_control.gpio, GPIO_MODE_OUTPUT);
|
|
|
+ gpio_set_level(amp_control.gpio, !amp_control.active);
|
|
|
|
|
|
- LOG_INFO("setting amplifier GPIO %d", amp_gpio);
|
|
|
+ LOG_INFO("setting amplifier GPIO %d (active:%d)", amp_control.gpio, amp_control.active);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -146,7 +152,8 @@ 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;
|
|
|
-
|
|
|
+ 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 +189,14 @@ 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
|
|
|
+ char *spdif_config = config_alloc_get_str("spdif_config", CONFIG_SPDIF_CONFIG, "bck=" STR(CONFIG_SPDIF_BCK_IO)
|
|
|
+ ",ws=" STR(CONFIG_SPDIF_WS_IO) ",do=" STR(CONFIG_SPDIF_DO_IO));
|
|
|
+
|
|
|
+ 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 +205,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 +229,64 @@ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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)
|
|
|
+ ",sda=" STR(CONFIG_I2C_SDA) ",scl=" STR(CONFIG_I2C_SCL)
|
|
|
+ ",mute" STR(CONFIG_MUTE_GPIO));
|
|
|
+ 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);
|
|
|
+ if ((p = strcasestr(dac_config, "mute")) != NULL) {
|
|
|
+ char mute[8];
|
|
|
+ sscanf(p, "%*[^=]=%7[^,]", mute);
|
|
|
+ mute_control.gpio = atoi(mute);
|
|
|
+ if ((p = strchr(mute, ':')) != NULL) mute_control.active = atoi(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);
|
|
|
+
|
|
|
+ if (res == ESP_OK && mute_control.gpio >= 0) {
|
|
|
+ gpio_pad_select_gpio(mute_control.gpio);
|
|
|
+ gpio_set_direction(mute_control.gpio, GPIO_MODE_OUTPUT);
|
|
|
+ gpio_set_level(mute_control.gpio, mute_control.active);
|
|
|
+ }
|
|
|
+
|
|
|
+ LOG_INFO("%s DAC using I2S bck:%d, ws:%d, do:%d, mute:%d:%d (res:%d)", model, i2s_pin_config.bck_io_num, i2s_pin_config.ws_io_num,
|
|
|
+ i2s_pin_config.data_out_num, mute_control.gpio, mute_control.active, res);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 ",
|
|
@@ -314,8 +354,8 @@ void output_close_i2s(void) {
|
|
|
* change volume
|
|
|
*/
|
|
|
bool output_volume_i2s(unsigned left, unsigned right) {
|
|
|
- adac->volume(left, right);
|
|
|
- return false;
|
|
|
+ if (mute_control.gpio >= 0) gpio_set_level(mute_control.gpio, (left | right) ? !mute_control.active : mute_control.active);
|
|
|
+ return adac->volume(left, right);
|
|
|
}
|
|
|
|
|
|
/****************************************************************************************
|
|
@@ -396,8 +436,8 @@ static void *output_thread_i2s(void *arg) {
|
|
|
LOG_INFO("Output state is %d", output.state);
|
|
|
if (output.state == OUTPUT_OFF) {
|
|
|
led_blink(LED_GREEN, 100, 2500);
|
|
|
- if (amp_gpio != -1) gpio_set_level(amp_gpio, 0);
|
|
|
- LOG_INFO("switching off amp GPIO %d", amp_gpio);
|
|
|
+ if (amp_control.gpio != -1) gpio_set_level(amp_control.gpio, !amp_control.active);
|
|
|
+ LOG_INFO("switching off amp GPIO %d", amp_control.gpio);
|
|
|
} else if (output.state == OUTPUT_STOPPED) {
|
|
|
adac->speaker(false);
|
|
|
led_blink(LED_GREEN, 200, 1000);
|
|
@@ -460,7 +500,7 @@ static void *output_thread_i2s(void *arg) {
|
|
|
i2s_zero_dma_buffer(CONFIG_I2S_NUM);
|
|
|
i2s_start(CONFIG_I2S_NUM);
|
|
|
adac->power(ADAC_ON);
|
|
|
- if (amp_gpio != -1) gpio_set_level(amp_gpio, 1);
|
|
|
+ if (amp_control.gpio != -1) gpio_set_level(amp_control.gpio, amp_control.active);
|
|
|
}
|
|
|
|
|
|
// this does not work well as set_sample_rates resets the fifos (and it's too early)
|