From d28932934482cd5df5871d8276adc1cc812e9fc4 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 28 Mar 2023 11:02:17 -0400 Subject: [PATCH 1/9] Handle HID OUT reports with no report ID --- shared-module/usb_hid/Device.c | 44 +++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/shared-module/usb_hid/Device.c b/shared-module/usb_hid/Device.c index 680bb83893..6f530bfef8 100644 --- a/shared-module/usb_hid/Device.c +++ b/shared-module/usb_hid/Device.c @@ -293,24 +293,40 @@ uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t // Callback invoked when we receive Set_Report request through control endpoint void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t const *buffer, uint16_t bufsize) { (void)itf; - if (report_type == HID_REPORT_TYPE_INVALID) { - report_id = buffer[0]; - buffer++; - bufsize--; + + usb_hid_device_obj_t *hid_device = NULL; + size_t id_idx; + + if (report_id == 0 && report_type == HID_REPORT_TYPE_INVALID) { + // This could be a report with a non-zero report ID in the first byte, or + // it could be for report ID 0. + // Heuristic: see if there's a device with report ID 0, and if its report length matches + // the size of the incoming buffer. In that case, assume the first byte is not the report ID, + // but is data. Otherwise use the first byte as the report id. + if (usb_hid_get_device_with_report_id(0, &hid_device, &id_idx) && + hid_device && + hid_device->out_report_buffers[id_idx] && + hid_device->out_report_lengths[id_idx] == bufsize) { + // Use as is, with report_id 0. + } else { + // No matching report ID 0, so use the first byte as the report ID. + report_id = buffer[0]; + buffer++; + bufsize--; + } } else if (report_type != HID_REPORT_TYPE_OUTPUT && report_type != HID_REPORT_TYPE_FEATURE) { return; } - usb_hid_device_obj_t *hid_device; - size_t id_idx; - // Find device with this report id, and get the report id index. - if (usb_hid_get_device_with_report_id(report_id, &hid_device, &id_idx)) { + // report_id might be changed due to parsing above, so test again. + if ((report_id == 0 && report_type == HID_REPORT_TYPE_INVALID) || + // Fetch the matching device if we don't already have the report_id 0 device. + (usb_hid_get_device_with_report_id(report_id, &hid_device, &id_idx) && + hid_device && + hid_device->out_report_buffers[id_idx] && + hid_device->out_report_lengths[id_idx] == bufsize)) { // If a report of the correct size has been read, save it in the proper OUT report buffer. - if (hid_device && - hid_device->out_report_buffers[id_idx] && - hid_device->out_report_lengths[id_idx] >= bufsize) { - memcpy(hid_device->out_report_buffers[id_idx], buffer, bufsize); - hid_device->out_report_buffers_updated[id_idx] = true; - } + memcpy(hid_device->out_report_buffers[id_idx], buffer, bufsize); + hid_device->out_report_buffers_updated[id_idx] = true; } } From 11082435f1d6c916d6274e77e842c0d10cbbff6a Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 28 Mar 2023 16:05:17 -0400 Subject: [PATCH 2/9] shrink SAMD21 builds by a few hundred bytes --- ports/atmel-samd/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index 343dcf7d63..31e69f36c9 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -116,12 +116,12 @@ else # -finline-limit=80 or so is similar to not having it on. # There is no simple default value, though. ifeq ($(SHRINK_BUILD), 1) - CFLAGS += -finline-limit=45 + CFLAGS += -finline-limit=45 --param max-inline-insns-auto=110 endif # We used to do this but it seems to not reduce space any more, at least in gcc 11. # Leave it here, commented out, just for reference. - # --param inline-unit-growth=15 --param max-inline-insns-auto=20 + # --param inline-unit-growth=15 ifdef CFLAGS_BOARD CFLAGS += $(CFLAGS_BOARD) From b2535496b08849d93cec582d5894a748b60cbb96 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 28 Mar 2023 16:28:31 -0400 Subject: [PATCH 3/9] use max-inline-insns-auto=110 only on SAMD21 --- ports/atmel-samd/Makefile | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index 31e69f36c9..ca906ab98f 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -104,19 +104,16 @@ else CFLAGS += -DNDEBUG # Do a default shrink for small builds, including all SAMD21 builds. - ifeq ($(CIRCUITPY_FULL_BUILD),0) - SHRINK_BUILD = 1 - else - ifeq ($(CHIP_FAMILY), samd21) - SHRINK_BUILD = 1 - endif - endif - # -finline-limit can shrink the image size. # -finline-limit=80 or so is similar to not having it on. # There is no simple default value, though. - ifeq ($(SHRINK_BUILD), 1) - CFLAGS += -finline-limit=45 --param max-inline-insns-auto=110 + ifeq ($(CIRCUITPY_FULL_BUILD),0) + CFLAGS += -finline-limit=45 + else + ifeq ($(CHIP_FAMILY), samd21) + # max-inline-insns-auto increases the size of SAMD51 builds. + CFLAGS += -finline-limit=45 --param max-inline-insns-auto=110 + endif endif # We used to do this but it seems to not reduce space any more, at least in gcc 11. From cc3d0f6fa15bf3532ad99f289db29555a9e37858 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 29 Mar 2023 10:03:56 -0500 Subject: [PATCH 4/9] getenv: treat a read error like eof Otherwise, the following would occur: * settings.toml is in the process of being written by host computer * soft-reset begins * web workflow tries to grab CIRCUITPY_WIFI_SSID, but loops forever because FAT filesystem is in inconsistent state and file reads error * settings.toml write by host computer never completes and the filesystem remains corrupt * restarting yields a soft-bricked device, because startup reads CIRCUITPY_WIFI_SSID again --- shared-module/os/getenv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-module/os/getenv.c b/shared-module/os/getenv.c index 870973c5d2..6585aeb88c 100644 --- a/shared-module/os/getenv.c +++ b/shared-module/os/getenv.c @@ -72,7 +72,7 @@ STATIC void close_file(file_arg *active_file) { // nothing } STATIC bool is_eof(file_arg *active_file) { - return f_eof(active_file); + return f_eof(active_file) | f_error(active_file); } // Return 0 if there is no next character (EOF). From a8bbb21eeb5bcdab5eb71be3793c6efd3dd0622d Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 29 Mar 2023 10:09:01 -0500 Subject: [PATCH 5/9] Use short-circuiting or (also may save some code size) Co-authored-by: Dan Halbert --- shared-module/os/getenv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-module/os/getenv.c b/shared-module/os/getenv.c index 6585aeb88c..76cbf48dae 100644 --- a/shared-module/os/getenv.c +++ b/shared-module/os/getenv.c @@ -72,7 +72,7 @@ STATIC void close_file(file_arg *active_file) { // nothing } STATIC bool is_eof(file_arg *active_file) { - return f_eof(active_file) | f_error(active_file); + return f_eof(active_file) || f_error(active_file); } // Return 0 if there is no next character (EOF). From 173bc198c604aa9d3475bed79acdaaf57f47f5dc Mon Sep 17 00:00:00 2001 From: Gregory Neverov <42853258+gneverov@users.noreply.github.com> Date: Wed, 29 Mar 2023 11:49:32 -0700 Subject: [PATCH 6/9] don't set tcp_err callback for listen sockets --- ports/raspberrypi/common-hal/socketpool/Socket.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ports/raspberrypi/common-hal/socketpool/Socket.c b/ports/raspberrypi/common-hal/socketpool/Socket.c index fe12f461fb..0696cc7ae9 100644 --- a/ports/raspberrypi/common-hal/socketpool/Socket.c +++ b/ports/raspberrypi/common-hal/socketpool/Socket.c @@ -706,8 +706,6 @@ bool socketpool_socket(socketpool_socketpool_obj_t *self, case MOD_NETWORK_SOCK_STREAM: { // Register the socket object as our callback argument. tcp_arg(socket->pcb.tcp, (void *)socket); - // Register our error callback. - tcp_err(socket->pcb.tcp, _lwip_tcp_error); break; } case MOD_NETWORK_SOCK_DGRAM: { @@ -916,10 +914,11 @@ void socketpool_socket_close(socketpool_socket_obj_t *socket) { case SOCKETPOOL_SOCK_STREAM: { // Deregister callback (pcb.tcp is set to NULL below so must deregister now) tcp_arg(socket->pcb.tcp, NULL); - tcp_err(socket->pcb.tcp, NULL); - tcp_recv(socket->pcb.tcp, NULL); if (socket->pcb.tcp->state != LISTEN) { + tcp_err(socket->pcb.tcp, NULL); + tcp_recv(socket->pcb.tcp, NULL); + // Schedule a callback to abort the connection if it's not cleanly closed after // the given timeout. The callback must be set before calling tcp_close since // the latter may free the pcb; if it doesn't then the callback will be active. @@ -977,6 +976,7 @@ void common_hal_socketpool_socket_connect(socketpool_socket_obj_t *socket, // Register our receive callback. MICROPY_PY_LWIP_ENTER tcp_recv(socket->pcb.tcp, _lwip_tcp_recv); + tcp_err(socket->pcb.tcp, _lwip_tcp_error); socket->state = STATE_CONNECTING; err = tcp_connect(socket->pcb.tcp, &dest, port, _lwip_tcp_connected); if (err != ERR_OK) { From 1a5afd485bfa5aa5269a25d0ab40ab47413cf3f6 Mon Sep 17 00:00:00 2001 From: Ted Hess Date: Wed, 29 Mar 2023 15:47:17 -0400 Subject: [PATCH 7/9] Allow web_workflow to startup after deep sleep alarm wakeup --- supervisor/shared/web_workflow/web_workflow.c | 1 + 1 file changed, 1 insertion(+) diff --git a/supervisor/shared/web_workflow/web_workflow.c b/supervisor/shared/web_workflow/web_workflow.c index 2caacbd39b..75ab78bf03 100644 --- a/supervisor/shared/web_workflow/web_workflow.c +++ b/supervisor/shared/web_workflow/web_workflow.c @@ -261,6 +261,7 @@ void supervisor_start_web_workflow(void) { const mcu_reset_reason_t reset_reason = common_hal_mcu_processor_get_reset_reason(); if (reset_reason != RESET_REASON_POWER_ON && reset_reason != RESET_REASON_RESET_PIN && + reset_reason != RESET_REASON_DEEP_SLEEP_ALARM && reset_reason != RESET_REASON_UNKNOWN && reset_reason != RESET_REASON_SOFTWARE) { return; From b746fd05baad4daf776366d7cf9e833c159cd075 Mon Sep 17 00:00:00 2001 From: Bill Sideris Date: Wed, 29 Mar 2023 23:58:54 +0300 Subject: [PATCH 8/9] 24Mhz -> 240Mhz --- ports/espressif/common-hal/microcontroller/Processor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/espressif/common-hal/microcontroller/Processor.c b/ports/espressif/common-hal/microcontroller/Processor.c index 1f09742955..c0e72c85b9 100644 --- a/ports/espressif/common-hal/microcontroller/Processor.c +++ b/ports/espressif/common-hal/microcontroller/Processor.c @@ -64,7 +64,7 @@ float common_hal_mcu_processor_get_voltage(void) { uint32_t common_hal_mcu_processor_get_frequency(void) { #if defined(CONFIG_IDF_TARGET_ESP32) - return CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ * 100000; + return CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ * 1000000; #elif defined(CONFIG_IDF_TARGET_ESP32C3) return CONFIG_ESP32C3_DEFAULT_CPU_FREQ_MHZ * 1000000; #elif defined(CONFIG_IDF_TARGET_ESP32S2) From a10ce2170c26e532080f99ecaeb753267c8bb912 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 29 Mar 2023 17:16:39 -0400 Subject: [PATCH 9/9] doc typo fix in OnDiskGif --- shared-bindings/gifio/OnDiskGif.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-bindings/gifio/OnDiskGif.c b/shared-bindings/gifio/OnDiskGif.c index a16cb92b4e..f6acccfabf 100644 --- a/shared-bindings/gifio/OnDiskGif.c +++ b/shared-bindings/gifio/OnDiskGif.c @@ -97,7 +97,7 @@ //| //| display_bus.send(42, struct.pack(">hh", 0, odg.bitmap.width - 1)) //| display_bus.send(43, struct.pack(">hh", 0, odg.bitmap.height - 1)) -//| display_bus.send(44, d.bitmap) +//| display_bus.send(44, odg.bitmap) //| //| # The following optional code will free the OnDiskGif and allocated resources //| # after use. This may be required before loading a new GIF in situations