3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
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.
39 /* Project Tags, for reading out using the ButtLoad project */
40 BUTTLOADTAG(ProjName
, "LUFA CDC App");
41 BUTTLOADTAG(BuildTime
, __TIME__
);
42 BUTTLOADTAG(BuildDate
, __DATE__
);
43 BUTTLOADTAG(LUFAVersion
, "LUFA V" LUFA_VERSION_STRING
);
45 /* Scheduler Task List */
48 { Task
: USB_USBTask
, TaskStatus
: TASK_STOP
},
49 { Task
: CDC_Task
, TaskStatus
: TASK_STOP
},
53 /** Contains the current baud rate and other settings of the virtual serial port. While this demo does not use
54 * the physical USART and thus does not use these settings, they must still be retained and returned to the host
55 * upon request or the host will assume the device is non-functional.
57 * These values are set by the host via a class-specific request, however they are not required to be used accurately.
58 * It is possible to completely ignore these value or use other settings as the host is completely unaware of the physical
59 * serial link characteristics and instead sends and recieves data in endpoint streams.
61 CDC_Line_Coding_t LineCoding
= { BaudRateBPS
: 9600,
62 CharFormat
: OneStopBit
,
63 ParityType
: Parity_None
,
66 /** String to print through the virtual serial port when the joystick is pressed upwards. */
67 char JoystickUpString
[] = "Joystick Up\r\n";
69 /** String to print through the virtual serial port when the joystick is pressed downwards. */
70 char JoystickDownString
[] = "Joystick Down\r\n";
72 /** String to print through the virtual serial port when the joystick is pressed left. */
73 char JoystickLeftString
[] = "Joystick Left\r\n";
75 /** String to print through the virtual serial port when the joystick is pressed right. */
76 char JoystickRightString
[] = "Joystick Right\r\n";
78 /** String to print through the virtual serial port when the joystick is pressed inwards. */
79 char JoystickPressedString
[] = "Joystick Pressed\r\n";
81 /** Main program entry point. This routine configures the hardware required by the application, then
82 * starts the scheduler to run the application tasks.
86 /* Disable watchdog if enabled by bootloader/fuses */
87 MCUSR
&= ~(1 << WDRF
);
90 /* Disable clock division */
91 clock_prescale_set(clock_div_1
);
93 /* Hardware Initialization */
97 /* Indicate USB not ready */
98 UpdateStatus(Status_USBNotReady
);
100 /* Initialize Scheduler so that it can be used */
103 /* Initialize USB Subsystem */
106 /* Scheduling - routine never returns, so put this last in the main function */
110 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
111 * starts the library USB task to begin the enumeration and USB management process.
113 EVENT_HANDLER(USB_Connect
)
115 /* Start USB management task */
116 Scheduler_SetTaskMode(USB_USBTask
, TASK_RUN
);
118 /* Indicate USB enumerating */
119 UpdateStatus(Status_USBEnumerating
);
122 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
123 * the status LEDs and stops the USB management and CDC management tasks.
125 EVENT_HANDLER(USB_Disconnect
)
127 /* Stop running CDC and USB management tasks */
128 Scheduler_SetTaskMode(CDC_Task
, TASK_STOP
);
129 Scheduler_SetTaskMode(USB_USBTask
, TASK_STOP
);
131 /* Indicate USB not ready */
132 UpdateStatus(Status_USBNotReady
);
135 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
136 * of the USB device after enumeration - the device endpoints are configured and the CDC management task started.
138 EVENT_HANDLER(USB_ConfigurationChanged
)
140 /* Setup CDC Notification, Rx and Tx Endpoints */
141 Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM
, EP_TYPE_INTERRUPT
,
142 ENDPOINT_DIR_IN
, CDC_NOTIFICATION_EPSIZE
,
143 ENDPOINT_BANK_SINGLE
);
145 Endpoint_ConfigureEndpoint(CDC_TX_EPNUM
, EP_TYPE_BULK
,
146 ENDPOINT_DIR_IN
, CDC_TXRX_EPSIZE
,
147 ENDPOINT_BANK_SINGLE
);
149 Endpoint_ConfigureEndpoint(CDC_RX_EPNUM
, EP_TYPE_BULK
,
150 ENDPOINT_DIR_OUT
, CDC_TXRX_EPSIZE
,
151 ENDPOINT_BANK_SINGLE
);
153 /* Indicate USB connected and ready */
154 UpdateStatus(Status_USBReady
);
157 Scheduler_SetTaskMode(CDC_Task
, TASK_RUN
);
160 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
161 * control requests that are not handled internally by the USB library (including the CDC control commands,
162 * which are all issued via the control endpoint), so that they can be handled appropriately for the application.
164 EVENT_HANDLER(USB_UnhandledControlPacket
)
166 uint8_t* LineCodingData
= (uint8_t*)&LineCoding
;
168 /* Process CDC specific control requests */
171 case REQ_GetLineEncoding
:
172 if (bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
174 /* Acknowledge the SETUP packet, ready for data transfer */
175 Endpoint_ClearSetupReceived();
177 /* Write the line coding data to the control endpoint */
178 Endpoint_Write_Control_Stream_LE(LineCodingData
, sizeof(CDC_Line_Coding_t
));
180 /* Finalize the stream transfer to send the last packet or clear the host abort */
181 Endpoint_ClearSetupOUT();
185 case REQ_SetLineEncoding
:
186 if (bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
188 /* Acknowledge the SETUP packet, ready for data transfer */
189 Endpoint_ClearSetupReceived();
191 /* Read the line coding data in from the host into the global struct */
192 Endpoint_Read_Control_Stream_LE(LineCodingData
, sizeof(CDC_Line_Coding_t
));
194 /* Finalize the stream transfer to clear the last packet from the host */
195 Endpoint_ClearSetupIN();
199 case REQ_SetControlLineState
:
200 if (bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
203 /* NOTE: Here you can read in the line state mask from the host, to get the current state of the output handshake
204 lines. The mask is read in from the wValue parameter, and can be masked against the CONTROL_LINE_OUT_* masks
205 to determine the RTS and DTR line states using the following code:
208 uint16_t wIndex
= Endpoint_Read_Word_LE();
210 // Do something with the given line states in wIndex
213 /* Acknowledge the SETUP packet, ready for data transfer */
214 Endpoint_ClearSetupReceived();
216 /* Acknowledge status stage */
217 while (!(Endpoint_IsSetupINReady()));
218 Endpoint_ClearSetupIN();
225 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
226 * log to a serial port, or anything else that is suitable for status updates.
228 * \param CurrentStatus Current status of the system, from the CDC_StatusCodes_t enum
230 void UpdateStatus(uint8_t CurrentStatus
)
232 uint8_t LEDMask
= LEDS_NO_LEDS
;
234 /* Set the LED mask to the appropriate LED mask based on the given status code */
235 switch (CurrentStatus
)
237 case Status_USBNotReady
:
238 LEDMask
= (LEDS_LED1
);
240 case Status_USBEnumerating
:
241 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
243 case Status_USBReady
:
244 LEDMask
= (LEDS_LED2
| LEDS_LED4
);
248 /* Set the board LEDs to the new LED mask */
249 LEDs_SetAllLEDs(LEDMask
);
252 /** Function to manage CDC data transmission and reception to and from the host. */
255 char* ReportString
= NULL
;
256 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
257 static bool ActionSent
= false;
260 /* NOTE: Here you can use the notification endpoint to send back line state changes to the host, for the special RS-232
261 handshake signal lines (and some error states), via the CONTROL_LINE_IN_* masks and the following code:
263 USB_Notification_Header_t Notification
= (USB_Notification_Header_t
)
265 NotificationType
: (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
266 Notification
: NOTIF_SerialState
,
269 wLength
: sizeof(uint16_t),
272 uint16_t LineStateMask
;
274 // Set LineStateMask here to a mask of CONTROL_LINE_IN_* masks to set the input handshake line states to send to the host
276 Endpoint_SelectEndpoint(CDC_NOTIFICATION_EPNUM
);
277 Endpoint_Write_Stream_LE(&Notification
, sizeof(Notification
));
278 Endpoint_Write_Stream_LE(&LineStateMask
, sizeof(LineStateMask
));
279 Endpoint_ClearCurrentBank();
282 /* Determine if a joystick action has occurred */
283 if (JoyStatus_LCL
& JOY_UP
)
284 ReportString
= JoystickUpString
;
285 else if (JoyStatus_LCL
& JOY_DOWN
)
286 ReportString
= JoystickDownString
;
287 else if (JoyStatus_LCL
& JOY_LEFT
)
288 ReportString
= JoystickLeftString
;
289 else if (JoyStatus_LCL
& JOY_RIGHT
)
290 ReportString
= JoystickRightString
;
291 else if (JoyStatus_LCL
& JOY_PRESS
)
292 ReportString
= JoystickPressedString
;
294 /* Flag management - Only allow one string to be sent per action */
295 if (ReportString
== NULL
)
299 else if (ActionSent
== false)
303 /* Select the Serial Tx Endpoint */
304 Endpoint_SelectEndpoint(CDC_TX_EPNUM
);
306 /* Write the String to the Endpoint */
307 Endpoint_Write_Stream_LE(ReportString
, strlen(ReportString
));
309 /* Finalize the stream transfer to send the last packet */
310 Endpoint_ClearCurrentBank();
312 /* Wait until the endpoint is ready for another packet */
313 while (!(Endpoint_ReadWriteAllowed()));
315 /* Send an empty packet to ensure that the host does not buffer data sent to it */
316 Endpoint_ClearCurrentBank();
319 /* Select the Serial Rx Endpoint */
320 Endpoint_SelectEndpoint(CDC_RX_EPNUM
);
322 /* Throw away any received data from the host */
323 if (Endpoint_ReadWriteAllowed())
324 Endpoint_ClearCurrentBank();