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.

lufa.c 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 2012 Jun Wako <[email protected]>
  3. * This file is based on LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse.
  4. */
  5. /*
  6. LUFA Library
  7. Copyright (C) Dean Camera, 2012.
  8. dean [at] fourwalledcubicle [dot] com
  9. www.lufa-lib.org
  10. */
  11. /*
  12. Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  13. Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
  14. Permission to use, copy, modify, distribute, and sell this
  15. software and its documentation for any purpose is hereby granted
  16. without fee, provided that the above copyright notice appear in
  17. all copies and that both that the copyright notice and this
  18. permission notice and warranty disclaimer appear in supporting
  19. documentation, and that the name of the author not be used in
  20. advertising or publicity pertaining to distribution of the
  21. software without specific, written prior permission.
  22. The author disclaim all warranties with regard to this
  23. software, including all implied warranties of merchantability
  24. and fitness. In no event shall the author be liable for any
  25. special, indirect or consequential damages or any damages
  26. whatsoever resulting from loss of use, data or profits, whether
  27. in an action of contract, negligence or other tortious action,
  28. arising out of or in connection with the use or performance of
  29. this software.
  30. */
  31. #include "report.h"
  32. #include "host.h"
  33. #include "host_driver.h"
  34. #include "keyboard.h"
  35. #include "lufa.h"
  36. static uint8_t keyboard_led_stats = 0;
  37. report_keyboard_t keyboard_report_sent;
  38. report_mouse_t mouse_report_sent;
  39. /* Host driver */
  40. static uint8_t keyboard_leds(void);
  41. static void send_keyboard(report_keyboard_t *report);
  42. static void send_mouse(report_mouse_t *report);
  43. static void send_system(uint16_t data);
  44. static void send_consumer(uint16_t data);
  45. static host_driver_t lufa_driver = {
  46. keyboard_leds,
  47. send_keyboard,
  48. send_mouse,
  49. send_system,
  50. send_consumer
  51. };
  52. int main(void)
  53. {
  54. SetupHardware();
  55. sei();
  56. keyboard_init();
  57. host_set_driver(&lufa_driver);
  58. while (1) {
  59. keyboard_proc();
  60. Keyboard_HID_Task();
  61. USB_USBTask();
  62. }
  63. }
  64. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  65. void SetupHardware(void)
  66. {
  67. /* Disable watchdog if enabled by bootloader/fuses */
  68. MCUSR &= ~(1 << WDRF);
  69. wdt_disable();
  70. /* Disable clock division */
  71. clock_prescale_set(clock_div_1);
  72. USB_Init();
  73. }
  74. /** Event handler for the USB_Connect event. */
  75. void EVENT_USB_Device_Connect(void)
  76. {
  77. }
  78. /** Event handler for the USB_Disconnect event. */
  79. void EVENT_USB_Device_Disconnect(void)
  80. {
  81. }
  82. /** Event handler for the USB_ConfigurationChanged event.
  83. * This is fired when the host sets the current configuration of the USB device after enumeration.
  84. */
  85. void EVENT_USB_Device_ConfigurationChanged(void)
  86. {
  87. bool ConfigSuccess = true;
  88. /* Setup Keyboard HID Report Endpoints */
  89. ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  90. HID_EPSIZE, ENDPOINT_BANK_SINGLE);
  91. ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
  92. HID_EPSIZE, ENDPOINT_BANK_SINGLE);
  93. /* Setup Mouse HID Report Endpoint */
  94. ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  95. HID_EPSIZE, ENDPOINT_BANK_SINGLE);
  96. }
  97. /** Event handler for the USB_ControlRequest event.
  98. * This is fired before passing along unhandled control requests to the library for processing internally.
  99. */
  100. void EVENT_USB_Device_ControlRequest(void)
  101. {
  102. uint8_t* ReportData;
  103. uint8_t ReportSize;
  104. /* Handle HID Class specific requests */
  105. switch (USB_ControlRequest.bRequest)
  106. {
  107. case HID_REQ_GetReport:
  108. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  109. {
  110. Endpoint_ClearSETUP();
  111. /* Determine if it is the mouse or the keyboard data that is being requested */
  112. if (!(USB_ControlRequest.wIndex))
  113. {
  114. ReportData = (uint8_t*)&keyboard_report_sent;
  115. ReportSize = sizeof(keyboard_report_sent);
  116. }
  117. else
  118. {
  119. ReportData = (uint8_t*)&mouse_report_sent;
  120. ReportSize = sizeof(mouse_report_sent);
  121. }
  122. /* Write the report data to the control endpoint */
  123. Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
  124. Endpoint_ClearOUT();
  125. }
  126. break;
  127. case HID_REQ_SetReport:
  128. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  129. {
  130. Endpoint_ClearSETUP();
  131. /* Wait until the LED report has been sent by the host */
  132. while (!(Endpoint_IsOUTReceived()))
  133. {
  134. if (USB_DeviceState == DEVICE_STATE_Unattached)
  135. return;
  136. }
  137. /* Read in the LED report from the host */
  138. keyboard_led_stats = Endpoint_Read_8();
  139. Endpoint_ClearOUT();
  140. Endpoint_ClearStatusStage();
  141. }
  142. break;
  143. }
  144. }
  145. /** Keyboard task.
  146. * This processes host LED status reports sent to the device via the keyboard OUT reporting endpoint.
  147. */
  148. void Keyboard_HID_Task(void)
  149. {
  150. /* Select the Keyboard LED Report Endpoint */
  151. Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM);
  152. /* Check if Keyboard LED Endpoint Ready for Read/Write */
  153. if (Endpoint_IsReadWriteAllowed())
  154. {
  155. /* Read in the LED report from the host */
  156. keyboard_led_stats = Endpoint_Read_8();
  157. /* Handshake the OUT Endpoint - clear endpoint and ready for next report */
  158. Endpoint_ClearOUT();
  159. }
  160. }
  161. /*******************************************************************************
  162. * Host driver
  163. ******************************************************************************/
  164. static uint8_t keyboard_leds(void)
  165. {
  166. return keyboard_led_stats;
  167. }
  168. static void send_keyboard(report_keyboard_t *report)
  169. {
  170. // TODO: handle NKRO report
  171. /* Select the Keyboard Report Endpoint */
  172. Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
  173. /* Check if Keyboard Endpoint Ready for Read/Write */
  174. if (Endpoint_IsReadWriteAllowed())
  175. {
  176. /* Write Keyboard Report Data */
  177. Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL);
  178. /* Finalize the stream transfer to send the last packet */
  179. Endpoint_ClearIN();
  180. }
  181. keyboard_report_sent = *report;
  182. }
  183. static void send_mouse(report_mouse_t *report)
  184. {
  185. /* Select the Mouse Report Endpoint */
  186. Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
  187. /* Check if Mouse Endpoint Ready for Read/Write */
  188. if (Endpoint_IsReadWriteAllowed())
  189. {
  190. /* Write Mouse Report Data */
  191. Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
  192. /* Finalize the stream transfer to send the last packet */
  193. Endpoint_ClearIN();
  194. }
  195. mouse_report_sent = *report;
  196. }
  197. static void send_system(uint16_t data)
  198. {
  199. }
  200. static void send_consumer(uint16_t data)
  201. {
  202. }