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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright 2012 Jun Wako <[email protected]>
  3. * This file is based on:
  4. * LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse
  5. * LUFA-120219/Demos/Device/Lowlevel/GenericHID
  6. */
  7. /*
  8. LUFA Library
  9. Copyright (C) Dean Camera, 2012.
  10. dean [at] fourwalledcubicle [dot] com
  11. www.lufa-lib.org
  12. */
  13. /*
  14. Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  15. Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
  16. Permission to use, copy, modify, distribute, and sell this
  17. software and its documentation for any purpose is hereby granted
  18. without fee, provided that the above copyright notice appear in
  19. all copies and that both that the copyright notice and this
  20. permission notice and warranty disclaimer appear in supporting
  21. documentation, and that the name of the author not be used in
  22. advertising or publicity pertaining to distribution of the
  23. software without specific, written prior permission.
  24. The author disclaim all warranties with regard to this
  25. software, including all implied warranties of merchantability
  26. and fitness. In no event shall the author be liable for any
  27. special, indirect or consequential damages or any damages
  28. whatsoever resulting from loss of use, data or profits, whether
  29. in an action of contract, negligence or other tortious action,
  30. arising out of or in connection with the use or performance of
  31. this software.
  32. */
  33. #include "report.h"
  34. #include "host.h"
  35. #include "host_driver.h"
  36. #include "keyboard.h"
  37. #include "sendchar.h"
  38. #include "debug.h"
  39. #include "lufa.h"
  40. static uint8_t keyboard_led_stats = 0;
  41. report_keyboard_t keyboard_report_sent;
  42. report_mouse_t mouse_report_sent;
  43. /* Host driver */
  44. static uint8_t keyboard_leds(void);
  45. static void send_keyboard(report_keyboard_t *report);
  46. static void send_mouse(report_mouse_t *report);
  47. static void send_system(uint16_t data);
  48. static void send_consumer(uint16_t data);
  49. static host_driver_t lufa_driver = {
  50. keyboard_leds,
  51. send_keyboard,
  52. send_mouse,
  53. send_system,
  54. send_consumer
  55. };
  56. int main(void)
  57. {
  58. SetupHardware();
  59. sei();
  60. print_enable = true;
  61. debug_enable = true;
  62. debug_matrix = true;
  63. debug_keyboard = true;
  64. debug_mouse = true;
  65. _delay_ms(3000);
  66. print("abcdefg\n");
  67. keyboard_init();
  68. host_set_driver(&lufa_driver);
  69. while (1) {
  70. keyboard_proc();
  71. Keyboard_HID_Task();
  72. Generic_HID_Task();
  73. USB_USBTask();
  74. }
  75. }
  76. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  77. void SetupHardware(void)
  78. {
  79. /* Disable watchdog if enabled by bootloader/fuses */
  80. MCUSR &= ~(1 << WDRF);
  81. wdt_disable();
  82. /* Disable clock division */
  83. clock_prescale_set(clock_div_1);
  84. USB_Init();
  85. }
  86. /** Event handler for the USB_Connect event. */
  87. void EVENT_USB_Device_Connect(void)
  88. {
  89. }
  90. /** Event handler for the USB_Disconnect event. */
  91. void EVENT_USB_Device_Disconnect(void)
  92. {
  93. }
  94. /** Event handler for the USB_ConfigurationChanged event.
  95. * This is fired when the host sets the current configuration of the USB device after enumeration.
  96. */
  97. void EVENT_USB_Device_ConfigurationChanged(void)
  98. {
  99. bool ConfigSuccess = true;
  100. /* Setup Keyboard HID Report Endpoints */
  101. ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  102. HID_EPSIZE, ENDPOINT_BANK_SINGLE);
  103. ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
  104. HID_EPSIZE, ENDPOINT_BANK_SINGLE);
  105. /* Setup Mouse HID Report Endpoint */
  106. ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  107. HID_EPSIZE, ENDPOINT_BANK_SINGLE);
  108. /* Setup Generic HID Report Endpoints */
  109. ConfigSuccess &= Endpoint_ConfigureEndpoint(GENERIC_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  110. GENERIC_EPSIZE, ENDPOINT_BANK_SINGLE);
  111. ConfigSuccess &= Endpoint_ConfigureEndpoint(GENERIC_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
  112. GENERIC_EPSIZE, ENDPOINT_BANK_SINGLE);
  113. }
  114. /** Event handler for the USB_ControlRequest event.
  115. * This is fired before passing along unhandled control requests to the library for processing internally.
  116. */
  117. void EVENT_USB_Device_ControlRequest(void)
  118. {
  119. uint8_t* ReportData = NULL;
  120. uint8_t ReportSize = 0;
  121. /* Handle HID Class specific requests */
  122. switch (USB_ControlRequest.bRequest)
  123. {
  124. case HID_REQ_GetReport:
  125. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  126. {
  127. Endpoint_ClearSETUP();
  128. // Interface
  129. switch (USB_ControlRequest.wIndex) {
  130. case 1: // Keyboard
  131. ReportData = (uint8_t*)&keyboard_report_sent;
  132. ReportSize = sizeof(keyboard_report_sent);
  133. break;
  134. case 2: // Mouse
  135. ReportData = (uint8_t*)&mouse_report_sent;
  136. ReportSize = sizeof(mouse_report_sent);
  137. break;
  138. case 3: // Generic
  139. break;
  140. }
  141. /* Write the report data to the control endpoint */
  142. Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
  143. Endpoint_ClearOUT();
  144. }
  145. break;
  146. case HID_REQ_SetReport:
  147. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  148. {
  149. Endpoint_ClearSETUP();
  150. /* Wait until the LED report has been sent by the host */
  151. while (!(Endpoint_IsOUTReceived()))
  152. {
  153. if (USB_DeviceState == DEVICE_STATE_Unattached)
  154. return;
  155. }
  156. // Interface
  157. switch (USB_ControlRequest.wIndex) {
  158. case 1: // Keyboard
  159. /* Read in the LED report from the host */
  160. keyboard_led_stats = Endpoint_Read_8();
  161. break;
  162. case 2: // Mouse
  163. break;
  164. case 3: // Generic
  165. break;
  166. }
  167. Endpoint_ClearOUT();
  168. Endpoint_ClearStatusStage();
  169. }
  170. break;
  171. }
  172. }
  173. /** Keyboard task.
  174. * This processes host LED status reports sent to the device via the keyboard OUT reporting endpoint.
  175. */
  176. void Keyboard_HID_Task(void)
  177. {
  178. /* Device must be connected and configured for the task to run */
  179. if (USB_DeviceState != DEVICE_STATE_Configured)
  180. return;
  181. /* Select the Keyboard LED Report Endpoint */
  182. Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM);
  183. /* Check if Keyboard LED Endpoint Ready for Read/Write */
  184. if (Endpoint_IsReadWriteAllowed())
  185. {
  186. /* Read in the LED report from the host */
  187. keyboard_led_stats = Endpoint_Read_8();
  188. /* Handshake the OUT Endpoint - clear endpoint and ready for next report */
  189. Endpoint_ClearOUT();
  190. }
  191. }
  192. void Generic_HID_Task(void)
  193. {
  194. /* Device must be connected and configured for the task to run */
  195. if (USB_DeviceState != DEVICE_STATE_Configured)
  196. return;
  197. Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);
  198. /* Check to see if a packet has been sent from the host */
  199. if (Endpoint_IsOUTReceived())
  200. {
  201. /* Check to see if the packet contains data */
  202. if (Endpoint_IsReadWriteAllowed())
  203. {
  204. /* Create a temporary buffer to hold the read in report from the host */
  205. uint8_t GenericData[GENERIC_REPORT_SIZE];
  206. /* Read Generic Report Data */
  207. Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData), NULL);
  208. /* Process Generic Report Data */
  209. //TODO: ProcessGenericHIDReport(GenericData);
  210. }
  211. /* Finalize the stream transfer to send the last packet */
  212. Endpoint_ClearOUT();
  213. }
  214. /* IN packet */
  215. Endpoint_SelectEndpoint(GENERIC_IN_EPNUM);
  216. // send IN packet
  217. if (Endpoint_IsINReady())
  218. Endpoint_ClearIN();
  219. }
  220. /*******************************************************************************
  221. * Host driver
  222. ******************************************************************************/
  223. static uint8_t keyboard_leds(void)
  224. {
  225. return keyboard_led_stats;
  226. }
  227. static void send_keyboard(report_keyboard_t *report)
  228. {
  229. // TODO: handle NKRO report
  230. /* Select the Keyboard Report Endpoint */
  231. Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
  232. /* Check if Keyboard Endpoint Ready for Read/Write */
  233. if (Endpoint_IsReadWriteAllowed())
  234. {
  235. /* Write Keyboard Report Data */
  236. Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL);
  237. /* Finalize the stream transfer to send the last packet */
  238. Endpoint_ClearIN();
  239. }
  240. keyboard_report_sent = *report;
  241. }
  242. static void send_mouse(report_mouse_t *report)
  243. {
  244. /* Select the Mouse Report Endpoint */
  245. Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
  246. /* Check if Mouse Endpoint Ready for Read/Write */
  247. if (Endpoint_IsReadWriteAllowed())
  248. {
  249. /* Write Mouse Report Data */
  250. Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
  251. /* Finalize the stream transfer to send the last packet */
  252. Endpoint_ClearIN();
  253. }
  254. mouse_report_sent = *report;
  255. }
  256. static void send_system(uint16_t data)
  257. {
  258. }
  259. static void send_consumer(uint16_t data)
  260. {
  261. }
  262. /*******************************************************************************
  263. * sendchar
  264. ******************************************************************************/
  265. int8_t sendchar(uint8_t c)
  266. {
  267. if (USB_DeviceState != DEVICE_STATE_Configured)
  268. return -1;
  269. Endpoint_SelectEndpoint(GENERIC_IN_EPNUM);
  270. uint8_t timeout = 10;
  271. uint16_t prevFN = USB_Device_GetFrameNumber();
  272. while (!Endpoint_IsINReady()) {
  273. switch (USB_DeviceState) {
  274. case DEVICE_STATE_Unattached:
  275. case DEVICE_STATE_Suspended:
  276. return -1;
  277. }
  278. if (Endpoint_IsStalled())
  279. return -1;
  280. uint16_t currFN = USB_Device_GetFrameNumber();
  281. if (prevFN != USB_Device_GetFrameNumber()) {
  282. if (!(timeout--))
  283. return -1;
  284. prevFN = USB_Device_GetFrameNumber();
  285. }
  286. }
  287. Endpoint_Write_8(c);
  288. // send when packet is full
  289. if (!Endpoint_IsReadWriteAllowed())
  290. Endpoint_ClearIN();
  291. return 0;
  292. }