Skip to content

Commit 24c416c

Browse files
committed
stm32/mboot: Increase USB rx_buf and DFU buf sizes to full 2048 bytes.
The DFU USB config descriptor returns 0x0800=2048 for the supported transfer size, and this applies to both TX (IN) and RX (OUT). So increase the rx_buf to support this size without having a buffer overflow on received data. With this patch mboot in USB DFU mode now works with dfu-util.
1 parent 039f196 commit 24c416c

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

ports/stm32/mboot/main.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,8 @@ uint8_t i2c_slave_process_tx_byte(void) {
619619
/******************************************************************************/
620620
// DFU
621621

622+
#define DFU_XFER_SIZE (2048)
623+
622624
enum {
623625
DFU_DNLOAD = 1,
624626
DFU_UPLOAD = 2,
@@ -649,7 +651,7 @@ typedef struct _dfu_state_t {
649651
uint16_t wBlockNum;
650652
uint16_t wLength;
651653
uint32_t addr;
652-
uint8_t buf[64] __attribute__((aligned(4)));
654+
uint8_t buf[DFU_XFER_SIZE] __attribute__((aligned(4)));
653655
} dfu_state_t;
654656

655657
static dfu_state_t dfu_state;
@@ -762,7 +764,7 @@ static int dfu_handle_tx(int cmd, int arg, int len, uint8_t *buf, int max_len) {
762764
/******************************************************************************/
763765
// USB
764766

765-
#define USB_TX_LEN (2048)
767+
#define USB_XFER_SIZE (DFU_XFER_SIZE)
766768

767769
enum {
768770
USB_PHY_FS_ID = 0,
@@ -776,8 +778,8 @@ typedef struct _pyb_usbdd_obj_t {
776778
uint8_t bRequest;
777779
uint16_t wValue;
778780
uint16_t wLength;
779-
uint8_t rx_buf[64];
780-
uint8_t tx_buf[USB_TX_LEN];
781+
uint8_t rx_buf[USB_XFER_SIZE];
782+
uint8_t tx_buf[USB_XFER_SIZE];
781783
bool tx_pending;
782784

783785
// RAM to hold the current descriptors, which we configure on the fly
@@ -800,7 +802,7 @@ static const uint8_t dev_descr[0x12] = "\x12\x01\x00\x01\x00\x00\x00\x40\x83\x04
800802
static uint8_t cfg_descr[9 + 9 + 9] =
801803
"\x09\x02\x1b\x00\x01\x01\x00\xc0\x32"
802804
"\x09\x04\x00\x00\x00\xfe\x01\x02\x04"
803-
"\x09\x21\x0b\xff\x00\x00\x08\x1a\x01" // \x00\x08 goes with tx_buf[USB_TX_LEN]
805+
"\x09\x21\x0b\xff\x00\x00\x08\x1a\x01" // \x00\x08 goes with USB_XFER_SIZE
804806
;
805807

806808
static uint8_t *pyb_usbdd_DeviceDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) {
@@ -908,7 +910,7 @@ static uint8_t pyb_usbdd_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *r
908910
}
909911
} else if (req->bmRequest == 0xa1) {
910912
// device-to-host request
911-
int len = dfu_handle_tx(self->bRequest, self->wValue, self->wLength, self->tx_buf, USB_TX_LEN);
913+
int len = dfu_handle_tx(self->bRequest, self->wValue, self->wLength, self->tx_buf, USB_XFER_SIZE);
912914
if (len >= 0) {
913915
self->tx_pending = true;
914916
USBD_CtlSendData(&self->hUSBDDevice, self->tx_buf, len);

0 commit comments

Comments
 (0)