3      Copyright (C) Dean Camera, 2010. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, distribute, and sell this  
  13   software and its documentation for any purpose is hereby granted 
  14   without fee, provided that the above copyright notice appear in  
  15   all copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting  
  17   documentation, and that the name of the author not be used in  
  18   advertising or publicity pertaining to distribution of the  
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  31 #define  __INCLUDE_FROM_USB_DRIVER 
  32 #include "../HighLevel/USBMode.h" 
  34 #if defined(USB_CAN_BE_HOST) 
  36 #define  __INCLUDE_FROM_HOST_C 
  39 void USB_Host_ProcessNextHostState(void) 
  41         uint8_t ErrorCode    
= HOST_ENUMERROR_NoError
; 
  42         uint8_t SubErrorCode 
= HOST_ENUMERROR_NoError
; 
  44         static uint16_t WaitMSRemaining
; 
  45         static uint8_t  PostWaitState
; 
  47         switch (USB_HostState
) 
  49                 case HOST_STATE_WaitForDevice
: 
  52                                 if ((SubErrorCode 
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
) 
  54                                         USB_HostState 
= PostWaitState
; 
  55                                         ErrorCode     
= HOST_ENUMERROR_WaitStage
; 
  59                                 if (!(--WaitMSRemaining
)) 
  60                                   USB_HostState 
= PostWaitState
; 
  64                 case HOST_STATE_Powered
: 
  65                         WaitMSRemaining 
= HOST_DEVICE_SETTLE_DELAY_MS
; 
  67                         USB_HostState 
= HOST_STATE_Powered_WaitForDeviceSettle
; 
  69                 case HOST_STATE_Powered_WaitForDeviceSettle
: 
  70                         if (WaitMSRemaining
--) 
  77                                 USB_Host_VBUS_Manual_Off(); 
  80                                 USB_Host_VBUS_Auto_Enable(); 
  81                                 USB_Host_VBUS_Auto_On(); 
  83                                 USB_HostState 
= HOST_STATE_Powered_WaitForConnect
; 
  87                 case HOST_STATE_Powered_WaitForConnect
:          
  88                         if (USB_INT_HasOccurred(USB_INT_DCONNI
)) 
  90                                 USB_INT_Clear(USB_INT_DCONNI
); 
  91                                 USB_INT_Clear(USB_INT_DDISCI
); 
  93                                 USB_INT_Clear(USB_INT_VBERRI
); 
  94                                 USB_INT_Enable(USB_INT_VBERRI
); 
  99                                 HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Powered_DoReset
); 
 103                 case HOST_STATE_Powered_DoReset
: 
 104                         USB_Host_ResetDevice(); 
 106                         HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Powered_ConfigPipe
); 
 108                 case HOST_STATE_Powered_ConfigPipe
: 
 109                         Pipe_ConfigurePipe(PIPE_CONTROLPIPE
, EP_TYPE_CONTROL
, 
 110                                                            PIPE_TOKEN_SETUP
, ENDPOINT_CONTROLEP
, 
 111                                                            PIPE_CONTROLPIPE_DEFAULT_SIZE
, PIPE_BANK_SINGLE
);             
 113                         if (!(Pipe_IsConfigured())) 
 115                                 ErrorCode    
= HOST_ENUMERROR_PipeConfigError
; 
 120                         USB_HostState 
= HOST_STATE_Default
; 
 122                 case HOST_STATE_Default
: 
 123                         USB_ControlRequest 
= (USB_Request_Header_t
) 
 125                                         .bmRequestType 
= (REQDIR_DEVICETOHOST 
| REQTYPE_STANDARD 
| REQREC_DEVICE
), 
 126                                         .bRequest      
= REQ_GetDescriptor
, 
 127                                         .wValue        
= (DTYPE_Device 
<< 8), 
 132                         uint8_t DataBuffer
[8]; 
 134                         if ((SubErrorCode 
= USB_Host_SendControlRequest(DataBuffer
)) != HOST_SENDCONTROL_Successful
) 
 136                                 ErrorCode 
= HOST_ENUMERROR_ControlError
; 
 140                         USB_ControlPipeSize 
= DataBuffer
[offsetof(USB_Descriptor_Device_t
, Endpoint0Size
)]; 
 142                         USB_Host_ResetDevice(); 
 144                         HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Default_PostReset
); 
 146                 case HOST_STATE_Default_PostReset
: 
 148                         Pipe_DeallocateMemory();                 
 149                         Pipe_ResetPipe(PIPE_CONTROLPIPE
); 
 151                         Pipe_ConfigurePipe(PIPE_CONTROLPIPE
, EP_TYPE_CONTROL
, 
 152                                            PIPE_TOKEN_SETUP
, ENDPOINT_CONTROLEP
, 
 153                                            USB_ControlPipeSize
, PIPE_BANK_SINGLE
); 
 155                         if (!(Pipe_IsConfigured())) 
 157                                 ErrorCode    
= HOST_ENUMERROR_PipeConfigError
; 
 162                         USB_ControlRequest 
= (USB_Request_Header_t
) 
 164                                         .bmRequestType 
= (REQDIR_HOSTTODEVICE 
| REQTYPE_STANDARD 
| REQREC_DEVICE
), 
 165                                         .bRequest      
= REQ_SetAddress
, 
 166                                         .wValue        
= USB_HOST_DEVICEADDRESS
, 
 171                         if ((SubErrorCode 
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
) 
 173                                 ErrorCode 
= HOST_ENUMERROR_ControlError
; 
 177                         HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Default_PostAddressSet
); 
 179                 case HOST_STATE_Default_PostAddressSet
