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 Pipe_ConfigurePipe(PIPE_CONTROLPIPE
, EP_TYPE_CONTROL
,
115 PIPE_TOKEN_SETUP
, ENDPOINT_CONTROLEP
,
116 PIPE_CONTROLPIPE_DEFAULT_SIZE
, PIPE_BANK_SINGLE
);
118 if (!(Pipe_IsConfigured()))
120 ErrorCode
= HOST_ENUMERROR_PipeConfigError
;
125 USB_HostState
= HOST_STATE_Default
;
127 case HOST_STATE_Default
:
128 USB_ControlRequest
= (USB_Request_Header_t
)
130 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_DEVICE
),
131 .bRequest
= REQ_GetDescriptor
,
132 .wValue
= (DTYPE_Device
<< 8),
137 uint8_t DataBuffer
[8];
139 if ((SubErrorCode
= USB_Host_SendControlRequest(DataBuffer
)) != HOST_SENDCONTROL_Successful
)
141 ErrorCode
= HOST_ENUMERROR_ControlError
;
145 USB_Host_ControlPipeSize
= DataBuffer
[offsetof(USB_Descriptor_Device_t
, Endpoint0Size
)];
147 USB_Host_ResetDevice();
149 HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Default_PostReset
);
151 case HOST_STATE_Default_PostReset
:
152 Pipe_ConfigurePipe(PIPE_CONTROLPIPE
, EP_TYPE_CONTROL
,
153 PIPE_TOKEN_SETUP
, ENDPOINT_CONTROLEP
,
154 USB_Host_ControlPipeSize
, PIPE_BANK_SINGLE
);
156 if (!(Pipe_IsConfigured()))
158 ErrorCode
= HOST_ENUMERROR_PipeConfigError
;
163 USB_ControlRequest
= (USB_Request_Header_t
)
165 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_DEVICE
),
166 .bRequest
= REQ_SetAddress
,
167 .wValue
= USB_HOST_DEVICEADDRESS
,
172 if ((SubErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
174 ErrorCode
= HOST_ENUMERROR_ControlError
;
178 HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Default_PostAddressSet
);
180 case HOST_STATE_Default_PostAddressSet
:
181 USB_Host_SetDeviceAddress(USB_HOST_DEVICEADDRESS
);
183 USB_HostState
= HOST_STATE_Addressed
;
185 EVENT_USB_Host_DeviceEnumerationComplete();
189 if ((ErrorCode
!= HOST_ENUMERROR_NoError
) && (USB_HostState
!= HOST_STATE_Unattached
))
191 EVENT_USB_Host_DeviceEnumerationFailed(ErrorCode
, SubErrorCode
);
193 USB_Host_VBUS_Auto_Off();
195 EVENT_USB_Host_DeviceUnattached();
197 USB_ResetInterface();
201 uint8_t USB_Host_WaitMS(uint8_t MS
)
203 bool BusSuspended
= USB_Host_IsBusSuspended();
204 uint8_t ErrorCode
= HOST_WAITERROR_Successful
;
205 bool HSOFIEnabled
= USB_INT_IsEnabled(USB_INT_HSOFI
);
207 USB_INT_Disable(USB_INT_HSOFI
);
208 USB_INT_Clear(USB_INT_HSOFI
);
210 USB_Host_ResumeBus();
214 if (USB_INT_HasOccurred(USB_INT_HSOFI
))
216 USB_INT_Clear(USB_INT_HSOFI
);
220 if ((USB_HostState
== HOST_STATE_Unattached
) || (USB_CurrentMode
!= USB_MODE_Host
))
222 ErrorCode
= HOST_WAITERROR_DeviceDisconnect
;
230 ErrorCode
= HOST_WAITERROR_PipeError
;
235 if (Pipe_IsStalled())
238 ErrorCode
= HOST_WAITERROR_SetupStalled
;
245 USB_Host_SuspendBus();
248 USB_INT_Enable(USB_INT_HSOFI
);
253 static void USB_Host_ResetDevice(void)
255 bool BusSuspended
= USB_Host_IsBusSuspended();
257 USB_INT_Disable(USB_INT_DDISCI
);
260 while (!(USB_Host_IsBusResetComplete()));
261 USB_Host_ResumeBus();
263 USB_Host_ConfigurationNumber
= 0;
265 bool HSOFIEnabled
= USB_INT_IsEnabled(USB_INT_HSOFI
);
267 USB_INT_Disable(USB_INT_HSOFI
);
268 USB_INT_Clear(USB_INT_HSOFI
);
270 for (uint8_t MSRem
= 10; MSRem
!= 0; MSRem
--)
272 /* Workaround for powerless-pull-up devices. After a USB bus reset,
273 all disconnection interrupts are suppressed while a USB frame is
274 looked for - if it is found within 10ms, the device is still
277 if (USB_INT_HasOccurred(USB_INT_HSOFI
))
279 USB_INT_Clear(USB_INT_HSOFI
);
280 USB_INT_Clear(USB_INT_DDISCI
);
288 USB_INT_Enable(USB_INT_HSOFI
);
291 USB_Host_SuspendBus();
293 USB_INT_Enable(USB_INT_DDISCI
);