Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
Repozitorijs ir arhivēts. Tam var aplūkot failus un to var klonēt, bet nevar iesūtīt jaunas izmaiņas, kā arī atvērt jaunas problēmas/izmaiņu pieprasījumus.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. #include "led.h"
  112. void EVENT_USB_Device_Connect(void)
  113. {
  114. }
  115. void EVENT_USB_Device_Disconnect(void)
  116. {
  117. }
  118. void EVENT_USB_Device_Reset(void)
  119. {
  120. }
  121. void EVENT_USB_Device_Suspend()
  122. {
  123. led_set(1<<USB_LED_CAPS_LOCK);
  124. }
  125. void EVENT_USB_Device_WakeUp()
  126. {
  127. led_set(0);
  128. }
  129. void EVENT_USB_Device_StartOfFrame(void)
  130. {
  131. Console_Task();
  132. }
  133. /** Event handler for the USB_ConfigurationChanged event.
  134. * This is fired when the host sets the current configuration of the USB device after enumeration.
  135. */
  136. #if LUFA_VERSION_INTEGER < 0x120730
  137. /* old API 120219 */
  138. #define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint(epnum, eptype, epdir, epsize, epbank)
  139. #else
  140. /* new API >= 120730 */
  141. #define ENDPOINT_BANK_SINGLE 1
  142. #define ENDPOINT_BANK_DOUBLE 2
  143. #define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint((epdir) | (epnum) , eptype, epsize, epbank)
  144. #endif
  145. void EVENT_USB_Device_ConfigurationChanged(void)
  146. {
  147. bool ConfigSuccess = true;
  148. /* Setup Keyboard HID Report Endpoints */
  149. ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  150. KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
  151. #ifdef MOUSE_ENABLE
  152. /* Setup Mouse HID Report Endpoint */
  153. ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  154. MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
  155. #endif
  156. #ifdef EXTRAKEY_ENABLE
  157. /* Setup Extra HID Report Endpoint */
  158. ConfigSuccess &= ENDPOINT_CONFIG(EXTRAKEY_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  159. EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
  160. #endif
  161. #ifdef CONSOLE_ENABLE
  162. /* Setup Console HID Report Endpoints */
  163. ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  164. CONSOLE_EPSIZE, ENDPOINT_BANK_DOUBLE);
  165. ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
  166. CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
  167. #endif
  168. }
  169. /*
  170. Appendix G: HID Request Support Requirements
  171. The following table enumerates the requests that need to be supported by various types of HID class devices.
  172. Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
  173. ------------------------------------------------------------------------------------------
  174. Boot Mouse Required Optional Optional Optional Required Required
  175. Non-Boot Mouse Required Optional Optional Optional Optional Optional
  176. Boot Keyboard Required Optional Required Required Required Required
  177. Non-Boot Keybrd Required Optional Required Required Optional Optional
  178. Other Device Required Optional Optional Optional Optional Optional
  179. */
  180. /** Event handler for the USB_ControlRequest event.
  181. * This is fired before passing along unhandled control requests to the library for processing internally.
  182. */
  183. void EVENT_USB_Device_ControlRequest(void)
  184. {
  185. uint8_t* ReportData = NULL;
  186. uint8_t ReportSize = 0;
  187. /* Handle HID Class specific requests */
  188. switch (USB_ControlRequest.bRequest)
  189. {
  190. case HID_REQ_GetReport:
  191. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  192. {
  193. Endpoint_ClearSETUP();
  194. // Interface
  195. switch (USB_ControlRequest.wIndex) {
  196. case KEYBOARD_INTERFACE:
  197. // TODO: test/check
  198. ReportData = (uint8_t*)&keyboard_report_sent;
  199. ReportSize = sizeof(keyboard_report_sent);
  200. break;
  201. }
  202. /* Write the report data to the control endpoint */
  203. Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
  204. Endpoint_ClearOUT();
  205. }
  206. break;
  207. case HID_REQ_SetReport:
  208. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  209. {
  210. // Interface
  211. switch (USB_ControlRequest.wIndex) {
  212. case KEYBOARD_INTERFACE:
  213. Endpoint_ClearSETUP();
  214. while (!(Endpoint_IsOUTReceived())) {
  215. if (USB_DeviceState == DEVICE_STATE_Unattached)
  216. return;
  217. }
  218. keyboard_led_stats = Endpoint_Read_8();
  219. Endpoint_ClearOUT();
  220. Endpoint_ClearStatusStage();
  221. break;
  222. }
  223. }
  224. break;
  225. case HID_REQ_GetProtocol:
  226. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  227. {
  228. Endpoint_ClearSETUP();
  229. while (!(Endpoint_IsINReady()));
  230. Endpoint_Write_8(protocol_report);
  231. Endpoint_ClearIN();
  232. Endpoint_ClearStatusStage();
  233. }
  234. break;
  235. case HID_REQ_SetProtocol:
  236. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  237. {
  238. Endpoint_ClearSETUP();
  239. Endpoint_ClearStatusStage();
  240. protocol_report = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
  241. }
  242. break;
  243. case HID_REQ_SetIdle:
  244. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  245. {
  246. Endpoint_ClearSETUP();
  247. Endpoint_ClearStatusStage();
  248. idle_duration = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
  249. }
  250. break;
  251. case HID_REQ_GetIdle:
  252. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  253. {
  254. Endpoint_ClearSETUP();
  255. while (!(Endpoint_IsINReady()));
  256. Endpoint_Write_8(idle_duration);
  257. Endpoint_ClearIN();
  258. Endpoint_ClearStatusStage();
  259. }
  260. break;
  261. }
  262. }
  263. /*******************************************************************************
  264. * Host driver
  265. ******************************************************************************/
  266. static uint8_t keyboard_leds(void)
  267. {
  268. return keyboard_led_stats;
  269. }
  270. static void send_keyboard(report_keyboard_t *report)
  271. {
  272. uint8_t timeout = 0;
  273. // TODO: handle NKRO report
  274. /* Select the Keyboard Report Endpoint */
  275. Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
  276. /* Check if Keyboard Endpoint Ready for Read/Write */
  277. while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
  278. /* Write Keyboard Report Data */
  279. Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL);
  280. /* Finalize the stream transfer to send the last packet */
  281. Endpoint_ClearIN();
  282. keyboard_report_sent = *report;
  283. }
  284. static void send_mouse(report_mouse_t *report)
  285. {
  286. #ifdef MOUSE_ENABLE
  287. uint8_t timeout = 0;
  288. /* Select the Mouse Report Endpoint */
  289. Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
  290. /* Check if Mouse Endpoint Ready for Read/Write */
  291. while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
  292. /* Write Mouse Report Data */
  293. Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
  294. /* Finalize the stream transfer to send the last packet */
  295. Endpoint_ClearIN();
  296. #endif
  297. }
  298. static void send_system(uint16_t data)
  299. {
  300. uint8_t timeout = 0;
  301. report_extra_t r = {
  302. .report_id = REPORT_ID_SYSTEM,
  303. .usage = data
  304. };
  305. Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
  306. while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
  307. Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
  308. Endpoint_ClearIN();
  309. }
  310. static void send_consumer(uint16_t data)
  311. {
  312. uint8_t timeout = 0;
  313. report_extra_t r = {
  314. .report_id = REPORT_ID_CONSUMER,
  315. .usage = data
  316. };
  317. Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
  318. while (--timeout && !Endpoint_IsReadWriteAllowed()) ;
  319. Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
  320. Endpoint_ClearIN();
  321. }
  322. /*******************************************************************************
  323. * sendchar
  324. ******************************************************************************/
  325. #ifdef CONSOLE_ENABLE
  326. #define SEND_TIMEOUT 5
  327. int8_t sendchar(uint8_t c)
  328. {
  329. // Not wait once timeouted.
  330. // Because sendchar() is called so many times, waiting each call causes big lag.
  331. static bool timeouted = false;
  332. if (USB_DeviceState != DEVICE_STATE_Configured)
  333. return -1;
  334. uint8_t ep = Endpoint_GetCurrentEndpoint();
  335. Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
  336. if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
  337. Endpoint_SelectEndpoint(ep);
  338. return -1;
  339. }
  340. if (timeouted && !Endpoint_IsReadWriteAllowed()) {
  341. Endpoint_SelectEndpoint(ep);
  342. return - 1;
  343. }
  344. timeouted = false;
  345. uint8_t timeout = SEND_TIMEOUT;
  346. uint16_t prevFN = USB_Device_GetFrameNumber();
  347. while (!Endpoint_IsReadWriteAllowed()) {
  348. switch (USB_DeviceState) {
  349. case DEVICE_STATE_Unattached:
  350. case DEVICE_STATE_Suspended:
  351. return -1;
  352. }
  353. if (Endpoint_IsStalled()) {
  354. Endpoint_SelectEndpoint(ep);
  355. return -1;
  356. }
  357. if (prevFN != USB_Device_GetFrameNumber()) {
  358. if (!(timeout--)) {
  359. timeouted = true;
  360. Endpoint_SelectEndpoint(ep);
  361. return -1;
  362. }
  363. prevFN = USB_Device_GetFrameNumber();
  364. }
  365. }
  366. Endpoint_Write_8(c);
  367. // send when bank is full
  368. if (!Endpoint_IsReadWriteAllowed())
  369. Endpoint_ClearIN();
  370. Endpoint_SelectEndpoint(ep);
  371. return 0;
  372. }
  373. #else
  374. int8_t sendchar(uint8_t c)
  375. {
  376. return 0;
  377. }
  378. #endif
  379. /*******************************************************************************
  380. * main
  381. ******************************************************************************/
  382. static void SetupHardware(void)
  383. {
  384. /* Disable watchdog if enabled by bootloader/fuses */
  385. MCUSR &= ~(1 << WDRF);
  386. wdt_disable();
  387. /* Disable clock division */
  388. clock_prescale_set(clock_div_1);
  389. // Leonardo needs. Without this USB device is not recognized.
  390. USB_Disable();
  391. USB_Init();
  392. // for Console_Task
  393. USB_Device_EnableSOFEvents();
  394. }
  395. #include "matrix.h"
  396. static bool wakeup_condition(void)
  397. {
  398. matrix_scan();
  399. for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
  400. if (matrix_get_row(r)) return true;
  401. }
  402. return false;
  403. }
  404. int main(void) __attribute__ ((weak));
  405. int main(void)
  406. {
  407. SetupHardware();
  408. keyboard_init();
  409. host_set_driver(&lufa_driver);
  410. sei();
  411. while (1) {
  412. while (USB_DeviceState == DEVICE_STATE_Suspended) {
  413. // TODO: power saving
  414. if (USB_Device_RemoteWakeupEnabled) {
  415. if (wakeup_condition()) {
  416. USB_Device_SendRemoteWakeup();
  417. }
  418. }
  419. }
  420. keyboard_task();
  421. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  422. USB_USBTask();
  423. #endif
  424. }
  425. }