: 
 180                         USB_Host_SetDeviceAddress(USB_HOST_DEVICEADDRESS
); 
 182                         EVENT_USB_Host_DeviceEnumerationComplete(); 
 183                         USB_HostState 
= HOST_STATE_Addressed
; 
 187         if ((ErrorCode 
!= HOST_ENUMERROR_NoError
) && (USB_HostState 
!= HOST_STATE_Unattached
)) 
 189                 EVENT_USB_Host_DeviceEnumerationFailed(ErrorCode
, SubErrorCode
); 
 191                 USB_Host_VBUS_Auto_Off(); 
 193                 EVENT_USB_Host_DeviceUnattached(); 
 195                 USB_ResetInterface(); 
 199 uint8_t USB_Host_WaitMS(uint8_t MS
) 
 201         bool    BusSuspended 
= USB_Host_IsBusSuspended(); 
 202         uint8_t ErrorCode    
= HOST_WAITERROR_Successful
; 
 204         USB_Host_ResumeBus(); 
 208                 if (USB_INT_HasOccurred(USB_INT_HSOFI
)) 
 210                         USB_INT_Clear(USB_INT_HSOFI
); 
 214                 if ((USB_HostState 
== HOST_STATE_Unattached
) || (USB_CurrentMode 
== USB_MODE_DEVICE
)) 
 216                         ErrorCode 
= HOST_WAITERROR_DeviceDisconnect
; 
 221                 if (Pipe_IsError() == true) 
 224                         ErrorCode 
= HOST_WAITERROR_PipeError
; 
 229                 if (Pipe_IsStalled() == true) 
 232                         ErrorCode 
= HOST_WAITERROR_SetupStalled
; 
 239           USB_Host_SuspendBus(); 
 244 static void USB_Host_ResetDevice(void) 
 246         bool BusSuspended 
= USB_Host_IsBusSuspended(); 
 248         USB_INT_Disable(USB_INT_DDISCI
); 
 251         while (!(USB_Host_IsBusResetComplete())); 
 253         USB_Host_ResumeBus(); 
 255         USB_INT_Clear(USB_INT_HSOFI
); 
 257         for (uint8_t MSRem 
= 10; MSRem 
!= 0; MSRem
--) 
 259                 /* Workaround for powerless-pull-up devices. After a USB bus reset, 
 260                    all disconnection interrupts are suppressed while a USB frame is 
 261                    looked for - if it is found within 10ms, the device is still 
 264                 if (USB_INT_HasOccurred(USB_INT_HSOFI
)) 
 266                         USB_INT_Clear(USB_INT_HSOFI
); 
 267                         USB_INT_Clear(USB_INT_DDISCI
); 
 275           USB_Host_SuspendBus(); 
 277         USB_INT_Enable(USB_INT_DDISCI
); 
 280 uint8_t USB_Host_SetDeviceConfiguration(const uint8_t ConfigNumber
) 
 282         USB_ControlRequest 
= (USB_Request_Header_t
) 
 284                         .bmRequestType 
= (REQDIR_HOSTTODEVICE 
| REQTYPE_STANDARD 
| REQREC_DEVICE
), 
 285                         .bRequest      
= REQ_SetConfiguration
, 
 286                         .wValue        
= ConfigNumber
, 
 291         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 293         return USB_Host_SendControlRequest(NULL
); 
 296 uint8_t USB_Host_GetDeviceDescriptor(void* const DeviceDescriptorPtr
) 
 298         USB_ControlRequest 
= (USB_Request_Header_t
) 
 300                         .bmRequestType 
= (REQDIR_DEVICETOHOST 
| REQTYPE_STANDARD 
| REQREC_DEVICE
), 
 301                         .bRequest      
= REQ_GetDescriptor
, 
 302                         .wValue        
= (DTYPE_Device 
<< 8), 
 304                         .wLength       
= sizeof(USB_Descriptor_Device_t
), 
 307         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 309         return USB_Host_SendControlRequest(DeviceDescriptorPtr
); 
 312 uint8_t USB_Host_GetDeviceStringDescriptor(uint8_t Index
, void* const Buffer
, uint8_t BufferLength
) 
 314         USB_ControlRequest 
= (USB_Request_Header_t
) 
 316                         .bmRequestType 
= (REQDIR_DEVICETOHOST 
| REQTYPE_STANDARD 
| REQREC_DEVICE
), 
 317                         .bRequest      
= REQ_GetDescriptor
, 
 318                         .wValue        
= (DTYPE_String 
<< 8) | Index
, 
 320                         .wLength       
= BufferLength
, 
 323         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 325         return USB_Host_SendControlRequest(Buffer
); 
 328 uint8_t USB_Host_ClearPipeStall(uint8_t EndpointNum
) 
 330         if (Pipe_GetPipeToken() == PIPE_TOKEN_IN
) 
 331           EndpointNum 
|= ENDPOINT_DESCRIPTOR_DIR_IN
; 
 333         USB_ControlRequest 
= (USB_Request_Header_t
) 
 335                         .bmRequestType 
= (REQDIR_HOSTTODEVICE 
| REQTYPE_STANDARD 
| REQREC_ENDPOINT
), 
 336                         .bRequest      
= REQ_ClearFeature
, 
 337                         .wValue        
= FEATURE_ENDPOINT_HALT
, 
 338                         .wIndex        
= EndpointNum
, 
 342         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 344         return USB_Host_SendControlRequest(NULL
);