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