3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
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/receive 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 /** \ingroup Group_EndpointRW
47 * @defgroup Group_EndpointPrimitiveRW Read/Write of Primitive Data Types
49 * Functions, macros, variables, enums and types related to data reading and writing of primitive data types
50 * from and to endpoints.
53 /** \ingroup Group_EndpointRW
54 * @defgroup Group_EndpointStreamRW Read/Write of Multi-Byte Streams
56 * Functions, macros, variables, enums and types related to data reading and writing of data streams from
60 /** @defgroup Group_EndpointPacketManagement Endpoint Packet Management
62 * Functions, macros, variables, enums and types related to packet management of endpoints.
65 #ifndef __ENDPOINT_H__
66 #define __ENDPOINT_H__
69 #if defined(__AVR32__)
73 #elif defined(__AVR__)
75 #include <avr/pgmspace.h>
76 #include <avr/eeprom.h>
80 #include "../../../Common/Common.h"
82 #include "../HighLevel/USBTask.h"
84 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
85 #include "../HighLevel/StreamCallbacks.h"
88 /* Enable C linkage for C++ Compilers: */
89 #if defined(__cplusplus)
93 /* Preprocessor Checks: */
94 #if !defined(__INCLUDE_FROM_USB_DRIVER)
95 #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
98 #if defined(__AVR32__) && !defined(__AVR32_EPREG_X)
99 #define __AVR32_EPREG_X(x) ((volatile uint32_t*)AVR32_USBB_ ## x)[USB_SelectedEPNumber]
102 /* Public Interface - May be used in end-application: */
104 #if defined(__AVR32__) || defined(__DOXYGEN__)
105 /** Endpoint data direction mask for \ref Endpoint_ConfigureEndpoint(). This indicates that the endpoint
106 * should be initialized in the OUT direction - i.e. data flows from host to device.
108 #define ENDPOINT_DIR_OUT 0
110 /** Endpoint data direction mask for \ref Endpoint_ConfigureEndpoint(). This indicates that the endpoint
111 * should be initialized in the IN direction - i.e. data flows from device to host.
113 #define ENDPOINT_DIR_IN AVR32_USBB_EPDIR_IN
115 /** Mask for the bank mode selection for the \ref Endpoint_ConfigureEndpoint() macro. This indicates
116 * that the endpoint should have one single bank, which requires less USB FIFO memory but results
117 * in slower transfers as only one USB device (the AVR or the host) can access the endpoint's
118 * bank at the one time.
120 #define ENDPOINT_BANK_SINGLE AVR32_USBB_EPBK_SINGLE
122 /** Mask for the bank mode selection for the \ref Endpoint_ConfigureEndpoint() macro. This indicates
123 * that the endpoint should have two banks, which requires more USB FIFO memory but results
124 * in faster transfers as one USB device (the AVR or the host) can access one bank while the other
125 * accesses the second bank.
127 #define ENDPOINT_BANK_DOUBLE AVR32_USBB_EPBK_DOUBLE
128 #elif defined(__AVR__)
129 #define ENDPOINT_DIR_OUT (0 << EPDIR)
130 #define ENDPOINT_DIR_IN (1 << EPDIR)
131 #define ENDPOINT_BANK_SINGLE (0 << EPBK0)
132 #define ENDPOINT_BANK_DOUBLE (1 << EPBK0)
135 /** Endpoint address for the default control endpoint, which always resides in address 0. This is
136 * defined for convenience to give more readable code when used with the endpoint macros.
138 #define ENDPOINT_CONTROLEP 0
140 #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
141 /** Default size of the default control endpoint's bank, until altered by the Endpoint0Size value
142 * in the device descriptor. Not available if the FIXED_CONTROL_ENDPOINT_SIZE token is defined.
144 #define ENDPOINT_CONTROLEP_DEFAULT_SIZE 8
147 /** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's
148 * numerical address in the device.
150 #define ENDPOINT_EPNUM_MASK 0x07
152 /** Endpoint direction mask, for masking against endpoint addresses to retrieve the endpoint's
153 * direction for comparing with the ENDPOINT_DESCRIPTOR_DIR_* masks.
155 #define ENDPOINT_EPDIR_MASK 0x80
157 /** Endpoint bank size mask, for masking against endpoint addresses to retrieve the endpoint's
158 * bank size in the device.
160 #define ENDPOINT_EPSIZE_MASK 0x7FF
162 /** Maximum size in bytes of a given endpoint.
164 * \param[in] n Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1)
166 #define ENDPOINT_MAX_SIZE(n) _ENDPOINT_GET_MAXSIZE(n)
168 /** Indicates if the given endpoint supports double banking.
170 * \param[in] n Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1)
172 #define ENDPOINT_DOUBLEBANK_SUPPORTED(n) _ENDPOINT_GET_DOUBLEBANK(n)
174 #if !defined(CONTROL_ONLY_DEVICE)
175 #if (defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR) || \
176 defined(USB_SERIES_UC3B_AVR) || defined(__DOXYGEN__))
177 /** Total number of endpoints (including the default control endpoint at address 0) which may
178 * be used in the device. Different USB AVR models support different amounts of endpoints,
179 * this value reflects the maximum number of endpoints for the currently selected AVR model.
181 #define ENDPOINT_TOTAL_ENDPOINTS 7
183 #define ENDPOINT_TOTAL_ENDPOINTS 5
186 #define ENDPOINT_TOTAL_ENDPOINTS 1
189 /* Pseudo-Function Macros: */
190 #if defined(__DOXYGEN__)
191 /** Indicates the number of bytes currently stored in the current endpoint's selected bank.
193 * \note The return width of this function may differ, depending on the maximum endpoint bank size
194 * of the selected AVR model.
196 * \ingroup Group_EndpointRW
198 * \return Total number of bytes in the currently selected Endpoint's FIFO buffer
200 static inline uint16_t Endpoint_BytesInEndpoint(void);
202 /** Get the endpoint address of the currently selected endpoint. This is typically used to save
203 * the currently selected endpoint number so that it can be restored after another endpoint has
206 * \return Index of the currently selected endpoint
208 static inline uint8_t Endpoint_GetCurrentEndpoint(void);
210 /** Selects the given endpoint number. If the address from the device descriptors is used, the
211 * value should be masked with the \ref ENDPOINT_EPNUM_MASK constant to extract only the endpoint
212 * number (and discarding the endpoint direction bit).
214 * Any endpoint operations which do not require the endpoint number to be indicated will operate on
215 * the currently selected endpoint.
217 * \param[in] EndpointNumber Endpoint number to select
219 static inline void Endpoint_SelectEndpoint(uint8_t EndpointNumber
);
221 /** Resets the endpoint bank FIFO. This clears all the endpoint banks and resets the USB controller's
222 * In and Out pointers to the bank's contents.
224 * \param[in] EndpointNumber Endpoint number whose FIFO buffers are to be reset
226 static inline void Endpoint_ResetFIFO(uint8_t EndpointNumber
);
228 /** Enables the currently selected endpoint so that data can be sent and received through it to
231 * \note Endpoints must first be configured properly via \ref Endpoint_ConfigureEndpoint().
233 static inline void Endpoint_EnableEndpoint(void);
235 /** Disables the currently selected endpoint so that data cannot be sent and received through it
236 * to and from a host.
238 static inline void Endpoint_DisableEndpoint(void);
240 /** Determines if the currently selected endpoint is enabled, but not necessarily configured.
242 * \return Boolean True if the currently selected endpoint is enabled, false otherwise
244 static inline bool Endpoint_IsEnabled(void);
246 /** Determines if the currently selected endpoint may be read from (if data is waiting in the endpoint
247 * bank and the endpoint is an OUT direction, or if the bank is not yet full if the endpoint is an IN
248 * direction). This function will return false if an error has occurred in the endpoint, if the endpoint
249 * is an OUT direction and no packet (or an empty packet) has been received, or if the endpoint is an IN
250 * direction and the endpoint bank is full.
252 * \ingroup Group_EndpointPacketManagement
254 * \return Boolean true if the currently selected endpoint may be read from or written to, depending on its direction
256 static inline bool Endpoint_IsReadWriteAllowed(void);
258 /** Determines if the currently selected endpoint is configured.
260 * \return Boolean true if the currently selected endpoint has been configured, false otherwise
262 static inline bool Endpoint_IsConfigured(void);
264 /** Returns a mask indicating which INTERRUPT type endpoints have interrupted - i.e. their
265 * interrupt duration has elapsed. Which endpoints have interrupted can be determined by
266 * masking the return value against (1 << {Endpoint Number}).
268 * \return Mask whose bits indicate which endpoints have interrupted
270 static inline uint8_t Endpoint_GetEndpointInterrupts(void);
272 /** Determines if the specified endpoint number has interrupted (valid only for INTERRUPT type
275 * \param[in] 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 \ref 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 /** Sets the direction of the currently selected endpoint.
366 * \param[in] DirectionMask New endpoint direction, as a ENDPOINT_DIR_* mask.
368 static inline void Endpoint_SetEndpointDirection(uint8_t DirectionMask
);
370 #if defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
371 #define Endpoint_BytesInEndpoint() UEBCX
372 #elif defined(USB_SERIES_4_AVR)
373 #define Endpoint_BytesInEndpoint() (((uint16_t)UEBCHX << 8) | UEBCLX)
374 #elif defined(USB_SERIES_2_AVR)
375 #define Endpoint_BytesInEndpoint() UEBCLX
376 #elif defined(USB_SERIES_UC3B_AVR)
377 #define Endpoint_BytesInEndpoint() ((__AVR32_EPREG_X(UESTA0) & AVR32_USBB_BYCT_MASK) >> AVR32_USBB_BYCT)
380 #if defined(__AVR32__)
381 #if !defined(CONTROL_ONLY_DEVICE)
382 #define Endpoint_GetCurrentEndpoint() USB_SelectedEPNumber
383 #define Endpoint_SelectEndpoint(epnum) MACROS{ USB_SelectedEPNumber = (epnum); }MACROE
384 #define Endpoint_IsReadWriteAllowed() (__AVR32_EPREG_X(UESTA0) & AVR32_USBB_RWALL_MASK)
386 #define Endpoint_GetCurrentEndpoint() ENDPOINT_CONTROLEP
387 #define Endpoint_SelectEndpoint(epnum) (void)(epnum)
390 #define Endpoint_ResetFIFO(epnum) MACROS{ AVR32_USBB.uerst |= (AVR32_USBB_EPRST0_MASK << (epnum)); \
391 AVR32_USBB.uerst &= ~(AVR32_USBB_EPRST0_MASK << (epnum)); }MACROE
392 #define Endpoint_EnableEndpoint() MACROS{ AVR32_USBB.uerst |= (AVR32_USBB_UERST_EPEN0_MASK << USB_SelectedEPNumber); }MACROE
393 #define Endpoint_DisableEndpoint() MACROS{ AVR32_USBB.uerst &= ~(AVR32_USBB_UERST_EPEN0_MASK << USB_SelectedEPNumber); }MACROE
394 #define Endpoint_IsEnabled() ((AVR32_USBB.uerst & (AVR32_USBB_UERST_EPEN0_MASK << USB_SelectedEPNumber)) ? true : false)
396 #define Endpoint_IsConfigured() ((__AVR32_EPREG_X(UESTA0) & AVR32_USBB_UESTA0_CFGOK_MASK) ? true : false)
397 #define Endpoint_GetEndpointInterrupts() (AVR32_USBB.UDINT >> AVR32_USBB_EP0INT)
398 #define Endpoint_HasEndpointInterrupted(n) ((AVR32_USBB.UDINT & (AVR32_USBB_EP0INT << (n))) ? true : false)
399 #define Endpoint_IsINReady() ((__AVR32_EPREG_X(UESTA0) & AVR32_USBB_TXINI) ? true : false)
400 #define Endpoint_IsOUTReceived() ((__AVR32_EPREG_X(UESTA0) & AVR32_USBB_RXOUTI) ? true : false)
401 #define Endpoint_IsSETUPReceived() ((__AVR32_EPREG_X(UESTA0) & AVR32_USBB_RXSTPI) ? true : false)
402 #define Endpoint_ClearSETUP() MACROS{ __AVR32_EPREG_X(UESTA0CLR) = AVR32_USBB_RXSTPIC; }MACROE
403 #define Endpoint_ClearIN() MACROS{ __AVR32_EPREG_X(UESTA0CLR) = AVR32_USBB_TXINIC; \
404 __AVR32_EPREG_X(UECON0CLR) = AVR32_USBB_FIFOCONC; }MACROE
405 #define Endpoint_ClearOUT() MACROS{ __AVR32_EPREG_X(UESTA0CLR) = AVR32_USBB_RXOUTI; \
406 __AVR32_EPREG_X(UECON0CLR) = AVR32_USBB_FIFOCONC; }MACROE
407 #define Endpoint_StallTransaction() MACROS{ __AVR32_EPREG_X(UECON0SET) = AVR32_USBB_STALLRQS; }MACROE
408 #define Endpoint_ClearStall() MACROS{ __AVR32_EPREG_X(UECON0CLR) = AVR32_USBB_STALLRQC; }MACROE
409 #define Endpoint_IsStalled() ((__AVR32_EPREG_X(UECON0) & AVR32_USBB_STALLRQ) ? true : false)
410 #define Endpoint_ResetDataToggle() MACROS{ __AVR32_EPREG_X(UECON0CLR) = AVR32_USBB_RSTDTS; }MACROE
411 #define Endpoint_GetEndpointDirection() ((__AVR32_EPREG_X(UECFG0) & ENDPOINT_DIR_IN) ? true : false)
412 #define Endpoint_SetEndpointDirection(dir) MACROS{ __AVR32_EPREG_X(UECFG0) = \
413 ((__AVR32_EPREG_X(UECFG0) & ENDPOINT_DIR_IN) | (dir)); }MACROE
414 #elif defined(__AVR__)
415 #if !defined(CONTROL_ONLY_DEVICE)
416 #define Endpoint_GetCurrentEndpoint() (UENUM & ENDPOINT_EPNUM_MASK)
417 #define Endpoint_SelectEndpoint(epnum) MACROS{ UENUM = (epnum); }MACROE
418 #define Endpoint_IsReadWriteAllowed() ((UEINTX & (1 << RWAL)) ? true : false)
420 #define Endpoint_GetCurrentEndpoint() ENDPOINT_CONTROLEP
421 #define Endpoint_SelectEndpoint(epnum) (void)(epnum)
424 #define Endpoint_ResetFIFO(epnum) MACROS{ UERST = (1 << (epnum)); UERST = 0; }MACROE
425 #define Endpoint_EnableEndpoint() MACROS{ UECONX |= (1 << EPEN); }MACROE
426 #define Endpoint_DisableEndpoint() MACROS{ UECONX &= ~(1 << EPEN); }MACROE
427 #define Endpoint_IsEnabled() ((UECONX & (1 << EPEN)) ? true : false)
429 #define Endpoint_IsConfigured() ((UESTA0X & (1 << CFGOK)) ? true : false)
430 #define Endpoint_GetEndpointInterrupts() UEINT
431 #define Endpoint_HasEndpointInterrupted(n) ((UEINT & (1 << (n))) ? true : false)
432 #define Endpoint_IsINReady() ((UEINTX & (1 << TXINI)) ? true : false)
433 #define Endpoint_IsOUTReceived() ((UEINTX & (1 << RXOUTI)) ? true : false)
434 #define Endpoint_IsSETUPReceived() ((UEINTX & (1 << RXSTPI)) ? true : false)
435 #define Endpoint_ClearSETUP() MACROS{ UEINTX &= ~(1 << RXSTPI); }MACROE
437 #if !defined(CONTROL_ONLY_DEVICE)
438 #define Endpoint_ClearIN() MACROS{ uint8_t Temp = UEINTX; UEINTX = (Temp & ~(1 << TXINI)); \
439 UEINTX = (Temp & ~(1 << FIFOCON)); }MACROE
440 #define Endpoint_ClearOUT() MACROS{ uint8_t Temp = UEINTX; UEINTX = (Temp & ~(1 << RXOUTI)); \
441 UEINTX = (Temp & ~(1 << FIFOCON)); }MACROE
443 #define Endpoint_ClearIN() MACROS{ UEINTX &= ~(1 << TXINI); }MACROE
444 #define Endpoint_ClearOUT() MACROS{ UEINTX &= ~(1 << RXOUTI); }MACROE
447 #define Endpoint_StallTransaction() MACROS{ UECONX |= (1 << STALLRQ); }MACROE
448 #define Endpoint_ClearStall() MACROS{ UECONX |= (1 << STALLRQC); }MACROE
449 #define Endpoint_IsStalled() ((UECONX & (1 << STALLRQ)) ? true : false)
450 #define Endpoint_ResetDataToggle() MACROS{ UECONX |= (1 << RSTDT); }MACROE
451 #define Endpoint_GetEndpointDirection() (UECFG0X & ENDPOINT_DIR_IN)
452 #define Endpoint_SetEndpointDirection(dir) MACROS{ UECFG0X = ((UECFG0X & ~ENDPOINT_DIR_IN) | (dir)); }MACROE
457 /** Enum for the possible error return codes of the \ref Endpoint_WaitUntilReady() function.
459 * \ingroup Group_EndpointRW
461 enum Endpoint_WaitUntilReady_ErrorCodes_t
463 ENDPOINT_READYWAIT_NoError
= 0, /**< Endpoint is ready for next packet, no error. */
464 ENDPOINT_READYWAIT_EndpointStalled
= 1, /**< The endpoint was stalled during the stream
465 * transfer by the host or device.
467 ENDPOINT_READYWAIT_DeviceDisconnected
= 2, /**< Device was disconnected from the host while
468 * waiting for the endpoint to become ready.
470 ENDPOINT_READYWAIT_Timeout
= 3, /**< The host failed to accept or send the next packet
471 * within the software timeout period set by the
472 * \ref USB_STREAM_TIMEOUT_MS macro.
476 /** Enum for the possible error return codes of the Endpoint_*_Stream_* functions.
478 * \ingroup Group_EndpointStreamRW
480 enum Endpoint_Stream_RW_ErrorCodes_t
482 ENDPOINT_RWSTREAM_NoError
= 0, /**< Command completed successfully, no error. */
483 ENDPOINT_RWSTREAM_EndpointStalled
= 1, /**< The endpoint was stalled during the stream
484 * transfer by the host or device.
486 ENDPOINT_RWSTREAM_DeviceDisconnected
= 2, /**< Device was disconnected from the host during
489 ENDPOINT_RWSTREAM_Timeout
= 3, /**< The host failed to accept or send the next packet
490 * within the software timeout period set by the
491 * \ref USB_STREAM_TIMEOUT_MS macro.
493 ENDPOINT_RWSTREAM_CallbackAborted
= 4, /**< Indicates that the stream's callback function
494 * aborted the transfer early.
498 /** Enum for the possible error return codes of the Endpoint_*_Control_Stream_* functions..
500 * \ingroup Group_EndpointStreamRW
502 enum Endpoint_ControlStream_RW_ErrorCodes_t
504 ENDPOINT_RWCSTREAM_NoError
= 0, /**< Command completed successfully, no error. */
505 ENDPOINT_RWCSTREAM_HostAborted
= 1, /**< The aborted the transfer prematurely. */
506 ENDPOINT_RWCSTREAM_DeviceDisconnected
= 2, /**< Device was disconnected from the host during
511 /* Inline Functions: */
512 /** Reads one byte from the currently selected endpoint's bank, for OUT direction endpoints.
514 * \ingroup Group_EndpointPrimitiveRW
516 * \return Next byte in the currently selected endpoint's FIFO buffer
518 static inline uint8_t Endpoint_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
519 static inline uint8_t Endpoint_Read_Byte(void)
521 #if defined(__AVR32__)
522 return __AVR32_EPREG_X(UEDAT0
);
523 #elif defined(__AVR__)
528 /** Writes one byte from the currently selected endpoint's bank, for IN direction endpoints.
530 * \ingroup Group_EndpointPrimitiveRW
532 * \param[in] Byte Next byte to write into the the currently selected endpoint's FIFO buffer
534 static inline void Endpoint_Write_Byte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
535 static inline void Endpoint_Write_Byte(const uint8_t Byte
)
537 #if defined(__AVR32__)
538 __AVR32_EPREG_X(UEDAT0
) = Byte
;
539 #elif defined(__AVR__)
544 /** Discards one byte from the currently selected endpoint's bank, for OUT direction endpoints.
546 * \ingroup Group_EndpointPrimitiveRW
548 static inline void Endpoint_Discard_Byte(void) ATTR_ALWAYS_INLINE
;
549 static inline void Endpoint_Discard_Byte(void)
553 #if defined(__AVR32__)
554 Dummy
= __AVR32_EPREG_X(UEDAT0
);
555 #elif defined(__AVR__)
560 /** Reads two bytes from the currently selected endpoint's bank in little endian format, for OUT
561 * direction endpoints.
563 * \ingroup Group_EndpointPrimitiveRW
565 * \return Next word in the currently selected endpoint's FIFO buffer
567 static inline uint16_t Endpoint_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
568 static inline uint16_t Endpoint_Read_Word_LE(void)
576 #if defined(__AVR32__)
577 Data
.Bytes
[0] = __AVR32_EPREG_X(UEDAT0
);
578 Data
.Bytes
[1] = __AVR32_EPREG_X(UEDAT0
);
579 #elif defined(__AVR__)
580 Data
.Bytes
[0] = UEDATX
;
581 Data
.Bytes
[1] = UEDATX
;
587 /** Reads two bytes from the currently selected endpoint's bank in big endian format, for OUT
588 * direction endpoints.
590 * \ingroup Group_EndpointPrimitiveRW
592 * \return Next word in the currently selected endpoint's FIFO buffer
594 static inline uint16_t Endpoint_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
595 static inline uint16_t Endpoint_Read_Word_BE(void)
603 #if defined(__AVR32__)
604 Data
.Bytes
[1] = __AVR32_EPREG_X(UEDAT0
);
605 Data
.Bytes
[0] = __AVR32_EPREG_X(UEDAT0
);
606 #elif defined(__AVR__)
607 Data
.Bytes
[1] = UEDATX
;
608 Data
.Bytes
[0] = UEDATX
;
614 /** Writes two bytes to the currently selected endpoint's bank in little endian format, for IN
615 * direction endpoints.
617 * \ingroup Group_EndpointPrimitiveRW
619 * \param[in] Word Next word to write to the currently selected endpoint's FIFO buffer
621 static inline void Endpoint_Write_Word_LE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
622 static inline void Endpoint_Write_Word_LE(const uint16_t Word
)
624 #if defined(__AVR32__)
625 __AVR32_EPREG_X(UEDAT0
) = (Word
& 0xFF);
626 __AVR32_EPREG_X(UEDAT0
) = (Word
>> 8);
627 #elif defined(__AVR__)
628 UEDATX
= (Word
& 0xFF);
629 UEDATX
= (Word
>> 8);
633 /** Writes two bytes to the currently selected endpoint's bank in big endian format, for IN
634 * direction endpoints.
636 * \ingroup Group_EndpointPrimitiveRW
638 * \param[in] Word Next word to write to the currently selected endpoint's FIFO buffer
640 static inline void Endpoint_Write_Word_BE(const uint16_t Word
) ATTR_ALWAYS_INLINE
;
641 static inline void Endpoint_Write_Word_BE(const uint16_t Word
)
643 #if defined(__AVR32__)
644 __AVR32_EPREG_X(UEDAT0
) = (Word
>> 8);
645 __AVR32_EPREG_X(UEDAT0
) = (Word
& 0xFF);
646 #elif defined(__AVR__)
647 UEDATX
= (Word
>> 8);
648 UEDATX
= (Word
& 0xFF);
652 /** Discards two bytes from the currently selected endpoint's bank, for OUT direction endpoints.
654 * \ingroup Group_EndpointPrimitiveRW
656 static inline void Endpoint_Discard_Word(void) ATTR_ALWAYS_INLINE
;
657 static inline void Endpoint_Discard_Word(void)
661 #if defined(__AVR32__)
662 Dummy
= __AVR32_EPREG_X(UEDAT0
);
663 Dummy
= __AVR32_EPREG_X(UEDAT0
);
664 #elif defined(__AVR__)
670 /** Reads four bytes from the currently selected endpoint's bank in little endian format, for OUT
671 * direction endpoints.
673 * \ingroup Group_EndpointPrimitiveRW
675 * \return Next double word in the currently selected endpoint's FIFO buffer
677 static inline uint32_t Endpoint_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
678 static inline uint32_t Endpoint_Read_DWord_LE(void)
686 #if defined(__AVR32__)
687 Data
.Bytes
[0] = __AVR32_EPREG_X(UEDAT0
);
688 Data
.Bytes
[1] = __AVR32_EPREG_X(UEDAT0
);
689 Data
.Bytes
[2] = __AVR32_EPREG_X(UEDAT0
);
690 Data
.Bytes
[3] = __AVR32_EPREG_X(UEDAT0
);
691 #elif defined(__AVR__)
692 Data
.Bytes
[0] = UEDATX
;
693 Data
.Bytes
[1] = UEDATX
;
694 Data
.Bytes
[2] = UEDATX
;
695 Data
.Bytes
[3] = UEDATX
;
701 /** Reads four bytes from the currently selected endpoint's bank in big endian format, for OUT
702 * direction endpoints.
704 * \ingroup Group_EndpointPrimitiveRW
706 * \return Next double word in the currently selected endpoint's FIFO buffer
708 static inline uint32_t Endpoint_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
;
709 static inline uint32_t Endpoint_Read_DWord_BE(void)
717 #if defined(__AVR32__)
718 Data
.Bytes
[3] = __AVR32_EPREG_X(UEDAT0
);
719 Data
.Bytes
[2] = __AVR32_EPREG_X(UEDAT0
);
720 Data
.Bytes
[1] = __AVR32_EPREG_X(UEDAT0
);
721 Data
.Bytes
[0] = __AVR32_EPREG_X(UEDAT0
);
722 #elif defined(__AVR__)
723 Data
.Bytes
[3] = UEDATX
;
724 Data
.Bytes
[2] = UEDATX
;
725 Data
.Bytes
[1] = UEDATX
;
726 Data
.Bytes
[0] = UEDATX
;
732 /** Writes four bytes to the currently selected endpoint's bank in little endian format, for IN
733 * direction endpoints.
735 * \ingroup Group_EndpointPrimitiveRW
737 * \param[in] DWord Next double word to write to the currently selected endpoint's FIFO buffer
739 static inline void Endpoint_Write_DWord_LE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
740 static inline void Endpoint_Write_DWord_LE(const uint32_t DWord
)
742 #if defined(__AVR32__)
743 __AVR32_EPREG_X(UEDAT0
) = (DWord
& 0xFF);
744 __AVR32_EPREG_X(UEDAT0
) = (DWord
>> 8);
745 __AVR32_EPREG_X(UEDAT0
) = (DWord
>> 16);
746 __AVR32_EPREG_X(UEDAT0
) = (DWord
>> 24);
747 #elif defined(__AVR__)
748 UEDATX
= (DWord
& 0xFF);
749 UEDATX
= (DWord
>> 8);
750 UEDATX
= (DWord
>> 16);
751 UEDATX
= (DWord
>> 24);
755 /** Writes four bytes to the currently selected endpoint's bank in big endian format, for IN
756 * direction endpoints.
758 * \ingroup Group_EndpointPrimitiveRW
760 * \param[in] DWord Next double word to write to the currently selected endpoint's FIFO buffer
762 static inline void Endpoint_Write_DWord_BE(const uint32_t DWord
) ATTR_ALWAYS_INLINE
;
763 static inline void Endpoint_Write_DWord_BE(const uint32_t DWord
)
765 #if defined(__AVR32__)
766 __AVR32_EPREG_X(UEDAT0
) = (DWord
>> 24);
767 __AVR32_EPREG_X(UEDAT0
) = (DWord
>> 16);
768 __AVR32_EPREG_X(UEDAT0
) = (DWord
>> 8);
769 __AVR32_EPREG_X(UEDAT0
) = (DWord
& 0xFF);
770 #elif defined(__AVR__)
771 UEDATX
= (DWord
>> 24);
772 UEDATX
= (DWord
>> 16);
773 UEDATX
= (DWord
>> 8);
774 UEDATX
= (DWord
& 0xFF);
778 /** Discards four bytes from the currently selected endpoint's bank, for OUT direction endpoints.
780 * \ingroup Group_EndpointPrimitiveRW
782 static inline void Endpoint_Discard_DWord(void) ATTR_ALWAYS_INLINE
;
783 static inline void Endpoint_Discard_DWord(void)
787 #if defined(__AVR32__)
788 Dummy
= __AVR32_EPREG_X(UEDAT0
);
789 Dummy
= __AVR32_EPREG_X(UEDAT0
);
790 Dummy
= __AVR32_EPREG_X(UEDAT0
);
791 Dummy
= __AVR32_EPREG_X(UEDAT0
);
792 #elif defined(__AVR__)
800 /* External Variables: */
801 /** Global indicating the maximum packet size of the default control endpoint located at address
802 * 0 in the device. This value is set to the value indicated in the device descriptor in the user
803 * project once the USB interface is initialized into device mode.
805 * If space is an issue, it is possible to fix this to a static value by defining the control
806 * endpoint size in the FIXED_CONTROL_ENDPOINT_SIZE token passed to the compiler in the makefile
807 * via the -D switch. When a fixed control endpoint size is used, the size is no longer dynamically
808 * read from the descriptors at runtime and instead fixed to the given value. When used, it is
809 * important that the descriptor control endpoint size value matches the size given as the
810 * FIXED_CONTROL_ENDPOINT_SIZE token - it is recommended that the FIXED_CONTROL_ENDPOINT_SIZE token
811 * be used in the descriptors to ensure this.
813 * \note This variable should be treated as read-only in the user application, and never manually
816 #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
817 extern uint8_t USB_ControlEndpointSize
;
819 #define USB_ControlEndpointSize FIXED_CONTROL_ENDPOINT_SIZE
822 /* Function Prototypes: */
823 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
824 #define __CALLBACK_PARAM , StreamCallbackPtr_t Callback
826 #define __CALLBACK_PARAM
829 /** Configures the specified endpoint number with the given endpoint type, direction, bank size
830 * and banking mode. Endpoints should be allocated in ascending order by their address in the
831 * device (i.e. endpoint 1 should be configured before endpoint 2 and so on) to prevent fragmentation
832 * of the USB FIFO memory.
834 * The endpoint type may be one of the EP_TYPE_* macros listed in LowLevel.h and the direction
835 * may be either \ref ENDPOINT_DIR_OUT or \ref ENDPOINT_DIR_IN.
837 * The bank size must indicate the maximum packet size that the endpoint can handle. Different
838 * endpoint numbers can handle different maximum packet sizes - refer to the chosen USB AVR's
839 * datasheet to determine the maximum bank size for each endpoint.
841 * The banking mode must be a ENDPOINT_BANK_* mask.
843 * \note The default control endpoint does not have to be manually configured, as it is automatically
844 * configured by the library internally.
846 * \note This routine will select the specified endpoint, and the endpoint will remain selected
847 * once the routine completes regardless of if the endpoint configuration succeeds.
849 * \return Boolean true if the configuration succeeded, false otherwise
851 bool Endpoint_ConfigureEndpoint(const uintN_t Number
, const uintN_t Type
, const uintN_t Direction
,
852 const uint16_t Size
, const uintN_t Banks
);
854 /** Spin-loops until the currently selected non-control endpoint is ready for the next packet of data
855 * to be read or written to it.
857 * \note This routine should not be called on CONTROL type endpoints.
859 * \ingroup Group_EndpointRW
861 * \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
863 uint8_t Endpoint_WaitUntilReady(void);
865 /** Completes the status stage of a control transfer on a CONTROL type endpoint automatically,
866 * with respect to the data direction. This is a convenience function which can be used to
867 * simplify user control request handling.
869 void Endpoint_ClearStatusStage(void);
871 /** Reads and discards the given number of bytes from the endpoint from the given buffer,
872 * discarding fully read packets from the host as needed. The last packet is not automatically
873 * discarded once the remaining bytes has been read; the user is responsible for manually
874 * discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between
875 * each USB packet, the given stream callback function is executed repeatedly until the next
876 * packet is ready, allowing for early aborts of stream transfers.
878 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
879 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
880 * disabled and this function has the Callback parameter omitted.
882 * \note This routine should not be used on CONTROL type endpoints.
884 * \ingroup Group_EndpointStreamRW
886 * \param[in] Length Number of bytes to send via the currently selected endpoint.
887 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
889 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
891 uint8_t Endpoint_Discard_Stream(uint16_t Length __CALLBACK_PARAM
);
893 /** Writes the given number of bytes to the endpoint from the given buffer in little endian,
894 * sending full packets to the host as needed. The last packet filled is not automatically sent;
895 * the user is responsible for manually sending the last written packet to the host via the
896 * \ref Endpoint_ClearIN() macro. Between each USB packet, the given stream callback function
897 * is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early
898 * aborts of stream transfers.
900 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
901 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
902 * disabled and this function has the Callback parameter omitted.
904 * \note This routine should not be used on CONTROL type endpoints.
906 * \ingroup Group_EndpointStreamRW
908 * \param[in] Buffer Pointer to the source data buffer to read from.
909 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
910 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
912 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
914 uint8_t Endpoint_Write_Stream_LE(const void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
916 /** EEPROM buffer source version of \ref Endpoint_Write_Stream_LE().
918 * \ingroup Group_EndpointStreamRW
920 * \param[in] Buffer Pointer to the source data buffer to read from.
921 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
922 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
924 * \note Not available on AVR32 UC3B targets.
926 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
928 uint8_t Endpoint_Write_EStream_LE(const void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
930 /** FLASH buffer source version of \ref Endpoint_Write_Stream_LE().
932 * \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
934 * \ingroup Group_EndpointStreamRW
936 * \param[in] Buffer Pointer to the source data buffer to read from.
937 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
938 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
940 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
942 uint8_t Endpoint_Write_PStream_LE(const void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
944 /** Writes the given number of bytes to the endpoint from the given buffer in big endian,
945 * sending full packets to the host as needed. The last packet filled is not automatically sent;
946 * the user is responsible for manually sending the last written packet to the host via the
947 * \ref Endpoint_ClearIN() macro. Between each USB packet, the given stream callback function
948 * is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early
949 * aborts of stream transfers.
951 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
952 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
953 * disabled and this function has the Callback parameter omitted.
955 * \note This routine should not be used on CONTROL type endpoints.
957 * \ingroup Group_EndpointStreamRW
959 * \param[in] Buffer Pointer to the source data buffer to read from.
960 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
961 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
963 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
965 uint8_t Endpoint_Write_Stream_BE(const void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
967 /** EEPROM buffer source version of \ref Endpoint_Write_Stream_BE().
969 * \ingroup Group_EndpointStreamRW
971 * \param[in] Buffer Pointer to the source data buffer to read from.
972 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
973 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
975 * \note Not available on AVR32 UC3B targets.
977 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
979 uint8_t Endpoint_Write_EStream_BE(const void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
981 /** FLASH buffer source version of \ref Endpoint_Write_Stream_BE().
983 * \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
985 * \ingroup Group_EndpointStreamRW
987 * \param[in] Buffer Pointer to the source data buffer to read from.
988 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
989 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
991 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
993 uint8_t Endpoint_Write_PStream_BE(const void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
995 /** Reads the given number of bytes from the endpoint from the given buffer in little endian,
996 * discarding fully read packets from the host as needed. The last packet is not automatically
997 * discarded once the remaining bytes has been read; the user is responsible for manually
998 * discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between
999 * each USB packet, the given stream callback function is executed repeatedly until the endpoint
1000 * is ready to accept the next packet, allowing for early aborts of stream transfers.
1002 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
1003 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
1004 * disabled and this function has the Callback parameter omitted.
1006 * \note This routine should not be used on CONTROL type endpoints.
1008 * \ingroup Group_EndpointStreamRW
1010 * \param[out] Buffer Pointer to the destination data buffer to write to.
1011 * \param[in] Length Number of bytes to send via the currently selected endpoint.
1012 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1014 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
1016 uint8_t Endpoint_Read_Stream_LE(void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1018 /** EEPROM buffer source version of \ref Endpoint_Read_Stream_LE().
1020 * \ingroup Group_EndpointStreamRW
1022 * \param[out] Buffer Pointer to the destination data buffer to write to, located in EEPROM memory space.
1023 * \param[in] Length Number of bytes to send via the currently selected endpoint.
1024 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1026 * \note Not available on AVR32 UC3B targets.
1028 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
1030 uint8_t Endpoint_Read_EStream_LE(void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1032 /** Reads the given number of bytes from the endpoint from the given buffer in big endian,
1033 * discarding fully read packets from the host as needed. The last packet is not automatically
1034 * discarded once the remaining bytes has been read; the user is responsible for manually
1035 * discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between
1036 * each USB packet, the given stream callback function is executed repeatedly until the endpoint
1037 * is ready to accept the next packet, allowing for early aborts of stream transfers.
1039 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
1040 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
1041 * disabled and this function has the Callback parameter omitted.
1043 * \note This routine should not be used on CONTROL type endpoints.
1045 * \ingroup Group_EndpointStreamRW
1047 * \param[out] Buffer Pointer to the destination data buffer to write to.
1048 * \param[in] Length Number of bytes to send via the currently selected endpoint.
1049 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1051 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
1053 uint8_t Endpoint_Read_Stream_BE(void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1055 /** EEPROM buffer source version of \ref Endpoint_Read_Stream_BE().
1057 * \ingroup Group_EndpointStreamRW
1059 * \param[out] Buffer Pointer to the destination data buffer to write to, located in EEPROM memory space.
1060 * \param[in] Length Number of bytes to send via the currently selected endpoint.
1061 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1063 * \note Not available on AVR32 UC3B targets.
1065 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
1067 uint8_t Endpoint_Read_EStream_BE(void* Buffer
, uint16_t Length __CALLBACK_PARAM
) ATTR_NON_NULL_PTR_ARG(1);
1069 /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in little endian,
1070 * sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared
1071 * in both failure and success states; the user is responsible for manually clearing the setup OUT to
1072 * finalize the transfer via the \ref Endpoint_ClearOUT() macro.
1074 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1075 * to clear the status stage when using this routine in a control transaction.
1077 * \note This routine should only be used on CONTROL type endpoints.
1079 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1080 * together; i.e. the entire stream data must be read or written at the one time.
1082 * \ingroup Group_EndpointStreamRW
1084 * \param[in] Buffer Pointer to the source data buffer to read from.
1085 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
1087 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1089 uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1091 /** EEPROM buffer source version of Endpoint_Write_Control_Stream_LE.
1093 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1094 * to clear the status stage when using this routine in a control transaction.
1096 * \note This routine should only be used on CONTROL type endpoints.
1098 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1099 * together; i.e. the entire stream data must be read or written at the one time.
1101 * \ingroup Group_EndpointStreamRW
1103 * \param[in] Buffer Pointer to the source data buffer to read from.
1104 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
1106 * \note Not available on AVR32 UC3B targets.
1108 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1110 uint8_t Endpoint_Write_Control_EStream_LE(const void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1112 /** FLASH buffer source version of \ref Endpoint_Write_Control_Stream_LE().
1114 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1115 * to clear the status stage when using this routine in a control transaction.
1117 * \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
1119 * \note This routine should only be used on CONTROL type endpoints.
1121 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1122 * together; i.e. the entire stream data must be read or written at the one time.
1124 * \ingroup Group_EndpointStreamRW
1126 * \param[in] Buffer Pointer to the source data buffer to read from.
1127 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
1129 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1131 uint8_t Endpoint_Write_Control_PStream_LE(const void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1133 /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in big endian,
1134 * sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared
1135 * in both failure and success states; the user is responsible for manually clearing the setup OUT to
1136 * finalize the transfer via the \ref Endpoint_ClearOUT() macro.
1138 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1139 * to clear the status stage when using this routine in a control transaction.
1141 * \note This routine should only be used on CONTROL type endpoints.
1143 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1144 * together; i.e. the entire stream data must be read or written at the one time.
1146 * \ingroup Group_EndpointStreamRW
1148 * \param[in] Buffer Pointer to the source data buffer to read from.
1149 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
1151 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1153 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1155 /** EEPROM buffer source version of \ref Endpoint_Write_Control_Stream_BE().
1157 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1158 * to clear the status stage when using this routine in a control transaction.
1160 * \note This routine should only be used on CONTROL type endpoints.
1162 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1163 * together; i.e. the entire stream data must be read or written at the one time.
1165 * \ingroup Group_EndpointStreamRW
1167 * \param[in] Buffer Pointer to the source data buffer to read from.
1168 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
1170 * \note Not available on AVR32 UC3B targets.
1172 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1174 uint8_t Endpoint_Write_Control_EStream_BE(const void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1176 /** FLASH buffer source version of \ref Endpoint_Write_Control_Stream_BE().
1178 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1179 * to clear the status stage when using this routine in a control transaction.
1181 * \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
1183 * \note This routine should only be used on CONTROL type endpoints.
1185 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1186 * together; i.e. the entire stream data must be read or written at the one time.
1188 * \ingroup Group_EndpointStreamRW
1190 * \param[in] Buffer Pointer to the source data buffer to read from.
1191 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
1193 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1195 uint8_t Endpoint_Write_Control_PStream_BE(const void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1197 /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in little endian,
1198 * discarding fully read packets from the host as needed. The device IN acknowledgement is not
1199 * automatically sent after success or failure states; the user is responsible for manually sending the
1200 * setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro.
1202 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1203 * to clear the status stage when using this routine in a control transaction.
1205 * \note This routine should only be used on CONTROL type endpoints.
1207 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1208 * together; i.e. the entire stream data must be read or written at the one time.
1210 * \ingroup Group_EndpointStreamRW
1212 * \param[out] Buffer Pointer to the destination data buffer to write to.
1213 * \param[in] Length Number of bytes to send via the currently selected endpoint.
1215 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1217 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1219 /** EEPROM buffer source version of \ref Endpoint_Read_Control_Stream_LE().
1221 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1222 * to clear the status stage when using this routine in a control transaction.
1224 * \note This routine should only be used on CONTROL type endpoints.
1226 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1227 * together; i.e. the entire stream data must be read or written at the one time.
1229 * \ingroup Group_EndpointStreamRW
1231 * \param[out] Buffer Pointer to the destination data buffer to write to.
1232 * \param[in] Length Number of bytes to send via the currently selected endpoint.
1234 * \note Not available on AVR32 UC3B targets.
1236 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1238 uint8_t Endpoint_Read_Control_EStream_LE(void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1240 /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in big endian,
1241 * discarding fully read packets from the host as needed. The device IN acknowledgement is not
1242 * automatically sent after success or failure states; the user is responsible for manually sending the
1243 * setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro.
1245 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1246 * to clear the status stage when using this routine in a control transaction.
1248 * \note This routine should only be used on CONTROL type endpoints.
1250 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1251 * together; i.e. the entire stream data must be read or written at the one time.
1253 * \ingroup Group_EndpointStreamRW
1255 * \param[out] Buffer Pointer to the destination data buffer to write to.
1256 * \param[in] Length Number of bytes to send via the currently selected endpoint.
1258 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1260 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1262 /** EEPROM buffer source version of \ref Endpoint_Read_Control_Stream_BE().
1264 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1265 * to clear the status stage when using this routine in a control transaction.
1267 * \note This routine should only be used on CONTROL type endpoints.
1269 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1270 * together; i.e. the entire stream data must be read or written at the one time.
1272 * \ingroup Group_EndpointStreamRW
1274 * \param[out] Buffer Pointer to the destination data buffer to write to.
1275 * \param[in] Length Number of bytes to send via the currently selected endpoint.
1277 * \note Not available on AVR32 UC3B targets.
1279 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1281 uint8_t Endpoint_Read_Control_EStream_BE(void* Buffer
, uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1);
1283 /* Private Interface - For use in library only: */
1284 #if !defined(__DOXYGEN__)
1286 #if defined(__AVR32__)
1287 #define Endpoint_AllocateMemory() MACROS{ __AVR32_EPREG_X(UECFG10) |= AVR32_USBB_UECFG0_ALLOC_MASK; }MACROE
1288 #define Endpoint_DeallocateMemory() MACROS{ __AVR32_EPREG_X(UECFG10) &= ~AVR32_USBB_UECFG0_ALLOC_MASK; }MACROE
1290 #define Endpoint_ConfigureEndpoint(Number, Type, Direction, Size, Banks) \
1291 Endpoint_ConfigureEndpoint_Prv((Number), \
1292 (((Type) << AVR32_USBB_UECFG0_EPTYPE) | (Direction)), \
1293 (AVR32_USBB_UECFG0_ALLOC_MASK | (Banks) | \
1294 (__builtin_constant_p(Size) ? \
1295 Endpoint_BytesToEPSizeMask(Size) : \
1296 Endpoint_BytesToEPSizeMaskDynamic(Size))))
1297 #elif defined(__AVR__)
1298 #define Endpoint_AllocateMemory() MACROS{ UECFG1X |= (1 << ALLOC); }MACROE
1299 #define Endpoint_DeallocateMemory() MACROS{ UECFG1X &= ~(1 << ALLOC); }MACROE
1301 #define Endpoint_ConfigureEndpoint(Number, Type, Direction, Size, Banks) \
1302 Endpoint_ConfigureEndpoint_Prv((Number), \
1303 (((Type) << EPTYPE0) | (Direction)), \
1304 ((1 << ALLOC) | (Banks) | \
1305 (__builtin_constant_p(Size) ? \
1306 Endpoint_BytesToEPSizeMask(Size) : \
1307 Endpoint_BytesToEPSizeMaskDynamic(Size))))
1310 #define _ENDPOINT_GET_MAXSIZE(n) _ENDPOINT_GET_MAXSIZE2(ENDPOINT_DETAILS_EP ## n)
1311 #define _ENDPOINT_GET_MAXSIZE2(details) _ENDPOINT_GET_MAXSIZE3(details)
1312 #define _ENDPOINT_GET_MAXSIZE3(maxsize, db) maxsize
1314 #define _ENDPOINT_GET_DOUBLEBANK(n) _ENDPOINT_GET_DOUBLEBANK2(ENDPOINT_DETAILS_EP ## n)
1315 #define _ENDPOINT_GET_DOUBLEBANK2(details) _ENDPOINT_GET_DOUBLEBANK3(details)
1316 #define _ENDPOINT_GET_DOUBLEBANK3(maxsize, db) db
1318 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
1319 #define ENDPOINT_DETAILS_EP0 64, true
1320 #define ENDPOINT_DETAILS_EP1 256, true
1321 #define ENDPOINT_DETAILS_EP2 64, true
1322 #define ENDPOINT_DETAILS_EP3 64, true
1323 #define ENDPOINT_DETAILS_EP4 64, true
1324 #define ENDPOINT_DETAILS_EP5 64, true
1325 #define ENDPOINT_DETAILS_EP6 64, true
1326 #elif defined(USB_SERIES_UC3B_AVR)
1327 #define ENDPOINT_DETAILS_EP0 64, false
1328 #define ENDPOINT_DETAILS_EP1 64, true
1329 #define ENDPOINT_DETAILS_EP2 64, true
1330 #define ENDPOINT_DETAILS_EP3 64, true
1331 #define ENDPOINT_DETAILS_EP4 64, true
1332 #define ENDPOINT_DETAILS_EP5 256, true
1333 #define ENDPOINT_DETAILS_EP6 256, true
1334 #elif defined(USB_SERIES_2_AVR)
1335 #define ENDPOINT_DETAILS_EP0 64, true
1336 #define ENDPOINT_DETAILS_EP1 64, false
1337 #define ENDPOINT_DETAILS_EP2 64, false
1338 #define ENDPOINT_DETAILS_EP3 64, true
1339 #define ENDPOINT_DETAILS_EP4 64, true
1342 /* Function Prototypes: */
1343 void Endpoint_ClearEndpoints(void);
1344 uintN_t
Endpoint_BytesToEPSizeMaskDynamic(const uint16_t Size
);
1345 bool Endpoint_ConfigureEndpoint_Prv(const uintN_t Number
, const uintN_t UECFG0XData
, const uintN_t UECFG1XData
);
1347 /* Inline Functions: */
1348 static inline uintN_t
Endpoint_BytesToEPSizeMask(const uint16_t Bytes
) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE
;
1349 static inline uintN_t
Endpoint_BytesToEPSizeMask(const uint16_t Bytes
)
1351 #if defined(__AVR32__)
1352 uint8_t MaskVal
= 0;
1353 uint16_t CheckBytes
= 8;
1355 while (CheckBytes
< Bytes
)
1361 return (MaskVal
<< AVR32_USBB_EPSIZE
);
1363 uint8_t MaskVal
= 0;
1364 uint16_t CheckBytes
= 8;
1366 while (CheckBytes
< Bytes
)
1372 return (MaskVal
<< EPSIZE0
);
1378 /* Disable C linkage for C++ Compilers: */
1379 #if defined(__cplusplus)