Skip to content

experimental - add ST NUCLEO L073RZ USB support #11360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions TESTS/host_tests/pyusb_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to have a specific PR for this "common" test part ?
(to be tested with all other targets?)

try:
num_bytes_written += ep_out.write(payload_out)
except usb.core.USBError:
Expand Down
41 changes: 41 additions & 0 deletions targets/TARGET_STM/TARGET_STM32L0/device/stm32l0xx_hal_pcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions targets/TARGET_STM/USBPhyHw.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 49 additions & 2 deletions targets/TARGET_STM/USBPhy_STM32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,33 @@ 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;
} else {
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 */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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);
Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion targets/targets.json
Original file line number Diff line number Diff line change
Expand Up @@ -3421,7 +3421,8 @@
"SERIAL_ASYNCH",
"TRNG",
"FLASH",
"MPU"
"MPU",
"USBDEVICE"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't be part of PR, as board doesn't have any USB connection.
This part has to be part of mbed_app.json

],
"release_versions": ["2", "5"],
"bootloader_supported": true,
Expand Down