Skip to content

Initial alarm and sleep PR: time alarms with light and deep sleep; PinAlarms not yet implemented #3767

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

Merged
merged 41 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
90b9ec6
Initial Sleep Support
microdev1 Sep 18, 2020
3a30887
Update soft reboot message
microdev1 Sep 18, 2020
e310b87
Get io wake working
microdev1 Sep 19, 2020
05a3f20
Add function to get time elapsed during sleep
microdev1 Sep 22, 2020
21ba61a
Add function to disable alarm
microdev1 Sep 22, 2020
e5ff55b
Renamed alarm modules
microdev1 Sep 23, 2020
4d8ffdc
restructure alarm modules
microdev1 Sep 24, 2020
da44972
Fix build error
microdev1 Sep 24, 2020
59df1a1
Add alarm_touch module
microdev1 Sep 24, 2020
e359389
Add description of alarm modules
microdev1 Sep 24, 2020
930cf14
Add check for invalid io, function to disable all alarms
microdev1 Sep 26, 2020
0e444f0
Implement sleep on code.py exit
microdev1 Sep 30, 2020
354536c
Update translation
microdev1 Oct 7, 2020
1196d4b
move to new module
tannewt Oct 14, 2020
85dadf3
More API changes
tannewt Oct 15, 2020
9a4efed
Start tweaking the workflow to sleep
tannewt Oct 28, 2020
bb77f1d
wip: initial code changes, starting from @tannewt's sleepio branch
dhalbert Nov 16, 2020
ffff02c
Merge remote-tracking branch 'adafruit/main' into sleep
dhalbert Nov 16, 2020
682054a
WIP: redo API; not compiled yet
dhalbert Nov 19, 2020
5bb3c32
merge from main
dhalbert Nov 19, 2020
649c930
wip
dhalbert Nov 19, 2020
cd436ba
Merge remote-tracking branch 'adafruit/main' into sleep
dhalbert Nov 19, 2020
39e1f52
wip; not compiling yet
dhalbert Nov 19, 2020
e4c6699
compiles
dhalbert Nov 21, 2020
75559f3
wip: ResetReason to microcontroller.cpu
dhalbert Nov 22, 2020
a0f1ec3
wip
dhalbert Nov 23, 2020
25591a3
Merge branch 'esp32s2-common-hal-mcu-delay-us' into sleep
dhalbert Nov 23, 2020
3abee9b
compiles; maybe ready to test, or almost
dhalbert Nov 23, 2020
7a45afc
working, but need to avoid deep sleeping too fast before USB ready
dhalbert Nov 24, 2020
f868cc5
some API renaming and bug fixes; fix docs
dhalbert Nov 25, 2020
9dbea36
changed alarm.time API
dhalbert Nov 25, 2020
ef0830b
merge from upstream + wip
dhalbert Nov 25, 2020
104a089
deep sleep working; deep sleep delay when connected
dhalbert Nov 27, 2020
e308a9e
working! PinAlarm not implemented yet.
dhalbert Nov 27, 2020
596e0e4
merge from upstream
dhalbert Nov 27, 2020
f96475c
update Requests; rolled back by accident
dhalbert Nov 27, 2020
65e2fe4
fix stub problems; touch up doc
dhalbert Nov 28, 2020
2830cc9
make translate
dhalbert Nov 28, 2020
28d9e91
Disable complex arithmetic on SAMD21 builds to make space
dhalbert Nov 28, 2020
8b7c23c
address review comments
dhalbert Dec 2, 2020
72fa7d8
fix doc errors
dhalbert Dec 2, 2020
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
44 changes: 42 additions & 2 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-11-24 15:40-0500\n"
"POT-Creation-Date: 2020-11-27 23:57-0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -844,6 +844,10 @@ msgstr ""
msgid "Expected an Address"
msgstr ""

#: shared-bindings/alarm/__init__.c
msgid "Expected an alarm"
msgstr ""

#: shared-module/_pixelbuf/PixelBuf.c
#, c-format
msgid "Expected tuple of length %d, got %d"
Expand Down Expand Up @@ -989,6 +993,10 @@ msgstr ""
msgid "I2SOut not available"
msgstr ""

#: ports/esp32s2/common-hal/alarm/pin/__init__.c
msgid "IOs 0, 2 & 4 do not support internal pullup in sleep"
msgstr ""

#: shared-bindings/aesio/aes.c
#, c-format
msgid "IV must be %d bytes long"
Expand Down Expand Up @@ -1440,6 +1448,10 @@ msgid ""
"%d bpp given"
msgstr ""

#: ports/esp32s2/common-hal/alarm/__init__.c
msgid "Only one alarm.time alarm can be set."
msgstr ""

