17 #if defined( USB_PRESENT ) && ( USB_COUNT == 1 )
19 #if defined( USB_DEVICE )
30 static USBD_Device_TypeDef device;
31 USBD_Device_TypeDef *dev = &device;
33 static const char *stateNames[] =
76 USBD_Ep_TypeDef *ep = USBD_GetEpFromAddr( epAddr );
81 DEBUG_USB_API_PUTS(
"\nUSBD_AbortTransfer(), Illegal endpoint" );
88 DEBUG_USB_API_PUTS(
"\nUSBD_AbortTransfer(), Illegal endpoint" );
94 if ( ep->state == D_EP_IDLE )
102 ep->state = D_EP_IDLE;
103 if ( ep->xferCompleteCb )
105 callback = ep->xferCompleteCb;
106 ep->xferCompleteCb = NULL;
111 USBDHAL_DeactivateEp( ep );
148 USBDHAL_Disconnect();
164 USBD_Ep_TypeDef *ep = USBD_GetEpFromAddr( epAddr );
168 DEBUG_USB_API_PUTS(
"\nUSBD_EpIsBusy(), Illegal endpoint" );
173 if ( ep->state == D_EP_IDLE )
206 return stateNames[ state ];
231 uint8_t *conf, *confEnd;
232 #if defined( CMU_OSCENCMD_USHFRCOEN )
237 uint32_t totalRxFifoSize, totalTxFifoSize, numInEps, numOutEps;
240 #if !defined( USB_CORECLK_HFRCO ) || !defined( CMU_OSCENCMD_USHFRCOEN )
249 #if !defined( CMU_OSCENCMD_USHFRCOEN )
250 #if ( USB_USBC_32kHz_CLK == USB_USBC_32kHz_CLK_LFXO )
259 #if ( USB_USBC_32kHz_CLK == USB_USBC_32kHz_CLK_LFXO )
269 memset( dev, 0,
sizeof( USBD_Device_TypeDef ) );
271 dev->setup = dev->setupPkt;
281 dev->remoteWakeupEnabled =
false;
295 if ( ep->packetSize == 32 )
299 else if ( ep->packetSize == 64 )
310 ep->state = D_EP_IDLE;
311 ep->xferCompleteCb = NULL;
312 ep->fifoSize = ep->packetSize / 4;
317 #
if defined( DEBUG_USB_API )
324 conf = (uint8_t*)dev->configDescriptor;
325 confEnd = conf + dev->configDescriptor->wTotalLength;
327 while ( conf < confEnd )
336 if ( ( conf != confEnd ) ||
337 ( i != dev->configDescriptor->wTotalLength ) )
339 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal configuration descriptor" );
350 conf = (uint8_t*)dev->configDescriptor;
351 confEnd = conf + dev->configDescriptor->wTotalLength;
354 while ( conf < confEnd )
358 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal configuration descriptor" );
368 #if defined( __GNUC__ )
369 #pragma GCC diagnostic push
370 #pragma GCC diagnostic ignored "-Warray-bounds"
372 ep = &dev->ep[ numEps ];
373 #if defined( __GNUC__ )
374 #pragma GCC diagnostic pop
381 ep->mask = 1 << ep->num;
386 ep->state = D_EP_IDLE;
387 ep->xferCompleteCb = NULL;
391 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal EP fifo buffer configuration" );
399 ep->txFifoNum = txFifoNum++;
400 ep->fifoSize = ( ( ep->packetSize + 3 ) / 4 )
402 dev->inEpAddr2EpIndex[ ep->num ] = numEps;
403 totalTxFifoSize += ep->fifoSize;
404 if ( ep->num > MAX_NUM_IN_EPS )
406 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal IN EP address" );
414 ep->fifoSize = ( ( ( ep->packetSize + 3 ) / 4 ) + 1 )
416 dev->outEpAddr2EpIndex[ ep->num ] = numEps;
417 totalRxFifoSize += ep->fifoSize;
418 if ( ep->num > MAX_NUM_OUT_EPS )
420 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal OUT EP address" );
434 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal interface number" );
438 dev->numberOfInterfaces++;
449 totalRxFifoSize += 10 + 1 + ( 2 * (MAX_NUM_OUT_EPS + 1) );
451 if ( dev->configDescriptor->bNumInterfaces != dev->numberOfInterfaces )
453 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal interface count" );
458 if ( numEps != NUM_EP_USED )
460 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal EP count" );
465 if ( numInEps > MAX_NUM_IN_EPS )
467 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal IN EP count" );
472 if ( numOutEps > MAX_NUM_OUT_EPS )
474 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), Illegal OUT EP count" );
484 #if defined( CMU_OSCENCMD_USHFRCOEN )
485 CMU->USHFRCOCONF = CMU_USHFRCOCONF_BAND_48MHZ;
489 CMU->USBCRCTRL |= CMU_USBCRCTRL_EN;
494 && (chipRev.
major == 1)
495 && (chipRev.
minor == 0))
498 USB->CTRL = USB_CTRL_LEMOSCCTRL_GATE
500 | USB_CTRL_LEMPHYCTRL;
504 USB->CTRL = USB_CTRL_LEMOSCCTRL_GATE
506 | USB_CTRL_LEMPHYCTRL
508 | USB_CTRL_LEMADDRMEN;
514 USBHAL_DisableGlobalInt();
516 if ( USBDHAL_CoreInit( totalRxFifoSize, totalTxFifoSize ) ==
USB_STATUS_OK )
518 USBDHAL_EnableUsbResetAndSuspendInt();
519 USBHAL_EnableGlobalInt();
526 DEBUG_USB_API_PUTS(
"\nUSBD_Init(), FIFO setup error" );
531 #if ( USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_ONVBUSOFF )
532 if ( USBHAL_VbusIsOn() )
577 USBD_Ep_TypeDef *ep = USBD_GetEpFromAddr( epAddr );
581 DEBUG_USB_API_PUTS(
"\nUSBD_Read(), Illegal endpoint" );
586 if ( ( byteCount > MAX_XFER_LEN ) ||
587 ( ( byteCount / ep->packetSize ) > MAX_PACKETS_PR_XFER ) )
589 DEBUG_USB_API_PUTS(
"\nUSBD_Read(), Illegal transfer size" );
594 if ( (uint32_t)data & 3 )
596 DEBUG_USB_API_PUTS(
"\nUSBD_Read(), Misaligned data buffer" );
602 if ( USBDHAL_EpIsStalled( ep ) )
605 DEBUG_USB_API_PUTS(
"\nUSBD_Read(), Endpoint is halted" );
609 if ( ep->state != D_EP_IDLE )
612 DEBUG_USB_API_PUTS(
"\nUSBD_Read(), Endpoint is busy" );
619 DEBUG_USB_API_PUTS(
"\nUSBD_Read(), Device not configured" );
623 ep->buf = (uint8_t*)data;
624 ep->remaining = byteCount;
631 else if ( ep->in !=
false )
634 DEBUG_USB_API_PUTS(
"\nUSBD_Read(), Illegal EP direction" );
639 ep->state = D_EP_RECEIVING;
640 ep->xferCompleteCb = callback;
668 ( dev->remoteWakeupEnabled ==
false ) )
671 DEBUG_USB_API_PUTS(
"\nUSBD_RemoteWakeup(), Illegal remote wakeup" );
675 USBDINT_RemoteWakeup();
695 #if ( USB_PWRSAVE_MODE )
696 return USBD_poweredDown ?
true :
false;
708 currentState = dev->state;
711 dev->savedState = currentState;
714 dev->lastState = dev->state;
715 dev->state = newState;
717 if ( ( dev->callbacks->usbStateChange ) &&
718 ( currentState != newState ) )
720 dev->callbacks->usbStateChange( currentState, newState );
740 USBD_Ep_TypeDef *ep = USBD_GetEpFromAddr( epAddr );
744 DEBUG_USB_API_PUTS(
"\nUSBD_StallEp(), Illegal request" );
751 DEBUG_USB_API_PUTS(
"\nUSBD_StallEp(), Illegal endpoint" );
757 retVal = USBDHAL_StallEp( ep );
780 USBHAL_DisableGlobalInt();
781 USBHAL_DisableUsbInt();
782 USBHAL_DisablePhyPins();
802 USBD_Ep_TypeDef *ep = USBD_GetEpFromAddr( epAddr );
806 DEBUG_USB_API_PUTS(
"\nUSBD_UnStallEp(), Illegal request" );
813 DEBUG_USB_API_PUTS(
"\nUSBD_UnStallEp(), Illegal endpoint" );
819 retVal = USBDHAL_UnStallEp( ep );
855 USBD_Ep_TypeDef *ep = USBD_GetEpFromAddr( epAddr );
859 DEBUG_USB_API_PUTS(
"\nUSBD_Write(), Illegal endpoint" );
864 if ( ( byteCount > MAX_XFER_LEN ) ||
865 ( ( byteCount / ep->packetSize ) > MAX_PACKETS_PR_XFER ) )
867 DEBUG_USB_API_PUTS(
"\nUSBD_Write(), Illegal transfer size" );
872 if ( (uint32_t)data & 3 )
874 DEBUG_USB_API_PUTS(
"\nUSBD_Write(), Misaligned data buffer" );
880 if ( USBDHAL_EpIsStalled( ep ) )
883 DEBUG_USB_API_PUTS(
"\nUSBD_Write(), Endpoint is halted" );
887 if ( ep->state != D_EP_IDLE )
890 DEBUG_USB_API_PUTS(
"\nUSBD_Write(), Endpoint is busy" );
897 DEBUG_USB_API_PUTS(
"\nUSBD_Write(), Device not configured" );
901 ep->buf = (uint8_t*)data;
902 ep->remaining = byteCount;
909 else if ( ep->in !=
true )
912 DEBUG_USB_API_PUTS(
"\nUSBD_Write(), Illegal EP direction" );
917 ep->state = D_EP_TRANSMITTING;
918 ep->xferCompleteCb = callback;
925 #if defined(USB_HOST)
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
#define CORE_DECLARE_IRQ_STATE
USB Device stack initialization structure.
void SYSTEM_ChipRevisionGet(SYSTEM_ChipRevision_TypeDef *rev)
Get chip major/minor revision.
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.
#define _USB_DOEP0CTL_MPS_32B
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.
#define CORE_ENTER_ATOMIC()
const uint8_t * configDescriptor
#define CORE_ATOMIC_SECTION(yourcode)
#define CMU_HFCORECLKEN0_USBC
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.
Core interrupt handling API.
void CMU_ClockEnable(CMU_Clock_TypeDef clock, bool enable)
Enable/disable a clock.
USBD_Callbacks_TypeDef_Pointer callbacks
#define CMU_HFCORECLKEN0_USB
int USBD_RemoteWakeup(void)
Perform a remote wakeup signalling sequence.
int USBD_StallEp(int epAddr)
Set an endpoint in the stalled (halted) state.
#define CORE_EXIT_ATOMIC()
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.
#define _USB_DOEP0CTL_MPS_64B
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.
USB protocol stack library, internal type definitions.
bool USBD_SafeToEnterEM2(void)
Check if it is ok to enter energy mode EM2.
#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.