Skip to content

Commit f868cc5

Browse files
committed
some API renaming and bug fixes; fix docs
1 parent 7a45afc commit f868cc5

File tree

8 files changed

+73
-66
lines changed

8 files changed

+73
-66
lines changed

main.c

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
#endif
9999

100100
// How long to wait for host to enumerate (secs).
101-
#define CIRCUITPY_USB_ENUMERATION_DELAY 1
101+
#define CIRCUITPY_USB_ENUMERATION_DELAY 5
102102

103103
// How long to flash errors on the RGB status LED before going to sleep (secs)
104104
#define CIRCUITPY_FLASH_ERROR_PERIOD 10
@@ -319,26 +319,28 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
319319
bool ok = result.return_code != PYEXEC_EXCEPTION;
320320

321321
ESP_LOGI("main", "common_hal_alarm_enable_deep_sleep_alarms()");
322-
// Decide whether to deep sleep.
323322
#if CIRCUITPY_ALARM
324323
// Enable pin or time alarms before sleeping.
325324
common_hal_alarm_enable_deep_sleep_alarms();
326325
#endif
327326

328-
// Normally we won't deep sleep if there was an error or if we are connected to a host
329-
// but either of those can be enabled.
330-
// *********DON'T SLEEP IF USB HASN'T HAD TIME TO ENUMERATE.
331-
bool will_deep_sleep =
332-
(ok || supervisor_workflow_get_allow_deep_sleep_on_error()) &&
333-
(!supervisor_workflow_active() || supervisor_workflow_get_allow_deep_sleep_when_connected());
334-
335-
ESP_LOGI("main", "ok %d", will_deep_sleep);
336-
ESP_LOGI("main", "...on_error() %d", supervisor_workflow_get_allow_deep_sleep_on_error());
337-
ESP_LOGI("main", "supervisor_workflow_active() %d", supervisor_workflow_active());
338-
ESP_LOGI("main", "...when_connected() %d", supervisor_workflow_get_allow_deep_sleep_when_connected());
339-
will_deep_sleep = false;
327+
340328
prep_rgb_status_animation(&result, found_main, safe_mode, &animation);
341329
while (true) {
330+
// Normally we won't deep sleep if there was an error or if we are connected to a host
331+
// but either of those can be enabled.
332+
// It's ok to deep sleep if we're not connected to a host, but we need to make sure
333+
// we're giving enough time for USB enumeration to happen.
334+
bool deep_sleep_allowed =
335+
(ok || supervisor_workflow_get_allow_deep_sleep_on_error()) &&
336+
(!supervisor_workflow_active() || supervisor_workflow_get_allow_deep_sleep_when_connected()) &&
337+
(supervisor_ticks_ms64() > CIRCUITPY_USB_ENUMERATION_DELAY * 1024);
338+
339+
ESP_LOGI("main", "ok %d", deep_sleep_allowed);
340+
ESP_LOGI("main", "...on_error() %d", supervisor_workflow_get_allow_deep_sleep_on_error());
341+
ESP_LOGI("main", "supervisor_workflow_active() %d", supervisor_workflow_active());
342+
ESP_LOGI("main", "...when_connected() %d", supervisor_workflow_get_allow_deep_sleep_when_connected());
343+
ESP_LOGI("main", "supervisor_ticks_ms64() %lld", supervisor_ticks_ms64());
342344
RUN_BACKGROUND_TASKS;
343345
if (reload_requested) {
344346
supervisor_set_run_reason(RUN_REASON_AUTO_RELOAD);
@@ -360,7 +362,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
360362
print_code_py_status_message(safe_mode);
361363
}
362364
// We won't be going into the REPL if we're going to sleep.
363-
if (!will_deep_sleep) {
365+
if (!deep_sleep_allowed) {
364366
print_safe_mode_message(safe_mode);
365367
serial_write("\n");
366368
serial_write_compressed(translate("Press any key to enter the REPL. Use CTRL-D to reload."));
@@ -379,27 +381,31 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
379381
#endif
380382

381383
bool animation_done = false;
382-
if (will_deep_sleep && ok) {
384+
385+
if (deep_sleep_allowed && ok) {
383386
// Skip animation if everything is OK.
384387
animation_done = true;
385388
} else {
386389
animation_done = tick_rgb_status_animation(&animation);
387390
}
388391
// Do an error animation only once before deep-sleeping.
389-
if (animation_done && will_deep_sleep) {
390-
int64_t remaining_enumeration_wait = CIRCUITPY_USB_ENUMERATION_DELAY * 1024 - supervisor_ticks_ms64();
391-
// If USB isn't enumerated then deep sleep after our waiting period.
392-
if (ok && remaining_enumeration_wait < 0) {
392+
if (animation_done) {
393+
if (deep_sleep_allowed) {
393394
common_hal_mcu_deep_sleep();
394395
// Does not return.
395396
}
397+
396398
// Wake up every so often to flash the error code.
397399
if (!ok) {
398400
port_interrupt_after_ticks(CIRCUITPY_FLASH_ERROR_PERIOD * 1024);
399401
} else {
400-
port_interrupt_after_ticks(remaining_enumeration_wait);
402+
int64_t remaining_enumeration_wait =
403+
CIRCUITPY_USB_ENUMERATION_DELAY * 1024 - supervisor_ticks_ms64();
404+
if (remaining_enumeration_wait > 0) {
405+
port_interrupt_after_ticks(remaining_enumeration_wait);
406+
}
407+
port_sleep_until_interrupt();
401408
}
402-
port_sleep_until_interrupt();
403409
}
404410
}
405411
}

shared-bindings/alarm/__init__.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
//|
3939
//| Light sleep leaves the CPU and RAM powered so that CircuitPython can resume where it left off
4040
//| after being woken up. CircuitPython automatically goes into a light sleep when `time.sleep()` is
41-
//| called. To light sleep until a non-time alarm use `alarm.sleep_until_alarm()`. Any active
41+
//| called. To light sleep until a non-time alarm use `alarm.sleep_until_alarms()`. Any active
4242
//| peripherals, such as I2C, are left on.
4343
//|
4444
//| Deep sleep shuts down power to nearly all of the chip including the CPU and RAM. This can save
@@ -51,7 +51,7 @@
5151
//|
5252
//| An error includes an uncaught exception, or sys.exit() called with a non-zero argument
5353
//|
54-
//| To set alarms for deep sleep use `alarm.restart_on_alarm()` they will apply to next deep sleep only."""
54+
//| To set alarms for deep sleep use `alarm.set_deep_sleep_alarms()` they will apply to next deep sleep only."""
5555
//|
5656
//| wake_alarm: Alarm
5757
//| """The most recent alarm to wake us up from a sleep (light or deep.)"""

shared-bindings/alarm/pin/PinAlarm.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,26 @@
3838
//| """Trigger an alarm when a pin changes state."""
3939
//|
4040
//| def __init__(self, *pins: microcontroller.Pin, value: bool, all_same_value: bool = False, edge: bool = False, pull: bool = False) -> None:
41-
//| """Create an alarm triggered by a `~microcontroller.Pin` level. The alarm is not active
42-
//| until it is listed in an `alarm`-enabling function, such as `alarm.sleep_until_alarm()` or
43-
//| `alarm.restart_on_alarm()`.
41+
//| """Create an alarm triggered by a `microcontroller.Pin` level. The alarm is not active
42+
//| until it is listed in an `alarm`-enabling function, such as `alarm.sleep_until_alarms()` or
43+
//| `alarm.set_deep_sleep_alarms()`.
4444
//|
45-
//| :param pins: The pins to monitor. On some ports, the choice of pins
46-
//| may be limited due to hardware restrictions, particularly for deep-sleep alarms.
45+
//| :param microcontroller.Pin \*pins: The pins to monitor. On some ports, the choice of pins
46+
//| may be limited due to hardware restrictions, particularly for deep-sleep alarms.
4747
//| :param bool value: When active, trigger when the pin value is high (``True``) or low (``False``).
48-
//| On some ports, multiple `PinAlarm` objects may need to have coordinated values
49-
//| for deep-sleep alarms.
50-
//| :param bool all_same_value: If ``True``, all pins listed must be at `value` to trigger the alarm.
51-
//| If ``False``, any one of the pins going to `value` will trigger the alarm.
48+
//| On some ports, multiple `PinAlarm` objects may need to have coordinated values
49+
//| for deep-sleep alarms.
50+
//| :param bool all_same_value: If ``True``, all pins listed must be at ``value`` to trigger the alarm.
51+
//| If ``False``, any one of the pins going to ``value`` will trigger the alarm.
5252
//| :param bool edge: If ``True``, trigger only when there is a transition to the specified
53-
//| value of `value`. If ``True``, if the alarm becomes active when the pin value already
54-
//| matches `value`, the alarm is not triggered: the pin must transition from ``not value``
55-
//| to ``value`` to trigger the alarm. On some ports, edge-triggering may not be available,
56-
//| particularly for deep-sleep alarms.
57-
//| :param bool pull: Enable a pull-up or pull-down which pulls the pin to value opposite
58-
//| opposite that of `value`. For instance, if `value` is set to ``True``, setting `pull`
59-
//| to ``True`` will enable a pull-down, to hold the pin low normally until an outside signal
60-
//| pulls it high.
53+
//| value of ``value``. If ``True``, if the alarm becomes active when the pin value already
54+
//| matches ``value``, the alarm is not triggered: the pin must transition from ``not value``
55+
//| to ``value`` to trigger the alarm. On some ports, edge-triggering may not be available,
56+
//| particularly for deep-sleep alarms.
57+
//| :param bool pull: Enable a pull-up or pull-down which pulls the pin to the level opposite
58+
//| opposite that of ``value``. For instance, if ``value`` is set to ``True``, setting ``pull``
59+
//| to ``True`` will enable a pull-down, to hold the pin low normally until an outside signal
60+
//| pulls it high.
6161
//| """
6262
//| ...
6363
//|

shared-bindings/alarm/time/DurationAlarm.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
//| """Trigger an alarm at a specified interval from now."""
3838
//|
3939
//| def __init__(self, secs: float) -> None:
40-
//| """Create an alarm that will be triggered in `secs` seconds from the time
40+
//| """Create an alarm that will be triggered in ``secs`` seconds from the time
4141
//| sleep starts. The alarm is not active until it is listed in an
42-
//| `alarm`-enabling function, such as `alarm.sleep_until_alarm()` or
43-
//| `alarm.restart_on_alarm()`.
42+
//| `alarm`-enabling function, such as `alarm.sleep_until_alarms()` or
43+
//| `alarm.set_deep_sleep_alarms()`.
4444
//| """
4545
//| ...
4646
//|

shared-bindings/microcontroller/Processor.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ const mp_obj_property_t mcu_processor_frequency_obj = {
6767
},
6868
};
6969

70-
//| reset_reason: `microcontroller.ResetReason`
71-
//| """The reason the microcontroller started up from reset state."""
70+
//| reset_reason: microcontroller.ResetReason
71+
//| """The reason the microcontroller started up from reset state."""
7272
//|
7373
STATIC mp_obj_t mcu_processor_get_reset_reason(mp_obj_t self) {
7474
return cp_enum_find(&mcu_reset_reason_type, common_hal_mcu_processor_get_reset_reason());

shared-bindings/supervisor/RunReason.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ MAKE_ENUM_VALUE(supervisor_run_reason_type, run_reason, REPL_RELOAD, RUN_REASON_
3737
//| """The reason that CircuitPython started running."""
3838
//|
3939
//| STARTUP: object
40-
//| """CircuitPython started the microcontroller started up. See `microcontroller.cpu.reset_reason`
40+
//| """CircuitPython started the microcontroller started up. See `microcontroller.Processor.reset_reason`
4141
//| for more detail on why the microcontroller was started."""
4242
//|
4343
//| AUTO_RELOAD: object

shared-bindings/supervisor/Runtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ const mp_obj_property_t supervisor_serial_bytes_available_obj = {
9898

9999

100100
//| run_reason: RunReason
101-
//| """Returns why CircuitPython started running this particular time.
101+
//| """Returns why CircuitPython started running this particular time."""
102102
//|
103103
STATIC mp_obj_t supervisor_get_run_reason(mp_obj_t self) {
104104
return cp_enum_find(&supervisor_run_reason_type, _run_reason);

shared-bindings/supervisor/__init__.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,19 @@
4747
//| This object is the sole instance of `supervisor.Runtime`."""
4848
//|
4949

50-
//| def allow_deep_sleep(*, when_connected: bool = False, on_error: bool = False):
51-
//| """Configure when CircuitPython can go into deep sleep. Deep sleep can occur
52-
//| after a program has finished running or when `supervisor.deep_sleep_now()` is called.
50+
//| def allow_deep_sleep(*, when_connected: bool = False, on_error: bool = False) -> None:
51+
//| """Configure when CircuitPython can go into deep sleep. Deep sleep can occur
52+
//| after a program has finished running or when `supervisor.exit_and_deep_sleep()` is called.
5353
//|
54-
//| :param bool when_connected: If ``True``, CircuitPython will go into deep sleep
55-
//| when a program finishes, even if it is connected to a host computer over USB or other means.
56-
//| It will disconnect from the host before sleeping.
57-
//| If ``False``, deep sleep will not be entered if connected.
58-
//| :param bool on_error: If ``True``, deep sleep will be entered if even a program
59-
//| terminated due to an exception or fatal error. If ``False``, an error will cause
60-
//| CircuitPython to stay awake, flashing error codes on the status RGB LED, if available.
61-
//| ...
54+
//| :param bool when_connected: If ``True``, CircuitPython will go into deep sleep
55+
//| when a program finishes, even if it is connected to a host computer over USB or other means.
56+
//| It will disconnect from the host before sleeping.
57+
//| If ``False``, deep sleep will not be entered if connected.
58+
//| :param bool on_error: If ``True``, deep sleep will be entered if even a program
59+
//| terminated due to an exception or fatal error. If ``False``, an error will cause
60+
//| CircuitPython to stay awake, flashing error codes on the status RGB LED, if available.
61+
//| """
62+
//| ...
6263
//|
6364
STATIC mp_obj_t supervisor_allow_deep_sleep(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
6465
enum { ARG_when_connected, ARG_on_error };
@@ -77,16 +78,16 @@ STATIC mp_obj_t supervisor_allow_deep_sleep(size_t n_args, const mp_obj_t *pos_a
7778
}
7879
MP_DEFINE_CONST_FUN_OBJ_KW(supervisor_allow_deep_sleep_obj, 0, supervisor_allow_deep_sleep);
7980

80-
//| def deep_sleep(): -> None
81+
//| def exit_and_deep_sleep() -> None:
8182
//| """Go into deep sleep mode immediately, if not connected to a host computer.
82-
//| But if connected and `supervisor.runtime.allow_deep_sleep(when_connected=true)`
83-
//| has not been called, simply restart.
84-
//|
83+
//| But if connected and ``supervisor.allow_deep_sleep(when_connected=true)``
84+
//| has not been called, simply restart."""
85+
//| ...
8586

86-
STATIC mp_obj_t supervisor_deep_sleep(void) {
87+
STATIC mp_obj_t supervisor_exit_and_deep_sleep(void) {
8788
common_hal_mcu_deep_sleep();
8889
}
89-
MP_DEFINE_CONST_FUN_OBJ_0(supervisor_deep_sleep_obj, supervisor_deep_sleep);
90+
MP_DEFINE_CONST_FUN_OBJ_0(supervisor_exit_and_deep_sleep_obj, supervisor_exit_and_deep_sleep);
9091

9192
//| def enable_autoreload() -> None:
9293
//| """Enable autoreload based on USB file write activity."""
@@ -156,7 +157,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(supervisor_set_next_stack_limit_obj, supervisor_set_ne
156157
STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = {
157158
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_supervisor) },
158159
{ MP_ROM_QSTR(MP_QSTR_allow_deep_sleep), MP_ROM_PTR(&supervisor_allow_deep_sleep_obj) },
159-
{ MP_ROM_QSTR(MP_QSTR_deep_sleep), MP_ROM_PTR(&supervisor_deep_sleep_obj) },
160+
{ MP_ROM_QSTR(MP_QSTR_exit_and_deep_sleep), MP_ROM_PTR(&supervisor_exit_and_deep_sleep_obj) },
160161
{ MP_ROM_QSTR(MP_QSTR_enable_autoreload), MP_ROM_PTR(&supervisor_enable_autoreload_obj) },
161162
{ MP_ROM_QSTR(MP_QSTR_disable_autoreload), MP_ROM_PTR(&supervisor_disable_autoreload_obj) },
162163
{ MP_ROM_QSTR(MP_QSTR_set_rgb_status_brightness), MP_ROM_PTR(&supervisor_set_rgb_status_brightness_obj) },

0 commit comments

Comments
 (0)