Skip to content

Commit

Permalink
update libusb
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Jul 4, 2024
1 parent 635d295 commit 6d4c319
Show file tree
Hide file tree
Showing 23 changed files with 104 additions and 81 deletions.
2 changes: 1 addition & 1 deletion boards/STM32F722RE.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion boards/STM32F745XG.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion boards/STM32F765VI.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion boards/STM32H743VI.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 18 additions & 3 deletions lib/libusb_stm32/demo/cdc_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 7 additions & 3 deletions lib/libusb_stm32/demo/cdc_startup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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) */
Expand Down
2 changes: 1 addition & 1 deletion lib/libusb_stm32/inc/hid_usage_keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/libusb_stm32/inc/hid_usage_simulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions lib/libusb_stm32/inc/stm32_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,33 @@
/* bit value */
#define _BV(bit) (0x01 << (bit))

#if defined(STM32F0)
#if defined(STM32F0xx)
#include <stm32f0xx.h>
#elif defined(STM32F1)
#elif defined(STM32F1xx)
#include <stm32f1xx.h>
#elif defined(STM32F2)
#elif defined(STM32F2xx)
#include <stm32f2xx.h>
#elif defined(STM32F3)
#elif defined(STM32F3xx)
#include <stm32f3xx.h>
#elif defined(STM32F4)
#elif defined(STM32F4xx)
#include <stm32f4xx.h>
#elif defined(STM32F7)
#elif defined(STM32F7xx)
#include <stm32f7xx.h>
#elif defined(STM32H7)
#elif defined(STM32H7xx)
#include <stm32h7xx.h>
#elif defined(STM32L0)
#elif defined(STM32L0xx)
#include <stm32l0xx.h>
#elif defined(STM32L1)
#elif defined(STM32L1xx)
#include <stm32l1xx.h>
#elif defined(STM32L4)
#elif defined(STM32L4xx)
#include <stm32l4xx.h>
#elif defined(STM32L5)
#elif defined(STM32L5xx)
#include <stm32l5xx.h>
#elif defined(STM32G0)
#elif defined(STM32G0xx)
#include <stm32g0xx.h>
#elif defined(STM32G4)
#elif defined(STM32G4xx)
#include <stm32g4xx.h>
#elif defined(STM32WB)
#elif defined(STM32WBxx)
#include <stm32wbxx.h>
#else
#error "STM32 family not defined"
Expand Down
5 changes: 5 additions & 0 deletions lib/libusb_stm32/inc/usbd_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions lib/libusb_stm32/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/libusb_stm32/src/usbd_stm32f103_devfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 4 additions & 5 deletions lib/libusb_stm32/src/usbd_stm32f105_otgfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand Down
19 changes: 7 additions & 12 deletions lib/libusb_stm32/src/usbd_stm32f429_otgfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand Down
5 changes: 3 additions & 2 deletions lib/libusb_stm32/src/usbd_stm32f429_otghs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/libusb_stm32/src/usbd_stm32f446_otgfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/libusb_stm32/src/usbd_stm32f446_otghs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/libusb_stm32/src/usbd_stm32h743_otgfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/libusb_stm32/src/usbd_stm32l052_devfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 6d4c319

Please sign in to comment.