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 data types.
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 "../HighLevel/StreamCallbacks.h"
52 /* Enable C linkage for C++ Compilers: */
53 #if defined(__cplusplus)
57 /* Public Interface - May be used in end-application: */
59 /** Endpoint data direction mask for Endpoint_ConfigureEndpoint(). This indicates that the endpoint
60 * should be initialized in the OUT direction - i.e. data flows from host to device.
62 #define ENDPOINT_DIR_OUT (0 << EPDIR)
64 /** Endpoint data direction mask for Endpoint_ConfigureEndpoint(). This indicates that the endpoint
65 * should be initialized in the IN direction - i.e. data flows from device to host.
67 #define ENDPOINT_DIR_IN (1 << EPDIR)
69 /** Mask for the bank mode selection for the Endpoint_ConfigureEndpoint() macro. This indicates
70 * that the endpoint should have one single bank, which requires less USB FIFO memory but results
71 * in slower transfers as only one USB device (the AVR or the host) can access the endpoint's
72 * bank at the one time.
74 #define ENDPOINT_BANK_SINGLE (0 << EPBK0)
76 /** Mask for the bank mode selection for the Endpoint_ConfigureEndpoint() macro. This indicates
77 * that the endpoint should have two banks, which requires more USB FIFO memory but results
78 * in faster transfers as one USB device (the AVR or the host) can access one bank while the other
79 * accesses the second bank.
81 #define ENDPOINT_BANK_DOUBLE (1 << EPBK0)
83 /** Endpoint address for the default control endpoint, which always resides in address 0. This is
84 * defined for convenience to give more readable code when used with the endpoint macros.
86 #define ENDPOINT_CONTROLEP 0
88 /** Default size of the default control endpoint's bank, until altered by the Endpoint0Size value
89 * in the device descriptor. Not available if the FIXED_CONTROL_ENDPOINT_SIZE token is defined.
91 #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
92 #define ENDPOINT_CONTROLEP_DEFAULT_SIZE 8
95 /** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's
96 * numerical address in the device.
98 #define ENDPOINT_EPNUM_MASK 0x03
100 /** Endpoint bank size mask, for masking against endpoint addresses to retrieve the endpoint's
101 * bank size in the device.
103 #define ENDPOINT_EPSIZE_MASK 0x7FF
105 /** Maximum size in bytes of a given endpoint.
107 * \param n Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1)
109 #define ENDPOINT_MAX_SIZE(n) _ENDPOINT_GET_MAXSIZE(n)
111 /** Indicates if the given endpoint supports double banking.
113 * \param n Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1)
115 #define ENDPOINT_DOUBLEBANK_SUPPORTED(n) _ENDPOINT_GET_DOUBLEBANK(n)
117 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER) || defined(__DOXYGEN__)
118 /** Total number of endpoints (including the default control endpoint at address 0) which may
119 * be used in the device. Different USB AVR models support different amounts of endpoints,
120 * this value reflects the maximum number of endpoints for the currently selected AVR model.
122 #define ENDPOINT_TOTAL_ENDPOINTS 7
124 #define ENDPOINT_TOTAL_ENDPOINTS 5
127 /** Interrupt definition for the endpoint SETUP interrupt (for CONTROL type endpoints). Should be
128 * used with the USB_INT_* macros located in USBInterrupt.h.
130 * This interrupt will fire if enabled on a CONTROL type endpoint if a new control packet is
131 * received from the host.
133 * \note This interrupt must be enabled and cleared on *each* endpoint which requires it (after the
134 * endpoint is selected), and will fire the common endpoint interrupt vector.
136 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
138 #define ENDPOINT_INT_SETUP UEIENX, (1 << RXSTPE), UEINTX, (1 << RXSTPI)
140 /** Interrupt definition for the endpoint IN interrupt (for INTERRUPT type endpoints). Should be
141 * used with the USB_INT_* macros located in USBInterrupt.h.
143 * This interrupt will fire if enabled on an INTERRUPT type endpoint if a the endpoint interrupt
144 * period has elapsed and the endpoint is ready for a new packet to be written to its FIFO buffer
147 * \note This interrupt must be enabled and cleared on *each* endpoint which requires it (after the
148 * endpoint is selected), and will fire the common endpoint interrupt vector.
150 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
152 #define ENDPOINT_INT_IN UEIENX, (1 << TXINE) , UEINTX, (1 << TXINI)
154 /** Interrupt definition for the endpoint OUT interrupt (for INTERRUPT type endpoints). Should be
155 * used with the USB_INT_* macros located in USBInterrupt.h.
157 * This interrupt will fire if enabled on an INTERRUPT type endpoint if a the endpoint interrupt
158 * period has elapsed and the endpoint is ready for a packet from the host to be read from its
159 * FIFO buffer (if received).
161 * \note This interrupt must be enabled and cleared on *each* endpoint which requires it (after the
162 * endpoint is selected), and will fire the common endpoint interrupt vector.
164 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
166 #define ENDPOINT_INT_OUT UEIENX, (1 << RXOUTE), UEINTX, (1 << RXOUTI)
168 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER) || defined(__DOXYGEN__)
169 /** Indicates the number of bytes currently stored in the current endpoint's selected bank. */
170 #define Endpoint_BytesInEndpoint() UEBCX
172 #define Endpoint_BytesInEndpoint() UEBCLX
175 /** Returns the endpoint address of the currently selected endpoint. This is typically used to save
176 * the currently selected endpoint number so that it can be restored after another endpoint has
179 #define Endpoint_GetCurrentEndpoint() (UENUM & ENDPOINT_EPNUM_MASK)
181 /** Selects the given endpoint number. If the address from the device descriptors is used, the
182 * value should be masked with the ENDPOINT_EPNUM_MASK constant to extract only the endpoint
183 * number (and discarding the endpoint direction bit).
185 * Any endpoint operations which do not require the endpoint number to be indicated will operate on
186 * the currently selected endpoint.
188 #define Endpoint_SelectEndpoint(epnum) MACROS{ UENUM = epnum; }MACROE
190 /** Resets the endpoint bank FIFO. This clears all the endpoint banks and resets the USB controller's
191 * In and Out pointers to the bank's contents.
193 #define Endpoint_ResetFIFO(epnum) MACROS{ UERST = (1 << epnum); UERST = 0; }MACROE
195 /** Enables the currently selected endpoint so that data can be sent and received through it to
198 * \note Endpoints must first be configured properly rather than just being enabled via the
199 * Endpoint_ConfigureEndpoint() macro, which calls Endpoint_EnableEndpoint() automatically.
201 #define Endpoint_EnableEndpoint() MACROS{ UECONX |= (1 << EPEN); }MACROE
203 /** Disables the currently selected endpoint so that data cannot be sent and received through it
204 * to and from a host.
206 #define Endpoint_DisableEndpoint() MACROS{ UECONX &= ~(1 << EPEN); }MACROE
208 /** Returns true if the currently selected endpoint is enabled, false otherwise. */
209 #define Endpoint_IsEnabled() ((UECONX & (1 << EPEN)) ? true : false)
211 /** Returns true if the currently selected endpoint may be read from (if data is waiting in the endpoint
212 * bank and the endpoint is an OUT direction, or if the bank is not yet full if the endpoint is an
213 * IN direction). This function will return false if an error has occurred in the endpoint, or if
214 * the endpoint is an OUT direction and no packet has been received, or if the endpoint is an IN
215 * direction and the endpoint bank is full.
217 #define Endpoint_ReadWriteAllowed() ((UEINTX & (1 << RWAL)) ? true : false)
219 /** Returns true if the currently selected endpoint is configured, false otherwise. */
220 #define Endpoint_IsConfigured() ((UESTA0X & (1 << CFGOK)) ? true : false)
222 /** Returns a mask indicating which INTERRUPT type endpoints have interrupted - i.e. their
223 * interrupt duration has elapsed. Which endpoints have interrupted can be determined by
224 * masking the return value against (1 << {Endpoint Number}).
226 #define Endpoint_GetEndpointInterrupts() UEINT
228 /** Clears the endpoint interrupt flag. This clears the specified endpoint number's interrupt
229 * mask in the endpoint interrupt flag register.
231 #define Endpoint_ClearEndpointInterrupt(n) MACROS{ UEINT &= ~(1 << n); }MACROE
233 /** Returns true if the specified endpoint number has interrupted (valid only for INTERRUPT type
234 * endpoints), false otherwise.
236 #define Endpoint_HasEndpointInterrupted(n) ((UEINT & (1 << n)) ? true : false)
238 /** Clears the currently selected endpoint bank, and switches to the alternate bank if the currently
239 * selected endpoint is dual-banked. When cleared, this either frees the bank up for the next packet
240 * from the host (if the endpoint is of the OUT direction) or sends the packet contents to the host
241 * (if the endpoint is of the IN direction).
243 #define Endpoint_ClearCurrentBank() MACROS{ UEINTX &= ~(1 << FIFOCON); }MACROE
245 /** Returns true if the current CONTROL type endpoint is ready for an IN packet, false otherwise. */
246 #define Endpoint_IsSetupINReady() ((UEINTX & (1 << TXINI)) ? true : false)
248 /** Returns true if the current CONTROL type endpoint is ready for an OUT packet, false otherwise. */
249 #define Endpoint_IsSetupOUTReceived() ((UEINTX & (1 << RXOUTI)) ? true : false)
251 /** Returns true if the current CONTROL type endpoint is ready for a SETUP packet, false otherwise. */
252 #define Endpoint_IsSetupReceived() ((UEINTX & (1 << RXSTPI)) ? true : false)
254 /** Clears a received SETUP packet on the currently selected CONTROL type endpoint. */
255 #define Endpoint_ClearSetupReceived() MACROS{ UEINTX &= ~(1 << RXSTPI); }MACROE
257 /** Sends an IN packet to the host on the currently selected CONTROL type endpoint. */
258 #define Endpoint_ClearSetupIN() MACROS{ UEINTX &= ~(1 << TXINI); }MACROE
260 /** Acknowledges an OUT packet to the host on the currently selected CONTROL type endpoint, freeing
261 * up the endpoint for the next packet.
263 #define Endpoint_ClearSetupOUT() MACROS{ UEINTX &= ~(1 << RXOUTI); }MACROE
265 /** Stalls the current endpoint, indicating to the host that a logical problem occurred with the
266 * indicated endpoint and that the current transfer sequence should be aborted. This provides a
267 * way for devices to indicate invalid commands to the host so that the current transfer can be
268 * aborted and the host can begin its own recovery sequence.
270 * The currently selected endpoint remains stalled until either the Endpoint_ClearStall() macro
271 * is called, or the host issues a CLEAR FEATURE request to the device for the currently selected
274 #define Endpoint_StallTransaction() MACROS{ UECONX |= (1 << STALLRQ); }MACROE
276 /** Clears the stall on the currently selected endpoint. */
277 #define Endpoint_ClearStall() MACROS{ UECONX |= (1 << STALLRQC); }MACROE
279 /** Returns true if the currently selected endpoint is stalled, false otherwise. */
280 #define Endpoint_IsStalled() ((UECONX & (1 << STALLRQ)) ? true : false)
282 /** Resets the data toggle of the currently selected endpoint. */
283 #define Endpoint_ResetDataToggle() MACROS{ UECONX |= (1 << RSTDT); }MACROE
286 /** Enum for the possible error return codes of the Endpoint_WaitUntilReady function */
287 enum Endpoint_WaitUntilReady_ErrorCodes_t
289 ENDPOINT_READYWAIT_NoError
= 0, /**< Endpoint is ready for next packet, no error. */
290 ENDPOINT_READYWAIT_EndpointStalled
= 1, /**< The endpoint was stalled during the stream
291 * transfer by the host or device.
293 ENDPOINT_READYWAIT_DeviceDisconnected
= 2, /**< Device was disconnected from the host while
294 * waiting for the endpoint to become ready.
296 ENDPOINT_READYWAIT_Timeout
= 3, /**< The host failed to accept or send the next packet
297 * within the software timeout period set by the
298 * USB_STREAM_TIMEOUT_MS macro.
302 /** Enum for the possible error return codes of the Endpoint_*_Stream_* functions. */
303 enum Endpoint_Stream_RW_ErrorCodes_t
305 ENDPOINT_RWSTREAM_ERROR_NoError
= 0, /**< Command completed successfully, no error. */
306 ENDPOINT_RWSTREAM_ERROR_EndpointStalled
= 1, /**< The endpoint was stalled during the stream
307 * transfer by the host or device.
309 ENDPOINT_RWSTREAM_ERROR_DeviceDisconnected
= 1, /**< Device was disconnected from the host during
312 ENDPOINT_RWSTREAM_ERROR_Timeout
= 2, /**< The host failed to accept or send the next packet
313 * within the software timeout period set by the
314 * USB_STREAM_TIMEOUT_MS macro.
316 ENDPOINT_RWSTREAM_ERROR_CallbackAborted
= 3, /**< Indicates that the stream's callback function
317 * aborted the transfer early.
321 /** Enum for the possible error return codes of the Endpoint_*_Control_Stream_* functions. */
322 enum Endpoint_ControlStream_RW_ErrorCodes_t
324 ENDPOINT_RWCSTREAM_ERROR_NoError
= 0, /**< Command completed successfully, no error. */
325 ENDPOINT_RWCSTREAM_ERROR_HostAborted
= 1, /**< The aborted the transfer prematurely. */
328 /* Inline Functions: */
329 /** Reads one byte from the currently selected endpoint's bank, for OUT direction endpoints. */
330 static inline uint8_t Endpoint_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
331 static inline uint8_t Endpoint_Read_Byte(void)
336 /** Writes one byte from the currently selected endpoint's bank, for IN direction endpoints. */
337 static inline void Endpoint_Write_Byte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
338 static inline void Endpoint_Write_Byte(const uint8_t Byte
)
343 /** Discards one byte from the currently selected endpoint's bank, for OUT direction endpoints. */
344 static inline void Endpoint_Discard_Byte(void) ATTR_ALWAYS_INLINE
;
345 static inline void Endpoint_Discard_Byte(void)
352 /** Reads two bytes from the currently selected endpoint's bank in little endian format, for OUT
353 * direction endpoints.
355 static inline uint16_t Endpoint_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
356 static inline uint16_t Endpoint_Read_Word_LE(void)
361 Data
|= (((uint16_t)UEDATX
) << 8);
366 /** Reads two bytes from the currently selected endpoint's bank in big endian format, for OUT
367 * direction endpoints.
369 static inline uint16_t Endpoint_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
370 static inline uint16_t Endpoint_Read_Word_BE(void)
374 Data
= (((uint16_t)UEDATX
) << 8);
380 /** Writes two bytes to the currently selected endpoint's bank in little endian format, for IN
381 * direction endpoints.
383 static inline void Endpoint_Write_Word_LE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
384 static inline void Endpoint_Write_Word_LE(const uint16_t Word
)
386 UEDATX
= (Word
& 0xFF);
387 UEDATX
= (Word
>> 8);
390 /** Writes two bytes to the currently selected endpoint's bank in big endian format, for IN
391 * direction endpoints.
393 static inline void Endpoint_Write_Word_BE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
394 static inline void Endpoint_Write_Word_BE(const uint16_t Word
)
396 UEDATX
= (Word
>> 8);
397 UEDATX
= (Word
& 0xFF);
400 /** Discards two bytes from the currently selected endpoint's bank, for OUT direction endpoints. */
401 static inline void Endpoint_Discard_Word(void) ATTR_ALWAYS_INLINE
;
402 static inline void Endpoint_Discard_Word(void)
410 /** Reads four bytes from the currently selected endpoint's bank in little endian format, for OUT
411 * direction endpoints.
413 static inline uint32_t Endpoint_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
414 static inline uint32_t Endpoint_Read_DWord_LE(void)
422 Data
.Bytes
[0] = UEDATX
;
423 Data
.Bytes
[1] = UEDATX
;
424 Data
.Bytes
[2] = UEDATX
;
425 Data
.Bytes
[3] = UEDATX
;
430 /** Reads four bytes from the currently selected endpoint's bank in big endian format, for OUT
431 * direction endpoints.
433 static inline uint32_t Endpoint_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
434 static inline uint32_t Endpoint_Read_DWord_BE(void)
442 Data
.Bytes
[3] = UEDATX
;
443 Data
.Bytes
[2] = UEDATX
;
444 Data
.Bytes
[1] = UEDATX
;
445 Data
.Bytes
[0] = UEDATX
;
450 /** Writes four bytes to the currently selected endpoint's bank in little endian format, for IN
451 * direction endpoints.
453 static inline void Endpoint_Write_DWord_LE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
454 static inline void Endpoint_Write_DWord_LE(const uint32_t DWord
)
456 UEDATX
= (DWord
& 0xFF);
457 UEDATX
= (DWord
>> 8);
458 UEDATX
= (DWord
>> 16);
459 UEDATX
= (DWord
>> 24);
462 /** Writes four bytes to the currently selected endpoint's bank in big endian format, for IN
463 * direction endpoints.
465 static inline void Endpoint_Write_DWord_BE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
466 static inline void Endpoint_Write_DWord_BE(const uint32_t DWord
)
468 UEDATX
= (DWord
>> 24);
469 UEDATX
= (DWord
>> 16);
470 UEDATX
= (DWord
>> 8);
471 UEDATX
= (DWord
& 0xFF);
474 /** Discards four bytes from the currently selected endpoint's bank, for OUT direction endpoints. */
475 static inline void Endpoint_Discard_DWord(void) ATTR_ALWAYS_INLINE
;
476 static inline void Endpoint_Discard_DWord(void)
486 /* External Variables: */
487 /** Global indicating the maximum packet size of the default control endpoint located at address
488 * 0 in the device. This value is set to the value indicated in the device descriptor in the user
489 * project once the USB interface is initialized into device mode.
491 * If space is an issue, it is possible to fix this to a static value by defining the control
492 * endpoint size in the FIXED_CONTROL_ENDPOINT_SIZE token passed to the compiler in the makefile
493 * via the -D switch. When a fixed control endpoint size is used, the size is no longer dynamically
494 * read from the descriptors at runtime and instead fixed to the given value. When used, it is
495 * important that the descriptor control endpoint size value matches the size given as the
496 * FIXED_CONTROL_ENDPOINT_SIZE token - it is recommended that the FIXED_CONTROL_ENDPOINT_SIZE token
497 * be used in the descriptors to ensure this.
499 * \note This variable should be treated as read-only in the user application, and never manually
502 #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
503 extern uint8_t USB_ControlEndpointSize
;
505 #define USB_ControlEndpointSize FIXED_CONTROL_ENDPOINT_SIZE
508 /* Function Prototypes: */
509 /** Configures the specified endpoint number with the given endpoint type, direction, bank size
510 * and banking mode. Endpoints should be allocated in ascending order by their address in the
511 * device (i.e. endpoint 1 should be configured before endpoint 2 and so on).
513 * The endpoint type may be one of the EP_TYPE_* macros listed in LowLevel.h and the direction
514 * may be either ENDPOINT_DIR_OUT or ENDPOINT_DIR_IN.
516 * The bank size must indicate the maximum packet size that the endpoint can handle. Different
517 * endpoint numbers can handle different maximum packet sizes - refer to the chosen USB AVR's
518 * datasheet to determine the maximum bank size for each endpoint.
520 * The banking mode may be either ENDPOINT_BANK_SINGLE or ENDPOINT_BANK_DOUBLE.
522 * The success of this routine can be determined via the Endpoint_IsConfigured() macro.
524 * By default, the routine is entirely dynamic, and will accept both constant and variable inputs.
525 * If dynamic configuration is unused, a small space savings can be made by defining the
526 * STATIC_ENDPOINT_CONFIGURATION macro via the -D switch to the compiler, to optimize for constant
529 * \note This routine will select the specified endpoint, and the endpoint will remain selected
530 * once the routine completes regardless of if the endpoint configuration succeeds.
532 * \return Boolean true if the configuration succeeded, false otherwise
534 bool Endpoint_ConfigureEndpoint(const uint8_t Number
, const uint8_t Type
, const uint8_t Direction
,
535 const uint16_t Size
, const uint8_t Banks
);
537 /** Spinloops until the currently selected non-control endpoint is ready for the next packet of data
538 * to be read or written to it.
540 * \note This routine should not be called on CONTROL type endpoints.
542 * \return A value from the Endpoint_WaitUntilReady_ErrorCodes_t enum.
544 uint8_t Endpoint_WaitUntilReady(void);
546 /** Reads and discards the given number of bytes from the endpoint from the given buffer,
547 * discarding fully read packets from the host as needed. The last packet is not automatically
548 * discarded once the remaining bytes has been read; the user is responsible for manually
549 * discarding the last packet from the host via the Endpoint_ClearCurrentBank() macro. Between
550 * each USB packet, the given stream callback function is executed repeatedly until the next
551 * packet is ready, allowing for early aborts of stream transfers.
553 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
554 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
555 * and this function has the Callback parameter omitted.
557 * \note This routine should not be used on CONTROL type endpoints.
559 * \param Length Number of bytes to send via the currently selected endpoint.
560 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
562 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
564 uint8_t Endpoint_Discard_Stream(uint16_t Length
565 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
566 , uint8_t (* const Callback
)(void)
570 /** Writes the given number of bytes to the endpoint from the given buffer in little endian,
571 * sending full packets to the host as needed. The last packet filled is not automatically sent;
572 * the user is responsible for manually sending the last written packet to the host via the
573 * Endpoint_ClearCurrentBank() macro. Between each USB packet, the given stream callback function
574 * is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early
575 * aborts of stream transfers.
577 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
578 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
579 * and this function has the Callback parameter omitted.
581 * \note This routine should not be used on CONTROL type endpoints.
583 * \param Buffer Pointer to the source data buffer to read from.
584 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
585 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
587 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
589 uint8_t Endpoint_Write_Stream_LE(const void* Buffer
, uint16_t Length
590 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
591 , uint8_t (* const Callback
)(void)
593 ) ATTR_NON_NULL_PTR_ARG(1);
595 /** Writes the given number of bytes to the endpoint from the given buffer in big endian,
596 * sending full packets to the host as needed. The last packet filled is not automatically sent;
597 * the user is responsible for manually sending the last written packet to the host via the
598 * Endpoint_ClearCurrentBank() macro. Between each USB packet, the given stream callback function
599 * is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early
600 * aborts of stream transfers.
602 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
603 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
604 * and this function has the Callback parameter omitted.
606 * \note This routine should not be used on CONTROL type endpoints.
608 * \param Buffer Pointer to the source data buffer to read from.
609 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
610 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
612 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
614 uint8_t Endpoint_Write_Stream_BE(const void* Buffer
, uint16_t Length
615 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
616 , uint8_t (* const Callback
)(void)
618 ) ATTR_NON_NULL_PTR_ARG(1);
620 /** Reads the given number of bytes from the endpoint from the given buffer in little endian,
621 * discarding fully read packets from the host as needed. The last packet is not automatically
622 * discarded once the remaining bytes has been read; the user is responsible for manually
623 * discarding the last packet from the host via the Endpoint_ClearCurrentBank() macro. Between
624 * each USB packet, the given stream callback function is executed repeatedly until the endpoint
625 * is ready to accept the next packet, allowing for early aborts of stream transfers.
627 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
628 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
629 * and this function has the Callback parameter omitted.
631 * \note This routine should not be used on CONTROL type endpoints.
633 * \param Buffer Pointer to the destination data buffer to write to.
634 * \param Length Number of bytes to send via the currently selected endpoint.
635 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
637 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
639 uint8_t Endpoint_Read_Stream_LE(void* Buffer
, uint16_t Length
640 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
641 , uint8_t (* const Callback
)(void)
643 ) ATTR_NON_NULL_PTR_ARG(1);
645 /** Reads the given number of bytes from the endpoint from the given buffer in big endian,
646 * discarding fully read packets from the host as needed. The last packet is not automatically
647 * discarded once the remaining bytes has been read; the user is responsible for manually
648 * discarding the last packet from the host via the Endpoint_ClearCurrentBank() macro. Between
649 * each USB packet, the given stream callback function is executed repeatedly until the endpoint
650 * is ready to accept the next packet, allowing for early aborts of stream transfers.
652 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
653 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
654 * and this function has the Callback parameter omitted.
656 * \note This routine should not be used on CONTROL type endpoints.
658 * \param Buffer Pointer to the destination data buffer to write to.
659 * \param Length Number of bytes to send via the currently selected endpoint.
660 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
662 * \return A value from the Endpoint_Stream_RW_ErrorCodes_t enum.
664 uint8_t Endpoint_Read_Stream_BE(void* Buffer
, uint16_t Length
665 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
666 , uint8_t (* const Callback
)(void)
668 ) ATTR_NON_NULL_PTR_ARG(1);
670 /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in little endian,
671 * sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared
672 * in both failure and success states; the user is responsible for manually clearing the setup OUT to
673 * finalize the transfer via the Endpoint_ClearSetupOUT() macro.
675 * \note This routine should only be used on CONTROL type endpoints.
677 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
678 * together; i.e. the entire stream data must be read or written at the one time.
680 * \param Buffer Pointer to the source data buffer to read from.
681 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
683 * \return A value from the Endpoint_ControlStream_RW_ErrorCodes_t enum.
685 uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
687 /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in big endian,
688 * sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared
689 * in both failure and success states; the user is responsible for manually clearing the setup OUT to
690 * finalize the transfer via the Endpoint_ClearSetupOUT() macro.
692 * \note This routine should only be used on CONTROL type endpoints.
694 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
695 * together; i.e. the entire stream data must be read or written at the one time.
697 * \param Buffer Pointer to the source data buffer to read from.
698 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
700 * \return A value from the Endpoint_ControlStream_RW_ErrorCodes_t enum.
702 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
704 /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in little endian,
705 * discarding fully read packets from the host as needed. The device IN acknowledgement is not
706 * automatically sent after success or failure states; the user is responsible for manually sending the
707 * setup IN to finalize the transfer via the Endpoint_ClearSetupIN() macro.
709 * \note This routine should only be used on CONTROL type endpoints.
711 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
712 * together; i.e. the entire stream data must be read or written at the one time.
714 * \param Buffer Pointer to the destination data buffer to write to.
715 * \param Length Number of bytes to send via the currently selected endpoint.
717 * \return A value from the Endpoint_ControlStream_RW_ErrorCodes_t enum.
719 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
721 /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in big endian,
722 * discarding fully read packets from the host as needed. The device IN acknowledgement is not
723 * automatically sent after success or failure states; the user is responsible for manually sending the
724 * setup IN to finalize the transfer via the Endpoint_ClearSetupIN() macro.
726 * \note This routine should only be used on CONTROL type endpoints.
728 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
729 * together; i.e. the entire stream data must be read or written at the one time.
731 * \param Buffer Pointer to the destination data buffer to write to.
732 * \param Length Number of bytes to send via the currently selected endpoint.
734 * \return A value from the Endpoint_ControlStream_RW_ErrorCodes_t enum.
736 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
738 /* Function Aliases: */
739 /** Alias for Endpoint_Discard_Byte().
741 #define Endpoint_Ignore_Byte() Endpoint_Discard_Byte()
743 /** Alias for Endpoint_Discard_Word().
745 #define Endpoint_Ignore_Word() Endpoint_Discard_Word()
747 /** Alias for Endpoint_Discard_DWord().
749 #define Endpoint_Ignore_DWord() Endpoint_Discard_DWord()
751 /** Alias for Endpoint_Read_Word_LE(). By default USB transfers use little endian format, thus
752 * the command with no endianness specified indicates little endian mode.
754 #define Endpoint_Read_Word() Endpoint_Read_Word_LE()
756 /** Alias for Endpoint_Write_Word_LE(). By default USB transfers use little endian format, thus
757 * the command with no endianness specified indicates little endian mode.
759 #define Endpoint_Write_Word(Word) Endpoint_Write_Word_LE(Word)
761 /** Alias for Endpoint_Read_DWord_LE(). By default USB transfers use little endian format, thus
762 * the command with no endianness specified indicates little endian mode.
764 #define Endpoint_Read_DWord() Endpoint_Read_DWord_LE()
766 /** Alias for Endpoint_Write_DWord_LE(). By default USB transfers use little endian format, thus
767 * the command with no endianness specified indicates little endian mode.
769 #define Endpoint_Write_DWord(DWord) Endpoint_Write_DWord_LE(DWord)
771 /** Alias for Endpoint_Read_Stream_LE(). By default USB transfers use little endian format, thus
772 * the command with no endianness specified indicates little endian mode.
774 #if !defined(NO_STREAM_CALLBACKS)
775 #define Endpoint_Read_Stream(Buffer, Length, Callback) Endpoint_Read_Stream_LE(Buffer, Length, Callback)
777 #define Endpoint_Read_Stream(Buffer, Length) Endpoint_Read_Stream_LE(Buffer, Length)
780 /** Alias for Endpoint_Write_Stream_LE(). By default USB transfers use little endian format, thus
781 * the command with no endianness specified indicates little endian mode.
783 #if !defined(NO_STREAM_CALLBACKS)
784 #define Endpoint_Write_Stream(Buffer, Length, Callback) Endpoint_Write_Stream_LE(Buffer, Length, Callback)
786 #define Endpoint_Write_Stream(Buffer, Length) Endpoint_Write_Stream_LE(Buffer, Length)
789 /** Alias for Endpoint_Read_Control_Stream_LE(). By default USB transfers use little endian format, thus
790 * the command with no endianness specified indicates little endian mode.
792 #define Endpoint_Read_Control_Stream(Data, Length) Endpoint_Read_Control_Stream_LE(Data, Length)
794 /** Alias for Endpoint_Write_Control_Stream_LE(). By default USB transfers use little endian format, thus
795 * the command with no endianness specified indicates little endian mode.
797 #define Endpoint_Write_Control_Stream(Data, Length) Endpoint_Write_Control_Stream_LE(Data, Length)
799 /* Private Interface - For use in library only: */
800 #if !defined(__DOXYGEN__)
802 #define Endpoint_AllocateMemory() MACROS{ UECFG1X |= (1 << ALLOC); }MACROE
803 #define Endpoint_DeallocateMemory() MACROS{ UECFG1X &= ~(1 << ALLOC); }MACROE
805 #define _ENDPOINT_GET_MAXSIZE(n) _ENDPOINT_GET_MAXSIZE2(ENDPOINT_DETAILS_EP ## n)
806 #define _ENDPOINT_GET_MAXSIZE2(details) _ENDPOINT_GET_MAXSIZE3(details)
807 #define _ENDPOINT_GET_MAXSIZE3(maxsize, db) maxsize
809 #define _ENDPOINT_GET_DOUBLEBANK(n) _ENDPOINT_GET_DOUBLEBANK2(ENDPOINT_DETAILS_EP ## n)
810 #define _ENDPOINT_GET_DOUBLEBANK2(details) _ENDPOINT_GET_DOUBLEBANK3(details)
811 #define _ENDPOINT_GET_DOUBLEBANK3(maxsize, db) db
813 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER)
814 #define ENDPOINT_DETAILS_EP0 64, true
815 #define ENDPOINT_DETAILS_EP1 256, true
816 #define ENDPOINT_DETAILS_EP2 64, true
817 #define ENDPOINT_DETAILS_EP3 64, true
818 #define ENDPOINT_DETAILS_EP4 64, true
819 #define ENDPOINT_DETAILS_EP5 64, true
820 #define ENDPOINT_DETAILS_EP6 64, true
822 #define ENDPOINT_DETAILS_EP0 64, true
823 #define ENDPOINT_DETAILS_EP1 64, false
824 #define ENDPOINT_DETAILS_EP2 64, false
825 #define ENDPOINT_DETAILS_EP3 64, true
826 #define ENDPOINT_DETAILS_EP4 64, true
829 #if defined(STATIC_ENDPOINT_CONFIGURATION)
830 #define Endpoint_ConfigureEndpoint(Number, Type, Direction, Size, Banks) \
831 Endpoint_ConfigureEndpointStatic(Number, \
832 ((Type << EPTYPE0) | Direction), \
833 ((1 << ALLOC) | Banks | Endpoint_BytesToEPSizeMask(Size)));
836 /* Function Prototypes: */
837 void Endpoint_ClearEndpoints(void);
838 bool Endpoint_ConfigureEndpointStatic(const uint8_t Number
, const uint8_t UECFG0XData
, const uint8_t UECFG1XData
);
840 /* Inline Functions: */
841 static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes
) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE
;
842 static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes
)
845 return (0 << EPSIZE0
);
846 else if (Bytes
<= 16)
847 return (1 << EPSIZE0
);
848 else if (Bytes
<= 32)
849 return (2 << EPSIZE0
);
850 #if defined(USB_LIMITED_CONTROLLER)
852 return (3 << EPSIZE0
);
854 else if (Bytes
<= 64)
855 return (3 << EPSIZE0
);
856 else if (Bytes
<= 128)
857 return (4 << EPSIZE0
);
859 return (5 << EPSIZE0
);
865 /* Disable C linkage for C++ Compilers: */
866 #if defined(__cplusplus)