17 #if defined( USB_PRESENT ) && ( USB_COUNT == 1 )
19 #if defined( USB_HOST )
26 #if ( USB_VBUSOVRCUR_PORT != USB_VBUSOVRCUR_PORT_NONE )
32 USBH_Hc_TypeDef hcs[ NUM_HC_USED + 2 ];
33 int USBH_attachRetryCount;
34 USBH_Init_TypeDef USBH_initData;
35 volatile USBH_PortState_TypeDef USBH_portStatus;
36 const USBH_AttachTiming_TypeDef USBH_attachTiming[]=
44 #define PORT_VBUS_DELAY 250
45 #define DEFAULT_CTRL_TIMEOUT 1000
47 static void Timeout(
int hcnum );
49 #if defined( __ICCARM__ )
50 #pragma diag_suppress=Pe175
53 static void Timeout0(
void){ Timeout(0); }
54 static void Timeout1(
void){ Timeout(1); }
55 static void Timeout2(
void){ Timeout(2); }
56 static void Timeout3(
void){ Timeout(3); }
57 static void Timeout4(
void){ Timeout(4); }
58 static void Timeout5(
void){ Timeout(5); }
59 static void Timeout6(
void){ Timeout(6); }
60 static void Timeout7(
void){ Timeout(7); }
61 static void Timeout8(
void){ Timeout(8); }
62 static void Timeout9(
void){ Timeout(9); }
63 static void Timeout10(
void){ Timeout(10); }
64 static void Timeout11(
void){ Timeout(11); }
65 static void Timeout12(
void){ Timeout(12); }
66 static void Timeout13(
void){ Timeout(13); }
68 #if defined( __ICCARM__ )
69 #pragma diag_default=Pe175
74 Timeout0, Timeout1, Timeout2, Timeout3, Timeout4, Timeout5,
75 Timeout6, Timeout7, Timeout8, Timeout9, Timeout10, Timeout11,
79 static void Timeout(
int hcnum )
85 #if defined( __GNUC__ )
86 #pragma GCC diagnostic push
87 #pragma GCC diagnostic ignored "-Warray-bounds"
92 #if defined( __GNUC__ )
93 #pragma GCC diagnostic pop
96 hcchar = USBHHAL_GetHcChar( hcnum );
101 USBHHAL_HCHalt( hcnum, hcchar );
102 hc->status |= HCS_TIMEOUT;
112 USBHHAL_HCStart( hcnum );
114 ep->epDesc.bInterval, hcTimeoutFunc[ hcnum ] );
118 ep->timeout -=
SL_MIN( ep->timeout, ep->epDesc.bInterval );
122 USBHHAL_HCStart( hcnum );
124 SL_MIN( ep->timeout, ep->epDesc.bInterval ),
125 hcTimeoutFunc[ hcnum ] );
129 USBHHAL_HCHalt( hcnum, hcchar );
130 hc->status |= HCS_TIMEOUT;
162 int USBH_AssignHostChannel( USBH_Ep_TypeDef *ep, uint8_t hcnum )
166 ep->packetSize = ep->epDesc.wMaxPacketSize;
167 ep->addr = ep->epDesc.bEndpointAddress;
168 ep->state = H_EP_IDLE;
172 DEBUG_USB_API_PUTS(
"\nUSBH_AssignHostChannel(),"
173 " ep NULL pointer" );
178 if ( hcnum >= MAX_NUM_HOSTCHANNELS )
180 DEBUG_USB_API_PUTS(
"\nUSBH_AssignHostChannel(),"
181 " Illegal host channel number" );
249 int USBH_ControlMsg( USBH_Ep_TypeDef *ep,
250 uint8_t bmRequestType,
263 DEBUG_USB_API_PUTS(
"\nUSBH_ControlMsg(), ep NULL pointer" );
268 if ( !USBH_DeviceConnected() )
270 DEBUG_USB_API_PUTS(
"\nUSBH_ControlMsg(), No device connected" );
274 if ( (uint32_t)data & 3 )
276 DEBUG_USB_API_PUTS(
"\nUSBH_ControlMsg(), Misaligned data buffer" );
282 if ( ep->state != H_EP_IDLE )
285 DEBUG_USB_API_PUTS(
"\nUSBH_ControlMsg(), Endpoint is busy" );
289 if ( !hcs[ ep->hcIn ].idle || !hcs[ ep->hcOut ].idle )
292 DEBUG_USB_API_PUTS(
"\nUSBH_ControlMsg(), Host channel is busy" );
296 ep->setup.bmRequestType = bmRequestType;
297 ep->setup.bRequest = bRequest;
298 ep->setup.wValue = wValue;
299 ep->setup.wIndex = wIndex;
300 ep->setup.wLength = wLength;
302 ep->xferCompleted =
false;
303 ep->state = H_EP_SETUP;
304 ep->xferCompleteCb = callback;
307 ep->timeout = timeout;
313 timeout, hcTimeoutFunc[ ep->hcOut ] );
316 USBH_CtlSendSetup( ep );
370 int USBH_ControlMsgB( USBH_Ep_TypeDef *ep,
371 uint8_t bmRequestType,
383 DEBUG_USB_API_PUTS(
"\nUSBH_ControlMsgB() called with int's disabled" );
388 retVal = USBH_ControlMsg( ep,
400 while ( ! ep->xferCompleted );
404 retVal = ep->xferred;
418 ep->toggle = USB_PID_DATA1;
419 ep->remaining = length;
421 hc = &hcs[ ep->hcIn ];
424 hc->remaining = length;
426 USBHHAL_HCStart( ep->hcIn );
431 #if defined( USB_RAW_API )
432 int USBH_CtlRxRaw( uint8_t pid, USBH_Ep_TypeDef *ep,
void *data,
int byteCount )
434 uint16_t packets, len;
436 uint32_t hcchar, status;
441 packets = ( byteCount + ep->packetSize - 1 ) / ep->packetSize;
444 len = packets * ep->packetSize;
448 USB->HC[ hcnum ].INTMSK = USB_HC_INT_STALL | USB_HC_INT_NAK | USB_HC_INT_ACK;
449 USB->HC[ hcnum ].INT = 0xFFFFFFFF;
451 USB->HAINTMSK |= 1 << hcnum;
452 USB->HC[ hcnum ].CHAR =
453 ( ep->parentDevice->addr << _USB_HC_CHAR_DEVADDR_SHIFT ) |
455 ( ep->type << _USB_HC_CHAR_EPTYPE_SHIFT ) |
456 ( ep->packetSize << _USB_HC_CHAR_MPS_SHIFT ) |
457 ( USB_HC_CHAR_EPDIR ) |
458 ( ep->parentDevice->speed ==
459 HPRT_L_SPEED >> _USB_HPRT_PRTSPD_SHIFT
460 ? USB_HC_CHAR_LSPDDEV : 0 );
461 USB->HC[ hcnum ].TSIZ =
462 ( ( len << _USB_HC_TSIZ_XFERSIZE_SHIFT ) &
463 _USB_HC_TSIZ_XFERSIZE_MASK ) |
464 ( ( packets << _USB_HC_TSIZ_PKTCNT_SHIFT ) &
465 _USB_HC_TSIZ_PKTCNT_MASK ) |
466 ( ( pid << _USB_HC_TSIZ_PID_SHIFT ) &
467 _USB_HC_TSIZ_PID_MASK );
468 USB->HC[ hcnum ].DMAADDR = (uint32_t)data;
469 hcchar = (
USB->HC[ hcnum ].CHAR & ~USB_HC_CHAR_CHDIS ) | USB_HC_CHAR_CHENA;
470 USB->HC[ hcnum ].CHAR = hcchar;
474 while ( ( USBHAL_GetCoreInts() & USB_GINTSTS_HCHINT ) == 0 ){}
475 if ( USBHHAL_GetHostChannelInts() & ( 1 << hcnum ) )
477 status = USBHHAL_GetHcInts( hcnum );
478 hcchar |= USB_HC_CHAR_CHDIS;
479 USB->HC[ hcnum ].CHAR = hcchar;
480 USB->HC[ hcnum ].INTMSK = 0;
481 USB->HC[ hcnum ].INT = 0xFFFFFFFF;
483 if ( status & USB_HC_INT_STALL )
485 else if ( status & USB_HC_INT_NAK )
487 else if ( status & USB_HC_INT_ACK )
502 ep->toggle = USB_PID_DATA1;
503 ep->remaining = length;
505 hc = &hcs[ ep->hcOut ];
508 hc->remaining = length;
510 USBHHAL_HCStart( ep->hcOut );
519 hc = &hcs[ ep->hcOut ];
520 hc->buf = (uint8_t*)&ep->setup;
524 hcs[ ep->hcIn ].ep = ep;
526 ep->toggle = USB_PID_SETUP;
530 USBHHAL_HCInit( ep->hcIn );
533 USBHHAL_HCInit( ep->hcOut );
535 USBHHAL_HCStart( ep->hcOut );
540 #if defined( USB_RAW_API )
541 int USBH_CtlTxRaw( uint8_t pid, USBH_Ep_TypeDef *ep,
void *data,
int byteCount )
545 uint32_t hcchar, status;
550 packets = ( byteCount + ep->packetSize - 1 ) / ep->packetSize;
556 USB->HC[ hcnum ].INTMSK = USB_HC_INT_STALL | USB_HC_INT_NAK | USB_HC_INT_ACK;
557 USB->HC[ hcnum ].INT = 0xFFFFFFFF;
559 USB->HAINTMSK |= 1 << hcnum;
560 USB->HC[ hcnum ].CHAR =
561 ( ep->parentDevice->addr << _USB_HC_CHAR_DEVADDR_SHIFT ) |
563 ( ep->type << _USB_HC_CHAR_EPTYPE_SHIFT ) |
564 ( ep->packetSize << _USB_HC_CHAR_MPS_SHIFT ) |
565 ( ep->parentDevice->speed ==
566 HPRT_L_SPEED >> _USB_HPRT_PRTSPD_SHIFT
567 ? USB_HC_CHAR_LSPDDEV : 0 );
568 USB->HC[ hcnum ].TSIZ =
569 ( ( byteCount << _USB_HC_TSIZ_XFERSIZE_SHIFT ) &
570 _USB_HC_TSIZ_XFERSIZE_MASK ) |
571 ( ( packets << _USB_HC_TSIZ_PKTCNT_SHIFT ) &
572 _USB_HC_TSIZ_PKTCNT_MASK ) |
573 ( ( pid << _USB_HC_TSIZ_PID_SHIFT ) &
574 _USB_HC_TSIZ_PID_MASK );
575 USB->HC[ hcnum ].DMAADDR = (uint32_t)data;
576 hcchar = (
USB->HC[ hcnum ].CHAR & ~USB_HC_CHAR_CHDIS ) | USB_HC_CHAR_CHENA;
577 USB->HC[ hcnum ].CHAR = hcchar;
581 while ( ( USBHAL_GetCoreInts() & USB_GINTSTS_HCHINT ) == 0 ){}
582 if ( USBHHAL_GetHostChannelInts() & ( 1 << hcnum ) )
584 status = USBHHAL_GetHcInts( hcnum );
585 hcchar |= USB_HC_CHAR_CHDIS;
586 USB->HC[ hcnum ].CHAR = hcchar;
587 USB->HC[ hcnum ].INTMSK = 0;
588 USB->HC[ hcnum ].INT = 0xFFFFFFFF;
590 if ( status & USB_HC_INT_STALL )
592 else if ( status & USB_HC_INT_NAK )
594 else if ( status & USB_HC_INT_ACK )
613 bool USBH_DeviceConnected(
void )
615 return USBH_portStatus == H_PORT_CONNECTED;
648 int USBH_GetConfigurationDescriptorB( USBH_Device_TypeDef *device,
650 uint8_t configIndex )
652 if ( device == NULL )
654 DEBUG_USB_API_PUTS(
"\nUSBH_GetConfigurationDescriptorB(),"
655 " device NULL pointer" );
662 DEBUG_USB_API_PUTS(
"\nUSBH_GetConfigurationDescriptorB(),"
663 " buf NULL pointer" );
668 return USBH_ControlMsgB( &device->ep0,
676 DEFAULT_CTRL_TIMEOUT );
705 int USBH_GetDeviceDescriptorB( USBH_Device_TypeDef *device,
708 if ( device == NULL )
710 DEBUG_USB_API_PUTS(
"\nUSBH_GetDeviceDescriptorB(),"
711 " device NULL pointer" );
718 DEBUG_USB_API_PUTS(
"\nUSBH_GetDeviceDescriptorB(),"
719 " buf NULL pointer" );
724 return USBH_ControlMsgB( &device->ep0,
732 DEFAULT_CTRL_TIMEOUT );
742 uint8_t USBH_GetPortSpeed(
void )
744 return USBHHAL_GetPortSpeed();
780 int USBH_GetStringB( USBH_Device_TypeDef *device, uint8_t *buf,
int bufLen,
781 uint8_t stringIndex, uint16_t langID )
786 if ( device == NULL )
788 DEBUG_USB_API_PUTS(
"\nUSBH_GetStringB(), device NULL pointer" );
795 DEBUG_USB_API_PUTS(
"\nUSBH_GetStringB(), buf NULL pointer" );
800 retVal = USBH_ControlMsgB(
809 DEFAULT_CTRL_TIMEOUT );
839 int USBH_Init(
const USBH_Init_TypeDef *p )
843 DEBUG_USB_API_PUTS(
"\nUSBH_Init(), init struct NULL pointer" );
848 if ( ( p->rxFifoSize < MIN_EP_FIFO_SIZE_INBYTES )
849 || ( p->nptxFifoSize < MIN_EP_FIFO_SIZE_INBYTES )
850 || ( p->ptxFifoSize < MIN_EP_FIFO_SIZE_INBYTES )
851 || ( (p->rxFifoSize + p->nptxFifoSize + p->ptxFifoSize) >
852 (MAX_HOST_FIFO_SIZE_INWORDS * 4) ) )
854 DEBUG_USB_API_PUTS(
"\nUSBH_Init(), Illegal Tx/Rx FIFO memory configuration" );
862 USBH_portStatus = H_PORT_DISCONNECTED;
866 CMU->CMD = CMU_CMD_USBCCLKSEL_HFCLKNODIV;
872 NVIC_EnableIRQ( USB_IRQn );
911 int USBH_InitDeviceData( USBH_Device_TypeDef *device,
915 uint8_t deviceSpeed )
919 if ( device == NULL )
921 DEBUG_USB_API_PUTS(
"\nUSBH_InitDeviceData(), device NULL pointer" );
926 memset( device, 0,
sizeof( USBH_Device_TypeDef ) );
930 device->numEp = numEp;
931 device->speed = deviceSpeed;
933 device->ep0.packetSize = 8;
936 memcpy( &device->ep0,
937 &((USBH_Device_TypeDef*)buf)->ep0,
sizeof( USBH_Ep_TypeDef ) );
939 device->ep0.parentDevice = device;
941 device->ep0.hcOut = 0;
942 device->ep0.hcIn = 1;
946 for ( i=0; i<numEp; i++ )
948 memset( &device->ep[i], 0,
sizeof( USBH_Ep_TypeDef ) );
949 device->ep[i].parentDevice = device;
950 device->ep[i].toggle = USB_PID_DATA0;
956 memcpy( &device->devDesc,
958 memcpy( &device->confDesc,
960 memcpy( &device->itfDesc,
965 for ( i=0; i<numEp; i++ )
967 memcpy( &device->ep[i].epDesc,
968 USBH_QGetEndpointDescriptor( buf, 0, 0, i ),
989 int USBH_PortReset(
void )
993 DEBUG_USB_API_PUTS(
"\nUSBH_PortReset() called with int's disabled" );
998 USBH_portStatus = H_PORT_CONNECTED_RESETTING;
1000 USBHHAL_PortReset(
true );
1006 USBHHAL_PortReset(
false );
1010 USBH_portStatus = H_PORT_DISCONNECTED;
1022 int USBH_PortResume(
void )
1026 DEBUG_USB_API_PUTS(
"\nUSBH_PortResume() called with int's disabled" );
1027 EFM_ASSERT(
false );
1032 USBHHAL_PortResume(
true );
1038 USBHHAL_PortResume(
false );
1041 return USB_STATUS_OK;
1048 void USBH_PortSuspend(
void )
1051 USBHHAL_PortSuspend();
1057 int USBH_PortVbusOn(
bool on )
1061 DEBUG_USB_API_PUTS(
"\nUSBH_PortVbusOn() called with int's disabled" );
1062 EFM_ASSERT(
false );
1067 USBHHAL_VbusOn( on );
1071 USBH_portStatus = H_PORT_DISCONNECTED;
1081 #if defined( USB_USE_PRINTF )
1102 int USBH_PrintConfigurationDescriptor(
1109 if ( config == NULL )
1111 DEBUG_USB_API_PUTS(
"\nUSBH_PrintConfigurationDescriptor(),"
1112 " config NULL pointer" );
1113 EFM_ASSERT(
false );
1117 USB_PRINTF(
"\n\nConfiguration descriptor:" );
1131 if ( remaining > 0 )
1134 c = (
char*)(config + 1);
1135 while ( remaining-- )
1165 if ( device == NULL )
1167 DEBUG_USB_API_PUTS(
"\nUSBH_PrintDeviceDescriptor(),"
1168 " device NULL pointer" );
1169 EFM_ASSERT(
false );
1209 int USBH_PrintEndpointDescriptor(
1212 if ( endpoint == NULL )
1214 DEBUG_USB_API_PUTS(
"\nUSBH_PrintEndpointDescriptor(),"
1215 " endpoint NULL pointer" );
1216 EFM_ASSERT(
false );
1248 int USBH_PrintInterfaceDescriptor(
1251 if ( interface == NULL )
1253 DEBUG_USB_API_PUTS(
"\nUSBH_PrintInterfaceDescriptor(),"
1254 " interface NULL pointer" );
1255 EFM_ASSERT(
false );
1274 #if defined( __ICCARM__ )
1275 #pragma diag_suppress=Pa039
1297 void USBH_PrintString(
const char *pre,
1310 c = (
char*)&s->
name;
1323 #if defined( __ICCARM__ )
1324 #pragma diag_default=Pa039
1361 const uint8_t *buf,
int configIndex )
1364 const uint8_t *start, *end;
1367 dev = USBH_QGetDeviceDescriptor( buf );
1370 if ( configIndex < dev->bNumConfigurations )
1373 start = buf +
sizeof( USBH_Device_TypeDef );
1378 (uint8_t*)(((USBH_Device_TypeDef*)buf)->ep));
1382 while ( start < end )
1390 if ( i == configIndex )
1422 DEBUG_USB_API_PUTS(
"\nUSBH_QGetDeviceDescriptor(), buf NULL pointer" );
1423 EFM_ASSERT(
false );
1464 const uint8_t *buf,
int configIndex,
int interfaceIndex,
int endpointIndex )
1467 uint8_t *start, *end;
1470 itf = USBH_QGetInterfaceDescriptor( buf, configIndex, interfaceIndex );
1473 if ( endpointIndex < itf->bNumEndpoints )
1479 end = (uint8_t*)USBH_QGetInterfaceDescriptor(
1480 buf, configIndex, interfaceIndex + 1 );
1484 end = (uint8_t*)((USBH_Device_TypeDef*)buf)->ep;
1489 while ( start < end )
1497 if ( i == endpointIndex )
1536 const uint8_t *buf,
int configIndex,
int interfaceIndex )
1539 uint8_t *start, *end;
1542 conf = USBH_QGetConfigurationDescriptor( buf, configIndex );
1545 if ( interfaceIndex < conf->bNumInterfaces )
1551 end = (uint8_t*)USBH_QGetConfigurationDescriptor( buf, configIndex + 1 );
1555 end = (uint8_t*)((USBH_Device_TypeDef*)buf)->ep;
1560 while ( start < end )
1568 if ( ( i == interfaceIndex ) &&
1615 int USBH_QueryDeviceB( uint8_t *buf,
size_t bufsize, uint8_t deviceSpeed )
1617 USBH_Device_TypeDef *device = (USBH_Device_TypeDef*)buf;
1619 USBH_InitDeviceData( device, NULL, NULL, 0, deviceSpeed );
1621 device->speed = deviceSpeed;
1622 device->ep = (USBH_Ep_TypeDef*)(buf+bufsize);
1625 if ( 8 != USBH_GetDeviceDescriptorB( device,
1627 SL_MIN( 8U, bufsize ) ) )
1631 device->ep0.packetSize = device->devDesc.bMaxPacketSize0;
1636 buf +
sizeof( USBH_Device_TypeDef ),
1641 memcpy( &device->devDesc,
1642 buf +
sizeof( USBH_Device_TypeDef ), USB_DEVICE_DESCSIZE );
1647 buf +
sizeof( USBH_Device_TypeDef ),
1649 bufsize - (
size_t)USB_DEVICE_DESCSIZE ),
1654 memcpy( &device->confDesc,
1655 buf +
sizeof( USBH_Device_TypeDef ), USB_CONFIG_DESCSIZE );
1660 USBH_GetConfigurationDescriptorB(
1662 buf +
sizeof( USBH_Device_TypeDef ),
1663 SL_MIN( device->confDesc.wTotalLength,
1664 bufsize -
sizeof( USBH_Device_TypeDef )),
1706 int USBH_Read( USBH_Ep_TypeDef *ep,
void *data,
int byteCount,
int timeout,
1709 USBH_Hc_TypeDef *hc = &hcs[ ep->hcIn ];
1714 DEBUG_USB_API_PUTS(
"\nUSBH_Read(), ep NULL pointer" );
1715 EFM_ASSERT(
false );
1719 if ( !USBH_DeviceConnected() )
1721 DEBUG_USB_API_PUTS(
"\nUSBH_Read(), No device connected" );
1725 if ( ( byteCount > MAX_XFER_LEN ) ||
1726 ( ( byteCount / ep->packetSize ) > MAX_PACKETS_PR_XFER ) )
1728 DEBUG_USB_API_PUTS(
"\nUSBH_Read(), Illegal transfer size" );
1729 EFM_ASSERT(
false );
1733 if ( (uint32_t)data & 3 )
1735 DEBUG_USB_API_PUTS(
"\nUSBH_Read(), Misaligned data buffer" );
1736 EFM_ASSERT(
false );
1741 if ( ep->state != H_EP_IDLE )
1744 DEBUG_USB_API_PUTS(
"\nUSBH_Read(), Endpoint is busy" );
1751 DEBUG_USB_API_PUTS(
"\nUSBH_Read(), Illegal EP direction" );
1752 EFM_ASSERT(
false );
1759 DEBUG_USB_API_PUTS(
"\nUSBH_Read(), Host channel is busy" );
1763 ep->xferCompleted =
false;
1764 ep->state = H_EP_DATA_IN;
1765 ep->xferCompleteCb = callback;
1767 ep->remaining = byteCount;
1769 ep->timeout = timeout;
1773 hc->remaining = byteCount;
1779 timeout =
SL_MIN( timeout, ep->epDesc.bInterval );
1781 timeout = ep->epDesc.bInterval;
1787 timeout, hcTimeoutFunc[ ep->hcIn ] );
1790 USBHHAL_HCInit( ep->hcIn );
1791 USBHHAL_HCStart( ep->hcIn );
1827 int USBH_ReadB( USBH_Ep_TypeDef *ep,
void *data,
int byteCount,
int timeout )
1833 DEBUG_USB_API_PUTS(
"\nUSBH_ReadB() called with int's disabled" );
1834 EFM_ASSERT(
false );
1838 retVal = USBH_Read( ep, data, byteCount, timeout, NULL );
1840 if ( retVal == USB_STATUS_OK )
1842 while ( ! ep->xferCompleted );
1844 if ( ( retVal = ep->xferStatus ) == USB_STATUS_OK )
1846 retVal = ep->xferred;
1873 int USBH_SetAddressB( USBH_Device_TypeDef *device, uint8_t deviceAddress )
1877 if ( device == NULL )
1879 DEBUG_USB_API_PUTS(
"\nUSBH_SetAddressB(), device NULL pointer" );
1880 EFM_ASSERT(
false );
1886 DEBUG_USB_API_PUTS(
"\nUSBH_SetAddressB(), Illegal device address" );
1887 EFM_ASSERT(
false );
1891 retVal = USBH_ControlMsgB(
1900 DEFAULT_CTRL_TIMEOUT );
1902 if ( retVal == USB_STATUS_OK )
1904 device->addr = deviceAddress;
1930 int USBH_SetAltInterfaceB( USBH_Device_TypeDef *device,
1931 uint8_t interfaceIndex, uint8_t alternateSetting )
1935 if ( device == NULL )
1937 DEBUG_USB_API_PUTS(
"\nUSBH_SetAltInterfaceB(), device NULL pointer" );
1938 EFM_ASSERT(
false );
1942 retVal = USBH_ControlMsgB(
1951 DEFAULT_CTRL_TIMEOUT );
1953 if ( retVal == USB_STATUS_OK )
1961 for ( i=0; i<device->numEp; i++ )
1963 device->ep[i].toggle = USB_PID_DATA0;
1990 int USBH_SetConfigurationB( USBH_Device_TypeDef *device, uint8_t configValue )
1994 if ( device == NULL )
1996 DEBUG_USB_API_PUTS(
"\nUSBH_SetConfigurationB(), device NULL pointer" );
1997 EFM_ASSERT(
false );
2001 retVal = USBH_ControlMsgB(
2010 DEFAULT_CTRL_TIMEOUT );
2012 if ( retVal == USB_STATUS_OK )
2020 for ( i=0; i<device->numEp; i++ )
2022 device->ep[i].toggle = USB_PID_DATA0;
2044 int USBH_StallEpB( USBH_Ep_TypeDef *ep )
2048 DEBUG_USB_API_PUTS(
"\nUSBH_StallEpB(), ep NULL pointer" );
2049 EFM_ASSERT(
false );
2053 return USBH_ControlMsgB( &ep->parentDevice->ep0,
2061 DEFAULT_CTRL_TIMEOUT );
2071 void USBH_Stop(
void )
2076 for ( i = 0; i < NUM_HC_USED + 2; i++ )
2081 USBHAL_DisableGlobalInt();
2083 USBH_PortVbusOn(
false );
2084 USBHAL_DisablePhyPins();
2104 int USBH_UnStallEpB( USBH_Ep_TypeDef *ep )
2110 DEBUG_USB_API_PUTS(
"\nUSBH_UnStallEpB(), ep NULL pointer" );
2111 EFM_ASSERT(
false );
2115 retVal = USBH_ControlMsgB( &ep->parentDevice->ep0,
2123 DEFAULT_CTRL_TIMEOUT );
2125 if ( retVal == USB_STATUS_OK )
2127 ep->toggle = USB_PID_DATA0;
2135 static void InitUsb(
void )
2139 memset( hcs, 0,
sizeof( hcs ) );
2140 for ( i = 0; i < NUM_HC_USED + 2; i++ )
2142 hcs[ i ].idle =
true;
2146 USBHHAL_CoreInit( USBH_initData.rxFifoSize, USBH_initData.nptxFifoSize,
2147 USBH_initData.ptxFifoSize );
2148 NVIC_ClearPendingIRQ( USB_IRQn );
2181 int USBH_WaitForDeviceConnectionB( uint8_t *buf,
int timeoutInSeconds )
2184 USBH_Device_TypeDef *device;
2185 int accumulatedTime, deadLine;
2189 DEBUG_USB_API_PUTS(
"\nUSBH_WaitForDeviceConnectionB(),"
2190 " buf NULL pointer" );
2191 EFM_ASSERT(
false );
2196 accumulatedTime = 0;
2197 USBH_attachRetryCount = 0;
2198 deadLine = timeoutInSeconds * 1000;
2199 device = (USBH_Device_TypeDef*)buf;
2204 if ( USBHHAL_InitializedAndPowered() )
2206 USBH_PortVbusOn(
false );
2208 accumulatedTime += PORT_VBUS_DELAY;
2214 accumulatedTime += 50;
2217 USBH_PortVbusOn(
true );
2218 DEBUG_USB_INT_LO_PUTCHAR(
'1' );
2220 accumulatedTime += PORT_VBUS_DELAY;
2224 USBHHAL_EnableInts();
2225 USBHAL_EnableGlobalInt();
2231 if ( USBH_DeviceConnected() )
2234 if ( ( deadLine ) &&
2235 ( accumulatedTime >= deadLine ) )
2240 #if ( USB_VBUSOVRCUR_PORT != USB_VBUSOVRCUR_PORT_NONE )
2241 if (
GPIO_PinInGet( USB_VBUSOVRCUR_PORT, USB_VBUSOVRCUR_PIN ) ==
2242 USB_VBUSOVRCUR_POLARITY )
2244 DEBUG_USB_INT_LO_PUTCHAR(
'~' );
2245 USBHHAL_PortReset(
false );
2246 USBHHAL_VbusOn(
false );
2248 USBH_portStatus = H_PORT_OVERCURRENT;
2254 accumulatedTime += 50;
2261 accumulatedTime += 100;
2265 USBH_InitDeviceData( device, NULL, NULL, 0, USBH_GetPortSpeed() );
2268 if ( 8 == USBH_GetDeviceDescriptorB( device, &device->devDesc, 8 ) )
2276 accumulatedTime += DEFAULT_CTRL_TIMEOUT;
2285 accumulatedTime += PORT_VBUS_DELAY;
2287 if ( ( deadLine ) &&
2288 ( accumulatedTime >= deadLine ) )
2291 USBH_attachRetryCount = ( USBH_attachRetryCount + 1 ) %
2292 (
sizeof( USBH_attachTiming ) /
2293 sizeof( USBH_AttachTiming_TypeDef ) );
2333 int USBH_Write( USBH_Ep_TypeDef *ep,
void *data,
int byteCount,
2336 USBH_Hc_TypeDef *hc = &hcs[ ep->hcOut ];
2341 DEBUG_USB_API_PUTS(
"\nUSBH_Write(), ep NULL pointer" );
2342 EFM_ASSERT(
false );
2346 if ( !USBH_DeviceConnected() )
2348 DEBUG_USB_API_PUTS(
"\nUSBH_Write(), No device connected" );
2352 if ( ( byteCount > MAX_XFER_LEN ) ||
2353 ( ( byteCount / ep->packetSize ) > MAX_PACKETS_PR_XFER ) )
2355 DEBUG_USB_API_PUTS(
"\nUSBH_Write(), Illegal transfer size" );
2356 EFM_ASSERT(
false );
2360 if ( (uint32_t)data & 3 )
2362 DEBUG_USB_API_PUTS(
"\nUSBH_Write(), Misaligned data buffer" );
2363 EFM_ASSERT(
false );
2368 if ( ep->state != H_EP_IDLE )
2371 DEBUG_USB_API_PUTS(
"\nUSBH_Write(), Endpoint is busy" );
2378 DEBUG_USB_API_PUTS(
"\nUSBH_Write(), Illegal EP direction" );
2379 EFM_ASSERT(
false );
2386 DEBUG_USB_API_PUTS(
"\nUSBH_Write(), Host channel is busy" );
2390 ep->xferCompleted =
false;
2391 ep->state = H_EP_DATA_OUT;
2392 ep->xferCompleteCb = callback;
2394 ep->remaining = byteCount;
2396 ep->timeout = timeout;
2400 hc->remaining = byteCount;
2406 timeout =
SL_MIN( timeout, ep->epDesc.bInterval );
2408 timeout = ep->epDesc.bInterval;
2414 timeout, hcTimeoutFunc[ ep->hcOut ] );
2417 USBHHAL_HCInit( ep->hcOut );
2418 USBHHAL_HCStart( ep->hcOut );
2452 int USBH_WriteB( USBH_Ep_TypeDef *ep,
void *data,
int byteCount,
int timeout )
2458 DEBUG_USB_API_PUTS(
"\nUSBH_WriteB() called with int's disabled" );
2459 EFM_ASSERT(
false );
2463 retVal = USBH_Write( ep, data, byteCount, timeout, NULL );
2465 if ( retVal == USB_STATUS_OK )
2467 while ( ! ep->xferCompleted );
2469 if ( ( retVal = ep->xferStatus ) == USB_STATUS_OK )
2471 retVal = ep->xferred;
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.
#define USB_INTERFACE_DESCSIZE
#define CMU_HFCORECLKEN0_USB
#define SET_CONFIGURATION
#define CORE_IN_IRQ_CONTEXT()
uint8_t bInterfaceProtocol
#define CORE_DECLARE_IRQ_STATE
#define USB_SETUP_DIR_D2H
void USBTIMER_Stop(uint32_t id)
Stop a timer.
uint8_t bInterfaceSubClass
USB protocol stack library API for EFM32/EZR32.
#define USB_SETUP_DIR_H2D
#define USB_STRING_DESCRIPTOR
#define USB_SETUP_RECIPIENT_ENDPOINT
int USB_PRINTF(const char *format,...)
Transmit "printf" formated data on the debug serial port.
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
uint8_t bConfigurationValue
#define USB_SETUP_DIR_MASK
void(* USBTIMER_Callback_TypeDef)(void)
USBTIMER callback function.
void USBTIMER_DelayMs(uint32_t msec)
Active wait millisecond delay function. Can also be used inside interrupt handlers.
USB Interface Descriptor.
int(* USB_XferCompleteCb_TypeDef)(USB_Status_TypeDef status, uint32_t xferred, uint32_t remaining)
USB transfer callback function.
USB_Status_TypeDef
USB transfer status enumerator.
#define CORE_ENTER_ATOMIC()
#define USB_DEVICE_DESCRIPTOR
#define CORE_ATOMIC_SECTION(yourcode)
#define USB_CONFIG_DESCSIZE
void USB_PUTS(const char *p)
Transmit a zero terminated string on the debug serial port.
General Purpose IO (GPIO) peripheral API.
uint8_t bAlternateSetting
#define CONFIG_DESC_BM_TRANSFERTYPE
USB protocol stack library API for EFM32/EZR32.
#define USB_SETUP_RECIPIENT_DEVICE
Core interrupt handling API.
#define USB_SETUP_RECIPIENT_INTERFACE
#define USB_DEVICE_DESCSIZE
uint8_t bNumConfigurations
#define CMU_HFCORECLKEN0_USBC
#define USB_SETUP_TYPE_STANDARD_MASK
#define CORE_IRQ_DISABLED()
#define CORE_EXIT_ATOMIC()
#define USB_INTERFACE_DESCRIPTOR
#define USB_ENDPOINT_DESCSIZE
#define USB_MAX_DEVICE_ADDRESS
#define USB_FEATURE_ENDPOINT_HALT
USB protocol stack library, low level USB peripheral access.
void USBTIMER_Init(void)
Activate the hardware timer used to pace the 1 millisecond timer system.
#define SL_MIN(a, b)
Macro for getting minimum value. No sideeffects, a and b are evaluated once only. ...
USB Configuration Descriptor.
#define USB_CONFIG_DESCRIPTOR
#define USB_SETUP_PKT_SIZE
USB protocol stack library, internal type definitions.
int USB_PUTCHAR(char c)
Transmit a single char on the debug serial port.
void USBTIMER_Start(uint32_t id, uint32_t timeout, USBTIMER_Callback_TypeDef callback)
Start a timer.
__STATIC_INLINE unsigned int GPIO_PinInGet(GPIO_Port_TypeDef port, unsigned int pin)
Read the pad value for a single pin in a GPIO port.
#define USB_ENDPOINT_DESCRIPTOR