main.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. #include "rgblight_reconfig.h"
  33. #if (defined(RGB_MIDI) || defined(RGBLIGHT_ANIMATIONS)) && defined(RGBLIGHT_ENABLE)
  34. # include "rgblight.h"
  35. #endif
  36. #ifdef SLEEP_LED_ENABLE
  37. # include "sleep_led.h"
  38. #endif
  39. #ifdef SERIAL_LINK_ENABLE
  40. # include "serial_link/system/serial_link.h"
  41. #endif
  42. #ifdef VISUALIZER_ENABLE
  43. # include "visualizer/visualizer.h"
  44. #endif
  45. #ifdef MIDI_ENABLE
  46. # include "qmk_midi.h"
  47. #endif
  48. #ifdef STM32_EEPROM_ENABLE
  49. # include "eeprom_stm32.h"
  50. #endif
  51. #include "suspend.h"
  52. #include "wait.h"
  53. /* -------------------------
  54. * TMK host driver defs
  55. * -------------------------
  56. */
  57. /* declarations */
  58. uint8_t keyboard_leds(void);
  59. void send_keyboard(report_keyboard_t *report);
  60. void send_mouse(report_mouse_t *report);
  61. void send_system(uint16_t data);
  62. void send_consumer(uint16_t data);
  63. /* host struct */
  64. host_driver_t chibios_driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer};
  65. #ifdef VIRTSER_ENABLE
  66. void virtser_task(void);
  67. #endif
  68. #ifdef RAW_ENABLE
  69. void raw_hid_task(void);
  70. #endif
  71. #ifdef CONSOLE_ENABLE
  72. void console_task(void);
  73. #endif
  74. #ifdef MIDI_ENABLE
  75. void midi_ep_task(void);
  76. #endif
  77. /* TESTING
  78. * Amber LED blinker thread, times are in milliseconds.
  79. */
  80. /* set this variable to non-zero anywhere to blink once */
  81. // static THD_WORKING_AREA(waThread1, 128);
  82. // static THD_FUNCTION(Thread1, arg) {
  83. // (void)arg;
  84. // chRegSetThreadName("blinker");
  85. // while (true) {
  86. // systime_t time;
  87. // time = USB_DRIVER.state == USB_ACTIVE ? 250 : 500;
  88. // palClearLine(LINE_CAPS_LOCK);
  89. // chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
  90. // palSetLine(LINE_CAPS_LOCK);
  91. // chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
  92. // }
  93. // }
  94. /* Main thread
  95. */
  96. int main(void) {
  97. /* ChibiOS/RT init */
  98. halInit();
  99. chSysInit();
  100. #ifdef STM32_EEPROM_ENABLE
  101. EEPROM_Init();
  102. #endif
  103. // TESTING
  104. // chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  105. keyboard_setup();
  106. /* Init USB */
  107. init_usb_driver(&USB_DRIVER);
  108. /* init printf */
  109. init_printf(NULL, sendchar_pf);
  110. #ifdef MIDI_ENABLE
  111. setup_midi();
  112. #endif
  113. #ifdef SERIAL_LINK_ENABLE
  114. init_serial_link();
  115. #endif
  116. #ifdef VISUALIZER_ENABLE
  117. visualizer_init();
  118. #endif
  119. host_driver_t *driver = NULL;
  120. /* Wait until the USB or serial link is active */
  121. while (true) {
  122. #if defined(WAIT_FOR_USB) || defined(SERIAL_LINK_ENABLE)
  123. if (USB_DRIVER.state == USB_ACTIVE) {
  124. driver = &chibios_driver;
  125. break;
  126. }
  127. #else
  128. driver = &chibios_driver;
  129. break;
  130. #endif
  131. #ifdef SERIAL_LINK_ENABLE
  132. if (is_serial_link_connected()) {
  133. driver = get_serial_link_driver();
  134. break;
  135. }
  136. serial_link_update();
  137. #endif
  138. wait_ms(50);
  139. }
  140. /* Do need to wait here!
  141. * Otherwise the next print might start a transfer on console EP
  142. * before the USB is completely ready, which sometimes causes
  143. * HardFaults.
  144. */
  145. wait_ms(50);
  146. print("USB configured.\n");
  147. /* init TMK modules */
  148. keyboard_init();
  149. host_set_driver(driver);
  150. #ifdef SLEEP_LED_ENABLE
  151. sleep_led_init();
  152. #endif
  153. print("Keyboard start.\n");
  154. /* Main loop */
  155. while (true) {
  156. #if !defined(NO_USB_STARTUP_CHECK)
  157. if (USB_DRIVER.state == USB_SUSPENDED) {
  158. print("[s]");
  159. # ifdef VISUALIZER_ENABLE
  160. visualizer_suspend();
  161. # endif
  162. while (USB_DRIVER.state == USB_SUSPENDED) {
  163. /* Do this in the suspended state */
  164. # ifdef SERIAL_LINK_ENABLE
  165. serial_link_update();
  166. # endif
  167. suspend_power_down(); // on AVR this deep sleeps for 15ms
  168. /* Remote wakeup */
  169. if (suspend_wakeup_condition()) {
  170. usbWakeupHost(&USB_DRIVER);
  171. }
  172. }
  173. /* Woken up */
  174. // variables has been already cleared by the wakeup hook
  175. send_keyboard_report();
  176. # ifdef MOUSEKEY_ENABLE
  177. mousekey_send();
  178. # endif /* MOUSEKEY_ENABLE */
  179. # ifdef VISUALIZER_ENABLE
  180. visualizer_resume();
  181. # endif
  182. }
  183. #endif
  184. keyboard_task();
  185. #ifdef CONSOLE_ENABLE
  186. console_task();
  187. #endif
  188. #ifdef MIDI_ENABLE
  189. midi_ep_task();
  190. #endif
  191. #ifdef VIRTSER_ENABLE
  192. virtser_task();
  193. #endif
  194. #ifdef RAW_ENABLE
  195. raw_hid_task();
  196. #endif
  197. }
  198. }