Rename FunctionAttributes.h to Attributes.h, as some attributes are applicable to...
[pub/USBasp.git] / Demos / Device / LowLevel / VirtualSerial / VirtualSerial.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 VirtualSerial demo. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
35 */
36
37 #include "VirtualSerial.h"
38
39 /** Contains the current baud rate and other settings of the 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 LineEncoding = { .BaudRateBPS = 0,
48 .CharFormat = OneStopBit,
49 .ParityType = Parity_None,
50 .DataBits = 8 };
51
52 /** Main program entry point. This routine contains the overall program flow, including initial
53 * setup of all components and the main program loop.
54 */
55 int main(void)
56 {
57 SetupHardware();
58
59 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
60
61 for (;;)
62 {
63 CDC_Task();
64 USB_USBTask();
65 }
66 }
67
68 /** Configures the board hardware and chip peripherals for the demo's functionality. */
69 void SetupHardware(void)
70 {
71 /* Disable watchdog if enabled by bootloader/fuses */
72 MCUSR &= ~(1 << WDRF);
73 wdt_disable();
74
75 /* Disable clock division */
76 clock_prescale_set(clock_div_1);
77
78 /* Hardware Initialization */
79 Joystick_Init();
80 LEDs_Init();
81 USB_Init();
82 }
83
84 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
85 * starts the library USB task to begin the enumeration and USB management process.
86 */
87 void EVENT_USB_Device_Connect(void)
88 {
89 /* Indicate USB enumerating */
90 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
91 }
92
93 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
94 * the status LEDs and stops the USB management and CDC management tasks.
95 */
96 void EVENT_USB_Device_Disconnect(void)
97 {
98 /* Indicate USB not ready */
99 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
100 }
101
102 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
103 * of the USB device after enumeration - the device endpoints are configured and the CDC management task started.
104 */
105 void EVENT_USB_Device_ConfigurationChanged(void)
106 {
107 /* Indicate USB connected and ready */
108 LEDs_SetAllLEDs(LEDMASK_USB_READY);
109
110 /* Setup CDC Notification, Rx and Tx Endpoints */
111 if (!(Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT,
112 ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE,
113 ENDPOINT_BANK_SINGLE)))
114 {
115 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
116 }
117
118 if (!(Endpoint_ConfigureEndpoint(CDC_TX_EPNUM, EP_TYPE_BULK,
119 ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE,
120 ENDPOINT_BANK_SINGLE)))
121 {
122 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
123 }
124
125 if (!(Endpoint_ConfigureEndpoint(CDC_RX_EPNUM, EP_TYPE_BULK,
126 ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE,
127 ENDPOINT_BANK_SINGLE)))
128 {
129 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
130 }
131
132 /* Reset line encoding baud rate so that the host knows to send new values */
133 LineEncoding.BaudRateBPS = 0;
134 }
135
136 /** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific
137 * control requests that are not handled internally by the USB library (including the CDC control commands,
138 * which are all issued via the control endpoint), so that they can be handled appropriately for the application.
139 */
140 void EVENT_USB_Device_UnhandledControlRequest(void)
141 {
142 /* Process CDC specific control requests */
143 switch (USB_ControlRequest.bRequest)
144 {
145 case REQ_GetLineEncoding:
146 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
147 {
148 /* Acknowledge the SETUP packet, ready for data transfer */
149 Endpoint_ClearSETUP();
150
151 /* Write the line coding data to the control endpoint */
152 Endpoint_Write_Control_Stream_LE(&LineEncoding, sizeof(CDC_Line_Coding_t));
153
154 /* Finalize the stream transfer to send the last packet or clear the host abort */
155 Endpoint_ClearOUT();
156 }
157
158 break;
159 case REQ_SetLineEncoding:
160 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
161 {
162 /* Acknowledge the SETUP packet, ready for data transfer */
163 Endpoint_ClearSETUP();
164
165 /* Read the line coding data in from the host into the global struct */
166 Endpoint_Read_Control_Stream_LE(&LineEncoding, sizeof(CDC_Line_Coding_t));
167
168 /* Finalize the stream transfer to clear the last packet from the host */
169 Endpoint_ClearIN();
170 }
171
172 break;
173 case REQ_SetControlLineState:
174 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
175 {
176 /* Acknowledge the SETUP packet, ready for data transfer */
177 Endpoint_ClearSETUP();
178
179 /* NOTE: Here you can read in the line state mask from the host, to get the current state of the output handshake
180 lines. The mask is read in from the wValue parameter in USB_ControlRequest, and can be masked against the
181 CONTROL_LINE_OUT_* masks to determine the RTS and DTR line states using the following code:
182 */
183
184 Endpoint_ClearStatusStage();
185 }
186
187 break;
188 }
189 }
190
191 /** Function to manage CDC data transmission and reception to and from the host. */
192 void CDC_Task(void)
193 {
194 char* ReportString = NULL;
195 uint8_t JoyStatus_LCL = Joystick_GetStatus();
196 static bool ActionSent = false;
197
198 /* Device must be connected and configured for the task to run */
199 if (USB_DeviceState != DEVICE_STATE_Configured)
200 return;
201
202 #if 0
203 /* NOTE: Here you can use the notification endpoint to send back line state changes to the host, for the special RS-232
204 * handshake signal lines (and some error states), via the CONTROL_LINE_IN_* masks and the following code:
205 */
206 USB_Notification_Header_t Notification = (USB_Notification_Header_t)
207 {
208 .NotificationType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
209 .Notification = NOTIF_SerialState,
210 .wValue = 0,
211 .wIndex = 0,
212 .wLength = sizeof(uint16_t),
213 };
214
215 uint16_t LineStateMask;
216
217 // Set LineStateMask here to a mask of CONTROL_LINE_IN_* masks to set the input handshake line states to send to the host
218
219 Endpoint_SelectEndpoint(CDC_NOTIFICATION_EPNUM);
220 Endpoint_Write_Stream_LE(&Notification, sizeof(Notification));
221 Endpoint_Write_Stream_LE(&LineStateMask, sizeof(LineStateMask));
222 Endpoint_ClearIN();
223 #endif
224
225 /* Determine if a joystick action has occurred */
226 if (JoyStatus_LCL & JOY_UP)
227 ReportString = "Joystick Up\r\n";
228 else if (JoyStatus_LCL & JOY_DOWN)
229 ReportString = "Joystick Down\r\n";
230 else if (JoyStatus_LCL & JOY_LEFT)
231 ReportString = "Joystick Left\r\n";
232 else if (JoyStatus_LCL & JOY_RIGHT)
233 ReportString = "Joystick Right\r\n";
234 else if (JoyStatus_LCL & JOY_PRESS)
235 ReportString = "Joystick Pressed\r\n";
236 else
237 ActionSent = false;
238
239 /* Flag management - Only allow one string to be sent per action */
240 if ((ReportString != NULL) && (ActionSent == false) && LineEncoding.BaudRateBPS)
241 {
242 ActionSent = true;
243
244 /* Select the Serial Tx Endpoint */
245 Endpoint_SelectEndpoint(CDC_TX_EPNUM);
246
247 /* Write the String to the Endpoint */
248 Endpoint_Write_Stream_LE(ReportString, strlen(ReportString));
249
250 /* Remember if the packet to send completely fills the endpoint */
251 bool IsFull = (Endpoint_BytesInEndpoint() == CDC_TXRX_EPSIZE);
252
253 /* Finalize the stream transfer to send the last packet */
254 Endpoint_ClearIN();
255
256 /* If the last packet filled the endpoint, send an empty packet to release the buffer on
257 * the receiver (otherwise all data will be cached until a non-full packet is received) */
258 if (IsFull)
259 {
260 /* Wait until the endpoint is ready for another packet */
261 Endpoint_WaitUntilReady();
262
263 /* Send an empty packet to ensure that the host does not buffer data sent to it */
264 Endpoint_ClearIN();
265 }
266 }
267
268 /* Select the Serial Rx Endpoint */
269 Endpoint_SelectEndpoint(CDC_RX_EPNUM);
270
271 /* Throw away any received data from the host */
272 if (Endpoint_IsOUTReceived())
273 Endpoint_ClearOUT();
274 }