main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * (c) 2015 flabberast <s3+flabbergast@sdfeu.org>
  3. *
  4. * Based on the following work:
  5. * - Guillaume Duc's raw hid example (MIT License)
  6. * https://github.com/guiduc/usb-hid-chibios-example
  7. * - PJRC Teensy examples (MIT License)
  8. * https://www.pjrc.com/teensy/usb_keyboard.html
  9. * - hasu's TMK keyboard code (GPL v2 and some code Modified BSD)
  10. * https://github.com/tmk/tmk_keyboard/
  11. * - ChibiOS demo code (Apache 2.0 License)
  12. * http://www.chibios.org
  13. *
  14. * Since some GPL'd code is used, this work is licensed under
  15. * GPL v2 or later.
  16. */
  17. #include "ch.h"
  18. #include "hal.h"
  19. #include "usb_main.h"
  20. /* TMK includes */
  21. #include "report.h"
  22. #include "host.h"
  23. #include "host_driver.h"
  24. #include "keyboard.h"
  25. #include "action.h"
  26. #include "action_util.h"
  27. #include "mousekey.h"
  28. #include "led.h"
  29. #include "sendchar.h"
  30. #include "debug.h"
  31. #include "printf.h"
  32. #ifdef SLEEP_LED_ENABLE
  33. #include "sleep_led.h"
  34. #endif
  35. #ifdef SERIAL_LINK_ENABLE
  36. #include "serial_link/system/serial_link.h"
  37. #endif
  38. #ifdef VISUALIZER_ENABLE
  39. #include "visualizer/visualizer.h"
  40. #endif
  41. #ifdef MIDI_ENABLE
  42. #include "qmk_midi.h"
  43. #endif
  44. #include "suspend.h"
  45. #include "wait.h"
  46. /* -------------------------
  47. * TMK host driver defs
  48. * -------------------------
  49. */
  50. /* declarations */
  51. uint8_t keyboard_leds(void);
  52. void send_keyboard(report_keyboard_t *report);
  53. void send_mouse(report_mouse_t *report);
  54. void send_system(uint16_t data);
  55. void send_consumer(uint16_t data);
  56. /* host struct */
  57. host_driver_t chibios_driver = {
  58. keyboard_leds,
  59. send_keyboard,
  60. send_mouse,
  61. send_system,
  62. send_consumer
  63. };
  64. #ifdef VIRTSER_ENABLE
  65. void virtser_task(void);
  66. #endif
  67. #ifdef RAW_HID_ENABLE
  68. void raw_hid_task(void);
  69. #endif
  70. #ifdef CONSOLE_ENABLE
  71. void console_task(void);
  72. #endif
  73. /* TESTING
  74. * Amber LED blinker thread, times are in milliseconds.
  75. */
  76. /* set this variable to non-zero anywhere to blink once */
  77. // static THD_WORKING_AREA(waThread1, 128);
  78. // static THD_FUNCTION(Thread1, arg) {
  79. // (void)arg;
  80. // chRegSetThreadName("blinker");
  81. // while (true) {
  82. // systime_t time;
  83. // time = USB_DRIVER.state == USB_ACTIVE ? 250 : 500;
  84. // palClearLine(LINE_CAPS_LOCK);
  85. // chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
  86. // palSetLine(LINE_CAPS_LOCK);
  87. // chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
  88. // }
  89. // }
  90. /* Main thread
  91. */
  92. int main(void) {
  93. /* ChibiOS/RT init */
  94. halInit();
  95. chSysInit();
  96. // TESTING
  97. // chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  98. /* Init USB */
  99. init_usb_driver(&USB_DRIVER);
  100. /* init printf */
  101. init_printf(NULL,sendchar_pf);
  102. #ifdef MIDI_ENABLE
  103. setup_midi();
  104. #endif
  105. #ifdef SERIAL_LINK_ENABLE
  106. init_serial_link();
  107. #endif
  108. #ifdef VISUALIZER_ENABLE
  109. visualizer_init();
  110. #endif
  111. host_driver_t* driver = NULL;
  112. /* Wait until the USB or serial link is active */
  113. while (true) {
  114. #if !defined(NO_USB_STARTUP_CHECK)
  115. if(USB_DRIVER.state == USB_ACTIVE)
  116. #endif
  117. {
  118. driver = &chibios_driver;
  119. break;
  120. }
  121. #ifdef SERIAL_LINK_ENABLE
  122. if(is_serial_link_connected()) {
  123. driver = get_serial_link_driver();
  124. break;
  125. }
  126. serial_link_update();
  127. #endif
  128. wait_ms(50);
  129. }
  130. /* Do need to wait here!
  131. * Otherwise the next print might start a transfer on console EP
  132. * before the USB is completely ready, which sometimes causes
  133. * HardFaults.
  134. */
  135. wait_ms(50);
  136. print("USB configured.\n");
  137. /* init TMK modules */
  138. keyboard_init();
  139. host_set_driver(driver);
  140. #ifdef SLEEP_LED_ENABLE
  141. sleep_led_init();
  142. #endif
  143. print("Keyboard start.\n");
  144. /* Main loop */
  145. while(true) {
  146. if(USB_DRIVER.state == USB_SUSPENDED) {
  147. print("[s]");
  148. #ifdef VISUALIZER_ENABLE
  149. visualizer_suspend();
  150. #endif
  151. while(USB_DRIVER.state == USB_SUSPENDED) {
  152. /* Do this in the suspended state */
  153. #ifdef SERIAL_LINK_ENABLE
  154. serial_link_update();
  155. #endif
  156. suspend_power_down(); // on AVR this deep sleeps for 15ms
  157. /* Remote wakeup */
  158. if(suspend_wakeup_condition()) {
  159. usbWakeupHost(&USB_DRIVER);
  160. }
  161. }
  162. /* Woken up */
  163. // variables has been already cleared by the wakeup hook
  164. send_keyboard_report();
  165. #ifdef MOUSEKEY_ENABLE
  166. mousekey_send();
  167. #endif /* MOUSEKEY_ENABLE */
  168. #ifdef VISUALIZER_ENABLE
  169. visualizer_resume();
  170. #endif
  171. }
  172. keyboard_task();
  173. #ifdef CONSOLE_ENABLE
  174. console_task();
  175. #endif
  176. #ifdef VIRTSER_ENABLE
  177. virtser_task();
  178. #endif
  179. #ifdef RAW_HID_ENABLE
  180. raw_hid_task();
  181. #endif
  182. }
  183. }