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 Endpoint data stream transmission and reception management for the AVR32 UC3 microcontrollers. 
  33  *  \copydetails Group_EndpointStreamRW_UC3 
  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_EndpointStreamRW 
  40  *  \defgroup Group_EndpointStreamRW_UC3 Read/Write of Multi-Byte Streams (UC3) 
  41  *  \brief Endpoint data stream transmission and reception management for the Atmel AVR32 UC3 architecture. 
  43  *  Functions, macros, variables, enums and types related to data reading and writing of data streams from 
  49 #ifndef __ENDPOINT_STREAM_UC3_H__ 
  50 #define __ENDPOINT_STREAM_UC3_H__ 
  53                 #include "../../../../Common/Common.h" 
  54                 #include "../USBMode.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: */ 
  67                 /* Function Prototypes: */ 
  68                         /** \name Stream functions for null data */ 
  71                         /** Reads and discards the given number of bytes from the currently selected endpoint's bank, 
  72                          *  discarding fully read packets from the host as needed. The last packet is not automatically 
  73                          *  discarded once the remaining bytes has been read; the user is responsible for manually 
  74                          *  discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. 
  76                          *  If the BytesProcessed parameter is \c NULL, the entire stream transfer is attempted at once, 
  77                          *  failing or succeeding as a single unit. If the BytesProcessed parameter points to a valid 
  78                          *  storage location, the transfer will instead be performed as a series of chunks. Each time 
  79                          *  the endpoint bank becomes empty while there is still data to process (and after the current 
  80                          *  packet has been acknowledged) the BytesProcessed location will be updated with the total number 
  81                          *  of bytes processed in the stream, and the function will exit with an error code of 
  82                          *  \ref ENDPOINT_RWSTREAM_IncompleteTransfer. This allows for any abort checking to be performed 
  83                          *  in the user code - to continue the transfer, call the function again with identical parameters 
  84                          *  and it will resume until the BytesProcessed value reaches the total transfer length. 
  86                          *  <b>Single Stream Transfer Example:</b> 
  90                          *  if ((ErrorCode = Endpoint_Discard_Stream(512, NULL)) != ENDPOINT_RWSTREAM_NoError) 
  92                          *       // Stream failed to complete - check ErrorCode here 
  96                          *  <b>Partial Stream Transfers Example:</b> 
  99                          *  uint16_t BytesProcessed; 
 101                          *  BytesProcessed = 0; 
 102                          *  while ((ErrorCode = Endpoint_Discard_Stream(512, &BytesProcessed)) == ENDPOINT_RWSTREAM_IncompleteTransfer) 
 104                          *      // Stream not yet complete - do other actions here, abort if required 
 107                          *  if (ErrorCode != ENDPOINT_RWSTREAM_NoError) 
 109                          *      // Stream failed to complete - check ErrorCode here 
 113                          *  \note This routine should not be used on CONTROL type endpoints. 
 115                          *  \param[in] Length          Number of bytes to discard via the currently selected endpoint. 
 116                          *  \param[in] BytesProcessed  Pointer to a location where the total number of bytes processed in the current 
 117                          *                             transaction should be updated, \c NULL if the entire stream should be read at once. 
 119                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 121                         uint8_t Endpoint_Discard_Stream(uint16_t Length
, 
 122                                                         uint16_t* const BytesProcessed
); 
 124                         /** Writes a given number of zeroed bytes to the currently selected endpoint's bank, sending 
 125                          *  full packets to the host as needed. The last packet is not automatically sent once the  
 126                          *  remaining bytes have been written; the user is responsible for manually sending the last 
 127                          *  packet to the host via the \ref Endpoint_ClearIN() macro. 
 129                          *  If the BytesProcessed parameter is \c NULL, the entire stream transfer is attempted at once, 
 130                          *  failing or succeeding as a single unit. If the BytesProcessed parameter points to a valid 
 131                          *  storage location, the transfer will instead be performed as a series of chunks. Each time 
 132                          *  the endpoint bank becomes full while there is still data to process (and after the current 
 133                          *  packet transmission has been initiated) the BytesProcessed location will be updated with the 
 134                          *  total number of bytes processed in the stream, and the function will exit with an error code of 
 135                          *  \ref ENDPOINT_RWSTREAM_IncompleteTransfer. This allows for any abort checking to be performed 
 136                          *  in the user code - to continue the transfer, call the function again with identical parameters 
 137                          *  and it will resume until the BytesProcessed value reaches the total transfer length. 
 139                          *  <b>Single Stream Transfer Example:</b> 
 143                          *  if ((ErrorCode = Endpoint_Null_Stream(512, NULL)) != ENDPOINT_RWSTREAM_NoError) 
 145                          *       // Stream failed to complete - check ErrorCode here 
 149                          *  <b>Partial Stream Transfers Example:</b> 
 152                          *  uint16_t BytesProcessed; 
 154                          *  BytesProcessed = 0; 
 155                          *  while ((ErrorCode = Endpoint_Null_Stream(512, &BytesProcessed)) == ENDPOINT_RWSTREAM_IncompleteTransfer) 
 157                          *      // Stream not yet complete - do other actions here, abort if required 
 160                          *  if (ErrorCode != ENDPOINT_RWSTREAM_NoError) 
 162                          *      // Stream failed to complete - check ErrorCode here 
 166                          *  \note This routine should not be used on CONTROL type endpoints. 
 168                          *  \param[in] Length          Number of zero bytes to send via the currently selected endpoint. 
 169                          *  \param[in] BytesProcessed  Pointer to a location where the total number of bytes processed in the current 
 170                          *                             transaction should be updated, \c NULL if the entire stream should be read at once. 
 172                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 174                         uint8_t Endpoint_Null_Stream(uint16_t Length
, 
 175                                                      uint16_t* const BytesProcessed
); 
 179                         /** \name Stream functions for RAM source/destination data */ 
 182                         /** Writes the given number of bytes to the endpoint from the given buffer in little endian, 
 183                          *  sending full packets to the host as needed. The last packet filled is not automatically sent; 
 184                          *  the user is responsible for manually sending the last written packet to the host via the 
 185                          *  \ref Endpoint_ClearIN() macro. 
 187                          *  If the BytesProcessed parameter is \c NULL, the entire stream transfer is attempted at once, 
 188                          *  failing or succeeding as a single unit. If the BytesProcessed parameter points to a valid 
 189                          *  storage location, the transfer will instead be performed as a series of chunks. Each time 
 190                          *  the endpoint bank becomes full while there is still data to process (and after the current 
 191                          *  packet transmission has been initiated) the BytesProcessed location will be updated with the 
 192                          *  total number of bytes processed in the stream, and the function will exit with an error code of 
 193                          *  \ref ENDPOINT_RWSTREAM_IncompleteTransfer. This allows for any abort checking to be performed 
 194                          *  in the user code - to continue the transfer, call the function again with identical parameters 
 195                          *  and it will resume until the BytesProcessed value reaches the total transfer length. 
 197                          *  <b>Single Stream Transfer Example:</b> 
 199                          *  uint8_t DataStream[512]; 
 202                          *  if ((ErrorCode = Endpoint_Write_Stream_LE(DataStream, sizeof(DataStream), 
 203                          *                                            NULL)) != ENDPOINT_RWSTREAM_NoError) 
 205                          *       // Stream failed to complete - check ErrorCode here 
 209                          *  <b>Partial Stream Transfers Example:</b> 
 211                          *  uint8_t  DataStream[512]; 
 213                          *  uint16_t BytesProcessed; 
 215                          *  BytesProcessed = 0; 
 216                          *  while ((ErrorCode = Endpoint_Write_Stream_LE(DataStream, sizeof(DataStream), 
 217                          *                                               &BytesProcessed)) == ENDPOINT_RWSTREAM_IncompleteTransfer) 
 219                          *      // Stream not yet complete - do other actions here, abort if required 
 222                          *  if (ErrorCode != ENDPOINT_RWSTREAM_NoError) 
 224                          *      // Stream failed to complete - check ErrorCode here 
 228                          *  \note This routine should not be used on CONTROL type endpoints. 
 230                          *  \param[in] Buffer          Pointer to the source data buffer to read from. 
 231                          *  \param[in] Length          Number of bytes to read for the currently selected endpoint into the buffer. 
 232                          *  \param[in] BytesProcessed  Pointer to a location where the total number of bytes processed in the current 
 233                          *                             transaction should be updated, \c NULL if the entire stream should be written at once. 
 235                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 237                         uint8_t Endpoint_Write_Stream_LE(const void* const Buffer
, 
 239                                                          uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1); 
 241                         /** Writes the given number of bytes to the endpoint from the given buffer in big endian, 
 242                          *  sending full packets to the host as needed. The last packet filled is not automatically sent; 
 243                          *  the user is responsible for manually sending the last written packet to the host via the 
 244                          *  \ref Endpoint_ClearIN() macro. 
 246                          *  \note This routine should not be used on CONTROL type endpoints. 
 248                          *  \param[in] Buffer          Pointer to the source data buffer to read from. 
 249                          *  \param[in] Length          Number of bytes to read for the currently selected endpoint into the buffer. 
 250                          *  \param[in] BytesProcessed  Pointer to a location where the total number of bytes processed in the current 
 251                          *                             transaction should be updated, \c NULL if the entire stream should be written at once. 
 253                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 255                         uint8_t Endpoint_Write_Stream_BE(const void* const Buffer
, 
 257                                                          uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1); 
 259                         /** Reads the given number of bytes from the endpoint from the given buffer in little endian, 
 260                          *  discarding fully read packets from the host as needed. The last packet is not automatically 
 261                          *  discarded once the remaining bytes has been read; the user is responsible for manually 
 262                          *  discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. 
 264                          *  If the BytesProcessed parameter is \c NULL, the entire stream transfer is attempted at once, 
 265                          *  failing or succeeding as a single unit. If the BytesProcessed parameter points to a valid 
 266                          *  storage location, the transfer will instead be performed as a series of chunks. Each time 
 267                          *  the endpoint bank becomes empty while there is still data to process (and after the current 
 268                          *  packet has been acknowledged) the BytesProcessed location will be updated with the total number 
 269                          *  of bytes processed in the stream, and the function will exit with an error code of 
 270                          *  \ref ENDPOINT_RWSTREAM_IncompleteTransfer. This allows for any abort checking to be performed 
 271                          *  in the user code - to continue the transfer, call the function again with identical parameters 
 272                          *  and it will resume until the BytesProcessed value reaches the total transfer length. 
 274                          *  <b>Single Stream Transfer Example:</b> 
 276                          *  uint8_t DataStream[512]; 
 279                          *  if ((ErrorCode = Endpoint_Read_Stream_LE(DataStream, sizeof(DataStream), 
 280                          *                                           NULL)) != ENDPOINT_RWSTREAM_NoError) 
 282                          *       // Stream failed to complete - check ErrorCode here 
 286                          *  <b>Partial Stream Transfers Example:</b> 
 288                          *  uint8_t  DataStream[512]; 
 290                          *  uint16_t BytesProcessed; 
 292                          *  BytesProcessed = 0; 
 293                          *  while ((ErrorCode = Endpoint_Read_Stream_LE(DataStream, sizeof(DataStream), 
 294                          *                                              &BytesProcessed)) == ENDPOINT_RWSTREAM_IncompleteTransfer) 
 296                          *      // Stream not yet complete - do other actions here, abort if required 
 299                          *  if (ErrorCode != ENDPOINT_RWSTREAM_NoError) 
 301                          *      // Stream failed to complete - check ErrorCode here 
 305                          *  \note This routine should not be used on CONTROL type endpoints. 
 307                          *  \param[out] Buffer          Pointer to the destination data buffer to write to. 
 308                          *  \param[in]  Length          Number of bytes to send via the currently selected endpoint. 
 309                          *  \param[in]  BytesProcessed  Pointer to a location where the total number of bytes processed in the current 
 310                          *                              transaction should be updated, \c NULL if the entire stream should be read at once. 
 312                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 314                         uint8_t Endpoint_Read_Stream_LE(void* const Buffer
, 
 316                                                         uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1); 
 318                         /** Reads the given number of bytes from the endpoint from the given buffer in big endian, 
 319                          *  discarding fully read packets from the host as needed. The last packet is not automatically 
 320                          *  discarded once the remaining bytes has been read; the user is responsible for manually 
 321                          *  discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. 
 323                          *  \note This routine should not be used on CONTROL type endpoints. 
 325                          *  \param[out] Buffer          Pointer to the destination data buffer to write to. 
 326                          *  \param[in]  Length          Number of bytes to send via the currently selected endpoint. 
 327                          *  \param[in]  BytesProcessed  Pointer to a location where the total number of bytes processed in the current 
 328                          *                              transaction should be updated, \c NULL if the entire stream should be read at once. 
 330                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 332                         uint8_t Endpoint_Read_Stream_BE(void* const Buffer
, 
 334                                                         uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1); 
 336                         /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in little endian, 
 337                          *  sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared 
 338                          *  in both failure and success states; the user is responsible for manually clearing the setup OUT to 
 339                          *  finalize the transfer via the \ref Endpoint_ClearOUT() macro. 
 341                          *  \note This function automatically clears the control transfer's status stage. Do not manually attempt 
 342                          *        to clear the status stage when using this routine in a control transaction. 
 345                          *  \note This routine should only be used on CONTROL type endpoints. 
 347                          *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained 
 348                          *           together; i.e. the entire stream data must be read or written at the one time. 
 350                          *  \param[in] Buffer  Pointer to the source data buffer to read from. 
 351                          *  \param[in] Length  Number of bytes to read for the currently selected endpoint into the buffer. 
 353                          *  \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum. 
 355                         uint8_t Endpoint_Write_Control_Stream_LE(const void* const Buffer
, 
 356                                                                  uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1); 
 358                         /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in big endian, 
 359                          *  sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared 
 360                          *  in both failure and success states; the user is responsible for manually clearing the setup OUT to 
 361                          *  finalize the transfer via the \ref Endpoint_ClearOUT() macro. 
 363                          *  \note This function automatically clears the control transfer's status stage. Do not manually attempt 
 364                          *        to clear the status stage when using this routine in a control transaction. 
 367                          *  \note This routine should only be used on CONTROL type endpoints. 
 369                          *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained 
 370                          *           together; i.e. the entire stream data must be read or written at the one time. 
 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 endpoint into the buffer. 
 375                          *  \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum. 
 377                         uint8_t Endpoint_Write_Control_Stream_BE(const void* const Buffer
, 
 378                                                                  uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1); 
 380                         /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in little endian, 
 381                          *  discarding fully read packets from the host as needed. The device IN acknowledgement is not 
 382                          *  automatically sent after success or failure states; the user is responsible for manually sending the 
 383                          *  setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro. 
 385                          *  \note This function automatically clears the control transfer's status stage. Do not manually attempt 
 386                          *        to clear the status stage when using this routine in a control transaction. 
 389                          *  \note This routine should only be used on CONTROL type endpoints. 
 391                          *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained 
 392                          *           together; i.e. the entire stream data must be read or written at the one time. 
 394                          *  \param[out] Buffer  Pointer to the destination data buffer to write to. 
 395                          *  \param[in]  Length  Number of bytes to send via the currently selected endpoint. 
 397                          *  \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum. 
 399                         uint8_t Endpoint_Read_Control_Stream_LE(void* const Buffer
, 
 400                                                                 uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1); 
 402                         /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in big endian, 
 403                          *  discarding fully read packets from the host as needed. The device IN acknowledgement is not 
 404                          *  automatically sent after success or failure states; the user is responsible for manually sending the 
 405                          *  setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro. 
 407                          *  \note This function automatically clears the control transfer's status stage. Do not manually attempt 
 408                          *        to clear the status stage when using this routine in a control transaction. 
 411                          *  \note This routine should only be used on CONTROL type endpoints. 
 413                          *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained 
 414                          *           together; i.e. the entire stream data must be read or written at the one time. 
 416                          *  \param[out] Buffer  Pointer to the destination data buffer to write to. 
 417                          *  \param[in]  Length  Number of bytes to send via the currently selected endpoint. 
 419                          *  \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum. 
 421                         uint8_t Endpoint_Read_Control_Stream_BE(void* const Buffer
, 
 422                                                                 uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1); 
 425         /* Disable C linkage for C++ Compilers: */ 
 426                 #if defined(__cplusplus)