Skip to content

Commit a0f1ec3

Browse files
committed
wip
1 parent 75559f3 commit a0f1ec3

File tree

38 files changed

+287
-151
lines changed

38 files changed

+287
-151
lines changed

main.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
#include "supervisor/usb.h"
6262

6363
#include "shared-bindings/microcontroller/__init__.h"
64+
#include "shared-bindings/microcontroller/Processor.h"
65+
#include "shared-bindings/supervisor/Runtime.h"
6466

6567
#include "boards/board.h"
6668

@@ -327,24 +329,27 @@ bool run_code_py(safe_mode_t safe_mode) {
327329
#endif
328330
rgb_status_animation_t animation;
329331
bool ok = result.return_code != PYEXEC_EXCEPTION;
330-
#if CIRCUITPY_ALARM
331332
// If USB isn't enumerated then deep sleep.
332333
if (ok && !supervisor_workflow_active() && supervisor_ticks_ms64() > CIRCUITPY_USB_ENUMERATION_DELAY * 1024) {
333-
common_hal_alarm_restart_on_alarm(0, NULL);
334+
common_hal_mcu_deep_sleep();
334335
}
335-
#endif
336336
// Show the animation every N seconds.
337337
prep_rgb_status_animation(&result, found_main, safe_mode, &animation);
338338
while (true) {
339339
RUN_BACKGROUND_TASKS;
340340
if (reload_requested) {
341+
supervisor_set_run_reason(RUN_REASON_AUTO_RELOAD);
341342
reload_requested = false;
342343
return true;
343344
}
344345

345346
if (serial_connected() && serial_bytes_available()) {
346347
// Skip REPL if reload was requested.
347-
return (serial_read() == CHAR_CTRL_D);
348+
bool ctrl_d = serial_read() == CHAR_CTRL_D;
349+
if (ctrl_d) {
350+
supervisor_set_run_reason(RUN_REASON_REPL_RELOAD);
351+
}
352+
return (ctrl_d);
348353
}
349354

350355
if (!serial_connected_before_animation && serial_connected()) {
@@ -521,6 +526,9 @@ int __attribute__((used)) main(void) {
521526
reset_devices();
522527
reset_board();
523528

529+
// This is first time we are running CircuitPython after a reset or power-up.
530+
supervisor_set_run_reason(RUN_REASON_STARTUP);
531+
524532
// If not in safe mode turn on autoreload by default but before boot.py in case it wants to change it.
525533
if (safe_mode == NO_SAFE_MODE) {
526534
autoreload_enable();

ports/atmel-samd/common-hal/microcontroller/__init__.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ void common_hal_mcu_reset(void) {
8484
reset();
8585
}
8686

87+
void common_hal_mcu_deep_sleep(void) {
88+
//deep sleep call here
89+
}
90+
8791
// The singleton microcontroller.Processor object, bound to microcontroller.cpu
8892
// It currently only has properties, and no state.
8993
const mcu_processor_obj_t common_hal_mcu_processor_obj = {

ports/cxd56/common-hal/microcontroller/__init__.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ void common_hal_mcu_reset(void) {
8181
boardctl(BOARDIOC_RESET, 0);
8282
}
8383

84+
void common_hal_mcu_deep_sleep(void) {
85+
//deep sleep call here
86+
}
87+
8488
STATIC const mp_rom_map_elem_t mcu_pin_globals_table[] = {
8589
{ MP_ROM_QSTR(MP_QSTR_UART2_RXD), MP_ROM_PTR(&pin_UART2_RXD) },
8690
{ MP_ROM_QSTR(MP_QSTR_UART2_TXD), MP_ROM_PTR(&pin_UART2_TXD) },

ports/esp32s2/common-hal/alarm/__init__.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,20 @@
2525
* THE SOFTWARE.
2626
*/
2727

28+
#include "py/objtuple.h"
29+
2830
#include "shared-bindings/alarm/__init__.h"
2931
#include "shared-bindings/alarm/pin/PinAlarm.h"
3032
#include "shared-bindings/alarm/time/DurationAlarm.h"
3133

3234
#include "esp_sleep.h"
3335

36+
STATIC mp_obj_tuple_t *_deep_sleep_alarms;
37+
38+
void alarm_reset(void) {
39+
_deep_sleep_alarms = &mp_const_empty_tuple;
40+
}
41+
3442
void common_hal_alarm_disable_all(void) {
3543
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
3644
}
@@ -63,3 +71,38 @@ mp_obj_t common_hal_alarm_get_wake_alarm(void) {
6371
}
6472
return mp_const_none;
6573
}
74+
75+
mp_obj_t common_hal_alarm_sleep_until_alarms(size_t n_alarms, const mp_obj_t *alarms) {
76+
mp_raise_NotImplementedError(NULL);
77+
}
78+
79+
void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *alarms) {
80+
bool time_alarm_set = false;
81+
for (size_t i = 0; i < n_alarms; i++) {
82+
if (MP_OBJ_IS_TYPE(alarms[i], &alarm_pin_pin_alarm_type)) {
83+
mp_raise_NotImplementedError(translate("PinAlarm deep sleep not yet implemented"));
84+
}
85+
if (MP_OBJ_IS_TYPE(alarms[i], &alarm_time_duration_alarm_type)) {
86+
if (time_alarm_set) {
87+
mp_raise_ValueError(translate("Only one alarm.time alarm can be set."));
88+
}
89+
time_alarm_set = true;
90+
}
91+
}
92+
93+
_deep_sleep_alarms = mp_obj_new_tuple(n_alarms, alarms);
94+
}
95+
96+
void common_hal_deep_sleep_with_alarms(void) {
97+
for (size_t i = 0; i < _deep_sleep_alarms.len; i++) {
98+
mp_obj_t alarm = _deep_sleep_alarms.items[i]
99+
if (MP_OBJ_IS_TYPE(alarm, &alarm_time_duration_alarm_type)) {
100+
alarm_time_duration_alarm_obj_t *duration_alarm = MP_OBJ_TO_PTR(alarm);
101+
esp_sleep_enable_timer_wakeup(
102+
(uint64_t) (duration_alarm->duration * 1000000.0f));
103+
}
104+
// TODO: handle pin alarms
105+
}
106+
107+
common_hal_mcu_deep_sleep();
108+
}

ports/esp32s2/common-hal/alarm/pin/PinAlarm.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,24 @@
3030
#include "shared-bindings/alarm/pin/PinAlarm.h"
3131
#include "shared-bindings/microcontroller/Pin.h"
3232

33-
void common_hal_alarm_pin_pin_alarm_construct(alarm_pin_pin_alarm_obj_t *self, const mcu_pin_obj_t *pin, bool level, bool edge, bool pull) {
34-
self->pin = pin;
35-
self->level = level;
33+
void common_hal_alarm_pin_pin_alarm_construct(alarm_pin_pin_alarm_obj_t *self, const mp_obj_t pins[], size_t num_pins, bool value, bool all_same_value, bool edge, bool pull) {
34+
self->pins = mp_obj_new_tuple(num_pins, pins);
35+
self->value = value;
36+
self->all_same_value = all_same_value;
3637
self->edge = edge;
3738
self->pull = pull;
3839
}
3940

40-
const mcu_pin_obj_t *common_hal_alarm_pin_pin_alarm_get_pin(alarm_pin_pin_alarm_obj_t *self) {
41-
return self->pin;
41+
const mp_obj_tuple_t *common_hal_alarm_pin_pin_alarm_get_pins(alarm_pin_pin_alarm_obj_t *self) {
42+
return self->pins;
4243
}
4344

44-
bool common_hal_alarm_pin_pin_alarm_get_level(alarm_pin_pin_alarm_obj_t *self) {
45-
return self->level;
45+
bool common_hal_alarm_pin_pin_alarm_get_value(alarm_pin_pin_alarm_obj_t *self) {
46+
return self->value;
47+
}
48+
49+
bool common_hal_alarm_pin_pin_alarm_get_all_same_value(alarm_pin_pin_alarm_obj_t *self) {
50+
return self->all_same_value;
4651
}
4752

4853
bool common_hal_alarm_pin_pin_alarm_get_edge(alarm_pin_pin_alarm_obj_t *self) {

ports/esp32s2/common-hal/alarm/pin/PinAlarm.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
2827
#include "py/obj.h"
28+
#include "py/objtuple.h"
2929

3030
typedef struct {
3131
mp_obj_base_t base;
32-
const mcu_pin_obj_t *pin;
33-
bool level;
32+
mp_obj_tuple_t *pins;
33+
bool value;
34+
bool all_same_value;
3435
bool edge;
3536
bool pull;
3637
} alarm_pin_pin_alarm_obj_t;

ports/esp32s2/common-hal/microcontroller/Processor.c

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@
3131
#include "py/runtime.h"
3232

3333
#include "common-hal/microcontroller/Processor.h"
34+
#include "shared-bindings/microcontroller/ResetReason.h"
3435
#include "supervisor/shared/translate.h"
3536

37+
#include "esp_sleep.h"
38+
#include "esp_system.h"
39+
3640
#include "soc/efuse_reg.h"
3741

3842
#include "components/driver/esp32s2/include/driver/temp_sensor.h"
@@ -77,21 +81,42 @@ void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {
7781
}
7882

7983
mcu_reset_reason_t common_hal_mcu_processor_get_reset_reason(void) {
80-
switch (esp_sleep_get_wakeup_cause()) {
81-
case ESP_SLEEP_WAKEUP_TIMER:
82-
return RESET_REASON_DEEP_SLEEP_ALARM;
83-
84-
case ESP_SLEEP_WAKEUP_EXT0:
85-
return RESET_REASON_DEEP_SLEEP_ALARM;
86-
87-
case ESP_SLEEP_WAKEUP_TOUCHPAD:
88-
//TODO: implement TouchIO
89-
case ESP_SLEEP_WAKEUP_UNDEFINED:
84+
switch (esp_reset_reason()) {
85+
case ESP_RST_POWERON:
86+
return RESET_REASON_POWER_ON;
87+
88+
case ESP_RST_SW:
89+
case ESP_RST_PANIC:
90+
return RESET_REASON_SOFTWARE;
91+
92+
case ESP_RST_INT_WDT:
93+
case ESP_RST_TASK_WDT:
94+
case ESP_RST_WDT:
95+
return RESET_REASON_WATCHDOG;
96+
97+
case ESP_RST_BROWNOUT:
98+
return RESET_REASON_BROWNOUT;
99+
100+
case ESP_RST_SDIO:
101+
case ESP_RST_EXT:
102+
return RESET_REASON_RESET_PIN;
103+
104+
case ESP_RST_DEEPSLEEP:
105+
switch (esp_sleep_get_wakeup_cause()) {
106+
case ESP_SLEEP_WAKEUP_TIMER:
107+
case ESP_SLEEP_WAKEUP_EXT0:
108+
case ESP_SLEEP_WAKEUP_EXT1:
109+
case ESP_SLEEP_WAKEUP_TOUCHPAD:
110+
return RESET_REASON_DEEP_SLEEP_ALARM;
111+
112+
case ESP_SLEEP_WAKEUP_UNDEFINED:
113+
default:
114+
return RESET_REASON_UNKNOWN;
115+
}
116+
117+
case ESP_RST_UNKNOWN:
90118
default:
91-
return RESET_REASON_POWER_APPLIED;
92-
}
93-
}
119+
return RESET_REASON_UNKNOWN;
94120

95-
mcu_reset_reason_t common_hal_mcu_processor_get_reset_reason(void) {
96-
return RESET_REASON_POWER_ON;
121+
}
97122
}

ports/esp32s2/common-hal/microcontroller/Processor.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#ifndef MICROPY_INCLUDED_LITEX_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H
28-
#define MICROPY_INCLUDED_LITEX_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H
27+
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H
28+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H
2929

3030
#define COMMON_HAL_MCU_PROCESSOR_UID_LENGTH 6
3131

@@ -36,4 +36,4 @@ typedef struct {
3636
// Stores no state currently.
3737
} mcu_processor_obj_t;
3838

39-
#endif // MICROPY_INCLUDED_LITEX_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H
39+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H

ports/esp32s2/common-hal/microcontroller/__init__.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ void common_hal_mcu_reset(void) {
7979
while(1);
8080
}
8181

82+
void common_hal_mcu_deep_sleep(void) {
83+
// Shut down wifi cleanly.
84+
esp_wifi_stop();
85+
86+
// Does not return.
87+
esp_deep_sleep_start();
88+
}
89+
8290
// The singleton microcontroller.Processor object, bound to microcontroller.cpu
8391
// It currently only has properties, and no state.
8492
const mcu_processor_obj_t common_hal_mcu_processor_obj = {

ports/esp32s2/common-hal/rtc/RTC.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_RTC_RTC_H
28-
#define MICROPY_INCLUDED_NRF_COMMON_HAL_RTC_RTC_H
27+
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_RTC_RTC_H
28+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_RTC_RTC_H
2929

3030
extern void rtc_init(void);
3131
extern void rtc_reset(void);
3232
extern void common_hal_rtc_init(void);
3333

34-
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_RTC_RTC_H
34+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_RTC_RTC_H

0 commit comments

Comments
 (0)