17 #if defined( USB_PRESENT ) && ( USB_COUNT == 1 )
19 #if defined( USB_HOST )
25 #if ( USB_VBUSOVRCUR_PORT != USB_VBUSOVRCUR_PORT_NONE )
31 USBH_Hc_TypeDef hcs[ NUM_HC_USED + 2 ];
32 int USBH_attachRetryCount;
33 USBH_Init_TypeDef USBH_initData;
34 volatile USBH_PortState_TypeDef USBH_portStatus;
35 const USBH_AttachTiming_TypeDef USBH_attachTiming[]=
43 #define PORT_VBUS_DELAY 250
44 #define DEFAULT_CTRL_TIMEOUT 1000
46 static void Timeout(
int hcnum );
48 #if defined( __ICCARM__ )
49 #pragma diag_suppress=Pe175
52 static void Timeout0(
void){ Timeout(0); }
53 static void Timeout1(
void){ Timeout(1); }
54 static void Timeout2(
void){ Timeout(2); }
55 static void Timeout3(
void){ Timeout(3); }
56 static void Timeout4(
void){ Timeout(4); }
57 static void Timeout5(
void){ Timeout(5); }
58 static void Timeout6(
void){ Timeout(6); }
59 static void Timeout7(
void){ Timeout(7); }
60 static void Timeout8(
void){ Timeout(8); }
61 static void Timeout9(
void){ Timeout(9); }
62 static void Timeout10(
void){ Timeout(10); }
63 static void Timeout11(
void){ Timeout(11); }
64 static void Timeout12(
void){ Timeout(12); }
65 static void Timeout13(
void){ Timeout(13); }
67 #if defined( __ICCARM__ )
68 #pragma diag_default=Pe175
73 Timeout0, Timeout1, Timeout2, Timeout3, Timeout4, Timeout5,
74 Timeout6, Timeout7, Timeout8, Timeout9, Timeout10, Timeout11,
78 static void Timeout(
int hcnum )
84 #if defined( __GNUC__ )
85 #pragma GCC diagnostic push
86 #pragma GCC diagnostic ignored "-Warray-bounds"
91 #if defined( __GNUC__ )
92 #pragma GCC diagnostic pop
95 hcchar = USBHHAL_GetHcChar( hcnum );
100 USBHHAL_HCHalt( hcnum, hcchar );
101 hc->status |= HCS_TIMEOUT;
111 USBHHAL_HCStart( hcnum );
113 ep->epDesc.bInterval, hcTimeoutFunc[ hcnum ] );
117 ep->timeout -=
EFM32_MIN( ep->timeout, ep->epDesc.bInterval );
121 USBHHAL_HCStart( hcnum );
123 EFM32_MIN( ep->timeout, ep->epDesc.bInterval ),
124 hcTimeoutFunc[ hcnum ] );
128 USBHHAL_HCHalt( hcnum, hcchar );
129 hc->status |= HCS_TIMEOUT;
160 int USBH_AssignHostChannel( USBH_Ep_TypeDef *ep, uint8_t hcnum )
164 ep->packetSize = ep->epDesc.wMaxPacketSize;
165 ep->addr = ep->epDesc.bEndpointAddress;
166 ep->state = H_EP_IDLE;
170 DEBUG_USB_API_PUTS(
"\nUSBH_AssignHostChannel(),"
171 " ep NULL pointer" );
176 if ( hcnum >= MAX_NUM_HOSTCHANNELS )
178 DEBUG_USB_API_PUTS(
"\nUSBH_AssignHostChannel(),"
179 " Illegal host channel number" );
246 int USBH_ControlMsg( USBH_Ep_TypeDef *ep,
247 uint8_t bmRequestType,
258 DEBUG_USB_API_PUTS(
"\nUSBH_ControlMsg(), ep NULL pointer" );
263 if ( !USBH_DeviceConnected() )
265 DEBUG_USB_API_PUTS(
"\nUSBH_ControlMsg(), No device connected" );
269 if ( (uint32_t)data & 3 )
271 DEBUG_USB_API_PUTS(
"\nUSBH_ControlMsg(), Misaligned data buffer" );
277 if ( ep->state != H_EP_IDLE )
280 DEBUG_USB_API_PUTS(
"\nUSBH_ControlMsg(), Endpoint is busy" );
284 if ( !hcs[ ep->hcIn ].idle || !hcs[ ep->hcOut ].idle )
287 DEBUG_USB_API_PUTS(
"\nUSBH_ControlMsg(), Host channel is busy" );
291 ep->setup.bmRequestType = bmRequestType;
292 ep->setup.bRequest = bRequest;
293 ep->setup.wValue = wValue;
294 ep->setup.wIndex = wIndex;
295 ep->setup.wLength = wLength;
297 ep->xferCompleted =
false;
298 ep->state = H_EP_SETUP;
299 ep->xferCompleteCb = callback;
302 ep->timeout = timeout;
308 timeout, hcTimeoutFunc[ ep->hcOut ] );
311 USBH_CtlSendSetup( ep );
367 int USBH_ControlMsgB( USBH_Ep_TypeDef *ep,
368 uint8_t bmRequestType,
381 DEBUG_USB_API_PUTS(
"\nUSBH_ControlMsgB() called with int's disabled" );
386 retVal = USBH_ControlMsg( ep,
399 while ( ! ep->xferCompleted );
403 retVal = ep->xferred;
417 ep->toggle = USB_PID_DATA1;
418 ep->remaining = length;
420 hc = &hcs[ ep->hcIn ];
423 hc->remaining = length;
425 USBHHAL_HCStart( ep->hcIn );
430 #if defined( USB_RAW_API )
431 int USBH_CtlRxRaw( uint8_t pid, USBH_Ep_TypeDef *ep,
void *data,
int byteCount )
433 uint16_t packets, len;
435 uint32_t hcchar, status;
439 packets = ( byteCount + ep->packetSize - 1 ) / ep->packetSize;
442 len = packets * ep->packetSize;
446 USB->HC[ hcnum ].INTMSK = USB_HC_INT_STALL | USB_HC_INT_NAK | USB_HC_INT_ACK;
447 USB->HC[ hcnum ].INT = 0xFFFFFFFF;
449 USB->HAINTMSK |= 1 << hcnum;
450 USB->HC[ hcnum ].CHAR =
451 ( ep->parentDevice->addr << _USB_HC_CHAR_DEVADDR_SHIFT ) |
453 ( ep->type << _USB_HC_CHAR_EPTYPE_SHIFT ) |
454 ( ep->packetSize << _USB_HC_CHAR_MPS_SHIFT ) |
455 ( USB_HC_CHAR_EPDIR ) |
456 ( ep->parentDevice->speed ==
457 HPRT_L_SPEED >> _USB_HPRT_PRTSPD_SHIFT
458 ? USB_HC_CHAR_LSPDDEV : 0 );
459 USB->HC[ hcnum ].TSIZ =
460 ( ( len << _USB_HC_TSIZ_XFERSIZE_SHIFT ) &
461 _USB_HC_TSIZ_XFERSIZE_MASK ) |
462 ( ( packets << _USB_HC_TSIZ_PKTCNT_SHIFT ) &
463 _USB_HC_TSIZ_PKTCNT_MASK ) |
464 ( ( pid << _USB_HC_TSIZ_PID_SHIFT ) &
465 _USB_HC_TSIZ_PID_MASK );
466 USB->HC[ hcnum ].DMAADDR = (uint32_t)data;
467 hcchar = (
USB->HC[ hcnum ].CHAR & ~USB_HC_CHAR_CHDIS ) | USB_HC_CHAR_CHENA;
468 USB->HC[ hcnum ].CHAR = hcchar;
472 while ( ( USBHAL_GetCoreInts() & USB_GINTSTS_HCHINT ) == 0 ){}
473 if ( USBHHAL_GetHostChannelInts() & ( 1 << hcnum ) )
475 status = USBHHAL_GetHcInts( hcnum );
476 hcchar |= USB_HC_CHAR_CHDIS;
477 USB->HC[ hcnum ].CHAR = hcchar;
478 USB->HC[ hcnum ].INTMSK = 0;
479 USB->HC[ hcnum ].INT = 0xFFFFFFFF;
481 if ( status & USB_HC_INT_STALL )
483 else if ( status & USB_HC_INT_NAK )
485 else if ( status & USB_HC_INT_ACK )
501 ep->toggle = USB_PID_DATA1;
502 ep->remaining = length;
504 hc = &hcs[ ep->hcOut ];
507 hc->remaining = length;
509 USBHHAL_HCStart( ep->hcOut );
518 hc = &hcs[ ep->hcOut ];
519 hc->buf = (uint8_t*)&ep->setup;
523 hcs[ ep->hcIn ].ep = ep;
525 ep->toggle = USB_PID_SETUP;
529 USBHHAL_HCInit( ep->hcIn );
532 USBHHAL_HCInit( ep->hcOut );
534 USBHHAL_HCStart( ep->hcOut );
539 #if defined( USB_RAW_API )
540 int USBH_CtlTxRaw( uint8_t pid, USBH_Ep_TypeDef *ep,
void *data,
int byteCount )
544 uint32_t hcchar, status;
548 packets = ( byteCount + ep->packetSize - 1 ) / ep->packetSize;
554 USB->HC[ hcnum ].INTMSK = USB_HC_INT_STALL | USB_HC_INT_NAK | USB_HC_INT_ACK;
555 USB->HC[ hcnum ].INT = 0xFFFFFFFF;
557 USB->HAINTMSK |= 1 << hcnum;
558 USB->HC[ hcnum ].CHAR =
559 ( ep->parentDevice->addr << _USB_HC_CHAR_DEVADDR_SHIFT ) |
561 ( ep->type << _USB_HC_CHAR_EPTYPE_SHIFT ) |
562 ( ep->packetSize << _USB_HC_CHAR_MPS_SHIFT ) |
563 ( ep->parentDevice->speed ==
564 HPRT_L_SPEED >> _USB_HPRT_PRTSPD_SHIFT
565 ? USB_HC_CHAR_LSPDDEV : 0 );
566 USB->HC[ hcnum ].TSIZ =
567 ( ( byteCount << _USB_HC_TSIZ_XFERSIZE_SHIFT ) &
568 _USB_HC_TSIZ_XFERSIZE_MASK ) |
569 ( ( packets << _USB_HC_TSIZ_PKTCNT_SHIFT ) &
570 _USB_HC_TSIZ_PKTCNT_MASK ) |
571 ( ( pid << _USB_HC_TSIZ_PID_SHIFT ) &
572 _USB_HC_TSIZ_PID_MASK );
573 USB->HC[ hcnum ].DMAADDR = (uint32_t)data;
574 hcchar = (
USB->HC[ hcnum ].CHAR & ~USB_HC_CHAR_CHDIS ) | USB_HC_CHAR_CHENA;
575 USB->HC[ hcnum ].CHAR = hcchar;
579 while ( ( USBHAL_GetCoreInts() & USB_GINTSTS_HCHINT ) == 0 ){}
580 if ( USBHHAL_GetHostChannelInts() & ( 1 << hcnum ) )
582 status = USBHHAL_GetHcInts( hcnum );
583 hcchar |= USB_HC_CHAR_CHDIS;
584 USB->HC[ hcnum ].CHAR = hcchar;
585 USB->HC[ hcnum ].INTMSK = 0;
586 USB->HC[ hcnum ].INT = 0xFFFFFFFF;
588 if ( status & USB_HC_INT_STALL )
590 else if ( status & USB_HC_INT_NAK )
592 else if ( status & USB_HC_INT_ACK )
612 bool USBH_DeviceConnected(
void )
614 return USBH_portStatus == H_PORT_CONNECTED;
647 int USBH_GetConfigurationDescriptorB( USBH_Device_TypeDef *device,
649 uint8_t configIndex )
651 if ( device == NULL )
653 DEBUG_USB_API_PUTS(
"\nUSBH_GetConfigurationDescriptorB(),"
654 " device NULL pointer" );
661 DEBUG_USB_API_PUTS(
"\nUSBH_GetConfigurationDescriptorB(),"
662 " buf NULL pointer" );
667 return USBH_ControlMsgB( &device->ep0,
675 DEFAULT_CTRL_TIMEOUT );
704 int USBH_GetDeviceDescriptorB( USBH_Device_TypeDef *device,
707 if ( device == NULL )
709 DEBUG_USB_API_PUTS(
"\nUSBH_GetDeviceDescriptorB(),"
710 " device NULL pointer" );
717 DEBUG_USB_API_PUTS(
"\nUSBH_GetDeviceDescriptorB(),"
718 " buf NULL pointer" );
723 return USBH_ControlMsgB( &device->ep0,
731 DEFAULT_CTRL_TIMEOUT );
741 uint8_t USBH_GetPortSpeed(
void )
743 return USBHHAL_GetPortSpeed();
779 int USBH_GetStringB( USBH_Device_TypeDef *device, uint8_t *buf,
int bufLen,
780 uint8_t stringIndex, uint16_t langID )
785 if ( device == NULL )
787 DEBUG_USB_API_PUTS(
"\nUSBH_GetStringB(), device NULL pointer" );
794 DEBUG_USB_API_PUTS(
"\nUSBH_GetStringB(), buf NULL pointer" );
799 retVal = USBH_ControlMsgB(
808 DEFAULT_CTRL_TIMEOUT );
837 int USBH_Init(
const USBH_Init_TypeDef *p )
841 DEBUG_USB_API_PUTS(
"\nUSBH_Init(), init struct NULL pointer" );
846 if ( ( p->rxFifoSize < MIN_EP_FIFO_SIZE_INBYTES )
847 || ( p->nptxFifoSize < MIN_EP_FIFO_SIZE_INBYTES )
848 || ( p->ptxFifoSize < MIN_EP_FIFO_SIZE_INBYTES )
849 || ( (p->rxFifoSize + p->nptxFifoSize + p->ptxFifoSize) >
850 (MAX_HOST_FIFO_SIZE_INWORDS * 4) ) )
852 DEBUG_USB_API_PUTS(
"\nUSBH_Init(), Illegal Tx/Rx FIFO memory configuration" );
860 USBH_portStatus = H_PORT_DISCONNECTED;
864 CMU->CMD = CMU_CMD_USBCCLKSEL_HFCLKNODIV;
905 int USBH_InitDeviceData( USBH_Device_TypeDef *device,
909 uint8_t deviceSpeed )
913 if ( device == NULL )
915 DEBUG_USB_API_PUTS(
"\nUSBH_InitDeviceData(), device NULL pointer" );
920 memset( device, 0,
sizeof( USBH_Device_TypeDef ) );
924 device->numEp = numEp;
925 device->speed = deviceSpeed;
927 device->ep0.packetSize = 8;
930 memcpy( &device->ep0,
931 &((USBH_Device_TypeDef*)buf)->ep0,
sizeof( USBH_Ep_TypeDef ) );
933 device->ep0.parentDevice = device;
935 device->ep0.hcOut = 0;
936 device->ep0.hcIn = 1;
940 for ( i=0; i<numEp; i++ )
942 memset( &device->ep[i], 0,
sizeof( USBH_Ep_TypeDef ) );
943 device->ep[i].parentDevice = device;
944 device->ep[i].toggle = USB_PID_DATA0;
950 memcpy( &device->devDesc,
952 memcpy( &device->confDesc,
954 memcpy( &device->itfDesc,
959 for ( i=0; i<numEp; i++ )
961 memcpy( &device->ep[i].epDesc,
962 USBH_QGetEndpointDescriptor( buf, 0, 0, i ),
982 int USBH_PortReset(
void )
987 DEBUG_USB_API_PUTS(
"\nUSBH_PortReset() called with int's disabled" );
992 USBH_portStatus = H_PORT_CONNECTED_RESETTING;
993 USBHHAL_PortReset(
true );
997 USBHHAL_PortReset(
false );
1000 USBH_portStatus = H_PORT_DISCONNECTED;
1012 int USBH_PortResume(
void )
1017 DEBUG_USB_API_PUTS(
"\nUSBH_PortResume() called with int's disabled" );
1018 EFM_ASSERT(
false );
1022 USBHHAL_PortResume(
true );
1026 USBHHAL_PortResume(
false );
1036 void USBH_PortSuspend(
void )
1039 USBHHAL_PortSuspend();
1045 int USBH_PortVbusOn(
bool on )
1050 DEBUG_USB_API_PUTS(
"\nUSBH_PortVbusOn() called with int's disabled" );
1051 EFM_ASSERT(
false );
1055 USBHHAL_VbusOn( on );
1058 USBH_portStatus = H_PORT_DISCONNECTED;
1068 #if defined( USB_USE_PRINTF )
1085 int USBH_PrintConfigurationDescriptor(
1092 if ( config == NULL )
1094 DEBUG_USB_API_PUTS(
"\nUSBH_PrintConfigurationDescriptor(),"
1095 " config NULL pointer" );
1096 EFM_ASSERT(
false );
1100 USB_PRINTF(
"\n\nConfiguration descriptor:" );
1114 if ( remaining > 0 )
1117 c = (
char*)(config + 1);
1118 while ( remaining-- )
1144 if ( device == NULL )
1146 DEBUG_USB_API_PUTS(
"\nUSBH_PrintDeviceDescriptor(),"
1147 " device NULL pointer" );
1148 EFM_ASSERT(
false );
1184 int USBH_PrintEndpointDescriptor(
1187 if ( endpoint == NULL )
1189 DEBUG_USB_API_PUTS(
"\nUSBH_PrintEndpointDescriptor(),"
1190 " endpoint NULL pointer" );
1191 EFM_ASSERT(
false );
1219 int USBH_PrintInterfaceDescriptor(
1222 if ( interface == NULL )
1224 DEBUG_USB_API_PUTS(
"\nUSBH_PrintInterfaceDescriptor(),"
1225 " interface NULL pointer" );
1226 EFM_ASSERT(
false );
1245 #if defined( __ICCARM__ )
1246 #pragma diag_suppress=Pa039
1268 void USBH_PrintString(
const char *pre,
1281 c = (
char*)&s->
name;
1294 #if defined( __ICCARM__ )
1295 #pragma diag_default=Pa039
1332 const uint8_t *buf,
int configIndex )
1335 const uint8_t *start, *end;
1338 dev = USBH_QGetDeviceDescriptor( buf );
1341 if ( configIndex < dev->bNumConfigurations )
1344 start = buf +
sizeof( USBH_Device_TypeDef );
1349 (uint8_t*)(((USBH_Device_TypeDef*)buf)->ep));
1353 while ( start < end )
1361 if ( i == configIndex )
1393 DEBUG_USB_API_PUTS(
"\nUSBH_QGetDeviceDescriptor(), buf NULL pointer" );
1394 EFM_ASSERT(
false );
1435 const uint8_t *buf,
int configIndex,
int interfaceIndex,
int endpointIndex )
1438 uint8_t *start, *end;
1441 itf = USBH_QGetInterfaceDescriptor( buf, configIndex, interfaceIndex );
1444 if ( endpointIndex < itf->bNumEndpoints )
1450 end = (uint8_t*)USBH_QGetInterfaceDescriptor(
1451 buf, configIndex, interfaceIndex + 1 );
1455 end = (uint8_t*)((USBH_Device_TypeDef*)buf)->ep;
1460 while ( start < end )
1468 if ( i == endpointIndex )
1507 const uint8_t *buf,
int configIndex,
int interfaceIndex )
1510 uint8_t *start, *end;
1513 conf = USBH_QGetConfigurationDescriptor( buf, configIndex );
1516 if ( interfaceIndex < conf->bNumInterfaces )
1522 end = (uint8_t*)USBH_QGetConfigurationDescriptor( buf, configIndex + 1 );
1526 end = (uint8_t*)((USBH_Device_TypeDef*)buf)->ep;
1531 while ( start < end )
1539 if ( ( i == interfaceIndex ) &&
1582 int USBH_QueryDeviceB( uint8_t *buf,
size_t bufsize, uint8_t deviceSpeed )
1584 USBH_Device_TypeDef *device = (USBH_Device_TypeDef*)buf;
1586 USBH_InitDeviceData( device, NULL, NULL, 0, deviceSpeed );
1588 device->speed = deviceSpeed;
1589 device->ep = (USBH_Ep_TypeDef*)(buf+bufsize);
1592 if ( 8 != USBH_GetDeviceDescriptorB( device,
1598 device->ep0.packetSize = device->devDesc.bMaxPacketSize0;
1603 buf +
sizeof( USBH_Device_TypeDef ),
1608 memcpy( &device->devDesc,
1609 buf +
sizeof( USBH_Device_TypeDef ), USB_DEVICE_DESCSIZE );
1614 buf +
sizeof( USBH_Device_TypeDef ),
1616 bufsize - (
size_t)USB_DEVICE_DESCSIZE ),
1621 memcpy( &device->confDesc,
1622 buf +
sizeof( USBH_Device_TypeDef ), USB_CONFIG_DESCSIZE );
1627 USBH_GetConfigurationDescriptorB(
1629 buf +
sizeof( USBH_Device_TypeDef ),
1630 EFM32_MIN( device->confDesc.wTotalLength,
1631 bufsize -
sizeof( USBH_Device_TypeDef )),
1672 int USBH_Read( USBH_Ep_TypeDef *ep,
void *data,
int byteCount,
int timeout,
1675 USBH_Hc_TypeDef *hc = &hcs[ ep->hcIn ];
1679 DEBUG_USB_API_PUTS(
"\nUSBH_Read(), ep NULL pointer" );
1680 EFM_ASSERT(
false );
1684 if ( !USBH_DeviceConnected() )
1686 DEBUG_USB_API_PUTS(
"\nUSBH_Read(), No device connected" );
1690 if ( ( byteCount > MAX_XFER_LEN ) ||
1691 ( ( byteCount / ep->packetSize ) > MAX_PACKETS_PR_XFER ) )
1693 DEBUG_USB_API_PUTS(
"\nUSBH_Read(), Illegal transfer size" );
1694 EFM_ASSERT(
false );
1698 if ( (uint32_t)data & 3 )
1700 DEBUG_USB_API_PUTS(
"\nUSBH_Read(), Misaligned data buffer" );
1701 EFM_ASSERT(
false );
1706 if ( ep->state != H_EP_IDLE )
1709 DEBUG_USB_API_PUTS(
"\nUSBH_Read(), Endpoint is busy" );
1716 DEBUG_USB_API_PUTS(
"\nUSBH_Read(), Illegal EP direction" );
1717 EFM_ASSERT(
false );
1724 DEBUG_USB_API_PUTS(
"\nUSBH_Read(), Host channel is busy" );
1728 ep->xferCompleted =
false;
1729 ep->state = H_EP_DATA_IN;
1730 ep->xferCompleteCb = callback;
1732 ep->remaining = byteCount;
1734 ep->timeout = timeout;
1738 hc->remaining = byteCount;
1744 timeout =
EFM32_MIN( timeout, ep->epDesc.bInterval );
1746 timeout = ep->epDesc.bInterval;
1752 timeout, hcTimeoutFunc[ ep->hcIn ] );
1755 USBHHAL_HCInit( ep->hcIn );
1756 USBHHAL_HCStart( ep->hcIn );
1794 int USBH_ReadB( USBH_Ep_TypeDef *ep,
void *data,
int byteCount,
int timeout )
1801 DEBUG_USB_API_PUTS(
"\nUSBH_ReadB() called with int's disabled" );
1802 EFM_ASSERT(
false );
1806 retVal = USBH_Read( ep, data, byteCount, timeout, NULL );
1811 while ( ! ep->xferCompleted );
1815 retVal = ep->xferred;
1841 int USBH_SetAddressB( USBH_Device_TypeDef *device, uint8_t deviceAddress )
1845 if ( device == NULL )
1847 DEBUG_USB_API_PUTS(
"\nUSBH_SetAddressB(), device NULL pointer" );
1848 EFM_ASSERT(
false );
1854 DEBUG_USB_API_PUTS(
"\nUSBH_SetAddressB(), Illegal device address" );
1855 EFM_ASSERT(
false );
1859 retVal = USBH_ControlMsgB(
1868 DEFAULT_CTRL_TIMEOUT );
1872 device->addr = deviceAddress;
1897 int USBH_SetAltInterfaceB( USBH_Device_TypeDef *device,
1898 uint8_t interfaceIndex, uint8_t alternateSetting )
1902 if ( device == NULL )
1904 DEBUG_USB_API_PUTS(
"\nUSBH_SetAltInterfaceB(), device NULL pointer" );
1905 EFM_ASSERT(
false );
1909 retVal = USBH_ControlMsgB(
1918 DEFAULT_CTRL_TIMEOUT );
1928 for ( i=0; i<device->numEp; i++ )
1930 device->ep[i].toggle = USB_PID_DATA0;
1956 int USBH_SetConfigurationB( USBH_Device_TypeDef *device, uint8_t configValue )
1960 if ( device == NULL )
1962 DEBUG_USB_API_PUTS(
"\nUSBH_SetConfigurationB(), device NULL pointer" );
1963 EFM_ASSERT(
false );
1967 retVal = USBH_ControlMsgB(
1976 DEFAULT_CTRL_TIMEOUT );
1986 for ( i=0; i<device->numEp; i++ )
1988 device->ep[i].toggle = USB_PID_DATA0;
2009 int USBH_StallEpB( USBH_Ep_TypeDef *ep )
2013 DEBUG_USB_API_PUTS(
"\nUSBH_StallEpB(), ep NULL pointer" );
2014 EFM_ASSERT(
false );
2018 return USBH_ControlMsgB( &ep->parentDevice->ep0,
2026 DEFAULT_CTRL_TIMEOUT );
2036 void USBH_Stop(
void )
2041 for ( i = 0; i < NUM_HC_USED + 2; i++ )
2046 USBHAL_DisableGlobalInt();
2048 USBH_PortVbusOn(
false );
2049 USBHAL_DisablePhyPins();
2068 int USBH_UnStallEpB( USBH_Ep_TypeDef *ep )
2074 DEBUG_USB_API_PUTS(
"\nUSBH_UnStallEpB(), ep NULL pointer" );
2075 EFM_ASSERT(
false );
2079 retVal = USBH_ControlMsgB( &ep->parentDevice->ep0,
2087 DEFAULT_CTRL_TIMEOUT );
2091 ep->toggle = USB_PID_DATA0;
2099 static void InitUsb(
void )
2103 memset( hcs, 0,
sizeof( hcs ) );
2104 for ( i = 0; i < NUM_HC_USED + 2; i++ )
2106 hcs[ i ].idle =
true;
2110 USBHHAL_CoreInit( USBH_initData.rxFifoSize, USBH_initData.nptxFifoSize,
2111 USBH_initData.ptxFifoSize );
2146 int USBH_WaitForDeviceConnectionB( uint8_t *buf,
int timeoutInSeconds )
2149 USBH_Device_TypeDef *device;
2150 int accumulatedTime, deadLine;
2154 DEBUG_USB_API_PUTS(
"\nUSBH_WaitForDeviceConnectionB(),"
2155 " buf NULL pointer" );
2156 EFM_ASSERT(
false );
2161 accumulatedTime = 0;
2162 USBH_attachRetryCount = 0;
2163 deadLine = timeoutInSeconds * 1000;
2164 device = (USBH_Device_TypeDef*)buf;
2169 if ( USBHHAL_InitializedAndPowered() )
2171 USBH_PortVbusOn(
false );
2173 accumulatedTime += PORT_VBUS_DELAY;
2179 accumulatedTime += 50;
2182 USBH_PortVbusOn(
true );
2183 DEBUG_USB_INT_LO_PUTCHAR(
'1' );
2185 accumulatedTime += PORT_VBUS_DELAY;
2189 USBHHAL_EnableInts();
2190 USBHAL_EnableGlobalInt();
2196 if ( USBH_DeviceConnected() )
2199 if ( ( deadLine ) &&
2200 ( accumulatedTime >= deadLine ) )
2205 #if ( USB_VBUSOVRCUR_PORT != USB_VBUSOVRCUR_PORT_NONE )
2206 if (
GPIO_PinInGet( USB_VBUSOVRCUR_PORT, USB_VBUSOVRCUR_PIN ) ==
2207 USB_VBUSOVRCUR_POLARITY )
2209 DEBUG_USB_INT_LO_PUTCHAR(
'~' );
2210 USBHHAL_PortReset(
false );
2211 USBHHAL_VbusOn(
false );
2213 USBH_portStatus = H_PORT_OVERCURRENT;
2219 accumulatedTime += 50;
2226 accumulatedTime += 100;
2230 USBH_InitDeviceData( device, NULL, NULL, 0, USBH_GetPortSpeed() );
2233 if ( 8 == USBH_GetDeviceDescriptorB( device, &device->devDesc, 8 ) )
2241 accumulatedTime += DEFAULT_CTRL_TIMEOUT;
2250 accumulatedTime += PORT_VBUS_DELAY;
2252 if ( ( deadLine ) &&
2253 ( accumulatedTime >= deadLine ) )
2256 USBH_attachRetryCount = ( USBH_attachRetryCount + 1 ) %
2257 (
sizeof( USBH_attachTiming ) /
2258 sizeof( USBH_AttachTiming_TypeDef ) );
2297 int USBH_Write( USBH_Ep_TypeDef *ep,
void *data,
int byteCount,
2300 USBH_Hc_TypeDef *hc = &hcs[ ep->hcOut ];
2304 DEBUG_USB_API_PUTS(
"\nUSBH_Write(), ep NULL pointer" );
2305 EFM_ASSERT(
false );
2309 if ( !USBH_DeviceConnected() )
2311 DEBUG_USB_API_PUTS(
"\nUSBH_Write(), No device connected" );
2315 if ( ( byteCount > MAX_XFER_LEN ) ||
2316 ( ( byteCount / ep->packetSize ) > MAX_PACKETS_PR_XFER ) )
2318 DEBUG_USB_API_PUTS(
"\nUSBH_Write(), Illegal transfer size" );
2319 EFM_ASSERT(
false );
2323 if ( (uint32_t)data & 3 )
2325 DEBUG_USB_API_PUTS(
"\nUSBH_Write(), Misaligned data buffer" );
2326 EFM_ASSERT(
false );
2331 if ( ep->state != H_EP_IDLE )
2334 DEBUG_USB_API_PUTS(
"\nUSBH_Write(), Endpoint is busy" );
2341 DEBUG_USB_API_PUTS(
"\nUSBH_Write(), Illegal EP direction" );
2342 EFM_ASSERT(
false );
2349 DEBUG_USB_API_PUTS(
"\nUSBH_Write(), Host channel is busy" );
2353 ep->xferCompleted =
false;
2354 ep->state = H_EP_DATA_OUT;
2355 ep->xferCompleteCb = callback;
2357 ep->remaining = byteCount;
2359 ep->timeout = timeout;
2363 hc->remaining = byteCount;
2369 timeout =
EFM32_MIN( timeout, ep->epDesc.bInterval );
2371 timeout = ep->epDesc.bInterval;
2377 timeout, hcTimeoutFunc[ ep->hcOut ] );
2380 USBHHAL_HCInit( ep->hcOut );
2381 USBHHAL_HCStart( ep->hcOut );
2417 int USBH_WriteB( USBH_Ep_TypeDef *ep,
void *data,
int byteCount,
int timeout )
2424 DEBUG_USB_API_PUTS(
"\nUSBH_WriteB() called with int's disabled" );
2425 EFM_ASSERT(
false );
2429 retVal = USBH_Write( ep, data, byteCount, timeout, NULL );
2434 while ( ! ep->xferCompleted );
2438 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
uint8_t bInterfaceProtocol
#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.
__STATIC_INLINE uint32_t INT_Enable(void)
Enable interrupts.
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 USB_DEVICE_DESCRIPTOR
#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
#define USB_SETUP_RECIPIENT_INTERFACE
#define USB_DEVICE_DESCSIZE
uint8_t bNumConfigurations
#define CMU_HFCORECLKEN0_USBC
#define USB_SETUP_TYPE_STANDARD_MASK
#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.
USB Configuration Descriptor.
#define USB_CONFIG_DESCRIPTOR
__STATIC_INLINE uint32_t INT_Disable(void)
Disable interrupts.
#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