3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, 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.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
31 /** \ingroup Group_USB
32 * @defgroup Group_EndpointManagement Endpoint Management
34 * Functions, macros and enums related to endpoint management when in USB Device mode. This
35 * module contains the endpoint management macros, as well as endpoint interrupt and data
36 * send/recieve functions for various data types.
41 /** @defgroup Group_EndpointRW Endpoint Data Reading and Writing
43 * Functions, macros, variables, enums and types related to data reading and writing from and to endpoints.
46 /** @defgroup Group_EndpointPacketManagement Endpoint Packet Management
48 * Functions, macros, variables, enums and types related to packet management of endpoints.
51 #ifndef __ENDPOINT_H__
52 #define __ENDPOINT_H__
58 #include "../../../Common/Common.h"
59 #include "../HighLevel/USBTask.h"
61 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
62 #include "../HighLevel/StreamCallbacks.h"
65 /* Enable C linkage for C++ Compilers: */
66 #if defined(__cplusplus)
70 /* Public Interface - May be used in end-application: */
72 /** Endpoint data direction mask for \ref Endpoint_ConfigureEndpoint(). This indicates that the endpoint
73 * should be initialized in the OUT direction - i.e. data flows from host to device.
75 #define ENDPOINT_DIR_OUT (0 << EPDIR)
77 /** Endpoint data direction mask for \ref Endpoint_ConfigureEndpoint(). This indicates that the endpoint
78 * should be initialized in the IN direction - i.e. data flows from device to host.
80 #define ENDPOINT_DIR_IN (1 << EPDIR)
82 /** Mask for the bank mode selection for the \ref Endpoint_ConfigureEndpoint() macro. This indicates
83 * that the endpoint should have one single bank, which requires less USB FIFO memory but results
84 * in slower transfers as only one USB device (the AVR or the host) can access the endpoint's
85 * bank at the one time.
87 #define ENDPOINT_BANK_SINGLE (0 << EPBK0)
89 /** Mask for the bank mode selection for the \ref Endpoint_ConfigureEndpoint() macro. This indicates
90 * that the endpoint should have two banks, which requires more USB FIFO memory but results
91 * in faster transfers as one USB device (the AVR or the host) can access one bank while the other
92 * accesses the second bank.
94 #define ENDPOINT_BANK_DOUBLE (1 << EPBK0)
96 /** Endpoint address for the default control endpoint, which always resides in address 0. This is
97 * defined for convenience to give more readable code when used with the endpoint macros.
99 #define ENDPOINT_CONTROLEP 0
101 #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
102 /** Default size of the default control endpoint's bank, until altered by the Endpoint0Size value
103 * in the device descriptor. Not available if the FIXED_CONTROL_ENDPOINT_SIZE token is defined.
105 #define ENDPOINT_CONTROLEP_DEFAULT_SIZE 8
108 /** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's
109 * numerical address in the device.
111 #define ENDPOINT_EPNUM_MASK 0x07
113 /** Endpoint bank size mask, for masking against endpoint addresses to retrieve the endpoint's
114 * bank size in the device.
116 #define ENDPOINT_EPSIZE_MASK 0x7FF
118 /** Maximum size in bytes of a given endpoint.
120 * \param n Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1)
122 #define ENDPOINT_MAX_SIZE(n) _ENDPOINT_GET_MAXSIZE(n)
124 /** Indicates if the given endpoint supports double banking.
126 * \param n Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1)
128 #define ENDPOINT_DOUBLEBANK_SUPPORTED(n) _ENDPOINT_GET_DOUBLEBANK(n)
130 #if !defined(CONTROL_ONLY_DEVICE)
131 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER) || defined(__DOXYGEN__)
132 /** Total number of endpoints (including the default control endpoint at address 0) which may
133 * be used in the device. Different USB AVR models support different amounts of endpoints,
134 * this value reflects the maximum number of endpoints for the currently selected AVR model.
136 #define ENDPOINT_TOTAL_ENDPOINTS 7
138 #define ENDPOINT_TOTAL_ENDPOINTS 5
141 #define ENDPOINT_TOTAL_ENDPOINTS 1
144 /* Pseudo-Function Macros: */
145 #if defined(__DOXYGEN__)
146 /** Indicates the number of bytes currently stored in the current endpoint's selected bank.
148 * \note The return width of this function may differ, depending on the maximum endpoint bank size
149 * of the selected AVR model.
151 * \ingroup Group_EndpointRW
153 * \return Total number of bytes in the currently selected Endpoint's FIFO buffer
155 static inline uint16_t Endpoint_BytesInEndpoint(void);
157 /** Get the endpoint address of the currently selected endpoint. This is typically used to save
158 * the currently selected endpoint number so that it can be restored after another endpoint has
161 * \return Index of the currently selected endpoint
163 static inline uint8_t Endpoint_GetCurrentEndpoint(void);
165 /** Selects the given endpoint number. If the address from the device descriptors is used, the
166 * value should be masked with the \ref ENDPOINT_EPNUM_MASK constant to extract only the endpoint
167 * number (and discarding the endpoint direction bit).
169 * Any endpoint operations which do not require the endpoint number to be indicated will operate on
170 * the currently selected endpoint.
172 * \param EndpointNumber Endpoint number to select
174 static inline void Endpoint_SelectEndpoint(uint8_t EndpointNumber
);
176 /** Resets the endpoint bank FIFO. This clears all the endpoint banks and resets the USB controller's
177 * In and Out pointers to the bank's contents.
179 * \param EndpointNumber Endpoint number whose FIFO buffers are to be reset
181 static inline void Endpoint_ResetFIFO(uint8_t EndpointNumber
);
183 /** Enables the currently selected endpoint so that data can be sent and received through it to
186 * \note Endpoints must first be configured properly via \ref Endpoint_ConfigureEndpoint().
188 static inline void Endpoint_EnableEndpoint(void);
190 /** Disables the currently selected endpoint so that data cannot be sent and received through it
191 * to and from a host.
193 static inline void Endpoint_DisableEndpoint(void);
195 /** Determines if the currently selected endpoint is enabled, but not necessarily configured.
197 * \return Boolean True if the currently selected endpoint is enabled, false otherwise
199 static inline bool Endpoint_IsEnabled(void);
201 /** Determines if the currently selected endpoint may be read from (if data is waiting in the endpoint
202 * bank and the endpoint is an OUT direction, or if the bank is not yet full if the endpoint is an IN
203 * direction). This function will return false if an error has occurred in the endpoint, if the endpoint
204 * is an OUT direction and no packet (or an empty packet) has been received, or if the endpoint is an IN
205 * direction and the endpoint bank is full.
207 * \ingroup Group_EndpointPacketManagement
209 * \return Boolean true if the currently selected endpoint may be read from or written to, depending on its direction
211 static inline bool Endpoint_IsReadWriteAllowed(void);
213 /** Determines if the currently selected endpoint is configured.
215 * \return Boolean true if the currently selected endpoint has been configured, false otherwise
217 static inline bool Endpoint_IsConfigured(void);
219 /** Returns a mask indicating which INTERRUPT type endpoints have interrupted - i.e. their
220 * interrupt duration has elapsed. Which endpoints have interrupted can be determined by
221 * masking the return value against (1 << {Endpoint Number}).
223 * \return Mask whose bits indicate which endpoints have interrupted
225 static inline uint8_t Endpoint_GetEndpointInterrupts(void);
227 /** Determines if the specified endpoint number has interrupted (valid only for INTERRUPT type
230 * \param EndpointNumber Index of the endpoint whose interrupt flag should be tested
232 * \return Boolean true if the specified endpoint has interrupted, false otherwise
234 static inline bool Endpoint_HasEndpointInterrupted(uint8_t EndpointNumber
);
236 /** Determines if the selected IN endpoint is ready for a new packet.
238 * \ingroup Group_EndpointPacketManagement
240 * \return Boolean true if the current endpoint is ready for an IN packet, false otherwise.
242 static inline bool Endpoint_IsINReady(void);
244 /** Determines if the selected OUT endpoint has received new packet.
246 * \ingroup Group_EndpointPacketManagement
248 * \return Boolean true if current endpoint is has received an OUT packet, false otherwise.
250 static inline bool Endpoint_IsOUTReceived(void);
252 /** Determines if the current CONTROL type endpoint has received a SETUP packet.
254 * \ingroup Group_EndpointPacketManagement
256 * \return Boolean true if the selected endpoint has received a SETUP packet, false otherwise.
258 static inline bool Endpoint_IsSETUPReceived(void);
260 /** Clears a received SETUP packet on the currently selected CONTROL type endpoint, freeing up the
261 * endpoint for the next packet.
263 * \ingroup Group_EndpointPacketManagement
265 * \note This is not applicable for non CONTROL type endpoints.
267 static inline void Endpoint_ClearSETUP(void);
269 /** Sends an IN packet to the host on the currently selected endpoint, freeing up the endpoint for the
270 * next packet and switching to the alternative endpoint bank if double banked.
272 * \ingroup Group_EndpointPacketManagement
274 static inline void Endpoint_ClearIN(void);
276 /** Acknowledges an OUT packet to the host on the currently selected endpoint, freeing up the endpoint
277 * for the next packet and switching to the alternative endpoint bank if double banked.
279 * \ingroup Group_EndpointPacketManagement
281 static inline void Endpoint_ClearOUT(void);
283 /** Stalls the current endpoint, indicating to the host that a logical problem occurred with the
284 * indicated endpoint and that the current transfer sequence should be aborted. This provides a
285 * way for devices to indicate invalid commands to the host so that the current transfer can be
286 * aborted and the host can begin its own recovery sequence.
288 * The currently selected endpoint remains stalled until either the \ref Endpoint_ClearStall() macro
289 * is called, or the host issues a CLEAR FEATURE request to the device for the currently selected
292 * \ingroup Group_EndpointPacketManagement
294 static inline void Endpoint_StallTransaction(void);
296 /** Clears the STALL condition on the currently selected endpoint.
298 * \ingroup Group_EndpointPacketManagement
300 static inline void Endpoint_ClearStall(void);
302 /** Determines if the currently selected endpoint is stalled, false otherwise.
304 * \ingroup Group_EndpointPacketManagement
306 * \return Boolean true if the currently selected endpoint is stalled, false otherwise
308 static inline bool Endpoint_IsStalled(void);
310 /** Resets the data toggle of the currently selected endpoint. */
311 static inline void Endpoint_ResetDataToggle(void);
313 /** Determines the currently selected endpoint's direction.
315 * \return The currently selected endpoint's direction, as a ENDPOINT_DIR_* mask.
317 static inline uint8_t Endpoint_GetEndpointDirection(void);
319 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER) || defined(__DOXYGEN__)
320 #define Endpoint_BytesInEndpoint() UEBCX
322 #define Endpoint_BytesInEndpoint() UEBCLX
325 #if !defined(CONTROL_ONLY_DEVICE)
326 #define Endpoint_GetCurrentEndpoint() (UENUM & ENDPOINT_EPNUM_MASK)
328 #define Endpoint_GetCurrentEndpoint() ENDPOINT_CONTROLEP
331 #if !defined(CONTROL_ONLY_DEVICE)
332 #define Endpoint_SelectEndpoint(epnum) MACROS{ UENUM = epnum; }MACROE
334 #define Endpoint_SelectEndpoint(epnum) (void)epnum
337 #define Endpoint_ResetFIFO(epnum) MACROS{ UERST = (1 << epnum); UERST = 0; }MACROE
339 #define Endpoint_EnableEndpoint() MACROS{ UECONX |= (1 << EPEN); }MACROE
341 #define Endpoint_DisableEndpoint() MACROS{ UECONX &= ~(1 << EPEN); }MACROE
343 #define Endpoint_IsEnabled() ((UECONX & (1 << EPEN)) ? true : false)
345 #if !defined(CONTROL_ONLY_DEVICE)
346 #define Endpoint_IsReadWriteAllowed() ((UEINTX & (1 << RWAL)) ? true : false)
349 #define Endpoint_IsConfigured() ((UESTA0X & (1 << CFGOK)) ? true : false)
351 #define Endpoint_GetEndpointInterrupts() UEINT
353 #define Endpoint_HasEndpointInterrupted(n) ((UEINT & (1 << n)) ? true : false)
355 #define Endpoint_IsINReady() ((UEINTX & (1 << TXINI)) ? true : false)
357 #define Endpoint_IsOUTReceived() ((UEINTX & (1 << RXOUTI)) ? true : false)
359 #define Endpoint_IsSETUPReceived() ((UEINTX & (1 << RXSTPI)) ? true : false)
361 #define Endpoint_ClearSETUP() MACROS{ UEINTX &= ~(1 << RXSTPI); }MACROE
363 #if !defined(CONTROL_ONLY_DEVICE)
364 #define Endpoint_ClearIN() MACROS{ uint8_t Temp = UEINTX; UEINTX = (Temp & ~(1 << TXINI)); \
365 UEINTX = (Temp & ~(1 << FIFOCON)); }MACROE
367 #define Endpoint_ClearIN() MACROS{ UEINTX &= ~(1 << TXINI); }MACROE
370 #if !defined(CONTROL_ONLY_DEVICE)
371 #define Endpoint_ClearOUT() MACROS{ uint8_t Temp = UEINTX; UEINTX = (Temp & ~(1 << RXOUTI)); \
372 UEINTX = (Temp & ~(1 << FIFOCON)); }MACROE
374 #define Endpoint_ClearOUT() MACROS{ UEINTX &= ~(1 << RXOUTI); }MACROE
377 #define Endpoint_StallTransaction() MACROS{ UECONX |= (1 << STALLRQ); }MACROE
379 #define Endpoint_ClearStall() MACROS{ UECONX |= (1 << STALLRQC); }MACROE
381 #define Endpoint_IsStalled() ((UECONX & (1 << STALLRQ)) ? true : false)
383 #define Endpoint_ResetDataToggle() MACROS{ UECONX |= (1 << RSTDT); }MACROE
385 #define Endpoint_GetEndpointDirection() (UECFG0X & ENDPOINT_DIR_IN)
389 /** Enum for the possible error return codes of the \ref Endpoint_WaitUntilReady() function.
391 * \ingroup Group_EndpointRW
393 enum Endpoint_WaitUntilReady_ErrorCodes_t
395 ENDPOINT_READYWAIT_NoError
= 0, /**< Endpoint is ready for next packet, no error. */
396 ENDPOINT_READYWAIT_EndpointStalled
= 1, /**< The endpoint was stalled during the stream
397 * transfer by the host or device.
399 ENDPOINT_READYWAIT_DeviceDisconnected
= 2, /**< Device was disconnected from the host while
400 * waiting for the endpoint to become ready.
402 ENDPOINT_READYWAIT_Timeout
= 3, /**< The host failed to accept or send the next packet
403 * within the software timeout period set by the
404 * \ref USB_STREAM_TIMEOUT_MS macro.
408 /** Enum for the possible error return codes of the Endpoint_*_Stream_* functions.
410 * \ingroup Group_EndpointRW
412 enum Endpoint_Stream_RW_ErrorCodes_t
414 ENDPOINT_RWSTREAM_NoError
= 0, /**< Command completed successfully, no error. */
415 ENDPOINT_RWSTREAM_EndpointStalled
= 1, /**< The endpoint was stalled during the stream
416 * transfer by the host or device.
418 ENDPOINT_RWSTREAM_DeviceDisconnected
= 1, /**< Device was disconnected from the host during
421 ENDPOINT_RWSTREAM_Timeout
= 2, /**< The host failed to accept or send the next packet
422 * within the software timeout period set by the
423 * \ref USB_STREAM_TIMEOUT_MS macro.
425 ENDPOINT_RWSTREAM_CallbackAborted
= 3, /**< Indicates that the stream's callback function
426 * aborted the transfer early.
430 /** Enum for the possible error return codes of the Endpoint_*_Control_Stream_* functions..
432 * \ingroup Group_EndpointRW
434 enum Endpoint_ControlStream_RW_ErrorCodes_t
436 ENDPOINT_RWCSTREAM_NoError
= 0, /**< Command completed successfully, no error. */
437 ENDPOINT_RWCSTREAM_HostAborted
= 1, /**< The aborted the transfer prematurely. */
440 /* Inline Functions: */
441 /** Reads one byte from the currently selected endpoint's bank, for OUT direction endpoints.
443 * \ingroup Group_EndpointRW
445 * \return Next byte in the currently selected endpoint's FIFO buffer
447 static inline uint8_t Endpoint_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
448 static inline uint8_t Endpoint_Read_Byte(void)
453 /** Writes one byte from the currently selected endpoint's bank, for IN direction endpoints.
455 * \ingroup Group_EndpointRW
457 * \param Byte Next byte to write into the the currently selected endpoint's FIFO buffer
459 static inline void Endpoint_Write_Byte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
460 static inline void Endpoint_Write_Byte(const uint8_t Byte
)
465 /** Discards one byte from the currently selected endpoint's bank, for OUT direction endpoints.
467 * \ingroup Group_EndpointRW
469 static inline void Endpoint_Discard_Byte(void) ATTR_ALWAYS_INLINE
;
470 static inline void Endpoint_Discard_Byte(void)
477 /** Reads two bytes from the currently selected endpoint's bank in little endian format, for OUT
478 * direction endpoints.
480 * \ingroup Group_EndpointRW
482 * \return Next word in the currently selected endpoint's FIFO buffer
484 static inline uint16_t Endpoint_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
485 static inline uint16_t Endpoint_Read_Word_LE(void)
490 Data
|= (((uint16_t)UEDATX
) << 8);
495 /** Reads two bytes from the currently selected endpoint's bank in big endian format, for OUT
496 * direction endpoints.
498 * \ingroup Group_EndpointRW
500 * \return Next word in the currently selected endpoint's FIFO buffer
502 static inline uint16_t Endpoint_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
503 static inline uint16_t Endpoint_Read_Word_BE(void)
507 Data
= (((uint16_t)UEDATX
) << 8);
513 /** Writes two bytes to the currently selected endpoint's bank in little endian format, for IN
514 * direction endpoints.
516 * \ingroup Group_EndpointRW
518 * \param Word Next word to write to the currently selected endpoint's FIFO buffer
520 static inline void Endpoint_Write_Word_LE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
521 static inline void Endpoint_Write_Word_LE(const uint16_t Word
)
523 UEDATX
= (Word
& 0xFF);
524 UEDATX
= (Word
>> 8);
527 /** Writes two bytes to the currently selected endpoint's bank in big endian format, for IN
528 * direction endpoints.
530 * \ingroup Group_EndpointRW
532 * \param Word Next word to write to the currently selected endpoint's FIFO buffer
534 static inline void Endpoint_Write_Word_BE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
535 static inline void Endpoint_Write_Word_BE(const uint16_t Word
)
537 UEDATX
= (Word
>> 8);
538 UEDATX
= (Word
& 0xFF);
541 /** Discards two bytes from the currently selected endpoint's bank, for OUT direction endpoints.
543 * \ingroup Group_EndpointRW
545 static inline void Endpoint_Discard_Word(void) ATTR_ALWAYS_INLINE
;
546 static inline void Endpoint_Discard_Word(void)
554 /** Reads four bytes from the currently selected endpoint's bank in little endian format, for OUT
555 * direction endpoints.
557 * \ingroup Group_EndpointRW
559 * \return Next double word in the currently selected endpoint's FIFO buffer
561 static inline uint32_t Endpoint_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
562 static inline uint32_t Endpoint_Read_DWord_LE(void)
570 Data
.Bytes
[0] = UEDATX
;
571 Data
.Bytes
[1] = UEDATX
;
572 Data
.Bytes
[2] = UEDATX
;
573 Data
.Bytes
[3] = UEDATX
;
578 /** Reads four bytes from the currently selected endpoint's bank in big endian format, for OUT
579 * direction endpoints.
581 * \ingroup Group_EndpointRW
583 * \return Next double word in the currently selected endpoint's FIFO buffer
585 static inline uint32_t Endpoint_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
586 static inline uint32_t Endpoint_Read_DWord_BE(void)
594 Data
.Bytes
[3] = UEDATX
;
595 Data
.Bytes
[2] = UEDATX
;
596 Data
.Bytes
[1] = UEDATX
;
597 Data
.Bytes
[0] = UEDATX
;
602 /** Writes four bytes to the currently selected endpoint's bank in little endian format, for IN
603 * direction endpoints.
605 * \ingroup Group_EndpointRW
607 * \param DWord Next double word to write to the currently selected endpoint's FIFO buffer
609 static inline void Endpoint_Write_DWord_LE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
610 static inline void Endpoint_Write_DWord_LE(const uint32_t DWord
)
612 UEDATX
= (DWord
& 0xFF);
613 UEDATX
= (DWord
>> 8);
614 UEDATX
= (DWord
>> 16);
615 UEDATX
= (DWord
>> 24);
618 /** Writes four bytes to the currently selected endpoint's bank in big endian format, for IN
619 * direction endpoints.
621 * \ingroup Group_EndpointRW
623 * \param DWord Next double word to write to the currently selected endpoint's FIFO buffer
625 static inline void Endpoint_Write_DWord_BE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
626 static inline void Endpoint_Write_DWord_BE(const uint32_t DWord
)
628 UEDATX
= (DWord
>> 24);
629 UEDATX
= (DWord
>> 16);
630 UEDATX
= (DWord
>> 8);
631 UEDATX
= (DWord
& 0xFF);
634 /** Discards four bytes from the currently selected endpoint's bank, for OUT direction endpoints.
636 * \ingroup Group_EndpointRW
638 static inline void Endpoint_Discard_DWord(void) ATTR_ALWAYS_INLINE
;
639 static inline void Endpoint_Discard_DWord(void)
649 /* External Variables: */
650 /** Global indicating the maximum packet size of the default control endpoint located at address
651 * 0 in the device. This value is set to the value indicated in the device descriptor in the user
652 * project once the USB interface is initialized into device mode.
654 * If space is an issue, it is possible to fix this to a static value by defining the control
655 * endpoint size in the FIXED_CONTROL_ENDPOINT_SIZE token passed to the compiler in the makefile
656 * via the -D switch. When a fixed control endpoint size is used, the size is no longer dynamically
657 * read from the descriptors at runtime and instead fixed to the given value. When used, it is
658 * important that the descriptor control endpoint size value matches the size given as the
659 * FIXED_CONTROL_ENDPOINT_SIZE token - it is recommended that the FIXED_CONTROL_ENDPOINT_SIZE token
660 * be used in the descriptors to ensure this.
662 * \note This variable should be treated as read-only in the user application, and never manually
665 #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
666 extern uint8_t USB_ControlEndpointSize
;
668 #define USB_ControlEndpointSize FIXED_CONTROL_ENDPOINT_SIZE
671 /* Function Prototypes: */
672 /** Configures the specified endpoint number with the given endpoint type, direction, bank size
673 * and banking mode. Endpoints should be allocated in ascending order by their address in the
674 * device (i.e. endpoint 1 should be configured before endpoint 2 and so on).
676 * The endpoint type may be one of the EP_TYPE_* macros listed in LowLevel.h and the direction
677 * may be either \ref ENDPOINT_DIR_OUT or \ref ENDPOINT_DIR_IN.
679 * The bank size must indicate the maximum packet size that the endpoint can handle. Different
680 * endpoint numbers can handle different maximum packet sizes - refer to the chosen USB AVR's
681 * datasheet to determine the maximum bank size for each endpoint.
683 * The banking mode may be either \ref ENDPOINT_BANK_SINGLE or \ref ENDPOINT_BANK_DOUBLE.
685 * The success of this routine can be determined via the \ref Endpoint_IsConfigured() macro.
687 * \note This routine will select the specified endpoint, and the endpoint will remain selected
688 * once the routine completes regardless of if the endpoint configuration succeeds.
690 * \return Boolean true if the configuration succeeded, false otherwise
692 bool Endpoint_ConfigureEndpoint(const uint8_t Number
, const uint8_t Type
, const uint8_t Direction
,
693 const uint16_t Size
, const uint8_t Banks
);
695 #if !defined(CONTROL_ONLY_DEVICE)
697 /** Spinloops until the currently selected non-control endpoint is ready for the next packet of data
698 * to be read or written to it.
700 * \note This routine should not be called on CONTROL type endpoints.
702 * \ingroup Group_EndpointRW
704 * \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
706 uint8_t Endpoint_WaitUntilReady(void);
708 /** Reads and discards the given number of bytes from the endpoint from the given buffer,
709 * discarding fully read packets from the host as needed. The last packet is not automatically
710 * discarded once the remaining bytes has been read; the user is responsible for manually
711 * discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between
712 * each USB packet, the given stream callback function is executed repeatedly until the next
713 * packet is ready, allowing for early aborts of stream transfers.
715 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
716 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
717 * disabled and this function has the Callback parameter omitted.
719 * \note This routine should not be used on CONTROL type endpoints.
721 * \ingroup Group_EndpointRW
723 * \param Length Number of bytes to send via the currently selected endpoint.
724 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
726 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
728 uint8_t Endpoint_Discard_Stream(uint16_t Length
729 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
730 , StreamCallbackPtr_t Callback
734 /** Writes the given number of bytes to the endpoint from the given buffer in little endian,
735 * sending full packets to the host as needed. The last packet filled is not automatically sent;
736 * the user is responsible for manually sending the last written packet to the host via the
737 * \ref Endpoint_ClearIN() macro. Between each USB packet, the given stream callback function
738 * is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early
739 * aborts of stream transfers.
741 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
742 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
743 * disabled and this function has the Callback parameter omitted.
745 * \note This routine should not be used on CONTROL type endpoints.
747 * \ingroup Group_EndpointRW
749 * \param Buffer Pointer to the source data buffer to read from.
750 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
751 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
753 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
755 uint8_t Endpoint_Write_Stream_LE(const void* Buffer
, uint16_t Length
756 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
757 , StreamCallbackPtr_t Callback
759 ) ATTR_NON_NULL_PTR_ARG(1);
761 /** Writes the given number of bytes to the endpoint from the given buffer in big endian,
762 * sending full packets to the host as needed. The last packet filled is not automatically sent;
763 * the user is responsible for manually sending the last written packet to the host via the
764 * \ref Endpoint_ClearIN() macro. Between each USB packet, the given stream callback function
765 * is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early
766 * aborts of stream transfers.
768 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
769 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
770 * disabled and this function has the Callback parameter omitted.
772 * \note This routine should not be used on CONTROL type endpoints.
774 * \ingroup Group_EndpointRW
776 * \param Buffer Pointer to the source data buffer to read from.
777 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
778 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
780 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
782 uint8_t Endpoint_Write_Stream_BE(const void* Buffer
, uint16_t Length
783 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
784 , StreamCallbackPtr_t Callback
786 ) ATTR_NON_NULL_PTR_ARG(1);
788 /** Reads the given number of bytes from the endpoint from the given buffer in little endian,
789 * discarding fully read packets from the host as needed. The last packet is not automatically
790 * discarded once the remaining bytes has been read; the user is responsible for manually
791 * discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between
792 * each USB packet, the given stream callback function is executed repeatedly until the endpoint
793 * is ready to accept the next packet, allowing for early aborts of stream transfers.
795 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
796 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
797 * disabled and this function has the Callback parameter omitted.
799 * \note This routine should not be used on CONTROL type endpoints.
801 * \ingroup Group_EndpointRW
803 * \param Buffer Pointer to the destination data buffer to write to.
804 * \param Length Number of bytes to send via the currently selected endpoint.
805 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
807 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
809 uint8_t Endpoint_Read_Stream_LE(void* Buffer
, uint16_t Length
810 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
811 , StreamCallbackPtr_t Callback
813 ) ATTR_NON_NULL_PTR_ARG(1);
815 /** Reads the given number of bytes from the endpoint from the given buffer in big endian,
816 * discarding fully read packets from the host as needed. The last packet is not automatically
817 * discarded once the remaining bytes has been read; the user is responsible for manually
818 * discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between
819 * each USB packet, the given stream callback function is executed repeatedly until the endpoint
820 * is ready to accept the next packet, allowing for early aborts of stream transfers.
822 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
823 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
824 * disabled and this function has the Callback parameter omitted.
826 * \note This routine should not be used on CONTROL type endpoints.
828 * \ingroup Group_EndpointRW
830 * \param Buffer Pointer to the destination data buffer to write to.
831 * \param Length Number of bytes to send via the currently selected endpoint.
832 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
834 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
836 uint8_t Endpoint_Read_Stream_BE(void* Buffer
, uint16_t Length
837 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
838 , StreamCallbackPtr_t Callback
840 ) ATTR_NON_NULL_PTR_ARG(1);
844 /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in little endian,
845 * sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared
846 * in both failure and success states; the user is responsible for manually clearing the setup OUT to
847 * finalize the transfer via the \ref Endpoint_ClearOUT() macro.
849 * \note This routine should only be used on CONTROL type endpoints.
851 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
852 * together; i.e. the entire stream data must be read or written at the one time.
854 * \ingroup Group_EndpointRW
856 * \param Buffer Pointer to the source data buffer to read from.
857 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
859 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
861 uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
863 /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in big endian,
864 * sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared
865 * in both failure and success states; the user is responsible for manually clearing the setup OUT to
866 * finalize the transfer via the \ref Endpoint_ClearOUT() macro.
868 * \note This routine should only be used on CONTROL type endpoints.
870 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
871 * together; i.e. the entire stream data must be read or written at the one time.
873 * \ingroup Group_EndpointRW
875 * \param Buffer Pointer to the source data buffer to read from.
876 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
878 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
880 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
882 /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in little endian,
883 * discarding fully read packets from the host as needed. The device IN acknowledgement is not
884 * automatically sent after success or failure states; the user is responsible for manually sending the
885 * setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro.
887 * \note This routine should only be used on CONTROL type endpoints.
889 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
890 * together; i.e. the entire stream data must be read or written at the one time.
892 * \ingroup Group_EndpointRW
894 * \param Buffer Pointer to the destination data buffer to write to.
895 * \param Length Number of bytes to send via the currently selected endpoint.
897 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
899 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
901 /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in big endian,
902 * discarding fully read packets from the host as needed. The device IN acknowledgement is not
903 * automatically sent after success or failure states; the user is responsible for manually sending the
904 * setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro.
906 * \note This routine should only be used on CONTROL type endpoints.
908 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
909 * together; i.e. the entire stream data must be read or written at the one time.
911 * \ingroup Group_EndpointRW
913 * \param Buffer Pointer to the destination data buffer to write to.
914 * \param Length Number of bytes to send via the currently selected endpoint.
916 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
918 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
920 /* Private Interface - For use in library only: */
921 #if !defined(__DOXYGEN__)
923 #define Endpoint_AllocateMemory() MACROS{ UECFG1X |= (1 << ALLOC); }MACROE
924 #define Endpoint_DeallocateMemory() MACROS{ UECFG1X &= ~(1 << ALLOC); }MACROE
926 #define _ENDPOINT_GET_MAXSIZE(n) _ENDPOINT_GET_MAXSIZE2(ENDPOINT_DETAILS_EP ## n)
927 #define _ENDPOINT_GET_MAXSIZE2(details) _ENDPOINT_GET_MAXSIZE3(details)
928 #define _ENDPOINT_GET_MAXSIZE3(maxsize, db) maxsize
930 #define _ENDPOINT_GET_DOUBLEBANK(n) _ENDPOINT_GET_DOUBLEBANK2(ENDPOINT_DETAILS_EP ## n)
931 #define _ENDPOINT_GET_DOUBLEBANK2(details) _ENDPOINT_GET_DOUBLEBANK3(details)
932 #define _ENDPOINT_GET_DOUBLEBANK3(maxsize, db) db
934 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER)
935 #define ENDPOINT_DETAILS_EP0 64, true
936 #define ENDPOINT_DETAILS_EP1 256, true
937 #define ENDPOINT_DETAILS_EP2 64, true
938 #define ENDPOINT_DETAILS_EP3 64, true
939 #define ENDPOINT_DETAILS_EP4 64, true
940 #define ENDPOINT_DETAILS_EP5 64, true
941 #define ENDPOINT_DETAILS_EP6 64, true
943 #define ENDPOINT_DETAILS_EP0 64, true
944 #define ENDPOINT_DETAILS_EP1 64, false
945 #define ENDPOINT_DETAILS_EP2 64, false
946 #define ENDPOINT_DETAILS_EP3 64, true
947 #define ENDPOINT_DETAILS_EP4 64, true
950 #define Endpoint_ConfigureEndpoint(Number, Type, Direction, Size, Banks) \
951 Endpoint_ConfigureEndpoint_Prv(Number, \
952 ((Type << EPTYPE0) | Direction), \
953 ((1 << ALLOC) | Banks | \
954 (__builtin_constant_p(Size) ? \
955 Endpoint_BytesToEPSizeMask(Size) : \
956 Endpoint_BytesToEPSizeMaskDynamic(Size))))
958 /* Function Prototypes: */
959 void Endpoint_ClearEndpoints(void);
960 uint8_t Endpoint_BytesToEPSizeMaskDynamic(const uint16_t Size
);
961 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number
, const uint8_t UECFG0XData
, const uint8_t UECFG1XData
);
963 /* Inline Functions: */
964 static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes
) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE
;
965 static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes
)
968 uint16_t CheckBytes
= 8;
970 while (CheckBytes
< Bytes
)
976 return (MaskVal
<< EPSIZE0
);
981 /* Disable C linkage for C++ Compilers: */
982 #if defined(__cplusplus)