mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-06 19:47:02 +03:00
retrofit to gcc8
This commit is contained in:
22
components/platform_console/cmd_decl.h
Normal file
22
components/platform_console/cmd_decl.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/* Console example — declarations of command registration functions.
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "cmd_system.h"
|
||||
#include "cmd_wifi.h"
|
||||
#include "cmd_nvs.h"
|
||||
#include "cmd_i2ctools.h"
|
||||
#include "cmd_ota.h"
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
919
components/platform_console/cmd_i2ctools.c
Normal file
919
components/platform_console/cmd_i2ctools.c
Normal file
@@ -0,0 +1,919 @@
|
||||
/* cmd_i2ctools.c
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
|
||||
#include <stdio.h>
|
||||
#include "cmd_i2ctools.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "driver/i2c.h"
|
||||
#include "esp_console.h"
|
||||
#include "esp_log.h"
|
||||
#include "string.h"
|
||||
#include "stdio.h"
|
||||
#include "platform_config.h"
|
||||
#include "accessors.h"
|
||||
|
||||
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
|
||||
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
|
||||
#define WRITE_BIT I2C_MASTER_WRITE /*!< I2C master write */
|
||||
#define READ_BIT I2C_MASTER_READ /*!< I2C master read */
|
||||
#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
|
||||
#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
|
||||
#define ACK_VAL 0x0 /*!< I2C ack value */
|
||||
#define NACK_VAL 0x1 /*!< I2C nack value */
|
||||
|
||||
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_I2C_LOCKED
|
||||
static i2c_port_t i2c_port = I2C_NUM_1;
|
||||
#else
|
||||
static i2c_port_t i2c_port = I2C_NUM_0;
|
||||
#endif
|
||||
|
||||
static struct {
|
||||
struct arg_int *chip_address;
|
||||
struct arg_int *register_address;
|
||||
struct arg_int *data_length;
|
||||
struct arg_end *end;
|
||||
} i2cget_args;
|
||||
|
||||
static struct {
|
||||
struct arg_int *chip_address;
|
||||
struct arg_int *register_address;
|
||||
struct arg_int *data;
|
||||
struct arg_end *end;
|
||||
} i2cset_args;
|
||||
|
||||
static struct {
|
||||
struct arg_int *chip_address;
|
||||
struct arg_int *size;
|
||||
struct arg_end *end;
|
||||
} i2cdump_args;
|
||||
|
||||
static struct {
|
||||
struct arg_lit *load;
|
||||
struct arg_int *port;
|
||||
struct arg_int *freq;
|
||||
struct arg_int *sda;
|
||||
struct arg_int *scl;
|
||||
struct arg_end *end;
|
||||
} i2cconfig_args;
|
||||
|
||||
|
||||
static struct {
|
||||
struct arg_int *port;
|
||||
struct arg_end *end;
|
||||
} i2cstop_args;
|
||||
|
||||
static struct {
|
||||
struct arg_int *port;
|
||||
struct arg_end *end;
|
||||
} i2ccheck_args;
|
||||
|
||||
static struct {
|
||||
struct arg_lit *clear;
|
||||
struct arg_lit *hflip;
|
||||
struct arg_lit *vflip;
|
||||
struct arg_lit *rotate;
|
||||
struct arg_int *address;
|
||||
struct arg_int *width;
|
||||
struct arg_int *height;
|
||||
struct arg_str *name;
|
||||
struct arg_str *driver;
|
||||
struct arg_end *end;
|
||||
} i2cdisp_args;
|
||||
|
||||
|
||||
bool is_i2c_started(i2c_port_t port){
|
||||
esp_err_t ret = ESP_OK;
|
||||
ESP_LOGD(TAG,"Determining if i2c is started on port %u", port);
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
ret = i2c_master_start(cmd);
|
||||
if(ret == ESP_OK){
|
||||
ret = i2c_master_write_byte(cmd,WRITE_BIT, ACK_CHECK_EN);
|
||||
}
|
||||
if(ret == ESP_OK){
|
||||
ret = i2c_master_stop(cmd);
|
||||
}
|
||||
if(ret == ESP_OK){
|
||||
ret = i2c_master_cmd_begin(port, cmd, 50 / portTICK_RATE_MS);
|
||||
}
|
||||
i2c_cmd_link_delete(cmd);
|
||||
ESP_LOGD(TAG,"i2c is %s. %s",ret!=ESP_ERR_INVALID_STATE?"started":"not started", esp_err_to_name(ret));
|
||||
return (ret!=ESP_ERR_INVALID_STATE);
|
||||
}
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint8_t address;
|
||||
char * description;
|
||||
} i2c_db_t;
|
||||
|
||||
|
||||
// the list was taken from https://i2cdevices.org/addresses
|
||||
// on 2020-01-16
|
||||
static const i2c_db_t i2c_db[] = {
|
||||
{ .address = 0x00, .description = "Unknown"},
|
||||
{ .address = 0x01, .description = "Unknown"},
|
||||
{ .address = 0x02, .description = "Unknown"},
|
||||
{ .address = 0x03, .description = "Unknown"},
|
||||
{ .address = 0x04, .description = "Unknown"},
|
||||
{ .address = 0x05, .description = "Unknown"},
|
||||
{ .address = 0x06, .description = "Unknown"},
|
||||
{ .address = 0x07, .description = "Unknown"},
|
||||
{ .address = 0x0c, .description = "AK8975"},
|
||||
{ .address = 0x0d, .description = "AK8975"},
|
||||
{ .address = 0x0e, .description = "MAG3110 AK8975 IST-8310"},
|
||||
{ .address = 0x0f, .description = "AK8975"},
|
||||
{ .address = 0x10, .description = "VEML7700 VML6075"},
|
||||
{ .address = 0x11, .description = "Si4713 SAA5246 SAA5243P/K SAA5243P/L SAA5243P/E SAA5243P/H"},
|
||||
{ .address = 0x13, .description = "VCNL40x0"},
|
||||
{ .address = 0x18, .description = "MCP9808 LIS3DH LSM303"},
|
||||
{ .address = 0x19, .description = "MCP9808 LIS3DH LSM303"},
|
||||
{ .address = 0x1a, .description = "MCP9808"},
|
||||
{ .address = 0x1b, .description = "MCP9808"},
|
||||
{ .address = 0x1c, .description = "MCP9808 MMA845x FXOS8700"},
|
||||
{ .address = 0x1d, .description = "MCP9808 MMA845x ADXL345 FXOS8700"},
|
||||
{ .address = 0x1e, .description = "MCP9808 FXOS8700 HMC5883 LSM303 LSM303"},
|
||||
{ .address = 0x1f, .description = "MCP9808 FXOS8700"},
|
||||
{ .address = 0x20, .description = "FXAS21002 MCP23008 MCP23017 Chirp!"},
|
||||
{ .address = 0x21, .description = "FXAS21002 MCP23008 MCP23017 SAA4700"},
|
||||
{ .address = 0x22, .description = "MCP23008 MCP23017 PCA1070"},
|
||||
{ .address = 0x23, .description = "MCP23008 MCP23017 SAA4700"},
|
||||
{ .address = 0x24, .description = "MCP23008 MCP23017 PCD3311C PCD3312C"},
|
||||
{ .address = 0x25, .description = "MCP23008 MCP23017 PCD3311C PCD3312C"},
|
||||
{ .address = 0x26, .description = "MCP23008 MCP23017"},
|
||||
{ .address = 0x27, .description = "MCP23008 MCP23017"},
|
||||
{ .address = 0x28, .description = "BNO055 CAP1188"},
|
||||
{ .address = 0x29, .description = "BNO055 CAP1188 TCS34725 TSL2591 VL53L0x VL6180X"},
|
||||
{ .address = 0x2a, .description = "CAP1188"},
|
||||
{ .address = 0x2b, .description = "CAP1188"},
|
||||
{ .address = 0x2c, .description = "CAP1188 AD5248 AD5251 AD5252 CAT5171"},
|
||||
{ .address = 0x2d, .description = "CAP1188 AD5248 AD5251 AD5252 CAT5171"},
|
||||
{ .address = 0x2e, .description = "AD5248 AD5251 AD5252"},
|
||||
{ .address = 0x2f, .description = "AD5248 AD5243 AD5251 AD5252"},
|
||||
{ .address = 0x30, .description = "SAA2502"},
|
||||
{ .address = 0x31, .description = "SAA2502"},
|
||||
{ .address = 0x38, .description = "FT6x06 VEML6070 BMA150 SAA1064"},
|
||||
{ .address = 0x39, .description = "TSL2561 APDS-9960 VEML6070 SAA1064"},
|
||||
{ .address = 0x3a, .description = "PCF8577C SAA1064"},
|
||||
{ .address = 0x3b, .description = "SAA1064 PCF8569"},
|
||||
{ .address = 0x3c, .description = "SSD1305 SSD1306 PCF8578 PCF8569 SH1106"},
|
||||
{ .address = 0x3d, .description = "SSD1305 SSD1306 PCF8578 SH1106"},
|
||||
{ .address = 0x40, .description = "HTU21D-F TMP007 PCA9685 NE5751 TDA8421 INA260 TEA6320 TEA6330 TMP006 TEA6300 Si7021 INA219 TDA9860"},
|
||||
{ .address = 0x41, .description = "TMP007 PCA9685 STMPE811 TDA8424 NE5751 TDA8421 INA260 STMPE610 TDA8425 TMP006 INA219 TDA9860 TDA8426"},
|
||||
{ .address = 0x42, .description = "HDC1008 TMP007 TMP006 PCA9685 INA219 TDA8415 TDA8417 INA260"},
|
||||
{ .address = 0x43, .description = "HDC1008 TMP007 TMP006 PCA9685 INA219 INA260"},
|
||||
{ .address = 0x44, .description = "TMP007 TMP006 PCA9685 INA219 STMPE610 SHT31 ISL29125 STMPE811 TDA4688 TDA4672 TDA4780 TDA4670 TDA8442 TDA4687 TDA4671 TDA4680 INA260"},
|
||||
{ .address = 0x45, .description = "TMP007 TMP006 PCA9685 INA219 SHT31 TDA8376 INA260"},
|
||||
{ .address = 0x46, .description = "TMP007 TMP006 PCA9685 INA219 TDA9150 TDA8370 INA260"},
|
||||
{ .address = 0x47, .description = "TMP007 TMP006 PCA9685 INA219 INA260"},
|
||||
{ .address = 0x48, .description = "PCA9685 INA219 PN532 TMP102 INA260 ADS1115"},
|
||||
{ .address = 0x49, .description = "TSL2561 PCA9685 INA219 TMP102 INA260 ADS1115 AS7262"},
|
||||
{ .address = 0x4a, .description = "PCA9685 INA219 TMP102 ADS1115 MAX44009 INA260"},
|
||||
{ .address = 0x4b, .description = "PCA9685 INA219 TMP102 ADS1115 MAX44009 INA260"},
|
||||
{ .address = 0x4c, .description = "PCA9685 INA219 INA260"},
|
||||
{ .address = 0x4d, .description = "PCA9685 INA219 INA260"},
|
||||
{ .address = 0x4e, .description = "PCA9685 INA219 INA260"},
|
||||
{ .address = 0x4f, .description = "PCA9685 INA219 INA260"},
|
||||
{ .address = 0x50, .description = "PCA9685 MB85RC"},
|
||||
{ .address = 0x51, .description = "PCA9685 MB85RC"},
|
||||
{ .address = 0x52, .description = "PCA9685 MB85RC Nunchuck controller APDS-9250"},
|
||||
{ .address = 0x53, .description = "ADXL345 PCA9685 MB85RC"},
|
||||
{ .address = 0x54, .description = "PCA9685 MB85RC"},
|
||||
{ .address = 0x55, .description = "PCA9685 MB85RC"},
|
||||
{ .address = 0x56, .description = "PCA9685 MB85RC"},
|
||||
{ .address = 0x57, .description = "PCA9685 MB85RC MAX3010x"},
|
||||
{ .address = 0x58, .description = "PCA9685 TPA2016 SGP30"},
|
||||
{ .address = 0x59, .description = "PCA9685"},
|
||||
{ .address = 0x5a, .description = "PCA9685 CCS811 MLX90614 DRV2605 MPR121"},
|
||||
{ .address = 0x5b, .description = "PCA9685 CCS811 MPR121"},
|
||||
{ .address = 0x5c, .description = "PCA9685 AM2315 MPR121"},
|
||||
{ .address = 0x5d, .description = "PCA9685 MPR121"},
|
||||
{ .address = 0x5e, .description = "PCA9685"},
|
||||
{ .address = 0x5f, .description = "PCA9685 HTS221"},
|
||||
{ .address = 0x60, .description = "PCA9685 MPL115A2 MPL3115A2 Si5351A Si1145 MCP4725A0 TEA5767 TSA5511 SAB3037 SAB3035 MCP4725A1"},
|
||||
{ .address = 0x61, .description = "PCA9685 Si5351A MCP4725A0 TEA6100 TSA5511 SAB3037 SAB3035 MCP4725A1"},
|
||||
{ .address = 0x62, .description = "PCA9685 MCP4725A1 TSA5511 SAB3037 SAB3035 UMA1014T"},
|
||||
{ .address = 0x63, .description = "Si4713 PCA9685 MCP4725A1 TSA5511 SAB3037 SAB3035 UMA1014T"},
|
||||
{ .address = 0x64, .description = "PCA9685 MCP4725A2 MCP4725A1"},
|
||||
{ .address = 0x65, .description = "PCA9685 MCP4725A2 MCP4725A1"},
|
||||
{ .address = 0x66, .description = "PCA9685 MCP4725A3 IS31FL3731 MCP4725A1"},
|
||||
{ .address = 0x67, .description = "PCA9685 MCP4725A3 MCP4725A1"},
|
||||
{ .address = 0x68, .description = "PCA9685 AMG8833 DS1307 PCF8523 DS3231 MPU-9250 ITG3200 PCF8573 MPU6050"},
|
||||
{ .address = 0x69, .description = "PCA9685 AMG8833 MPU-9250 ITG3200 PCF8573 SPS30 MPU6050"},
|
||||
{ .address = 0x6a, .description = "PCA9685 L3GD20H PCF8573"},
|
||||
{ .address = 0x6b, .description = "PCA9685 L3GD20H PCF8573"},
|
||||
{ .address = 0x6c, .description = "PCA9685"},
|
||||
{ .address = 0x6d, .description = "PCA9685"},
|
||||
{ .address = 0x6e, .description = "PCA9685"},
|
||||
{ .address = 0x6f, .description = "PCA9685"},
|
||||
{ .address = 0x70, .description = "PCA9685 TCA9548 HT16K33"},
|
||||
{ .address = 0x71, .description = "PCA9685 TCA9548 HT16K33"},
|
||||
{ .address = 0x72, .description = "PCA9685 TCA9548 HT16K33"},
|
||||
{ .address = 0x73, .description = "PCA9685 TCA9548 HT16K33"},
|
||||
{ .address = 0x74, .description = "PCA9685 TCA9548 HT16K33"},
|
||||
{ .address = 0x75, .description = "PCA9685 TCA9548 HT16K33"},
|
||||
{ .address = 0x76, .description = "PCA9685 TCA9548 HT16K33 BME280 BMP280 MS5607 MS5611 BME680"},
|
||||
{ .address = 0x77, .description = "PCA9685 TCA9548 HT16K33 IS31FL3731 BME280 BMP280 MS5607 BMP180 BMP085 BMA180 MS5611 BME680"},
|
||||
{ .address = 0x78, .description = "PCA9685"},
|
||||
{ .address = 0x79, .description = "PCA9685"},
|
||||
{ .address = 0x7a, .description = "PCA9685"},
|
||||
{ .address = 0x7b, .description = "PCA9685"},
|
||||
{ .address = 0x7c, .description = "PCA9685"},
|
||||
{ .address = 0x7d, .description = "PCA9685"},
|
||||
{ .address = 0x7e, .description = "PCA9685"},
|
||||
{ .address = 0x7f, .description = "PCA9685"},
|
||||
{ .address = 0, .description = NULL}
|
||||
};
|
||||
void i2c_load_configuration(){
|
||||
ESP_LOGD(TAG,"Loading configuration from nvs");
|
||||
const i2c_config_t * conf = config_i2c_get((int *)&i2c_port);
|
||||
i2c_gpio_scl = conf->scl_io_num;
|
||||
i2c_gpio_sda = conf->sda_io_num;
|
||||
i2c_frequency = conf->master.clk_speed;
|
||||
}
|
||||
|
||||
const char * i2c_get_description(uint8_t address){
|
||||
uint8_t i=0;
|
||||
while(i2c_db[i].description && i2c_db[i].address!=address) i++;
|
||||
return i2c_db[i].description?i2c_db[i].description:"Unlisted";
|
||||
}
|
||||
|
||||
static esp_err_t i2c_get_port(int port, i2c_port_t *i2c_port)
|
||||
{
|
||||
if (port >= I2C_NUM_MAX) {
|
||||
ESP_LOGE(TAG, "Wrong port number: %d", port);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
switch (port) {
|
||||
case 0:
|
||||
*i2c_port = I2C_NUM_0;
|
||||
break;
|
||||
case 1:
|
||||
*i2c_port = I2C_NUM_1;
|
||||
break;
|
||||
default:
|
||||
*i2c_port = I2C_NUM_0;
|
||||
break;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
static esp_err_t i2c_master_driver_install(){
|
||||
esp_err_t err=ESP_OK;
|
||||
ESP_LOGD(TAG,"Installing i2c driver on port %u", 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()
|
||||
{
|
||||
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,
|
||||
.scl_io_num = i2c_gpio_scl,
|
||||
.scl_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.master.clk_speed = i2c_frequency
|
||||
};
|
||||
ESP_LOGI(TAG,"Initializing i2c driver configuration.\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 config load failed. %s", esp_err_to_name(err));
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_err_t i2c_initialize_driver_from_config(){
|
||||
esp_err_t err = ESP_OK;
|
||||
ESP_LOGD(TAG,"Initializing driver from configuration.");
|
||||
i2c_load_configuration();
|
||||
if(is_i2c_started(i2c_port)){
|
||||
ESP_LOGW(TAG, "Stopping i2c driver on port %u", i2c_port);
|
||||
// stop the current driver instance
|
||||
if((err=i2c_driver_delete(i2c_port))!=ESP_OK){
|
||||
ESP_LOGE(TAG,"i2c driver delete failed. %s", esp_err_to_name(err));
|
||||
}
|
||||
}
|
||||
if(err==ESP_OK){
|
||||
err = i2c_master_driver_initialize();
|
||||
}
|
||||
if(err == ESP_OK){
|
||||
err = i2c_master_driver_install();
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
static int do_i2c_stop(int argc, char **argv ){
|
||||
|
||||
int nerrors = arg_parse(argc, argv, (void **)&i2cstop_args);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, i2cstop_args.end, argv[0]);
|
||||
return 0;
|
||||
}
|
||||
if (i2cstop_args.port->count && i2c_get_port(i2cstop_args.port->ival[0], &i2c_port) != ESP_OK) {
|
||||
return 1;
|
||||
}
|
||||
ESP_LOGW(TAG,"Stopping i2c on port %u.",i2c_port);
|
||||
i2c_driver_delete(i2c_port);
|
||||
return 0;
|
||||
}
|
||||
static int do_i2c_check(int argc, char **argv ){
|
||||
|
||||
i2c_port_t port=0;
|
||||
int nerrors = arg_parse(argc, argv, (void **)&i2ccheck_args);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, i2ccheck_args.end, argv[0]);
|
||||
return 0;
|
||||
}
|
||||
port=i2c_port;
|
||||
|
||||
if (i2ccheck_args.port->count && i2c_get_port(i2ccheck_args.port->ival[0], &port) != ESP_OK) {
|
||||
return 1;
|
||||
}
|
||||
bool started=is_i2c_started(port);
|
||||
ESP_LOGI(TAG,"i2c is %s on port %u.", started?"started":"not started",port );
|
||||
return 0;
|
||||
}
|
||||
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){
|
||||
ESP_LOGI(TAG,"Display configuration string is : \n"
|
||||
"display_config = \"%s\"",config_string);
|
||||
free(config_string);
|
||||
}
|
||||
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 width=0, height=0, address=60;
|
||||
char * name = NULL;
|
||||
char * driver= NULL;
|
||||
char config_string[200]={};
|
||||
int nerrors = arg_parse(argc, argv, (void **)&i2cdisp_args);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, i2cdisp_args.end, argv[0]);
|
||||
return 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];
|
||||
}
|
||||
|
||||
/* Check "--width" option */
|
||||
if (i2cdisp_args.width->count) {
|
||||
width=i2cdisp_args.width->ival[0];
|
||||
}
|
||||
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 ++;
|
||||
}
|
||||
/* Check "--name" option */
|
||||
if (i2cdisp_args.name->count) {
|
||||
name=strdup(i2cdisp_args.name->sval[0]);
|
||||
}
|
||||
|
||||
/* Check "--driver" option */
|
||||
if (i2cdisp_args.driver->count) {
|
||||
driver=strdup(i2cdisp_args.driver->sval[0]);
|
||||
}
|
||||
|
||||
if(!name) name = strdup("I2C");
|
||||
if(!driver) driver = strdup("SSD1306");
|
||||
|
||||
bool rotate = i2cdisp_args.rotate->count>0;
|
||||
|
||||
snprintf(config_string, sizeof(config_string),"%s:width=%i,height=%i,address=%i,driver=%s%s%s",
|
||||
name,width,height,address,driver,rotate || i2cdisp_args.hflip->count?",HFlip":"",rotate || i2cdisp_args.vflip->count?",VFlip":"" );
|
||||
free(name);
|
||||
free(driver);
|
||||
|
||||
if(nerrors!=0){
|
||||
return 0;
|
||||
}
|
||||
|
||||
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 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,"Loading i2c config");
|
||||
i2c_load_configuration();
|
||||
}
|
||||
else {
|
||||
|
||||
/* Check "--port" option */
|
||||
if (i2cconfig_args.port->count) {
|
||||
if (i2c_get_port(i2cconfig_args.port->ival[0], &i2c_port) != ESP_OK) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
/* Check "--freq" option */
|
||||
if (i2cconfig_args.freq->count) {
|
||||
i2c_frequency = i2cconfig_args.freq->ival[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){
|
||||
ESP_LOGI(TAG, "Uninstall i2c driver from port %u if needed",i2c_port);
|
||||
if(is_i2c_started(i2c_port)){
|
||||
if((err=i2c_driver_delete(i2c_port))!=ESP_OK){
|
||||
ESP_LOGE(TAG,"i2c driver delete failed. %s", esp_err_to_name(err));
|
||||
res = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!res){
|
||||
ESP_LOGI(TAG,"Initializing driver with config scl=%u sda=%u speed=%u port=%u",i2c_gpio_scl,i2c_gpio_sda,i2c_frequency,i2c_port);
|
||||
if((err=i2c_master_driver_initialize())==ESP_OK){
|
||||
ESP_LOGI(TAG,"Initalize success.");
|
||||
// now start the i2c driver
|
||||
ESP_LOGI(TAG,"Starting 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(!res && !i2cconfig_args.load->count){
|
||||
ESP_LOGI(TAG,"Storing i2c parameters.");
|
||||
i2c_config_t config={
|
||||
.mode = I2C_MODE_MASTER,
|
||||
.sda_io_num = i2c_gpio_sda,
|
||||
.sda_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.scl_io_num = i2c_gpio_scl,
|
||||
.scl_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.master.clk_speed = i2c_frequency
|
||||
};
|
||||
config_i2c_set(&config, i2c_port);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
#define RUN_SHOW_ERROR(c)
|
||||
|
||||
|
||||
static int do_i2cdump_cmd(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse(argc, argv, (void **)&i2cdump_args);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, i2cdump_args.end, argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Check chip address: "-c" option */
|
||||
int chip_addr = i2cdump_args.chip_address->ival[0];
|
||||
/* Check read size: "-s" option */
|
||||
int size = 1;
|
||||
if (i2cdump_args.size->count) {
|
||||
size = i2cdump_args.size->ival[0];
|
||||
}
|
||||
if (size != 1 && size != 2 && size != 4) {
|
||||
ESP_LOGE(TAG, "Wrong read size. Only support 1,2,4");
|
||||
return 1;
|
||||
}
|
||||
esp_err_t ret = i2c_initialize_driver_from_config();
|
||||
if(ret!=ESP_OK) return 0;
|
||||
|
||||
uint8_t data_addr;
|
||||
uint8_t data[4];
|
||||
int32_t block[16];
|
||||
printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f"
|
||||
" 0123456789abcdef\r\n");
|
||||
for (int i = 0; i < 128; i += 16) {
|
||||
printf("%02x: ", i);
|
||||
for (int j = 0; j < 16; j += size) {
|
||||
fflush(stdout);
|
||||
data_addr = i + j;
|
||||
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);
|
||||
i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN);
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, chip_addr << 1 | READ_BIT, ACK_CHECK_EN);
|
||||
if (size > 1) {
|
||||
i2c_master_read(cmd, data, size - 1, ACK_VAL);
|
||||
}
|
||||
i2c_master_read_byte(cmd, data + size - 1, NACK_VAL);
|
||||
i2c_master_stop(cmd);
|
||||
esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 50 / portTICK_RATE_MS);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
if (ret == ESP_OK) {
|
||||
for (int k = 0; k < size; k++) {
|
||||
printf("%02x ", data[k]);
|
||||
block[j + k] = data[k];
|
||||
}
|
||||
} else {
|
||||
for (int k = 0; k < size; k++) {
|
||||
printf("XX ");
|
||||
block[j + k] = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf(" ");
|
||||
for (int k = 0; k < 16; k++) {
|
||||
if (block[k] < 0) {
|
||||
printf("X");
|
||||
}
|
||||
if ((block[k] & 0xff) == 0x00 || (block[k] & 0xff) == 0xff) {
|
||||
printf(".");
|
||||
} else if ((block[k] & 0xff) < 32 || (block[k] & 0xff) >= 127) {
|
||||
printf("?");
|
||||
} else {
|
||||
printf("%c", block[k] & 0xff);
|
||||
}
|
||||
}
|
||||
printf("\r\n");
|
||||
}
|
||||
// Don't stop the driver; our firmware may be using it for screen, etc
|
||||
//i2c_driver_delete(i2c_port);
|
||||
return 0;
|
||||
}
|
||||
static int do_i2cset_cmd(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse(argc, argv, (void **)&i2cset_args);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, i2cset_args.end, argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Check chip address: "-c" option */
|
||||
int chip_addr = i2cset_args.chip_address->ival[0];
|
||||
/* Check register address: "-r" option */
|
||||
int data_addr = 0;
|
||||
if (i2cset_args.register_address->count) {
|
||||
data_addr = i2cset_args.register_address->ival[0];
|
||||
}
|
||||
/* Check data: "-d" option */
|
||||
int len = i2cset_args.data->count;
|
||||
|
||||
i2c_master_driver_initialize();
|
||||
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);
|
||||
if (i2cset_args.register_address->count) {
|
||||
i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN);
|
||||
}
|
||||
for (int i = 0; i < len; i++) {
|
||||
i2c_master_write_byte(cmd, i2cset_args.data->ival[i], ACK_CHECK_EN);
|
||||
}
|
||||
i2c_master_stop(cmd);
|
||||
esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Write OK");
|
||||
} else if (ret == ESP_ERR_TIMEOUT) {
|
||||
ESP_LOGW(TAG, "Bus is busy");
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Write Failed");
|
||||
}
|
||||
// Don't stop the driver; our firmware may be using it for screen, etc
|
||||
//i2c_driver_delete(i2c_port);
|
||||
return 0;
|
||||
}
|
||||
|
||||
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]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Check chip address: "-c" option */
|
||||
int chip_addr = i2cget_args.chip_address->ival[0];
|
||||
/* Check register address: "-r" option */
|
||||
int data_addr = -1;
|
||||
if (i2cget_args.register_address->count) {
|
||||
data_addr = i2cget_args.register_address->ival[0];
|
||||
}
|
||||
/* Check data length: "-l" option */
|
||||
int len = 1;
|
||||
if (i2cget_args.data_length->count) {
|
||||
len = i2cget_args.data_length->ival[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);
|
||||
i2c_master_start(cmd);
|
||||
}
|
||||
i2c_master_write_byte(cmd, chip_addr << 1 | READ_BIT, ACK_CHECK_EN);
|
||||
if (len > 1) {
|
||||
i2c_master_read(cmd, data, len - 1, ACK_VAL);
|
||||
}
|
||||
i2c_master_read_byte(cmd, data + len - 1, NACK_VAL);
|
||||
i2c_master_stop(cmd);
|
||||
esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
if (ret == ESP_OK) {
|
||||
for (int i = 0; i < len; i++) {
|
||||
printf("0x%02x ", data[i]);
|
||||
if ((i + 1) % 16 == 0) {
|
||||
printf("\r\n");
|
||||
}
|
||||
}
|
||||
if (len % 16) {
|
||||
printf("\r\n");
|
||||
}
|
||||
} else if (ret == ESP_ERR_TIMEOUT) {
|
||||
ESP_LOGW(TAG, "Bus is busy");
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Read failed");
|
||||
}
|
||||
free(data);
|
||||
// Don't stop the driver; our firmware may be using it for screen, etc
|
||||
//i2c_driver_delete(i2c_port);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_i2cdetect_cmd(int argc, char **argv)
|
||||
{
|
||||
uint8_t matches[128]={};
|
||||
int last_match=0;
|
||||
esp_err_t ret = i2c_initialize_driver_from_config();
|
||||
if(ret!=ESP_OK) return 0;
|
||||
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) {
|
||||
printf("%02x: ", i);
|
||||
for (int j = 0; j < 16 ; j++) {
|
||||
fflush(stdout);
|
||||
address = i + j;
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, (address << 1) | WRITE_BIT, ACK_CHECK_EN);
|
||||
i2c_master_stop(cmd);
|
||||
ret = i2c_master_cmd_begin(i2c_port, cmd, 50 / portTICK_RATE_MS);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
if (ret == ESP_OK) {
|
||||
printf("%02x ", address);
|
||||
matches[++last_match-1] = address;
|
||||
} else if (ret == ESP_ERR_TIMEOUT) {
|
||||
printf("UU ");
|
||||
} else {
|
||||
printf("-- ");
|
||||
}
|
||||
}
|
||||
|
||||
printf("\r\n");
|
||||
|
||||
}
|
||||
if(last_match) {
|
||||
printf("\r\n------------------------------------------------------------------------------------"
|
||||
"\r\nDetected the following devices (names provided by https://i2cdevices.org/addresses).");
|
||||
|
||||
for(int i=0;i<last_match;i++){
|
||||
//printf("%02x = %s\r\n", matches[i], i2c_get_description(matches[i]));
|
||||
printf("\r\n%u [%02xh]- %s", matches[i], matches[i], i2c_get_description(matches[i]));
|
||||
}
|
||||
printf("\r\n------------------------------------------------------------------------------------\r\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
static void register_i2c_set_display(){
|
||||
i2cdisp_args.address = arg_int0("a", "address", "<n>", "Set the device address, default 60");
|
||||
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.name = arg_str0("t", "type", "<I2C|SPI>", "Set the display type. default I2C");
|
||||
i2cdisp_args.driver = arg_str0("d", "driver", "<string>", "Set the display driver name. Default SSD1306");
|
||||
i2cdisp_args.clear = arg_litn(NULL, "clear", 0, 1, "clear configuration and return");
|
||||
i2cdisp_args.hflip = arg_litn(NULL, "hf", 0, 1, "Flip picture horizontally");
|
||||
i2cdisp_args.vflip = arg_litn(NULL, "vf", 0, 1, "Flip picture vertically");
|
||||
i2cdisp_args.rotate = arg_litn("r", "rotate", 0, 1, "Rotate the picture 180 deg");
|
||||
i2cdisp_args.end = arg_end(8);
|
||||
const esp_console_cmd_t i2c_set_display= {
|
||||
.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 = "getdisplay",
|
||||
.help="Shows display options and global i2c configuration",
|
||||
.hint = NULL,
|
||||
.func = &do_i2c_show_display,
|
||||
.argtable = NULL
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&i2c_set_display));
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&i2c_show_display));
|
||||
}
|
||||
static void register_i2cdectect(void)
|
||||
{
|
||||
const esp_console_cmd_t i2cdetect_cmd = {
|
||||
.command = "i2cdetect",
|
||||
.help = "Scan I2C bus for devices",
|
||||
.hint = NULL,
|
||||
.func = &do_i2cdetect_cmd,
|
||||
.argtable = NULL
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&i2cdetect_cmd));
|
||||
}
|
||||
|
||||
static void register_i2cget(void)
|
||||
{
|
||||
i2cget_args.chip_address = arg_int1("c", "chip", "<chip_addr>", "Specify the address of the chip on that bus");
|
||||
i2cget_args.register_address = arg_int0("r", "register", "<register_addr>", "Specify the address on that chip to read from");
|
||||
i2cget_args.data_length = arg_int0("l", "length", "<length>", "Specify the length to read from that data address");
|
||||
i2cget_args.end = arg_end(1);
|
||||
const esp_console_cmd_t i2cget_cmd = {
|
||||
.command = "i2cget",
|
||||
.help = "Read registers visible through the I2C bus",
|
||||
.hint = NULL,
|
||||
.func = &do_i2cget_cmd,
|
||||
.argtable = &i2cget_args
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&i2cget_cmd));
|
||||
}
|
||||
|
||||
|
||||
static void register_i2cset(void)
|
||||
{
|
||||
i2cset_args.chip_address = arg_int1("c", "chip", "<chip_addr>", "Specify the address of the chip on that bus");
|
||||
i2cset_args.register_address = arg_int0("r", "register", "<register_addr>", "Specify the address on that chip to read from");
|
||||
i2cset_args.data = arg_intn(NULL, NULL, "<data>", 0, 256, "Specify the data to write to that data address");
|
||||
i2cset_args.end = arg_end(2);
|
||||
const esp_console_cmd_t i2cset_cmd = {
|
||||
.command = "i2cset",
|
||||
.help = "Set registers visible through the I2C bus",
|
||||
.hint = NULL,
|
||||
.func = &do_i2cset_cmd,
|
||||
.argtable = &i2cset_args
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&i2cset_cmd));
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void register_i2cdump(void)
|
||||
{
|
||||
i2cdump_args.chip_address = arg_int1("c", "chip", "<chip_addr>", "Specify the address of the chip on that bus");
|
||||
i2cdump_args.size = arg_int0("s", "size", "<size>", "Specify the size of each read");
|
||||
i2cdump_args.end = arg_end(3);
|
||||
const esp_console_cmd_t i2cdump_cmd = {
|
||||
.command = "i2cdump",
|
||||
.help = "Examine registers visible through the I2C bus",
|
||||
.hint = NULL,
|
||||
.func = &do_i2cdump_cmd,
|
||||
.argtable = &i2cdump_args
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&i2cdump_cmd));
|
||||
}
|
||||
|
||||
static void register_i2ccheck(){
|
||||
i2ccheck_args.port = arg_int0("p", "port", "<0|1>", "Set the I2C bus port number");
|
||||
i2ccheck_args.end = arg_end(2);
|
||||
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "i2ccheck",
|
||||
.help = "Check if the I2C bus is installed",
|
||||
.hint = NULL,
|
||||
.func = &do_i2c_check,
|
||||
.argtable = &i2ccheck_args
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
|
||||
}
|
||||
|
||||
|
||||
static void register_i2cstop(){
|
||||
i2cstop_args.port = arg_int0("p", "port", "<0|1>", "Set the I2C bus port number");
|
||||
i2cstop_args.end = arg_end(2);
|
||||
|
||||
const esp_console_cmd_t i2cconfig_cmd = {
|
||||
.command = "i2cstop",
|
||||
.help = "Stop the I2C bus",
|
||||
.hint = NULL,
|
||||
.func = &do_i2c_stop,
|
||||
.argtable = &i2cstop_args
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&i2cconfig_cmd));
|
||||
}
|
||||
|
||||
static void register_i2cconfig(void)
|
||||
{
|
||||
i2cconfig_args.port = arg_int0("p", "port", "<0|1>", "Set the I2C bus port number");
|
||||
i2cconfig_args.freq = arg_int0("f", "freq", "<Hz>", "Set the frequency(Hz) of I2C bus. e.g. 100000");
|
||||
i2cconfig_args.sda = arg_int0("d", "sda", "<gpio>", "Set the gpio for I2C SDA. e.g. 19");
|
||||
i2cconfig_args.scl = arg_int0("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(4);
|
||||
const esp_console_cmd_t i2cconfig_cmd = {
|
||||
.command = "i2cconfig",
|
||||
.help = "Config I2C bus",
|
||||
.hint = NULL,
|
||||
.func = &do_i2cconfig_cmd,
|
||||
.argtable = &i2cconfig_args
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&i2cconfig_cmd));
|
||||
}
|
||||
|
||||
void register_i2ctools(void)
|
||||
{
|
||||
register_i2cconfig();
|
||||
register_i2cdectect();
|
||||
register_i2cget();
|
||||
register_i2cset();
|
||||
register_i2cdump();
|
||||
register_i2c_set_display();
|
||||
register_i2cstop();
|
||||
register_i2ccheck();
|
||||
}
|
||||
20
components/platform_console/cmd_i2ctools.h
Normal file
20
components/platform_console/cmd_i2ctools.h
Normal file
@@ -0,0 +1,20 @@
|
||||
/* cmd_i2ctools.h
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void register_i2ctools(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
554
components/platform_console/cmd_nvs.c
Normal file
554
components/platform_console/cmd_nvs.c
Normal file
@@ -0,0 +1,554 @@
|
||||
/* Console example — NVS commands
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
//#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#include "nvs_flash.h"
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
#include "esp_log.h"
|
||||
#include "esp_console.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_err.h"
|
||||
#include "cmd_nvs.h"
|
||||
#include "nvs.h"
|
||||
#include "nvs_utilities.h"
|
||||
|
||||
|
||||
|
||||
static const char *ARG_TYPE_STR = "type can be: i8, u8, i16, u16 i32, u32 i64, u64, str, blob";
|
||||
static const char * TAG = "cmd_nvs";
|
||||
|
||||
static struct {
|
||||
struct arg_str *key;
|
||||
struct arg_str *type;
|
||||
struct arg_str *value;
|
||||
struct arg_end *end;
|
||||
} set_args;
|
||||
|
||||
static struct {
|
||||
struct arg_str *key;
|
||||
struct arg_str *type;
|
||||
struct arg_end *end;
|
||||
} get_args;
|
||||
|
||||
static struct {
|
||||
struct arg_str *key;
|
||||
struct arg_end *end;
|
||||
} erase_args;
|
||||
|
||||
static struct {
|
||||
struct arg_str *namespace;
|
||||
struct arg_end *end;
|
||||
} erase_all_args;
|
||||
|
||||
static struct {
|
||||
struct arg_str *partition;
|
||||
struct arg_str *namespace;
|
||||
struct arg_str *type;
|
||||
struct arg_end *end;
|
||||
} list_args;
|
||||
|
||||
|
||||
|
||||
static esp_err_t store_blob(nvs_handle nvs, const char *key, const char *str_values)
|
||||
{
|
||||
uint8_t value;
|
||||
size_t str_len = strlen(str_values);
|
||||
size_t blob_len = str_len / 2;
|
||||
|
||||
if (str_len % 2) {
|
||||
ESP_LOGE(TAG, "Blob data must contain even number of characters");
|
||||
return ESP_ERR_NVS_TYPE_MISMATCH;
|
||||
}
|
||||
|
||||
char *blob = (char *)malloc(blob_len);
|
||||
if (blob == NULL) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
for (int i = 0, j = 0; i < str_len; i++) {
|
||||
char ch = str_values[i];
|
||||
if (ch >= '0' && ch <= '9') {
|
||||
value = ch - '0';
|
||||
} else if (ch >= 'A' && ch <= 'F') {
|
||||
value = ch - 'A' + 10;
|
||||
} else if (ch >= 'a' && ch <= 'f') {
|
||||
value = ch - 'a' + 10;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Blob data contain invalid character");
|
||||
free(blob);
|
||||
return ESP_ERR_NVS_TYPE_MISMATCH;
|
||||
}
|
||||
|
||||
if (i & 1) {
|
||||
blob[j++] += value;
|
||||
} else {
|
||||
blob[j] = value << 4;
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t err = nvs_set_blob(nvs, key, blob, blob_len);
|
||||
free(blob);
|
||||
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_commit(nvs);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static esp_err_t set_value_in_nvs(const char *key, const char *str_type, const char *str_value)
|
||||
{
|
||||
esp_err_t err;
|
||||
nvs_handle nvs;
|
||||
bool range_error = false;
|
||||
|
||||
nvs_type_t type = str_to_type(str_type);
|
||||
|
||||
if (type == NVS_TYPE_ANY) {
|
||||
return ESP_ERR_NVS_TYPE_MISMATCH;
|
||||
}
|
||||
|
||||
err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (type == NVS_TYPE_I8) {
|
||||
int32_t value = strtol(str_value, NULL, 0);
|
||||
if (value < INT8_MIN || value > INT8_MAX || errno == ERANGE) {
|
||||
range_error = true;
|
||||
} else {
|
||||
err = nvs_set_i8(nvs, key, (int8_t)value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_U8) {
|
||||
uint32_t value = strtoul(str_value, NULL, 0);
|
||||
if (value > UINT8_MAX || errno == ERANGE) {
|
||||
range_error = true;
|
||||
} else {
|
||||
err = nvs_set_u8(nvs, key, (uint8_t)value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_I16) {
|
||||
int32_t value = strtol(str_value, NULL, 0);
|
||||
if (value < INT16_MIN || value > INT16_MAX || errno == ERANGE) {
|
||||
range_error = true;
|
||||
} else {
|
||||
err = nvs_set_i16(nvs, key, (int16_t)value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_U16) {
|
||||
uint32_t value = strtoul(str_value, NULL, 0);
|
||||
if (value > UINT16_MAX || errno == ERANGE) {
|
||||
range_error = true;
|
||||
} else {
|
||||
err = nvs_set_u16(nvs, key, (uint16_t)value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_I32) {
|
||||
int32_t value = strtol(str_value, NULL, 0);
|
||||
if (errno != ERANGE) {
|
||||
err = nvs_set_i32(nvs, key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_U32) {
|
||||
uint32_t value = strtoul(str_value, NULL, 0);
|
||||
if (errno != ERANGE) {
|
||||
err = nvs_set_u32(nvs, key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_I64) {
|
||||
int64_t value = strtoll(str_value, NULL, 0);
|
||||
if (errno != ERANGE) {
|
||||
err = nvs_set_i64(nvs, key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_U64) {
|
||||
uint64_t value = strtoull(str_value, NULL, 0);
|
||||
if (errno != ERANGE) {
|
||||
err = nvs_set_u64(nvs, key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_STR) {
|
||||
err = nvs_set_str(nvs, key, str_value);
|
||||
} else if (type == NVS_TYPE_BLOB) {
|
||||
err = store_blob(nvs, key, str_value);
|
||||
}
|
||||
|
||||
if (range_error || errno == ERANGE) {
|
||||
nvs_close(nvs);
|
||||
return ESP_ERR_NVS_VALUE_TOO_LONG;
|
||||
}
|
||||
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Set value ok. Committing '%s'", key);
|
||||
err = nvs_commit(nvs);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Value stored under key '%s'", key);
|
||||
}
|
||||
}
|
||||
|
||||
nvs_close(nvs);
|
||||
return err;
|
||||
}
|
||||
|
||||
static esp_err_t get_value_from_nvs(const char *key, const char *str_type)
|
||||
{
|
||||
nvs_handle nvs;
|
||||
esp_err_t err;
|
||||
|
||||
nvs_type_t type = str_to_type(str_type);
|
||||
|
||||
if (type == NVS_TYPE_ANY) {
|
||||
return ESP_ERR_NVS_TYPE_MISMATCH;
|
||||
}
|
||||
|
||||
err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (type == NVS_TYPE_I8) {
|
||||
int8_t value;
|
||||
err = nvs_get_i8(nvs, key, &value);
|
||||
if (err == ESP_OK) {
|
||||
printf("Value associated with key '%s' is %d \n", key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_U8) {
|
||||
uint8_t value;
|
||||
err = nvs_get_u8(nvs, key, &value);
|
||||
if (err == ESP_OK) {
|
||||
printf("Value associated with key '%s' is %u \n", key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_I16) {
|
||||
int16_t value;
|
||||
err = nvs_get_i16(nvs, key, &value);
|
||||
if (err == ESP_OK) {
|
||||
printf("Value associated with key '%s' is %d \n", key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_U16) {
|
||||
uint16_t value;
|
||||
if ((err = nvs_get_u16(nvs, key, &value)) == ESP_OK) {
|
||||
printf("Value associated with key '%s' is %u", key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_I32) {
|
||||
int32_t value;
|
||||
if ((err = nvs_get_i32(nvs, key, &value)) == ESP_OK) {
|
||||
printf("Value associated with key '%s' is %d \n", key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_U32) {
|
||||
uint32_t value;
|
||||
if ((err = nvs_get_u32(nvs, key, &value)) == ESP_OK) {
|
||||
printf("Value associated with key '%s' is %u \n", key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_I64) {
|
||||
int64_t value;
|
||||
if ((err = nvs_get_i64(nvs, key, &value)) == ESP_OK) {
|
||||
printf("Value associated with key '%s' is %lld \n", key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_U64) {
|
||||
uint64_t value;
|
||||
if ( (err = nvs_get_u64(nvs, key, &value)) == ESP_OK) {
|
||||
printf("Value associated with key '%s' is %llu \n", key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_STR) {
|
||||
size_t len=0;
|
||||
if ( (err = nvs_get_str(nvs, key, NULL, &len)) == ESP_OK) {
|
||||
char *str = (char *)malloc(len);
|
||||
if ( (err = nvs_get_str(nvs, key, str, &len)) == ESP_OK) {
|
||||
printf("String associated with key '%s' is %s \n", key, str);
|
||||
}
|
||||
free(str);
|
||||
}
|
||||
} else if (type == NVS_TYPE_BLOB) {
|
||||
size_t len;
|
||||
if ( (err = nvs_get_blob(nvs, key, NULL, &len)) == ESP_OK) {
|
||||
char *blob = (char *)malloc(len);
|
||||
if ( (err = nvs_get_blob(nvs, key, blob, &len)) == ESP_OK) {
|
||||
printf("Blob associated with key '%s' is %d bytes long: \n", key, len);
|
||||
print_blob(blob, len);
|
||||
}
|
||||
free(blob);
|
||||
}
|
||||
}
|
||||
|
||||
nvs_close(nvs);
|
||||
return err;
|
||||
}
|
||||
|
||||
static esp_err_t erase(const char *key)
|
||||
{
|
||||
nvs_handle nvs;
|
||||
|
||||
esp_err_t err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_erase_key(nvs, key);
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_commit(nvs);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Value with key '%s' erased", key);
|
||||
}
|
||||
}
|
||||
nvs_close(nvs);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static esp_err_t erase_all(const char *name)
|
||||
{
|
||||
nvs_handle nvs;
|
||||
|
||||
esp_err_t err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_erase_all(nvs);
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_commit(nvs);
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Namespace '%s' was %s erased", name, (err == ESP_OK) ? "" : "not");
|
||||
nvs_close(nvs);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static int set_value(int argc, char **argv)
|
||||
{
|
||||
ESP_LOGD(TAG, "%s %u - Parsing keys ",__func__,__LINE__);
|
||||
int nerrors = arg_parse(argc, argv, (void **) &set_args);
|
||||
|
||||
if (nerrors != 0) {
|
||||
ESP_LOGE(TAG, "%s %u - Error Parsing keys ",__func__,__LINE__);
|
||||
arg_print_errors(stderr, set_args.end, argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *key = set_args.key->sval[0];
|
||||
const char *type = set_args.type->sval[0];
|
||||
const char *values = set_args.value->sval[0];
|
||||
ESP_LOGI(TAG, "Setting '%s' (type %s)", key,type);
|
||||
esp_err_t err = set_value_in_nvs(key, type, values);
|
||||
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "%s", esp_err_to_name(err));
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
static int get_value(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse(argc, argv, (void **) &get_args);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, get_args.end, argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *key = get_args.key->sval[0];
|
||||
const char *type = get_args.type->sval[0];
|
||||
|
||||
esp_err_t err = get_value_from_nvs(key, type);
|
||||
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "%s", esp_err_to_name(err));
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int erase_value(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse(argc, argv, (void **) &erase_args);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, erase_args.end, argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *key = erase_args.key->sval[0];
|
||||
|
||||
esp_err_t err = erase(key);
|
||||
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "%s", esp_err_to_name(err));
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int erase_namespace(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse(argc, argv, (void **) &erase_all_args);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, erase_all_args.end, argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *name = erase_all_args.namespace->sval[0];
|
||||
|
||||
esp_err_t err = erase_all(name);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "%s", esp_err_to_name(err));
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int erase_wifi_manager(int argc, char **argv)
|
||||
{
|
||||
nvs_handle nvs;
|
||||
esp_err_t err = nvs_open("config", NVS_READWRITE, &nvs);
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_erase_all(nvs);
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_commit(nvs);
|
||||
}
|
||||
}
|
||||
nvs_close(nvs);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "wifi manager configuration was not erase. %s", esp_err_to_name(err));
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
ESP_LOGW(TAG, "Wifi manager configuration was erased");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int list(const char *part, const char *name, const char *str_type)
|
||||
{
|
||||
nvs_type_t type = str_to_type(str_type);
|
||||
|
||||
nvs_iterator_t it = nvs_entry_find(part, NULL, type);
|
||||
if (it == NULL) {
|
||||
ESP_LOGE(TAG, "No such enty was found");
|
||||
return 1;
|
||||
}
|
||||
|
||||
do {
|
||||
nvs_entry_info_t info;
|
||||
nvs_entry_info(it, &info);
|
||||
it = nvs_entry_next(it);
|
||||
|
||||
printf("namespace '%s', key '%s', type '%s' \n",
|
||||
info.namespace_name, info.key, type_to_str(info.type));
|
||||
} while (it != NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
static int list_entries(int argc, char **argv)
|
||||
{
|
||||
list_args.partition->sval[0] = "";
|
||||
list_args.namespace->sval[0] = "";
|
||||
list_args.type->sval[0] = "";
|
||||
|
||||
int nerrors = arg_parse(argc, argv, (void **) &list_args);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, list_args.end, argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *part = list_args.partition->sval[0];
|
||||
const char *name = list_args.namespace->sval[0];
|
||||
const char *type = list_args.type->sval[0];
|
||||
|
||||
return list(part, name, type);
|
||||
}
|
||||
void register_nvs()
|
||||
{
|
||||
set_args.key = arg_str1(NULL, NULL, "<key>", "key of the value to be set");
|
||||
set_args.type = arg_str1(NULL, NULL, "<type>", ARG_TYPE_STR);
|
||||
set_args.value = arg_str1("v", "value", "<value>", "value to be stored");
|
||||
set_args.end = arg_end(2);
|
||||
|
||||
get_args.key = arg_str1(NULL, NULL, "<key>", "key of the value to be read");
|
||||
get_args.type = arg_str1(NULL, NULL, "<type>", ARG_TYPE_STR);
|
||||
get_args.end = arg_end(2);
|
||||
|
||||
erase_args.key = arg_str1(NULL, NULL, "<key>", "key of the value to be erased");
|
||||
erase_args.end = arg_end(2);
|
||||
|
||||
erase_all_args.namespace = arg_str1(NULL, NULL, "<namespace>", "namespace to be erased");
|
||||
erase_all_args.end = arg_end(2);
|
||||
|
||||
list_args.partition = arg_str1(NULL, NULL, "<partition>", "partition name");
|
||||
list_args.namespace = arg_str0("n", "namespace", "<namespace>", "namespace name");
|
||||
list_args.type = arg_str0("t", "type", "<type>", ARG_TYPE_STR);
|
||||
list_args.end = arg_end(2);
|
||||
const esp_console_cmd_t set_cmd = {
|
||||
.command = "nvs_set",
|
||||
.help = "Set variable in selected namespace. Blob type must be comma separated list of hex values. \n"
|
||||
"Examples:\n"
|
||||
" nvs_set VarName i32 -v 123 \n"
|
||||
" nvs_set VarName srt -v YourString \n"
|
||||
" nvs_set VarName blob -v 0123456789abcdef \n",
|
||||
.hint = NULL,
|
||||
.func = &set_value,
|
||||
.argtable = &set_args
|
||||
};
|
||||
|
||||
const esp_console_cmd_t get_cmd = {
|
||||
.command = "nvs_get",
|
||||
.help = "Get variable from selected namespace. \n"
|
||||
"Example: nvs_get VarName i32",
|
||||
.hint = NULL,
|
||||
.func = &get_value,
|
||||
.argtable = &get_args
|
||||
};
|
||||
|
||||
const esp_console_cmd_t erase_cmd = {
|
||||
.command = "nvs_erase",
|
||||
.help = "Erase variable from current namespace",
|
||||
.hint = NULL,
|
||||
.func = &erase_value,
|
||||
.argtable = &erase_args
|
||||
};
|
||||
|
||||
const esp_console_cmd_t erase_namespace_cmd = {
|
||||
.command = "nvs_erase_namespace",
|
||||
.help = "Erases specified namespace",
|
||||
.hint = NULL,
|
||||
.func = &erase_namespace,
|
||||
.argtable = &erase_all_args
|
||||
};
|
||||
const esp_console_cmd_t erase_wifimanager_cmd = {
|
||||
.command = "nvs_erase_wifi_manager",
|
||||
.help = "Erases wifi_manager's config",
|
||||
.hint = NULL,
|
||||
.func = &erase_wifi_manager,
|
||||
.argtable = NULL
|
||||
};
|
||||
|
||||
|
||||
const esp_console_cmd_t list_entries_cmd = {
|
||||
.command = "nvs_list",
|
||||
.help = "List stored key-value pairs stored in NVS."
|
||||
"Namespace and type can be specified to print only those key-value pairs.\n"
|
||||
"Following command list variables stored inside 'nvs' partition, under namespace 'storage' with type uint32_t"
|
||||
"Example: nvs_list nvs -n storage -t u32 \n",
|
||||
.hint = NULL,
|
||||
.func = &list_entries,
|
||||
.argtable = &list_args
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&list_entries_cmd));
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&set_cmd));
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&get_cmd));
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&erase_cmd));
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&erase_namespace_cmd));
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&erase_wifimanager_cmd));
|
||||
|
||||
}
|
||||
#ifdef __cplusplus
|
||||
extern }
|
||||
#endif
|
||||
22
components/platform_console/cmd_nvs.h
Normal file
22
components/platform_console/cmd_nvs.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/* Console example — declarations of command registration functions.
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#pragma once
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Register NVS functions
|
||||
void register_nvs();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
74
components/platform_console/cmd_ota.c
Normal file
74
components/platform_console/cmd_ota.c
Normal file
@@ -0,0 +1,74 @@
|
||||
/* Console example — various system commands
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#include "cmd_ota.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "esp_log.h"
|
||||
#include "esp_console.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_sleep.h"
|
||||
#include "esp_spi_flash.h"
|
||||
#include "driver/rtc_io.h"
|
||||
#include "driver/uart.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "soc/rtc_cntl_reg.h"
|
||||
#include "esp32/rom/uart.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
static const char * TAG = "ota";
|
||||
extern esp_err_t start_ota(const char * bin_url);
|
||||
static struct {
|
||||
struct arg_str *url;
|
||||
struct arg_end *end;
|
||||
} ota_args;
|
||||
/* 'heap' command prints minumum heap size */
|
||||
static int perform_ota_update(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse(argc, argv, (void **) &ota_args);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, ota_args.end, argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *url = ota_args.url->sval[0];
|
||||
|
||||
esp_err_t err=ESP_OK;
|
||||
ESP_LOGI(TAG, "Starting ota: %s", url);
|
||||
start_ota(url);
|
||||
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "%s", esp_err_to_name(err));
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void register_ota_cmd()
|
||||
{
|
||||
ota_args.url= arg_str1(NULL, NULL, "<url>", "url of the binary app file");
|
||||
ota_args.end = arg_end(2);
|
||||
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "update",
|
||||
.help = "Updates the application binary from the provided URL",
|
||||
.hint = NULL,
|
||||
.func = &perform_ota_update,
|
||||
.argtable = &ota_args
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
|
||||
|
||||
|
||||
19
components/platform_console/cmd_ota.h
Normal file
19
components/platform_console/cmd_ota.h
Normal file
@@ -0,0 +1,19 @@
|
||||
/* Console example — various system commands
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Register system functions
|
||||
void register_ota_cmd();
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
127
components/platform_console/cmd_squeezelite.c
Normal file
127
components/platform_console/cmd_squeezelite.c
Normal file
@@ -0,0 +1,127 @@
|
||||
//size_t esp_console_split_argv(char *line, char **argv, size_t argv_size);
|
||||
#include "cmd_squeezelite.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "cmd_decl.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp_console.h"
|
||||
#include "esp_pthread.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "pthread.h"
|
||||
#include "platform_esp32.h"
|
||||
#include "nvs.h"
|
||||
#include "nvs_flash.h"
|
||||
//extern char current_namespace[];
|
||||
static const char * TAG = "squeezelite_cmd";
|
||||
#define SQUEEZELITE_THREAD_STACK_SIZE (6*1024)
|
||||
extern int main(int argc, char **argv);
|
||||
static int launchsqueezelite(int argc, char **argv);
|
||||
pthread_t thread_squeezelite;
|
||||
pthread_t thread_squeezelite_runner;
|
||||
/** Arguments used by 'squeezelite' function */
|
||||
static struct {
|
||||
struct arg_str *parameters;
|
||||
struct arg_end *end;
|
||||
} squeezelite_args;
|
||||
static struct {
|
||||
int argc;
|
||||
char ** argv;
|
||||
} thread_parms ;
|
||||
static void * squeezelite_runner_thread(){
|
||||
ESP_LOGI(TAG ,"Calling squeezelite");
|
||||
main(thread_parms.argc,thread_parms.argv);
|
||||
return NULL;
|
||||
}
|
||||
#define ADDITIONAL_SQUEEZELILTE_ARGS 5
|
||||
static void * squeezelite_thread(){
|
||||
int * exit_code;
|
||||
static bool isRunning=false;
|
||||
if(isRunning) {
|
||||
ESP_LOGE(TAG,"Squeezelite already running. Exiting!");
|
||||
return NULL;
|
||||
}
|
||||
isRunning=true;
|
||||
// Let's not wait on WiFi to allow squeezelite to run in bluetooth mode
|
||||
// ESP_LOGI(TAG,"Waiting for WiFi.");
|
||||
// while(!wait_for_wifi()){usleep(100000);};
|
||||
ESP_LOGV(TAG ,"Number of args received: %u",thread_parms.argc );
|
||||
ESP_LOGV(TAG ,"Values:");
|
||||
for(int i = 0;i<thread_parms.argc; i++){
|
||||
ESP_LOGV(TAG ," %s",thread_parms.argv[i]);
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG,"Starting Squeezelite runner Thread");
|
||||
esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
|
||||
cfg.thread_name= "squeezelite-run";
|
||||
cfg.inherit_cfg = true;
|
||||
cfg.stack_size = SQUEEZELITE_THREAD_STACK_SIZE ;
|
||||
esp_pthread_set_cfg(&cfg);
|
||||
// no attribute if we want esp stack stack to prevail
|
||||
pthread_create(&thread_squeezelite_runner, NULL, squeezelite_runner_thread,NULL);
|
||||
// Wait for thread completion so we can free up memory.
|
||||
pthread_join(thread_squeezelite_runner,(void **)&exit_code);
|
||||
|
||||
ESP_LOGV(TAG ,"Exited from squeezelite's main(). Freeing argv structure.");
|
||||
for(int i=0;i<thread_parms.argc;i++){
|
||||
ESP_LOGV(TAG ,"Freeing char buffer for parameter %u", i+1);
|
||||
free(thread_parms.argv[i]);
|
||||
}
|
||||
ESP_LOGV(TAG ,"Freeing argv pointer");
|
||||
free(thread_parms.argv);
|
||||
isRunning=false;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int launchsqueezelite(int argc, char **argv)
|
||||
{
|
||||
ESP_LOGV(TAG ,"Begin");
|
||||
|
||||
ESP_LOGV(TAG, "Parameters:");
|
||||
for(int i = 0;i<argc; i++){
|
||||
ESP_LOGV(TAG, " %s",argv[i]);
|
||||
}
|
||||
ESP_LOGV(TAG,"Saving args in thread structure");
|
||||
|
||||
thread_parms.argc=0;
|
||||
thread_parms.argv = malloc(sizeof(char**)*(argc+ADDITIONAL_SQUEEZELILTE_ARGS));
|
||||
memset(thread_parms.argv,'\0',sizeof(char**)*(argc+ADDITIONAL_SQUEEZELILTE_ARGS));
|
||||
|
||||
for(int i=0;i<argc;i++){
|
||||
ESP_LOGD(TAG ,"assigning parm %u : %s",i,argv[i]);
|
||||
thread_parms.argv[thread_parms.argc++]=strdup(argv[i]);
|
||||
}
|
||||
|
||||
if(argc==1){
|
||||
// There isn't a default configuration that would actually work
|
||||
// if no parameter is passed.
|
||||
ESP_LOGV(TAG ,"Adding argv value at %u. Prev value: %s",thread_parms.argc,thread_parms.argv[thread_parms.argc-1]);
|
||||
thread_parms.argv[thread_parms.argc++]=strdup("-?");
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG,"Starting Squeezelite Thread");
|
||||
esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
|
||||
cfg.thread_name= "squeezelite";
|
||||
cfg.inherit_cfg = true;
|
||||
esp_pthread_set_cfg(&cfg);
|
||||
pthread_create(&thread_squeezelite, NULL, squeezelite_thread,NULL);
|
||||
ESP_LOGD(TAG ,"Back to console thread!");
|
||||
return 0;
|
||||
}
|
||||
void register_squeezelite(){
|
||||
|
||||
squeezelite_args.parameters = arg_str0(NULL, NULL, "<parms>", "command line for squeezelite. -h for help, --defaults to launch with default values.");
|
||||
squeezelite_args.end = arg_end(1);
|
||||
const esp_console_cmd_t launch_squeezelite = {
|
||||
.command = "squeezelite",
|
||||
.help = "Starts squeezelite",
|
||||
.hint = NULL,
|
||||
.func = &launchsqueezelite,
|
||||
.argtable = &squeezelite_args
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&launch_squeezelite) );
|
||||
|
||||
}
|
||||
13
components/platform_console/cmd_squeezelite.h
Normal file
13
components/platform_console/cmd_squeezelite.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Register WiFi functions
|
||||
void register_squeezelite();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
489
components/platform_console/cmd_system.c
Normal file
489
components/platform_console/cmd_system.c
Normal file
@@ -0,0 +1,489 @@
|
||||
/* Console example — various system commands
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#define LOG_LOCAL_LEVEL ESP_LOG_INFO
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "esp_log.h"
|
||||
#include "esp_console.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_spi_flash.h"
|
||||
#include "driver/rtc_io.h"
|
||||
#include "driver/uart.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "soc/rtc_cntl_reg.h"
|
||||
#include "esp32/rom/uart.h"
|
||||
#include "cmd_system.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_partition.h"
|
||||
#include "esp_ota_ops.h"
|
||||
#include "platform_esp32.h"
|
||||
#include "platform_config.h"
|
||||
#include "esp_sleep.h"
|
||||
#include "driver/uart.h" // for the uart driver access
|
||||
|
||||
#ifdef CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS
|
||||
#define WITH_TASKS_INFO 1
|
||||
#endif
|
||||
|
||||
|
||||
static const char * TAG = "cmd_system";
|
||||
|
||||
static void register_free();
|
||||
static void register_heap();
|
||||
static void register_version();
|
||||
static void register_restart();
|
||||
static void register_deep_sleep();
|
||||
static void register_light_sleep();
|
||||
static void register_factory_boot();
|
||||
static void register_restart_ota();
|
||||
#if WITH_TASKS_INFO
|
||||
static void register_tasks();
|
||||
#endif
|
||||
|
||||
void register_system()
|
||||
{
|
||||
register_free();
|
||||
register_heap();
|
||||
register_version();
|
||||
register_restart();
|
||||
register_deep_sleep();
|
||||
register_light_sleep();
|
||||
register_factory_boot();
|
||||
register_restart_ota();
|
||||
#if WITH_TASKS_INFO
|
||||
register_tasks();
|
||||
#endif
|
||||
}
|
||||
|
||||
/* 'version' command */
|
||||
static int get_version(int argc, char **argv)
|
||||
{
|
||||
esp_chip_info_t info;
|
||||
esp_chip_info(&info);
|
||||
printf("IDF Version:%s\r\n", esp_get_idf_version());
|
||||
printf("Chip info:\r\n");
|
||||
printf("\tmodel:%s\r\n", info.model == CHIP_ESP32 ? "ESP32" : "Unknow");
|
||||
printf("\tcores:%d\r\n", info.cores);
|
||||
printf("\tfeature:%s%s%s%s%d%s\r\n",
|
||||
info.features & CHIP_FEATURE_WIFI_BGN ? "/802.11bgn" : "",
|
||||
info.features & CHIP_FEATURE_BLE ? "/BLE" : "",
|
||||
info.features & CHIP_FEATURE_BT ? "/BT" : "",
|
||||
info.features & CHIP_FEATURE_EMB_FLASH ? "/Embedded-Flash:" : "/External-Flash:",
|
||||
spi_flash_get_chip_size() / (1024 * 1024), " MB");
|
||||
printf("\trevision number:%d\r\n", info.revision);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void register_version()
|
||||
{
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "version",
|
||||
.help = "Get version of chip and SDK",
|
||||
.hint = NULL,
|
||||
.func = &get_version,
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
|
||||
/** 'restart' command restarts the program */
|
||||
|
||||
|
||||
esp_err_t guided_boot(esp_partition_subtype_t partition_subtype)
|
||||
{
|
||||
#if RECOVERY_APPLICATION
|
||||
if(partition_subtype ==ESP_PARTITION_SUBTYPE_APP_FACTORY){
|
||||
ESP_LOGW(TAG,"RECOVERY application is already active");
|
||||
if(!wait_for_commit()){
|
||||
ESP_LOGW(TAG,"Unable to commit configuration. ");
|
||||
}
|
||||
ESP_LOGW(TAG, "Restarting after tx complete");
|
||||
uart_wait_tx_done(UART_NUM_1, 500 / portTICK_RATE_MS);
|
||||
esp_restart();
|
||||
return ESP_OK;
|
||||
}
|
||||
#else
|
||||
if(partition_subtype !=ESP_PARTITION_SUBTYPE_APP_FACTORY){
|
||||
ESP_LOGW(TAG,"SQUEEZELITE application is already active");
|
||||
if(!wait_for_commit()){
|
||||
ESP_LOGW(TAG,"Unable to commit configuration. ");
|
||||
}
|
||||
ESP_LOGW(TAG, "Restarting after tx complete");
|
||||
uart_wait_tx_done(UART_NUM_1, 500 / portTICK_RATE_MS);
|
||||
esp_restart();
|
||||
return ESP_OK;
|
||||
}
|
||||
#endif
|
||||
esp_err_t err = ESP_OK;
|
||||
bool bFound=false;
|
||||
ESP_LOGI(TAG, "Looking for partition type %u",partition_subtype);
|
||||
const esp_partition_t *partition;
|
||||
esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_APP, partition_subtype, NULL);
|
||||
|
||||
if(it == NULL){
|
||||
ESP_LOGE(TAG,"Unable initialize partition iterator!");
|
||||
set_status_message(ERROR, "Reboot failed. Cannot iterate through partitions");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGD(TAG, "Found partition. Getting info.");
|
||||
partition = (esp_partition_t *) esp_partition_get(it);
|
||||
ESP_LOGD(TAG, "Releasing partition iterator");
|
||||
esp_partition_iterator_release(it);
|
||||
if(partition != NULL){
|
||||
ESP_LOGI(TAG, "Found application partition %s sub type %u", partition->label,partition_subtype);
|
||||
err=esp_ota_set_boot_partition(partition);
|
||||
if(err!=ESP_OK){
|
||||
ESP_LOGE(TAG,"Unable to set partition as active for next boot. %s",esp_err_to_name(err));
|
||||
bFound=false;
|
||||
set_status_message(ERROR, "Unable to select partition for reboot.");
|
||||
}
|
||||
else{
|
||||
ESP_LOGW(TAG, "Application partition %s sub type %u is selected for boot", partition->label,partition_subtype);
|
||||
bFound=true;
|
||||
set_status_message(WARNING, "Rebooting!");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG,"partition type %u not found! Unable to reboot to recovery.",partition_subtype);
|
||||
set_status_message(ERROR, "Partition not found.");
|
||||
}
|
||||
ESP_LOGD(TAG, "Yielding to other processes");
|
||||
taskYIELD();
|
||||
if(bFound) {
|
||||
ESP_LOGW(TAG,"Configuration %s changes. ",config_has_changes()?"has":"does not have");
|
||||
if(!wait_for_commit()){
|
||||
ESP_LOGW(TAG,"Unable to commit configuration. ");
|
||||
}
|
||||
ESP_LOGW(TAG, "Restarting after tx complete");
|
||||
uart_wait_tx_done(UART_NUM_1, 500 / portTICK_RATE_MS);
|
||||
esp_restart();
|
||||
}
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static int restart(int argc, char **argv)
|
||||
{
|
||||
ESP_LOGW(TAG, "\n\nPerforming a simple restart to the currently active partition.");
|
||||
if(!wait_for_commit()){
|
||||
ESP_LOGW(TAG,"Unable to commit configuration. ");
|
||||
}
|
||||
ESP_LOGW(TAG, "Restarting after tx complete");
|
||||
uart_wait_tx_done(UART_NUM_1, 500 / portTICK_RATE_MS);
|
||||
esp_restart();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void simple_restart()
|
||||
{
|
||||
ESP_LOGW(TAG,"\n\n Called to perform a simple system reboot.");
|
||||
if(!wait_for_commit()){
|
||||
ESP_LOGW(TAG,"Unable to commit configuration. ");
|
||||
}
|
||||
|
||||
ESP_LOGW(TAG, "Restarting after tx complete");
|
||||
uart_wait_tx_done(UART_NUM_1, 500 / portTICK_RATE_MS);
|
||||
esp_restart();
|
||||
}
|
||||
|
||||
esp_err_t guided_restart_ota(){
|
||||
ESP_LOGW(TAG,"\n\nCalled for a reboot to OTA Application");
|
||||
guided_boot(ESP_PARTITION_SUBTYPE_APP_OTA_0);
|
||||
return ESP_FAIL; // return fail. This should never return... we're rebooting!
|
||||
}
|
||||
esp_err_t guided_factory(){
|
||||
ESP_LOGW(TAG,"\n\nCalled for a reboot to recovery application");
|
||||
guided_boot(ESP_PARTITION_SUBTYPE_APP_FACTORY);
|
||||
return ESP_FAIL; // return fail. This should never return... we're rebooting!
|
||||
}
|
||||
static int restart_factory(int argc, char **argv)
|
||||
{
|
||||
ESP_LOGW(TAG, "Executing guided boot into recovery");
|
||||
guided_boot(ESP_PARTITION_SUBTYPE_APP_FACTORY);
|
||||
return 0; // return fail. This should never return... we're rebooting!
|
||||
}
|
||||
static int restart_ota(int argc, char **argv)
|
||||
{
|
||||
ESP_LOGW(TAG, "Executing guided boot into ota app 0");
|
||||
guided_boot(ESP_PARTITION_SUBTYPE_APP_OTA_0);
|
||||
return 0; // return fail. This should never return... we're rebooting!
|
||||
}
|
||||
static void register_restart()
|
||||
{
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "restart",
|
||||
.help = "Software reset of the chip",
|
||||
.hint = NULL,
|
||||
.func = &restart,
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
static void register_restart_ota()
|
||||
{
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "restart_ota",
|
||||
.help = "Selects the ota app partition to boot from and performa a software reset of the chip",
|
||||
.hint = NULL,
|
||||
.func = &restart_ota,
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
|
||||
static void register_factory_boot()
|
||||
{
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "recovery",
|
||||
.help = "Resets and boot to recovery (if available)",
|
||||
.hint = NULL,
|
||||
.func = &restart_factory,
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
/** 'free' command prints available heap memory */
|
||||
|
||||
static int free_mem(int argc, char **argv)
|
||||
{
|
||||
printf("%d\n", esp_get_free_heap_size());
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void register_free()
|
||||
{
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "free",
|
||||
.help = "Get the current size of free heap memory",
|
||||
.hint = NULL,
|
||||
.func = &free_mem,
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
|
||||
/* 'heap' command prints minumum heap size */
|
||||
static int heap_size(int argc, char **argv)
|
||||
{
|
||||
uint32_t heap_size = heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT);
|
||||
ESP_LOGI(TAG, "min heap size: %u", heap_size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void register_heap()
|
||||
{
|
||||
const esp_console_cmd_t heap_cmd = {
|
||||
.command = "heap",
|
||||
.help = "Get minimum size of free heap memory that was available during program execution",
|
||||
.hint = NULL,
|
||||
.func = &heap_size,
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&heap_cmd) );
|
||||
|
||||
}
|
||||
|
||||
/** 'tasks' command prints the list of tasks and related information */
|
||||
#if WITH_TASKS_INFO
|
||||
|
||||
static int tasks_info(int argc, char **argv)
|
||||
{
|
||||
const size_t bytes_per_task = 40; /* see vTaskList description */
|
||||
char *task_list_buffer = malloc(uxTaskGetNumberOfTasks() * bytes_per_task);
|
||||
if (task_list_buffer == NULL) {
|
||||
ESP_LOGE(TAG, "failed to allocate buffer for vTaskList output");
|
||||
return 1;
|
||||
}
|
||||
fputs("Task Name\tStatus\tPrio\tHWM\tTask#", stdout);
|
||||
#ifdef CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID
|
||||
fputs("\tAffinity", stdout);
|
||||
#endif
|
||||
fputs("\n", stdout);
|
||||
vTaskList(task_list_buffer);
|
||||
fputs(task_list_buffer, stdout);
|
||||
free(task_list_buffer);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void register_tasks()
|
||||
{
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "tasks",
|
||||
.help = "Get information about running tasks",
|
||||
.hint = NULL,
|
||||
.func = &tasks_info,
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
|
||||
#endif // WITH_TASKS_INFO
|
||||
|
||||
/** 'deep_sleep' command puts the chip into deep sleep mode */
|
||||
|
||||
static struct {
|
||||
struct arg_int *wakeup_time;
|
||||
struct arg_int *wakeup_gpio_num;
|
||||
struct arg_int *wakeup_gpio_level;
|
||||
struct arg_end *end;
|
||||
} deep_sleep_args;
|
||||
|
||||
|
||||
static int deep_sleep(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse(argc, argv, (void **) &deep_sleep_args);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, deep_sleep_args.end, argv[0]);
|
||||
return 1;
|
||||
}
|
||||
if (deep_sleep_args.wakeup_time->count) {
|
||||
uint64_t timeout = 1000ULL * deep_sleep_args.wakeup_time->ival[0];
|
||||
ESP_LOGI(TAG, "Enabling timer wakeup, timeout=%lluus", timeout);
|
||||
ESP_ERROR_CHECK( esp_sleep_enable_timer_wakeup(timeout) );
|
||||
}
|
||||
if (deep_sleep_args.wakeup_gpio_num->count) {
|
||||
int io_num = deep_sleep_args.wakeup_gpio_num->ival[0];
|
||||
if (!rtc_gpio_is_valid_gpio(io_num)) {
|
||||
ESP_LOGE(TAG, "GPIO %d is not an RTC IO", io_num);
|
||||
return 1;
|
||||
}
|
||||
int level = 0;
|
||||
if (deep_sleep_args.wakeup_gpio_level->count) {
|
||||
level = deep_sleep_args.wakeup_gpio_level->ival[0];
|
||||
if (level != 0 && level != 1) {
|
||||
ESP_LOGE(TAG, "Invalid wakeup level: %d", level);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
ESP_LOGI(TAG, "Enabling wakeup on GPIO%d, wakeup on %s level",
|
||||
io_num, level ? "HIGH" : "LOW");
|
||||
|
||||
ESP_ERROR_CHECK( esp_sleep_enable_ext1_wakeup(1ULL << io_num, level) );
|
||||
}
|
||||
rtc_gpio_isolate(GPIO_NUM_12);
|
||||
esp_deep_sleep_start();
|
||||
}
|
||||
|
||||
static void register_deep_sleep()
|
||||
{
|
||||
deep_sleep_args.wakeup_time =
|
||||
arg_int0("t", "time", "<t>", "Wake up time, ms");
|
||||
deep_sleep_args.wakeup_gpio_num =
|
||||
arg_int0(NULL, "io", "<n>",
|
||||
"If specified, wakeup using GPIO with given number");
|
||||
deep_sleep_args.wakeup_gpio_level =
|
||||
arg_int0(NULL, "io_level", "<0|1>", "GPIO level to trigger wakeup");
|
||||
deep_sleep_args.end = arg_end(3);
|
||||
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "deep_sleep",
|
||||
.help = "Enter deep sleep mode. "
|
||||
"Two wakeup modes are supported: timer and GPIO. "
|
||||
"If no wakeup option is specified, will sleep indefinitely.",
|
||||
.hint = NULL,
|
||||
.func = &deep_sleep,
|
||||
.argtable = &deep_sleep_args
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
|
||||
/** 'light_sleep' command puts the chip into light sleep mode */
|
||||
|
||||
static struct {
|
||||
struct arg_int *wakeup_time;
|
||||
struct arg_int *wakeup_gpio_num;
|
||||
struct arg_int *wakeup_gpio_level;
|
||||
struct arg_end *end;
|
||||
} light_sleep_args;
|
||||
|
||||
static int light_sleep(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse(argc, argv, (void **) &light_sleep_args);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, light_sleep_args.end, argv[0]);
|
||||
return 1;
|
||||
}
|
||||
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
|
||||
if (light_sleep_args.wakeup_time->count) {
|
||||
uint64_t timeout = 1000ULL * light_sleep_args.wakeup_time->ival[0];
|
||||
ESP_LOGI(TAG, "Enabling timer wakeup, timeout=%lluus", timeout);
|
||||
ESP_ERROR_CHECK( esp_sleep_enable_timer_wakeup(timeout) );
|
||||
}
|
||||
int io_count = light_sleep_args.wakeup_gpio_num->count;
|
||||
if (io_count != light_sleep_args.wakeup_gpio_level->count) {
|
||||
ESP_LOGE(TAG, "Should have same number of 'io' and 'io_level' arguments");
|
||||
return 1;
|
||||
}
|
||||
for (int i = 0; i < io_count; ++i) {
|
||||
int io_num = light_sleep_args.wakeup_gpio_num->ival[i];
|
||||
int level = light_sleep_args.wakeup_gpio_level->ival[i];
|
||||
if (level != 0 && level != 1) {
|
||||
ESP_LOGE(TAG, "Invalid wakeup level: %d", level);
|
||||
return 1;
|
||||
}
|
||||
ESP_LOGI(TAG, "Enabling wakeup on GPIO%d, wakeup on %s level",
|
||||
io_num, level ? "HIGH" : "LOW");
|
||||
|
||||
ESP_ERROR_CHECK( gpio_wakeup_enable(io_num, level ? GPIO_INTR_HIGH_LEVEL : GPIO_INTR_LOW_LEVEL) );
|
||||
}
|
||||
if (io_count > 0) {
|
||||
ESP_ERROR_CHECK( esp_sleep_enable_gpio_wakeup() );
|
||||
}
|
||||
if (CONFIG_CONSOLE_UART_NUM <= UART_NUM_1) {
|
||||
ESP_LOGI(TAG, "Enabling UART wakeup (press ENTER to exit light sleep)");
|
||||
ESP_ERROR_CHECK( uart_set_wakeup_threshold(CONFIG_CONSOLE_UART_NUM, 3) );
|
||||
ESP_ERROR_CHECK( esp_sleep_enable_uart_wakeup(CONFIG_CONSOLE_UART_NUM) );
|
||||
}
|
||||
fflush(stdout);
|
||||
uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM);
|
||||
esp_light_sleep_start();
|
||||
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
|
||||
const char *cause_str;
|
||||
switch (cause) {
|
||||
case ESP_SLEEP_WAKEUP_GPIO:
|
||||
cause_str = "GPIO";
|
||||
break;
|
||||
case ESP_SLEEP_WAKEUP_UART:
|
||||
cause_str = "UART";
|
||||
break;
|
||||
case ESP_SLEEP_WAKEUP_TIMER:
|
||||
cause_str = "timer";
|
||||
break;
|
||||
default:
|
||||
cause_str = "unknown";
|
||||
printf("%d\n", cause);
|
||||
}
|
||||
ESP_LOGI(TAG, "Woke up from: %s", cause_str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void register_light_sleep()
|
||||
{
|
||||
light_sleep_args.wakeup_time =
|
||||
arg_int0("t", "time", "<t>", "Wake up time, ms");
|
||||
light_sleep_args.wakeup_gpio_num =
|
||||
arg_intn(NULL, "io", "<n>", 0, 8,
|
||||
"If specified, wakeup using GPIO with given number");
|
||||
light_sleep_args.wakeup_gpio_level =
|
||||
arg_intn(NULL, "io_level", "<0|1>", 0, 8, "GPIO level to trigger wakeup");
|
||||
light_sleep_args.end = arg_end(3);
|
||||
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "light_sleep",
|
||||
.help = "Enter light sleep mode. "
|
||||
"Two wakeup modes are supported: timer and GPIO. "
|
||||
"Multiple GPIO pins can be specified using pairs of "
|
||||
"'io' and 'io_level' arguments. "
|
||||
"Will also wake up on UART input.",
|
||||
.hint = NULL,
|
||||
.func = &light_sleep,
|
||||
.argtable = &light_sleep_args
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
|
||||
23
components/platform_console/cmd_system.h
Normal file
23
components/platform_console/cmd_system.h
Normal file
@@ -0,0 +1,23 @@
|
||||
/* Console example — various system commands
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#pragma once
|
||||
#include "esp_system.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Register system functions
|
||||
void register_system();
|
||||
esp_err_t guided_factory();
|
||||
esp_err_t guided_restart_ota();
|
||||
void simple_restart();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
200
components/platform_console/cmd_wifi.c
Normal file
200
components/platform_console/cmd_wifi.c
Normal file
@@ -0,0 +1,200 @@
|
||||
/* Console example — WiFi commands
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
// cmd_wifi has been replaced by wifi-manager
|
||||
/* Console example <20> WiFi commands
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#include "cmd_wifi.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cmd_decl.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_console.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "tcpip_adapter.h"
|
||||
#include "esp_event.h"
|
||||
#include "led.h"
|
||||
extern bool bypass_wifi_manager;
|
||||
#define JOIN_TIMEOUT_MS (10000)
|
||||
|
||||
extern EventGroupHandle_t wifi_event_group;
|
||||
extern const int CONNECTED_BIT;
|
||||
static const char * TAG = "cmd_wifi";
|
||||
/** Arguments used by 'join' function */
|
||||
static struct {
|
||||
struct arg_int *timeout;
|
||||
struct arg_str *ssid;
|
||||
struct arg_str *password;
|
||||
struct arg_end *end;
|
||||
} join_args;
|
||||
|
||||
///** Arguments used by 'join' function */
|
||||
//static struct {
|
||||
// struct arg_int *autoconnect;
|
||||
// struct arg_end *end;
|
||||
//} auto_connect_args;
|
||||
|
||||
static void event_handler(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data)
|
||||
{
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
led_blink_pushed(LED_GREEN, 250, 250);
|
||||
esp_wifi_connect();
|
||||
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
led_unpush(LED_GREEN);
|
||||
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
||||
}
|
||||
}
|
||||
//bool wait_for_wifi(){
|
||||
//
|
||||
// bool connected=(xEventGroupGetBits(wifi_event_group) & CONNECTED_BIT)!=0;
|
||||
//
|
||||
// if(!connected){
|
||||
// ESP_LOGD(TAG,"Waiting for WiFi...");
|
||||
// connected = (xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
|
||||
// pdFALSE, pdTRUE, JOIN_TIMEOUT_MS / portTICK_PERIOD_MS)& CONNECTED_BIT)!=0;
|
||||
// if(!connected){
|
||||
// ESP_LOGD(TAG,"wifi timeout.");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// ESP_LOGI(TAG,"WiFi Connected!");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// return connected;
|
||||
//
|
||||
//}
|
||||
static void initialise_wifi(void)
|
||||
{
|
||||
static bool initialized = false;
|
||||
if (initialized) {
|
||||
return;
|
||||
}
|
||||
tcpip_adapter_init();
|
||||
// Now moved to esp_app_main: wifi_event_group = xEventGroupCreate();
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
|
||||
ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &event_handler, NULL) );
|
||||
ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) );
|
||||
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
|
||||
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) );
|
||||
ESP_ERROR_CHECK( esp_wifi_start() );
|
||||
initialized = true;
|
||||
led_blink(LED_GREEN, 250, 250);
|
||||
}
|
||||
|
||||
static bool wifi_join(const char *ssid, const char *pass, int timeout_ms)
|
||||
{
|
||||
initialise_wifi();
|
||||
wifi_config_t wifi_config = { 0 };
|
||||
strncpy((char *) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
|
||||
if (pass) {
|
||||
strncpy((char *) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password));
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
|
||||
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
|
||||
ESP_ERROR_CHECK( esp_wifi_connect() );
|
||||
|
||||
int bits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
|
||||
pdFALSE, pdTRUE, timeout_ms / portTICK_PERIOD_MS);
|
||||
return (bits & CONNECTED_BIT) != 0;
|
||||
}
|
||||
|
||||
|
||||
static int set_auto_connect(int argc, char **argv)
|
||||
{
|
||||
// int nerrors = arg_parse(argc, argv, (void **) &join_args);
|
||||
// if (nerrors != 0) {
|
||||
// arg_print_errors(stderr, join_args.end, argv[0]);
|
||||
// return 1;
|
||||
// }
|
||||
// ESP_LOGI(__func__, "Connecting to '%s'",
|
||||
// join_args.ssid->sval[0]);
|
||||
//
|
||||
// /* set default value*/
|
||||
// if (join_args.timeout->count == 0) {
|
||||
// join_args.timeout->ival[0] = JOIN_TIMEOUT_MS;
|
||||
// }
|
||||
//
|
||||
// bool connected = wifi_join(join_args.ssid->sval[0],
|
||||
// join_args.password->sval[0],
|
||||
// join_args.timeout->ival[0]);
|
||||
// if (!connected) {
|
||||
// ESP_LOGW(__func__, "Connection timed out");
|
||||
// return 1;
|
||||
// }
|
||||
// ESP_LOGI(__func__, "Connected");
|
||||
return 0;
|
||||
}
|
||||
static int connect(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse(argc, argv, (void **) &join_args);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, join_args.end, argv[0]);
|
||||
return 1;
|
||||
}
|
||||
ESP_LOGI(__func__, "Connecting to '%s'",
|
||||
join_args.ssid->sval[0]);
|
||||
|
||||
/* set default value*/
|
||||
if (join_args.timeout->count == 0) {
|
||||
join_args.timeout->ival[0] = JOIN_TIMEOUT_MS;
|
||||
}
|
||||
|
||||
bool connected = wifi_join(join_args.ssid->sval[0],
|
||||
join_args.password->sval[0],
|
||||
join_args.timeout->ival[0]);
|
||||
if (!connected) {
|
||||
ESP_LOGW(__func__, "Connection timed out");
|
||||
return 1;
|
||||
}
|
||||
ESP_LOGI(__func__, "Connected");
|
||||
return 0;
|
||||
}
|
||||
void register_wifi_join()
|
||||
{
|
||||
join_args.timeout = arg_int0(NULL, "timeout", "<t>", "Connection timeout, ms");
|
||||
join_args.ssid = arg_str1(NULL, NULL, "<ssid>", "SSID of AP");
|
||||
join_args.password = arg_str0(NULL, NULL, "<pass>", "PSK of AP");
|
||||
join_args.end = arg_end(2);
|
||||
|
||||
const esp_console_cmd_t join_cmd = {
|
||||
.command = "join",
|
||||
.help = "Join WiFi AP as a station",
|
||||
.hint = NULL,
|
||||
.func = &connect,
|
||||
.argtable = &join_args
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&join_cmd) );
|
||||
}
|
||||
|
||||
void register_wifi()
|
||||
{
|
||||
register_wifi_join();
|
||||
if(bypass_wifi_manager){
|
||||
initialise_wifi();
|
||||
}
|
||||
}
|
||||
18
components/platform_console/cmd_wifi.h
Normal file
18
components/platform_console/cmd_wifi.h
Normal file
@@ -0,0 +1,18 @@
|
||||
/* Console example <20> declarations of command registration functions.
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Register WiFi functions
|
||||
void register_wifi();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
266
components/platform_console/platform_console.c
Normal file
266
components/platform_console/platform_console.c
Normal file
@@ -0,0 +1,266 @@
|
||||
/* Console example
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#include "platform_console.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "esp_system.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_console.h"
|
||||
#include "esp_vfs_dev.h"
|
||||
#include "driver/uart.h"
|
||||
#include "linenoise/linenoise.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "nvs.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "pthread.h"
|
||||
#include "platform_esp32.h"
|
||||
#include "esp_pthread.h"
|
||||
#include "cmd_decl.h"
|
||||
#include "wifi_manager.h"
|
||||
|
||||
#include "cmd_squeezelite.h"
|
||||
#include "platform_config.h"
|
||||
pthread_t thread_console;
|
||||
static void * console_thread();
|
||||
void console_start();
|
||||
static const char * TAG = "console";
|
||||
extern bool bypass_wifi_manager;
|
||||
|
||||
|
||||
/* Prompt to be printed before each line.
|
||||
* This can be customized, made dynamic, etc.
|
||||
*/
|
||||
const char* prompt = LOG_COLOR_I "squeezelite-esp32> " LOG_RESET_COLOR;
|
||||
|
||||
/* Console command history can be stored to and loaded from a file.
|
||||
* The easiest way to do this is to use FATFS filesystem on top of
|
||||
* wear_levelling library.
|
||||
*/
|
||||
|
||||
#define MOUNT_PATH "/data"
|
||||
#define HISTORY_PATH MOUNT_PATH "/history.txt"
|
||||
void run_command(char * line);
|
||||
|
||||
void process_autoexec(){
|
||||
int i=1;
|
||||
char autoexec_name[21]={0};
|
||||
char * autoexec_value=NULL;
|
||||
uint8_t autoexec_flag=0;
|
||||
|
||||
char * str_flag = config_alloc_get(NVS_TYPE_STR, "autoexec");
|
||||
if(!bypass_wifi_manager){
|
||||
ESP_LOGW(TAG, "Processing autoexec commands while wifi_manager active. Wifi related commands will be ignored.");
|
||||
}
|
||||
#if RECOVERY_APPLICATION
|
||||
ESP_LOGD(TAG, "Processing autoexec commands in recovery mode. Squeezelite commands will be ignored.");
|
||||
#endif
|
||||
|
||||
if(str_flag !=NULL ){
|
||||
autoexec_flag=atoi(str_flag);
|
||||
ESP_LOGI(TAG,"autoexec is set to %s auto-process", autoexec_flag>0?"perform":"skip");
|
||||
if(autoexec_flag == 1) {
|
||||
do {
|
||||
snprintf(autoexec_name,sizeof(autoexec_name)-1,"autoexec%u",i++);
|
||||
ESP_LOGD(TAG,"Getting command name %s", autoexec_name);
|
||||
autoexec_value= config_alloc_get(NVS_TYPE_STR, autoexec_name);
|
||||
if(autoexec_value!=NULL ){
|
||||
if(!bypass_wifi_manager && strstr(autoexec_value, "join ")!=NULL ){
|
||||
ESP_LOGW(TAG,"Ignoring wifi join command.");
|
||||
}
|
||||
#if RECOVERY_APPLICATION
|
||||
else if(!strstr(autoexec_value, "squeezelite " ) ){
|
||||
ESP_LOGW(TAG,"Ignoring command. ");
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
ESP_LOGI(TAG,"Running command %s = %s", autoexec_name, autoexec_value);
|
||||
run_command(autoexec_value);
|
||||
}
|
||||
ESP_LOGD(TAG,"Freeing memory for command %s name", autoexec_name);
|
||||
free(autoexec_value);
|
||||
}
|
||||
else {
|
||||
ESP_LOGD(TAG,"No matching command found for name %s", autoexec_name);
|
||||
break;
|
||||
}
|
||||
} while(1);
|
||||
}
|
||||
free(str_flag);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGD(TAG,"No matching command found for name autoexec.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void initialize_console() {
|
||||
|
||||
/* Disable buffering on stdin */
|
||||
setvbuf(stdin, NULL, _IONBF, 0);
|
||||
|
||||
/* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
|
||||
esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
|
||||
/* Move the caret to the beginning of the next line on '\n' */
|
||||
esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
|
||||
|
||||
/* Configure UART. Note that REF_TICK is used so that the baud rate remains
|
||||
* correct while APB frequency is changing in light sleep mode.
|
||||
*/
|
||||
const uart_config_t uart_config = { .baud_rate =
|
||||
CONFIG_CONSOLE_UART_BAUDRATE, .data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1,
|
||||
.use_ref_tick = true };
|
||||
ESP_ERROR_CHECK(uart_param_config(CONFIG_CONSOLE_UART_NUM, &uart_config));
|
||||
|
||||
/* Install UART driver for interrupt-driven reads and writes */
|
||||
ESP_ERROR_CHECK(
|
||||
uart_driver_install(CONFIG_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0));
|
||||
|
||||
/* Tell VFS to use UART driver */
|
||||
esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM);
|
||||
|
||||
/* Initialize the console */
|
||||
esp_console_config_t console_config = { .max_cmdline_args = 22,
|
||||
.max_cmdline_length = 600,
|
||||
#if CONFIG_LOG_COLORS
|
||||
.hint_color = atoi(LOG_COLOR_CYAN)
|
||||
#endif
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_init(&console_config));
|
||||
|
||||
/* Configure linenoise line completion library */
|
||||
/* Enable multiline editing. If not set, long commands will scroll within
|
||||
* single line.
|
||||
*/
|
||||
linenoiseSetMultiLine(1);
|
||||
|
||||
/* Tell linenoise where to get command completions and hints */
|
||||
linenoiseSetCompletionCallback(&esp_console_get_completion);
|
||||
linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
|
||||
|
||||
/* Set command history size */
|
||||
linenoiseHistorySetMaxLen(100);
|
||||
|
||||
/* Load command history from filesystem */
|
||||
//linenoiseHistoryLoad(HISTORY_PATH);
|
||||
}
|
||||
|
||||
void console_start() {
|
||||
|
||||
initialize_console();
|
||||
|
||||
/* Register commands */
|
||||
esp_console_register_help_command();
|
||||
register_system();
|
||||
register_nvs();
|
||||
register_wifi();
|
||||
#if RECOVERY_APPLICATION!=1
|
||||
register_squeezelite();
|
||||
#elif RECOVERY_APPLICATION==1
|
||||
register_ota_cmd();
|
||||
#else
|
||||
#error "Unknown build configuration"
|
||||
#endif
|
||||
register_i2ctools();
|
||||
printf("\n"
|
||||
#if RECOVERY_APPLICATION
|
||||
"****************************************************************\n"
|
||||
"RECOVERY APPLICATION\n"
|
||||
"This mode is used to flash Squeezelite into the OTA partition\n"
|
||||
"****\n\n"
|
||||
#endif
|
||||
"Type 'help' to get the list of commands.\n"
|
||||
"Use UP/DOWN arrows to navigate through command history.\n"
|
||||
"Press TAB when typing command name to auto-complete.\n"
|
||||
"\n"
|
||||
#if !RECOVERY_APPLICATION
|
||||
"To automatically execute lines at startup:\n"
|
||||
"\tSet NVS variable autoexec (U8) = 1 to enable, 0 to disable automatic execution.\n"
|
||||
"\tSet NVS variable autoexec[1~9] (string)to a command that should be executed automatically\n"
|
||||
#endif
|
||||
"\n"
|
||||
"\n");
|
||||
|
||||
/* Figure out if the terminal supports escape sequences */
|
||||
int probe_status = linenoiseProbe();
|
||||
if (probe_status) { /* zero indicates success */
|
||||
printf("\n****************************\n"
|
||||
"Your terminal application does not support escape sequences.\n"
|
||||
"Line editing and history features are disabled.\n"
|
||||
"On Windows, try using Putty instead.\n"
|
||||
"****************************\n");
|
||||
linenoiseSetDumbMode(1);
|
||||
#if CONFIG_LOG_COLORS
|
||||
/* Since the terminal doesn't support escape sequences,
|
||||
* don't use color codes in the prompt.
|
||||
*/
|
||||
prompt = "squeezelite-esp32> ";
|
||||
#endif //CONFIG_LOG_COLORS
|
||||
|
||||
}
|
||||
esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
|
||||
cfg.thread_name= "console";
|
||||
cfg.inherit_cfg = true;
|
||||
#if RECOVERY_APPLICATION
|
||||
cfg.stack_size = 4096 ;
|
||||
#endif
|
||||
esp_pthread_set_cfg(&cfg);
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
|
||||
pthread_create(&thread_console, &attr, console_thread, NULL);
|
||||
pthread_attr_destroy(&attr);
|
||||
}
|
||||
void run_command(char * line){
|
||||
/* Try to run the command */
|
||||
int ret;
|
||||
esp_err_t err = esp_console_run(line, &ret);
|
||||
|
||||
if (err == ESP_ERR_NOT_FOUND) {
|
||||
ESP_LOGE(TAG,"Unrecognized command: %s\n", line);
|
||||
} else if (err == ESP_ERR_INVALID_ARG) {
|
||||
// command was empty
|
||||
} else if (err == ESP_OK && ret != ESP_OK) {
|
||||
ESP_LOGW(TAG,"Command returned non-zero error code: 0x%x (%s)\n", ret,
|
||||
esp_err_to_name(err));
|
||||
} else if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG,"Internal error: %s\n", esp_err_to_name(err));
|
||||
}
|
||||
}
|
||||
static void * console_thread() {
|
||||
#if !RECOVERY_APPLICATION
|
||||
process_autoexec();
|
||||
#endif
|
||||
/* Main loop */
|
||||
while (1) {
|
||||
/* Get a line using linenoise.
|
||||
* The line is returned when ENTER is pressed.
|
||||
*/
|
||||
char* line = linenoise(prompt);
|
||||
if (line == NULL) { /* Ignore empty lines */
|
||||
continue;
|
||||
}
|
||||
/* Add the command to the history */
|
||||
linenoiseHistoryAdd(line);
|
||||
|
||||
/* Save command history to filesystem */
|
||||
linenoiseHistorySave(HISTORY_PATH);
|
||||
printf("\n");
|
||||
run_command(line);
|
||||
/* linenoise allocates line buffer on the heap, so need to free it */
|
||||
linenoiseFree(line);
|
||||
taskYIELD();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
18
components/platform_console/platform_console.h
Normal file
18
components/platform_console/platform_console.h
Normal file
@@ -0,0 +1,18 @@
|
||||
/* Console example — declarations of command registration functions.
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user