Fixed error in the pipe unordered allocation algorithm for the AVR8 devices breaking...
[pub/USBasp.git] / LUFA / Drivers / USB / Core / AVR8 / Pipe_AVR8.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
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 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../USBMode.h"
33
34 #if defined(USB_CAN_BE_HOST)
35
36 #include "../Pipe.h"
37
38 uint8_t USB_Host_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;
39
40 bool Pipe_ConfigurePipe(const uint8_t Number,
41 const uint8_t Type,
42 const uint8_t Token,
43 const uint8_t EndpointNumber,
44 const uint16_t Size,
45 const uint8_t Banks)
46 {
47 #if defined(ORDERED_EP_CONFIG)
48 Pipe_SelectPipe(Number);
49 Pipe_EnablePipe();
50
51 UPCFG1X = 0;
52
53 UPCFG0X = ((Type << EPTYPE0) | Token | ((EndpointNumber & PIPE_EPNUM_MASK) << PEPNUM0));
54 UPCFG1X = ((1 << ALLOC) | Banks | Pipe_BytesToEPSizeMask(Size));
55
56 Pipe_SetInfiniteINRequests();
57
58 return Pipe_IsConfigured();
59 #else
60 for (uint8_t PNum = Number; PNum < PIPE_TOTAL_PIPES; PNum++)
61 {
62 uint8_t UPCFG0XTemp;
63 uint8_t UPCFG1XTemp;
64 uint8_t UPCFG2XTemp;
65 uint8_t UPIENXTemp;
66
67 Pipe_SelectPipe(PNum);
68
69 if (PNum == Number)
70 {
71 UPCFG0XTemp = ((Type << EPTYPE0) | Token | ((EndpointNumber & PIPE_EPNUM_MASK) << PEPNUM0));
72 UPCFG1XTemp = ((1 << ALLOC) | Banks | Pipe_BytesToEPSizeMask(Size));
73 UPCFG2XTemp = 0;
74 UPIENXTemp = 0;
75 }
76 else
77 {
78 UPCFG0XTemp = UPCFG0X;
79 UPCFG1XTemp = UPCFG1X;
80 UPCFG2XTemp = UPCFG2X;
81 UPIENXTemp = UPIENX;
82 }
83
84 if (!(UPCFG1XTemp & (1 << ALLOC)))
85 continue;
86
87 Pipe_DisablePipe();
88 UPCFG1X &= ~(1 << ALLOC);
89
90 Pipe_EnablePipe();
91 UPCFG0X = UPCFG0XTemp;
92 UPCFG1X = UPCFG1XTemp;
93 UPCFG2X = UPCFG2XTemp;
94 UPIENX = UPIENXTemp;
95
96 Pipe_SetInfiniteINRequests();
97
98 if (!(Pipe_IsConfigured()))
99 return false;
100 }
101
102 Pipe_SelectPipe(Number);
103 return true;
104 #endif
105 }
106
107 void Pipe_ClearPipes(void)
108 {
109 UPINT = 0;
110
111 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
112 {
113 Pipe_SelectPipe(PNum);
114 UPIENX = 0;
115 UPINTX = 0;
116 UPCFG1X = 0;
117 Pipe_DisablePipe();
118 }
119 }
120
121 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress)
122 {
123 uint8_t PrevPipeNumber = Pipe_GetCurrentPipe();
124
125 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
126 {
127 Pipe_SelectPipe(PNum);
128
129 if (!(Pipe_IsConfigured()))
130 continue;
131
132 if (Pipe_GetBoundEndpointAddress() == EndpointAddress)
133 return true;
134 }
135
136 Pipe_SelectPipe(PrevPipeNumber);
137 return false;
138 }
139
140 uint8_t Pipe_WaitUntilReady(void)
141 {
142 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
143 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
144 #else
145 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
146 #endif
147
148 uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
149
150 for (;;)
151 {
152 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
153 {
154 if (Pipe_IsINReceived())
155 return PIPE_READYWAIT_NoError;
156 }
157 else
158 {
159 if (Pipe_IsOUTReady())
160 return PIPE_READYWAIT_NoError;
161 }
162
163 if (Pipe_IsStalled())
164 return PIPE_READYWAIT_PipeStalled;
165 else if (USB_HostState == HOST_STATE_Unattached)
166 return PIPE_READYWAIT_DeviceDisconnected;
167
168 uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
169
170 if (CurrentFrameNumber != PreviousFrameNumber)
171 {
172 PreviousFrameNumber = CurrentFrameNumber;
173
174 if (!(TimeoutMSRem--))
175 return PIPE_READYWAIT_Timeout;
176 }
177 }
178 }
179
180 #endif
181