Skip to content

Commit

Permalink
Merge pull request #3647 from DavePutz/issue3579
Browse files Browse the repository at this point in the history
Issue3579 - Check for CTRL-C During sleep on esp32s2
  • Loading branch information
tannewt committed Nov 10, 2020
2 parents 75a977f + fe7ed99 commit ddb3590
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
35 changes: 35 additions & 0 deletions ports/esp32s2/supervisor/esp_port.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef MICROPY_INCLUDED_ESP32S2_SUPERVISOR_PORT_H
#define MICROPY_INCLUDED_ESP32S2_SUPERVISOR_PORT_H

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

extern TaskHandle_t sleeping_circuitpython_task;

#endif // MICROPY_INCLUDED_ESP32S2_SUPERVISOR_PORT_H
23 changes: 14 additions & 9 deletions ports/esp32s2/supervisor/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "supervisor/port.h"
#include "boards/board.h"
#include "modules/module.h"
#include "py/runtime.h"
#include "supervisor/esp_port.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
Expand Down Expand Up @@ -199,25 +201,28 @@ void port_disable_tick(void) {
esp_timer_stop(_tick_timer);
}

TickType_t sleep_time_set;
TickType_t sleep_time_duration;

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;
sleeping_circuitpython_task = xTaskGetCurrentTaskHandle();
}

void port_sleep_until_interrupt(void) {
// FreeRTOS delay here maybe.
// Light sleep shuts down BLE and wifi.
// esp_light_sleep_start()

uint32_t NotifyValue = 0;

if (sleep_time_duration == 0) {
return;
}
vTaskDelayUntil(&sleep_time_set, sleep_time_duration);
xTaskNotifyWait(0x01,0x01,&NotifyValue,
sleep_time_duration );
if (NotifyValue == 1) {
sleeping_circuitpython_task = NULL;
mp_handle_pending();
}
}


// Wrap main in app_main that the IDF expects.
extern void main(void);
void app_main(void) {
Expand Down
22 changes: 22 additions & 0 deletions ports/esp32s2/supervisor/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
StackType_t usb_device_stack[USBD_STACK_SIZE];
StaticTask_t usb_device_taskdef;

TaskHandle_t sleeping_circuitpython_task = NULL;

// USB Device Driver task
// This top level thread process all usb events and invoke callbacks
void usb_device_task(void* param)
Expand Down Expand Up @@ -114,3 +116,23 @@ 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();
// CircuitPython's VM is run in a separate FreeRTOS task from TinyUSB.
// So, we must notify the other task when a CTRL-C is received.
if (sleeping_circuitpython_task != NULL) {
xTaskNotifyGive(sleeping_circuitpython_task);
}
}
}

0 comments on commit ddb3590

Please sign in to comment.