Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MassStorageHost.c 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * Main source file for the MassStorageHost demo. This file contains the main tasks of
  29. * the demo and is responsible for the initial application hardware configuration.
  30. */
  31. #include "MassStorageHost.h"
  32. /** LUFA Mass Storage Class driver interface configuration and state information. This structure is
  33. * passed to all Mass Storage Class driver functions, so that multiple instances of the same class
  34. * within a device can be differentiated from one another.
  35. */
  36. USB_ClassInfo_MS_Host_t FlashDisk_MS_Interface =
  37. {
  38. .Config =
  39. {
  40. .DataINPipe =
  41. {
  42. .Address = (PIPE_DIR_IN | 1),
  43. .Banks = 1,
  44. },
  45. .DataOUTPipe =
  46. {
  47. .Address = (PIPE_DIR_OUT | 2),
  48. .Banks = 1,
  49. },
  50. },
  51. };
  52. /** Main program entry point. This routine configures the hardware required by the application, then
  53. * enters a loop to run the application tasks in sequence.
  54. */
  55. int main(void)
  56. {
  57. SetupHardware();
  58. puts_P(PSTR(ESC_FG_CYAN "Mass Storage Host Demo running.\r\n" ESC_FG_WHITE));
  59. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  60. GlobalInterruptEnable();
  61. for (;;)
  62. {
  63. MassStorageHost_Task();
  64. MS_Host_USBTask(&FlashDisk_MS_Interface);
  65. USB_USBTask();
  66. }
  67. }
  68. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  69. void SetupHardware(void)
  70. {
  71. #if (ARCH == ARCH_AVR8)
  72. /* Disable watchdog if enabled by bootloader/fuses */
  73. MCUSR &= ~(1 << WDRF);
  74. wdt_disable();
  75. /* Disable clock division */
  76. clock_prescale_set(clock_div_1);
  77. #endif
  78. /* Hardware Initialization */
  79. Serial_Init(9600, false);
  80. LEDs_Init();
  81. USB_Init();
  82. /* Create a stdio stream for the serial port for stdin and stdout */
  83. Serial_CreateStream(NULL);
  84. }
  85. /** Task to manage an enumerated USB Mass Storage device once connected, to print out
  86. * data from the device.
  87. */
  88. void MassStorageHost_Task(void)
  89. {
  90. if (USB_HostState != HOST_STATE_Configured)
  91. return;
  92. LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
  93. puts_P(PSTR("Waiting until ready...\r\n"));
  94. for (;;)
  95. {
  96. uint8_t ErrorCode = MS_Host_TestUnitReady(&FlashDisk_MS_Interface, 0);
  97. if (!(ErrorCode))
  98. break;
  99. /* Check if an error other than a logical command error (device busy) received */
  100. if (ErrorCode != MS_ERROR_LOGICAL_CMD_FAILED)
  101. {
  102. puts_P(PSTR("Error waiting for device to be ready.\r\n"));
  103. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  104. USB_Host_SetDeviceConfiguration(0);
  105. return;
  106. }
  107. }
  108. puts_P(PSTR("Retrieving Capacity...\r\n"));
  109. SCSI_Capacity_t DiskCapacity;
  110. if (MS_Host_ReadDeviceCapacity(&FlashDisk_MS_Interface, 0, &DiskCapacity))
  111. {
  112. puts_P(PSTR("Error retrieving device capacity.\r\n"));
  113. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  114. USB_Host_SetDeviceConfiguration(0);
  115. return;
  116. }
  117. printf_P(PSTR("%lu blocks of %lu bytes.\r\n"), DiskCapacity.Blocks, DiskCapacity.BlockSize);
  118. uint8_t BlockBuffer[DiskCapacity.BlockSize];
  119. if (MS_Host_ReadDeviceBlocks(&FlashDisk_MS_Interface, 0, 0x00000000, 1, DiskCapacity.BlockSize, BlockBuffer))
  120. {
  121. puts_P(PSTR("Error reading device block.\r\n"));
  122. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  123. USB_Host_SetDeviceConfiguration(0);
  124. return;
  125. }
  126. puts_P(PSTR("\r\nContents of first block:\r\n"));
  127. for (uint16_t Chunk = 0; Chunk < (DiskCapacity.BlockSize >> 4); Chunk++)
  128. {
  129. uint8_t* ChunkPtr = &BlockBuffer[Chunk << 4];
  130. /* Print out the 16 bytes of the chunk in HEX format */
  131. for (uint8_t ByteOffset = 0; ByteOffset < (1 << 4); ByteOffset++)
  132. {
  133. char CurrByte = *(ChunkPtr + ByteOffset);
  134. printf_P(PSTR("%.2X "), CurrByte);
  135. }
  136. printf_P(PSTR(" "));
  137. /* Print out the 16 bytes of the chunk in ASCII format */
  138. for (uint8_t ByteOffset = 0; ByteOffset < (1 << 4); ByteOffset++)
  139. {
  140. char CurrByte = *(ChunkPtr + ByteOffset);
  141. putchar(isprint(CurrByte) ? CurrByte : '.');
  142. }
  143. printf_P(PSTR("\r\n"));
  144. }
  145. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  146. USB_Host_SetDeviceConfiguration(0);
  147. }
  148. /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
  149. * starts the library USB task to begin the enumeration and USB management process.
  150. */
  151. void EVENT_USB_Host_DeviceAttached(void)
  152. {
  153. puts_P(PSTR("Device Attached.\r\n"));
  154. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  155. }
  156. /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
  157. * stops the library USB task management process.
  158. */
  159. void EVENT_USB_Host_DeviceUnattached(void)
  160. {
  161. puts_P(PSTR("\r\nDevice Unattached.\r\n"));
  162. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  163. }
  164. /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
  165. * enumerated by the host and is now ready to be used by the application.
  166. */
  167. void EVENT_USB_Host_DeviceEnumerationComplete(void)
  168. {
  169. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  170. uint16_t ConfigDescriptorSize;
  171. uint8_t ConfigDescriptorData[512];
  172. if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
  173. sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
  174. {
  175. puts_P(PSTR("Error Retrieving Configuration Descriptor.\r\n"));
  176. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  177. return;
  178. }
  179. if (MS_Host_ConfigurePipes(&FlashDisk_MS_Interface,
  180. ConfigDescriptorSize, ConfigDescriptorData) != MS_ENUMERROR_NoError)
  181. {
  182. puts_P(PSTR("Attached Device Not a Valid Mass Storage Device.\r\n"));
  183. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  184. return;
  185. }
  186. if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
  187. {
  188. puts_P(PSTR("Error Setting Device Configuration.\r\n"));
  189. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  190. return;
  191. }
  192. uint8_t MaxLUNIndex;
  193. if (MS_Host_GetMaxLUN(&FlashDisk_MS_Interface, &MaxLUNIndex))
  194. {
  195. puts_P(PSTR("Error retrieving max LUN index.\r\n"));
  196. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  197. USB_Host_SetDeviceConfiguration(0);
  198. return;
  199. }
  200. printf_P(PSTR("Total LUNs: %d - Using first LUN in device.\r\n"), (MaxLUNIndex + 1));
  201. if (MS_Host_ResetMSInterface(&FlashDisk_MS_Interface))
  202. {
  203. puts_P(PSTR("Error resetting Mass Storage interface.\r\n"));
  204. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  205. USB_Host_SetDeviceConfiguration(0);
  206. return;
  207. }
  208. SCSI_Request_Sense_Response_t SenseData;
  209. if (MS_Host_RequestSense(&FlashDisk_MS_Interface, 0, &SenseData) != 0)
  210. {
  211. puts_P(PSTR("Error retrieving device sense.\r\n"));
  212. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  213. USB_Host_SetDeviceConfiguration(0);
  214. return;
  215. }
  216. if (MS_Host_PreventAllowMediumRemoval(&FlashDisk_MS_Interface, 0, true))
  217. {
  218. puts_P(PSTR("Error setting Prevent Device Removal bit.\r\n"));
  219. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  220. USB_Host_SetDeviceConfiguration(0);
  221. return;
  222. }
  223. SCSI_Inquiry_Response_t InquiryData;
  224. if (MS_Host_GetInquiryData(&FlashDisk_MS_Interface, 0, &InquiryData))
  225. {
  226. puts_P(PSTR("Error retrieving device Inquiry data.\r\n"));
  227. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  228. USB_Host_SetDeviceConfiguration(0);
  229. return;
  230. }
  231. printf_P(PSTR("Vendor \"%.8s\", Product \"%.16s\"\r\n"), InquiryData.VendorID, InquiryData.ProductID);
  232. puts_P(PSTR("Mass Storage Device Enumerated.\r\n"));
  233. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  234. }
  235. /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
  236. void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
  237. {
  238. USB_Disable();
  239. printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
  240. " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
  241. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  242. for(;;);
  243. }
  244. /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
  245. * enumerating an attached USB device.
  246. */
  247. void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
  248. const uint8_t SubErrorCode)
  249. {
  250. printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
  251. " -- Error Code %d\r\n"
  252. " -- Sub Error Code %d\r\n"
  253. " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
  254. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  255. }