Add new ARCH option to the makefiles to (eventually) specify the target device archit...
[pub/USBasp.git] / Demos / Device / LowLevel / VirtualSerial / VirtualSerial.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 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 static CDC_LineEncoding_t LineEncoding = { .BaudRateBPS = 0,
48 .CharFormat = CDC_LINEENCODING_OneStopBit,
49 .ParityType = CDC_PARITY_None,
50 .DataBits = 8 };
51
52
53 /** Main program entry point. This routine contains the overall program flow, including initial
54 * setup of all components and the main program loop.
55 */
56 int main(void)
57 {
58 SetupHardware();
59
60 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
61 sei();
62
63 for (;;)
64 {
65 CDC_Task();
66 USB_USBTask();
67 }
68 }
69
70 /** Configures the board hardware and chip peripherals for the demo's functionality. */
71 void SetupHardware(void)
72 {
73 /* Disable watchdog if enabled by bootloader/fuses */
74 MCUSR &= ~(1 << WDRF);
75 wdt_disable();
76
77 /* Disable clock division */
78 clock_prescale_set(clock_div_1);
79
80 /* Hardware Initialization */
81 Joystick_Init();
82 LEDs_Init();
83 USB_Init();
84 }
85
86 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
87 * starts the library USB task to begin the enumeration and USB management process.
88 */
89 void EVENT_USB_Device_Connect(void)
90 {
91 /* Indicate USB enumerating */
92 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
93 }
94
95 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
96 * the status LEDs and stops the USB management and CDC management tasks.
97 */
98 void EVENT_USB_Device_Disconnect(void)
99 {
100 /* Indicate USB not ready */
101 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
102 }
103
104 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
105 * of the USB device after enumeration - the device endpoints are configured and the CDC management task started.
106 */
107 void EVENT_USB_Device_ConfigurationChanged(void)
108 {
109 bool ConfigSuccess = true;
110
111 /* Setup CDC Data Endpoints */
112 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
113 CDC_NOTIFICATION_EPSIZE, ENDPOINT_BANK_SINGLE);
114 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_TX_EPNUM, EP_TYPE_BULK, ENDPOINT_DIR_IN,
115 CDC_TXRX_EPSIZE, ENDPOINT_BANK_SINGLE);
116 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_RX_EPNUM, EP_TYPE_BULK, ENDPOINT_DIR_OUT,
117 CDC_TXRX_EPSIZE, ENDPOINT_BANK_SINGLE);
118
119 /* Reset line encoding baud rate so that the host knows to send new values */
120 LineEncoding.BaudRateBPS = 0;
121
122 /* Indicate endpoint configuration success or failure */
123 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
124 }
125
126 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
127 * the device from the USB host before passing along unhandled control requests to the library for processing
128 * internally.
129 */
130 void EVENT_USB_Device_ControlRequest(void)
131 {
132 /* Process CDC specific control requests */
133 switch (USB_ControlRequest.bRequest)
134 {
135 case CDC_REQ_GetLineEncoding:
136 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
137 {
138 Endpoint_ClearSETUP();
139
140 /* Write the line coding data to the control endpoint */
141 Endpoint_Write_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
142 Endpoint_ClearOUT();
143 }
144
145 break;
146 case CDC_REQ_SetLineEncoding:
147 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
148 {
149 Endpoint_ClearSETUP();
150
151 /* Read the line coding data in from the host into the global struct */
152 Endpoint_Read_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
153 Endpoint_ClearIN();
154 }
155
156 break;
157 case CDC_REQ_SetControlLineState:
158 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
159 {
160 Endpoint_ClearSETUP();
161 Endpoint_ClearStatusStage();
162
163 /* NOTE: Here you can read in the line state mask from the host, to get the current state of the output handshake
164 lines. The mask is read in from the wValue parameter in USB_ControlRequest, and can be masked against the
165 CONTROL_LINE_OUT_* masks to determine the RTS and DTR line states using the following code:
166 */
167 }
168
169 break;
170 }
171 }
172
173 /** Function to manage CDC data transmission and reception to and from the host. */
174 void CDC_Task(void)
175 {
176 char* ReportString = NULL;
177 uint8_t JoyStatus_LCL = Joystick_GetStatus();
178 static bool ActionSent = false;
179
180 /* Device must be connected and configured for the task to run */
181 if (USB_DeviceState != DEVICE_STATE_Configured)
182 return;
183
184 /* Determine if a joystick action has occurred */
185 if (JoyStatus_LCL & JOY_UP)
186 ReportString = "Joystick Up\r\n";
187 else if (JoyStatus_LCL & JOY_DOWN)
188 ReportString = "Joystick Down\r\n";
189 else if (JoyStatus_LCL & JOY_LEFT)
190 ReportString = "Joystick Left\r\n";
191 else if (JoyStatus_LCL & JOY_RIGHT)
192 ReportString = "Joystick Right\r\n";
193 else if (JoyStatus_LCL & JOY_PRESS)
194 ReportString = "Joystick Pressed\r\n";
195 else
196 ActionSent = false;
197
198 /* Flag management - Only allow one string to be sent per action */
199 if ((ReportString != NULL) && (ActionSent == false) && LineEncoding.BaudRateBPS)
200 {
201 ActionSent = true;
202
203 /* Select the Serial Tx Endpoint */
204 Endpoint_SelectEndpoint(CDC_TX_EPNUM);
205
206 /* Write the String to the Endpoint */
207 Endpoint_Write_Stream_LE(ReportString, strlen(ReportString), NULL);
208
209 /* Remember if the packet to send completely fills the endpoint */
210 bool IsFull = (Endpoint_BytesInEndpoint() == CDC_TXRX_EPSIZE);
211
212 /* Finalize the stream transfer to send the last packet */
213 Endpoint_ClearIN();
214
215 /* If the last packet filled the endpoint, send an empty packet to release the buffer on
216 * the receiver (otherwise all data will be cached until a non-full packet is received) */
217 if (IsFull)
218 {
219 /* Wait until the endpoint is ready for another packet */
220 Endpoint_WaitUntilReady();
221
222 /* Send an empty packet to ensure that the host does not buffer data sent to it */
223 Endpoint_ClearIN();
224 }
225 }
226
227 /* Select the Serial Rx Endpoint */
228 Endpoint_SelectEndpoint(CDC_RX_EPNUM);
229
230 /* Throw away any received data from the host */
231 if (Endpoint_IsOUTReceived())
232 Endpoint_ClearOUT();
233 }
234