3      Copyright (C) Dean Camera, 2009. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, and distribute this software 
  13   and its documentation for any purpose and without fee is hereby 
  14   granted, provided that the above copyright notice appear in all 
  15   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 #include "../HighLevel/USBMode.h" 
  33 #if defined(USB_CAN_BE_HOST) 
  35 #define  INCLUDE_FROM_HOST_C 
  38 void USB_Host_ProcessNextHostState(void) 
  40         uint8_t ErrorCode    
= HOST_ENUMERROR_NoError
; 
  41         uint8_t SubErrorCode 
= HOST_ENUMERROR_NoError
; 
  43         static uint16_t WaitMSRemaining
; 
  44         static uint8_t  PostWaitState
; 
  46         switch (USB_HostState
) 
  48                 case HOST_STATE_WaitForDevice
: 
  51                                 if ((SubErrorCode 
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
) 
  53                                         USB_HostState 
= PostWaitState
; 
  54                                         ErrorCode     
= HOST_ENUMERROR_WaitStage
; 
  58                                 if (!(--WaitMSRemaining
)) 
  59                                   USB_HostState 
= PostWaitState
; 
  63                 case HOST_STATE_Powered
: 
  64                         WaitMSRemaining 
= HOST_DEVICE_SETTLE_DELAY_MS
; 
  66                         USB_HostState 
= HOST_STATE_Powered_WaitForDeviceSettle
; 
  68                 case HOST_STATE_Powered_WaitForDeviceSettle
: 
  69                         #if HOST_DEVICE_SETTLE_DELAY_MS > 0 
  72                         if (!(WaitMSRemaining
--)) 
  74                                 USB_Host_VBUS_Manual_Off(); 
  77                                 USB_Host_VBUS_Auto_Enable(); 
  78                                 USB_Host_VBUS_Auto_On(); 
  80                                 USB_HostState 
= HOST_STATE_Powered_WaitForConnect
; 
  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
); 
 101                                 HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Powered_DoReset
); 
 105                 case HOST_STATE_Powered_DoReset
: 
 106                         USB_Host_ResetDevice(); 
 108                         HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Powered_ConfigPipe
); 
 110                 case HOST_STATE_Powered_ConfigPipe
: 
 111                         Pipe_ConfigurePipe(PIPE_CONTROLPIPE
, EP_TYPE_CONTROL
, 
 112                                                            PIPE_TOKEN_SETUP
, ENDPOINT_CONTROLEP
, 
 113                                                            PIPE_CONTROLPIPE_DEFAULT_SIZE
, PIPE_BANK_SINGLE
);             
 115                         if (!(Pipe_IsConfigured())) 
 117                                 ErrorCode    
= HOST_ENUMERROR_PipeConfigError
; 
 122                         USB_HostState 
= HOST_STATE_Default
; 
 124                 case HOST_STATE_Default
: 
 125                         USB_ControlRequest 
= (USB_Request_Header_t
) 
 127                                         .bmRequestType 
= (REQDIR_DEVICETOHOST 
| REQTYPE_STANDARD 
| REQREC_DEVICE
), 
 128                                         .bRequest      
= REQ_GetDescriptor
, 
 129                                         .wValue        
= (DTYPE_Device 
<< 8), 
 134                         uint8_t DataBuffer
[8]; 
 136                         if ((SubErrorCode 
= USB_Host_SendControlRequest(DataBuffer
)) != HOST_SENDCONTROL_Successful
) 
 138                                 ErrorCode 
= HOST_ENUMERROR_ControlError
; 
 142                         #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) 
 143                         USB_ControlPipeSize 
= DataBuffer
[offsetof(USB_Descriptor_Device_t
, Endpoint0Size
)]; 
 145                         USB_ControlPipeSize 
= DataBuffer
[offsetof(USB_Descriptor_Device_t
, bMaxPacketSize0
)];                    
 148                         USB_Host_ResetDevice(); 
 150                         HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Default_PostReset
); 
 152                 case HOST_STATE_Default_PostReset
: 
 154                         Pipe_DeallocateMemory();                 
 155                         Pipe_ResetPipe(PIPE_CONTROLPIPE
); 
 157                         Pipe_ConfigurePipe(PIPE_CONTROLPIPE
, EP_TYPE_CONTROL
, 
 158                                            PIPE_TOKEN_SETUP
, ENDPOINT_CONTROLEP
, 
 159                                            USB_ControlPipeSize
, PIPE_BANK_SINGLE
); 
 161                         if (!(Pipe_IsConfigured())) 
 163                                 ErrorCode    
= HOST_ENUMERROR_PipeConfigError
; 
 168                         USB_ControlRequest 
= (USB_Request_Header_t
) 
 170                                         .bmRequestType 
= (REQDIR_HOSTTODEVICE 
| REQTYPE_STANDARD 
| REQREC_DEVICE
), 
 171                                         .bRequest      
= REQ_SetAddress
, 
 172                                         .wValue        
