3      Copyright (C) Dean Camera, 2018. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2018  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 disclaims 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 
= BytesProcessed ? 
*BytesProcessed 
: 0; 
  48         if ((ErrorCode 
= Endpoint_WaitUntilReady())) 
  51         while (BytesInTransfer 
< Length
) 
  53                 if (!(Endpoint_IsReadWriteAllowed())) 
  57                         if (BytesProcessed 
!= NULL
) 
  59                                 *BytesProcessed 
= BytesInTransfer
; 
  60                                 return ENDPOINT_RWSTREAM_IncompleteTransfer
; 
  63                         if ((ErrorCode 
= Endpoint_WaitUntilReady())) 
  73         return ENDPOINT_RWSTREAM_NoError
; 
  76 uint8_t Endpoint_Null_Stream(uint16_t Length
, 
  77                              uint16_t* const BytesProcessed
) 
  80         uint16_t BytesInTransfer 
= BytesProcessed ? 
*BytesProcessed 
: 0; 
  82         if ((ErrorCode 
= Endpoint_WaitUntilReady())) 
  85         while (BytesInTransfer 
< Length
) 
  87                 if (!(Endpoint_IsReadWriteAllowed())) 
  91                         if (BytesProcessed 
!= NULL
) 
  93                                 *BytesProcessed 
= BytesInTransfer
; 
  94                                 return ENDPOINT_RWSTREAM_IncompleteTransfer
; 
  97                         if ((ErrorCode 
= Endpoint_WaitUntilReady())) 
 107         return ENDPOINT_RWSTREAM_NoError
; 
 110 /* The following abuses the C preprocessor in order to copy-paste common code with slight alterations, 
 111  * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */ 
 113 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Stream_LE 
 114 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 115 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 116 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 117 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 118 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr) 
 119 #include "Template/Template_Endpoint_RW.c" 
 121 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Stream_BE 
 122 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 123 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 124 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 125 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 126 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr) 
 127 #include "Template/Template_Endpoint_RW.c" 
 129 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Stream_LE 
 130 #define  TEMPLATE_BUFFER_TYPE                      void* 
 131 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT() 
 132 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 133 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 134 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8() 
 135 #include "Template/Template_Endpoint_RW.c" 
 137 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Stream_BE 
 138 #define  TEMPLATE_BUFFER_TYPE                      void* 
 139 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT() 
 140 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 141 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 142 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8() 
 143 #include "Template/Template_Endpoint_RW.c" 
 145 #if defined(ARCH_HAS_FLASH_ADDRESS_SPACE) 
 146         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_PStream_LE 
 147         #define  TEMPLATE_BUFFER_TYPE                      const void* 
 148         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 149         #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 150         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 151         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(pgm_read_byte(BufferPtr)) 
 152         #include "Template/Template_Endpoint_RW.c" 
 154         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_PStream_BE 
 155         #define  TEMPLATE_BUFFER_TYPE                      const void* 
 156         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 157         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 158         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 159         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(pgm_read_byte(BufferPtr)) 
 160         #include "Template/Template_Endpoint_RW.c" 
 163 #if defined(ARCH_HAS_EEPROM_ADDRESS_SPACE) 
 164         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_EStream_LE 
 165         #define  TEMPLATE_BUFFER_TYPE                      const void* 
 166         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 167         #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 168         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 169         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(eeprom_read_byte(BufferPtr)) 
 170         #include "Template/Template_Endpoint_RW.c" 
 172         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_EStream_BE 
 173         #define  TEMPLATE_BUFFER_TYPE                      const void* 
 174         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 175         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 176         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 177         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(eeprom_read_byte(BufferPtr)) 
 178         #include "Template/Template_Endpoint_RW.c" 
 180         #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_EStream_LE 
 181         #define  TEMPLATE_BUFFER_TYPE                      void* 
 182         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT() 
 183         #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 184         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 185         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Endpoint_Read_8()) 
 186         #include "Template/Template_Endpoint_RW.c" 
 188         #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_EStream_BE 
 189         #define  TEMPLATE_BUFFER_TYPE                      void* 
 190         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT() 
 191         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 192         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 193         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Endpoint_Read_8()) 
 194         #include "Template/Template_Endpoint_RW.c" 
 199 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_Stream_LE 
 200 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 201 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 202 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr) 
 203 #include "Template/Template_Endpoint_Control_W.c" 
 205 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_Stream_BE 
 206 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 207 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 208 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr) 
 209 #include "Template/Template_Endpoint_Control_W.c" 
 211 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_Stream_LE 
 212 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 213 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 214 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8() 
 215 #include "Template/Template_Endpoint_Control_R.c" 
 217 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_Stream_BE 
 218 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 219 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 220 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8() 
 221 #include "Template/Template_Endpoint_Control_R.c" 
 223 #if defined(ARCH_HAS_FLASH_ADDRESS_SPACE) 
 224         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_PStream_LE 
 225         #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 226         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 227         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(pgm_read_byte(BufferPtr)) 
 228         #include "Template/Template_Endpoint_Control_W.c" 
 230         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_PStream_BE 
 231         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 232         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 233         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(pgm_read_byte(BufferPtr)) 
 234         #include "Template/Template_Endpoint_Control_W.c" 
 237 #if defined(ARCH_HAS_EEPROM_ADDRESS_SPACE) 
 238         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_EStream_LE 
 239         #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 240         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 241         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(eeprom_read_byte(BufferPtr)) 
 242         #include "Template/Template_Endpoint_Control_W.c" 
 244         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_EStream_BE 
 245         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 246         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 247         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(eeprom_read_byte(BufferPtr)) 
 248         #include "Template/Template_Endpoint_Control_W.c" 
 250         #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_EStream_LE 
 251         #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 252         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount 
 253         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Endpoint_Read_8()) 
 254         #include "Template/Template_Endpoint_Control_R.c" 
 256         #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_EStream_BE 
 257         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 258         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount 
 259         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Endpoint_Read_8()) 
 260         #include "Template/Template_Endpoint_Control_R.c"