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.

MassStorageKeyboard.c 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. Copyright 2010 Matthias Hullin (lufa [at] matthias [dot] hullin [dot] net)
  10. Permission to use, copy, modify, distribute, and sell this
  11. software and its documentation for any purpose is hereby granted
  12. without fee, provided that the above copyright notice appear in
  13. all copies and that both that the copyright notice and this
  14. permission notice and warranty disclaimer appear in supporting
  15. documentation, and that the name of the author not be used in
  16. advertising or publicity pertaining to distribution of the
  17. software without specific, written prior permission.
  18. The author disclaims all warranties with regard to this
  19. software, including all implied warranties of merchantability
  20. and fitness. In no event shall the author be liable for any
  21. special, indirect or consequential damages or any damages
  22. whatsoever resulting from loss of use, data or profits, whether
  23. in an action of contract, negligence or other tortious action,
  24. arising out of or in connection with the use or performance of
  25. this software.
  26. */
  27. /** \file
  28. *
  29. * Main source file for the MassStorageKeyboard demo. This file contains the main tasks of
  30. * the demo and is responsible for the initial application hardware configuration.
  31. */
  32. #include "MassStorageKeyboard.h"
  33. /** LUFA Mass Storage Class driver interface configuration and state information. This structure is
  34. * passed to all Mass Storage Class driver functions, so that multiple instances of the same class
  35. * within a device can be differentiated from one another.
  36. */
  37. USB_ClassInfo_MS_Device_t Disk_MS_Interface =
  38. {
  39. .Config =
  40. {
  41. .InterfaceNumber = INTERFACE_ID_MassStorage,
  42. .DataINEndpoint =
  43. {
  44. .Address = MASS_STORAGE_IN_EPADDR,
  45. .Size = MASS_STORAGE_IO_EPSIZE,
  46. .Banks = 1,
  47. },
  48. .DataOUTEndpoint =
  49. {
  50. .Address = MASS_STORAGE_OUT_EPADDR,
  51. .Size = MASS_STORAGE_IO_EPSIZE,
  52. .Banks = 1,
  53. },
  54. .TotalLUNs = TOTAL_LUNS,
  55. },
  56. };
  57. /** Buffer to hold the previously generated Keyboard HID report, for comparison purposes inside the HID class driver. */
  58. static uint8_t PrevKeyboardHIDReportBuffer[sizeof(USB_KeyboardReport_Data_t)];
  59. /** LUFA HID Class driver interface configuration and state information. This structure is
  60. * passed to all HID Class driver functions, so that multiple instances of the same class
  61. * within a device can be differentiated from one another.
  62. */
  63. USB_ClassInfo_HID_Device_t Keyboard_HID_Interface =
  64. {
  65. .Config =
  66. {
  67. .InterfaceNumber = INTERFACE_ID_Keyboard,
  68. .ReportINEndpoint =
  69. {
  70. .Address = KEYBOARD_EPADDR,
  71. .Size = KEYBOARD_EPSIZE,
  72. .Banks = 1,
  73. },
  74. .PrevReportINBuffer = PrevKeyboardHIDReportBuffer,
  75. .PrevReportINBufferSize = sizeof(PrevKeyboardHIDReportBuffer),
  76. },
  77. };
  78. /** Main program entry point. This routine contains the overall program flow, including initial
  79. * setup of all components and the main program loop.
  80. */
  81. int main(void)
  82. {
  83. SetupHardware();
  84. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  85. GlobalInterruptEnable();
  86. for (;;)
  87. {
  88. MS_Device_USBTask(&Disk_MS_Interface);
  89. HID_Device_USBTask(&Keyboard_HID_Interface);
  90. USB_USBTask();
  91. }
  92. }
  93. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  94. void SetupHardware(void)
  95. {
  96. #if (ARCH == ARCH_AVR8)
  97. /* Disable watchdog if enabled by bootloader/fuses */
  98. MCUSR &= ~(1 << WDRF);
  99. wdt_disable();
  100. /* Disable clock division */
  101. clock_prescale_set(clock_div_1);
  102. #elif (ARCH == ARCH_XMEGA)
  103. /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
  104. XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
  105. XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
  106. /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
  107. XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
  108. XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
  109. PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
  110. #endif
  111. /* Hardware Initialization */
  112. LEDs_Init();
  113. Joystick_Init();
  114. Buttons_Init();
  115. Dataflash_Init();
  116. USB_Init();
  117. /* Check if the Dataflash is working, abort if not */
  118. if (!(DataflashManager_CheckDataflashOperation()))
  119. {
  120. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  121. for(;;);
  122. }
  123. /* Clear Dataflash sector protections, if enabled */
  124. DataflashManager_ResetDataflashProtections();
  125. }
  126. /** Event handler for the library USB Connection event. */
  127. void EVENT_USB_Device_Connect(void)
  128. {
  129. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  130. }
  131. /** Event handler for the library USB Disconnection event. */
  132. void EVENT_USB_Device_Disconnect(void)
  133. {
  134. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  135. }
  136. /** Event handler for the library USB Configuration Changed event. */
  137. void EVENT_USB_Device_ConfigurationChanged(void)
  138. {
  139. bool ConfigSuccess = true;
  140. ConfigSuccess &= HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface);
  141. ConfigSuccess &= MS_Device_ConfigureEndpoints(&Disk_MS_Interface);
  142. USB_Device_EnableSOFEvents();
  143. LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
  144. }
  145. /** Event handler for the library USB Control Request reception event. */
  146. void EVENT_USB_Device_ControlRequest(void)
  147. {
  148. MS_Device_ProcessControlRequest(&Disk_MS_Interface);
  149. HID_Device_ProcessControlRequest(&Keyboard_HID_Interface);
  150. }
  151. /** Mass Storage class driver callback function the reception of SCSI commands from the host, which must be processed.
  152. *
  153. * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface configuration structure being referenced
  154. */
  155. bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
  156. {
  157. bool CommandSuccess;
  158. LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
  159. CommandSuccess = SCSI_DecodeSCSICommand(MSInterfaceInfo);
  160. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  161. return CommandSuccess;
  162. }
  163. /** Event handler for the USB device Start Of Frame event. */
  164. void EVENT_USB_Device_StartOfFrame(void)
  165. {
  166. HID_Device_MillisecondElapsed(&Keyboard_HID_Interface);
  167. }
  168. /** HID class driver callback function for the creation of HID reports to the host.
  169. *
  170. * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
  171. * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
  172. * \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
  173. * \param[out] ReportData Pointer to a buffer where the created report should be stored
  174. * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
  175. *
  176. * \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
  177. */
  178. bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
  179. uint8_t* const ReportID,
  180. const uint8_t ReportType,
  181. void* ReportData,
  182. uint16_t* const ReportSize)
  183. {
  184. USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;
  185. uint8_t JoyStatus_LCL = Joystick_GetStatus();
  186. uint8_t ButtonStatus_LCL = Buttons_GetStatus();
  187. KeyboardReport->Modifier = HID_KEYBOARD_MODIFIER_LEFTSHIFT;
  188. if (JoyStatus_LCL & JOY_UP)
  189. KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_A;
  190. else if (JoyStatus_LCL & JOY_DOWN)
  191. KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_B;
  192. if (JoyStatus_LCL & JOY_LEFT)
  193. KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_C;
  194. else if (JoyStatus_LCL & JOY_RIGHT)
  195. KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_D;
  196. if (JoyStatus_LCL & JOY_PRESS)
  197. KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_E;
  198. if (ButtonStatus_LCL & BUTTONS_BUTTON1)
  199. KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_F;
  200. *ReportSize = sizeof(USB_KeyboardReport_Data_t);
  201. return false;
  202. }
  203. /** HID class driver callback function for the processing of HID reports from the host.
  204. *
  205. * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
  206. * \param[in] ReportID Report ID of the received report from the host
  207. * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
  208. * \param[in] ReportData Pointer to a buffer where the received report has been stored
  209. * \param[in] ReportSize Size in bytes of the received HID report
  210. */
  211. void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
  212. const uint8_t ReportID,
  213. const uint8_t ReportType,
  214. const void* ReportData,
  215. const uint16_t ReportSize)
  216. {
  217. uint8_t LEDMask = LEDS_NO_LEDS;
  218. uint8_t* LEDReport = (uint8_t*)ReportData;
  219. if (*LEDReport & HID_KEYBOARD_LED_NUMLOCK)
  220. LEDMask |= LEDS_LED1;
  221. if (*LEDReport & HID_KEYBOARD_LED_CAPSLOCK)
  222. LEDMask |= LEDS_LED3;
  223. if (*LEDReport & HID_KEYBOARD_LED_SCROLLLOCK)
  224. LEDMask |= LEDS_LED4;
  225. LEDs_SetAllLEDs(LEDMask);
  226. }