3 Copyright (C) Dean Camera, 2011.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
32 * \brief USB host pipe stream function definitions.
34 * This file contains structures, function prototypes and macros related to the sending and receiving of
35 * arbitrary data streams through the device's data pipes when the library is initialized in USB host mode.
37 * \note This file should not be included directly. It is automatically included as needed by the USB driver
38 * dispatch header located in LUFA/Drivers/USB/USB.h.
41 /** \ingroup Group_PipeRW
42 * \defgroup Group_PipeStreamRW Read/Write of Multi-Byte Streams
44 * Functions, macros, variables, enums and types related to data reading and writing of data streams from
50 #ifndef __PIPE_STREAM_H__
51 #define __PIPE_STREAM_H__
54 #include <avr/pgmspace.h>
55 #include <avr/eeprom.h>
58 #include "../../../Common/Common.h"
61 /* Enable C linkage for C++ Compilers: */
62 #if defined(__cplusplus)
66 /* Preprocessor Checks: */
67 #if !defined(__INCLUDE_FROM_USB_DRIVER)
68 #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
71 /* Public Interface - May be used in end-application: */
73 /** Enum for the possible error return codes of the Pipe_*_Stream_* functions. */
74 enum Pipe_Stream_RW_ErrorCodes_t
76 PIPE_RWSTREAM_NoError
= 0, /**< Command completed successfully, no error. */
77 PIPE_RWSTREAM_PipeStalled
= 1, /**< The device stalled the pipe during the transfer. */
78 PIPE_RWSTREAM_DeviceDisconnected
= 2, /**< Device was disconnected from the host during
81 PIPE_RWSTREAM_Timeout
= 3, /**< The device failed to accept or send the next packet
82 * within the software timeout period set by the
83 * \ref USB_STREAM_TIMEOUT_MS macro.
85 PIPE_RWSTREAM_IncompleteTransfer
= 4, /**< Indicates that the pipe bank became full/empty before the
86 * complete contents of the stream could be transferred.
90 /* Function Prototypes: */
92 /** \name Stream functions for null data */
95 /** Reads and discards the given number of bytes from the pipe, discarding fully read packets from the host
96 * as needed. The last packet is not automatically discarded once the remaining bytes has been read; the
97 * user is responsible for manually discarding the last packet from the device via the \ref Pipe_ClearIN() macro.
99 * If the BytesProcessed parameter is \c NULL, the entire stream transfer is attempted at once, failing or
100 * succeeding as a single unit. If the BytesProcessed parameter points to a valid storage location, the transfer
101 * will instead be performed as a series of chunks. Each time the pipe bank becomes empty while there is still data
102 * to process (and after the current packet has been acknowledged) the BytesProcessed location will be updated with
103 * the total number of bytes processed in the stream, and the function will exit with an error code of
104 * \ref PIPE_RWSTREAM_IncompleteTransfer. This allows for any abort checking to be performed in the user code - to
105 * continue the transfer, call the function again with identical parameters and it will resume until the BytesProcessed
106 * value reaches the total transfer length.
108 * <b>Single Stream Transfer Example:</b>
112 * if ((ErrorCode = Pipe_Discard_Stream(512, NULL)) != PIPE_RWSTREAM_NoError)
114 * // Stream failed to complete - check ErrorCode here
118 * <b>Partial Stream Transfers Example:</b>
121 * uint16_t BytesProcessed;
123 * BytesProcessed = 0;
124 * while ((ErrorCode = Pipe_Discard_Stream(512, &BytesProcessed)) == PIPE_RWSTREAM_IncompleteTransfer)
126 * // Stream not yet complete - do other actions here, abort if required
129 * if (ErrorCode != PIPE_RWSTREAM_NoError)
131 * // Stream failed to complete - check ErrorCode here
135 * \note The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
136 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
138 * \param[in] Length Number of bytes to discard via the currently selected pipe.
139 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
140 * updated, \c NULL if the entire stream should be processed at once.
142 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
144 uint8_t Pipe_Discard_Stream(uint16_t Length
,
145 uint16_t* const BytesProcessed
);
147 /** Writes a given number of zeroed bytes to the pipe, sending full pipe packets from the host to the device
148 * as needed. The last packet is not automatically sent once the remaining bytes has been written; the
149 * user is responsible for manually discarding the last packet from the device via the \ref Pipe_ClearOUT() macro.
151 * If the BytesProcessed parameter is \c NULL, the entire stream transfer is attempted at once, failing or
152 * succeeding as a single unit. If the BytesProcessed parameter points to a valid storage location, the transfer
153 * will instead be performed as a series of chunks. Each time the pipe bank becomes full while there is still data
154 * to process (and after the current packet transmission has been initiated) the BytesProcessed location will be
155 * updated with the total number of bytes processed in the stream, and the function will exit with an error code of
156 * \ref PIPE_RWSTREAM_IncompleteTransfer. This allows for any abort checking to be performed in the user code - to
157 * continue the transfer, call the function again with identical parameters and it will resume until the BytesProcessed
158 * value reaches the total transfer length.
160 * <b>Single Stream Transfer Example:</b>
164 * if ((ErrorCode = Pipe_Null_Stream(512, NULL)) != PIPE_RWSTREAM_NoError)
166 * // Stream failed to complete - check ErrorCode here
170 * <b>Partial Stream Transfers Example:</b>
173 * uint16_t BytesProcessed;
175 * BytesProcessed = 0;
176 * while ((ErrorCode = Pipe_Null_Stream(512, &BytesProcessed)) == PIPE_RWSTREAM_IncompleteTransfer)
178 * // Stream not yet complete - do other actions here, abort if required
181 * if (ErrorCode != PIPE_RWSTREAM_NoError)
183 * // Stream failed to complete - check ErrorCode here
187 * \note The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
188 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
190 * \param[in] Length Number of zero bytes to write via the currently selected pipe.
191 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
192 * updated, \c NULL if the entire stream should be processed at once.
194 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
196 uint8_t Pipe_Null_Stream(uint16_t Length
,
197 uint16_t* const BytesProcessed
);
201 /** \name Stream functions for RAM source/destination data */
204 /** Writes the given number of bytes to the pipe from the given buffer in little endian,
205 * sending full packets to the device as needed. The last packet filled is not automatically sent;
206 * the user is responsible for manually sending the last written packet to the host via the
207 * \ref Pipe_ClearOUT() macro. Between each USB packet, the given stream callback function is
208 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
210 * If the BytesProcessed parameter is \c NULL, the entire stream transfer is attempted at once,
211 * failing or succeeding as a single unit. If the BytesProcessed parameter points to a valid
212 * storage location, the transfer will instead be performed as a series of chunks. Each time
213 * the pipe bank becomes full while there is still data to process (and after the current
214 * packet transmission has been initiated) the BytesProcessed location will be updated with the
215 * total number of bytes processed in the stream, and the function will exit with an error code of
216 * \ref PIPE_RWSTREAM_IncompleteTransfer. This allows for any abort checking to be performed
217 * in the user code - to continue the transfer, call the function again with identical parameters
218 * and it will resume until the BytesProcessed value reaches the total transfer length.
220 * <b>Single Stream Transfer Example:</b>
222 * uint8_t DataStream[512];
225 * if ((ErrorCode = Pipe_Write_Stream_LE(DataStream, sizeof(DataStream),
226 * NULL)) != PIPE_RWSTREAM_NoError)
228 * // Stream failed to complete - check ErrorCode here
232 * <b>Partial Stream Transfers Example:</b>
234 * uint8_t DataStream[512];
236 * uint16_t BytesProcessed;
238 * BytesProcessed = 0;
239 * while ((ErrorCode = Pipe_Write_Stream_LE(DataStream, sizeof(DataStream),
240 * &BytesProcessed)) == PIPE_RWSTREAM_IncompleteTransfer)
242 * // Stream not yet complete - do other actions here, abort if required
245 * if (ErrorCode != PIPE_RWSTREAM_NoError)
247 * // Stream failed to complete - check ErrorCode here
251 * \note The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
252 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
254 * \param[in] Buffer Pointer to the source data buffer to read from.
255 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
256 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
257 * updated, \c NULL if the entire stream should be written at once.
259 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
261 uint8_t Pipe_Write_Stream_LE(const void* const Buffer
,
263 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
265 /** Writes the given number of bytes to the pipe from the given buffer in big endian,
266 * sending full packets to the device as needed. The last packet filled is not automatically sent;
267 * the user is responsible for manually sending the last written packet to the host via the
268 * \ref Pipe_ClearOUT() macro. Between each USB packet, the given stream callback function is
269 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
271 * \note The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
272 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
274 * \param[in] Buffer Pointer to the source data buffer to read from.
275 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
276 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
277 * updated, \c NULL if the entire stream should be written at once.
279 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
281 uint8_t Pipe_Write_Stream_BE(const void* const Buffer
,
283 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
285 /** Reads the given number of bytes from the pipe into the given buffer in little endian,
286 * sending full packets to the device as needed. The last packet filled is not automatically sent;
287 * the user is responsible for manually sending the last written packet to the host via the
288 * \ref Pipe_ClearIN() macro. Between each USB packet, the given stream callback function is
289 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
291 * If the BytesProcessed parameter is \c NULL, the entire stream transfer is attempted at once,
292 * failing or succeeding as a single unit. If the BytesProcessed parameter points to a valid
293 * storage location, the transfer will instead be performed as a series of chunks. Each time
294 * the pipe bank becomes empty while there is still data to process (and after the current
295 * packet has been acknowledged) the BytesProcessed location will be updated with the total number
296 * of bytes processed in the stream, and the function will exit with an error code of
297 * \ref PIPE_RWSTREAM_IncompleteTransfer. This allows for any abort checking to be performed
298 * in the user code - to continue the transfer, call the function again with identical parameters
299 * and it will resume until the BytesProcessed value reaches the total transfer length.
301 * <b>Single Stream Transfer Example:</b>
303 * uint8_t DataStream[512];
306 * if ((ErrorCode = Pipe_Read_Stream_LE(DataStream, sizeof(DataStream),
307 * NULL)) != PIPE_RWSTREAM_NoError)
309 * // Stream failed to complete - check ErrorCode here
313 * <b>Partial Stream Transfers Example:</b>
315 * uint8_t DataStream[512];
317 * uint16_t BytesProcessed;
319 * BytesProcessed = 0;
320 * while ((ErrorCode = Pipe_Read_Stream_LE(DataStream, sizeof(DataStream),
321 * &BytesProcessed)) == PIPE_RWSTREAM_IncompleteTransfer)
323 * // Stream not yet complete - do other actions here, abort if required
326 * if (ErrorCode != PIPE_RWSTREAM_NoError)
328 * // Stream failed to complete - check ErrorCode here
332 * \note The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
333 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
335 * \param[out] Buffer Pointer to the source data buffer to write to.
336 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
337 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
338 * updated, \c NULL if the entire stream should be read at once.
340 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
342 uint8_t Pipe_Read_Stream_LE(void* const Buffer
,
344 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
346 /** Reads the given number of bytes from the pipe into the given buffer in big endian,
347 * sending full packets to the device as needed. The last packet filled is not automatically sent;
348 * the user is responsible for manually sending the last written packet to the host via the
349 * \ref Pipe_ClearIN() macro. Between each USB packet, the given stream callback function is
350 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
352 * \note The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
353 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
355 * \param[out] Buffer Pointer to the source data buffer to write to.
356 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
357 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
358 * updated, \c NULL if the entire stream should be read at once.
360 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
362 uint8_t Pipe_Read_Stream_BE(void* const Buffer
,
364 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
367 /** \name Stream functions for EEPROM source/destination data */
370 /** EEPROM buffer source version of \ref Pipe_Write_Stream_LE().
372 * \param[in] Buffer Pointer to the source data buffer to read from.
373 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
374 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
375 * updated, \c NULL if the entire stream should be written at once.
377 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
379 uint8_t Pipe_Write_EStream_LE(const void* const Buffer
,
381 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
383 /** EEPROM buffer source version of \ref Pipe_Write_Stream_BE().
385 * \param[in] Buffer Pointer to the source data buffer to read from.
386 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
387 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
388 * updated, \c NULL if the entire stream should be written at once.
390 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
392 uint8_t Pipe_Write_EStream_BE(const void* const Buffer
,
394 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
396 /** EEPROM buffer source version of \ref Pipe_Read_Stream_LE().
398 * \param[out] Buffer Pointer to the source data buffer to write to.
399 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
400 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
401 * updated, \c NULL if the entire stream should be read at once.
403 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
405 uint8_t Pipe_Read_EStream_LE(void* const Buffer
,
407 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
409 /** EEPROM buffer source version of \ref Pipe_Read_Stream_BE().
411 * \param[out] Buffer Pointer to the source data buffer to write to.
412 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
413 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
414 * updated, \c NULL if the entire stream should be read at once.
416 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
418 uint8_t Pipe_Read_EStream_BE(void* const Buffer
,
420 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
423 /** \name Stream functions for PROGMEM source/destination data */
426 /** FLASH buffer source version of \ref Pipe_Write_Stream_LE().
428 * \pre The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
430 * \param[in] Buffer Pointer to the source data buffer to read from.
431 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
432 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
433 * updated, \c NULL if the entire stream should be written at once.
435 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
437 uint8_t Pipe_Write_PStream_LE(const void* const Buffer
,
439 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
441 /** FLASH buffer source version of \ref Pipe_Write_Stream_BE().
443 * \pre The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
445 * \param[in] Buffer Pointer to the source data buffer to read from.
446 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
447 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
448 * updated, \c NULL if the entire stream should be written at once.
450 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
452 uint8_t Pipe_Write_PStream_BE(const void* const Buffer
,
454 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
457 /* Disable C linkage for C++ Compilers: */
458 #if defined(__cplusplus)