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