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.

MassStorage.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 MassStorage demo. This file contains the main tasks of
  29. * the demo and is responsible for the initial application hardware configuration.
  30. */
  31. #include "MassStorage.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_Device_t Disk_MS_Interface =
  37. {
  38. .Config =
  39. {
  40. .InterfaceNumber = INTERFACE_ID_MassStorage,
  41. .DataINEndpoint =
  42. {
  43. .Address = MASS_STORAGE_IN_EPADDR,
  44. .Size = MASS_STORAGE_IO_EPSIZE,
  45. .Banks = 1,
  46. },
  47. .DataOUTEndpoint =
  48. {
  49. .Address = MASS_STORAGE_OUT_EPADDR,
  50. .Size = MASS_STORAGE_IO_EPSIZE,
  51. .Banks = 1,
  52. },
  53. .TotalLUNs = TOTAL_LUNS,
  54. },
  55. };
  56. /** Main program entry point. This routine contains the overall program flow, including initial
  57. * setup of all components and the main program loop.
  58. */
  59. int main(void)
  60. {
  61. SetupHardware();
  62. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  63. GlobalInterruptEnable();
  64. for (;;)
  65. {
  66. MS_Device_USBTask(&Disk_MS_Interface);
  67. USB_USBTask();
  68. }
  69. }
  70. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  71. void SetupHardware(void)
  72. {
  73. #if (ARCH == ARCH_AVR8)
  74. /* Disable watchdog if enabled by bootloader/fuses */
  75. MCUSR &= ~(1 << WDRF);
  76. wdt_disable();
  77. /* Disable clock division */
  78. clock_prescale_set(clock_div_1);
  79. #elif (ARCH == ARCH_XMEGA)
  80. /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
  81. XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
  82. XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
  83. /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
  84. XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
  85. XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
  86. PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
  87. #endif
  88. /* Hardware Initialization */
  89. LEDs_Init();
  90. Dataflash_Init();
  91. USB_Init();
  92. /* Check if the Dataflash is working, abort if not */
  93. if (!(DataflashManager_CheckDataflashOperation()))
  94. {
  95. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  96. for(;;);
  97. }
  98. /* Clear Dataflash sector protections, if enabled */
  99. DataflashManager_ResetDataflashProtections();
  100. }
  101. /** Event handler for the library USB Connection event. */
  102. void EVENT_USB_Device_Connect(void)
  103. {
  104. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  105. }
  106. /** Event handler for the library USB Disconnection event. */
  107. void EVENT_USB_Device_Disconnect(void)
  108. {
  109. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  110. }
  111. /** Event handler for the library USB Configuration Changed event. */
  112. void EVENT_USB_Device_ConfigurationChanged(void)
  113. {
  114. bool ConfigSuccess = true;
  115. ConfigSuccess &= MS_Device_ConfigureEndpoints(&Disk_MS_Interface);
  116. LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
  117. }
  118. /** Event handler for the library USB Control Request reception event. */
  119. void EVENT_USB_Device_ControlRequest(void)
  120. {
  121. MS_Device_ProcessControlRequest(&Disk_MS_Interface);
  122. }
  123. /** Mass Storage class driver callback function the reception of SCSI commands from the host, which must be processed.
  124. *
  125. * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface configuration structure being referenced
  126. */
  127. bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
  128. {
  129. bool CommandSuccess;
  130. LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
  131. CommandSuccess = SCSI_DecodeSCSICommand(MSInterfaceInfo);
  132. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  133. return CommandSuccess;
  134. }