Skip to content

Commit 39e1f52

Browse files
committed
wip; not compiling yet
1 parent cd436ba commit 39e1f52

File tree

14 files changed

+108
-159
lines changed

14 files changed

+108
-159
lines changed

main.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include "supervisor/shared/safe_mode.h"
5757
#include "supervisor/shared/status_leds.h"
5858
#include "supervisor/shared/stack.h"
59+
#include "supervisor/shared/workflow.h"
5960
#include "supervisor/serial.h"
6061
#include "supervisor/usb.h"
6162

@@ -92,6 +93,12 @@
9293
#include "common-hal/canio/CAN.h"
9394
#endif
9495

96+
// How long to wait for host to enumerate (secs).
97+
#define CIRCUITPY_USB_ENUMERATION_DELAY 1
98+
99+
// How long to flash errors on the RGB status LED before going to sleep (secs)
100+
#define CIRCUITPY_FLASH_ERROR_PERIOD 10
101+
95102
void do_str(const char *src, mp_parse_input_kind_t input_kind) {
96103
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
97104
if (lex == NULL) {
@@ -319,11 +326,11 @@ bool run_code_py(safe_mode_t safe_mode) {
319326
bool refreshed_epaper_display = false;
320327
#endif
321328
rgb_status_animation_t animation;
322-
bool ok = result->return_code != PYEXEC_EXCEPTION;
329+
bool ok = result.return_code != PYEXEC_EXCEPTION;
323330
#if CIRCUITPY_ALARM
324331
// If USB isn't enumerated then deep sleep.
325332
if (ok && !supervisor_workflow_active() && supervisor_ticks_ms64() > CIRCUITPY_USB_ENUMERATION_DELAY * 1024) {
326-
common_hal_sleep_deep_sleep();
333+
common_hal_mcu_deep_sleep();
327334
}
328335
#endif
329336
// Show the animation every N seconds.
@@ -365,8 +372,8 @@ bool run_code_py(safe_mode_t safe_mode) {
365372
int64_t remaining_enumeration_wait = CIRCUITPY_USB_ENUMERATION_DELAY * 1024 - supervisor_ticks_ms64();
366373
// If USB isn't enumerated then deep sleep after our waiting period.
367374
if (ok && remaining_enumeration_wait < 0) {
368-
common_hal_sleep_deep_sleep();
369-
return; // Doesn't actually get here.
375+
common_hal_mcu_deep_sleep();
376+
return false; // Doesn't actually get here.
370377
}
371378
#endif
372379
// Wake up every so often to flash the error code.
@@ -424,7 +431,7 @@ void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
424431
// Wait 1.5 seconds before opening CIRCUITPY_BOOT_OUTPUT_FILE for write,
425432
// in case power is momentary or will fail shortly due to, say a low, battery.
426433
#if CIRCUITPY_ALARM
427-
if (common_hal_sleep_get_reset_reason() == RESET_REASON_POWER_ON) {
434+
if (common_hal_alarm_get_reset_reason() == RESET_REASON_POWER_ON) {
428435
#endif
429436
mp_hal_delay_ms(1500);
430437
#if CIRCUITPY_ALARM

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ void common_hal_alarm_disable_all(void) {
3535
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
3636
}
3737

38+
mp_obj_t common_hal_alarm_get_reset_reason(void) {
39+
switch (esp_sleep_get_wakeup_cause()) {
40+
case ESP_SLEEP_WAKEUP_TIMER:
41+
return RESET_REASON_DEEP_SLEEP_ALARM;
42+
case ESP_SLEEP_WAKEUP_EXT0:
43+
return RESET_REASON_DEEP_SLEEP_ALARM;
44+
case ESP_SLEEP_WAKEUP_TOUCHPAD:
45+
//TODO: implement TouchIO
46+
case ESP_SLEEP_WAKEUP_UNDEFINED:
47+
default:
48+
return mp_const_none;
49+
break;
50+
}
51+
}
52+
53+
3854
mp_obj_t common_hal_alarm_get_wake_alarm(void) {
3955
switch (esp_sleep_get_wakeup_cause()) {
4056
case ESP_SLEEP_WAKEUP_TIMER: ;

shared-bindings/alarm/__init__.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@
3434
#include "py/obj.h"
3535
#include "py/runtime.h"
3636

37-
#include "shared-bindings/alarm/pin/__init__.h"
38-
#include "shared-bindings/alarm/time/__init__.h"
39-
4037
STATIC mp_obj_t alarm_sleep_until_alarm(size_t n_args, const mp_obj_t *args) {
4138
// TODO
39+
return mp_const_none;
4240
}
4341
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(alarm_sleep_until_alarm_obj, 1, MP_OBJ_FUN_ARGS_MAX, alarm_sleep_until_alarm);
4442

@@ -51,6 +49,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(alarm_sleep_until_alarm_obj, 1, MP_OBJ_FUN_A
5149
//|
5250
STATIC mp_obj_t alarm_restart_on_alarm(size_t n_args, const mp_obj_t *args) {
5351
// TODO
52+
return mp_const_none;
5453
}
5554
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(alarm_restart_on_alarm_obj, 1, MP_OBJ_FUN_ARGS_MAX, alarm_restart_on_alarm);
5655

@@ -102,7 +101,6 @@ mp_map_elem_t alarm_module_globals_table[] = {
102101
};
103102
STATIC MP_DEFINE_MUTABLE_DICT(alarm_module_globals, alarm_module_globals_table);
104103

105-
// These are called from common_hal code to set the current wake alarm.
106104
void common_hal_alarm_set_wake_alarm(mp_obj_t alarm) {
107105
// Equivalent of:
108106
// alarm.wake_alarm = alarm
@@ -113,7 +111,16 @@ void common_hal_alarm_set_wake_alarm(mp_obj_t alarm) {
113111
}
114112
}
115113

116-
// These are called from common hal code to set the current wake alarm.
114+
alarm_reset_reason_t common_hal_alarm_get_reset_reason(void) {
115+
mp_map_elem_t *elem =
116+
mp_map_lookup(&alarm_module_globals_table, MP_ROM_QSTR(MP_QSTR_reset_reason), MP_MAP_LOOKUP);
117+
if (elem) {
118+
return elem->value;
119+
} else {
120+
return mp_const_none;
121+
}
122+
}
123+
117124
void common_hal_alarm_set_reset_reason(mp_obj_t reset_reason) {
118125
// Equivalent of:
119126
// alarm.reset_reason = reset_reason

shared-bindings/alarm/__init__.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@
2929

3030
#include "py/obj.h"
3131

32+
#include "shared-bindings/alarm/ResetReason.h"
33+
3234
extern void common_hal_alarm_set_wake_alarm(mp_obj_t alarm);
35+
36+
extern alarm_reset_reason_t common_hal_alarm_get_reset_reason(void);
3337
extern void common_hal_alarm_set_reset_reason(mp_obj_t reset_reason);
3438

3539
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ALARM___INIT___H

shared-bindings/canio/BusState.c

Lines changed: 0 additions & 70 deletions
This file was deleted.

shared-bindings/canio/BusState.h

Lines changed: 0 additions & 33 deletions
This file was deleted.

shared-bindings/canio/__init__.c

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

27-
#include "py/obj.h"
28-
29-
#include "shared-bindings/canio/__init__.h"
30-
31-
#include "shared-bindings/canio/BusState.h"
32-
#include "shared-bindings/canio/CAN.h"
33-
#include "shared-bindings/canio/Match.h"
34-
#include "shared-bindings/canio/Message.h"
35-
#include "shared-bindings/canio/Listener.h"
36-
3727
//| """CAN bus access
3828
//|
3929
//| The `canio` module contains low level classes to support the CAN bus
@@ -67,6 +57,56 @@
6757
//| """
6858
//|
6959

60+
#include "py/obj.h"
61+
#include "py/enum.h"
62+
63+
#include "shared-bindings/canio/__init__.h"
64+
#include "shared-bindings/canio/CAN.h"
65+
#include "shared-bindings/canio/Match.h"
66+
#include "shared-bindings/canio/Message.h"
67+
#include "shared-bindings/canio/Listener.h"
68+
69+
MAKE_ENUM_VALUE(canio_bus_state_type, bus_state, ERROR_ACTIVE, BUS_STATE_ERROR_ACTIVE);
70+
MAKE_ENUM_VALUE(canio_bus_state_type, bus_state, ERROR_PASSIVE, BUS_STATE_ERROR_PASSIVE);
71+
MAKE_ENUM_VALUE(canio_bus_state_type, bus_state, ERROR_WARNING, BUS_STATE_ERROR_WARNING);
72+
MAKE_ENUM_VALUE(canio_bus_state_type, bus_state, BUS_OFF, BUS_STATE_OFF);
73+
74+
//| class BusState:
75+
//| """The state of the CAN bus"""
76+
//|
77+
//| ERROR_ACTIVE: object
78+
//| """The bus is in the normal (active) state"""
79+
//|
80+
//| ERROR_WARNING: object
81+
//| """The bus is in the normal (active) state, but a moderate number of errors have occurred recently.
82+
//|
83+
//| NOTE: Not all implementations may use ERROR_WARNING. Do not rely on seeing ERROR_WARNING before ERROR_PASSIVE."""
84+
//|
85+
//| ERROR_PASSIVE: object
86+
//| """The bus is in the passive state due to the number of errors that have occurred recently.
87+
//|
88+
//| This device will acknowledge packets it receives, but cannot transmit messages.
89+
//| If additional errors occur, this device may progress to BUS_OFF.
90+
//| If it successfully acknowledges other packets on the bus, it can return to ERROR_WARNING or ERROR_ACTIVE and transmit packets.
91+
//| """
92+
//|
93+
//| BUS_OFF: object
94+
//| """The bus has turned off due to the number of errors that have
95+
//| occurred recently. It must be restarted before it will send or receive
96+
//| packets. This device will neither send or acknowledge packets on the bus."""
97+
//|
98+
MAKE_ENUM_MAP(canio_bus_state) {
99+
MAKE_ENUM_MAP_ENTRY(bus_state, ERROR_ACTIVE),
100+
MAKE_ENUM_MAP_ENTRY(bus_state, ERROR_PASSIVE),
101+
MAKE_ENUM_MAP_ENTRY(bus_state, ERROR_WARNING),
102+
MAKE_ENUM_MAP_ENTRY(bus_state, BUS_OFF),
103+
};
104+
STATIC MP_DEFINE_CONST_DICT(canio_bus_state_locals_dict, canio_bus_state_locals_table);
105+
106+
MAKE_PRINTER(canio, canio_bus_state);
107+
108+
MAKE_ENUM_TYPE(canio, BusState, canio_bus_state);
109+
70110
STATIC const mp_rom_map_elem_t canio_module_globals_table[] = {
71111
{ MP_ROM_QSTR(MP_QSTR_BusState), MP_ROM_PTR(&canio_bus_state_type) },
72112
{ MP_ROM_QSTR(MP_QSTR_CAN), MP_ROM_PTR(&canio_can_type) },

shared-bindings/canio/__init__.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@
2525
*/
2626

2727
#pragma once
28+
29+
typedef enum {
30+
BUS_STATE_ERROR_ACTIVE, BUS_STATE_ERROR_PASSIVE, BUS_STATE_ERROR_WARNING, BUS_STATE_OFF
31+
} canio_bus_state_t;
32+
33+
extern const mp_obj_type_t canio_bus_state_type;

supervisor/shared/rgb_led_status.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,5 @@ bool tick_rgb_status_animation(rgb_status_animation_t* status) {
483483
}
484484
}
485485
#endif
486+
return false; // Animation is not finished.
486487
}

supervisor/shared/serial.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ bool serial_connected(void) {
6969
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
7070
return true;
7171
#else
72-
// True if DTR is asserted, and the USB connection is up.
73-
// tud_cdc_get_line_state(): bit 0 is DTR, bit 1 is RTS
74-
return (tud_cdc_get_line_state() & 1) && tud_ready();
72+
return tud_cdc_connected();
7573
#endif
7674
}
7775

0 commit comments

Comments
 (0)