more changes to cmd_i2c

This commit is contained in:
Sebastien
2020-01-15 19:29:59 -05:00
parent 7e125729d9
commit da9d4a219e
3 changed files with 162 additions and 75 deletions

View File

@@ -30,7 +30,11 @@ static const char *TAG = "cmd_i2ctools";
static gpio_num_t i2c_gpio_sda = 19; static gpio_num_t i2c_gpio_sda = 19;
static gpio_num_t i2c_gpio_scl = 18; static gpio_num_t i2c_gpio_scl = 18;
static uint32_t i2c_frequency = 100000; 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; static i2c_port_t i2c_port = I2C_NUM_0;
#endif
static esp_err_t i2c_get_port(int port, i2c_port_t *i2c_port) static esp_err_t i2c_get_port(int port, i2c_port_t *i2c_port)
{ {
@@ -51,9 +55,23 @@ static esp_err_t i2c_get_port(int port, i2c_port_t *i2c_port)
} }
return ESP_OK; 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() static esp_err_t i2c_master_driver_initialize()
{ {
esp_err_t err=ESP_OK;
i2c_config_t conf = { i2c_config_t conf = {
.mode = I2C_MODE_MASTER, .mode = I2C_MODE_MASTER,
.sda_io_num = i2c_gpio_sda, .sda_io_num = i2c_gpio_sda,
@@ -62,10 +80,15 @@ static esp_err_t i2c_master_driver_initialize()
.scl_pullup_en = GPIO_PULLUP_ENABLE, .scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = i2c_frequency .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 { static struct {
struct arg_lit *load;
struct arg_int *port; struct arg_int *port;
struct arg_int *freq; struct arg_int *freq;
struct arg_int *sda; struct arg_int *sda;
@@ -76,8 +99,6 @@ static struct {
static struct { static struct {
struct arg_lit *clear; struct arg_lit *clear;
struct arg_int *address; struct arg_int *address;
struct arg_int *sda;
struct arg_int *scl;
struct arg_int *width; struct arg_int *width;
struct arg_int *height; struct arg_int *height;
struct arg_str *name; struct arg_str *name;
@@ -88,6 +109,9 @@ static struct {
static struct { static struct {
struct arg_end *end; struct arg_end *end;
} i2cdisp_show_args; } i2cdisp_show_args;
static int do_i2c_show_display(int argc, char **argv){ static int do_i2c_show_display(int argc, char **argv){
char * config_string = (char * )config_alloc_get(NVS_TYPE_STR, "display_config") ; char * config_string = (char * )config_alloc_get(NVS_TYPE_STR, "display_config") ;
if(config_string){ if(config_string){
@@ -98,12 +122,17 @@ static int do_i2c_show_display(int argc, char **argv){
else { else {
ESP_LOGW(TAG,"No display configuration found in nvs config display_config"); 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; return 0;
} }
static int do_i2c_set_display(int argc, char **argv) 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 * name = strdup("I2S");
char * driver= strdup("SSD136"); char * driver= strdup("SSD136");
char config_string[200]={}; char config_string[200]={};
@@ -118,27 +147,13 @@ static int do_i2c_set_display(int argc, char **argv)
if (i2cdisp_args.clear->count) { if (i2cdisp_args.clear->count) {
ESP_LOGW(TAG,"Clearing display config"); ESP_LOGW(TAG,"Clearing display config");
config_set_value(NVS_TYPE_STR, "display_config", ""); config_set_value(NVS_TYPE_STR, "display_config", "");
return 0;
} }
/* Check "--address" option */ /* Check "--address" option */
if (i2cdisp_args.address->count) { if (i2cdisp_args.address->count) {
address=i2cdisp_args.address->ival[0]; 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];
}
else {
ESP_LOGE(TAG,"Missing parameter: --scl");
nerrors ++;
}
/* Check "--width" option */ /* Check "--width" option */
if (i2cdisp_args.width->count) { if (i2cdisp_args.width->count) {
@@ -163,12 +178,12 @@ static int do_i2c_set_display(int argc, char **argv)
name=strdup(i2cdisp_args.name->sval[0]); name=strdup(i2cdisp_args.name->sval[0]);
} }
/* Check "--name" option */ /* Check "--driver" option */
if (i2cdisp_args.driver->count) { if (i2cdisp_args.driver->count) {
free(driver); free(driver);
driver=strdup(i2cdisp_args.driver->sval[0]); 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(name);
free(driver); 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" ESP_LOGI(TAG,"Updating display configuration string configuration to :\n"
"display_config = \"%s\"",config_string ); "display_config = \"%s\"",config_string );
return config_set_value(NVS_TYPE_STR, "display_config", config_string)!=ESP_OK; return config_set_value(NVS_TYPE_STR, "display_config", config_string)!=ESP_OK;
} }
static void register_i2c_set_display(){ static void register_i2c_set_display(){
i2cdisp_args.address = arg_int0(NULL, "address", "<n>", "Set the I2C bus port number (decimal format, default 120)"); i2cdisp_args.address = arg_int0(NULL, "address", "<n>", "Set the I2C bus port number (decimal format, default 120)");
i2cdisp_args.sda = arg_int0("d", "sda", "<gpio>", "Set the gpio for I2C SDA");
i2cdisp_args.scl = arg_int0("c", "scl", "<gpio>", "Set the gpio for I2C SCL");
i2cdisp_args.width = arg_int0("w", "width", "<n>", "Set the display width"); i2cdisp_args.width = arg_int0("w", "width", "<n>", "Set the display width");
i2cdisp_args.height = arg_int0("h", "height", "<n>", "Set the display height"); i2cdisp_args.height = arg_int0("h", "height", "<n>", "Set the display height");
i2cdisp_args.name = arg_str0("n", "name", "<string>", "Set the display type. Default is I2S"); i2cdisp_args.name = arg_str0("n", "name", "<string>", "Set the display type. Default is I2S");
i2cdisp_args.driver = arg_str0("d", "driver", "<string>", "Set the display driver name"); i2cdisp_args.driver = arg_str0("d", "driver", "<string>", "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_args.end = arg_end(2);
i2cdisp_show_args.end = arg_end(1); i2cdisp_show_args.end = arg_end(1);
const esp_console_cmd_t i2c_set_display= { const esp_console_cmd_t i2c_set_display= {
.command = "set_i2c_display", .command = "setdisplay",
.help="Sets the i2c display options for the board", .help="Sets the display options for the board",
.hint = NULL, .hint = NULL,
.func = &do_i2c_set_display, .func = &do_i2c_set_display,
.argtable = &i2cdisp_args .argtable = &i2cdisp_args
}; };
const esp_console_cmd_t i2c_show_display= { const esp_console_cmd_t i2c_show_display= {
.command = "show_i2c_display", .command = "show_display",
.help="Sets the i2c display options for the board", .help="Shows the board display options and i2c configuration",
.hint = NULL, .hint = NULL,
.func = &do_i2c_show_display, .func = &do_i2c_show_display,
.argtable = &i2cdisp_show_args .argtable = &i2cdisp_show_args
@@ -216,11 +228,29 @@ static void register_i2c_set_display(){
static int do_i2cconfig_cmd(int argc, char **argv) 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); int nerrors = arg_parse(argc, argv, (void **)&i2cconfig_args);
if (nerrors != 0) { if (nerrors != 0) {
arg_print_errors(stderr, i2cconfig_args.end, argv[0]); arg_print_errors(stderr, i2cconfig_args.end, argv[0]);
return 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 */ /* Check "--port" option */
if (i2cconfig_args.port->count) { if (i2cconfig_args.port->count) {
@@ -232,19 +262,63 @@ static int do_i2cconfig_cmd(int argc, char **argv)
if (i2cconfig_args.freq->count) { if (i2cconfig_args.freq->count) {
i2c_frequency = i2cconfig_args.freq->ival[0]; i2c_frequency = i2cconfig_args.freq->ival[0];
} }
if (i2cconfig_args.sda->count){
/* Check "--sda" option */ /* Check "--sda" option */
i2c_gpio_sda = i2cconfig_args.sda->ival[0]; i2c_gpio_sda = i2cconfig_args.sda->ival[0];
/* Check "--scl" option */ }
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]; i2c_gpio_scl = i2cconfig_args.scl->ival[0];
return 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) static void register_i2cconfig(void)
{ {
i2cconfig_args.port = arg_int0(NULL, "port", "<0|1>", "Set the I2C bus port number"); i2cconfig_args.port = arg_int1("p", "port", "<0|1>", "Set the I2C bus port number");
i2cconfig_args.freq = arg_int0(NULL, "freq", "<Hz>", "Set the frequency(Hz) of I2C bus"); i2cconfig_args.freq = arg_int1("f", "freq", "<Hz>", "Set the frequency(Hz) of I2C bus. e.g. 100000");
i2cconfig_args.sda = arg_int1(NULL, "sda", "<gpio>", "Set the gpio for I2C SDA"); i2cconfig_args.sda = arg_int1("d", "sda", "<gpio>", "Set the gpio for I2C SDA. e.g. 19");
i2cconfig_args.scl = arg_int1(NULL, "scl", "<gpio>", "Set the gpio for I2C SCL"); i2cconfig_args.scl = arg_int1("c", "scl", "<gpio>", "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); i2cconfig_args.end = arg_end(2);
const esp_console_cmd_t i2cconfig_cmd = { const esp_console_cmd_t i2cconfig_cmd = {
.command = "i2cconfig", .command = "i2cconfig",
@@ -255,21 +329,20 @@ static void register_i2cconfig(void)
}; };
ESP_ERROR_CHECK(esp_console_cmd_register(&i2cconfig_cmd)); ESP_ERROR_CHECK(esp_console_cmd_register(&i2cconfig_cmd));
} }
#define RUN_SHOW_ERROR(c)
static int do_i2cdetect_cmd(int argc, char **argv) static int do_i2cdetect_cmd(int argc, char **argv)
{ {
i2c_master_driver_initialize(); esp_err_t err=ESP_OK;
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;
}
uint8_t address; uint8_t address;
printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n"); 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) { for (int i = 0; i < 128; i += 16) {
@@ -319,6 +392,7 @@ static struct {
static int do_i2cget_cmd(int argc, char **argv) static int do_i2cget_cmd(int argc, char **argv)
{ {
esp_err_t err=ESP_OK;
int nerrors = arg_parse(argc, argv, (void **)&i2cget_args); int nerrors = arg_parse(argc, argv, (void **)&i2cget_args);
if (nerrors != 0) { if (nerrors != 0) {
arg_print_errors(stderr, i2cget_args.end, argv[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) { if (i2cget_args.data_length->count) {
len = i2cget_args.data_length->ival[0]; 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_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd); i2c_master_start(cmd);
uint8_t *data = malloc(len);
if (data_addr != -1) { if (data_addr != -1) {
i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN); i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN);
i2c_master_write_byte(cmd, data_addr, 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; int len = i2cset_args.data->count;
i2c_master_driver_initialize(); 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_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd); i2c_master_start(cmd);
i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN); 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; return 1;
} }
i2c_master_driver_initialize(); 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_addr;
uint8_t data[4]; uint8_t data[4];
int32_t block[16]; int32_t block[16];

View File

@@ -45,7 +45,7 @@ void display_init(char *welcome) {
char * drivername=strstr(item,"driver"); char * drivername=strstr(item,"driver");
if( !drivername || (drivername && (strstr(drivername,"SSD1306") || strstr(drivername,"ssd1306")))){ if( !drivername || (drivername && (strstr(drivername,"SSD1306") || strstr(drivername,"ssd1306")))){
handle = &SSD1306_handle; handle = &SSD1306_handle;
if (handle->init(item)) { if (handle->init(item, welcome)) {
slimp_handler_chain = slimp_handler; slimp_handler_chain = slimp_handler;
slimp_handler = display_handler; slimp_handler = display_handler;
ESP_LOGI(TAG, "Display initialization successful"); ESP_LOGI(TAG, "Display initialization successful");

View File

@@ -18,7 +18,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
*/ */
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE //#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>