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();
141 return ENDPOINT_RWSTREAM_ERROR_NoError
;
144 uint8_t Endpoint_Write_Stream_LE(const void* Buffer
, uint16_t Length
145 #if !defined(NO_STREAM_CALLBACKS)
146 , uint8_t (* const Callback
)(void)
150 uint8_t* DataStream
= (uint8_t*)Buffer
;
153 if ((ErrorCode
= Endpoint_WaitUntilReady()))
158 if (!(Endpoint_IsReadWriteAllowed()))
162 #if !defined(NO_STREAM_CALLBACKS)
163 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
164 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
167 if ((ErrorCode
= Endpoint_WaitUntilReady()))
172 Endpoint_Write_Byte(*(DataStream
++));
177 return ENDPOINT_RWSTREAM_ERROR_NoError
;
180 uint8_t Endpoint_Write_Stream_BE(const void* Buffer
, uint16_t Length
181 #if !defined(NO_STREAM_CALLBACKS)
182 , uint8_t (* const Callback
)(void)
186 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
189 if ((ErrorCode
= Endpoint_WaitUntilReady()))
194 if (!(Endpoint_IsReadWriteAllowed()))
198 #if !defined(NO_STREAM_CALLBACKS)
199 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
200 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
203 if ((ErrorCode
= Endpoint_WaitUntilReady()))
208 Endpoint_Write_Byte(*(DataStream
--));
213 return ENDPOINT_RWSTREAM_ERROR_NoError
;
216 uint8_t Endpoint_Read_Stream_LE(void* Buffer
, uint16_t Length
217 #if !defined(NO_STREAM_CALLBACKS)
218 , uint8_t (* const Callback
)(void)
222 uint8_t* DataStream
= (uint8_t*)Buffer
;
225 if ((ErrorCode
= Endpoint_WaitUntilReady()))
230 if (!(Endpoint_IsReadWriteAllowed()))
234 #if !defined(NO_STREAM_CALLBACKS)
235 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
236 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
239 if ((ErrorCode
= Endpoint_WaitUntilReady()))
244 *(DataStream
++) = Endpoint_Read_Byte();
249 return ENDPOINT_RWSTREAM_ERROR_NoError
;
252 uint8_t Endpoint_Read_Stream_BE(void* Buffer
, uint16_t Length
253 #if !defined(NO_STREAM_CALLBACKS)
254 , uint8_t (* const Callback
)(void)
258 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
261 if ((ErrorCode
= Endpoint_WaitUntilReady()))
266 if (!(Endpoint_IsReadWriteAllowed()))
270 #if !defined(NO_STREAM_CALLBACKS)
271 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
272 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
275 if ((ErrorCode
= Endpoint_WaitUntilReady()))
280 *(DataStream
--) = Endpoint_Read_Byte();
285 return ENDPOINT_RWSTREAM_ERROR_NoError
;
289 uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer
, uint16_t Length
)
291 uint8_t* DataStream
= (uint8_t*)Buffer
;
292 bool LastPacketFull
= false;
293 bool ShortTransfer
= (Length
< USB_ControlRequest
.wLength
);
295 while (Length
&& !(Endpoint_IsOUTReceived()))
297 while (!(Endpoint_IsINReady()));
299 while (Length
&& (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize
))
301 Endpoint_Write_Byte(*(DataStream
++));
305 LastPacketFull
= (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize
);
309 if (Endpoint_IsOUTReceived())
310 return ENDPOINT_RWCSTREAM_ERROR_HostAborted
;
312 if (LastPacketFull
|| ShortTransfer
)
314 while (!(Endpoint_IsINReady()));
318 while (!(Endpoint_IsOUTReceived()));
320 return ENDPOINT_RWCSTREAM_ERROR_NoError
;
323 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer
, uint16_t Length
)
325 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
326 bool LastPacketFull
= false;
327 bool ShortTransfer
= (Length
< USB_ControlRequest
.wLength
);
329 while (Length
&& !(Endpoint_IsOUTReceived()))
331 while (!(Endpoint_IsINReady()));
333 while (Length
&& (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize
))
335 Endpoint_Write_Byte(*(DataStream
--));
339 LastPacketFull
= (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize
);
343 if (Endpoint_IsOUTReceived())
344 return ENDPOINT_RWCSTREAM_ERROR_HostAborted
;
346 if (LastPacketFull
|| ShortTransfer
)
348 while (!(Endpoint_IsINReady()));
352 while (!(Endpoint_IsOUTReceived()));
354 return ENDPOINT_RWCSTREAM_ERROR_NoError
;
357 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer
, uint16_t Length
)
359 uint8_t* DataStream
= (uint8_t*)Buffer
;
363 while (!(Endpoint_IsOUTReceived()));
365 while (Length
&& Endpoint_BytesInEndpoint())
367 *(DataStream
++) = Endpoint_Read_Byte();
374 while (!(Endpoint_IsINReady()));
376 return ENDPOINT_RWCSTREAM_ERROR_NoError
;
379 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer
, uint16_t Length
)
381 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
385 while (!(Endpoint_IsOUTReceived()));
387 while (Length
&& Endpoint_BytesInEndpoint())
389 *(DataStream
--) = Endpoint_Read_Byte();
396 while (!(Endpoint_IsINReady()));
398 return ENDPOINT_RWCSTREAM_ERROR_NoError
;