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 Pipe data stream transmission and reception management.
33 * \copydetails Group_PipeStreamRW
35 * \note This file should not be included directly. It is automatically included as needed by the USB driver
36 * dispatch header located in LUFA/Drivers/USB/USB.h.
39 /** \ingroup Group_PipeRW
40 * \defgroup Group_PipeStreamRW Read/Write of Multi-Byte Streams
41 * \brief Pipe data stream transmission and reception management.
43 * Functions, macros, variables, enums and types related to data reading and writing of data streams from
49 #ifndef __PIPE_STREAM_H__
50 #define __PIPE_STREAM_H__
53 #include "../../../Common/Common.h"
56 /* Enable C linkage for C++ Compilers: */
57 #if defined(__cplusplus)
61 /* Preprocessor Checks: */
62 #if !defined(__INCLUDE_FROM_USB_DRIVER)
63 #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
66 /* Public Interface - May be used in end-application: */
68 /** Enum for the possible error return codes of the Pipe_*_Stream_* functions. */
69 enum Pipe_Stream_RW_ErrorCodes_t
71 PIPE_RWSTREAM_NoError
= 0, /**< Command completed successfully, no error. */
72 PIPE_RWSTREAM_PipeStalled
= 1, /**< The device stalled the pipe during the transfer. */
73 PIPE_RWSTREAM_DeviceDisconnected
= 2, /**< Device was disconnected from the host during
76 PIPE_RWSTREAM_Timeout
= 3, /**< The device failed to accept or send the next packet
77 * within the software timeout period set by the
78 * \ref USB_STREAM_TIMEOUT_MS macro.
80 PIPE_RWSTREAM_IncompleteTransfer
= 4, /**< Indicates that the pipe bank became full/empty before the
81 * complete contents of the stream could be transferred.
85 /* Function Prototypes: */
87 /** \name Stream functions for null data */
90 /** Reads and discards the given number of bytes from the pipe, discarding fully read packets from the host
91 * as needed. The last packet is not automatically discarded once the remaining bytes has been read; the
92 * user is responsible for manually discarding the last packet from the device via the \ref Pipe_ClearIN() macro.
94 * If the BytesProcessed parameter is \c NULL, the entire stream transfer is attempted at once, failing or
95 * succeeding as a single unit. If the BytesProcessed parameter points to a valid storage location, the transfer
96 * will instead be performed as a series of chunks. Each time the pipe bank becomes empty while there is still data
97 * to process (and after the current packet has been acknowledged) the BytesProcessed location will be updated with
98 * the total number of bytes processed in the stream, and the function will exit with an error code of
99 * \ref PIPE_RWSTREAM_IncompleteTransfer. This allows for any abort checking to be performed in the user code - to
100 * continue the transfer, call the function again with identical parameters and it will resume until the BytesProcessed
101 * value reaches the total transfer length.
103 * <b>Single Stream Transfer Example:</b>
107 * if ((ErrorCode = Pipe_Discard_Stream(512, NULL)) != PIPE_RWSTREAM_NoError)
109 * // Stream failed to complete - check ErrorCode here
113 * <b>Partial Stream Transfers Example:</b>
116 * uint16_t BytesProcessed;
118 * BytesProcessed = 0;
119 * while ((ErrorCode = Pipe_Discard_Stream(512, &BytesProcessed)) == PIPE_RWSTREAM_IncompleteTransfer)
121 * // Stream not yet complete - do other actions here, abort if required
124 * if (ErrorCode != PIPE_RWSTREAM_NoError)
126 * // Stream failed to complete - check ErrorCode here
130 * \note The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
131 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
133 * \param[in] Length Number of bytes to discard via the currently selected pipe.
134 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
135 * updated, \c NULL if the entire stream should be processed at once.
137 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
139 uint8_t Pipe_Discard_Stream(uint16_t Length
,
140 uint16_t* const BytesProcessed
);
142 /** Writes a given number of zeroed bytes to the pipe, sending full pipe packets from the host to the device
143 * as needed. The last packet is not automatically sent once the remaining bytes has been written; the
144 * user is responsible for manually discarding the last packet from the device via the \ref Pipe_ClearOUT() macro.
146 * If the BytesProcessed parameter is \c NULL, the entire stream transfer is attempted at once, failing or
147 * succeeding as a single unit. If the BytesProcessed parameter points to a valid storage location, the transfer
148 * will instead be performed as a series of chunks. Each time the pipe bank becomes full while there is still data
149 * to process (and after the current packet transmission has been initiated) the BytesProcessed location will be
150 * updated with the total number of bytes processed in the stream, and the function will exit with an error code of
151 * \ref PIPE_RWSTREAM_IncompleteTransfer. This allows for any abort checking to be performed in the user code - to
152 * continue the transfer, call the function again with identical parameters and it will resume until the BytesProcessed
153 * value reaches the total transfer length.
155 * <b>Single Stream Transfer Example:</b>
159 * if ((ErrorCode = Pipe_Null_Stream(512, NULL)) != PIPE_RWSTREAM_NoError)
161 * // Stream failed to complete - check ErrorCode here
165 * <b>Partial Stream Transfers Example:</b>
168 * uint16_t BytesProcessed;
170 * BytesProcessed = 0;
171 * while ((ErrorCode = Pipe_Null_Stream(512, &BytesProcessed)) == PIPE_RWSTREAM_IncompleteTransfer)
173 * // Stream not yet complete - do other actions here, abort if required
176 * if (ErrorCode != PIPE_RWSTREAM_NoError)
178 * // Stream failed to complete - check ErrorCode here
182 * \note The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
183 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
185 * \param[in] Length Number of zero bytes to write via the currently selected pipe.
186 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
187 * updated, \c NULL if the entire stream should be processed at once.
189 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
191 uint8_t Pipe_Null_Stream(uint16_t Length
,
192 uint16_t* const BytesProcessed
);
196 /** \name Stream functions for RAM source/destination data */
199 /** Writes the given number of bytes to the pipe from the given buffer in little endian,
200 * sending full packets to the device as needed. The last packet filled is not automatically sent;
201 * the user is responsible for manually sending the last written packet to the host via the
202 * \ref Pipe_ClearOUT() macro. Between each USB packet, the given stream callback function is
203 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
205 * If the BytesProcessed parameter is \c NULL, the entire stream transfer is attempted at once,
206 * failing or succeeding as a single unit. If the BytesProcessed parameter points to a valid
207 * storage location, the transfer will instead be performed as a series of chunks. Each time
208 * the pipe bank becomes full while there is still data to process (and after the current
209 * packet transmission has been initiated) the BytesProcessed location will be updated with the
210 * total number of bytes processed in the stream, and the function will exit with an error code of
211 * \ref PIPE_RWSTREAM_IncompleteTransfer. This allows for any abort checking to be performed
212 * in the user code - to continue the transfer, call the function again with identical parameters
213 * and it will resume until the BytesProcessed value reaches the total transfer length.
215 * <b>Single Stream Transfer Example:</b>
217 * uint8_t DataStream[512];
220 * if ((ErrorCode = Pipe_Write_Stream_LE(DataStream, sizeof(DataStream),
221 * NULL)) != PIPE_RWSTREAM_NoError)
223 * // Stream failed to complete - check ErrorCode here
227 * <b>Partial Stream Transfers Example:</b>
229 * uint8_t DataStream[512];
231 * uint16_t BytesProcessed;
233 * BytesProcessed = 0;
234 * while ((ErrorCode = Pipe_Write_Stream_LE(DataStream, sizeof(DataStream),
235 * &BytesProcessed)) == PIPE_RWSTREAM_IncompleteTransfer)
237 * // Stream not yet complete - do other actions here, abort if required
240 * if (ErrorCode != PIPE_RWSTREAM_NoError)
242 * // Stream failed to complete - check ErrorCode here
246 * \note The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
247 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
249 * \param[in] Buffer Pointer to the source data buffer to read from.
250 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
251 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
252 * updated, \c NULL if the entire stream should be written at once.
254 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
256 uint8_t Pipe_Write_Stream_LE(const void* const Buffer
,
258 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
260 /** Writes the given number of bytes to the pipe from the given buffer in big endian,
261 * sending full packets to the device as needed. The last packet filled is not automatically sent;
262 * the user is responsible for manually sending the last written packet to the host via the
263 * \ref Pipe_ClearOUT() macro. Between each USB packet, the given stream callback function is
264 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
266 * \note The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
267 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
269 * \param[in] Buffer Pointer to the source data buffer to read from.
270 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
271 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
272 * updated, \c NULL if the entire stream should be written at once.
274 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
276 uint8_t Pipe_Write_Stream_BE(const void* const Buffer
,
278 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
280 /** Reads the given number of bytes from the pipe into the given buffer in little endian,
281 * sending full packets to the device as needed. The last packet filled is not automatically sent;
282 * the user is responsible for manually sending the last written packet to the host via the
283 * \ref Pipe_ClearIN() macro. Between each USB packet, the given stream callback function is
284 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
286 * If the BytesProcessed parameter is \c NULL, the entire stream transfer is attempted at once,
287 * failing or succeeding as a single unit. If the BytesProcessed parameter points to a valid
288 * storage location, the transfer will instead be performed as a series of chunks. Each time
289 * the pipe bank becomes empty while there is still data to process (and after the current
290 * packet has been acknowledged) the BytesProcessed location will be updated with the total number
291 * of bytes processed in the stream, and the function will exit with an error code of
292 * \ref PIPE_RWSTREAM_IncompleteTransfer. This allows for any abort checking to be performed
293 * in the user code - to continue the transfer, call the function again with identical parameters
294 * and it will resume until the BytesProcessed value reaches the total transfer length.
296 * <b>Single Stream Transfer Example:</b>
298 * uint8_t DataStream[512];
301 * if ((ErrorCode = Pipe_Read_Stream_LE(DataStream, sizeof(DataStream),
302 * NULL)) != PIPE_RWSTREAM_NoError)
304 * // Stream failed to complete - check ErrorCode here
308 * <b>Partial Stream Transfers Example:</b>
310 * uint8_t DataStream[512];
312 * uint16_t BytesProcessed;
314 * BytesProcessed = 0;
315 * while ((ErrorCode = Pipe_Read_Stream_LE(DataStream, sizeof(DataStream),
316 * &BytesProcessed)) == PIPE_RWSTREAM_IncompleteTransfer)
318 * // Stream not yet complete - do other actions here, abort if required
321 * if (ErrorCode != PIPE_RWSTREAM_NoError)
323 * // Stream failed to complete - check ErrorCode here
327 * \note The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
328 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
330 * \param[out] Buffer Pointer to the source data buffer to write to.
331 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
332 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
333 * updated, \c NULL if the entire stream should be read at once.
335 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
337 uint8_t Pipe_Read_Stream_LE(void* const Buffer
,
339 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
341 /** Reads the given number of bytes from the pipe into the given buffer in big endian,
342 * sending full packets to the device as needed. The last packet filled is not automatically sent;
343 * the user is responsible for manually sending the last written packet to the host via the
344 * \ref Pipe_ClearIN() macro. Between each USB packet, the given stream callback function is
345 * executed repeatedly until the next packet is ready, allowing for early aborts of stream transfers.
347 * \note The pipe token is set automatically, thus this can be used on bi-directional pipes directly without
348 * having to explicitly change the data direction with a call to \ref Pipe_SetPipeToken().
350 * \param[out] Buffer Pointer to the source data buffer to write to.
351 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
352 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
353 * updated, \c NULL if the entire stream should be read at once.
355 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
357 uint8_t Pipe_Read_Stream_BE(void* const Buffer
,
359 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
362 /** \name Stream functions for EEPROM source/destination data */
365 /** EEPROM buffer source version of \ref Pipe_Write_Stream_LE().
367 * \param[in] Buffer Pointer to the source data buffer to read from.
368 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
369 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
370 * updated, \c NULL if the entire stream should be written at once.
372 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
374 uint8_t Pipe_Write_EStream_LE(const void* const Buffer
,
376 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
378 /** EEPROM buffer source version of \ref Pipe_Write_Stream_BE().
380 * \param[in] Buffer Pointer to the source data buffer to read from.
381 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
382 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
383 * updated, \c NULL if the entire stream should be written at once.
385 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
387 uint8_t Pipe_Write_EStream_BE(const void* const Buffer
,
389 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
391 /** EEPROM buffer source version of \ref Pipe_Read_Stream_LE().
393 * \param[out] Buffer Pointer to the source data buffer to write to.
394 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
395 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
396 * updated, \c NULL if the entire stream should be read at once.
398 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
400 uint8_t Pipe_Read_EStream_LE(void* const Buffer
,
402 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
404 /** EEPROM buffer source version of \ref Pipe_Read_Stream_BE().
406 * \param[out] Buffer Pointer to the source data buffer to write to.
407 * \param[in] Length Number of bytes to read for the currently selected pipe to read from.
408 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
409 * updated, \c NULL if the entire stream should be read at once.
411 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
413 uint8_t Pipe_Read_EStream_BE(void* const Buffer
,
415 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
418 /** \name Stream functions for PROGMEM source/destination data */
421 /** FLASH buffer source version of \ref Pipe_Write_Stream_LE().
423 * \pre The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
425 * \param[in] Buffer Pointer to the source data buffer to read from.
426 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
427 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
428 * updated, \c NULL if the entire stream should be written at once.
430 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
432 uint8_t Pipe_Write_PStream_LE(const void* const Buffer
,
434 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
436 /** FLASH buffer source version of \ref Pipe_Write_Stream_BE().
438 * \pre The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
440 * \param[in] Buffer Pointer to the source data buffer to read from.
441 * \param[in] Length Number of bytes to read for the currently selected pipe into the buffer.
442 * \param[in] BytesProcessed Pointer to a location where the total number of bytes already processed should
443 * updated, \c NULL if the entire stream should be written at once.
445 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
447 uint8_t Pipe_Write_PStream_BE(const void* const Buffer
,
449 uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1);
452 /* Disable C linkage for C++ Compilers: */
453 #if defined(__cplusplus)