More porting of the USB core to the AVR32.
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Pipe.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 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 /** \ingroup Group_USB
32 * @defgroup Group_PipeManagement Pipe Management
33 *
34 * This module contains functions, macros and enums related to pipe management when in USB Host mode. This
35 * module contains the pipe management macros, as well as pipe interrupt and data send/receive functions
36 * for various data types.
37 *
38 * @{
39 */
40
41 /** @defgroup Group_PipeRW Pipe Data Reading and Writing
42 *
43 * Functions, macros, variables, enums and types related to data reading and writing from and to pipes.
44 */
45
46 /** \ingroup Group_PipeRW
47 * @defgroup Group_PipePrimitiveRW Read/Write of Primitive Data Types
48 *
49 * Functions, macros, variables, enums and types related to data reading and writing of primitive data types
50 * from and to pipes.
51 */
52
53 /** \ingroup Group_PipeRW
54 * @defgroup Group_PipeStreamRW Read/Write of Multi-Byte Streams
55 *
56 * Functions, macros, variables, enums and types related to data reading and writing of data streams from
57 * and to pipes.
58 */
59
60 /** @defgroup Group_PipePacketManagement Pipe Packet Management
61 *
62 * Functions, macros, variables, enums and types related to packet management of pipes.
63 */
64
65 /** @defgroup Group_PipeControlReq Pipe Control Request Management
66 *
67 * Module for host mode request processing. This module allows for the transmission of standard, class and
68 * vendor control requests to the default control endpoint of an attached device while in host mode.
69 *
70 * \see Chapter 9 of the USB 2.0 specification.
71 */
72
73 #ifndef __PIPE_H__
74 #define __PIPE_H__
75
76 /* Includes: */
77 #if defined(__AVR32__)
78 #include <avr32/io.h>
79 #include <stdint.h>
80 #include <stdbool.h>
81 #elif defined(__AVR__)
82 #include <avr/io.h>
83 #include <avr/pgmspace.h>
84 #include <avr/eeprom.h>
85 #include <stdbool.h>
86 #endif
87
88 #include "../../../Common/Common.h"
89 #include "LowLevel.h"
90 #include "../HighLevel/USBTask.h"
91
92 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
93 #include "../HighLevel/StreamCallbacks.h"
94 #endif
95
96 /* Enable C linkage for C++ Compilers: */
97 #if defined(__cplusplus)
98 extern "C" {
99 #endif
100
101 /* Preprocessor Checks: */
102 #if !defined(__INCLUDE_FROM_USB_DRIVER)
103 #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
104 #endif
105
106 /* Public Interface - May be used in end-application: */
107 /* Macros: */
108 /** Mask for \ref Pipe_GetErrorFlags(), indicating that an overflow error occurred in the pipe on the received data. */
109 #define PIPE_ERRORFLAG_OVERFLOW (1 << 6)
110
111 /** Mask for \ref Pipe_GetErrorFlags(), indicating that an underflow error occurred in the pipe on the received data. */
112 #define PIPE_ERRORFLAG_UNDERFLOW (1 << 5)
113
114 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a CRC error occurred in the pipe on the received data. */
115 #define PIPE_ERRORFLAG_CRC16 (1 << 4)
116
117 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware timeout error occurred in the pipe. */
118 #define PIPE_ERRORFLAG_TIMEOUT (1 << 3)
119
120 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware PID error occurred in the pipe. */
121 #define PIPE_ERRORFLAG_PID (1 << 2)
122
123 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware data PID error occurred in the pipe. */
124 #define PIPE_ERRORFLAG_DATAPID (1 << 1)
125
126 /** Mask for \ref Pipe_GetErrorFlags(), indicating that a hardware data toggle error occurred in the pipe. */
127 #define PIPE_ERRORFLAG_DATATGL (1 << 0)
128
129 /** Token mask for \ref Pipe_ConfigurePipe(). This sets the pipe as a SETUP token (for CONTROL type pipes),
130 * which will trigger a control request on the attached device when data is written to the pipe.
131 */
132 #define PIPE_TOKEN_SETUP (0 << PTOKEN0)
133
134 /** Token mask for \ref Pipe_ConfigurePipe(). This sets the pipe as a IN token (for non-CONTROL type pipes),
135 * indicating that the pipe data will flow from device to host.
136 */
137 #define PIPE_TOKEN_IN (1 << PTOKEN0)
138
139 /** Token mask for \ref Pipe_ConfigurePipe(). This sets the pipe as a OUT token (for non-CONTROL type pipes),
140 * indicating that the pipe data will flow from host to device.
141 */
142 #define PIPE_TOKEN_OUT (2 << PTOKEN0)
143
144 /** Mask for the bank mode selection for the \ref Pipe_ConfigurePipe() macro. This indicates that the pipe
145 * should have one single bank, which requires less USB FIFO memory but results in slower transfers as
146 * only one USB device (the AVR or the attached device) can access the pipe's bank at the one time.
147 */
148 #define PIPE_BANK_SINGLE (0 << EPBK0)
149
150 /** Mask for the bank mode selection for the \ref Pipe_ConfigurePipe() macro. This indicates that the pipe
151 * should have two banks, which requires more USB FIFO memory but results in faster transfers as one
152 * USB device (the AVR or the attached device) can access one bank while the other accesses the second
153 * bank.
154 */
155 #define PIPE_BANK_DOUBLE (1 << EPBK0)
156
157 /** Pipe address for the default control pipe, which always resides in address 0. This is
158 * defined for convenience to give more readable code when used with the pipe macros.
159 */
160 #define PIPE_CONTROLPIPE 0
161
162 /** Default size of the default control pipe's bank, until altered by the Endpoint0Size value
163 * in the device descriptor of the attached device.
164 */
165 #define PIPE_CONTROLPIPE_DEFAULT_SIZE 64
166
167 /** Pipe number mask, for masking against pipe addresses to retrieve the pipe's numerical address
168 * in the device.
169 */
170 #define PIPE_PIPENUM_MASK 0x07
171
172 /** Total number of pipes (including the default control pipe at address 0) which may be used in
173 * the device. Different USB AVR models support different amounts of pipes, this value reflects
174 * the maximum number of pipes for the currently selected AVR model.
175 */
176 #define PIPE_TOTAL_PIPES 7
177
178 /** Size in bytes of the largest pipe bank size possible in the device. Not all banks on each AVR
179 * model supports the largest bank size possible on the device; different pipe numbers support
180 * different maximum bank sizes. This value reflects the largest possible bank of any pipe on the
181 * currently selected USB AVR model.
182 */
183 #define PIPE_MAX_SIZE 256
184
185 /** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's
186 * numerical address in the attached device.
187 */
188 #define PIPE_EPNUM_MASK 0x0F
189
190 /** Endpoint direction mask, for masking against endpoint addresses to retrieve the endpoint's
191 * direction for comparing with the ENDPOINT_DESCRIPTOR_DIR_* masks.
192 */
193 #define PIPE_EPDIR_MASK 0x80
194
195 /* Pseudo-Function Macros: */
196 #if defined(__DOXYGEN__)
197 /** Indicates the number of bytes currently stored in the current pipes's selected bank.
198 *
199 * \note The return width of this function may differ, depending on the maximum pipe bank size
200 * of the selected AVR model.
201 *
202 * \ingroup Group_PipeRW
203 *
204 * \return Total number of bytes in the currently selected Pipe's FIFO buffer
205 */
206 static inline uint16_t Pipe_BytesInPipe(void);
207
208 /** Returns the pipe address of the currently selected pipe. This is typically used to save the
209 * currently selected pipe number so that it can be restored after another pipe has been manipulated.
210 *
211 * \return Index of the currently selected pipe
212 */
213 static inline uint8_t Pipe_GetCurrentPipe(void);
214
215 /** Selects the given pipe number. Any pipe operations which do not require the pipe number to be
216 * indicated will operate on the currently selected pipe.
217 *
218 * \param[in] PipeNumber Index of the pipe to select
219 */
220 static inline void Pipe_SelectPipe(uint8_t PipeNumber);
221
222 /** Resets the desired pipe, including the pipe banks and flags.
223 *
224 * \param[in] PipeNumber Index of the pipe to reset
225 */
226 static inline void Pipe_ResetPipe(uint8_t PipeNumber);
227
228 /** Enables the currently selected pipe so that data can be sent and received through it to and from
229 * an attached device.
230 *
231 * \note Pipes must first be configured properly via \ref Pipe_ConfigurePipe().
232 */
233 static inline void Pipe_EnablePipe(void);
234
235 /** Disables the currently selected pipe so that data cannot be sent and received through it to and
236 * from an attached device.
237 */
238 static inline void Pipe_DisablePipe(void);
239
240 /** Determines if the currently selected pipe is enabled, but not necessarily configured.
241 *
242 * \return Boolean True if the currently selected pipe is enabled, false otherwise
243 */
244 static inline bool Pipe_IsEnabled(void);
245
246 /** Gets the current pipe token, indicating the pipe's data direction and type.
247 *
248 * \return The current pipe token, as a PIPE_TOKEN_* mask
249 */
250 static inline uint8_t Pipe_GetPipeToken(void);
251
252 /** Sets the token for the currently selected pipe to one of the tokens specified by the PIPE_TOKEN_*
253 * masks. This can be used on CONTROL type pipes, to allow for bidirectional transfer of data during
254 * control requests, or on regular pipes to allow for half-duplex bidirectional data transfer to devices
255 * which have two endpoints of opposite direction sharing the same endpoint address within the device.
256 *
257 * \param[in] Token New pipe token to set the selected pipe to, as a PIPE_TOKEN_* mask
258 */
259 static inline void Pipe_SetPipeToken(uint8_t Token);
260
261 /** Configures the currently selected pipe to allow for an unlimited number of IN requests. */
262 static inline void Pipe_SetInfiniteINRequests(void);
263
264 /** Configures the currently selected pipe to only allow the specified number of IN requests to be
265 * accepted by the pipe before it is automatically frozen.
266 *
267 * \param[in] TotalINRequests Total number of IN requests that the pipe may receive before freezing
268 */
269 static inline void Pipe_SetFiniteINRequests(uint8_t TotalINRequests);
270
271 /** Determines if the currently selected pipe is configured.
272 *
273 * \return Boolean true if the selected pipe is configured, false otherwise
274 */
275 static inline bool Pipe_IsConfigured(void);
276
277 /** Retrieves the endpoint number of the endpoint within the attached device that the currently selected
278 * pipe is bound to.
279 *
280 * \return Endpoint number the currently selected pipe is bound to
281 */
282 static inline uint8_t Pipe_BoundEndpointNumber(void);
283
284 /** Sets the period between interrupts for an INTERRUPT type pipe to a specified number of milliseconds.
285 *
286 * \param[in] Milliseconds Number of milliseconds between each pipe poll
287 */
288 static inline void Pipe_SetInterruptPeriod(uint8_t Milliseconds);
289
290 /** Returns a mask indicating which pipe's interrupt periods have elapsed, indicating that the pipe should
291 * be serviced.
292 *
293 * \return Mask whose bits indicate which pipes have interrupted
294 */
295 static inline uint8_t Pipe_GetPipeInterrupts(void);
296
297 /** Determines if the specified pipe number has interrupted (valid only for INTERRUPT type
298 * pipes).
299 *
300 * \param[in] PipeNumber Index of the pipe whose interrupt flag should be tested
301 *
302 * \return Boolean true if the specified pipe has interrupted, false otherwise
303 */
304 static inline bool Pipe_HasPipeInterrupted(uint8_t PipeNumber);
305
306 /** Unfreezes the selected pipe, allowing it to communicate with an attached device. */
307 static inline void Pipe_Unfreeze(void);
308
309 /** Freezes the selected pipe, preventing it from communicating with an attached device. */
310 static inline void Pipe_Freeze(void);
311
312 /** Determines if the currently selected pipe is frozen, and not able to accept data.
313 *
314 * \return Boolean true if the currently selected pipe is frozen, false otherwise
315 */
316 static inline bool Pipe_IsFrozen(void);
317
318 /** Clears the master pipe error flag. */
319 static inline void Pipe_ClearError(void);
320
321 /** Determines if the master pipe error flag is set for the currently selected pipe, indicating that
322 * some sort of hardware error has occurred on the pipe.
323 *
324 * \see \ref Pipe_GetErrorFlags() macro for information on retrieving the exact error flag.
325 *
326 * \return Boolean true if an error has occurred on the selected pipe, false otherwise
327 */
328 static inline bool Pipe_IsError(void);
329
330 /** Clears all the currently selected pipe's hardware error flags, but does not clear the master error
331 * flag for the pipe.
332 */
333 static inline void Pipe_ClearErrorFlags(void);
334
335 /** Gets a mask of the hardware error flags which have occurred on the currently selected pipe. This
336 * value can then be masked against the PIPE_ERRORFLAG_* masks to determine what error has occurred.
337 *
338 * \return Mask comprising of PIPE_ERRORFLAG_* bits indicating what error has occurred on the selected pipe
339 */
340 static inline uint8_t Pipe_GetErrorFlags(void);
341
342 /** Determines if the currently selected pipe may be read from (if data is waiting in the pipe
343 * bank and the pipe is an IN direction, or if the bank is not yet full if the pipe is an OUT
344 * direction). This function will return false if an error has occurred in the pipe, or if the pipe
345 * is an IN direction and no packet (or an empty packet) has been received, or if the pipe is an OUT
346 * direction and the pipe bank is full.
347 *
348 * \note This function is not valid on CONTROL type pipes.
349 *
350 * \ingroup Group_PipePacketManagement
351 *
352 * \return Boolean true if the currently selected pipe may be read from or written to, depending on its direction
353 */
354 static inline bool Pipe_IsReadWriteAllowed(void);
355
356 /** Determines if an IN request has been received on the currently selected pipe.
357 *
358 * \ingroup Group_PipePacketManagement
359 *
360 * \return Boolean true if the current pipe has received an IN packet, false otherwise.
361 */
362 static inline bool Pipe_IsINReceived(void);
363
364 /** Determines if the currently selected pipe is ready to send an OUT request.
365 *
366 * \ingroup Group_PipePacketManagement
367 *
368 * \return Boolean true if the current pipe is ready for an OUT packet, false otherwise.
369 */
370 static inline bool Pipe_IsOUTReady(void);
371
372 /** Determines if no SETUP request is currently being sent to the attached device on the selected
373 * CONTROL type pipe.
374 *
375 * \ingroup Group_PipePacketManagement
376 *
377 * \return Boolean true if the current pipe is ready for a SETUP packet, false otherwise.
378 */
379 static inline bool Pipe_IsSETUPSent(void);
380
381 /** Sends the currently selected CONTROL type pipe's contents to the device as a SETUP packet.
382 *
383 * \ingroup Group_PipePacketManagement
384 */
385 static inline void Pipe_ClearSETUP(void);
386
387 /** Acknowledges the reception of a setup IN request from the attached device on the currently selected
388 * pipe, freeing the bank ready for the next packet.
389 *
390 * \ingroup Group_PipePacketManagement
391 */
392 static inline void Pipe_ClearIN(void);
393
394 /** Sends the currently selected pipe's contents to the device as an OUT packet on the selected pipe, freeing
395 * the bank ready for the next packet.
396 *
397 * \ingroup Group_PipePacketManagement
398 */
399 static inline void Pipe_ClearOUT(void);
400
401 /** Determines if the device sent a NAK (Negative Acknowledge) in response to the last sent packet on
402 * the currently selected pipe. This occurs when the host sends a packet to the device, but the device
403 * is not currently ready to handle the packet (i.e. its endpoint banks are full). Once a NAK has been
404 * received, it must be cleared using \ref Pipe_ClearNAKReceived() before the previous (or any other) packet
405 * can be re-sent.
406 *
407 * \ingroup Group_PipePacketManagement
408 *
409 * \return Boolean true if an NAK has been received on the current pipe, false otherwise
410 */
411 static inline bool Pipe_IsNAKReceived(void);
412
413 /** Clears the NAK condition on the currently selected pipe.
414 *
415 * \ingroup Group_PipePacketManagement
416 *
417 * \see \ref Pipe_IsNAKReceived() for more details.
418 */
419 static inline void Pipe_ClearNAKReceived(void);
420
421 /** Determines if the currently selected pipe has had the STALL condition set by the attached device.
422 *
423 * \ingroup Group_PipePacketManagement
424 *
425 * \return Boolean true if the current pipe has been stalled by the attached device, false otherwise
426 */
427 static inline bool Pipe_IsStalled(void);
428
429 /** Clears the STALL condition detection flag on the currently selected pipe, but does not clear the
430 * STALL condition itself (this must be done via a ClearFeature control request to the device).
431 *
432 * \ingroup Group_PipePacketManagement
433 */
434 static inline void Pipe_ClearStall(void);
435 #else
436 #define Pipe_BytesInPipe() UPBCX
437
438 #define Pipe_GetCurrentPipe() (UPNUM & PIPE_PIPENUM_MASK)
439
440 #define Pipe_SelectPipe(pipenum) MACROS{ UPNUM = (pipenum); }MACROE
441
442 #define Pipe_ResetPipe(pipenum) MACROS{ UPRST = (1 << (pipenum)); UPRST = 0; }MACROE
443
444 #define Pipe_EnablePipe() MACROS{ UPCONX |= (1 << PEN); }MACROE
445
446 #define Pipe_DisablePipe() MACROS{ UPCONX &= ~(1 << PEN); }MACROE
447
448 #define Pipe_IsEnabled() ((UPCONX & (1 << PEN)) ? true : false)
449
450 #define Pipe_GetPipeToken() (UPCFG0X & PIPE_TOKEN_MASK)
451
452 #define Pipe_SetPipeToken(token) MACROS{ UPCFG0X = ((UPCFG0X & ~PIPE_TOKEN_MASK) | (token)); }MACROE
453
454 #define Pipe_SetInfiniteINRequests() MACROS{ UPCONX |= (1 << INMODE); }MACROE
455
456 #define Pipe_SetFiniteINRequests(n) MACROS{ UPCONX &= ~(1 << INMODE); UPINRQX = (n); }MACROE
457
458 #define Pipe_IsConfigured() ((UPSTAX & (1 << CFGOK)) ? true : false)
459
460 #define Pipe_BoundEndpointNumber() ((UPCFG0X >> PEPNUM0) & PIPE_EPNUM_MASK)
461
462 #define Pipe_SetInterruptPeriod(ms) MACROS{ UPCFG2X = (ms); }MACROE
463
464 #define Pipe_GetPipeInterrupts() UPINT
465
466 #define Pipe_HasPipeInterrupted(n) ((UPINT & (1 << (n))) ? true : false)
467
468 #define Pipe_Unfreeze() MACROS{ UPCONX &= ~(1 << PFREEZE); }MACROE
469
470 #define Pipe_Freeze() MACROS{ UPCONX |= (1 << PFREEZE); }MACROE
471
472 #define Pipe_IsFrozen() ((UPCONX & (1 << PFREEZE)) ? true : false)
473
474 #define Pipe_ClearError() MACROS{ UPINTX &= ~(1 << PERRI); }MACROE
475
476 #define Pipe_IsError() ((UPINTX & (1 << PERRI)) ? true : false)
477
478 #define Pipe_ClearErrorFlags() MACROS{ UPERRX = 0; }MACROE
479
480 #define Pipe_GetErrorFlags() ((UPERRX & (PIPE_ERRORFLAG_CRC16 | PIPE_ERRORFLAG_TIMEOUT | \
481 PIPE_ERRORFLAG_PID | PIPE_ERRORFLAG_DATAPID | \
482 PIPE_ERRORFLAG_DATATGL)) | \
483 (UPSTAX & PIPE_ERRORFLAG_OVERFLOW | PIPE_ERRORFLAG_UNDERFLOW))
484
485 #define Pipe_IsReadWriteAllowed() ((UPINTX & (1 << RWAL)) ? true : false)
486
487 #define Pipe_IsINReceived() ((UPINTX & (1 << RXINI)) ? true : false)
488
489 #define Pipe_IsOUTReady() ((UPINTX & (1 << TXOUTI)) ? true : false)
490
491 #define Pipe_IsSETUPSent() ((UPINTX & (1 << TXSTPI)) ? true : false)
492
493 #define Pipe_ClearIN() MACROS{ uint8_t Temp = UPINTX; UPINTX = (Temp & ~(1 << RXINI)); \
494 UPINTX = (Temp & ~(1 << FIFOCON)); }MACROE
495
496 #define Pipe_ClearOUT() MACROS{ uint8_t Temp = UPINTX; UPINTX = (Temp & ~(1 << TXOUTI)); \
497 UPINTX = (Temp & ~(1 << FIFOCON)); }MACROE
498
499 #define Pipe_ClearSETUP() MACROS{ uint8_t Temp = UPINTX; UPINTX = (Temp & ~(1 << TXSTPI)); \
500 UPINTX = (Temp & ~(1 << FIFOCON)); }MACROE
501
502 #define Pipe_IsNAKReceived() ((UPINTX & (1 << NAKEDI)) ? true : false)
503
504 #define Pipe_ClearNAKReceived() MACROS{ UPINTX &= ~(1 << NAKEDI); }MACROE
505
506 #define Pipe_IsStalled() ((UPINTX & (1 << RXSTALLI)) ? true : false)
507
508 #define Pipe_ClearStall() MACROS{ UPINTX &= ~(1 << RXSTALLI); }MACROE
509 #endif
510
511 /* Enums: */
512 /** Enum for the possible error return codes of the Pipe_WaitUntilReady function
513 *
514 * \ingroup Group_PipeRW
515 */
516 enum Pipe_WaitUntilReady_ErrorCodes_t
517 {
518 PIPE_READYWAIT_NoError = 0, /**< Pipe ready for next packet, no error */
519 PIPE_READYWAIT_PipeStalled = 1, /**< The device stalled the pipe while waiting. */
520 PIPE_READYWAIT_DeviceDisconnected = 2, /**< Device was disconnected from the host while waiting. */
521 PIPE_READYWAIT_Timeout = 3, /**< The device failed to accept or send the next packet
522 * within the software timeout period set by the
523 * \ref USB_STREAM_TIMEOUT_MS macro.
524 */
525 };
526
527 /** Enum for the possible error return codes of the Pipe_*_Stream_* functions.
528 *
529 * \ingroup Group_PipeRW
530 */
531 enum Pipe_Stream_RW_ErrorCodes_t
532 {
533 PIPE_RWSTREAM_NoError = 0, /**< Command completed successfully, no error. */
534 PIPE_RWSTREAM_PipeStalled = 1, /**< The device stalled the pipe during the transfer. */
535 PIPE_RWSTREAM_DeviceDisconnected = 2, /**< Device was disconnected from the host during
536 * the transfer.
537 */
538 PIPE_RWSTREAM_Timeout = 3, /**< The device failed to accept or send the next packet
539 * within the software timeout period set by the
540 * \ref USB_STREAM_TIMEOUT_MS macro.
541 */
542 PIPE_RWSTREAM_CallbackAborted = 4, /**< Indicates that the stream's callback function aborted
543 * the transfer early.
544 */
545 };
546
547 /* Inline Functions: */
548 /** Reads one byte from the currently selected pipe's bank, for OUT direction pipes.
549 *
550 * \ingroup Group_PipePrimitiveRW
551 *
552 * \return Next byte in the currently selected pipe's FIFO buffer
553 */
554 static inline uint8_t Pipe_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
555 static inline uint8_t Pipe_Read_Byte(void)
556 {
557 #if defined(__AVR32__)
558 return __AVR32_EPREG_X(UEDAT0);
559 #elif defined(__AVR__)
560 return UPDATX;
561 #endif
562 }
563
564 /** Writes one byte from the currently selected pipe's bank, for IN direction pipes.
565 *
566 * \ingroup Group_PipePrimitiveRW
567 *
568 * \param[in] Byte Next byte to write into the the currently selected pipe's FIFO buffer
569 */
570 static inline void Pipe_Write_Byte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
571 static inline void Pipe_Write_Byte(const uint8_t Byte)
572 {
573 #if defined(__AVR32__)
574 __AVR32_EPREG_X(UEDAT0) = Byte;
575 #elif defined(__AVR__)
576 UPDATX = Byte;
577 #endif
578 }
579
580 /** Discards one byte from the currently selected pipe's bank, for OUT direction pipes.
581 *
582 * \ingroup Group_PipePrimitiveRW
583 */
584 static inline void Pipe_Discard_Byte(void) ATTR_ALWAYS_INLINE;
585 static inline void Pipe_Discard_Byte(void)
586 {
587 uint8_t Dummy;
588
589 #if defined(__AVR32__)
590 Dummy = __AVR32_EPREG_X(UEDAT0);
591 #elif defined(__AVR__)
592 Dummy = UPDATX;
593 #endif
594 }
595
596 /** Reads two bytes from the currently selected pipe's bank in little endian format, for OUT
597 * direction pipes.
598 *
599 * \ingroup Group_PipePrimitiveRW
600 *
601 * \return Next word in the currently selected pipe's FIFO buffer
602 */
603 static inline uint16_t Pipe_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
604 static inline uint16_t Pipe_Read_Word_LE(void)
605 {
606 union
607 {
608 uint16_t Word;
609 uint8_t Bytes[2];
610 } Data;
611
612 #if defined(__AVR32__)
613 Data.Bytes[0] = __AVR32_EPREG_X(UEDAT0);
614 Data.Bytes[1] = __AVR32_EPREG_X(UEDAT0);
615 #elif defined(__AVR__)
616 Data.Bytes[0] = UPDATX;
617 Data.Bytes[1] = UPDATX;
618 #endif
619
620 return Data.Word;
621 }
622
623 /** Reads two bytes from the currently selected pipe's bank in big endian format, for OUT
624 * direction pipes.
625 *
626 * \ingroup Group_PipePrimitiveRW
627 *
628 * \return Next word in the currently selected pipe's FIFO buffer
629 */
630 static inline uint16_t Pipe_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
631 static inline uint16_t Pipe_Read_Word_BE(void)
632 {
633 union
634 {
635 uint16_t Word;
636 uint8_t Bytes[2];
637 } Data;
638
639 #if defined(__AVR32__)
640 Data.Bytes[1] = __AVR32_EPREG_X(UEDAT0);
641 Data.Bytes[0] = __AVR32_EPREG_X(UEDAT0);
642 #elif defined(__AVR__)
643 Data.Bytes[1] = UPDATX;
644 Data.Bytes[0] = UPDATX;
645 #endif
646
647 return Data.Word;
648 }
649
650 /** Writes two bytes to the currently selected pipe's bank in little endian format, for IN
651 * direction pipes.
652 *
653 * \ingroup Group_PipePrimitiveRW
654 *
655 * \param[in] Word Next word to write to the currently selected pipe's FIFO buffer
656 */
657 static inline void Pipe_Write_Word_LE(const uint16_t Word) ATTR_ALWAYS_INLINE;
658 static inline void Pipe_Write_Word_LE(const uint16_t Word)
659 {
660 #if defined(__AVR32__)
661 __AVR32_EPREG_X(UEDAT0) = (Word & 0xFF);
662 __AVR32_EPREG_X(UEDAT0) = (Word >> 8);
663 #elif defined(__AVR__)
664 UPDATX = (Word & 0xFF);
665 UPDATX = (Word >> 8);
666 #endif
667 }
668
669 /** Writes two bytes to the currently selected pipe's bank in big endian format, for IN
670 * direction pipes.
671 *
672 * \ingroup Group_PipePrimitiveRW
673 *
674 * \param[in] Word Next word to write to the currently selected pipe's FIFO buffer
675 */
676 static inline void Pipe_Write_Word_BE(const uint16_t Word) ATTR_ALWAYS_INLINE;
677 static inline void Pipe_Write_Word_BE(const uint16_t Word)
678 {
679 #if defined(__AVR32__)
680 __AVR32_EPREG_X(UEDAT0) = (Word >> 8);
681 __AVR32_EPREG_X(UEDAT0) = (Word & 0xFF);
682 #elif defined(__AVR__)
683 UPDATX = (Word >> 8);
684 UPDATX = (Word & 0xFF);
685 #endif
686 }
687
688 /** Discards two bytes from the currently selected pipe's bank, for OUT direction pipes.
689 *
690 * \ingroup Group_PipePrimitiveRW
691 */
692 static inline void Pipe_Discard_Word(void) ATTR_ALWAYS_INLINE;
693 static inline void Pipe_Discard_Word(void)
694 {
695 uint8_t Dummy;
696
697 #if defined(__AVR32__)
698 Dummy = __AVR32_EPREG_X(UEDAT0);
699 Dummy = __AVR32_EPREG_X(UEDAT0);
700 #elif defined(__AVR__)
701 Dummy = UPDATX;
702 Dummy = UPDATX;
703 #endif
704 }
705
706 /** Reads four bytes from the currently selected pipe's bank in little endian format, for OUT
707 * direction pipes.
708 *
709 * \ingroup Group_PipePrimitiveRW
710 *
711 * \return Next double word in the currently selected pipe's FIFO buffer
712 */
713 static inline uint32_t Pipe_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
714 static inline uint32_t Pipe_Read_DWord_LE(void)
715 {
716 union
717 {
718 uint32_t DWord;
719 uint8_t Bytes[4];
720 } Data;
721
722 #if defined(__AVR32__)
723 Data.Bytes[0] = __AVR32_EPREG_X(UEDAT0);
724 Data.Bytes[1] = __AVR32_EPREG_X(UEDAT0);
725 Data.Bytes[2] = __AVR32_EPREG_X(UEDAT0);
726 Data.Bytes[3] = __AVR32_EPREG_X(UEDAT0);
727 #elif defined(__AVR__)
728 Data.Bytes[0] = UPDATX;
729 Data.Bytes[1] = UPDATX;
730 Data.Bytes[2] = UPDATX;
731 Data.Bytes[3] = UPDATX;
732 #endif
733
734 return Data.DWord;
735 }
736
737 /** Reads four bytes from the currently selected pipe's bank in big endian format, for OUT
738 * direction pipes.
739 *
740 * \ingroup Group_PipePrimitiveRW
741 *
742 * \return Next double word in the currently selected pipe's FIFO buffer
743 */
744 static inline uint32_t Pipe_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
745 static inline uint32_t Pipe_Read_DWord_BE(void)
746 {
747 union
748 {
749 uint32_t DWord;
750 uint8_t Bytes[4];
751 } Data;
752
753 #if defined(__AVR32__)
754 Data.Bytes[3] = __AVR32_EPREG_X(UEDAT0);
755 Data.Bytes[2] = __AVR32_EPREG_X(UEDAT0);
756 Data.Bytes[1] = __AVR32_EPREG_X(UEDAT0);
757 Data.Bytes[0] = __AVR32_EPREG_X(UEDAT0);
758 #elif defined(__AVR__)
759 Data.Bytes[3] = UPDATX;
760 Data.Bytes[2] = UPDATX;
761 Data.Bytes[1] = UPDATX;
762 Data.Bytes[0] = UPDATX;
763 #endif
764
765 return Data.DWord;
766 }
767
768 /** Writes four bytes to the currently selected pipe's bank in little endian format, for IN
769 * direction pipes.
770 *
771 * \ingroup Group_PipePrimitiveRW
772 *
773 * \param[in] DWord Next double word to write to the currently selected pipe's FIFO buffer
774 */
775 static inline void Pipe_Write_DWord_LE(const uint32_t DWord) ATTR_ALWAYS_INLINE;
776 static inline void Pipe_Write_DWord_LE(const uint32_t DWord)
777 {
778 #if defined(__AVR32__)
779 __AVR32_EPREG_X(UEDAT0) = (DWord & 0xFF);
780 __AVR32_EPREG_X(UEDAT0) = (DWord >> 8);
781 __AVR32_EPREG_X(UEDAT0) = (DWord >> 16);
782 __AVR32_EPREG_X(UEDAT0) = (DWord >> 24);
783 #elif defined(__AVR__)
784 UPDATX = (DWord & 0xFF);
785 UPDATX = (DWord >> 8);
786 UPDATX = (DWord >> 16);
787 UPDATX = (DWord >> 24);
788 #endif
789 }
790
791 /** Writes four bytes to the currently selected pipe's bank in big endian format, for IN
792 * direction pipes.
793 *
794 * \ingroup Group_PipePrimitiveRW
795 *
796 * \param[in] DWord Next double word to write to the currently selected pipe's FIFO buffer
797 */
798 static inline void Pipe_Write_DWord_BE(const uint32_t DWord) ATTR_ALWAYS_INLINE;
799 static inline void Pipe_Write_DWord_BE(const uint32_t DWord)
800 {
801 #if defined(__AVR32__)
802 __AVR32_EPREG_X(UEDAT0) = (DWord >> 24);
803 __AVR32_EPREG_X(UEDAT0) = (DWord >> 16);
804 __AVR32_EPREG_X(UEDAT0) = (DWord >> 8);
805 __AVR32_EPREG_X(UEDAT0) = (DWord & 0xFF);
806 #elif defined(__AVR__)
807 UPDATX = (DWord >> 24);
808 UPDATX = (DWord >> 16);
809 UPDATX = (DWord >> 8);
810 UPDATX = (DWord & 0xFF);
811 #endif
812 }
813
814 /** Discards four bytes from the currently selected pipe's bank, for OUT direction pipes.
815 *
816 * \ingroup Group_PipePrimitiveRW
817 */
818 static inline void Pipe_Discard_DWord(void) ATTR_ALWAYS_INLINE;
819 static inline void Pipe_Discard_DWord(void)
820 {
821 uint8_t Dummy;
822
823 #if defined(__AVR32__)
824 Dummy = __AVR32_EPREG_X(UEDAT0);
825 Dummy = __AVR32_EPREG_X(UEDAT0);
826 Dummy = __AVR32_EPREG_X(UEDAT0);
827 Dummy = __AVR32_EPREG_X(UEDAT0);
828 #elif defined(__AVR__)
829 Dummy = UPDATX;
830 Dummy = UPDATX;
831 Dummy = UPDATX;
832 Dummy = UPDATX;
833 #endif
834 }
835
836 /* External Variables: */
837 /** Global indicating the maximum packet size of the default control pipe located at address
838 * 0 in the device. This value is set to the value indicated in the attached device's device
839 * descriptor once the USB interface is initialized into host mode and a device is attached
840 * to the USB bus.
841 *
842 * \note This variable should be treated as read-only in the user application, and never manually
843 * changed in value.
844 */
845 extern uint8_t USB_ControlPipeSize;
846
847 /* Function Prototypes: */
848 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
849 #define __CALLBACK_PARAM , StreamCallbackPtr_t Callback
850 #else
851 #define __CALLBACK_PARAM
852 #endif
853
854 /** Configures the specified pipe number with the given pipe type, token, target endpoint number in the
855 * attached device, bank size and banking mode. Pipes should be allocated in ascending order by their
856 * address in the device (i.e. pipe 1 should be configured before pipe 2 and so on) to prevent fragmentation
857 * of the USB FIFO memory.
858 *
859 * The pipe type may be one of the EP_TYPE_* macros listed in LowLevel.h, the token may be one of the
860 * PIPE_TOKEN_* masks.
861 *
862 * The bank size must indicate the maximum packet size that the pipe can handle. Different pipe
863 * numbers can handle different maximum packet sizes - refer to the chosen USB AVR's datasheet to
864 * determine the maximum bank size for each pipe.
865 *
866 * The banking mode may be either \ref PIPE_BANK_SINGLE or \ref PIPE_BANK_DOUBLE.
867 *
868 * A newly configured pipe is frozen by default, and must be unfrozen before use via the \ref Pipe_Unfreeze()
869 * before being used. Pipes should be kept frozen unless waiting for data from a device while in IN mode, or
870 * sending data to the device in OUT mode. IN type pipes are also automatically configured to accept infinite
871 * numbers of IN requests without automatic freezing - this can be overridden by a call to
872 * \ref Pipe_SetFiniteINRequests().
873 *
874 * \note The default control pipe does not have to be manually configured, as it is automatically
875 * configured by the library internally.
876 *
877 * \note This routine will select the specified pipe, and the pipe will remain selected once the
878 * routine completes regardless of if the pipe configuration succeeds.
879 *
880 * \return Boolean true if the configuration is successful, false otherwise
881 */
882 bool Pipe_ConfigurePipe(const uint8_t Number, const uint8_t Type, const uint8_t Token, const uint8_t EndpointNumber,
883 const uint16_t Size, const uint8_t Banks);
884
885 /** Spin-loops until the currently selected non-control pipe is ready for the next packed of data to be read
886 * or written to it, aborting in the case of an error condition (such as a timeout or device disconnect).
887 *
888 * \ingroup Group_PipeRW
889 *
890 * \return A value from the Pipe_WaitUntilReady_ErrorCodes_t enum.
891 */
892 uint8_t Pipe_WaitUntilReady(void);
893
894 /** Determines if a pipe has been bound to the given device endpoint address. If a pipe which is bound to the given
895 * endpoint is found, it is automatically selected.
896 *
897 * \param[in] EndpointAddress Address and direction mask of the endpoint within the attached device to check
898 *
899 * \return Boolean true if a pipe bound to the given endpoint address of the specified direction is found, false
900 * otherwise
901 */
902 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress);
903
904 /** Reads and discards the given number of bytes from the pipe, discarding fully read packets from the host
905 * as needed. The last packet is not automatically discarded once the remaining bytes has been read; the
906 * user is responsible for manually discarding the last packet from the device via the \ref Pipe_ClearIN() macro.
907 * Between each USB packet, the given stream callback function is executed repeatedly until the next packet is ready,
908 * allowing for early aborts of stream transfers.
909 *
910 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
911 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
912 * disabled and this function has the Callback parameter omitted.
913 *
914 * The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
915 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
916 *
917 * \ingroup Group_PipeStreamRW
918 *
919 * \param[in] Length Number of bytes to send via the currently selected pipe.
920 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
921 *
922 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
923 */
924 uint8_t Pipe_Discard_Stream(uint16_t Length __CALLBACK_PARAM);
925
926 /** Writes the given number of bytes to the pipe from the given buffer in little endian,
927 * sending full packets to the device as needed. The last packet filled is not automatically sent;
928 * the user is responsible for manually sending the last written packet to the host via the
929 * \ref Pipe_ClearOUT() macro. Between each USB packet, the given stream callback function is
930 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
931 *
932 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
933 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
934 * disabled and this function has the Callback parameter omitted.
935 *
936 * The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
937 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
938 *
939 * \ingroup Group_PipeStreamRW
940 *
941 * \param[in] Buffer Pointer to the source data buffer to read from.
942 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
943 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
944 *
945 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
946 */
947 uint8_t Pipe_Write_Stream_LE(const void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
948
949 /** EEPROM buffer source version of \ref Pipe_Write_Stream_LE().
950 *
951 * \ingroup Group_PipeStreamRW
952 *
953 * \param[in] Buffer Pointer to the source data buffer to read from.
954 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
955 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
956 *
957 * \note Not available on AVR32 UC3B targets.
958 *
959 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
960 */
961 uint8_t Pipe_Write_EStream_LE(const void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
962
963 /** FLASH buffer source version of \ref Pipe_Write_Stream_LE().
964 *
965 * \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
966 *
967 * \ingroup Group_PipeStreamRW
968 *
969 * \param[in] Buffer Pointer to the source data buffer to read from.
970 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
971 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
972 *
973 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
974 */
975 uint8_t Pipe_Write_PStream_LE(const void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
976
977 /** Writes the given number of bytes to the pipe from the given buffer in big endian,
978 * sending full packets to the device as needed. The last packet filled is not automatically sent;
979 * the user is responsible for manually sending the last written packet to the host via the
980 * \ref Pipe_ClearOUT() macro. Between each USB packet, the given stream callback function is
981 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
982 *
983 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
984 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
985 * disabled and this function has the Callback parameter omitted.
986 *
987 * The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
988 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
989 *
990 * \ingroup Group_PipeStreamRW
991 *
992 * \param[in] Buffer Pointer to the source data buffer to read from.
993 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
994 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
995 *
996 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
997 */
998 uint8_t Pipe_Write_Stream_BE(const void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
999
1000 /** EEPROM buffer source version of \ref Pipe_Write_Stream_BE().
1001 *
1002 * \ingroup Group_PipeStreamRW
1003 *
1004 * \param[in] Buffer Pointer to the source data buffer to read from.
1005 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
1006 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1007 *
1008 * \note Not available on AVR32 UC3B targets.
1009 *
1010 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1011 */
1012 uint8_t Pipe_Write_EStream_BE(const void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
1013
1014 /** FLASH buffer source version of \ref Pipe_Write_Stream_BE().
1015 *
1016 * \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
1017 *
1018 * \ingroup Group_PipeStreamRW
1019 *
1020 * \param[in] Buffer Pointer to the source data buffer to read from.
1021 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
1022 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1023 *
1024 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1025 */
1026 uint8_t Pipe_Write_PStream_BE(const void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
1027
1028 /** Reads the given number of bytes from the pipe into the given buffer in little endian,
1029 * sending full packets to the device as needed. The last packet filled is not automatically sent;
1030 * the user is responsible for manually sending the last written packet to the host via the
1031 * \ref Pipe_ClearIN() macro. Between each USB packet, the given stream callback function is
1032 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
1033 *
1034 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
1035 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
1036 * disabled and this function has the Callback parameter omitted.
1037 *
1038 * The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
1039 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
1040 *
1041 * \ingroup Group_PipeStreamRW
1042 *
1043 * \param[out] Buffer Pointer to the source data buffer to write to.
1044 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
1045 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1046 *
1047 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1048 */
1049 uint8_t Pipe_Read_Stream_LE(void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
1050
1051 /** EEPROM buffer source version of \ref Pipe_Read_Stream_LE().
1052 *
1053 * \ingroup Group_PipeStreamRW
1054 *
1055 * \param[out] Buffer Pointer to the source data buffer to write to.
1056 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
1057 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1058 *
1059 * \note Not available on AVR32 UC3B targets.
1060 *
1061 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1062 */
1063 uint8_t Pipe_Read_EStream_LE(void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
1064
1065 /** Reads the given number of bytes from the pipe into the given buffer in big endian,
1066 * sending full packets to the device as needed. The last packet filled is not automatically sent;
1067 * the user is responsible for manually sending the last written packet to the host via the
1068 * \ref Pipe_ClearIN() macro. Between each USB packet, the given stream callback function is
1069 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
1070 *
1071 * The callback routine should be created according to the information in \ref Group_StreamCallbacks.
1072 * If the token NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are
1073 * disabled and this function has the Callback parameter omitted.
1074 *
1075 * The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
1076 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
1077 *
1078 * \ingroup Group_PipeStreamRW
1079 *
1080 * \param[out] Buffer Pointer to the source data buffer to write to.
1081 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
1082 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1083 *
1084 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1085 */
1086 uint8_t Pipe_Read_Stream_BE(void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
1087
1088 /** EEPROM buffer source version of \ref Pipe_Read_Stream_BE().
1089 *
1090 * \ingroup Group_PipeStreamRW
1091 *
1092 * \param[out] Buffer Pointer to the source data buffer to write to.
1093 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
1094 * \param[in] Callback Name of a callback routine to call between successive USB packet transfers, NULL if no callback
1095 *
1096 * \note Not available on AVR32 UC3B targets.
1097 *
1098 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
1099 */
1100 uint8_t Pipe_Read_EStream_BE(void* Buffer, uint16_t Length __CALLBACK_PARAM) ATTR_NON_NULL_PTR_ARG(1);
1101
1102 /* Private Interface - For use in library only: */
1103 #if !defined(__DOXYGEN__)
1104 /* Macros: */
1105 #define PIPE_TOKEN_MASK (0x03 << PTOKEN0)
1106
1107 #if !defined(ENDPOINT_CONTROLEP)
1108 #define ENDPOINT_CONTROLEP 0
1109 #endif
1110
1111 #define Pipe_AllocateMemory() MACROS{ UPCFG1X |= (1 << ALLOC); }MACROE
1112 #define Pipe_DeallocateMemory() MACROS{ UPCFG1X &= ~(1 << ALLOC); }MACROE
1113
1114 /* Function Prototypes: */
1115 void Pipe_ClearPipes(void);
1116
1117 /* Inline Functions: */
1118 static inline uintN_t Pipe_BytesToEPSizeMask(uint16_t Bytes) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE;
1119 static inline uintN_t Pipe_BytesToEPSizeMask(uint16_t Bytes)
1120 {
1121 #if defined(__AVR32__)
1122 // TODO
1123 return 0;
1124 #elif defined(__AVR__)
1125 if (Bytes <= 8)
1126 return (0 << EPSIZE0);
1127 else if (Bytes <= 16)
1128 return (1 << EPSIZE0);
1129 else if (Bytes <= 32)
1130 return (2 << EPSIZE0);
1131 else if (Bytes <= 64)
1132 return (3 << EPSIZE0);
1133 else if (Bytes <= 128)
1134 return (4 << EPSIZE0);
1135 else
1136 return (5 << EPSIZE0);
1137 #endif
1138 }
1139
1140 #endif
1141
1142 /* Disable C linkage for C++ Compilers: */
1143 #if defined(__cplusplus)
1144 }
1145 #endif
1146
1147 #endif
1148
1149 /** @} */