Add missing SVN eol-style property to files where it was missing.
[pub/USBasp.git] / Demos / Device / ClassDriver / VirtualSerialMassStorage / VirtualSerialMassStorage.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 VirtualSerialMassStorage demo. This file contains the main tasks of
34 * the demo and is responsible for the initial application hardware configuration.
35 */
36
37 #include "VirtualSerialMassStorage.h"
38
39 /** LUFA CDC Class driver interface configuration and state information. This structure is
40 * passed to all CDC Class driver functions, so that multiple instances of the same class
41 * within a device can be differentiated from one another.
42 */
43 USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
44 {
45 .Config =
46 {
47 .ControlInterfaceNumber = 0,
48
49 .DataINEndpointNumber = CDC_TX_EPNUM,
50 .DataINEndpointSize = CDC_TXRX_EPSIZE,
51 .DataINEndpointDoubleBank = false,
52
53 .DataOUTEndpointNumber = CDC_RX_EPNUM,
54 .DataOUTEndpointSize = CDC_TXRX_EPSIZE,
55 .DataOUTEndpointDoubleBank = false,
56
57 .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM,
58 .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,
59 .NotificationEndpointDoubleBank = false,
60 },
61 };
62
63 /** LUFA Mass Storage Class driver interface configuration and state information. This structure is
64 * passed to all Mass Storage Class driver functions, so that multiple instances of the same class
65 * within a device can be differentiated from one another.
66 */
67 USB_ClassInfo_MS_Device_t Disk_MS_Interface =
68 {
69 .Config =
70 {
71 .InterfaceNumber = 2,
72
73 .DataINEndpointNumber = MASS_STORAGE_IN_EPNUM,
74 .DataINEndpointSize = MASS_STORAGE_IO_EPSIZE,
75 .DataINEndpointDoubleBank = false,
76
77 .DataOUTEndpointNumber = MASS_STORAGE_OUT_EPNUM,
78 .DataOUTEndpointSize = MASS_STORAGE_IO_EPSIZE,
79 .DataOUTEndpointDoubleBank = false,
80
81 .TotalLUNs = TOTAL_LUNS,
82 },
83 };
84
85 /** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be
86 * used like any regular character stream in the C APIs
87 */
88 static FILE USBSerialStream;
89
90
91 /** Main program entry point. This routine contains the overall program flow, including initial
92 * setup of all components and the main program loop.
93 */
94 int main(void)
95 {
96 SetupHardware();
97
98 /* Create a regular character stream for the interface so that it can be used with the stdio.h functions */
99 CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);
100
101 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
102 sei();
103
104 for (;;)
105 {
106 CheckJoystickMovement();
107
108 /* Must throw away unused bytes from the host, or it will lock up while waiting for the device */
109 CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
110
111 CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
112 MS_Device_USBTask(&Disk_MS_Interface);
113 USB_USBTask();
114 }
115 }
116
117 /** Configures the board hardware and chip peripherals for the demo's functionality. */
118 void SetupHardware(void)
119 {
120 /* Disable watchdog if enabled by bootloader/fuses */
121 MCUSR &= ~(1 << WDRF);
122 wdt_disable();
123
124 /* Disable clock division */
125 clock_prescale_set(clock_div_1);
126
127 /* Hardware Initialization */
128 LEDs_Init();
129 Joystick_Init();
130 SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_FALLING | SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);
131 Dataflash_Init();
132 USB_Init();
133
134 /* Clear Dataflash sector protections, if enabled */
135 DataflashManager_ResetDataflashProtections();
136 }
137
138 /** Checks for changes in the position of the board joystick, sending strings to the host upon each change. */
139 void CheckJoystickMovement(void)
140 {
141 uint8_t JoyStatus_LCL = Joystick_GetStatus();
142 char* ReportString = NULL;
143 static bool ActionSent = false;
144
145 if (JoyStatus_LCL & JOY_UP)
146 ReportString = "Joystick Up\r\n";
147 else if (JoyStatus_LCL & JOY_DOWN)
148 ReportString = "Joystick Down\r\n";
149 else if (JoyStatus_LCL & JOY_LEFT)
150 ReportString = "Joystick Left\r\n";
151 else if (JoyStatus_LCL & JOY_RIGHT)
152 ReportString = "Joystick Right\r\n";
153 else if (JoyStatus_LCL & JOY_PRESS)
154 ReportString = "Joystick Pressed\r\n";
155 else
156 ActionSent = false;
157
158 if ((ReportString != NULL) && (ActionSent == false))
159 {
160 ActionSent = true;
161
162 /* Write the string to the virtual COM port via the created character stream */
163 fputs(ReportString, &USBSerialStream);
164
165 /* Alternatively, without the stream: */
166 // CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString);
167 }
168 }
169
170 /** Event handler for the library USB Connection event. */
171 void EVENT_USB_Device_Connect(void)
172 {
173 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
174 }
175
176 /** Event handler for the library USB Disconnection event. */
177 void EVENT_USB_Device_Disconnect(void)
178 {
179 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
180 }
181
182 /** Event handler for the library USB Configuration Changed event. */
183 void EVENT_USB_Device_ConfigurationChanged(void)
184 {
185 bool ConfigSuccess = true;
186
187 ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
188 ConfigSuccess &= MS_Device_ConfigureEndpoints(&Disk_MS_Interface);
189
190 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
191 }
192
193 /** Event handler for the library USB Control Request reception event. */
194 void EVENT_USB_Device_ControlRequest(void)
195 {
196 CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
197 MS_Device_ProcessControlRequest(&Disk_MS_Interface);
198 }
199
200 /** Mass Storage class driver callback function the reception of SCSI commands from the host, which must be processed.
201 *
202 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface configuration structure being referenced
203 */
204 bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
205 {
206 bool CommandSuccess;
207
208 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
209 CommandSuccess = SCSI_DecodeSCSICommand(MSInterfaceInfo);
210 LEDs_SetAllLEDs(LEDMASK_USB_READY);
211
212 return CommandSuccess;
213 }