]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/Demos/Host/ClassDriver/MassStorageHost/MassStorageHost.c
Squashed 'tmk_core/' changes from 8da1898..e5f9940
[tmk_keyboard.git] / protocol / lufa / LUFA-git / Demos / Host / ClassDriver / MassStorageHost / MassStorageHost.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 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 MassStorageHost demo. This file contains the main tasks of
34 * the demo and is responsible for the initial application hardware configuration.
35 */
36
37 #include "MassStorageHost.h"
38
39 /** LUFA Mass Storage Class driver interface configuration and state information. This structure is
40 * passed to all Mass Storage 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_MS_Host_t FlashDisk_MS_Interface =
44 {
45 .Config =
46 {
47 .DataINPipe =
48 {
49 .Address = (PIPE_DIR_IN | 1),
50 .Banks = 1,
51 },
52 .DataOUTPipe =
53 {
54 .Address = (PIPE_DIR_OUT | 2),
55 .Banks = 1,
56 },
57 },
58 };
59
60
61 /** Main program entry point. This routine configures the hardware required by the application, then
62 * enters a loop to run the application tasks in sequence.
63 */
64 int main(void)
65 {
66 SetupHardware();
67
68 puts_P(PSTR(ESC_FG_CYAN "Mass Storage Host Demo running.\r\n" ESC_FG_WHITE));
69
70 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
71 GlobalInterruptEnable();
72
73 for (;;)
74 {
75 MassStorageHost_Task();
76
77 MS_Host_USBTask(&FlashDisk_MS_Interface);
78 USB_USBTask();
79 }
80 }
81
82 /** Configures the board hardware and chip peripherals for the demo's functionality. */
83 void SetupHardware(void)
84 {
85 #if (ARCH == ARCH_AVR8)
86 /* Disable watchdog if enabled by bootloader/fuses */
87 MCUSR &= ~(1 << WDRF);
88 wdt_disable();
89
90 /* Disable clock division */
91 clock_prescale_set(clock_div_1);
92 #endif
93
94 /* Hardware Initialization */
95 Serial_Init(9600, false);
96 LEDs_Init();
97 USB_Init();
98
99 /* Create a stdio stream for the serial port for stdin and stdout */
100 Serial_CreateStream(NULL);
101 }
102
103 /** Task to manage an enumerated USB Mass Storage device once connected, to print out
104 * data from the device.
105 */
106 void MassStorageHost_Task(void)
107 {
108 if (USB_HostState != HOST_STATE_Configured)
109 return;
110
111 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
112
113 puts_P(PSTR("Waiting until ready...\r\n"));
114
115 for (;;)
116 {
117 uint8_t ErrorCode = MS_Host_TestUnitReady(&FlashDisk_MS_Interface, 0);
118
119 if (!(ErrorCode))
120 break;
121
122 /* Check if an error other than a logical command error (device busy) received */
123 if (ErrorCode != MS_ERROR_LOGICAL_CMD_FAILED)
124 {
125 puts_P(PSTR("Error waiting for device to be ready.\r\n"));
126 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
127 USB_Host_SetDeviceConfiguration(0);
128 return;
129 }
130 }
131
132 puts_P(PSTR("Retrieving Capacity...\r\n"));
133
134 SCSI_Capacity_t DiskCapacity;
135 if (MS_Host_ReadDeviceCapacity(&FlashDisk_MS_Interface, 0, &DiskCapacity))
136 {
137 puts_P(PSTR("Error retrieving device capacity.\r\n"));
138 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
139 USB_Host_SetDeviceConfiguration(0);
140 return;
141 }
142
143 printf_P(PSTR("%lu blocks of %lu bytes.\r\n"), DiskCapacity.Blocks, DiskCapacity.BlockSize);
144
145 uint8_t BlockBuffer[DiskCapacity.BlockSize];
146
147 if (MS_Host_ReadDeviceBlocks(&FlashDisk_MS_Interface, 0, 0x00000000, 1, DiskCapacity.BlockSize, BlockBuffer))
148 {
149 puts_P(PSTR("Error reading device block.\r\n"));
150 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
151 USB_Host_SetDeviceConfiguration(0);
152 return;
153 }
154
155 puts_P(PSTR("\r\nContents of first block:\r\n"));
156
157 for (uint16_t Chunk = 0; Chunk < (DiskCapacity.BlockSize >> 4); Chunk++)
158 {
159 uint8_t* ChunkPtr = &BlockBuffer[Chunk << 4];
160
161 /* Print out the 16 bytes of the chunk in HEX format */
162 for (uint8_t ByteOffset = 0; ByteOffset < (1 << 4); ByteOffset++)
163 {
164 char CurrByte = *(ChunkPtr + ByteOffset);
165 printf_P(PSTR("%.2X "), CurrByte);
166 }
167
168 printf_P(PSTR(" "));
169
170 /* Print out the 16 bytes of the chunk in ASCII format */
171 for (uint8_t ByteOffset = 0; ByteOffset < (1 << 4); ByteOffset++)
172 {
173 char CurrByte = *(ChunkPtr + ByteOffset);
174 putchar(isprint(CurrByte) ? CurrByte : '.');
175 }
176
177 printf_P(PSTR("\r\n"));
178 }
179
180 LEDs_SetAllLEDs(LEDMASK_USB_READY);
181 USB_Host_SetDeviceConfiguration(0);
182 }
183
184 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
185 * starts the library USB task to begin the enumeration and USB management process.
186 */
187 void EVENT_USB_Host_DeviceAttached(void)
188 {
189 puts_P(PSTR("Device Attached.\r\n"));
190 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
191 }
192
193 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
194 * stops the library USB task management process.
195 */
196 void EVENT_USB_Host_DeviceUnattached(void)
197 {
198 puts_P(PSTR("\r\nDevice Unattached.\r\n"));
199 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
200 }
201
202 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
203 * enumerated by the host and is now ready to be used by the application.
204 */
205 void EVENT_USB_Host_DeviceEnumerationComplete(void)
206 {
207 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
208
209 uint16_t ConfigDescriptorSize;
210 uint8_t ConfigDescriptorData[512];
211
212 if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
213 sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
214 {
215 puts_P(PSTR("Error Retrieving Configuration Descriptor.\r\n"));
216 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
217 return;
218 }
219
220 if (MS_Host_ConfigurePipes(&FlashDisk_MS_Interface,
221 ConfigDescriptorSize, ConfigDescriptorData) != MS_ENUMERROR_NoError)
222 {
223 puts_P(PSTR("Attached Device Not a Valid Mass Storage Device.\r\n"));
224 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
225 return;
226 }
227
228 if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
229 {
230 puts_P(PSTR("Error Setting Device Configuration.\r\n"));
231 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
232 return;
233 }
234
235 uint8_t MaxLUNIndex;
236 if (MS_Host_GetMaxLUN(&FlashDisk_MS_Interface, &MaxLUNIndex))
237 {
238 puts_P(PSTR("Error retrieving max LUN index.\r\n"));
239 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
240 USB_Host_SetDeviceConfiguration(0);
241 return;
242 }
243
244 printf_P(PSTR("Total LUNs: %d - Using first LUN in device.\r\n"), (MaxLUNIndex + 1));
245
246 if (MS_Host_ResetMSInterface(&FlashDisk_MS_Interface))
247 {
248 puts_P(PSTR("Error resetting Mass Storage interface.\r\n"));
249 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
250 USB_Host_SetDeviceConfiguration(0);
251 return;
252 }
253
254 SCSI_Request_Sense_Response_t SenseData;
255 if (MS_Host_RequestSense(&FlashDisk_MS_Interface, 0, &SenseData) != 0)
256 {
257 puts_P(PSTR("Error retrieving device sense.\r\n"));
258 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
259 USB_Host_SetDeviceConfiguration(0);
260 return;
261 }
262
263 if (MS_Host_PreventAllowMediumRemoval(&FlashDisk_MS_Interface, 0, true))
264 {
265 puts_P(PSTR("Error setting Prevent Device Removal bit.\r\n"));
266 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
267 USB_Host_SetDeviceConfiguration(0);
268 return;
269 }
270
271 SCSI_Inquiry_Response_t InquiryData;
272 if (MS_Host_GetInquiryData(&FlashDisk_MS_Interface, 0, &InquiryData))
273 {
274 puts_P(PSTR("Error retrieving device Inquiry data.\r\n"));
275 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
276 USB_Host_SetDeviceConfiguration(0);
277 return;
278 }
279
280 printf_P(PSTR("Vendor \"%.8s\", Product \"%.16s\"\r\n"), InquiryData.VendorID, InquiryData.ProductID);
281
282 puts_P(PSTR("Mass Storage Device Enumerated.\r\n"));
283 LEDs_SetAllLEDs(LEDMASK_USB_READY);
284 }
285
286 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
287 void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
288 {
289 USB_Disable();
290
291 printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
292 " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
293
294 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
295 for(;;);
296 }
297
298 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
299 * enumerating an attached USB device.
300 */
301 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
302 const uint8_t SubErrorCode)
303 {
304 printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
305 " -- Error Code %d\r\n"
306 " -- Sub Error Code %d\r\n"
307 " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
308
309 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
310 }
311
Imprint / Impressum