/***************************************************************************//** * @brief * Initializes USB device hardware and internal protocol stack data structures, * then connects the data-line (D+ or D-) pullup resistor to signal host that * enumeration can begin. * * @note * You may later use @ref USBD_Disconnect() and @ref USBD_Connect() to force * reenumeration. * * @param[in] p * Pointer to device initialization struct. See @ref USBD_Init_TypeDef. * * @return * @ref USB_STATUS_OK on success, else an appropriate error code. ******************************************************************************/ int USBD_Init( const USBD_Init_TypeDef *p ) { USBD_Ep_TypeDef *ep; CORE_DECLARE_IRQ_STATE; #if !defined( USB_CORECLK_HFRCO ) || !defined( CMU_OSCENCMD_USHFRCOEN ) /* Devices supporting crystal-less USB can use HFRCO or HFXO as core clock. */ /* All other devices must use HFXO as core clock. */ if ( CMU_ClockSelectGet( cmuClock_HF ) != cmuSelect_HFXO ) { CMU_ClockSelectSet( cmuClock_HF, cmuSelect_HFXO ); } #endif #if !defined( CMU_OSCENCMD_USHFRCOEN ) #if ( USB_USBC_32kHz_CLK == USB_USBC_32kHz_CLK_LFXO ) CMU_OscillatorEnable(cmuOsc_LFXO, true, false); #else CMU_OscillatorEnable(cmuOsc_LFRCO, true, false); #endif #else CMU_ClockEnable(cmuClock_CORELE, true); /* LFC clock is needed to detect USB suspend when LEMIDLE is activated. */ #if ( USB_USBC_32kHz_CLK == USB_USBC_32kHz_CLK_LFXO ) CMU_ClockSelectSet(cmuClock_LFC, cmuSelect_LFXO); #else CMU_ClockSelectSet(cmuClock_LFC, cmuSelect_LFRCO); #endif CMU_ClockEnable(cmuClock_USBLE, true); #endif USBTIMER_Init(); memset( dev, 0, sizeof( USBD_Device_TypeDef ) ); dev->setup = dev->setupPkt; dev->state = USBD_STATE_LASTMARKER; dev->savedState = USBD_STATE_NONE; dev->lastState = USBD_STATE_NONE; dev->callbacks = p->callbacks; dev->remoteWakeupEnabled = false; /* Initialize EP0 */ ep = &dev->ep[ 0 ]; ep->in = false; ep->buf = NULL; ep->num = 0; ep->mask = 1; ep->addr = 0; ep->type = USB_EPTYPE_CTRL; ep->txFifoNum = 0; /* FIXME! */ ep->packetSize = 64; dev->ep0MpsCode = _USB_DOEP0CTL_MPS_64B; ep->remaining = 0; ep->xferred = 0; ep->state = D_EP_IDLE; ep->xferCompleteCb = NULL; ep->fifoSize = ep->packetSize / 4; totalTxFifoSize = ep->fifoSize * p->bufferingMultiplier[ 0 ]; totalRxFifoSize = (ep->fifoSize + 1) * p->bufferingMultiplier[ 0 ]; /* Rx-FIFO size: SETUP packets : 4*n + 6 n=#CTRL EP's * GOTNAK : 1 * Status info : 2*n n=#OUT EP's (EP0 included) in HW */ totalRxFifoSize += 10 + 1 + ( 2 * (MAX_NUM_OUT_EPS + 1) ); CORE_ENTER_CRITICAL(); /* Enable USB clock */ CMU->HFCORECLKEN0 |= CMU_HFCORECLKEN0_USB | CMU_HFCORECLKEN0_USBC; #if defined( CMU_OSCENCMD_USHFRCOEN ) CMU->USHFRCOCONF = CMU_USHFRCOCONF_BAND_48MHZ; CMU_ClockSelectSet( cmuClock_USBC, cmuSelect_USHFRCO ); /* Enable USHFRCO Clock Recovery mode. */ CMU->USBCRCTRL |= CMU_USBCRCTRL_EN; /* Turn on Low Energy Mode (LEM) features. */ USB->CTRL = USB_CTRL_LEMOSCCTRL_GATE | USB_CTRL_LEMIDLEEN | USB_CTRL_LEMPHYCTRL; #else CMU_ClockSelectSet( cmuClock_USBC, cmuSelect_HFCLK ); #endif USBHAL_DisableGlobalInt(); if ( USBDHAL_CoreInit( totalRxFifoSize, totalTxFifoSize ) == USB_STATUS_OK ) { USBDHAL_EnableUsbResetAndSuspendInt(); USBHAL_EnableGlobalInt(); NVIC_ClearPendingIRQ( USB_IRQn ); NVIC_EnableIRQ( USB_IRQn ); } else { CORE_EXIT_CRITICAL(); DEBUG_USB_API_PUTS( "\nUSBD_Init(), FIFO setup error" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } #if ( USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_ONVBUSOFF ) if ( USBHAL_VbusIsOn() ) { USBD_SetUsbState( USBD_STATE_POWERED ); } else #endif { USBD_SetUsbState( USBD_STATE_NONE ); } CORE_EXIT_CRITICAL(); return USB_STATUS_OK; }
/***************************************************************************//** * @brief * Initializes USB device hardware and internal protocol stack data structures, * then connects the data-line (D+ or D-) pullup resistor to signal host that * enumeration can begin. * * @note * You may later use @ref USBD_Disconnect() and @ref USBD_Connect() to force * reenumeration. * * @param[in] p * Pointer to device initialization struct. See @ref USBD_Init_TypeDef. * * @return * @ref USB_STATUS_OK on success, else an appropriate error code. ******************************************************************************/ int USBD_Init( const USBD_Init_TypeDef *p ) { int numEps; USBD_Ep_TypeDef *ep; uint8_t txFifoNum; uint8_t *conf, *confEnd; USB_EndpointDescriptor_TypeDef *epd; uint32_t totalRxFifoSize, totalTxFifoSize, numInEps, numOutEps; CMU_ClockSelectSet( cmuClock_HF, cmuSelect_HFXO ); #if ( USB_USBC_32kHz_CLK == USB_USBC_32kHz_CLK_LFXO ) CMU_OscillatorEnable( cmuOsc_LFXO, true, false ); #else CMU_OscillatorEnable( cmuOsc_LFRCO, true, false ); #endif USBTIMER_Init(); memset( dev, 0, sizeof( USBD_Device_TypeDef ) ); dev->setup = dev->setupPkt; dev->deviceDescriptor = p->deviceDescriptor; dev->configDescriptor = (USB_ConfigurationDescriptor_TypeDef*) p->configDescriptor; dev->stringDescriptors = p->stringDescriptors; dev->numberOfStrings = p->numberOfStrings; dev->state = USBD_STATE_LASTMARKER; dev->savedState = USBD_STATE_NONE; dev->lastState = USBD_STATE_NONE; dev->callbacks = p->callbacks; dev->remoteWakeupEnabled = false; /* Initialize EP0 */ ep = &dev->ep[ 0 ]; ep->in = false; ep->buf = NULL; ep->num = 0; ep->mask = 1; ep->addr = 0; ep->type = USB_EPTYPE_CTRL; ep->txFifoNum = 0; ep->packetSize = USB_EP0_SIZE; ep->remaining = 0; ep->xferred = 0; ep->state = D_EP_IDLE; ep->xferCompleteCb = NULL; ep->fifoSize = USB_EP0_SIZE / 4; totalTxFifoSize = ep->fifoSize * p->bufferingMultiplier[ 0 ]; totalRxFifoSize = (ep->fifoSize + 1) * p->bufferingMultiplier[ 0 ]; #if defined( DEBUG_USB_API ) /* Do a sanity check on the configuration descriptor */ { int i; /* Check if bLength's adds up exactly to wTotalLength */ i = 0; conf = (uint8_t*)dev->configDescriptor; confEnd = conf + dev->configDescriptor->wTotalLength; while ( conf < confEnd ) { if ( *conf == 0 ) break; i += *conf; conf += *conf; } if ( ( conf != confEnd ) || ( i != dev->configDescriptor->wTotalLength ) ) { DEBUG_USB_API_PUTS( "\nUSBD_Init(), Illegal configuration descriptor" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } } #endif /* defined( DEBUG_USB_API ) */ /* Parse configuration decriptor */ numEps = 0; numInEps = 0; numOutEps = 0; conf = (uint8_t*)dev->configDescriptor; confEnd = conf + dev->configDescriptor->wTotalLength; txFifoNum = 1; while ( conf < confEnd ) { if ( *conf == 0 ) { DEBUG_USB_API_PUTS( "\nUSBD_Init(), Illegal configuration descriptor" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } if ( *(conf + 1) == USB_ENDPOINT_DESCRIPTOR ) { numEps++; epd = (USB_EndpointDescriptor_TypeDef*)conf; ep = &dev->ep[ numEps ]; ep->in = ( epd->bEndpointAddress & USB_SETUP_DIR_MASK ) != 0; ep->buf = NULL; ep->addr = epd->bEndpointAddress; ep->num = ep->addr & USB_EPNUM_MASK; ep->mask = 1 << ep->num; ep->type = epd->bmAttributes & CONFIG_DESC_BM_TRANSFERTYPE; ep->packetSize = epd->wMaxPacketSize; ep->remaining = 0; ep->xferred = 0; ep->state = D_EP_IDLE; ep->xferCompleteCb = NULL; if ( ep->in ) { numInEps++; ep->txFifoNum = txFifoNum++; ep->fifoSize = (ep->packetSize/4) * p->bufferingMultiplier[ numEps ]; dev->inEpAddr2EpIndex[ ep->num ] = numEps; totalTxFifoSize += ep->fifoSize; if ( ep->num > MAX_NUM_IN_EPS ) { DEBUG_USB_API_PUTS( "\nUSBD_Init(), Illegal IN EP address" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } } else { numOutEps++; ep->fifoSize = (ep->packetSize/4 + 1) * p->bufferingMultiplier[ numEps ]; dev->outEpAddr2EpIndex[ ep->num ] = numEps; totalRxFifoSize += ep->fifoSize; if ( ep->num > MAX_NUM_OUT_EPS ) { DEBUG_USB_API_PUTS( "\nUSBD_Init(), Illegal OUT EP address" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } } } conf += *conf; } /* Rx-FIFO size: SETUP packets : 4*n + 6 n=#CTRL EP's * GOTNAK : 1 * Status info : 2*n n=#OUT EP's (EP0 included) in HW */ totalRxFifoSize += 10 + 1 + ( 2 * (MAX_NUM_OUT_EPS + 1) ); if ( numEps != NUM_EP_USED ) { DEBUG_USB_API_PUTS( "\nUSBD_Init(), Illegal EP count" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } if ( numInEps > MAX_NUM_IN_EPS ) { DEBUG_USB_API_PUTS( "\nUSBD_Init(), Illegal IN EP count" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } if ( numOutEps > MAX_NUM_OUT_EPS ) { DEBUG_USB_API_PUTS( "\nUSBD_Init(), Illegal OUT EP count" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } INT_Disable(); /* Enable USB clock */ CMU->HFCORECLKEN0 |= CMU_HFCORECLKEN0_USB | CMU_HFCORECLKEN0_USBC; CMU_ClockSelectSet( cmuClock_USBC, cmuSelect_HFCLK ); USBHAL_DisableGlobalInt(); if ( USBDHAL_CoreInit( totalRxFifoSize, totalTxFifoSize ) == USB_STATUS_OK ) { USBDHAL_EnableUsbResetInt(); USBHAL_EnableGlobalInt(); NVIC_ClearPendingIRQ( USB_IRQn ); NVIC_EnableIRQ( USB_IRQn ); } else { INT_Enable(); DEBUG_USB_API_PUTS( "\nUSBD_Init(), FIFO setup error" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } #if ( USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_ONVBUSOFF ) if ( USBHAL_VbusIsOn() ) { USBD_SetUsbState( USBD_STATE_POWERED ); } else #endif { USBD_SetUsbState( USBD_STATE_NONE ); } INT_Enable(); return USB_STATUS_OK; }
/**************************************************************************//** * The main entry point. *****************************************************************************/ int main(void) { int msElapsed, i; /* Set new vector table pointer */ SCB->VTOR = 0x20000000; /* Enable peripheral clocks. */ CMU->HFPERCLKDIV = CMU_HFPERCLKDIV_HFPERCLKEN; CMU->HFPERCLKEN0 = CMU_HFPERCLKEN0_GPIO | BOOTLOADER_USART_CLOCK | AUTOBAUD_TIMER_CLOCK ; /* Enable DMA interface */ CMU->HFCORECLKEN0 = CMU_HFCORECLKEN0_DMA; #if defined( BL_DEBUG ) RETARGET_SerialInit(); /* Setup debug serialport etc. */ USB_PUTS( "EFM32 USB/USART0 bootloader\r\n" ); #endif /* Calculate CRC16 for the bootloader itself and the Device Information page. */ /* This is used for production testing and can safely be omitted in */ /* your own code. */ bootloaderCRC = CRC_calc((void *) 0x0, (void *) BOOTLOADER_SIZE); bootloaderCRC |= CRC_calc((void *) 0x0FE081B2, (void *) 0x0FE08200) << 16; StartRTC(); #if !defined( SIMULATE_SWDCLK_PIN_HI ) while ( SWDCLK_PIN_IS_LO() ) { USB_PUTS( "SWDCLK is low\r\n" ); if ( BOOT_checkFirmwareIsValid() ) { USB_PUTS( "Booting application\r\n " ); BOOT_boot(); } else { USB_PUTS( "No valid application, resetting EFM32... \r\n" ); /* Go to EM2 and wait for RTC wakeup. */ EMU_EnterEM2( false ); } } #endif NVIC_DisableIRQ( RTC_IRQn ); /* Try to start HFXO. */ CMU_OscillatorEnable( cmuOsc_HFXO, true, false ); /* Wait approx. 1 second to see if HFXO starts. */ i = 1500000; while ( i && !( CMU->STATUS & CMU_STATUS_HFXORDY ) ) { i--; } USBTIMER_Init(); if ( i == 0 ) { CMU_HFRCOBandSet( cmuHFRCOBand_28MHz ); } else { CMU_ClockSelectSet( cmuClock_HF, cmuSelect_HFXO ); USBD_Init( &initstruct ); /* Start USB CDC functionality */ } AUTOBAUD_start(); /* Start autobaud */ /* Wait 30 seconds for USART or USB connection */ msElapsed = 0; while ( msElapsed < 30000 ) { if ( AUTOBAUD_completed() ) break; if ( CDC_Configured ) { BOOTLDIO_setMode( CDC_Configured ); break; } USBTIMER_DelayMs( 100 ); msElapsed += 100; } AUTOBAUD_stop(); if ( msElapsed >= 30000 ) { USB_PUTS( "USART0/USB timeout, resetting EFM32...\r\n " ); Disconnect( 0, 2000 ); SCB->AIRCR = 0x05FA0004; /* Reset EFM32. */ } /* Print a message to show that we are in bootloader mode */ BOOTLDIO_printString("\r\n\r\n" BOOTLOADER_VERSION_STRING ); /* Print the chip ID. This is useful for production tracking */ BOOTLDIO_printHex(DEVINFO->UNIQUEH); BOOTLDIO_printHex(DEVINFO->UNIQUEL); BOOTLDIO_printString("\r\n"); /* Figure out correct flash geometry. */ FLASH_CalcPageSize(); /* Initialize flash for writing */ FLASH_init(); /* Start executing command line */ commandlineLoop(); }
/***************************************************************************//** * @brief * Initializes USB device hardware and internal protocol stack data structures, * then connects the data-line (D+ or D-) pullup resistor to signal host that * enumeration can begin. * * @note * You may later use @ref USBD_Disconnect() and @ref USBD_Connect() to force * reenumeration. * * @param[in] p * Pointer to device initialization struct. See @ref USBD_Init_TypeDef. * * @return * @ref USB_STATUS_OK on success, else an appropriate error code. ******************************************************************************/ int USBD_Init( const USBD_Init_TypeDef *p ) { int numEps; USBD_Ep_TypeDef *ep; uint8_t txFifoNum; uint8_t *conf, *confEnd; USB_EndpointDescriptor_TypeDef *epd; USB_InterfaceDescriptor_TypeDef *id; uint32_t totalRxFifoSize, totalTxFifoSize, numInEps, numOutEps; #if !defined( USB_CORECLK_HFRCO ) || !defined( CMU_OSCENCMD_USHFRCOEN ) /* Devices supporting crystal-less USB can use HFRCO or HFXO as core clock. */ /* All other devices must use HFXO as core clock. */ if ( CMU_ClockSelectGet( cmuClock_HF ) != cmuSelect_HFXO ) { CMU_ClockSelectSet( cmuClock_HF, cmuSelect_HFXO ); } #endif #if !defined( CMU_OSCENCMD_USHFRCOEN ) #if ( USB_USBC_32kHz_CLK == USB_USBC_32kHz_CLK_LFXO ) CMU_OscillatorEnable(cmuOsc_LFXO, true, false); #else CMU_OscillatorEnable(cmuOsc_LFRCO, true, false); #endif #else CMU_ClockEnable(cmuClock_CORELE, true); /* LFC clock is needed to detect USB suspend when LEMIDLE is activated. */ #if ( USB_USBC_32kHz_CLK == USB_USBC_32kHz_CLK_LFXO ) CMU_ClockSelectSet(cmuClock_LFC, cmuSelect_LFXO); #else CMU_ClockSelectSet(cmuClock_LFC, cmuSelect_LFRCO); #endif CMU_ClockEnable(cmuClock_USBLE, true); #endif USBTIMER_Init(); memset( dev, 0, sizeof( USBD_Device_TypeDef ) ); dev->setup = dev->setupPkt; dev->deviceDescriptor = p->deviceDescriptor; dev->configDescriptor = (USB_ConfigurationDescriptor_TypeDef*) p->configDescriptor; dev->stringDescriptors = p->stringDescriptors; dev->numberOfStrings = p->numberOfStrings; dev->state = USBD_STATE_LASTMARKER; dev->savedState = USBD_STATE_NONE; dev->lastState = USBD_STATE_NONE; dev->callbacks = p->callbacks; dev->remoteWakeupEnabled = false; /* Initialize EP0 */ ep = &dev->ep[ 0 ]; ep->in = false; ep->buf = NULL; ep->num = 0; ep->mask = 1; ep->addr = 0; ep->type = USB_EPTYPE_CTRL; ep->txFifoNum = 0; ep->packetSize = p->deviceDescriptor->bMaxPacketSize0; if ( ep->packetSize == 32 ) { dev->ep0MpsCode = _USB_DOEP0CTL_MPS_32B; } else if ( ep->packetSize == 64 ) { dev->ep0MpsCode = _USB_DOEP0CTL_MPS_64B; } else { return USB_STATUS_ILLEGAL; } ep->remaining = 0; ep->xferred = 0; ep->state = D_EP_IDLE; ep->xferCompleteCb = NULL; ep->fifoSize = ep->packetSize / 4; totalTxFifoSize = ep->fifoSize * p->bufferingMultiplier[ 0 ]; totalRxFifoSize = (ep->fifoSize + 1) * p->bufferingMultiplier[ 0 ]; #if defined( DEBUG_USB_API ) /* Do a sanity check on the configuration descriptor */ { int i; /* Check if bLength's adds up exactly to wTotalLength */ i = 0; conf = (uint8_t*)dev->configDescriptor; confEnd = conf + dev->configDescriptor->wTotalLength; while ( conf < confEnd ) { if ( *conf == 0 ) break; i += *conf; conf += *conf; } if ( ( conf != confEnd ) || ( i != dev->configDescriptor->wTotalLength ) ) { DEBUG_USB_API_PUTS( "\nUSBD_Init(), Illegal configuration descriptor" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } } #endif /* defined( DEBUG_USB_API ) */ /* Parse configuration decriptor */ numEps = 0; numInEps = 0; numOutEps = 0; conf = (uint8_t*)dev->configDescriptor; confEnd = conf + dev->configDescriptor->wTotalLength; txFifoNum = 1; while ( conf < confEnd ) { if ( *conf == 0 ) { DEBUG_USB_API_PUTS( "\nUSBD_Init(), Illegal configuration descriptor" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } if ( *(conf + 1) == USB_ENDPOINT_DESCRIPTOR ) { numEps++; epd = (USB_EndpointDescriptor_TypeDef*)conf; #if defined( __GNUC__ ) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Warray-bounds" #endif ep = &dev->ep[ numEps ]; #if defined( __GNUC__ ) #pragma GCC diagnostic pop #endif ep->in = ( epd->bEndpointAddress & USB_SETUP_DIR_MASK ) != 0; ep->buf = NULL; ep->addr = epd->bEndpointAddress; ep->num = ep->addr & USB_EPNUM_MASK; ep->mask = 1 << ep->num; ep->type = epd->bmAttributes & CONFIG_DESC_BM_TRANSFERTYPE; ep->packetSize = epd->wMaxPacketSize; ep->remaining = 0; ep->xferred = 0; ep->state = D_EP_IDLE; ep->xferCompleteCb = NULL; if ( p->bufferingMultiplier[ numEps ] == 0 ) { DEBUG_USB_API_PUTS( "\nUSBD_Init(), Illegal EP fifo buffer configuration" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } if ( ep->in ) { numInEps++; ep->txFifoNum = txFifoNum++; ep->fifoSize = ( ( ep->packetSize + 3 ) / 4 ) * p->bufferingMultiplier[ numEps ]; dev->inEpAddr2EpIndex[ ep->num ] = numEps; totalTxFifoSize += ep->fifoSize; if ( ep->num > MAX_NUM_IN_EPS ) { DEBUG_USB_API_PUTS( "\nUSBD_Init(), Illegal IN EP address" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } } else { numOutEps++; ep->fifoSize = ( ( ( ep->packetSize + 3 ) / 4 ) + 1 ) * p->bufferingMultiplier[ numEps ]; dev->outEpAddr2EpIndex[ ep->num ] = numEps; totalRxFifoSize += ep->fifoSize; if ( ep->num > MAX_NUM_OUT_EPS ) { DEBUG_USB_API_PUTS( "\nUSBD_Init(), Illegal OUT EP address" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } } } else if ( *(conf + 1) == USB_INTERFACE_DESCRIPTOR ) { id = (USB_InterfaceDescriptor_TypeDef*)conf; if ( id->bAlternateSetting == 0 ) // Only check default interfaces { if ( dev->numberOfInterfaces != id->bInterfaceNumber ) { DEBUG_USB_API_PUTS( "\nUSBD_Init(), Illegal interface number" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } dev->numberOfInterfaces++; } } conf += *conf; } /* Rx-FIFO size: SETUP packets : 4*n + 6 n=#CTRL EP's * GOTNAK : 1 * Status info : 2*n n=#OUT EP's (EP0 included) in HW */ totalRxFifoSize += 10 + 1 + ( 2 * (MAX_NUM_OUT_EPS + 1) ); if ( dev->configDescriptor->bNumInterfaces != dev->numberOfInterfaces ) { DEBUG_USB_API_PUTS( "\nUSBD_Init(), Illegal interface count" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } if ( numEps != NUM_EP_USED ) { DEBUG_USB_API_PUTS( "\nUSBD_Init(), Illegal EP count" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } if ( numInEps > MAX_NUM_IN_EPS ) { DEBUG_USB_API_PUTS( "\nUSBD_Init(), Illegal IN EP count" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } if ( numOutEps > MAX_NUM_OUT_EPS ) { DEBUG_USB_API_PUTS( "\nUSBD_Init(), Illegal OUT EP count" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } INT_Disable(); /* Enable USB clock */ CMU->HFCORECLKEN0 |= CMU_HFCORECLKEN0_USB | CMU_HFCORECLKEN0_USBC; #if defined( CMU_OSCENCMD_USHFRCOEN ) CMU->USHFRCOCONF = CMU_USHFRCOCONF_BAND_48MHZ; CMU_ClockSelectSet( cmuClock_USBC, cmuSelect_USHFRCO ); /* Enable USHFRCO Clock Recovery mode. */ CMU->USBCRCTRL |= CMU_USBCRCTRL_EN; /* Turn on Low Energy Mode (LEM) features. */ USB->CTRL = USB_CTRL_LEMOSCCTRL_GATE | USB_CTRL_LEMIDLEEN | USB_CTRL_LEMPHYCTRL; #else CMU_ClockSelectSet( cmuClock_USBC, cmuSelect_HFCLK ); #endif USBHAL_DisableGlobalInt(); if ( USBDHAL_CoreInit( totalRxFifoSize, totalTxFifoSize ) == USB_STATUS_OK ) { USBDHAL_EnableUsbResetAndSuspendInt(); USBHAL_EnableGlobalInt(); NVIC_ClearPendingIRQ( USB_IRQn ); NVIC_EnableIRQ( USB_IRQn ); } else { INT_Enable(); DEBUG_USB_API_PUTS( "\nUSBD_Init(), FIFO setup error" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } #if ( USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_ONVBUSOFF ) if ( USBHAL_VbusIsOn() ) { USBD_SetUsbState( USBD_STATE_POWERED ); } else #endif { USBD_SetUsbState( USBD_STATE_NONE ); } INT_Enable(); return USB_STATUS_OK; }