2017-08-15 04:11:19 -04:00
|
|
|
/* 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 <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2022-12-09 02:04:55 -05:00
|
|
|
#include <inttypes.h>
|
2020-05-12 08:54:55 -04:00
|
|
|
#include <unistd.h>
|
2017-08-15 04:11:19 -04:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_console.h"
|
2022-01-12 01:53:47 -05:00
|
|
|
#include "esp_chip_info.h"
|
2017-04-21 00:32:50 -04:00
|
|
|
#include "esp_sleep.h"
|
2022-06-27 03:24:07 -04:00
|
|
|
#include "esp_flash.h"
|
2017-08-15 04:11:19 -04:00
|
|
|
#include "driver/rtc_io.h"
|
2018-08-13 20:44:04 -04:00
|
|
|
#include "driver/uart.h"
|
2017-08-15 04:11:19 -04:00
|
|
|
#include "argtable3/argtable3.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2018-09-20 07:26:14 -04:00
|
|
|
#include "cmd_system.h"
|
2018-01-31 01:04:40 -05:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS
|
|
|
|
#define WITH_TASKS_INFO 1
|
|
|
|
#endif
|
2017-08-15 04:11:19 -04:00
|
|
|
|
2018-09-20 07:26:14 -04:00
|
|
|
static const char *TAG = "cmd_system";
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void register_free(void);
|
|
|
|
static void register_heap(void);
|
|
|
|
static void register_version(void);
|
|
|
|
static void register_restart(void);
|
|
|
|
static void register_deep_sleep(void);
|
|
|
|
static void register_light_sleep(void);
|
2018-01-31 01:04:40 -05:00
|
|
|
#if WITH_TASKS_INFO
|
2019-07-16 05:33:30 -04:00
|
|
|
static void register_tasks(void);
|
2018-01-31 01:04:40 -05:00
|
|
|
#endif
|
2022-06-21 11:14:08 -04:00
|
|
|
static void register_log_level(void);
|
2017-08-15 04:11:19 -04:00
|
|
|
|
2020-04-30 10:52:39 -04:00
|
|
|
void register_system_common(void)
|
2017-08-15 04:11:19 -04:00
|
|
|
{
|
|
|
|
register_free();
|
2018-09-20 07:26:14 -04:00
|
|
|
register_heap();
|
|
|
|
register_version();
|
2017-08-15 04:11:19 -04:00
|
|
|
register_restart();
|
2018-01-31 01:04:40 -05:00
|
|
|
#if WITH_TASKS_INFO
|
|
|
|
register_tasks();
|
|
|
|
#endif
|
2022-06-21 11:14:08 -04:00
|
|
|
register_log_level();
|
2017-08-15 04:11:19 -04:00
|
|
|
}
|
|
|
|
|
2020-04-30 10:52:39 -04:00
|
|
|
void register_system_sleep(void)
|
|
|
|
{
|
|
|
|
register_deep_sleep();
|
|
|
|
register_light_sleep();
|
|
|
|
}
|
|
|
|
|
|
|
|
void register_system(void)
|
|
|
|
{
|
|
|
|
register_system_common();
|
|
|
|
register_system_sleep();
|
|
|
|
}
|
|
|
|
|
2018-09-20 07:26:14 -04:00
|
|
|
/* 'version' command */
|
|
|
|
static int get_version(int argc, char **argv)
|
|
|
|
{
|
2021-10-17 20:08:25 -04:00
|
|
|
const char *model;
|
2018-09-20 07:26:14 -04:00
|
|
|
esp_chip_info_t info;
|
2022-06-27 03:24:07 -04:00
|
|
|
uint32_t flash_size;
|
2018-09-20 07:26:14 -04:00
|
|
|
esp_chip_info(&info);
|
2021-10-17 20:08:25 -04:00
|
|
|
|
|
|
|
switch(info.model) {
|
|
|
|
case CHIP_ESP32:
|
|
|
|
model = "ESP32";
|
|
|
|
break;
|
|
|
|
case CHIP_ESP32S2:
|
|
|
|
model = "ESP32-S2";
|
|
|
|
break;
|
|
|
|
case CHIP_ESP32S3:
|
|
|
|
model = "ESP32-S3";
|
|
|
|
break;
|
|
|
|
case CHIP_ESP32C3:
|
|
|
|
model = "ESP32-C3";
|
|
|
|
break;
|
2022-10-19 03:57:24 -04:00
|
|
|
case CHIP_ESP32H4:
|
|
|
|
model = "ESP32-H4";
|
2021-10-17 20:08:25 -04:00
|
|
|
break;
|
2022-07-12 12:01:31 -04:00
|
|
|
case CHIP_ESP32C2:
|
|
|
|
model = "ESP32-C2";
|
|
|
|
break;
|
2021-10-17 20:08:25 -04:00
|
|
|
default:
|
|
|
|
model = "Unknown";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-27 03:24:07 -04:00
|
|
|
if(esp_flash_get_size(NULL, &flash_size) != ESP_OK) {
|
|
|
|
printf("Get flash size failed");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-09-20 07:26:14 -04:00
|
|
|
printf("IDF Version:%s\r\n", esp_get_idf_version());
|
|
|
|
printf("Chip info:\r\n");
|
2021-10-17 20:08:25 -04:00
|
|
|
printf("\tmodel:%s\r\n", model);
|
2018-09-20 07:26:14 -04:00
|
|
|
printf("\tcores:%d\r\n", info.cores);
|
2022-12-09 02:04:55 -05:00
|
|
|
printf("\tfeature:%s%s%s%s%"PRIu32"%s\r\n",
|
2018-09-20 07:26:14 -04:00
|
|
|
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:",
|
2022-06-27 03:24:07 -04:00
|
|
|
flash_size / (1024 * 1024), " MB");
|
2018-09-20 07:26:14 -04:00
|
|
|
printf("\trevision number:%d\r\n", info.revision);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void register_version(void)
|
2018-09-20 07:26:14 -04:00
|
|
|
{
|
|
|
|
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) );
|
|
|
|
}
|
|
|
|
|
2017-08-15 04:11:19 -04:00
|
|
|
/** 'restart' command restarts the program */
|
|
|
|
|
2018-09-20 07:26:14 -04:00
|
|
|
static int restart(int argc, char **argv)
|
2017-08-15 04:11:19 -04:00
|
|
|
{
|
2018-09-20 07:26:14 -04:00
|
|
|
ESP_LOGI(TAG, "Restarting");
|
2017-08-15 04:11:19 -04:00
|
|
|
esp_restart();
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void register_restart(void)
|
2017-08-15 04:11:19 -04:00
|
|
|
{
|
|
|
|
const esp_console_cmd_t cmd = {
|
|
|
|
.command = "restart",
|
2018-09-20 07:26:14 -04:00
|
|
|
.help = "Software reset of the chip",
|
2017-08-15 04:11:19 -04:00
|
|
|
.hint = NULL,
|
|
|
|
.func = &restart,
|
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 'free' command prints available heap memory */
|
|
|
|
|
2018-09-20 07:26:14 -04:00
|
|
|
static int free_mem(int argc, char **argv)
|
2017-08-15 04:11:19 -04:00
|
|
|
{
|
2022-12-09 02:04:55 -05:00
|
|
|
printf("%"PRIu32"\n", esp_get_free_heap_size());
|
2017-08-15 04:11:19 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void register_free(void)
|
2017-08-15 04:11:19 -04:00
|
|
|
{
|
|
|
|
const esp_console_cmd_t cmd = {
|
|
|
|
.command = "free",
|
2018-09-20 07:26:14 -04:00
|
|
|
.help = "Get the current size of free heap memory",
|
2017-08-15 04:11:19 -04:00
|
|
|
.hint = NULL,
|
|
|
|
.func = &free_mem,
|
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
|
|
|
}
|
|
|
|
|
2018-09-20 07:26:14 -04:00
|
|
|
/* '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);
|
2022-12-09 02:04:55 -05:00
|
|
|
printf("min heap size: %"PRIu32"\n", heap_size);
|
2018-09-20 07:26:14 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void register_heap(void)
|
2018-09-20 07:26:14 -04:00
|
|
|
{
|
|
|
|
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) );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-01-31 01:04:40 -05:00
|
|
|
/** 'tasks' command prints the list of tasks and related information */
|
|
|
|
#if WITH_TASKS_INFO
|
|
|
|
|
2018-09-20 07:26:14 -04:00
|
|
|
static int tasks_info(int argc, char **argv)
|
2018-01-31 01:04:40 -05:00
|
|
|
{
|
|
|
|
const size_t bytes_per_task = 40; /* see vTaskList description */
|
2018-09-20 07:26:14 -04:00
|
|
|
char *task_list_buffer = malloc(uxTaskGetNumberOfTasks() * bytes_per_task);
|
2018-01-31 01:04:40 -05:00
|
|
|
if (task_list_buffer == NULL) {
|
2018-09-20 07:26:14 -04:00
|
|
|
ESP_LOGE(TAG, "failed to allocate buffer for vTaskList output");
|
2018-01-31 01:04:40 -05:00
|
|
|
return 1;
|
|
|
|
}
|
2018-07-09 03:25:59 -04:00
|
|
|
fputs("Task Name\tStatus\tPrio\tHWM\tTask#", stdout);
|
|
|
|
#ifdef CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID
|
|
|
|
fputs("\tAffinity", stdout);
|
|
|
|
#endif
|
|
|
|
fputs("\n", stdout);
|
2018-01-31 01:04:40 -05:00
|
|
|
vTaskList(task_list_buffer);
|
|
|
|
fputs(task_list_buffer, stdout);
|
|
|
|
free(task_list_buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void register_tasks(void)
|
2018-01-31 01:04:40 -05:00
|
|
|
{
|
|
|
|
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
|
|
|
|
|
2017-08-15 04:11:19 -04:00
|
|
|
/** 'deep_sleep' command puts the chip into deep sleep mode */
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
struct arg_int *wakeup_time;
|
2021-01-18 04:46:37 -05:00
|
|
|
#if SOC_PM_SUPPORT_EXT_WAKEUP
|
2017-08-15 04:11:19 -04:00
|
|
|
struct arg_int *wakeup_gpio_num;
|
|
|
|
struct arg_int *wakeup_gpio_level;
|
2021-01-18 04:46:37 -05:00
|
|
|
#endif
|
2017-08-15 04:11:19 -04:00
|
|
|
struct arg_end *end;
|
|
|
|
} deep_sleep_args;
|
|
|
|
|
|
|
|
|
2018-09-20 07:26:14 -04:00
|
|
|
static int deep_sleep(int argc, char **argv)
|
2017-08-15 04:11:19 -04:00
|
|
|
{
|
2018-09-20 07:26:14 -04:00
|
|
|
int nerrors = arg_parse(argc, argv, (void **) &deep_sleep_args);
|
2017-08-15 04:11:19 -04:00
|
|
|
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];
|
2018-09-20 07:26:14 -04:00
|
|
|
ESP_LOGI(TAG, "Enabling timer wakeup, timeout=%lluus", timeout);
|
2017-04-21 00:32:50 -04:00
|
|
|
ESP_ERROR_CHECK( esp_sleep_enable_timer_wakeup(timeout) );
|
2017-08-15 04:11:19 -04:00
|
|
|
}
|
2021-01-18 04:46:37 -05:00
|
|
|
|
|
|
|
#if SOC_PM_SUPPORT_EXT_WAKEUP
|
2017-08-15 04:11:19 -04:00
|
|
|
if (deep_sleep_args.wakeup_gpio_num->count) {
|
|
|
|
int io_num = deep_sleep_args.wakeup_gpio_num->ival[0];
|
2020-11-23 01:09:16 -05:00
|
|
|
if (!esp_sleep_is_valid_wakeup_gpio(io_num)) {
|
2018-09-20 07:26:14 -04:00
|
|
|
ESP_LOGE(TAG, "GPIO %d is not an RTC IO", io_num);
|
2017-08-15 04:11:19 -04:00
|
|
|
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) {
|
2018-09-20 07:26:14 -04:00
|
|
|
ESP_LOGE(TAG, "Invalid wakeup level: %d", level);
|
2017-08-15 04:11:19 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2018-09-20 07:26:14 -04:00
|
|
|
ESP_LOGI(TAG, "Enabling wakeup on GPIO%d, wakeup on %s level",
|
|
|
|
io_num, level ? "HIGH" : "LOW");
|
2017-08-15 04:11:19 -04:00
|
|
|
|
2017-04-21 00:32:50 -04:00
|
|
|
ESP_ERROR_CHECK( esp_sleep_enable_ext1_wakeup(1ULL << io_num, level) );
|
2021-01-18 04:46:37 -05:00
|
|
|
ESP_LOGE(TAG, "GPIO wakeup from deep sleep currently unsupported on ESP32-C3");
|
2017-08-15 04:11:19 -04:00
|
|
|
}
|
2021-01-18 04:46:37 -05:00
|
|
|
#endif // SOC_PM_SUPPORT_EXT_WAKEUP
|
2020-12-14 22:00:02 -05:00
|
|
|
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2018-02-09 11:34:38 -05:00
|
|
|
rtc_gpio_isolate(GPIO_NUM_12);
|
2020-12-14 22:00:02 -05:00
|
|
|
#endif //CONFIG_IDF_TARGET_ESP32
|
|
|
|
|
2017-08-15 04:11:19 -04:00
|
|
|
esp_deep_sleep_start();
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void register_deep_sleep(void)
|
2017-08-15 04:11:19 -04:00
|
|
|
{
|
2021-01-18 04:46:37 -05:00
|
|
|
int num_args = 1;
|
2017-08-15 04:11:19 -04:00
|
|
|
deep_sleep_args.wakeup_time =
|
2018-09-20 07:26:14 -04:00
|
|
|
arg_int0("t", "time", "<t>", "Wake up time, ms");
|
2021-01-18 04:46:37 -05:00
|
|
|
#if SOC_PM_SUPPORT_EXT_WAKEUP
|
2017-08-15 04:11:19 -04:00
|
|
|
deep_sleep_args.wakeup_gpio_num =
|
2018-09-20 07:26:14 -04:00
|
|
|
arg_int0(NULL, "io", "<n>",
|
|
|
|
"If specified, wakeup using GPIO with given number");
|
2017-08-15 04:11:19 -04:00
|
|
|
deep_sleep_args.wakeup_gpio_level =
|
2018-09-20 07:26:14 -04:00
|
|
|
arg_int0(NULL, "io_level", "<0|1>", "GPIO level to trigger wakeup");
|
2021-01-18 04:46:37 -05:00
|
|
|
num_args += 2;
|
|
|
|
#endif
|
|
|
|
deep_sleep_args.end = arg_end(num_args);
|
2017-08-15 04:11:19 -04:00
|
|
|
|
|
|
|
const esp_console_cmd_t cmd = {
|
|
|
|
.command = "deep_sleep",
|
|
|
|
.help = "Enter deep sleep mode. "
|
2021-01-18 04:46:37 -05:00
|
|
|
#if SOC_PM_SUPPORT_EXT_WAKEUP
|
2018-09-20 07:26:14 -04:00
|
|
|
"Two wakeup modes are supported: timer and GPIO. "
|
2021-01-18 04:46:37 -05:00
|
|
|
#else
|
|
|
|
"Timer wakeup mode is supported. "
|
|
|
|
#endif
|
2018-09-20 07:26:14 -04:00
|
|
|
"If no wakeup option is specified, will sleep indefinitely.",
|
2017-08-15 04:11:19 -04:00
|
|
|
.hint = NULL,
|
|
|
|
.func = &deep_sleep,
|
|
|
|
.argtable = &deep_sleep_args
|
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
2018-08-13 20:44:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** '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;
|
|
|
|
|
2018-09-20 07:26:14 -04:00
|
|
|
static int light_sleep(int argc, char **argv)
|
2018-08-13 20:44:04 -04:00
|
|
|
{
|
2018-09-20 07:26:14 -04:00
|
|
|
int nerrors = arg_parse(argc, argv, (void **) &light_sleep_args);
|
2018-08-13 20:44:04 -04:00
|
|
|
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];
|
2018-09-20 07:26:14 -04:00
|
|
|
ESP_LOGI(TAG, "Enabling timer wakeup, timeout=%lluus", timeout);
|
2018-08-13 20:44:04 -04:00
|
|
|
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) {
|
2018-09-20 07:26:14 -04:00
|
|
|
ESP_LOGE(TAG, "Should have same number of 'io' and 'io_level' arguments");
|
2018-08-13 20:44:04 -04:00
|
|
|
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) {
|
2018-09-20 07:26:14 -04:00
|
|
|
ESP_LOGE(TAG, "Invalid wakeup level: %d", level);
|
2018-08-13 20:44:04 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2018-09-20 07:26:14 -04:00
|
|
|
ESP_LOGI(TAG, "Enabling wakeup on GPIO%d, wakeup on %s level",
|
|
|
|
io_num, level ? "HIGH" : "LOW");
|
2018-08-13 20:44:04 -04:00
|
|
|
|
|
|
|
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() );
|
|
|
|
}
|
2020-05-12 08:54:55 -04:00
|
|
|
if (CONFIG_ESP_CONSOLE_UART_NUM >= 0 && CONFIG_ESP_CONSOLE_UART_NUM <= UART_NUM_1) {
|
2018-09-20 07:26:14 -04:00
|
|
|
ESP_LOGI(TAG, "Enabling UART wakeup (press ENTER to exit light sleep)");
|
2019-11-27 08:32:52 -05:00
|
|
|
ESP_ERROR_CHECK( uart_set_wakeup_threshold(CONFIG_ESP_CONSOLE_UART_NUM, 3) );
|
2019-04-30 06:51:55 -04:00
|
|
|
ESP_ERROR_CHECK( esp_sleep_enable_uart_wakeup(CONFIG_ESP_CONSOLE_UART_NUM) );
|
2018-08-13 20:44:04 -04:00
|
|
|
}
|
|
|
|
fflush(stdout);
|
2020-05-12 08:54:55 -04:00
|
|
|
fsync(fileno(stdout));
|
2018-08-13 20:44:04 -04:00
|
|
|
esp_light_sleep_start();
|
|
|
|
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
|
2018-09-20 07:26:14 -04:00
|
|
|
const char *cause_str;
|
2018-08-13 20:44:04 -04:00
|
|
|
switch (cause) {
|
2018-09-20 07:26:14 -04:00
|
|
|
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);
|
2018-08-13 20:44:04 -04:00
|
|
|
}
|
2018-09-20 07:26:14 -04:00
|
|
|
ESP_LOGI(TAG, "Woke up from: %s", cause_str);
|
2018-08-13 20:44:04 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void register_light_sleep(void)
|
2018-08-13 20:44:04 -04:00
|
|
|
{
|
|
|
|
light_sleep_args.wakeup_time =
|
2018-09-20 07:26:14 -04:00
|
|
|
arg_int0("t", "time", "<t>", "Wake up time, ms");
|
2018-08-13 20:44:04 -04:00
|
|
|
light_sleep_args.wakeup_gpio_num =
|
2018-09-20 07:26:14 -04:00
|
|
|
arg_intn(NULL, "io", "<n>", 0, 8,
|
|
|
|
"If specified, wakeup using GPIO with given number");
|
2018-08-13 20:44:04 -04:00
|
|
|
light_sleep_args.wakeup_gpio_level =
|
2018-09-20 07:26:14 -04:00
|
|
|
arg_intn(NULL, "io_level", "<0|1>", 0, 8, "GPIO level to trigger wakeup");
|
2018-08-13 20:44:04 -04:00
|
|
|
light_sleep_args.end = arg_end(3);
|
|
|
|
|
|
|
|
const esp_console_cmd_t cmd = {
|
|
|
|
.command = "light_sleep",
|
|
|
|
.help = "Enter light sleep mode. "
|
2018-09-20 07:26:14 -04:00
|
|
|
"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.",
|
2018-08-13 20:44:04 -04:00
|
|
|
.hint = NULL,
|
|
|
|
.func = &light_sleep,
|
|
|
|
.argtable = &light_sleep_args
|
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
2017-08-15 04:11:19 -04:00
|
|
|
}
|
2022-06-21 11:14:08 -04:00
|
|
|
|
|
|
|
/** log_level command changes log level via esp_log_level_set */
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
struct arg_str *tag;
|
|
|
|
struct arg_str *level;
|
|
|
|
struct arg_end *end;
|
|
|
|
} log_level_args;
|
|
|
|
|
|
|
|
static const char* s_log_level_names[] = {
|
|
|
|
"none",
|
|
|
|
"error",
|
|
|
|
"warn",
|
|
|
|
"info",
|
|
|
|
"debug",
|
|
|
|
"verbose"
|
|
|
|
};
|
|
|
|
|
|
|
|
static int log_level(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int nerrors = arg_parse(argc, argv, (void **) &log_level_args);
|
|
|
|
if (nerrors != 0) {
|
|
|
|
arg_print_errors(stderr, log_level_args.end, argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
assert(log_level_args.tag->count == 1);
|
|
|
|
assert(log_level_args.level->count == 1);
|
|
|
|
const char* tag = log_level_args.tag->sval[0];
|
|
|
|
const char* level_str = log_level_args.level->sval[0];
|
|
|
|
esp_log_level_t level;
|
|
|
|
size_t level_len = strlen(level_str);
|
|
|
|
for (level = ESP_LOG_NONE; level <= ESP_LOG_VERBOSE; level++) {
|
|
|
|
if (memcmp(level_str, s_log_level_names[level], level_len) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (level > ESP_LOG_VERBOSE) {
|
|
|
|
printf("Invalid log level '%s', choose from none|error|warn|info|debug|verbose\n", level_str);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (level > CONFIG_LOG_MAXIMUM_LEVEL) {
|
|
|
|
printf("Can't set log level to %s, max level limited in menuconfig to %s. "
|
|
|
|
"Please increase CONFIG_LOG_MAXIMUM_LEVEL in menuconfig.\n",
|
|
|
|
s_log_level_names[level], s_log_level_names[CONFIG_LOG_MAXIMUM_LEVEL]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
esp_log_level_set(tag, level);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void register_log_level(void)
|
|
|
|
{
|
|
|
|
log_level_args.tag = arg_str1(NULL, NULL, "<tag|*>", "Log tag to set the level for, or * to set for all tags");
|
|
|
|
log_level_args.level = arg_str1(NULL, NULL, "<none|error|warn|debug|verbose>", "Log level to set. Abbreviated words are accepted.");
|
|
|
|
log_level_args.end = arg_end(2);
|
|
|
|
|
|
|
|
const esp_console_cmd_t cmd = {
|
|
|
|
.command = "log_level",
|
|
|
|
.help = "Set log level for all tags or a specific tag.",
|
|
|
|
.hint = NULL,
|
|
|
|
.func = &log_level,
|
|
|
|
.argtable = &log_level_args
|
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
|
|
|
}
|