Changes to handle Ctrl-C during sleep

This commit is contained in:
root 2020-11-05 21:27:21 -06:00
parent c2aa54ae66
commit d948e6570f
2 changed files with 34 additions and 18 deletions

View File

@ -30,6 +30,7 @@
#include "supervisor/port.h"
#include "boards/board.h"
#include "modules/module.h"
#include "py/runtime.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
@ -54,6 +55,7 @@
uint32_t* heap;
uint32_t heap_size;
extern TaskHandle_t xTaskToNotify;
STATIC esp_timer_handle_t _tick_timer;
@ -188,34 +190,28 @@ void port_disable_tick(void) {
esp_timer_stop(_tick_timer);
}
TickType_t sleep_time_set;
TickType_t sleep_time_duration;
uint32_t NotifyValue = 0;
BaseType_t notify_wait = 0;
void port_interrupt_after_ticks(uint32_t ticks) {
sleep_time_set = xTaskGetTickCount();
sleep_time_duration = ticks / portTICK_PERIOD_MS;
// esp_sleep_enable_timer_wakeup(uint64_t time_in_us)
sleep_time_duration = (ticks * 100)/1024;
xTaskToNotify = xTaskGetCurrentTaskHandle();
}
void port_sleep_until_interrupt(void) {
// FreeRTOS delay here maybe.
// Light sleep shuts down BLE and wifi.
// esp_light_sleep_start()
if (sleep_time_duration == 0) {
return;
}
// Need to run in a loop in order to check if CTRL-C was received
TickType_t start_ticks = 0;
while (sleep_time_duration > start_ticks ) {
vTaskDelayUntil(&sleep_time_set, 1);
if ( mp_hal_is_interrupted() ) {
notify_wait = xTaskNotifyWait(0x01,0x01,&NotifyValue,
sleep_time_duration );
if (NotifyValue == 1) {
xTaskToNotify = NULL;
mp_handle_pending();
}
start_ticks = start_ticks + 1;
}
}
// Wrap main in app_main that the IDF expects.
extern void main(void);
void app_main(void) {

View File

@ -52,6 +52,8 @@
StackType_t usb_device_stack[USBD_STACK_SIZE];
StaticTask_t usb_device_taskdef;
TaskHandle_t xTaskToNotify = NULL;
// USB Device Driver task
// This top level thread process all usb events and invoke callbacks
void usb_device_task(void* param)
@ -114,3 +116,21 @@ void init_usb_hardware(void) {
usb_device_stack,
&usb_device_taskdef);
}
/**
* Callback invoked when received an "wanted" char.
* @param itf Interface index (for multiple cdc interfaces)
* @param wanted_char The wanted char (set previously)
*/
void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char)
{
(void) itf; // not used
// Workaround for using lib/utils/interrupt_char.c
// Compare mp_interrupt_char with wanted_char and ignore if not matched
if (mp_interrupt_char == wanted_char) {
tud_cdc_read_flush(); // flush read fifo
mp_keyboard_interrupt();
if (xTaskToNotify != NULL) {
xTaskNotifyGive(xTaskToNotify);
}
}
}