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;
294 if (Length
> USB_ControlRequest
.wLength
)
295 Length
= USB_ControlRequest
.wLength
;
297 while (Length
&& !(Endpoint_IsOUTReceived()))
299 while (!(Endpoint_IsINReady()));
301 while (Length
&& (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize
))
303 Endpoint_Write_Byte(*(DataStream
++));
307 LastPacketFull
= (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize
);
311 if (Endpoint_IsOUTReceived())
312 return ENDPOINT_RWCSTREAM_ERROR_HostAborted
;
316 while (!(Endpoint_IsINReady()));
320 while (!(Endpoint_IsOUTReceived()));
322 return ENDPOINT_RWCSTREAM_ERROR_NoError
;
325 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer
, uint16_t Length
)
327 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
328 bool LastPacketFull
= false;
330 if (Length
> USB_ControlRequest
.wLength
)
331 Length
= USB_ControlRequest
.wLength
;
333 while (Length
&& !(Endpoint_IsOUTReceived()))
335 if (Endpoint_IsINReady())
337 while (Length
&& (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize
))
339 Endpoint_Write_Byte(*(DataStream
--));
343 LastPacketFull
= (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize
);
348 if (Endpoint_IsOUTReceived())
349 return ENDPOINT_RWCSTREAM_ERROR_HostAborted
;
353 while (!(Endpoint_IsINReady()));
357 while (!(Endpoint_IsOUTReceived()));
359 return ENDPOINT_RWCSTREAM_ERROR_NoError
;
362 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer
, uint16_t Length
)
364 uint8_t* DataStream
= (uint8_t*)Buffer
;
368 if (Endpoint_IsOUTReceived())
370 while (Length
&& Endpoint_BytesInEndpoint())
372 *(DataStream
++) = Endpoint_Read_Byte();
380 while (!(Endpoint_IsINReady()));
382 return ENDPOINT_RWCSTREAM_ERROR_NoError
;
385 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer
, uint16_t Length
)
387 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
391 if (Endpoint_IsOUTReceived())
393 while (Length
&& Endpoint_BytesInEndpoint())
395 *(DataStream
--) = Endpoint_Read_Byte();
403 while (!(Endpoint_IsINReady()));
405 return ENDPOINT_RWCSTREAM_ERROR_NoError
;