17 #if defined( USB_PRESENT ) && ( USB_COUNT == 1 )
19 #if defined( USB_DEVICE )
28 static USBD_Device_TypeDef device;
29 USBD_Device_TypeDef *dev = &device;
31 static const char *stateNames[] =
70 USBD_Ep_TypeDef *ep = USBD_GetEpFromAddr( epAddr );
74 DEBUG_USB_API_PUTS(
"\nUSBD_AbortTransfer(), Illegal endpoint" );
81 DEBUG_USB_API_PUTS(
"\nUSBD_AbortTransfer(), Illegal endpoint" );
87 if ( ep->state == D_EP_IDLE )
95 ep->state = D_EP_IDLE;
96 if ( ep->xferCompleteCb )
98 callback = ep->xferCompleteCb;
99 ep->xferCompleteCb = NULL;
104 USBDHAL_DeactivateEp( ep );
141 USBDHAL_Disconnect();
157 USBD_Ep_TypeDef *ep = USBD_GetEpFromAddr( epAddr );
161 DEBUG_USB_API_PUTS(
"\nUSBD_EpIsBusy(), Illegal endpoint" );
166 if ( ep->state == D_EP_IDLE )
199 return stateNames[ state ];
223 uint8_t *conf, *confEnd;
226 uint32_t totalRxFifoSize, totalTxFifoSize, numInEps, numOutEps;
228 #if !defined( USB_CORECLK_HFRCO ) || !defined( CMU_OSCENCMD_USHFRCOEN )
237 #if !defined( CMU_OSCENCMD_USHFRCOEN )
238 #if ( USB_USBC_32kHz_CLK == USB_USBC_32kHz_CLK_LFXO )
247 #if ( USB_USBC_32kHz_CLK == USB_USBC_32kHz_CLK_LFXO )
257 memset( dev, 0,
sizeof( USBD_Device_TypeDef ) );
259 dev->setup = dev->setupPkt;
269 dev->remoteWakeupEnabled =
false;
283 if ( ep->packetSize == 32 )
287 else if ( ep->packetSize == 64 )
298 ep->state = D_EP_IDLE;
299 ep->xferCompleteCb = NULL;
300 ep->fifoSize = ep->packetSize / 4;
305 #
if defined( DEBUG_USB_API )
312 conf = (uint8_t*)dev->configDescriptor;
313 confEnd = conf + dev->configDescriptor->wTotalLength;
315 while ( conf < confEnd )
324 if ( ( conf != confEnd ) ||
325 ( i != dev->configDescriptor->wTotalLength ) )
327 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal configuration descriptor" );
338 conf = (uint8_t*)dev->configDescriptor;
339 confEnd = conf + dev->configDescriptor->wTotalLength;
342 while ( conf < confEnd )
346 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal configuration descriptor" );
356 #if defined( __GNUC__ )
357 #pragma GCC diagnostic push
358 #pragma GCC diagnostic ignored "-Warray-bounds"
360 ep = &dev->ep[ numEps ];
361 #if defined( __GNUC__ )
362 #pragma GCC diagnostic pop
369 ep->mask = 1 << ep->num;
374 ep->state = D_EP_IDLE;
375 ep->xferCompleteCb = NULL;
379 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal EP fifo buffer configuration" );
387 ep->txFifoNum = txFifoNum++;
388 ep->fifoSize = ( ( ep->packetSize + 3 ) / 4 )
390 dev->inEpAddr2EpIndex[ ep->num ] = numEps;
391 totalTxFifoSize += ep->fifoSize;
392 if ( ep->num > MAX_NUM_IN_EPS )
394 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal IN EP address" );
402 ep->fifoSize = ( ( ( ep->packetSize + 3 ) / 4 ) + 1 )
404 dev->outEpAddr2EpIndex[ ep->num ] = numEps;
405 totalRxFifoSize += ep->fifoSize;
406 if ( ep->num > MAX_NUM_OUT_EPS )
408 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal OUT EP address" );
422 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal interface number" );
426 dev->numberOfInterfaces++;
437 totalRxFifoSize += 10 + 1 + ( 2 * (MAX_NUM_OUT_EPS + 1) );
439 if ( dev->configDescriptor->bNumInterfaces != dev->numberOfInterfaces )
441 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal interface count" );
446 if ( numEps != NUM_EP_USED )
448 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal EP count" );
453 if ( numInEps > MAX_NUM_IN_EPS )
455 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal IN EP count" );
460 if ( numOutEps > MAX_NUM_OUT_EPS )
462 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal OUT EP count" );
472 #if defined( CMU_OSCENCMD_USHFRCOEN )
473 CMU->USHFRCOCONF = CMU_USHFRCOCONF_BAND_48MHZ;
477 CMU->USBCRCTRL |= CMU_USBCRCTRL_EN;
480 USB->CTRL = USB_CTRL_LEMOSCCTRL_GATE
482 | USB_CTRL_LEMPHYCTRL;
487 USBHAL_DisableGlobalInt();
489 if ( USBDHAL_CoreInit( totalRxFifoSize, totalTxFifoSize ) ==
USB_STATUS_OK )
491 USBDHAL_EnableUsbResetAndSuspendInt();
492 USBHAL_EnableGlobalInt();
499 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), FIFO setup error" );
504 #if ( USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_ONVBUSOFF )
505 if ( USBHAL_VbusIsOn() )
548 USBD_Ep_TypeDef *ep = USBD_GetEpFromAddr( epAddr );
552 DEBUG_USB_API_PUTS(
"\nUSBD_Read(), Illegal endpoint" );
557 if ( ( byteCount > MAX_XFER_LEN ) ||
558 ( ( byteCount / ep->packetSize ) > MAX_PACKETS_PR_XFER ) )
560 DEBUG_USB_API_PUTS(
"\nUSBD_Read(), Illegal transfer size" );
565 if ( (uint32_t)data & 3 )
567 DEBUG_USB_API_PUTS(
"\nUSBD_Read(), Misaligned data buffer" );
573 if ( USBDHAL_EpIsStalled( ep ) )
576 DEBUG_USB_API_PUTS(
"\nUSBD_Read(), Endpoint is halted" );
580 if ( ep->state != D_EP_IDLE )
583 DEBUG_USB_API_PUTS(
"\nUSBD_Read(), Endpoint is busy" );
590 DEBUG_USB_API_PUTS(
"\nUSBD_Read(), Device not configured" );
594 ep->buf = (uint8_t*)data;
595 ep->remaining = byteCount;
602 else if ( ep->in !=
false )
605 DEBUG_USB_API_PUTS(
"\nUSBD_Read(), Illegal EP direction" );
610 ep->state = D_EP_RECEIVING;
611 ep->xferCompleteCb = callback;
636 ( dev->remoteWakeupEnabled ==
false ) )
639 DEBUG_USB_API_PUTS(
"\nUSBD_RemoteWakeup(), Illegal remote wakeup" );
643 USBDHAL_SetRemoteWakeup();
647 USBDHAL_ClearRemoteWakeup();
667 #if ( USB_PWRSAVE_MODE )
668 return USBD_poweredDown ?
true :
false;
680 currentState = dev->state;
683 dev->savedState = currentState;
686 dev->lastState = dev->state;
687 dev->state = newState;
689 if ( ( dev->callbacks->usbStateChange ) &&
690 ( currentState != newState ) )
692 dev->callbacks->usbStateChange( currentState, newState );
711 USBD_Ep_TypeDef *ep = USBD_GetEpFromAddr( epAddr );
715 DEBUG_USB_API_PUTS(
"\nUSBD_StallEp(), Illegal request" );
722 DEBUG_USB_API_PUTS(
"\nUSBD_StallEp(), Illegal endpoint" );
728 retVal = USBDHAL_StallEp( ep );
751 USBHAL_DisableGlobalInt();
752 USBHAL_DisableUsbInt();
753 USBHAL_DisablePhyPins();
772 USBD_Ep_TypeDef *ep = USBD_GetEpFromAddr( epAddr );
776 DEBUG_USB_API_PUTS(
"\nUSBD_UnStallEp(), Illegal request" );
783 DEBUG_USB_API_PUTS(
"\nUSBD_UnStallEp(), Illegal endpoint" );
789 retVal = USBDHAL_UnStallEp( ep );
823 USBD_Ep_TypeDef *ep = USBD_GetEpFromAddr( epAddr );
827 DEBUG_USB_API_PUTS(
"\nUSBD_Write(), Illegal endpoint" );
832 if ( ( byteCount > MAX_XFER_LEN ) ||
833 ( ( byteCount / ep->packetSize ) > MAX_PACKETS_PR_XFER ) )
835 DEBUG_USB_API_PUTS(
"\nUSBD_Write(), Illegal transfer size" );
840 if ( (uint32_t)data & 3 )
842 DEBUG_USB_API_PUTS(
"\nUSBD_Write(), Misaligned data buffer" );
848 if ( USBDHAL_EpIsStalled( ep ) )
851 DEBUG_USB_API_PUTS(
"\nUSBD_Write(), Endpoint is halted" );
855 if ( ep->state != D_EP_IDLE )
858 DEBUG_USB_API_PUTS(
"\nUSBD_Write(), Endpoint is busy" );
865 DEBUG_USB_API_PUTS(
"\nUSBD_Write(), Device not configured" );
869 ep->buf = (uint8_t*)data;
870 ep->remaining = byteCount;
877 else if ( ep->in !=
true )
880 DEBUG_USB_API_PUTS(
"\nUSBD_Write(), Illegal EP direction" );
885 ep->state = D_EP_TRANSMITTING;
886 ep->xferCompleteCb = callback;
USB protocol stack library API for EFM32/EZR32.
Clock management unit (CMU) API.
void CMU_ClockSelectSet(CMU_Clock_TypeDef clock, CMU_Select_TypeDef ref)
Select reference clock/oscillator used for a clock branch.
const uint8_t * bufferingMultiplier
USB Device stack initialization structure.
#define _USB_DOEP0CTL_MPS_32B
__STATIC_INLINE uint32_t INT_Enable(void)
Enable interrupts.
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
#define USB_SETUP_DIR_MASK
const USB_DeviceDescriptor_TypeDef * deviceDescriptor
void USBD_Connect(void)
Start USB device operation.
void USBTIMER_DelayMs(uint32_t msec)
Active wait millisecond delay function. Can also be used inside interrupt handlers.
USB Interface Descriptor.
int USBD_Init(const USBD_Init_TypeDef *p)
Initializes USB device hardware and internal protocol stack data structures, then connects the data-l...
USBD_State_TypeDef
USB device state enumerator.
int(* USB_XferCompleteCb_TypeDef)(USB_Status_TypeDef status, uint32_t xferred, uint32_t remaining)
USB transfer callback function.
void USBD_AbortAllTransfers(void)
Abort all pending transfers.
USB_Status_TypeDef
USB transfer status enumerator.
const uint8_t * configDescriptor
uint8_t bAlternateSetting
const void *const * stringDescriptors
int USBD_AbortTransfer(int epAddr)
Abort a pending transfer on a specific endpoint.
#define CONFIG_DESC_BM_TRANSFERTYPE
USB protocol stack library API for EFM32/EZR32.
void CMU_ClockEnable(CMU_Clock_TypeDef clock, bool enable)
Enable/disable a clock.
#define CMU_HFCORECLKEN0_USBC
USBD_Callbacks_TypeDef_Pointer callbacks
int USBD_RemoteWakeup(void)
Perform a remote wakeup signalling sequence.
int USBD_StallEp(int epAddr)
Set an endpoint in the stalled (halted) state.
#define CMU_HFCORECLKEN0_USB
USBD_State_TypeDef USBD_GetUsbState(void)
Get current USB device state.
#define USB_INTERFACE_DESCRIPTOR
void CMU_OscillatorEnable(CMU_Osc_TypeDef osc, bool enable, bool wait)
Enable/disable oscillator.
int USBD_Write(int epAddr, void *data, int byteCount, USB_XferCompleteCb_TypeDef callback)
Start a write (IN) transfer on an endpoint.
int USBD_UnStallEp(int epAddr)
Reset stall state on a stalled (halted) endpoint.
const uint8_t numberOfStrings
void USBD_Stop(void)
Stop USB device stack operation.
USB protocol stack library, low level USB peripheral access.
void USBD_Disconnect(void)
Stop USB device operation.
void USBTIMER_Init(void)
Activate the hardware timer used to pace the 1 millisecond timer system.
USB Configuration Descriptor.
const char * USBD_GetUsbStateName(USBD_State_TypeDef state)
Get a string naming a device USB state.
__STATIC_INLINE uint32_t INT_Disable(void)
Disable interrupts.
USB protocol stack library, internal type definitions.
bool USBD_SafeToEnterEM2(void)
Check if it is ok to enter energy mode EM2.
#define _USB_DOEP0CTL_MPS_64B
#define USB_ENDPOINT_DESCRIPTOR
bool USBD_EpIsBusy(int epAddr)
Check if an endpoint is busy doing a transfer.
int USBD_Read(int epAddr, void *data, int byteCount, USB_XferCompleteCb_TypeDef callback)
Start a read (OUT) transfer on an endpoint.
CMU_Select_TypeDef CMU_ClockSelectGet(CMU_Clock_TypeDef clock)
Get currently selected reference clock used for a clock branch.