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