Skip to content

light sleep in regular exection (wip) #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions boards/sdkconfig.common
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=5120
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="boards/partitions.csv"
CONFIG_PARTITION_TABLE_FILENAME="boards/partitions.csv"

CONFIG_PM_ENABLE=y
CONFIG_FREERTOS_USE_TICKLESS_IDLE=y
CONFIG_FREERTOS_HZ=1000
1 change: 1 addition & 0 deletions main/jdesp.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "services/interfaces/jd_adc.h"
#include "services/interfaces/jd_pwm.h"
#include "services/interfaces/jd_flash.h"
#include "services/interfaces/jd_hw_pwr.h"
#include "interfaces/jd_usb.h"
#include "network/jd_network.h"

Expand Down
14 changes: 13 additions & 1 deletion main/led.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "hal/ledc_ll.h"
#include "hal/gpio_ll.h"
#include "esp_rom_gpio.h"
#include "esp_pm.h"

#define LEDC_TIMER_DIV_NUM_MAX (0x3FFFF)

Expand Down Expand Up @@ -190,11 +191,22 @@ void pin_setup_analog_input(int pin) {
CHK(gpio_set_direction(pin, GPIO_MODE_DISABLE));
}

void pwr_enter_no_sleep(void) {}
void pwr_enter_tim(void) {}
void pwr_leave_tim(void) {}

void pwr_enter_pll(void) {}
void pwr_leave_pll(void) {}

void power_pin_enable(int en) {}

static esp_pm_lock_handle_t pwr_handle;
void pwr_enter_no_sleep(void) {
if (!pwr_handle) {
esp_pm_lock_create(ESP_PM_CPU_FREQ_MAX, 0, "PWR", &pwr_handle);
}
esp_pm_lock_acquire(pwr_handle);
}

void pwr_leave_no_sleep(void) {
esp_pm_lock_release(pwr_handle);
}
18 changes: 18 additions & 0 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "esp_timer.h"
#include "esp_event.h"
#include "esp_pm.h"
#include "esp_task_wdt.h"

#ifdef CONFIG_IDF_TARGET_ESP32C3
Expand Down Expand Up @@ -87,6 +88,13 @@ static void loop_handler(void *event_handler_arg, esp_event_base_t event_base, i
n = 0;
}

static uint8_t pm_en;
if (!pm_en && now >= (10 << 20)) {
pm_en = 1;
pwr_leave_no_sleep();
DMESG("enabling light sleep");
}

CHK(esp_task_wdt_reset());

#if defined(PIN_BOOT_BTN)
Expand Down Expand Up @@ -185,6 +193,16 @@ void app_main() {
CHK(esp_timer_create(&args, &main_loop_tick_timer));
sync_main_loop_timer();

pwr_enter_no_sleep(); // take power lock, it's released a bit later
#if CONFIG_IDF_TARGET_ESP32C3
const esp_pm_config_esp32c3_t config = {
.max_freq_mhz = CONFIG_ESP32C3_DEFAULT_CPU_FREQ_MHZ,
.min_freq_mhz = (int)rtc_clk_xtal_freq_get(),
.light_sleep_enable = true,
};
CHK(esp_pm_configure(&config));
#endif

DMESG("app_main mostly done");

CHK(esp_event_handler_instance_register(JD_EVENT, 1, loop_handler, NULL, NULL));
Expand Down
23 changes: 22 additions & 1 deletion main/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,22 @@ void usb_init() {

#include "hal/usb_serial_jtag_ll.h"
#include "soc/periph_defs.h"
#include "esp_pm.h"

static void check_usb(void) {
static bool usb_detected;
static esp_pm_lock_handle_t pwr_handle;

if (usb_detected)
return;
if (USB_SERIAL_JTAG.fram_num.sof_frame_index) {
usb_detected = true;
LOG("detected USB connection; preventing sleep");
if (!pwr_handle)
esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "USB", &pwr_handle);
esp_pm_lock_acquire(pwr_handle);
}
}

static void fill_buffer(void) {
uint8_t buf[64];
Expand All @@ -138,6 +154,7 @@ static void fill_buffer(void) {
}

void jd_usb_pull_ready(void) {
check_usb();
target_disable_irq();
if (usb_serial_jtag_ll_txfifo_writable())
fill_buffer();
Expand All @@ -159,8 +176,10 @@ static void usb_serial_jtag_isr_handler(void *arg) {
int r = usb_serial_jtag_ll_read_rxfifo(buf, sizeof(buf));
usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT);
usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT);
if (r)
if (r) {
pwr_enter_no_sleep();
jd_usb_push(buf, r);
}
}
}

Expand All @@ -171,6 +190,8 @@ void usb_pre_init() {

void usb_init() {
LOG("init");
check_usb();

usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY |
USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT);
usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY |
Expand Down