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 #if !defined(STATIC_ENDPOINT_CONFIGURATION)
43 bool Endpoint_ConfigureEndpoint(const uint8_t Number
, const uint8_t Type
, const uint8_t Direction
,
44 const uint16_t Size
, const uint8_t Banks
)
46 Endpoint_SelectEndpoint(Number
);
47 Endpoint_EnableEndpoint();
51 UECFG0X
= ((Type
<< EPTYPE0
) | Direction
);
52 UECFG1X
= ((1 << ALLOC
) | Banks
| Endpoint_BytesToEPSizeMask(Size
));
54 return Endpoint_IsConfigured();
57 bool Endpoint_ConfigureEndpointStatic(const uint8_t Number
, const uint8_t UECFG0XData
, const uint8_t UECFG1XData
)
59 Endpoint_SelectEndpoint(Number
);
60 Endpoint_EnableEndpoint();
64 UECFG0X
= UECFG0XData
;
65 UECFG1X
= UECFG1XData
;
67 return Endpoint_IsConfigured();
71 void Endpoint_ClearEndpoints(void)
75 for (uint8_t EPNum
= 0; EPNum
< ENDPOINT_TOTAL_ENDPOINTS
; EPNum
++)
77 Endpoint_SelectEndpoint(EPNum
);
80 Endpoint_DeallocateMemory();
81 Endpoint_DisableEndpoint();
85 #if !defined(CONTROL_ONLY_DEVICE)
86 uint8_t Endpoint_WaitUntilReady(void)
88 uint16_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
90 USB_INT_Clear(USB_INT_SOFI
);
94 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN
)
96 if (Endpoint_IsINReady())
97 return ENDPOINT_READYWAIT_NoError
;
101 if (Endpoint_IsOUTReceived())
102 return ENDPOINT_READYWAIT_NoError
;
105 if (!(USB_IsConnected
))
106 return ENDPOINT_READYWAIT_DeviceDisconnected
;
107 else if (Endpoint_IsStalled())
108 return ENDPOINT_READYWAIT_EndpointStalled
;
110 if (USB_INT_HasOccurred(USB_INT_SOFI
))
112 USB_INT_Clear(USB_INT_SOFI
);
114 if (!(TimeoutMSRem
--))
115 return ENDPOINT_READYWAIT_Timeout
;
120 uint8_t Endpoint_Discard_Stream(uint16_t Length
121 #if !defined(NO_STREAM_CALLBACKS)
122 , uint8_t (* const Callback
)(void)
128 if ((ErrorCode
= Endpoint_WaitUntilReady()))
133 if (!(Endpoint_IsReadWriteAllowed()))
137 #if !defined(NO_STREAM_CALLBACKS)
138 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
139 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
142 if ((ErrorCode
= Endpoint_WaitUntilReady()))
147 Endpoint_Discard_Byte();
151 return ENDPOINT_RWSTREAM_ERROR_NoError
;
154 uint8_t Endpoint_Write_Stream_LE(const void* Buffer
, uint16_t Length
155 #if !defined(NO_STREAM_CALLBACKS)
156 , uint8_t (* const Callback
)(void)
160 uint8_t* DataStream
= (uint8_t*)Buffer
;
163 if ((ErrorCode
= Endpoint_WaitUntilReady()))
168 if (!(Endpoint_IsReadWriteAllowed()))
172 #if !defined(NO_STREAM_CALLBACKS)
173 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
174 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
177 if ((ErrorCode
= Endpoint_WaitUntilReady()))
182 Endpoint_Write_Byte(*(DataStream
++));
186 return ENDPOINT_RWSTREAM_ERROR_NoError
;
189 uint8_t Endpoint_Write_Stream_BE(const void* Buffer
, uint16_t Length
190 #if !defined(NO_STREAM_CALLBACKS)
191 , uint8_t (* const Callback
)(void)
195 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
198 if ((ErrorCode
= Endpoint_WaitUntilReady()))
203 if (!(Endpoint_IsReadWriteAllowed()))
207 #if !defined(NO_STREAM_CALLBACKS)
208 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
209 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
212 if ((ErrorCode
= Endpoint_WaitUntilReady()))
217 Endpoint_Write_Byte(*(DataStream
--));
221 return ENDPOINT_RWSTREAM_ERROR_NoError
;
224 uint8_t Endpoint_Read_Stream_LE(void* Buffer
, uint16_t Length
225 #if !defined(NO_STREAM_CALLBACKS)
226 , uint8_t (* const Callback
)(void)
230 uint8_t* DataStream
= (uint8_t*)Buffer
;
233 if ((ErrorCode
= Endpoint_WaitUntilReady()))
238 if (!(Endpoint_IsReadWriteAllowed()))
242 #if !defined(NO_STREAM_CALLBACKS)
243 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
244 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
247 if ((ErrorCode
= Endpoint_WaitUntilReady()))
252 *(DataStream
++) = Endpoint_Read_Byte();
256 return ENDPOINT_RWSTREAM_ERROR_NoError
;
259 uint8_t Endpoint_Read_Stream_BE(void* Buffer
, uint16_t Length
260 #if !defined(NO_STREAM_CALLBACKS)
261 , uint8_t (* const Callback
)(void)
265 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
268 if ((ErrorCode
= Endpoint_WaitUntilReady()))
273 if (!(Endpoint_IsReadWriteAllowed()))
277 #if !defined(NO_STREAM_CALLBACKS)
278 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
279 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
282 if ((ErrorCode
= Endpoint_WaitUntilReady()))
287 *(DataStream
--) = Endpoint_Read_Byte();
291 return ENDPOINT_RWSTREAM_ERROR_NoError
;
295 uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer
, uint16_t Length
)
297 uint8_t* DataStream
= (uint8_t*)Buffer
;
298 bool LastPacketFull
= false;
299 bool ShortTransfer
= (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
++));
312 LastPacketFull
= (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize
);
316 if (Endpoint_IsOUTReceived())
317 return ENDPOINT_RWCSTREAM_ERROR_HostAborted
;
319 if (LastPacketFull
|| ShortTransfer
)
321 while (!(Endpoint_IsINReady()));
325 while (!(Endpoint_IsOUTReceived()));
327 return ENDPOINT_RWCSTREAM_ERROR_NoError
;
330 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer
, uint16_t Length
)
332 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
333 bool LastPacketFull
= false;
334 bool ShortTransfer
= (Length
< USB_ControlRequest
.wLength
);
336 while (Length
&& !(Endpoint_IsOUTReceived()))
338 while (!(Endpoint_IsINReady()));
340 while (Length
&& (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize
))
342 Endpoint_Write_Byte(*(DataStream
--));
347 LastPacketFull
= (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize
);
351 if (Endpoint_IsOUTReceived())
352 return ENDPOINT_RWCSTREAM_ERROR_HostAborted
;
354 if (LastPacketFull
|| ShortTransfer
)
356 while (!(Endpoint_IsINReady()));
360 while (!(Endpoint_IsOUTReceived()));
362 return ENDPOINT_RWCSTREAM_ERROR_NoError
;
365 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer
, uint16_t Length
)
367 uint8_t* DataStream
= (uint8_t*)Buffer
;
371 while (!(Endpoint_IsOUTReceived()));
373 while (Length
&& Endpoint_BytesInEndpoint())
375 *(DataStream
++) = Endpoint_Read_Byte();
383 while (!(Endpoint_IsINReady()));
385 return ENDPOINT_RWCSTREAM_ERROR_NoError
;
388 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer
, uint16_t Length
)
390 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
394 while (!(Endpoint_IsOUTReceived()));
396 while (Length
&& Endpoint_BytesInEndpoint())
398 *(DataStream
--) = Endpoint_Read_Byte();
406 while (!(Endpoint_IsINReady()));
408 return ENDPOINT_RWCSTREAM_ERROR_NoError
;