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 
  33  *  Main source file for the RNDISEthernet demo. This file contains the main tasks of 
  34  *  the demo and is responsible for the initial application hardware configuration. 
  37 #include "RNDISEthernet.h" 
  39 /** LUFA RNDIS Class driver interface configuration and state information. This structure is 
  40  *  passed to all RNDIS Class driver functions, so that multiple instances of the same class 
  41  *  within a device can be differentiated from one another. 
  43 USB_ClassInfo_RNDIS_Device_t Ethernet_RNDIS_Interface 
= 
  47                                 .ControlInterfaceNumber         
= 0, 
  49                                 .DataINEndpointNumber           
= CDC_TX_EPNUM
, 
  50                                 .DataINEndpointSize             
= CDC_TXRX_EPSIZE
, 
  51                                 .DataINEndpointDoubleBank       
= false, 
  53                                 .DataOUTEndpointNumber          
= CDC_RX_EPNUM
, 
  54                                 .DataOUTEndpointSize            
= CDC_TXRX_EPSIZE
, 
  55                                 .DataOUTEndpointDoubleBank      
= false, 
  57                                 .NotificationEndpointNumber     
= CDC_NOTIFICATION_EPNUM
, 
  58                                 .NotificationEndpointSize       
= CDC_NOTIFICATION_EPSIZE
, 
  59                                 .NotificationEndpointDoubleBank 
= false, 
  61                                 .AdapterVendorDescription       
= "LUFA RNDIS Demo Adapter", 
  62                                 .AdapterMACAddress              
= {ADAPTER_MAC_ADDRESS
}, 
  66 /** Global to store the incoming frame from the host before it is processed by the device. */ 
  67 static Ethernet_Frame_Info_t FrameIN
; 
  69 /** Global to store the outgoing frame created in the device before it is sent to the host. */ 
  70 static Ethernet_Frame_Info_t FrameOUT
; 
  72 /** Main program entry point. This routine contains the overall program flow, including initial 
  73  *  setup of all components and the main program loop. 
  82         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  87                 if (RNDIS_Device_IsPacketReceived(&Ethernet_RNDIS_Interface
)) 
  89                         LEDs_SetAllLEDs(LEDMASK_USB_BUSY
); 
  91                         RNDIS_Device_ReadPacket(&Ethernet_RNDIS_Interface
, &FrameIN
.FrameData
, &FrameIN
.FrameLength
); 
  92                         Ethernet_ProcessPacket(&FrameIN
, &FrameOUT
); 
  94                         if (FrameOUT
.FrameLength
) 
  96                                 RNDIS_Device_SendPacket(&Ethernet_RNDIS_Interface
, &FrameOUT
.FrameData
, FrameOUT
.FrameLength
);                           
  97                                 FrameOUT
.FrameLength 
= 0; 
 100                         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 103                 TCP_TCPTask(&Ethernet_RNDIS_Interface
, &FrameOUT
); 
 105                 RNDIS_Device_USBTask(&Ethernet_RNDIS_Interface
); 
 110 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
 111 void SetupHardware(void) 
 113         /* Disable watchdog if enabled by bootloader/fuses */ 
 114         MCUSR 
&= ~(1 << WDRF
); 
 117         /* Disable clock division */ 
 118         clock_prescale_set(clock_div_1
); 
 120         /* Hardware Initialization */ 
 122         Serial_Init(9600, false); 
 125         /* Create a stdio stream for the serial port for stdin and stdout */ 
 126         Serial_CreateStream(NULL
); 
 129 /** Event handler for the library USB Connection event. */ 
 130 void EVENT_USB_Device_Connect(void) 
 132         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 135 /** Event handler for the library USB Disconnection event. */ 
 136 void EVENT_USB_Device_Disconnect(void) 
 138         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 141 /** Event handler for the library USB Configuration Changed event. */ 
 142 void EVENT_USB_Device_ConfigurationChanged(void) 
 144         bool ConfigSuccess 
= true; 
 146         ConfigSuccess 
&= RNDIS_Device_ConfigureEndpoints(&Ethernet_RNDIS_Interface
); 
 148         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY 
: LEDMASK_USB_ERROR
); 
 151 /** Event handler for the library USB Control Request reception event. */ 
 152 void EVENT_USB_Device_ControlRequest(void) 
 154         RNDIS_Device_ProcessControlRequest(&Ethernet_RNDIS_Interface
);