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 uint8_t UPCFG0XTemp
[PIPE_TOTAL_PIPES
];
49 uint8_t UPCFG1XTemp
[PIPE_TOTAL_PIPES
];
51 for (uint8_t PNum
= 0; PNum
< PIPE_TOTAL_PIPES
; PNum
++)
53 Pipe_SelectPipe(PNum
);
54 UPCFG0XTemp
[PNum
] = UPCFG0X
;
55 UPCFG1XTemp
[PNum
] = UPCFG1X
;
58 UPCFG0XTemp
[Number
] = ((Type
<< EPTYPE0
) | Token
| ((EndpointNumber
& PIPE_EPNUM_MASK
) << PEPNUM0
));
59 UPCFG1XTemp
[Number
] = ((1 << ALLOC
) | Banks
| Pipe_BytesToEPSizeMask(Size
));
61 for (uint8_t PNum
= 0; PNum
< PIPE_TOTAL_PIPES
; PNum
++)
63 Pipe_SelectPipe(PNum
);
70 for (uint8_t PNum
= 0; PNum
< PIPE_TOTAL_PIPES
; PNum
++)
72 if (!(UPCFG1XTemp
[PNum
] & (1 << ALLOC
)))
75 Pipe_SelectPipe(PNum
);
78 UPCFG0X
= UPCFG0XTemp
[PNum
];
79 UPCFG1X
= UPCFG1XTemp
[PNum
];
81 if (!(Pipe_IsConfigured()))
85 Pipe_SelectPipe(Number
);
86 Pipe_SetInfiniteINRequests();
91 void Pipe_ClearPipes(void)
95 for (uint8_t PNum
= 0; PNum
< PIPE_TOTAL_PIPES
; PNum
++)
97 Pipe_SelectPipe(PNum
);
105 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress
)
107 uint8_t PrevPipeNumber
= Pipe_GetCurrentPipe();
109 for (uint8_t PNum
= 0; PNum
< PIPE_TOTAL_PIPES
; PNum
++)
111 Pipe_SelectPipe(PNum
);
113 if (!(Pipe_IsConfigured()))
116 uint8_t PipeToken
= Pipe_GetPipeToken();
117 bool PipeTokenCorrect
= true;
119 if (PipeToken
!= PIPE_TOKEN_SETUP
)
120 PipeTokenCorrect
= (PipeToken
== ((EndpointAddress
& PIPE_EPDIR_MASK
) ? PIPE_TOKEN_IN
: PIPE_TOKEN_OUT
));
122 if (PipeTokenCorrect
&& (Pipe_BoundEndpointNumber() == (EndpointAddress
& PIPE_EPNUM_MASK
)))
126 Pipe_SelectPipe(PrevPipeNumber
);
130 uint8_t Pipe_WaitUntilReady(void)
132 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
133 uint8_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
135 uint16_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
140 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN
)
142 if (Pipe_IsINReceived())
143 return PIPE_READYWAIT_NoError
;
147 if (Pipe_IsOUTReady())
148 return PIPE_READYWAIT_NoError
;
151 if (Pipe_IsStalled())
152 return PIPE_READYWAIT_PipeStalled
;
153 else if (USB_HostState
== HOST_STATE_Unattached
)
154 return PIPE_READYWAIT_DeviceDisconnected
;
156 if (USB_INT_HasOccurred(USB_INT_HSOFI
))
158 USB_INT_Clear(USB_INT_HSOFI
);
160 if (!(TimeoutMSRem
--))
161 return PIPE_READYWAIT_Timeout
;
166 uint8_t Pipe_Discard_Stream(uint16_t Length
167 #if !defined(NO_STREAM_CALLBACKS)
168 , StreamCallbackPtr_t Callback
174 Pipe_SetPipeToken(PIPE_TOKEN_IN
);
176 if ((ErrorCode
= Pipe_WaitUntilReady()))
179 #if defined(FAST_STREAM_TRANSFERS)
180 uint8_t BytesRemToAlignment
= (Pipe_BytesInPipe() & 0x07);
184 Length
-= BytesRemToAlignment
;
186 switch (BytesRemToAlignment
)
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()))
207 case 7: Pipe_Discard_Byte();
208 case 6: Pipe_Discard_Byte();
209 case 5: Pipe_Discard_Byte();
210 case 4: Pipe_Discard_Byte();
211 case 3: Pipe_Discard_Byte();
212 case 2: Pipe_Discard_Byte();
213 case 1: Pipe_Discard_Byte();
214 } while (Length
>= 8);
221 if (!(Pipe_IsReadWriteAllowed()))
225 #if !defined(NO_STREAM_CALLBACKS)
226 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
227 return PIPE_RWSTREAM_CallbackAborted
;
230 if ((ErrorCode
= Pipe_WaitUntilReady()))
240 return PIPE_RWSTREAM_NoError
;
243 /* The following abuses the C preprocessor in order to copy-past common code with slight alterations,
244 * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */
246 #define TEMPLATE_FUNC_NAME Pipe_Write_Stream_LE
247 #define TEMPLATE_BUFFER_TYPE const void*
248 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
249 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
250 #define TEMPLATE_BUFFER_OFFSET(Length) 0
251 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(*((uint8_t*)BufferPtr++))
252 #include "Template/Template_Pipe_RW.c"
254 #define TEMPLATE_FUNC_NAME Pipe_Write_PStream_LE
255 #define TEMPLATE_BUFFER_TYPE const void*
256 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
257 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
258 #define TEMPLATE_BUFFER_OFFSET(Length) 0
259 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++))
260 #include "Template/Template_Pipe_RW.c"
262 #define TEMPLATE_FUNC_NAME Pipe_Write_EStream_LE
263 #define TEMPLATE_BUFFER_TYPE const void*
264 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
265 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
266 #define TEMPLATE_BUFFER_OFFSET(Length) 0
267 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++))
268 #include "Template/Template_Pipe_RW.c"
270 #define TEMPLATE_FUNC_NAME Pipe_Write_Stream_BE
271 #define TEMPLATE_BUFFER_TYPE const void*
272 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
273 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
274 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
275 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(*((uint8_t*)BufferPtr--))
276 #include "Template/Template_Pipe_RW.c"
278 #define TEMPLATE_FUNC_NAME Pipe_Write_PStream_BE
279 #define TEMPLATE_BUFFER_TYPE const void*
280 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
281 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
282 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
283 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--))
284 #include "Template/Template_Pipe_RW.c"
286 #define TEMPLATE_FUNC_NAME Pipe_Write_EStream_BE
287 #define TEMPLATE_BUFFER_TYPE const void*
288 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
289 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
290 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
291 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--))
292 #include "Template/Template_Pipe_RW.c"
294 #define TEMPLATE_FUNC_NAME Pipe_Read_Stream_LE
295 #define TEMPLATE_BUFFER_TYPE void*
296 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
297 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
298 #define TEMPLATE_BUFFER_OFFSET(Length) 0
299 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr++) = Pipe_Read_Byte()
300 #include "Template/Template_Pipe_RW.c"
302 #define TEMPLATE_FUNC_NAME Pipe_Read_EStream_LE
303 #define TEMPLATE_BUFFER_TYPE void*
304 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
305 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
306 #define TEMPLATE_BUFFER_OFFSET(Length) 0
307 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr++, Pipe_Read_Byte())
308 #include "Template/Template_Pipe_RW.c"
310 #define TEMPLATE_FUNC_NAME Pipe_Read_Stream_BE
311 #define TEMPLATE_BUFFER_TYPE void*
312 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
313 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
314 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
315 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr--) = Pipe_Read_Byte()
316 #include "Template/Template_Pipe_RW.c"
318 #define TEMPLATE_FUNC_NAME Pipe_Read_EStream_BE
319 #define TEMPLATE_BUFFER_TYPE void*
320 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
321 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
322 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
323 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr--, Pipe_Read_Byte())
324 #include "Template/Template_Pipe_RW.c"