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 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 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 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 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 0x03
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(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER) || defined(__DOXYGEN__)
131 /** Total number of endpoints (including the default control endpoint at address 0) which may
132 * be used in the device. Different USB AVR models support different amounts of endpoints,
133 * this value reflects the maximum number of endpoints for the currently selected AVR model.
135 #define ENDPOINT_TOTAL_ENDPOINTS 7
137 #define ENDPOINT_TOTAL_ENDPOINTS 5
140 /** Interrupt definition for the endpoint SETUP interrupt (for CONTROL type endpoints). Should be
141 * used with the USB_INT_* macros located in USBInterrupt.h.
143 * This interrupt will fire if enabled on a CONTROL type endpoint if a new control packet is
144 * received from the host.
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.
149 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
151 #define ENDPOINT_INT_SETUP UEIENX, (1 << RXSTPE), UEINTX, (1 << RXSTPI)
153 /** Interrupt definition for the endpoint IN interrupt (for INTERRUPT type endpoints). Should be
154 * used with the USB_INT_* macros located in USBInterrupt.h.
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 new packet to be written to its FIFO buffer
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.
163 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
165 #define ENDPOINT_INT_IN UEIENX, (1 << TXINE) , UEINTX, (1 << TXINI)
167 /** Interrupt definition for the endpoint OUT interrupt (for INTERRUPT type endpoints). Should be
168 * used with the USB_INT_* macros located in USBInterrupt.h.
170 * This interrupt will fire if enabled on an INTERRUPT type endpoint if a the endpoint interrupt
171 * period has elapsed and the endpoint is ready for a packet from the host to be read from its
172 * FIFO buffer (if received).
174 * \note This interrupt must be enabled and cleared on *each* endpoint which requires it (after the
175 * endpoint is selected), and will fire the common endpoint interrupt vector.
177 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
179 #define ENDPOINT_INT_OUT UEIENX, (1 << RXOUTE), UEINTX, (1 << RXOUTI)
181 /* Pseudo-Function Macros: */
182 #if defined(__DOXYGEN__)
183 /** Indicates the number of bytes currently stored in the current endpoint's selected bank.
185 * \note The return width of this function may differ, depending on the maximum endpoint bank size
186 * of the selected AVR model.
188 * \ingroup Group_EndpointRW
190 * \return Total number of bytes in the currently selected Endpoint's FIFO buffer
192 static inline uint16_t Endpoint_BytesInEndpoint(void);
194 /** Get the endpoint address of the currently selected endpoint. This is typically used to save
195 * the currently selected endpoint number so that it can be restored after another endpoint has
198 * \return Index of the currently selected endpoint
200 static inline uint8_t Endpoint_GetCurrentEndpoint(void);
202 /** Selects the given endpoint number. If the address from the device descriptors is used, the
203 * value should be masked with the ENDPOINT_EPNUM_MASK constant to extract only the endpoint
204 * number (and discarding the endpoint direction bit).
206 * Any endpoint operations which do not require the endpoint number to be indicated will operate on
207 * the currently selected endpoint.
209 * \param EndpointNumber Endpoint number to select
211 static inline void Endpoint_SelectEndpoint(uint8_t EndpointNumber
);
213 /** Resets the endpoint bank FIFO. This clears all the endpoint banks and resets the USB controller's
214 * In and Out pointers to the bank's contents.
216 * \param EndpointNumber Endpoint number whose FIFO buffers are to be reset
218 static inline void Endpoint_ResetFIFO(uint8_t EndpointNumber
);
220 /** Enables the currently selected endpoint so that data can be sent and received through it to
223 * \note Endpoints must first be configured properly rather than just being enabled via the
224 * Endpoint_ConfigureEndpoint() macro, which calls Endpoint_EnableEndpoint() automatically.
226 static inline void Endpoint_EnableEndpoint(void);
228 /** Disables the currently selected endpoint so that data cannot be sent and received through it
229 * to and from a host.
231 static inline void Endpoint_DisableEndpoint(void);
233 /** Determines if the currently selected endpoint is enabled, but not necessarily configured.
235 * \return Boolean True if the currently selected endpoint is enabled, false otherwise
237 static inline bool Endpoint_IsEnabled(void);
239 /** Determines if the currently selected endpoint may be read from (if data is waiting in the endpoint
240 * bank and the endpoint is an OUT direction, or if the bank is not yet full if the endpoint is an IN
241 * direction). This function will return false if an error has occurred in the endpoint, if the endpoint
242 * is an OUT direction and no packet (or an empty packet) has been received, or if the endpoint is an IN
243 * direction and the endpoint bank is full.
245 * \ingroup Group_EndpointPacketManagement
247 * \return Boolean true if the currently selected endpoint may be read from or written to, depending on its direction
249 static inline bool Endpoint_IsReadWriteAllowed(void);
251 /** Determines if the currently selected endpoint is configured.
253 * \return Boolean true if the currently selected endpoint has been configured, false otherwise
255 static inline bool Endpoint_IsConfigured(void);
257 /** Returns a mask indicating which INTERRUPT type endpoints have interrupted - i.e. their
258 * interrupt duration has elapsed. Which endpoints have interrupted can be determined by
259 * masking the return value against (1 << {Endpoint Number}).
261 * \return Mask whose bits indicate which endpoints have interrupted
263 static inline uint8_t Endpoint_GetEndpointInterrupts(void);
265 /** Clears the endpoint interrupt flag. This clears the specified endpoint number's interrupt
266 * mask in the endpoint interrupt flag register.
268 * \param EndpointNumber Index of the endpoint whose interrupt flag should be cleared
270 static inline void Endpoint_ClearEndpointInterrupt(uint8_t EndpointNumber
);
272 /** Determines if the specified endpoint number has interrupted (valid only for INTERRUPT type
275 * \param EndpointNumber Index of the endpoint whose interrupt flag should be tested
277 * \return Boolean true if the specified endpoint has interrupted, false otherwise
279 static inline bool Endpoint_HasEndpointInterrupted(uint8_t EndpointNumber
);
281 /** Determines if the selected IN endpoint is ready for a new packet.
283 * \ingroup Group_EndpointPacketManagement
285 * \return Boolean true if the current endpoint is ready for an IN packet, false otherwise.
287 static inline bool Endpoint_IsINReady(void);
289 /** Determines if the selected OUT endpoint has received new packet.
291 * \ingroup Group_EndpointPacketManagement
293 * \return Boolean true if current endpoint is has received an OUT packet, false otherwise.
295 static inline bool Endpoint_IsOUTReceived(void);
297 /** Determines if the current CONTROL type endpoint has received a SETUP packet.
299 * \ingroup Group_EndpointPacketManagement
301 * \return Boolean true if the selected endpoint has received a SETUP packet, false otherwise.
303 static inline bool Endpoint_IsSETUPReceived(void);
305 /** Clears a received SETUP packet on the currently selected CONTROL type endpoint, freeing up the
306 * endpoint for the next packet.
308 * \ingroup Group_EndpointPacketManagement
310 * \note This is not applicable for non CONTROL type endpoints.
312 static inline void Endpoint_ClearSETUP(void);
314 /** Sends an IN packet to the host on the currently selected endpoint, freeing up the endpoint for the
315 * next packet and switching to the alternative endpoint bank if double banked.
317 * \ingroup Group_EndpointPacketManagement
319 static inline void Endpoint_ClearIN(void);
321 /** Acknowledges an OUT packet to the host on the currently selected endpoint, freeing up the endpoint
322 * for the next packet and switching to the alternative endpoint bank if double banked.
324 * \ingroup Group_EndpointPacketManagement
326 static inline void Endpoint_ClearOUT(void);
328 /** Stalls the current endpoint, indicating to the host that a logical problem occurred with the
329 * indicated endpoint and that the current transfer sequence should be aborted. This provides a
330 * way for devices to indicate invalid commands to the host so that the current transfer can be
331 * aborted and the host can begin its own recovery sequence.
333 * The currently selected endpoint remains stalled until either the Endpoint_ClearStall() macro
334 * is called, or the host issues a CLEAR FEATURE request to the device for the currently selected
337 * \ingroup Group_EndpointPacketManagement
339 static inline void Endpoint_StallTransaction(void);
341 /** Clears the STALL condition on the currently selected endpoint.
343 * \ingroup Group_EndpointPacketManagement
345 static inline void Endpoint_ClearStall(void);
347 /** Determines if the currently selected endpoint is stalled, false otherwise.
349 * \ingroup Group_EndpointPacketManagement
351 * \return Boolean true if the currently selected endpoint is stalled, false otherwise
353 static inline bool Endpoint_IsStalled(void);
355 /** Resets the data toggle of the currently selected endpoint. */
356 static inline void Endpoint_ResetDataToggle(void);
358 /** Determines the currently selected endpoint's direction.
360 * \return The currently selected endpoint's direction, as a ENDPOINT_DIR_* mask.
362 static inline uint8_t Endpoint_GetEndpointDirection(void);
364 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER) || defined(__DOXYGEN__)
365 #define Endpoint_BytesInEndpoint() UEBCX
367 #define Endpoint_BytesInEndpoint() UEBCLX
370 #define Endpoint_GetCurrentEndpoint() (UENUM & ENDPOINT_EPNUM_MASK)
372 #define Endpoint_SelectEndpoint(epnum) MACROS{ UENUM = epnum; }MACROE
374 #define Endpoint_ResetFIFO(epnum) MACROS{ UERST = (1 << epnum); UERST = 0; }MACROE
376 #define Endpoint_EnableEndpoint() MACROS{ UECONX |= (1 << EPEN); }MACROE
378 #define Endpoint_DisableEndpoint() MACROS{ UECONX &= ~(1 << EPEN); }MACROE
380 #define Endpoint_IsEnabled() ((UECONX & (1 << EPEN)) ? true : false)
382 #define Endpoint_IsReadWriteAllowed() ((UEINTX & (1 << RWAL)) ? true : false)
384 #define Endpoint_IsConfigured() ((UESTA0X & (1 << CFGOK)) ? true : false)
386 #define Endpoint_GetEndpointInterrupts() UEINT
388 #define Endpoint_ClearEndpointInterrupt(n) MACROS{ UEINT &= ~(1 << n); }MACROE
390 #define Endpoint_HasEndpointInterrupted(n) ((UEINT & (1 << n)) ? true : false)
392 #define Endpoint_IsINReady() ((UEINTX & (1 << TXINI)) ? true : false)
394 #define Endpoint_IsOUTReceived() ((UEINTX & (1 << RXOUTI)) ? true : false)
396 #define Endpoint_IsSETUPReceived() ((UEINTX & (1 << RXSTPI)) ? true : false)
398 #define Endpoint_ClearSETUP() MACROS{ UEINTX &= ~(1 << RXSTPI); }MACROE
400 #define Endpoint_ClearIN() MACROS{ uint8_t Temp = UEINTX; UEINTX = (Temp & ~(1 << TXINI)); \
401 UEINTX = (Temp & ~(1 << FIFOCON)); }MACROE
403 #define Endpoint_ClearOUT() MACROS{ uint8_t Temp = UEINTX; UEINTX = (Temp & ~(1 << RXOUTI)); \
404 UEINTX = (Temp & ~(1 << FIFOCON)); }MACROE
406 #define Endpoint_StallTransaction() MACROS{ UECONX |= (1 << STALLRQ); }MACROE
408 #define Endpoint_ClearStall() MACROS{ UECONX |= (1 << STALLRQC); }MACROE
410 #define Endpoint_IsStalled() ((UECONX & (1 << STALLRQ)) ? true : false)
412 #define Endpoint_ResetDataToggle() MACROS{ UECONX |= (1 << RSTDT); }MACROE
414 #define Endpoint_GetEndpointDirection() (UECFG0X & ENDPOINT_DIR_IN)
418 /** Enum for the possible error return codes of the Endpoint_WaitUntilReady function.
420 * \ingroup Group_EndpointRW
422 enum Endpoint_WaitUntilReady_ErrorCodes_t
424 ENDPOINT_READYWAIT_NoError
= 0, /**< Endpoint is ready for next packet, no error. */
425 ENDPOINT_READYWAIT_EndpointStalled
= 1, /**< The endpoint was stalled during the stream
426 * transfer by the host or device.
428 ENDPOINT_READYWAIT_DeviceDisconnected
= 2, /**< Device was disconnected from the host while
429 * waiting for the endpoint to become ready.
431 ENDPOINT_READYWAIT_Timeout
= 3, /**< The host failed to accept or send the next packet
432 * within the software timeout period set by the
433 * USB_STREAM_TIMEOUT_MS macro.
437 /** Enum for the possible error return codes of the Endpoint_*_Stream_* functions.
439 * \ingroup Group_EndpointRW
441 enum Endpoint_Stream_RW_ErrorCodes_t
443 ENDPOINT_RWSTREAM_ERROR_NoError
= 0, /**< Command completed successfully, no error. */
444 ENDPOINT_RWSTREAM_ERROR_EndpointStalled
= 1, /**< The endpoint was stalled during the stream
445 * transfer by the host or device.
447 ENDPOINT_RWSTREAM_ERROR_DeviceDisconnected
= 1, /**< Device was disconnected from the host during
450 ENDPOINT_RWSTREAM_ERROR_Timeout
= 2, /**< The host failed to accept or send the next packet
451 * within the software timeout period set by the
452 * USB_STREAM_TIMEOUT_MS macro.
454 ENDPOINT_RWSTREAM_ERROR_CallbackAborted
= 3, /**< Indicates that the stream's callback function
455 * aborted the transfer early.
459 /** Enum for the possible error return codes of the Endpoint_*_Control_Stream_* functions..
461 * \ingroup Group_EndpointRW
463 enum Endpoint_ControlStream_RW_ErrorCodes_t
465 ENDPOINT_RWCSTREAM_ERROR_NoError
= 0, /**< Command completed successfully, no error. */
466 ENDPOINT_RWCSTREAM_ERROR_HostAborted
= 1, /**< The aborted the transfer prematurely. */
469 /* Inline Functions: */
470 /** Reads one byte from the currently selected endpoint's bank, for OUT direction endpoints.
472 * \ingroup Group_EndpointRW
474 * \return Next byte in the currently selected endpoint's FIFO buffer
476 static inline uint8_t Endpoint_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
477 static inline uint8_t Endpoint_Read_Byte(void)
482 /** Writes one byte from the currently selected endpoint's bank, for IN direction endpoints.
484 * \ingroup Group_EndpointRW
486 * \param Byte Next byte to write into the the currently selected endpoint's FIFO buffer
488 static inline void Endpoint_Write_Byte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
489 static inline void Endpoint_Write_Byte(const uint8_t Byte
)
494 /** Discards one byte from the currently selected endpoint's bank, for OUT direction endpoints.
496 * \ingroup Group_EndpointRW
498 static inline void Endpoint_Discard_Byte(void) ATTR_ALWAYS_INLINE
;
499 static inline void Endpoint_Discard_Byte(void)
506 /** Reads two bytes from the currently selected endpoint's bank in little endian format, for OUT
507 * direction endpoints.
509 * \ingroup Group_EndpointRW
511 * \return Next word in the currently selected endpoint's FIFO buffer
513 static inline uint16_t Endpoint_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
514 static inline uint16_t Endpoint_Read_Word_LE(void)
519 Data
|= (((uint16_t)UEDATX
) << 8);
524 /** Reads two bytes from the currently selected endpoint's bank in big endian format, for OUT
525 * direction endpoints.
527 * \ingroup Group_EndpointRW
529 * \return Next word in the currently selected endpoint's FIFO buffer
531 static inline uint16_t Endpoint_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
532 static inline uint16_t Endpoint_Read_Word_BE(void)
536 Data
= (((uint16_t)UEDATX
) << 8);
542 /** Writes two bytes to the currently selected endpoint's bank in little endian format, for IN
543 * direction endpoints.
545 * \ingroup Group_EndpointRW
547 * \param Word Next word to write to the currently selected endpoint's FIFO buffer
549 static inline void Endpoint_Write_Word_LE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
550 static inline void Endpoint_Write_Word_LE(const uint16_t Word
)
552 UEDATX
= (Word
& 0xFF);
553 UEDATX
= (Word
>> 8);
556 /** Writes two bytes to the currently selected endpoint's bank in big endian format, for IN
557 * direction endpoints.
559 * \ingroup Group_EndpointRW
561 * \param Word Next word to write to the currently selected endpoint's FIFO buffer
563 static inline void Endpoint_Write_Word_BE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
564 static inline void Endpoint_Write_Word_BE(const uint16_t Word
)
566 UEDATX
= (Word
>> 8);
567 UEDATX
= (Word
& 0xFF);
570 /** Discards two bytes from the currently selected endpoint's bank, for OUT direction endpoints.
572 * \ingroup Group_EndpointRW
574 static inline void Endpoint_Discard_Word(void) ATTR_ALWAYS_INLINE
;
575 static inline void Endpoint_Discard_Word(void)
583 /** Reads four bytes from the currently selected endpoint's bank in little endian format, for OUT
584 * direction endpoints.
586 * \ingroup Group_EndpointRW
588 * \return Next double word in the currently selected endpoint's FIFO buffer
590 static inline uint32_t Endpoint_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
591 static inline uint32_t Endpoint_Read_DWord_LE(void)
599 Data
.Bytes
[0] = UEDATX
;
600 Data
.Bytes
[1] = UEDATX
;
601 Data
.Bytes
[2] = UEDATX
;
602 Data
.Bytes
[3] = UEDATX
;
607 /** Reads four bytes from the currently selected endpoint's bank in big endian format, for OUT
608 * direction endpoints.
610 * \ingroup Group_EndpointRW
612 * \return Next double word in the currently selected endpoint's FIFO buffer
614 static inline uint32_t Endpoint_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
615 static inline uint32_t Endpoint_Read_DWord_BE(void)
623 Data
.Bytes
[3] = UEDATX
;
624 Data
.Bytes
[2] = UEDATX
;
625 Data
.Bytes
[1] = UEDATX
;
626 Data
.Bytes
[0] = UEDATX
;
631 /** Writes four bytes to the currently selected endpoint's bank in little endian format, for IN
632 * direction endpoints.
634 * \ingroup Group_EndpointRW
636 * \param DWord Next double word to write to the currently selected endpoint's FIFO buffer
638 static inline void Endpoint_Write_DWord_LE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
639 static inline void Endpoint_Write_DWord_LE(const uint32_t DWord
)
641 UEDATX
= (DWord
& 0xFF);
642 UEDATX
= (DWord
>> 8);
643 UEDATX
= (DWord
>> 16);
644 UEDATX
= (DWord
>> 24);
647 /** Writes four bytes to the currently selected endpoint's bank in big endian format, for IN
648 * direction endpoints.
650 * \ingroup Group_EndpointRW
652 * \param DWord Next double word to write to the currently selected endpoint's FIFO buffer
654 static inline void Endpoint_Write_DWord_BE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
655 static inline void Endpoint_Write_DWord_BE(const uint32_t DWord
)
657 UEDATX
= (DWord
>> 24);
658 UEDATX
= (DWord
>> 16);
659 UEDATX
= (DWord
>> 8);
660 UEDATX
= (DWord
& 0xFF);
663 /** Discards four bytes from the currently selected endpoint's bank, for OUT direction endpoints.
665 * \ingroup Group_EndpointRW
667 static inline void Endpoint_Discard_DWord(void) ATTR_ALWAYS_INLINE
;
668 static inline void Endpoint_Discard_DWord(void)
678 /* External Variables: */
679 /** Global indicating the maximum packet size of the default control endpoint located at address
680 * 0 in the device. This value is set to the value indicated in the device descriptor in the user
681 * project once the USB interface is initialized into device mode.
683 * If space is an issue, it is possible to fix this to a static value by defining the control
684 * endpoint size in the FIXED_CONTROL_ENDPOINT_SIZE token passed to the compiler in the makefile
685 * via the -D switch. When a fixed control endpoint size is used, the size is no longer dynamically
686 * read from the descriptors at runtime and instead fixed to the given value. When used, it is
687 * important that the descriptor control endpoint size value matches the size given as the
688 * FIXED_CONTROL_ENDPOINT_SIZE token - it is recommended that the FIXED_CONTROL_ENDPOINT_SIZE token
689 * be used in the descriptors to ensure this.
691 * \note This variable should be treated as read-only in the user application, and never manually
694 #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
695 extern uint8_t USB_ControlEndpointSize
;
697 #define USB_ControlEndpointSize FIXED_CONTROL_ENDPOINT_SIZE
700 /* Function Prototypes: */
701 /** Configures the specified endpoint number with the given endpoint type, direction, bank size
702 * and banking mode. Endpoints should be allocated in ascending order by their address in the
703 * device (i.e. endpoint 1 should be configured before endpoint 2 and so on).
705 * The endpoint type may be one of the EP_TYPE_* macros listed in LowLevel.h and the direction
706 * may be either ENDPOINT_DIR_OUT or ENDPOINT_DIR_IN.
708 * The bank size must indicate the maximum packet size that the endpoint can handle. Different
709 * endpoint numbers can handle different maximum packet sizes - refer to the chosen USB AVR's
710 * datasheet to determine the maximum bank size for each endpoint.
712 * The banking mode may be either ENDPOINT_BANK_SINGLE or ENDPOINT_BANK_DOUBLE.
714 * The success of this routine can be determined via the Endpoint_IsConfigured() macro.
716 * By default, the routine is entirely dynamic, and will accept both constant and variable inputs.
717 * If dynamic configuration is unused, a small space savings can be made by defining the
718 * STATIC_ENDPOINT_CONFIGURATION macro via the -D switch to the compiler, to optimize for constant
721 * \note This routine will select the specified endpoint, and the endpoint will remain selected
722 * once the routine completes regardless of if the endpoint configuration succeeds.
724 * \return Boolean true if the configuration succeeded, false otherwise
726 bool Endpoint_ConfigureEndpoint(const uint8_t Number
, const uint8_t Type
, const uint8_t Direction
,
727 const uint16_t Size
, const uint8_t Banks
);
729 /** Spinloops until the currently selected non-control endpoint is ready for the next packet of data
730 * to be read or written to it.
732 * \note This routine should not be called on CONTROL type endpoints.
734 * \ingroup Group_EndpointRW
736 * \return A value from the Endpoint_WaitUntilReady_ErrorCodes_t enum.
738 uint8_t Endpoint_WaitUntilReady(void);
740 /** Reads and discards the given number of bytes from the endpoint from the given buffer,
741 * discarding fully read packets from the host as needed. The last packet is not automatically
742 * discarded once the remaining bytes has been read; the user is responsible for manually
743 * discarding the last packet from the host via the Endpoint_ClearOUT() macro. Between
744 * each USB packet, the given stream callback function is executed repeatedly until the next
745 * packet is ready, allowing for early aborts of stream transfers.
747 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
748 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
749 * and this function has the Callback parameter omitted.
751 * \note This routine should not be used on CONTROL type endpoints.
753 * \ingroup Group_EndpointRW
755 * \param Length Number of bytes to send via the currently selected endpoint.
756 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
758 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
760 uint8_t Endpoint_Discard_Stream(uint16_t Length
761 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
762 , uint8_t (* const Callback
)(void)
766 /** Writes the given number of bytes to the endpoint from the given buffer in little endian,
767 * sending full packets to the host as needed. The last packet filled is not automatically sent;
768 * the user is responsible for manually sending the last written packet to the host via the
769 * Endpoint_ClearIN() macro. Between each USB packet, the given stream callback function
770 * is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early
771 * aborts of stream transfers.
773 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
774 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
775 * and this function has the Callback parameter omitted.
777 * \note This routine should not be used on CONTROL type endpoints.
779 * \ingroup Group_EndpointRW
781 * \param Buffer Pointer to the source data buffer to read from.
782 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
783 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
785 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
787 uint8_t Endpoint_Write_Stream_LE(const void* Buffer
, uint16_t Length
788 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
789 , uint8_t (* const Callback
)(void)
791 ) ATTR_NON_NULL_PTR_ARG(1);
793 /** Writes the given number of bytes to the endpoint from the given buffer in big endian,
794 * sending full packets to the host as needed. The last packet filled is not automatically sent;
795 * the user is responsible for manually sending the last written packet to the host via the
796 * Endpoint_ClearIN() macro. Between each USB packet, the given stream callback function
797 * is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early
798 * aborts of stream transfers.
800 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
801 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
802 * and this function has the Callback parameter omitted.
804 * \note This routine should not be used on CONTROL type endpoints.
806 * \ingroup Group_EndpointRW
808 * \param Buffer Pointer to the source data buffer to read from.
809 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
810 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
812 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
814 uint8_t Endpoint_Write_Stream_BE(const void* Buffer
, uint16_t Length
815 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
816 , uint8_t (* const Callback
)(void)
818 ) ATTR_NON_NULL_PTR_ARG(1);
820 /** Reads the given number of bytes from the endpoint from the given buffer in little endian,
821 * discarding fully read packets from the host as needed. The last packet is not automatically
822 * discarded once the remaining bytes has been read; the user is responsible for manually
823 * discarding the last packet from the host via the Endpoint_ClearOUT() macro. Between
824 * each USB packet, the given stream callback function is executed repeatedly until the endpoint
825 * is ready to accept the next packet, allowing for early aborts of stream transfers.
827 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
828 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
829 * and this function has the Callback parameter omitted.
831 * \note This routine should not be used on CONTROL type endpoints.
833 * \ingroup Group_EndpointRW
835 * \param Buffer Pointer to the destination data buffer to write to.
836 * \param Length Number of bytes to send via the currently selected endpoint.
837 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
839 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
841 uint8_t Endpoint_Read_Stream_LE(void* Buffer
, uint16_t Length
842 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
843 , uint8_t (* const Callback
)(void)
845 ) ATTR_NON_NULL_PTR_ARG(1);
847 /** Reads the given number of bytes from the endpoint from the given buffer in big endian,
848 * discarding fully read packets from the host as needed. The last packet is not automatically
849 * discarded once the remaining bytes has been read; the user is responsible for manually
850 * discarding the last packet from the host via the Endpoint_ClearOUT() macro. Between
851 * each USB packet, the given stream callback function is executed repeatedly until the endpoint
852 * is ready to accept the next packet, allowing for early aborts of stream transfers.
854 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
855 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
856 * and this function has the Callback parameter omitted.
858 * \note This routine should not be used on CONTROL type endpoints.
860 * \ingroup Group_EndpointRW
862 * \param Buffer Pointer to the destination data buffer to write to.
863 * \param Length Number of bytes to send via the currently selected endpoint.
864 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
866 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
868 uint8_t Endpoint_Read_Stream_BE(void* Buffer
, uint16_t Length
869 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
870 , uint8_t (* const Callback
)(void)
872 ) ATTR_NON_NULL_PTR_ARG(1);
874 /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in little endian,
875 * sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared
876 * in both failure and success states; the user is responsible for manually clearing the setup OUT to
877 * finalize the transfer via the Endpoint_ClearOUT() macro.
879 * \note This routine should only be used on CONTROL type endpoints.
881 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
882 * together; i.e. the entire stream data must be read or written at the one time.
884 * \ingroup Group_EndpointRW
886 * \param Buffer Pointer to the source data buffer to read from.
887 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
889 * \return A value from the Endpoint_ControlStream_RW_ErrorCodes_t enum.
891 uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
893 /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in big endian,
894 * sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared
895 * in both failure and success states; the user is responsible for manually clearing the setup OUT to
896 * finalize the transfer via the Endpoint_ClearOUT() macro.
898 * \note This routine should only be used on CONTROL type endpoints.
900 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
901 * together; i.e. the entire stream data must be read or written at the one time.
903 * \ingroup Group_EndpointRW
905 * \param Buffer Pointer to the source data buffer to read from.
906 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
908 * \return A value from the Endpoint_ControlStream_RW_ErrorCodes_t enum.
910 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
912 /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in little endian,
913 * discarding fully read packets from the host as needed. The device IN acknowledgement is not
914 * automatically sent after success or failure states; the user is responsible for manually sending the
915 * setup IN to finalize the transfer via the Endpoint_ClearIN() macro.
917 * \note This routine should only be used on CONTROL type endpoints.
919 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
920 * together; i.e. the entire stream data must be read or written at the one time.
922 * \ingroup Group_EndpointRW
924 * \param Buffer Pointer to the destination data buffer to write to.
925 * \param Length Number of bytes to send via the currently selected endpoint.
927 * \return A value from the Endpoint_ControlStream_RW_ErrorCodes_t enum.
929 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
931 /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in big endian,
932 * discarding fully read packets from the host as needed. The device IN acknowledgement is not
933 * automatically sent after success or failure states; the user is responsible for manually sending the
934 * setup IN to finalize the transfer via the Endpoint_ClearIN() macro.
936 * \note This routine should only be used on CONTROL type endpoints.
938 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
939 * together; i.e. the entire stream data must be read or written at the one time.
941 * \ingroup Group_EndpointRW
943 * \param Buffer Pointer to the destination data buffer to write to.
944 * \param Length Number of bytes to send via the currently selected endpoint.
946 * \return A value from the Endpoint_ControlStream_RW_ErrorCodes_t enum.
948 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
950 /* Private Interface - For use in library only: */
951 #if !defined(__DOXYGEN__)
953 #define Endpoint_AllocateMemory() MACROS{ UECFG1X |= (1 << ALLOC); }MACROE
954 #define Endpoint_DeallocateMemory() MACROS{ UECFG1X &= ~(1 << ALLOC); }MACROE
956 #define _ENDPOINT_GET_MAXSIZE(n) _ENDPOINT_GET_MAXSIZE2(ENDPOINT_DETAILS_EP ## n)
957 #define _ENDPOINT_GET_MAXSIZE2(details) _ENDPOINT_GET_MAXSIZE3(details)
958 #define _ENDPOINT_GET_MAXSIZE3(maxsize, db) maxsize
960 #define _ENDPOINT_GET_DOUBLEBANK(n) _ENDPOINT_GET_DOUBLEBANK2(ENDPOINT_DETAILS_EP ## n)
961 #define _ENDPOINT_GET_DOUBLEBANK2(details) _ENDPOINT_GET_DOUBLEBANK3(details)
962 #define _ENDPOINT_GET_DOUBLEBANK3(maxsize, db) db
964 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER)
965 #define ENDPOINT_DETAILS_EP0 64, true
966 #define ENDPOINT_DETAILS_EP1 256, true
967 #define ENDPOINT_DETAILS_EP2 64, true
968 #define ENDPOINT_DETAILS_EP3 64, true
969 #define ENDPOINT_DETAILS_EP4 64, true
970 #define ENDPOINT_DETAILS_EP5 64, true
971 #define ENDPOINT_DETAILS_EP6 64, true
973 #define ENDPOINT_DETAILS_EP0 64, true
974 #define ENDPOINT_DETAILS_EP1 64, false
975 #define ENDPOINT_DETAILS_EP2 64, false
976 #define ENDPOINT_DETAILS_EP3 64, true
977 #define ENDPOINT_DETAILS_EP4 64, true
980 #if defined(STATIC_ENDPOINT_CONFIGURATION)
981 #define Endpoint_ConfigureEndpoint(Number, Type, Direction, Size, Banks) \
982 Endpoint_ConfigureEndpointStatic(Number, \
983 ((Type << EPTYPE0) | Direction), \
984 ((1 << ALLOC) | Banks | Endpoint_BytesToEPSizeMask(Size)));
987 /* Function Prototypes: */
988 void Endpoint_ClearEndpoints(void);
989 bool Endpoint_ConfigureEndpointStatic(const uint8_t Number
, const uint8_t UECFG0XData
, const uint8_t UECFG1XData
);
991 /* Inline Functions: */
992 static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes
) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE
;
993 static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes
)
996 return (0 << EPSIZE0
);
997 else if (Bytes
<= 16)
998 return (1 << EPSIZE0
);
999 else if (Bytes
<= 32)
1000 return (2 << EPSIZE0
);
1001 #if defined(USB_LIMITED_CONTROLLER)
1003 return (3 << EPSIZE0
);
1005 else if (Bytes
<= 64)
1006 return (3 << EPSIZE0
);
1007 else if (Bytes
<= 128)
1008 return (4 << EPSIZE0
);
1010 return (5 << EPSIZE0
);
1016 /* Disable C linkage for C++ Compilers: */
1017 #if defined(__cplusplus)