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
33 * Functions, macros and enums related to endpoint management when in USB Device mode. This
34 * module contains the endpoint management macros, as well as endpoint interrupt and data
35 * send/recieve functions for various datatypes.
38 #ifndef __ENDPOINT_H__
39 #define __ENDPOINT_H__
45 #include "../../../Common/Common.h"
46 #include "../HighLevel/USBTask.h"
48 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
49 #include "StreamCallbacks.h"
51 /* Enable C linkage for C++ Compilers: */
52 #if defined(__cplusplus)
56 /* Public Interface - May be used in end-application: */
58 /** Endpoint data direction mask for Endpoint_ConfigureEndpoint(). This indicates that the endpoint
59 * should be initialized in the OUT direction - i.e. data flows from host to device.
61 #define ENDPOINT_DIR_OUT (0 << EPDIR)
63 /** Endpoint data direction mask for Endpoint_ConfigureEndpoint(). This indicates that the endpoint
64 * should be initialized in the IN direction - i.e. data flows from device to host.
66 #define ENDPOINT_DIR_IN (1 << EPDIR)
68 /** Mask for the bank mode selection for the Endpoint_ConfigureEndpoint() macro. This indicates
69 * that the endpoint should have one single bank, which requires less USB FIFO memory but results
70 * in slower transfers as only one USB device (the AVR or the host) can access the endpoint's
71 * bank at the one time.
73 #define ENDPOINT_BANK_SINGLE (0 << EPBK0)
75 /** Mask for the bank mode selection for the Endpoint_ConfigureEndpoint() macro. This indicates
76 * that the endpoint should have two banks, which requires more USB FIFO memory but results
77 * in faster transfers as one USB device (the AVR or the host) can access one bank while the other
78 * accesses the second bank.
80 #define ENDPOINT_BANK_DOUBLE (1 << EPBK0)
82 /** Endpoint address for the default control endpoint, which always resides in address 0. This is
83 * defined for convenience to give more readable code when used with the endpoint macros.
85 #define ENDPOINT_CONTROLEP 0
87 /** Default size of the default control endpoint's bank, until altered by the Endpoint0Size value
88 * in the device descriptor. Not available if the FIXED_CONTROL_ENDPOINT_SIZE token is defined.
90 #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
91 #define ENDPOINT_CONTROLEP_DEFAULT_SIZE 8
94 /** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's
95 * numerical address in the device.
97 #define ENDPOINT_EPNUM_MASK 0b111
99 /** Endpoint bank size mask, for masking against endpoint addresses to retrieve the endpoint's
100 * bank size in the device.
102 #define ENDPOINT_EPSIZE_MASK 0x7FF
104 /** Maximum size in bytes of a given endpoint.
106 * \param n Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1)
108 #define ENDPOINT_MAX_SIZE(n) _ENDPOINT_GET_MAXSIZE(n)
110 /** Indicates if the given endpoint supports double banking.
112 * \param n Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1)
114 #define ENDPOINT_DOUBLEBANK_SUPPORTED(n) _ENDPOINT_GET_DOUBLEBANK(n)
116 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER) || defined(__DOXYGEN__)
117 /** Total number of endpoints (including the default control endpoint at address 0) which may
118 * be used in the device. Different USB AVR models support different amounts of endpoints,
119 * this value reflects the maximum number of endpoints for the currently selected AVR model.
121 #define ENDPOINT_TOTAL_ENDPOINTS 7
123 #define ENDPOINT_TOTAL_ENDPOINTS 5
126 /** Interrupt definition for the endpoint SETUP interrupt (for CONTROL type endpoints). Should be
127 * used with the USB_INT_* macros located in USBInterrupt.h.
129 * This interrupt will fire if enabled on a CONTROL type endpoint if a new control packet is
130 * received from the host.
132 * \note This interrupt must be enabled and cleared on *each* endpoint which requires it (after the
133 * endpoint is selected), and will fire the common endpoint interrupt vector.
135 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
137 #define ENDPOINT_INT_SETUP UEIENX, (1 << RXSTPE), UEINTX, (1 << RXSTPI)
139 /** Interrupt definition for the endpoint IN interrupt (for INTERRUPT type endpoints). Should be
140 * used with the USB_INT_* macros located in USBInterrupt.h.
142 * This interrupt will fire if enabled on an INTERRUPT type endpoint if a the endpoint interrupt
143 * period has elapsed and the endpoint is ready for a new packet to be written to its FIFO buffer
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_IN UEIENX, (1 << TXINE) , UEINTX, (1 << TXINI)
153 /** Interrupt definition for the endpoint OUT interrupt (for INTERRUPT type endpoints). Should be
154 * used with the USB_INT_* macros located in USBInterrupt.h.
156 * This interrupt will fire if enabled on an INTERRUPT type endpoint if a the endpoint interrupt
157 * period has elapsed and the endpoint is ready for a packet from the host to be read from its
158 * FIFO buffer (if received).
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_OUT UEIENX, (1 << RXOUTE), UEINTX, (1 << RXOUTI)
167 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER) || defined(__DOXYGEN__)
168 /** Indicates the number of bytes currently stored in the current endpoint's selected bank. */
169 #define Endpoint_BytesInEndpoint() UEBCX
171 #define Endpoint_BytesInEndpoint() UEBCLX
174 /** Returns the endpoint address of the currently selected endpoint. This is typically used to save
175 * the currently selected endpoint number so that it can be restored after another endpoint has
178 #define Endpoint_GetCurrentEndpoint() (UENUM & ENDPOINT_EPNUM_MASK)
180 /** Selects the given endpoint number. If the address from the device descriptors is used, the
181 * value should be masked with the ENDPOINT_EPNUM_MASK constant to extract only the endpoint
182 * number (and discarding the endpoint direction bit).
184 * Any endpoint operations which do not require the endpoint number to be indicated will operate on
185 * the currently selected endpoint.
187 #define Endpoint_SelectEndpoint(epnum) MACROS{ UENUM = epnum; }MACROE
189 /** Resets the endpoint bank FIFO. This clears all the endpoint banks and resets the USB controller's
190 * In and Out pointers to the bank's contents.
192 #define Endpoint_ResetFIFO(epnum) MACROS{ UERST = (1 << epnum); UERST = 0; }MACROE
194 /** Enables the currently selected endpoint so that data can be sent and received through it to
197 * \note Endpoints must first be configured properly rather than just being enabled via the
198 * Endpoint_ConfigureEndpoint() macro, which calls Endpoint_EnableEndpoint() automatically.
200 #define Endpoint_EnableEndpoint() MACROS{ UECONX |= (1 << EPEN); }MACROE
202 /** Disables the currently selected endpoint so that data cannot be sent and received through it
203 * to and from a host.
205 #define Endpoint_DisableEndpoint() MACROS{ UECONX &= ~(1 << EPEN); }MACROE
207 /** Returns true if the currently selected endpoint is enabled, false otherwise. */
208 #define Endpoint_IsEnabled() ((UECONX & (1 << EPEN)) ? true : false)
210 /** Returns true if the currently selected endpoint may be read from (if data is waiting in the endpoint
211 * bank and the endpoint is an OUT direction, or if the bank is not yet full if the endpoint is an
212 * IN direction). This function will return false if an error has occured in the endpoint, or if
213 * the endpoint is an OUT direction and no packet has been received, or if the endpoint is an IN
214 * direction and the endpoint bank is full.
216 #define Endpoint_ReadWriteAllowed() ((UEINTX & (1 << RWAL)) ? true : false)
218 /** Returns true if the currently selected endpoint is configured, false otherwise. */
219 #define Endpoint_IsConfigured() ((UESTA0X & (1 << CFGOK)) ? true : false)
221 /** Returns a mask indicating which INTERRUPT type endpoints have interrupted - i.e. their
222 * interrupt duration has elapsed. Which endpoints have interrupted can be determined by
223 * masking the return value against (1 << {Endpoint Number}).
225 #define Endpoint_GetEndpointInterrupts() UEINT
227 /** Clears the endpoint interrupt flag. This clears the specified endpoint number's interrupt
228 * mask in the endpoint interrupt flag register.
230 #define Endpoint_ClearEndpointInterrupt(n) MACROS{ UEINT &= ~(1 << n); }MACROE
232 /** Returns true if the specified endpoint number has interrupted (valid only for INTERRUPT type
233 * endpoints), false otherwise.
235 #define Endpoint_HasEndpointInterrupted(n) ((UEINT & (1 << n)) ? true : false)
237 /** Clears the currently selected endpoint bank, and switches to the alternate bank if the currently
238 * selected endpoint is dual-banked. When cleared, this either frees the bank up for the next packet
239 * from the host (if the endpoint is of the OUT direction) or sends the packet contents to the host
240 * (if the endpoint is of the IN direction).
242 #define Endpoint_ClearCurrentBank() MACROS{ UEINTX &= ~(1 << FIFOCON); }MACROE
244 /** Returns true if the current CONTROL type endpoint is ready for an IN packet, false otherwise. */
245 #define Endpoint_IsSetupINReady() ((UEINTX & (1 << TXINI)) ? true : false)
247 /** Returns true if the current CONTROL type endpoint is ready for an OUT packet, false otherwise. */
248 #define Endpoint_IsSetupOUTReceived() ((UEINTX & (1 << RXOUTI)) ? true : false)
250 /** Returns true if the current CONTROL type endpoint is ready for a SETUP packet, false otherwise. */
251 #define Endpoint_IsSetupReceived() ((UEINTX & (1 << RXSTPI)) ? true : false)
253 /** Clears a received SETUP packet on the currently selected CONTROL type endpoint. */
254 #define Endpoint_ClearSetupReceived() MACROS{ UEINTX &= ~(1 << RXSTPI); }MACROE
256 /** Sends an IN packet to the host on the currently selected CONTROL type endpoint. */
257 #define Endpoint_ClearSetupIN() MACROS{ UEINTX &= ~(1 << TXINI); }MACROE
259 /** Acknowedges an OUT packet to the host on the currently selected CONTROL type endpoint, freeing
260 * up the endpoint for the next packet.
262 #define Endpoint_ClearSetupOUT() MACROS{ UEINTX &= ~(1 << RXOUTI); }MACROE
264 /** Stalls the current endpoint, indicating to the host that a logical problem occured with the
265 * indicated endpoint and that the current transfer sequence should be aborted. This provides a
266 * way for devices to indicate invalid commands to the host so that the current transfer can be
267 * aborted and the host can begin its own recovery seqeuence.
269 * The currently selected endpoint remains stalled until either the Endpoint_ClearStall() macro
270 * is called, or the host issues a CLEAR FEATURE request to the device for the currently selected
273 #define Endpoint_StallTransaction() MACROS{ UECONX |= (1 << STALLRQ); }MACROE
275 /** Clears the stall on the currently selected endpoint. */
276 #define Endpoint_ClearStall() MACROS{ UECONX |= (1 << STALLRQC); }MACROE
278 /** Returns true if the currently selected endpoint is stalled, false othewise. */
279 #define Endpoint_IsStalled() ((UECONX & (1 << STALLRQ)) ? true : false)
281 /** Resets the data toggle of the currently selected endpoint. */
282 #define Endpoint_ResetDataToggle() MACROS{ UECONX |= (1 << RSTDT); }MACROE
285 /** Enum for the possible error return codes of the Endpoint_WaitUntilReady function */
286 enum Endpoint_WaitUntilReady_ErrorCodes_t
288 ENDPOINT_READYWAIT_NoError
= 0, /**< Endpoint is ready for next packet, no error. */
289 ENDPOINT_READYWAIT_EndpointStalled
= 1, /**< The endpoint was stalled during the stream
290 * transfer by the host or device.
292 ENDPOINT_READYWAIT_DeviceDisconnected
= 2, /**< Device was disconnected from the host while
293 * waiting for the endpoint to become ready.
295 ENDPOINT_READYWAIT_Timeout
= 3, /**< The host failed to accept or send the next packet
296 * within the software timeout period set by the
297 * USB_STREAM_TIMEOUT_MS macro.
301 /** Enum for the possible error return codes of the Endpoint_*_Stream_* functions. */
302 enum Endpoint_Stream_RW_ErrorCodes_t
304 ENDPOINT_RWSTREAM_ERROR_NoError
= 0, /**< Command completed successfully, no error. */
305 ENDPOINT_RWSTREAM_ERROR_EndpointStalled
= 1, /**< The endpoint was stalled during the stream
306 * transfer by the host or device.
308 ENDPOINT_RWSTREAM_ERROR_DeviceDisconnected
= 1, /**< Device was disconnected from the host during
311 ENDPOINT_RWSTREAM_ERROR_Timeout
= 2, /**< The host failed to accept or send the next packet
312 * within the software timeout period set by the
313 * USB_STREAM_TIMEOUT_MS macro.
315 ENDPOINT_RWSTREAM_ERROR_CallbackAborted
= 3, /**< Indicates that the stream's callback function
316 * aborted the transfer early.
320 /** Enum for the possible error return codes of the Endpoint_*_Control_Stream_* functions. */
321 enum Endpoint_ControlStream_RW_ErrorCodes_t
323 ENDPOINT_RWCSTREAM_ERROR_NoError
= 0, /**< Command completed successfully, no error. */
324 ENDPOINT_RWCSTREAM_ERROR_HostAborted
= 1, /**< The aborted the transfer prematurely. */
327 /* Inline Functions: */
328 /** Reads one byte from the currently selected endpoint's bank, for OUT direction endpoints. */
329 static inline uint8_t Endpoint_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
330 static inline uint8_t Endpoint_Read_Byte(void)
335 /** Writes one byte from the currently selected endpoint's bank, for IN direction endpoints. */
336 static inline void Endpoint_Write_Byte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
337 static inline void Endpoint_Write_Byte(const uint8_t Byte
)
342 /** Discards one byte from the currently selected endpoint's bank, for OUT direction endpoints. */
343 static inline void Endpoint_Discard_Byte(void) ATTR_ALWAYS_INLINE
;
344 static inline void Endpoint_Discard_Byte(void)
351 /** Reads two bytes from the currently selected endpoint's bank in little endian format, for OUT
352 * direction endpoints.
354 static inline uint16_t Endpoint_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
355 static inline uint16_t Endpoint_Read_Word_LE(void)
360 Data
|= (((uint16_t)UEDATX
) << 8);
365 /** Reads two bytes from the currently selected endpoint's bank in big endian format, for OUT
366 * direction endpoints.
368 static inline uint16_t Endpoint_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
369 static inline uint16_t Endpoint_Read_Word_BE(void)
373 Data
= (((uint16_t)UEDATX
) << 8);
379 /** Writes two bytes to the currently selected endpoint's bank in little endian format, for IN
380 * direction endpoints.
382 static inline void Endpoint_Write_Word_LE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
383 static inline void Endpoint_Write_Word_LE(const uint16_t Word
)
385 UEDATX
= (Word
& 0xFF);
386 UEDATX
= (Word
>> 8);
389 /** Writes two bytes to the currently selected endpoint's bank in big endian format, for IN
390 * direction endpoints.
392 static inline void Endpoint_Write_Word_BE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
393 static inline void Endpoint_Write_Word_BE(const uint16_t Word
)
395 UEDATX
= (Word
>> 8);
396 UEDATX
= (Word
& 0xFF);
399 /** Discards two bytes from the currently selected endpoint's bank, for OUT direction endpoints. */
400 static inline void Endpoint_Discard_Word(void) ATTR_ALWAYS_INLINE
;
401 static inline void Endpoint_Discard_Word(void)
409 /** Reads four bytes from the currently selected endpoint's bank in little endian format, for OUT
410 * direction endpoints.
412 static inline uint32_t Endpoint_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
413 static inline uint32_t Endpoint_Read_DWord_LE(void)
421 Data
.Bytes
[0] = UEDATX
;
422 Data
.Bytes
[1] = UEDATX
;
423 Data
.Bytes
[2] = UEDATX
;
424 Data
.Bytes
[3] = UEDATX
;
429 /** Reads four bytes from the currently selected endpoint's bank in big endian format, for OUT
430 * direction endpoints.
432 static inline uint32_t Endpoint_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
433 static inline uint32_t Endpoint_Read_DWord_BE(void)
441 Data
.Bytes
[3] = UEDATX
;
442 Data
.Bytes
[2] = UEDATX
;
443 Data
.Bytes
[1] = UEDATX
;
444 Data
.Bytes
[0] = UEDATX
;
449 /** Writes four bytes to the currently selected endpoint's bank in little endian format, for IN
450 * direction endpoints.
452 static inline void Endpoint_Write_DWord_LE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
453 static inline void Endpoint_Write_DWord_LE(const uint32_t DWord
)
455 UEDATX
= (DWord
& 0xFF);
456 UEDATX
= (DWord
>> 8);
457 UEDATX
= (DWord
>> 16);
458 UEDATX
= (DWord
>> 24);
461 /** Writes four bytes to the currently selected endpoint's bank in big endian format, for IN
462 * direction endpoints.
464 static inline void Endpoint_Write_DWord_BE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
465 static inline void Endpoint_Write_DWord_BE(const uint32_t DWord
)
467 UEDATX
= (DWord
>> 24);
468 UEDATX
= (DWord
>> 16);
469 UEDATX
= (DWord
>> 8);
470 UEDATX
= (DWord
& 0xFF);
473 /** Discards four bytes from the currently selected endpoint's bank, for OUT direction endpoints. */
474 static inline void Endpoint_Discard_DWord(void) ATTR_ALWAYS_INLINE
;
475 static inline void Endpoint_Discard_DWord(void)
485 /* External Variables: */
486 /** Global indicating the maximum packet size of the default control endpoint located at address
487 * 0 in the device. This value is set to the value indicated in the device descriptor in the user
488 * project once the USB interface is initialized into device mode.
490 * If space is an issue, it is possible to fix this to a static value by defining the control
491 * endpoint size in the FIXED_CONTROL_ENDPOINT_SIZE token passed to the compiler in the makefile
492 * via the -D switch. When a fixed control endpoint size is used, the size is no longer dynamically
493 * read from the descriptors at runtime and instead fixed to the given value. When used, it is
494 * important that the descriptor control endpoint size value matches the size given as the
495 * FIXED_CONTROL_ENDPOINT_SIZE token - it is recommended that the FIXED_CONTROL_ENDPOINT_SIZE token
496 * be used in the descriptors to ensure this.
498 * \note This variable should be treated as read-only in the user application, and never manually
501 #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
502 extern uint8_t USB_ControlEndpointSize
;
504 #define USB_ControlEndpointSize FIXED_CONTROL_ENDPOINT_SIZE
507 /* Function Prototypes: */
508 /** Configures the specified endpoint number with the given endpoint type, direction, bank size
509 * and banking mode. Endpoints should be allocated in ascending order by their address in the
510 * device (i.e. endpoint 1 should be configured before endpoint 2 and so on).
512 * The endpoint type may be one of the EP_TYPE_* macros listed in LowLevel.h and the direction
513 * may be either ENDPOINT_DIR_OUT or ENDPOINT_DIR_IN.
515 * The bank size must indicate the maximum packet size that the endpoint can handle. Different
516 * endpoint numbers can handle different maximum packet sizes - refer to the chosen USB AVR's
517 * datasheet to determine the maximum bank size for each endpoint.
519 * The banking mode may be either ENDPOINT_BANK_SINGLE or ENDPOINT_BANK_DOUBLE.
521 * The success of this routine can be determined via the Endpoint_IsConfigured() macro.
523 * By default, the routine is entirely dynamic, and will accept both constant and variable inputs.
524 * If dynamic configuration is unused, a small space savings can be made by defining the
525 * STATIC_ENDPOINT_CONFIGURATION macro via the -D switch to the compiler, to optimize for constant
528 * \note This routine will select the specified endpoint, and the endpoint will remain selected
529 * once the routine completes regardless of if the endpoint configuration succeeds.
531 * \return Boolean true if the configuration succeeded, false otherwise
533 bool Endpoint_ConfigureEndpoint(const uint8_t Number
, const uint8_t Type
, const uint8_t Direction
,
534 const uint16_t Size
, const uint8_t Banks
);
536 /** Spinloops until the currently selected non-control endpoint is ready for the next packet of data
537 * to be read or written to it.
539 * \note This routine should not be called on CONTROL type endpoints.
541 * \return A value from the Endpoint_WaitUntilReady_ErrorCodes_t enum.
543 uint8_t Endpoint_WaitUntilReady(void);
545 /** Reads and discards the given number of bytes from the endpoint from the given buffer,
546 * discarding fully read packets from the host as needed. The last packet is not automatically
547 * discarded once the remaining bytes has been read; the user is responsible for manually
548 * discarding the last packet from the host via the Endpoint_ClearCurrentBank() macro. Between
549 * each USB packet, the given stream callback function is executed repeatedly until the next
550 * packet is ready, allowing for early aborts of stream transfers.
552 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
553 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
554 * and this function has the Callback parameter ommitted.
556 * \note This routine should not be used on CONTROL type endpoints.
558 * \param Length Number of bytes to send via the currently selected endpoint.
559 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
561 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
563 uint8_t Endpoint_Discard_Stream(uint16_t Length
564 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
565 , uint8_t (* const Callback
)(void)
569 /** Writes the given number of bytes to the endpoint from the given buffer in little endian,
570 * sending full packets to the host as needed. The last packet filled is not automatically sent;
571 * the user is responsible for manually sending the last written packet to the host via the
572 * Endpoint_ClearCurrentBank() macro. Between each USB packet, the given stream callback function
573 * is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early
574 * aborts of stream transfers.
576 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
577 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
578 * and this function has the Callback parameter ommitted.
580 * \note This routine should not be used on CONTROL type endpoints.
582 * \param Buffer Pointer to the source data buffer to read from.
583 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
584 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
586 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
588 uint8_t Endpoint_Write_Stream_LE(const void* Buffer
, uint16_t Length
589 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
590 , uint8_t (* const Callback
)(void)
592 ) ATTR_NON_NULL_PTR_ARG(1);
594 /** Writes the given number of bytes to the endpoint from the given buffer in big endian,
595 * sending full packets to the host as needed. The last packet filled is not automatically sent;
596 * the user is responsible for manually sending the last written packet to the host via the
597 * Endpoint_ClearCurrentBank() macro. Between each USB packet, the given stream callback function
598 * is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early
599 * aborts of stream transfers.
601 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
602 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
603 * and this function has the Callback parameter ommitted.
605 * \note This routine should not be used on CONTROL type endpoints.
607 * \param Buffer Pointer to the source data buffer to read from.
608 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
609 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
611 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
613 uint8_t Endpoint_Write_Stream_BE(const void* Buffer
, uint16_t Length
614 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
615 , uint8_t (* const Callback
)(void)
617 ) ATTR_NON_NULL_PTR_ARG(1);
619 /** Reads the given number of bytes from the endpoint from the given buffer in little endian,
620 * discarding fully read packets from the host as needed. The last packet is not automatically
621 * discarded once the remaining bytes has been read; the user is responsible for manually
622 * discarding the last packet from the host via the Endpoint_ClearCurrentBank() macro. Between
623 * each USB packet, the given stream callback function is executed repeatedly until the endpoint
624 * is ready to accept the next packet, allowing for early aborts of stream transfers.
626 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
627 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
628 * and this function has the Callback parameter ommitted.
630 * \note This routine should not be used on CONTROL type endpoints.
632 * \param Buffer Pointer to the destination data buffer to write to.
633 * \param Length Number of bytes to send via the currently selected endpoint.
634 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
636 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
638 uint8_t Endpoint_Read_Stream_LE(void* Buffer
, uint16_t Length
639 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
640 , uint8_t (* const Callback
)(void)
642 ) ATTR_NON_NULL_PTR_ARG(1);
644 /** Reads the given number of bytes from the endpoint from the given buffer in big endian,
645 * discarding fully read packets from the host as needed. The last packet is not automatically
646 * discarded once the remaining bytes has been read; the user is responsible for manually
647 * discarding the last packet from the host via the Endpoint_ClearCurrentBank() macro. Between
648 * each USB packet, the given stream callback function is executed repeatedly until the endpoint
649 * is ready to accept the next packet, allowing for early aborts of stream transfers.
651 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
652 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
653 * and this function has the Callback parameter ommitted.
655 * \note This routine should not be used on CONTROL type endpoints.
657 * \param Buffer Pointer to the destination data buffer to write to.
658 * \param Length Number of bytes to send via the currently selected endpoint.
659 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
661 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
663 uint8_t Endpoint_Read_Stream_BE(void* Buffer
, uint16_t Length
664 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
665 , uint8_t (* const Callback
)(void)
667 ) ATTR_NON_NULL_PTR_ARG(1);
669 /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in little endian,
670 * sending full packets to the host as needed. The host OUT acknowedgement is not automatically cleared
671 * in both failure and success states; the user is responsible for manually clearing the setup OUT to
672 * finalize the transfer via the Endpoint_ClearSetupOUT() macro.
674 * \note This routine should only be used on CONTROL type endpoints.
676 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
677 * together; i.e. the entire stream data must be read or written at the one time.
679 * \param Buffer Pointer to the source data buffer to read from.
680 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
682 * \return A value from the Endpoint_ControlStream_RW_ErrorCodes_t enum.
684 uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
686 /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in big endian,
687 * sending full packets to the host as needed. The host OUT acknowedgement is not automatically cleared
688 * in both failure and success states; the user is responsible for manually clearing the setup OUT to
689 * finalize the transfer via the Endpoint_ClearSetupOUT() macro.
691 * \note This routine should only be used on CONTROL type endpoints.
693 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
694 * together; i.e. the entire stream data must be read or written at the one time.
696 * \param Buffer Pointer to the source data buffer to read from.
697 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
699 * \return A value from the Endpoint_ControlStream_RW_ErrorCodes_t enum.
701 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
703 /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in little endian,
704 * discarding fully read packets from the host as needed. The device IN acknowedgement is not
705 * automatically sent after success or failure states; the user is responsible for manually sending the
706 * setup IN to finalize the transfer via the Endpoint_ClearSetupIN() macro.
708 * \note This routine should only be used on CONTROL type endpoints.
710 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
711 * together; i.e. the entire stream data must be read or written at the one time.
713 * \param Buffer Pointer to the destination data buffer to write to.
714 * \param Length Number of bytes to send via the currently selected endpoint.
716 * \return A value from the Endpoint_ControlStream_RW_ErrorCodes_t enum.
718 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
720 /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in big endian,
721 * discarding fully read packets from the host as needed. The device IN acknowedgement is not
722 * automatically sent after success or failure states; the user is responsible for manually sending the
723 * setup IN to finalize the transfer via the Endpoint_ClearSetupIN() macro.
725 * \note This routine should only be used on CONTROL type endpoints.
727 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
728 * together; i.e. the entire stream data must be read or written at the one time.
730 * \param Buffer Pointer to the destination data buffer to write to.
731 * \param Length Number of bytes to send via the currently selected endpoint.
733 * \return A value from the Endpoint_ControlStream_RW_ErrorCodes_t enum.
735 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
737 /* Function Aliases: */
738 /** Alias for Endpoint_Discard_Byte().
740 #define Endpoint_Ignore_Byte() Endpoint_Discard_Byte()
742 /** Alias for Endpoint_Discard_Word().
744 #define Endpoint_Ignore_Word() Endpoint_Discard_Word()
746 /** Alias for Endpoint_Discard_DWord().
748 #define Endpoint_Ignore_DWord() Endpoint_Discard_DWord()
750 /** Alias for Endpoint_Read_Word_LE(). By default USB transfers use little endian format, thus
751 * the command with no endianness specifier indicates little endian mode.
753 #define Endpoint_Read_Word() Endpoint_Read_Word_LE()
755 /** Alias for Endpoint_Write_Word_LE(). By default USB transfers use little endian format, thus
756 * the command with no endianness specifier indicates little endian mode.
758 #define Endpoint_Write_Word(Word) Endpoint_Write_Word_LE(Word)
760 /** Alias for Endpoint_Read_DWord_LE(). By default USB transfers use little endian format, thus
761 * the command with no endianness specifier indicates little endian mode.
763 #define Endpoint_Read_DWord() Endpoint_Read_DWord_LE()
765 /** Alias for Endpoint_Write_DWord_LE(). By default USB transfers use little endian format, thus
766 * the command with no endianness specifier indicates little endian mode.
768 #define Endpoint_Write_DWord(DWord) Endpoint_Write_DWord_LE(DWord)
770 /** Alias for Endpoint_Read_Stream_LE(). By default USB transfers use little endian format, thus
771 * the command with no endianness specifier indicates little endian mode.
773 #if !defined(NO_STREAM_CALLBACKS)
774 #define Endpoint_Read_Stream(Buffer, Length, Callback) Endpoint_Read_Stream_LE(Buffer, Length, Callback)
776 #define Endpoint_Read_Stream(Buffer, Length) Endpoint_Read_Stream_LE(Buffer, Length)
779 /** Alias for Endpoint_Write_Stream_LE(). By default USB transfers use little endian format, thus
780 * the command with no endianness specifier indicates little endian mode.
782 #if !defined(NO_STREAM_CALLBACKS)
783 #define Endpoint_Write_Stream(Buffer, Length, Callback) Endpoint_Write_Stream_LE(Buffer, Length, Callback)
785 #define Endpoint_Write_Stream(Buffer, Length) Endpoint_Write_Stream_LE(Buffer, Length)
788 /** Alias for Endpoint_Read_Control_Stream_LE(). By default USB transfers use little endian format, thus
789 * the command with no endianness specifier indicates little endian mode.
791 #define Endpoint_Read_Control_Stream(Data, Length) Endpoint_Read_Control_Stream_LE(Data, Length)
793 /** Alias for Endpoint_Write_Control_Stream_LE(). By default USB transfers use little endian format, thus
794 * the command with no endianness specifier indicates little endian mode.
796 #define Endpoint_Write_Control_Stream(Data, Length) Endpoint_Write_Control_Stream_LE(Data, Length)
798 /* Private Interface - For use in library only: */
799 #if !defined(__DOXYGEN__)
801 #define Endpoint_AllocateMemory() MACROS{ UECFG1X |= (1 << ALLOC); }MACROE
802 #define Endpoint_DeallocateMemory() MACROS{ UECFG1X &= ~(1 << ALLOC); }MACROE
804 #define _ENDPOINT_GET_MAXSIZE(n) _ENDPOINT_GET_MAXSIZE2(ENDPOINT_DETAILS_EP ## n)
805 #define _ENDPOINT_GET_MAXSIZE2(details) _ENDPOINT_GET_MAXSIZE3(details)
806 #define _ENDPOINT_GET_MAXSIZE3(maxsize, db) maxsize
808 #define _ENDPOINT_GET_DOUBLEBANK(n) _ENDPOINT_GET_DOUBLEBANK2(ENDPOINT_DETAILS_EP ## n)
809 #define _ENDPOINT_GET_DOUBLEBANK2(details) _ENDPOINT_GET_DOUBLEBANK3(details)
810 #define _ENDPOINT_GET_DOUBLEBANK3(maxsize, db) db
812 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER)
813 #define ENDPOINT_DETAILS_EP0 64, true
814 #define ENDPOINT_DETAILS_EP1 256, true
815 #define ENDPOINT_DETAILS_EP2 64, true
816 #define ENDPOINT_DETAILS_EP3 64, true
817 #define ENDPOINT_DETAILS_EP4 64, true
818 #define ENDPOINT_DETAILS_EP5 64, true
819 #define ENDPOINT_DETAILS_EP6 64, true
821 #define ENDPOINT_DETAILS_EP0 64, true
822 #define ENDPOINT_DETAILS_EP1 64, false
823 #define ENDPOINT_DETAILS_EP2 64, false
824 #define ENDPOINT_DETAILS_EP3 64, true
825 #define ENDPOINT_DETAILS_EP4 64, true
828 #if defined(STATIC_ENDPOINT_CONFIGURATION)
829 #define Endpoint_ConfigureEndpoint(Number, Type, Direction, Size, Banks) \
830 Endpoint_ConfigureEndpointStatic(Number, \
831 ((Type << EPTYPE0) | Direction), \
832 ((1 << ALLOC) | Banks | Endpoint_BytesToEPSizeMask(Size)));
835 /* Function Prototypes: */
836 void Endpoint_ClearEndpoints(void);
837 bool Endpoint_ConfigureEndpointStatic(const uint8_t Number
, const uint8_t UECFG0XData
, const uint8_t UECFG1XData
);
839 /* Inline Functions: */
840 static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes
) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE
;
841 static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes
)
844 return (0 << EPSIZE0
);
845 else if (Bytes
<= 16)
846 return (1 << EPSIZE0
);
847 else if (Bytes
<= 32)
848 return (2 << EPSIZE0
);
849 #if defined(USB_LIMITED_CONTROLLER)
851 return (3 << EPSIZE0
);
853 else if (Bytes
<= 64)
854 return (3 << EPSIZE0
);
855 else if (Bytes
<= 128)
856 return (4 << EPSIZE0
);
858 return (5 << EPSIZE0
);
864 /* Disable C linkage for C++ Compilers: */
865 #if defined(__cplusplus)