1414#include "common-hal/microcontroller/Pin.h"
1515
1616static void init_usb_vbus_sense (void ) {
17-
1817 #if (BOARD_NO_VBUS_SENSE )
19- // Disable VBUS sensing
20- #ifdef USB_OTG_GCCFG_VBDEN
21- // Deactivate VBUS Sensing B
22- USB_OTG_FS -> GCCFG &= ~USB_OTG_GCCFG_VBDEN ;
23-
24- #if (BOARD_NO_USB_OTG_ID_SENSE )
25- USB_OTG_FS -> GUSBCFG &= ~USB_OTG_GUSBCFG_FHMOD ;
26- USB_OTG_FS -> GUSBCFG |= USB_OTG_GUSBCFG_FDMOD ;
27- #endif
28-
29- // B-peripheral session valid override enable
30- USB_OTG_FS -> GOTGCTL |= USB_OTG_GOTGCTL_BVALOEN ;
31- USB_OTG_FS -> GOTGCTL |= USB_OTG_GOTGCTL_BVALOVAL ;
32- #elif !defined( STM32L433XX )
33- USB_OTG_FS -> GCCFG |= USB_OTG_GCCFG_NOVBUSSENS ;
34- USB_OTG_FS -> GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN ;
35- USB_OTG_FS -> GCCFG &= ~USB_OTG_GCCFG_VBUSASEN ;
36- #endif
18+ // Disable VBUS sensing
19+ #ifdef USB_OTG_GCCFG_VBDEN
20+ // Deactivate VBUS Sensing B
21+ USB_OTG_FS -> GCCFG &= ~USB_OTG_GCCFG_VBDEN ;
22+
23+ #if (BOARD_NO_USB_OTG_ID_SENSE )
24+ USB_OTG_FS -> GUSBCFG &= ~USB_OTG_GUSBCFG_FHMOD ;
25+ USB_OTG_FS -> GUSBCFG |= USB_OTG_GUSBCFG_FDMOD ;
26+ #endif // BOARD_NO_USB_OTG_ID_SENSE
27+
28+ // B-peripheral session valid override enable
29+ USB_OTG_FS -> GOTGCTL |= USB_OTG_GOTGCTL_BVALOEN ;
30+ USB_OTG_FS -> GOTGCTL |= USB_OTG_GOTGCTL_BVALOVAL ;
31+
32+ USB_OTG_FS -> GCCFG |= USB_OTG_GCCFG_NOVBUSSENS ;
33+ USB_OTG_FS -> GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN ;
34+ USB_OTG_FS -> GCCFG &= ~USB_OTG_GCCFG_VBUSASEN ;
35+ #endif
3736 #else
38- // Enable VBUS hardware sensing
39- #ifdef USB_OTG_GCCFG_VBDEN
40- USB_OTG_FS -> GCCFG |= USB_OTG_GCCFG_VBDEN ;
41- #else
42- USB_OTG_FS -> GCCFG &= ~USB_OTG_GCCFG_NOVBUSSENS ;
43- USB_OTG_FS -> GCCFG |= USB_OTG_GCCFG_VBUSBSEN ; // B Device sense
44- #endif
37+ // Enable VBUS hardware sensing
38+ #ifdef USB_OTG_GCCFG_VBDEN
39+ USB_OTG_FS -> GCCFG |= USB_OTG_GCCFG_VBDEN ;
40+ #else
41+ USB_OTG_FS -> GCCFG &= ~USB_OTG_GCCFG_NOVBUSSENS ;
42+ USB_OTG_FS -> GCCFG |= USB_OTG_GCCFG_VBUSBSEN ; // B Device sense
43+ #endif
4544 #endif
4645}
4746
@@ -69,12 +68,15 @@ void init_usb_hardware(void) {
6968 GPIO_InitStruct .Pull = GPIO_NOPULL ;
7069 #if CPY_STM32H7
7170 GPIO_InitStruct .Alternate = GPIO_AF10_OTG1_FS ;
72- #elif CPY_STM32F4 || CPY_STM32F7 || defined(STM32L4R5XX )
71+ #elif CPY_STM32F4 || CPY_STM32F7 || defined(STM32L4R5xx )
7372 GPIO_InitStruct .Alternate = GPIO_AF10_OTG_FS ;
74- #elif defined(STM32L433XX )
73+ #elif defined(STM32L433xx )
7574 GPIO_InitStruct .Alternate = GPIO_AF10_USB_FS ;
75+ #else
76+ #error Unknown MCU
7677 #endif
7778 HAL_GPIO_Init (GPIOA , & GPIO_InitStruct );
79+
7880 never_reset_pin_number (0 , 11 );
7981 never_reset_pin_number (0 , 12 );
8082 claim_pin (0 , 11 );
@@ -119,7 +121,7 @@ void init_usb_hardware(void) {
119121 #if CPY_STM32H7
120122 HAL_PWREx_EnableUSBVoltageDetector ();
121123 __HAL_RCC_USB2_OTG_FS_CLK_ENABLE ();
122- #elif CPY_STM32F4 || CPY_STM32F7 || defined(STM32L4R5XX )
124+ #elif CPY_STM32F4 || CPY_STM32F7 || defined(STM32L4R5xx )
123125 /* Peripheral clock enable */
124126 __HAL_RCC_USB_OTG_FS_CLK_ENABLE ();
125127 #else
@@ -129,6 +131,11 @@ void init_usb_hardware(void) {
129131 init_usb_vbus_sense ();
130132}
131133
132- void OTG_FS_IRQHandler (void ) {
134+ #if defined(STM32L433xx )
135+ void USB_IRQHandler (void )
136+ #else
137+ void OTG_FS_IRQHandler (void )
138+ #endif
139+ {
133140 usb_irq_handler (0 );
134141}
0 commit comments