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 
  31 #define  __INCLUDE_FROM_USB_DRIVER 
  32 #include "../USBMode.h" 
  34 #if defined(USB_CAN_BE_DEVICE) 
  36 #include "EndpointStream_AVR8.h" 
  38 #if !defined(CONTROL_ONLY_DEVICE) 
  39 uint8_t Endpoint_Discard_Stream(uint16_t Length
, 
  40                                 uint16_t* const BytesProcessed
) 
  43         uint16_t BytesInTransfer 
= 0; 
  45         if ((ErrorCode 
= Endpoint_WaitUntilReady())) 
  48         if (BytesProcessed 
!= NULL
) 
  49           Length 
-= *BytesProcessed
; 
  53                 if (!(Endpoint_IsReadWriteAllowed())) 
  57                         if (BytesProcessed 
!= NULL
) 
  59                                 *BytesProcessed 
+= BytesInTransfer
; 
  60                                 return ENDPOINT_RWSTREAM_IncompleteTransfer
; 
  63                         if ((ErrorCode 
= Endpoint_WaitUntilReady())) 
  75         return ENDPOINT_RWSTREAM_NoError
; 
  78 uint8_t Endpoint_Null_Stream(uint16_t Length
, 
  79                              uint16_t* const BytesProcessed
) 
  82         uint16_t BytesInTransfer 
= 0; 
  84         if ((ErrorCode 
= Endpoint_WaitUntilReady())) 
  87         if (BytesProcessed 
!= NULL
) 
  88           Length 
-= *BytesProcessed
; 
  92                 if (!(Endpoint_IsReadWriteAllowed())) 
  96                         if (BytesProcessed 
!= NULL
) 
  98                                 *BytesProcessed 
+= BytesInTransfer
; 
  99                                 return ENDPOINT_RWSTREAM_IncompleteTransfer
; 
 102                         if ((ErrorCode 
= Endpoint_WaitUntilReady())) 
 114         return ENDPOINT_RWSTREAM_NoError
; 
 117 /* The following abuses the C preprocessor in order to copy-paste common code with slight alterations, 
 118  * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */ 
 120 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Stream_LE 
 121 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 122 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 123 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 124 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 125 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr) 
 126 #include "Template/Template_Endpoint_RW.c" 
 128 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Stream_BE 
 129 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 130 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 131 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 132 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 133 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr) 
 134 #include "Template/Template_Endpoint_RW.c" 
 136 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Stream_LE 
 137 #define  TEMPLATE_BUFFER_TYPE                      void* 
 138 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT() 
 139 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 140 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 141 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8() 
 142 #include "Template/Template_Endpoint_RW.c" 
 144 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Stream_BE 
 145 #define  TEMPLATE_BUFFER_TYPE                      void* 
 146 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT() 
 147 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 148 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 149 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8() 
 150 #include "Template/Template_Endpoint_RW.c" 
 152 #if defined(ARCH_HAS_FLASH_ADDRESS_SPACE) 
 153         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_PStream_LE 
 154         #define  TEMPLATE_BUFFER_TYPE                      const void* 
 155         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 156         #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 157         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 158         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(pgm_read_byte(BufferPtr)) 
 159         #include "Template/Template_Endpoint_RW.c" 
 161         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_PStream_BE 
 162         #define  TEMPLATE_BUFFER_TYPE                      const void* 
 163         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 164         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 165         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 166         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(pgm_read_byte(BufferPtr)) 
 167         #include "Template/Template_Endpoint_RW.c" 
 170 #if defined(ARCH_HAS_EEPROM_ADDRESS_SPACE) 
 171         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_EStream_LE 
 172         #define  TEMPLATE_BUFFER_TYPE                      const void* 
 173         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 174         #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 175         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 176         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(eeprom_read_byte(BufferPtr)) 
 177         #include "Template/Template_Endpoint_RW.c" 
 179         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_EStream_BE 
 180         #define  TEMPLATE_BUFFER_TYPE                      const void* 
 181         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 182         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 183         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 184         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(eeprom_read_byte(BufferPtr)) 
 185         #include "Template/Template_Endpoint_RW.c" 
 187         #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_EStream_LE 
 188         #define  TEMPLATE_BUFFER_TYPE                      void* 
 189         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT() 
 190         #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 191         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 192         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Endpoint_Read_8()) 
 193         #include "Template/Template_Endpoint_RW.c" 
 195         #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_EStream_BE 
 196         #define  TEMPLATE_BUFFER_TYPE                      void* 
 197         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT() 
 198         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 199         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 200         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Endpoint_Read_8()) 
 201         #include "Template/Template_Endpoint_RW.c" 
 206 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_Stream_LE 
 207 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 208 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 209 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr) 
 210 #include "Template/Template_Endpoint_Control_W.c" 
 212 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_Stream_BE 
 213 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 214 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 215 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr) 
 216 #include "Template/Template_Endpoint_Control_W.c" 
 218 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_Stream_LE 
 219 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 220 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 221 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8() 
 222 #include "Template/Template_Endpoint_Control_R.c" 
 224 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_Stream_BE 
 225 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 226 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 227 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8() 
 228 #include "Template/Template_Endpoint_Control_R.c" 
 230 #if defined(ARCH_HAS_FLASH_ADDRESS_SPACE) 
 231         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_PStream_LE 
 232         #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 233         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 234         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(pgm_read_byte(BufferPtr)) 
 235         #include "Template/Template_Endpoint_Control_W.c" 
 237         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_PStream_BE 
 238         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 239         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 240         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(pgm_read_byte(BufferPtr)) 
 241         #include "Template/Template_Endpoint_Control_W.c" 
 244 #if defined(ARCH_HAS_EEPROM_ADDRESS_SPACE) 
 245         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_EStream_LE 
 246         #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 247         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 248         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(eeprom_read_byte(BufferPtr)) 
 249         #include "Template/Template_Endpoint_Control_W.c" 
 251         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_EStream_BE 
 252         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 253         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 254         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(eeprom_read_byte(BufferPtr)) 
 255         #include "Template/Template_Endpoint_Control_W.c" 
 257         #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_EStream_LE 
 258         #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 259         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 260         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Endpoint_Read_8()) 
 261         #include "Template/Template_Endpoint_Control_R.c" 
 263         #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_EStream_BE 
 264         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 265         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 266         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Endpoint_Read_8()) 
 267         #include "Template/Template_Endpoint_Control_R.c"