3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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 #include "../HighLevel/USBMode.h"
33 #if defined(USB_CAN_BE_HOST)
35 #define INCLUDE_FROM_PIPE_C
38 uint8_t USB_ControlPipeSize
= PIPE_CONTROLPIPE_DEFAULT_SIZE
;
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
)
43 Pipe_SelectPipe(Number
);
48 UPCFG0X
= ((Type
<< EPTYPE0
) | Token
| ((EndpointNumber
& PIPE_EPNUM_MASK
) << PEPNUM0
));
49 UPCFG1X
= ((1 << ALLOC
) | Banks
| Pipe_BytesToEPSizeMask(Size
));
51 if (Token
== PIPE_TOKEN_IN
)
52 Pipe_SetInfiniteINRequests();
54 return Pipe_IsConfigured();
57 void Pipe_ClearPipes(void)
61 for (uint8_t PNum
= 0; PNum
< PIPE_TOTAL_PIPES
; PNum
++)
64 Pipe_SelectPipe(PNum
);
68 Pipe_ClearErrorFlags();
69 Pipe_DeallocateMemory();
74 uint8_t Pipe_WaitUntilReady(void)
76 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
77 uint8_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
79 uint16_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
84 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN
)
86 if (Pipe_IsINReceived())
87 return PIPE_READYWAIT_NoError
;
91 if (Pipe_IsOUTReady())
92 return PIPE_READYWAIT_NoError
;
96 return PIPE_READYWAIT_PipeStalled
;
97 else if (!(USB_IsConnected
))
98 return PIPE_READYWAIT_DeviceDisconnected
;
100 if (USB_INT_HasOccurred(USB_INT_HSOFI
))
102 USB_INT_Clear(USB_INT_HSOFI
);
104 if (!(TimeoutMSRem
--))
105 return PIPE_READYWAIT_Timeout
;
110 uint8_t Pipe_Write_Stream_LE(const void* Data
, uint16_t Length
111 #if !defined(NO_STREAM_CALLBACKS)
112 , StreamCallbackPtr_t Callback
116 uint8_t* DataStream
= (uint8_t*)Data
;
119 Pipe_SetToken(PIPE_TOKEN_OUT
);
121 if ((ErrorCode
= Pipe_WaitUntilReady()))
126 if (!(Pipe_IsReadWriteAllowed()))
130 #if !defined(NO_STREAM_CALLBACKS)
131 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
132 return PIPE_RWSTREAM_CallbackAborted
;
135 if ((ErrorCode
= Pipe_WaitUntilReady()))
140 Pipe_Write_Byte(*(DataStream
++));
145 return PIPE_RWSTREAM_NoError
;
148 uint8_t Pipe_Write_Stream_BE(const void* Data
, uint16_t Length
149 #if !defined(NO_STREAM_CALLBACKS)
150 , StreamCallbackPtr_t Callback
154 uint8_t* DataStream
= (uint8_t*)(Data
+ Length
- 1);
157 Pipe_SetToken(PIPE_TOKEN_OUT
);
159 if ((ErrorCode
= Pipe_WaitUntilReady()))
164 if (!(Pipe_IsReadWriteAllowed()))
168 #if !defined(NO_STREAM_CALLBACKS)
169 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
170 return PIPE_RWSTREAM_CallbackAborted
;
173 if ((ErrorCode
= Pipe_WaitUntilReady()))
178 Pipe_Write_Byte(*(DataStream
--));
183 return PIPE_RWSTREAM_NoError
;
186 uint8_t Pipe_Discard_Stream(uint16_t Length
187 #if !defined(NO_STREAM_CALLBACKS)
188 , StreamCallbackPtr_t Callback
194 Pipe_SetToken(PIPE_TOKEN_IN
);
196 if ((ErrorCode
= Pipe_WaitUntilReady()))
201 if (!(Pipe_IsReadWriteAllowed()))
205 #if !defined(NO_STREAM_CALLBACKS)
206 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
207 return PIPE_RWSTREAM_CallbackAborted
;
210 if ((ErrorCode
= Pipe_WaitUntilReady()))
220 return PIPE_RWSTREAM_NoError
;
223 uint8_t Pipe_Read_Stream_LE(void* Buffer
, uint16_t Length
224 #if !defined(NO_STREAM_CALLBACKS)
225 , StreamCallbackPtr_t Callback
229 uint8_t* DataStream
= (uint8_t*)Buffer
;
232 Pipe_SetToken(PIPE_TOKEN_IN
);
234 if ((ErrorCode
= Pipe_WaitUntilReady()))
239 if (!(Pipe_IsReadWriteAllowed()))
243 #if !defined(NO_STREAM_CALLBACKS)
244 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
245 return PIPE_RWSTREAM_CallbackAborted
;
248 if ((ErrorCode
= Pipe_WaitUntilReady()))
253 *(DataStream
++) = Pipe_Read_Byte();
258 return PIPE_RWSTREAM_NoError
;
261 uint8_t Pipe_Read_Stream_BE(void* Buffer
, uint16_t Length
262 #if !defined(NO_STREAM_CALLBACKS)
263 , StreamCallbackPtr_t Callback
267 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
270 Pipe_SetToken(PIPE_TOKEN_IN
);
272 if ((ErrorCode
= Pipe_WaitUntilReady()))
277 if (!(Pipe_IsReadWriteAllowed()))
281 #if !defined(NO_STREAM_CALLBACKS)
282 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
283 return PIPE_RWSTREAM_CallbackAborted
;
286 if ((ErrorCode
= Pipe_WaitUntilReady()))
291 *(DataStream
--) = Pipe_Read_Byte();
296 return PIPE_RWSTREAM_NoError
;