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