5850fcf7c15d17a6c3320c70963a02c09b1a00a6
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Pipe.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 #define INCLUDE_FROM_PIPE_C
36 #include "Pipe.h"
37
38 uint8_t USB_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;
39
40 bool Pipe_ConfigurePipe(const uint8_t Number, const uint8_t Type, const uint8_t Token, const uint8_t EndpointNumber,
41 const uint16_t Size, const uint8_t Banks)
42 {
43 Pipe_SelectPipe(Number);
44 Pipe_EnablePipe();
45
46 UPCFG1X = 0;
47
48 UPCFG0X = ((Type << EPTYPE0) | Token | (EndpointNumber << PEPNUM0));
49 UPCFG1X = ((1 << ALLOC) | Banks | Pipe_BytesToEPSizeMask(Size));
50
51 return Pipe_IsConfigured();
52 }
53
54 void Pipe_ClearPipes(void)
55 {
56 UPINT = 0;
57
58 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
59 {
60 Pipe_ResetPipe(PNum);
61 Pipe_SelectPipe(PNum);
62 UPIENX = 0;
63 UPINTX = 0;
64 Pipe_ClearError();
65 Pipe_ClearErrorFlags();
66 Pipe_DeallocateMemory();
67 Pipe_DisablePipe();
68 }
69 }
70
71 uint8_t Pipe_WaitUntilReady(void)
72 {
73 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
74
75 USB_INT_Clear(USB_INT_HSOFI);
76
77 for (;;)
78 {
79 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
80 {
81 if (Pipe_IsINReceived())
82 return PIPE_READYWAIT_NoError;
83 }
84 else
85 {
86 if (Pipe_IsOUTReady())
87 return PIPE_READYWAIT_NoError;
88 }
89
90 if (Pipe_IsStalled())
91 return PIPE_READYWAIT_PipeStalled;
92 else if (!(USB_IsConnected))
93 return PIPE_READYWAIT_DeviceDisconnected;
94
95 if (USB_INT_HasOccurred(USB_INT_HSOFI))
96 {
97 USB_INT_Clear(USB_INT_HSOFI);
98
99 if (!(TimeoutMSRem--))
100 return PIPE_READYWAIT_Timeout;
101 }
102 }
103 }
104
105 uint8_t Pipe_Write_Stream_LE(const void* Data, uint16_t Length
106 #if !defined(NO_STREAM_CALLBACKS)
107 , uint8_t (* const Callback)(void)
108 #endif
109 )
110 {
111 uint8_t* DataStream = (uint8_t*)Data;
112 uint8_t ErrorCode;
113
114 if ((ErrorCode = Pipe_WaitUntilReady()))
115 return ErrorCode;
116
117 while (Length--)
118 {
119 if (!(Pipe_IsReadWriteAllowed()))
120 {
121 Pipe_ClearOUT();
122
123 #if !defined(NO_STREAM_CALLBACKS)
124 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
125 return PIPE_RWSTREAM_ERROR_CallbackAborted;
126 #endif
127
128 if ((ErrorCode = Pipe_WaitUntilReady()))
129 return ErrorCode;
130 }
131 else
132 {
133 Pipe_Write_Byte(*(DataStream++));
134 }
135 }
136
137 return PIPE_RWSTREAM_ERROR_NoError;
138 }
139
140 uint8_t Pipe_Write_Stream_BE(const void* Data, uint16_t Length
141 #if !defined(NO_STREAM_CALLBACKS)
142 , uint8_t (* const Callback)(void)
143 #endif
144 )
145 {
146 uint8_t* DataStream = (uint8_t*)(Data + Length - 1);
147 uint8_t ErrorCode;
148
149 if ((ErrorCode = Pipe_WaitUntilReady()))
150 return ErrorCode;
151
152 while (Length--)
153 {
154 if (!(Pipe_IsReadWriteAllowed()))
155 {
156 Pipe_ClearOUT();
157
158 #if !defined(NO_STREAM_CALLBACKS)
159 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
160 return PIPE_RWSTREAM_ERROR_CallbackAborted;
161 #endif
162
163 if ((ErrorCode = Pipe_WaitUntilReady()))
164 return ErrorCode;
165 }
166 else
167 {
168 Pipe_Write_Byte(*(DataStream--));
169 }
170 }
171
172 return PIPE_RWSTREAM_ERROR_NoError;
173 }
174
175 uint8_t Pipe_Discard_Stream(uint16_t Length
176 #if !defined(NO_STREAM_CALLBACKS)
177 , uint8_t (* const Callback)(void)
178 #endif
179 )
180 {
181 uint8_t ErrorCode;
182
183 if ((ErrorCode = Pipe_WaitUntilReady()))
184 return ErrorCode;
185
186 while (Length--)
187 {
188 if (!(Pipe_IsReadWriteAllowed()))
189 {
190 Pipe_ClearIN();
191
192 #if !defined(NO_STREAM_CALLBACKS)
193 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
194 return PIPE_RWSTREAM_ERROR_CallbackAborted;
195 #endif
196
197 if ((ErrorCode = Pipe_WaitUntilReady()))
198 return ErrorCode;
199 }
200 else
201 {
202 Pipe_Discard_Byte();
203 }
204 }
205
206 return PIPE_RWSTREAM_ERROR_NoError;
207 }
208
209 uint8_t Pipe_Read_Stream_LE(void* Buffer, uint16_t Length
210 #if !defined(NO_STREAM_CALLBACKS)
211 , uint8_t (* const Callback)(void)
212 #endif
213 )
214 {
215 uint8_t* DataStream = (uint8_t*)Buffer;
216 uint8_t ErrorCode;
217
218 if ((ErrorCode = Pipe_WaitUntilReady()))
219 return ErrorCode;
220
221 while (Length--)
222 {
223 if (!(Pipe_IsReadWriteAllowed()))
224 {
225 Pipe_ClearIN();
226
227 #if !defined(NO_STREAM_CALLBACKS)
228 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
229 return PIPE_RWSTREAM_ERROR_CallbackAborted;
230 #endif
231
232 if ((ErrorCode = Pipe_WaitUntilReady()))
233 return ErrorCode;
234 }
235 else
236 {
237 *(DataStream++) = Pipe_Read_Byte();
238 }
239 }
240
241 return PIPE_RWSTREAM_ERROR_NoError;
242 }
243
244 uint8_t Pipe_Read_Stream_BE(void* Buffer, uint16_t Length
245 #if !defined(NO_STREAM_CALLBACKS)
246 , uint8_t (* const Callback)(void)
247 #endif
248 )
249 {
250 uint8_t* DataStream = (uint8_t*)(Buffer + Length - 1);
251 uint8_t ErrorCode;
252
253 if ((ErrorCode = Pipe_WaitUntilReady()))
254 return ErrorCode;
255
256 while (Length--)
257 {
258 if (!(Pipe_IsReadWriteAllowed()))
259 {
260 Pipe_ClearIN();
261
262 #if !defined(NO_STREAM_CALLBACKS)
263 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
264 return PIPE_RWSTREAM_ERROR_CallbackAborted;
265 #endif
266
267 if ((ErrorCode = Pipe_WaitUntilReady()))
268 return ErrorCode;
269 }
270 else
271 {
272 *(DataStream--) = Pipe_Read_Byte();
273 }
274 }
275
276 return PIPE_RWSTREAM_ERROR_NoError;
277 }
278
279 #endif