diff --git a/boards/STM32F722RE.json b/boards/STM32F722RE.json index b0d402e61..2901925d8 100644 --- a/boards/STM32F722RE.json +++ b/boards/STM32F722RE.json @@ -2,7 +2,7 @@ "build": { "core": "stm32", "cpu": "cortex-m7", - "extra_flags": "-DSTM32F722xx -DSTM32F722 -DSTM32F7 -DSTM32 -DMCU_NAME=stm32f722 -DHSE_VALUE=8000000U", + "extra_flags": "-DSTM32F722xx -DSTM32F722 -DSTM32F7xx -DSTM32F7 -DSTM32 -DMCU_NAME=stm32f722 -DHSE_VALUE=8000000U", "f_cpu": "216000000L", "mcu": "stm32f722", "product_line": "STM32F722xx", diff --git a/boards/STM32F745XG.json b/boards/STM32F745XG.json index c00d1276b..e10bb8f03 100644 --- a/boards/STM32F745XG.json +++ b/boards/STM32F745XG.json @@ -2,7 +2,7 @@ "build": { "core": "stm32", "cpu": "cortex-m7", - "extra_flags": "-DSTM32F745xx -DSTM32F745 -DSTM32F7 -DSTM32 -DMCU_NAME=stm32f745 -DHSE_VALUE=8000000U", + "extra_flags": "-DSTM32F745xx -DSTM32F745 -DSTM32F7xx -DSTM32F7 -DSTM32 -DMCU_NAME=stm32f745 -DHSE_VALUE=8000000U", "f_cpu": "216000000L", "mcu": "stm32f745", "product_line": "STM32F745xx", diff --git a/boards/STM32F765VI.json b/boards/STM32F765VI.json index cba9f322f..41e47de1e 100644 --- a/boards/STM32F765VI.json +++ b/boards/STM32F765VI.json @@ -2,7 +2,7 @@ "build": { "core": "stm32", "cpu": "cortex-m7", - "extra_flags": "-DSTM32F765xx -DSTM32F765 -DSTM32F7 -DSTM32 -DMCU_NAME=stm32f765 -DHSE_VALUE=8000000U", + "extra_flags": "-DSTM32F765xx -DSTM32F765 -DSTM32F7xx -DSTM32F7 -DSTM32 -DMCU_NAME=stm32f765 -DHSE_VALUE=8000000U", "f_cpu": "216000000L", "mcu": "stm32f765", "product_line": "STM32F765xx", diff --git a/boards/STM32H743VI.json b/boards/STM32H743VI.json index acc67884c..29d09c3e4 100644 --- a/boards/STM32H743VI.json +++ b/boards/STM32H743VI.json @@ -2,7 +2,7 @@ "build": { "core": "stm32", "cpu": "cortex-m7", - "extra_flags": "-DSTM32H743xx -DSTM32H743 -DSTM32H7 -DSTM32 -DMCU_NAME=stm32h743 -DUSE_FAST_RAM -DUSE_DMA_RAM -DHSE_VALUE=8000000U", + "extra_flags": "-DSTM32H743xx -DSTM32H743 -DSTM32H7xx -DSTM32H7 -DSTM32 -DMCU_NAME=stm32h743 -DUSE_FAST_RAM -DUSE_DMA_RAM -DHSE_VALUE=8000000U", "f_cpu": "480000000L", "mcu": "stm32h743", "product_line": "STM32H743xx", diff --git a/lib/libusb_stm32/demo/cdc_loop.c b/lib/libusb_stm32/demo/cdc_loop.c index 986a7ba85..788d5b969 100644 --- a/lib/libusb_stm32/demo/cdc_loop.c +++ b/lib/libusb_stm32/demo/cdc_loop.c @@ -358,9 +358,24 @@ static void cdc_rxonly (usbd_device *dev, uint8_t event, uint8_t ep) { } static void cdc_txonly(usbd_device *dev, uint8_t event, uint8_t ep) { - uint8_t _t = dev->driver->frame_no(); - memset(fifo, _t, CDC_DATA_SZ); - usbd_ep_write(dev, ep, fifo, CDC_DATA_SZ); + static uint8_t psize = 0x00U; + static uint8_t remained = 0x00U; + static uint8_t lastsym = 0x00U; + + uint8_t _t = (remained < CDC_DATA_SZ) ? remained : CDC_DATA_SZ; + // fill buffer by sequental data + for (size_t i = 0; i < _t; ++i) { + fifo[i] = lastsym++; + } + usbd_ep_write(dev, ep, fifo, _t); + + if (remained < CDC_DATA_SZ) { + // bulk xfer completed. increase bulk size + remained = ++psize; + } else { + // continue to send remained data or ZLP + remained -= _t; + } } static void cdc_rxtx(usbd_device *dev, uint8_t event, uint8_t ep) { diff --git a/lib/libusb_stm32/demo/cdc_startup.c b/lib/libusb_stm32/demo/cdc_startup.c index 612b64f7c..42b02c350 100644 --- a/lib/libusb_stm32/demo/cdc_startup.c +++ b/lib/libusb_stm32/demo/cdc_startup.c @@ -104,7 +104,7 @@ static void cdc_init_rcc (void) { /* switch to PLL */ _BMD(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_PLL); _WVL(RCC->CFGR, RCC_CFGR_SWS, RCC_CFGR_SWS_PLL); - + _BST(RCC->AHBENR, RCC_AHBENR_GPIOAEN); _BST(GPIOA->AFR[1], (0x0E << 12) | (0x0E << 16)); _BMD(GPIOA->MODER, (0x03 << 22) | (0x03 << 24), (0x02 << 22) | (0x02 << 24)); @@ -141,8 +141,12 @@ static void cdc_init_rcc (void) { /* enabling PLL */ _BST(RCC->CR, RCC_CR_PLLON); _WBS(RCC->CR, RCC_CR_PLLRDY); - /* switching to PLL */ - _BMD(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_PLL); + /* Setup CFGR to PLL*/ + /* APB1 | APB2 */ + /* STM32F411 <50Mhz | <100MHz */ + /* STM32F429 <45MHz | <90MHz */ + /* STM32F405, STM32F401 <42MHz | <84MHz */ + _BMD(RCC->CFGR, RCC_CFGR_SW | RCC_CFGR_PPRE1, RCC_CFGR_SW_PLL | RCC_CFGR_PPRE1_DIV2); _WVL(RCC->CFGR, RCC_CFGR_SWS, RCC_CFGR_SWS_PLL); #if defined(USBD_PRIMARY_OTGHS) /* enabling GPIOB and setting PB13, PB14 and PB15 to AF11 (USB_OTG2FS) */ diff --git a/lib/libusb_stm32/inc/hid_usage_keyboard.h b/lib/libusb_stm32/inc/hid_usage_keyboard.h index 90872e3f9..2cd0f9715 100644 --- a/lib/libusb_stm32/inc/hid_usage_keyboard.h +++ b/lib/libusb_stm32/inc/hid_usage_keyboard.h @@ -78,7 +78,7 @@ #define HID_KEYBOARD_SEMICOLON 0x33 #define HID_KEYBOARD_APOSTROPHE 0x34 #define HID_KEYBOARD_GRAVE_ACCENT 0x35 -#define HID_KEYBOARD_COLON 0x36 +#define HID_KEYBOARD_COMMA 0x36 #define HID_KEYBOARD_DOT 0x37 #define HID_KEYBOARD_SLASH 0x38 #define HID_KEYBOARD_CAPS_LOCK 0x39 diff --git a/lib/libusb_stm32/inc/hid_usage_simulation.h b/lib/libusb_stm32/inc/hid_usage_simulation.h index 7075183fa..84934edbf 100644 --- a/lib/libusb_stm32/inc/hid_usage_simulation.h +++ b/lib/libusb_stm32/inc/hid_usage_simulation.h @@ -14,7 +14,7 @@ */ #ifndef _USB_HID_USAGE_SIMUL_H_ -#define _USB_HID_USAHE_SUMUL_H_ +#define _USB_HID_USAGE_SIMUL_H_ #ifdef __cplusplus extern "C" { #endif diff --git a/lib/libusb_stm32/inc/stm32_compat.h b/lib/libusb_stm32/inc/stm32_compat.h index c2415432c..156456781 100644 --- a/lib/libusb_stm32/inc/stm32_compat.h +++ b/lib/libusb_stm32/inc/stm32_compat.h @@ -21,33 +21,33 @@ /* bit value */ #define _BV(bit) (0x01 << (bit)) -#if defined(STM32F0) +#if defined(STM32F0xx) #include -#elif defined(STM32F1) +#elif defined(STM32F1xx) #include -#elif defined(STM32F2) +#elif defined(STM32F2xx) #include -#elif defined(STM32F3) +#elif defined(STM32F3xx) #include -#elif defined(STM32F4) +#elif defined(STM32F4xx) #include -#elif defined(STM32F7) +#elif defined(STM32F7xx) #include -#elif defined(STM32H7) +#elif defined(STM32H7xx) #include -#elif defined(STM32L0) +#elif defined(STM32L0xx) #include -#elif defined(STM32L1) +#elif defined(STM32L1xx) #include -#elif defined(STM32L4) +#elif defined(STM32L4xx) #include -#elif defined(STM32L5) +#elif defined(STM32L5xx) #include -#elif defined(STM32G0) +#elif defined(STM32G0xx) #include -#elif defined(STM32G4) +#elif defined(STM32G4xx) #include -#elif defined(STM32WB) +#elif defined(STM32WBxx) #include #else #error "STM32 family not defined" diff --git a/lib/libusb_stm32/inc/usbd_core.h b/lib/libusb_stm32/inc/usbd_core.h index 587509f7d..bc30b86ac 100644 --- a/lib/libusb_stm32/inc/usbd_core.h +++ b/lib/libusb_stm32/inc/usbd_core.h @@ -269,6 +269,11 @@ typedef int32_t (*usbd_hw_ep_read)(uint8_t ep, void *buf, uint16_t blen); * \param buf pointer to data buffer * \param blen size of data will be written * \return number of written bytes + * + * The \ref usbd_evt_eptx event will fire for the endpoint once the + * write is \e complete. A trick commonly used is to enqueue a zero-length + * write as soon as the endpoint is configured, so that it would + * "complete", firing an event, the next time an IN request arrives. */ typedef int32_t (*usbd_hw_ep_write)(uint8_t ep, const void *buf, uint16_t blen); diff --git a/lib/libusb_stm32/readme.md b/lib/libusb_stm32/readme.md index a365c7932..504c87d5c 100644 --- a/lib/libusb_stm32/readme.md +++ b/lib/libusb_stm32/readme.md @@ -128,6 +128,10 @@ STM32G431RB, STM32F411CEUx, STM32F405RG, STM32F446RE, STM32F373CC, STM32L053R8, STM32F745VE, STM32F401CE, STM32H743. See [hardware.md](hardware.md) for details. +### Don't copy-paste the startup code from the demo without considering RCC and USB clock requirements. +The HSI oscillator usually does not meet the timing requirements for USB and may cause performance loss +and a high error rate. + ### Implemented definitions for classes ### 1. USB HID based on [Device Class Definition for Human Interface Devices (HID) Version 1.11](https://www.usb.org/sites/default/files/documents/hid1_11.pdf) 2. USB DFU based on [USB Device Firmware Upgrade Specification, Revision 1.1](https://www.usb.org/sites/default/files/DFU_1.1.pdf) diff --git a/lib/libusb_stm32/src/usbd_stm32f103_devfs.c b/lib/libusb_stm32/src/usbd_stm32f103_devfs.c index 2e8143586..bd41ad580 100644 --- a/lib/libusb_stm32/src/usbd_stm32f103_devfs.c +++ b/lib/libusb_stm32/src/usbd_stm32f103_devfs.c @@ -153,7 +153,7 @@ static uint16_t get_next_pma(uint16_t sz) { if ((tbl->tx.addr) && (tbl->tx.addr < _result)) _result = tbl->tx.addr; if ((tbl->rx.addr) && (tbl->rx.addr < _result)) _result = tbl->rx.addr; } - return (_result < (0x020 + sz)) ? 0 : (_result - sz); + return (_result < (0x020U + sz)) ? 0 : (_result - sz); } static uint32_t getinfo(void) { @@ -345,7 +345,7 @@ static void ep_deconfig(uint8_t ep) { } static uint16_t pma_read (uint8_t *buf, uint16_t blen, pma_rec *rx) { - uint16_t tmp; + uint16_t tmp = 0; uint16_t *pma = PMA(rx->addr); uint16_t rxcnt = rx->cnt & 0x03FF; rx->cnt &= ~0x3FF; diff --git a/lib/libusb_stm32/src/usbd_stm32f105_otgfs.c b/lib/libusb_stm32/src/usbd_stm32f105_otgfs.c index e3c0f4b8f..770af5e87 100644 --- a/lib/libusb_stm32/src/usbd_stm32f105_otgfs.c +++ b/lib/libusb_stm32/src/usbd_stm32f105_otgfs.c @@ -321,7 +321,7 @@ static void ep_deconfig(uint8_t ep) { } static int32_t ep_read(uint8_t ep, void* buf, uint16_t blen) { - uint32_t len, tmp; + uint32_t len, tmp = 0; volatile uint32_t *fifo = EPFIFO(0); /* no data in RX FIFO */ if (!(OTG->GINTSTS & USB_OTG_GINTSTS_RXFLVL)) return -1; @@ -338,6 +338,7 @@ static int32_t ep_read(uint8_t ep, void* buf, uint16_t blen) { tmp >>= 8; } } + _BST(EPOUT(ep)->DOEPCTL, USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA); return (len < blen) ? len : blen; } @@ -413,9 +414,7 @@ static void evt_poll(usbd_device *dev, usbd_evt_callback callback) { } evt = usbd_evt_epsetup; break; - case 0x03: /* OUT completed */ - case 0x04: /* SETUP completed */ - _BST(EPOUT(ep)->DOEPCTL, USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA); + // fall through default: /* pop GRXSTSP */ OTG->GRXSTSP; @@ -436,7 +435,7 @@ static void evt_poll(usbd_device *dev, usbd_evt_callback callback) { /* no more supported events */ return; } - return callback(dev, evt, ep); + callback(dev, evt, ep); } } diff --git a/lib/libusb_stm32/src/usbd_stm32f429_otgfs.c b/lib/libusb_stm32/src/usbd_stm32f429_otgfs.c index 059a54135..a41479e56 100644 --- a/lib/libusb_stm32/src/usbd_stm32f429_otgfs.c +++ b/lib/libusb_stm32/src/usbd_stm32f429_otgfs.c @@ -318,7 +318,7 @@ static void ep_deconfig(uint8_t ep) { } static int32_t ep_read(uint8_t ep, void* buf, uint16_t blen) { - uint32_t len, tmp; + uint32_t len, tmp = 0; volatile uint32_t *fifo = EPFIFO(0); /* no data in RX FIFO */ if (!(OTG->GINTSTS & USB_OTG_GINTSTS_RXFLVL)) return -1; @@ -335,26 +335,24 @@ static int32_t ep_read(uint8_t ep, void* buf, uint16_t blen) { tmp >>= 8; } } + _BST(EPOUT(ep)->DOEPCTL, USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA); return (len < blen) ? len : blen; } static int32_t ep_write(uint8_t ep, const void *buf, uint16_t blen) { - uint32_t len, tmp; + uint32_t len, tmp = 0; ep &= 0x7F; volatile uint32_t* fifo = EPFIFO(ep); USB_OTG_INEndpointTypeDef* epi = EPIN(ep); + /* check if EP enabled*/ + if (ep != 0 && epi->DIEPCTL & USB_OTG_DIEPCTL_EPENA) return -1; /* transfer data size in 32-bit words */ len = (blen + 3) >> 2; /* no enough space in TX fifo */ - if (len > epi->DTXFSTS) return -1; - if (ep != 0 && epi->DIEPCTL & USB_OTG_DIEPCTL_EPENA) { - return -1; - } - epi->DIEPTSIZ = 0; + if (len > 0 && len > _FLD2VAL(USB_OTG_DTXFSTS_INEPTFSAV, epi->DTXFSTS)) return -1; epi->DIEPTSIZ = (1 << 19) + blen; _BMD(epi->DIEPCTL, USB_OTG_DIEPCTL_STALL, USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_CNAK); /* push data to FIFO */ - tmp = 0; for (int idx = 0; idx < blen; idx++) { tmp |= (uint32_t)((const uint8_t*)buf)[idx] << ((idx & 0x03) << 3); if ((idx & 0x03) == 0x03 || (idx + 1) == blen) { @@ -410,9 +408,6 @@ static void evt_poll(usbd_device *dev, usbd_evt_callback callback) { } evt = usbd_evt_epsetup; break; - case 0x03: /* OUT completed */ - case 0x04: /* SETUP completed */ - _BST(EPOUT(ep)->DOEPCTL, USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA); default: /* pop GRXSTSP */ OTG->GRXSTSP; @@ -433,7 +428,7 @@ static void evt_poll(usbd_device *dev, usbd_evt_callback callback) { /* no more supported events */ return; } - return callback(dev, evt, ep); + callback(dev, evt, ep); } } diff --git a/lib/libusb_stm32/src/usbd_stm32f429_otghs.c b/lib/libusb_stm32/src/usbd_stm32f429_otghs.c index 996c7014b..10a48dc9d 100644 --- a/lib/libusb_stm32/src/usbd_stm32f429_otghs.c +++ b/lib/libusb_stm32/src/usbd_stm32f429_otghs.c @@ -320,7 +320,7 @@ static void ep_deconfig(uint8_t ep) { } static int32_t ep_read(uint8_t ep, void* buf, uint16_t blen) { - uint32_t len, tmp; + uint32_t len, tmp = 0; volatile uint32_t *fifo = EPFIFO(0); /* no data in RX FIFO */ if (!(OTG->GINTSTS & USB_OTG_GINTSTS_RXFLVL)) return -1; @@ -418,6 +418,7 @@ static void evt_poll(usbd_device *dev, usbd_evt_callback callback) { case 0x03: /* OUT completed */ case 0x04: /* SETUP completed */ _BST(EPOUT(ep)->DOEPCTL, USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA); + // fall through default: /* pop GRXSTSP */ OTG->GRXSTSP; @@ -438,7 +439,7 @@ static void evt_poll(usbd_device *dev, usbd_evt_callback callback) { /* no more supported events */ return; } - return callback(dev, evt, ep); + callback(dev, evt, ep); } } diff --git a/lib/libusb_stm32/src/usbd_stm32f446_otgfs.c b/lib/libusb_stm32/src/usbd_stm32f446_otgfs.c index ea1d586e7..d933d589d 100644 --- a/lib/libusb_stm32/src/usbd_stm32f446_otgfs.c +++ b/lib/libusb_stm32/src/usbd_stm32f446_otgfs.c @@ -309,7 +309,7 @@ static void ep_deconfig(uint8_t ep) { } static int32_t ep_read(uint8_t ep, void* buf, uint16_t blen) { - uint32_t len, tmp; + uint32_t len, tmp = 0; ep &= 0x7F; volatile uint32_t *fifo = EPFIFO(0); USB_OTG_OUTEndpointTypeDef* epo = EPOUT(ep); @@ -418,7 +418,7 @@ static void evt_poll(usbd_device *dev, usbd_evt_callback callback) { /* no more supported events */ return; } - return callback(dev, evt, ep); + callback(dev, evt, ep); } } diff --git a/lib/libusb_stm32/src/usbd_stm32f446_otghs.c b/lib/libusb_stm32/src/usbd_stm32f446_otghs.c index 4e4a8b3ab..102db9df7 100644 --- a/lib/libusb_stm32/src/usbd_stm32f446_otghs.c +++ b/lib/libusb_stm32/src/usbd_stm32f446_otghs.c @@ -309,7 +309,7 @@ static void ep_deconfig(uint8_t ep) { } static int32_t ep_read(uint8_t ep, void* buf, uint16_t blen) { - uint32_t len, tmp; + uint32_t len, tmp = 0; ep &= 0x7F; volatile uint32_t *fifo = EPFIFO(0); USB_OTG_OUTEndpointTypeDef* epo = EPOUT(ep); @@ -418,7 +418,7 @@ static void evt_poll(usbd_device *dev, usbd_evt_callback callback) { /* no more supported events */ return; } - return callback(dev, evt, ep); + callback(dev, evt, ep); } } diff --git a/lib/libusb_stm32/src/usbd_stm32h743_otgfs.c b/lib/libusb_stm32/src/usbd_stm32h743_otgfs.c index 7ea623614..d3494d57d 100644 --- a/lib/libusb_stm32/src/usbd_stm32h743_otgfs.c +++ b/lib/libusb_stm32/src/usbd_stm32h743_otgfs.c @@ -309,7 +309,7 @@ static void ep_deconfig(uint8_t ep) { } static int32_t ep_read(uint8_t ep, void* buf, uint16_t blen) { - uint32_t len, tmp; + uint32_t len, tmp = 0; ep &= 0x7F; volatile uint32_t *fifo = EPFIFO(0); USB_OTG_OUTEndpointTypeDef* epo = EPOUT(ep); @@ -418,7 +418,7 @@ static void evt_poll(usbd_device *dev, usbd_evt_callback callback) { /* no more supported events */ return; } - return callback(dev, evt, ep); + callback(dev, evt, ep); } } diff --git a/lib/libusb_stm32/src/usbd_stm32l052_devfs.c b/lib/libusb_stm32/src/usbd_stm32l052_devfs.c index e2ba11e82..d6004c34a 100644 --- a/lib/libusb_stm32/src/usbd_stm32l052_devfs.c +++ b/lib/libusb_stm32/src/usbd_stm32l052_devfs.c @@ -101,7 +101,7 @@ static uint16_t get_next_pma(uint16_t sz) { if ((tbl->rx.addr) && (tbl->rx.addr < _result)) _result = tbl->rx.addr; if ((tbl->tx.addr) && (tbl->tx.addr < _result)) _result = tbl->tx.addr; } - return (_result < (0x020 + sz)) ? 0 : (_result - sz); + return (_result < (0x020U + sz)) ? 0 : (_result - sz); } static uint32_t getinfo(void) { @@ -283,7 +283,7 @@ static void ep_deconfig(uint8_t ep) { } static uint16_t pma_read (uint8_t *buf, uint16_t blen, pma_rec *rx) { - uint16_t tmp; + uint16_t tmp = 0; uint16_t *pma = (void*)(USB_PMAADDR + rx->addr); uint16_t rxcnt = rx->cnt & 0x03FF; rx->cnt &= ~0x3FF; diff --git a/lib/libusb_stm32/src/usbd_stm32l100_devfs.c b/lib/libusb_stm32/src/usbd_stm32l100_devfs.c index 77488eda8..f502476d4 100644 --- a/lib/libusb_stm32/src/usbd_stm32l100_devfs.c +++ b/lib/libusb_stm32/src/usbd_stm32l100_devfs.c @@ -90,7 +90,7 @@ static uint16_t get_next_pma(uint16_t sz) { if ((tbl->tx.addr) && (tbl->tx.addr < _result)) _result = tbl->tx.addr; if ((tbl->rx.addr) && (tbl->rx.addr < _result)) _result = tbl->rx.addr; } - return (_result < (0x020 + sz)) ? 0 : (_result - sz); + return (_result < (0x020U + sz)) ? 0 : (_result - sz); } static uint32_t getinfo(void) { @@ -255,7 +255,7 @@ static void ep_deconfig(uint8_t ep) { } static uint16_t pma_read (uint8_t *buf, uint16_t blen, pma_rec *rx) { - uint16_t tmp; + uint16_t tmp = 0; uint16_t *pma = (void*)(USB_PMAADDR + 2 * rx->addr); uint16_t rxcnt = rx->cnt & 0x03FF; rx->cnt &= ~0x3FF; diff --git a/lib/libusb_stm32/src/usbd_stm32l433_devfs.c b/lib/libusb_stm32/src/usbd_stm32l433_devfs.c index 133624cc1..be0e0a542 100644 --- a/lib/libusb_stm32/src/usbd_stm32l433_devfs.c +++ b/lib/libusb_stm32/src/usbd_stm32l433_devfs.c @@ -94,7 +94,7 @@ static uint16_t get_next_pma(uint16_t sz) { if ((tbl->rx.addr) && (tbl->rx.addr < _result)) _result = tbl->rx.addr; if ((tbl->tx.addr) && (tbl->tx.addr < _result)) _result = tbl->tx.addr; } - return (_result < (0x020 + sz)) ? 0 : (_result - sz); + return (_result < (0x020U + sz)) ? 0 : (_result - sz); } static uint32_t getinfo(void) { @@ -272,22 +272,20 @@ static void ep_deconfig(uint8_t ep) { } static uint16_t pma_read (uint8_t *buf, uint16_t blen, pma_rec *rx) { - uint16_t *pma = (void*)(USB_PMAADDR + rx->addr); + uint16_t tmp = 0; + volatile uint16_t *pma = (volatile uint16_t *)(USB_PMAADDR + rx->addr); uint16_t rxcnt = rx->cnt & 0x03FF; rx->cnt &= ~0x3FF; - - if (blen > rxcnt) { - blen = rxcnt; - } - rxcnt = blen; - while (blen) { - uint16_t _t = *pma; - *buf++ = _t & 0xFF; - if (--blen) { - *buf++ = _t >> 8; - pma++; - blen--; - } else break; + for(int idx = 0; idx < rxcnt; idx++) { + if ((idx & 0x01) == 0) { + tmp = *pma++; + } + if (idx < blen) { + buf[idx] = tmp & 0xFF; + tmp >>= 8; + } else { + return blen; + } } return rxcnt; } @@ -336,14 +334,16 @@ static int32_t ep_read(uint8_t ep, void *buf, uint16_t blen) { } static void pma_write(const uint8_t *buf, uint16_t blen, pma_rec *tx) { - uint16_t *pma = (void*)(USB_PMAADDR + tx->addr); + volatile uint16_t *pma = (volatile uint16_t *)(USB_PMAADDR + tx->addr); + uint16_t tmp = 0; tx->cnt = blen; - while (blen > 1) { - *pma++ = buf[1] << 8 | buf[0]; - buf += 2; - blen -= 2; + for (int idx=0; idx < blen; idx++) { + tmp |= buf[idx] << ((idx & 0x01) ? 8 : 0); + if ((idx & 0x01) || (idx + 1) == blen) { + *pma++ = tmp; + tmp = 0; + } } - if (blen) *pma = *buf; } static int32_t ep_write(uint8_t ep, const void *buf, uint16_t blen) { diff --git a/lib/libusb_stm32/src/usbd_stm32l476_otgfs.c b/lib/libusb_stm32/src/usbd_stm32l476_otgfs.c index 66294bc67..6b0eda731 100644 --- a/lib/libusb_stm32/src/usbd_stm32l476_otgfs.c +++ b/lib/libusb_stm32/src/usbd_stm32l476_otgfs.c @@ -342,7 +342,7 @@ static void ep_deconfig(uint8_t ep) { } static int32_t ep_read(uint8_t ep, void* buf, uint16_t blen) { - uint32_t len, tmp; + uint32_t len, tmp = 0; ep &= 0x7F; volatile uint32_t *fifo = EPFIFO(0); USB_OTG_OUTEndpointTypeDef* epo = EPOUT(ep); @@ -451,7 +451,7 @@ static void evt_poll(usbd_device *dev, usbd_evt_callback callback) { /* no more supported events */ return; } - return callback(dev, evt, ep); + callback(dev, evt, ep); } } diff --git a/lib/libusb_stm32/src/usbd_stm32wb55_devfs.c b/lib/libusb_stm32/src/usbd_stm32wb55_devfs.c index 07a50b0ac..1e234538f 100644 --- a/lib/libusb_stm32/src/usbd_stm32wb55_devfs.c +++ b/lib/libusb_stm32/src/usbd_stm32wb55_devfs.c @@ -94,7 +94,7 @@ static uint16_t get_next_pma(uint16_t sz) { if ((tbl->rx.addr) && (tbl->rx.addr < _result)) _result = tbl->rx.addr; if ((tbl->tx.addr) && (tbl->tx.addr < _result)) _result = tbl->tx.addr; } - return (_result < (unsigned)(0x020 + sz)) ? 0 : (_result - sz); + return (_result < (0x020U + sz)) ? 0 : (_result - sz); } static uint32_t getinfo(void) {