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 uint8_t Endpoint_WaitUntilReady(void)
87 uint16_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
89 USB_INT_Clear(USB_INT_SOFI
);
93 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN
)
95 if (Endpoint_IsINReady())
96 return ENDPOINT_READYWAIT_NoError
;
100 if (Endpoint_IsOUTReceived())
101 return ENDPOINT_READYWAIT_NoError
;
104 if (!(USB_IsConnected
))
105 return ENDPOINT_READYWAIT_DeviceDisconnected
;
106 else if (Endpoint_IsStalled())
107 return ENDPOINT_READYWAIT_EndpointStalled
;
109 if (USB_INT_HasOccurred(USB_INT_SOFI
))
111 USB_INT_Clear(USB_INT_SOFI
);
113 if (!(TimeoutMSRem
--))
114 return ENDPOINT_READYWAIT_Timeout
;
119 uint8_t Endpoint_Discard_Stream(uint16_t Length
120 #if !defined(NO_STREAM_CALLBACKS)
121 , uint8_t (* const Callback
)(void)
127 if ((ErrorCode
= Endpoint_WaitUntilReady()))
132 if (!(Endpoint_IsReadWriteAllowed()))
136 #if !defined(NO_STREAM_CALLBACKS)
137 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
138 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
141 if ((ErrorCode
= Endpoint_WaitUntilReady()))
146 Endpoint_Discard_Byte();
150 return ENDPOINT_RWSTREAM_ERROR_NoError
;
153 uint8_t Endpoint_Write_Stream_LE(const void* Buffer
, uint16_t Length
154 #if !defined(NO_STREAM_CALLBACKS)
155 , uint8_t (* const Callback
)(void)
159 uint8_t* DataStream
= (uint8_t*)Buffer
;
162 if ((ErrorCode
= Endpoint_WaitUntilReady()))
167 if (!(Endpoint_IsReadWriteAllowed()))
171 #if !defined(NO_STREAM_CALLBACKS)
172 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
173 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
176 if ((ErrorCode
= Endpoint_WaitUntilReady()))
181 Endpoint_Write_Byte(*(DataStream
++));
185 return ENDPOINT_RWSTREAM_ERROR_NoError
;
188 uint8_t Endpoint_Write_Stream_BE(const void* Buffer
, uint16_t Length
189 #if !defined(NO_STREAM_CALLBACKS)
190 , uint8_t (* const Callback
)(void)
194 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
197 if ((ErrorCode
= Endpoint_WaitUntilReady()))
202 if (!(Endpoint_IsReadWriteAllowed()))
206 #if !defined(NO_STREAM_CALLBACKS)
207 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
208 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
211 if ((ErrorCode
= Endpoint_WaitUntilReady()))
216 Endpoint_Write_Byte(*(DataStream
--));
220 return ENDPOINT_RWSTREAM_ERROR_NoError
;
223 uint8_t Endpoint_Read_Stream_LE(void* Buffer
, uint16_t Length
224 #if !defined(NO_STREAM_CALLBACKS)
225 , uint8_t (* const Callback
)(void)
229 uint8_t* DataStream
= (uint8_t*)Buffer
;
232 if ((ErrorCode
= Endpoint_WaitUntilReady()))
237 if (!(Endpoint_IsReadWriteAllowed()))
241 #if !defined(NO_STREAM_CALLBACKS)
242 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
243 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
246 if ((ErrorCode
= Endpoint_WaitUntilReady()))
251 *(DataStream
++) = Endpoint_Read_Byte();
255 return ENDPOINT_RWSTREAM_ERROR_NoError
;
258 uint8_t Endpoint_Read_Stream_BE(void* Buffer
, uint16_t Length
259 #if !defined(NO_STREAM_CALLBACKS)
260 , uint8_t (* const Callback
)(void)
264 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
267 if ((ErrorCode
= Endpoint_WaitUntilReady()))
272 if (!(Endpoint_IsReadWriteAllowed()))
276 #if !defined(NO_STREAM_CALLBACKS)
277 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
278 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
281 if ((ErrorCode
= Endpoint_WaitUntilReady()))
286 *(DataStream
--) = Endpoint_Read_Byte();
290 return ENDPOINT_RWSTREAM_ERROR_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;
297 bool ShortTransfer
= (Length
< USB_ControlRequest
.wLength
);
299 while (Length
&& !(Endpoint_IsOUTReceived()))
301 while (!(Endpoint_IsINReady()));
303 while (Length
&& (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize
))
305 Endpoint_Write_Byte(*(DataStream
++));
310 LastPacketFull
= (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize
);
314 if (Endpoint_IsOUTReceived())
315 return ENDPOINT_RWCSTREAM_ERROR_HostAborted
;
317 if (LastPacketFull
|| ShortTransfer
)
319 while (!(Endpoint_IsINReady()));
323 while (!(Endpoint_IsOUTReceived()));
325 return ENDPOINT_RWCSTREAM_ERROR_NoError
;
328 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer
, uint16_t Length
)
330 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
331 bool LastPacketFull
= false;
332 bool ShortTransfer
= (Length
< USB_ControlRequest
.wLength
);
334 while (Length
&& !(Endpoint_IsOUTReceived()))
336 while (!(Endpoint_IsINReady()));
338 while (Length
&& (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize
))
340 Endpoint_Write_Byte(*(DataStream
--));
345 LastPacketFull
= (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize
);
349 if (Endpoint_IsOUTReceived())
350 return ENDPOINT_RWCSTREAM_ERROR_HostAborted
;
352 if (LastPacketFull
|| ShortTransfer
)
354 while (!(Endpoint_IsINReady()));
358 while (!(Endpoint_IsOUTReceived()));
360 return ENDPOINT_RWCSTREAM_ERROR_NoError
;
363 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer
, uint16_t Length
)
365 uint8_t* DataStream
= (uint8_t*)Buffer
;
369 while (!(Endpoint_IsOUTReceived()));
371 while (Length
&& Endpoint_BytesInEndpoint())
373 *(DataStream
++) = Endpoint_Read_Byte();
381 while (!(Endpoint_IsINReady()));
383 return ENDPOINT_RWCSTREAM_ERROR_NoError
;
386 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer
, uint16_t Length
)
388 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
392 while (!(Endpoint_IsOUTReceived()));
394 while (Length
&& Endpoint_BytesInEndpoint())
396 *(DataStream
--) = Endpoint_Read_Byte();
404 while (!(Endpoint_IsINReady()));
406 return ENDPOINT_RWCSTREAM_ERROR_NoError
;