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