From e45a61f57e505d80f4828f568ebb78239851487e Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 27 Jul 2023 09:15:58 -0700 Subject: [PATCH] Align fatfs window buffer for tinyusb Also, update tinyusb. --- lib/oofatfs/ff.h | 2 +- lib/oofatfs/ffconf.h | 6 ++++++ lib/tinyusb | 2 +- locale/circuitpython.pot | 9 +++++++-- ports/raspberrypi/lib/Pico-PIO-USB | 2 +- py/circuitpy_mpconfig.h | 6 ++++++ shared-module/usb/core/Device.c | 21 +++++++++++++++++---- 7 files changed, 39 insertions(+), 9 deletions(-) diff --git a/lib/oofatfs/ff.h b/lib/oofatfs/ff.h index b133d770db..d77ddf5761 100644 --- a/lib/oofatfs/ff.h +++ b/lib/oofatfs/ff.h @@ -162,7 +162,7 @@ typedef struct { DWORD bitbase; /* Allocation bitmap base sector */ #endif DWORD winsect; /* Current sector appearing in the win[] */ - BYTE win[FF_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */ + __attribute__((aligned(FF_WINDOW_ALIGNMENT),)) BYTE win[FF_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg). */ } FATFS; diff --git a/lib/oofatfs/ffconf.h b/lib/oofatfs/ffconf.h index 52ff6d0a7a..2967244118 100644 --- a/lib/oofatfs/ffconf.h +++ b/lib/oofatfs/ffconf.h @@ -267,6 +267,12 @@ / for variable sector size mode and disk_ioctl() function needs to implement / GET_SECTOR_SIZE command. */ +#ifdef MICROPY_FATFS_WINDOW_ALIGNMENT +#define FF_WINDOW_ALIGNMENT (MICROPY_FATFS_WINDOW_ALIGNMENT) +#else +#define FF_WINDOW_ALIGNMENT 1 +#endif + #define FF_USE_TRIM 0 /* This option switches support for ATA-TRIM. (0:Disable or 1:Enable) diff --git a/lib/tinyusb b/lib/tinyusb index 6c7c9f2ef5..db59494b1b 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit 6c7c9f2ef5a80d5a6879e9c3558162188c6cf889 +Subproject commit db59494b1b24f7dad26c5c66c85a195a2cf09466 diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 9ebb85d13f..892b1368c4 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -1748,6 +1748,10 @@ msgstr "" msgid "Pins must share PWM slice" msgstr "" +#: shared-module/usb/core/Device.c +msgid "Pipe error" +msgstr "" + #: py/builtinhelp.c msgid "Plus any modules on the filesystem\n" msgstr "" @@ -1825,7 +1829,8 @@ msgstr "" #: shared-bindings/_bleio/__init__.c #: shared-bindings/memorymonitor/AllocationSize.c -#: shared-bindings/pulseio/PulseIn.c shared-module/displayio/Bitmap.c +#: shared-bindings/pulseio/PulseIn.c shared-module/bitmaptools/__init__.c +#: shared-module/displayio/Bitmap.c msgid "Read-only" msgstr "" @@ -3985,7 +3990,7 @@ msgstr "" msgid "sosfilt requires iterable arguments" msgstr "" -#: shared-bindings/bitmaptools/__init__.c shared-bindings/displayio/Bitmap.c +#: shared-bindings/bitmaptools/__init__.c msgid "source palette too large" msgstr "" diff --git a/ports/raspberrypi/lib/Pico-PIO-USB b/ports/raspberrypi/lib/Pico-PIO-USB index 5a7aa8d4e7..d00a10a8c4 160000 --- a/ports/raspberrypi/lib/Pico-PIO-USB +++ b/ports/raspberrypi/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit 5a7aa8d4e78f9a50e4fb9defcf3488b3fc44aff1 +Subproject commit d00a10a8c425d0d40f81b87169102944b01f3bb3 diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index e1909a064f..7e858212a5 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -611,6 +611,12 @@ void background_callback_run_all(void); #define CIRCUITPY_DIGITALIO_HAVE_INVALID_DRIVE_MODE (0) #endif +// Align the internal sector buffer. Useful when it is passed into TinyUSB for +// loads. +#ifndef MICROPY_FATFS_WINDOW_ALIGNMENT +#define MICROPY_FATFS_WINDOW_ALIGNMENT CIRCUITPY_TUSB_MEM_ALIGN +#endif + #define FF_FS_CASE_INSENSITIVE_COMPARISON_ASCII_ONLY (1) #define FF_FS_MAKE_VOLID (1) diff --git a/shared-module/usb/core/Device.c b/shared-module/usb/core/Device.c index 67756341ec..faed49e383 100644 --- a/shared-module/usb/core/Device.c +++ b/shared-module/usb/core/Device.c @@ -49,6 +49,7 @@ void tuh_umount_cb(uint8_t dev_addr) { } STATIC xfer_result_t _xfer_result; +STATIC size_t _actual_len; bool common_hal_usb_core_device_construct(usb_core_device_obj_t *self, uint8_t device_number) { if (device_number == 0 || device_number > CFG_TUH_DEVICE_MAX + CFG_TUH_HUB) { return false; @@ -78,6 +79,9 @@ uint16_t common_hal_usb_core_device_get_idProduct(usb_core_device_obj_t *self) { STATIC void _transfer_done_cb(tuh_xfer_t *xfer) { // Store the result so we stop waiting for the transfer. _xfer_result = xfer->result; + // The passed in xfer is not the original one we passed in, so we need to + // copy any info out that we want (like actual_len.) + _actual_len = xfer->actual_len; } STATIC bool _wait_for_callback(void) { @@ -159,11 +163,14 @@ STATIC size_t _xfer(tuh_xfer_t *xfer, mp_int_t timeout) { } xfer_result_t result = _xfer_result; _xfer_result = 0xff; - if (result == XFER_RESULT_STALLED || result == 0xff) { + if (result == XFER_RESULT_STALLED) { + mp_raise_usb_core_USBError(translate("Pipe error")); + } + if (result == 0xff) { mp_raise_usb_core_USBTimeoutError(); } if (result == XFER_RESULT_SUCCESS) { - return xfer->actual_len; + return _actual_len; } return 0; @@ -192,7 +199,10 @@ STATIC bool _open_endpoint(usb_core_device_obj_t *self, mp_int_t endpoint) { } tusb_desc_configuration_t *desc_cfg = (tusb_desc_configuration_t *)desc_buf; - uint8_t const *desc_end = ((uint8_t const *)desc_cfg) + tu_le16toh(desc_cfg->wTotalLength); + uint32_t total_length = tu_le16toh(desc_cfg->wTotalLength); + // Cap to the buffer size we requested. + total_length = MIN(total_length, sizeof(desc_buf)); + uint8_t const *desc_end = ((uint8_t const *)desc_cfg) + total_length; uint8_t const *p_desc = tu_desc_next(desc_cfg); // parse each interfaces @@ -281,7 +291,10 @@ mp_int_t common_hal_usb_core_device_ctrl_transfer(usb_core_device_obj_t *self, } xfer_result_t result = _xfer_result; _xfer_result = 0xff; - if (result == XFER_RESULT_STALLED || result == 0xff) { + if (result == XFER_RESULT_STALLED) { + mp_raise_usb_core_USBError(translate("Pipe error")); + } + if (result == 0xff) { mp_raise_usb_core_USBTimeoutError(); } if (result == XFER_RESULT_SUCCESS) {