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