Fixed bug in RNDISEthernet and DualCDC demos not using the correct USB_ControlRequest...
[pub/USBasp.git] / Demos / Device / DualCDC / DualCDC.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
33 * Main source file for the DualCDC demo. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
35 */
36
37 #include "DualCDC.h"
38
39 /* Scheduler Task List */
40 TASK_LIST
41 {
42 { .Task = USB_USBTask , .TaskStatus = TASK_STOP },
43 { .Task = CDC1_Task , .TaskStatus = TASK_STOP },
44 { .Task = CDC2_Task , .TaskStatus = TASK_STOP },
45 };
46
47 /* Globals: */
48 /** Contains the current baud rate and other settings of the first virtual serial port. While this demo does not use
49 * the physical USART and thus does not use these settings, they must still be retained and returned to the host
50 * upon request or the host will assume the device is non-functional.
51 *
52 * These values are set by the host via a class-specific request, however they are not required to be used accurately.
53 * It is possible to completely ignore these value or use other settings as the host is completely unaware of the physical
54 * serial link characteristics and instead sends and receives data in endpoint streams.
55 */
56 CDC_Line_Coding_t LineCoding1 = { .BaudRateBPS = 9600,
57 .CharFormat = OneStopBit,
58 .ParityType = Parity_None,
59 .DataBits = 8 };
60
61 /** Contains the current baud rate and other settings of the second virtual serial port. While this demo does not use
62 * the physical USART and thus does not use these settings, they must still be retained and returned to the host
63 * upon request or the host will assume the device is non-functional.
64 *
65 * These values are set by the host via a class-specific request, however they are not required to be used accurately.
66 * It is possible to completely ignore these value or use other settings as the host is completely unaware of the physical
67 * serial link characteristics and instead sends and receives data in endpoint streams.
68 */
69 CDC_Line_Coding_t LineCoding2 = { .BaudRateBPS = 9600,
70 .CharFormat = OneStopBit,
71 .ParityType = Parity_None,
72 .DataBits = 8 };
73
74 /** String to print through the first virtual serial port when the joystick is pressed upwards. */
75 char JoystickUpString[] = "Joystick Up\r\n";
76
77 /** String to print through the first virtual serial port when the joystick is pressed downward. */
78 char JoystickDownString[] = "Joystick Down\r\n";
79
80 /** String to print through the first virtual serial port when the joystick is pressed left. */
81 char JoystickLeftString[] = "Joystick Left\r\n";
82
83 /** String to print through the first virtual serial port when the joystick is pressed right. */
84 char JoystickRightString[] = "Joystick Right\r\n";
85
86 /** String to print through the first virtual serial port when the joystick is pressed inwards. */
87 char JoystickPressedString[] = "Joystick Pressed\r\n";
88
89 /** Main program entry point. This routine configures the hardware required by the application, then
90 * starts the scheduler to run the application tasks.
91 */
92 int main(void)
93 {
94 /* Disable watchdog if enabled by bootloader/fuses */
95 MCUSR &= ~(1 << WDRF);
96 wdt_disable();
97
98 /* Disable clock division */
99 clock_prescale_set(clock_div_1);
100
101 /* Hardware Initialization */
102 Joystick_Init();
103 LEDs_Init();
104
105 /* Indicate USB not ready */
106 UpdateStatus(Status_USBNotReady);
107
108 /* Initialize Scheduler so that it can be used */
109 Scheduler_Init();
110
111 /* Initialize USB Subsystem */
112 USB_Init();
113
114 /* Scheduling - routine never returns, so put this last in the main function */
115 Scheduler_Start();
116 }
117
118 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
119 * starts the library USB task to begin the enumeration and USB management process.
120 */
121 EVENT_HANDLER(USB_Connect)
122 {
123 /* Start USB management task */
124 Scheduler_SetTaskMode(USB_USBTask, TASK_RUN);
125
126 /* Indicate USB enumerating */
127 UpdateStatus(Status_USBEnumerating);
128 }
129
130 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
131 * the status LEDs and stops the USB management and CDC management tasks.
132 */
133 EVENT_HANDLER(USB_Disconnect)
134 {
135 /* Stop running CDC and USB management tasks */
136 Scheduler_SetTaskMode(CDC1_Task, TASK_STOP);
137 Scheduler_SetTaskMode(CDC2_Task, TASK_STOP);
138 Scheduler_SetTaskMode(USB_USBTask, TASK_STOP);
139
140 /* Indicate USB not ready */
141 UpdateStatus(Status_USBNotReady);
142 }
143
144 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
145 * of the USB device after enumeration - the device endpoints are configured and the CDC management tasks are started.
146 */
147 EVENT_HANDLER(USB_ConfigurationChanged)
148 {
149 /* Setup CDC Notification, Rx and Tx Endpoints for the first CDC */
150 Endpoint_ConfigureEndpoint(CDC1_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT,
151 ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE,
152 ENDPOINT_BANK_SINGLE);
153
154 Endpoint_ConfigureEndpoint(CDC1_TX_EPNUM, EP_TYPE_BULK,
155 ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE,
156 ENDPOINT_BANK_SINGLE);
157
158 Endpoint_ConfigureEndpoint(CDC1_RX_EPNUM, EP_TYPE_BULK,
159 ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE,
160 ENDPOINT_BANK_SINGLE);
161
162 /* Setup CDC Notification, Rx and Tx Endpoints for the second CDC */
163 Endpoint_ConfigureEndpoint(CDC2_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT,
164 ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE,
165 ENDPOINT_BANK_SINGLE);
166
167 Endpoint_ConfigureEndpoint(CDC2_TX_EPNUM, EP_TYPE_BULK,
168 ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE,
169 ENDPOINT_BANK_SINGLE);
170
171 Endpoint_ConfigureEndpoint(CDC2_RX_EPNUM, EP_TYPE_BULK,
172 ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE,
173 ENDPOINT_BANK_SINGLE);
174
175 /* Indicate USB connected and ready */
176 UpdateStatus(Status_USBReady);
177
178 /* Start CDC tasks */
179 Scheduler_SetTaskMode(CDC1_Task, TASK_RUN);
180 Scheduler_SetTaskMode(CDC2_Task, TASK_RUN);
181 }
182
183 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
184 * control requests that are not handled internally by the USB library (including the CDC control commands,
185 * which are all issued via the control endpoint), so that they can be handled appropriately for the application.
186 */
187 EVENT_HANDLER(USB_UnhandledControlPacket)
188 {
189 /* Determine which interface's Line Coding data is being set from the wIndex parameter */
190 uint8_t* LineCodingData = (USB_ControlRequest.wIndex == 0) ? (uint8_t*)&LineCoding1 : (uint8_t*)&LineCoding2;
191
192 /* Process CDC specific control requests */
193 switch (USB_ControlRequest.bRequest)
194 {
195 case REQ_GetLineEncoding:
196 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
197 {
198 /* Acknowledge the SETUP packet, ready for data transfer */
199 Endpoint_ClearSETUP();
200
201 /* Write the line coding data to the control endpoint */
202 Endpoint_Write_Control_Stream_LE(LineCodingData, sizeof(CDC_Line_Coding_t));
203
204 /* Finalize the stream transfer to send the last packet or clear the host abort */
205 Endpoint_ClearOUT();
206 }
207
208 break;
209 case REQ_SetLineEncoding:
210 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
211 {
212 /* Acknowledge the SETUP packet, ready for data transfer */
213 Endpoint_ClearSETUP();
214
215 /* Read the line coding data in from the host into the global struct */
216 Endpoint_Read_Control_Stream_LE(LineCodingData, sizeof(CDC_Line_Coding_t));
217
218 /* Finalize the stream transfer to clear the last packet from the host */
219 Endpoint_ClearIN();
220 }
221
222 break;
223 case REQ_SetControlLineState:
224 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
225 {
226 /* Acknowledge the SETUP packet, ready for data transfer */
227 Endpoint_ClearSETUP();
228
229 /* Acknowledge status stage */
230 while (!(Endpoint_IsINReady()));
231 Endpoint_ClearIN();
232 }
233
234 break;
235 }
236 }
237
238 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
239 * log to a serial port, or anything else that is suitable for status updates.
240 *
241 * \param CurrentStatus Current status of the system, from the DualCDC_StatusCodes_t enum
242 */
243 void UpdateStatus(uint8_t CurrentStatus)
244 {
245 uint8_t LEDMask = LEDS_NO_LEDS;
246
247 /* Set the LED mask to the appropriate LED mask based on the given status code */
248 switch (CurrentStatus)
249 {
250 case Status_USBNotReady:
251 LEDMask = (LEDS_LED1);
252 break;
253 case Status_USBEnumerating:
254 LEDMask = (LEDS_LED1 | LEDS_LED2);
255 break;
256 case Status_USBReady:
257 LEDMask = (LEDS_LED2 | LEDS_LED4);
258 break;
259 }
260
261 /* Set the board LEDs to the new LED mask */
262 LEDs_SetAllLEDs(LEDMask);
263 }
264
265 /** Function to manage CDC data transmission and reception to and from the host for the first CDC interface, which sends joystick
266 * movements to the host as ASCII strings.
267 */
268 TASK(CDC1_Task)
269 {
270 char* ReportString = NULL;
271 uint8_t JoyStatus_LCL = Joystick_GetStatus();
272 static bool ActionSent = false;
273
274 /* Determine if a joystick action has occurred */
275 if (JoyStatus_LCL & JOY_UP)
276 ReportString = JoystickUpString;
277 else if (JoyStatus_LCL & JOY_DOWN)
278 ReportString = JoystickDownString;
279 else if (JoyStatus_LCL & JOY_LEFT)
280 ReportString = JoystickLeftString;
281 else if (JoyStatus_LCL & JOY_RIGHT)
282 ReportString = JoystickRightString;
283 else if (JoyStatus_LCL & JOY_PRESS)
284 ReportString = JoystickPressedString;
285
286 /* Flag management - Only allow one string to be sent per action */
287 if (ReportString == NULL)
288 {
289 ActionSent = false;
290 }
291 else if (ActionSent == false)
292 {
293 ActionSent = true;
294
295 /* Select the Serial Tx Endpoint */
296 Endpoint_SelectEndpoint(CDC1_TX_EPNUM);
297
298 /* Write the String to the Endpoint */
299 Endpoint_Write_Stream_LE(ReportString, strlen(ReportString));
300
301 /* Finalize the stream transfer to send the last packet */
302 Endpoint_ClearIN();
303
304 /* Wait until the endpoint is ready for another packet */
305 while (!(Endpoint_IsINReady()));
306
307 /* Send an empty packet to ensure that the host does not buffer data sent to it */
308 Endpoint_ClearIN();
309 }
310
311 /* Select the Serial Rx Endpoint */
312 Endpoint_SelectEndpoint(CDC1_RX_EPNUM);
313
314 /* Throw away any received data from the host */
315 if (Endpoint_IsOUTReceived())
316 Endpoint_ClearOUT();
317 }
318
319 /** Function to manage CDC data transmission and reception to and from the host for the second CDC interface, which echoes back
320 * all data sent to it from the host.
321 */
322 TASK(CDC2_Task)
323 {
324 /* Select the Serial Rx Endpoint */
325 Endpoint_SelectEndpoint(CDC2_RX_EPNUM);
326
327 /* Check to see if any data has been received */
328 if (Endpoint_IsOUTReceived())
329 {
330 /* Create a temp buffer big enough to hold the incoming endpoint packet */
331 uint8_t Buffer[Endpoint_BytesInEndpoint()];
332
333 /* Remember how large the incoming packet is */
334 uint16_t DataLength = Endpoint_BytesInEndpoint();
335
336 /* Read in the incoming packet into the buffer */
337 Endpoint_Read_Stream_LE(&Buffer, DataLength);
338
339 /* Finalize the stream transfer to send the last packet */
340 Endpoint_ClearOUT();
341
342 /* Select the Serial Tx Endpoint */
343 Endpoint_SelectEndpoint(CDC2_TX_EPNUM);
344
345 /* Write the received data to the endpoint */
346 Endpoint_Write_Stream_LE(&Buffer, DataLength);
347
348 /* Finalize the stream transfer to send the last packet */
349 Endpoint_ClearIN();
350
351 /* Wait until the endpoint is ready for the next packet */
352 while (!(Endpoint_IsINReady()));
353
354 /* Send an empty packet to prevent host buffering */
355 Endpoint_ClearIN();
356 }
357 }