8000 Merge pull request #5272 from tannewt/fix_nrf_wdt_crashes · domdfcoding/circuitpython@fbb005b · GitHub
[go: up one dir, main page]

Skip to content

Commit fbb005b

Browse files
authored
Merge pull request adafruit#5272 from tannewt/fix_nrf_wdt_crashes
Fix two watchdog crashes
2 parents 65753a1 + 92a4319 commit fbb005b

File tree

8 files changed

+72
-21
lines changed

8 files changed

+72
-21
lines changed

lib/utils/pyexec.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,21 @@ int pyexec_friendly_repl(void) {
650650
}
651651

652652
vstr_reset(&line);
653-
int ret = readline(&line, ">>> ");
653+
654+
nlr_buf_t nlr;
655+
nlr.ret_val = NULL;
656+
int ret = 0;
657+
if (nlr_push(&nlr) == 0) {
658+
ret = readline(&line, ">>> ");
659+
} else {
660+
// Uncaught exception
661+
mp_handle_pending(false); // clear any pending exceptions (and run any callbacks)
662+
663+
// Print exceptions but stay in the REPL. There are very few delayed
664+
// exceptions. The WatchDogTimer can raise one though.
665+
mp_hal_stdout_tx_str("\r\n");
666+
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
667+
}
654668
mp_parse_input_kind_t parse_input_kind = MP_PARSE_SINGLE_INPUT;
655669