#: shared-module/displayio/ColorConverter.c
msgid "Only one color can be transparent at a time"
msgstr ""
Expand Down Expand Up @@ -1497,6 +1509,10 @@ msgstr ""
msgid "Pin number already reserved by EXTI"
msgstr ""

#: ports/esp32s2/common-hal/alarm/__init__.c
msgid "PinAlarm not yet implemented"
msgstr ""

#: shared-bindings/rgbmatrix/RGBMatrix.c
#, c-format
msgid ""
Expand Down Expand Up @@ -1558,7 +1574,7 @@ msgstr ""
msgid "RTC calibration is not supported on this board"
msgstr ""

#: shared-bindings/time/__init__.c
#: shared-bindings/alarm/time/TimeAlarm.c shared-bindings/time/__init__.c
msgid "RTC is not supported on this board"
msgstr ""

Expand Down Expand Up @@ -1704,6 +1720,10 @@ msgstr ""
msgid "Supply at least one UART pin"
msgstr ""

#: shared-bindings/alarm/time/TimeAlarm.c
msgid "Supply one of monotonic_time or epoch_time"
msgstr ""

#: shared-bindings/gnss/GNSS.c
msgid "System entry must be gnss.SatelliteSystem"
msgstr ""
Expand Down Expand Up @@ -1767,6 +1787,10 @@ msgstr ""
msgid "Tile width must exactly divide bitmap width"
msgstr ""

#: shared-bindings/alarm/time/TimeAlarm.c
msgid "Time is in the past."
msgstr ""

#: ports/nrf/common-hal/_bleio/Adapter.c
#, c-format
msgid "Timeout is too long: Maximum timeout length is %d seconds"
Expand Down Expand Up @@ -2481,6 +2505,10 @@ msgstr ""
msgid "end_x should be an int"
msgstr ""

#: shared-bindings/alarm/time/TimeAlarm.c
msgid "epoch_time not supported on this board"
msgstr ""

#: ports/nrf/common-hal/busio/UART.c
#, c-format
msgid "error = 0x%08lX"
Expand Down Expand Up @@ -2850,6 +2878,10 @@ msgstr ""
msgid "invalid syntax for number"
msgstr ""

#: ports/esp32s2/common-hal/alarm/pin/__init__.c
msgid "io must be rtc io"
msgstr ""

#: py/objtype.c
msgid "issubclass() arg 1 must be a class"
msgstr ""
Expand Down Expand Up @@ -3537,6 +3569,10 @@ msgstr ""
msgid "trapz is defined for 1D arrays of equal length"
msgstr ""

#: ports/esp32s2/common-hal/alarm/pin/__init__.c
msgid "trigger level must be 0 or 1"
msgstr ""

#: extmod/ulab/code/linalg/linalg.c
msgid "tuple index out of range"
msgstr ""
Expand Down Expand Up @@ -3679,6 +3715,10 @@ msgstr ""
msgid "vectors must have same lengths"
msgstr ""

#: ports/esp32s2/common-hal/alarm/pin/__init__.c
msgid "wakeup conflict"
msgstr ""

#: ports/esp32s2/common-hal/watchdog/WatchDogTimer.c
msgid "watchdog not initialized"
msgstr ""
Expand Down
88 changes: 49 additions & 39 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,29 @@
#include "mpconfigboard.h"
#include "supervisor/background_callback.h"
#include "supervisor/cpu.h"
#include "supervisor/filesystem.h"
#include "supervisor/memory.h"
#include "supervisor/port.h"
#include "supervisor/filesystem.h"
#include "supervisor/serial.h"
#include "supervisor/shared/autoreload.h"
#include "supervisor/shared/translate.h"
#include "supervisor/shared/rgb_led_status.h"
#include "supervisor/shared/safe_mode.h"
#include "supervisor/shared/status_leds.h"
#include "supervisor/shared/stack.h"
#include "supervisor/serial.h"
#include "supervisor/shared/status_leds.h"
#include "supervisor/shared/translate.h"
#include "supervisor/shared/workflow.h"
#include "supervisor/usb.h"

#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Processor.h"
#include "shared-bindings/supervisor/Runtime.h"

#include "boards/board.h"

#if CIRCUITPY_ALARM
#include "shared-bindings/alarm/__init__.h"
#endif

#if CIRCUITPY_DISPLAYIO
#include "shared-module/displayio/__init__.h"
#endif
Expand All @@ -85,26 +95,6 @@
#include "common-hal/canio/CAN.h"
#endif

