Skip to content

Commit d948e65

Browse files
committed
Changes to handle Ctrl-C during sleep
1 parent c2aa54a commit d948e65

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

ports/esp32s2/supervisor/port.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "supervisor/port.h"
3131
#include "boards/board.h"
3232
#include "modules/module.h"
33+
#include "py/runtime.h"
3334

3435
#include "freertos/FreeRTOS.h"
3536
#include "freertos/task.h"
@@ -54,6 +55,7 @@
5455

5556
uint32_t* heap;
5657
uint32_t heap_size;
58+
extern TaskHandle_t xTaskToNotify;
5759

5860
STATIC esp_timer_handle_t _tick_timer;
5961

@@ -188,34 +190,28 @@ void port_disable_tick(void) {
188190
esp_timer_stop(_tick_timer);
189191
}
190192

191-
TickType_t sleep_time_set;
192193
TickType_t sleep_time_duration;
194+
uint32_t NotifyValue = 0;
195+
BaseType_t notify_wait = 0;
196+
193197
void port_interrupt_after_ticks(uint32_t ticks) {
194-
sleep_time_set = xTaskGetTickCount();
195-
sleep_time_duration = ticks / portTICK_PERIOD_MS;
196-
// esp_sleep_enable_timer_wakeup(uint64_t time_in_us)
198+
sleep_time_duration = (ticks * 100)/1024;
199+
xTaskToNotify = xTaskGetCurrentTaskHandle();
197200
}
198201

199202
void port_sleep_until_interrupt(void) {
200-
// FreeRTOS delay here maybe.
201-
// Light sleep shuts down BLE and wifi.
202-
// esp_light_sleep_start()
203+
203204
if (sleep_time_duration == 0) {
204205
return;
205206
}
206-
// Need to run in a loop in order to check if CTRL-C was received
207-
TickType_t start_ticks = 0;
208-
while (sleep_time_duration > start_ticks ) {
209-
vTaskDelayUntil(&sleep_time_set, 1);
210-
if ( mp_hal_is_interrupted() ) {
211-
mp_handle_pending();
212-
}
213-
start_ticks = start_ticks + 1;
214-
}
215-
207+
notify_wait = xTaskNotifyWait(0x01,0x01,&NotifyValue,
208+
sleep_time_duration );
209+
if (NotifyValue == 1) {
210+
xTaskToNotify = NULL;
211+
mp_handle_pending();
212+
}
216213
}
217214

218-
219215
// Wrap main in app_main that the IDF expects.
220216
extern void main(void);
221217
void app_main(void) {

ports/esp32s2/supervisor/usb.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
StackType_t usb_device_stack[USBD_STACK_SIZE];
5353
StaticTask_t usb_device_taskdef;
5454

55+
TaskHandle_t xTaskToNotify = NULL;
56+
5557
// USB Device Driver task
5658
// This top level thread process all usb events and invoke callbacks
5759
void usb_device_task(void* param)
@@ -114,3 +116,21 @@ void init_usb_hardware(void) {
114116
usb_device_stack,
115117
&usb_device_taskdef);
116118
}
119+
/**
120+
* Callback invoked when received an "wanted" char.
121+
* @param itf Interface index (for multiple cdc interfaces)
122+
* @param wanted_char The wanted char (set previously)
123+
*/
124+
void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char)
125+
{
126+
(void) itf; // not used
127+
// Workaround for using lib/utils/interrupt_char.c
128+
// Compare mp_interrupt_char with wanted_char and ignore if not matched
129+
if (mp_interrupt_char == wanted_char) {
130+
tud_cdc_read_flush(); // flush read fifo
131+
mp_keyboard_interrupt();
132+
if (xTaskToNotify != NULL) {
133+
xTaskNotifyGive(xTaskToNotify);
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)