Skip to content

Commit ddb3590

Browse files
authored
Merge pull request #3647 from DavePutz/issue3579
Issue3579 - Check for CTRL-C During sleep on esp32s2
2 parents 75a977f + fe7ed99 commit ddb3590

File tree

3 files changed

+71
-9
lines changed

3 files changed

+71
-9
lines changed

ports/esp32s2/supervisor/esp_port.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ESP32S2_SUPERVISOR_PORT_H
28+
#define MICROPY_INCLUDED_ESP32S2_SUPERVISOR_PORT_H
29+
30+
#include "freertos/FreeRTOS.h"
31+
#include "freertos/task.h"
32+
33+
extern TaskHandle_t sleeping_circuitpython_task;
34+
35+
#endif // MICROPY_INCLUDED_ESP32S2_SUPERVISOR_PORT_H

ports/esp32s2/supervisor/port.c

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

3436
#include "freertos/FreeRTOS.h"
3537
#include "freertos/task.h"
@@ -199,25 +201,28 @@ void port_disable_tick(void) {
199201
esp_timer_stop(_tick_timer);
200202
}
201203

202-
TickType_t sleep_time_set;
203204
TickType_t sleep_time_duration;
205+
204206
void port_interrupt_after_ticks(uint32_t ticks) {
205-
sleep_time_set = xTaskGetTickCount();
206-
sleep_time_duration = ticks / portTICK_PERIOD_MS;
207-
// esp_sleep_enable_timer_wakeup(uint64_t time_in_us)
207+
sleep_time_duration = (ticks * 100)/1024;
208+
sleeping_circuitpython_task = xTaskGetCurrentTaskHandle();
208209
}
209210

210211
void port_sleep_until_interrupt(void) {
211-
// FreeRTOS delay here maybe.
212-
// Light sleep shuts down BLE and wifi.
213-
// esp_light_sleep_start()
212+
213+
uint32_t NotifyValue = 0;
214+
214215
if (sleep_time_duration == 0) {
215216
return;
216217
}
217-
vTaskDelayUntil(&sleep_time_set, sleep_time_duration);
218+
xTaskNotifyWait(0x01,0x01,&NotifyValue,
219+
sleep_time_duration );
220+
if (NotifyValue == 1) {
221+
sleeping_circuitpython_task = NULL;
222+
mp_handle_pending();
223+
}
218224
}
219225

220-
221226
// Wrap main in app_main that the IDF expects.
222227
extern void main(void);
223228
void app_main(void) {

ports/esp32s2/supervisor/usb.c

Lines changed: 22 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 sleeping_circuitpython_task = 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,23 @@ 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+
// CircuitPython's VM is run in a separate FreeRTOS task from TinyUSB.
133+
// So, we must notify the other task when a CTRL-C is received.
134+
if (sleeping_circuitpython_task != NULL) {
135+
xTaskNotifyGive(sleeping_circuitpython_task);
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)