= USB_HOST_DEVICEADDRESS
, 
 177                         if ((SubErrorCode 
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
) 
 179                                 ErrorCode 
= HOST_ENUMERROR_ControlError
; 
 183                         HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Default_PostAddressSet
); 
 185                 case HOST_STATE_Default_PostAddressSet
: 
 186                         USB_Host_SetDeviceAddress(USB_HOST_DEVICEADDRESS
); 
 188                         EVENT_USB_DeviceEnumerationComplete(); 
 189                         USB_HostState 
= HOST_STATE_Addressed
; 
 194         if ((ErrorCode 
!= HOST_ENUMERROR_NoError
) && (USB_HostState 
!= HOST_STATE_Unattached
)) 
 196                 EVENT_USB_DeviceEnumerationFailed(ErrorCode
, SubErrorCode
); 
 198                 USB_Host_VBUS_Auto_Off(); 
 200                 EVENT_USB_DeviceUnattached(); 
 201                 EVENT_USB_Disconnect(); 
 203                 USB_ResetInterface(); 
 207 uint8_t USB_Host_WaitMS(uint8_t MS
) 
 209         bool    BusSuspended 
= USB_Host_IsBusSuspended(); 
 210         uint8_t ErrorCode    
= HOST_WAITERROR_Successful
; 
 212         USB_Host_ResumeBus(); 
 216                 if (USB_INT_HasOccurred(USB_INT_HSOFI
)) 
 218                         USB_INT_Clear(USB_INT_HSOFI
); 
 222                 if ((USB_HostState 
== HOST_STATE_Unattached
) || (USB_CurrentMode 
== USB_MODE_DEVICE
)) 
 224                         ErrorCode 
= HOST_WAITERROR_DeviceDisconnect
; 
 229                 if (Pipe_IsError() == true) 
 232                         ErrorCode 
= HOST_WAITERROR_PipeError
; 
 237                 if (Pipe_IsStalled() == true) 
 240                         ErrorCode 
= HOST_WAITERROR_SetupStalled
; 
 247           USB_Host_SuspendBus(); 
 252 static void USB_Host_ResetDevice(void) 
 254         bool BusSuspended 
= USB_Host_IsBusSuspended(); 
 256         USB_INT_Disable(USB_INT_DDISCI
); 
 259         while (!(USB_Host_IsBusResetComplete())); 
 261         USB_Host_ResumeBus(); 
 263         USB_INT_Clear(USB_INT_HSOFI
); 
 265         for (uint8_t MSRem 
= 10; MSRem 
!= 0; MSRem
--) 
 267                 /* Workaround for powerless-pull-up devices. After a USB bus reset, 
 268                    all disconnection interrupts are suppressed while a USB frame is 
 269                    looked for - if it is found within 10ms, the device is still 
 272                 if (USB_INT_HasOccurred(USB_INT_HSOFI
)) 
 274                         USB_INT_Clear(USB_INT_HSOFI
); 
 275                         USB_INT_Clear(USB_INT_DDISCI
); 
 283           USB_Host_SuspendBus(); 
 285         USB_INT_Enable(USB_INT_DDISCI
); 
 288 uint8_t USB_Host_SetDeviceConfiguration(const uint8_t ConfigNumber
) 
 290         USB_ControlRequest 
= (USB_Request_Header_t
) 
 292                         .bmRequestType 
= (REQDIR_HOSTTODEVICE 
| REQTYPE_STANDARD 
| REQREC_DEVICE
), 
 293                         .bRequest      
= REQ_SetConfiguration
, 
 294                         .wValue        
= ConfigNumber
, 
 299         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 301         return USB_Host_SendControlRequest(NULL
); 
 304 uint8_t USB_Host_GetDeviceDescriptor(void* const DeviceDescriptorPtr
) 
 306         USB_ControlRequest 
= (USB_Request_Header_t
) 
 308                         bmRequestType
: (REQDIR_DEVICETOHOST 
| REQTYPE_STANDARD 
| REQREC_DEVICE
), 
 309                         bRequest
:      REQ_GetDescriptor
, 
 310                         wValue
:        (DTYPE_Device 
<< 8), 
 312                         wLength
:       sizeof(USB_Descriptor_Device_t
), 
 315         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 317         return USB_Host_SendControlRequest(DeviceDescriptorPtr
); 
 320 uint8_t USB_Host_ClearPipeStall(uint8_t EndpointNum
) 
 322         if (Pipe_GetPipeToken() == PIPE_TOKEN_IN
) 
 323           EndpointNum 
|= (1 << 7); 
 325         USB_ControlRequest 
= (USB_Request_Header_t
) 
 327                         .bmRequestType 
= (REQDIR_HOSTTODEVICE 
| REQTYPE_STANDARD 
| REQREC_ENDPOINT
), 
 328                         .bRequest      
= REQ_ClearFeature
, 
 329                         .wValue        
= FEATURE_ENDPOINT_HALT
, 
 330                         .wIndex        
= EndpointNum
, 
 334         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 336         return USB_Host_SendControlRequest(NULL
);