2a07747929aa7f489d28c839a9d0cfa5b2e3b5cb
[pub/lufa.git] / LUFA / Drivers / USB / Core / AVR8 / Endpoint.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 * \brief USB Endpoint definitions for the AVR8 microcontrollers.
33 * \copydetails Group_EndpointManagement_AVR8
34 *
35 * \note This file should not be included directly. It is automatically included as needed by the USB driver
36 * dispatch header located in LUFA/Drivers/USB/USB.h.
37 */
38
39 /** \ingroup Group_EndpointRW
40 * \defgroup Group_EndpointRW_AVR8 Endpoint Data Reading and Writing (AVR8)
41 * \brief USB Endpoint definitions for the AVR8 microcontrollers.
42 *
43 * Functions, macros, variables, enums and types related to data reading and writing from and to endpoints.
44 */
45
46 /** \ingroup Group_EndpointPrimitiveRW
47 * \defgroup Group_EndpointPrimitiveRW_AVR8 Read/Write of Primitive Data Types (AVR8)
48 *
49 * Functions, macros, variables, enums and types related to data reading and writing of primitive data types
50 * from and to endpoints.
51 */
52
53 /** \ingroup Group_EndpointPacketManagement
54 * \defgroup Group_EndpointPacketManagement_AVR8 Endpoint Packet Management (AVR8)
55 *
56 * Functions, macros, variables, enums and types related to packet management of endpoints.
57 */
58
59 /** \ingroup Group_EndpointManagement
60 * \defgroup Group_EndpointManagement_AVR8 Endpoint Management (AVR8)
61 *
62 * Functions, macros and enums related to endpoint management when in USB Device mode. This
63 * module contains the endpoint management macros, as well as endpoint interrupt and data
64 * send/receive functions for various data types.
65 *
66 * @{
67 */
68
69 #ifndef __ENDPOINT_AVR8_H__
70 #define __ENDPOINT_AVR8_H__
71
72 /* Includes: */
73 #include "../../../../Common/Common.h"
74 #include "../USBTask.h"
75 #include "../USBInterrupt.h"
76
77 /* Enable C linkage for C++ Compilers: */
78 #if defined(__cplusplus)
79 extern "C" {
80 #endif
81
82 /* Preprocessor Checks: */
83 #if !defined(__INCLUDE_FROM_USB_DRIVER)
84 #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
85 #endif
86
87 /* Private Interface - For use in library only: */
88 #if !defined(__DOXYGEN__)
89 /* Macros: */
90 #define _ENDPOINT_GET_MAXSIZE(EPIndex) _ENDPOINT_GET_MAXSIZE2(ENDPOINT_DETAILS_EP ## EPIndex)
91 #define _ENDPOINT_GET_MAXSIZE2(EPDetails) _ENDPOINT_GET_MAXSIZE3(EPDetails)
92 #define _ENDPOINT_GET_MAXSIZE3(MaxSize, Banks) (MaxSize)
93
94 #define _ENDPOINT_GET_BANKS(EPIndex) _ENDPOINT_GET_BANKS2(ENDPOINT_DETAILS_EP ## EPIndex)
95 #define _ENDPOINT_GET_BANKS2(EPDetails) _ENDPOINT_GET_BANKS3(EPDetails)
96 #define _ENDPOINT_GET_BANKS3(MaxSize, Banks) (Banks)
97
98 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
99 #define ENDPOINT_DETAILS_MAXEP 7
100
101 #define ENDPOINT_DETAILS_EP0 64, 2
102 #define ENDPOINT_DETAILS_EP1 256, 2
103 #define ENDPOINT_DETAILS_EP2 64, 2
104 #define ENDPOINT_DETAILS_EP3 64, 2
105 #define ENDPOINT_DETAILS_EP4 64, 2
106 #define ENDPOINT_DETAILS_EP5 64, 2
107 #define ENDPOINT_DETAILS_EP6 64, 2
108 #else
109 #define ENDPOINT_DETAILS_MAXEP 5
110
111 #define ENDPOINT_DETAILS_EP0 64, 2
112 #define ENDPOINT_DETAILS_EP1 64, 1
113 #define ENDPOINT_DETAILS_EP2 64, 1
114 #define ENDPOINT_DETAILS_EP3 64, 2
115 #define ENDPOINT_DETAILS_EP4 64, 2
116 #endif
117
118 /* Inline Functions: */
119 static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes) ATTR_WARN_UNUSED_RESULT ATTR_CONST
120 ATTR_ALWAYS_INLINE;
121 static inline uint8_t Endpoint_BytesToEPSizeMask(const uint16_t Bytes)
122 {
123 uint8_t MaskVal = 0;
124 uint16_t CheckBytes = 8;
125
126 while (CheckBytes < Bytes)
127 {
128 MaskVal++;
129 CheckBytes <<= 1;
130 }
131
132 return (MaskVal << EPSIZE0);
133 }
134
135 /* Function Prototypes: */
136 void Endpoint_ClearEndpoints(void);
137 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,
138 const uint8_t UECFG0XData,
139 const uint8_t UECFG1XData);
140
141 #endif
142
143 /* Public Interface - May be used in end-application: */
144 /* Macros: */
145 /** \name Endpoint Direction Masks */
146 //@{
147 /** Endpoint data direction mask for \ref Endpoint_ConfigureEndpoint(). This indicates that the endpoint
148 * should be initialized in the OUT direction - i.e. data flows from host to device.
149 */
150 #define ENDPOINT_DIR_OUT (0 << EPDIR)
151
152 /** Endpoint data direction mask for \ref Endpoint_ConfigureEndpoint(). This indicates that the endpoint
153 * should be initialized in the IN direction - i.e. data flows from device to host.
154 */
155 #define ENDPOINT_DIR_IN (1 << EPDIR)
156 //@}
157
158 /** \name Endpoint Bank Mode Masks */
159 //@{
160 /** Mask for the bank mode selection for the \ref Endpoint_ConfigureEndpoint() macro. This indicates
161 * that the endpoint should have one single bank, which requires less USB FIFO memory but results
162 * in slower transfers as only one USB device (the AVR or the host) can access the endpoint's
163 * bank at the one time.
164 */
165 #define ENDPOINT_BANK_SINGLE (0 << EPBK0)
166
167 /** Mask for the bank mode selection for the \ref Endpoint_ConfigureEndpoint() macro. This indicates
168 * that the endpoint should have two banks, which requires more USB FIFO memory but results
169 * in faster transfers as one USB device (the AVR or the host) can access one bank while the other
170 * accesses the second bank.
171 */
172 #define ENDPOINT_BANK_DOUBLE (1 << EPBK0)
173 //@}
174
175 /** Endpoint address for the default control endpoint, which always resides in address 0. This is
176 * defined for convenience to give more readable code when used with the endpoint macros.
177 */
178 #define ENDPOINT_CONTROLEP 0
179
180 #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
181 /** Default size of the default control endpoint's bank, until altered by the control endpoint bank size
182 * value in the device descriptor. Not available if the \c FIXED_CONTROL_ENDPOINT_SIZE token is defined.
183 */
184 #define ENDPOINT_CONTROLEP_DEFAULT_SIZE 8
185 #endif
186
187 /** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's
188 * numerical address in the device.
189 */
190 #define ENDPOINT_EPNUM_MASK 0x07
191
192 /** Endpoint direction mask, for masking against endpoint addresses to retrieve the endpoint's
193 * direction for comparing with the \c ENDPOINT_DESCRIPTOR_DIR_* masks.
194 */
195 #define ENDPOINT_EPDIR_MASK 0x80
196
197 /** Endpoint bank size mask, for masking against endpoint addresses to retrieve the endpoint's
198 * bank size in the device.
199 */
200 #define ENDPOINT_EPSIZE_MASK 0x7F
201
202 /** Retrives the maximum bank size in bytes of a given endpoint.
203 *
204 * \note This macro will only work correctly on endpoint indexes that are compile-time constants
205 * defined by the preprocessor.
206 *
207 * \param[in] EPIndex Endpoint number, a value between 0 and (\ref ENDPOINT_TOTAL_ENDPOINTS - 1)
208 */
209 #define ENDPOINT_MAX_SIZE(EPIndex) _ENDPOINT_GET_MAXSIZE(EPIndex)
210
211 /** Retrieves the total number of banks supported by the given endpoint.
212 *
213 * \note This macro will only work correctly on endpoint indexes that are compile-time constants
214 * defined by the preprocessor.
215 *
216 * \param[in] EPIndex Endpoint number, a value between 0 and (\ref ENDPOINT_TOTAL_ENDPOINTS - 1)
217 */
218 #define ENDPOINT_BANKS_SUPPORTED(EPIndex) _ENDPOINT_GET_BANKS(EPIndex)
219
220 #if !defined(CONTROL_ONLY_DEVICE) || defined(__DOXYGEN__)
221 /** Total number of endpoints (including the default control endpoint at address 0) which may
222 * be used in the device. Different USB AVR models support different amounts of endpoints,
223 * this value reflects the maximum number of endpoints for the currently selected AVR model.
224 */
225 #define ENDPOINT_TOTAL_ENDPOINTS ENDPOINT_DETAILS_MAXEP
226 #else
227 #define ENDPOINT_TOTAL_ENDPOINTS 1
228 #endif
229
230 /* Enums: */
231 /** Enum for the possible error return codes of the \ref Endpoint_WaitUntilReady() function.
232 *
233 * \ingroup Group_EndpointRW_AVR8
234 */
235 enum Endpoint_WaitUntilReady_ErrorCodes_t
236 {
237 ENDPOINT_READYWAIT_NoError = 0, /**< Endpoint is ready for next packet, no error. */
238 ENDPOINT_READYWAIT_EndpointStalled = 1, /**< The endpoint was stalled during the stream
239 * transfer by the host or device.
240 */
241 ENDPOINT_READYWAIT_DeviceDisconnected = 2, /**< Device was disconnected from the host while
242 * waiting for the endpoint to become ready.
243 */
244 ENDPOINT_READYWAIT_BusSuspended = 3, /**< The USB bus has been suspended by the host and
245 * no USB endpoint traffic can occur until the bus
246 * has resumed.
247 */
248 ENDPOINT_READYWAIT_Timeout = 4, /**< The host failed to accept or send the next packet
249 * within the software timeout period set by the
250 * \ref USB_STREAM_TIMEOUT_MS macro.
251 */
252 };
253
254 /* Inline Functions: */
255 /** Configures the specified endpoint number with the given endpoint type, direction, bank size
256 * and banking mode. Once configured, the endpoint may be read from or written to, depending
257 * on its direction.
258 *
259 * \param[in] Number Endpoint number to configure. This must be more than 0 and less than
260 * \ref ENDPOINT_TOTAL_ENDPOINTS.
261 *
262 * \param[in] Type Type of endpoint to configure, a \c EP_TYPE_* mask. Not all endpoint types
263 * are available on Low Speed USB devices - refer to the USB 2.0 specification.
264 *
265 * \param[in] Direction Endpoint data direction, either \ref ENDPOINT_DIR_OUT or \ref ENDPOINT_DIR_IN.
266 * All endpoints (except Control type) are unidirectional - data may only be read
267 * from or written to the endpoint bank based on its direction, not both.
268 *
269 * \param[in] Size Size of the endpoint's bank, where packets are stored before they are transmitted
270 * to the USB host, or after they have been received from the USB host (depending on
271 * the endpoint's data direction). The bank size must indicate the maximum packet size
272 * that the endpoint can handle.
273 *
274 * \param[in] Banks Number of banks to use for the endpoint being configured, an \c ENDPOINT_BANK_* mask.
275 * More banks uses more USB DPRAM, but offers better performance. Isochronous type
276 * endpoints <b>must</b> have at least two banks.
277 *
278 * \note When the \c ORDERED_EP_CONFIG compile time option is used, Endpoints <b>must</b> be configured in
279 * ascending order, or bank corruption will occur.
280 * \n\n
281 *
282 * \note Certain models of USB AVR's endpoints may have different maximum packet sizes based on the endpoint's
283 * index - refer to the chosen USB AVR's datasheet to determine the maximum bank size for each endpoint.
284 * \n\n
285 *
286 * \note The default control endpoint should not be manually configured by the user application, as
287 * it is automatically configured by the library internally.
288 * \n\n
289 *
290 * \note This routine will automatically select the specified endpoint upon success. Upon failure, the endpoint
291 * which failed to reconfigure correctly will be selected.
292 *
293 * \return Boolean \c true if the configuration succeeded, \c false otherwise.
294 */
295 static inline bool Endpoint_ConfigureEndpoint(const uint8_t Number,
296 const uint8_t Type,
297 const uint8_t Direction,
298 const uint16_t Size,
299 const uint8_t Banks) ATTR_ALWAYS_INLINE;
300 static inline bool Endpoint_ConfigureEndpoint(const uint8_t Number,
301 const uint8_t Type,
302 const uint8_t Direction,
303 const uint16_t Size,
304 const uint8_t Banks)
305 {
306 return Endpoint_ConfigureEndpoint_Prv(Number, (((Type) << EPTYPE0) | (Direction)),
307 ((1 << ALLOC) | Banks | Endpoint_BytesToEPSizeMask(Size)));
308 }
309
310 /** Indicates the number of bytes currently stored in the current endpoint's selected bank.
311 *
312 * \note The return width of this function may differ, depending on the maximum endpoint bank size
313 * of the selected AVR model.
314 *
315 * \ingroup Group_EndpointRW_AVR8
316 *
317 * \return Total number of bytes in the currently selected Endpoint's FIFO buffer.
318 */
319 static inline uint16_t Endpoint_BytesInEndpoint(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
320 static inline uint16_t Endpoint_BytesInEndpoint(void)
321 {
322 #if defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
323 return UEBCX;
324 #elif defined(USB_SERIES_4_AVR)
325 return (((uint16_t)UEBCHX << 8) | UEBCLX);
326 #elif defined(USB_SERIES_2_AVR)
327 return UEBCLX;
328 #endif
329 }
330
331 /** Get the endpoint address of the currently selected endpoint. This is typically used to save
332 * the currently selected endpoint number so that it can be restored after another endpoint has
333 * been manipulated.
334 *
335 * \return Index of the currently selected endpoint.
336 */
337 static inline uint8_t Endpoint_GetCurrentEndpoint(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
338 static inline uint8_t Endpoint_GetCurrentEndpoint(void)
339 {
340 #if !defined(CONTROL_ONLY_DEVICE)
341 return (UENUM & ENDPOINT_EPNUM_MASK);
342 #else
343 return ENDPOINT_CONTROLEP;
344 #endif
345 }
346
347 /** Selects the given endpoint number. If the address from the device descriptors is used, the
348 * value should be masked with the \ref ENDPOINT_EPNUM_MASK constant to extract only the endpoint
349 * number (and discarding the endpoint direction bit).
350 *
351 * Any endpoint operations which do not require the endpoint number to be indicated will operate on
352 * the currently selected endpoint.
353 *
354 * \param[in] EndpointNumber Endpoint number to select.
355 */
356 static inline void Endpoint_SelectEndpoint(const uint8_t EndpointNumber) ATTR_ALWAYS_INLINE;
357 static inline void Endpoint_SelectEndpoint(const uint8_t EndpointNumber)
358 {
359 #if !defined(CONTROL_ONLY_DEVICE)
360 UENUM = EndpointNumber;
361 #endif
362 }
363
364 /** Resets the endpoint bank FIFO. This clears all the endpoint banks and resets the USB controller's
365 * data In and Out pointers to the bank's contents.
366 *
367 * \param[in] EndpointNumber Endpoint number whose FIFO buffers are to be reset.
368 */
369 static inline void Endpoint_ResetFIFO(const uint8_t EndpointNumber) ATTR_ALWAYS_INLINE;
370 static inline void Endpoint_ResetFIFO(const uint8_t EndpointNumber)
371 {
372 UERST = (1 << EndpointNumber);
373 UERST = 0;
374 }
375
376 /** Enables the currently selected endpoint so that data can be sent and received through it to
377 * and from a host.
378 *
379 * \note Endpoints must first be configured properly via \ref Endpoint_ConfigureEndpoint().
380 */
381 static inline void Endpoint_EnableEndpoint(void) ATTR_ALWAYS_INLINE;
382 static inline void Endpoint_EnableEndpoint(void)
383 {
384 UECONX |= (1 << EPEN);
385 }
386
387 /** Disables the currently selected endpoint so that data cannot be sent and received through it
388 * to and from a host.
389 */
390 static inline void Endpoint_DisableEndpoint(void) ATTR_ALWAYS_INLINE;
391 static inline void Endpoint_DisableEndpoint(void)
392 {
393 UECONX &= ~(1 << EPEN);
394 }
395
396 /** Determines if the currently selected endpoint is enabled, but not necessarily configured.
397 *
398 * \return Boolean \c true if the currently selected endpoint is enabled, \c false otherwise.
399 */
400 static inline bool Endpoint_IsEnabled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
401 static inline bool Endpoint_IsEnabled(void)
402 {
403 return ((UECONX & (1 << EPEN)) ? true : false);
404 }
405
406 /** Aborts all pending IN transactions on the currently selected endpoint, once the bank
407 * has been queued for transmission to the host via \ref Endpoint_ClearIN(). This function
408 * will terminate all queued transactions, resetting the endpoint banks ready for a new
409 * packet.
410 *
411 * \ingroup Group_EndpointPacketManagement_AVR8
412 */
413 static inline void Endpoint_AbortPendingIN(void)
414 {
415 while (UESTA0X & (0x03 << NBUSYBK0))
416 {
417 UEINTX |= (1 << RXOUTI);
418 while (UEINTX & (1 << RXOUTI));
419 }
420 }
421
422 /** Retrieves the number of busy banks in the currently selected endpoint, which have been queued for
423 * transmission via the \ref Endpoint_ClearIN() command, or are awaiting acknowledgement via the
424 * \ref Endpoint_ClearOUT() command.
425 *
426 * \ingroup Group_EndpointPacketManagement_AVR8
427 *
428 * \return Total number of busy banks in the selected endpoint.
429 */
430 static inline uint8_t Endpoint_GetBusyBanks(void)
431 {
432 return (UESTA0X & (0x03 << NBUSYBK0));
433 }
434
435 /** Determines if the currently selected endpoint may be read from (if data is waiting in the endpoint
436 * bank and the endpoint is an OUT direction, or if the bank is not yet full if the endpoint is an IN
437 * direction). This function will return false if an error has occurred in the endpoint, if the endpoint
438 * is an OUT direction and no packet (or an empty packet) has been received, or if the endpoint is an IN
439 * direction and the endpoint bank is full.
440 *
441 * \ingroup Group_EndpointPacketManagement_AVR8
442 *
443 * \return Boolean \c true if the currently selected endpoint may be read from or written to, depending
444 * on its direction.
445 */
446 static inline bool Endpoint_IsReadWriteAllowed(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
447 static inline bool Endpoint_IsReadWriteAllowed(void)
448 {
449 return ((UEINTX & (1 << RWAL)) ? true : false);
450 }
451
452 /** Determines if the currently selected endpoint is configured.
453 *
454 * \return Boolean \c true if the currently selected endpoint has been configured, \c false otherwise.
455 */
456 static inline bool Endpoint_IsConfigured(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
457 static inline bool Endpoint_IsConfigured(void)
458 {
459 return ((UESTA0X & (1 << CFGOK)) ? true : false);
460 }
461
462 /** Returns a mask indicating which INTERRUPT type endpoints have interrupted - i.e. their
463 * interrupt duration has elapsed. Which endpoints have interrupted can be determined by
464 * masking the return value against <tt>(1 << <i>{Endpoint Number}</i>)</tt>.
465 *
466 * \return Mask whose bits indicate which endpoints have interrupted.
467 */
468 static inline uint8_t Endpoint_GetEndpointInterrupts(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
469 static inline uint8_t Endpoint_GetEndpointInterrupts(void)
470 {
471 return UEINT;
472 }
473
474 /** Determines if the specified endpoint number has interrupted (valid only for INTERRUPT type
475 * endpoints).
476 *
477 * \param[in] EndpointNumber Index of the endpoint whose interrupt flag should be tested.
478 *
479 * \return Boolean \c true if the specified endpoint has interrupted, \c false otherwise.
480 */
481 static inline bool Endpoint_HasEndpointInterrupted(const uint8_t EndpointNumber) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
482 static inline bool Endpoint_HasEndpointInterrupted(const uint8_t EndpointNumber)
483 {
484 return ((UEINT & (1 << EndpointNumber)) ? true : false);
485 }
486
487 /** Determines if the selected IN endpoint is ready for a new packet to be sent to the host.
488 *
489 * \ingroup Group_EndpointPacketManagement_AVR8
490 *
491 * \return Boolean \c true if the current endpoint is ready for an IN packet, \c false otherwise.
492 */
493 static inline bool Endpoint_IsINReady(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
494 static inline bool Endpoint_IsINReady(void)
495 {
496 return ((UEINTX & (1 << TXINI)) ? true : false);
497 }
498
499 /** Determines if the selected OUT endpoint has received new packet from the host.
500 *
501 * \ingroup Group_EndpointPacketManagement_AVR8
502 *
503 * \return Boolean \c true if current endpoint is has received an OUT packet, \c false otherwise.
504 */
505 static inline bool Endpoint_IsOUTReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
506 static inline bool Endpoint_IsOUTReceived(void)
507 {
508 return ((UEINTX & (1 << RXOUTI)) ? true : false);
509 }
510
511 /** Determines if the current CONTROL type endpoint has received a SETUP packet.
512 *
513 * \ingroup Group_EndpointPacketManagement_AVR8
514 *
515 * \return Boolean \c true if the selected endpoint has received a SETUP packet, \c false otherwise.
516 */
517 static inline bool Endpoint_IsSETUPReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
518 static inline bool Endpoint_IsSETUPReceived(void)
519 {
520 return ((UEINTX & (1 << RXSTPI)) ? true : false);
521 }
522
523 /** Clears a received SETUP packet on the currently selected CONTROL type endpoint, freeing up the
524 * endpoint for the next packet.
525 *
526 * \ingroup Group_EndpointPacketManagement_AVR8
527 *
528 * \note This is not applicable for non CONTROL type endpoints.
529 */
530 static inline void Endpoint_ClearSETUP(void) ATTR_ALWAYS_INLINE;
531 static inline void Endpoint_ClearSETUP(void)
532 {
533 UEINTX &= ~(1 << RXSTPI);
534 }
535
536 /** Sends an IN packet to the host on the currently selected endpoint, freeing up the endpoint for the
537 * next packet and switching to the alternative endpoint bank if double banked.
538 *
539 * \ingroup Group_EndpointPacketManagement_AVR8
540 */
541 static inline void Endpoint_ClearIN(void) ATTR_ALWAYS_INLINE;
542 static inline void Endpoint_ClearIN(void)
543 {
544 #if !defined(CONTROL_ONLY_DEVICE)
545 UEINTX &= ~((1 << TXINI) | (1 << FIFOCON));
546 #else
547 UEINTX &= ~(1 << TXINI);
548 #endif
549 }
550
551 /** Acknowledges an OUT packet to the host on the currently selected endpoint, freeing up the endpoint
552 * for the next packet and switching to the alternative endpoint bank if double banked.
553 *
554 * \ingroup Group_EndpointPacketManagement_AVR8
555 */
556 static inline void Endpoint_ClearOUT(void) ATTR_ALWAYS_INLINE;
557 static inline void Endpoint_ClearOUT(void)
558 {
559 #if !defined(CONTROL_ONLY_DEVICE)
560 UEINTX &= ~((1 << RXOUTI) | (1 << FIFOCON));
561 #else
562 UEINTX &= ~(1 << RXOUTI);
563 #endif
564 }
565
566 /** Stalls the current endpoint, indicating to the host that a logical problem occurred with the
567 * indicated endpoint and that the current transfer sequence should be aborted. This provides a
568 * way for devices to indicate invalid commands to the host so that the current transfer can be
569 * aborted and the host can begin its own recovery sequence.
570 *
571 * The currently selected endpoint remains stalled until either the \ref Endpoint_ClearStall() macro
572 * is called, or the host issues a CLEAR FEATURE request to the device for the currently selected
573 * endpoint.
574 *
575 * \ingroup Group_EndpointPacketManagement_AVR8
576 */
577 static inline void Endpoint_StallTransaction(void) ATTR_ALWAYS_INLINE;
578 static inline void Endpoint_StallTransaction(void)
579 {
580 UECONX |= (1 << STALLRQ);
581 }
582
583 /** Clears the STALL condition on the currently selected endpoint.
584 *
585 * \ingroup Group_EndpointPacketManagement_AVR8
586 */
587 static inline void Endpoint_ClearStall(void) ATTR_ALWAYS_INLINE;
588 static inline void Endpoint_ClearStall(void)
589 {
590 UECONX |= (1 << STALLRQC);
591 }
592
593 /** Determines if the currently selected endpoint is stalled, false otherwise.
594 *
595 * \ingroup Group_EndpointPacketManagement_AVR8
596 *
597 * \return Boolean \c true if the currently selected endpoint is stalled, \c false otherwise.
598 */
599 static inline bool Endpoint_IsStalled(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
600 static inline bool Endpoint_IsStalled(void)
601 {
602 return ((UECONX & (1 << STALLRQ)) ? true : false);
603 }
604
605 /** Resets the data toggle of the currently selected endpoint. */
606 static inline void Endpoint_ResetDataToggle(void) ATTR_ALWAYS_INLINE;
607 static inline void Endpoint_ResetDataToggle(void)
608 {
609 UECONX |= (1 << RSTDT);
610 }
611
612 /** Determines the currently selected endpoint's direction.
613 *
614 * \return The currently selected endpoint's direction, as a \c ENDPOINT_DIR_* mask.
615 */
616 static inline uint8_t Endpoint_GetEndpointDirection(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
617 static inline uint8_t Endpoint_GetEndpointDirection(void)
618 {
619 return (UECFG0X & ENDPOINT_DIR_IN);
620 }
621
622 /** Sets the direction of the currently selected endpoint.
623 *
624 * \param[in] DirectionMask New endpoint direction, as a \c ENDPOINT_DIR_* mask.
625 */
626 static inline void Endpoint_SetEndpointDirection(const uint8_t DirectionMask) ATTR_ALWAYS_INLINE;
627 static inline void Endpoint_SetEndpointDirection(const uint8_t DirectionMask)
628 {
629 UECFG0X = ((UECFG0X & ~ENDPOINT_DIR_IN) | DirectionMask);
630 }
631
632 /** Reads one byte from the currently selected endpoint's bank, for OUT direction endpoints.
633 *
634 * \ingroup Group_EndpointPrimitiveRW_AVR8
635 *
636 * \return Next byte in the currently selected endpoint's FIFO buffer.
637 */
638 static inline uint8_t Endpoint_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
639 static inline uint8_t Endpoint_Read_Byte(void)
640 {
641 return UEDATX;
642 }
643
644 /** Writes one byte from the currently selected endpoint's bank, for IN direction endpoints.
645 *
646 * \ingroup Group_EndpointPrimitiveRW_AVR8
647 *
648 * \param[in] Byte Next byte to write into the the currently selected endpoint's FIFO buffer.
649 */
650 static inline void Endpoint_Write_Byte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
651 static inline void Endpoint_Write_Byte(const uint8_t Byte)
652 {
653 UEDATX = Byte;
654 }
655
656 /** Discards one byte from the currently selected endpoint's bank, for OUT direction endpoints.
657 *
658 * \ingroup Group_EndpointPrimitiveRW_AVR8
659 */
660 static inline void Endpoint_Discard_Byte(void) ATTR_ALWAYS_INLINE;
661 static inline void Endpoint_Discard_Byte(void)
662 {
663 uint8_t Dummy;
664
665 Dummy = UEDATX;
666 }
667
668 /** Reads two bytes from the currently selected endpoint's bank in little endian format, for OUT
669 * direction endpoints.
670 *
671 * \ingroup Group_EndpointPrimitiveRW_AVR8
672 *
673 * \return Next word in the currently selected endpoint's FIFO buffer.
674 */
675 static inline uint16_t Endpoint_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
676 static inline uint16_t Endpoint_Read_Word_LE(void)
677 {
678 union
679 {
680 uint16_t Word;
681 uint8_t Bytes[2];
682 } Data;
683
684 Data.Bytes[0] = UEDATX;
685 Data.Bytes[1] = UEDATX;
686
687 return Data.Word;
688 }
689
690 /** Reads two bytes from the currently selected endpoint's bank in big endian format, for OUT
691 * direction endpoints.
692 *
693 * \ingroup Group_EndpointPrimitiveRW_AVR8
694 *
695 * \return Next word in the currently selected endpoint's FIFO buffer.
696 */
697 static inline uint16_t Endpoint_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
698 static inline uint16_t Endpoint_Read_Word_BE(void)
699 {
700 union
701 {
702 uint16_t Word;
703 uint8_t Bytes[2];
704 } Data;
705
706 Data.Bytes[1] = UEDATX;
707 Data.Bytes[0] = UEDATX;
708
709 return Data.Word;
710 }
711
712 /** Writes two bytes to the currently selected endpoint's bank in little endian format, for IN
713 * direction endpoints.
714 *
715 * \ingroup Group_EndpointPrimitiveRW_AVR8
716 *
717 * \param[in] Word Next word to write to the currently selected endpoint's FIFO buffer.
718 */
719 static inline void Endpoint_Write_Word_LE(const uint16_t Word) ATTR_ALWAYS_INLINE;
720 static inline void Endpoint_Write_Word_LE(const uint16_t Word)
721 {
722 UEDATX = (Word & 0xFF);
723 UEDATX = (Word >> 8);
724 }
725
726 /** Writes two bytes to the currently selected endpoint's bank in big endian format, for IN
727 * direction endpoints.
728 *
729 * \ingroup Group_EndpointPrimitiveRW_AVR8
730 *
731 * \param[in] Word Next word to write to the currently selected endpoint's FIFO buffer.
732 */
733 static inline void Endpoint_Write_Word_BE(const uint16_t Word) ATTR_ALWAYS_INLINE;
734 static inline void Endpoint_Write_Word_BE(const uint16_t Word)
735 {
736 UEDATX = (Word >> 8);
737 UEDATX = (Word & 0xFF);
738 }
739
740 /** Discards two bytes from the currently selected endpoint's bank, for OUT direction endpoints.
741 *
742 * \ingroup Group_EndpointPrimitiveRW_AVR8
743 */
744 static inline void Endpoint_Discard_Word(void) ATTR_ALWAYS_INLINE;
745 static inline void Endpoint_Discard_Word(void)
746 {
747 uint8_t Dummy;
748
749 Dummy = UEDATX;
750 Dummy = UEDATX;
751 }
752
753 /** Reads four bytes from the currently selected endpoint's bank in little endian format, for OUT
754 * direction endpoints.
755 *
756 * \ingroup Group_EndpointPrimitiveRW_AVR8
757 *
758 * \return Next double word in the currently selected endpoint's FIFO buffer.
759 */
760 static inline uint32_t Endpoint_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
761 static inline uint32_t Endpoint_Read_DWord_LE(void)
762 {
763 union
764 {
765 uint32_t DWord;
766 uint8_t Bytes[4];
767 } Data;
768
769 Data.Bytes[0] = UEDATX;
770 Data.Bytes[1] = UEDATX;
771 Data.Bytes[2] = UEDATX;
772 Data.Bytes[3] = UEDATX;
773
774 return Data.DWord;
775 }
776
777 /** Reads four bytes from the currently selected endpoint's bank in big endian format, for OUT
778 * direction endpoints.
779 *
780 * \ingroup Group_EndpointPrimitiveRW_AVR8
781 *
782 * \return Next double word in the currently selected endpoint's FIFO buffer.
783 */
784 static inline uint32_t Endpoint_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
785 static inline uint32_t Endpoint_Read_DWord_BE(void)
786 {
787 union
788 {
789 uint32_t DWord;
790 uint8_t Bytes[4];
791 } Data;
792
793 Data.Bytes[3] = UEDATX;
794 Data.Bytes[2] = UEDATX;
795 Data.Bytes[1] = UEDATX;
796 Data.Bytes[0] = UEDATX;
797
798 return Data.DWord;
799 }
800
801 /** Writes four bytes to the currently selected endpoint's bank in little endian format, for IN
802 * direction endpoints.
803 *
804 * \ingroup Group_EndpointPrimitiveRW_AVR8
805 *
806 * \param[in] DWord Next double word to write to the currently selected endpoint's FIFO buffer.
807 */
808 static inline void Endpoint_Write_DWord_LE(const uint32_t DWord) ATTR_ALWAYS_INLINE;
809 static inline void Endpoint_Write_DWord_LE(const uint32_t DWord)
810 {
811 UEDATX = (DWord & 0xFF);
812 UEDATX = (DWord >> 8);
813 UEDATX = (DWord >> 16);
814 UEDATX = (DWord >> 24);
815 }
816
817 /** Writes four bytes to the currently selected endpoint's bank in big endian format, for IN
818 * direction endpoints.
819 *
820 * \ingroup Group_EndpointPrimitiveRW_AVR8
821 *
822 * \param[in] DWord Next double word to write to the currently selected endpoint's FIFO buffer.
823 */
824 static inline void Endpoint_Write_DWord_BE(const uint32_t DWord) ATTR_ALWAYS_INLINE;
825 static inline void Endpoint_Write_DWord_BE(const uint32_t DWord)
826 {
827 UEDATX = (DWord >> 24);
828 UEDATX = (DWord >> 16);
829 UEDATX = (DWord >> 8);
830 UEDATX = (DWord & 0xFF);
831 }
832
833 /** Discards four bytes from the currently selected endpoint's bank, for OUT direction endpoints.
834 *
835 * \ingroup Group_EndpointPrimitiveRW_AVR8
836 */
837 static inline void Endpoint_Discard_DWord(void) ATTR_ALWAYS_INLINE;
838 static inline void Endpoint_Discard_DWord(void)
839 {
840 uint8_t Dummy;
841
842 Dummy = UEDATX;
843 Dummy = UEDATX;
844 Dummy = UEDATX;
845 Dummy = UEDATX;
846 }
847
848 /* External Variables: */
849 /** Global indicating the maximum packet size of the default control endpoint located at address
850 * 0 in the device. This value is set to the value indicated in the device descriptor in the user
851 * project once the USB interface is initialized into device mode.
852 *
853 * If space is an issue, it is possible to fix this to a static value by defining the control
854 * endpoint size in the \c FIXED_CONTROL_ENDPOINT_SIZE token passed to the compiler in the makefile
855 * via the -D switch. When a fixed control endpoint size is used, the size is no longer dynamically
856 * read from the descriptors at runtime and instead fixed to the given value. When used, it is
857 * important that the descriptor control endpoint size value matches the size given as the
858 * \c FIXED_CONTROL_ENDPOINT_SIZE token - it is recommended that the \c FIXED_CONTROL_ENDPOINT_SIZE token
859 * be used in the device descriptors to ensure this.
860 *
861 * \note This variable should be treated as read-only in the user application, and never manually
862 * changed in value.
863 */
864 #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
865 extern uint8_t USB_ControlEndpointSize;
866 #else
867 #define USB_ControlEndpointSize FIXED_CONTROL_ENDPOINT_SIZE
868 #endif
869
870 /* Function Prototypes: */
871 /** Completes the status stage of a control transfer on a CONTROL type endpoint automatically,
872 * with respect to the data direction. This is a convenience function which can be used to
873 * simplify user control request handling.
874 */
875 void Endpoint_ClearStatusStage(void);
876
877 /** Spin-loops until the currently selected non-control endpoint is ready for the next packet of data
878 * to be read or written to it.
879 *
880 * \note This routine should not be called on CONTROL type endpoints.
881 *
882 * \ingroup Group_EndpointRW_AVR8
883 *
884 * \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
885 */
886 uint8_t Endpoint_WaitUntilReady(void);
887
888 /* Disable C linkage for C++ Compilers: */
889 #if defined(__cplusplus)
890 }
891 #endif
892
893 #endif
894
895 /** @} */
896