diff --git a/TESTS/host_tests/pyusb_basic.py b/TESTS/host_tests/pyusb_basic.py index 2f39678fe29..d28a444d631 100644 --- a/TESTS/host_tests/pyusb_basic.py +++ b/TESTS/host_tests/pyusb_basic.py @@ -1369,8 +1369,7 @@ def ep_test_abort(dev, log, verbose=False): payload_size = (NUM_PACKETS_UNTIL_ABORT + NUM_PACKETS_AFTER_ABORT) * ep_out.wMaxPacketSize num_bytes_written = 0 while num_bytes_written < payload_size: - payload_out = array.array('B', (num_bytes_written/ep_out.wMaxPacketSize - for _ in range(ep_out.wMaxPacketSize))) + payload_out = array.array('B', ([num_bytes_written//ep_out.wMaxPacketSize] * ep_out.wMaxPacketSize)) try: num_bytes_written += ep_out.write(payload_out) except usb.core.USBError: diff --git a/targets/TARGET_STM/TARGET_STM32L0/device/stm32l0xx_hal_pcd.c b/targets/TARGET_STM/TARGET_STM32L0/device/stm32l0xx_hal_pcd.c index 9cf95a751e6..df9be4070e9 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/device/stm32l0xx_hal_pcd.c +++ b/targets/TARGET_STM/TARGET_STM32L0/device/stm32l0xx_hal_pcd.c @@ -627,6 +627,47 @@ void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd) * @{ */ + // MBED PATCH + /** + * @brief Abort a transaction. + * @param hpcd: PCD handle + * @param ep_addr: endpoint address + * @retval HAL status + */ + HAL_StatusTypeDef HAL_PCD_EP_Abort(PCD_HandleTypeDef *hpcd, uint8_t ep_addr) + { + HAL_StatusTypeDef ret = HAL_OK; + PCD_EPTypeDef *ep; + + if ((0x80 & ep_addr) == 0x80) + { + ep = &hpcd->IN_ep[ep_addr & 0x7F]; + } + else + { + ep = &hpcd->OUT_ep[ep_addr]; + } + + __HAL_LOCK(hpcd); + + ep->num = ep_addr & 0x7F; + ep->is_in = ((ep_addr & 0x80) == 0x80); + + if (ep->is_in) + { + PCD_SET_EP_TX_STATUS(hpcd->Instance, ep->num, USB_EP_TX_NAK); + } + else + { + PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_DIS); + } + + __HAL_UNLOCK(hpcd); + + return ret; + } + // MBED PATCH + /** * @brief Connect the USB device * @param hpcd: PCD handle diff --git a/targets/TARGET_STM/TARGET_STM32L0/device/stm32l0xx_hal_pcd.h b/targets/TARGET_STM/TARGET_STM32L0/device/stm32l0xx_hal_pcd.h index d3af2f86f24..439a1a3dc37 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/device/stm32l0xx_hal_pcd.h +++ b/targets/TARGET_STM/TARGET_STM32L0/device/stm32l0xx_hal_pcd.h @@ -743,6 +743,7 @@ void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd); * @{ */ /* Peripheral Control functions ************************************************/ +HAL_StatusTypeDef HAL_PCD_EP_Abort(PCD_HandleTypeDef *hpcd, uint8_t ep_addr);//MBED PATCH HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd); HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd); HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address); diff --git a/targets/TARGET_STM/USBPhyHw.h b/targets/TARGET_STM/USBPhyHw.h index 698ebb23558..55f66131d17 100644 --- a/targets/TARGET_STM/USBPhyHw.h +++ b/targets/TARGET_STM/USBPhyHw.h @@ -32,6 +32,8 @@ defined(TARGET_DISCO_F769NI) || \ defined(TARGET_DISCO_F746NG_OTG_HS) #define USBHAL_IRQn OTG_HS_IRQn +#elif defined(TARGET_NUCLEO_L073RZ) +#define USBHAL_IRQn USB_IRQn #else #define USBHAL_IRQn OTG_FS_IRQn #endif diff --git a/targets/TARGET_STM/USBPhy_STM32.cpp b/targets/TARGET_STM/USBPhy_STM32.cpp index e5ff9c16749..dcea98d7c36 100644 --- a/targets/TARGET_STM/USBPhy_STM32.cpp +++ b/targets/TARGET_STM/USBPhy_STM32.cpp @@ -50,6 +50,7 @@ static const uint32_t tx_ep_sizes[NUM_ENDPOINTS] = { uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo) { +#ifndef TARGET_NUCLEO_L073RZ uint32_t len; if (fifo == 0) { len = hpcd->Instance->DIEPTXF0_HNPTXFSIZ >> 16; @@ -57,15 +58,25 @@ uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo) len = hpcd->Instance->DIEPTXF[fifo - 1] >> 16; } return len * 4; +#else + return 1024; +#endif } void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) { USBPhyHw *priv = ((USBPhyHw *)(hpcd->pData)); +#ifndef TARGET_NUCLEO_L073RZ USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; if (priv->sof_enabled) { priv->events->sof((USBx_DEVICE->DSTS & USB_OTG_DSTS_FNSOF) >> 8); } +#else + uint32_t sofnum = (hpcd->Instance->FNR) & USB_FNR_FN; + if (priv->sof_enabled) { + priv->events->sof(sofnum); + } +#endif } /* this call at device reception completion on a Out Enpoint */ @@ -186,6 +197,11 @@ void USBPhyHw::init(USBPhyEvents *events) hpcd.Init.phy_itface = PCD_PHY_EMBEDDED; hpcd.Init.Sof_enable = 1; hpcd.Init.speed = PCD_SPEED_HIGH; +#elif defined(TARGET_NUCLEO_L073RZ) + hpcd.Instance = USB; + hpcd.Init.phy_itface = PCD_PHY_EMBEDDED; + hpcd.Init.Sof_enable = 1; + hpcd.Init.speed = PCD_SPEED_FULL; #else hpcd.Instance = USB_OTG_FS; hpcd.Init.phy_itface = PCD_PHY_EMBEDDED; @@ -290,7 +306,11 @@ void USBPhyHw::init(USBPhyEvents *events) pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); // DM pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); // DP __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); - +#elif defined(TARGET_NUCLEO_L073RZ) + __HAL_RCC_GPIOA_CLK_ENABLE(); + pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF0_USB)); // DM + pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF0_USB)); // DP + __HAL_RCC_USB_CLK_ENABLE(); #else #error "USB pins are not configured !" #endif @@ -302,7 +322,20 @@ void USBPhyHw::init(USBPhyEvents *events) hpcd.State = HAL_PCD_STATE_RESET; HAL_PCD_Init(&hpcd); - +#if defined(TARGET_NUCLEO_L073RZ) + // EP0 + HAL_PCDEx_PMAConfig(&hpcd, 0x00, PCD_SNG_BUF, 0x30); + HAL_PCDEx_PMAConfig(&hpcd, 0x80, PCD_SNG_BUF, 0x70); + // EP1 + HAL_PCDEx_PMAConfig(&hpcd, 0x01, PCD_SNG_BUF, 0xB0); + HAL_PCDEx_PMAConfig(&hpcd, 0x81, PCD_SNG_BUF, 0xF0); + // EP2 + HAL_PCDEx_PMAConfig(&hpcd, 0x02, PCD_SNG_BUF, 0x130); + HAL_PCDEx_PMAConfig(&hpcd, 0x82, PCD_SNG_BUF, 0x170); + // EP3 + HAL_PCDEx_PMAConfig(&hpcd, 0x03, PCD_DBL_BUF, 0x01F001B0); + HAL_PCDEx_PMAConfig(&hpcd, 0x83, PCD_SNG_BUF, 0x230); +#else uint32_t total_bytes = 0; /* Reserve space in the RX buffer for: @@ -324,6 +357,7 @@ void USBPhyHw::init(USBPhyEvents *events) /* 1.25 kbytes */ MBED_ASSERT(total_bytes <= 1280); +#endif // Configure interrupt vector NVIC_SetVector(USBHAL_IRQn, (uint32_t)&_usbisr); @@ -349,12 +383,25 @@ bool USBPhyHw::powered() void USBPhyHw::connect() { +#if defined(TARGET_NUCLEO_L073RZ) + /*set wInterrupt_Mask global variable*/ + uint32_t wInterrupt_Mask = USB_CNTR_CTRM | USB_CNTR_WKUPM | USB_CNTR_SUSPM | USB_CNTR_ERRM | + USB_CNTR_SOFM | USB_CNTR_ESOFM | USB_CNTR_RESETM; + /*Set interrupt mask*/ + hpcd.Instance->CNTR = wInterrupt_Mask; + HAL_PCD_DevConnect(&hpcd); +#else HAL_PCD_Start(&hpcd); +#endif } void USBPhyHw::disconnect() { HAL_PCD_Stop(&hpcd); +#if defined(TARGET_NUCLEO_L073RZ) + HAL_PCD_DevDisconnect(&hpcd); + HAL_Delay(4U); +#endif } void USBPhyHw::configure() diff --git a/targets/targets.json b/targets/targets.json index 78e4170cb4c..9b8d4e7c9d3 100644 --- a/targets/targets.json +++ b/targets/targets.json @@ -3421,7 +3421,8 @@ "SERIAL_ASYNCH", "TRNG", "FLASH", - "MPU" + "MPU", + "USBDEVICE" ], "release_versions": ["2", "5"], "bootloader_supported": true,