3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
31 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../HighLevel/USBMode.h"
34 #if defined(USB_CAN_BE_HOST)
36 #define __INCLUDE_FROM_PIPE_C
39 uint8_t USB_ControlPipeSize
= PIPE_CONTROLPIPE_DEFAULT_SIZE
;
41 bool Pipe_ConfigurePipe(const uint8_t Number
,
44 const uint8_t EndpointNumber
,
48 Pipe_SelectPipe(Number
);
53 UPCFG0X
= ((Type
<< EPTYPE0
) | Token
| ((EndpointNumber
& PIPE_EPNUM_MASK
) << PEPNUM0
));
54 UPCFG1X
= ((1 << ALLOC
) | Banks
| Pipe_BytesToEPSizeMask(Size
));
56 Pipe_SetInfiniteINRequests();
58 return Pipe_IsConfigured();
61 void Pipe_ClearPipes(void)
65 for (uint8_t PNum
= 0; PNum
< PIPE_TOTAL_PIPES
; PNum
++)
67 Pipe_SelectPipe(PNum
);
75 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress
)
77 uint8_t PrevPipeNumber
= Pipe_GetCurrentPipe();
79 for (uint8_t PNum
= 0; PNum
< PIPE_TOTAL_PIPES
; PNum
++)
81 Pipe_SelectPipe(PNum
);
83 if (!(Pipe_IsConfigured()))
86 uint8_t PipeToken
= Pipe_GetPipeToken();
87 bool PipeTokenCorrect
= true;
89 if (PipeToken
!= PIPE_TOKEN_SETUP
)
90 PipeTokenCorrect
= (PipeToken
== ((EndpointAddress
& PIPE_EPDIR_MASK
) ? PIPE_TOKEN_IN
: PIPE_TOKEN_OUT
));
92 if (PipeTokenCorrect
&& (Pipe_BoundEndpointNumber() == (EndpointAddress
& PIPE_EPNUM_MASK
)))
96 Pipe_SelectPipe(PrevPipeNumber
);
100 uint8_t Pipe_WaitUntilReady(void)
102 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
103 uint8_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
105 uint16_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
110 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN
)
112 if (Pipe_IsINReceived())
113 return PIPE_READYWAIT_NoError
;
117 if (Pipe_IsOUTReady())
118 return PIPE_READYWAIT_NoError
;
121 if (Pipe_IsStalled())
122 return PIPE_READYWAIT_PipeStalled
;
123 else if (USB_HostState
== HOST_STATE_Unattached
)
124 return PIPE_READYWAIT_DeviceDisconnected
;
126 if (USB_INT_HasOccurred(USB_INT_HSOFI
))
128 USB_INT_Clear(USB_INT_HSOFI
);
130 if (!(TimeoutMSRem
--))
131 return PIPE_READYWAIT_Timeout
;
136 uint8_t Pipe_Discard_Stream(uint16_t Length
137 #if !defined(NO_STREAM_CALLBACKS)
138 , StreamCallbackPtr_t Callback
144 Pipe_SetPipeToken(PIPE_TOKEN_IN
);
146 if ((ErrorCode
= Pipe_WaitUntilReady()))
149 #if defined(FAST_STREAM_TRANSFERS)
150 uint8_t BytesRemToAlignment
= (Pipe_BytesInPipe() & 0x07);
154 Length
-= BytesRemToAlignment
;
156 switch (BytesRemToAlignment
)
161 if (!(Pipe_IsReadWriteAllowed()))
165 #if !defined(NO_STREAM_CALLBACKS)
166 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
167 return PIPE_RWSTREAM_CallbackAborted
;
170 if ((ErrorCode
= Pipe_WaitUntilReady()))
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);
191 if (!(Pipe_IsReadWriteAllowed()))
195 #if !defined(NO_STREAM_CALLBACKS)
196 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
197 return PIPE_RWSTREAM_CallbackAborted
;
200 if ((ErrorCode
= Pipe_WaitUntilReady()))
210 return PIPE_RWSTREAM_NoError
;
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. */
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"