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.

KeyboardMouse.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 Denver Gingerich (denver [at] ossguy [dot] com)
  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 KeyboardMouse demo. This file contains the main tasks of the demo and
  30. * is responsible for the initial application hardware configuration.
  31. */
  32. #include "KeyboardMouse.h"
  33. /** Global structure to hold the current keyboard interface HID report, for transmission to the host */
  34. static USB_KeyboardReport_Data_t KeyboardReportData;
  35. /** Global structure to hold the current mouse interface HID report, for transmission to the host */
  36. static USB_MouseReport_Data_t MouseReportData;
  37. /** Main program entry point. This routine configures the hardware required by the application, then
  38. * enters a loop to run the application tasks in sequence.
  39. */
  40. int main(void)
  41. {
  42. SetupHardware();
  43. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  44. GlobalInterruptEnable();
  45. for (;;)
  46. {
  47. Keyboard_HID_Task();
  48. Mouse_HID_Task();
  49. USB_USBTask();
  50. }
  51. }
  52. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  53. void SetupHardware(void)
  54. {
  55. #if (ARCH == ARCH_AVR8)
  56. /* Disable watchdog if enabled by bootloader/fuses */
  57. MCUSR &= ~(1 << WDRF);
  58. wdt_disable();
  59. /* Disable clock division */
  60. clock_prescale_set(clock_div_1);
  61. #elif (ARCH == ARCH_XMEGA)
  62. /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
  63. XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
  64. XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
  65. /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
  66. XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
  67. XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
  68. PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
  69. #endif
  70. /* Hardware Initialization */
  71. Joystick_Init();
  72. LEDs_Init();
  73. USB_Init();
  74. }
  75. /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
  76. * starts the library USB task to begin the enumeration and USB management process.
  77. */
  78. void EVENT_USB_Device_Connect(void)
  79. {
  80. /* Indicate USB enumerating */
  81. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  82. }
  83. /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
  84. * the status LEDs and stops the USB management task.
  85. */
  86. void EVENT_USB_Device_Disconnect(void)
  87. {
  88. /* Indicate USB not ready */
  89. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  90. }
  91. /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
  92. * of the USB device after enumeration, and configures the keyboard and mouse device endpoints.
  93. */
  94. void EVENT_USB_Device_ConfigurationChanged(void)
  95. {
  96. bool ConfigSuccess = true;
  97. /* Setup Keyboard HID Report Endpoints */
  98. ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPADDR, EP_TYPE_INTERRUPT, HID_EPSIZE, 1);
  99. ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_OUT_EPADDR, EP_TYPE_INTERRUPT, HID_EPSIZE, 1);
  100. /* Setup Mouse HID Report Endpoint */
  101. ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPADDR, EP_TYPE_INTERRUPT, HID_EPSIZE, 1);
  102. /* Indicate endpoint configuration success or failure */
  103. LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
  104. }
  105. /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
  106. * the device from the USB host before passing along unhandled control requests to the library for processing
  107. * internally.
  108. */
  109. void EVENT_USB_Device_ControlRequest(void)
  110. {
  111. uint8_t* ReportData;
  112. uint8_t ReportSize;
  113. /* Handle HID Class specific requests */
  114. switch (USB_ControlRequest.bRequest)
  115. {
  116. case HID_REQ_GetReport:
  117. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  118. {
  119. Endpoint_ClearSETUP();
  120. /* Determine if it is the mouse or the keyboard data that is being requested */
  121. if (!(USB_ControlRequest.wIndex))
  122. {
  123. ReportData = (uint8_t*)&KeyboardReportData;
  124. ReportSize = sizeof(KeyboardReportData);
  125. }
  126. else
  127. {
  128. ReportData = (uint8_t*)&MouseReportData;
  129. ReportSize = sizeof(MouseReportData);
  130. }
  131. /* Write the report data to the control endpoint */
  132. Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
  133. Endpoint_ClearOUT();
  134. /* Clear the report data afterwards */
  135. memset(ReportData, 0, ReportSize);
  136. }
  137. break;
  138. case HID_REQ_SetReport:
  139. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  140. {
  141. Endpoint_ClearSETUP();
  142. /* Wait until the LED report has been sent by the host */
  143. while (!(Endpoint_IsOUTReceived()))
  144. {
  145. if (USB_DeviceState == DEVICE_STATE_Unattached)
  146. return;
  147. }
  148. /* Read in the LED report from the host */
  149. uint8_t LEDStatus = Endpoint_Read_8();
  150. Endpoint_ClearOUT();
  151. Endpoint_ClearStatusStage();
  152. /* Process the incoming LED report */
  153. Keyboard_ProcessLEDReport(LEDStatus);
  154. }
  155. break;
  156. }
  157. }
  158. /** Processes a given Keyboard LED report from the host, and sets the board LEDs to match. Since the Keyboard
  159. * LED report can be sent through either the control endpoint (via a HID SetReport request) or the HID OUT
  160. * endpoint, the processing code is placed here to avoid duplicating it and potentially having different
  161. * behavior depending on the method used to sent it.
  162. */
  163. void Keyboard_ProcessLEDReport(const uint8_t LEDStatus)
  164. {
  165. uint8_t LEDMask = LEDS_LED2;
  166. if (LEDStatus & HID_KEYBOARD_LED_NUMLOCK)
  167. LEDMask |= LEDS_LED1;
  168. if (LEDStatus & HID_KEYBOARD_LED_CAPSLOCK)
  169. LEDMask |= LEDS_LED3;
  170. if (LEDStatus & HID_KEYBOARD_LED_SCROLLLOCK)
  171. LEDMask |= LEDS_LED4;
  172. /* Set the status LEDs to the current Keyboard LED status */
  173. LEDs_SetAllLEDs(LEDMask);
  174. }
  175. /** Keyboard task. This generates the next keyboard HID report for the host, and transmits it via the
  176. * keyboard IN endpoint when the host is ready for more data. Additionally, it processes host LED status
  177. * reports sent to the device via the keyboard OUT reporting endpoint.
  178. */
  179. void Keyboard_HID_Task(void)
  180. {
  181. uint8_t JoyStatus_LCL = Joystick_GetStatus();
  182. /* Device must be connected and configured for the task to run */
  183. if (USB_DeviceState != DEVICE_STATE_Configured)
  184. return;
  185. /* Check if board button is not pressed, if so mouse mode enabled */
  186. if (!(Buttons_GetStatus() & BUTTONS_BUTTON1))
  187. {
  188. /* Make sent key uppercase by indicating that the left shift key is pressed */
  189. KeyboardReportData.Modifier = HID_KEYBOARD_MODIFIER_LEFTSHIFT;
  190. if (JoyStatus_LCL & JOY_UP)
  191. KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_A;
  192. else if (JoyStatus_LCL & JOY_DOWN)
  193. KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_B;
  194. if (JoyStatus_LCL & JOY_LEFT)
  195. KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_C;
  196. else if (JoyStatus_LCL & JOY_RIGHT)
  197. KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_D;
  198. if (JoyStatus_LCL & JOY_PRESS)
  199. KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_E;
  200. }
  201. /* Select the Keyboard Report Endpoint */
  202. Endpoint_SelectEndpoint(KEYBOARD_IN_EPADDR);
  203. /* Check if Keyboard Endpoint Ready for Read/Write */
  204. if (Endpoint_IsReadWriteAllowed())
  205. {
  206. /* Write Keyboard Report Data */
  207. Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData), NULL);
  208. /* Finalize the stream transfer to send the last packet */
  209. Endpoint_ClearIN();
  210. /* Clear the report data afterwards */
  211. memset(&KeyboardReportData, 0, sizeof(KeyboardReportData));
  212. }
  213. /* Select the Keyboard LED Report Endpoint */
  214. Endpoint_SelectEndpoint(KEYBOARD_OUT_EPADDR);
  215. /* Check if Keyboard LED Endpoint Ready for Read/Write */
  216. if (Endpoint_IsReadWriteAllowed())
  217. {
  218. /* Read in and process the LED report from the host */
  219. Keyboard_ProcessLEDReport(Endpoint_Read_8());
  220. /* Handshake the OUT Endpoint - clear endpoint and ready for next report */
  221. Endpoint_ClearOUT();
  222. }
  223. }
  224. /** Mouse task. This generates the next mouse HID report for the host, and transmits it via the
  225. * mouse IN endpoint when the host is ready for more data.
  226. */
  227. void Mouse_HID_Task(void)
  228. {
  229. uint8_t JoyStatus_LCL = Joystick_GetStatus();
  230. /* Device must be connected and configured for the task to run */
  231. if (USB_DeviceState != DEVICE_STATE_Configured)
  232. return;
  233. /* Check if board button is pressed, if so mouse mode enabled */
  234. if (Buttons_GetStatus() & BUTTONS_BUTTON1)
  235. {
  236. if (JoyStatus_LCL & JOY_UP)
  237. MouseReportData.Y = 1;
  238. else if (JoyStatus_LCL & JOY_DOWN)
  239. MouseReportData.Y = -1;
  240. if (JoyStatus_LCL & JOY_RIGHT)
  241. MouseReportData.X = 1;
  242. else if (JoyStatus_LCL & JOY_LEFT)
  243. MouseReportData.X = -1;
  244. if (JoyStatus_LCL & JOY_PRESS)
  245. MouseReportData.Button |= (1 << 0);
  246. }
  247. /* Select the Mouse Report Endpoint */
  248. Endpoint_SelectEndpoint(MOUSE_IN_EPADDR);
  249. /* Check if Mouse Endpoint Ready for Read/Write */
  250. if (Endpoint_IsReadWriteAllowed())
  251. {
  252. /* Write Mouse Report Data */
  253. Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData), NULL);
  254. /* Finalize the stream transfer to send the last packet */
  255. Endpoint_ClearIN();
  256. /* Clear the report data afterwards */
  257. memset(&MouseReportData, 0, sizeof(MouseReportData));
  258. }
  259. }