/***************************************************************************//** * @brief * Abort a pending transfer on a specific endpoint. * * @param[in] epAddr * The address of the endpoint to abort. ******************************************************************************/ int USBD_AbortTransfer( int epAddr ) { USB_XferCompleteCb_TypeDef callback; USBD_Ep_TypeDef *ep = USBD_GetEpFromAddr( epAddr ); CORE_DECLARE_IRQ_STATE; if ( ep == NULL ) { DEBUG_USB_API_PUTS( "\nUSBD_AbortTransfer(), Illegal endpoint" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } if ( ep->num == 0 ) { DEBUG_USB_API_PUTS( "\nUSBD_AbortTransfer(), Illegal endpoint" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } CORE_ENTER_CRITICAL(); if ( ep->state == D_EP_IDLE ) { CORE_EXIT_CRITICAL(); return USB_STATUS_OK; } USBD_AbortEp( ep ); ep->state = D_EP_IDLE; if ( ep->xferCompleteCb ) { callback = ep->xferCompleteCb; ep->xferCompleteCb = NULL; if ( ( dev->lastState == USBD_STATE_CONFIGURED ) && ( dev->state == USBD_STATE_ADDRESSED ) ) { USBDHAL_DeactivateEp( ep ); } DEBUG_TRACE_ABORT( USB_STATUS_EP_ABORTED ); callback( USB_STATUS_EP_ABORTED, ep->xferred, ep->remaining ); } CORE_EXIT_CRITICAL(); return USB_STATUS_OK; }
/***************************************************************************//** * @brief * Reset stall state on a stalled (halted) endpoint. * * @param[in] epAddr * The address of the endpoint to un-stall. * * @return * @ref USB_STATUS_OK on success, else an appropriate error code. ******************************************************************************/ int USBD_UnStallEp( int epAddr ) { USB_Status_TypeDef retVal; CORE_DECLARE_IRQ_STATE; USBD_Ep_TypeDef *ep = USBD_GetEpFromAddr( epAddr ); if ( ep == NULL ) { DEBUG_USB_API_PUTS( "\nUSBD_UnStallEp(), Illegal request" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } if ( ep->num == 0 ) { DEBUG_USB_API_PUTS( "\nUSBD_UnStallEp(), Illegal endpoint" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } CORE_ENTER_CRITICAL(); retVal = USBDHAL_UnStallEp( ep ); CORE_EXIT_CRITICAL(); if ( retVal != USB_STATUS_OK ) { retVal = USB_STATUS_ILLEGAL; } return retVal; }
/***************************************************************************//** * @brief * Abort all pending transfers. * * @details * Aborts transfers for all endpoints currently in use. Pending * transfers on the default endpoint (EP0) are not aborted. ******************************************************************************/ void USBD_AbortAllTransfers( void ) { CORE_DECLARE_IRQ_STATE; CORE_ENTER_CRITICAL(); USBDHAL_AbortAllTransfers( USB_STATUS_EP_ABORTED ); CORE_EXIT_CRITICAL(); }
/***************************************************************************//** * @brief * Stop USB device operation. * * @details * Device operation is stopped by disconnecting the pullup resistor from the * appropriate USB data line. Often referred to as a "soft" disconnect. ******************************************************************************/ void USBD_Disconnect( void ) { CORE_DECLARE_IRQ_STATE; CORE_ENTER_CRITICAL(); USBDHAL_Disconnect(); CORE_EXIT_CRITICAL(); }
/***************************************************************************//** * @brief * Perform a remote wakeup signalling sequence. * * @note * It is the responsibility of the application to ensure that remote wakeup * is not attempted before the device has been suspended for at least 5 * miliseconds. This function should not be called from within an interrupt * handler. * * @return * @ref USB_STATUS_OK on success, else an appropriate error code. ******************************************************************************/ int USBD_RemoteWakeup( void ) { CORE_DECLARE_IRQ_STATE; CORE_ENTER_CRITICAL(); if ( ( dev->state != USBD_STATE_SUSPENDED ) || ( dev->remoteWakeupEnabled == false ) ) { CORE_EXIT_CRITICAL(); DEBUG_USB_API_PUTS( "\nUSBD_RemoteWakeup(), Illegal remote wakeup" ); return USB_STATUS_ILLEGAL; } USBDHAL_SetRemoteWakeup(); CORE_EXIT_CRITICAL(); USBTIMER_DelayMs( 10 ); CORE_ENTER_CRITICAL(); USBDHAL_ClearRemoteWakeup(); CORE_EXIT_CRITICAL(); return USB_STATUS_OK; }
uint32_t otPlatAlarmMilliGetNow(void) { uint32_t timer_lo; uint32_t timer_ms; CORE_DECLARE_IRQ_STATE; CORE_ENTER_CRITICAL(); timer_lo = RAIL_GetTime(); if (timer_lo < sTimerLo) { sTimerHi++; } sTimerLo = timer_lo; timer_ms = (((uint64_t)sTimerHi << 32) | sTimerLo) / 1000; CORE_EXIT_CRITICAL(); return timer_ms; }
/***************************************************************************//** * @brief * Add a new endpoint * * @param[in] epAddr * Endpoint address * * @param[in] transferType * Endpoint type, one of @ref USB_EPTYPE_BULK, @ref USB_EPTYPE_INTR or * @ref USB_EPTYPE_ISOC. * * @param[in] maxPacketSize * Maximum packet size of the new endpoint, in bytes * * @param[in] bufferMult * FIFO buffer size multiplier * * @return * @ref USB_STATUS_OK on success, else an appropriate error code. ******************************************************************************/ int USBD_AddEndpoint(int epAddr, int transferType, int maxPacketSize, int bufferMult) { CORE_DECLARE_IRQ_STATE; USBD_Ep_TypeDef *ep; numEps++; ep = &dev->ep[ numEps ]; ep->in = ( epAddr & USB_SETUP_DIR_MASK ) != 0; ep->buf = NULL; ep->addr = epAddr; ep->num = ep->addr & USB_EPNUM_MASK; ep->mask = 1 << ep->num; ep->type = transferType; ep->packetSize = maxPacketSize; ep->remaining = 0; ep->xferred = 0; ep->state = D_EP_IDLE; ep->xferCompleteCb = NULL; if ( ep->in ) { ep->txFifoNum = txFifoNum++; ep->fifoSize = ( ( ep->packetSize + 3 ) / 4 ) * bufferMult; dev->inEpAddr2EpIndex[ ep->num ] = numEps; totalTxFifoSize += ep->fifoSize; if ( ep->num > MAX_NUM_IN_EPS ) { DEBUG_USB_API_PUTS( "\nUSBD_AddEndpoint(), Illegal IN EP address" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } } else { ep->fifoSize = ( ( ( ep->packetSize + 3 ) / 4 ) + 1 ) * bufferMult; dev->outEpAddr2EpIndex[ ep->num ] = numEps; totalRxFifoSize += ep->fifoSize; if ( ep->num > MAX_NUM_OUT_EPS ) { DEBUG_USB_API_PUTS( "\nUSBD_AddEndpoint(), Illegal OUT EP address" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } } USB_PRINTF("USBD: Added endpoint %d to slot %d, in %d, addr 0x%x, type %d, ps %d, fifo %ld (total tx %ld, rx %ld)\n", ep->num, numEps, ep->in, ep->addr, ep->type, ep->packetSize, ep->fifoSize, totalTxFifoSize, totalRxFifoSize); CORE_ENTER_CRITICAL(); #if defined( CMU_OSCENCMD_USHFRCOEN ) /* Happy Gecko workaround: disable LEM GATE mode if using ISOC endpoints. */ if ( transferType == USB_EPTYPE_ISOC ) { USB->CTRL = (USB->CTRL & ~_USB_CTRL_LEMOSCCTRL_MASK) | USB_CTRL_LEMOSCCTRL_NONE; } #endif int ret = USBDHAL_ReconfigureFifos(totalRxFifoSize, totalTxFifoSize); CORE_EXIT_CRITICAL(); if( ret != USB_STATUS_OK ) { return ret; } USBDHAL_ActivateEp(ep, false); return USB_STATUS_OK; }
/***************************************************************************//** * @brief * Start a write (IN) transfer on an endpoint. * * @param[in] epAddr * Endpoint address. * * @param[in] data * Pointer to transfer data buffer. This buffer must be WORD (4 byte) aligned. * * @param[in] byteCount * Transfer length. * * @param[in] callback * Function to be called on transfer completion. Supply NULL if no callback * is needed. See @ref USB_XferCompleteCb_TypeDef. * * @return * @ref USB_STATUS_OK on success, else an appropriate error code. ******************************************************************************/ int USBD_Write( int epAddr, void *data, int byteCount, USB_XferCompleteCb_TypeDef callback ) { USBD_Ep_TypeDef *ep = USBD_GetEpFromAddr( epAddr ); CORE_DECLARE_IRQ_STATE; USB_PRINTF("USBD: Write addr %x, data %p, size %d, cb 0x%lx\n", epAddr, data, byteCount, (uint32_t)callback); if ( ep == NULL ) { DEBUG_USB_API_PUTS( "\nUSBD_Write(), Illegal endpoint" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } if ( ( byteCount > MAX_XFER_LEN ) || ( ( byteCount / ep->packetSize ) > MAX_PACKETS_PR_XFER ) ) { DEBUG_USB_API_PUTS( "\nUSBD_Write(), Illegal transfer size" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } if ( (uint32_t)data & 3 ) { DEBUG_USB_API_PUTS( "\nUSBD_Write(), Misaligned data buffer" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } CORE_ENTER_CRITICAL(); if ( USBDHAL_EpIsStalled( ep ) ) { CORE_EXIT_CRITICAL(); DEBUG_USB_API_PUTS( "\nUSBD_Write(), Endpoint is halted" ); return USB_STATUS_EP_STALLED; } if ( ep->state != D_EP_IDLE ) { CORE_EXIT_CRITICAL(); DEBUG_USB_API_PUTS( "\nUSBD_Write(), Endpoint is busy" ); return USB_STATUS_EP_BUSY; } if ( ( ep->num > 0 ) && ( USBD_GetUsbState() != USBD_STATE_CONFIGURED ) ) { CORE_EXIT_CRITICAL(); DEBUG_USB_API_PUTS( "\nUSBD_Write(), Device not configured" ); return USB_STATUS_DEVICE_UNCONFIGURED; } ep->buf = (uint8_t*)data; ep->remaining = byteCount; ep->xferred = 0; if ( ep->num == 0 ) { ep->in = true; } else if ( ep->in != true ) { CORE_EXIT_CRITICAL(); DEBUG_USB_API_PUTS( "\nUSBD_Write(), Illegal EP direction" ); EFM_ASSERT( false ); return USB_STATUS_ILLEGAL; } ep->state = D_EP_TRANSMITTING; ep->xferCompleteCb = callback; USBD_ArmEp( ep ); 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 ) { 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; }