Fix typo in the Atmel Studio integration Getting Started page.
[pub/USBasp.git] / Demos / Device / ClassDriver / VirtualSerialMassStorage / VirtualSerialMassStorage.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2013.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2013 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 disclaims 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 .DataINEndpoint =
49 {
50 .Address = CDC_TX_EPADDR,
51 .Size = CDC_TXRX_EPSIZE,
52 .Banks = 1,
53 },
54 .DataOUTEndpoint =
55 {
56 .Address = CDC_RX_EPADDR,
57 .Size = CDC_TXRX_EPSIZE,
58 .Banks = 1,
59 },
60 .NotificationEndpoint =
61 {
62 .Address = CDC_NOTIFICATION_EPADDR,
63 .Size = CDC_NOTIFICATION_EPSIZE,
64 .Banks = 1,
65 },
66 },
67 };
68
69 /** LUFA Mass Storage Class driver interface configuration and state information. This structure is
70 * passed to all Mass Storage Class driver functions, so that multiple instances of the same class
71 * within a device can be differentiated from one another.
72 */
73 USB_ClassInfo_MS_Device_t Disk_MS_Interface =
74 {
75 .Config =
76 {
77 .InterfaceNumber = 2,
78 .DataINEndpoint =
79 {
80 .Address = MASS_STORAGE_IN_EPADDR,
81 .Size = MASS_STORAGE_IO_EPSIZE,
82 .Banks = 1,
83 },
84 .DataOUTEndpoint =
85 {
86 .Address = MASS_STORAGE_OUT_EPADDR,
87 .Size = MASS_STORAGE_IO_EPSIZE,
88 .Banks = 1,
89 },
90 .TotalLUNs = TOTAL_LUNS,
91 },
92 };
93
94 /** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be
95 * used like any regular character stream in the C APIs
96 */
97 static FILE USBSerialStream;
98
99
100 /** Main program entry point. This routine contains the overall program flow, including initial
101 * setup of all components and the main program loop.
102 */
103 int main(void)
104 {
105 SetupHardware();
106
107 /* Create a regular character stream for the interface so that it can be used with the stdio.h functions */
108 CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);
109
110 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
111 GlobalInterruptEnable();
112
113 for (;;)
114 {
115 CheckJoystickMovement();
116
117 /* Must throw away unused bytes from the host, or it will lock up while waiting for the device */
118 CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
119
120 CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
121 MS_Device_USBTask(&Disk_MS_Interface);
122 USB_USBTask();
123 }
124 }
125
126 /** Configures the board hardware and chip peripherals for the demo's functionality. */
127 void SetupHardware(void)
128 {
129 #if (ARCH == ARCH_AVR8)
130 /* Disable watchdog if enabled by bootloader/fuses */
131 MCUSR &= ~(1 << WDRF);
132 wdt_disable();
133
134 /* Disable clock division */
135 clock_prescale_set(clock_div_1);
136 #elif (ARCH == ARCH_XMEGA)
137 /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
138 XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
139 XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
140
141 /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
142 XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
143 XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
144
145 PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
146 #endif
147
148 /* Hardware Initialization */
149 LEDs_Init();
150 Joystick_Init();
151 SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_FALLING | SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);
152 Dataflash_Init();
153 USB_Init();
154
155 /* Check if the Dataflash is working, abort if not */
156 if (!(DataflashManager_CheckDataflashOperation()))
157 {
158 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
159 for(;;);
160 }
161
162 /* Clear Dataflash sector protections, if enabled */
163 DataflashManager_ResetDataflashProtections();
164 }
165
166 /** Checks for changes in the position of the board joystick, sending strings to the host upon each change. */
167 void CheckJoystickMovement(void)
168 {
169 uint8_t JoyStatus_LCL = Joystick_GetStatus();
170 char* ReportString = NULL;
171 static bool ActionSent = false;
172
173 if (JoyStatus_LCL & JOY_UP)
174 ReportString = "Joystick Up\r\n";
175 else if (JoyStatus_LCL & JOY_DOWN)
176 ReportString = "Joystick Down\r\n";
177 else if (JoyStatus_LCL & JOY_LEFT)
178 ReportString = "Joystick Left\r\n";
179 else if (JoyStatus_LCL & JOY_RIGHT)
180 ReportString = "Joystick Right\r\n";
181 else if (JoyStatus_LCL & JOY_PRESS)
182 ReportString = "Joystick Pressed\r\n";
183 else
184 ActionSent = false;
185
186 if ((ReportString != NULL) && (ActionSent == false))
187 {
188 ActionSent = true;
189
190 /* Write the string to the virtual COM port via the created character stream */
191 fputs(ReportString, &USBSerialStream);
192
193 /* Alternatively, without the stream: */
194 // CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString);
195 }
196 }
197
198 /** Event handler for the library USB Connection event. */
199 void EVENT_USB_Device_Connect(void)
200 {
201 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
202 }
203
204 /** Event handler for the library USB Disconnection event. */
205 void EVENT_USB_Device_Disconnect(void)
206 {
207 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
208 }
209
210 /** Event handler for the library USB Configuration Changed event. */
211 void EVENT_USB_Device_ConfigurationChanged(void)
212 {
213 bool ConfigSuccess = true;
214
215 ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
216 ConfigSuccess &= MS_Device_ConfigureEndpoints(&Disk_MS_Interface);
217
218 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
219 }
220
221 /** Event handler for the library USB Control Request reception event. */
222 void EVENT_USB_Device_ControlRequest(void)
223 {
224 CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
225 MS_Device_ProcessControlRequest(&Disk_MS_Interface);
226 }
227
228 /** Mass Storage class driver callback function the reception of SCSI commands from the host, which must be processed.
229 *
230 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface configuration structure being referenced
231 */
232 bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
233 {
234 bool CommandSuccess;
235
236 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
237 CommandSuccess = SCSI_DecodeSCSICommand(MSInterfaceInfo);
238 LEDs_SetAllLEDs(LEDMASK_USB_READY);
239
240 return CommandSuccess;
241 }
242