main.c 4.9 KB

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