Moved all source to the trunk directory.
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Endpoint.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
33 * Functions, macros and enums related to endpoint management when in USB Device mode. This
34 * module contains the endpoint management macros, as well as endpoint interrupt and data
35 * send/recieve functions for various datatypes.
36 */
37
38 #ifndef __ENDPOINT_H__
39 #define __ENDPOINT_H__
40
41 /* Includes: */
42 #include <avr/io.h>
43 #include <stdbool.h>
44
45 #include "../../../Common/Common.h"
46 #include "../HighLevel/USBTask.h"
47
48 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
49 #include "StreamCallbacks.h"
50 #endif
51 /* Enable C linkage for C++ Compilers: */
52 #if defined(__cplusplus)
53 extern "C" {
54 #endif
55
56 /* Public Interface - May be used in end-application: */
57 /* Macros: */
58 /** Endpoint data direction mask for Endpoint_ConfigureEndpoint(). This indicates that the endpoint
59 * should be initialized in the OUT direction - i.e. data flows from host to device.
60 */
61 #define ENDPOINT_DIR_OUT 0
62
63 /** Endpoint data direction mask for Endpoint_ConfigureEndpoint(). This indicates that the endpoint
64 * should be initialized in the IN direction - i.e. data flows from device to host.
65 */
66 #define ENDPOINT_DIR_IN (1 << EPDIR)
67
68 /** Mask for the bank mode selection for the Endpoint_ConfigureEndpoint() macro. This indicates
69 * that the endpoint should have one single bank, which requires less USB FIFO memory but results
70 * in slower transfers as only one USB device (the AVR or the host) can access the endpoint's
71 * bank at the one time.
72 */
73 #define ENDPOINT_BANK_SINGLE 0
74
75 /** Mask for the bank mode selection for the Endpoint_ConfigureEndpoint() macro. This indicates
76 * that the endpoint should have two banks, which requires more USB FIFO memory but results
77 * in faster transfers as one USB device (the AVR or the host) can access one bank while the other
78 * accesses the second bank.
79 */
80 #define ENDPOINT_BANK_DOUBLE (1 << EPBK0)
81
82 /** Endpoint address for the default control endpoint, which always resides in address 0. This is
83 * defined for convenience to give more readable code when used with the endpoint macros.
84 */
85 #define ENDPOINT_CONTROLEP 0
86
87 /** Default size of the default control endpoint's bank, until altered by the Endpoint0Size value
88 * in the device descriptor. Not available if the FIXED_CONTROL_ENDPOINT_SIZE token is defined.
89 */
90 #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
91 #define ENDPOINT_CONTROLEP_DEFAULT_SIZE 8
92 #endif
93
94 /** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's
95 * numerical address in the device.
96 */
97 #define ENDPOINT_EPNUM_MASK 0b111
98
99 /** Endpoint bank size mask, for masking against endpoint addresses to retrieve the endpoint's
100 * bank size in the device.
101 */
102 #define ENDPOINT_EPSIZE_MASK 0x7FF
103
104 /** Maximum size in bytes of a given endpoint.
105 *
106 * \param n Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1)
107 */
108 #define ENDPOINT_MAX_SIZE(n) _ENDPOINT_GET_MAXSIZE(n)
109
110 /** Indicates if the given endpoint supports double banking.
111 *
112 * \param n Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1)
113 */
114 #define ENDPOINT_DOUBLEBANK_SUPPORTED(n) _ENDPOINT_GET_DOUBLEBANK(n)
115
116 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER) || defined(__DOXYGEN__)
117 /** Total number of endpoints (including the default control endpoint at address 0) which may
118 * be used in the device. Different USB AVR models support different amounts of endpoints,
119 * this value reflects the maximum number of endpoints for the currently selected AVR model.
120 */
121 #define ENDPOINT_TOTAL_ENDPOINTS 7
122 #else
123 #define ENDPOINT_TOTAL_ENDPOINTS 5
124 #endif
125
126 /** Interrupt definition for the endpoint SETUP interrupt (for CONTROL type endpoints). Should be
127 * used with the USB_INT_* macros located in USBInterrupt.h.
128 *
129 * This interrupt will fire if enabled on a CONTROL type endpoint if a new control packet is
130 * received from the host.
131 */
132 #define ENDPOINT_INT_SETUP UEIENX, (1 << RXSTPE), UEINTX, (1 << RXSTPI)
133
134 /** Interrupt definition for the endpoint IN interrupt (for INTERRUPT type endpoints). Should be
135 * used with the USB_INT_* macros located in USBInterrupt.h.
136 *
137 * This interrupt will fire if enabled on an INTERRUPT type endpoint if a the endpoint interrupt
138 * period has elapsed and the endpoint is ready for a new packet to be written to its FIFO buffer
139 * (if required).
140 */
141 #define ENDPOINT_INT_IN UEIENX, (1 << TXINE) , UEINTX, (1 << TXINI)
142
143 /** Interrupt definition for the endpoint OUT interrupt (for INTERRUPT type endpoints). Should be
144 * used with the USB_INT_* macros located in USBInterrupt.h.
145 *
146 * This interrupt will fire if enabled on an INTERRUPT type endpoint if a the endpoint interrupt
147 * period has elapsed and the endpoint is ready for a packet from the host to be read from its
148 * FIFO buffer (if received).
149 */
150 #define ENDPOINT_INT_OUT UEIENX, (1 << RXOUTE), UEINTX, (1 << RXOUTI)
151
152 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER) || defined(__DOXYGEN__)
153 /** Indicates the number of bytes currently stored in the current endpoint's selected bank. */
154 #define Endpoint_BytesInEndpoint() UEBCX
155 #else
156 #define Endpoint_BytesInEndpoint() UEBCLX
157 #endif
158
159 /** Returns the endpoint address of the currently selected endpoint. This is typically used to save
160 * the currently selected endpoint number so that it can be restored after another endpoint has
161 * been manipulated.
162 */
163 #define Endpoint_GetCurrentEndpoint() (UENUM & ENDPOINT_EPNUM_MASK)
164
165 /** Selects the given endpoint number. If the address from the device descriptors is used, the
166 * value should be masked with the ENDPOINT_EPNUM_MASK constant to extract only the endpoint
167 * number (and discarding the endpoint direction bit).
168 *
169 * Any endpoint operations which do not require the endpoint number to be indicated will operate on
170 * the currently selected endpoint.
171 */
172 #define Endpoint_SelectEndpoint(epnum) MACROS{ UENUM = epnum; }MACROE
173
174 /** Resets the endpoint bank FIFO. This clears all the endpoint banks and resets the USB controller's
175 * In and Out pointers to the bank's contents.
176 */
177 #define Endpoint_ResetFIFO(epnum) MACROS{ UERST = (1 << epnum); UERST = 0; }MACROE
178
179 /** Enables the currently selected endpoint so that data can be sent and received through it to
180 * and from a host.
181 *
182 * \note Endpoints must first be configured properly rather than just being enabled via the
183 * Endpoint_ConfigureEndpoint() macro, which calls Endpoint_EnableEndpoint() automatically.
184 */
185 #define Endpoint_EnableEndpoint() MACROS{ UECONX |= (1 << EPEN); }MACROE
186
187 /** Disables the currently selected endpoint so that data cannot be sent and received through it
188 * to and from a host.
189 */
190 #define Endpoint_DisableEndpoint() MACROS{ UECONX &= ~(1 << EPEN); }MACROE
191
192 /** Returns true if the currently selected endpoint is enabled, false otherwise. */
193 #define Endpoint_IsEnabled() ((UECONX & (1 << EPEN)) ? true : false)
194
195 /** Returns true if the currently selected endpoint may be read from (if data is waiting in the endpoint
196 * bank and the endpoint is an OUT direction, or if the bank is not yet full if the endpoint is an
197 * IN direction). This function will return false if an error has occured in the endpoint, or if
198 * the endpoint is an OUT direction and no packet has been received, or if the endpoint is an IN
199 * direction and the endpoint bank is full.
200 */
201 #define Endpoint_ReadWriteAllowed() ((UEINTX & (1 << RWAL)) ? true : false)
202
203 /** Returns true if the currently selected endpoint is configured, false otherwise. */
204 #define Endpoint_IsConfigured() ((UESTA0X & (1 << CFGOK)) ? true : false)
205
206 /** Returns a mask indicating which INTERRUPT type endpoints have interrupted - i.e. their
207 * interrupt duration has elapsed. Which endpoints have interrupted can be determined by
208 * masking the return value against (1 << {Endpoint Number}).
209 */
210 #define Endpoint_GetEndpointInterrupts() UEINT
211
212 /** Clears the endpoint interrupt flag. This clears the specified endpoint number's interrupt
213 * mask in the endpoint interrupt flag register.
214 */
215 #define Endpoint_ClearEndpointInterrupt(n) MACROS{ UEINT &= ~(1 << n); }MACROE
216
217 /** Returns true if the specified endpoint number has interrupted (valid only for INTERRUPT type
218 * endpoints), false otherwise.
219 */
220 #define Endpoint_HasEndpointInterrupted(n) ((UEINT & (1 << n)) ? true : false)
221
222 /** Clears the currently selected endpoint bank, and switches to the alternate bank if the currently
223 * selected endpoint is dual-banked. When cleared, this either frees the bank up for the next packet
224 * from the host (if the endpoint is of the OUT direction) or sends the packet contents to the host
225 * (if the endpoint is of the IN direction).
226 */
227 #define Endpoint_ClearCurrentBank() MACROS{ UEINTX &= ~(1 << FIFOCON); }MACROE
228
229 /** Returns true if the current CONTROL type endpoint is ready for an IN packet, false otherwise. */
230 #define Endpoint_IsSetupINReady() ((UEINTX & (1 << TXINI)) ? true : false)
231
232 /** Returns true if the current CONTROL type endpoint is ready for an OUT packet, false otherwise. */
233 #define Endpoint_IsSetupOUTReceived() ((UEINTX & (1 << RXOUTI)) ? true : false)
234
235 /** Returns true if the current CONTROL type endpoint is ready for a SETUP packet, false otherwise. */
236 #define Endpoint_IsSetupReceived() ((UEINTX & (1 << RXSTPI)) ? true : false)
237
238 /** Clears a received SETUP packet on the currently selected CONTROL type endpoint. */
239 #define Endpoint_ClearSetupReceived() MACROS{ UEINTX &= ~(1 << RXSTPI); }MACROE
240
241 /** Sends an IN packet to the host on the currently selected CONTROL type endpoint. */
242 #define Endpoint_ClearSetupIN() MACROS{ UEINTX &= ~(1 << TXINI); }MACROE
243
244 /** Acknowedges an OUT packet to the host on the currently selected CONTROL type endpoint, freeing
245 * up the endpoint for the next packet.
246 */
247 #define Endpoint_ClearSetupOUT() MACROS{ UEINTX &= ~(1 << RXOUTI); }MACROE
248
249 /** Stalls the current endpoint, indicating to the host that a logical problem occured with the
250 * indicated endpoint and that the current transfer sequence should be aborted. This provides a
251 * way for devices to indicate invalid commands to the host so that the current transfer can be
252 * aborted and the host can begin its own recovery seqeuence.
253 *
254 * The currently selected endpoint remains stalled until either the Endpoint_ClearStall() macro
255 * is called, or the host issues a CLEAR FEATURE request to the device for the currently selected
256 * endpoint.
257 */
258 #define Endpoint_StallTransaction() MACROS{ UECONX |= (1 << STALLRQ); }MACROE
259
260 /** Clears the stall on the currently selected endpoint. */
261 #define Endpoint_ClearStall() MACROS{ UECONX |= (1 << STALLRQC); }MACROE
262
263 /** Returns true if the currently selected endpoint is stalled, false othewise. */
264 #define Endpoint_IsStalled() ((UECONX & (1 << STALLRQ)) ? true : false)
265
266 /** Resets the data toggle of the currently selected endpoint. */
267 #define Endpoint_ResetDataToggle() MACROS{ UECONX |= (1 << RSTDT); }MACROE
268
269 /* Enums: */
270 /** Enum for the possible error return codes of the Endpoint_WaitUntilReady function */
271 enum Endpoint_WaitUntilReady_ErrorCodes_t
272 {
273 ENDPOINT_READYWAIT_NoError = 0, /**< Endpoint is ready for next packet, no error. */
274 ENDPOINT_READYWAIT_EndpointStalled = 1, /**< The endpoint was stalled during the stream
275 * transfer by the host or device.
276 */
277 ENDPOINT_READYWAIT_DeviceDisconnected = 2, /**< Device was disconnected from the host while
278 * waiting for the endpoint to become ready.
279 */
280 ENDPOINT_READYWAIT_Timeout = 3, /**< The host failed to accept or send the next packet
281 * within the software timeout period set by the
282 * USB_STREAM_TIMEOUT_MS macro.
283 */
284 };
285
286 /** Enum for the possible error return codes of the Endpoint_*_Stream_* functions. */
287 enum Endpoint_Stream_RW_ErrorCodes_t
288 {
289 ENDPOINT_RWSTREAM_ERROR_NoError = 0, /**< Command completed successfully, no error. */
290 ENDPOINT_RWSTREAM_ERROR_EndpointStalled = 1, /**< The endpoint was stalled during the stream
291 * transfer by the host or device.
292 */
293 ENDPOINT_RWSTREAM_ERROR_DeviceDisconnected = 1, /**< Device was disconnected from the host during
294 * the transfer.
295 */
296 ENDPOINT_RWSTREAM_ERROR_Timeout = 2, /**< The host failed to accept or send the next packet
297 * within the software timeout period set by the
298 * USB_STREAM_TIMEOUT_MS macro.
299 */
300 ENDPOINT_RWSTREAM_ERROR_CallbackAborted = 3, /**< Indicates that the stream's callback function
301 * aborted the transfer early.
302 */
303 };
304
305 /** Enum for the possible error return codes of the Endpoint_*_Control_Stream_* functions. */
306 enum Endpoint_ControlStream_RW_ErrorCodes_t
307 {
308 ENDPOINT_RWCSTREAM_ERROR_NoError = 0, /**< Command completed successfully, no error. */
309 ENDPOINT_RWCSTREAM_ERROR_HostAborted = 1, /**< The aborted the transfer prematurely. */
310 };
311
312 /* Inline Functions: */
313 /** Reads one byte from the currently selected endpoint's bank, for OUT direction endpoints. */
314 static inline uint8_t Endpoint_Read_Byte(void) ATTR_WARN_UNUSED_RESULT;
315 static inline uint8_t Endpoint_Read_Byte(void)
316 {
317 return UEDATX;
318 }
319
320 /** Writes one byte from the currently selected endpoint's bank, for IN direction endpoints. */
321 static inline void Endpoint_Write_Byte(const uint8_t Byte)
322 {
323 UEDATX = Byte;
324 }
325
326 /** Discards one byte from the currently selected endpoint's bank, for OUT direction endpoints. */
327 static inline void Endpoint_Discard_Byte(void)
328 {
329 uint8_t Dummy;
330
331 Dummy = UEDATX;
332 }
333
334 /** Reads two bytes from the currently selected endpoint's bank in little endian format, for OUT
335 * direction endpoints.
336 */
337 static inline uint16_t Endpoint_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT;
338 static inline uint16_t Endpoint_Read_Word_LE(void)
339 {
340 uint16_t Data;
341
342 Data = UEDATX;
343 Data |= (((uint16_t)UEDATX) << 8);
344
345 return Data;
346 }
347
348 /** Reads two bytes from the currently selected endpoint's bank in big endian format, for OUT
349 * direction endpoints.
350 */
351 static inline uint16_t Endpoint_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT;
352 static inline uint16_t Endpoint_Read_Word_BE(void)
353 {
354 uint16_t Data;
355
356 Data = (((uint16_t)UEDATX) << 8);
357 Data |= UEDATX;
358
359 return Data;
360 }
361
362 /** Writes two bytes to the currently selected endpoint's bank in little endian format, for IN
363 * direction endpoints.
364 */
365 static inline void Endpoint_Write_Word_LE(const uint16_t Word)
366 {
367 UEDATX = (Word & 0xFF);
368 UEDATX = (Word >> 8);
369 }
370
371 /** Writes two bytes to the currently selected endpoint's bank in big endian format, for IN
372 * direction endpoints.
373 */
374 static inline void Endpoint_Write_Word_BE(const uint16_t Word)
375 {
376 UEDATX = (Word >> 8);
377 UEDATX = (Word & 0xFF);
378 }
379
380 /** Discards two bytes from the currently selected endpoint's bank, for OUT direction endpoints. */
381 static inline void Endpoint_Discard_Word(void)
382 {
383 uint8_t Dummy;
384
385 Dummy = UEDATX;
386 Dummy = UEDATX;
387 }
388
389 /** Reads four bytes from the currently selected endpoint's bank in little endian format, for OUT
390 * direction endpoints.
391 */
392 static inline uint32_t Endpoint_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT;
393 static inline uint32_t Endpoint_Read_DWord_LE(void)
394 {
395 union
396 {
397 uint32_t DWord;
398 uint8_t Bytes[4];
399 } Data;
400
401 Data.Bytes[0] = UEDATX;
402 Data.Bytes[1] = UEDATX;
403 Data.Bytes[2] = UEDATX;
404 Data.Bytes[3] = UEDATX;
405
406 return Data.DWord;
407 }
408
409 /** Reads four bytes from the currently selected endpoint's bank in big endian format, for OUT
410 * direction endpoints.
411 */
412 static inline uint32_t Endpoint_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT;
413 static inline uint32_t Endpoint_Read_DWord_BE(void)
414 {
415 union
416 {
417 uint32_t DWord;
418 uint8_t Bytes[4];
419 } Data;
420
421 Data.Bytes[3] = UEDATX;
422 Data.Bytes[2] = UEDATX;
423 Data.Bytes[1] = UEDATX;
424 Data.Bytes[0] = UEDATX;
425
426 return Data.DWord;
427 }
428
429 /** Writes four bytes to the currently selected endpoint's bank in little endian format, for IN
430 * direction endpoints.
431 */
432 static inline void Endpoint_Write_DWord_LE(const uint32_t DWord)
433 {
434 Endpoint_Write_Word_LE(DWord);
435 Endpoint_Write_Word_LE(DWord >> 16);
436 }
437
438 /** Writes four bytes to the currently selected endpoint's bank in big endian format, for IN
439 * direction endpoints.
440 */
441 static inline void Endpoint_Write_DWord_BE(const uint32_t DWord)
442 {
443 Endpoint_Write_Word_BE(DWord >> 16);
444 Endpoint_Write_Word_BE(DWord);
445 }
446
447 /** Discards four bytes from the currently selected endpoint's bank, for OUT direction endpoints. */
448 static inline void Endpoint_Discard_DWord(void)
449 {
450 uint8_t Dummy;
451
452 Dummy = UEDATX;
453 Dummy = UEDATX;
454 Dummy = UEDATX;
455 Dummy = UEDATX;
456 }
457
458 /* External Variables: */
459 /** Global indicating the maximum packet size of the default control endpoint located at address
460 * 0 in the device. This value is set to the value indicated in the device descriptor in the user
461 * project once the USB interface is initialized into device mode.
462 *
463 * If space is an issue, it is possible to fix this to a static value by defining the control
464 * endpoint size in the FIXED_CONTROL_ENDPOINT_SIZE token passed to the compiler in the makefile
465 * via the -D switch. When a fixed control endpoint size is used, the size is no longer dynamically
466 * read from the descriptors at runtime and instead fixed to the given value. When used, it is
467 * important that the descriptor control endpoint size value matches the size given as the
468 * FIXED_CONTROL_ENDPOINT_SIZE token - it is recommended that the FIXED_CONTROL_ENDPOINT_SIZE token
469 * be used in the descriptors to ensure this.
470 *
471 * \note This variable should be treated as read-only in the user application, and never manually
472 * changed in value.
473 */
474 #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
475 extern uint8_t USB_ControlEndpointSize;
476 #else
477 #define USB_ControlEndpointSize FIXED_CONTROL_ENDPOINT_SIZE
478 #endif
479
480 /* Function Prototypes: */
481 /** Configures the specified endpoint number with the given endpoint type, direction, bank size
482 * and banking mode. Endpoints should be allocated in ascending order by their address in the
483 * device (i.e. endpoint 1 should be configured before endpoint 2 and so on).
484 *
485 * The endpoint type may be one of the EP_TYPE_* macros listed in LowLevel.h and the direction
486 * may be either ENDPOINT_DIR_OUT or ENDPOINT_DIR_IN.
487 *
488 * The bank size must indicate the maximum packet size that the endpoint can handle. Different
489 * endpoint numbers can handle different maximum packet sizes - refer to the chosen USB AVR's
490 * datasheet to determine the maximum bank size for each endpoint.
491 *
492 * The banking mode may be either ENDPOINT_BANK_SINGLE or ENDPOINT_BANK_DOUBLE.
493 *
494 * The success of this routine can be determined via the Endpoint_IsConfigured() macro.
495 *
496 * By default, the routine is entirely dynamic, and will accept both constant and variable inputs.
497 * If dynamic configuration is unused, a small space savings can be made by defining the
498 * STATIC_ENDPOINT_CONFIGURATION macro via the -D switch to the compiler, to optimize for constant
499 * input values.
500 *
501 * \note This routine will select the specified endpoint, and the endpoint will remain selected
502 * once the routine completes regardless of if the endpoint configuration succeeds.
503 *
504 * \return Boolean true if the configuration succeeded, false otherwise
505 */
506 bool Endpoint_ConfigureEndpoint(const uint8_t Number, const uint8_t Type, const uint8_t Direction,
507 const uint16_t Size, const uint8_t Banks);
508
509 /** Spinloops until the currently selected non-control endpoint is ready for the next packet of data
510 * to be read or written to it.
511 *
512 * \note This routine should not be called on CONTROL type endpoints.
513 *
514 * \return A value from the Endpoint_WaitUntilReady_ErrorCodes_t enum.
515 */
516 uint8_t Endpoint_WaitUntilReady(void);
517
518 /** Reads and discards the given number of bytes from the endpoint from the given buffer,
519 * discarding fully read packets from the host as needed. The last packet is not automatically
520 * discarded once the remaining bytes has been read; the user is responsible for manually
521 * discarding the last packet from the host via the Endpoint_ClearCurrentBank() macro. Between
522 * each USB packet, the given stream callback function is executed repeatedly until the next
523 * packet is ready, allowing for early aborts of stream transfers.
524 *
525 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
526 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
527 * and this function has the Callback parameter ommitted.
528 *
529 * \note This routine should not be used on CONTROL type endpoints.
530 *
531 * \param Length Number of bytes to send via the currently selected endpoint.
532 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
533 *
534 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
535 */
536 uint8_t Endpoint_Discard_Stream(uint16_t Length
537 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
538 , uint8_t (* const Callback)(void)
539 #endif
540 );
541
542 /** Writes the given number of bytes to the endpoint from the given buffer in little endian,
543 * sending full packets to the host as needed. The last packet filled is not automatically sent;
544 * the user is responsible for manually sending the last written packet to the host via the
545 * Endpoint_ClearCurrentBank() macro. Between each USB packet, the given stream callback function
546 * is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early
547 * aborts of stream transfers.
548 *
549 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
550 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
551 * and this function has the Callback parameter ommitted.
552 *
553 * \note This routine should not be used on CONTROL type endpoints.
554 *
555 * \param Buffer Pointer to the source data buffer to read from.
556 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
557 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
558 *
559 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
560 */
561 uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length
562 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
563 , uint8_t (* const Callback)(void)
564 #endif
565 ) ATTR_NON_NULL_PTR_ARG(1);
566
567 /** Writes the given number of bytes to the endpoint from the given buffer in big endian,
568 * sending full packets to the host as needed. The last packet filled is not automatically sent;
569 * the user is responsible for manually sending the last written packet to the host via the
570 * Endpoint_ClearCurrentBank() macro. Between each USB packet, the given stream callback function
571 * is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early
572 * aborts of stream transfers.
573 *
574 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
575 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
576 * and this function has the Callback parameter ommitted.
577 *
578 * \note This routine should not be used on CONTROL type endpoints.
579 *
580 * \param Buffer Pointer to the source data buffer to read from.
581 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
582 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
583 *
584 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
585 */
586 uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length
587 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
588 , uint8_t (* const Callback)(void)
589 #endif
590 ) ATTR_NON_NULL_PTR_ARG(1);
591
592 /** Reads the given number of bytes from the endpoint from the given buffer in little endian,
593 * discarding fully read packets from the host as needed. The last packet is not automatically
594 * discarded once the remaining bytes has been read; the user is responsible for manually
595 * discarding the last packet from the host via the Endpoint_ClearCurrentBank() macro. Between
596 * each USB packet, the given stream callback function is executed repeatedly until the endpoint
597 * is ready to accept the next packet, allowing for early aborts of stream transfers.
598 *
599 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
600 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
601 * and this function has the Callback parameter ommitted.
602 *
603 * \note This routine should not be used on CONTROL type endpoints.
604 *
605 * \param Buffer Pointer to the destination data buffer to write to.
606 * \param Length Number of bytes to send via the currently selected endpoint.
607 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
608 *
609 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
610 */
611 uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length
612 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
613 , uint8_t (* const Callback)(void)
614 #endif
615 ) ATTR_NON_NULL_PTR_ARG(1);
616
617 /** Reads the given number of bytes from the endpoint from the given buffer in big endian,
618 * discarding fully read packets from the host as needed. The last packet is not automatically
619 * discarded once the remaining bytes has been read; the user is responsible for manually
620 * discarding the last packet from the host via the Endpoint_ClearCurrentBank() macro. Between
621 * each USB packet, the given stream callback function is executed repeatedly until the endpoint
622 * is ready to accept the next packet, allowing for early aborts of stream transfers.
623 *
624 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
625 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
626 * and this function has the Callback parameter ommitted.
627 *
628 * \note This routine should not be used on CONTROL type endpoints.
629 *
630 * \param Buffer Pointer to the destination data buffer to write to.
631 * \param Length Number of bytes to send via the currently selected endpoint.
632 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
633 *
634 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
635 */
636 uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length
637 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
638 , uint8_t (* const Callback)(void)
639 #endif
640 ) ATTR_NON_NULL_PTR_ARG(1);
641
642 /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in little endian,
643 * sending full packets to the host as needed. The host OUT acknowedgement is not automatically cleared
644 * in both failure and success states; the user is responsible for manually clearing the setup OUT to
645 * finalize the transfer via the Endpoint_ClearSetupOUT() macro.
646 *
647 * \note This routine should only be used on CONTROL type endpoints.
648 *
649 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
650 * together; i.e. the entire stream data must be read or written at the one time.
651 *
652 * \param Buffer Pointer to the source data buffer to read from.
653 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
654 *
655 * \return A value from the Endpoint_ControlStream_RW_ErrorCodes_t enum.
656 */
657 uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
658
659 /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in big endian,
660 * sending full packets to the host as needed. The host OUT acknowedgement is not automatically cleared
661 * in both failure and success states; the user is responsible for manually clearing the setup OUT to
662 * finalize the transfer via the Endpoint_ClearSetupOUT() macro.
663 *
664 * \note This routine should only be used on CONTROL type endpoints.
665 *
666 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
667 * together; i.e. the entire stream data must be read or written at the one time.
668 *
669 * \param Buffer Pointer to the source data buffer to read from.
670 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
671 *
672 * \return A value from the Endpoint_ControlStream_RW_ErrorCodes_t enum.
673 */
674 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
675
676 /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in little endian,
677 * discarding fully read packets from the host as needed. The device IN acknowedgement is not
678 * automatically sent after success or failure states; the user is responsible for manually sending the
679 * setup IN to finalize the transfer via the Endpoint_ClearSetupIN() macro.
680 *
681 * \note This routine should only be used on CONTROL type endpoints.
682 *
683 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
684 * together; i.e. the entire stream data must be read or written at the one time.
685 *
686 * \param Buffer Pointer to the destination data buffer to write to.
687 * \param Length Number of bytes to send via the currently selected endpoint.
688 *
689 * \return A value from the Endpoint_ControlStream_RW_ErrorCodes_t enum.
690 */
691 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
692
693 /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in big endian,
694 * discarding fully read packets from the host as needed. The device IN acknowedgement is not
695 * automatically sent after success or failure states; the user is responsible for manually sending the
696 * setup IN to finalize the transfer via the Endpoint_ClearSetupIN() macro.
697 *
698 * \note This routine should only be used on CONTROL type endpoints.
699 *
700 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
701 * together; i.e. the entire stream data must be read or written at the one time.
702 *
703 * \param Buffer Pointer to the destination data buffer to write to.
704 * \param Length Number of bytes to send via the currently selected endpoint.
705 *
706 * \return A value from the Endpoint_ControlStream_RW_ErrorCodes_t enum.
707 */
708 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
709
710 /* Function Aliases: */
711 /** Alias for Endpoint_Discard_Byte().
712 */
713 #define Endpoint_Ignore_Byte() Endpoint_Discard_Byte()
714
715 /** Alias for Endpoint_Discard_Word().
716 */
717 #define Endpoint_Ignore_Word() Endpoint_Discard_Word()
718
719 /** Alias for Endpoint_Discard_DWord().
720 */
721 #define Endpoint_Ignore_DWord() Endpoint_Discard_DWord()
722
723 /** Alias for Endpoint_Read_Word_LE(). By default USB transfers use little endian format, thus
724 * the command with no endianness specifier indicates little endian mode.
725 */
726 #define Endpoint_Read_Word() Endpoint_Read_Word_LE()
727
728 /** Alias for Endpoint_Write_Word_LE(). By default USB transfers use little endian format, thus
729 * the command with no endianness specifier indicates little endian mode.
730 */
731 #define Endpoint_Write_Word(Word) Endpoint_Write_Word_LE(Word)
732
733 /** Alias for Endpoint_Read_DWord_LE(). By default USB transfers use little endian format, thus
734 * the command with no endianness specifier indicates little endian mode.
735 */
736 #define Endpoint_Read_DWord() Endpoint_Read_DWord_LE()
737
738 /** Alias for Endpoint_Write_DWord_LE(). By default USB transfers use little endian format, thus
739 * the command with no endianness specifier indicates little endian mode.
740 */
741 #define Endpoint_Write_DWord(DWord) Endpoint_Write_DWord_LE(DWord)
742
743 /** Alias for Endpoint_Read_Stream_LE(). By default USB transfers use little endian format, thus
744 * the command with no endianness specifier indicates little endian mode.
745 */
746 #if !defined(NO_STREAM_CALLBACKS)
747 #define Endpoint_Read_Stream(Buffer, Length, Callback) Endpoint_Read_Stream_LE(Buffer, Length, Callback)
748 #else
749 #define Endpoint_Read_Stream(Buffer, Length) Endpoint_Read_Stream_LE(Buffer, Length)
750 #endif
751
752 /** Alias for Endpoint_Write_Stream_LE(). By default USB transfers use little endian format, thus
753 * the command with no endianness specifier indicates little endian mode.
754 */
755 #if !defined(NO_STREAM_CALLBACKS)
756 #define Endpoint_Write_Stream(Buffer, Length, Callback) Endpoint_Write_Stream_LE(Buffer, Length, Callback)
757 #else
758 #define Endpoint_Write_Stream(Buffer, Length) Endpoint_Write_Stream_LE(Buffer, Length)
759 #endif
760
761 /** Alias for Endpoint_Read_Control_Stream_LE(). By default USB transfers use little endian format, thus
762 * the command with no endianness specifier indicates little endian mode.
763 */
764 #define Endpoint_Read_Control_Stream(Data, Length) Endpoint_Read_Control_Stream_LE(Data, Length)
765
766 /** Alias for Endpoint_Write_Control_Stream_LE(). By default USB transfers use little endian format, thus
767 * the command with no endianness specifier indicates little endian mode.
768 */
769 #define Endpoint_Write_Control_Stream(Data, Length) Endpoint_Write_Control_Stream_LE(Data, Length)
770
771 /* Private Interface - For use in library only: */
772 #if !defined(__DOXYGEN__)
773 /* Macros: */
774 #define Endpoint_AllocateMemory() MACROS{ UECFG1X |= (1 << ALLOC); }MACROE
775 #define Endpoint_DeallocateMemory() MACROS{ UECFG1X &= ~(1 << ALLOC); }MACROE
776
777 #define _ENDPOINT_GET_MAXSIZE(n) _ENDPOINT_GET_MAXSIZE2(ENDPOINT_DETAILS_EP ## n)
778 #define _ENDPOINT_GET_MAXSIZE2(details) _ENDPOINT_GET_MAXSIZE3(details)
779 #define _ENDPOINT_GET_MAXSIZE3(maxsize, db) maxsize
780
781 #define _ENDPOINT_GET_DOUBLEBANK(n) _ENDPOINT_GET_DOUBLEBANK2(ENDPOINT_DETAILS_EP ## n)
782 #define _ENDPOINT_GET_DOUBLEBANK2(details) _ENDPOINT_GET_DOUBLEBANK3(details)
783 #define _ENDPOINT_GET_DOUBLEBANK3(maxsize, db) db
784
785 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER)
786 #define ENDPOINT_DETAILS_EP0 64, true
787 #define ENDPOINT_DETAILS_EP1 256, true
788 #define ENDPOINT_DETAILS_EP2 64, true
789 #define ENDPOINT_DETAILS_EP3 64, true
790 #define ENDPOINT_DETAILS_EP4 64, true
791 #define ENDPOINT_DETAILS_EP5 64, true
792 #define ENDPOINT_DETAILS_EP6 64, true
793 #else
794 #define ENDPOINT_DETAILS_EP0 64, true
795 #define ENDPOINT_DETAILS_EP1 64, false
796 #define ENDPOINT_DETAILS_EP2 64, false
797 #define ENDPOINT_DETAILS_EP3 64, true
798 #define ENDPOINT_DETAILS_EP4 64, true
799 #endif
800
801 #if defined(STATIC_ENDPOINT_CONFIGURATION)
802 #define Endpoint_ConfigureEndpoint(Number, Type, Direction, Size, Banks) \
803 Endpoint_ConfigureEndpointStatic(Number, \
804 ((Type << EPTYPE0) | Direction), \
805 ((1 << ALLOC) | Banks | Endpoint_BytesToEPSizeMask(Size)));
806 #endif
807
808 /* Function Prototypes: */
809 void Endpoint_ClearEndpoints(void);
810 bool Endpoint_ConfigureEndpointStatic(const uint8_t Number, const uint8_t UECFG0XData, const uint8_t UECFG1XData);
811
812 /* Inline Functions: */
813 static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYSINLINE;
814 static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes)
815 {
816 if (Bytes <= 8)
817 return (0 << EPSIZE0);
818 else if (Bytes <= 16)
819 return (1 << EPSIZE0);
820 else if (Bytes <= 32)
821 return (2 << EPSIZE0);
822 #if defined(USB_LIMITED_CONTROLLER)
823 else
824 return (3 << EPSIZE0);
825 #else
826 else if (Bytes <= 64)
827 return (3 << EPSIZE0);
828 else if (Bytes <= 128)
829 return (4 << EPSIZE0);
830 else
831 return (5 << EPSIZE0);
832 #endif
833 };
834
835 #endif
836
837 /* Disable C linkage for C++ Compilers: */
838 #if defined(__cplusplus)
839 }
840 #endif
841
842 #endif