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
;
83 USB_INT_Clear(USB_INT_SOFI
);
87 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN
)
89 if (Endpoint_IsINReady())
90 return ENDPOINT_READYWAIT_NoError
;
94 if (Endpoint_IsOUTReceived())
95 return ENDPOINT_READYWAIT_NoError
;
98 if (!(USB_IsConnected
))
99 return ENDPOINT_READYWAIT_DeviceDisconnected
;
100 else if (Endpoint_IsStalled())
101 return ENDPOINT_READYWAIT_EndpointStalled
;
103 if (USB_INT_HasOccurred(USB_INT_SOFI
))
105 USB_INT_Clear(USB_INT_SOFI
);
107 if (!(TimeoutMSRem
--))
108 return ENDPOINT_READYWAIT_Timeout
;
113 uint8_t Endpoint_Discard_Stream(uint16_t Length
114 #if !defined(NO_STREAM_CALLBACKS)
115 , StreamCallbackPtr_t Callback
121 if ((ErrorCode
= Endpoint_WaitUntilReady()))
126 if (!(Endpoint_IsReadWriteAllowed()))
130 #if !defined(NO_STREAM_CALLBACKS)
131 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
132 return ENDPOINT_RWSTREAM_CallbackAborted
;
135 if ((ErrorCode
= Endpoint_WaitUntilReady()))
140 Endpoint_Discard_Byte();
145 return ENDPOINT_RWSTREAM_NoError
;
148 uint8_t Endpoint_Write_Stream_LE(const void* Buffer
, uint16_t Length
149 #if !defined(NO_STREAM_CALLBACKS)
150 , StreamCallbackPtr_t Callback
154 uint8_t* DataStream
= (uint8_t*)Buffer
;
157 if ((ErrorCode
= Endpoint_WaitUntilReady()))
162 if (!(Endpoint_IsReadWriteAllowed()))
166 #if !defined(NO_STREAM_CALLBACKS)
167 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
168 return ENDPOINT_RWSTREAM_CallbackAborted
;
171 if ((ErrorCode
= Endpoint_WaitUntilReady()))
176 Endpoint_Write_Byte(*(DataStream
++));
181 return ENDPOINT_RWSTREAM_NoError
;
184 uint8_t Endpoint_Write_Stream_BE(const void* Buffer
, uint16_t Length
185 #if !defined(NO_STREAM_CALLBACKS)
186 , StreamCallbackPtr_t Callback
190 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
193 if ((ErrorCode
= Endpoint_WaitUntilReady()))
198 if (!(Endpoint_IsReadWriteAllowed()))
202 #if !defined(NO_STREAM_CALLBACKS)
203 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
204 return ENDPOINT_RWSTREAM_CallbackAborted
;
207 if ((ErrorCode
= Endpoint_WaitUntilReady()))
212 Endpoint_Write_Byte(*(DataStream
--));
217 return ENDPOINT_RWSTREAM_NoError
;
220 uint8_t Endpoint_Read_Stream_LE(void* Buffer
, uint16_t Length
221 #if !defined(NO_STREAM_CALLBACKS)
222 , StreamCallbackPtr_t Callback
226 uint8_t* DataStream
= (uint8_t*)Buffer
;
229 if ((ErrorCode
= Endpoint_WaitUntilReady()))
234 if (!(Endpoint_IsReadWriteAllowed()))
238 #if !defined(NO_STREAM_CALLBACKS)
239 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
240 return ENDPOINT_RWSTREAM_CallbackAborted
;
243 if ((ErrorCode
= Endpoint_WaitUntilReady()))
248 *(DataStream
++) = Endpoint_Read_Byte();
253 return ENDPOINT_RWSTREAM_NoError
;
256 uint8_t Endpoint_Read_Stream_BE(void* Buffer
, uint16_t Length
257 #if !defined(NO_STREAM_CALLBACKS)
258 , StreamCallbackPtr_t Callback
262 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
265 if ((ErrorCode
= Endpoint_WaitUntilReady()))
270 if (!(Endpoint_IsReadWriteAllowed()))
274 #if !defined(NO_STREAM_CALLBACKS)
275 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
276 return ENDPOINT_RWSTREAM_CallbackAborted
;
279 if ((ErrorCode
= Endpoint_WaitUntilReady()))
284 *(DataStream
--) = Endpoint_Read_Byte();
289 return ENDPOINT_RWSTREAM_NoError
;
293 uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer
, uint16_t Length
)
295 uint8_t* DataStream
= (uint8_t*)Buffer
;
296 bool LastPacketFull
= false;
298 if (Length
> USB_ControlRequest
.wLength
)
299 Length
= USB_ControlRequest
.wLength
;
301 while (Length
&& !(Endpoint_IsOUTReceived()))
303 while (!(Endpoint_IsINReady()));
305 while (Length
&& (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize
))
307 Endpoint_Write_Byte(*(DataStream
++));
311 LastPacketFull
= (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize
);
315 if (Endpoint_IsOUTReceived())
316 return ENDPOINT_RWCSTREAM_HostAborted
;
320 while (!(Endpoint_IsINReady()));
324 while (!(Endpoint_IsOUTReceived()));
326 return ENDPOINT_RWCSTREAM_NoError
;
329 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer
, uint16_t Length
)
331 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
332 bool LastPacketFull
= false;
334 if (Length
> USB_ControlRequest
.wLength
)
335 Length
= USB_ControlRequest
.wLength
;
337 while (Length
&& !(Endpoint_IsOUTReceived()))
339 if (Endpoint_IsINReady())
341 while (Length
&& (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize
))
343 Endpoint_Write_Byte(*(DataStream
--));
347 LastPacketFull
= (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize
);
352 if (Endpoint_IsOUTReceived())
353 return ENDPOINT_RWCSTREAM_HostAborted
;
357 while (!(Endpoint_IsINReady()));
361 while (!(Endpoint_IsOUTReceived()));
363 return ENDPOINT_RWCSTREAM_NoError
;
366 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer
, uint16_t Length
)
368 uint8_t* DataStream
= (uint8_t*)Buffer
;
372 if (Endpoint_IsOUTReceived())
374 while (Length
&& Endpoint_BytesInEndpoint())
376 *(DataStream
++) = Endpoint_Read_Byte();
384 while (!(Endpoint_IsINReady()));
386 return ENDPOINT_RWCSTREAM_NoError
;
389 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer
, uint16_t Length
)
391 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
395 if (Endpoint_IsOUTReceived())
397 while (Length
&& Endpoint_BytesInEndpoint())
399 *(DataStream
--) = Endpoint_Read_Byte();
407 while (!(Endpoint_IsINReady()));
409 return ENDPOINT_RWCSTREAM_NoError
;