Minor documentation improvements.
[pub/USBasp.git] / Demos / Device / ClassDriver / RNDISEthernet / RNDISEthernet.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2013.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2013 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
21 The author disclaims 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
28 this software.
29 */
30
31 /** \file
32 *
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.
35 */
36
37 #include "RNDISEthernet.h"
38
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.
42 */
43 USB_ClassInfo_RNDIS_Device_t Ethernet_RNDIS_Interface =
44 {
45 .Config =
46 {
47 .ControlInterfaceNumber = 0,
48 .DataINEndpoint =
49 {
50 .Address = CDC_TX_EPADDR,
51 .Size = CDC_TXRX_EPSIZE,
52 .Banks = 1,
53 },
54 .DataOUTEndpoint =
55 {
56 .Address = CDC_RX_EPADDR,
57 .Size = CDC_TXRX_EPSIZE,
58 .Banks = 1,
59 },
60 .NotificationEndpoint =
61 {
62 .Address = CDC_NOTIFICATION_EPADDR,
63 .Size = CDC_NOTIFICATION_EPSIZE,
64 .Banks = 1,
65 },
66 .AdapterVendorDescription = "LUFA RNDIS Demo Adapter",
67 .AdapterMACAddress = {ADAPTER_MAC_ADDRESS},
68 },
69 };
70
71 /** Global to store the incoming frame from the host before it is processed by the device. */
72 static Ethernet_Frame_Info_t FrameIN;
73
74 /** Global to store the outgoing frame created in the device before it is sent to the host. */
75 static Ethernet_Frame_Info_t FrameOUT;
76
77 /** Main program entry point. This routine contains the overall program flow, including initial
78 * setup of all components and the main program loop.
79 */
80 int main(void)
81 {
82 SetupHardware();
83
84 TCP_Init();
85 Webserver_Init();
86
87 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
88 GlobalInterruptEnable();
89
90 for (;;)
91 {
92 if (RNDIS_Device_IsPacketReceived(&Ethernet_RNDIS_Interface))
93 {
94 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
95
96 RNDIS_Device_ReadPacket(&Ethernet_RNDIS_Interface, &FrameIN.FrameData, &FrameIN.FrameLength);
97 Ethernet_ProcessPacket(&FrameIN, &FrameOUT);
98
99 if (FrameOUT.FrameLength)
100 {
101 RNDIS_Device_SendPacket(&Ethernet_RNDIS_Interface, &FrameOUT.FrameData, FrameOUT.FrameLength);
102 FrameOUT.FrameLength = 0;
103 }
104
105 LEDs_SetAllLEDs(LEDMASK_USB_READY);
106 }
107
108 TCP_TCPTask(&Ethernet_RNDIS_Interface, &FrameOUT);
109
110 RNDIS_Device_USBTask(&Ethernet_RNDIS_Interface);
111 USB_USBTask();
112 }
113 }
114
115 /** Configures the board hardware and chip peripherals for the demo's functionality. */
116 void SetupHardware(void)
117 {
118 /* Disable watchdog if enabled by bootloader/fuses */
119 MCUSR &= ~(1 << WDRF);
120 wdt_disable();
121
122 /* Disable clock division */
123 clock_prescale_set(clock_div_1);
124
125 /* Hardware Initialization */
126 LEDs_Init();
127 Serial_Init(9600, false);
128 USB_Init();
129
130 /* Create a stdio stream for the serial port for stdin and stdout */
131 Serial_CreateStream(NULL);
132 }
133
134 /** Event handler for the library USB Connection event. */
135 void EVENT_USB_Device_Connect(void)
136 {
137 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
138 }
139
140 /** Event handler for the library USB Disconnection event. */
141 void EVENT_USB_Device_Disconnect(void)
142 {
143 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
144 }
145
146 /** Event handler for the library USB Configuration Changed event. */
147 void EVENT_USB_Device_ConfigurationChanged(void)
148 {
149 bool ConfigSuccess = true;
150
151 ConfigSuccess &= RNDIS_Device_ConfigureEndpoints(&Ethernet_RNDIS_Interface);
152
153 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
154 }
155
156 /** Event handler for the library USB Control Request reception event. */
157 void EVENT_USB_Device_ControlRequest(void)
158 {
159 RNDIS_Device_ProcessControlRequest(&Ethernet_RNDIS_Interface);
160 }
161