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