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 uint16_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
79 USB_INT_Clear(USB_INT_SOFI
);
83 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN
)
85 if (Endpoint_IsINReady())
86 return ENDPOINT_READYWAIT_NoError
;
90 if (Endpoint_IsOUTReceived())
91 return ENDPOINT_READYWAIT_NoError
;
94 if (!(USB_IsConnected
))
95 return ENDPOINT_READYWAIT_DeviceDisconnected
;
96 else if (Endpoint_IsStalled())
97 return ENDPOINT_READYWAIT_EndpointStalled
;
99 if (USB_INT_HasOccurred(USB_INT_SOFI
))
101 USB_INT_Clear(USB_INT_SOFI
);
103 if (!(TimeoutMSRem
--))
104 return ENDPOINT_READYWAIT_Timeout
;
109 uint8_t Endpoint_Discard_Stream(uint16_t Length
110 #if !defined(NO_STREAM_CALLBACKS)
111 , uint8_t (* const Callback
)(void)
117 if ((ErrorCode
= Endpoint_WaitUntilReady()))
122 if (!(Endpoint_IsReadWriteAllowed()))
126 #if !defined(NO_STREAM_CALLBACKS)
127 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
128 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
131 if ((ErrorCode
= Endpoint_WaitUntilReady()))
136 Endpoint_Discard_Byte();
140 return ENDPOINT_RWSTREAM_ERROR_NoError
;
143 uint8_t Endpoint_Write_Stream_LE(const void* Buffer
, uint16_t Length
144 #if !defined(NO_STREAM_CALLBACKS)
145 , uint8_t (* const Callback
)(void)
149 uint8_t* DataStream
= (uint8_t*)Buffer
;
152 if ((ErrorCode
= Endpoint_WaitUntilReady()))
157 if (!(Endpoint_IsReadWriteAllowed()))
161 #if !defined(NO_STREAM_CALLBACKS)
162 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
163 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
166 if ((ErrorCode
= Endpoint_WaitUntilReady()))
171 Endpoint_Write_Byte(*(DataStream
++));
175 return ENDPOINT_RWSTREAM_ERROR_NoError
;
178 uint8_t Endpoint_Write_Stream_BE(const void* Buffer
, uint16_t Length
179 #if !defined(NO_STREAM_CALLBACKS)
180 , uint8_t (* const Callback
)(void)
184 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
187 if ((ErrorCode
= Endpoint_WaitUntilReady()))
192 if (!(Endpoint_IsReadWriteAllowed()))
196 #if !defined(NO_STREAM_CALLBACKS)
197 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
198 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
201 if ((ErrorCode
= Endpoint_WaitUntilReady()))
206 Endpoint_Write_Byte(*(DataStream
--));
210 return ENDPOINT_RWSTREAM_ERROR_NoError
;
213 uint8_t Endpoint_Read_Stream_LE(void* Buffer
, uint16_t Length
214 #if !defined(NO_STREAM_CALLBACKS)
215 , uint8_t (* const Callback
)(void)
219 uint8_t* DataStream
= (uint8_t*)Buffer
;
222 if ((ErrorCode
= Endpoint_WaitUntilReady()))
227 if (!(Endpoint_IsReadWriteAllowed()))
231 #if !defined(NO_STREAM_CALLBACKS)
232 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
233 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
236 if ((ErrorCode
= Endpoint_WaitUntilReady()))
241 *(DataStream
++) = Endpoint_Read_Byte();
245 return ENDPOINT_RWSTREAM_ERROR_NoError
;
248 uint8_t Endpoint_Read_Stream_BE(void* Buffer
, uint16_t Length
249 #if !defined(NO_STREAM_CALLBACKS)
250 , uint8_t (* const Callback
)(void)
254 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
257 if ((ErrorCode
= Endpoint_WaitUntilReady()))
262 if (!(Endpoint_IsReadWriteAllowed()))
266 #if !defined(NO_STREAM_CALLBACKS)
267 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
268 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
271 if ((ErrorCode
= Endpoint_WaitUntilReady()))
276 *(DataStream
--) = Endpoint_Read_Byte();
280 return ENDPOINT_RWSTREAM_ERROR_NoError
;
284 uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer
, uint16_t Length
)
286 uint8_t* DataStream
= (uint8_t*)Buffer
;
287 bool LastPacketFull
= false;
288 bool ShortTransfer
= (Length
< USB_ControlRequest
.wLength
);
290 while (Length
&& !(Endpoint_IsOUTReceived()))
292 while (!(Endpoint_IsINReady()));
294 while (Length
&& (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize
))
296 Endpoint_Write_Byte(*(DataStream
++));
301 LastPacketFull
= (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize
);
305 if (Endpoint_IsOUTReceived())
306 return ENDPOINT_RWCSTREAM_ERROR_HostAborted
;
308 if (LastPacketFull
|| ShortTransfer
)
310 while (!(Endpoint_IsINReady()));
314 while (!(Endpoint_IsOUTReceived()));
316 return ENDPOINT_RWCSTREAM_ERROR_NoError
;
319 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer
, uint16_t Length
)
321 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
322 bool LastPacketFull
= false;
323 bool ShortTransfer
= (Length
< USB_ControlRequest
.wLength
);
325 while (Length
&& !(Endpoint_IsOUTReceived()))
327 while (!(Endpoint_IsINReady()));
329 while (Length
&& (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize
))
331 Endpoint_Write_Byte(*(DataStream
--));
336 LastPacketFull
= (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize
);
340 if (Endpoint_IsOUTReceived())
341 return ENDPOINT_RWCSTREAM_ERROR_HostAborted
;
343 if (LastPacketFull
|| ShortTransfer
)
345 while (!(Endpoint_IsINReady()));
349 while (!(Endpoint_IsOUTReceived()));
351 return ENDPOINT_RWCSTREAM_ERROR_NoError
;
354 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer
, uint16_t Length
)
356 uint8_t* DataStream
= (uint8_t*)Buffer
;
360 while (!(Endpoint_IsOUTReceived()));
362 while (Length
&& Endpoint_BytesInEndpoint())
364 *(DataStream
++) = Endpoint_Read_Byte();
372 while (!(Endpoint_IsINReady()));
374 return ENDPOINT_RWCSTREAM_ERROR_NoError
;
377 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer
, uint16_t Length
)
379 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
383 while (!(Endpoint_IsOUTReceived()));
385 while (Length
&& Endpoint_BytesInEndpoint())
387 *(DataStream
--) = Endpoint_Read_Byte();
395 while (!(Endpoint_IsINReady()));
397 return ENDPOINT_RWCSTREAM_ERROR_NoError
;