/* * USB_IRQHandler() is the first level handler for the USB peripheral interrupt. */ void USB_IRQHandler( void ) { uint32_t status; bool servedVbusInterrupt = false; INT_Disable(); #if ( USB_PWRSAVE_MODE ) if ( USBD_poweredDown ) { /* Switch USBC clock from 32kHz to HFCLK to be able to read USB */ /* peripheral registers. */ /* If we woke up from EM2, HFCLK is now HFRCO. */ CMU_OscillatorEnable( cmuOsc_HFXO, true, false); /* Prepare HFXO. */ CMU->CMD = CMU_CMD_USBCCLKSEL_HFCLKNODIV; while ( !( CMU->STATUS & CMU_STATUS_USBCHFCLKSEL ) ) { } } #endif /* if ( USB_PWRSAVE_MODE ) */ if ( USB->IF && ( USB->CTRL & USB_CTRL_VREGOSEN ) ) { if ( USB->IF & USB_IF_VREGOSH ) { USB->IFC = USB_IFC_VREGOSH; if ( USB->STATUS & USB_STATUS_VREGOS ) { servedVbusInterrupt = true; DEBUG_USB_INT_LO_PUTS( "\nVboN" ); #if ( USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_ONVBUSOFF ) if ( UsbPowerUp() ) #endif { USBDHAL_EnableUsbResetInt(); } USBD_SetUsbState( USBD_STATE_POWERED ); } } if ( USB->IF & USB_IF_VREGOSL ) { USB->IFC = USB_IFC_VREGOSL; if ( !( USB->STATUS & USB_STATUS_VREGOS ) ) { servedVbusInterrupt = true; DEBUG_USB_INT_LO_PUTS( "\nVboF" ); USB->GINTMSK = 0; USB->GINTSTS = 0xFFFFFFFF; #if ( USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_ONVBUSOFF ) UsbPowerDown(); #endif USBD_SetUsbState( USBD_STATE_NONE ); } } } status = USBHAL_GetCoreInts(); if ( status == 0 ) { INT_Enable(); if ( !servedVbusInterrupt ) { DEBUG_USB_INT_LO_PUTS( "\nSinT" ); } return; } HANDLE_INT( USB_GINTSTS_RESETDET ) HANDLE_INT( USB_GINTSTS_WKUPINT ) HANDLE_INT( USB_GINTSTS_USBSUSP ) HANDLE_INT( USB_GINTSTS_SOF ) HANDLE_INT( USB_GINTSTS_ENUMDONE ) HANDLE_INT( USB_GINTSTS_USBRST ) HANDLE_INT( USB_GINTSTS_IEPINT ) HANDLE_INT( USB_GINTSTS_OEPINT ) INT_Enable(); if ( status != 0 ) { DEBUG_USB_INT_LO_PUTS( "\nUinT" ); } }
/***************************************************************************//** * @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; }