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
33 * Main source file for the RNDISEthernet demo. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
37 #include "RNDISEthernet.h"
39 /** Main program entry point. This routine configures the hardware required by the application, then
40 * enters a loop to run the application tasks in sequence.
46 /* Webserver Initialization */
50 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
62 /** Configures the board hardware and chip peripherals for the demo's functionality. */
63 void SetupHardware(void)
65 /* Disable watchdog if enabled by bootloader/fuses */
66 MCUSR
&= ~(1 << WDRF
);
69 /* Disable clock division */
70 clock_prescale_set(clock_div_1
);
72 /* Hardware Initialization */
74 SerialStream_Init(9600, false);
78 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
79 * starts the library USB task to begin the enumeration and USB management process.
81 void EVENT_USB_Device_Connect(void)
83 /* Indicate USB enumerating */
84 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
87 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
88 * the status LEDs and stops all the relevant tasks.
90 void EVENT_USB_Device_Disconnect(void)
92 /* Indicate USB not ready */
93 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
96 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
97 * of the USB device after enumeration, and configures the RNDIS device endpoints and starts the relevant tasks.
99 void EVENT_USB_Device_ConfigurationChanged(void)
101 bool ConfigSuccess
= true;
103 /* Setup RNDIS Data Endpoints */
104 ConfigSuccess
&= Endpoint_ConfigureEndpoint(CDC_TX_EPNUM
, EP_TYPE_BULK
, ENDPOINT_DIR_IN
,
105 CDC_TXRX_EPSIZE
, ENDPOINT_BANK_SINGLE
);
106 ConfigSuccess
&= Endpoint_ConfigureEndpoint(CDC_RX_EPNUM
, EP_TYPE_BULK
, ENDPOINT_DIR_OUT
,
107 CDC_TXRX_EPSIZE
, ENDPOINT_BANK_SINGLE
);
108 ConfigSuccess
&= Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_IN
,
109 CDC_NOTIFICATION_EPSIZE
, ENDPOINT_BANK_SINGLE
);
111 /* Indicate endpoint configuration success or failure */
112 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY
: LEDMASK_USB_ERROR
);
115 /** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific
116 * control requests that are not handled internally by the USB library (including the RNDIS control commands,
117 * which set up the USB RNDIS network adapter), so that they can be handled appropriately for the application.
119 void EVENT_USB_Device_UnhandledControlRequest(void)
121 /* Process RNDIS class commands */
122 switch (USB_ControlRequest
.bRequest
)
124 case REQ_SendEncapsulatedCommand
:
125 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
127 /* Clear the SETUP packet, ready for data transfer */
128 Endpoint_ClearSETUP();
130 /* Read in the RNDIS message into the message buffer */
131 Endpoint_Read_Control_Stream_LE(RNDISMessageBuffer
, USB_ControlRequest
.wLength
);
133 /* Finalize the stream transfer to clear the last packet from the host */
136 /* Process the RNDIS message */
137 ProcessRNDISControlMessage();
141 case REQ_GetEncapsulatedResponse
:
142 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
144 /* Clear the SETUP packet, ready for data transfer */
145 Endpoint_ClearSETUP();
147 /* Check if a response to the last message is ready */
148 if (!(MessageHeader
->MessageLength
))
150 /* Set the response to a single 0x00 byte to indicate that no response is ready */
151 RNDISMessageBuffer
[0] = 0;
152 MessageHeader
->MessageLength
= 1;
155 /* Write the message response data to the endpoint */
156 Endpoint_Write_Control_Stream_LE(RNDISMessageBuffer
, MessageHeader
->MessageLength
);
158 /* Finalize the stream transfer to send the last packet or clear the host abort */
161 /* Reset the message header once again after transmission */
162 MessageHeader
->MessageLength
= 0;
169 /** Task to manage the sending and receiving of encapsulated RNDIS data and notifications. This removes the RNDIS
170 * wrapper from received Ethernet frames and places them in the FrameIN global buffer, or adds the RNDIS wrapper
171 * to a frame in the FrameOUT global before sending the buffer contents to the host.
173 void RNDIS_Task(void)
175 /* Select the notification endpoint */
176 Endpoint_SelectEndpoint(CDC_NOTIFICATION_EPNUM
);
178 /* Check if a message response is ready for the host */
179 if (Endpoint_IsINReady() && ResponseReady
)
181 USB_Notification_t Notification
= (USB_Notification_t
)
183 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
184 .bNotification
= NOTIF_RESPONSE_AVAILABLE
,
190 /* Indicate that a message response is ready for the host */
191 Endpoint_Write_Stream_LE(&Notification
, sizeof(Notification
));
193 /* Finalize the stream transfer to send the last packet */
196 /* Indicate a response is no longer ready */
197 ResponseReady
= false;
200 /* Don't process the data endpoints until the system is in the data initialized state, and the buffer is free */
201 if ((CurrRNDISState
== RNDIS_Data_Initialized
) && !(MessageHeader
->MessageLength
))
203 /* Create a new packet header for reading/writing */
204 RNDIS_Packet_Message_t RNDISPacketHeader
;
206 /* Select the data OUT endpoint */
207 Endpoint_SelectEndpoint(CDC_RX_EPNUM
);
209 /* Check if the data OUT endpoint contains data, and that the IN buffer is empty */
210 if (Endpoint_IsOUTReceived() && !(FrameIN
.FrameInBuffer
))
212 /* Read in the packet message header */
213 Endpoint_Read_Stream_LE(&RNDISPacketHeader
, sizeof(RNDIS_Packet_Message_t
));
215 /* Stall the request if the data is too large */
216 if (RNDISPacketHeader
.DataLength
> ETHERNET_FRAME_SIZE_MAX
)
218 Endpoint_StallTransaction();
222 /* Read in the Ethernet frame into the buffer */
223 Endpoint_Read_Stream_LE(FrameIN
.FrameData
, RNDISPacketHeader
.DataLength
);
225 /* Finalize the stream transfer to send the last packet */
228 /* Store the size of the Ethernet frame */
229 FrameIN
.FrameLength
= RNDISPacketHeader
.DataLength
;
231 /* Indicate Ethernet IN buffer full */
232 FrameIN
.FrameInBuffer
= true;
235 /* Select the data IN endpoint */
236 Endpoint_SelectEndpoint(CDC_TX_EPNUM
);
238 /* Check if the data IN endpoint is ready for more data, and that the IN buffer is full */
239 if (Endpoint_IsINReady() && FrameOUT
.FrameInBuffer
)
241 /* Clear the packet header with all 0s so that the relevant fields can be filled */
242 memset(&RNDISPacketHeader
, 0, sizeof(RNDIS_Packet_Message_t
));
244 /* Construct the required packet header fields in the buffer */
245 RNDISPacketHeader
.MessageType
= REMOTE_NDIS_PACKET_MSG
;
246 RNDISPacketHeader
.MessageLength
= (sizeof(RNDIS_Packet_Message_t
) + FrameOUT
.FrameLength
);
247 RNDISPacketHeader
.DataOffset
= (sizeof(RNDIS_Packet_Message_t
) - sizeof(RNDIS_Message_Header_t
));
248 RNDISPacketHeader
.DataLength
= FrameOUT
.FrameLength
;
250 /* Send the packet header to the host */
251 Endpoint_Write_Stream_LE(&RNDISPacketHeader
, sizeof(RNDIS_Packet_Message_t
));
253 /* Send the Ethernet frame data to the host */
254 Endpoint_Write_Stream_LE(FrameOUT
.FrameData
, RNDISPacketHeader
.DataLength
);
256 /* Finalize the stream transfer to send the last packet */
259 /* Indicate Ethernet OUT buffer no longer full */
260 FrameOUT
.FrameInBuffer
= false;
265 /** Ethernet frame processing task. This task checks to see if a frame has been received, and if so hands off the processing
266 * of the frame to the Ethernet processing routines.
268 void Ethernet_Task(void)
270 /* Task for Ethernet processing. Incoming ethernet frames are loaded into the FrameIN structure, and
271 outgoing frames should be loaded into the FrameOUT structure. Both structures can only hold a single
272 Ethernet frame at a time, so the FrameInBuffer bool is used to indicate when the buffers contain data. */
274 /* Device must be connected and configured for the task to run */
275 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
278 /* Check if a frame has been written to the IN frame buffer */
279 if (FrameIN
.FrameInBuffer
)
281 /* Indicate packet processing started */
282 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
);
284 /* Process the ethernet frame - replace this with your own Ethernet handler code as desired */
285 Ethernet_ProcessPacket();
287 /* Indicate packet processing complete */
288 LEDs_SetAllLEDs(LEDMASK_USB_READY
);