main.c 5.1 KB

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