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" 
  55                 #include "../USBTask.h" 
  57         /* Enable C linkage for C++ Compilers: */ 
  58                 #if defined(__cplusplus) 
  62         /* Preprocessor Checks: */ 
  63                 #if !defined(__INCLUDE_FROM_USB_DRIVER) 
  64                         #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead. 
  67         /* Public Interface - May be used in end-application: */ 
  68                 /* Function Prototypes: */ 
  69                         /** \name Stream functions for null data */ 
  72                         /** Reads and discards the given number of bytes from the currently selected endpoint's bank, 
  73                          *  discarding fully read packets from the host as needed. The last packet is not automatically 
  74                          *  discarded once the remaining bytes has been read; the user is responsible for manually 
  75                          *  discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. 
  77                          *  If the BytesProcessed parameter is \c NULL, the entire stream transfer is attempted at once, 
  78                          *  failing or succeeding as a single unit. If the BytesProcessed parameter points to a valid 
  79                          *  storage location, the transfer will instead be performed as a series of chunks. Each time 
  80                          *  the endpoint bank becomes empty while there is still data to process (and after the current 
  81                          *  packet has been acknowledged) the BytesProcessed location will be updated with the total number 
  82                          *  of bytes processed in the stream, and the function will exit with an error code of 
  83                          *  \ref ENDPOINT_RWSTREAM_IncompleteTransfer. This allows for any abort checking to be performed 
  84                          *  in the user code - to continue the transfer, call the function again with identical parameters 
  85                          *  and it will resume until the BytesProcessed value reaches the total transfer length. 
  87                          *  <b>Single Stream Transfer Example:</b> 
  91                          *  if ((ErrorCode = Endpoint_Discard_Stream(512, NULL)) != ENDPOINT_RWSTREAM_NoError) 
  93                          *       // Stream failed to complete - check ErrorCode here 
  97                          *  <b>Partial Stream Transfers Example:</b> 
 100                          *  uint16_t BytesProcessed; 
 102                          *  BytesProcessed = 0; 
 103                          *  while ((ErrorCode = Endpoint_Discard_Stream(512, &BytesProcessed)) == ENDPOINT_RWSTREAM_IncompleteTransfer) 
 105                          *      // Stream not yet complete - do other actions here, abort if required 
 108                          *  if (ErrorCode != ENDPOINT_RWSTREAM_NoError) 
 110                          *      // Stream failed to complete - check ErrorCode here 
 114                          *  \note This routine should not be used on CONTROL type endpoints. 
 116                          *  \param[in] Length          Number of bytes to discard via the currently selected endpoint. 
 117                          *  \param[in] BytesProcessed  Pointer to a location where the total number of bytes processed in the current 
 118                          *                             transaction should be updated, \c NULL if the entire stream should be read at once. 
 120                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 122                         uint8_t Endpoint_Discard_Stream(uint16_t Length
, 
 123                                                         uint16_t* const BytesProcessed
); 
 125                         /** Writes a given number of zeroed bytes to the currently selected endpoint's bank, sending 
 126                          *  full packets to the host as needed. The last packet is not automatically sent once the  
 127                          *  remaining bytes have been written; the user is responsible for manually sending the last 
 128                          *  packet to the host via the \ref Endpoint_ClearIN() macro. 
 130                          *  If the BytesProcessed parameter is \c NULL, the entire stream transfer is attempted at once, 
 131                          *  failing or succeeding as a single unit. If the BytesProcessed parameter points to a valid 
 132                          *  storage location, the transfer will instead be performed as a series of chunks. Each time 
 133                          *  the endpoint bank becomes full while there is still data to process (and after the current 
 134                          *  packet transmission has been initiated) the BytesProcessed location will be updated with the 
 135                          *  total number of bytes processed in the stream, and the function will exit with an error code of 
 136                          *  \ref ENDPOINT_RWSTREAM_IncompleteTransfer. This allows for any abort checking to be performed 
 137                          *  in the user code - to continue the transfer, call the function again with identical parameters 
 138                          *  and it will resume until the BytesProcessed value reaches the total transfer length. 
 140                          *  <b>Single Stream Transfer Example:</b> 
 144                          *  if ((ErrorCode = Endpoint_Null_Stream(512, NULL)) != ENDPOINT_RWSTREAM_NoError) 
 146                          *       // Stream failed to complete - check ErrorCode here 
 150                          *  <b>Partial Stream Transfers Example:</b> 
 153                          *  uint16_t BytesProcessed; 
 155                          *  BytesProcessed = 0; 
 156                          *  while ((ErrorCode = Endpoint_Null_Stream(512, &BytesProcessed)) == ENDPOINT_RWSTREAM_IncompleteTransfer) 
 158                          *      // Stream not yet complete - do other actions here, abort if required 
 161                          *  if (ErrorCode != ENDPOINT_RWSTREAM_NoError) 
 163                          *      // Stream failed to complete - check ErrorCode here 
 167                          *  \note This routine should not be used on CONTROL type endpoints. 
 169                          *  \param[in] Length          Number of zero bytes to send via the currently selected endpoint. 
 170                          *  \param[in] BytesProcessed  Pointer to a location where the total number of bytes processed in the current 
 171                          *                             transaction should be updated, \c NULL if the entire stream should be read at once. 
 173                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 175                         uint8_t Endpoint_Null_Stream(uint16_t Length
, 
 176                                                      uint16_t* const BytesProcessed
); 
 180                         /** \name Stream functions for RAM source/destination data */ 
 183                         /** Writes the given number of bytes to the endpoint from the given buffer in little endian, 
 184                          *  sending full packets to the host as needed. The last packet filled is not automatically sent; 
 185                          *  the user is responsible for manually sending the last written packet to the host via the 
 186                          *  \ref Endpoint_ClearIN() macro. 
 188                          *  If the BytesProcessed parameter is \c NULL, the entire stream transfer is attempted at once, 
 189                          *  failing or succeeding as a single unit. If the BytesProcessed parameter points to a valid 
 190                          *  storage location, the transfer will instead be performed as a series of chunks. Each time 
 191                          *  the endpoint bank becomes full while there is still data to process (and after the current 
 192                          *  packet transmission has been initiated) the BytesProcessed location will be updated with the 
 193                          *  total number of bytes processed in the stream, and the function will exit with an error code of 
 194                          *  \ref ENDPOINT_RWSTREAM_IncompleteTransfer. This allows for any abort checking to be performed 
 195                          *  in the user code - to continue the transfer, call the function again with identical parameters 
 196                          *  and it will resume until the BytesProcessed value reaches the total transfer length. 
 198                          *  <b>Single Stream Transfer Example:</b> 
 200                          *  uint8_t DataStream[512]; 
 203                          *  if ((ErrorCode = Endpoint_Write_Stream_LE(DataStream, sizeof(DataStream), 
 204                          *                                            NULL)) != ENDPOINT_RWSTREAM_NoError) 
 206                          *       // Stream failed to complete - check ErrorCode here 
 210                          *  <b>Partial Stream Transfers Example:</b> 
 212                          *  uint8_t  DataStream[512]; 
 214                          *  uint16_t BytesProcessed; 
 216                          *  BytesProcessed = 0; 
 217                          *  while ((ErrorCode = Endpoint_Write_Stream_LE(DataStream, sizeof(DataStream), 
 218                          *                                               &BytesProcessed)) == ENDPOINT_RWSTREAM_IncompleteTransfer) 
 220                          *      // Stream not yet complete - do other actions here, abort if required 
 223                          *  if (ErrorCode != ENDPOINT_RWSTREAM_NoError) 
 225                          *      // Stream failed to complete - check ErrorCode here 
 229                          *  \note This routine should not be used on CONTROL type endpoints. 
 231                          *  \param[in] Buffer          Pointer to the source data buffer to read from. 
 232                          *  \param[in] Length          Number of bytes to read for the currently selected endpoint into the buffer. 
 233                          *  \param[in] BytesProcessed  Pointer to a location where the total number of bytes processed in the current 
 234                          *                             transaction should be updated, \c NULL if the entire stream should be written at once. 
 236                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 238                         uint8_t Endpoint_Write_Stream_LE(const void* const Buffer
, 
 240                                                          uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1); 
 242                         /** Writes the given number of bytes to the endpoint from the given buffer in big endian, 
 243                          *  sending full packets to the host as needed. The last packet filled is not automatically sent; 
 244                          *  the user is responsible for manually sending the last written packet to the host via the 
 245                          *  \ref Endpoint_ClearIN() macro. 
 247                          *  \note This routine should not be used on CONTROL type endpoints. 
 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 endpoint into the buffer. 
 251                          *  \param[in] BytesProcessed  Pointer to a location where the total number of bytes processed in the current 
 252                          *                             transaction should be updated, \c NULL if the entire stream should be written at once. 
 254                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 256                         uint8_t Endpoint_Write_Stream_BE(const void* const Buffer
, 
 258                                                          uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1); 
 260                         /** Reads the given number of bytes from the endpoint from the given buffer in little endian, 
 261                          *  discarding fully read packets from the host as needed. The last packet is not automatically 
 262                          *  discarded once the remaining bytes has been read; the user is responsible for manually 
 263                          *  discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. 
 265                          *  If the BytesProcessed parameter is \c NULL, the entire stream transfer is attempted at once, 
 266                          *  failing or succeeding as a single unit. If the BytesProcessed parameter points to a valid 
 267                          *  storage location, the transfer will instead be performed as a series of chunks. Each time 
 268                          *  the endpoint bank becomes empty while there is still data to process (and after the current 
 269                          *  packet has been acknowledged) the BytesProcessed location will be updated with the total number 
 270                          *  of bytes processed in the stream, and the function will exit with an error code of 
 271                          *  \ref ENDPOINT_RWSTREAM_IncompleteTransfer. This allows for any abort checking to be performed 
 272                          *  in the user code - to continue the transfer, call the function again with identical parameters 
 273                          *  and it will resume until the BytesProcessed value reaches the total transfer length. 
 275                          *  <b>Single Stream Transfer Example:</b> 
 277                          *  uint8_t DataStream[512]; 
 280                          *  if ((ErrorCode = Endpoint_Read_Stream_LE(DataStream, sizeof(DataStream), 
 281                          *                                           NULL)) != ENDPOINT_RWSTREAM_NoError) 
 283                          *       // Stream failed to complete - check ErrorCode here 
 287                          *  <b>Partial Stream Transfers Example:</b> 
 289                          *  uint8_t  DataStream[512]; 
 291                          *  uint16_t BytesProcessed; 
 293                          *  BytesProcessed = 0; 
 294                          *  while ((ErrorCode = Endpoint_Read_Stream_LE(DataStream, sizeof(DataStream), 
 295                          *                                              &BytesProcessed)) == ENDPOINT_RWSTREAM_IncompleteTransfer) 
 297                          *      // Stream not yet complete - do other actions here, abort if required 
 300                          *  if (ErrorCode != ENDPOINT_RWSTREAM_NoError) 
 302                          *      // Stream failed to complete - check ErrorCode here 
 306                          *  \note This routine should not be used on CONTROL type endpoints. 
 308                          *  \param[out] Buffer          Pointer to the destination data buffer to write to. 
 309                          *  \param[in]  Length          Number of bytes to send via the currently selected endpoint. 
 310                          *  \param[in]  BytesProcessed  Pointer to a location where the total number of bytes processed in the current 
 311                          *                              transaction should be updated, \c NULL if the entire stream should be read at once. 
 313                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 315                         uint8_t Endpoint_Read_Stream_LE(void* const Buffer
, 
 317                                                         uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1); 
 319                         /** Reads the given number of bytes from the endpoint from the given buffer in big endian, 
 320                          *  discarding fully read packets from the host as needed. The last packet is not automatically 
 321                          *  discarded once the remaining bytes has been read; the user is responsible for manually 
 322                          *  discarding the last packet from the host via the \ref Endpoint_ClearOUT() macro. 
 324                          *  \note This routine should not be used on CONTROL type endpoints. 
 326                          *  \param[out] Buffer          Pointer to the destination data buffer to write to. 
 327                          *  \param[in]  Length          Number of bytes to send via the currently selected endpoint. 
 328                          *  \param[in]  BytesProcessed  Pointer to a location where the total number of bytes processed in the current 
 329                          *                              transaction should be updated, \c NULL if the entire stream should be read at once. 
 331                          *  \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum. 
 333                         uint8_t Endpoint_Read_Stream_BE(void* const Buffer
, 
 335                                                         uint16_t* const BytesProcessed
) ATTR_NON_NULL_PTR_ARG(1); 
 337                         /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in little endian, 
 338                          *  sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared 
 339                          *  in both failure and success states; the user is responsible for manually clearing the setup OUT to 
 340                          *  finalize the transfer via the \ref Endpoint_ClearOUT() macro. 
 342                          *  \note This function automatically clears the control transfer's status stage. Do not manually attempt 
 343                          *        to clear the status stage when using this routine in a control transaction. 
 346                          *  \note This routine should only be used on CONTROL type endpoints. 
 348                          *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained 
 349                          *           together; i.e. the entire stream data must be read or written at the one time. 
 351                          *  \param[in] Buffer  Pointer to the source data buffer to read from. 
 352                          *  \param[in] Length  Number of bytes to read for the currently selected endpoint into the buffer. 
 354                          *  \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum. 
 356                         uint8_t Endpoint_Write_Control_Stream_LE(const void* const Buffer
, 
 357                                                                  uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1); 
 359                         /** Writes the given number of bytes to the CONTROL type endpoint from the given buffer in big endian, 
 360                          *  sending full packets to the host as needed. The host OUT acknowledgement is not automatically cleared 
 361                          *  in both failure and success states; the user is responsible for manually clearing the setup OUT to 
 362                          *  finalize the transfer via the \ref Endpoint_ClearOUT() macro. 
 364                          *  \note This function automatically clears the control transfer's status stage. Do not manually attempt 
 365                          *        to clear the status stage when using this routine in a control transaction. 
 368                          *  \note This routine should only be used on CONTROL type endpoints. 
 370                          *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained 
 371                          *           together; i.e. the entire stream data must be read or written at the one time. 
 373                          *  \param[in] Buffer  Pointer to the source data buffer to read from. 
 374                          *  \param[in] Length  Number of bytes to read for the currently selected endpoint into the buffer. 
 376                          *  \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum. 
 378                         uint8_t Endpoint_Write_Control_Stream_BE(const void* const Buffer
, 
 379                                                                  uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1); 
 381                         /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in little endian, 
 382                          *  discarding fully read packets from the host as needed. The device IN acknowledgement is not 
 383                          *  automatically sent after success or failure states; the user is responsible for manually sending the 
 384                          *  setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro. 
 386                          *  \note This function automatically clears the control transfer's status stage. Do not manually attempt 
 387                          *        to clear the status stage when using this routine in a control transaction. 
 390                          *  \note This routine should only be used on CONTROL type endpoints. 
 392                          *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained 
 393                          *           together; i.e. the entire stream data must be read or written at the one time. 
 395                          *  \param[out] Buffer  Pointer to the destination data buffer to write to. 
 396                          *  \param[in]  Length  Number of bytes to send via the currently selected endpoint. 
 398                          *  \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum. 
 400                         uint8_t Endpoint_Read_Control_Stream_LE(void* const Buffer
, 
 401                                                                 uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1); 
 403                         /** Reads the given number of bytes from the CONTROL endpoint from the given buffer in big endian, 
 404                          *  discarding fully read packets from the host as needed. The device IN acknowledgement is not 
 405                          *  automatically sent after success or failure states; the user is responsible for manually sending the 
 406                          *  setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro. 
 408                          *  \note This function automatically clears the control transfer's status stage. Do not manually attempt 
 409                          *        to clear the status stage when using this routine in a control transaction. 
 412                          *  \note This routine should only be used on CONTROL type endpoints. 
 414                          *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained 
 415                          *           together; i.e. the entire stream data must be read or written at the one time. 
 417                          *  \param[out] Buffer  Pointer to the destination data buffer to write to. 
 418                          *  \param[in]  Length  Number of bytes to send via the currently selected endpoint. 
 420                          *  \return A value from the \ref Endpoint_ControlStream_RW_ErrorCodes_t enum. 
 422                         uint8_t Endpoint_Read_Control_Stream_BE(void* const Buffer
, 
 423                                                                 uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1); 
 426         /* Disable C linkage for C++ Compilers: */ 
 427                 #if defined(__cplusplus)