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_DEVICE)
35 #define INCLUDE_FROM_ENDPOINT_C
38 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
39 uint8_t USB_ControlEndpointSize
= ENDPOINT_CONTROLEP_DEFAULT_SIZE
;
42 uint8_t Endpoint_BytesToEPSizeMaskDynamic(const uint16_t Size
)
44 return Endpoint_BytesToEPSizeMask(Size
);
47 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number
, const uint8_t UECFG0XData
, const uint8_t UECFG1XData
)
49 Endpoint_SelectEndpoint(Number
);
50 Endpoint_EnableEndpoint();
54 UECFG0X
= UECFG0XData
;
55 UECFG1X
= UECFG1XData
;
57 return Endpoint_IsConfigured();
60 void Endpoint_ClearEndpoints(void)
64 for (uint8_t EPNum
= 0; EPNum
< ENDPOINT_TOTAL_ENDPOINTS
; EPNum
++)
66 Endpoint_SelectEndpoint(EPNum
);
69 Endpoint_DeallocateMemory();
70 Endpoint_DisableEndpoint();
74 #if !defined(CONTROL_ONLY_DEVICE)
75 uint8_t Endpoint_WaitUntilReady(void)
77 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
78 uint8_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
80 uint16_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
85 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN
)
87 if (Endpoint_IsINReady())
88 return ENDPOINT_READYWAIT_NoError
;
92 if (Endpoint_IsOUTReceived())
93 return ENDPOINT_READYWAIT_NoError
;
96 if (!(USB_IsConnected
))
97 return ENDPOINT_READYWAIT_DeviceDisconnected
;
98 else if (Endpoint_IsStalled())
99 return ENDPOINT_READYWAIT_EndpointStalled
;
101 if (USB_INT_HasOccurred(USB_INT_SOFI
))
103 USB_INT_Clear(USB_INT_SOFI
);
105 if (!(TimeoutMSRem
--))
106 return ENDPOINT_READYWAIT_Timeout
;
111 uint8_t Endpoint_Discard_Stream(uint16_t Length
112 #if !defined(NO_STREAM_CALLBACKS)
113 , StreamCallbackPtr_t Callback
119 if ((ErrorCode
= Endpoint_WaitUntilReady()))
124 if (!(Endpoint_IsReadWriteAllowed()))
128 #if !defined(NO_STREAM_CALLBACKS)
129 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
130 return ENDPOINT_RWSTREAM_CallbackAborted
;
133 if ((ErrorCode
= Endpoint_WaitUntilReady()))
138 Endpoint_Discard_Byte();
143 return ENDPOINT_RWSTREAM_NoError
;
146 uint8_t Endpoint_Write_Stream_LE(const void* Buffer
, uint16_t Length
147 #if !defined(NO_STREAM_CALLBACKS)
148 , StreamCallbackPtr_t Callback
152 uint8_t* DataStream
= (uint8_t*)Buffer
;
155 if ((ErrorCode
= Endpoint_WaitUntilReady()))
160 if (!(Endpoint_IsReadWriteAllowed()))
164 #if !defined(NO_STREAM_CALLBACKS)
165 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
166 return ENDPOINT_RWSTREAM_CallbackAborted
;
169 if ((ErrorCode
= Endpoint_WaitUntilReady()))
174 Endpoint_Write_Byte(*(DataStream
++));
179 return ENDPOINT_RWSTREAM_NoError
;
182 uint8_t Endpoint_Write_Stream_BE(const void* Buffer
, uint16_t Length
183 #if !defined(NO_STREAM_CALLBACKS)
184 , StreamCallbackPtr_t Callback
188 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
191 if ((ErrorCode
= Endpoint_WaitUntilReady()))
196 if (!(Endpoint_IsReadWriteAllowed()))
200 #if !defined(NO_STREAM_CALLBACKS)
201 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
202 return ENDPOINT_RWSTREAM_CallbackAborted
;
205 if ((ErrorCode
= Endpoint_WaitUntilReady()))
210 Endpoint_Write_Byte(*(DataStream
--));
215 return ENDPOINT_RWSTREAM_NoError
;
218 uint8_t Endpoint_Read_Stream_LE(void* Buffer
, uint16_t Length
219 #if !defined(NO_STREAM_CALLBACKS)
220 , StreamCallbackPtr_t Callback
224 uint8_t* DataStream
= (uint8_t*)Buffer
;
227 if ((ErrorCode
= Endpoint_WaitUntilReady()))
232 if (!(Endpoint_IsReadWriteAllowed()))
236 #if !defined(NO_STREAM_CALLBACKS)
237 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
238 return ENDPOINT_RWSTREAM_CallbackAborted
;
241 if ((ErrorCode
= Endpoint_WaitUntilReady()))
246 *(DataStream
++) = Endpoint_Read_Byte();
251 return ENDPOINT_RWSTREAM_NoError
;
254 uint8_t Endpoint_Read_Stream_BE(void* Buffer
, uint16_t Length
255 #if !defined(NO_STREAM_CALLBACKS)
256 , StreamCallbackPtr_t Callback
260 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
263 if ((ErrorCode
= Endpoint_WaitUntilReady()))
268 if (!(Endpoint_IsReadWriteAllowed()))
272 #if !defined(NO_STREAM_CALLBACKS)
273 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
274 return ENDPOINT_RWSTREAM_CallbackAborted
;
277 if ((ErrorCode
= Endpoint_WaitUntilReady()))
282 *(DataStream
--) = Endpoint_Read_Byte();
287 return ENDPOINT_RWSTREAM_NoError
;
291 uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer
, uint16_t Length
)
293 uint8_t* DataStream
= (uint8_t*)Buffer
;
294 bool LastPacketFull
= false;
296 if (Length
> USB_ControlRequest
.wLength
)
297 Length
= USB_ControlRequest
.wLength
;
299 while (Length
&& !(Endpoint_IsOUTReceived()))
301 while (!(Endpoint_IsINReady()));
303 while (Length
&& (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize
))
305 Endpoint_Write_Byte(*(DataStream
++));
309 LastPacketFull
= (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize
);
313 if (Endpoint_IsOUTReceived())
314 return ENDPOINT_RWCSTREAM_HostAborted
;
318 while (!(Endpoint_IsINReady()));
322 while (!(Endpoint_IsOUTReceived()));
324 return ENDPOINT_RWCSTREAM_NoError
;
327 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer
, uint16_t Length
)
329 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
330 bool LastPacketFull
= false;
332 if (Length
> USB_ControlRequest
.wLength
)
333 Length
= USB_ControlRequest
.wLength
;
335 while (Length
&& !(Endpoint_IsOUTReceived()))
337 if (Endpoint_IsINReady())
339 while (Length
&& (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize
))
341 Endpoint_Write_Byte(*(DataStream
--));
345 LastPacketFull
= (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize
);
350 if (Endpoint_IsOUTReceived())
351 return ENDPOINT_RWCSTREAM_HostAborted
;
355 while (!(Endpoint_IsINReady()));
359 while (!(Endpoint_IsOUTReceived()));
361 return ENDPOINT_RWCSTREAM_NoError
;
364 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer
, uint16_t Length
)
366 uint8_t* DataStream
= (uint8_t*)Buffer
;
370 if (Endpoint_IsOUTReceived())
372 while (Length
&& Endpoint_BytesInEndpoint())
374 *(DataStream
++) = Endpoint_Read_Byte();
382 while (!(Endpoint_IsINReady()));
384 return ENDPOINT_RWCSTREAM_NoError
;
387 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer
, uint16_t Length
)
389 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
393 if (Endpoint_IsOUTReceived())
395 while (Length
&& Endpoint_BytesInEndpoint())
397 *(DataStream
--) = Endpoint_Read_Byte();
405 while (!(Endpoint_IsINReady()));
407 return ENDPOINT_RWCSTREAM_NoError
;