3      Copyright (C) Dean Camera, 2010. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2010  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 "../HighLevel/USBMode.h" 
  34 #if defined(USB_CAN_BE_DEVICE) 
  36 #define  __INCLUDE_FROM_ENDPOINT_C 
  39 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE) 
  40 uint8_t USB_ControlEndpointSize 
= ENDPOINT_CONTROLEP_DEFAULT_SIZE
; 
  43 uint8_t Endpoint_BytesToEPSizeMaskDynamic(const uint16_t Size
) 
  45         return Endpoint_BytesToEPSizeMask(Size
); 
  48 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number
, const uint8_t UECFG0XData
, const uint8_t UECFG1XData
) 
  50         Endpoint_SelectEndpoint(Number
); 
  51         Endpoint_EnableEndpoint(); 
  55         UECFG0X 
= UECFG0XData
; 
  56         UECFG1X 
= UECFG1XData
; 
  58         return Endpoint_IsConfigured(); 
  61 void Endpoint_ClearEndpoints(void) 
  65         for (uint8_t EPNum 
= 0; EPNum 
< ENDPOINT_TOTAL_ENDPOINTS
; EPNum
++) 
  67                 Endpoint_SelectEndpoint(EPNum
);  
  71                 Endpoint_DisableEndpoint(); 
  75 void Endpoint_ClearStatusStage(void) 
  77         if (USB_ControlRequest
.bmRequestType 
& REQDIR_DEVICETOHOST
) 
  79                 while (!(Endpoint_IsOUTReceived())) 
  81                         if (USB_DeviceState 
== DEVICE_STATE_Unattached
) 
  89                 while (!(Endpoint_IsINReady())) 
  91                         if (USB_DeviceState 
== DEVICE_STATE_Unattached
) 
  99 #if !defined(CONTROL_ONLY_DEVICE) 
 100 uint8_t Endpoint_WaitUntilReady(void) 
 102         #if (USB_STREAM_TIMEOUT_MS < 0xFF) 
 103         uint8_t  TimeoutMSRem 
= USB_STREAM_TIMEOUT_MS
;   
 105         uint16_t TimeoutMSRem 
= USB_STREAM_TIMEOUT_MS
; 
 110                 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN
) 
 112                         if (Endpoint_IsINReady()) 
 113                           return ENDPOINT_READYWAIT_NoError
; 
 117                         if (Endpoint_IsOUTReceived()) 
 118                           return ENDPOINT_READYWAIT_NoError
;             
 121                 if (USB_DeviceState 
== DEVICE_STATE_Unattached
) 
 122                   return ENDPOINT_READYWAIT_DeviceDisconnected
; 
 123                 else if (USB_DeviceState 
== DEVICE_STATE_Suspended
) 
 124                   return ENDPOINT_READYWAIT_BusSuspended
; 
 125                 else if (Endpoint_IsStalled()) 
 126                   return ENDPOINT_READYWAIT_EndpointStalled
; 
 128                 if (USB_INT_HasOccurred(USB_INT_SOFI
)) 
 130                         USB_INT_Clear(USB_INT_SOFI
); 
 132                         if (!(TimeoutMSRem
--)) 
 133                           return ENDPOINT_READYWAIT_Timeout
; 
 138 uint8_t Endpoint_Discard_Stream(uint16_t Length
 
 139 #if !defined(NO_STREAM_CALLBACKS) 
 140                                 , StreamCallbackPtr_t Callback
 
 146         if ((ErrorCode 
= Endpoint_WaitUntilReady())) 
 149         #if defined(FAST_STREAM_TRANSFERS) 
 150         uint8_t BytesRemToAlignment 
= (Endpoint_BytesInEndpoint() & 0x07); 
 154                 Length 
-= BytesRemToAlignment
; 
 156                 switch (BytesRemToAlignment
) 
 161                                         if (!(Endpoint_IsReadWriteAllowed())) 
 165                                                 #if !defined(NO_STREAM_CALLBACKS) 
 166                                                 if ((Callback 
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
)) 
 167                                                   return ENDPOINT_RWSTREAM_CallbackAborted
; 
 170                                                 if ((ErrorCode 
= Endpoint_WaitUntilReady())) 
 176                                         Endpoint_Discard_Byte(); 
 177                         case 7: Endpoint_Discard_Byte(); 
 178                         case 6: Endpoint_Discard_Byte(); 
 179                         case 5: Endpoint_Discard_Byte(); 
 180                         case 4: Endpoint_Discard_Byte(); 
 181                         case 3: Endpoint_Discard_Byte(); 
 182                         case 2: Endpoint_Discard_Byte(); 
 183                         case 1: Endpoint_Discard_Byte(); 
 184                                 } while (Length 
>= 8);   
 191                 if (!(Endpoint_IsReadWriteAllowed())) 
 195                         #if !defined(NO_STREAM_CALLBACKS) 
 196                         if ((Callback 
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
)) 
 197                           return ENDPOINT_RWSTREAM_CallbackAborted
