3 Copyright (C) Dean Camera, 2012.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2012 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 "../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 #if defined(NO_AUTO_VBUS_MANAGEMENT)
84 USB_Host_VBUS_Manual_Enable();
85 USB_Host_VBUS_Manual_On();
88 USB_HostState
= HOST_STATE_Powered_WaitForConnect
;
92 case HOST_STATE_Powered_WaitForConnect
:
93 if (USB_INT_HasOccurred(USB_INT_DCONNI
))
95 USB_INT_Clear(USB_INT_DCONNI
);
96 USB_INT_Clear(USB_INT_DDISCI
);
98 USB_INT_Clear(USB_INT_VBERRI
);
99 USB_INT_Enable(USB_INT_VBERRI
);
101 USB_Host_ResumeBus();
104 HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Powered_DoReset
);
108 case HOST_STATE_Powered_DoReset
:
109 USB_Host_ResetDevice();
111 HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Powered_ConfigPipe
);
113 case HOST_STATE_Powered_ConfigPipe
:
114 if (!(Pipe_ConfigurePipe(PIPE_CONTROLPIPE
, EP_TYPE_CONTROL
,
115 PIPE_TOKEN_SETUP
, ENDPOINT_CONTROLEP
,
116 PIPE_CONTROLPIPE_DEFAULT_SIZE
, PIPE_BANK_SINGLE
)))
118 ErrorCode
= HOST_ENUMERROR_PipeConfigError
;
123 USB_HostState
= HOST_STATE_Default
;
125 case HOST_STATE_Default
:
126 USB_ControlRequest
= (USB_Request_Header_t
)
128 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_DEVICE
),
129 .bRequest
= REQ_GetDescriptor
,
130 .wValue
= (DTYPE_Device
<< 8),
135 uint8_t DataBuffer
[8];
137 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
138 if ((SubErrorCode
= USB_Host_SendControlRequest(DataBuffer
)) != HOST_SENDCONTROL_Successful
)
140 ErrorCode
= HOST_ENUMERROR_ControlError
;
144 USB_Host_ControlPipeSize
= DataBuffer
[offsetof(USB_Descriptor_Device_t
, Endpoint0Size
)];
146 USB_Host_ResetDevice();
148 HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Default_PostReset
);
150 case HOST_STATE_Default_PostReset
:
151 if (!(Pipe_ConfigurePipe(PIPE_CONTROLPIPE
, EP_TYPE_CONTROL
,
152 PIPE_TOKEN_SETUP
, ENDPOINT_CONTROLEP
,
153 USB_Host_ControlPipeSize
, PIPE_BANK_SINGLE
)))
155 ErrorCode
= HOST_ENUMERROR_PipeConfigError
;
160 USB_ControlRequest
= (USB_Request_Header_t
)
162 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_DEVICE
),
163 .bRequest
= REQ_SetAddress
,
164 .wValue
= USB_HOST_DEVICEADDRESS
,
169 if ((SubErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
171 ErrorCode
= HOST_ENUMERROR_ControlError
;
175 HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Default_PostAddressSet
);
177 case HOST_STATE_Default_PostAddressSet
:
178 USB_Host_SetDeviceAddress(USB_HOST_DEVICEADDRESS
);
180 USB_HostState
= HOST_STATE_Addressed
;
182 EVENT_USB_Host_DeviceEnumerationComplete();
186 if ((ErrorCode
!= HOST_ENUMERROR_NoError
) && (USB_HostState
!= HOST_STATE_Unattached
))
188 EVENT_USB_Host_DeviceEnumerationFailed(ErrorCode
, SubErrorCode
);
190 USB_Host_VBUS_Auto_Off();
192 EVENT_USB_Host_DeviceUnattached();
194 USB_ResetInterface();
198 uint8_t USB_Host_WaitMS(uint8_t MS
)
200 bool BusSuspended
= USB_Host_IsBusSuspended();
201 uint8_t ErrorCode
= HOST_WAITERROR_Successful
;
202 bool HSOFIEnabled
= USB_INT_IsEnabled(USB_INT_HSOFI
);
204 USB_INT_Disable(USB_INT_HSOFI
);
205 USB_INT_Clear(USB_INT_HSOFI
);
207 USB_Host_ResumeBus();
211 if (USB_INT_HasOccurred(USB_INT_HSOFI
))
213 USB_INT_Clear(USB_INT_HSOFI
);
217 if ((USB_HostState
== HOST_STATE_Unattached
) || (USB_CurrentMode
!= USB_MODE_Host
))
219 ErrorCode
= HOST_WAITERROR_DeviceDisconnect
;
227 ErrorCode
= HOST_WAITERROR_PipeError
;
232 if (Pipe_IsStalled())
235 ErrorCode
= HOST_WAITERROR_SetupStalled
;
242 USB_Host_SuspendBus();
245 USB_INT_Enable(USB_INT_HSOFI
);
250 static void USB_Host_ResetDevice(void)
252 bool BusSuspended
= USB_Host_IsBusSuspended();
254 USB_INT_Disable(USB_INT_DDISCI
);
257 while (!(USB_Host_IsBusResetComplete()));
258 USB_Host_ResumeBus();
260 USB_Host_ConfigurationNumber
= 0;
262 bool HSOFIEnabled
= USB_INT_IsEnabled(USB_INT_HSOFI
);
264 USB_INT_Disable(USB_INT_HSOFI
);
265 USB_INT_Clear(USB_INT_HSOFI
);
267 for (uint8_t MSRem
= 10; MSRem
!= 0; MSRem
--)
269 /* Workaround for powerless-pull-up devices. After a USB bus reset,
270 all disconnection interrupts are suppressed while a USB frame is
271 looked for - if it is found within 10ms, the device is still
274 if (USB_INT_HasOccurred(USB_INT_HSOFI
))
276 USB_INT_Clear(USB_INT_HSOFI
);
277 USB_INT_Clear(USB_INT_DDISCI
);
285 USB_INT_Enable(USB_INT_HSOFI
);
288 USB_Host_SuspendBus();
290 USB_INT_Enable(USB_INT_DDISCI
);