From 60620dca4286fc9ce9ddf4c2f2e6a39c062a5056 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 8 Jan 2021 01:21:03 +0700 Subject: [PATCH 1/3] walkround for samd21 setup_packet overflow increase setup packet size from 8 to 12, since USB DMA controller is suspected to overflow the buffer with 2 extra bytes --- src/portable/microchip/samd/dcd_samd.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/portable/microchip/samd/dcd_samd.c b/src/portable/microchip/samd/dcd_samd.c index f1dca88a..35fac23a 100644 --- a/src/portable/microchip/samd/dcd_samd.c +++ b/src/portable/microchip/samd/dcd_samd.c @@ -37,14 +37,20 @@ /* MACRO TYPEDEF CONSTANT ENUM *------------------------------------------------------------------*/ static TU_ATTR_ALIGNED(4) UsbDeviceDescBank sram_registers[8][2]; -static TU_ATTR_ALIGNED(4) uint8_t _setup_packet[8]; + +// Setup packet is only 8 bytes in length. However under certain scenario, +// SAMD21 USB DMA controller is "suspected" to overwrite/overflow the setup_packet with 2 extra bytes. +// Which corrupt other variable and cause issue such as +// https://github.com/adafruit/circuitpython/issues/3912 (on macOS) +// Therefore we increase it to 12 bytes as walk-around until figuring out the root cause +static TU_ATTR_ALIGNED(4) uint8_t _setup_packet[8+4]; // ready for receiving SETUP packet static inline void prepare_setup(void) { // Only make sure the EP0 OUT buffer is ready sram_registers[0][0].ADDR.reg = (uint32_t) _setup_packet; - sram_registers[0][0].PCKSIZE.bit.MULTI_PACKET_SIZE = sizeof(_setup_packet); + sram_registers[0][0].PCKSIZE.bit.MULTI_PACKET_SIZE = sizeof(tusb_control_request_t); sram_registers[0][0].PCKSIZE.bit.BYTE_COUNT = 0; } From cc5dfd03cd64b1faad7374f6207cb80837134c89 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 8 Jan 2021 11:21:26 +0700 Subject: [PATCH 2/3] update comment and correct the size to 8+2 --- src/portable/microchip/samd/dcd_samd.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/portable/microchip/samd/dcd_samd.c b/src/portable/microchip/samd/dcd_samd.c index 35fac23a..4806342c 100644 --- a/src/portable/microchip/samd/dcd_samd.c +++ b/src/portable/microchip/samd/dcd_samd.c @@ -39,11 +39,14 @@ static TU_ATTR_ALIGNED(4) UsbDeviceDescBank sram_registers[8][2]; // Setup packet is only 8 bytes in length. However under certain scenario, -// SAMD21 USB DMA controller is "suspected" to overwrite/overflow the setup_packet with 2 extra bytes. -// Which corrupt other variable and cause issue such as -// https://github.com/adafruit/circuitpython/issues/3912 (on macOS) -// Therefore we increase it to 12 bytes as walk-around until figuring out the root cause -static TU_ATTR_ALIGNED(4) uint8_t _setup_packet[8+4]; +// USB DMA controller may decide to overwrite/overflow the buffer with +// 2 extra bytes of CRC. From datasheet's "Management of SETUP Transactions" section +// If the number of received data bytes is the maximum data payload specified by +// PCKSIZE.SIZE minus one, only the first CRC data is written to the data buffer. +// If the number of received data is equal or less than the data payload specified +// by PCKSIZE.SIZE minus two, both CRC data bytes are written to the data buffer. +// Therefore we will increase it to 10 bytes just to be safe +static TU_ATTR_ALIGNED(4) uint8_t _setup_packet[8+2]; // ready for receiving SETUP packet static inline void prepare_setup(void) From 3e1d85eed25c384cc9fe6e7e16fbfbdc491c5536 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 8 Jan 2021 11:43:26 +0700 Subject: [PATCH 3/3] comment clean up --- src/portable/microchip/samd/dcd_samd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/portable/microchip/samd/dcd_samd.c b/src/portable/microchip/samd/dcd_samd.c index 4806342c..68a15672 100644 --- a/src/portable/microchip/samd/dcd_samd.c +++ b/src/portable/microchip/samd/dcd_samd.c @@ -45,7 +45,7 @@ static TU_ATTR_ALIGNED(4) UsbDeviceDescBank sram_registers[8][2]; // PCKSIZE.SIZE minus one, only the first CRC data is written to the data buffer. // If the number of received data is equal or less than the data payload specified // by PCKSIZE.SIZE minus two, both CRC data bytes are written to the data buffer. -// Therefore we will increase it to 10 bytes just to be safe +// Therefore we will need to increase it to 10 bytes here. static TU_ATTR_ALIGNED(4) uint8_t _setup_packet[8+2]; // ready for receiving SETUP packet