Skip to content

Commit 1be0fde

Browse files
neilh10dpgeorge
authored andcommitted
stmhal: Enable two USB phys to be supported together.
This is refactoring to enable support for the two USB PHYs available on some STM32F4 processors to be used at the same time. The F405/7 & F429 have two USB PHYs, others such as the F411 only have one PHY. This has been tested separately on a pyb10 (USB_FS PHY) and F429DISC (USB_HS PHY) to be able to invoke a REPL/USB. I have modified a PYBV10 to support two PHYs. The long term objective is to support a 2nd USB PHY to be brought up as a USB HOST, and possibly a single USB PHY to be OTG.
1 parent 0891cf7 commit 1be0fde

File tree

7 files changed

+124
-81
lines changed

7 files changed

+124
-81
lines changed

stmhal/boards/PYBV10/stm32f4xx_hal_conf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
/* Exported constants --------------------------------------------------------*/
4848

4949
#define USE_USB_FS
50+
//#define USE_USB_HS
5051

5152
/* ########################## Module Selection ############################## */
5253
/**

stmhal/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ static inline mp_uint_t disable_irq(void) {
234234
#define free gc_free
235235
#define realloc gc_realloc
236236

237+
// see stm32f4XX_hal_conf.h USE_USB_FS & USE_USB_HS
238+
// at the moment only USB_FS is supported
237239
#define USE_DEVICE_MODE
238240
//#define USE_HOST_MODE
239241

stmhal/stm32_it.c

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@
8080
#include "dma.h"
8181

8282
extern void __fatal_error(const char*);
83-
extern PCD_HandleTypeDef pcd_handle;
84-
83+
extern PCD_HandleTypeDef pcd_fs_handle;
84+
extern PCD_HandleTypeDef pcd_hs_handle;
8585
/******************************************************************************/
8686
/* Cortex-M4 Processor Exceptions Handlers */
8787
/******************************************************************************/
@@ -305,28 +305,25 @@ void SysTick_Handler(void) {
305305
* @retval None
306306
*/
307307
#if defined(USE_USB_FS)
308-
#define OTG_XX_IRQHandler OTG_FS_IRQHandler
309-
#define OTG_XX_WKUP_IRQHandler OTG_FS_WKUP_IRQHandler
310-
#elif defined(USE_USB_HS)
311-
#define OTG_XX_IRQHandler OTG_HS_IRQHandler
312-
#define OTG_XX_WKUP_IRQHandler OTG_HS_WKUP_IRQHandler
308+
void OTG_FS_IRQHandler(void) {
309+
HAL_PCD_IRQHandler(&pcd_fs_handle);
310+
}
313311
#endif
314-
315-
#if defined(OTG_XX_IRQHandler)
316-
void OTG_XX_IRQHandler(void) {
317-
HAL_PCD_IRQHandler(&pcd_handle);
312+
#if defined(USE_USB_HS)
313+
void OTG_HS_IRQHandler(void) {
314+
HAL_PCD_IRQHandler(&pcd_hs_handle);
318315
}
319316
#endif
320317

318+
#if defined(USE_USB_FS) || defined(USE_USB_HS)
321319
/**
322-
* @brief This function handles USB OTG FS or HS Wakeup IRQ Handler.
323-
* @param None
320+
* @brief This function handles USB OTG Common FS/HS Wakeup functions.
321+
* @param *pcd_handle for FS or HS
324322
* @retval None
325323
*/
326-
#if defined(OTG_XX_WKUP_IRQHandler)
327-
void OTG_XX_WKUP_IRQHandler(void) {
324+
STATIC void OTG_CMD_WKUP_Handler(PCD_HandleTypeDef *pcd_handle) {
328325

329-
if ((&pcd_handle)->Init.low_power_enable) {
326+
if (pcd_handle->Init.low_power_enable) {
330327
/* Reset SLEEPDEEP bit of Cortex System Control Register */
331328
SCB->SCR &= (uint32_t)~((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
332329

@@ -353,16 +350,41 @@ void OTG_XX_WKUP_IRQHandler(void) {
353350
{}
354351

355352
/* ungate PHY clock */
356-
__HAL_PCD_UNGATE_PHYCLOCK((&pcd_handle));
353+
__HAL_PCD_UNGATE_PHYCLOCK(pcd_handle);
357354
}
358-
#ifdef USE_USB_FS
355+
356+
}
357+
#endif
358+
359+
#if defined(USE_USB_FS)
360+
/**
361+
* @brief This function handles USB OTG FS Wakeup IRQ Handler.
362+
* @param None
363+
* @retval None
364+
*/
365+
void OTG_FS_WKUP_IRQHandler(void) {
366+
367+
OTG_CMD_WKUP_Handler(&pcd_fs_handle);
368+
359369
/* Clear EXTI pending Bit*/
360370
__HAL_USB_FS_EXTI_CLEAR_FLAG();
361-
#elif defined(USE_USB_HS)
362-
/* Clear EXTI pending Bit*/
363-
__HAL_USB_HS_EXTI_CLEAR_FLAG();
371+
372+
}
364373
#endif
365374

375+
#if defined(USE_USB_HS)
376+
/**
377+
* @brief This function handles USB OTG HS Wakeup IRQ Handler.
378+
* @param None
379+
* @retval None
380+
*/
381+
void OTG_HS_WKUP_IRQHandler(void) {
382+
383+
OTG_CMD_WKUP_Handler(&pcd_hs_handle);
384+
385+
/* Clear EXTI pending Bit*/
386+
__HAL_USB_HS_EXTI_CLEAR_FLAG();
387+
366388
}
367389
#endif
368390

stmhal/stm32_it.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ void PendSV_Handler(void);
7474
void SysTick_Handler(void);
7575
#ifdef USE_USB_FS
7676
void OTG_FS_IRQHandler(void);
77-
#elif defined(USE_USB_HS)
77+
#endif
78+
#ifdef USE_USB_HS
7879
void OTG_HS_IRQHandler(void);
7980
#endif

stmhal/usb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ bool pyb_usb_dev_init(uint16_t vid, uint16_t pid, usb_device_mode_t mode, USBD_H
102102
if (USBD_SelectMode(mode, hid_info) != 0) {
103103
return false;
104104
}
105-
USBD_Init(&hUSBDDevice, (USBD_DescriptorsTypeDef*)&USBD_Descriptors, 0);
105+
USBD_Init(&hUSBDDevice, (USBD_DescriptorsTypeDef*)&USBD_Descriptors, USB_PHY_FS_ID);
106106
USBD_RegisterClass(&hUSBDDevice, &USBD_CDC_MSC_HID);
107107
USBD_CDC_RegisterInterface(&hUSBDDevice, (USBD_CDC_ItfTypeDef*)&USBD_CDC_fops);
108108
switch (pyb_usb_storage_medium) {

stmhal/usb.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ typedef enum {
4141
PYB_USB_STORAGE_MEDIUM_SDCARD,
4242
} pyb_usb_storage_medium_t;
4343

44+
typedef enum {
45+
USB_PHY_FS_ID = 0,
46+
USB_PHY_HS_ID = 1,
47+
} USB_PHY_ID;
48+
4449
extern mp_uint_t pyb_usb_flags;
4550
extern struct _USBD_HandleTypeDef hUSBDDevice;
4651
extern pyb_usb_storage_medium_t pyb_usb_storage_medium;

stmhal/usbd_conf.c

Lines changed: 70 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,18 @@
3434
#include "usbd_core.h"
3535
#include "py/obj.h"
3636
#include "irq.h"
37+
#include "usb.h"
3738

3839
/* Private typedef -----------------------------------------------------------*/
3940
/* Private define ------------------------------------------------------------*/
4041
/* Private macro -------------------------------------------------------------*/
4142
/* Private variables ---------------------------------------------------------*/
42-
PCD_HandleTypeDef pcd_handle;
43-
43+
#ifdef USE_USB_FS
44+
PCD_HandleTypeDef pcd_fs_handle;
45+
#endif
46+
#ifdef USE_USB_HS
47+
PCD_HandleTypeDef pcd_hs_handle;
48+
#endif
4449
/* Private function prototypes -----------------------------------------------*/
4550
/* Private functions ---------------------------------------------------------*/
4651

@@ -379,90 +384,97 @@ void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
379384
USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev)
380385
{
381386
#if defined(USE_USB_FS)
387+
if (pdev->id == USB_PHY_FS_ID)
388+
{
382389
/*Set LL Driver parameters */
383-
pcd_handle.Instance = USB_OTG_FS;
384-
pcd_handle.Init.dev_endpoints = 4;
385-
pcd_handle.Init.use_dedicated_ep1 = 0;
386-
pcd_handle.Init.ep0_mps = 0x40;
387-
pcd_handle.Init.dma_enable = 0;
388-
pcd_handle.Init.low_power_enable = 0;
389-
pcd_handle.Init.phy_itface = PCD_PHY_EMBEDDED;
390-
pcd_handle.Init.Sof_enable = 0;
391-
pcd_handle.Init.speed = PCD_SPEED_FULL;
390+
pcd_fs_handle.Instance = USB_OTG_FS;
391+
pcd_fs_handle.Init.dev_endpoints = 4;
392+
pcd_fs_handle.Init.use_dedicated_ep1 = 0;
393+
pcd_fs_handle.Init.ep0_mps = 0x40;
394+
pcd_fs_handle.Init.dma_enable = 0;
395+
pcd_fs_handle.Init.low_power_enable = 0;
396+
pcd_fs_handle.Init.phy_itface = PCD_PHY_EMBEDDED;
397+
pcd_fs_handle.Init.Sof_enable = 0;
398+
pcd_fs_handle.Init.speed = PCD_SPEED_FULL;
392399
#if !defined(MICROPY_HW_USB_VBUS_DETECT_PIN)
393-
pcd_handle.Init.vbus_sensing_enable = 0; // No VBUS Sensing on USB0
400+
pcd_fs_handle.Init.vbus_sensing_enable = 0; // No VBUS Sensing on USB0
394401
#else
395-
pcd_handle.Init.vbus_sensing_enable = 1;
402+
pcd_fs_handle.Init.vbus_sensing_enable = 1;
396403
#endif
397404
/* Link The driver to the stack */
398-
pcd_handle.pData = pdev;
399-
pdev->pData = &pcd_handle;
405+
pcd_fs_handle.pData = pdev;
406+
pdev->pData = &pcd_fs_handle;
400407
/*Initialize LL Driver */
401-
HAL_PCD_Init(&pcd_handle);
402-
403-
HAL_PCD_SetRxFiFo(&pcd_handle, 0x80);
404-
HAL_PCD_SetTxFiFo(&pcd_handle, 0, 0x20);
405-
HAL_PCD_SetTxFiFo(&pcd_handle, 1, 0x40);
406-
HAL_PCD_SetTxFiFo(&pcd_handle, 2, 0x20);
407-
HAL_PCD_SetTxFiFo(&pcd_handle, 3, 0x40);
408-
#elif defined(USE_USB_HS)
408+
HAL_PCD_Init(&pcd_fs_handle);
409+
410+
HAL_PCD_SetRxFiFo(&pcd_fs_handle, 0x80);
411+
HAL_PCD_SetTxFiFo(&pcd_fs_handle, 0, 0x20);
412+
HAL_PCD_SetTxFiFo(&pcd_fs_handle, 1, 0x40);
413+
HAL_PCD_SetTxFiFo(&pcd_fs_handle, 2, 0x20);
414+
HAL_PCD_SetTxFiFo(&pcd_fs_handle, 3, 0x40);
415+
}
416+
#endif
417+
#if defined(USE_USB_HS)
418+
if (pdev->id == USB_PHY_HS_ID)
419+
{
409420
#if defined(USE_USB_HS_IN_FS)
410421
/*Set LL Driver parameters */
411-
pcd_handle.Instance = USB_OTG_HS;
412-
pcd_handle.Init.dev_endpoints = 4;
413-
pcd_handle.Init.use_dedicated_ep1 = 0;
414-
pcd_handle.Init.ep0_mps = 0x40;
415-
pcd_handle.Init.dma_enable = 0;
416-
pcd_handle.Init.low_power_enable = 0;
417-
pcd_handle.Init.phy_itface = PCD_PHY_EMBEDDED;
418-
pcd_handle.Init.Sof_enable = 0;
419-
pcd_handle.Init.speed = PCD_SPEED_HIGH_IN_FULL;
422+
pcd_hs_handle.Instance = USB_OTG_HS;
423+
pcd_hs_handle.Init.dev_endpoints = 4;
424+
pcd_hs_handle.Init.use_dedicated_ep1 = 0;
425+
pcd_hs_handle.Init.ep0_mps = 0x40;
426+
pcd_hs_handle.Init.dma_enable = 0;
427+
pcd_hs_handle.Init.low_power_enable = 0;
428+
pcd_hs_handle.Init.phy_itface = PCD_PHY_EMBEDDED;
429+
pcd_hs_handle.Init.Sof_enable = 0;
430+
pcd_hs_handle.Init.speed = PCD_SPEED_HIGH_IN_FULL;
420431
#if !defined(MICROPY_HW_USB_VBUS_DETECT_PIN)
421-
pcd_handle.Init.vbus_sensing_enable = 0; // No VBUS Sensing on USB0
432+
pcd_hs_handle.Init.vbus_sensing_enable = 0; // No VBUS Sensing on USB0
422433
#else
423-
pcd_handle.Init.vbus_sensing_enable = 1;
434+
pcd_hs_handle.Init.vbus_sensing_enable = 1;
424435
#endif
425436
/* Link The driver to the stack */
426-
pcd_handle.pData = pdev;
427-
pdev->pData = &pcd_handle;
437+
pcd_hs_handle.pData = pdev;
438+
pdev->pData = &pcd_hs_handle;
428439
/*Initialize LL Driver */
429-
HAL_PCD_Init(&pcd_handle);
440+
HAL_PCD_Init(&pcd_hs_handle);
430441

431-
HAL_PCD_SetRxFiFo(&pcd_handle, 0x80);
432-
HAL_PCD_SetTxFiFo(&pcd_handle, 0, 0x20);
433-
HAL_PCD_SetTxFiFo(&pcd_handle, 1, 0x40);
434-
HAL_PCD_SetTxFiFo(&pcd_handle, 2, 0x20);
435-
HAL_PCD_SetTxFiFo(&pcd_handle, 3, 0x40);
442+
HAL_PCD_SetRxFiFo(&pcd_hs_handle, 0x80);
443+
HAL_PCD_SetTxFiFo(&pcd_hs_handle, 0, 0x20);
444+
HAL_PCD_SetTxFiFo(&pcd_hs_handle, 1, 0x40);
445+
HAL_PCD_SetTxFiFo(&pcd_hs_handle, 2, 0x20);
446+
HAL_PCD_SetTxFiFo(&pcd_hs_handle, 3, 0x40);
436447
#else // !defined(USE_USB_HS_IN_FS)
437448
/*Set LL Driver parameters */
438-
pcd_handle.Instance = USB_OTG_HS;
439-
pcd_handle.Init.dev_endpoints = 6;
440-
pcd_handle.Init.use_dedicated_ep1 = 0;
441-
pcd_handle.Init.ep0_mps = 0x40;
449+
pcd_hs_handle.Instance = USB_OTG_HS;
450+
pcd_hs_handle.Init.dev_endpoints = 6;
451+
pcd_hs_handle.Init.use_dedicated_ep1 = 0;
452+
pcd_hs_handle.Init.ep0_mps = 0x40;
442453

443454
/* Be aware that enabling USB-DMA mode will result in data being sent only by
444455
multiple of 4 packet sizes. This is due to the fact that USB-DMA does
445456
not allow sending data from non word-aligned addresses.
446457
For this specific application, it is advised to not enable this option
447458
unless required. */
448-
pcd_handle.Init.dma_enable = 0;
459+
pcd_hs_handle.Init.dma_enable = 0;
449460

450-
pcd_handle.Init.low_power_enable = 0;
451-
pcd_handle.Init.phy_itface = PCD_PHY_ULPI;
452-
pcd_handle.Init.Sof_enable = 0;
453-
pcd_handle.Init.speed = PCD_SPEED_HIGH;
454-
pcd_handle.Init.vbus_sensing_enable = 1;
461+
pcd_hs_handle.Init.low_power_enable = 0;
462+
pcd_hs_handle.Init.phy_itface = PCD_PHY_ULPI;
463+
pcd_hs_handle.Init.Sof_enable = 0;
464+
pcd_hs_handle.Init.speed = PCD_SPEED_HIGH;
465+
pcd_hs_handle.Init.vbus_sensing_enable = 1;
455466
/* Link The driver to the stack */
456-
pcd_handle.pData = pdev;
457-
pdev->pData = &pcd_handle;
467+
pcd_hs_handle.pData = pdev;
468+
pdev->pData = &pcd_hs_handle;
458469
/*Initialize LL Driver */
459-
HAL_PCD_Init(&pcd_handle);
470+
HAL_PCD_Init(&pcd_hs_handle);
460471

461-
HAL_PCD_SetRxFiFo(&pcd_handle, 0x200);
462-
HAL_PCD_SetTxFiFo(&pcd_handle, 0, 0x80);
463-
HAL_PCD_SetTxFiFo(&pcd_handle, 1, 0x174);
472+
HAL_PCD_SetRxFiFo(&pcd_hs_handle, 0x200);
473+
HAL_PCD_SetTxFiFo(&pcd_hs_handle, 0, 0x80);
474+
HAL_PCD_SetTxFiFo(&pcd_hs_handle, 1, 0x174);
464475

465476
#endif // !USE_USB_HS_IN_FS
477+
}
466478
#endif // USE_USB_HS
467479
return USBD_OK;
468480
}

0 commit comments

Comments
 (0)