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, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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 void Endpoint_ClearStatusStage(void)
76 if (USB_ControlRequest
.bmRequestType
& REQDIR_DEVICETOHOST
)
78 while (!(Endpoint_IsOUTReceived()))
80 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
88 while (!(Endpoint_IsINReady()))
90 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
98 #if !defined(CONTROL_ONLY_DEVICE)
99 uint8_t Endpoint_WaitUntilReady(void)
101 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
102 uint8_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
104 uint16_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
109 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN
)
111 if (Endpoint_IsINReady())
112 return ENDPOINT_READYWAIT_NoError
;
116 if (Endpoint_IsOUTReceived())
117 return ENDPOINT_READYWAIT_NoError
;
120 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
121 return ENDPOINT_READYWAIT_DeviceDisconnected
;
122 else if (Endpoint_IsStalled())
123 return ENDPOINT_READYWAIT_EndpointStalled
;
125 if (USB_INT_HasOccurred(USB_INT_SOFI
))
127 USB_INT_Clear(USB_INT_SOFI
);
129 if (!(TimeoutMSRem
--))
130 return ENDPOINT_READYWAIT_Timeout
;
135 uint8_t Endpoint_Discard_Stream(uint16_t Length
136 #if !defined(NO_STREAM_CALLBACKS)
137 , StreamCallbackPtr_t Callback
143 if ((ErrorCode
= Endpoint_WaitUntilReady()))
146 #if defined(FAST_STREAM_TRANSFERS)
147 uint8_t BytesRemToAlignment
= (Endpoint_BytesInEndpoint() & 0x07);
151 Length
-= BytesRemToAlignment
;
153 switch (BytesRemToAlignment
)
158 if (!(Endpoint_IsReadWriteAllowed()))
162 #if !defined(NO_STREAM_CALLBACKS)
163 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
164 return ENDPOINT_RWSTREAM_CallbackAborted
;
167 if ((ErrorCode
= Endpoint_WaitUntilReady()))
173 Endpoint_Discard_Byte();
174 case 7: Endpoint_Discard_Byte();
175 case 6: Endpoint_Discard_Byte();
176 case 5: Endpoint_Discard_Byte();
177 case 4: Endpoint_Discard_Byte();
178 case 3: Endpoint_Discard_Byte();
179 case 2: Endpoint_Discard_Byte();
180 case 1: Endpoint_Discard_Byte();
181 } while (Length
>= 8);
188 if (!(Endpoint_IsReadWriteAllowed()))
192 #if !defined(NO_STREAM_CALLBACKS)
193 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
194 return ENDPOINT_RWSTREAM_CallbackAborted
;
197 if ((ErrorCode
= Endpoint_WaitUntilReady()))
202 Endpoint_Discard_Byte();
207 return ENDPOINT_RWSTREAM_NoError
;
210 /* The following abuses the C preprocessor in order to copy-past common code with slight alterations,
211 * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */
213 #define TEMPLATE_FUNC_NAME Endpoint_Write_Stream_LE
214 #define TEMPLATE_BUFFER_TYPE const void*
215 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
216 #define TEMPLATE_BUFFER_OFFSET(Length) 0
217 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr++))
218 #include "Template/Template_Endpoint_RW.c"
220 #define TEMPLATE_FUNC_NAME Endpoint_Write_PStream_LE
221 #define TEMPLATE_BUFFER_TYPE const void*
222 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
223 #define TEMPLATE_BUFFER_OFFSET(Length) 0
224 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++))
225 #include "Template/Template_Endpoint_RW.c"
227 #define TEMPLATE_FUNC_NAME Endpoint_Write_EStream_LE
228 #define TEMPLATE_BUFFER_TYPE const void*
229 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
230 #define TEMPLATE_BUFFER_OFFSET(Length) 0
231 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++))
232 #include "Template/Template_Endpoint_RW.c"
234 #define TEMPLATE_FUNC_NAME Endpoint_Write_Stream_BE
235 #define TEMPLATE_BUFFER_TYPE const void*
236 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
237 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
238 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr--))
239 #include "Template/Template_Endpoint_RW.c"
241 #define TEMPLATE_FUNC_NAME Endpoint_Write_EStream_BE
242 #define TEMPLATE_BUFFER_TYPE const void*
243 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
244 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
245 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--))
246 #include "Template/Template_Endpoint_RW.c"
248 #define TEMPLATE_FUNC_NAME Endpoint_Write_PStream_BE
249 #define TEMPLATE_BUFFER_TYPE const void*
250 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
251 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
252 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--))
253 #include "Template/Template_Endpoint_RW.c"
255 #define TEMPLATE_FUNC_NAME Endpoint_Read_Stream_LE
256 #define TEMPLATE_BUFFER_TYPE void*
257 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
258 #define TEMPLATE_BUFFER_OFFSET(Length) 0
259 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr++) = Endpoint_Read_Byte()
260 #include "Template/Template_Endpoint_RW.c"
262 #define TEMPLATE_FUNC_NAME Endpoint_Read_EStream_LE
263 #define TEMPLATE_BUFFER_TYPE void*
264 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
265 #define TEMPLATE_BUFFER_OFFSET(Length) 0
266 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_write_byte((uint8_t*)BufferPtr++, Endpoint_Read_Byte())
267 #include "Template/Template_Endpoint_RW.c"
269 #define TEMPLATE_FUNC_NAME Endpoint_Read_Stream_BE
270 #define TEMPLATE_BUFFER_TYPE void*
271 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
272 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
273 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr--) = Endpoint_Read_Byte()
274 #include "Template/Template_Endpoint_RW.c"
276 #define TEMPLATE_FUNC_NAME Endpoint_Read_EStream_BE
277 #define TEMPLATE_BUFFER_TYPE void*
278 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
279 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
280 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_write_byte((uint8_t*)BufferPtr--, Endpoint_Read_Byte())
281 #include "Template/Template_Endpoint_RW.c"
285 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_Stream_LE
286 #define TEMPLATE_BUFFER_OFFSET(Length) 0
287 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr++))
288 #include "Template/Template_Endpoint_Control_W.c"
290 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_PStream_LE
291 #define TEMPLATE_BUFFER_OFFSET(Length) 0
292 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++))
293 #include "Template/Template_Endpoint_Control_W.c"
295 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_EStream_LE
296 #define TEMPLATE_BUFFER_OFFSET(Length) 0
297 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++))
298 #include "Template/Template_Endpoint_Control_W.c"
300 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_Stream_BE
301 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
302 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr--))
303 #include "Template/Template_Endpoint_Control_W.c"
305 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_PStream_BE
306 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
307 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--))
308 #include "Template/Template_Endpoint_Control_W.c"
310 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_EStream_BE
311 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
312 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--))
313 #include "Template/Template_Endpoint_Control_W.c"
315 #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_Stream_LE
316 #define TEMPLATE_BUFFER_OFFSET(Length) 0
317 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr++) = Endpoint_Read_Byte()
318 #include "Template/Template_Endpoint_Control_R.c"
320 #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_EStream_LE
321 #define TEMPLATE_BUFFER_OFFSET(Length) 0
322 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_write_byte((uint8_t*)BufferPtr++, Endpoint_Read_Byte())
323 #include "Template/Template_Endpoint_Control_R.c"
325 #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_Stream_BE
326 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
327 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr--) = Endpoint_Read_Byte()
328 #include "Template/Template_Endpoint_Control_R.c"
330 #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_EStream_BE
331 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
332 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_write_byte((uint8_t*)BufferPtr--, Endpoint_Read_Byte())
333 #include "Template/Template_Endpoint_Control_R.c"