From a7f50526953c649dab9ed29afe8fecdfae39f99d Mon Sep 17 00:00:00 2001 From: Ilya Stolyarov Date: Tue, 9 Jul 2019 17:29:15 +0300 Subject: IMPR: [usb] Some style improvements --- include/libopencm3/usb/dwc/otg_common.h | 2 +- lib/usb/usb_dwc.c | 26 ++++++++++++++++++++++---- lib/usb/usb_dwc_common.c | 4 ++-- lib/usb/usb_private.h | 8 +++++--- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/include/libopencm3/usb/dwc/otg_common.h b/include/libopencm3/usb/dwc/otg_common.h index 366ed14d..e85184c7 100644 --- a/include/libopencm3/usb/dwc/otg_common.h +++ b/include/libopencm3/usb/dwc/otg_common.h @@ -248,7 +248,7 @@ #define OTG_DCTL_RWUSIG (1 << 0) /* OTG device configuration register (OTG_DCFG) */ -#define OTG_DCFG_DSPD_MASK (0x3 << 0) +#define OTG_DCFG_DSPD_MASK (0b11 << 0) #define OTG_DCFG_DSPD_FS_INT 0x0003 #define OTG_DCFG_DSPD OTG_DCFG_DSPD_FS_INT #define OTG_DCFG_NZLSOHSK 0x0004 diff --git a/lib/usb/usb_dwc.c b/lib/usb/usb_dwc.c index 8da22959..dc9b0d6e 100644 --- a/lib/usb/usb_dwc.c +++ b/lib/usb/usb_dwc.c @@ -33,6 +33,19 @@ static usbd_device *stm32_dwc_usbd_init(void); static usbd_device *stm32_dwc_ulpi_usbd_init(void); +/*---------------------------------------------------------------------------*/ +/** @brief Issue Pipeline Stall + +Issue a pipeline stall to make sure all write operations completed. + +After performing a data write operation and before using peripheral, +the software can issue a DSB instruction to guarantee the +completion of a previous data write operation. + +*/ + +static inline void pipeline_stall(void); + static struct _usbd_device usbd_dev; const struct _usbd_driver stm32_dwc_usb_driver = { @@ -114,9 +127,9 @@ static usbd_device *stm32_dwc_usbd_init(void) static usbd_device *stm32_dwc_ulpi_usbd_init(void) { rcc_periph_clock_enable(RCC_OTGHS); - __asm__("dsb"); // Errata RCC peripheral limitation + pipeline_stall() rcc_periph_clock_enable(RCC_OTGHSULPI); - __asm__("dsb"); // Errata RCC peripheral limitation + pipeline_stall() /* Wait for AHB idle. */ while (!(OTG_HS_GRSTCTL & OTG_GRSTCTL_AHBIDL)); @@ -130,7 +143,7 @@ static usbd_device *stm32_dwc_ulpi_usbd_init(void) OTG_HS_DCFG |= OTG_DCFG_DSPD_HS_EXT; /* Restart the PHY clock. */ - OTG_HS_PCGCCTL = 0; + OTG_HS_PCGCCTL = 0U; OTG_HS_GRXFSIZ = stm32_dwc_usb_driver_ulpi.rx_fifo_size; usbd_dev.fifo_mem_top = stm32_dwc_usb_driver_ulpi.rx_fifo_size; @@ -163,8 +176,13 @@ static usbd_device *stm32_dwc_ulpi_usbd_init(void) OTG_GINTMSK_USBSUSPM | OTG_GINTMSK_WUIM; - OTG_HS_DAINTMSK = 0xF; + OTG_HS_DAINTMSK = 0xFU; OTG_HS_DIEPMSK = OTG_DIEPMSK_XFRCM; return &usbd_dev; } + +static inline void pipeline_stall(void) +{ + __asm__ volatile("dsb":::"memory"); +} diff --git a/lib/usb/usb_dwc_common.c b/lib/usb/usb_dwc_common.c index 3a33020f..ae80db40 100644 --- a/lib/usb/usb_dwc_common.c +++ b/lib/usb/usb_dwc_common.c @@ -119,7 +119,7 @@ void dwc_endpoints_reset(usbd_device *usbd_dev) usbd_dev->fifo_mem_top = usbd_dev->fifo_mem_top_ep0; /* Disable any currently active endpoints */ - for (i = 1; i < 8; i++) { + for (i = 1; i < ENDPOINT_COUNT; i++) { if (REBASE(OTG_DOEPCTL(i)) & OTG_DOEPCTL0_EPENA) { REBASE(OTG_DOEPCTL(i)) |= OTG_DOEPCTL0_EPDIS; } @@ -339,7 +339,7 @@ void dwc_poll(usbd_device *usbd_dev) * There is no global interrupt flag for transmit complete. * The XFRC bit must be checked in each OTG_DIEPINT(x). */ - for (i = 0; i < 8; i++) { /* Iterate over endpoints. */ + for (i = 0; i < ENDPOINT_COUNT; i++) { /* Iterate over endpoints. */ if (REBASE(OTG_DIEPINT(i)) & OTG_DIEPINTX_XFRC) { /* Transfer complete. */ if (usbd_dev->user_callback_ctr[i] diff --git a/lib/usb/usb_private.h b/lib/usb/usb_private.h index 0bd15d7f..8e1a091c 100644 --- a/lib/usb/usb_private.h +++ b/lib/usb/usb_private.h @@ -41,6 +41,8 @@ LGPL License Terms @ref lgpl_license #define MAX_USER_CONTROL_CALLBACK 4 #define MAX_USER_SET_CONFIG_CALLBACK 4 +#define ENDPOINT_COUNT 8 + #define MIN(a, b) ((a) < (b) ? (a) : (b)) /** Internal collection of device information. */ @@ -83,7 +85,7 @@ struct _usbd_device { uint8_t type_mask; } user_control_callback[MAX_USER_CONTROL_CALLBACK]; - usbd_endpoint_callback user_callback_ctr[8][3]; + usbd_endpoint_callback user_callback_ctr[ENDPOINT_COUNT][3]; /* User callback function for some standard USB function hooks */ usbd_set_config_callback user_callback_set_config[MAX_USER_SET_CONFIG_CALLBACK]; @@ -96,12 +98,12 @@ struct _usbd_device { uint16_t fifo_mem_top; uint16_t fifo_mem_top_ep0; - uint8_t force_nak[8]; + uint8_t force_nak[ENDPOINT_COUNT]; /* * We keep a backup copy of the out endpoint size registers to restore * them after a transaction. */ - uint32_t doeptsiz[8]; + uint32_t doeptsiz[ENDPOINT_COUNT]; /* * Received packet size for each endpoint. This is assigned in * stm32f107_poll() which reads the packet status push register GRXSTSP -- cgit v1.2.3