mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-06 11:36:59 +03:00
auto-detect DAC
This commit is contained in:
@@ -60,7 +60,9 @@ static void battery_callback(TimerHandle_t xTimer) {
|
||||
*/
|
||||
void battery_svc_init(void) {
|
||||
battery.channel = CONFIG_BAT_CHANNEL;
|
||||
#ifdef CONFIG_BAT_SCALE
|
||||
battery.scale = atof(CONFIG_BAT_SCALE);
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_BAT_LOCKED
|
||||
char *nvs_item = config_alloc_get_default(NVS_TYPE_STR, "bat_config", "n", 0);
|
||||
|
||||
@@ -36,9 +36,9 @@ static struct led_s {
|
||||
|
||||
static struct {
|
||||
int gpio;
|
||||
int state;
|
||||
} green = { CONFIG_LED_GREEN_GPIO, CONFIG_LED_GREEN_GPIO_LEVEL},
|
||||
red = { CONFIG_LED_RED_GPIO, CONFIG_LED_RED_GPIO_LEVEL };
|
||||
int active;
|
||||
} green = { CONFIG_LED_GREEN_GPIO, 0 },
|
||||
red = { CONFIG_LED_RED_GPIO, 0 };
|
||||
|
||||
/****************************************************************************************
|
||||
*
|
||||
@@ -162,19 +162,27 @@ void set_led_gpio(int gpio, char *value) {
|
||||
|
||||
if (strcasestr(value, "green")) {
|
||||
green.gpio = gpio;
|
||||
if ((p = strchr(value, ':')) != NULL) green.state = atoi(p + 1);
|
||||
if ((p = strchr(value, ':')) != NULL) green.active = atoi(p + 1);
|
||||
} else if (strcasestr(value, "red")) {
|
||||
red.state = gpio;
|
||||
if ((p = strchr(value, ':')) != NULL) red.state = atoi(p + 1);
|
||||
red.active = gpio;
|
||||
if ((p = strchr(value, ':')) != NULL) red.active = atoi(p + 1);
|
||||
}
|
||||
}
|
||||
|
||||
void led_svc_init(void) {
|
||||
|
||||
#ifdef CONFIG_LED_GREEN_GPIO_LEVEL
|
||||
green.active = CONFIG_LED_GREEN_GPIO_LEVEL;
|
||||
#endif
|
||||
#ifdef CONFIG_LED_RED_GPIO_LEVEL
|
||||
red.active = CONFIG_LED_RED_GPIO_LEVEL;
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_LED_LOCKED
|
||||
parse_set_GPIO(set_led_gpio);
|
||||
#endif
|
||||
ESP_LOGI(TAG,"Configuring LEDs green:%d (active:%d), red:%d (active:%d)", green.gpio, green.state, red.gpio, red.state);
|
||||
ESP_LOGI(TAG,"Configuring LEDs green:%d (active:%d), red:%d (active:%d)", green.gpio, green.active, red.gpio, red.active);
|
||||
|
||||
led_config(LED_GREEN, green.gpio, green.state);
|
||||
led_config(LED_RED, red.gpio, red.state);
|
||||
led_config(LED_GREEN, green.gpio, green.active);
|
||||
led_config(LED_RED, red.gpio, red.active);
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "globdefs.h"
|
||||
#include "config.h"
|
||||
#include "accessors.h"
|
||||
|
||||
#define MONITOR_TIMER (10*1000)
|
||||
|
||||
static const char *TAG = "monitor";
|
||||
@@ -30,8 +31,8 @@ static TimerHandle_t monitor_timer;
|
||||
static struct {
|
||||
int gpio;
|
||||
int active;
|
||||
} jack = { CONFIG_JACK_GPIO, CONFIG_JACK_GPIO_LEVEL },
|
||||
spkfault = { CONFIG_SPKFAULT_GPIO, CONFIG_SPKFAULT_GPIO_LEVEL };
|
||||
} jack = { CONFIG_JACK_GPIO, 0 },
|
||||
spkfault = { CONFIG_SPKFAULT_GPIO, 0 };
|
||||
|
||||
void (*jack_handler_svc)(bool inserted);
|
||||
bool jack_inserted_svc(void);
|
||||
@@ -91,13 +92,28 @@ void set_jack_gpio(int gpio, char *value) {
|
||||
jack.gpio = gpio;
|
||||
if ((p = strchr(value, ':')) != NULL) jack.active = atoi(p + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************************
|
||||
*
|
||||
*/
|
||||
void set_spkfault_gpio(int gpio, char *value) {
|
||||
if (strcasestr(value, "spkfault")) {
|
||||
char *p;
|
||||
spkfault.gpio = gpio;
|
||||
if ((p = strchr(value, ':')) != NULL) spkfault.active = atoi(p + 1);
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************************
|
||||
*
|
||||
*/
|
||||
void monitor_svc_init(void) {
|
||||
ESP_LOGI(TAG, "Initializing monitoring");
|
||||
|
||||
#ifdef CONFIG_JACK_GPIO_LEVEL
|
||||
jack.active = CONFIG_JACK_GPIO_LEVEL;
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_JACK_LOCKED
|
||||
parse_set_GPIO(set_jack_gpio);
|
||||
@@ -108,6 +124,10 @@ void monitor_svc_init(void) {
|
||||
ESP_LOGI(TAG,"Adding jack (%s) detection GPIO %d", jack.active ? "high" : "low", jack.gpio);
|
||||
button_create(NULL, jack.gpio, jack.active ? BUTTON_HIGH : BUTTON_LOW, false, 250, jack_handler_default, 0, -1);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_SPKFAULT_GPIO_LEVEL
|
||||
spkfault.active = CONFIG_SPKFAULT_GPIO_LEVEL;
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_SPKFAULT_LOCKED
|
||||
parse_set_GPIO(set_spkfault_gpio);
|
||||
|
||||
@@ -73,7 +73,6 @@ static int i2c_port;
|
||||
static bool init(int i2c_port_num, int i2s_num, i2s_config_t *i2s_config) {
|
||||
esp_err_t res;
|
||||
|
||||
ESP_LOGI(TAG, "Initializing AC101");
|
||||
i2c_port = i2c_port_num;
|
||||
|
||||
// configure i2c
|
||||
@@ -88,17 +87,21 @@ static bool init(int i2c_port_num, int i2s_num, i2s_config_t *i2s_config) {
|
||||
|
||||
i2c_param_config(i2c_port, &i2c_config);
|
||||
i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false);
|
||||
ESP_LOGI(TAG, "DAC using I2C sda:%u, scl:%u", i2c_config.sda_io_num, i2c_config.scl_io_num);
|
||||
|
||||
res = i2c_read_reg(CHIP_AUDIO_RS);
|
||||
|
||||
if (!res) {
|
||||
ESP_LOGW(TAG, "No AC101 detected");
|
||||
i2c_driver_delete(i2c_port);
|
||||
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);
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
|
||||
if (ESP_OK != res) {
|
||||
ESP_LOGE(TAG, "AC101 reset failed! %d", res);
|
||||
return false;
|
||||
}
|
||||
|
||||
// enable the PLL from BCLK source
|
||||
i2c_write_reg(PLL_CTRL1, BIN(0000,0001,0100,1111)); // F=1,M=1,PLL,INT=31 (medium)
|
||||
i2c_write_reg(PLL_CTRL2, BIN(1000,0110,0000,0000)); // PLL, F=96,N_i=1024-96,F=0,N_f=0*0.2;
|
||||
|
||||
@@ -54,7 +54,7 @@ static bool init(int i2c_port_num, int i2s_num, i2s_config_t *config) {
|
||||
i2s_driver_install(i2s_num, config, 0, NULL);
|
||||
i2s_set_pin(i2s_num, &i2s_pin_config);
|
||||
|
||||
ESP_LOGI(TAG, "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);
|
||||
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);
|
||||
|
||||
return true;
|
||||
} else {
|
||||
|
||||
@@ -242,9 +242,6 @@ void output_init_i2s(log_level level, char *device, unsigned output_buf_size, ch
|
||||
gpio_set_level(CONFIG_SPDIF_DO_IO, 0);
|
||||
#endif
|
||||
|
||||
// not very pretty ...
|
||||
adac = &ADAC;
|
||||
|
||||
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)
|
||||
@@ -253,8 +250,13 @@ void output_init_i2s(log_level level, char *device, unsigned output_buf_size, ch
|
||||
dma_buf_frames = DMA_BUF_COUNT * DMA_BUF_LEN;
|
||||
|
||||
// finally let DAC driver initialize I2C and I2S
|
||||
adac->init(I2C_PORT, CONFIG_I2S_NUM, &i2s_config);
|
||||
}
|
||||
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 {
|
||||
dac_external.init(I2C_PORT, CONFIG_I2S_NUM, &i2s_config);
|
||||
adac = &dac_external;
|
||||
}
|
||||
}
|
||||
|
||||
LOG_INFO("Initializing I2S mode %s with rate: %d, bits per sample: %d, buffer frames: %d, number of buffers: %d ",
|
||||
spdif ? "S/PDIF" : "normal",
|
||||
|
||||
@@ -78,15 +78,8 @@ static int tas57_detect(void);
|
||||
* init
|
||||
*/
|
||||
static bool init(int i2c_port_num, int i2s_num, i2s_config_t *i2s_config) {
|
||||
LOG_INFO("Initializing TAS57xx ");
|
||||
|
||||
i2c_port = i2c_port_num;
|
||||
|
||||
// init volume & mute
|
||||
gpio_pad_select_gpio(VOLUME_GPIO);
|
||||
gpio_set_direction(VOLUME_GPIO, GPIO_MODE_OUTPUT);
|
||||
gpio_set_level(VOLUME_GPIO, 0);
|
||||
|
||||
// configure i2c
|
||||
i2c_config_t i2c_config = {
|
||||
.mode = I2C_MODE_MASTER,
|
||||
@@ -96,13 +89,26 @@ static bool init(int i2c_port_num, int i2s_num, i2s_config_t *i2s_config) {
|
||||
.scl_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.master.clk_speed = 100000,
|
||||
};
|
||||
|
||||
i2c_param_config(i2c_port, &i2c_config);
|
||||
i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false);
|
||||
LOG_INFO("DAC using I2C sda:%u, scl:%u", i2c_config.sda_io_num, i2c_config.scl_io_num);
|
||||
|
||||
// find which TAS we are using
|
||||
|
||||
// find which TAS we are using (if any)
|
||||
tas57_addr = tas57_detect();
|
||||
|
||||
if (!tas57_addr) {
|
||||
LOG_WARN("No TAS57xx detected");
|
||||
i2c_driver_delete(i2c_port);
|
||||
return 0;
|
||||
}
|
||||
|
||||
LOG_INFO("TAS57xx DAC using I2C sda:%u, scl:%u", i2c_config.sda_io_num, i2c_config.scl_io_num);
|
||||
|
||||
// init volume & mute
|
||||
gpio_pad_select_gpio(VOLUME_GPIO);
|
||||
gpio_set_direction(VOLUME_GPIO, GPIO_MODE_OUTPUT);
|
||||
gpio_set_level(VOLUME_GPIO, 0);
|
||||
|
||||
i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
|
||||
|
||||
for (int i = 0; tas57xx_init_sequence[i].reg != 0xff; i++) {
|
||||
|
||||
Reference in New Issue
Block a user