diff --git a/components/cmd_i2c/cmd_i2ctools.c b/components/cmd_i2c/cmd_i2ctools.c index aa64f641..219dd3ee 100644 --- a/components/cmd_i2c/cmd_i2ctools.c +++ b/components/cmd_i2c/cmd_i2ctools.c @@ -30,7 +30,11 @@ static const char *TAG = "cmd_i2ctools"; static gpio_num_t i2c_gpio_sda = 19; static gpio_num_t i2c_gpio_scl = 18; static uint32_t i2c_frequency = 100000; +#ifdef CONFIG_SQUEEZEAMP +static i2c_port_t i2c_port = I2C_NUM_1; +#else static i2c_port_t i2c_port = I2C_NUM_0; +#endif static esp_err_t i2c_get_port(int port, i2c_port_t *i2c_port) { @@ -51,10 +55,24 @@ static esp_err_t i2c_get_port(int port, i2c_port_t *i2c_port) } return ESP_OK; } +static esp_err_t i2c_master_driver_install(){ + esp_err_t err=ESP_OK; + + + if((err=i2c_driver_install(i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0))!=ESP_OK){ + ESP_LOGW(TAG,"i2c driver was already installed. Deleting it."); + i2c_driver_delete(i2c_port); + if((err=i2c_driver_install(i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0))!=ESP_OK){ + ESP_LOGE(TAG,"Driver install failed. %s", esp_err_to_name(err)); + } + } + return err; +} static esp_err_t i2c_master_driver_initialize() { - i2c_config_t conf = { + esp_err_t err=ESP_OK; + i2c_config_t conf = { .mode = I2C_MODE_MASTER, .sda_io_num = i2c_gpio_sda, .sda_pullup_en = GPIO_PULLUP_ENABLE, @@ -62,10 +80,15 @@ static esp_err_t i2c_master_driver_initialize() .scl_pullup_en = GPIO_PULLUP_ENABLE, .master.clk_speed = i2c_frequency }; - return i2c_param_config(i2c_port, &conf); + ESP_LOGI(TAG,"Initializing i2c driver.\n mode = I2C_MODE_MASTER, \n scl_pullup_en = GPIO_PULLUP_ENABLE, \n i2c port = %u, \n sda_io_num = %u, \n sda_pullup_en = GPIO_PULLUP_ENABLE, \n scl_io_num = %u, \n scl_pullup_en = GPIO_PULLUP_ENABLE, \n master.clk_speed = %u", i2c_port, i2c_gpio_sda,i2c_gpio_scl,i2c_frequency); + if((err=i2c_param_config(i2c_port, &conf))!=ESP_OK){ + ESP_LOGE(TAG,"i2c driver initialized failed. %s", esp_err_to_name(err)); + } + return err; } static struct { + struct arg_lit *load; struct arg_int *port; struct arg_int *freq; struct arg_int *sda; @@ -76,8 +99,6 @@ static struct { static struct { struct arg_lit *clear; struct arg_int *address; - struct arg_int *sda; - struct arg_int *scl; struct arg_int *width; struct arg_int *height; struct arg_str *name; @@ -88,6 +109,9 @@ static struct { static struct { struct arg_end *end; } i2cdisp_show_args; + + + static int do_i2c_show_display(int argc, char **argv){ char * config_string = (char * )config_alloc_get(NVS_TYPE_STR, "display_config") ; if(config_string){ @@ -98,12 +122,17 @@ static int do_i2c_show_display(int argc, char **argv){ else { ESP_LOGW(TAG,"No display configuration found in nvs config display_config"); } + char * nvs_item = config_alloc_get(NVS_TYPE_STR, "i2c_config"); + if (nvs_item) { + ESP_LOGI(TAG,"I2C configuration is: %s", nvs_item); + free(nvs_item); + } return 0; } static int do_i2c_set_display(int argc, char **argv) { - int sda = 0, scl=0, width=0, height=0, address=120; + int width=0, height=0, address=120; char * name = strdup("I2S"); char * driver= strdup("SSD136"); char config_string[200]={}; @@ -114,61 +143,47 @@ static int do_i2c_set_display(int argc, char **argv) } - /* Check "--clear" option */ - if (i2cdisp_args.clear->count) { - ESP_LOGW(TAG,"Clearing display config"); - config_set_value(NVS_TYPE_STR, "display_config", ""); - } - /* Check "--address" option */ - if (i2cdisp_args.address->count) { - address=i2cdisp_args.address->ival[0]; - } - /* Check "--sda" option */ - if (i2cdisp_args.sda->count) { - sda=i2cdisp_args.sda->ival[0]; - } - else { - ESP_LOGE(TAG,"Missing parameter: --sda"); - nerrors ++; - } - /* Check "--scl" option */ - if (i2cdisp_args.scl->count) { - scl=i2cdisp_args.scl->ival[0]; + /* Check "--clear" option */ + if (i2cdisp_args.clear->count) { + ESP_LOGW(TAG,"Clearing display config"); + config_set_value(NVS_TYPE_STR, "display_config", ""); + return 0; + } + + /* Check "--address" option */ + if (i2cdisp_args.address->count) { + address=i2cdisp_args.address->ival[0]; } - else { - ESP_LOGE(TAG,"Missing parameter: --scl"); - nerrors ++; - } /* Check "--width" option */ if (i2cdisp_args.width->count) { width=i2cdisp_args.width->ival[0]; } - else { - ESP_LOGE(TAG,"Missing parameter: --width"); - nerrors ++; - } + else { + ESP_LOGE(TAG,"Missing parameter: --width"); + nerrors ++; + } /* Check "--height" option */ if (i2cdisp_args.height->count) { height=i2cdisp_args.height->ival[0]; } - else { - ESP_LOGE(TAG,"Missing parameter: --height"); - nerrors ++; - } + else { + ESP_LOGE(TAG,"Missing parameter: --height"); + nerrors ++; + } /* Check "--name" option */ if (i2cdisp_args.name->count) { free(name); name=strdup(i2cdisp_args.name->sval[0]); } - /* Check "--name" option */ + /* Check "--driver" option */ if (i2cdisp_args.driver->count) { free(driver); driver=strdup(i2cdisp_args.driver->sval[0]); } - snprintf(config_string, sizeof(config_string),"%s:scl=%i,sda=%i,width=%i,height=%i,address=%i,driver=%s",name,scl,sda,width,height,address,driver ); + snprintf(config_string, sizeof(config_string),"%s:width=%i,height=%i,address=%i,driver=%s",name,width,height,address,driver ); free(name); free(driver); @@ -179,31 +194,28 @@ static int do_i2c_set_display(int argc, char **argv) ESP_LOGI(TAG,"Updating display configuration string configuration to :\n" "display_config = \"%s\"",config_string ); - return config_set_value(NVS_TYPE_STR, "display_config", config_string)!=ESP_OK; } static void register_i2c_set_display(){ i2cdisp_args.address = arg_int0(NULL, "address", "", "Set the I2C bus port number (decimal format, default 120)"); - i2cdisp_args.sda = arg_int0("d", "sda", "", "Set the gpio for I2C SDA"); - i2cdisp_args.scl = arg_int0("c", "scl", "", "Set the gpio for I2C SCL"); i2cdisp_args.width = arg_int0("w", "width", "", "Set the display width"); i2cdisp_args.height = arg_int0("h", "height", "", "Set the display height"); i2cdisp_args.name = arg_str0("n", "name", "", "Set the display type. Default is I2S"); i2cdisp_args.driver = arg_str0("d", "driver", "", "Set the display driver name"); - i2cdisp_args.clear = arg_litn(NULL, "clear", 0, 1, "clear configuration"); + i2cdisp_args.clear = arg_litn("c", "clear", 0, 1, "clear configuration and return"); i2cdisp_args.end = arg_end(2); i2cdisp_show_args.end = arg_end(1); const esp_console_cmd_t i2c_set_display= { - .command = "set_i2c_display", - .help="Sets the i2c display options for the board", + .command = "setdisplay", + .help="Sets the display options for the board", .hint = NULL, .func = &do_i2c_set_display, .argtable = &i2cdisp_args }; const esp_console_cmd_t i2c_show_display= { - .command = "show_i2c_display", - .help="Sets the i2c display options for the board", + .command = "show_display", + .help="Shows the board display options and i2c configuration", .hint = NULL, .func = &do_i2c_show_display, .argtable = &i2cdisp_show_args @@ -216,11 +228,29 @@ static void register_i2c_set_display(){ static int do_i2cconfig_cmd(int argc, char **argv) { + esp_err_t err=ESP_OK; + int res=0; int nerrors = arg_parse(argc, argv, (void **)&i2cconfig_args); if (nerrors != 0) { arg_print_errors(stderr, i2cconfig_args.end, argv[0]); return 0; } + /* Check "--load" option */ + if (i2cconfig_args.load->count) { + ESP_LOGW(TAG,"Clearing display config"); + char * nvs_item = config_alloc_get(NVS_TYPE_STR, "i2c_config"); + if (nvs_item) { + char * p=NULL; + ESP_LOGI(TAG,"Loading I2C configuration : %s", nvs_item); + if ((p = strcasestr(nvs_item, "scl")) != NULL) i2c_gpio_scl = atoi(strchr(p, '=') + 1); + if ((p = strcasestr(nvs_item, "sda")) != NULL) i2c_gpio_sda = atoi(strchr(p, '=') + 1); + if ((p = strcasestr(nvs_item, "speed")) != NULL) i2c_frequency = atoi(strchr(p, '=') + 1); + if ((p = strcasestr(nvs_item, "port")) != NULL) i2c_port = atoi(strchr(p, '=') + 1); + + free(nvs_item); + } + + } /* Check "--port" option */ if (i2cconfig_args.port->count) { @@ -232,19 +262,63 @@ static int do_i2cconfig_cmd(int argc, char **argv) if (i2cconfig_args.freq->count) { i2c_frequency = i2cconfig_args.freq->ival[0]; } - /* Check "--sda" option */ - i2c_gpio_sda = i2cconfig_args.sda->ival[0]; - /* Check "--scl" option */ - i2c_gpio_scl = i2cconfig_args.scl->ival[0]; - return 0; + if (i2cconfig_args.sda->count){ + /* Check "--sda" option */ + i2c_gpio_sda = i2cconfig_args.sda->ival[0]; + } + else { + ESP_LOGE(TAG,"Missing --sda option."); + res=1; + } + + if (i2cconfig_args.scl->count){ + /* Check "--sda" option */ + i2c_gpio_scl = i2cconfig_args.scl->ival[0]; + } + else { + ESP_LOGE(TAG,"Missing --scl option."); + res=1; + } + + +#ifdef CONFIG_SQUEEZEAMP + if (i2c_port == I2C_NUM_0) { + i2c_port = I2C_NUM_1; + ESP_LOGE(TAG, "can't use i2c port 0 on SqueezeAMP. Changing to port 1."); + } +#endif + if(!res){ + int buffer_size=255; + char * config_buffer=malloc(buffer_size); + memset(config_buffer,0x00,buffer_size); + snprintf(config_buffer,buffer_size,"scl=%u sda=%u speed=%u port=%u",i2c_gpio_scl,i2c_gpio_sda,i2c_frequency,i2c_port); + + ESP_LOGI(TAG,"Initializing driver with config %s", config_buffer); + if((err=i2c_master_driver_initialize())==ESP_OK){ + ESP_LOGI(TAG,"Initalize success. Storing config to nvs"); + config_set_value(NVS_TYPE_STR, "i2c_config", config_buffer); + ESP_LOGI(TAG,"Starting the i2s driver."); + // now start the i2c driver + if((err=i2c_master_driver_install())!=ESP_OK){ + res=1; + } + } + else { + ESP_LOGE(TAG,"I2C initialization failed. %s", esp_err_to_name(err)); + res=1; + } + if(config_buffer) free(config_buffer); + } + return res; } static void register_i2cconfig(void) { - i2cconfig_args.port = arg_int0(NULL, "port", "<0|1>", "Set the I2C bus port number"); - i2cconfig_args.freq = arg_int0(NULL, "freq", "", "Set the frequency(Hz) of I2C bus"); - i2cconfig_args.sda = arg_int1(NULL, "sda", "", "Set the gpio for I2C SDA"); - i2cconfig_args.scl = arg_int1(NULL, "scl", "", "Set the gpio for I2C SCL"); + i2cconfig_args.port = arg_int1("p", "port", "<0|1>", "Set the I2C bus port number"); + i2cconfig_args.freq = arg_int1("f", "freq", "", "Set the frequency(Hz) of I2C bus. e.g. 100000"); + i2cconfig_args.sda = arg_int1("d", "sda", "", "Set the gpio for I2C SDA. e.g. 19"); + i2cconfig_args.scl = arg_int1("c", "scl", "", "Set the gpio for I2C SCL. e.g. 18"); + i2cconfig_args.load = arg_litn("l", "load", 0, 1, "load existing configuration and return"); i2cconfig_args.end = arg_end(2); const esp_console_cmd_t i2cconfig_cmd = { .command = "i2cconfig", @@ -255,21 +329,20 @@ static void register_i2cconfig(void) }; ESP_ERROR_CHECK(esp_console_cmd_register(&i2cconfig_cmd)); } - - - - - - - - - - +#define RUN_SHOW_ERROR(c) static int do_i2cdetect_cmd(int argc, char **argv) { - i2c_master_driver_initialize(); - i2c_driver_install(i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0); + esp_err_t err=ESP_OK; + + if((err=i2c_master_driver_initialize())!=ESP_OK){ + ESP_LOGE(TAG,"Error initializing i2c driver. %s",esp_err_to_name(err)); + return 1; + } + if((err=i2c_master_driver_install())!=ESP_OK){ + return 1; + } + uint8_t address; printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n"); for (int i = 0; i < 128; i += 16) { @@ -319,6 +392,7 @@ static struct { static int do_i2cget_cmd(int argc, char **argv) { + esp_err_t err=ESP_OK; int nerrors = arg_parse(argc, argv, (void **)&i2cget_args); if (nerrors != 0) { arg_print_errors(stderr, i2cget_args.end, argv[0]); @@ -337,12 +411,19 @@ static int do_i2cget_cmd(int argc, char **argv) if (i2cget_args.data_length->count) { len = i2cget_args.data_length->ival[0]; } - uint8_t *data = malloc(len); - i2c_master_driver_initialize(); - i2c_driver_install(i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0); + + if((err=i2c_master_driver_initialize())!=ESP_OK){ + ESP_LOGE(TAG,"Error initializing i2c driver. %s",esp_err_to_name(err)); + return 1; + } + if((err=i2c_master_driver_install())!=ESP_OK){ + return 1; + } + i2c_cmd_handle_t cmd = i2c_cmd_link_create(); i2c_master_start(cmd); + uint8_t *data = malloc(len); if (data_addr != -1) { i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN); i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN); @@ -418,7 +499,10 @@ static int do_i2cset_cmd(int argc, char **argv) int len = i2cset_args.data->count; i2c_master_driver_initialize(); - i2c_driver_install(i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0); + if(i2c_master_driver_install()!=ESP_OK){ + return 1; + } + i2c_cmd_handle_t cmd = i2c_cmd_link_create(); i2c_master_start(cmd); i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN); @@ -484,7 +568,10 @@ static int do_i2cdump_cmd(int argc, char **argv) return 1; } i2c_master_driver_initialize(); - i2c_driver_install(i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0); + if(i2c_master_driver_install()!=ESP_OK){ + return 1; + } + uint8_t data_addr; uint8_t data[4]; int32_t block[16]; diff --git a/components/display/display.c b/components/display/display.c index b94206a1..6f46c267 100644 --- a/components/display/display.c +++ b/components/display/display.c @@ -45,7 +45,7 @@ void display_init(char *welcome) { char * drivername=strstr(item,"driver"); if( !drivername || (drivername && (strstr(drivername,"SSD1306") || strstr(drivername,"ssd1306")))){ handle = &SSD1306_handle; - if (handle->init(item)) { + if (handle->init(item, welcome)) { slimp_handler_chain = slimp_handler; slimp_handler = display_handler; ESP_LOGI(TAG, "Display initialization successful"); diff --git a/components/services/audio_controls.c b/components/services/audio_controls.c index d9e2b6b5..c504762e 100644 --- a/components/services/audio_controls.c +++ b/components/services/audio_controls.c @@ -18,7 +18,7 @@ * along with this program. If not, see . * */ -#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE +//#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE #include #include #include