fe02f7cd15dd11c3b68738a9f6de97a593ec816b
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Host.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 #include "../HighLevel/USBMode.h"
32
33 #if defined(USB_CAN_BE_HOST)
34
35 #include "Host.h"
36
37 uint8_t USB_Host_WaitMS(uint8_t MS)
38 {
39 bool BusSuspended = USB_Host_IsBusSuspended();
40 uint8_t ErrorCode = HOST_WAITERROR_Successful;
41
42 USB_INT_Clear(USB_INT_HSOFI);
43 USB_Host_ResumeBus();
44
45 while (MS)
46 {
47 if (USB_INT_HasOccurred(USB_INT_HSOFI))
48 {
49 USB_INT_Clear(USB_INT_HSOFI);
50 MS--;
51 }
52
53 if ((USB_IsConnected == false) || (USB_CurrentMode == USB_MODE_DEVICE))
54 {
55 ErrorCode = HOST_WAITERROR_DeviceDisconnect;
56
57 break;
58 }
59
60 if (Pipe_IsError() == true)
61 {
62 Pipe_ClearError();
63 ErrorCode = HOST_WAITERROR_PipeError;
64
65 break;
66 }
67
68 if (Pipe_IsStalled() == true)
69 {
70 Pipe_ClearStall();
71 ErrorCode = HOST_WAITERROR_SetupStalled;
72
73 break;
74 }
75 }
76
77 if (BusSuspended)
78 USB_Host_SuspendBus();
79
80 return ErrorCode;
81 }
82
83 void USB_Host_ResetDevice(void)
84 {
85 bool BusSuspended = USB_Host_IsBusSuspended();
86
87 USB_INT_Disable(USB_INT_DDISCI);
88
89 USB_Host_ResetBus();
90 while (!(USB_Host_IsBusResetComplete()));
91
92 USB_INT_Clear(USB_INT_HSOFI);
93 USB_Host_ResumeBus();
94
95 for (uint8_t MSRem = 10; MSRem != 0; MSRem--)
96 {
97 /* Workaround for powerless-pull-up devices. After a USB bus reset,
98 all disconnection interrupts are suppressed while a USB frame is
99 looked for - if it is found within 10ms, the device is still
100 present. */
101
102 if (USB_INT_HasOccurred(USB_INT_HSOFI))
103 {
104 USB_INT_Clear(USB_INT_DDISCI);
105 break;
106 }
107
108 _delay_ms(1);
109 }
110
111 if (BusSuspended)
112 USB_Host_SuspendBus();
113
114 USB_INT_Enable(USB_INT_DDISCI);
115 }
116 #endif