Speed up calls to Pipe_IsPipeBound() by immediately skipping unconfigured pipes,...
[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.fourwalledcubicle.com
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 #define __INCLUDE_FROM_PIPE_C
37 #include "Pipe.h"
38
39 uint8_t USB_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;
40
41 bool Pipe_ConfigurePipe(const uint8_t Number, const uint8_t Type, const uint8_t Token, const uint8_t EndpointNumber,
42 const uint16_t Size, const uint8_t Banks)
43 {
44 Pipe_SelectPipe(Number);
45 Pipe_EnablePipe();
46
47 UPCFG1X = 0;
48
49 UPCFG0X = ((Type << EPTYPE0) | Token | ((EndpointNumber & PIPE_EPNUM_MASK) << PEPNUM0));
50 UPCFG1X = ((1 << ALLOC) | Banks | Pipe_BytesToEPSizeMask(Size));
51
52 Pipe_SetInfiniteINRequests();
53
54 return Pipe_IsConfigured();
55 }
56
57 void Pipe_ClearPipes(void)
58 {
59 UPINT = 0;
60
61 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
62 {
63 Pipe_ResetPipe(PNum);
64 Pipe_SelectPipe(PNum);
65 UPIENX = 0;
66 UPINTX = 0;
67 Pipe_ClearError();
68 Pipe_ClearErrorFlags();
69 Pipe_DeallocateMemory();
70 Pipe_DisablePipe();
71 }
72 }
73
74 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress)
75 {
76 uint8_t PrevPipeNumber = Pipe_GetCurrentPipe();
77
78 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
79 {
80 Pipe_SelectPipe(PNum);
81
82 if (!(Pipe_IsConfigured()))
83 continue;
84
85 uint8_t PipeToken = Pipe_GetPipeToken();
86 bool PipeTokenCorrect = true;
87
88 if (PipeToken != PIPE_TOKEN_SETUP)
89 PipeTokenCorrect = (PipeToken == ((EndpointAddress & PIPE_EPDIR_MASK) ? PIPE_TOKEN_IN : PIPE_TOKEN_OUT));
90
91 if (PipeTokenCorrect && (Pipe_BoundEndpointNumber() == (EndpointAddress & PIPE_EPNUM_MASK)))
92 return true;
93 }
94
95 Pipe_SelectPipe(PrevPipeNumber);
96 return false;
97 }
98
99 uint8_t Pipe_WaitUntilReady(void)
100 {
101 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
102 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
103 #else
104 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
105 #endif
106
107 for (;;)
108 {
109 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
110 {
111 if (Pipe_IsINReceived())
112 return PIPE_READYWAIT_NoError;
113 }
114 else
115 {
116 if (Pipe_IsOUTReady())
117 return PIPE_READYWAIT_NoError;
118 }
119
120 if (Pipe_IsStalled())
121 return PIPE_READYWAIT_PipeStalled;
122 else if (USB_HostState == HOST_STATE_Unattached)
123 return PIPE_READYWAIT_DeviceDisconnected;
124
125 if (USB_INT_HasOccurred(USB_INT_HSOFI))
126 {
127 USB_INT_Clear(USB_INT_HSOFI);
128
129 if (!(TimeoutMSRem--))
130 return PIPE_READYWAIT_Timeout;
131 }
132 }
133 }
134
135 uint8_t Pipe_Discard_Stream(uint16_t Length
136 #if !defined(NO_STREAM_CALLBACKS)
137 , StreamCallbackPtr_t Callback
138 #endif
139 )
140 {
141 uint8_t ErrorCode;
142
143 Pipe_SetPipeToken(PIPE_TOKEN_IN);
144
145 if ((ErrorCode = Pipe_WaitUntilReady()))
146 return ErrorCode;
147
148 #if defined(FAST_STREAM_TRANSFERS)
149 uint8_t BytesRemToAlignment = (Pipe_BytesInPipe() & 0x07);
150
151 if (Length >= 8)
152 {
153 Length -= BytesRemToAlignment;
154
155 switch (BytesRemToAlignment)
156 {
157 default:
158 do
159 {
160 if (!(Pipe_IsReadWriteAllowed()))
161 {
162 Pipe_ClearIN();
163
164 #if !defined(NO_STREAM_CALLBACKS)
165 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
166 return PIPE_RWSTREAM_CallbackAborted;
167 #endif
168
169 if ((ErrorCode = Pipe_WaitUntilReady()))
170 return ErrorCode;
171 }
172
173 Length -= 8;
174
175 Pipe_Discard_Byte();
176 case 7: Pipe_Discard_Byte();
177 case 6: Pipe_Discard_Byte();
178 case 5: Pipe_Discard_Byte();
179 case 4: Pipe_Discard_Byte();
180 case 3: Pipe_Discard_Byte();
181 case 2: Pipe_Discard_Byte();
182 case 1: Pipe_Discard_Byte();
183 } while (Length >= 8);
184 }
185 }
186 #endif
187
188 while (Length)
189 {
190 if (!(Pipe_IsReadWriteAllowed()))
191 {
192 Pipe_ClearIN();
193
194 #if !defined(NO_STREAM_CALLBACKS)
195 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
196 return PIPE_RWSTREAM_CallbackAborted;
197 #endif
198
199 if ((ErrorCode = Pipe_WaitUntilReady()))
200 return ErrorCode;
201 }
202 else
203 {
204 Pipe_Discard_Byte();
205 Length--;
206 }
207 }
208
209 return PIPE_RWSTREAM_NoError;
210 }
211
212 /* The following abuses the C preprocessor in order to copy-past common code with slight alterations,
213 * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */
214
215 #define TEMPLATE_FUNC_NAME Pipe_Write_Stream_LE
216 #define TEMPLATE_BUFFER_TYPE const void*
217 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
218 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
219 #define TEMPLATE_BUFFER_OFFSET(Length) 0
220 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(*((uint8_t*)BufferPtr++))
221 #include "Template/Template_Pipe_RW.c"
222
223 #define TEMPLATE_FUNC_NAME Pipe_Write_PStream_LE
224 #define TEMPLATE_BUFFER_TYPE const void*
225 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
226 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
227 #define TEMPLATE_BUFFER_OFFSET(Length) 0
228 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++))
229 #include "Template/Template_Pipe_RW.c"
230
231 #define TEMPLATE_FUNC_NAME Pipe_Write_EStream_LE
232 #define TEMPLATE_BUFFER_TYPE const void*
233 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
234 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
235 #define TEMPLATE_BUFFER_OFFSET(Length) 0
236 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++))
237 #include "Template/Template_Pipe_RW.c"
238
239 #define TEMPLATE_FUNC_NAME Pipe_Write_Stream_BE
240 #define TEMPLATE_BUFFER_TYPE const void*
241 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
242 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
243 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
244 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(*((uint8_t*)BufferPtr--))
245 #include "Template/Template_Pipe_RW.c"
246
247 #define TEMPLATE_FUNC_NAME Pipe_Write_PStream_BE
248 #define TEMPLATE_BUFFER_TYPE const void*
249 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
250 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
251 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
252 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--))
253 #include "Template/Template_Pipe_RW.c"
254
255 #define TEMPLATE_FUNC_NAME Pipe_Write_EStream_BE
256 #define TEMPLATE_BUFFER_TYPE const void*
257 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
258 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
259 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
260 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--))
261 #include "Template/Template_Pipe_RW.c"
262
263 #define TEMPLATE_FUNC_NAME Pipe_Read_Stream_LE
264 #define TEMPLATE_BUFFER_TYPE void*
265 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
266 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
267 #define TEMPLATE_BUFFER_OFFSET(Length) 0
268 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr++) = Pipe_Read_Byte()
269 #include "Template/Template_Pipe_RW.c"
270
271 #define TEMPLATE_FUNC_NAME Pipe_Read_EStream_LE
272 #define TEMPLATE_BUFFER_TYPE void*
273 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
274 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
275 #define TEMPLATE_BUFFER_OFFSET(Length) 0
276 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr++, Pipe_Read_Byte())
277 #include "Template/Template_Pipe_RW.c"
278
279 #define TEMPLATE_FUNC_NAME Pipe_Read_Stream_BE
280 #define TEMPLATE_BUFFER_TYPE void*
281 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
282 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
283 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
284 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr--) = Pipe_Read_Byte()
285 #include "Template/Template_Pipe_RW.c"
286
287 #define TEMPLATE_FUNC_NAME Pipe_Read_EStream_BE
288 #define TEMPLATE_BUFFER_TYPE void*
289 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
290 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
291 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
292 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr--, Pipe_Read_Byte())
293 #include "Template/Template_Pipe_RW.c"
294
295 #endif