Upgrade doxygen configuration files to the latest version.
[pub/lufa.git] / Demos / Device / ClassDriver / RNDISEthernet / RNDISEthernet.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2021.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2021 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 /** Message buffer for RNDIS messages processed by the RNDIS device class driver. */
40 static uint8_t RNDIS_Message_Buffer[192];
41
42 /** Global to store the incoming frame from the host before it is processed by the device. */
43 static Ethernet_Frame_Info_t FrameIN;
44
45 /** Global to store the outgoing frame created in the device before it is sent to the host. */
46 static Ethernet_Frame_Info_t FrameOUT;
47
48 /** LUFA RNDIS Class driver interface configuration and state information. This structure is
49 * passed to all RNDIS Class driver functions, so that multiple instances of the same class
50 * within a device can be differentiated from one another.
51 */
52 USB_ClassInfo_RNDIS_Device_t Ethernet_RNDIS_Interface =
53 {
54 .Config =
55 {
56 .ControlInterfaceNumber = INTERFACE_ID_CDC_CCI,
57 .DataINEndpoint =
58 {
59 .Address = CDC_TX_EPADDR,
60 .Size = CDC_TXRX_EPSIZE,
61 .Banks = 1,
62 },
63 .DataOUTEndpoint =
64 {
65 .Address = CDC_RX_EPADDR,
66 .Size = CDC_TXRX_EPSIZE,
67 .Banks = 1,
68 },
69 .NotificationEndpoint =
70 {
71 .Address = CDC_NOTIFICATION_EPADDR,
72 .Size = CDC_NOTIFICATION_EPSIZE,
73 .Banks = 1,
74 },
75 .AdapterVendorDescription = "LUFA RNDIS Demo Adapter",
76 .AdapterMACAddress = {ADAPTER_MAC_ADDRESS},
77 .MessageBuffer = RNDIS_Message_Buffer,
78 .MessageBufferLength = sizeof(RNDIS_Message_Buffer),
79 },
80 };
81
82
83 /** Main program entry point. This routine contains the overall program flow, including initial
84 * setup of all components and the main program loop.
85 */
86 int main(void)
87 {
88 SetupHardware();
89
90 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
91 GlobalInterruptEnable();
92
93 for (;;)
94 {
95 if (RNDIS_Device_IsPacketReceived(&Ethernet_RNDIS_Interface))
96 {
97 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
98
99 RNDIS_Device_ReadPacket(&Ethernet_RNDIS_Interface, FrameIN.FrameData, sizeof(FrameIN.FrameData), &FrameIN.FrameLength);
100
101 // TODO: Process FrameIN here, and optionally fill FrameOUT.
102
103 if (FrameOUT.FrameLength)
104 {
105 RNDIS_Device_SendPacket(&Ethernet_RNDIS_Interface, &FrameOUT.FrameData, FrameOUT.FrameLength);
106 FrameOUT.FrameLength = 0;
107 }
108
109 LEDs_SetAllLEDs(LEDMASK_USB_READY);
110 }
111
112 RNDIS_Device_USBTask(&Ethernet_RNDIS_Interface);
113 USB_USBTask();
114 }
115 }
116
117 /** Configures the board hardware and chip peripherals for the demo's functionality. */
118 void SetupHardware(void)
119 {
120 #if (ARCH == ARCH_AVR8)
121 /* Disable watchdog if enabled by bootloader/fuses */
122 MCUSR &= ~(1 << WDRF);
123 wdt_disable();
124
125 /* Disable clock division */
126 clock_prescale_set(clock_div_1);
127 #elif (ARCH == ARCH_XMEGA)
128 /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
129 XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
130 XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
131
132 /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
133 XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
134 XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
135
136 PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
137 #endif
138
139 /* Hardware Initialization */
140 LEDs_Init();
141 Serial_Init(9600, false);
142 USB_Init();
143
144 /* Create a stdio stream for the serial port for stdin and stdout */
145 Serial_CreateStream(NULL);
146 }
147
148 /** Event handler for the library USB Connection event. */
149 void EVENT_USB_Device_Connect(void)
150 {
151 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
152 }
153
154 /** Event handler for the library USB Disconnection event. */
155 void EVENT_USB_Device_Disconnect(void)
156 {
157 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
158 }
159
160 /** Event handler for the library USB Configuration Changed event. */
161 void EVENT_USB_Device_ConfigurationChanged(void)
162 {
163 bool ConfigSuccess = true;
164
165 ConfigSuccess &= RNDIS_Device_ConfigureEndpoints(&Ethernet_RNDIS_Interface);
166
167 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
168 }
169
170 /** Event handler for the library USB Control Request reception event. */
171 void EVENT_USB_Device_ControlRequest(void)
172 {
173 /* Send MS OS Compatibility descriptor if requested by the host. */
174 CheckIfMSCompatibilityDescriptorRequest();
175
176 RNDIS_Device_ProcessControlRequest(&Ethernet_RNDIS_Interface);
177 }
178