3      Copyright (C) Dean Camera, 2012. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2012  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 
  31 #include "../../../../Common/Common.h" 
  32 #if (ARCH == ARCH_XMEGA) 
  34 #define  __INCLUDE_FROM_USB_DRIVER 
  35 #include "../USBMode.h" 
  37 #if defined(USB_CAN_BE_DEVICE) 
  39 #include "EndpointStream_XMEGA.h" 
  41 #if !defined(CONTROL_ONLY_DEVICE) 
  42 uint8_t Endpoint_Discard_Stream(uint16_t Length
, 
  43                                 uint16_t* const BytesProcessed
) 
  46         uint16_t BytesInTransfer 
= 0; 
  48         if ((ErrorCode 
= Endpoint_WaitUntilReady())) 
  51         if (BytesProcessed 
!= NULL
) 
  52           Length 
-= *BytesProcessed
; 
  56                 if (!(Endpoint_IsReadWriteAllowed())) 
  60                         if (BytesProcessed 
!= NULL
) 
  62                                 *BytesProcessed 
+= BytesInTransfer
; 
  63                                 return ENDPOINT_RWSTREAM_IncompleteTransfer
; 
  66                         if ((ErrorCode 
= Endpoint_WaitUntilReady())) 
  78         return ENDPOINT_RWSTREAM_NoError
; 
  81 uint8_t Endpoint_Null_Stream(uint16_t Length
, 
  82                              uint16_t* const BytesProcessed
) 
  85         uint16_t BytesInTransfer 
= 0; 
  87         if ((ErrorCode 
= Endpoint_WaitUntilReady())) 
  90         if (BytesProcessed 
!= NULL
) 
  91           Length 
-= *BytesProcessed
; 
  95                 if (!(Endpoint_IsReadWriteAllowed())) 
  99                         if (BytesProcessed 
!= NULL
) 
 101                                 *BytesProcessed 
+= BytesInTransfer
; 
 102                                 return ENDPOINT_RWSTREAM_IncompleteTransfer
; 
 105                         if ((ErrorCode 
= Endpoint_WaitUntilReady())) 
 117         return ENDPOINT_RWSTREAM_NoError
; 
 120 /* The following abuses the C preprocessor in order to copy-paste common code with slight alterations, 
 121  * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */ 
 123 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Stream_LE 
 124 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 125 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 126 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 127 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 128 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr) 
 129 #include "Template/Template_Endpoint_RW.c" 
 131 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Stream_BE 
 132 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 133 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 134 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 135 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 136 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr) 
 137 #include "Template/Template_Endpoint_RW.c" 
 139 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Stream_LE 
 140 #define  TEMPLATE_BUFFER_TYPE                      void* 
 141 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT() 
 142 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 143 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 144 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8() 
 145 #include "Template/Template_Endpoint_RW.c" 
 147 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Stream_BE 
 148 #define  TEMPLATE_BUFFER_TYPE                      void* 
 149 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT() 
 150 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 151 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 152 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8() 
 153 #include "Template/Template_Endpoint_RW.c" 
 155 #if defined(ARCH_HAS_FLASH_ADDRESS_SPACE) 
 156         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_PStream_LE 
 157         #define  TEMPLATE_BUFFER_TYPE                      const void* 
 158         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 159         #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 160         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 161         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(pgm_read_byte(BufferPtr)) 
 162         #include "Template/Template_Endpoint_RW.c" 
 164         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_PStream_BE 
 165         #define  TEMPLATE_BUFFER_TYPE                      const void* 
 166         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 167         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 168         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 169         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(pgm_read_byte(BufferPtr)) 
 170         #include "Template/Template_Endpoint_RW.c" 
 173 #if defined(ARCH_HAS_EEPROM_ADDRESS_SPACE) 
 174         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_EStream_LE 
 175         #define  TEMPLATE_BUFFER_TYPE                      const void* 
 176         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 177         #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 178         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 179         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(eeprom_read_byte(BufferPtr)) 
 180         #include "Template/Template_Endpoint_RW.c" 
 182         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_EStream_BE 
 183         #define  TEMPLATE_BUFFER_TYPE                      const void* 
 184         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 185         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 186         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 187         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(eeprom_read_byte(BufferPtr)) 
 188         #include "Template/Template_Endpoint_RW.c" 
 190         #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_EStream_LE 
 191         #define  TEMPLATE_BUFFER_TYPE                      void* 
 192         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT() 
 193         #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 194         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 195         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Endpoint_Read_8()) 
 196         #include "Template/Template_Endpoint_RW.c" 
 198         #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_EStream_BE 
 199         #define  TEMPLATE_BUFFER_TYPE                      void* 
 200         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT() 
 201         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 202         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 203         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Endpoint_Read_8()) 
 204         #include "Template/Template_Endpoint_RW.c" 
 209 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_Stream_LE 
 210 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 211 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 212 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr) 
 213 #include "Template/Template_Endpoint_Control_W.c" 
 215 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_Stream_BE 
 216 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 217 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 218 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr) 
 219 #include "Template/Template_Endpoint_Control_W.c" 
 221 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_Stream_LE 
 222 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 223 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 224 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8() 
 225 #include "Template/Template_Endpoint_Control_R.c" 
 227 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_Stream_BE 
 228 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 229 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 230 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8() 
 231 #include "Template/Template_Endpoint_Control_R.c" 
 233 #if defined(ARCH_HAS_FLASH_ADDRESS_SPACE) 
 234         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_PStream_LE 
 235         #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 236         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 237         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(pgm_read_byte(BufferPtr)) 
 238         #include "Template/Template_Endpoint_Control_W.c" 
 240         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_PStream_BE 
 241         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 242         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 243         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(pgm_read_byte(BufferPtr)) 
 244         #include "Template/Template_Endpoint_Control_W.c" 
 247 #if defined(ARCH_HAS_EEPROM_ADDRESS_SPACE) 
 248         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_EStream_LE 
 249         #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 250         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 251         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(eeprom_read_byte(BufferPtr)) 
 252         #include "Template/Template_Endpoint_Control_W.c" 
 254         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_EStream_BE 
 255         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 256         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 257         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(eeprom_read_byte(BufferPtr)) 
 258         #include "Template/Template_Endpoint_Control_W.c" 
 260         #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_EStream_LE 
 261         #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 262         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 263         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Endpoint_Read_8()) 
 264         #include "Template/Template_Endpoint_Control_R.c" 
 266         #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_EStream_BE 
 267         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 268         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 269         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Endpoint_Read_8()) 
 270         #include "Template/Template_Endpoint_Control_R.c"