1- From 9ff9911d012975a5999d06d306e5f5041a454d4b Mon Sep 17 00:00:00 2001
1+ From c9e850ec37c80401546158efbf2c9f655380e3d8 Mon Sep 17 00:00:00 2001
22From: Martino Facchin <m.facchin@arduino.cc>
33Date: Thu, 11 Mar 2021 11:50:18 +0100
4- Subject: [PATCH 66/67 ] STM32: USB: allow dynamic USB speed configuration based
4+ Subject: [PATCH 2/5 ] STM32: USB: allow dynamic USB speed configuration based
55 on bootloader parameters
66
77---
8- targets/TARGET_STM/USBPhy_STM32.cpp | 38 ++++++++++++++++++++++++++- --
9- 1 file changed, 34 insertions(+), 4 deletions(-)
8+ targets/TARGET_STM/USBPhy_STM32.cpp | 68 +++++++++++++++++++++++++++ --
9+ 1 file changed, 64 insertions(+), 4 deletions(-)
1010
1111diff --git a/targets/TARGET_STM/USBPhy_STM32.cpp b/targets/TARGET_STM/USBPhy_STM32.cpp
12- index 20eaa38278..060e8394ad 100644
12+ index 20eaa38278..bf29c50f6b 100644
1313--- a/targets/TARGET_STM/USBPhy_STM32.cpp
1414+++ b/targets/TARGET_STM/USBPhy_STM32.cpp
15- @@ -201,6 +201,16 @@ void USB_reenumerate()
15+ @@ -201,10 +201,29 @@ void USB_reenumerate()
1616 }
1717 #endif
1818
1919+ #ifdef USB_DYNAMIC_CONFIGURATION
20- + uint8_t getUSBSpeedFromBlootloader() {
20+ + #undef USBHAL_IRQn
21+ +
22+ + uint8_t getUSBSpeedFromBootloader() {
2123+ // 1 -> USE_USB_OTG_HS
2224+ // 2 -> USE_USB_OTG_FS
2325+ // 3 -> USE_USB_OTG_HS_IN_FS
@@ -29,49 +31,60 @@ index 20eaa38278..060e8394ad 100644
2931 void USBPhyHw::init(USBPhyEvents *events)
3032 {
3133 const PinMap *map = NULL;
32- @@ -224,7 +234,12 @@ void USBPhyHw::init(USBPhyEvents *events)
34+
35+ + #if defined(USB_DYNAMIC_CONFIGURATION)
36+ + auto USBHAL_IRQn = OTG_FS_IRQn;
37+ + if (getUSBSpeedFromBootloader() == 1) {
38+ + USBHAL_IRQn = OTG_HS_IRQn;
39+ + }
40+ + #endif
41+ +
42+ NVIC_DisableIRQ(USBHAL_IRQn);
43+
44+ if (this->events == NULL) {
45+ @@ -224,7 +243,12 @@ void USBPhyHw::init(USBPhyEvents *events)
3346 hpcd.Init.battery_charging_enable = DISABLE;
3447 #endif
3548
3649- #if (MBED_CONF_TARGET_USB_SPEED == USE_USB_OTG_HS)
3750+ #ifdef USB_DYNAMIC_CONFIGURATION
38- + if (getUSBSpeedFromBlootloader () == 1) {
51+ + if (getUSBSpeedFromBootloader () == 1) {
3952+ #endif
4053+
4154+ #if defined(USB_DYNAMIC_CONFIGURATION) || (MBED_CONF_TARGET_USB_SPEED == USE_USB_OTG_HS)
4255+
4356 hpcd.Instance = USB_OTG_HS;
4457 hpcd.Init.phy_itface = PCD_PHY_ULPI;
4558 hpcd.Init.Sof_enable = 1;
46- @@ -239,7 +254 ,12 @@ void USBPhyHw::init(USBPhyEvents *events)
59+ @@ -239,7 +263 ,12 @@ void USBPhyHw::init(USBPhyEvents *events)
4760 __HAL_RCC_USB_OTG_HS_ULPI_CLK_SLEEP_ENABLE();
4861 map = PinMap_USB_HS;
4962
5063- #elif (MBED_CONF_TARGET_USB_SPEED == USE_USB_HS_IN_FS)
5164+ #ifdef USB_DYNAMIC_CONFIGURATION
52- + } if (getUSBSpeedFromBlootloader () == 3) {
65+ + } if (getUSBSpeedFromBootloader () == 3) {
5366+ #endif
5467+ #endif
5568+
5669+ #if defined(USB_DYNAMIC_CONFIGURATION) || (MBED_CONF_TARGET_USB_SPEED == USE_USB_HS_IN_FS)
5770 hpcd.Instance = USB_OTG_HS;
5871 hpcd.Init.phy_itface = USB_OTG_EMBEDDED_PHY;
5972 hpcd.Init.Sof_enable = ENABLE;
60- @@ -259,7 +279 ,12 @@ void USBPhyHw::init(USBPhyEvents *events)
73+ @@ -259,7 +288 ,12 @@ void USBPhyHw::init(USBPhyEvents *events)
6174
6275 map = PinMap_USB_HS;
6376
6477- #elif (MBED_CONF_TARGET_USB_SPEED == USE_USB_OTG_FS)
6578+ #ifdef USB_DYNAMIC_CONFIGURATION
66- + } if (getUSBSpeedFromBlootloader () == 2) {
79+ + } if (getUSBSpeedFromBootloader () == 2) {
6780+ #endif
6881+ #endif
6982+
7083+ #if defined(USB_DYNAMIC_CONFIGURATION) || (MBED_CONF_TARGET_USB_SPEED == USE_USB_OTG_FS)
7184 hpcd.Instance = USB_OTG_FS;
7285 hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
7386 hpcd.Init.Sof_enable = 1;
74- @@ -276,7 +301 ,12 @@ void USBPhyHw::init(USBPhyEvents *events)
87+ @@ -276,7 +310 ,12 @@ void USBPhyHw::init(USBPhyEvents *events)
7588
7689 map = PinMap_USB_FS;
7790
@@ -85,6 +98,48 @@ index 20eaa38278..060e8394ad 100644
8598 hpcd.Instance = USB;
8699 hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
87100 hpcd.Init.speed = PCD_SPEED_FULL;
101+ @@ -386,6 +425,13 @@ void USBPhyHw::deinit()
102+ HAL_StatusTypeDef ret = HAL_PCD_DeInit(&hpcd);
103+ MBED_ASSERT(ret == HAL_OK);
104+
105+ + #if defined(USB_DYNAMIC_CONFIGURATION)
106+ + auto USBHAL_IRQn = OTG_FS_IRQn;
107+ + if (getUSBSpeedFromBootloader() == 1) {
108+ + USBHAL_IRQn = OTG_HS_IRQn;
109+ + }
110+ + #endif
111+ +
112+ NVIC_DisableIRQ(USBHAL_IRQn);
113+
114+ if (events != NULL) {
115+ @@ -620,6 +666,13 @@ void USBPhyHw::endpoint_abort(usb_ep_t endpoint)
116+
117+ void USBPhyHw::process()
118+ {
119+ + #if defined(USB_DYNAMIC_CONFIGURATION)
120+ + auto USBHAL_IRQn = OTG_FS_IRQn;
121+ + if (getUSBSpeedFromBootloader() == 1) {
122+ + USBHAL_IRQn = OTG_HS_IRQn;
123+ + }
124+ + #endif
125+ +
126+ HAL_PCD_IRQHandler(&instance->hpcd);
127+ // Re-enable interrupt
128+ NVIC_ClearPendingIRQ(USBHAL_IRQn);
129+ @@ -628,6 +681,13 @@ void USBPhyHw::process()
130+
131+ void USBPhyHw::_usbisr(void)
132+ {
133+ + #if defined(USB_DYNAMIC_CONFIGURATION)
134+ + auto USBHAL_IRQn = OTG_FS_IRQn;
135+ + if (getUSBSpeedFromBootloader() == 1) {
136+ + USBHAL_IRQn = OTG_HS_IRQn;
137+ + }
138+ + #endif
139+ +
140+ NVIC_DisableIRQ(USBHAL_IRQn);
141+ instance->events->start_process();
142+ }
88143- -
891442.30.1
90145
0 commit comments