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
32 * \brief USB device endpoint management definitions.
34 * This file contains structures, function prototypes and macros related to the management of the device's
35 * data endpoints when the library is initialized in USB device mode.
37 * \note This file should not be included directly. It is automatically included as needed by the USB driver
38 * dispatch header located in LUFA/Drivers/USB/USB.h.
41 /** \ingroup Group_USB
42 * @defgroup Group_EndpointManagement Endpoint Management
44 * Functions, macros and enums related to endpoint management when in USB Device mode. This
45 * module contains the endpoint management macros, as well as endpoint interrupt and data
46 * send/receive functions for various data types.
51 /** @defgroup Group_EndpointRW Endpoint Data Reading and Writing
53 * Functions, macros, variables, enums and types related to data reading and writing from and to endpoints.
56 /** \ingroup Group_EndpointRW
57 * @defgroup Group_EndpointPrimitiveRW Read/Write of Primitive Data Types
59 * Functions, macros, variables, enums and types related to data reading and writing of primitive data types
60 * from and to endpoints.
63 /** \ingroup Group_EndpointRW
64 * @defgroup Group_EndpointStreamRW Read/Write of Multi-Byte Streams
66 * Functions, macros, variables, enums and types related to data reading and writing of data streams from
70 /** @defgroup Group_EndpointPacketManagement Endpoint Packet Management
72 * Functions, macros, variables, enums and types related to packet management of endpoints.
75 #ifndef __ENDPOINT_H__
76 #define __ENDPOINT_H__
80 #include <avr/pgmspace.h>
81 #include <avr/eeprom.h>
84 #include "../../../Common/Common.h"
85 #include "../HighLevel/USBTask.h"
86 #include "USBInterrupt.h"
88 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
89 #include "../HighLevel/StreamCallbacks.h"
92 /* Enable C linkage for C++ Compilers: */
93 #if defined(__cplusplus)
97 /* Preprocessor Checks: */
98 #if !defined(__INCLUDE_FROM_USB_DRIVER)
99 #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
102 /* Public Interface - May be used in end-application: */
104 /** Endpoint data direction mask for \ref Endpoint_ConfigureEndpoint(). This indicates that the endpoint
105 * should be initialized in the OUT direction - i.e. data flows from host to device.
107 #define ENDPOINT_DIR_OUT (0 << EPDIR)
109 /** Endpoint data direction mask for \ref Endpoint_ConfigureEndpoint(). This indicates that the endpoint
110 * should be initialized in the IN direction - i.e. data flows from device to host.
112 #define ENDPOINT_DIR_IN (1 << EPDIR)
114 /** Mask for the bank mode selection for the \ref Endpoint_ConfigureEndpoint() macro. This indicates
115 * that the endpoint should have one single bank, which requires less USB FIFO memory but results
116 * in slower transfers as only one USB device (the AVR or the host) can access the endpoint's
117 * bank at the one time.
119 #define ENDPOINT_BANK_SINGLE (0 << EPBK0)
121 /** Mask for the bank mode selection for the \ref Endpoint_ConfigureEndpoint() macro. This indicates
122 * that the endpoint should have two banks, which requires more USB FIFO memory but results
123 * in faster transfers as one USB device (the AVR or the host) can access one bank while the other
124 * accesses the second bank.
126 #define ENDPOINT_BANK_DOUBLE (1 << EPBK0)
128 /** Endpoint address for the default control endpoint, which always resides in address 0. This is
129 * defined for convenience to give more readable code when used with the endpoint macros.
131 #define ENDPOINT_CONTROLEP 0
133 #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
134 /** Default size of the default control endpoint's bank, until altered by the Endpoint0Size value
135 * in the device descriptor. Not available if the FIXED_CONTROL_ENDPOINT_SIZE token is defined.
137 #define ENDPOINT_CONTROLEP_DEFAULT_SIZE 8
140 /** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's
141 * numerical address in the device.
143 #define ENDPOINT_EPNUM_MASK 0x07
145 /** Endpoint direction mask, for masking against endpoint addresses to retrieve the endpoint's
146 * direction for comparing with the ENDPOINT_DESCRIPTOR_DIR_* masks.
148 #define ENDPOINT_EPDIR_MASK 0x80
150 /** Endpoint bank size mask, for masking against endpoint addresses to retrieve the endpoint's
151 * bank size in the device.
153 #define ENDPOINT_EPSIZE_MASK 0x7F
155 /** Maximum size in bytes of a given endpoint.
157 * \param[in] n Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1)
159 #define ENDPOINT_MAX_SIZE(n) _ENDPOINT_GET_MAXSIZE(n)
161 /** Indicates if the given endpoint supports double banking.
163 * \param[in] n Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1)
165 #define ENDPOINT_DOUBLEBANK_SUPPORTED(n) _ENDPOINT_GET_DOUBLEBANK(n)
167 #if !defined(CONTROL_ONLY_DEVICE)
168 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR) || defined(__DOXYGEN__)
169 /** Total number of endpoints (including the default control endpoint at address 0) which may
170 * be used in the device. Different USB AVR models support different amounts of endpoints,
171 * this value reflects the maximum number of endpoints for the currently selected AVR model.
173 #define ENDPOINT_TOTAL_ENDPOINTS 7
175 #define ENDPOINT_TOTAL_ENDPOINTS 5
178 #define ENDPOINT_TOTAL_ENDPOINTS 1
182 /** Enum for the possible error return codes of the \ref Endpoint_WaitUntilReady() function.
184 * \ingroup Group_EndpointRW
186 enum Endpoint_WaitUntilReady_ErrorCodes_t
188 ENDPOINT_READYWAIT_NoError
= 0, /**< Endpoint is ready for next packet, no error. */
189 ENDPOINT_READYWAIT_EndpointStalled
= 1, /**< The endpoint was stalled during the stream
190 * transfer by the host or device.
192 ENDPOINT_READYWAIT_DeviceDisconnected
= 2, /**< Device was disconnected from the host while
193 * waiting for the endpoint to become ready.
195 ENDPOINT_READYWAIT_BusSuspended
= 3, /**< The USB bus has been suspended by the host and
196 * no USB endpoint traffic can occur until the bus
199 ENDPOINT_READYWAIT_Timeout
= 4, /**< The host failed to accept or send the next packet
200 * within the software timeout period set by the
201 * \ref USB_STREAM_TIMEOUT_MS macro.
205 /** Enum for the possible error return codes of the Endpoint_*_Stream_* functions.
207 * \ingroup Group_EndpointStreamRW
209 enum Endpoint_Stream_RW_ErrorCodes_t
211 ENDPOINT_RWSTREAM_NoError
= 0, /**< Command completed successfully, no error. */
212 ENDPOINT_RWSTREAM_EndpointStalled
= 1, /**< The endpoint was stalled during the stream
213 * transfer by the host or device.
215 ENDPOINT_RWSTREAM_DeviceDisconnected
= 2, /**< Device was disconnected from the host during
218 ENDPOINT_RWSTREAM_BusSuspended
= 3, /**< The USB bus has been suspended by the host and
219 * no USB endpoint traffic can occur until the bus
222 ENDPOINT_RWSTREAM_Timeout
= 4, /**< The host failed to accept or send the next packet
223 * within the software timeout period set by the
224 * \ref USB_STREAM_TIMEOUT_MS macro.
226 ENDPOINT_RWSTREAM_CallbackAborted
= 5, /**< Indicates that the stream's callback function
227 * aborted the transfer early.
231 /** Enum for the possible error return codes of the Endpoint_*_Control_Stream_* functions..
233 * \ingroup Group_EndpointStreamRW
235 enum Endpoint_ControlStream_RW_ErrorCodes_t
237 ENDPOINT_RWCSTREAM_NoError
= 0, /**< Command completed successfully, no error. */
238 ENDPOINT_RWCSTREAM_HostAborted
= 1, /**< The aborted the transfer prematurely. */
239 ENDPOINT_RWCSTREAM_DeviceDisconnected
= 2, /**< Device was disconnected from the host during
242 ENDPOINT_RWCSTREAM_BusSuspended
= 3, /**< The USB bus has been suspended by the host and
243 * no USB endpoint traffic can occur until the bus
248 /* Inline Functions: */
249 /** Indicates the number of bytes currently stored in the current endpoint's selected bank.
251 * \note The return width of this function may differ, depending on the maximum endpoint bank size
252 * of the selected AVR model.
254 * \ingroup Group_EndpointRW
256 * \return Total number of bytes in the currently selected Endpoint's FIFO buffer.
258 static inline uint16_t Endpoint_BytesInEndpoint(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
259 static inline uint16_t Endpoint_BytesInEndpoint(void)
261 #if defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
263 #elif defined(USB_SERIES_4_AVR)
264 return (((uint16_t)UEBCHX
<< 8) | UEBCLX
);
265 #elif defined(USB_SERIES_2_AVR)
270 /** Get the endpoint address of the currently selected endpoint. This is typically used to save
271 * the currently selected endpoint number so that it can be restored after another endpoint has
274 * \return Index of the currently selected endpoint.
276 static inline uint8_t Endpoint_GetCurrentEndpoint(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
277 static inline uint8_t Endpoint_GetCurrentEndpoint(void)
279 #if !defined(CONTROL_ONLY_DEVICE)
280 return (UENUM
& ENDPOINT_EPNUM_MASK
);
282 return ENDPOINT_CONTROLEP
;
286 /** Selects the given endpoint number. If the address from the device descriptors is used, the
287 * value should be masked with the \ref ENDPOINT_EPNUM_MASK constant to extract only the endpoint
288 * number (and discarding the endpoint direction bit).
290 * Any endpoint operations which do not require the endpoint number to be indicated will operate on
291 * the currently selected endpoint.
293 * \param[in] EndpointNumber Endpoint number to select.
295 static inline void Endpoint_SelectEndpoint(const uint8_t EndpointNumber
) ATTR_ALWAYS_INLINE
;
296 static inline void Endpoint_SelectEndpoint(const uint8_t EndpointNumber
)
298 #if !defined(CONTROL_ONLY_DEVICE)
299 UENUM
= EndpointNumber
;
303 /** Resets the endpoint bank FIFO. This clears all the endpoint banks and resets the USB controller's
304 * In and Out pointers to the bank's contents.
306 * \param[in] EndpointNumber Endpoint number whose FIFO buffers are to be reset.
308 static inline void Endpoint_ResetFIFO(const uint8_t EndpointNumber
) ATTR_ALWAYS_INLINE
;
309 static inline void Endpoint_ResetFIFO(const uint8_t EndpointNumber
)
311 UERST
= (1 << EndpointNumber
);
315 /** Enables the currently selected endpoint so that data can be sent and received through it to
318 * \note Endpoints must first be configured properly via \ref Endpoint_ConfigureEndpoint().
320 static inline void Endpoint_EnableEndpoint(void) ATTR_ALWAYS_INLINE
;
321 static inline void Endpoint_EnableEndpoint(void)
323 UECONX
|= (1 << EPEN
);
326 /** Disables the currently selected endpoint so that data cannot be sent and received through it
327 * to and from a host.
329 static inline void Endpoint_DisableEndpoint(void) ATTR_ALWAYS_INLINE
;
330 static inline void Endpoint_DisableEndpoint(void)
332 UECONX
&= ~(1 << EPEN
);
335 /** Determines if the currently selected endpoint is enabled, but not necessarily configured.
337 * \return Boolean True if the currently selected endpoint is enabled, false otherwise.
339 static inline bool Endpoint_IsEnabled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
340 static inline bool Endpoint_IsEnabled(void)
342 return ((UECONX
& (1 << EPEN
)) ?
true : false);
345 /** Determines if the currently selected endpoint may be read from (if data is waiting in the endpoint
346 * bank and the endpoint is an OUT direction, or if the bank is not yet full if the endpoint is an IN
347 * direction). This function will return false if an error has occurred in the endpoint, if the endpoint
348 * is an OUT direction and no packet (or an empty packet) has been received, or if the endpoint is an IN
349 * direction and the endpoint bank is full.
351 * \ingroup Group_EndpointPacketManagement
353 * \return Boolean true if the currently selected endpoint may be read from or written to, depending on its direction.
355 static inline bool Endpoint_IsReadWriteAllowed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
356 static inline bool Endpoint_IsReadWriteAllowed(void)
358 return ((UEINTX
& (1 << RWAL
)) ?
true : false);
361 /** Determines if the currently selected endpoint is configured.
363 * \return Boolean true if the currently selected endpoint has been configured, false otherwise.
365 static inline bool Endpoint_IsConfigured(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
366 static inline bool Endpoint_IsConfigured(void)
368 return ((UESTA0X
& (1 << CFGOK
)) ?
true : false);
371 /** Returns a mask indicating which INTERRUPT type endpoints have interrupted - i.e. their
372 * interrupt duration has elapsed. Which endpoints have interrupted can be determined by
373 * masking the return value against (1 << {Endpoint Number}).
375 * \return Mask whose bits indicate which endpoints have interrupted.
377 static inline uint8_t Endpoint_GetEndpointInterrupts(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
378 static inline uint8_t Endpoint_GetEndpointInterrupts(void)
383 /** Determines if the specified endpoint number has interrupted (valid only for INTERRUPT type
386 * \param[in] EndpointNumber Index of the endpoint whose interrupt flag should be tested.
388 * \return Boolean true if the specified endpoint has interrupted, false otherwise.
390 static inline bool Endpoint_HasEndpointInterrupted(const uint8_t EndpointNumber
) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
391 static inline bool Endpoint_HasEndpointInterrupted(const uint8_t EndpointNumber
)
393 return ((UEINT
& (1 << EndpointNumber
)) ?
true : false);
396 /** Determines if the selected IN endpoint is ready for a new packet.
398 * \ingroup Group_EndpointPacketManagement
400 * \return Boolean true if the current endpoint is ready for an IN packet, false otherwise.
402 static inline bool Endpoint_IsINReady(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
403 static inline bool Endpoint_IsINReady(void)
405 return ((UEINTX
& (1 << TXINI
)) ?
true : false);
408 /** Determines if the selected OUT endpoint has received new packet.
410 * \ingroup Group_EndpointPacketManagement
412 * \return Boolean true if current endpoint is has received an OUT packet, false otherwise.
414 static inline bool Endpoint_IsOUTReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
415 static inline bool Endpoint_IsOUTReceived(void)
417 return ((UEINTX
& (1 << RXOUTI
)) ?
true : false);
420 /** Determines if the current CONTROL type endpoint has received a SETUP packet.
422 * \ingroup Group_EndpointPacketManagement
424 * \return Boolean true if the selected endpoint has received a SETUP packet, false otherwise.
426 static inline bool Endpoint_IsSETUPReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
427 static inline bool Endpoint_IsSETUPReceived(void)
429 return ((UEINTX
& (1 << RXSTPI
)) ?
true : false);
432 /** Clears a received SETUP packet on the currently selected CONTROL type endpoint, freeing up the
433 * endpoint for the next packet.
435 * \ingroup Group_EndpointPacketManagement
437 * \note This is not applicable for non CONTROL type endpoints.
439 static inline void Endpoint_ClearSETUP(void) ATTR_ALWAYS_INLINE
;
440 static inline void Endpoint_ClearSETUP(void)
442 UEINTX
&= ~(1 << RXSTPI
);
445 /** Sends an IN packet to the host on the currently selected endpoint, freeing up the endpoint for the
446 * next packet and switching to the alternative endpoint bank if double banked.
448 * \ingroup Group_EndpointPacketManagement
450 static inline void Endpoint_ClearIN(void) ATTR_ALWAYS_INLINE
;
451 static inline void Endpoint_ClearIN(void)
453 #if !defined(CONTROL_ONLY_DEVICE)
454 UEINTX
&= ~((1 << TXINI
) | (1 << FIFOCON
));
456 UEINTX
&= ~(1 << TXINI
);
460 /** Acknowledges an OUT packet to the host on the currently selected endpoint, freeing up the endpoint
461 * for the next packet and switching to the alternative endpoint bank if double banked.
463 * \ingroup Group_EndpointPacketManagement
465 static inline void Endpoint_ClearOUT(void) ATTR_ALWAYS_INLINE
;
466 static inline void Endpoint_ClearOUT(void)
468 #if !defined(CONTROL_ONLY_DEVICE)
469 UEINTX
&= ~((1 << RXOUTI
) | (1 << FIFOCON
));
471 UEINTX
&= ~(1 << RXOUTI
);
475 /** Stalls the current endpoint, indicating to the host that a logical problem occurred with the
476 * indicated endpoint and that the current transfer sequence should be aborted. This provides a
477 * way for devices to indicate invalid commands to the host so that the current transfer can be
478 * aborted and the host can begin its own recovery sequence.
480 * The currently selected endpoint remains stalled until either the \ref Endpoint_ClearStall() macro
481 * is called, or the host issues a CLEAR FEATURE request to the device for the currently selected
484 * \ingroup Group_EndpointPacketManagement
486 static inline void Endpoint_StallTransaction(void) ATTR_ALWAYS_INLINE
;
487 static inline void Endpoint_StallTransaction(void)
489 UECONX
|= (1 << STALLRQ
);
492 /** Clears the STALL condition on the currently selected endpoint.
494 * \ingroup Group_EndpointPacketManagement
496 static inline void Endpoint_ClearStall(void) ATTR_ALWAYS_INLINE
;
497 static inline void Endpoint_ClearStall(void)
499 UECONX
|= (1 << STALLRQC
);
502 /** Determines if the currently selected endpoint is stalled, false otherwise.
504 * \ingroup Group_EndpointPacketManagement
506 * \return Boolean true if the currently selected endpoint is stalled, false otherwise.
508 static inline bool Endpoint_IsStalled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
509 static inline bool Endpoint_IsStalled(void)
511 return ((UECONX
& (1 << STALLRQ
)) ?
true : false);
514 /** Resets the data toggle of the currently selected endpoint. */
515 static inline void Endpoint_ResetDataToggle(void) ATTR_ALWAYS_INLINE
;
516 static inline void Endpoint_ResetDataToggle(void)
518 UECONX
|= (1 << RSTDT
);
521 /** Determines the currently selected endpoint's direction.
523 * \return The currently selected endpoint's direction, as a ENDPOINT_DIR_* mask.
525 static inline uint8_t Endpoint_GetEndpointDirection(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
526 static inline uint8_t Endpoint_GetEndpointDirection(void)
528 return (UECFG0X
& ENDPOINT_DIR_IN
);
531 /** Sets the direction of the currently selected endpoint.
533 * \param[in] DirectionMask New endpoint direction, as a ENDPOINT_DIR_* mask.
535 static inline void Endpoint_SetEndpointDirection(const uint8_t DirectionMask
) ATTR_ALWAYS_INLINE
;
536 static inline void Endpoint_SetEndpointDirection(const uint8_t DirectionMask
)
538 UECFG0X
= ((UECFG0X
& ~ENDPOINT_DIR_IN
) | DirectionMask
);
541 /** Reads one byte from the currently selected endpoint's bank, for OUT direction endpoints.
543 * \ingroup Group_EndpointPrimitiveRW
545 * \return Next byte in the currently selected endpoint's FIFO buffer.
547 static inline uint8_t Endpoint_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
548 static inline uint8_t Endpoint_Read_Byte(void)
553 /** Writes one byte from the currently selected endpoint's bank, for IN direction endpoints.
555 * \ingroup Group_EndpointPrimitiveRW
557 * \param[in] Byte Next byte to write into the the currently selected endpoint's FIFO buffer.
559 static inline void Endpoint_Write_Byte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
560 static inline void Endpoint_Write_Byte(const uint8_t Byte
)
565 /** Discards one byte from the currently selected endpoint's bank, for OUT direction endpoints.
567 * \ingroup Group_EndpointPrimitiveRW
569 static inline void Endpoint_Discard_Byte(void) ATTR_ALWAYS_INLINE
;
570 static inline void Endpoint_Discard_Byte(void)
577 /** Reads two bytes from the currently selected endpoint's bank in little endian format, for OUT
578 * direction endpoints.
580 * \ingroup Group_EndpointPrimitiveRW
582 * \return Next word in the currently selected endpoint's FIFO buffer.
584 static inline uint16_t Endpoint_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
585 static inline uint16_t Endpoint_Read_Word_LE(void)
593 Data
.Bytes
[0] = UEDATX
;
594 Data
.Bytes
[1] = UEDATX
;
599 /** Reads two bytes from the currently selected endpoint's bank in big endian format, for OUT
600 * direction endpoints.
602 * \ingroup Group_EndpointPrimitiveRW
604 * \return Next word in the currently selected endpoint's FIFO buffer.
606 static inline uint16_t Endpoint_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
607 static inline uint16_t Endpoint_Read_Word_BE(void)
615 Data
.Bytes
[1] = UEDATX
;
616 Data
.Bytes
[0] = UEDATX
;
621 /** Writes two bytes to the currently selected endpoint's bank in little endian format, for IN
622 * direction endpoints.
624 * \ingroup Group_EndpointPrimitiveRW
626 * \param[in] Word Next word to write to the currently selected endpoint's FIFO buffer.
628 static inline void Endpoint_Write_Word_LE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
629 static inline void Endpoint_Write_Word_LE(const uint16_t Word
)
631 UEDATX
= (Word
& 0xFF);
632 UEDATX
= (Word
>> 8);
635 /** Writes two bytes to the currently selected endpoint's bank in big endian format, for IN
636 * direction endpoints.
638 * \ingroup Group_EndpointPrimitiveRW
640 * \param[in] Word Next word to write to the currently selected endpoint's FIFO buffer.
642 static inline void Endpoint_Write_Word_BE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
643 static inline void Endpoint_Write_Word_BE(const uint16_t Word
)
645 UEDATX
= (Word
>> 8);
646 UEDATX
= (Word
& 0xFF);
649 /** Discards two bytes from the currently selected endpoint's bank, for OUT direction endpoints.
651 * \ingroup Group_EndpointPrimitiveRW
653 static inline void Endpoint_Discard_Word(void) ATTR_ALWAYS_INLINE
;
654 static inline void Endpoint_Discard_Word(void)
662 /** Reads four bytes from the currently selected endpoint's bank in little endian format, for OUT
663 * direction endpoints.
665 * \ingroup Group_EndpointPrimitiveRW
667 * \return Next double word in the currently selected endpoint's FIFO buffer.
669 static inline uint32_t Endpoint_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
670 static inline uint32_t Endpoint_Read_DWord_LE(void)
678 Data
.Bytes
[0] = UEDATX
;
679 Data
.Bytes
[1] = UEDATX
;
680 Data
.Bytes
[2] = UEDATX
;
681 Data
.Bytes
[3] = UEDATX
;
686 /** Reads four bytes from the currently selected endpoint's bank in big endian format, for OUT
687 * direction endpoints.
689 * \ingroup Group_EndpointPrimitiveRW
691 * \return Next double word in the currently selected endpoint's FIFO buffer.
693 static inline uint32_t Endpoint_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
694 static inline uint32_t Endpoint_Read_DWord_BE(void)
702 Data
.Bytes
[3] = UEDATX
;
703 Data
.Bytes
[2] = UEDATX
;
704 Data
.Bytes
[1] = UEDATX
;
705 Data
.Bytes
[0] = UEDATX
;
710 /** Writes four bytes to the currently selected endpoint's bank in little endian format, for IN
711 * direction endpoints.
713 * \ingroup Group_EndpointPrimitiveRW
715 * \param[in] DWord Next double word to write to the currently selected endpoint's FIFO buffer.
717 static inline void Endpoint_Write_DWord_LE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
718 static inline void Endpoint_Write_DWord_LE(const uint32_t DWord
)
720 UEDATX
= (DWord
& 0xFF);
721 UEDATX
= (DWord
>> 8);
722 UEDATX
= (DWord
>> 16);
723 UEDATX
= (DWord
>> 24);
726 /** Writes four bytes to the currently selected endpoint's bank in big endian format, for IN
727 * direction endpoints.
729 * \ingroup Group_EndpointPrimitiveRW
731 * \param[in] DWord Next double word to write to the currently selected endpoint's FIFO buffer.
733 static inline void Endpoint_Write_DWord_BE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
734 static inline void Endpoint_Write_DWord_BE(const uint32_t DWord
)
736 UEDATX
= (DWord
>> 24);
737 UEDATX
= (DWord
>> 16);
738 UEDATX
= (DWord
>> 8);
739 UEDATX
= (DWord
& 0xFF);
742 /** Discards four bytes from the currently selected endpoint's bank, for OUT direction endpoints.
744 * \ingroup Group_EndpointPrimitiveRW
746 static inline void Endpoint_Discard_DWord(void) ATTR_ALWAYS_INLINE
;
747 static inline void Endpoint_Discard_DWord(void)
757 /* External Variables: */
758 /** Global indicating the maximum packet size of the default control endpoint located at address
759 * 0 in the device. This value is set to the value indicated in the device descriptor in the user
760 * project once the USB interface is initialized into device mode.
762 * If space is an issue, it is possible to fix this to a static value by defining the control
763 * endpoint size in the FIXED_CONTROL_ENDPOINT_SIZE token passed to the compiler in the makefile
764 * via the -D switch. When a fixed control endpoint size is used, the size is no longer dynamically
765 * read from the descriptors at runtime and instead fixed to the given value. When used, it is
766 * important that the descriptor control endpoint size value matches the size given as the
767 * FIXED_CONTROL_ENDPOINT_SIZE token - it is recommended that the FIXED_CONTROL_ENDPOINT_SIZE token
768 * be used in the descriptors to ensure this.
770 * \note This variable should be treated as read-only in the user application, and never manually
773 #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
774 extern uint8_t USB_ControlEndpointSize
;
776 #define USB_ControlEndpointSize FIXED_CONTROL_ENDPOINT_SIZE
779 /* Function Prototypes: */
780 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
781 #define __CALLBACK_PARAM , StreamCallbackPtr_t Callback
783 #define __CALLBACK_PARAM
786 /** Configures the specified endpoint number with the given endpoint type, direction, bank size
787 * and banking mode. Endpoints should be allocated in ascending order by their address in the
788 * device (i.e. endpoint 1 should be configured before endpoint 2 and so on) to prevent fragmentation
789 * of the USB FIFO memory.
791 * The endpoint type may be one of the EP_TYPE_* macros listed in LowLevel.h and the direction
792 * may be either \ref ENDPOINT_DIR_OUT or \ref ENDPOINT_DIR_IN.
794 * The bank size must indicate the maximum packet size that the endpoint can handle. Different
795 * endpoint numbers can handle different maximum packet sizes - refer to the chosen USB AVR's
796 * datasheet to determine the maximum bank size for each endpoint.
798 * The banking mode may be either \ref ENDPOINT_BANK_SINGLE or \ref ENDPOINT_BANK_DOUBLE.
800 * \note The default control endpoint should not be manually configured by the user application, as
801 * it is automatically configured by the library internally.
804 * \note This routine will select the specified endpoint, and the endpoint will remain selected
805 * once the routine completes regardless of if the endpoint configuration succeeds.
807 * \return Boolean true if the configuration succeeded, false otherwise.
809 bool Endpoint_ConfigureEndpoint(const uint8_t Number
,
811 const uint8_t Direction
,
813 const uint8_t Banks
);
815 /** Spin-loops until the currently selected non-control endpoint is ready for the next packet of data
816 * to be read or written to it.
818 * \note This routine should not be called on CONTROL type endpoints.
820 * \ingroup Group_EndpointRW
822 * \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
824 uint8_t Endpoint_WaitUntilReady(void);
826 /** Completes the status stage of a control transfer on a CONTROL type endpoint automatically,
827 * with respect to the data direction. This is a convenience function which can be used to
828 * simplify user control request handling.
830 void Endpoint_ClearStatusStage(void);
832 /** Reads and discards the given number of bytes from the endpoint from the given buffer,
833 * discarding fully read packets from the host as needed. The last packet is not automatically
834 * discarded once the remaining bytes has been read; the user is responsible for manually
835 * discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between
836 * each USB packet, the given stream callback function is executed repeatedly until the next
837 * packet is ready, allowing for early aborts of stream transfers.
839 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
840 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
841 * disabled and this function has the Callback parameter omitted.
843 * \note This routine should not be used on CONTROL type endpoints.
845 * \ingroup Group_EndpointStreamRW
847 * \param[in] Length Number of bytes to send via the currently selected endpoint.
848 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
850 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
852 uint8_t Endpoint_Discard_Stream(uint16_t Length
855 /** Writes the given number of bytes to the endpoint from the given buffer in little endian,
856 * sending full packets to the host as needed. The last packet filled is not automatically sent;
857 * the user is responsible for manually sending the last written packet to the host via the
858 * \ref Endpoint_ClearIN() macro. Between each USB packet, the given stream callback function
859 * is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early
860 * aborts of stream transfers.
862 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
863 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
864 * disabled and this function has the Callback parameter omitted.
866 * \note This routine should not be used on CONTROL type endpoints.
868 * \ingroup Group_EndpointStreamRW
870 * \param[in] Buffer Pointer to the source data buffer to read from.
871 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
872 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
874 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
876 uint8_t Endpoint_Write_Stream_LE(const void* Buffer
,
878 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
880 /** EEPROM buffer source version of \ref Endpoint_Write_Stream_LE().
882 * \ingroup Group_EndpointStreamRW
884 * \param[in] Buffer Pointer to the source data buffer to read from.
885 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
886 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
888 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
890 uint8_t Endpoint_Write_EStream_LE(const void* Buffer
,
892 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
894 /** FLASH buffer source version of \ref Endpoint_Write_Stream_LE().
896 * \pre The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
898 * \ingroup Group_EndpointStreamRW
900 * \param[in] Buffer Pointer to the source data buffer to read from.
901 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
902 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
904 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
906 uint8_t Endpoint_Write_PStream_LE(const void* Buffer
,
908 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
910 /** Writes the given number of bytes to the endpoint from the given buffer in big endian,
911 * sending full packets to the host as needed. The last packet filled is not automatically sent;
912 * the user is responsible for manually sending the last written packet to the host via the
913 * \ref Endpoint_ClearIN() macro. Between each USB packet, the given stream callback function
914 * is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early
915 * aborts of stream transfers.
917 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
918 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
919 * disabled and this function has the Callback parameter omitted.
921 * \note This routine should not be used on CONTROL type endpoints.
923 * \ingroup Group_EndpointStreamRW
925 * \param[in] Buffer Pointer to the source data buffer to read from.
926 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
927 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
929 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
931 uint8_t Endpoint_Write_Stream_BE(const void* Buffer
,
933 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
935 /** EEPROM buffer source version of \ref Endpoint_Write_Stream_BE().
937 * \ingroup Group_EndpointStreamRW
939 * \param[in] Buffer Pointer to the source data buffer to read from.
940 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
941 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
943 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
945 uint8_t Endpoint_Write_EStream_BE(const void* Buffer
,
947 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
949 /** FLASH buffer source version of \ref Endpoint_Write_Stream_BE().
951 * \pre The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
953 * \ingroup Group_EndpointStreamRW
955 * \param[in] Buffer Pointer to the source data buffer to read from.
956 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
957 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
959 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
961 uint8_t Endpoint_Write_PStream_BE(const void* Buffer
,
963 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
965 /** Reads the given number of bytes from the endpoint from the given buffer in little endian,
966 * discarding fully read packets from the host as needed. The last packet is not automatically
967 * discarded once the remaining bytes has been read; the user is responsible for manually
968 * discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between
969 * each USB packet, the given stream callback function is executed repeatedly until the endpoint
970 * is ready to accept the next packet, allowing for early aborts of stream transfers.
972 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
973 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
974 * disabled and this function has the Callback parameter omitted.
976 * \note This routine should not be used on CONTROL type endpoints.
978 * \ingroup Group_EndpointStreamRW
980 * \param[out] Buffer Pointer to the destination data buffer to write to.
981 * \param[in] Length Number of bytes to send via the currently selected endpoint.
982 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
984 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
986 uint8_t Endpoint_Read_Stream_LE(void* Buffer
,
988 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
990 /** EEPROM buffer source version of \ref Endpoint_Read_Stream_LE().
992 * \ingroup Group_EndpointStreamRW
994 * \param[out] Buffer Pointer to the destination data buffer to write to, located in EEPROM memory space.
995 * \param[in] Length Number of bytes to send via the currently selected endpoint.
996 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
998 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
1000 uint8_t Endpoint_Read_EStream_LE(void* Buffer
,
1002 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1004 /** Reads the given number of bytes from the endpoint from the given buffer in big endian,
1005 * discarding fully read packets from the host as needed. The last packet is not automatically
1006 * discarded once the remaining bytes has been read; the user is responsible for manually
1007 * discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between
1008 * each USB packet, the given stream callback function is executed repeatedly until the endpoint
1009 * is ready to accept the next packet, allowing for early aborts of stream transfers.
1011 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
1012 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
1013 * disabled and this function has the Callback parameter omitted.
1015 * \note This routine should not be used on CONTROL type endpoints.
1017 * \ingroup Group_EndpointStreamRW
1019 * \param[out] Buffer Pointer to the destination data buffer to write to.
1020 * \param[in] Length Number of bytes to send via the currently selected endpoint.
1021 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
1023 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
1025 uint8_t Endpoint_Read_Stream_BE(void* Buffer
,
1027 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1029 /** EEPROM buffer source version of \ref Endpoint_Read_Stream_BE().
1031 * \ingroup Group_EndpointStreamRW
1033 * \param[out] Buffer Pointer to the destination data buffer to write to, located in EEPROM memory space.
1034 * \param[in] Length Number of bytes to send via the currently selected endpoint.
1035 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback.
1037 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
1039 uint8_t Endpoint_Read_EStream_BE(void* Buffer
,
1041 __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1043 /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in little endian,
1044 * sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared
1045 * in both failure and success states; the user is responsible for manually clearing the setup OUT to
1046 * finalize the transfer via the \ref Endpoint_ClearOUT() macro.
1048 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1049 * to clear the status stage when using this routine in a control transaction.
1052 * \note This routine should only be used on CONTROL type endpoints.
1054 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1055 * together; i.e. the entire stream data must be read or written at the one time.
1057 * \ingroup Group_EndpointStreamRW
1059 * \param[in] Buffer Pointer to the source data buffer to read from.
1060 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
1062 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1064 uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer
,
1065 uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1067 /** EEPROM buffer source version of Endpoint_Write_Control_Stream_LE.
1069 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1070 * to clear the status stage when using this routine in a control transaction.
1073 * \note This routine should only be used on CONTROL type endpoints.
1075 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1076 * together; i.e. the entire stream data must be read or written at the one time.
1078 * \ingroup Group_EndpointStreamRW
1080 * \param[in] Buffer Pointer to the source data buffer to read from.
1081 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
1083 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1085 uint8_t Endpoint_Write_Control_EStream_LE(const void* Buffer
,
1086 uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1088 /** FLASH buffer source version of \ref Endpoint_Write_Control_Stream_LE().
1090 * \pre The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
1092 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1093 * to clear the status stage when using this routine in a control transaction.
1096 * \note This routine should only be used on CONTROL type endpoints.
1098 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1099 * together; i.e. the entire stream data must be read or written at the one time.
1101 * \ingroup Group_EndpointStreamRW
1103 * \param[in] Buffer Pointer to the source data buffer to read from.
1104 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
1106 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1108 uint8_t Endpoint_Write_Control_PStream_LE(const void* Buffer
,
1109 uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1111 /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in big endian,
1112 * sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared
1113 * in both failure and success states; the user is responsible for manually clearing the setup OUT to
1114 * finalize the transfer via the \ref Endpoint_ClearOUT() macro.
1116 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1117 * to clear the status stage when using this routine in a control transaction.
1120 * \note This routine should only be used on CONTROL type endpoints.
1122 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1123 * together; i.e. the entire stream data must be read or written at the one time.
1125 * \ingroup Group_EndpointStreamRW
1127 * \param[in] Buffer Pointer to the source data buffer to read from.
1128 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
1130 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1132 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer
,
1133 uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1135 /** EEPROM buffer source version of \ref Endpoint_Write_Control_Stream_BE().
1137 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1138 * to clear the status stage when using this routine in a control transaction.
1141 * \note This routine should only be used on CONTROL type endpoints.
1143 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1144 * together; i.e. the entire stream data must be read or written at the one time.
1146 * \ingroup Group_EndpointStreamRW
1148 * \param[in] Buffer Pointer to the source data buffer to read from.
1149 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
1151 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1153 uint8_t Endpoint_Write_Control_EStream_BE(const void* Buffer
,
1154 uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1156 /** FLASH buffer source version of \ref Endpoint_Write_Control_Stream_BE().
1158 * \pre The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
1160 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1161 * to clear the status stage when using this routine in a control transaction.
1164 * \note This routine should only be used on CONTROL type endpoints.
1166 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1167 * together; i.e. the entire stream data must be read or written at the one time.
1169 * \ingroup Group_EndpointStreamRW
1171 * \param[in] Buffer Pointer to the source data buffer to read from.
1172 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
1174 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1176 uint8_t Endpoint_Write_Control_PStream_BE(const void* Buffer
,
1177 uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1179 /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in little endian,
1180 * discarding fully read packets from the host as needed. The device IN acknowledgement is not
1181 * automatically sent after success or failure states; the user is responsible for manually sending the
1182 * setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro.
1184 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1185 * to clear the status stage when using this routine in a control transaction.
1188 * \note This routine should only be used on CONTROL type endpoints.
1190 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1191 * together; i.e. the entire stream data must be read or written at the one time.
1193 * \ingroup Group_EndpointStreamRW
1195 * \param[out] Buffer Pointer to the destination data buffer to write to.
1196 * \param[in] Length Number of bytes to send via the currently selected endpoint.
1198 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1200 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer
,
1201 uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1203 /** EEPROM buffer source version of \ref Endpoint_Read_Control_Stream_LE().
1205 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1206 * to clear the status stage when using this routine in a control transaction.
1209 * \note This routine should only be used on CONTROL type endpoints.
1211 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1212 * together; i.e. the entire stream data must be read or written at the one time.
1214 * \ingroup Group_EndpointStreamRW
1216 * \param[out] Buffer Pointer to the destination data buffer to write to.
1217 * \param[in] Length Number of bytes to send via the currently selected endpoint.
1219 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1221 uint8_t Endpoint_Read_Control_EStream_LE(void* Buffer
,
1222 uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1224 /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in big endian,
1225 * discarding fully read packets from the host as needed. The device IN acknowledgement is not
1226 * automatically sent after success or failure states; the user is responsible for manually sending the
1227 * setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro.
1229 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1230 * to clear the status stage when using this routine in a control transaction.
1233 * \note This routine should only be used on CONTROL type endpoints.
1235 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1236 * together; i.e. the entire stream data must be read or written at the one time.
1238 * \ingroup Group_EndpointStreamRW
1240 * \param[out] Buffer Pointer to the destination data buffer to write to.
1241 * \param[in] Length Number of bytes to send via the currently selected endpoint.
1243 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1245 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer
,
1246 uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1248 /** EEPROM buffer source version of \ref Endpoint_Read_Control_Stream_BE().
1250 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1251 * to clear the status stage when using this routine in a control transaction.
1254 * \note This routine should only be used on CONTROL type endpoints.
1256 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1257 * together; i.e. the entire stream data must be read or written at the one time.
1259 * \ingroup Group_EndpointStreamRW
1261 * \param[out] Buffer Pointer to the destination data buffer to write to.
1262 * \param[in] Length Number of bytes to send via the currently selected endpoint.
1264 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1266 uint8_t Endpoint_Read_Control_EStream_BE(void* Buffer
,
1267 uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1269 /* Private Interface - For use in library only: */
1270 #if !defined(__DOXYGEN__)
1272 #define _ENDPOINT_GET_MAXSIZE(n) _ENDPOINT_GET_MAXSIZE2(ENDPOINT_DETAILS_EP ## n)
1273 #define _ENDPOINT_GET_MAXSIZE2(details) _ENDPOINT_GET_MAXSIZE3(details)
1274 #define _ENDPOINT_GET_MAXSIZE3(maxsize, db) maxsize
1276 #define _ENDPOINT_GET_DOUBLEBANK(n) _ENDPOINT_GET_DOUBLEBANK2(ENDPOINT_DETAILS_EP ## n)
1277 #define _ENDPOINT_GET_DOUBLEBANK2(details) _ENDPOINT_GET_DOUBLEBANK3(details)
1278 #define _ENDPOINT_GET_DOUBLEBANK3(maxsize, db) db
1280 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
1281 #define ENDPOINT_DETAILS_EP0 64, true
1282 #define ENDPOINT_DETAILS_EP1 256, true
1283 #define ENDPOINT_DETAILS_EP2 64, true
1284 #define ENDPOINT_DETAILS_EP3 64, true
1285 #define ENDPOINT_DETAILS_EP4 64, true
1286 #define ENDPOINT_DETAILS_EP5 64, true
1287 #define ENDPOINT_DETAILS_EP6 64, true
1289 #define ENDPOINT_DETAILS_EP0 64, true
1290 #define ENDPOINT_DETAILS_EP1 64, false
1291 #define ENDPOINT_DETAILS_EP2 64, false
1292 #define ENDPOINT_DETAILS_EP3 64, true
1293 #define ENDPOINT_DETAILS_EP4 64, true
1296 #define Endpoint_ConfigureEndpoint(Number, Type, Direction, Size, Banks) \
1297 Endpoint_ConfigureEndpoint_Prv((Number), \
1298 (((Type) << EPTYPE0) | (Direction)), \
1299 ((1 << ALLOC) | (Banks) | \
1300 (__builtin_constant_p(Size) ? \
1301 Endpoint_BytesToEPSizeMask(Size) : \
1302 Endpoint_BytesToEPSizeMaskDynamic(Size))))
1304 /* Inline Functions: */
1305 static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes
) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE
;
1306 static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes
)
1308 uint8_t MaskVal
= 0;
1309 uint16_t CheckBytes
= 8;
1311 while (CheckBytes
< Bytes
)
1317 return (MaskVal
<< EPSIZE0
);
1320 /* Function Prototypes: */
1321 void Endpoint_ClearEndpoints(void);
1322 uint8_t Endpoint_BytesToEPSizeMaskDynamic(const uint16_t Size
);
1323 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number
,
1324 const uint8_t UECFG0XData
,
1325 const uint8_t UECFG1XData
);
1328 /* Disable C linkage for C++ Compilers: */
1329 #if defined(__cplusplus)