More Doxygen fixes - ensure no undocumented function parameters.
[pub/USBasp.git] / Demos / Device / LowLevel / CDC / CDC.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 CDC demo. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
35 */
36
37 #include "CDC.h"
38
39 /* Globals: */
40 /** Contains the current baud rate and other settings of the virtual serial port. While this demo does not use
41 * the physical USART and thus does not use these settings, they must still be retained and returned to the host
42 * upon request or the host will assume the device is non-functional.
43 *
44 * These values are set by the host via a class-specific request, however they are not required to be used accurately.
45 * It is possible to completely ignore these value or use other settings as the host is completely unaware of the physical
46 * serial link characteristics and instead sends and receives data in endpoint streams.
47 */
48 CDC_Line_Coding_t LineCoding = { .BaudRateBPS = 9600,
49 .CharFormat = OneStopBit,
50 .ParityType = Parity_None,
51 .DataBits = 8 };
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
62 for (;;)
63 {
64 CDC_Task();
65 USB_USBTask();
66 }
67 }
68
69 /** Configures the board hardware and chip peripherals for the demo's functionality. */
70 void SetupHardware(void)
71 {
72 /* Disable watchdog if enabled by bootloader/fuses */
73 MCUSR &= ~(1 << WDRF);
74 wdt_disable();
75
76 /* Disable clock division */
77 clock_prescale_set(clock_div_1);
78
79 /* Hardware Initialization */
80 Joystick_Init();
81 LEDs_Init();
82 USB_Init();
83 }
84
85 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
86 * starts the library USB task to begin the enumeration and USB management process.
87 */
88 void EVENT_USB_Connect(void)
89 {
90 /* Indicate USB enumerating */
91 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
92 }
93
94 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
95 * the status LEDs and stops the USB management and CDC management tasks.
96 */
97 void EVENT_USB_Disconnect(void)
98 {
99 /* Indicate USB not ready */
100 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
101 }
102
103 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
104 * of the USB device after enumeration - the device endpoints are configured and the CDC management task started.
105 */
106 void EVENT_USB_ConfigurationChanged(void)
107 {
108 /* Indicate USB connected and ready */
109 LEDs_SetAllLEDs(LEDMASK_USB_READY);
110
111 /* Setup CDC Notification, Rx and Tx Endpoints */
112 if (!(Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT,
113 ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE,
114 ENDPOINT_BANK_SINGLE)))
115 {
116 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
117 }
118
119 if (!(Endpoint_ConfigureEndpoint(CDC_TX_EPNUM, EP_TYPE_BULK,
120 ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE,
121 ENDPOINT_BANK_SINGLE)))
122 {
123 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
124 }
125
126 if (!(Endpoint_ConfigureEndpoint(CDC_RX_EPNUM, EP_TYPE_BULK,
127 ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE,
128 ENDPOINT_BANK_SINGLE)))
129 {
130 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
131 }
132 }
133
134 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
135 * control requests that are not handled internally by the USB library (including the CDC control commands,
136 * which are all issued via the control endpoint), so that they can be handled appropriately for the application.
137 */
138 void EVENT_USB_UnhandledControlPacket(void)
139 {
140 uint8_t* LineCodingData = (uint8_t*)&LineCoding;
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(LineCodingData, 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(LineCodingData, 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 /* Acknowledge status stage */
185 while (!(Endpoint_IsINReady()));
186 Endpoint_ClearIN();
187 }
188
189 break;
190 }
191 }
192
193 /** Function to manage CDC data transmission and reception to and from the host. */
194 void CDC_Task(void)
195 {
196 char* ReportString = NULL;
197 uint8_t JoyStatus_LCL = Joystick_GetStatus();
198 static bool ActionSent = false;
199
200 char* JoystickStrings[] =
201 {
202 "Joystick Up\r\n",
203 "Joystick Down\r\n",
204 "Joystick Left\r\n",
205 "Joystick Right\r\n",
206 "Joystick Pressed\r\n",
207 };
208
209 #if 0
210 /* NOTE: Here you can use the notification endpoint to send back line state changes to the host, for the special RS-232
211 handshake signal lines (and some error states), via the CONTROL_LINE_IN_* masks and the following code:
212 */
213 USB_Notification_Header_t Notification = (USB_Notification_Header_t)
214 {
215 .NotificationType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
216 .Notification = NOTIF_SerialState,
217 .wValue = 0,
218 .wIndex = 0,
219 .wLength = sizeof(uint16_t),
220 };
221
222 uint16_t LineStateMask;
223
224 // Set LineStateMask here to a mask of CONTROL_LINE_IN_* masks to set the input handshake line states to send to the host
225
226 Endpoint_SelectEndpoint(CDC_NOTIFICATION_EPNUM);
227 Endpoint_Write_Stream_LE(&Notification, sizeof(Notification));
228 Endpoint_Write_Stream_LE(&LineStateMask, sizeof(LineStateMask));
229 Endpoint_ClearIN();
230 #endif
231
232 /* Determine if a joystick action has occurred */
233 if (JoyStatus_LCL & JOY_UP)
234 ReportString = JoystickStrings[0];
235 else if (JoyStatus_LCL & JOY_DOWN)
236 ReportString = JoystickStrings[1];
237 else if (JoyStatus_LCL & JOY_LEFT)
238 ReportString = JoystickStrings[2];
239 else if (JoyStatus_LCL & JOY_RIGHT)
240 ReportString = JoystickStrings[3];
241 else if (JoyStatus_LCL & JOY_PRESS)
242 ReportString = JoystickStrings[4];
243
244 /* Flag management - Only allow one string to be sent per action */
245 if (ReportString == NULL)
246 {
247 ActionSent = false;
248 }
249 else if (ActionSent == false)
250 {
251 ActionSent = true;
252
253 /* Select the Serial Tx Endpoint */
254 Endpoint_SelectEndpoint(CDC_TX_EPNUM);
255
256 /* Write the String to the Endpoint */
257 Endpoint_Write_Stream_LE(ReportString, strlen(ReportString));
258
259 /* Remember if the packet to send completely fills the endpoint */
260 bool IsFull = (Endpoint_BytesInEndpoint() == CDC_TXRX_EPSIZE);
261
262 /* Finalize the stream transfer to send the last packet */
263 Endpoint_ClearIN();
264
265 /* If the last packet filled the endpoint, send an empty packet to release the buffer on
266 * the receiver (otherwise all data will be cached until a non-full packet is received) */
267 if (IsFull)
268 {
269 /* Wait until the endpoint is ready for another packet */
270 while (!(Endpoint_IsINReady()));
271
272 /* Send an empty packet to ensure that the host does not buffer data sent to it */
273 Endpoint_ClearIN();
274 }
275 }
276
277 /* Select the Serial Rx Endpoint */
278 Endpoint_SelectEndpoint(CDC_RX_EPNUM);
279
280 /* Throw away any received data from the host */
281 if (Endpoint_IsOUTReceived())
282 Endpoint_ClearOUT();
283 }