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.

1107 lines
29 KiB

Merge ChibiOS and LUFA descriptor support (#2362) * Move lufa descriptor to protocol/usb_descriptor * Try to compile usb_descriptor on ChibiOS * Add lufa_utils for ChibiOS Lufa USB descriptors for ChibiOS * More lufa_util compatibility fixes * First compiling version of shared USB descriptor * Send the usb descriptors * Fix the CONSOLE output on ChibiOS * Add errors for unsupported interfaces * Enable support for vitual serial port USB descriptors * Implement virtual serial port for ChibiOS * Cleanup the lufa_utils Use the default lufa header files * Add raw hid support for ChibiOS This is completely untested * Enable midi compilation on ChibiOS * Move midi functionality out of lufa.c * Don't register sysex callback when not needed * ChibiOS compilation fixes * Update ChibiOS submodule * Fix the Midi USB descriptor It didn't work properly when both Midi and Virtual serial port was enabled. * Add MIDI support for ChibiOS * Fix USB descriptor strings on ChibiOS * Use serial usb driver for raw hid * Generalize the ChibiOS stream like drivers This makes the initialization much more simple and eliminates a lot of the code duplication. * Convert console output to chibios stream driver * Fixes for ChibiOS update * Update the ChibiOS contrib submodule To include the usb data toggle synchronization fixes * Fix duplicate reset enumeration on ChibiOS * Add missing include * Add number of endpoints check for ChibiOS * Enable serial USB driver on all keyboards * Add missing includes when API is enabled withot midi * Add another missing inlcude
6 years ago
7 years ago
7 years ago
Merge ChibiOS and LUFA descriptor support (#2362) * Move lufa descriptor to protocol/usb_descriptor * Try to compile usb_descriptor on ChibiOS * Add lufa_utils for ChibiOS Lufa USB descriptors for ChibiOS * More lufa_util compatibility fixes * First compiling version of shared USB descriptor * Send the usb descriptors * Fix the CONSOLE output on ChibiOS * Add errors for unsupported interfaces * Enable support for vitual serial port USB descriptors * Implement virtual serial port for ChibiOS * Cleanup the lufa_utils Use the default lufa header files * Add raw hid support for ChibiOS This is completely untested * Enable midi compilation on ChibiOS * Move midi functionality out of lufa.c * Don't register sysex callback when not needed * ChibiOS compilation fixes * Update ChibiOS submodule * Fix the Midi USB descriptor It didn't work properly when both Midi and Virtual serial port was enabled. * Add MIDI support for ChibiOS * Fix USB descriptor strings on ChibiOS * Use serial usb driver for raw hid * Generalize the ChibiOS stream like drivers This makes the initialization much more simple and eliminates a lot of the code duplication. * Convert console output to chibios stream driver * Fixes for ChibiOS update * Update the ChibiOS contrib submodule To include the usb data toggle synchronization fixes * Fix duplicate reset enumeration on ChibiOS * Add missing include * Add number of endpoints check for ChibiOS * Enable serial USB driver on all keyboards * Add missing includes when API is enabled withot midi * Add another missing inlcude
6 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
Merge ChibiOS and LUFA descriptor support (#2362) * Move lufa descriptor to protocol/usb_descriptor * Try to compile usb_descriptor on ChibiOS * Add lufa_utils for ChibiOS Lufa USB descriptors for ChibiOS * More lufa_util compatibility fixes * First compiling version of shared USB descriptor * Send the usb descriptors * Fix the CONSOLE output on ChibiOS * Add errors for unsupported interfaces * Enable support for vitual serial port USB descriptors * Implement virtual serial port for ChibiOS * Cleanup the lufa_utils Use the default lufa header files * Add raw hid support for ChibiOS This is completely untested * Enable midi compilation on ChibiOS * Move midi functionality out of lufa.c * Don't register sysex callback when not needed * ChibiOS compilation fixes * Update ChibiOS submodule * Fix the Midi USB descriptor It didn't work properly when both Midi and Virtual serial port was enabled. * Add MIDI support for ChibiOS * Fix USB descriptor strings on ChibiOS * Use serial usb driver for raw hid * Generalize the ChibiOS stream like drivers This makes the initialization much more simple and eliminates a lot of the code duplication. * Convert console output to chibios stream driver * Fixes for ChibiOS update * Update the ChibiOS contrib submodule To include the usb data toggle synchronization fixes * Fix duplicate reset enumeration on ChibiOS * Add missing include * Add number of endpoints check for ChibiOS * Enable serial USB driver on all keyboards * Add missing includes when API is enabled withot midi * Add another missing inlcude
6 years ago
8 years ago
Merge ChibiOS and LUFA descriptor support (#2362) * Move lufa descriptor to protocol/usb_descriptor * Try to compile usb_descriptor on ChibiOS * Add lufa_utils for ChibiOS Lufa USB descriptors for ChibiOS * More lufa_util compatibility fixes * First compiling version of shared USB descriptor * Send the usb descriptors * Fix the CONSOLE output on ChibiOS * Add errors for unsupported interfaces * Enable support for vitual serial port USB descriptors * Implement virtual serial port for ChibiOS * Cleanup the lufa_utils Use the default lufa header files * Add raw hid support for ChibiOS This is completely untested * Enable midi compilation on ChibiOS * Move midi functionality out of lufa.c * Don't register sysex callback when not needed * ChibiOS compilation fixes * Update ChibiOS submodule * Fix the Midi USB descriptor It didn't work properly when both Midi and Virtual serial port was enabled. * Add MIDI support for ChibiOS * Fix USB descriptor strings on ChibiOS * Use serial usb driver for raw hid * Generalize the ChibiOS stream like drivers This makes the initialization much more simple and eliminates a lot of the code duplication. * Convert console output to chibios stream driver * Fixes for ChibiOS update * Update the ChibiOS contrib submodule To include the usb data toggle synchronization fixes * Fix duplicate reset enumeration on ChibiOS * Add missing include * Add number of endpoints check for ChibiOS * Enable serial USB driver on all keyboards * Add missing includes when API is enabled withot midi * Add another missing inlcude
6 years ago
8 years ago
Merge ChibiOS and LUFA descriptor support (#2362) * Move lufa descriptor to protocol/usb_descriptor * Try to compile usb_descriptor on ChibiOS * Add lufa_utils for ChibiOS Lufa USB descriptors for ChibiOS * More lufa_util compatibility fixes * First compiling version of shared USB descriptor * Send the usb descriptors * Fix the CONSOLE output on ChibiOS * Add errors for unsupported interfaces * Enable support for vitual serial port USB descriptors * Implement virtual serial port for ChibiOS * Cleanup the lufa_utils Use the default lufa header files * Add raw hid support for ChibiOS This is completely untested * Enable midi compilation on ChibiOS * Move midi functionality out of lufa.c * Don't register sysex callback when not needed * ChibiOS compilation fixes * Update ChibiOS submodule * Fix the Midi USB descriptor It didn't work properly when both Midi and Virtual serial port was enabled. * Add MIDI support for ChibiOS * Fix USB descriptor strings on ChibiOS * Use serial usb driver for raw hid * Generalize the ChibiOS stream like drivers This makes the initialization much more simple and eliminates a lot of the code duplication. * Convert console output to chibios stream driver * Fixes for ChibiOS update * Update the ChibiOS contrib submodule To include the usb data toggle synchronization fixes * Fix duplicate reset enumeration on ChibiOS * Add missing include * Add number of endpoints check for ChibiOS * Enable serial USB driver on all keyboards * Add missing includes when API is enabled withot midi * Add another missing inlcude
6 years ago
8 years ago
Merge ChibiOS and LUFA descriptor support (#2362) * Move lufa descriptor to protocol/usb_descriptor * Try to compile usb_descriptor on ChibiOS * Add lufa_utils for ChibiOS Lufa USB descriptors for ChibiOS * More lufa_util compatibility fixes * First compiling version of shared USB descriptor * Send the usb descriptors * Fix the CONSOLE output on ChibiOS * Add errors for unsupported interfaces * Enable support for vitual serial port USB descriptors * Implement virtual serial port for ChibiOS * Cleanup the lufa_utils Use the default lufa header files * Add raw hid support for ChibiOS This is completely untested * Enable midi compilation on ChibiOS * Move midi functionality out of lufa.c * Don't register sysex callback when not needed * ChibiOS compilation fixes * Update ChibiOS submodule * Fix the Midi USB descriptor It didn't work properly when both Midi and Virtual serial port was enabled. * Add MIDI support for ChibiOS * Fix USB descriptor strings on ChibiOS * Use serial usb driver for raw hid * Generalize the ChibiOS stream like drivers This makes the initialization much more simple and eliminates a lot of the code duplication. * Convert console output to chibios stream driver * Fixes for ChibiOS update * Update the ChibiOS contrib submodule To include the usb data toggle synchronization fixes * Fix duplicate reset enumeration on ChibiOS * Add missing include * Add number of endpoints check for ChibiOS * Enable serial USB driver on all keyboards * Add missing includes when API is enabled withot midi * Add another missing inlcude
6 years ago
8 years ago
Merge ChibiOS and LUFA descriptor support (#2362) * Move lufa descriptor to protocol/usb_descriptor * Try to compile usb_descriptor on ChibiOS * Add lufa_utils for ChibiOS Lufa USB descriptors for ChibiOS * More lufa_util compatibility fixes * First compiling version of shared USB descriptor * Send the usb descriptors * Fix the CONSOLE output on ChibiOS * Add errors for unsupported interfaces * Enable support for vitual serial port USB descriptors * Implement virtual serial port for ChibiOS * Cleanup the lufa_utils Use the default lufa header files * Add raw hid support for ChibiOS This is completely untested * Enable midi compilation on ChibiOS * Move midi functionality out of lufa.c * Don't register sysex callback when not needed * ChibiOS compilation fixes * Update ChibiOS submodule * Fix the Midi USB descriptor It didn't work properly when both Midi and Virtual serial port was enabled. * Add MIDI support for ChibiOS * Fix USB descriptor strings on ChibiOS * Use serial usb driver for raw hid * Generalize the ChibiOS stream like drivers This makes the initialization much more simple and eliminates a lot of the code duplication. * Convert console output to chibios stream driver * Fixes for ChibiOS update * Update the ChibiOS contrib submodule To include the usb data toggle synchronization fixes * Fix duplicate reset enumeration on ChibiOS * Add missing include * Add number of endpoints check for ChibiOS * Enable serial USB driver on all keyboards * Add missing includes when API is enabled withot midi * Add another missing inlcude
6 years ago
8 years ago
Merge ChibiOS and LUFA descriptor support (#2362) * Move lufa descriptor to protocol/usb_descriptor * Try to compile usb_descriptor on ChibiOS * Add lufa_utils for ChibiOS Lufa USB descriptors for ChibiOS * More lufa_util compatibility fixes * First compiling version of shared USB descriptor * Send the usb descriptors * Fix the CONSOLE output on ChibiOS * Add errors for unsupported interfaces * Enable support for vitual serial port USB descriptors * Implement virtual serial port for ChibiOS * Cleanup the lufa_utils Use the default lufa header files * Add raw hid support for ChibiOS This is completely untested * Enable midi compilation on ChibiOS * Move midi functionality out of lufa.c * Don't register sysex callback when not needed * ChibiOS compilation fixes * Update ChibiOS submodule * Fix the Midi USB descriptor It didn't work properly when both Midi and Virtual serial port was enabled. * Add MIDI support for ChibiOS * Fix USB descriptor strings on ChibiOS * Use serial usb driver for raw hid * Generalize the ChibiOS stream like drivers This makes the initialization much more simple and eliminates a lot of the code duplication. * Convert console output to chibios stream driver * Fixes for ChibiOS update * Update the ChibiOS contrib submodule To include the usb data toggle synchronization fixes * Fix duplicate reset enumeration on ChibiOS * Add missing include * Add number of endpoints check for ChibiOS * Enable serial USB driver on all keyboards * Add missing includes when API is enabled withot midi * Add another missing inlcude
6 years ago
8 years ago
8 years ago
8 years ago
8 years ago
Makefile redo & other features (#395) * .build containment implemented * no destructive variable setting - builds in either folder * make from 3 places * cleans before each build * make from root with keyboard=keyboard, keymap=keymap * make from keyboard/keyboard with keymap=keymap * make from keymaps/keymap * only implemented on planck * adds color diag to avr-gcc * makefiles for all plancks, clean-up * quick build-all makefile for plancks * reformatting of make output (colors) * color toggle, tmk path corrections * correct if statement for color * move config.h to main makefile, updates preonic, atomic * format update, all keyboards targets * makefile optional for build all target, alps and arrow_pad updated * alps updated * make planck default, trying out travis recipe for all-keyboards * all-keymaps target, different travis recipe * updates alps64 * updates keyboards to new format * updates clue* projects * all projects updated, specialise EZ .hex, let .hex through * updates travis * automatically find root, keyboard, keymap * silent echo, cleaned-up mass make output * updates all keyboards' .hex files except EZ * Rename Bantam44.c to bantam44.c * Rename Bantam44.h to bantam44.h * nananana * adds six key keyboard * does same to ez as rest * updates send_string example * brings ergodox_ez up to date * updates template/new project script * adds sixkeyboard * adds readme for sixkeyboard * adds sixkeyboard to travis * filenames, gitignore mess * define clock prescaler stuff manually * make quick, size test example * documentation and dfu-no-build
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
Merge ChibiOS and LUFA descriptor support (#2362) * Move lufa descriptor to protocol/usb_descriptor * Try to compile usb_descriptor on ChibiOS * Add lufa_utils for ChibiOS Lufa USB descriptors for ChibiOS * More lufa_util compatibility fixes * First compiling version of shared USB descriptor * Send the usb descriptors * Fix the CONSOLE output on ChibiOS * Add errors for unsupported interfaces * Enable support for vitual serial port USB descriptors * Implement virtual serial port for ChibiOS * Cleanup the lufa_utils Use the default lufa header files * Add raw hid support for ChibiOS This is completely untested * Enable midi compilation on ChibiOS * Move midi functionality out of lufa.c * Don't register sysex callback when not needed * ChibiOS compilation fixes * Update ChibiOS submodule * Fix the Midi USB descriptor It didn't work properly when both Midi and Virtual serial port was enabled. * Add MIDI support for ChibiOS * Fix USB descriptor strings on ChibiOS * Use serial usb driver for raw hid * Generalize the ChibiOS stream like drivers This makes the initialization much more simple and eliminates a lot of the code duplication. * Convert console output to chibios stream driver * Fixes for ChibiOS update * Update the ChibiOS contrib submodule To include the usb data toggle synchronization fixes * Fix duplicate reset enumeration on ChibiOS * Add missing include * Add number of endpoints check for ChibiOS * Enable serial USB driver on all keyboards * Add missing includes when API is enabled withot midi * Add another missing inlcude
6 years ago
8 years ago
7 years ago
7 years ago
8 years ago
Merge ChibiOS and LUFA descriptor support (#2362) * Move lufa descriptor to protocol/usb_descriptor * Try to compile usb_descriptor on ChibiOS * Add lufa_utils for ChibiOS Lufa USB descriptors for ChibiOS * More lufa_util compatibility fixes * First compiling version of shared USB descriptor * Send the usb descriptors * Fix the CONSOLE output on ChibiOS * Add errors for unsupported interfaces * Enable support for vitual serial port USB descriptors * Implement virtual serial port for ChibiOS * Cleanup the lufa_utils Use the default lufa header files * Add raw hid support for ChibiOS This is completely untested * Enable midi compilation on ChibiOS * Move midi functionality out of lufa.c * Don't register sysex callback when not needed * ChibiOS compilation fixes * Update ChibiOS submodule * Fix the Midi USB descriptor It didn't work properly when both Midi and Virtual serial port was enabled. * Add MIDI support for ChibiOS * Fix USB descriptor strings on ChibiOS * Use serial usb driver for raw hid * Generalize the ChibiOS stream like drivers This makes the initialization much more simple and eliminates a lot of the code duplication. * Convert console output to chibios stream driver * Fixes for ChibiOS update * Update the ChibiOS contrib submodule To include the usb data toggle synchronization fixes * Fix duplicate reset enumeration on ChibiOS * Add missing include * Add number of endpoints check for ChibiOS * Enable serial USB driver on all keyboards * Add missing includes when API is enabled withot midi * Add another missing inlcude
6 years ago
  1. /*
  2. * Copyright 2012 Jun Wako <wakojun@gmail.com>
  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 "action.h"
  38. #include "led.h"
  39. #include "sendchar.h"
  40. #include "debug.h"
  41. #ifdef SLEEP_LED_ENABLE
  42. #include "sleep_led.h"
  43. #endif
  44. #include "suspend.h"
  45. #include "usb_descriptor.h"
  46. #include "lufa.h"
  47. #include "quantum.h"
  48. #include <util/atomic.h>
  49. #include "outputselect.h"
  50. #ifdef NKRO_ENABLE
  51. #include "keycode_config.h"
  52. extern keymap_config_t keymap_config;
  53. #endif
  54. #ifdef AUDIO_ENABLE
  55. #include <audio.h>
  56. #endif
  57. #ifdef BLUETOOTH_ENABLE
  58. #ifdef MODULE_ADAFRUIT_BLE
  59. #include "adafruit_ble.h"
  60. #else
  61. #include "bluetooth.h"
  62. #endif
  63. #endif
  64. #ifdef VIRTSER_ENABLE
  65. #include "virtser.h"
  66. #endif
  67. #if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE)
  68. #include "rgblight.h"
  69. #endif
  70. #ifdef MIDI_ENABLE
  71. #include "qmk_midi.h"
  72. #endif
  73. #ifdef RAW_ENABLE
  74. #include "raw_hid.h"
  75. #endif
  76. uint8_t keyboard_idle = 0;
  77. /* 0: Boot Protocol, 1: Report Protocol(default) */
  78. uint8_t keyboard_protocol = 1;
  79. static uint8_t keyboard_led_stats = 0;
  80. static report_keyboard_t keyboard_report_sent;
  81. /* Host driver */
  82. static uint8_t keyboard_leds(void);
  83. static void send_keyboard(report_keyboard_t *report);
  84. static void send_mouse(report_mouse_t *report);
  85. static void send_system(uint16_t data);
  86. static void send_consumer(uint16_t data);
  87. host_driver_t lufa_driver = {
  88. keyboard_leds,
  89. send_keyboard,
  90. send_mouse,
  91. send_system,
  92. send_consumer,
  93. };
  94. #ifdef VIRTSER_ENABLE
  95. USB_ClassInfo_CDC_Device_t cdc_device =
  96. {
  97. .Config =
  98. {
  99. .ControlInterfaceNumber = CCI_INTERFACE,
  100. .DataINEndpoint =
  101. {
  102. .Address = CDC_IN_EPADDR,
  103. .Size = CDC_EPSIZE,
  104. .Banks = 1,
  105. },
  106. .DataOUTEndpoint =
  107. {
  108. .Address = CDC_OUT_EPADDR,
  109. .Size = CDC_EPSIZE,
  110. .Banks = 1,
  111. },
  112. .NotificationEndpoint =
  113. {
  114. .Address = CDC_NOTIFICATION_EPADDR,
  115. .Size = CDC_NOTIFICATION_EPSIZE,
  116. .Banks = 1,
  117. },
  118. },
  119. };
  120. #endif
  121. #ifdef RAW_ENABLE
  122. /** \brief Raw HID Send
  123. *
  124. * FIXME: Needs doc
  125. */
  126. void raw_hid_send( uint8_t *data, uint8_t length )
  127. {
  128. // TODO: implement variable size packet
  129. if ( length != RAW_EPSIZE )
  130. {
  131. return;
  132. }
  133. if (USB_DeviceState != DEVICE_STATE_Configured)
  134. {
  135. return;
  136. }
  137. // TODO: decide if we allow calls to raw_hid_send() in the middle
  138. // of other endpoint usage.
  139. uint8_t ep = Endpoint_GetCurrentEndpoint();
  140. Endpoint_SelectEndpoint(RAW_IN_EPNUM);
  141. // Check to see if the host is ready to accept another packet
  142. if (Endpoint_IsINReady())
  143. {
  144. // Write data
  145. Endpoint_Write_Stream_LE(data, RAW_EPSIZE, NULL);
  146. // Finalize the stream transfer to send the last packet
  147. Endpoint_ClearIN();
  148. }
  149. Endpoint_SelectEndpoint(ep);
  150. }
  151. /** \brief Raw HID Receive
  152. *
  153. * FIXME: Needs doc
  154. */
  155. __attribute__ ((weak))
  156. void raw_hid_receive( uint8_t *data, uint8_t length )
  157. {
  158. // Users should #include "raw_hid.h" in their own code
  159. // and implement this function there. Leave this as weak linkage
  160. // so users can opt to not handle data coming in.
  161. }
  162. /** \brief Raw HID Task
  163. *
  164. * FIXME: Needs doc
  165. */
  166. static void raw_hid_task(void)
  167. {
  168. // Create a temporary buffer to hold the read in data from the host
  169. uint8_t data[RAW_EPSIZE];
  170. bool data_read = false;
  171. // Device must be connected and configured for the task to run
  172. if (USB_DeviceState != DEVICE_STATE_Configured)
  173. return;
  174. Endpoint_SelectEndpoint(RAW_OUT_EPNUM);
  175. // Check to see if a packet has been sent from the host
  176. if (Endpoint_IsOUTReceived())
  177. {
  178. // Check to see if the packet contains data
  179. if (Endpoint_IsReadWriteAllowed())
  180. {
  181. /* Read data */
  182. Endpoint_Read_Stream_LE(data, sizeof(data), NULL);
  183. data_read = true;
  184. }
  185. // Finalize the stream transfer to receive the last packet
  186. Endpoint_ClearOUT();
  187. if ( data_read )
  188. {
  189. raw_hid_receive( data, sizeof(data) );
  190. }
  191. }
  192. }
  193. #endif
  194. /*******************************************************************************
  195. * Console
  196. ******************************************************************************/
  197. #ifdef CONSOLE_ENABLE
  198. /** \brief Console Task
  199. *
  200. * FIXME: Needs doc
  201. */
  202. static void Console_Task(void)
  203. {
  204. /* Device must be connected and configured for the task to run */
  205. if (USB_DeviceState != DEVICE_STATE_Configured)
  206. return;
  207. uint8_t ep = Endpoint_GetCurrentEndpoint();
  208. #if 0
  209. // TODO: impl receivechar()/recvchar()
  210. Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
  211. /* Check to see if a packet has been sent from the host */
  212. if (Endpoint_IsOUTReceived())
  213. {
  214. /* Check to see if the packet contains data */
  215. if (Endpoint_IsReadWriteAllowed())
  216. {
  217. /* Create a temporary buffer to hold the read in report from the host */
  218. uint8_t ConsoleData[CONSOLE_EPSIZE];
  219. /* Read Console Report Data */
  220. Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
  221. /* Process Console Report Data */
  222. //ProcessConsoleHIDReport(ConsoleData);
  223. }
  224. /* Finalize the stream transfer to send the last packet */
  225. Endpoint_ClearOUT();
  226. }
  227. #endif
  228. /* IN packet */
  229. Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
  230. if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
  231. Endpoint_SelectEndpoint(ep);
  232. return;
  233. }
  234. // fill empty bank
  235. while (Endpoint_IsReadWriteAllowed())
  236. Endpoint_Write_8(0);
  237. // flash senchar packet
  238. if (Endpoint_IsINReady()) {
  239. Endpoint_ClearIN();
  240. }
  241. Endpoint_SelectEndpoint(ep);
  242. }
  243. #endif
  244. /*******************************************************************************
  245. * USB Events
  246. ******************************************************************************/
  247. /*
  248. * Event Order of Plug in:
  249. * 0) EVENT_USB_Device_Connect
  250. * 1) EVENT_USB_Device_Suspend
  251. * 2) EVENT_USB_Device_Reset
  252. * 3) EVENT_USB_Device_Wake
  253. */
  254. /** \brief Event USB Device Connect
  255. *
  256. * FIXME: Needs doc
  257. */
  258. void EVENT_USB_Device_Connect(void)
  259. {
  260. print("[C]");
  261. /* For battery powered device */
  262. if (!USB_IsInitialized) {
  263. USB_Disable();
  264. USB_Init();
  265. USB_Device_EnableSOFEvents();
  266. }
  267. }
  268. /** \brief Event USB Device Connect
  269. *
  270. * FIXME: Needs doc
  271. */
  272. void EVENT_USB_Device_Disconnect(void)
  273. {
  274. print("[D]");
  275. /* For battery powered device */
  276. USB_IsInitialized = false;
  277. /* TODO: This doesn't work. After several plug in/outs can not be enumerated.
  278. if (USB_IsInitialized) {
  279. USB_Disable(); // Disable all interrupts
  280. USB_Controller_Enable();
  281. USB_INT_Enable(USB_INT_VBUSTI);
  282. }
  283. */
  284. }
  285. /** \brief Event USB Device Connect
  286. *
  287. * FIXME: Needs doc
  288. */
  289. void EVENT_USB_Device_Reset(void)
  290. {
  291. print("[R]");
  292. }
  293. /** \brief Event USB Device Connect
  294. *
  295. * FIXME: Needs doc
  296. */
  297. void EVENT_USB_Device_Suspend()
  298. {
  299. print("[S]");
  300. #ifdef SLEEP_LED_ENABLE
  301. sleep_led_enable();
  302. #endif
  303. }
  304. /** \brief Event USB Device Connect
  305. *
  306. * FIXME: Needs doc
  307. */
  308. void EVENT_USB_Device_WakeUp()
  309. {
  310. print("[W]");
  311. suspend_wakeup_init();
  312. #ifdef SLEEP_LED_ENABLE
  313. sleep_led_disable();
  314. // NOTE: converters may not accept this
  315. led_set(host_keyboard_leds());
  316. #endif
  317. }
  318. #ifdef CONSOLE_ENABLE
  319. static bool console_flush = false;
  320. #define CONSOLE_FLUSH_SET(b) do { \
  321. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {\
  322. console_flush = b; \
  323. } \
  324. } while (0)
  325. /** \brief Event USB Device Start Of Frame
  326. *
  327. * FIXME: Needs doc
  328. * called every 1ms
  329. */
  330. void EVENT_USB_Device_StartOfFrame(void)
  331. {
  332. static uint8_t count;
  333. if (++count % 50) return;
  334. count = 0;
  335. if (!console_flush) return;
  336. Console_Task();
  337. console_flush = false;
  338. }
  339. #endif
  340. /** \brief Event handler for the USB_ConfigurationChanged event.
  341. *
  342. * This is fired when the host sets the current configuration of the USB device after enumeration.
  343. *
  344. * ATMega32u2 supports dual bank(ping-pong mode) only on endpoint 3 and 4,
  345. * it is safe to use single bank for all endpoints.
  346. */
  347. void EVENT_USB_Device_ConfigurationChanged(void)
  348. {
  349. bool ConfigSuccess = true;
  350. /* Setup Keyboard HID Report Endpoints */
  351. ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  352. KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
  353. #ifdef MOUSE_ENABLE
  354. /* Setup Mouse HID Report Endpoint */
  355. ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  356. MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
  357. #endif
  358. #ifdef EXTRAKEY_ENABLE
  359. /* Setup Extra HID Report Endpoint */
  360. ConfigSuccess &= ENDPOINT_CONFIG(EXTRAKEY_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  361. EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
  362. #endif
  363. #ifdef RAW_ENABLE
  364. /* Setup Raw HID Report Endpoints */
  365. ConfigSuccess &= ENDPOINT_CONFIG(RAW_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  366. RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
  367. ConfigSuccess &= ENDPOINT_CONFIG(RAW_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
  368. RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
  369. #endif
  370. #ifdef CONSOLE_ENABLE
  371. /* Setup Console HID Report Endpoints */
  372. ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  373. CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
  374. #if 0
  375. ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
  376. CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
  377. #endif
  378. #endif
  379. #ifdef NKRO_ENABLE
  380. /* Setup NKRO HID Report Endpoints */
  381. ConfigSuccess &= ENDPOINT_CONFIG(NKRO_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  382. NKRO_EPSIZE, ENDPOINT_BANK_SINGLE);
  383. #endif
  384. #ifdef MIDI_ENABLE
  385. ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE);
  386. ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_OUT_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE);
  387. #endif
  388. #ifdef VIRTSER_ENABLE
  389. ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPADDR, EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, ENDPOINT_BANK_SINGLE);
  390. ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_OUT_EPADDR, EP_TYPE_BULK, CDC_EPSIZE, ENDPOINT_BANK_SINGLE);
  391. ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_IN_EPADDR, EP_TYPE_BULK, CDC_EPSIZE, ENDPOINT_BANK_SINGLE);
  392. #endif
  393. }
  394. /* FIXME: Expose this table in the docs somehow
  395. Appendix G: HID Request Support Requirements
  396. The following table enumerates the requests that need to be supported by various types of HID class devices.
  397. Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
  398. ------------------------------------------------------------------------------------------
  399. Boot Mouse Required Optional Optional Optional Required Required
  400. Non-Boot Mouse Required Optional Optional Optional Optional Optional
  401. Boot Keyboard Required Optional Required Required Required Required
  402. Non-Boot Keybrd Required Optional Required Required Optional Optional
  403. Other Device Required Optional Optional Optional Optional Optional
  404. */
  405. /** \brief Event handler for the USB_ControlRequest event.
  406. *
  407. * This is fired before passing along unhandled control requests to the library for processing internally.
  408. */
  409. void EVENT_USB_Device_ControlRequest(void)
  410. {
  411. uint8_t* ReportData = NULL;
  412. uint8_t ReportSize = 0;
  413. /* Handle HID Class specific requests */
  414. switch (USB_ControlRequest.bRequest)
  415. {
  416. case HID_REQ_GetReport:
  417. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  418. {
  419. Endpoint_ClearSETUP();
  420. // Interface
  421. switch (USB_ControlRequest.wIndex) {
  422. case KEYBOARD_INTERFACE:
  423. // TODO: test/check
  424. ReportData = (uint8_t*)&keyboard_report_sent;
  425. ReportSize = sizeof(keyboard_report_sent);
  426. break;
  427. }
  428. /* Write the report data to the control endpoint */
  429. Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
  430. Endpoint_ClearOUT();
  431. }
  432. break;
  433. case HID_REQ_SetReport:
  434. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  435. {
  436. // Interface
  437. switch (USB_ControlRequest.wIndex) {
  438. case KEYBOARD_INTERFACE:
  439. #ifdef NKRO_ENABLE
  440. case NKRO_INTERFACE:
  441. #endif
  442. Endpoint_ClearSETUP();
  443. while (!(Endpoint_IsOUTReceived())) {
  444. if (USB_DeviceState == DEVICE_STATE_Unattached)
  445. return;
  446. }
  447. keyboard_led_stats = Endpoint_Read_8();
  448. Endpoint_ClearOUT();
  449. Endpoint_ClearStatusStage();
  450. break;
  451. }
  452. }
  453. break;
  454. case HID_REQ_GetProtocol:
  455. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  456. {
  457. if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
  458. Endpoint_ClearSETUP();
  459. while (!(Endpoint_IsINReady()));
  460. Endpoint_Write_8(keyboard_protocol);
  461. Endpoint_ClearIN();
  462. Endpoint_ClearStatusStage();
  463. }
  464. }
  465. break;
  466. case HID_REQ_SetProtocol:
  467. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  468. {
  469. if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
  470. Endpoint_ClearSETUP();
  471. Endpoint_ClearStatusStage();
  472. keyboard_protocol = (USB_ControlRequest.wValue & 0xFF);
  473. clear_keyboard();
  474. }
  475. }
  476. break;
  477. case HID_REQ_SetIdle:
  478. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  479. {
  480. Endpoint_ClearSETUP();
  481. Endpoint_ClearStatusStage();
  482. keyboard_idle = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
  483. }
  484. break;
  485. case HID_REQ_GetIdle:
  486. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  487. {
  488. Endpoint_ClearSETUP();
  489. while (!(Endpoint_IsINReady()));
  490. Endpoint_Write_8(keyboard_idle);
  491. Endpoint_ClearIN();
  492. Endpoint_ClearStatusStage();
  493. }
  494. break;
  495. }
  496. #ifdef VIRTSER_ENABLE
  497. CDC_Device_ProcessControlRequest(&cdc_device);
  498. #endif
  499. }
  500. /*******************************************************************************
  501. * Host driver
  502. ******************************************************************************/
  503. /** \brief Keyboard LEDs
  504. *
  505. * FIXME: Needs doc
  506. */
  507. static uint8_t keyboard_leds(void)
  508. {
  509. return keyboard_led_stats;
  510. }
  511. /** \brief Send Keyboard
  512. *
  513. * FIXME: Needs doc
  514. */
  515. static void send_keyboard(report_keyboard_t *report)
  516. {
  517. uint8_t timeout = 255;
  518. uint8_t where = where_to_send();
  519. #ifdef BLUETOOTH_ENABLE
  520. if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
  521. #ifdef MODULE_ADAFRUIT_BLE
  522. adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys));
  523. #elif MODULE_RN42
  524. bluefruit_serial_send(0xFD);
  525. bluefruit_serial_send(0x09);
  526. bluefruit_serial_send(0x01);
  527. for (uint8_t i = 0; i < KEYBOARD_EPSIZE; i++) {
  528. bluefruit_serial_send(report->raw[i]);
  529. }
  530. #else
  531. bluefruit_serial_send(0xFD);
  532. for (uint8_t i = 0; i < KEYBOARD_EPSIZE; i++) {
  533. bluefruit_serial_send(report->raw[i]);
  534. }
  535. #endif
  536. }
  537. #endif
  538. if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
  539. return;
  540. }
  541. /* Select the Keyboard Report Endpoint */
  542. #ifdef NKRO_ENABLE
  543. if (keyboard_protocol && keymap_config.nkro) {
  544. /* Report protocol - NKRO */
  545. Endpoint_SelectEndpoint(NKRO_IN_EPNUM);
  546. /* Check if write ready for a polling interval around 1ms */
  547. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(4);
  548. if (!Endpoint_IsReadWriteAllowed()) return;
  549. /* Write Keyboard Report Data */
  550. Endpoint_Write_Stream_LE(report, NKRO_EPSIZE, NULL);
  551. }
  552. else
  553. #endif
  554. {
  555. /* Boot protocol */
  556. Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
  557. /* Check if write ready for a polling interval around 10ms */
  558. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  559. if (!Endpoint_IsReadWriteAllowed()) return;
  560. /* Write Keyboard Report Data */
  561. Endpoint_Write_Stream_LE(report, KEYBOARD_EPSIZE, NULL);
  562. }
  563. /* Finalize the stream transfer to send the last packet */
  564. Endpoint_ClearIN();
  565. keyboard_report_sent = *report;
  566. }
  567. /** \brief Send Mouse
  568. *
  569. * FIXME: Needs doc
  570. */
  571. static void send_mouse(report_mouse_t *report)
  572. {
  573. #ifdef MOUSE_ENABLE
  574. uint8_t timeout = 255;
  575. uint8_t where = where_to_send();
  576. #ifdef BLUETOOTH_ENABLE
  577. if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
  578. #ifdef MODULE_ADAFRUIT_BLE
  579. // FIXME: mouse buttons
  580. adafruit_ble_send_mouse_move(report->x, report->y, report->v, report->h, report->buttons);
  581. #else
  582. bluefruit_serial_send(0xFD);
  583. bluefruit_serial_send(0x00);
  584. bluefruit_serial_send(0x03);
  585. bluefruit_serial_send(report->buttons);
  586. bluefruit_serial_send(report->x);
  587. bluefruit_serial_send(report->y);
  588. bluefruit_serial_send(report->v); // should try sending the wheel v here
  589. bluefruit_serial_send(report->h); // should try sending the wheel h here
  590. bluefruit_serial_send(0x00);
  591. #endif
  592. }
  593. #endif
  594. if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
  595. return;
  596. }
  597. /* Select the Mouse Report Endpoint */
  598. Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
  599. /* Check if write ready for a polling interval around 10ms */
  600. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  601. if (!Endpoint_IsReadWriteAllowed()) return;
  602. /* Write Mouse Report Data */
  603. Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
  604. /* Finalize the stream transfer to send the last packet */
  605. Endpoint_ClearIN();
  606. #endif
  607. }
  608. /** \brief Send System
  609. *
  610. * FIXME: Needs doc
  611. */
  612. static void send_system(uint16_t data)
  613. {
  614. uint8_t timeout = 255;
  615. if (USB_DeviceState != DEVICE_STATE_Configured)
  616. return;
  617. report_extra_t r = {
  618. .report_id = REPORT_ID_SYSTEM,
  619. .usage = data - SYSTEM_POWER_DOWN + 1
  620. };
  621. Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
  622. /* Check if write ready for a polling interval around 10ms */
  623. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  624. if (!Endpoint_IsReadWriteAllowed()) return;
  625. Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
  626. Endpoint_ClearIN();
  627. }
  628. /** \brief Send Consumer
  629. *
  630. * FIXME: Needs doc
  631. */
  632. static void send_consumer(uint16_t data)
  633. {
  634. uint8_t timeout = 255;
  635. uint8_t where = where_to_send();
  636. #ifdef BLUETOOTH_ENABLE
  637. if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
  638. #ifdef MODULE_ADAFRUIT_BLE
  639. adafruit_ble_send_consumer_key(data, 0);
  640. #elif MODULE_RN42
  641. static uint16_t last_data = 0;
  642. if (data == last_data) return;
  643. last_data = data;
  644. uint16_t bitmap = CONSUMER2RN42(data);
  645. bluefruit_serial_send(0xFD);
  646. bluefruit_serial_send(0x03);
  647. bluefruit_serial_send(0x03);
  648. bluefruit_serial_send(bitmap&0xFF);
  649. bluefruit_serial_send((bitmap>>8)&0xFF);
  650. #else
  651. static uint16_t last_data = 0;
  652. if (data == last_data) return;
  653. last_data = data;
  654. uint16_t bitmap = CONSUMER2BLUEFRUIT(data);
  655. bluefruit_serial_send(0xFD);
  656. bluefruit_serial_send(0x00);
  657. bluefruit_serial_send(0x02);
  658. bluefruit_serial_send((bitmap>>8)&0xFF);
  659. bluefruit_serial_send(bitmap&0xFF);
  660. bluefruit_serial_send(0x00);
  661. bluefruit_serial_send(0x00);
  662. bluefruit_serial_send(0x00);
  663. bluefruit_serial_send(0x00);
  664. #endif
  665. }
  666. #endif
  667. if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
  668. return;
  669. }
  670. report_extra_t r = {
  671. .report_id = REPORT_ID_CONSUMER,
  672. .usage = data
  673. };
  674. Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
  675. /* Check if write ready for a polling interval around 10ms */
  676. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  677. if (!Endpoint_IsReadWriteAllowed()) return;
  678. Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
  679. Endpoint_ClearIN();
  680. }
  681. /*******************************************************************************
  682. * sendchar
  683. ******************************************************************************/
  684. #ifdef CONSOLE_ENABLE
  685. #define SEND_TIMEOUT 5
  686. /** \brief Send Char
  687. *
  688. * FIXME: Needs doc
  689. */
  690. int8_t sendchar(uint8_t c)
  691. {
  692. // Not wait once timeouted.
  693. // Because sendchar() is called so many times, waiting each call causes big lag.
  694. static bool timeouted = false;
  695. // prevents Console_Task() from running during sendchar() runs.
  696. // or char will be lost. These two function is mutually exclusive.
  697. CONSOLE_FLUSH_SET(false);
  698. if (USB_DeviceState != DEVICE_STATE_Configured)
  699. return -1;
  700. uint8_t ep = Endpoint_GetCurrentEndpoint();
  701. Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
  702. if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
  703. goto ERROR_EXIT;
  704. }
  705. if (timeouted && !Endpoint_IsReadWriteAllowed()) {
  706. goto ERROR_EXIT;
  707. }
  708. timeouted = false;
  709. uint8_t timeout = SEND_TIMEOUT;
  710. while (!Endpoint_IsReadWriteAllowed()) {
  711. if (USB_DeviceState != DEVICE_STATE_Configured) {
  712. goto ERROR_EXIT;
  713. }
  714. if (Endpoint_IsStalled()) {
  715. goto ERROR_EXIT;
  716. }
  717. if (!(timeout--)) {
  718. timeouted = true;
  719. goto ERROR_EXIT;
  720. }
  721. _delay_ms(1);
  722. }
  723. Endpoint_Write_8(c);
  724. // send when bank is full
  725. if (!Endpoint_IsReadWriteAllowed()) {
  726. while (!(Endpoint_IsINReady()));
  727. Endpoint_ClearIN();
  728. } else {
  729. CONSOLE_FLUSH_SET(true);
  730. }
  731. Endpoint_SelectEndpoint(ep);
  732. return 0;
  733. ERROR_EXIT:
  734. Endpoint_SelectEndpoint(ep);
  735. return -1;
  736. }
  737. #else
  738. int8_t sendchar(uint8_t c)
  739. {
  740. return 0;
  741. }
  742. #endif
  743. /*******************************************************************************
  744. * MIDI
  745. ******************************************************************************/
  746. #ifdef MIDI_ENABLE
  747. USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface =
  748. {
  749. .Config =
  750. {
  751. .StreamingInterfaceNumber = AS_INTERFACE,
  752. .DataINEndpoint =
  753. {
  754. .Address = MIDI_STREAM_IN_EPADDR,
  755. .Size = MIDI_STREAM_EPSIZE,
  756. .Banks = 1,
  757. },
  758. .DataOUTEndpoint =
  759. {
  760. .Address = MIDI_STREAM_OUT_EPADDR,
  761. .Size = MIDI_STREAM_EPSIZE,
  762. .Banks = 1,
  763. },
  764. },
  765. };
  766. void send_midi_packet(MIDI_EventPacket_t* event) {
  767. MIDI_Device_SendEventPacket(&USB_MIDI_Interface, event);
  768. }
  769. bool recv_midi_packet(MIDI_EventPacket_t* const event) {
  770. return MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, event);
  771. }
  772. #endif
  773. /*******************************************************************************
  774. * VIRTUAL SERIAL
  775. ******************************************************************************/
  776. #ifdef VIRTSER_ENABLE
  777. /** \brief Virtual Serial Init
  778. *
  779. * FIXME: Needs doc
  780. */
  781. void virtser_init(void)
  782. {
  783. cdc_device.State.ControlLineStates.DeviceToHost = CDC_CONTROL_LINE_IN_DSR ;
  784. CDC_Device_SendControlLineStateChange(&cdc_device);
  785. }
  786. /** \brief Virtual Serial Receive
  787. *
  788. * FIXME: Needs doc
  789. */
  790. void virtser_recv(uint8_t c) __attribute__ ((weak));
  791. void virtser_recv(uint8_t c)
  792. {
  793. // Ignore by default
  794. }
  795. /** \brief Virtual Serial Task
  796. *
  797. * FIXME: Needs doc
  798. */
  799. void virtser_task(void)
  800. {
  801. uint16_t count = CDC_Device_BytesReceived(&cdc_device);
  802. uint8_t ch;
  803. if (count)
  804. {
  805. ch = CDC_Device_ReceiveByte(&cdc_device);
  806. virtser_recv(ch);
  807. }
  808. }
  809. /** \brief Virtual Serial Send
  810. *
  811. * FIXME: Needs doc
  812. */
  813. void virtser_send(const uint8_t byte)
  814. {
  815. uint8_t timeout = 255;
  816. uint8_t ep = Endpoint_GetCurrentEndpoint();
  817. if (cdc_device.State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR)
  818. {
  819. /* IN packet */
  820. Endpoint_SelectEndpoint(cdc_device.Config.DataINEndpoint.Address);
  821. if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
  822. Endpoint_SelectEndpoint(ep);
  823. return;
  824. }
  825. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  826. Endpoint_Write_8(byte);
  827. CDC_Device_Flush(&cdc_device);
  828. if (Endpoint_IsINReady()) {
  829. Endpoint_ClearIN();
  830. }
  831. Endpoint_SelectEndpoint(ep);
  832. }
  833. }
  834. #endif
  835. /*******************************************************************************
  836. * main
  837. ******************************************************************************/
  838. /** \brief Setup MCU
  839. *
  840. * FIXME: Needs doc
  841. */
  842. static void setup_mcu(void)
  843. {
  844. /* Disable watchdog if enabled by bootloader/fuses */
  845. MCUSR &= ~(1 << WDRF);
  846. wdt_disable();
  847. /* Disable clock division */
  848. // clock_prescale_set(clock_div_1);
  849. CLKPR = (1 << CLKPCE);
  850. CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
  851. }
  852. /** \brief Setup USB
  853. *
  854. * FIXME: Needs doc
  855. */
  856. static void setup_usb(void)
  857. {
  858. // Leonardo needs. Without this USB device is not recognized.
  859. USB_Disable();
  860. USB_Init();
  861. // for Console_Task
  862. USB_Device_EnableSOFEvents();
  863. print_set_sendchar(sendchar);
  864. }
  865. /** \brief Main
  866. *
  867. * FIXME: Needs doc
  868. */
  869. int main(void) __attribute__ ((weak));
  870. int main(void)
  871. {
  872. #ifdef MIDI_ENABLE
  873. setup_midi();
  874. #endif
  875. setup_mcu();
  876. keyboard_setup();
  877. setup_usb();
  878. sei();
  879. #if defined(MODULE_ADAFRUIT_EZKEY) || defined(MODULE_RN42)
  880. serial_init();
  881. #endif
  882. /* wait for USB startup & debug output */
  883. #ifdef WAIT_FOR_USB
  884. while (USB_DeviceState != DEVICE_STATE_Configured) {
  885. #if defined(INTERRUPT_CONTROL_ENDPOINT)
  886. ;
  887. #else
  888. USB_USBTask();
  889. #endif
  890. }
  891. print("USB configured.\n");
  892. #else
  893. USB_USBTask();
  894. #endif
  895. /* init modules */
  896. keyboard_init();
  897. host_set_driver(&lufa_driver);
  898. #ifdef SLEEP_LED_ENABLE
  899. sleep_led_init();
  900. #endif
  901. #ifdef VIRTSER_ENABLE
  902. virtser_init();
  903. #endif
  904. print("Keyboard start.\n");
  905. while (1) {
  906. #if !defined(NO_USB_STARTUP_CHECK)
  907. while (USB_DeviceState == DEVICE_STATE_Suspended) {
  908. print("[s]");
  909. suspend_power_down();
  910. if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
  911. USB_Device_SendRemoteWakeup();
  912. }
  913. }
  914. #endif
  915. keyboard_task();
  916. #ifdef MIDI_ENABLE
  917. MIDI_Device_USBTask(&USB_MIDI_Interface);
  918. #endif
  919. #if defined(RGBLIGHT_ANIMATIONS) & defined(RGBLIGHT_ENABLE)
  920. rgblight_task();
  921. #endif
  922. #ifdef MODULE_ADAFRUIT_BLE
  923. adafruit_ble_task();
  924. #endif
  925. #ifdef VIRTSER_ENABLE
  926. virtser_task();
  927. CDC_Device_USBTask(&cdc_device);
  928. #endif
  929. #ifdef RAW_ENABLE
  930. raw_hid_task();
  931. #endif
  932. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  933. USB_USBTask();
  934. #endif
  935. }
  936. }
  937. uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
  938. const uint16_t wIndex,
  939. const void** const DescriptorAddress)
  940. {
  941. return get_usb_descriptor(wValue, wIndex, DescriptorAddress);
  942. }