porting master changes

There is a divergence in accessors.c that I've not resolved
This commit is contained in:
Philippe G
2020-08-02 23:13:46 -07:00
parent e6a4c85adc
commit 0865496d76
28 changed files with 544 additions and 337 deletions

View File

@@ -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 bool 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);
}
@@ -170,8 +164,9 @@ static void deinit(void) {
/****************************************************************************************
* change volume
*/
static void volume(unsigned left, unsigned right) {
static bool volume(unsigned left, unsigned right) {
// nothing at that point, volume is handled by backend
return false;
}
/****************************************************************************************

View File

@@ -15,14 +15,15 @@
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);
void (*headset)(bool active);
void (*volume)(unsigned left, unsigned right);
bool (*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;

View File

@@ -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

View File

@@ -28,7 +28,6 @@ extern struct buffer *outputbuf;
// this is the only system-wide loglevel variable
extern log_level loglevel;
static bool enable_bt_sink;
static bool enable_airplay;

View File

@@ -573,8 +573,9 @@ void draw_VU(struct GDS_Device * display, const uint8_t *data, int level, int x,
// adjust to current display window
if (width > VU_WIDTH) {
if (rotate) y += (width - VU_WIDTH) / 2;
else x += (width - VU_WIDTH) / 2;
width = VU_WIDTH;
x += (width - VU_WIDTH) / 2;
} else {
data += (VU_WIDTH - width) / 2 * VU_HEIGHT;
}

View File

@@ -12,46 +12,163 @@
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <driver/i2s.h>
#include "driver/i2c.h"
#include "esp_log.h"
#include "cJSON.h"
#include "platform_config.h"
#include "adac.h"
static bool init(int i2c_port_num, int i2s_num, i2s_config_t *config);
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) { };
static const char TAG[] = "DAC external";
struct adac_s dac_external = { init, deinit, power, speaker, headset, volume };
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);
static char TAG[] = "DAC external";
static bool i2c_json_execute(char *set);
static esp_err_t i2c_write_reg(uint8_t reg, uint8_t val);
static uint8_t i2c_read_reg(uint8_t reg);
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");
const struct adac_s dac_external = { "i2s", init, deinit, power, speaker, headset, volume };
static int i2c_port, i2c_addr;
static cJSON *i2c_json;
/****************************************************************************************
* init
*/
static bool init(char *config, int i2c_port_num) {
char *p;
i2c_port = i2c_port_num;
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);
}
// 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, NULL);
i2c_json = cJSON_Parse(p);
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);
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;
} 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);
}
ESP_LOGI(TAG, "DAC uses I2C @%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;
}
}
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");
}
/****************************************************************************************
*
*/
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")) {
uint8_t data = i2c_read_reg(reg->valueint);
data |= (uint8_t) val->valueint;
i2c_write_reg(reg->valueint, data);
} else if (!strcasecmp(mode->valuestring, "and")) {
uint8_t data = i2c_read_reg(reg->valueint);
data &= (uint8_t) val->valueint;
i2c_write_reg(reg->valueint, data);
}
}
return true;
}
/****************************************************************************************
*
*/
static esp_err_t i2c_write_reg(uint8_t reg, uint8_t val) {
esp_err_t ret;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, i2c_addr | I2C_MASTER_WRITE, I2C_MASTER_NACK);
i2c_master_write_byte(cmd, reg, I2C_MASTER_NACK);
i2c_master_write_byte(cmd, val, I2C_MASTER_NACK);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if (ret != ESP_OK) {
ESP_LOGW(TAG, "I2C write failed");
}
return ret;
}
/****************************************************************************************
*
*/
static uint8_t i2c_read_reg(uint8_t reg) {
esp_err_t ret;
uint8_t data = 0;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, i2c_addr | I2C_MASTER_WRITE, I2C_MASTER_NACK);
i2c_master_write_byte(cmd, reg, I2C_MASTER_NACK);
i2c_master_start(cmd);
i2c_master_write_byte(cmd, i2c_addr | I2C_MASTER_READ, I2C_MASTER_NACK);
i2c_master_read_byte(cmd, &data, I2C_MASTER_NACK);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if (ret != ESP_OK) {
ESP_LOGW(TAG, "I2C read failed");
}
return data;
}

View File

@@ -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)

View File

@@ -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 bool 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,8 +59,7 @@ 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 void dac_cmd(dac_cmd_e cmd, ...);
@@ -69,19 +68,23 @@ 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);
i2c_param_config(i2c_port, &i2c_config);
i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false);
@@ -89,13 +92,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,32 +104,21 @@ 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", i2c_config.sda_io_num, i2c_config.scl_io_num);
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);
return true;
} else {
LOG_ERROR("could not intialize TAS57xx %d", res);
if (res != ESP_OK) {
ESP_LOGE(TAG, "could not intialize TAS57xx %d", res);
return false;
}
return true;
}
/****************************************************************************************
@@ -141,10 +131,7 @@ 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);
}
static bool volume(unsigned left, unsigned right) { return false; }
/****************************************************************************************
* power
@@ -161,7 +148,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;
}
}
@@ -177,8 +164,7 @@ static void speaker(bool active) {
/****************************************************************************************
* headset
*/
static void headset(bool active) {
}
static void headset(bool active) { }
/****************************************************************************************
* DAC specific commands
@@ -192,7 +178,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 +192,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 +202,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 +221,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];
}
}