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
);
91 while (!(Endpoint_ReadWriteAllowed()))
93 if (!(USB_IsConnected
))
94 return ENDPOINT_READYWAIT_DeviceDisconnected
;
95 else if (Endpoint_IsStalled())
96 return ENDPOINT_READYWAIT_EndpointStalled
;
98 if (USB_INT_HasOccurred(USB_INT_SOFI
))
100 USB_INT_Clear(USB_INT_SOFI
);
102 if (!(TimeoutMSRem
--))
103 return ENDPOINT_READYWAIT_Timeout
;
107 return ENDPOINT_READYWAIT_NoError
;
110 uint8_t Endpoint_Discard_Stream(uint16_t Length
111 #if !defined(NO_STREAM_CALLBACKS)
112 , uint8_t (* const Callback
)(void)
118 if ((ErrorCode
= Endpoint_WaitUntilReady()))
123 if (!(Endpoint_ReadWriteAllowed()))
125 Endpoint_ClearCurrentBank();
127 #if !defined(NO_STREAM_CALLBACKS)
128 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
129 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
132 if ((ErrorCode
= Endpoint_WaitUntilReady()))
136 Endpoint_Discard_Byte();
139 return ENDPOINT_RWSTREAM_ERROR_NoError
;
142 uint8_t Endpoint_Write_Stream_LE(const void* Buffer
, uint16_t Length
143 #if !defined(NO_STREAM_CALLBACKS)
144 , uint8_t (* const Callback
)(void)
148 uint8_t* DataStream
= (uint8_t*)Buffer
;
151 if ((ErrorCode
= Endpoint_WaitUntilReady()))
156 if (!(Endpoint_ReadWriteAllowed()))
158 Endpoint_ClearCurrentBank();
160 #if !defined(NO_STREAM_CALLBACKS)
161 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
162 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
165 if ((ErrorCode
= Endpoint_WaitUntilReady()))
169 Endpoint_Write_Byte(*(DataStream
++));
172 return ENDPOINT_RWSTREAM_ERROR_NoError
;
175 uint8_t Endpoint_Write_Stream_BE(const void* Buffer
, uint16_t Length
176 #if !defined(NO_STREAM_CALLBACKS)
177 , uint8_t (* const Callback
)(void)
181 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
184 if ((ErrorCode
= Endpoint_WaitUntilReady()))
189 if (!(Endpoint_ReadWriteAllowed()))
191 Endpoint_ClearCurrentBank();
193 #if !defined(NO_STREAM_CALLBACKS)
194 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
195 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
198 if ((ErrorCode
= Endpoint_WaitUntilReady()))
202 Endpoint_Write_Byte(*(DataStream
--));
205 return ENDPOINT_RWSTREAM_ERROR_NoError
;
208 uint8_t Endpoint_Read_Stream_LE(void* Buffer
, uint16_t Length
209 #if !defined(NO_STREAM_CALLBACKS)
210 , uint8_t (* const Callback
)(void)
214 uint8_t* DataStream
= (uint8_t*)Buffer
;
217 if ((ErrorCode
= Endpoint_WaitUntilReady()))
222 if (!(Endpoint_ReadWriteAllowed()))
224 Endpoint_ClearCurrentBank();
226 #if !defined(NO_STREAM_CALLBACKS)
227 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
228 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
231 if ((ErrorCode
= Endpoint_WaitUntilReady()))
235 *(DataStream
++) = Endpoint_Read_Byte();
238 return ENDPOINT_RWSTREAM_ERROR_NoError
;
241 uint8_t Endpoint_Read_Stream_BE(void* Buffer
, uint16_t Length
242 #if !defined(NO_STREAM_CALLBACKS)
243 , uint8_t (* const Callback
)(void)
247 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
250 if ((ErrorCode
= Endpoint_WaitUntilReady()))
255 if (!(Endpoint_ReadWriteAllowed()))
257 Endpoint_ClearCurrentBank();
259 #if !defined(NO_STREAM_CALLBACKS)
260 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
261 return ENDPOINT_RWSTREAM_ERROR_CallbackAborted
;
264 if ((ErrorCode
= Endpoint_WaitUntilReady()))
268 *(DataStream
--) = Endpoint_Read_Byte();
271 return ENDPOINT_RWSTREAM_ERROR_NoError
;
274 uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer
, uint16_t Length
)
276 uint8_t* DataStream
= (uint8_t*)Buffer
;
279 while (Length
&& !(Endpoint_IsSetupOUTReceived()))
281 while (!(Endpoint_IsSetupINReady()));
283 while (Length
&& (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize
))
285 Endpoint_Write_Byte(*(DataStream
++));
290 SendZLP
= (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize
);
291 Endpoint_ClearSetupIN();
294 if (Endpoint_IsSetupOUTReceived())
295 return ENDPOINT_RWCSTREAM_ERROR_HostAborted
;
299 while (!(Endpoint_IsSetupINReady()));
300 Endpoint_ClearSetupIN();
303 while (!(Endpoint_IsSetupOUTReceived()));
305 return ENDPOINT_RWCSTREAM_ERROR_NoError
;
308 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer
, uint16_t Length
)
310 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
313 while (Length
&& !(Endpoint_IsSetupOUTReceived()))
315 while (!(Endpoint_IsSetupINReady()));
317 while (Length
&& (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize
))
319 Endpoint_Write_Byte(*(DataStream
--));
324 SendZLP
= (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize
);
325 Endpoint_ClearSetupIN();
328 if (Endpoint_IsSetupOUTReceived())
329 return ENDPOINT_RWCSTREAM_ERROR_HostAborted
;
333 while (!(Endpoint_IsSetupINReady()));
334 Endpoint_ClearSetupIN();
337 while (!(Endpoint_IsSetupOUTReceived()));
339 return ENDPOINT_RWCSTREAM_ERROR_NoError
;
342 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer
, uint16_t Length
)
344 uint8_t* DataStream
= (uint8_t*)Buffer
;
348 while (!(Endpoint_IsSetupOUTReceived()));
350 while (Length
&& Endpoint_BytesInEndpoint())
352 *(DataStream
++) = Endpoint_Read_Byte();
357 Endpoint_ClearSetupOUT();
360 while (!(Endpoint_IsSetupINReady()));
362 return ENDPOINT_RWCSTREAM_ERROR_NoError
;
365 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer
, uint16_t Length
)
367 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
371 while (!(Endpoint_IsSetupOUTReceived()));
373 while (Length
&& Endpoint_BytesInEndpoint())
375 *(DataStream
--) = Endpoint_Read_Byte();
380 Endpoint_ClearSetupOUT();
383 while (!(Endpoint_IsSetupINReady()));
385 return ENDPOINT_RWCSTREAM_ERROR_NoError
;