diff --git a/os/hal/ports/STM32/LLD/USBv1/hal_usb_lld.c b/os/hal/ports/STM32/LLD/USBv1/hal_usb_lld.c index 5922a643e..c8026ef44 100644 --- a/os/hal/ports/STM32/LLD/USBv1/hal_usb_lld.c +++ b/os/hal/ports/STM32/LLD/USBv1/hal_usb_lld.c @@ -74,16 +74,16 @@ static uint8_t ep0setup_buffer[8]; * @brief EP0 initialization structure. */ static const USBEndpointConfig ep0config = { - USB_EP_MODE_TYPE_CTRL, - _usb_ep0setup, - _usb_ep0in, - _usb_ep0out, - 0x40, - 0x40, - &ep0_state.in, - &ep0_state.out, - 1, - ep0setup_buffer + .ep_mode = USB_EP_MODE_TYPE_CTRL, + .setup_cb = _usb_ep0setup, + .in_cb = _usb_ep0in, + .out_cb = _usb_ep0out, + .in_maxsize = 0x40U, + .out_maxsize = 0x40U, + .in_state = &ep0_state.in, + .out_state = &ep0_state.out, + .ep_buffers = 1U, + .setup_buf = ep0setup_buffer }; /*===========================================================================*/ @@ -99,7 +99,7 @@ static void usb_pm_reset(USBDriver *usbp) { /* The first 64 bytes are reserved for the descriptors table. The effective available RAM for endpoint buffers is just 448 bytes.*/ - usbp->pmnext = 64; + usbp->pmnext = 64U; } /** @@ -113,8 +113,10 @@ static uint32_t usb_pm_alloc(USBDriver *usbp, size_t size) { uint32_t next; next = usbp->pmnext; - usbp->pmnext += (size + 1) & ~1; + usbp->pmnext += (size + 1U) & ~1U; + osalDbgAssert(usbp->pmnext <= STM32_USB_PMA_SIZE, "PMA overflow"); + return next; } @@ -180,20 +182,20 @@ static size_t usb_packet_read_to_buffer(usbep_t ep, uint8_t *buf) { *(buf + 14) = (uint8_t)w; *(buf + 15) = (uint8_t)(w >> 8); - i -= 16; - buf += 16; - pmap += 8; + i -= 16U; + buf += 16U; + pmap += 8U; } #endif /* STM32_USB_USE_FAST_COPY */ - while (i >= 2) { + while (i >= 2U) { uint32_t w = *pmap++; *buf++ = (uint8_t)w; *buf++ = (uint8_t)(w >> 8); - i -= 2; + i -= 2U; } - if (i >= 1) { + if (i >= 1U) { *buf = (uint8_t)*pmap; } @@ -264,8 +266,8 @@ static void usb_packet_write_from_buffer(usbep_t ep, *(pmap + 7) = (stm32_usb_pma_t)w; i -= 16; - buf += 16; - pmap += 8; + buf += 16U; + pmap += 8U; } #endif /* STM32_USB_USE_FAST_COPY */ @@ -301,7 +303,7 @@ static void usb_serve_endpoints(USBDriver *usbp, uint32_t istr) { isp->txcnt += isp->txlast; n = isp->txsize - isp->txcnt; - if (n > 0) { + if (n > 0U) { /* Transfer not completed, there are more packets to send.*/ if (n > epcp->in_maxsize) n = epcp->in_maxsize; @@ -339,7 +341,7 @@ static void usb_serve_endpoints(USBDriver *usbp, uint32_t istr) { /* Transaction data updated.*/ osp->rxcnt += n; osp->rxsize -= n; - osp->rxpkts -= 1; + osp->rxpkts -= 1U; /* The transaction is completed if the specified number of packets has been received or the current packet is a short packet.*/ @@ -375,7 +377,7 @@ OSAL_IRQ_HANDLER(STM32_USB1_HP_HANDLER) { /* Endpoint events handling.*/ istr = STM32_USB->ISTR; - while (istr & ISTR_CTR) { + while ((istr & ISTR_CTR) != 0U) { usb_serve_endpoints(usbp, istr); istr = STM32_USB->ISTR; } @@ -401,12 +403,12 @@ OSAL_IRQ_HANDLER(STM32_USB1_LP_HANDLER) { STM32_USB->ISTR = ~istr; /* USB bus reset condition handling.*/ - if (istr & ISTR_RESET) { + if ((istr & ISTR_RESET) != 0U) { _usb_reset(usbp); } /* USB bus SUSPEND condition handling.*/ - if (istr & ISTR_SUSP) { + if ((istr & ISTR_SUSP) != 0U) { STM32_USB->CNTR |= CNTR_FSUSP; #if STM32_USB_LOW_POWER_ON_SUSPEND STM32_USB->CNTR |= CNTR_LP_MODE; @@ -415,9 +417,9 @@ OSAL_IRQ_HANDLER(STM32_USB1_LP_HANDLER) { } /* USB bus WAKEUP condition handling.*/ - if (istr & ISTR_WKUP) { + if ((istr & ISTR_WKUP) != 0U) { uint32_t fnr = STM32_USB->FNR; - if (!(fnr & FNR_RXDP)) { + if ((fnr & FNR_RXDP) == 0U) { STM32_USB->CNTR &= ~CNTR_FSUSP; _usb_wakeup(usbp); } @@ -431,17 +433,17 @@ OSAL_IRQ_HANDLER(STM32_USB1_LP_HANDLER) { } /* SOF handling.*/ - if (istr & ISTR_SOF) { + if ((istr & ISTR_SOF) != 0U) { _usb_isr_invoke_sof_cb(usbp); } /* ERR handling.*/ - if (istr & ISTR_ERR) { + if ((istr & ISTR_ERR) != 0U) { /* CHTODO */ } /* Endpoint events handling.*/ - while (istr & ISTR_CTR) { + while ((istr & ISTR_CTR) != 0U) { usb_serve_endpoints(usbp, istr); istr = STM32_USB->ISTR; } @@ -485,16 +487,20 @@ void usb_lld_start(USBDriver *usbp) { /* USB clock enabled.*/ rccEnableUSB(true); + rccResetUSB(); + /* Powers up the transceiver while holding the USB in reset state.*/ STM32_USB->CNTR = CNTR_FRES; + /* Enabling the USB IRQ vectors, this also gives enough time to allow the transceiver power up (1uS).*/ #if STM32_USB1_HP_NUMBER != STM32_USB1_LP_NUMBER nvicEnableVector(STM32_USB1_HP_NUMBER, STM32_USB_USB1_HP_IRQ_PRIORITY); #endif nvicEnableVector(STM32_USB1_LP_NUMBER, STM32_USB_USB1_LP_IRQ_PRIORITY); + /* Releases the USB reset.*/ - STM32_USB->CNTR = 0; + STM32_USB->CNTR = 0U; } #endif /* Reset procedure enforced on driver start.*/ @@ -515,10 +521,12 @@ void usb_lld_stop(USBDriver *usbp) { if (usbp->state != USB_STOP) { #if STM32_USB_USE_USB1 if (&USBD1 == usbp) { + #if STM32_USB1_HP_NUMBER != STM32_USB1_LP_NUMBER nvicDisableVector(STM32_USB1_HP_NUMBER); #endif nvicDisableVector(STM32_USB1_LP_NUMBER); + STM32_USB->CNTR = CNTR_PDWN | CNTR_FRES; rccDisableUSB(); } @@ -538,10 +546,10 @@ void usb_lld_reset(USBDriver *usbp) { /* Post reset initialization.*/ STM32_USB->BTABLE = BTABLE_ADDR; - STM32_USB->ISTR = 0; + STM32_USB->ISTR = 0U; STM32_USB->DADDR = DADDR_EF; cntr = /* CNTR_ESOFM | */ CNTR_RESETM | CNTR_SUSPM | - CNTR_WKUPM | /* CNTR_ERRM | CNTR_PMAOVRM |*/ CNTR_CTRM; + CNTR_WKUPM | CNTR_ERRM |/* CNTR_PMAOVRM |*/ CNTR_CTRM; /* The SOF interrupt is only enabled if a callback is defined for this service because it is an high rate source.*/ if (usbp->config->sof_cb != NULL) @@ -553,7 +561,7 @@ void usb_lld_reset(USBDriver *usbp) { /* EP0 initialization.*/ usbp->epc[0] = &ep0config; - usb_lld_init_endpoint(usbp, 0); + usb_lld_init_endpoint(usbp, 0U); } /** @@ -609,7 +617,7 @@ void usb_lld_init_endpoint(USBDriver *usbp, usbep_t ep) { /* IN endpoint handling.*/ if (epcp->in_state != NULL) { - dp->TXCOUNT0 = 0; + dp->TXCOUNT0 = 0U; dp->TXADDR0 = usb_pm_alloc(usbp, epcp->in_maxsize); #if STM32_USB_USE_ISOCHRONOUS @@ -632,10 +640,10 @@ void usb_lld_init_endpoint(USBDriver *usbp, usbep_t ep) { /* Endpoint size and address initialization.*/ if (epcp->out_maxsize > 62) - nblocks = (((((epcp->out_maxsize - 1) | 0x1f) + 1) / 32) << 10) | + nblocks = (((((epcp->out_maxsize - 1U) | 0x1FU) + 1U) / 32U) << 10) | 0x8000; else - nblocks = ((((epcp->out_maxsize - 1) | 1) + 1) / 2) << 10; + nblocks = ((((epcp->out_maxsize - 1U) | 1U) + 1U) / 2U) << 10; dp->RXCOUNT0 = nblocks; dp->RXADDR0 = usb_pm_alloc(usbp, epcp->out_maxsize); @@ -771,8 +779,8 @@ void usb_lld_start_out(USBDriver *usbp, usbep_t ep) { USBOutEndpointState *osp = usbp->epc[ep]->out_state; /* Transfer initialization.*/ - if (osp->rxsize == 0) { /* Special case for zero sized packets.*/ - osp->rxpkts = 1; + if (osp->rxsize == 0U) { /* Special case for zero sized packets.*/ + osp->rxpkts = 1U; } else { osp->rxpkts = (uint16_t)((osp->rxsize + usbp->epc[ep]->out_maxsize - 1) / diff --git a/os/hal/ports/STM32/LLD/USBv2/hal_usb_lld.c b/os/hal/ports/STM32/LLD/USBv2/hal_usb_lld.c index 0a35dd01a..cd582fe29 100644 --- a/os/hal/ports/STM32/LLD/USBv2/hal_usb_lld.c +++ b/os/hal/ports/STM32/LLD/USBv2/hal_usb_lld.c @@ -168,16 +168,16 @@ static uint8_t ep0setup_buffer[8]; * @brief EP0 initialization structure. */ static const USBEndpointConfig ep0config = { - USB_EP_MODE_TYPE_CTRL, - _usb_ep0setup, - _usb_ep0in, - _usb_ep0out, - 0x40, - 0x40, - &ep0_state.in, - &ep0_state.out, - 1, - ep0setup_buffer + .ep_mode = USB_EP_MODE_TYPE_CTRL, + .setup_cb = _usb_ep0setup, + .in_cb = _usb_ep0in, + .out_cb = _usb_ep0out, + .in_maxsize = 0x40U, + .out_maxsize = 0x40U, + .in_state = &ep0_state.in, + .out_state = &ep0_state.out, + .ep_buffers = 1U, + .setup_buf = ep0setup_buffer }; /*===========================================================================*/ @@ -207,7 +207,7 @@ static uint32_t usb_pm_alloc(USBDriver *usbp, size_t size) { uint32_t next; next = usbp->pmnext; - usbp->pmnext += (size + 3) & ~3; + usbp->pmnext += (size + 3U) & ~3U; osalDbgAssert(usbp->pmnext <= STM32_USB_PMA_SIZE, "PMA overflow"); @@ -418,7 +418,7 @@ static void usb_serve_endpoints(USBDriver *usbp, uint32_t istr) { isp->txcnt += isp->txlast; n = isp->txsize - isp->txcnt; - if (n > 0) { + if (n > 0U) { /* Transfer not completed, there are more packets to send.*/ if (n > epcp->in_maxsize) n = epcp->in_maxsize; @@ -456,7 +456,7 @@ static void usb_serve_endpoints(USBDriver *usbp, uint32_t istr) { /* Transaction data updated.*/ osp->rxcnt += n; osp->rxsize -= n; - osp->rxpkts -= 1; + osp->rxpkts -= 1U; /* The transaction is completed if the specified number of packets has been received or the current packet is a short packet.*/ @@ -614,7 +614,7 @@ void usb_lld_start(USBDriver *usbp) { nvicEnableVector(STM32_USB1_LP_NUMBER, STM32_USB_USB1_LP_IRQ_PRIORITY); /* Releases the USB reset.*/ - STM32_USB->CNTR = 0; + STM32_USB->CNTR = 0U; } #endif /* Reset procedure enforced on driver start.*/ @@ -657,10 +657,10 @@ void usb_lld_reset(USBDriver *usbp) { uint32_t cntr; /* Post reset initialization.*/ - STM32_USB->ISTR = 0; + STM32_USB->ISTR = 0U; STM32_USB->DADDR = USB_DADDR_EF; cntr = /* USB_CNTR_ESOFM | */ USB_CNTR_RESETM | USB_CNTR_SUSPM | - USB_CNTR_WKUPM | /* USB_CNTR_ERRM | USB_CNTR_PMAOVRM |*/ USB_CNTR_CTRM; + USB_CNTR_WKUPM | USB_CNTR_ERRM |/* USB_CNTR_PMAOVRM |*/ USB_CNTR_CTRM; /* The SOF interrupt is only enabled if a callback is defined for this service because it is an high rate source.*/ if (usbp->config->sof_cb != NULL) @@ -672,7 +672,7 @@ void usb_lld_reset(USBDriver *usbp) { /* EP0 initialization.*/ usbp->epc[0] = &ep0config; - usb_lld_init_endpoint(usbp, 0); + usb_lld_init_endpoint(usbp, 0U); } /**