6d98b78f08e7cc71e0d7a98ecee7f46c6e4d5487
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Endpoint.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \ingroup Group_USB
32 * @defgroup Group_EndpointManagement Endpoint Management
33 *
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.
37 *
38 * @{
39 */
40
41 /** @defgroup Group_EndpointRW Endpoint Data Reading and Writing
42 *
43 * Functions, macros, variables, enums and types related to data reading and writing from and to endpoints.
44 */
45
46 /** \ingroup Group_EndpointRW
47 * @defgroup Group_EndpointPrimitiveRW Read/Write of Primitive Data Types
48 *
49 * Functions, macros, variables, enums and types related to data reading and writing of primitive data types
50 * from and to endpoints.
51 */
52
53 /** \ingroup Group_EndpointRW
54 * @defgroup Group_EndpointStreamRW Read/Write of Multi-Byte Streams
55 *
56 * Functions, macros, variables, enums and types related to data reading and writing of data streams from
57 * and to endpoints.
58 */
59
60 /** @defgroup Group_EndpointPacketManagement Endpoint Packet Management
61 *
62 * Functions, macros, variables, enums and types related to packet management of endpoints.
63 */
64
65 #ifndef __ENDPOINT_H__
66 #define __ENDPOINT_H__
67
68 /* Includes: */
69 #include <avr/io.h>
70 #include <avr/pgmspace.h>
71 #include <avr/eeprom.h>
72 #include <stdbool.h>
73
74 #include "../../../Common/Common.h"
75 #include "../HighLevel/USBTask.h"
76
77 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
78 #include "../HighLevel/StreamCallbacks.h"
79 #endif
80
81 /* Enable C linkage for C++ Compilers: */
82 #if defined(__cplusplus)
83 extern "C" {
84 #endif
85
86 /* Preprocessor Checks: */
87 #if !defined(__INCLUDE_FROM_USB_DRIVER)
88 #error Do not include this file directly. Include LUFA/Drivers/USB.h instead.
89 #endif
90
91 /* Public Interface - May be used in end-application: */
92 /* Macros: */
93 /** Endpoint data direction mask for \ref Endpoint_ConfigureEndpoint(). This indicates that the endpoint
94 * should be initialized in the OUT direction - i.e. data flows from host to device.
95 */
96 #define ENDPOINT_DIR_OUT (0 << EPDIR)
97
98 /** Endpoint data direction mask for \ref Endpoint_ConfigureEndpoint(). This indicates that the endpoint
99 * should be initialized in the IN direction - i.e. data flows from device to host.
100 */
101 #define ENDPOINT_DIR_IN (1 << EPDIR)
102
103 /** Mask for the bank mode selection for the \ref Endpoint_ConfigureEndpoint() macro. This indicates
104 * that the endpoint should have one single bank, which requires less USB FIFO memory but results
105 * in slower transfers as only one USB device (the AVR or the host) can access the endpoint's
106 * bank at the one time.
107 */
108 #define ENDPOINT_BANK_SINGLE (0 << EPBK0)
109
110 /** Mask for the bank mode selection for the \ref Endpoint_ConfigureEndpoint() macro. This indicates
111 * that the endpoint should have two banks, which requires more USB FIFO memory but results
112 * in faster transfers as one USB device (the AVR or the host) can access one bank while the other
113 * accesses the second bank.
114 */
115 #define ENDPOINT_BANK_DOUBLE (1 << EPBK0)
116
117 /** Endpoint address for the default control endpoint, which always resides in address 0. This is
118 * defined for convenience to give more readable code when used with the endpoint macros.
119 */
120 #define ENDPOINT_CONTROLEP 0
121
122 #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
123 /** Default size of the default control endpoint's bank, until altered by the Endpoint0Size value
124 * in the device descriptor. Not available if the FIXED_CONTROL_ENDPOINT_SIZE token is defined.
125 */
126 #define ENDPOINT_CONTROLEP_DEFAULT_SIZE 8
127 #endif
128
129 /** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's
130 * numerical address in the device.
131 */
132 #define ENDPOINT_EPNUM_MASK 0x07
133
134 /** Endpoint direction mask, for masking against endpoint addresses to retrieve the endpoint's
135 * direction for comparing with the ENDPOINT_DESCRIPTOR_DIR_* masks.
136 */
137 #define ENDPOINT_EPDIR_MASK 0x80
138
139 /** Endpoint bank size mask, for masking against endpoint addresses to retrieve the endpoint's
140 * bank size in the device.
141 */
142 #define ENDPOINT_EPSIZE_MASK 0x7FF
143
144 /** Maximum size in bytes of a given endpoint.
145 *
146 * \param[in] n Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1)
147 */
148 #define ENDPOINT_MAX_SIZE(n) _ENDPOINT_GET_MAXSIZE(n)
149
150 /** Indicates if the given endpoint supports double banking.
151 *
152 * \param[in] n Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1)
153 */
154 #define ENDPOINT_DOUBLEBANK_SUPPORTED(n) _ENDPOINT_GET_DOUBLEBANK(n)
155
156 #if !defined(CONTROL_ONLY_DEVICE)
157 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR) || defined(__DOXYGEN__)
158 /** Total number of endpoints (including the default control endpoint at address 0) which may
159 * be used in the device. Different USB AVR models support different amounts of endpoints,
160 * this value reflects the maximum number of endpoints for the currently selected AVR model.
161 */
162 #define ENDPOINT_TOTAL_ENDPOINTS 7
163 #else
164 #define ENDPOINT_TOTAL_ENDPOINTS 5
165 #endif
166 #else
167 #define ENDPOINT_TOTAL_ENDPOINTS 1
168 #endif
169
170 /* Pseudo-Function Macros: */
171 #if defined(__DOXYGEN__)
172 /** Indicates the number of bytes currently stored in the current endpoint's selected bank.
173 *
174 * \note The return width of this function may differ, depending on the maximum endpoint bank size
175 * of the selected AVR model.
176 *
177 * \ingroup Group_EndpointRW
178 *
179 * \return Total number of bytes in the currently selected Endpoint's FIFO buffer
180 */
181 static inline uint16_t Endpoint_BytesInEndpoint(void);
182
183 /** Get the endpoint address of the currently selected endpoint. This is typically used to save
184 * the currently selected endpoint number so that it can be restored after another endpoint has
185 * been manipulated.
186 *
187 * \return Index of the currently selected endpoint
188 */
189 static inline uint8_t Endpoint_GetCurrentEndpoint(void);
190
191 /** Selects the given endpoint number. If the address from the device descriptors is used, the
192 * value should be masked with the \ref ENDPOINT_EPNUM_MASK constant to extract only the endpoint
193 * number (and discarding the endpoint direction bit).
194 *
195 * Any endpoint operations which do not require the endpoint number to be indicated will operate on
196 * the currently selected endpoint.
197 *
198 * \param[in] EndpointNumber Endpoint number to select
199 */
200 static inline void Endpoint_SelectEndpoint(uint8_t EndpointNumber);
201
202 /** Resets the endpoint bank FIFO. This clears all the endpoint banks and resets the USB controller's
203 * In and Out pointers to the bank's contents.
204 *
205 * \param[in] EndpointNumber Endpoint number whose FIFO buffers are to be reset
206 */
207 static inline void Endpoint_ResetFIFO(uint8_t EndpointNumber);
208
209 /** Enables the currently selected endpoint so that data can be sent and received through it to
210 * and from a host.
211 *
212 * \note Endpoints must first be configured properly via \ref Endpoint_ConfigureEndpoint().
213 */
214 static inline void Endpoint_EnableEndpoint(void);
215
216 /** Disables the currently selected endpoint so that data cannot be sent and received through it
217 * to and from a host.
218 */
219 static inline void Endpoint_DisableEndpoint(void);
220
221 /** Determines if the currently selected endpoint is enabled, but not necessarily configured.
222 *
223 * \return Boolean True if the currently selected endpoint is enabled, false otherwise
224 */
225 static inline bool Endpoint_IsEnabled(void);
226
227 /** Determines if the currently selected endpoint may be read from (if data is waiting in the endpoint
228 * bank and the endpoint is an OUT direction, or if the bank is not yet full if the endpoint is an IN
229 * direction). This function will return false if an error has occurred in the endpoint, if the endpoint
230 * is an OUT direction and no packet (or an empty packet) has been received, or if the endpoint is an IN
231 * direction and the endpoint bank is full.
232 *
233 * \ingroup Group_EndpointPacketManagement
234 *
235 * \return Boolean true if the currently selected endpoint may be read from or written to, depending on its direction
236 */
237 static inline bool Endpoint_IsReadWriteAllowed(void);
238
239 /** Determines if the currently selected endpoint is configured.
240 *
241 * \return Boolean true if the currently selected endpoint has been configured, false otherwise
242 */
243 static inline bool Endpoint_IsConfigured(void);
244
245 /** Returns a mask indicating which INTERRUPT type endpoints have interrupted - i.e. their
246 * interrupt duration has elapsed. Which endpoints have interrupted can be determined by
247 * masking the return value against (1 << {Endpoint Number}).
248 *
249 * \return Mask whose bits indicate which endpoints have interrupted
250 */
251 static inline uint8_t Endpoint_GetEndpointInterrupts(void);
252
253 /** Determines if the specified endpoint number has interrupted (valid only for INTERRUPT type
254 * endpoints).
255 *
256 * \param[in] EndpointNumber Index of the endpoint whose interrupt flag should be tested
257 *
258 * \return Boolean true if the specified endpoint has interrupted, false otherwise
259 */
260 static inline bool Endpoint_HasEndpointInterrupted(uint8_t EndpointNumber);
261
262 /** Determines if the selected IN endpoint is ready for a new packet.
263 *
264 * \ingroup Group_EndpointPacketManagement
265 *
266 * \return Boolean true if the current endpoint is ready for an IN packet, false otherwise.
267 */
268 static inline bool Endpoint_IsINReady(void);
269
270 /** Determines if the selected OUT endpoint has received new packet.
271 *
272 * \ingroup Group_EndpointPacketManagement
273 *
274 * \return Boolean true if current endpoint is has received an OUT packet, false otherwise.
275 */
276 static inline bool Endpoint_IsOUTReceived(void);
277
278 /** Determines if the current CONTROL type endpoint has received a SETUP packet.
279 *
280 * \ingroup Group_EndpointPacketManagement
281 *
282 * \return Boolean true if the selected endpoint has received a SETUP packet, false otherwise.
283 */
284 static inline bool Endpoint_IsSETUPReceived(void);
285
286 /** Clears a received SETUP packet on the currently selected CONTROL type endpoint, freeing up the
287 * endpoint for the next packet.
288 *
289 * \ingroup Group_EndpointPacketManagement
290 *
291 * \note This is not applicable for non CONTROL type endpoints.
292 */
293 static inline void Endpoint_ClearSETUP(void);
294
295 /** Sends an IN packet to the host on the currently selected endpoint, freeing up the endpoint for the
296 * next packet and switching to the alternative endpoint bank if double banked.
297 *
298 * \ingroup Group_EndpointPacketManagement
299 */
300 static inline void Endpoint_ClearIN(void);
301
302 /** Acknowledges an OUT packet to the host on the currently selected endpoint, freeing up the endpoint
303 * for the next packet and switching to the alternative endpoint bank if double banked.
304 *
305 * \ingroup Group_EndpointPacketManagement
306 */
307 static inline void Endpoint_ClearOUT(void);
308
309 /** Stalls the current endpoint, indicating to the host that a logical problem occurred with the
310 * indicated endpoint and that the current transfer sequence should be aborted. This provides a
311 * way for devices to indicate invalid commands to the host so that the current transfer can be
312 * aborted and the host can begin its own recovery sequence.
313 *
314 * The currently selected endpoint remains stalled until either the \ref Endpoint_ClearStall() macro
315 * is called, or the host issues a CLEAR FEATURE request to the device for the currently selected
316 * endpoint.
317 *
318 * \ingroup Group_EndpointPacketManagement
319 */
320 static inline void Endpoint_StallTransaction(void);
321
322 /** Clears the STALL condition on the currently selected endpoint.
323 *
324 * \ingroup Group_EndpointPacketManagement
325 */
326 static inline void Endpoint_ClearStall(void);
327
328 /** Determines if the currently selected endpoint is stalled, false otherwise.
329 *
330 * \ingroup Group_EndpointPacketManagement
331 *
332 * \return Boolean true if the currently selected endpoint is stalled, false otherwise
333 */
334 static inline bool Endpoint_IsStalled(void);
335
336 /** Resets the data toggle of the currently selected endpoint. */
337 static inline void Endpoint_ResetDataToggle(void);
338
339 /** Determines the currently selected endpoint's direction.
340 *
341 * \return The currently selected endpoint's direction, as a ENDPOINT_DIR_* mask.
342 */
343 static inline uint8_t Endpoint_GetEndpointDirection(void);
344
345 /** Sets the direction of the currently selected endpoint.
346 *
347 * \param[in] DirectionMask New endpoint direction, as a ENDPOINT_DIR_* mask.
348 */
349 static inline void Endpoint_SetEndpointDirection(uint8_t DirectionMask);
350 #else
351 #if defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
352 #define Endpoint_BytesInEndpoint() UEBCX
353 #elif defined(USB_SERIES_4_AVR)
354 #define Endpoint_BytesInEndpoint() (((uint16_t)UEBCHX << 8) | UEBCLX)
355 #elif defined(USB_SERIES_2_AVR)
356 #define Endpoint_BytesInEndpoint() UEBCLX
357 #endif
358
359 #if !defined(CONTROL_ONLY_DEVICE)
360 #define Endpoint_GetCurrentEndpoint() (UENUM & ENDPOINT_EPNUM_MASK)
361 #else
362 #define Endpoint_GetCurrentEndpoint() ENDPOINT_CONTROLEP
363 #endif
364
365 #if !defined(CONTROL_ONLY_DEVICE)
366 #define Endpoint_SelectEndpoint(epnum) MACROS{ UENUM = (epnum); }MACROE
367 #else
368 #define Endpoint_SelectEndpoint(epnum) (void)(epnum)
369 #endif
370
371 #define Endpoint_ResetFIFO(epnum) MACROS{ UERST = (1 << (epnum)); UERST = 0; }MACROE
372
373 #define Endpoint_EnableEndpoint() MACROS{ UECONX |= (1 << EPEN); }MACROE
374
375 #define Endpoint_DisableEndpoint() MACROS{ UECONX &= ~(1 << EPEN); }MACROE
376
377 #define Endpoint_IsEnabled() ((UECONX & (1 << EPEN)) ? true : false)
378
379 #if !defined(CONTROL_ONLY_DEVICE)
380 #define Endpoint_IsReadWriteAllowed() ((UEINTX & (1 << RWAL)) ? true : false)
381 #endif
382
383 #define Endpoint_IsConfigured() ((UESTA0X & (1 << CFGOK)) ? true : false)
384
385 #define Endpoint_GetEndpointInterrupts() UEINT
386
387 #define Endpoint_HasEndpointInterrupted(n) ((UEINT & (1 << (n))) ? true : false)
388
389 #define Endpoint_IsINReady() ((UEINTX & (1 << TXINI)) ? true : false)
390
391 #define Endpoint_IsOUTReceived() ((UEINTX & (1 << RXOUTI)) ? true : false)
392
393 #define Endpoint_IsSETUPReceived() ((UEINTX & (1 << RXSTPI)) ? true : false)
394
395 #define Endpoint_ClearSETUP() MACROS{ UEINTX &= ~(1 << RXSTPI); }MACROE
396
397 #if !defined(CONTROL_ONLY_DEVICE)
398 #define Endpoint_ClearIN() MACROS{ uint8_t Temp = UEINTX; UEINTX = (Temp & ~(1 << TXINI)); \
399 UEINTX = (Temp & ~(1 << FIFOCON)); }MACROE
400 #else
401 #define Endpoint_ClearIN() MACROS{ UEINTX &= ~(1 << TXINI); }MACROE
402 #endif
403
404 #if !defined(CONTROL_ONLY_DEVICE)
405 #define Endpoint_ClearOUT() MACROS{ uint8_t Temp = UEINTX; UEINTX = (Temp & ~(1 << RXOUTI)); \
406 UEINTX = (Temp & ~(1 << FIFOCON)); }MACROE
407 #else
408 #define Endpoint_ClearOUT() MACROS{ UEINTX &= ~(1 << RXOUTI); }MACROE
409 #endif
410
411 #define Endpoint_StallTransaction() MACROS{ UECONX |= (1 << STALLRQ); }MACROE
412
413 #define Endpoint_ClearStall() MACROS{ UECONX |= (1 << STALLRQC); }MACROE
414
415 #define Endpoint_IsStalled() ((UECONX & (1 << STALLRQ)) ? true : false)
416
417 #define Endpoint_ResetDataToggle() MACROS{ UECONX |= (1 << RSTDT); }MACROE
418
419 #define Endpoint_GetEndpointDirection() (UECFG0X & ENDPOINT_DIR_IN)
420
421 #define Endpoint_SetEndpointDirection(dir) MACROS{ UECFG0X = ((UECFG0X & ~ENDPOINT_DIR_IN) | (dir)); }MACROE
422 #endif
423
424 /* Enums: */
425 /** Enum for the possible error return codes of the \ref Endpoint_WaitUntilReady() function.
426 *
427 * \ingroup Group_EndpointRW
428 */
429 enum Endpoint_WaitUntilReady_ErrorCodes_t
430 {
431 ENDPOINT_READYWAIT_NoError = 0, /**< Endpoint is ready for next packet, no error. */
432 ENDPOINT_READYWAIT_EndpointStalled = 1, /**< The endpoint was stalled during the stream
433 * transfer by the host or device.
434 */
435 ENDPOINT_READYWAIT_DeviceDisconnected = 2, /**< Device was disconnected from the host while
436 * waiting for the endpoint to become ready.
437 */
438 ENDPOINT_READYWAIT_Timeout = 3, /**< The host failed to accept or send the next packet
439 * within the software timeout period set by the
440 * \ref USB_STREAM_TIMEOUT_MS macro.
441 */
442 };
443
444 /** Enum for the possible error return codes of the Endpoint_*_Stream_* functions.
445 *
446 * \ingroup Group_EndpointStreamRW
447 */
448 enum Endpoint_Stream_RW_ErrorCodes_t
449 {
450 ENDPOINT_RWSTREAM_NoError = 0, /**< Command completed successfully, no error. */
451 ENDPOINT_RWSTREAM_EndpointStalled = 1, /**< The endpoint was stalled during the stream
452 * transfer by the host or device.
453 */
454 ENDPOINT_RWSTREAM_DeviceDisconnected = 2, /**< Device was disconnected from the host during
455 * the transfer.
456 */
457 ENDPOINT_RWSTREAM_Timeout = 3, /**< The host failed to accept or send the next packet
458 * within the software timeout period set by the
459 * \ref USB_STREAM_TIMEOUT_MS macro.
460 */
461 ENDPOINT_RWSTREAM_CallbackAborted = 4, /**< Indicates that the stream's callback function
462 * aborted the transfer early.
463 */
464 };
465
466 /** Enum for the possible error return codes of the Endpoint_*_Control_Stream_* functions..
467 *
468 * \ingroup Group_EndpointStreamRW
469 */
470 enum Endpoint_ControlStream_RW_ErrorCodes_t
471 {
472 ENDPOINT_RWCSTREAM_NoError = 0, /**< Command completed successfully, no error. */
473 ENDPOINT_RWCSTREAM_HostAborted = 1, /**< The aborted the transfer prematurely. */
474 ENDPOINT_RWCSTREAM_DeviceDisconnected = 2, /**< Device was disconnected from the host during
475 * the transfer.
476 */
477 };
478
479 /* Inline Functions: */
480 /** Reads one byte from the currently selected endpoint's bank, for OUT direction endpoints.
481 *
482 * \ingroup Group_EndpointPrimitiveRW
483 *
484 * \return Next byte in the currently selected endpoint's FIFO buffer
485 */
486 static inline uint8_t Endpoint_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
487 static inline uint8_t Endpoint_Read_Byte(void)
488 {
489 return UEDATX;
490 }
491
492 /** Writes one byte from the currently selected endpoint's bank, for IN direction endpoints.
493 *
494 * \ingroup Group_EndpointPrimitiveRW
495 *
496 * \param[in] Byte Next byte to write into the the currently selected endpoint's FIFO buffer
497 */
498 static inline void Endpoint_Write_Byte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
499 static inline void Endpoint_Write_Byte(const uint8_t Byte)
500 {
501 UEDATX = Byte;
502 }
503
504 /** Discards one byte from the currently selected endpoint's bank, for OUT direction endpoints.
505 *
506 * \ingroup Group_EndpointPrimitiveRW
507 */
508 static inline void Endpoint_Discard_Byte(void) ATTR_ALWAYS_INLINE;
509 static inline void Endpoint_Discard_Byte(void)
510 {
511 uint8_t Dummy;
512
513 Dummy = UEDATX;
514 }
515
516 /** Reads two bytes from the currently selected endpoint's bank in little endian format, for OUT
517 * direction endpoints.
518 *
519 * \ingroup Group_EndpointPrimitiveRW
520 *
521 * \return Next word in the currently selected endpoint's FIFO buffer
522 */
523 static inline uint16_t Endpoint_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
524 static inline uint16_t Endpoint_Read_Word_LE(void)
525 {
526 union
527 {
528 uint16_t Word;
529 uint8_t Bytes[2];
530 } Data;
531
532 Data.Bytes[0] = UEDATX;
533 Data.Bytes[1] = UEDATX;
534
535 return Data.Word;
536 }
537
538 /** Reads two bytes from the currently selected endpoint's bank in big endian format, for OUT
539 * direction endpoints.
540 *
541 * \ingroup Group_EndpointPrimitiveRW
542 *
543 * \return Next word in the currently selected endpoint's FIFO buffer
544 */
545 static inline uint16_t Endpoint_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
546 static inline uint16_t Endpoint_Read_Word_BE(void)
547 {
548 union
549 {
550 uint16_t Word;
551 uint8_t Bytes[2];
552 } Data;
553
554 Data.Bytes[1] = UEDATX;
555 Data.Bytes[0] = UEDATX;
556
557 return Data.Word;
558 }
559
560 /** Writes two bytes to the currently selected endpoint's bank in little endian format, for IN
561 * direction endpoints.
562 *
563 * \ingroup Group_EndpointPrimitiveRW
564 *
565 * \param[in] Word Next word to write to the currently selected endpoint's FIFO buffer
566 */
567 static inline void Endpoint_Write_Word_LE(const uint16_t Word) ATTR_ALWAYS_INLINE;
568 static inline void Endpoint_Write_Word_LE(const uint16_t Word)
569 {
570 UEDATX = (Word & 0xFF);
571 UEDATX = (Word >> 8);
572 }
573
574 /** Writes two bytes to the currently selected endpoint's bank in big endian format, for IN
575 * direction endpoints.
576 *
577 * \ingroup Group_EndpointPrimitiveRW
578 *
579 * \param[in] Word Next word to write to the currently selected endpoint's FIFO buffer
580 */
581 static inline void Endpoint_Write_Word_BE(const uint16_t Word) ATTR_ALWAYS_INLINE;
582 static inline void Endpoint_Write_Word_BE(const uint16_t Word)
583 {
584 UEDATX = (Word >> 8);
585 UEDATX = (Word & 0xFF);
586 }
587
588 /** Discards two bytes from the currently selected endpoint's bank, for OUT direction endpoints.
589 *
590 * \ingroup Group_EndpointPrimitiveRW
591 */
592 static inline void Endpoint_Discard_Word(void) ATTR_ALWAYS_INLINE;
593 static inline void Endpoint_Discard_Word(void)
594 {
595 uint8_t Dummy;
596
597 Dummy = UEDATX;
598 Dummy = UEDATX;
599 }
600
601 /** Reads four bytes from the currently selected endpoint's bank in little endian format, for OUT
602 * direction endpoints.
603 *
604 * \ingroup Group_EndpointPrimitiveRW
605 *
606 * \return Next double word in the currently selected endpoint's FIFO buffer
607 */
608 static inline uint32_t Endpoint_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
609 static inline uint32_t Endpoint_Read_DWord_LE(void)
610 {
611 union
612 {
613 uint32_t DWord;
614 uint8_t Bytes[4];
615 } Data;
616
617 Data.Bytes[0] = UEDATX;
618 Data.Bytes[1] = UEDATX;
619 Data.Bytes[2] = UEDATX;
620 Data.Bytes[3] = UEDATX;
621
622 return Data.DWord;
623 }
624
625 /** Reads four bytes from the currently selected endpoint's bank in big endian format, for OUT
626 * direction endpoints.
627 *
628 * \ingroup Group_EndpointPrimitiveRW
629 *
630 * \return Next double word in the currently selected endpoint's FIFO buffer
631 */
632 static inline uint32_t Endpoint_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
633 static inline uint32_t Endpoint_Read_DWord_BE(void)
634 {
635 union
636 {
637 uint32_t DWord;
638 uint8_t Bytes[4];
639 } Data;
640
641 Data.Bytes[3] = UEDATX;
642 Data.Bytes[2] = UEDATX;
643 Data.Bytes[1] = UEDATX;
644 Data.Bytes[0] = UEDATX;
645
646 return Data.DWord;
647 }
648
649 /** Writes four bytes to the currently selected endpoint's bank in little endian format, for IN
650 * direction endpoints.
651 *
652 * \ingroup Group_EndpointPrimitiveRW
653 *
654 * \param[in] DWord Next double word to write to the currently selected endpoint's FIFO buffer
655 */
656 static inline void Endpoint_Write_DWord_LE(const uint32_t DWord) ATTR_ALWAYS_INLINE;
657 static inline void Endpoint_Write_DWord_LE(const uint32_t DWord)
658 {
659 UEDATX = (DWord & 0xFF);
660 UEDATX = (DWord >> 8);
661 UEDATX = (DWord >> 16);
662 UEDATX = (DWord >> 24);
663 }
664
665 /** Writes four bytes to the currently selected endpoint's bank in big endian format, for IN
666 * direction endpoints.
667 *
668 * \ingroup Group_EndpointPrimitiveRW
669 *
670 * \param[in] DWord Next double word to write to the currently selected endpoint's FIFO buffer
671 */
672 static inline void Endpoint_Write_DWord_BE(const uint32_t DWord) ATTR_ALWAYS_INLINE;
673 static inline void Endpoint_Write_DWord_BE(const uint32_t DWord)
674 {
675 UEDATX = (DWord >> 24);
676 UEDATX = (DWord >> 16);
677 UEDATX = (DWord >> 8);
678 UEDATX = (DWord & 0xFF);
679 }
680
681 /** Discards four bytes from the currently selected endpoint's bank, for OUT direction endpoints.
682 *
683 * \ingroup Group_EndpointPrimitiveRW
684 */
685 static inline void Endpoint_Discard_DWord(void) ATTR_ALWAYS_INLINE;
686 static inline void Endpoint_Discard_DWord(void)
687 {
688 uint8_t Dummy;
689
690 Dummy = UEDATX;
691 Dummy = UEDATX;
692 Dummy = UEDATX;
693 Dummy = UEDATX;
694 }
695
696 /* External Variables: */
697 /** Global indicating the maximum packet size of the default control endpoint located at address
698 * 0 in the device. This value is set to the value indicated in the device descriptor in the user
699 * project once the USB interface is initialized into device mode.
700 *
701 * If space is an issue, it is possible to fix this to a static value by defining the control
702 * endpoint size in the FIXED_CONTROL_ENDPOINT_SIZE token passed to the compiler in the makefile
703 * via the -D switch. When a fixed control endpoint size is used, the size is no longer dynamically
704 * read from the descriptors at runtime and instead fixed to the given value. When used, it is
705 * important that the descriptor control endpoint size value matches the size given as the
706 * FIXED_CONTROL_ENDPOINT_SIZE token - it is recommended that the FIXED_CONTROL_ENDPOINT_SIZE token
707 * be used in the descriptors to ensure this.
708 *
709 * \note This variable should be treated as read-only in the user application, and never manually
710 * changed in value.
711 */
712 #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
713 extern uint8_t USB_ControlEndpointSize;
714 #else
715 #define USB_ControlEndpointSize FIXED_CONTROL_ENDPOINT_SIZE
716 #endif
717
718 /* Function Prototypes: */
719 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
720 #define __CALLBACK_PARAM , StreamCallbackPtr_t Callback
721 #else
722 #define __CALLBACK_PARAM
723 #endif
724
725 /** Configures the specified endpoint number with the given endpoint type, direction, bank size
726 * and banking mode. Endpoints should be allocated in ascending order by their address in the
727 * device (i.e. endpoint 1 should be configured before endpoint 2 and so on) to prevent fragmentation
728 * of the USB FIFO memory.
729 *
730 * The endpoint type may be one of the EP_TYPE_* macros listed in LowLevel.h and the direction
731 * may be either \ref ENDPOINT_DIR_OUT or \ref ENDPOINT_DIR_IN.
732 *
733 * The bank size must indicate the maximum packet size that the endpoint can handle. Different
734 * endpoint numbers can handle different maximum packet sizes - refer to the chosen USB AVR's
735 * datasheet to determine the maximum bank size for each endpoint.
736 *
737 * The banking mode may be either \ref ENDPOINT_BANK_SINGLE or \ref ENDPOINT_BANK_DOUBLE.
738 *
739 * \note The default control endpoint does not have to be manually configured, as it is automatically
740 * configured by the library internally.
741 *
742 * \note This routine will select the specified endpoint, and the endpoint will remain selected
743 * once the routine completes regardless of if the endpoint configuration succeeds.
744 *
745 * \return Boolean true if the configuration succeeded, false otherwise
746 */
747 bool Endpoint_ConfigureEndpoint(const uint8_t Number, const uint8_t Type, const uint8_t Direction,
748 const uint16_t Size, const uint8_t Banks);
749
750 /** Spin-loops until the currently selected non-control endpoint is ready for the next packet of data
751 * to be read or written to it.
752 *
753 * \note This routine should not be called on CONTROL type endpoints.
754 *
755 * \ingroup Group_EndpointRW
756 *
757 * \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
758 */
759 uint8_t Endpoint_WaitUntilReady(void);
760
761 /** Completes the status stage of a control transfer on a CONTROL type endpoint automatically,
762 * with respect to the data direction. This is a convenience function which can be used to
763 * simplify user control request handling.
764 */
765 void Endpoint_ClearStatusStage(void);
766
767 /** Reads and discards the given number of bytes from the endpoint from the given buffer,
768 * discarding fully read packets from the host as needed. The last packet is not automatically
769 * discarded once the remaining bytes has been read; the user is responsible for manually
770 * discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between
771 * each USB packet, the given stream callback function is executed repeatedly until the next
772 * packet is ready, allowing for early aborts of stream transfers.
773 *
774 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
775 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
776 * disabled and this function has the Callback parameter omitted.
777 *
778 * \note This routine should not be used on CONTROL type endpoints.
779 *
780 * \ingroup Group_EndpointStreamRW
781 *
782 * \param[in] Length Number of bytes to send via the currently selected endpoint.
783 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
784 *
785 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
786 */
787 uint8_t Endpoint_Discard_Stream(uint16_t Length __CALLBACK_PARAM);
788
789 /** Writes the given number of bytes to the endpoint from the given buffer in little endian,
790 * sending full packets to the host as needed. The last packet filled is not automatically sent;
791 * the user is responsible for manually sending the last written packet to the host via the
792 * \ref Endpoint_ClearIN() macro. Between each USB packet, the given stream callback function
793 * is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early
794 * aborts of stream transfers.
795 *
796 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
797 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
798 * disabled and this function has the Callback parameter omitted.
799 *
800 * \note This routine should not be used on CONTROL type endpoints.
801 *
802 * \ingroup Group_EndpointStreamRW
803 *
804 * \param[in] Buffer Pointer to the source data buffer to read from.
805 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
806 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
807 *
808 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
809 */
810 uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
811
812 /** EEPROM buffer source version of \ref Endpoint_Write_Stream_LE().
813 *
814 * \ingroup Group_EndpointStreamRW
815 *
816 * \param[in] Buffer Pointer to the source data buffer to read from.
817 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
818 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
819 *
820 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
821 */
822 uint8_t Endpoint_Write_EStream_LE(const void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
823
824 /** FLASH buffer source version of \ref Endpoint_Write_Stream_LE().
825 *
826 * \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
827 *
828 * \ingroup Group_EndpointStreamRW
829 *
830 * \param[in] Buffer Pointer to the source data buffer to read from.
831 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
832 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
833 *
834 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
835 */
836 uint8_t Endpoint_Write_PStream_LE(const void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
837
838 /** Writes the given number of bytes to the endpoint from the given buffer in big endian,
839 * sending full packets to the host as needed. The last packet filled is not automatically sent;
840 * the user is responsible for manually sending the last written packet to the host via the
841 * \ref Endpoint_ClearIN() macro. Between each USB packet, the given stream callback function
842 * is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early
843 * aborts of stream transfers.
844 *
845 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
846 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
847 * disabled and this function has the Callback parameter omitted.
848 *
849 * \note This routine should not be used on CONTROL type endpoints.
850 *
851 * \ingroup Group_EndpointStreamRW
852 *
853 * \param[in] Buffer Pointer to the source data buffer to read from.
854 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
855 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
856 *
857 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
858 */
859 uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
860
861 /** EEPROM buffer source version of \ref Endpoint_Write_Stream_BE().
862 *
863 * \ingroup Group_EndpointStreamRW
864 *
865 * \param[in] Buffer Pointer to the source data buffer to read from.
866 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
867 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
868 *
869 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
870 */
871 uint8_t Endpoint_Write_EStream_BE(const void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
872
873 /** FLASH buffer source version of \ref Endpoint_Write_Stream_BE().
874 *
875 * \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
876 *
877 * \ingroup Group_EndpointStreamRW
878 *
879 * \param[in] Buffer Pointer to the source data buffer to read from.
880 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
881 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
882 *
883 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
884 */
885 uint8_t Endpoint_Write_PStream_BE(const void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
886
887 /** Reads the given number of bytes from the endpoint from the given buffer in little endian,
888 * discarding fully read packets from the host as needed. The last packet is not automatically
889 * discarded once the remaining bytes has been read; the user is responsible for manually
890 * discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between
891 * each USB packet, the given stream callback function is executed repeatedly until the endpoint
892 * is ready to accept the next packet, allowing for early aborts of stream transfers.
893 *
894 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
895 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
896 * disabled and this function has the Callback parameter omitted.
897 *
898 * \note This routine should not be used on CONTROL type endpoints.
899 *
900 * \ingroup Group_EndpointStreamRW
901 *
902 * \param[out] Buffer Pointer to the destination data buffer to write to.
903 * \param[in] Length Number of bytes to send via the currently selected endpoint.
904 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
905 *
906 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
907 */
908 uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
909
910 /** EEPROM buffer source version of \ref Endpoint_Read_Stream_LE().
911 *
912 * \ingroup Group_EndpointStreamRW
913 *
914 * \param[out] Buffer Pointer to the destination data buffer to write to, located in EEPROM memory space.
915 * \param[in] Length Number of bytes to send via the currently selected endpoint.
916 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
917 *
918 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
919 */
920 uint8_t Endpoint_Read_EStream_LE(void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
921
922 /** Reads the given number of bytes from the endpoint from the given buffer in big endian,
923 * discarding fully read packets from the host as needed. The last packet is not automatically
924 * discarded once the remaining bytes has been read; the user is responsible for manually
925 * discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between
926 * each USB packet, the given stream callback function is executed repeatedly until the endpoint
927 * is ready to accept the next packet, allowing for early aborts of stream transfers.
928 *
929 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
930 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
931 * disabled and this function has the Callback parameter omitted.
932 *
933 * \note This routine should not be used on CONTROL type endpoints.
934 *
935 * \ingroup Group_EndpointStreamRW
936 *
937 * \param[out] Buffer Pointer to the destination data buffer to write to.
938 * \param[in] Length Number of bytes to send via the currently selected endpoint.
939 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
940 *
941 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
942 */
943 uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
944
945 /** EEPROM buffer source version of \ref Endpoint_Read_Stream_BE().
946 *
947 * \ingroup Group_EndpointStreamRW
948 *
949 * \param[out] Buffer Pointer to the destination data buffer to write to, located in EEPROM memory space.
950 * \param[in] Length Number of bytes to send via the currently selected endpoint.
951 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
952 *
953 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
954 */
955 uint8_t Endpoint_Read_EStream_BE(void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
956
957 /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in little endian,
958 * sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared
959 * in both failure and success states; the user is responsible for manually clearing the setup OUT to
960 * finalize the transfer via the \ref Endpoint_ClearOUT() macro.
961 *
962 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
963 * to clear the status stage when using this routine in a control transaction.
964 *
965 * \note This routine should only be used on CONTROL type endpoints.
966 *
967 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
968 * together; i.e. the entire stream data must be read or written at the one time.
969 *
970 * \ingroup Group_EndpointStreamRW
971 *
972 * \param[in] Buffer Pointer to the source data buffer to read from.
973 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
974 *
975 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
976 */
977 uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
978
979 /** EEPROM buffer source version of Endpoint_Write_Control_Stream_LE.
980 *
981 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
982 * to clear the status stage when using this routine in a control transaction.
983 *
984 * \note This routine should only be used on CONTROL type endpoints.
985 *
986 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
987 * together; i.e. the entire stream data must be read or written at the one time.
988 *
989 * \ingroup Group_EndpointStreamRW
990 *
991 * \param[in] Buffer Pointer to the source data buffer to read from.
992 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
993 *
994 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
995 */
996 uint8_t Endpoint_Write_Control_EStream_LE(const void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
997
998 /** FLASH buffer source version of \ref Endpoint_Write_Control_Stream_LE().
999 *
1000 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1001 * to clear the status stage when using this routine in a control transaction.
1002 *
1003 * \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
1004 *
1005 * \note This routine should only be used on CONTROL type endpoints.
1006 *
1007 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1008 * together; i.e. the entire stream data must be read or written at the one time.
1009 *
1010 * \ingroup Group_EndpointStreamRW
1011 *
1012 * \param[in] Buffer Pointer to the source data buffer to read from.
1013 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
1014 *
1015 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1016 */
1017 uint8_t Endpoint_Write_Control_PStream_LE(const void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
1018
1019 /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in big endian,
1020 * sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared
1021 * in both failure and success states; the user is responsible for manually clearing the setup OUT to
1022 * finalize the transfer via the \ref Endpoint_ClearOUT() macro.
1023 *
1024 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1025 * to clear the status stage when using this routine in a control transaction.
1026 *
1027 * \note This routine should only be used on CONTROL type endpoints.
1028 *
1029 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1030 * together; i.e. the entire stream data must be read or written at the one time.
1031 *
1032 * \ingroup Group_EndpointStreamRW
1033 *
1034 * \param[in] Buffer Pointer to the source data buffer to read from.
1035 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
1036 *
1037 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1038 */
1039 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
1040
1041 /** EEPROM buffer source version of \ref Endpoint_Write_Control_Stream_BE().
1042 *
1043 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1044 * to clear the status stage when using this routine in a control transaction.
1045 *
1046 * \note This routine should only be used on CONTROL type endpoints.
1047 *
1048 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1049 * together; i.e. the entire stream data must be read or written at the one time.
1050 *
1051 * \ingroup Group_EndpointStreamRW
1052 *
1053 * \param[in] Buffer Pointer to the source data buffer to read from.
1054 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
1055 *
1056 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1057 */
1058 uint8_t Endpoint_Write_Control_EStream_BE(const void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
1059
1060 /** FLASH buffer source version of \ref Endpoint_Write_Control_Stream_BE().
1061 *
1062 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1063 * to clear the status stage when using this routine in a control transaction.
1064 *
1065 * \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
1066 *
1067 * \note This routine should only be used on CONTROL type endpoints.
1068 *
1069 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1070 * together; i.e. the entire stream data must be read or written at the one time.
1071 *
1072 * \ingroup Group_EndpointStreamRW
1073 *
1074 * \param[in] Buffer Pointer to the source data buffer to read from.
1075 * \param[in] Length Number of bytes to read for the currently selected endpoint into the buffer.
1076 *
1077 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1078 */
1079 uint8_t Endpoint_Write_Control_PStream_BE(const void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
1080
1081 /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in little endian,
1082 * discarding fully read packets from the host as needed. The device IN acknowledgement is not
1083 * automatically sent after success or failure states; the user is responsible for manually sending the
1084 * setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro.
1085 *
1086 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1087 * to clear the status stage when using this routine in a control transaction.
1088 *
1089 * \note This routine should only be used on CONTROL type endpoints.
1090 *
1091 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1092 * together; i.e. the entire stream data must be read or written at the one time.
1093 *
1094 * \ingroup Group_EndpointStreamRW
1095 *
1096 * \param[out] Buffer Pointer to the destination data buffer to write to.
1097 * \param[in] Length Number of bytes to send via the currently selected endpoint.
1098 *
1099 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1100 */
1101 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
1102
1103 /** EEPROM buffer source version of \ref Endpoint_Read_Control_Stream_LE().
1104 *
1105 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1106 * to clear the status stage when using this routine in a control transaction.
1107 *
1108 * \note This routine should only be used on CONTROL type endpoints.
1109 *
1110 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1111 * together; i.e. the entire stream data must be read or written at the one time.
1112 *
1113 * \ingroup Group_EndpointStreamRW
1114 *
1115 * \param[out] Buffer Pointer to the destination data buffer to write to.
1116 * \param[in] Length Number of bytes to send via the currently selected endpoint.
1117 *
1118 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1119 */
1120 uint8_t Endpoint_Read_Control_EStream_LE(void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
1121
1122 /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in big endian,
1123 * discarding fully read packets from the host as needed. The device IN acknowledgement is not
1124 * automatically sent after success or failure states; the user is responsible for manually sending the
1125 * setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro.
1126 *
1127 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1128 * to clear the status stage when using this routine in a control transaction.
1129 *
1130 * \note This routine should only be used on CONTROL type endpoints.
1131 *
1132 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1133 * together; i.e. the entire stream data must be read or written at the one time.
1134 *
1135 * \ingroup Group_EndpointStreamRW
1136 *
1137 * \param[out] Buffer Pointer to the destination data buffer to write to.
1138 * \param[in] Length Number of bytes to send via the currently selected endpoint.
1139 *
1140 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1141 */
1142 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
1143
1144 /** EEPROM buffer source version of \ref Endpoint_Read_Control_Stream_BE().
1145 *
1146 * \note This function automatically clears the control transfer's status stage. Do not manually attempt
1147 * to clear the status stage when using this routine in a control transaction.
1148 *
1149 * \note This routine should only be used on CONTROL type endpoints.
1150 *
1151 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
1152 * together; i.e. the entire stream data must be read or written at the one time.
1153 *
1154 * \ingroup Group_EndpointStreamRW
1155 *
1156 * \param[out] Buffer Pointer to the destination data buffer to write to.
1157 * \param[in] Length Number of bytes to send via the currently selected endpoint.
1158 *
1159 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
1160 */
1161 uint8_t Endpoint_Read_Control_EStream_BE(void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
1162
1163 /* Private Interface - For use in library only: */
1164 #if !defined(__DOXYGEN__)
1165 /* Macros: */
1166 #define Endpoint_AllocateMemory() MACROS{ UECFG1X |= (1 << ALLOC); }MACROE
1167 #define Endpoint_DeallocateMemory() MACROS{ UECFG1X &= ~(1 << ALLOC); }MACROE
1168
1169 #define _ENDPOINT_GET_MAXSIZE(n) _ENDPOINT_GET_MAXSIZE2(ENDPOINT_DETAILS_EP ## n)
1170 #define _ENDPOINT_GET_MAXSIZE2(details) _ENDPOINT_GET_MAXSIZE3(details)
1171 #define _ENDPOINT_GET_MAXSIZE3(maxsize, db) maxsize
1172
1173 #define _ENDPOINT_GET_DOUBLEBANK(n) _ENDPOINT_GET_DOUBLEBANK2(ENDPOINT_DETAILS_EP ## n)
1174 #define _ENDPOINT_GET_DOUBLEBANK2(details) _ENDPOINT_GET_DOUBLEBANK3(details)
1175 #define _ENDPOINT_GET_DOUBLEBANK3(maxsize, db) db
1176
1177 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
1178 #define ENDPOINT_DETAILS_EP0 64, true
1179 #define ENDPOINT_DETAILS_EP1 256, true
1180 #define ENDPOINT_DETAILS_EP2 64, true
1181 #define ENDPOINT_DETAILS_EP3 64, true
1182 #define ENDPOINT_DETAILS_EP4 64, true
1183 #define ENDPOINT_DETAILS_EP5 64, true
1184 #define ENDPOINT_DETAILS_EP6 64, true
1185 #else
1186 #define ENDPOINT_DETAILS_EP0 64, true
1187 #define ENDPOINT_DETAILS_EP1 64, false
1188 #define ENDPOINT_DETAILS_EP2 64, false
1189 #define ENDPOINT_DETAILS_EP3 64, true
1190 #define ENDPOINT_DETAILS_EP4 64, true
1191 #endif
1192
1193 #define Endpoint_ConfigureEndpoint(Number, Type, Direction, Size, Banks) \
1194 Endpoint_ConfigureEndpoint_Prv((Number), \
1195 (((Type) << EPTYPE0) | (Direction)), \
1196 ((1 << ALLOC) | (Banks) | \
1197 (__builtin_constant_p(Size) ? \
1198 Endpoint_BytesToEPSizeMask(Size) : \
1199 Endpoint_BytesToEPSizeMaskDynamic(Size))))
1200
1201 /* Function Prototypes: */
1202 void Endpoint_ClearEndpoints(void);
1203 uint8_t Endpoint_BytesToEPSizeMaskDynamic(const uint16_t Size);
1204 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number, const uint8_t UECFG0XData, const uint8_t UECFG1XData);
1205
1206 /* Inline Functions: */
1207 static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE;
1208 static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes)
1209 {
1210 uint8_t MaskVal = 0;
1211 uint16_t CheckBytes = 8;
1212
1213 while (CheckBytes < Bytes)
1214 {
1215 MaskVal++;
1216 CheckBytes <<= 1;
1217 }
1218
1219 return (MaskVal << EPSIZE0);
1220 }
1221
1222 #endif
1223
1224 /* Disable C linkage for C++ Compilers: */
1225 #if defined(__cplusplus)
1226 }
1227 #endif
1228
1229 #endif
1230
1231 /** @} */