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_HOST) 
  36 #include "PipeStream_AVR8.h" 
  38 uint8_t Pipe_Discard_Stream(uint16_t Length
, 
  39                             uint16_t* const BytesProcessed
) 
  42         uint16_t BytesInTransfer 
= 0; 
  44         Pipe_SetPipeToken(PIPE_TOKEN_IN
); 
  46         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
  49         if (BytesProcessed 
!= NULL
) 
  50           Length 
-= *BytesProcessed
; 
  54                 if (!(Pipe_IsReadWriteAllowed())) 
  58                         if (BytesProcessed 
!= NULL
) 
  60                                 *BytesProcessed 
+= BytesInTransfer
; 
  61                                 return PIPE_RWSTREAM_IncompleteTransfer
; 
  64                         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
  76         return PIPE_RWSTREAM_NoError
; 
  79 uint8_t Pipe_Null_Stream(uint16_t Length
, 
  80                          uint16_t* const BytesProcessed
) 
  83         uint16_t BytesInTransfer 
= 0; 
  85         Pipe_SetPipeToken(PIPE_TOKEN_OUT
); 
  87         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
  90         if (BytesProcessed 
!= NULL
) 
  91           Length 
-= *BytesProcessed
; 
  95                 if (!(Pipe_IsReadWriteAllowed())) 
  99                         if (BytesProcessed 
!= NULL
) 
 101                                 *BytesProcessed 
+= BytesInTransfer
; 
 102                                 return PIPE_RWSTREAM_IncompleteTransfer
; 
 107                         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 119         return PIPE_RWSTREAM_NoError
; 
 122 /* The following abuses the C preprocessor in order to copy-past common code with slight alterations, 
 123  * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */ 
 125 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_Stream_LE 
 126 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 127 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT 
 128 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT() 
 129 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 130 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream += Amount 
 131 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_8(*BufferPtr) 
 132 #include "Template/Template_Pipe_RW.c" 
 134 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_Stream_BE 
 135 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 136 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT 
 137 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT() 
 138 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 139 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream -= Amount 
 140 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_8(*BufferPtr) 
 141 #include "Template/Template_Pipe_RW.c" 
 143 #define  TEMPLATE_FUNC_NAME                        Pipe_Read_Stream_LE 
 144 #define  TEMPLATE_BUFFER_TYPE                      void* 
 145 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_IN 
 146 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearIN() 
 147 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 148 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream += Amount 
 149 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Pipe_Read_8() 
 150 #include "Template/Template_Pipe_RW.c" 
 152 #define  TEMPLATE_FUNC_NAME                        Pipe_Read_Stream_BE 
 153 #define  TEMPLATE_BUFFER_TYPE                      void* 
 154 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_IN 
 155 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearIN() 
 156 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 157 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream -= Amount 
 158 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Pipe_Read_8() 
 159 #include "Template/Template_Pipe_RW.c" 
 161 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_PStream_LE 
 162 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 163 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT 
 164 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT() 
 165 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 166 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream += Amount 
 167 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_8(pgm_read_byte(BufferPtr)) 
 168 #include "Template/Template_Pipe_RW.c" 
 170 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_PStream_BE 
 171 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 172 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT 
 173 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT() 
 174 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 175 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream -= Amount 
 176 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_8(pgm_read_byte(BufferPtr)) 
 177 #include "Template/Template_Pipe_RW.c" 
 179 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_EStream_LE 
 180 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 181 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT 
 182 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT() 
 183 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 184 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream += Amount 
 185 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_8(eeprom_read_byte(BufferPtr)) 
 186 #include "Template/Template_Pipe_RW.c" 
 188 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_EStream_BE 
 189 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 190 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT 
 191 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT() 
 192 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 193 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream -= Amount 
 194 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_8(eeprom_read_byte(BufferPtr)) 
 195 #include "Template/Template_Pipe_RW.c" 
 197 #define  TEMPLATE_FUNC_NAME                        Pipe_Read_EStream_LE 
 198 #define  TEMPLATE_BUFFER_TYPE                      void* 
 199 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_IN 
 200 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearIN() 
 201 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 202 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream += Amount 
 203 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Pipe_Read_8()) 
 204 #include "Template/Template_Pipe_RW.c" 
 206 #define  TEMPLATE_FUNC_NAME                        Pipe_Read_EStream_BE 
 207 #define  TEMPLATE_BUFFER_TYPE                      void* 
 208 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_IN 
 209 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearIN() 
 210 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 211 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream -= Amount 
 212 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Pipe_Read_8()) 
 213 #include "Template/Template_Pipe_RW.c"