Re-add in old Endpoint/Pipe workaround for unordered pipes - add new ORDERED_EP_CONFI...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Pipe.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2010 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 "../HighLevel/USBMode.h"
33
34 #if defined(USB_CAN_BE_HOST)
35
36 #include "Pipe.h"
37
38 uint8_t USB_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 uint8_t UPCFG0XTemp[PIPE_TOTAL_PIPES];
61 uint8_t UPCFG1XTemp[PIPE_TOTAL_PIPES];
62 uint8_t UPCFG2XTemp[PIPE_TOTAL_PIPES];
63 uint8_t UPCONXTemp[PIPE_TOTAL_PIPES];
64 uint8_t UPINRQXTemp[PIPE_TOTAL_PIPES];
65 uint8_t UPIENXTemp[PIPE_TOTAL_PIPES];
66
67 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
68 {
69 Pipe_SelectPipe(PNum);
70 UPCFG0XTemp[PNum] = UPCFG0X;
71 UPCFG1XTemp[PNum] = UPCFG1X;
72 UPCFG2XTemp[PNum] = UPCFG2X;
73 UPCONXTemp[PNum] = UPCONX;
74 UPINRQXTemp[PNum] = UPINRQX;
75 UPIENXTemp[PNum] = UPIENX;
76 }
77
78 UPCFG0XTemp[Number] = ((Type << EPTYPE0) | Token | ((EndpointNumber & PIPE_EPNUM_MASK) << PEPNUM0));
79 UPCFG1XTemp[Number] = ((1 << ALLOC) | Banks | Pipe_BytesToEPSizeMask(Size));
80 UPCFG2XTemp[Number] = 0;
81 UPCONXTemp[Number] = (1 << INMODE);
82 UPINRQXTemp[Number] = 0;
83 UPIENXTemp[Number] = 0;
84
85 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
86 {
87 Pipe_SelectPipe(PNum);
88 UPIENX = 0;
89 UPINTX = 0;
90 UPCFG1X = 0;
91 Pipe_DisablePipe();
92 }
93
94 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
95 {
96 if (!(UPCFG1XTemp[PNum] & (1 << ALLOC)))
97 continue;
98
99 Pipe_SelectPipe(PNum);
100 Pipe_EnablePipe();
101
102 UPCFG0X = UPCFG0XTemp[PNum];
103 UPCFG1X = UPCFG1XTemp[PNum];
104 UPCFG2X = UPCFG2XTemp[PNum];
105 UPCONX |= UPCONXTemp[PNum];
106 UPINRQX = UPINRQXTemp[PNum];
107 UPIENX = UPIENXTemp[PNum];
108
109 if (!(Pipe_IsConfigured()))
110 return false;
111 }
112
113 Pipe_SelectPipe(Number);
114 return true;
115 #endif
116 }
117
118 void Pipe_ClearPipes(void)
119 {
120 UPINT = 0;
121
122 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
123 {
124 Pipe_SelectPipe(PNum);
125 UPIENX = 0;
126 UPINTX = 0;
127 UPCFG1X = 0;
128 Pipe_DisablePipe();
129 }
130 }
131
132 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress)
133 {
134 uint8_t PrevPipeNumber = Pipe_GetCurrentPipe();
135
136 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
137 {
138 Pipe_SelectPipe(PNum);
139
140 if (!(Pipe_IsConfigured()))
141 continue;
142
143 uint8_t PipeToken = Pipe_GetPipeToken();
144 bool PipeTokenCorrect = true;
145
146 if (PipeToken != PIPE_TOKEN_SETUP)
147 PipeTokenCorrect = (PipeToken == ((EndpointAddress & PIPE_EPDIR_MASK) ? PIPE_TOKEN_IN : PIPE_TOKEN_OUT));
148
149 if (PipeTokenCorrect && (Pipe_BoundEndpointNumber() == (EndpointAddress & PIPE_EPNUM_MASK)))
150 return true;
151 }
152
153 Pipe_SelectPipe(PrevPipeNumber);
154 return false;
155 }
156
157 uint8_t Pipe_WaitUntilReady(void)
158 {
159 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
160 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
161 #else
162 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
163 #endif
164
165 uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
166
167 for (;;)
168 {
169 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
170 {
171 if (Pipe_IsINReceived())
172 return PIPE_READYWAIT_NoError;
173 }
174 else
175 {
176 if (Pipe_IsOUTReady())
177 return PIPE_READYWAIT_NoError;
178 }
179
180 if (Pipe_IsStalled())
181 return PIPE_READYWAIT_PipeStalled;
182 else if (USB_HostState == HOST_STATE_Unattached)
183 return PIPE_READYWAIT_DeviceDisconnected;
184
185 uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
186
187 if (CurrentFrameNumber != PreviousFrameNumber)
188 {
189 PreviousFrameNumber = CurrentFrameNumber;
190
191 if (!(TimeoutMSRem--))
192 return PIPE_READYWAIT_Timeout;
193 }
194 }
195 }
196
197 #endif
198