; 
 200                         if ((ErrorCode 
= Endpoint_WaitUntilReady())) 
 205                         Endpoint_Discard_Byte(); 
 210         return ENDPOINT_RWSTREAM_NoError
; 
 213 /* The following abuses the C preprocessor in order to copy-past common code with slight alterations, 
 214  * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */ 
 216 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Stream_LE 
 217 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 218 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 219 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 220 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(*((uint8_t*)BufferPtr++)) 
 221 #include "Template/Template_Endpoint_RW.c" 
 223 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_PStream_LE 
 224 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 225 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 226 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 227 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++)) 
 228 #include "Template/Template_Endpoint_RW.c" 
 230 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_EStream_LE 
 231 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 232 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 233 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 234 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++)) 
 235 #include "Template/Template_Endpoint_RW.c" 
 237 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Stream_BE 
 238 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 239 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 240 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 241 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(*((uint8_t*)BufferPtr--)) 
 242 #include "Template/Template_Endpoint_RW.c" 
 244 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_EStream_BE 
 245 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 246 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 247 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 248 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--)) 
 249 #include "Template/Template_Endpoint_RW.c" 
 251 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_PStream_BE 
 252 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 253 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN() 
 254 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 255 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--)) 
 256 #include "Template/Template_Endpoint_RW.c" 
 258 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Stream_LE 
 259 #define  TEMPLATE_BUFFER_TYPE                      void* 
 260 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT() 
 261 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 262 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *((uint8_t*)BufferPtr++) = Endpoint_Read_Byte() 
 263 #include "Template/Template_Endpoint_RW.c" 
 265 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_EStream_LE 
 266 #define  TEMPLATE_BUFFER_TYPE                      void* 
 267 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT() 
 268 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 269 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte((uint8_t*)BufferPtr++, Endpoint_Read_Byte()) 
 270 #include "Template/Template_Endpoint_RW.c" 
 272 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Stream_BE 
 273 #define  TEMPLATE_BUFFER_TYPE                      void* 
 274 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT() 
 275 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 276 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *((uint8_t*)BufferPtr--) = Endpoint_Read_Byte() 
 277 #include "Template/Template_Endpoint_RW.c" 
 279 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_EStream_BE 
 280 #define  TEMPLATE_BUFFER_TYPE                      void* 
 281 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT() 
 282 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 283 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte((uint8_t*)BufferPtr--, Endpoint_Read_Byte()) 
 284 #include "Template/Template_Endpoint_RW.c" 
 288 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_Stream_LE 
 289 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 290 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(*((uint8_t*)BufferPtr++)) 
 291 #include "Template/Template_Endpoint_Control_W.c" 
 293 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_PStream_LE 
 294 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 295 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++)) 
 296 #include "Template/Template_Endpoint_Control_W.c" 
 298 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_EStream_LE 
 299 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 300 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++)) 
 301 #include "Template/Template_Endpoint_Control_W.c" 
 303 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_Stream_BE 
 304 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 305 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(*((uint8_t*)BufferPtr--)) 
 306 #include "Template/Template_Endpoint_Control_W.c" 
 308 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_PStream_BE 
 309 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 310 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--)) 
 311 #include "Template/Template_Endpoint_Control_W.c" 
 313 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_EStream_BE 
 314 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 315 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--)) 
 316 #include "Template/Template_Endpoint_Control_W.c" 
 318 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_Stream_LE 
 319 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 320 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *((uint8_t*)BufferPtr++) = Endpoint_Read_Byte() 
 321 #include "Template/Template_Endpoint_Control_R.c" 
 323 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_EStream_LE 
 324 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 325 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte((uint8_t*)BufferPtr++, Endpoint_Read_Byte()) 
 326 #include "Template/Template_Endpoint_Control_R.c" 
 328 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_Stream_BE 
 329 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 330 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *((uint8_t*)BufferPtr--) = Endpoint_Read_Byte() 
 331 #include "Template/Template_Endpoint_Control_R.c" 
 333 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_EStream_BE 
 334 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 335 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte((uint8_t*)BufferPtr--, Endpoint_Read_Byte()) 
 336 #include "Template/Template_Endpoint_Control_R.c"