5555#define  IOCON_PIO_MODE_INACT      0x00u   // No addition pin function
5656#define  IOCON_PIO_OPENDRAIN_DI    0x00u   // Open drain is disabled
5757#define  IOCON_PIO_SLEW_STANDARD   0x00u   // Standard mode, output slew rate control is enabled
58+ #define  IOCON_PIO_MODE_PULLUP     0x20u   /*!<@brief Selects pull-up function */ 
5859
5960#define  IOCON_PIO_DIG_FUNC0_EN    (IOCON_PIO_DIGITAL_EN | IOCON_PIO_FUNC0) // Digital pin function 0 enabled
6061#define  IOCON_PIO_DIG_FUNC1_EN    (IOCON_PIO_DIGITAL_EN | IOCON_PIO_FUNC1) // Digital pin function 1 enabled
@@ -197,13 +198,14 @@ void board_init(void) {
197198  USART_Init (UART_DEV , & uart_config , 12000000 );
198199#endif 
199200
200-    // USB VBUS 
201+ #if  ( CFG_TUD_ENABLED   &&   BOARD_TUD_RHPORT   ==   0 )  ||  ( CFG_TUH_ENABLED   &&   BOARD_TUH_RHPORT   ==   0 ) 
201202  /* PORT0 PIN22 configured as USB0_VBUS */ 
202203  IOCON_PinMuxSet (IOCON , 0U , 22U , IOCON_PIO_DIG_FUNC7_EN );
203- 
204- #if  defined(BOARD_TUD_RHPORT )  &&  BOARD_TUD_RHPORT  ==  0 
205204  // Port0 is Full Speed 
206205
206+   NVIC_ClearPendingIRQ (USB0_IRQn );
207+   NVIC_ClearPendingIRQ (USB0_NEEDCLK_IRQn );
208+ 
207209  /* Turn on USB0 Phy */ 
208210  POWER_DisablePD (kPDRUNCFG_PD_USB0_PHY );
209211
@@ -212,21 +214,55 @@ void board_init(void) {
212214  RESET_PeripheralReset (kUSB0HSL_RST_SHIFT_RSTn );
213215  RESET_PeripheralReset (kUSB0HMR_RST_SHIFT_RSTn );
214216
215-   // Enable USB Clock Adjustments to trim the FRO for the full speed controller 
216-   ANACTRL -> FRO192M_CTRL  |= ANACTRL_FRO192M_CTRL_USBCLKADJ_MASK ;
217-   CLOCK_SetClkDiv (kCLOCK_DivUsb0Clk , 1 , false);
218-   CLOCK_AttachClk (kFRO_HF_to_USB0_CLK );
219- 
220-   /*According to reference manual, device mode setting has to be set by access usb host register */ 
221-   CLOCK_EnableClock (kCLOCK_Usbhsl0 );  // enable usb0 host clock 
222-   USBFSH -> PORTMODE  |= USBFSH_PORTMODE_DEV_ENABLE_MASK ;
223-   CLOCK_DisableClock (kCLOCK_Usbhsl0 ); // disable usb0 host clock 
224- 
225-   /* enable USB Device clock */ 
226-   CLOCK_EnableUsbfs0DeviceClock (kCLOCK_UsbfsSrcFro , CLOCK_GetFreq (kCLOCK_FroHf ));
217+   if  (BOARD_TUD_RHPORT  ==  0 ) {
218+     // Enable USB Clock Adjustments to trim the FRO for the full speed controller 
219+     ANACTRL -> FRO192M_CTRL  |= ANACTRL_FRO192M_CTRL_USBCLKADJ_MASK ;
220+     CLOCK_SetClkDiv (kCLOCK_DivUsb0Clk , 1 , false);
221+     CLOCK_AttachClk (kFRO_HF_to_USB0_CLK );
222+ 
223+     /*According to reference manual, device mode setting has to be set by access usb host register */ 
224+     CLOCK_EnableClock (kCLOCK_Usbhsl0 );  // enable usb0 host clock 
225+     USBFSH -> PORTMODE  |= USBFSH_PORTMODE_DEV_ENABLE_MASK ;
226+     CLOCK_DisableClock (kCLOCK_Usbhsl0 ); // disable usb0 host clock 
227+     /* enable USB Device clock */ 
228+     CLOCK_EnableUsbfs0DeviceClock (kCLOCK_UsbfsSrcFro , CLOCK_GetFreq (kCLOCK_FroHf ));
229+   } else  {
230+     const  uint32_t  port1_pin12_config  =  (/* Pin is configured as USB0_PORTPWRN */ 
231+                                          IOCON_PIO_FUNC4  |
232+                                          /* Selects pull-up function */ 
233+                                          IOCON_PIO_MODE_PULLUP  |
234+                                          /* Standard mode, output slew rate control is enabled */ 
235+                                          IOCON_PIO_SLEW_STANDARD  |
236+                                          /* Input function is not inverted */ 
237+                                          IOCON_PIO_INV_DI  |
238+                                          /* Enables digital function */ 
239+                                          IOCON_PIO_DIGITAL_EN  |
240+                                          /* Open drain is disabled */ 
241+                                          IOCON_PIO_OPENDRAIN_DI );
242+     /* PORT1 PIN12 (coords: 67) is configured as USB0_PORTPWRN */ 
243+     IOCON_PinMuxSet (IOCON , 1U , 12U , port1_pin12_config );
244+ 
245+     const  uint32_t  port0_pin28_config  =  (/* Pin is configured as USB0_OVERCURRENTN */ 
246+                                          IOCON_PIO_FUNC7  |
247+                                          /* Selects pull-up function */ 
248+                                          IOCON_PIO_MODE_PULLUP  |
249+                                          /* Standard mode, output slew rate control is enabled */ 
250+                                          IOCON_PIO_SLEW_STANDARD  |
251+                                          /* Input function is not inverted */ 
252+                                          IOCON_PIO_INV_DI  |
253+                                          /* Enables digital function */ 
254+                                          IOCON_PIO_DIGITAL_EN  |
255+                                          /* Open drain is disabled */ 
256+                                          IOCON_PIO_OPENDRAIN_DI );
257+     /* PORT0 PIN28 (coords: 66) is configured as USB0_OVERCURRENTN */ 
258+     IOCON_PinMuxSet (IOCON , 0U , 28U , port0_pin28_config );
259+ 
260+     CLOCK_EnableUsbfs0HostClock (kCLOCK_UsbfsSrcPll1 , 48000000U );
261+     USBFSH -> PORTMODE  &= ~USBFSH_PORTMODE_DEV_ENABLE_MASK ;
262+   }
227263#endif 
228264
229- #if  defined( BOARD_TUD_RHPORT )   &&  BOARD_TUD_RHPORT  ==  1 
265+ #if  ( CFG_TUD_ENABLED   &&   BOARD_TUD_RHPORT   ==   1 )  ||  ( CFG_TUH_ENABLED   &&  BOARD_TUH_RHPORT  ==  1 ) 
230266  // Port1 is High Speed 
231267
232268  /* Turn on USB1 Phy */ 
@@ -266,9 +302,9 @@ void board_init(void) {
266302//  phytx |= USBPHY_TX_D_CAL(0x0C) | USBPHY_TX_TXCAL45DP(0x06) | USBPHY_TX_TXCAL45DM(0x06); 
267303//  USBPHY->TX = phytx; 
268304
269-      ARM_MPU_SetMemAttr (0 , 0x44 ); // Normal memory, non-cacheable (inner and outer) 
270-      ARM_MPU_SetRegion (0 , ARM_MPU_RBAR (0x40100000 , ARM_MPU_SH_NON , 0 , 1 , 1 ), ARM_MPU_RLAR (0x40104000 , 0 ));
271-      ARM_MPU_Enable (MPU_CTRL_PRIVDEFENA_Msk  | MPU_CTRL_HFNMIENA_Msk );
305+   ARM_MPU_SetMemAttr (0 , 0x44 ); // Normal memory, non-cacheable (inner and outer) 
306+   ARM_MPU_SetRegion (0 , ARM_MPU_RBAR (0x40100000 , ARM_MPU_SH_NON , 0 , 1 , 1 ), ARM_MPU_RLAR (0x40104000 , 0 ));
307+   ARM_MPU_Enable (MPU_CTRL_PRIVDEFENA_Msk  | MPU_CTRL_HFNMIENA_Msk );
272308#endif 
273309}
274310
0 commit comments