void do_str(const char *src, mp_parse_input_kind_t input_kind) {
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
if (lex == NULL) {
//printf("MemoryError: lexer could not allocate memory\n");
return;
}

nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
qstr source_name = lex->source_name;
mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
mp_obj_t module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, true);
mp_call_function_0(module_fun);
nlr_pop();
} else {
// uncaught exception
mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
}
}

#if MICROPY_ENABLE_PYSTACK
static size_t PLACE_IN_DTCM_BSS(_pystack[CIRCUITPY_PYSTACK_SIZE / sizeof(size_t)]);
#endif
Expand All @@ -115,9 +105,13 @@ static void reset_devices(void) {
#endif
}

void start_mp(supervisor_allocation* heap) {
STATIC void start_mp(supervisor_allocation* heap) {
reset_status_led();
autoreload_stop();
supervisor_workflow_reset();
#if CIRCUITPY_ALARM
alarm_reset();
#endif

// Stack limit should be less than real stack size, so we have a chance
// to recover from limit hit. (Limit is measured in bytes.)
Expand Down Expand Up @@ -166,7 +160,7 @@ void start_mp(supervisor_allocation* heap) {
#endif
}

void stop_mp(void) {
STATIC void stop_mp(void) {
#if CIRCUITPY_NETWORK
network_module_deinit();
#endif
Expand All @@ -191,7 +185,7 @@ void stop_mp(void) {

// Look for the first file that exists in the list of filenames, using mp_import_stat().
// Return its index. If no file found, return -1.
const char* first_existing_file_in_list(const char * const * filenames) {
STATIC const char* first_existing_file_in_list(const char * const * filenames) {
for (int i = 0; filenames[i] != (char*)""; i++) {
mp_import_stat_t stat = mp_import_stat(filenames[i]);
if (stat == MP_IMPORT_STAT_FILE) {
Expand All @@ -201,7 +195,7 @@ const char* first_existing_file_in_list(const char * const * filenames) {
return NULL;
}

bool maybe_run_list(const char * const * filenames, pyexec_result_t* exec_result) {
STATIC bool maybe_run_list(const char * const * filenames, pyexec_result_t* exec_result) {
const char* filename = first_existing_file_in_list(filenames);
if (filename == NULL) {
return false;
Expand All @@ -215,7 +209,7 @@ bool maybe_run_list(const char * const * filenames, pyexec_result_t* exec_result
return true;
}

void cleanup_after_vm(supervisor_allocation* heap) {
STATIC void cleanup_after_vm(supervisor_allocation* heap) {
// Reset port-independent devices, like CIRCUITPY_BLEIO_HCI.
reset_devices();
// Turn off the display and flush the fileystem before the heap disappears.
Expand Down Expand Up @@ -244,7 +238,7 @@ void cleanup_after_vm(supervisor_allocation* heap) {
reset_status_led();
}

void print_code_py_status_message(safe_mode_t safe_mode) {
STATIC void print_code_py_status_message(safe_mode_t safe_mode) {
if (autoreload_is_enabled()) {
serial_write_compressed(translate("Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.\n"));
} else {
Expand All @@ -256,7 +250,7 @@ void print_code_py_status_message(safe_mode_t safe_mode) {
}
}

bool run_code_py(safe_mode_t safe_mode) {
STATIC bool run_code_py(safe_mode_t safe_mode) {
bool serial_connected_at_start = serial_connected();
#if CIRCUITPY_AUTORELOAD_DELAY_MS > 0
if (serial_connected_at_start) {
Expand All @@ -276,10 +270,12 @@ bool run_code_py(safe_mode_t safe_mode) {
if (safe_mode == NO_SAFE_MODE) {
new_status_color(MAIN_RUNNING);

static const char * const supported_filenames[] = STRING_LIST("code.txt", "code.py", "main.py", "main.txt");
static const char * const supported_filenames[] = STRING_LIST(
"code.txt", "code.py", "main.py", "main.txt");
#if CIRCUITPY_FULL_BUILD
static const char * const double_extension_filenames[] = STRING_LIST("code.txt.py", "code.py.txt", "code.txt.txt","code.py.py",
"main.txt.py", "main.py.txt", "main.txt.txt","main.py.py");
static const char * const double_extension_filenames[] = STRING_LIST(
"code.txt.py", "code.py.txt", "code.txt.txt","code.py.py",
"main.txt.py", "main.py.txt", "main.txt.txt","main.py.py");
#endif

stack_resize();
Expand All @@ -302,6 +298,8 @@ bool run_code_py(safe_mode_t safe_mode) {
}
}

// Program has finished running.

// Display a different completion message if the user has no USB attached (cannot save files)
if (!serial_connected_at_start) {
serial_write_compressed(translate("\nCode done running. Waiting for reload.\n"));
Expand All @@ -311,24 +309,32 @@ bool run_code_py(safe_mode_t safe_mode) {
#if CIRCUITPY_DISPLAYIO
bool refreshed_epaper_display = false;
#endif

rgb_status_animation_t animation;
prep_rgb_status_animation(&result, found_main, safe_mode, &animation);
while (true) {

RUN_BACKGROUND_TASKS;
if (reload_requested) {
supervisor_set_run_reason(RUN_REASON_AUTO_RELOAD);
reload_requested = false;
return true;
}

if (serial_connected() && serial_bytes_available()) {
// Skip REPL if reload was requested.
return (serial_read() == CHAR_CTRL_D);
bool ctrl_d = serial_read() == CHAR_CTRL_D;
if (ctrl_d) {
supervisor_set_run_reason(RUN_REASON_REPL_RELOAD);
}
return (ctrl_d);
}

if (!serial_connected_before_animation && serial_connected()) {
if (!serial_connected_at_start) {
print_code_py_status_message(safe_mode);
}

print_safe_mode_message(safe_mode);
serial_write("\n");
serial_write_compressed(translate("Press any key to enter the REPL. Use CTRL-D to reload."));
Expand All @@ -351,7 +357,7 @@ bool run_code_py(safe_mode_t safe_mode) {

FIL* boot_output_file;

void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
// If not in safe mode, run boot before initing USB and capture output in a
// file.
if (filesystem_present() && safe_mode == NO_SAFE_MODE && MP_STATE_VM(vfs_mount_table) != NULL) {
Expand Down Expand Up @@ -392,8 +398,9 @@ void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
if (!skip_boot_output) {
// Wait 1.5 seconds before opening CIRCUITPY_BOOT_OUTPUT_FILE for write,
// in case power is momentary or will fail shortly due to, say a low, battery.
mp_hal_delay_ms(1500);

if (common_hal_mcu_processor_get_reset_reason() == RESET_REASON_POWER_ON) {
mp_hal_delay_ms(1500);
}
// USB isn't up, so we can write the file.
filesystem_set_internal_writable_by_usb(false);
f_open(fs, boot_output_file, CIRCUITPY_BOOT_OUTPUT_FILE, FA_WRITE | FA_CREATE_ALWAYS);
Expand Down Expand Up @@ -429,7 +436,7 @@ void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
}
}

int run_repl(void) {
STATIC int run_repl(void) {
int exit_code = PYEXEC_FORCED_EXIT;
stack_resize();
filesystem_flush();
Expand Down Expand Up @@ -482,6 +489,9 @@ int __attribute__((used)) main(void) {
reset_devices();
reset_board();

// This is first time we are running CircuitPython after a reset or power-up.
supervisor_set_run_reason(RUN_REASON_STARTUP);

// If not in safe mode turn on autoreload by default but before boot.py in case it wants to change it.
if (safe_mode == NO_SAFE_MODE) {
autoreload_enable();
Expand Down
5 changes: 5 additions & 0 deletions ports/atmel-samd/common-hal/microcontroller/Processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@

#include "py/mphal.h"
#include "common-hal/microcontroller/Processor.h"
#include "shared-bindings/microcontroller/ResetReason.h"

#include "samd/adc.h"

Expand Down Expand Up @@ -349,3 +350,7 @@ void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {
}
}
}

mcu_reset_reason_t common_hal_mcu_processor_get_reset_reason(void) {
return RESET_REASON_POWER_ON;
}
1 change: 1 addition & 0 deletions ports/atmel-samd/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#define CIRCUITPY_MCU_FAMILY samd21
#define MICROPY_PY_SYS_PLATFORM "Atmel SAMD21"
#define SPI_FLASH_MAX_BAUDRATE 8000000
#define MICROPY_PY_BUILTINS_COMPLEX (0)
#define MICROPY_PY_BUILTINS_NOTIMPLEMENTED (0)
#define MICROPY_PY_FUNCTION_ATTRS (0)
// MICROPY_PY_UJSON depends on MICROPY_PY_IO
Expand Down
5 changes: 5 additions & 0 deletions ports/cxd56/common-hal/microcontroller/Processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
// For NAN: remove when not needed.
#include <math.h>
#include "py/mphal.h"
#include "shared-bindings/microcontroller/ResetReason.h"

uint32_t common_hal_mcu_processor_get_frequency(void) {
return cxd56_get_cpu_baseclk();
Expand All @@ -47,3 +48,7 @@ float common_hal_mcu_processor_get_voltage(void) {
void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {
boardctl(BOARDIOC_UNIQUEID, (uintptr_t) raw_id);
}

mcu_reset_reason_t common_hal_mcu_processor_get_reset_reason(void) {
return RESET_REASON_POWER_ON;
}
Loading