From f49cbee6834031dc2e5406d6735ecc178e1c265d Mon Sep 17 00:00:00 2001 From: Karl Palsson Date: Sat, 19 Sep 2015 21:41:41 +0000 Subject: [PATCH] usb: otg-dev: disable and flush endpoints on reset Only resetting the fifo memory pointers can result in corrupt data. Tested on f4 disco board with the gadget0 test suite. --- include/libopencm3/stm32/otg_common.h | 1 + lib/usb/usb_fx07_common.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/libopencm3/stm32/otg_common.h b/include/libopencm3/stm32/otg_common.h index 77f19e07..f3d13300 100644 --- a/include/libopencm3/stm32/otg_common.h +++ b/include/libopencm3/stm32/otg_common.h @@ -124,6 +124,7 @@ #define OTG_GRSTCTL_AHBIDL (1 << 31) /* Bits 30:11 - Reserved */ #define OTG_GRSTCTL_TXFNUM_MASK (0x1f << 6) +#define OTG_GRSTCTL_TXFNUM_ALL (0x10 << 6) #define OTG_GRSTCTL_TXFFLSH (1 << 5) #define OTG_GRSTCTL_RXFFLSH (1 << 4) /* Bit 3 - Reserved */ diff --git a/lib/usb/usb_fx07_common.c b/lib/usb/usb_fx07_common.c index a3be368d..ffb46f11 100644 --- a/lib/usb/usb_fx07_common.c +++ b/lib/usb/usb_fx07_common.c @@ -117,8 +117,22 @@ void stm32fx07_ep_setup(usbd_device *usbd_dev, uint8_t addr, uint8_t type, void stm32fx07_endpoints_reset(usbd_device *usbd_dev) { + int i; /* The core resets the endpoints automatically on reset. */ usbd_dev->fifo_mem_top = usbd_dev->fifo_mem_top_ep0; + + /* Disable any currently active endpoints */ + for (i = 1; i < 4; i++) { + if (REBASE(OTG_DOEPCTL(i)) & OTG_DOEPCTL0_EPENA) { + REBASE(OTG_DOEPCTL(i)) |= OTG_DOEPCTL0_EPDIS; + } + if (REBASE(OTG_DIEPCTL(i)) & OTG_DIEPCTL0_EPENA) { + REBASE(OTG_DIEPCTL(i)) |= OTG_DIEPCTL0_EPDIS; + } + } + + /* Flush all tx/rx fifos */ + REBASE(OTG_GRSTCTL) = OTG_GRSTCTL_TXFFLSH | OTG_GRSTCTL_TXFNUM_ALL | OTG_GRSTCTL_RXFFLSH; } void stm32fx07_ep_stall_set(usbd_device *usbd_dev, uint8_t addr, uint8_t stall)