Keyboard firmwares for Atmel AVR and Cortex-M
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

lufa.c 13KB

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