Keyboard firmwares for Atmel AVR and Cortex-M
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 "descriptor.h"
  40. #include "lufa.h"
  41. static uint8_t idle_duration = 0;
  42. static uint8_t protocol_report = 1;
  43. static uint8_t keyboard_led_stats = 0;
  44. static report_keyboard_t keyboard_report_sent;
  45. /* Host driver */
  46. static uint8_t keyboard_leds(void);
  47. static void send_keyboard(report_keyboard_t *report);
  48. static void send_mouse(report_mouse_t *report);
  49. static void send_system(uint16_t data);
  50. static void send_consumer(uint16_t data);
  51. host_driver_t lufa_driver = {
  52. keyboard_leds,
  53. send_keyboard,
  54. send_mouse,
  55. send_system,
  56. send_consumer
  57. };
  58. /*******************************************************************************
  59. * Console
  60. ******************************************************************************/
  61. #ifdef CONSOLE_ENABLE
  62. static void Console_Task(void)
  63. {
  64. /* Device must be connected and configured for the task to run */
  65. if (USB_DeviceState != DEVICE_STATE_Configured)
  66. return;
  67. uint8_t ep = Endpoint_GetCurrentEndpoint();
  68. #if 0
  69. // TODO: impl receivechar()/recvchar()
  70. Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
  71. /* Check to see if a packet has been sent from the host */
  72. if (Endpoint_IsOUTReceived())
  73. {
  74. /* Check to see if the packet contains data */
  75. if (Endpoint_IsReadWriteAllowed())
  76. {
  77. /* Create a temporary buffer to hold the read in report from the host */
  78. uint8_t ConsoleData[CONSOLE_EPSIZE];
  79. /* Read Console Report Data */
  80. Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
  81. /* Process Console Report Data */
  82. //ProcessConsoleHIDReport(ConsoleData);
  83. }
  84. /* Finalize the stream transfer to send the last packet */
  85. Endpoint_ClearOUT();
  86. }
  87. #endif
  88. /* IN packet */
  89. Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
  90. if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
  91. Endpoint_SelectEndpoint(ep);
  92. return;
  93. }
  94. // fill empty bank
  95. while (Endpoint_IsReadWriteAllowed())
  96. Endpoint_Write_8(0);
  97. // flash senchar packet
  98. if (Endpoint_IsINReady()) {
  99. Endpoint_ClearIN();
  100. }
  101. Endpoint_SelectEndpoint(ep);
  102. }
  103. #else
  104. static void Console_Task(void)
  105. {
  106. }
  107. #endif
  108. /*******************************************************************************
  109. * USB Events
  110. ******************************************************************************/
  111. /** Event handler for the USB_Connect event. */
  112. void EVENT_USB_Device_Connect(void)
  113. {
  114. }
  115. /** Event handler for the USB_Disconnect event. */
  116. void EVENT_USB_Device_Disconnect(void)
  117. {
  118. }
  119. void EVENT_USB_Device_StartOfFrame(void)
  120. {
  121. Console_Task();
  122. }
  123. /** Event handler for the USB_ConfigurationChanged event.
  124. * This is fired when the host sets the current configuration of the USB device after enumeration.
  125. */
  126. void EVENT_USB_Device_ConfigurationChanged(void)
  127. {
  128. bool ConfigSuccess = true;
  129. /* Setup Keyboard HID Report Endpoints */
  130. ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  131. KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
  132. #ifdef MOUSE_ENABLE
  133. /* Setup Mouse HID Report Endpoint */
  134. ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  135. MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
  136. #endif
  137. #ifdef EXTRAKEY_ENABLE
  138. /* Setup Extra HID Report Endpoint */
  139. ConfigSuccess &= Endpoint_ConfigureEndpoint(EXTRAKEY_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  140. EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
  141. #endif
  142. #ifdef CONSOLE_ENABLE
  143. /* Setup Console HID Report Endpoints */
  144. ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  145. CONSOLE_EPSIZE, ENDPOINT_BANK_DOUBLE);
  146. ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
  147. CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
  148. #endif
  149. }
  150. /*
  151. Appendix G: HID Request Support Requirements
  152. The following table enumerates the requests that need to be supported by various types of HID class devices.
  153. Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
  154. ------------------------------------------------------------------------------------------
  155. Boot Mouse Required Optional Optional Optional Required Required
  156. Non-Boot Mouse Required Optional Optional Optional Optional Optional
  157. Boot Keyboard Required Optional Required Required Required Required
  158. Non-Boot Keybrd Required Optional Required Required Optional Optional
  159. Other Device Required Optional Optional Optional Optional Optional
  160. */
  161. /** Event handler for the USB_ControlRequest event.
  162. * This is fired before passing along unhandled control requests to the library for processing internally.
  163. */
  164. void EVENT_USB_Device_ControlRequest(void)
  165. {
  166. uint8_t* ReportData = NULL;
  167. uint8_t ReportSize = 0;
  168. /* Handle HID Class specific requests */
  169. switch (USB_ControlRequest.bRequest)
  170. {
  171. case HID_REQ_GetReport:
  172. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  173. {
  174. Endpoint_ClearSETUP();
  175. // Interface
  176. switch (USB_ControlRequest.wIndex) {
  177. case KEYBOARD_INTERFACE:
  178. // TODO: test/check
  179. ReportData = (uint8_t*)&keyboard_report_sent;
  180. ReportSize = sizeof(keyboard_report_sent);
  181. break;
  182. }
  183. /* Write the report data to the control endpoint */
  184. Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
  185. Endpoint_ClearOUT();
  186. }
  187. break;
  188. case HID_REQ_SetReport:
  189. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  190. {
  191. // Interface
  192. switch (USB_ControlRequest.wIndex) {
  193. case KEYBOARD_INTERFACE:
  194. Endpoint_ClearSETUP();
  195. while (!(Endpoint_IsOUTReceived())) {
  196. if (USB_DeviceState == DEVICE_STATE_Unattached)
  197. return;
  198. }
  199. keyboard_led_stats = Endpoint_Read_8();
  200. Endpoint_ClearOUT();
  201. Endpoint_ClearStatusStage();
  202. break;
  203. }
  204. }
  205. break;
  206. case HID_REQ_GetProtocol:
  207. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  208. {
  209. Endpoint_ClearSETUP();
  210. while (!(Endpoint_IsINReady()));
  211. Endpoint_Write_8(protocol_report);
  212. Endpoint_ClearIN();
  213. Endpoint_ClearStatusStage();
  214. }
  215. break;
  216. case HID_REQ_SetProtocol:
  217. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  218. {
  219. Endpoint_ClearSETUP();
  220. Endpoint_ClearStatusStage();
  221. protocol_report = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
  222. }
  223. break;
  224. case HID_REQ_SetIdle:
  225. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  226. {
  227. Endpoint_ClearSETUP();
  228. Endpoint_ClearStatusStage();
  229. idle_duration = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
  230. }
  231. break;
  232. case HID_REQ_GetIdle:
  233. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  234. {
  235. Endpoint_ClearSETUP();
  236. while (!(Endpoint_IsINReady()));
  237. Endpoint_Write_8(idle_duration);
  238. Endpoint_ClearIN();
  239. Endpoint_ClearStatusStage();
  240. }
  241. break;
  242. }
  243. }
  244. /*******************************************************************************
  245. * Host driver
  246. ******************************************************************************/
  247. static uint8_t keyboard_leds(void)
  248. {
  249. return keyboard_led_stats;
  250. }
  251. static void send_keyboard(report_keyboard_t *report)
  252. {
  253. uint8_t timeout = 0;
  254. // TODO: handle NKRO report
  255. /* Select the Keyboard Report Endpoint */
  256. Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
  257. /* Check if Keyboard Endpoint Ready for Read/Write */
  258. while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
  259. /* Write Keyboard Report Data */
  260. Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL);
  261. /* Finalize the stream transfer to send the last packet */
  262. Endpoint_ClearIN();
  263. keyboard_report_sent = *report;
  264. }
  265. static void send_mouse(report_mouse_t *report)
  266. {
  267. #ifdef MOUSE_ENABLE
  268. uint8_t timeout = 0;
  269. /* Select the Mouse Report Endpoint */
  270. Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
  271. /* Check if Mouse Endpoint Ready for Read/Write */
  272. while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
  273. /* Write Mouse Report Data */
  274. Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
  275. /* Finalize the stream transfer to send the last packet */
  276. Endpoint_ClearIN();
  277. #endif
  278. }
  279. static void send_system(uint16_t data)
  280. {
  281. uint8_t timeout = 0;
  282. report_extra_t r = {
  283. .report_id = REPORT_ID_SYSTEM,
  284. .usage = data
  285. };
  286. Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
  287. while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
  288. Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
  289. Endpoint_ClearIN();
  290. }
  291. static void send_consumer(uint16_t data)
  292. {
  293. uint8_t timeout = 0;
  294. report_extra_t r = {
  295. .report_id = REPORT_ID_CONSUMER,
  296. .usage = data
  297. };
  298. Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
  299. while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
  300. Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
  301. Endpoint_ClearIN();
  302. }
  303. /*******************************************************************************
  304. * sendchar
  305. ******************************************************************************/
  306. #ifdef CONSOLE_ENABLE
  307. #define SEND_TIMEOUT 5
  308. int8_t sendchar(uint8_t c)
  309. {
  310. // Not wait once timeouted.
  311. // Because sendchar() is called so many times, waiting each call causes big lag.
  312. static bool timeouted = false;
  313. if (USB_DeviceState != DEVICE_STATE_Configured)
  314. return -1;
  315. uint8_t ep = Endpoint_GetCurrentEndpoint();
  316. Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
  317. if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
  318. Endpoint_SelectEndpoint(ep);
  319. return -1;
  320. }
  321. if (timeouted && !Endpoint_IsReadWriteAllowed()) {
  322. Endpoint_SelectEndpoint(ep);
  323. return - 1;
  324. }
  325. timeouted = false;
  326. uint8_t timeout = SEND_TIMEOUT;
  327. uint16_t prevFN = USB_Device_GetFrameNumber();
  328. while (!Endpoint_IsReadWriteAllowed()) {
  329. switch (USB_DeviceState) {
  330. case DEVICE_STATE_Unattached:
  331. case DEVICE_STATE_Suspended:
  332. return -1;
  333. }
  334. if (Endpoint_IsStalled()) {
  335. Endpoint_SelectEndpoint(ep);
  336. return -1;
  337. }
  338. if (prevFN != USB_Device_GetFrameNumber()) {
  339. if (!(timeout--)) {
  340. timeouted = true;
  341. Endpoint_SelectEndpoint(ep);
  342. return -1;
  343. }
  344. prevFN = USB_Device_GetFrameNumber();
  345. }
  346. }
  347. Endpoint_Write_8(c);
  348. // send when bank is full
  349. if (!Endpoint_IsReadWriteAllowed())
  350. Endpoint_ClearIN();
  351. Endpoint_SelectEndpoint(ep);
  352. return 0;
  353. }
  354. #else
  355. int8_t sendchar(uint8_t c)
  356. {
  357. return 0;
  358. }
  359. #endif
  360. /*******************************************************************************
  361. * main
  362. ******************************************************************************/
  363. static void SetupHardware(void)
  364. {
  365. /* Disable watchdog if enabled by bootloader/fuses */
  366. MCUSR &= ~(1 << WDRF);
  367. wdt_disable();
  368. /* Disable clock division */
  369. clock_prescale_set(clock_div_1);
  370. // Leonardo needs. Without this USB device is not recognized.
  371. USB_Disable();
  372. USB_Init();
  373. // for Console_Task
  374. USB_Device_EnableSOFEvents();
  375. }
  376. int main(void) __attribute__ ((weak));
  377. int main(void)
  378. {
  379. SetupHardware();
  380. sei();
  381. print_enable = true;
  382. debug_enable = true;
  383. debug_matrix = true;
  384. debug_keyboard = true;
  385. debug_mouse = true;
  386. // TODO: can't print here
  387. debug("LUFA init\n");
  388. keyboard_init();
  389. host_set_driver(&lufa_driver);
  390. while (1) {
  391. keyboard_proc();
  392. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  393. USB_USBTask();
  394. #endif
  395. }
  396. }