Skip to content

Commit f466a43

Browse files
authored
Merge pull request #8222 from tannewt/fat_window_align
Align fatfs window buffer for tinyusb
2 parents 5ccf36d + e45a61f commit f466a43

File tree

7 files changed

+39
-9
lines changed

7 files changed

+39
-9
lines changed

lib/oofatfs/ff.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ typedef struct {
162162
DWORD bitbase; /* Allocation bitmap base sector */
163163
#endif
164164
DWORD winsect; /* Current sector appearing in the win[] */
165-
BYTE win[FF_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */
165+
__attribute__((aligned(FF_WINDOW_ALIGNMENT),)) BYTE win[FF_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg). */
166166
} FATFS;
167167

168168

lib/oofatfs/ffconf.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@
267267
/ for variable sector size mode and disk_ioctl() function needs to implement
268268
/ GET_SECTOR_SIZE command. */
269269

270+
#ifdef MICROPY_FATFS_WINDOW_ALIGNMENT
271+
#define FF_WINDOW_ALIGNMENT (MICROPY_FATFS_WINDOW_ALIGNMENT)
272+
#else
273+
#define FF_WINDOW_ALIGNMENT 1
274+
#endif
275+
270276

271277
#define FF_USE_TRIM 0
272278
/* This option switches support for ATA-TRIM. (0:Disable or 1:Enable)

lib/tinyusb

Submodule tinyusb updated 63 files

locale/circuitpython.pot

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,10 @@ msgstr ""
17481748
msgid "Pins must share PWM slice"
17491749
msgstr ""
17501750

1751+
#: shared-module/usb/core/Device.c
1752+
msgid "Pipe error"
1753+
msgstr ""
1754+
17511755
#: py/builtinhelp.c
17521756
msgid "Plus any modules on the filesystem\n"
17531757
msgstr ""
@@ -1825,7 +1829,8 @@ msgstr ""
18251829

18261830
#: shared-bindings/_bleio/__init__.c
18271831
#: shared-bindings/memorymonitor/AllocationSize.c
1828-
#: shared-bindings/pulseio/PulseIn.c shared-module/displayio/Bitmap.c
1832+
#: shared-bindings/pulseio/PulseIn.c shared-module/bitmaptools/__init__.c
1833+
#: shared-module/displayio/Bitmap.c
18291834
msgid "Read-only"
18301835
msgstr ""
18311836

@@ -4021,7 +4026,7 @@ msgstr ""
40214026
msgid "sosfilt requires iterable arguments"
40224027
msgstr ""
40234028

4024-
#: shared-bindings/bitmaptools/__init__.c shared-bindings/displayio/Bitmap.c
4029+
#: shared-bindings/bitmaptools/__init__.c
40254030
msgid "source palette too large"
40264031
msgstr ""
40274032

py/circuitpy_mpconfig.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,12 @@ void background_callback_run_all(void);
611611
#define CIRCUITPY_DIGITALIO_HAVE_INVALID_DRIVE_MODE (0)
612612
#endif
613613

614+
// Align the internal sector buffer. Useful when it is passed into TinyUSB for
615+
// loads.
616+
#ifndef MICROPY_FATFS_WINDOW_ALIGNMENT
617+
#define MICROPY_FATFS_WINDOW_ALIGNMENT CIRCUITPY_TUSB_MEM_ALIGN
618+
#endif
619+
614620
#define FF_FS_CASE_INSENSITIVE_COMPARISON_ASCII_ONLY (1)
615621

616622
#define FF_FS_MAKE_VOLID (1)

shared-module/usb/core/Device.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void tuh_umount_cb(uint8_t dev_addr) {
4949
}
5050

5151
STATIC xfer_result_t _xfer_result;
52+
STATIC size_t _actual_len;
5253
bool common_hal_usb_core_device_construct(usb_core_device_obj_t *self, uint8_t device_number) {
5354
if (device_number == 0 || device_number > CFG_TUH_DEVICE_MAX + CFG_TUH_HUB) {
5455
return false;
@@ -78,6 +79,9 @@ uint16_t common_hal_usb_core_device_get_idProduct(usb_core_device_obj_t *self) {
7879
STATIC void _transfer_done_cb(tuh_xfer_t *xfer) {
7980
// Store the result so we stop waiting for the transfer.
8081
_xfer_result = xfer->result;
82+
// The passed in xfer is not the original one we passed in, so we need to
83+
// copy any info out that we want (like actual_len.)
84+
_actual_len = xfer->actual_len;
8185
}
8286

8387
STATIC bool _wait_for_callback(void) {
@@ -159,11 +163,14 @@ STATIC size_t _xfer(tuh_xfer_t *xfer, mp_int_t timeout) {
159163
}
160164
xfer_result_t result = _xfer_result;
161165
_xfer_result = 0xff;
162-
if (result == XFER_RESULT_STALLED || result == 0xff) {
166+
if (result == XFER_RESULT_STALLED) {
167+
mp_raise_usb_core_USBError(translate("Pipe error"));
168+
}
169+
if (result == 0xff) {
163170
mp_raise_usb_core_USBTimeoutError();
164171
}
165172
if (result == XFER_RESULT_SUCCESS) {
166-
return xfer->actual_len;
173+
return _actual_len;
167174
}
168175

169176
return 0;
@@ -192,7 +199,10 @@ STATIC bool _open_endpoint(usb_core_device_obj_t *self, mp_int_t endpoint) {
192199
}
193200
tusb_desc_configuration_t *desc_cfg = (tusb_desc_configuration_t *)desc_buf;
194201

195-
uint8_t const *desc_end = ((uint8_t const *)desc_cfg) + tu_le16toh(desc_cfg->wTotalLength);
202+
uint32_t total_length = tu_le16toh(desc_cfg->wTotalLength);
203+
// Cap to the buffer size we requested.
204+
total_length = MIN(total_length, sizeof(desc_buf));
205+
uint8_t const *desc_end = ((uint8_t const *)desc_cfg) + total_length;
196206
uint8_t const *p_desc = tu_desc_next(desc_cfg);
197207

198208
// parse each interfaces
@@ -281,7 +291,10 @@ mp_int_t common_hal_usb_core_device_ctrl_transfer(usb_core_device_obj_t *self,
281291
}
282292
xfer_result_t result = _xfer_result;
283293
_xfer_result = 0xff;
284-
if (result == XFER_RESULT_STALLED || result == 0xff) {
294+
if (result == XFER_RESULT_STALLED) {
295+
mp_raise_usb_core_USBError(translate("Pipe error"));
296+
}
297+
if (result == 0xff) {
285298
mp_raise_usb_core_USBTimeoutError();
286299
}
287300
if (result == XFER_RESULT_SUCCESS) {

0 commit comments

Comments
 (0)