3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 * starts the scheduler to run the USB management task.
46 /* Webserver Initialization */
50 printf_P(PSTR("\r\n\r\n****** RNDIS Demo running. ******\r\n"));
52 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
63 /** Configures the board hardware and chip peripherals for the demo's functionality. */
64 void SetupHardware(void)
66 /* Disable watchdog if enabled by bootloader/fuses */
67 MCUSR
&= ~(1 << WDRF
);
70 /* Disable clock division */
71 clock_prescale_set(clock_div_1
);
73 /* Hardware Initialization */
75 SerialStream_Init(9600, false);
79 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
80 * starts the library USB task to begin the enumeration and USB management process.
82 void EVENT_USB_Connect(void)
84 /* Indicate USB enumerating */
85 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
88 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
89 * the status LEDs and stops all the relevant tasks.
91 void EVENT_USB_Disconnect(void)
93 /* Indicate USB not ready */
94 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
97 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
98 * of the USB device after enumeration, and configures the RNDIS device endpoints and starts the relevant tasks.
100 void EVENT_USB_ConfigurationChanged(void)
102 /* Setup CDC Notification, Rx and Tx Endpoints */
103 Endpoint_ConfigureEndpoint(CDC_TX_EPNUM
, EP_TYPE_BULK
,
104 ENDPOINT_DIR_IN
, CDC_TXRX_EPSIZE
,
105 ENDPOINT_BANK_SINGLE
);
107 Endpoint_ConfigureEndpoint(CDC_RX_EPNUM
, EP_TYPE_BULK
,
108 ENDPOINT_DIR_OUT
, CDC_TXRX_EPSIZE
,
109 ENDPOINT_BANK_SINGLE
);
111 Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM
, EP_TYPE_INTERRUPT
,
112 ENDPOINT_DIR_IN
, CDC_NOTIFICATION_EPSIZE
,
113 ENDPOINT_BANK_SINGLE
);
115 /* Indicate USB connected and ready */
116 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
119 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
120 * control requests that are not handled internally by the USB library (including the RNDIS control commands,
121 * which set up the USB RNDIS network adapter), so that they can be handled appropriately for the application.
123 void EVENT_USB_UnhandledControlPacket(void)
125 /* Process RNDIS class commands */
126 switch (USB_ControlRequest
.bRequest
)
128 case REQ_SendEncapsulatedCommand
:
129 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
131 /* Clear the SETUP packet, ready for data transfer */
132 Endpoint_ClearSETUP();
134 /* Read in the RNDIS message into the message buffer */
135 Endpoint_Read_Control_Stream_LE(RNDISMessageBuffer
, USB_ControlRequest
.wLength
);
137 /* Finalize the stream transfer to clear the last packet from the host */
140 /* Process the RNDIS message */
141 ProcessRNDISControlMessage();
145 case REQ_GetEncapsulatedResponse
:
146 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
148 /* Clear the SETUP packet, ready for data transfer */
149 Endpoint_ClearSETUP();
151 /* Check if a response to the last message is ready */
152 if (!(MessageHeader
->MessageLength
))
154 /* Set the response to a single 0x00 byte to indicate that no response is ready */
155 RNDISMessageBuffer
[0] = 0;
156 MessageHeader
->MessageLength
= 1;
159 /* Write the message response data to the endpoint */
160 Endpoint_Write_Control_Stream_LE(RNDISMessageBuffer
, MessageHeader
->MessageLength
);
162 /* Finalize the stream transfer to send the last packet or clear the host abort */
165 /* Reset the message header once again after transmission */
166 MessageHeader
->MessageLength
= 0;
173 /** Task to manage the sending and receiving of encapsulated RNDIS data and notifications. This removes the RNDIS
174 * wrapper from received Ethernet frames and places them in the FrameIN global buffer, or adds the RNDIS wrapper
175 * to a frame in the FrameOUT global before sending the buffer contents to the host.
177 void RNDIS_Task(void)
179 /* Select the notification endpoint */
180 Endpoint_SelectEndpoint(CDC_NOTIFICATION_EPNUM
);
182 /* Check if a message response is ready for the host */
183 if (Endpoint_IsINReady() && ResponseReady
)
185 USB_Notification_t Notification
= (USB_Notification_t
)
187 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
188 .bNotification
= NOTIF_RESPONSE_AVAILABLE
,
194 /* Indicate that a message response is ready for the host */
195 Endpoint_Write_Stream_LE(&Notification
, sizeof(Notification
));
197 /* Finalize the stream transfer to send the last packet */
200 /* Indicate a response is no longer ready */
201 ResponseReady
= false;
204 /* Don't process the data endpoints until the system is in the data initialized state, and the buffer is free */
205 if ((CurrRNDISState
== RNDIS_Data_Initialized
) && !(MessageHeader
->MessageLength
))
207 /* Create a new packet header for reading/writing */
208 RNDIS_PACKET_MSG_t RNDISPacketHeader
;
210 /* Select the data OUT endpoint */
211 Endpoint_SelectEndpoint(CDC_RX_EPNUM
);
213 /* Check if the data OUT endpoint contains data, and that the IN buffer is empty */
214 if (Endpoint_IsOUTReceived() && !(FrameIN
.FrameInBuffer
))
216 /* Read in the packet message header */
217 Endpoint_Read_Stream_LE(&RNDISPacketHeader
, sizeof(RNDIS_PACKET_MSG_t
));
219 /* Stall the request if the data is too large */
220 if (RNDISPacketHeader
.DataLength
> ETHERNET_FRAME_SIZE_MAX
)
222 Endpoint_StallTransaction();
226 /* Read in the Ethernet frame into the buffer */
227 Endpoint_Read_Stream_LE(FrameIN
.FrameData
, RNDISPacketHeader
.DataLength
);
229 /* Finalize the stream transfer to send the last packet */
232 /* Store the size of the Ethernet frame */
233 FrameIN
.FrameLength
= RNDISPacketHeader
.DataLength
;
235 /* Indicate Ethernet IN buffer full */
236 FrameIN
.FrameInBuffer
= true;
239 /* Select the data IN endpoint */
240 Endpoint_SelectEndpoint(CDC_TX_EPNUM
);
242 /* Check if the data IN endpoint is ready for more data, and that the IN buffer is full */
243 if (Endpoint_IsINReady() && FrameOUT
.FrameInBuffer
)
245 /* Clear the packet header with all 0s so that the relevant fields can be filled */
246 memset(&RNDISPacketHeader
, 0, sizeof(RNDIS_PACKET_MSG_t
));
248 /* Construct the required packet header fields in the buffer */
249 RNDISPacketHeader
.MessageType
= REMOTE_NDIS_PACKET_MSG
;
250 RNDISPacketHeader
.MessageLength
= (sizeof(RNDIS_PACKET_MSG_t
) + FrameOUT
.FrameLength
);
251 RNDISPacketHeader
.DataOffset
= (sizeof(RNDIS_PACKET_MSG_t
) - sizeof(RNDIS_Message_Header_t
));
252 RNDISPacketHeader
.DataLength
= FrameOUT
.FrameLength
;
254 /* Send the packet header to the host */
255 Endpoint_Write_Stream_LE(&RNDISPacketHeader
, sizeof(RNDIS_PACKET_MSG_t
));
257 /* Send the Ethernet frame data to the host */
258 Endpoint_Write_Stream_LE(FrameOUT
.FrameData
, RNDISPacketHeader
.DataLength
);
260 /* Finalize the stream transfer to send the last packet */
263 /* Indicate Ethernet OUT buffer no longer full */
264 FrameOUT
.FrameInBuffer
= false;
269 /** Ethernet frame processing task. This task checks to see if a frame has been received, and if so hands off the processing
270 * of the frame to the Ethernet processing routines.
272 void Ethernet_Task(void)
274 /* Task for Ethernet processing. Incoming ethernet frames are loaded into the FrameIN structure, and
275 outgoing frames should be loaded into the FrameOUT structure. Both structures can only hold a single
276 Ethernet frame at a time, so the FrameInBuffer bool is used to indicate when the buffers contain data. */
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
);