Skip to content

Commit 397c051

Browse files
authored
Work/f411 fixup (#138)
* fix AHB1 clocks in demo for F411 #136 * #135 add sequental TX data and variable packet length * #135 fix F429 RX buffer overflow * #135 rewrite cdc_txonly() to achieve a cyclic bulk transaction length * #135 fix F105 RX buffer overflow * #135 set demo code back to loopback * #135 revert clear STALL on write
1 parent 8f59e4a commit 397c051

File tree

5 files changed

+35
-20
lines changed

5 files changed

+35
-20
lines changed

demo/cdc_loop.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,24 @@ static void cdc_rxonly (usbd_device *dev, uint8_t event, uint8_t ep) {
358358
}
359359

360360
static void cdc_txonly(usbd_device *dev, uint8_t event, uint8_t ep) {
361-
uint8_t _t = dev->driver->frame_no();
362-
memset(fifo, _t, CDC_DATA_SZ);
363-
usbd_ep_write(dev, ep, fifo, CDC_DATA_SZ);
361+
static uint8_t psize = 0x00U;
362+
static uint8_t remained = 0x00U;
363+
static uint8_t lastsym = 0x00U;
364+
365+
uint8_t _t = (remained < CDC_DATA_SZ) ? remained : CDC_DATA_SZ;
366+
// fill buffer by sequental data
367+
for (size_t i = 0; i < _t; ++i) {
368+
fifo[i] = lastsym++;
369+
}
370+
usbd_ep_write(dev, ep, fifo, _t);
371+
372+
if (remained < CDC_DATA_SZ) {
373+
// bulk xfer completed. increase bulk size
374+
remained = ++psize;
375+
} else {
376+
// continue to send remained data or ZLP
377+
remained -= _t;
378+
}
364379
}
365380

366381
static void cdc_rxtx(usbd_device *dev, uint8_t event, uint8_t ep) {

demo/cdc_startup.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static void cdc_init_rcc (void) {
104104
/* switch to PLL */
105105
_BMD(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_PLL);
106106
_WVL(RCC->CFGR, RCC_CFGR_SWS, RCC_CFGR_SWS_PLL);
107-
107+
108108
_BST(RCC->AHBENR, RCC_AHBENR_GPIOAEN);
109109
_BST(GPIOA->AFR[1], (0x0E << 12) | (0x0E << 16));
110110
_BMD(GPIOA->MODER, (0x03 << 22) | (0x03 << 24), (0x02 << 22) | (0x02 << 24));
@@ -141,8 +141,12 @@ static void cdc_init_rcc (void) {
141141
/* enabling PLL */
142142
_BST(RCC->CR, RCC_CR_PLLON);
143143
_WBS(RCC->CR, RCC_CR_PLLRDY);
144-
/* switching to PLL */
145-
_BMD(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_PLL);
144+
/* Setup CFGR to PLL*/
145+
/* APB1 | APB2 */
146+
/* STM32F411 <50Mhz | <100MHz */
147+
/* STM32F429 <45MHz | <90MHz */
148+
/* STM32F405, STM32F401 <42MHz | <84MHz */
149+
_BMD(RCC->CFGR, RCC_CFGR_SW | RCC_CFGR_PPRE1, RCC_CFGR_SW_PLL | RCC_CFGR_PPRE1_DIV2);
146150
_WVL(RCC->CFGR, RCC_CFGR_SWS, RCC_CFGR_SWS_PLL);
147151
#if defined(USBD_PRIMARY_OTGHS)
148152
/* enabling GPIOB and setting PB13, PB14 and PB15 to AF11 (USB_OTG2FS) */

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ STM32G431RB, STM32F411CEUx, STM32F405RG, STM32F446RE, STM32F373CC, STM32L053R8,
128128
STM32F745VE, STM32F401CE, STM32H743.
129129
See [hardware.md](hardware.md) for details.
130130

131+
### Don't copy-paste the startup code from the demo without considering RCC and USB clock requirements.
132+
The HSI oscillator usually does not meet the timing requirements for USB and may cause performance loss
133+
and a high error rate.
134+
131135
### Implemented definitions for classes ###
132136
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)
133137
2. USB DFU based on [USB Device Firmware Upgrade Specification, Revision 1.1](https://www.usb.org/sites/default/files/DFU_1.1.pdf)

src/usbd_stm32f105_otgfs.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ static int32_t ep_read(uint8_t ep, void* buf, uint16_t blen) {
338338
tmp >>= 8;
339339
}
340340
}
341+
_BST(EPOUT(ep)->DOEPCTL, USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
341342
return (len < blen) ? len : blen;
342343
}
343344

@@ -413,9 +414,6 @@ static void evt_poll(usbd_device *dev, usbd_evt_callback callback) {
413414
}
414415
evt = usbd_evt_epsetup;
415416
break;
416-
case 0x03: /* OUT completed */
417-
case 0x04: /* SETUP completed */
418-
_BST(EPOUT(ep)->DOEPCTL, USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
419417
// fall through
420418
default:
421419
/* pop GRXSTSP */

src/usbd_stm32f429_otgfs.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -335,26 +335,24 @@ static int32_t ep_read(uint8_t ep, void* buf, uint16_t blen) {
335335
tmp >>= 8;
336336
}
337337
}
338+
_BST(EPOUT(ep)->DOEPCTL, USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
338339
return (len < blen) ? len : blen;
339340
}
340341

341342
static int32_t ep_write(uint8_t ep, const void *buf, uint16_t blen) {
342-
uint32_t len, tmp;
343+
uint32_t len, tmp = 0;
343344
ep &= 0x7F;
344345
volatile uint32_t* fifo = EPFIFO(ep);
345346
USB_OTG_INEndpointTypeDef* epi = EPIN(ep);
347+
/* check if EP enabled*/
348+
if (ep != 0 && epi->DIEPCTL & USB_OTG_DIEPCTL_EPENA) return -1;
346349
/* transfer data size in 32-bit words */
347350
len = (blen + 3) >> 2;
348351
/* no enough space in TX fifo */
349-
if (len > epi->DTXFSTS) return -1;
350-
if (ep != 0 && epi->DIEPCTL & USB_OTG_DIEPCTL_EPENA) {
351-
return -1;
352-
}
353-
epi->DIEPTSIZ = 0;
352+
if (len > 0 && len > _FLD2VAL(USB_OTG_DTXFSTS_INEPTFSAV, epi->DTXFSTS)) return -1;
354353
epi->DIEPTSIZ = (1 << 19) + blen;
355354
_BMD(epi->DIEPCTL, USB_OTG_DIEPCTL_STALL, USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_CNAK);
356355
/* push data to FIFO */
357-
tmp = 0;
358356
for (int idx = 0; idx < blen; idx++) {
359357
tmp |= (uint32_t)((const uint8_t*)buf)[idx] << ((idx & 0x03) << 3);
360358
if ((idx & 0x03) == 0x03 || (idx + 1) == blen) {
@@ -410,10 +408,6 @@ static void evt_poll(usbd_device *dev, usbd_evt_callback callback) {
410408
}
411409
evt = usbd_evt_epsetup;
412410
break;
413-
case 0x03: /* OUT completed */
414-
case 0x04: /* SETUP completed */
415-
_BST(EPOUT(ep)->DOEPCTL, USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
416-
// fall through
417411
default:
418412
/* pop GRXSTSP */
419413
OTG->GRXSTSP;

0 commit comments

Comments
 (0)