Example #1
0
void __SVC_1(int ena)
{
#else
void USBD_Intr(int ena)
{
#endif

    if (ena) {
        NVIC_EnableIRQ(USB0_IRQn);          /* Enable USB interrupt */
    } else {
        NVIC_DisableIRQ(USB0_IRQn);         /* Disable USB interrupt */
    }
}


/*
 *  USB Device Initialize Function
 *   Called by the User to initialize USB Device
 *    Return Value:    None
 */

void USBD_Init(void)
{
    USBD_Intr(0);
    /* BASE_USB0_CLK */
    LPC_CGU->BASE_USB0_CLK =    (0x01 << 11) |       /* Autoblock En */
                                (0x07 << 24) ;       /* Clock source: PLL0 */
    LPC_CCU1->CLK_M4_USB0_CFG |= 1;

    while (!(LPC_CCU1->CLK_M4_USB0_STAT & 1));

    LPC_SCU->SFSP6_3 = 1;                 /* pwr en */
    LPC_SCU->SFSP6_6 = 3;                 /* pwr fault */
    LPC_SCU->SFSP8_1 = 1;                 /* port indicator LED control out 1 */
    LPC_SCU->SFSP8_2 = 1;                 /* port indicator LED control out 0 */
    LPC_USBx->USBCMD_D |= (1UL << 1);     /* usb reset */

    while (LPC_USBx->USBCMD_D & (1UL << 1));

    LPC_CREG->CREG0 &= ~(1 << 5);
    LPC_USBx->USBMODE_D  = 2 | (1UL << 3);/* device mode */
#if USBD_HS_ENABLE
    LPC_USBx->PORTSC1_D &= ~(1UL << 24);
#else
    LPC_USBx->PORTSC1_D |= (1UL << 24);
#endif
    LPC_USBx->OTGSC = 1 | (1UL << 3);
    Ep[EP_OUT_IDX(0)].maxPacket = USBD_MAX_PACKET0;
    LPC_USBx->USBINTR_D  = (1UL << 0) |   /* usb int enable */
                           (1UL << 2) |   /* port change detect int enable */
                           (1UL << 8) |   /* suspend int enable */
                           (1UL << 16) |  /* nak int enable */
                           (1UL << 6) |   /* reset int enable */
#ifdef __RTX
                           ((USBD_RTX_DevTask   != 0) ? (1UL << 7) : 0) |   /* SOF */
                           ((USBD_RTX_DevTask   != 0) ? (1UL << 1) : 0) ;   /* Error */
#else
                           ((USBD_P_SOF_Event   != 0) ? (1UL << 7) : 0) |   /* SOF */
                           ((USBD_P_Error_Event != 0) ? (1UL << 1) : 0) ;   /* Error */
#endif
    USBD_Reset();
    USBD_Intr(1);
}
void __SVC_1            (int ena) {
#else 
void USBD_Intr (int ena) {
#endif
  if (ena) {
    NVIC_EnableIRQ  (USB1_IRQn);        /* Enable USB interrupt               */
  } else {
    NVIC_DisableIRQ (USB1_IRQn);        /* Disable USB interrupt              */
  }
}


/*
 *  USB Device Initialize Function
 *   Called by the User to initialize USB Device
 *    Return Value:    None
 */

void USBD_Init (void) {

  USBD_Intr(0);

  /* BASE_USB1_CLK                                                            */
  LPC_CGU->BASE_USB1_CLK   = (0x01 << 11) |       /* Autoblock En             */
                             (0x0C << 24) ;       /* Clock source: IDIVA      */
  LPC_CCU1->CLK_M3_USB1_CFG |= 1;
  while (!(LPC_CCU1->CLK_M3_USB1_STAT & 1));
  LPC_CCU1->CLK_USB1_CFG |= 1;
  while (!(LPC_CCU1->CLK_USB1_STAT & 1));

  LPC_SCU->SFSP9_4 =      2;            /* USB1_IND0 function                 */
  LPC_SCU->SFSP9_3 =      2;            /* USB1_IND1 function                 */
  LPC_SCU->SFSP2_5 =     (2UL << 0 ) |  /* USB1_VBUS function                 */
                         (1UL << 3 ) |  /* enable pull down, repeater mode    */
                         (1UL << 6 );   /* input buffer enable                */

  LPC_USBx->USBCMD_D =   (1UL << 1 );   /* usb reset                          */
  while (LPC_USBx->USBCMD_D & 3);

  LPC_USBx->USBMODE_D  = (2UL << 0 ) |  /* device mode                        */
                         (1UL << 3 );   /* Setup lockout off                  */

  LPC_USBx->PORTSC1_D |= (1UL << 24) |  /* force full speed                   */
                         (3UL << 30);   /* 1.1 PHy (Full speed only)          */

  Ep[EP_OUT_IDX(0)].maxPacket = USBD_MAX_PACKET0;

  LPC_USBx->USBINTR_D  = (1UL << 0 ) |  /* usb int enable                     */
                         (1UL << 2 ) |  /* port change detect int enable      */
                         (1UL << 8 ) |  /* suspend int enable                 */
                         (1UL << 16) |  /* nak int enable                     */
                         (1UL << 6 ) |  /* reset int enable                   */
#ifdef __RTX
              ((USBD_RTX_DevTask   != 0) ? (1UL << 7) : 0) |   /* SOF         */
              ((USBD_RTX_DevTask   != 0) ? (1UL << 1) : 0) ;   /* Error       */
#else
              ((USBD_P_SOF_Event   != 0) ? (1UL << 7) : 0) |   /* SOF         */
              ((USBD_P_Error_Event != 0) ? (1UL << 1) : 0) ;   /* Error       */
#endif

  USBD_Reset();

  LPC_SCU->SFSUSB      = (1UL << 1 ) |  /* enable USB1                        */
                         (1UL << 4 );

  USBD_Intr(1);
}