Rewritten event system to remove all macros, to make user code clearer.
[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_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER) || 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_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER) || 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).
675 *
676 * The endpoint type may be one of the EP_TYPE_* macros listed in LowLevel.h and the direction
677 * may be either \ref ENDPOINT_DIR_OUT or \ref ENDPOINT_DIR_IN.
678 *
679 * The bank size must indicate the maximum packet size that the endpoint can handle. Different
680 * endpoint numbers can handle different maximum packet sizes - refer to the chosen USB AVR's
681 * datasheet to determine the maximum bank size for each endpoint.
682 *
683 * The banking mode may be either \ref ENDPOINT_BANK_SINGLE or \ref ENDPOINT_BANK_DOUBLE.
684 *
685 * The success of this routine can be determined via the \ref Endpoint_IsConfigured() macro.
686 *
687 * \note This routine will select the specified endpoint, and the endpoint will remain selected
688 * once the routine completes regardless of if the endpoint configuration succeeds.
689 *
690 * \return Boolean true if the configuration succeeded, false otherwise
691 */
692 bool Endpoint_ConfigureEndpoint(const uint8_t Number, const uint8_t Type, const uint8_t Direction,
693 const uint16_t Size, const uint8_t Banks);
694
695 #if !defined(CONTROL_ONLY_DEVICE)
696
697 /** Spinloops until the currently selected non-control endpoint is ready for the next packet of data
698 * to be read or written to it.
699 *
700 * \note This routine should not be called on CONTROL type endpoints.
701 *
702 * \ingroup Group_EndpointRW
703 *
704 * \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
705 */
706 uint8_t Endpoint_WaitUntilReady(void);
707
708 /** Reads and discards the given number of bytes from the endpoint from the given buffer,
709 * discarding fully read packets from the host as needed. The last packet is not automatically
710 * discarded once the remaining bytes has been read; the user is responsible for manually
711 * discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between
712 * each USB packet, the given stream callback function is executed repeatedly until the next
713 * packet is ready, allowing for early aborts of stream transfers.
714 *
715 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
716 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
717 * disabled and this function has the Callback parameter omitted.
718 *
719 * \note This routine should not be used on CONTROL type endpoints.
720 *
721 * \ingroup Group_EndpointRW
722 *
723 * \param Length Number of bytes to send via the currently selected endpoint.
724 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
725 *
726 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
727 */
728 uint8_t Endpoint_Discard_Stream(uint16_t Length
729 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
730 , StreamCallbackPtr_t Callback
731 #endif
732 );
733
734 /** Writes the given number of bytes to the endpoint from the given buffer in little endian,
735 * sending full packets to the host as needed. The last packet filled is not automatically sent;
736 * the user is responsible for manually sending the last written packet to the host via the
737 * \ref Endpoint_ClearIN() macro. Between each USB packet, the given stream callback function
738 * is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early
739 * aborts of stream transfers.
740 *
741 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
742 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
743 * disabled and this function has the Callback parameter omitted.
744 *
745 * \note This routine should not be used on CONTROL type endpoints.
746 *
747 * \ingroup Group_EndpointRW
748 *
749 * \param Buffer Pointer to the source data buffer to read from.
750 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
751 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
752 *
753 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
754 */
755 uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length
756 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
757 , StreamCallbackPtr_t Callback
758 #endif
759 ) ATTR_NON_NULL_PTR_ARG(1);
760
761 /** Writes the given number of bytes to the endpoint from the given buffer in big endian,
762 * sending full packets to the host as needed. The last packet filled is not automatically sent;
763 * the user is responsible for manually sending the last written packet to the host via the
764 * \ref Endpoint_ClearIN() macro. Between each USB packet, the given stream callback function
765 * is executed repeatedly until the endpoint is ready to accept the next packet, allowing for early
766 * aborts of stream transfers.
767 *
768 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
769 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
770 * disabled and this function has the Callback parameter omitted.
771 *
772 * \note This routine should not be used on CONTROL type endpoints.
773 *
774 * \ingroup Group_EndpointRW
775 *
776 * \param Buffer Pointer to the source data buffer to read from.
777 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
778 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
779 *
780 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
781 */
782 uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length
783 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
784 , StreamCallbackPtr_t Callback
785 #endif
786 ) ATTR_NON_NULL_PTR_ARG(1);
787
788 /** Reads the given number of bytes from the endpoint from the given buffer in little endian,
789 * discarding fully read packets from the host as needed. The last packet is not automatically
790 * discarded once the remaining bytes has been read; the user is responsible for manually
791 * discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between
792 * each USB packet, the given stream callback function is executed repeatedly until the endpoint
793 * is ready to accept the next packet, allowing for early aborts of stream transfers.
794 *
795 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
796 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
797 * disabled and this function has the Callback parameter omitted.
798 *
799 * \note This routine should not be used on CONTROL type endpoints.
800 *
801 * \ingroup Group_EndpointRW
802 *
803 * \param Buffer Pointer to the destination data buffer to write to.
804 * \param Length Number of bytes to send via the currently selected endpoint.
805 * \param Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
806 *
807 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
808 */
809 uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length
810 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
811 , StreamCallbackPtr_t Callback
812 #endif
813 ) ATTR_NON_NULL_PTR_ARG(1);
814
815 /** Reads the given number of bytes from the endpoint from the given buffer in big endian,
816 * discarding fully read packets from the host as needed. The last packet is not automatically
817 * discarded once the remaining bytes has been read; the user is responsible for manually
818 * discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. Between
819 * each USB packet, the given stream callback function is executed repeatedly until the endpoint
820 * is ready to accept the next packet, allowing for early aborts of stream transfers.
821 *
822 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
823 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
824 * disabled and this function has the Callback parameter omitted.
825 *
826 * \note This routine should not be used on CONTROL type endpoints.
827 *
828 * \ingroup Group_EndpointRW
829 *
830 * \param Buffer Pointer to the destination data buffer to write to.
831 * \param Length Number of bytes to send via the currently selected endpoint.
832 * \param 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_Read_Stream_BE(void* Buffer, uint16_t Length
837 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
838 , StreamCallbackPtr_t Callback
839 #endif
840 ) ATTR_NON_NULL_PTR_ARG(1);
841
842 #endif
843
844 /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in little endian,
845 * sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared
846 * in both failure and success states; the user is responsible for manually clearing the setup OUT to
847 * finalize the transfer via the \ref Endpoint_ClearOUT() macro.
848 *
849 * \note This routine should only be used on CONTROL type endpoints.
850 *
851 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
852 * together; i.e. the entire stream data must be read or written at the one time.
853 *
854 * \ingroup Group_EndpointRW
855 *
856 * \param Buffer Pointer to the source data buffer to read from.
857 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
858 *
859 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
860 */
861 uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
862
863 /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in big endian,
864 * sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared
865 * in both failure and success states; the user is responsible for manually clearing the setup OUT to
866 * finalize the transfer via the \ref Endpoint_ClearOUT() macro.
867 *
868 * \note This routine should only be used on CONTROL type endpoints.
869 *
870 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
871 * together; i.e. the entire stream data must be read or written at the one time.
872 *
873 * \ingroup Group_EndpointRW
874 *
875 * \param Buffer Pointer to the source data buffer to read from.
876 * \param Length Number of bytes to read for the currently selected endpoint into the buffer.
877 *
878 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
879 */
880 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
881
882 /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in little endian,
883 * discarding fully read packets from the host as needed. The device IN acknowledgement is not
884 * automatically sent after success or failure states; the user is responsible for manually sending the
885 * setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro.
886 *
887 * \note This routine should only be used on CONTROL type endpoints.
888 *
889 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
890 * together; i.e. the entire stream data must be read or written at the one time.
891 *
892 * \ingroup Group_EndpointRW
893 *
894 * \param Buffer Pointer to the destination data buffer to write to.
895 * \param Length Number of bytes to send via the currently selected endpoint.
896 *
897 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
898 */
899 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
900
901 /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in big endian,
902 * discarding fully read packets from the host as needed. The device IN acknowledgement is not
903 * automatically sent after success or failure states; the user is responsible for manually sending the
904 * setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro.
905 *
906 * \note This routine should only be used on CONTROL type endpoints.
907 *
908 * \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
909 * together; i.e. the entire stream data must be read or written at the one time.
910 *
911 * \ingroup Group_EndpointRW
912 *
913 * \param Buffer Pointer to the destination data buffer to write to.
914 * \param Length Number of bytes to send via the currently selected endpoint.
915 *
916 * \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum.
917 */
918 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
919
920 /* Private Interface - For use in library only: */
921 #if !defined(__DOXYGEN__)
922 /* Macros: */
923 #define Endpoint_AllocateMemory() MACROS{ UECFG1X |= (1 << ALLOC); }MACROE
924 #define Endpoint_DeallocateMemory() MACROS{ UECFG1X &= ~(1 << ALLOC); }MACROE
925
926 #define _ENDPOINT_GET_MAXSIZE(n) _ENDPOINT_GET_MAXSIZE2(ENDPOINT_DETAILS_EP ## n)
927 #define _ENDPOINT_GET_MAXSIZE2(details) _ENDPOINT_GET_MAXSIZE3(details)
928 #define _ENDPOINT_GET_MAXSIZE3(maxsize, db) maxsize
929
930 #define _ENDPOINT_GET_DOUBLEBANK(n) _ENDPOINT_GET_DOUBLEBANK2(ENDPOINT_DETAILS_EP ## n)
931 #define _ENDPOINT_GET_DOUBLEBANK2(details) _ENDPOINT_GET_DOUBLEBANK3(details)
932 #define _ENDPOINT_GET_DOUBLEBANK3(maxsize, db) db
933
934 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER)
935 #define ENDPOINT_DETAILS_EP0 64, true
936 #define ENDPOINT_DETAILS_EP1 256, true
937 #define ENDPOINT_DETAILS_EP2 64, true
938 #define ENDPOINT_DETAILS_EP3 64, true
939 #define ENDPOINT_DETAILS_EP4 64, true
940 #define ENDPOINT_DETAILS_EP5 64, true
941 #define ENDPOINT_DETAILS_EP6 64, true
942 #else
943 #define ENDPOINT_DETAILS_EP0 64, true
944 #define ENDPOINT_DETAILS_EP1 64, false
945 #define ENDPOINT_DETAILS_EP2 64, false
946 #define ENDPOINT_DETAILS_EP3 64, true
947 #define ENDPOINT_DETAILS_EP4 64, true
948 #endif
949
950 #define Endpoint_ConfigureEndpoint(Number, Type, Direction, Size, Banks) \
951 Endpoint_ConfigureEndpoint_Prv(Number, \
952 ((Type << EPTYPE0) | Direction), \
953 ((1 << ALLOC) | Banks | \
954 (__builtin_constant_p(Size) ? \
955 Endpoint_BytesToEPSizeMask(Size) : \
956 Endpoint_BytesToEPSizeMaskDynamic(Size))))
957
958 /* Function Prototypes: */
959 void Endpoint_ClearEndpoints(void);
960 uint8_t Endpoint_BytesToEPSizeMaskDynamic(const uint16_t Size);
961 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number, const uint8_t UECFG0XData, const uint8_t UECFG1XData);
962
963 /* Inline Functions: */
964 static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE;
965 static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes)
966 {
967 uint8_t MaskVal = 0;
968 uint16_t CheckBytes = 8;
969
970 while (CheckBytes < Bytes)
971 {
972 MaskVal++;
973 CheckBytes <<= 1;
974 }
975
976 return (MaskVal << EPSIZE0);
977 };
978
979 #endif
980
981 /* Disable C linkage for C++ Compilers: */
982 #if defined(__cplusplus)
983 }
984 #endif
985
986 #endif
987
988 /** @} */