Board Dataflash driver now allows for dataflash ICs which use different shifts for...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Pipe.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 /** \file
32 *
33 * Functions, macros and enums related to pipe management when in USB Host mode. This
34 * module contains the pipe management macros, as well as pipe interrupt and data
35 * send/recieve functions for various datatypes.
36 */
37
38 #ifndef __PIPE_H__
39 #define __PIPE_H__
40
41 /* Includes: */
42 #include <avr/io.h>
43 #include <stdbool.h>
44
45 #include "../../../Common/Common.h"
46 #include "../HighLevel/USBTask.h"
47
48 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
49 #include "StreamCallbacks.h"
50 #endif
51 /* Enable C linkage for C++ Compilers: */
52 #if defined(__cplusplus)
53 extern "C" {
54 #endif
55
56 /* Public Interface - May be used in end-application: */
57 /* Macros: */
58 /** Mask for Pipe_GetErrorFlags(), indicating that a CRC error occurred in the pipe on the received data. */
59 #define PIPE_ERRORFLAG_CRC16 (1 << 4)
60
61 /** Mask for Pipe_GetErrorFlags(), indicating that a hardware timeout error occurred in the pipe. */
62 #define PIPE_ERRORFLAG_TIMEOUT (1 << 3)
63
64 /** Mask for Pipe_GetErrorFlags(), indicating that a hardware PID error occurred in the pipe. */
65 #define PIPE_ERRORFLAG_PID (1 << 2)
66
67 /** Mask for Pipe_GetErrorFlags(), indicating that a hardware data PID error occurred in the pipe. */
68 #define PIPE_ERRORFLAG_DATAPID (1 << 1)
69
70 /** Mask for Pipe_GetErrorFlags(), indicating that a hardware data toggle error occurred in the pipe. */
71 #define PIPE_ERRORFLAG_DATATGL (1 << 0)
72
73 /** Token mask for Pipe_ConfigurePipe(). This sets the pipe as a SETUP token (for CONTROL type pipes),
74 * which will trigger a control request on the attached device when data is written to the pipe.
75 */
76 #define PIPE_TOKEN_SETUP (0b00 << PTOKEN0)
77
78 /** Token mask for Pipe_ConfigurePipe(). This sets the pipe as a IN token (for non-CONTROL type pipes),
79 * indicating that the pipe data will flow from device to host.
80 */
81 #define PIPE_TOKEN_IN (0b01 << PTOKEN0)
82
83 /** Token mask for Pipe_ConfigurePipe(). This sets the pipe as a IN token (for non-CONTROL type pipes),
84 * indicating that the pipe data will flow from host to device.
85 */
86 #define PIPE_TOKEN_OUT (0b10 << PTOKEN0)
87
88 /** Mask for the bank mode selection for the Pipe_ConfigurePipe() macro. This indicates that the pipe
89 * should have one single bank, which requires less USB FIFO memory but results in slower transfers as
90 * only one USB device (the AVR or the attached device) can access the pipe's bank at the one time.
91 */
92 #define PIPE_BANK_SINGLE (0 << EPBK0)
93
94 /** Mask for the bank mode selection for the Pipe_ConfigurePipe() macro. This indicates that the pipe
95 * should have two banks, which requires more USB FIFO memory but results in faster transfers as one
96 * USB device (the AVR or the attached device) can access one bank while the other accesses the second
97 * bank.
98 */
99 #define PIPE_BANK_DOUBLE (1 << EPBK0)
100
101 /** Pipe address for the default control pipe, which always resides in address 0. This is
102 * defined for convenience to give more readable code when used with the pipe macros.
103 */
104 #define PIPE_CONTROLPIPE 0
105
106 /** Default size of the default control pipe's bank, until altered by the Endpoint0Size value
107 * in the device descriptor of the attached device.
108 */
109 #define PIPE_CONTROLPIPE_DEFAULT_SIZE 8
110
111 /** Pipe number mask, for masking against pipe addresses to retrieve the pipe's numerical address
112 * in the device.
113 */
114 #define PIPE_PIPENUM_MASK 0x07
115
116 /** Total number of pipes (including the default control pipe at address 0) which may be used in
117 * the device. Different USB AVR models support different amounts of pipes, this value reflects
118 * the maximum number of pipes for the currently selected AVR model.
119 */
120 #define PIPE_TOTAL_PIPES 7
121
122 /** Size in bytes of the largest pipe bank size possible in the device. Not all banks on each AVR
123 * model supports the largest bank size possible on the device; different pipe numbers support
124 * different maximum bank sizes. This value reflects the largest possible bank of any pipe on the
125 * currently selected USB AVR model.
126 */
127 #define PIPE_MAX_SIZE 256
128
129 /** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's
130 * numerical address in the attached device.
131 */
132 #define PIPE_EPNUM_MASK 0x07
133
134 /** Endpoint bank size mask, for masking against endpoint addresses to retrieve the endpoint's
135 * bank size in the attached device.
136 */
137 #define PIPE_EPSIZE_MASK 0x7FF
138
139 /** Interrupt definition for the pipe IN interrupt (for INTERRUPT type pipes). Should be used with
140 * the USB_INT_* macros located in USBInterrupt.h.
141 *
142 * This interrupt will fire if enabled on an INTERRUPT type pipe if the pipe interrupt period has
143 * elapsed and the pipe is ready for the next packet from the attached device to be read out from its
144 * FIFO buffer (if received).
145 *
146 * \note This interrupt must be enabled and cleared on *each* pipe which requires it (after the pipe
147 * is selected), and will fire the common pipe interrupt vector.
148 *
149 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
150 */
151 #define PIPE_INT_IN UPIENX, (1 << RXINE) , UPINTX, (1 << RXINI)
152
153 /** Interrupt definition for the pipe OUT interrupt (for INTERRUPT type pipes). Should be used with
154 * the USB_INT_* macros located in USBInterrupt.h.
155 *
156 * This interrupt will fire if enabled on an INTERRUPT type endpoint if a the pipe interrupt period
157 * has elapsed and the pipe is ready for a packet to be written to the pipe's FIFO buffer and sent
158 * to the attached device (if required).
159 *
160 * \note This interrupt must be enabled and cleared on *each* pipe which requires it (after the pipe
161 * is selected), and will fire the common pipe interrupt vector.
162 *
163 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
164 */
165 #define PIPE_INT_OUT UPIENX, (1 << TXOUTE), UPINTX, (1 << TXOUTI)
166
167 /** Interrupt definition for the pipe SETUP bank ready interrupt (for CONTROL type pipes). Should be
168 * used with the USB_INT_* macros located in USBInterrupt.h.
169 *
170 * This interrupt will fire if enabled on an CONTROL type pipe when the pipe is ready for a new
171 * control request.
172 *
173 * \note This interrupt must be enabled and cleared on *each* pipe which requires it (after the pipe
174 * is selected), and will fire the common pipe interrupt vector.
175 *
176 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
177 */
178 #define PIPE_INT_SETUP UPIENX, (1 << TXSTPE) , UPINTX, (1 << TXSTPI)
179
180 /** Interrupt definition for the pipe error interrupt. Should be used with the USB_INT_* macros
181 * located in USBInterrupt.h.
182 *
183 * This interrupt will fire if enabled on a particular pipe if an error occurs on that pipe, such
184 * as a CRC mismatch error.
185 *
186 * \note This interrupt must be enabled and cleared on *each* pipe which requires it (after the pipe
187 * is selected), and will fire the common pipe interrupt vector.
188 *
189 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
190 *
191 * \see Pipe_GetErrorFlags() for more information on the pipe errors.
192 */
193 #define PIPE_INT_ERROR UPIENX, (1 << PERRE), UPINTX, (1 << PERRI)
194
195 /** Interrupt definition for the pipe NAK received interrupt. Should be used with the USB_INT_* macros
196 * located in USBInterrupt.h.
197 *
198 * This interrupt will fire if enabled on a particular pipe if an attached device returns a NAK in
199 * response to a sent packet.
200 *
201 * \note This interrupt must be enabled and cleared on *each* pipe which requires it (after the pipe
202 * is selected), and will fire the common pipe interrupt vector.
203 *
204 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
205 *
206 * \see Pipe_IsNAKReceived() for more information on pipe NAKs.
207 */
208 #define PIPE_INT_NAK UPIENX, (1 << NAKEDE), UPINTX, (1 << NAKEDI)
209
210 /** Interrupt definition for the pipe STALL received interrupt. Should be used with the USB_INT_* macros
211 * located in USBInterrupt.h.
212 *
213 * This interrupt will fire if enabled on a particular pipe if an attached device returns a STALL on the
214 * currently selected pipe. This will also fire if the pipe is an isochronous pipe and a CRC error occurs.
215 *
216 * \note This interrupt must be enabled and cleared on *each* pipe which requires it (after the pipe
217 * is selected), and will fire the common pipe interrupt vector.
218 *
219 * \see ENDPOINT_PIPE_vect for more information on the common pipe and endpoint interrupt vector.
220 */
221 #define PIPE_INT_STALL UPIENX, (1 << RXSTALLE), UPINTX, (1 << RXSTALLI)
222
223 /** Indicates the number of bytes currently stored in the current pipe's selected bank. */
224 #define Pipe_BytesInPipe() UPBCX
225
226 /** Resets the desired pipe, including the pipe banks and flags. */
227 #define Pipe_ResetPipe(pipenum) MACROS{ UPRST = (1 << pipenum); UPRST = 0; }MACROE
228
229 /** Selects the given pipe number. Any pipe operations which do not require the pipe number to be
230 * indicated will operate on the currently selected pipe.
231 */
232 #define Pipe_SelectPipe(pipenum) MACROS{ UPNUM = pipenum; }MACROE
233
234 /** Returns the pipe address of the currently selected pipe. This is typically used to save the
235 * currently selected pipe number so that it can be restored after another pipe has been manipulated.
236 */
237 #define Pipe_GetCurrentPipe() (UPNUM & PIPE_PIPENUM_MASK)
238
239 /** Enables the currently selected pipe so that data can be sent and received through it to and from
240 * an attached device.
241 *
242 * \note Pipes must first be configured properly rather than just being enabled via the
243 * Pipe_ConfigurePipe() macro, which calls Pipe_EnablePipe() automatically.
244 */
245 #define Pipe_EnablePipe() MACROS{ UPCONX |= (1 << PEN); }MACROE
246
247 /** Disables the currently selected pipe so that data cannot be sent and received through it to and
248 * from an attached device.
249 */
250 #define Pipe_DisablePipe() MACROS{ UPCONX &= ~(1 << PEN); }MACROE
251
252 /** Returns true if the currently selected pipe is enabled, false otherwise. */
253 #define Pipe_IsEnabled() ((UPCONX & (1 << PEN)) ? true : false)
254
255 /** Sets the token for the currently selected endpoint to one of the tokens specified by the PIPE_TOKEN_*
256 * masks. This should only be used on CONTROL type endpoints, to allow for bidirectional transfer of
257 * data during control requests.
258 */
259 #define Pipe_SetToken(token) MACROS{ UPCFG0X = ((UPCFG0X & ~PIPE_TOKEN_MASK) | token); }MACROE
260
261 /** Configures the currently selected pipe to allow for an unlimited number of IN requests. */
262 #define Pipe_SetInfiniteINRequests() MACROS{ UPCONX |= (1 << INMODE); }MACROE
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 #define Pipe_SetFiniteINRequests(n) MACROS{ UPCONX &= ~(1 << INMODE); UPINRQX = n; }MACROE
268
269 /** Returns true if the currently selected pipe is configured, false otherwise. */
270 #define Pipe_IsConfigured() ((UPSTAX & (1 << CFGOK)) ? true : false)
271
272 /** Sets the period between interrupts for an INTERRUPT type pipe to a specified number of milliseconds. */
273 #define Pipe_SetInterruptPeriod(ms) MACROS{ UPCFG2X = ms; }MACROE
274
275 /** Returns a mask indicating which pipe's interrupt periods have elapsed, indicating that the pipe should
276 * be serviced.
277 */
278 #define Pipe_GetPipeInterrupts() UPINT
279
280 /** Clears the interrupt flag for the specified pipe number. */
281 #define Pipe_ClearPipeInterrupt(n) MACROS{ UPINT &= ~(1 << n); }MACROE
282
283 /** Returns true if the specified pipe's interrupt period has elapsed, false otherwise. */
284 #define Pipe_HasPipeInterrupted(n) ((UPINT & (1 << n)) ? true : false)
285
286 /** Clears the pipe bank, and switches to the alternate bank if the currently selected pipe is
287 * dual-banked. When cleared, this either frees the bank up for the next packet from the host
288 * (if the endpoint is of the OUT direction) or sends the packet contents to the host (if the
289 * pipe is of the IN direction).
290 */
291 #define Pipe_ClearCurrentBank() MACROS{ UPINTX &= ~(1 << FIFOCON); }MACROE
292
293 /** Unfreezes the pipe, allowing it to communicate with an attached device. */
294 #define Pipe_Unfreeze() MACROS{ UPCONX &= ~(1 << PFREEZE); }MACROE
295
296 /** Freezes the pipe, preventing it from communicating with an attached device. */
297 #define Pipe_Freeze() MACROS{ UPCONX |= (1 << PFREEZE); }MACROE
298
299 /** Clears the master pipe error flag. */
300 #define Pipe_ClearError() MACROS{ UPINTX &= ~(1 << PERRI); }MACROE
301
302 /** Returns true if the master pipe error flag is set for the currently selected pipe, indicating that
303 * some sort of hardware error has occurred on the pipe.
304 *
305 * \see Pipe_GetErrorFlags() macro for information on retreiving the exact error flag.
306 */
307 #define Pipe_IsError() ((UPINTX & (1 << PERRI)) ? true : false)
308
309 /** Clears all the currently selected pipe's hardware error flags, but does not clear the master error
310 * flag for the pipe. */
311 #define Pipe_ClearErrorFlags() MACROS{ UPERRX = 0; }MACROE
312
313 /** Returns a mask of the hardware error flags which have occured on the currently selected pipe. This
314 * value can then be masked against the PIPE_ERRORFLAG_* masks to determine what error has occurred.
315 */
316 #define Pipe_GetErrorFlags() UPERRX
317
318 /** Returns true if the currently selected pipe may be read from (if data is waiting in the pipe
319 * bank and the pipe is an IN direction, or if the bank is not yet full if the pipe is an OUT
320 * direction). This function will return false if an error has occured in the pipe, or if the pipe
321 * is an IN direction and no packet has been received, or if the pipe is an OUT direction and the
322 * pipe bank is full.
323 */
324 #define Pipe_ReadWriteAllowed() ((UPINTX & (1 << RWAL)) ? true : false)
325
326 /** Clears the flag indicating that a SETUP request has been sent to the attached device from the
327 * currently selected CONTROL type pipe.
328 */
329 #define Pipe_ClearSetupSent() MACROS{ UPINTX &= ~(1 << TXSTPI); }MACROE
330
331 /** Returns true if no SETUP request is currently being sent to the attached device, false otherwise. */
332 #define Pipe_IsSetupSent() ((UPINTX & (1 << TXSTPI)) ? true : false)
333
334 /** Returns true if the currently selected pipe has been stalled by the attached device, false otherwise. */
335 #define Pipe_IsStalled() ((UPINTX & (1 << RXSTALLI)) ? true : false)
336
337 /** Clears the stall condition on the currently selected pipe. */
338 #define Pipe_ClearStall() MACROS{ UPINTX &= ~(1 << RXSTALLI); }MACROE
339
340 /** Returns true if an IN request has been received on the currently selected CONTROL type pipe, false
341 * otherwise.
342 */
343 #define Pipe_IsSetupINReceived() ((UPINTX & (1 << RXINI)) ? true : false)
344
345 /** Returns true if the currently selected CONTROL type pipe is ready to send an OUT request, false
346 * otherwise.
347 */
348 #define Pipe_IsSetupOUTReady() ((UPINTX & (1 << TXOUTI)) ? true : false)
349
350 /** Acknowledges the reception of a setup IN request from the attached device on the currently selected
351 * CONTROL type endpoint, allowing for the transmission of a setup OUT packet, or the reception of
352 * another setup IN packet.
353 */
354 #define Pipe_ClearSetupIN() MACROS{ UPINTX &= ~(1 << RXINI); UPINTX &= ~(1 << FIFOCON); }MACROE
355
356 /** Sends the currently selected CONTROL type pipe's contents to the device as a setup OUT packet. */
357 #define Pipe_ClearSetupOUT() MACROS{ UPINTX &= ~(1 << TXOUTI); UPINTX &= ~(1 << FIFOCON); }MACROE
358
359 /** Returns true if the device sent a NAK (Negative Acknowledge) in response to the last sent packet on
360 * the currently selected pipe. This ocurrs when the host sends a packet to the device, but the device
361 * is not currently ready to handle the packet (i.e. its endpoint banks are full). Once a NAK has been
362 * received, it must be cleard using Pipe_ClearNAKReceived() before the previous (or any other) packet
363 * can be re-sent.
364 */
365 #define Pipe_IsNAKReceived() ((UPINTX & (1 << NAKEDI)) ? true : false)
366
367 /** Clears the NAK condition on the currently selected pipe.
368 *
369 * \see Pipe_IsNAKReceived() for more details.
370 */
371 #define Pipe_ClearNAKReceived() MACROS{ UPINTX &= ~(1 << NAKEDI); }MACROE
372
373 /* Enums: */
374 /** Enum for the possible error return codes of the Pipe_WaitUntilReady function */
375 enum Pipe_WaitUntilReady_ErrorCodes_t
376 {
377 PIPE_READYWAIT_NoError = 0, /**< Pipe ready for next packet, no error */
378 PIPE_READYWAIT_PipeStalled = 1, /**< The device stalled the pipe while waiting. */
379 PIPE_READYWAIT_DeviceDisconnected = 2, /**< Device was disconnected from the host while waiting. */
380 PIPE_READYWAIT_Timeout = 3, /**< The device failed to accept or send the next packet
381 * within the software timeout period set by the
382 * USB_STREAM_TIMEOUT_MS macro.
383 */
384 };
385
386 /** Enum for the possible error return codes of the Pipe_*_Stream_* functions. */
387 enum Pipe_Stream_RW_ErrorCodes_t
388 {
389 PIPE_RWSTREAM_ERROR_NoError = 0, /**< Command completed successfully, no error. */
390 PIPE_RWSTREAM_ERROR_PipeStalled = 1, /**< The device stalled the pipe during the transfer. */
391 PIPE_RWSTREAM_ERROR_DeviceDisconnected = 2, /**< Device was disconnected from the host during
392 * the transfer.
393 */
394 PIPE_RWSTREAM_ERROR_Timeout = 3, /**< The device failed to accept or send the next packet
395 * within the software timeout period set by the
396 * USB_STREAM_TIMEOUT_MS macro.
397 */
398 PIPE_RWSTREAM_ERROR_CallbackAborted = 4, /**< Indicates that the stream's callback function aborted
399 * the transfer early.
400 */
401 };
402
403 /* Inline Functions: */
404 /** Reads one byte from the currently selected pipe's bank, for OUT direction pipes. */
405 static inline uint8_t Pipe_Read_Byte(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
406 static inline uint8_t Pipe_Read_Byte(void)
407 {
408 return UPDATX;
409 }
410
411 /** Writes one byte from the currently selected pipe's bank, for IN direction pipes. */
412 static inline void Pipe_Write_Byte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
413 static inline void Pipe_Write_Byte(const uint8_t Byte)
414 {
415 UPDATX = Byte;
416 }
417
418 /** Discards one byte from the currently selected pipe's bank, for OUT direction pipes. */
419 static inline void Pipe_Discard_Byte(void) ATTR_ALWAYS_INLINE;
420 static inline void Pipe_Discard_Byte(void)
421 {
422 uint8_t Dummy;
423
424 Dummy = UPDATX;
425 }
426
427 /** Reads two bytes from the currently selected pipe's bank in little endian format, for OUT
428 * direction pipes.
429 */
430 static inline uint16_t Pipe_Read_Word_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
431 static inline uint16_t Pipe_Read_Word_LE(void)
432 {
433 uint16_t Data;
434
435 Data = UPDATX;
436 Data |= (((uint16_t)UPDATX) << 8);
437
438 return Data;
439 }
440
441 /** Reads two bytes from the currently selected pipe's bank in big endian format, for OUT
442 * direction pipes.
443 */
444 static inline uint16_t Pipe_Read_Word_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
445 static inline uint16_t Pipe_Read_Word_BE(void)
446 {
447 uint16_t Data;
448
449 Data = (((uint16_t)UPDATX) << 8);
450 Data |= UPDATX;
451
452 return Data;
453 }
454
455 /** Writes two bytes to the currently selected pipe's bank in little endian format, for IN
456 * direction pipes.
457 */
458 static inline void Pipe_Write_Word_LE(const uint16_t Word) ATTR_ALWAYS_INLINE;
459 static inline void Pipe_Write_Word_LE(const uint16_t Word)
460 {
461 UPDATX = (Word & 0xFF);
462 UPDATX = (Word >> 8);
463 }
464
465 /** Writes two bytes to the currently selected pipe's bank in big endian format, for IN
466 * direction pipes.
467 */
468 static inline void Pipe_Write_Word_BE(const uint16_t Word) ATTR_ALWAYS_INLINE;
469 static inline void Pipe_Write_Word_BE(const uint16_t Word)
470 {
471 UPDATX = (Word >> 8);
472 UPDATX = (Word & 0xFF);
473 }
474
475 /** Discards two bytes from the currently selected pipe's bank, for OUT direction pipes. */
476 static inline void Pipe_Ignore_Word(void) ATTR_ALWAYS_INLINE;
477 static inline void Pipe_Ignore_Word(void)
478 {
479 uint8_t Dummy;
480
481 Dummy = UPDATX;
482 Dummy = UPDATX;
483 }
484
485 /** Reads four bytes from the currently selected pipe's bank in little endian format, for OUT
486 * direction pipes.
487 */
488 static inline uint32_t Pipe_Read_DWord_LE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
489 static inline uint32_t Pipe_Read_DWord_LE(void)
490 {
491 union
492 {
493 uint32_t DWord;
494 uint8_t Bytes[4];
495 } Data;
496
497 Data.Bytes[0] = UPDATX;
498 Data.Bytes[1] = UPDATX;
499 Data.Bytes[2] = UPDATX;
500 Data.Bytes[3] = UPDATX;
501
502 return Data.DWord;
503 }
504
505 /** Reads four bytes from the currently selected pipe's bank in big endian format, for OUT
506 * direction pipes.
507 */
508 static inline uint32_t Pipe_Read_DWord_BE(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
509 static inline uint32_t Pipe_Read_DWord_BE(void)
510 {
511 union
512 {
513 uint32_t DWord;
514 uint8_t Bytes[4];
515 } Data;
516
517 Data.Bytes[3] = UPDATX;
518 Data.Bytes[2] = UPDATX;
519 Data.Bytes[1] = UPDATX;
520 Data.Bytes[0] = UPDATX;
521
522 return Data.DWord;
523 }
524
525 /** Writes four bytes to the currently selected pipe's bank in little endian format, for IN
526 * direction pipes.
527 */
528 static inline void Pipe_Write_DWord_LE(const uint32_t DWord) ATTR_ALWAYS_INLINE;
529 static inline void Pipe_Write_DWord_LE(const uint32_t DWord)
530 {
531 Pipe_Write_Word_LE(DWord);
532 Pipe_Write_Word_LE(DWord >> 16);
533 }
534
535 /** Writes four bytes to the currently selected pipe's bank in big endian format, for IN
536 * direction pipes.
537 */
538 static inline void Pipe_Write_DWord_BE(const uint32_t DWord) ATTR_ALWAYS_INLINE;
539 static inline void Pipe_Write_DWord_BE(const uint32_t DWord)
540 {
541 Pipe_Write_Word_BE(DWord >> 16);
542 Pipe_Write_Word_BE(DWord);
543 }
544
545 /** Discards four bytes from the currently selected pipe's bank, for OUT direction pipes. */
546 static inline void Pipe_Ignore_DWord(void) ATTR_ALWAYS_INLINE;
547 static inline void Pipe_Ignore_DWord(void)
548 {
549 uint8_t Dummy;
550
551 Dummy = UPDATX;
552 Dummy = UPDATX;
553 Dummy = UPDATX;
554 Dummy = UPDATX;
555 }
556
557 /* External Variables: */
558 /** Global indicating the maximum packet size of the default control pipe located at address
559 * 0 in the device. This value is set to the value indicated in the attached device's device
560 * descriptor once the USB interface is initialized into host mode and a device is attached
561 * to the USB bus.
562 *
563 * \note This variable should be treated as read-only in the user application, and never manually
564 * changed in value.
565 */
566 extern uint8_t USB_ControlPipeSize;
567
568 /* Function Prototypes: */
569 /** Configures the specified pipe number with the given pipe type, token, target endpoint number in the
570 * attached device, bank size and banking mode. Pipes should be allocated in ascending order by their
571 * address in the device (i.e. pipe 1 should be configured before pipe 2 and so on).
572 *
573 * The pipe type may be one of the EP_TYPE_* macros listed in LowLevel.h, the token may be one of the
574 * PIPE_TOKEN_* masks.
575 *
576 * The bank size must indicate the maximum packet size that the pipe can handle. Different pipe
577 * numbers can handle different maximum packet sizes - refer to the chosen USB AVR's datasheet to
578 * determine the maximum bank size for each pipe.
579 *
580 * The banking mode may be either PIPE_BANK_SINGLE or PIPE_BANK_DOUBLE.
581 *
582 * A newly configured pipe is frozen by default, and must be unfrozen before use via the Pipe_Unfreeze() macro.
583 *
584 * \note This routine will select the specified pipe, and the pipe will remain selected once the
585 * routine completes regardless of if the pipe configuration succeeds.
586 *
587 * \return Boolean true if the configuration is successful, false otherwise
588 */
589 bool Pipe_ConfigurePipe(const uint8_t Number, const uint8_t Type, const uint8_t Token, const uint8_t EndpointNumber,
590 const uint16_t Size, const uint8_t Banks);
591
592 /** Spinloops until the currently selected non-control pipe is ready for the next packed of data
593 * to be read or written to it.
594 *
595 * \note This routine should not be called on CONTROL type pipes.
596 *
597 * \return A value from the Pipe_WaitUntilReady_ErrorCodes_t enum.
598 */
599 uint8_t Pipe_WaitUntilReady(void);
600
601 /** Writes the given number of bytes to the pipe from the given buffer in little endian,
602 * sending full packets to the device as needed. The last packet filled is not automatically sent;
603 * the user is responsible for manually sending the last written packet to the host via the
604 * Pipe_ClearCurrentBank() macro. Between each USB packet, the given stream callback function is
605 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
606 *
607 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
608 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
609 * and this function has the Callback parameter ommitted.
610 *
611 * \param Buffer Pointer to the source data buffer to read from.
612 * \param Length Number of bytes to read for the currently selected pipe into the buffer.
613 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
614 *
615 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
616 */
617 uint8_t Pipe_Write_Stream_LE(const void* Buffer, uint16_t Length
618 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
619 , uint8_t (* const Callback)(void)
620 #endif
621 ) ATTR_NON_NULL_PTR_ARG(1);
622
623 /** Writes the given number of bytes to the pipe from the given buffer in big endian,
624 * sending full packets to the device as needed. The last packet filled is not automatically sent;
625 * the user is responsible for manually sending the last written packet to the host via the
626 * Pipe_ClearCurrentBank() macro. Between each USB packet, the given stream callback function is
627 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
628 *
629 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
630 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
631 * and this function has the Callback parameter ommitted.
632 *
633 * \param Buffer Pointer to the source data buffer to read from.
634 * \param Length Number of bytes to read for the currently selected pipe into the buffer.
635 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
636 *
637 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
638 */
639 uint8_t Pipe_Write_Stream_BE(const void* Buffer, uint16_t Length
640 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
641 , uint8_t (* const Callback)(void)
642 #endif
643 ) ATTR_NON_NULL_PTR_ARG(1);
644
645 /** Reads and discards the given number of bytes from the pipe, discarding fully read packets from the host
646 * as needed. The last packet is not automatically discarded once the remaining bytes has been read; the
647 * user is responsible for manually discarding the last packet from the host via the Pipe_ClearCurrentBank() macro.
648 * Between each USB packet, the given stream callback function is executed repeatedly until the next packet is ready,
649 * allowing for early aborts of stream transfers.
650 *
651 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
652 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
653 * and this function has the Callback parameter ommitted.
654 *
655 * \param Length Number of bytes to send via the currently selected pipe.
656 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
657 *
658 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
659 */
660 uint8_t Pipe_Discard_Stream(uint16_t Length
661 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
662 , uint8_t (* const Callback)(void)
663 #endif
664 );
665
666 /** Reads the given number of bytes from the pipe into the given buffer in little endian,
667 * sending full packets to the device as needed. The last packet filled is not automatically sent;
668 * the user is responsible for manually sending the last written packet to the host via the
669 * Pipe_ClearCurrentBank() macro. Between each USB packet, the given stream callback function is
670 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
671 *
672 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
673 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
674 * and this function has the Callback parameter ommitted.
675 *
676 * \param Buffer Pointer to the source data buffer to write to.
677 * \param Length Number of bytes to read for the currently selected pipe to read from.
678 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
679 *
680 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
681 */
682 uint8_t Pipe_Read_Stream_LE(void* Buffer, uint16_t Length
683 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
684 , uint8_t (* const Callback)(void)
685 #endif
686 ) ATTR_NON_NULL_PTR_ARG(1);
687
688 /** Reads the given number of bytes from the pipe into the given buffer in big endian,
689 * sending full packets to the device as needed. The last packet filled is not automatically sent;
690 * the user is responsible for manually sending the last written packet to the host via the
691 * Pipe_ClearCurrentBank() macro. Between each USB packet, the given stream callback function is
692 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
693 *
694 * The callback routine should be created using the STREAM_CALLBACK() macro. If the token
695 * NO_STREAM_CALLBACKS is passed via the -D option to the compiler, stream callbacks are disabled
696 * and this function has the Callback parameter ommitted.
697 *
698 * \param Buffer Pointer to the source data buffer to write to.
699 * \param Length Number of bytes to read for the currently selected pipe to read from.
700 * \param Callback Name of a callback routine to call between sucessive USB packet transfers, NULL if no callback
701 *
702 * \return A value from the Pipe_Stream_RW_ErrorCodes_t enum.
703 */
704 uint8_t Pipe_Read_Stream_BE(void* Buffer, uint16_t Length
705 #if !defined(NO_STREAM_CALLBACKS) || defined(__DOXYGEN__)
706 , uint8_t (* const Callback)(void)
707 #endif
708 ) ATTR_NON_NULL_PTR_ARG(1);
709
710 /* Function Aliases: */
711 /** Alias for Pipe_Discard_Byte().
712 */
713 #define Pipe_Ignore_Byte() Pipe_Discard_Byte()
714
715 /** Alias for Pipe_Discard_Word().
716 */
717 #define Pipe_Ignore_Word() Pipe_Discard_Word()
718
719 /** Alias for Pipe_Discard_DWord().
720 */
721 #define Pipe_Ignore_DWord() Pipe_Discard_DWord()
722
723 /** Alias for Pipe_Read_Word_LE(). By default USB transfers use little endian format, thus
724 * the command with no endianness specifier indicates little endian mode.
725 */
726 #define Pipe_Read_Word() Pipe_Read_Word_LE()
727
728 /** Alias for Pipe_Write_Word_LE(). By default USB transfers use little endian format, thus
729 * the command with no endianness specifier indicates little endian mode.
730 */
731 #define Pipe_Write_Word(Word) Pipe_Write_Word_LE(Word)
732
733 /** Alias for Pipe_Read_DWord_LE(). By default USB transfers use little endian format, thus
734 * the command with no endianness specifier indicates little endian mode.
735 */
736 #define Pipe_Read_DWord() Pipe_Read_DWord_LE()
737
738 /** Alias for Pipe_Write_DWord_LE(). By default USB transfers use little endian format, thus
739 * the command with no endianness specifier indicates little endian mode.
740 */
741 #define Pipe_Write_DWord(DWord) Pipe_Write_DWord_LE(DWord)
742
743 /** Alias for Pipe_Read_Stream_LE(). By default USB transfers use little endian format, thus
744 * the command with no endianness specifier indicates little endian mode.
745 */
746 #if !defined(NO_STREAM_CALLBACKS)
747 #define Pipe_Read_Stream(Buffer, Length, Callback) Pipe_Read_Stream_LE(Buffer, Length, Callback)
748 #else
749 #define Pipe_Read_Stream(Buffer, Length) Pipe_Read_Stream_LE(Buffer, Length)
750 #endif
751
752 /** Alias for Pipe_Write_Stream_LE(). By default USB transfers use little endian format, thus
753 * the command with no endianness specifier indicates little endian mode.
754 */
755 #if !defined(NO_STREAM_CALLBACKS)
756 #define Pipe_Write_Stream(Buffer, Length, Callback) Pipe_Read_Stream_LE(Buffer, Length, Callback)
757 #else
758 #define Pipe_Write_Stream(Buffer, Length) Pipe_Read_Stream_LE(Buffer, Length)
759 #endif
760
761 /* Private Interface - For use in library only: */
762 #if !defined(__DOXYGEN__)
763 /* Macros: */
764 #define PIPE_TOKEN_MASK (0x03 << PTOKEN0)
765
766 #define Pipe_AllocateMemory() MACROS{ UPCFG1X |= (1 << ALLOC); }MACROE
767 #define Pipe_DeallocateMemory() MACROS{ UPCFG1X &= ~(1 << ALLOC); }MACROE
768
769 /* Function Prototypes: */
770 void Pipe_ClearPipes(void);
771
772 /* Inline Functions: */
773 static inline uint8_t Pipe_BytesToEPSizeMask(uint16_t Bytes) ATTR_WARN_UNUSED_RESULT ATTR_CONST ATTR_ALWAYS_INLINE;
774 static inline uint8_t Pipe_BytesToEPSizeMask(uint16_t Bytes)
775 {
776 if (Bytes <= 8)
777 return (0 << EPSIZE0);
778 else if (Bytes <= 16)
779 return (1 << EPSIZE0);
780 else if (Bytes <= 32)
781 return (2 << EPSIZE0);
782 else if (Bytes <= 64)
783 return (3 << EPSIZE0);
784 else if (Bytes <= 128)
785 return (4 << EPSIZE0);
786 else
787 return (5 << EPSIZE0);
788 };
789
790 #endif
791
792 /* Disable C linkage for C++ Compilers: */
793 #if defined(__cplusplus)
794 }
795 #endif
796
797 #endif