summaryrefslogtreecommitdiff
path: root/shared-bindings/supervisor
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2020-11-23 22:44:53 -0500
committerDan Halbert <halbert@halwitz.org>2020-11-23 22:44:53 -0500
commit7a45afc54919162df342fab421ed113ff1771d01 (patch)
tree4d351d45751f40c096c89d0ea773a09a279d6b52 /shared-bindings/supervisor
parent3abee9b2563f0203f3d9521631499008708bb407 (diff)
working, but need to avoid deep sleeping too fast before USB ready
Diffstat (limited to 'shared-bindings/supervisor')
-rw-r--r--shared-bindings/supervisor/RunReason.c4
-rwxr-xr-xshared-bindings/supervisor/Runtime.c3
-rw-r--r--shared-bindings/supervisor/__init__.c51
3 files changed, 53 insertions, 5 deletions
diff --git a/shared-bindings/supervisor/RunReason.c b/shared-bindings/supervisor/RunReason.c
index 7e7a74d2f..73f62fed6 100644
--- a/shared-bindings/supervisor/RunReason.c
+++ b/shared-bindings/supervisor/RunReason.c
@@ -29,7 +29,7 @@
#include "shared-bindings/supervisor/RunReason.h"
MAKE_ENUM_VALUE(supervisor_run_reason_type, run_reason, STARTUP, RUN_REASON_STARTUP);
-MAKE_ENUM_VALUE(supervisor_run_reason_type, run_reason, AUTORELOAD, RUN_REASON_AUTO_RELOAD);
+MAKE_ENUM_VALUE(supervisor_run_reason_type, run_reason, AUTO_RELOAD, RUN_REASON_AUTO_RELOAD);
MAKE_ENUM_VALUE(supervisor_run_reason_type, run_reason, SUPERVISOR_RELOAD, RUN_REASON_SUPERVISOR_RELOAD);
MAKE_ENUM_VALUE(supervisor_run_reason_type, run_reason, REPL_RELOAD, RUN_REASON_REPL_RELOAD);
@@ -49,7 +49,7 @@ MAKE_ENUM_VALUE(supervisor_run_reason_type, run_reason, REPL_RELOAD, RUN_REASON_
//| REPL_RELOAD: object
//| """CircuitPython started due to the user typing CTRL-D in the REPL."""
//|
-MAKE_ENUM_MAP(run_reason) {
+MAKE_ENUM_MAP(supervisor_run_reason) {
MAKE_ENUM_MAP_ENTRY(run_reason, STARTUP),
MAKE_ENUM_MAP_ENTRY(run_reason, AUTO_RELOAD),
MAKE_ENUM_MAP_ENTRY(run_reason, SUPERVISOR_RELOAD),
diff --git a/shared-bindings/supervisor/Runtime.c b/shared-bindings/supervisor/Runtime.c
index 67193e051..1a283b35c 100755
--- a/shared-bindings/supervisor/Runtime.c
+++ b/shared-bindings/supervisor/Runtime.c
@@ -25,8 +25,11 @@
*/
#include <stdbool.h>
+#include "py/obj.h"
#include "py/enum.h"
+#include "py/runtime.h"
#include "py/objproperty.h"
+
#include "shared-bindings/supervisor/RunReason.h"
#include "shared-bindings/supervisor/Runtime.h"
diff --git a/shared-bindings/supervisor/__init__.c b/shared-bindings/supervisor/__init__.c
index 4d2d6db30..c13b19e48 100644
--- a/shared-bindings/supervisor/__init__.c
+++ b/shared-bindings/supervisor/__init__.c
@@ -32,7 +32,9 @@
#include "supervisor/shared/rgb_led_status.h"
#include "supervisor/shared/stack.h"
#include "supervisor/shared/translate.h"
+#include "supervisor/shared/workflow.h"
+#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/supervisor/__init__.h"
#include "shared-bindings/supervisor/Runtime.h"
@@ -45,6 +47,47 @@
//| This object is the sole instance of `supervisor.Runtime`."""
//|
+//| def allow_deep_sleep(*, when_connected: bool = False, on_error: bool = False):
+//| """Configure when CircuitPython can go into deep sleep. Deep sleep can occur
+//| after a program has finished running or when `supervisor.deep_sleep_now()` is called.
+//|
+//| :param bool when_connected: If ``True``, CircuitPython will go into deep sleep
+//| when a program finishes, even if it is connected to a host computer over USB or other means.
+//| It will disconnect from the host before sleeping.
+//| If ``False``, deep sleep will not be entered if connected.
+//| :param bool on_error: If ``True``, deep sleep will be entered if even a program
+//| terminated due to an exception or fatal error. If ``False``, an error will cause
+//| CircuitPython to stay awake, flashing error codes on the status RGB LED, if available.
+//| ...
+//|
+STATIC mp_obj_t supervisor_allow_deep_sleep(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ enum { ARG_when_connected, ARG_on_error };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_when_connected, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
+ { MP_QSTR_on_error, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
+ };
+
+ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+ mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+ supervisor_workflow_set_allow_deep_sleep_when_connected(args[ARG_when_connected].u_bool);
+ supervisor_workflow_set_allow_deep_sleep_on_error(args[ARG_on_error].u_bool);
+
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_KW(supervisor_allow_deep_sleep_obj, 0, supervisor_allow_deep_sleep);
+
+//| def deep_sleep(): -> None
+//| """Go into deep sleep mode immediately, if not connected to a host computer.
+//| But if connected and `supervisor.runtime.allow_deep_sleep(when_connected=true)`
+//| has not been called, simply restart.
+//|
+
+STATIC mp_obj_t supervisor_deep_sleep(void) {
+ common_hal_mcu_deep_sleep();
+}
+MP_DEFINE_CONST_FUN_OBJ_0(supervisor_deep_sleep_obj, supervisor_deep_sleep);
+
//| def enable_autoreload() -> None:
//| """Enable autoreload based on USB file write activity."""
//| ...
@@ -112,9 +155,11 @@ MP_DEFINE_CONST_FUN_OBJ_1(supervisor_set_next_stack_limit_obj, supervisor_set_ne
STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_supervisor) },
- { MP_OBJ_NEW_QSTR(MP_QSTR_enable_autoreload), MP_ROM_PTR(&supervisor_enable_autoreload_obj) },
- { MP_OBJ_NEW_QSTR(MP_QSTR_disable_autoreload), MP_ROM_PTR(&supervisor_disable_autoreload_obj) },
- { MP_OBJ_NEW_QSTR(MP_QSTR_set_rgb_status_brightness), MP_ROM_PTR(&supervisor_set_rgb_status_brightness_obj) },
+ { MP_ROM_QSTR(MP_QSTR_allow_deep_sleep), MP_ROM_PTR(&supervisor_allow_deep_sleep_obj) },
+ { MP_ROM_QSTR(MP_QSTR_deep_sleep), MP_ROM_PTR(&supervisor_deep_sleep_obj) },
+ { MP_ROM_QSTR(MP_QSTR_enable_autoreload), MP_ROM_PTR(&supervisor_enable_autoreload_obj) },
+ { MP_ROM_QSTR(MP_QSTR_disable_autoreload), MP_ROM_PTR(&supervisor_disable_autoreload_obj) },
+ { MP_ROM_QSTR(MP_QSTR_set_rgb_status_brightness), MP_ROM_PTR(&supervisor_set_rgb_status_brightness_obj) },
{ MP_ROM_QSTR(MP_QSTR_runtime), MP_ROM_PTR(&common_hal_supervisor_runtime_obj) },
{ MP_ROM_QSTR(MP_QSTR_reload), MP_ROM_PTR(&supervisor_reload_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_next_stack_limit), MP_ROM_PTR(&supervisor_set_next_stack_limit_obj) },