55 */
66#include "usbd_core.h"
77
8- #if (CONFIG_USB_DBG_LEVEL >= USB_DBG_LOG )
8+ #if defined( CONFIG_USB_DBG_LEVEL ) && defined( USB_DBG_LOG ) && (CONFIG_USB_DBG_LEVEL >= USB_DBG_LOG )
99#error "fsdev cannot enable USB_DBG_LOG"
1010#endif
1111
@@ -60,10 +60,30 @@ __WEAK void usb_dc_low_level_deinit(void)
6060{
6161}
6262
63+ /**
64+ * @brief Initialize the USB device controller.
65+ * @param busid USB bus index (fsdev supports a single instance, bus 0).
66+ * @retval 0 on success.
67+ */
6368int usb_dc_init (uint8_t busid )
6469{
6570 usb_dc_low_level_init ();
6671
72+ #if defined(N32H4X_FSDEV )
73+ /* N32H4x power-on sequence: the USB_CTRL reset value is 0x0300
74+ * -- PD(bit9, power-down) and FRST(bit8, force-reset) are both set.
75+ * 1. Clear PD, wait for the internal analog voltage reference to settle.
76+ * 2. Clear FRST.
77+ * 3. Clear the STS register to drop pending interrupts. */
78+ USB -> CNTR &= (uint32_t )~USB_CNTR_PDWN ; /* exit power-down */
79+
80+ /* Wait for the internal reference voltage to stabilise. */
81+ for (volatile uint32_t i = 0U ; i < 1000U ; i ++ ) {
82+ }
83+
84+ USB -> CNTR &= (uint32_t )~USB_CNTR_FRES ; /* clear force-reset flag */
85+ USB -> ISTR = 0U ; /* clear pending interrupts */
86+ #else
6787 /* Init Device */
6888 /* CNTR_FRES = 1 */
6989 USB -> CNTR = (uint16_t )USB_CNTR_FRES ;
@@ -73,6 +93,7 @@ int usb_dc_init(uint8_t busid)
7393
7494 /* Clear pending interrupts */
7595 USB -> ISTR = 0U ;
96+ #endif
7697
7798 /*Set Btable Address*/
7899 USB -> BTABLE = BTABLE_ADDRESS ;
@@ -91,8 +112,13 @@ int usb_dc_init(uint8_t busid)
91112 /* Set interrupt mask */
92113 USB -> CNTR = (uint16_t )winterruptmask ;
93114
115+ #if defined(N32H4X_FSDEV )
116+ /* Enabling DP Pull-UP through USB_CTRL.PU */
117+ USB -> CNTR |= (uint32_t )USB_CNTR_PU ;
118+ #else
94119 /* Enabling DP Pull-UP bit to Connect internal PU resistor on USB DP line */
95120 USB -> BCDR |= (uint16_t )USB_BCDR_DPPU ;
121+ #endif
96122
97123 return 0 ;
98124}
@@ -325,6 +351,10 @@ int usbd_ep_start_read(uint8_t busid, const uint8_t ep, uint8_t *data, uint32_t
325351 return 0 ;
326352}
327353
354+ /**
355+ * @brief USB FS device interrupt handler: dispatches ISTR events.
356+ * @param busid USB bus index (fsdev supports a single instance, bus 0).
357+ */
328358void USBD_IRQHandler (uint8_t busid )
329359{
330360 uint16_t wIstr , wEPVal ;
@@ -339,7 +369,13 @@ void USBD_IRQHandler(uint8_t busid)
339369 wIstr = USB -> ISTR ;
340370
341371 /* extract highest priority endpoint number */
372+ #if defined(N32H4X_FSDEV )
373+ /* EP_ID is at STS bits [11:8] */
374+ ep_idx = (uint8_t )((wIstr & USB_ISTR_EP_ID ) >> USB_ISTR_EP_ID_Pos );
375+ #else
376+ /* ST: EP_ID is at ISTR bits [3:0] */
342377 ep_idx = (uint8_t )(wIstr & USB_ISTR_EP_ID );
378+ #endif
343379
344380 if (ep_idx == 0U ) {
345381 if ((wIstr & USB_ISTR_DIR ) == 0U ) {
0 commit comments