656670
if (ret == CHAR_CTRL_A) {

locale/circuitpython.pot

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3939,6 +3939,7 @@ msgstr ""
39393939
#: ports/esp32s2/boards/gravitech_cucumber_r/mpconfigboard.h
39403940
#: ports/esp32s2/boards/gravitech_cucumber_rs/mpconfigboard.h
39413941
#: ports/esp32s2/boards/lilygo_ttgo_t8_s2_st7789/mpconfigboard.h
3942+
#: ports/esp32s2/boards/lolin_s2_mini/mpconfigboard.h
39423943
#: ports/esp32s2/boards/microdev_micro_s2/mpconfigboard.h
39433944
#: ports/esp32s2/boards/morpheans_morphesp-240/mpconfigboard.h
39443945
#: ports/esp32s2/boards/muselab_nanoesp32_s2_wroom/mpconfigboard.h

ports/atmel-samd/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161

6262
#define MICROPY_FATFS_EXFAT 0
6363

64+
// Only support simpler HID descriptors on SAMD21.
65+
#define CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR (1)
66+
6467
#endif // SAMD21
6568

6669
////////////////////////////////////////////////////////////////////////////////////////////////////

ports/nrf/common-hal/watchdog/WatchDogTimer.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <math.h>
2929
#include <string.h>
3030

31+
#include "py/gc.h"
3132
#include "py/obj.h"
3233
#include "py/objproperty.h"
3334
#include "py/runtime.h"
@@ -94,7 +95,11 @@ void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) {
9495

9596
void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) {
9697
if (self->mode == WATCHDOGMODE_RESET) {
97-
mp_raise_NotImplementedError(translate("WatchDogTimer cannot be deinitialized once mode is set to RESET"));
98+
if (gc_alloc_possible()) {
99+
mp_raise_NotImplementedError(translate("WatchDogTimer cannot be deinitialized once mode is set to RESET"));
100+
}
101+
// Don't change anything because RESET cannot be undone.
102+
return;
98103
}
99104
if (timer) {
100105
timer_free();

py/circuitpy_mpconfig.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,13 @@ void supervisor_run_background_tasks_if_tick(void);
717717

718718
#define CIRCUITPY_VERBOSE_BLE 0
719719

720+
// This trades ~1k flash space (1) for that much in RAM plus the cost to compute
721+
// the values once on init (0). Only turn it off, when you really need the flash
722+
// space and are willing to trade the RAM.
723+
#ifndef CIRCUITPY_PRECOMPUTE_QSTR_ATTR
724+
#define CIRCUITPY_PRECOMPUTE_QSTR_ATTR (1)
725+
#endif
726+
720727
// USB settings
721728

722729
// If the port requires certain USB endpoint numbers, define these in mpconfigport.h.
@@ -761,6 +768,16 @@ void supervisor_run_background_tasks_if_tick(void);
761768
#define USB_HID_EP_NUM_IN (0)
762769
#endif
763770

771+
// The most complicated device currently known of is the head and eye tracker, which requires 5
772+
// report ids.
773+
// https://usb.org/sites/default/files/hutrr74_-_usage_page_for_head_and_eye_trackers_0.pdf
774+
// The default descriptors only use 1, so that is the minimum.
775+
#ifndef CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR
776+
#define CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR (6)
777+
#elif CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR < 1
778+
#error "CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR must be at least 1"
779+
#endif
780+
764781
#ifndef USB_MIDI_EP_NUM_OUT
765782
#define USB_MIDI_EP_NUM_OUT (0)
766783
#endif

py/qstr.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,11 @@ mp_uint_t qstr_compute_hash(const byte *data, size_t len) {
7676
}
7777
return hash;
7878
}
79-
80-
const qstr_attr_t mp_qstr_const_attr[] = {
79+
#ifndef CIRCUITPY_PRECOMPUTE_QSTR_ATTR
80+
#define CIRCUITPY_PRECOMPUTE_QSTR_ATTR (1)
81+
#endif
82+
#if CIRCUITPY_PRECOMPUTE_QSTR_ATTR == 1
83+
const qstr_attr_t mp_qstr_const_attr[MP_QSTRnumber_of] = {
8184
#ifndef NO_QSTR
8285
#define QDEF(id, hash, len, str) { hash, len },
8386
#define TRANSLATION(id, length, compressed ...)
@@ -86,6 +89,9 @@ const qstr_attr_t mp_qstr_const_attr[] = {
8689
#undef QDEF
8790
#endif
8891
};
92+
#else
93+
qstr_attr_t mp_qstr_const_attr[MP_QSTRnumber_of];
94+
#endif
8995

9096
const qstr_pool_t mp_qstr_const_pool = {
9197
NULL, // no previous pool
@@ -115,6 +121,16 @@ void qstr_init(void) {
115121
MP_STATE_VM(last_pool) = (qstr_pool_t *)&CONST_POOL; // we won't modify the const_pool since it has no allocated room left
116122
MP_STATE_VM(qstr_last_chunk) = NULL;
117123

124+
#if CIRCUITPY_PRECOMPUTE_QSTR_ATTR == 0
125+
if (mp_qstr_const_attr[MP_QSTR_circuitpython].len == 0) {
126+
for (size_t i = 0; i < mp_qstr_const_pool.len; i++) {
127+
size_t len = strlen(mp_qstr_const_pool.qstrs[i]);
128+
mp_qstr_const_attr[i].hash = qstr_compute_hash((const byte *)mp_qstr_const_pool.qstrs[i], len);
129+
mp_qstr_const_attr[i].len = len;
130+
}
131+
}
132+
#endif
133+
118134
#if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
119135
mp_thread_mutex_init(&MP_STATE_VM(qstr_mutex));
120136
#endif

shared-module/usb_hid/Device.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const usb_hid_device_obj_t usb_hid_device_keyboard_obj = {
8181
.usage = 0x06,
8282
.num_report_ids = 1,
8383
.report_ids = { 0x01, },
84-
.in_report_lengths = { 8, 0, 0, 0, 0, 0, },
84+
.in_report_lengths = { 8, },
8585
.out_report_lengths = { 1, },
8686
};
8787

@@ -131,7 +131,7 @@ const usb_hid_device_obj_t usb_hid_device_mouse_obj = {
131131
.usage = 0x02,
132132
.num_report_ids = 1,
133133
.report_ids = { 0x02, },
134-
.in_report_lengths = { 4, 0, 0, 0, 0, 0, },
134+
.in_report_lengths = { 4, },
135135
.out_report_lengths = { 0, },
136136
};
137137

@@ -160,7 +160,7 @@ const usb_hid_device_obj_t usb_hid_device_consumer_control_obj = {
160160
.usage = 0x01,
161161
.num_report_ids = 1,
162162
.report_ids = { 0x03 },
163-
.in_report_lengths = { 2, 0, 0, 0, 0, 0, },
163+
.in_report_lengths = { 2, },
164164
.out_report_lengths = { 0, },
165165
};
166166

@@ -170,7 +170,7 @@ STATIC size_t get_report_id_idx(usb_hid_device_obj_t *self, size_t report_id) {
170170
return i;
171171
}
172172
}
173-
return MAX_REPORT_IDS_PER_DESCRIPTOR;
173+
return CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR;
174174
}
175175

176176
// See if report_id is used by this device. If it is -1, then return the sole report id used by this device,
@@ -180,16 +180,16 @@ uint8_t common_hal_usb_hid_device_validate_report_id(usb_hid_device_obj_t *self,
180180
return self->report_ids[0];
181181
}
182182
if (!(report_id_arg >= 0 &&
183-
get_report_id_idx(self, (size_t)report_id_arg) < MAX_REPORT_IDS_PER_DESCRIPTOR)) {
183+
get_report_id_idx(self, (size_t)report_id_arg) < CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR)) {
184184
mp_raise_ValueError_varg(translate("Invalid %q"), MP_QSTR_report_id);
185185
}
186186
return (uint8_t)report_id_arg;
187187
}
188188

189189
void common_hal_usb_hid_device_construct(usb_hid_device_obj_t *self, mp_obj_t report_descriptor, uint8_t usage_page, uint8_t usage, size_t num_report_ids, uint8_t *report_ids, uint8_t *in_report_lengths, uint8_t *out_report_lengths) {
190-
if (num_report_ids > MAX_REPORT_IDS_PER_DESCRIPTOR) {
190+
if (num_report_ids > CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR) {
191191
mp_raise_ValueError_varg(translate("More than %d report ids not supported"),
192-
MAX_REPORT_IDS_PER_DESCRIPTOR);
192+
CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR);
193193
}
194194

195195
// report buffer pointers are NULL at start, and are created when USB is initialized.

shared-module/usb_hid/Device.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,16 @@
3232

3333
#include "py/obj.h"
3434

35-
// The most complicated device currently known of is the head and eye tracker, which requires 5
36-
// report ids.
37-
// https://usb.org/sites/default/files/hutrr74_-_usage_page_for_head_and_eye_trackers_0.pdf
38-
#define MAX_REPORT_IDS_PER_DESCRIPTOR (6)
39-
4035
typedef struct {
4136
mp_obj_base_t base;
4237
// Python buffer object whose contents are the descriptor.
4338
const uint8_t *report_descriptor;
44-
uint8_t *in_report_buffers[MAX_REPORT_IDS_PER_DESCRIPTOR];
45-
uint8_t *out_report_buffers[MAX_REPORT_IDS_PER_DESCRIPTOR];
39+
uint8_t *in_report_buffers[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR];
40+
uint8_t *out_report_buffers[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR];
4641
uint16_t report_descriptor_length;
47-
uint8_t report_ids[MAX_REPORT_IDS_PER_DESCRIPTOR];
48-
uint8_t in_report_lengths[MAX_REPORT_IDS_PER_DESCRIPTOR];
49-
uint8_t out_report_lengths[MAX_REPORT_IDS_PER_DESCRIPTOR];
42+
uint8_t report_ids[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR];
43+
uint8_t in_report_lengths[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR];
44+
uint8_t out_report_lengths[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR];
5045
uint8_t usage_page;
5146
uint8_t usage;
5247
uint8_t num_report_ids;

0 commit comments

Comments
 (0)
0