3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 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 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../HighLevel/USBMode.h"
34 #if defined(USB_CAN_BE_DEVICE)
36 #define __INCLUDE_FROM_ENDPOINT_C
39 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
40 uint8_t USB_ControlEndpointSize
= ENDPOINT_CONTROLEP_DEFAULT_SIZE
;
43 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number
,
44 const uint8_t UECFG0XData
,
45 const uint8_t UECFG1XData
)
47 uint8_t UECFG0XTemp
[ENDPOINT_TOTAL_ENDPOINTS
];
48 uint8_t UECFG1XTemp
[ENDPOINT_TOTAL_ENDPOINTS
];
50 for (uint8_t EPNum
= 0; EPNum
< ENDPOINT_TOTAL_ENDPOINTS
; EPNum
++)
52 Endpoint_SelectEndpoint(EPNum
);
53 UECFG0XTemp
[EPNum
] = UECFG0X
;
54 UECFG1XTemp
[EPNum
] = UECFG1X
;
57 UECFG0XTemp
[Number
] = UECFG0XData
;
58 UECFG1XTemp
[Number
] = UECFG1XData
;
60 for (uint8_t EPNum
= 1; EPNum
< ENDPOINT_TOTAL_ENDPOINTS
; EPNum
++)
62 Endpoint_SelectEndpoint(EPNum
);
66 Endpoint_DisableEndpoint();
69 for (uint8_t EPNum
= 0; EPNum
< ENDPOINT_TOTAL_ENDPOINTS
; EPNum
++)
71 if (!(UECFG1XTemp
[EPNum
] & (1 << ALLOC
)))
74 Endpoint_SelectEndpoint(EPNum
);
75 Endpoint_EnableEndpoint();
77 UECFG0X
= UECFG0XTemp
[EPNum
];
78 UECFG1X
= UECFG1XTemp
[EPNum
];
80 if (!(Endpoint_IsConfigured()))
84 Endpoint_SelectEndpoint(Number
);
88 void Endpoint_ClearEndpoints(void)
90 for (uint8_t EPNum
= 0; EPNum
< ENDPOINT_TOTAL_ENDPOINTS
; EPNum
++)
92 Endpoint_SelectEndpoint(EPNum
);
96 Endpoint_DisableEndpoint();
100 void Endpoint_ClearStatusStage(void)
102 if (USB_ControlRequest
.bmRequestType
& REQDIR_DEVICETOHOST
)
104 while (!(Endpoint_IsOUTReceived()))
106 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
114 while (!(Endpoint_IsINReady()))
116 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
124 #if !defined(CONTROL_ONLY_DEVICE)
125 uint8_t Endpoint_WaitUntilReady(void)
127 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
128 uint8_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
130 uint16_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
135 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN
)
137 if (Endpoint_IsINReady())
138 return ENDPOINT_READYWAIT_NoError
;
142 if (Endpoint_IsOUTReceived())
143 return ENDPOINT_READYWAIT_NoError
;
146 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
147 return ENDPOINT_READYWAIT_DeviceDisconnected
;
148 else if (USB_DeviceState
== DEVICE_STATE_Suspended
)
149 return ENDPOINT_READYWAIT_BusSuspended
;
150 else if (Endpoint_IsStalled())
151 return ENDPOINT_READYWAIT_EndpointStalled
;
153 if (USB_INT_HasOccurred(USB_INT_SOFI
))
155 USB_INT_Clear(USB_INT_SOFI
);
157 if (!(TimeoutMSRem
--))
158 return ENDPOINT_READYWAIT_Timeout
;
163 uint8_t Endpoint_Discard_Stream(uint16_t Length
164 #if !defined(NO_STREAM_CALLBACKS)
165 , StreamCallbackPtr_t Callback
171 if ((ErrorCode
= Endpoint_WaitUntilReady()))
174 #if defined(FAST_STREAM_TRANSFERS)
175 uint8_t BytesRemToAlignment
= (Endpoint_BytesInEndpoint() & 0x07);
179 Length
-= BytesRemToAlignment
;
181 switch (BytesRemToAlignment
)
186 if (!(Endpoint_IsReadWriteAllowed()))
190 #if !defined(NO_STREAM_CALLBACKS)
191 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
192 return ENDPOINT_RWSTREAM_CallbackAborted
;
195 if ((ErrorCode
= Endpoint_WaitUntilReady()))
201 Endpoint_Discard_Byte();
202 case 7: Endpoint_Discard_Byte();
203 case 6: Endpoint_Discard_Byte();
204 case 5: Endpoint_Discard_Byte();
205 case 4: Endpoint_Discard_Byte();
206 case 3: Endpoint_Discard_Byte();
207 case 2: Endpoint_Discard_Byte();
208 case 1: Endpoint_Discard_Byte();
209 } while (Length
>= 8);
216 if (!(Endpoint_IsReadWriteAllowed()))
220 #if !defined(NO_STREAM_CALLBACKS)
221 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
222 return ENDPOINT_RWSTREAM_CallbackAborted
;
225 if ((ErrorCode
= Endpoint_WaitUntilReady()))
230 Endpoint_Discard_Byte();
235 return ENDPOINT_RWSTREAM_NoError
;
238 /* The following abuses the C preprocessor in order to copy-past common code with slight alterations,
239 * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */
241 #define TEMPLATE_FUNC_NAME Endpoint_Write_Stream_LE
242 #define TEMPLATE_BUFFER_TYPE const void*
243 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
244 #define TEMPLATE_BUFFER_OFFSET(Length) 0
245 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr++))
246 #include "Template/Template_Endpoint_RW.c"
248 #define TEMPLATE_FUNC_NAME Endpoint_Write_PStream_LE
249 #define TEMPLATE_BUFFER_TYPE const void*
250 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
251 #define TEMPLATE_BUFFER_OFFSET(Length) 0
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_Write_EStream_LE
256 #define TEMPLATE_BUFFER_TYPE const void*
257 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
258 #define TEMPLATE_BUFFER_OFFSET(Length) 0
259 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++))
260 #include "Template/Template_Endpoint_RW.c"
262 #define TEMPLATE_FUNC_NAME Endpoint_Write_Stream_BE
263 #define TEMPLATE_BUFFER_TYPE const void*
264 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
265 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
266 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr--))
267 #include "Template/Template_Endpoint_RW.c"
269 #define TEMPLATE_FUNC_NAME Endpoint_Write_EStream_BE
270 #define TEMPLATE_BUFFER_TYPE const void*
271 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
272 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
273 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--))
274 #include "Template/Template_Endpoint_RW.c"
276 #define TEMPLATE_FUNC_NAME Endpoint_Write_PStream_BE
277 #define TEMPLATE_BUFFER_TYPE const void*
278 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
279 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
280 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--))
281 #include "Template/Template_Endpoint_RW.c"
283 #define TEMPLATE_FUNC_NAME Endpoint_Read_Stream_LE
284 #define TEMPLATE_BUFFER_TYPE void*
285 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
286 #define TEMPLATE_BUFFER_OFFSET(Length) 0
287 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr++) = Endpoint_Read_Byte()
288 #include "Template/Template_Endpoint_RW.c"
290 #define TEMPLATE_FUNC_NAME Endpoint_Read_EStream_LE
291 #define TEMPLATE_BUFFER_TYPE void*
292 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
293 #define TEMPLATE_BUFFER_OFFSET(Length) 0
294 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr++, Endpoint_Read_Byte())
295 #include "Template/Template_Endpoint_RW.c"
297 #define TEMPLATE_FUNC_NAME Endpoint_Read_Stream_BE
298 #define TEMPLATE_BUFFER_TYPE void*
299 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
300 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
301 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr--) = Endpoint_Read_Byte()
302 #include "Template/Template_Endpoint_RW.c"
304 #define TEMPLATE_FUNC_NAME Endpoint_Read_EStream_BE
305 #define TEMPLATE_BUFFER_TYPE void*
306 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
307 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
308 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr--, Endpoint_Read_Byte())
309 #include "Template/Template_Endpoint_RW.c"
313 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_Stream_LE
314 #define TEMPLATE_BUFFER_OFFSET(Length) 0
315 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr++))
316 #include "Template/Template_Endpoint_Control_W.c"
318 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_PStream_LE
319 #define TEMPLATE_BUFFER_OFFSET(Length) 0
320 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++))
321 #include "Template/Template_Endpoint_Control_W.c"
323 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_EStream_LE
324 #define TEMPLATE_BUFFER_OFFSET(Length) 0
325 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++))
326 #include "Template/Template_Endpoint_Control_W.c"
328 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_Stream_BE
329 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
330 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr--))
331 #include "Template/Template_Endpoint_Control_W.c"
333 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_PStream_BE
334 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
335 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--))
336 #include "Template/Template_Endpoint_Control_W.c"
338 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_EStream_BE
339 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
340 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--))
341 #include "Template/Template_Endpoint_Control_W.c"
343 #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_Stream_LE
344 #define TEMPLATE_BUFFER_OFFSET(Length) 0
345 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr++) = Endpoint_Read_Byte()
346 #include "Template/Template_Endpoint_Control_R.c"
348 #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_EStream_LE
349 #define TEMPLATE_BUFFER_OFFSET(Length) 0
350 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr++, Endpoint_Read_Byte())
351 #include "Template/Template_Endpoint_Control_R.c"
353 #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_Stream_BE
354 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
355 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr--) = Endpoint_Read_Byte()
356 #include "Template/Template_Endpoint_Control_R.c"
358 #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_EStream_BE
359 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
360 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr--, Endpoint_Read_Byte())
361 #include "Template/Template_Endpoint_Control_R.c"