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 Pipe_SetInfiniteINRequests();
53 return Pipe_IsConfigured();
56 void Pipe_ClearPipes(void)
60 for (uint8_t PNum
= 0; PNum
< PIPE_TOTAL_PIPES
; PNum
++)
63 Pipe_SelectPipe(PNum
);
67 Pipe_ClearErrorFlags();
68 Pipe_DeallocateMemory();
73 uint8_t Pipe_WaitUntilReady(void)
75 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
76 uint8_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
78 uint16_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
83 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN
)
85 if (Pipe_IsINReceived())
86 return PIPE_READYWAIT_NoError
;
90 if (Pipe_IsOUTReady())
91 return PIPE_READYWAIT_NoError
;
95 return PIPE_READYWAIT_PipeStalled
;
96 else if (!(USB_IsConnected
))
97 return PIPE_READYWAIT_DeviceDisconnected
;
99 if (USB_INT_HasOccurred(USB_INT_HSOFI
))
101 USB_INT_Clear(USB_INT_HSOFI
);
103 if (!(TimeoutMSRem
--))
104 return PIPE_READYWAIT_Timeout
;
109 uint8_t Pipe_Write_Stream_LE(const void* Data
, uint16_t Length
110 #if !defined(NO_STREAM_CALLBACKS)
111 , StreamCallbackPtr_t Callback
115 uint8_t* DataStream
= (uint8_t*)Data
;
118 Pipe_SetToken(PIPE_TOKEN_OUT
);
120 if ((ErrorCode
= Pipe_WaitUntilReady()))
125 if (!(Pipe_IsReadWriteAllowed()))
129 #if !defined(NO_STREAM_CALLBACKS)
130 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
131 return PIPE_RWSTREAM_CallbackAborted
;
134 if ((ErrorCode
= Pipe_WaitUntilReady()))
139 Pipe_Write_Byte(*(DataStream
++));
144 return PIPE_RWSTREAM_NoError
;
147 uint8_t Pipe_Write_Stream_BE(const void* Data
, uint16_t Length
148 #if !defined(NO_STREAM_CALLBACKS)
149 , StreamCallbackPtr_t Callback
153 uint8_t* DataStream
= (uint8_t*)(Data
+ Length
- 1);
156 Pipe_SetToken(PIPE_TOKEN_OUT
);
158 if ((ErrorCode
= Pipe_WaitUntilReady()))
163 if (!(Pipe_IsReadWriteAllowed()))
167 #if !defined(NO_STREAM_CALLBACKS)
168 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
169 return PIPE_RWSTREAM_CallbackAborted
;
172 if ((ErrorCode
= Pipe_WaitUntilReady()))
177 Pipe_Write_Byte(*(DataStream
--));
182 return PIPE_RWSTREAM_NoError
;
185 uint8_t Pipe_Discard_Stream(uint16_t Length
186 #if !defined(NO_STREAM_CALLBACKS)
187 , StreamCallbackPtr_t Callback
193 Pipe_SetToken(PIPE_TOKEN_IN
);
195 if ((ErrorCode
= Pipe_WaitUntilReady()))
200 if (!(Pipe_IsReadWriteAllowed()))
204 #if !defined(NO_STREAM_CALLBACKS)
205 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
206 return PIPE_RWSTREAM_CallbackAborted
;
209 if ((ErrorCode
= Pipe_WaitUntilReady()))
219 return PIPE_RWSTREAM_NoError
;
222 uint8_t Pipe_Read_Stream_LE(void* Buffer
, uint16_t Length
223 #if !defined(NO_STREAM_CALLBACKS)
224 , StreamCallbackPtr_t Callback
228 uint8_t* DataStream
= (uint8_t*)Buffer
;
231 Pipe_SetToken(PIPE_TOKEN_IN
);
233 if ((ErrorCode
= Pipe_WaitUntilReady()))
238 if (!(Pipe_IsReadWriteAllowed()))
242 #if !defined(NO_STREAM_CALLBACKS)
243 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
244 return PIPE_RWSTREAM_CallbackAborted
;
247 if ((ErrorCode
= Pipe_WaitUntilReady()))
252 *(DataStream
++) = Pipe_Read_Byte();
257 return PIPE_RWSTREAM_NoError
;
260 uint8_t Pipe_Read_Stream_BE(void* Buffer
, uint16_t Length
261 #if !defined(NO_STREAM_CALLBACKS)
262 , StreamCallbackPtr_t Callback
266 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
269 Pipe_SetToken(PIPE_TOKEN_IN
);
271 if ((ErrorCode
= Pipe_WaitUntilReady()))
276 if (!(Pipe_IsReadWriteAllowed()))
280 #if !defined(NO_STREAM_CALLBACKS)
281 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
282 return PIPE_RWSTREAM_CallbackAborted
;
285 if ((ErrorCode
= Pipe_WaitUntilReady()))
290 *(DataStream
--) = Pipe_Read_Byte();
295 return PIPE_RWSTREAM_NoError
;