main.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 "print.h"
  32. #ifndef EARLY_INIT_PERFORM_BOOTLOADER_JUMP
  33. // Change this to be TRUE once we've migrated keyboards to the new init system
  34. // Remember to change docs/platformdev_chibios_earlyinit.md as well.
  35. # define EARLY_INIT_PERFORM_BOOTLOADER_JUMP FALSE
  36. #endif
  37. #ifdef SLEEP_LED_ENABLE
  38. # include "sleep_led.h"
  39. #endif
  40. #ifdef SERIAL_LINK_ENABLE
  41. # include "serial_link/system/serial_link.h"
  42. #endif
  43. #ifdef VISUALIZER_ENABLE
  44. # include "visualizer/visualizer.h"
  45. #endif
  46. #ifdef MIDI_ENABLE
  47. # include "qmk_midi.h"
  48. #endif
  49. #include "suspend.h"
  50. #include "wait.h"
  51. /* -------------------------
  52. * TMK host driver defs
  53. * -------------------------
  54. */
  55. /* declarations */
  56. uint8_t keyboard_leds(void);
  57. void send_keyboard(report_keyboard_t *report);
  58. void send_mouse(report_mouse_t *report);
  59. void send_system(uint16_t data);
  60. void send_consumer(uint16_t data);
  61. void send_digitizer(report_digitizer_t *report);
  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. /* Early initialisation
  94. */
  95. __attribute__((weak)) void early_hardware_init_pre(void) {
  96. #if EARLY_INIT_PERFORM_BOOTLOADER_JUMP
  97. void enter_bootloader_mode_if_requested(void);
  98. enter_bootloader_mode_if_requested();
  99. #endif // EARLY_INIT_PERFORM_BOOTLOADER_JUMP
  100. }
  101. __attribute__((weak)) void early_hardware_init_post(void) {}
  102. __attribute__((weak)) void board_init(void) {}
  103. // This overrides what's normally in ChibiOS board definitions
  104. void __early_init(void) {
  105. early_hardware_init_pre();
  106. // This is the renamed equivalent of __early_init in the board.c file
  107. void __chibios_override___early_init(void);
  108. __chibios_override___early_init();
  109. early_hardware_init_post();
  110. }
  111. // This overrides what's normally in ChibiOS board definitions
  112. void boardInit(void) {
  113. // This is the renamed equivalent of boardInit in the board.c file
  114. void __chibios_override_boardInit(void);
  115. __chibios_override_boardInit();
  116. board_init();
  117. }
  118. /* Main thread
  119. */
  120. int main(void) {
  121. /* ChibiOS/RT init */
  122. halInit();
  123. chSysInit();
  124. // TESTING
  125. // chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  126. keyboard_setup();
  127. /* Init USB */
  128. usb_event_queue_init();
  129. init_usb_driver(&USB_DRIVER);
  130. #ifdef MIDI_ENABLE
  131. setup_midi();
  132. #endif
  133. #ifdef SERIAL_LINK_ENABLE
  134. init_serial_link();
  135. #endif
  136. #ifdef VISUALIZER_ENABLE
  137. visualizer_init();
  138. #endif
  139. host_driver_t *driver = NULL;
  140. /* Wait until the USB or serial link is active */
  141. while (true) {
  142. #if defined(WAIT_FOR_USB) || defined(SERIAL_LINK_ENABLE)
  143. if (USB_DRIVER.state == USB_ACTIVE) {
  144. driver = &chibios_driver;
  145. break;
  146. }
  147. #else
  148. driver = &chibios_driver;
  149. break;
  150. #endif
  151. #ifdef SERIAL_LINK_ENABLE
  152. if (is_serial_link_connected()) {
  153. driver = get_serial_link_driver();
  154. break;
  155. }
  156. serial_link_update();
  157. #endif
  158. wait_ms(50);
  159. }
  160. /* Do need to wait here!
  161. * Otherwise the next print might start a transfer on console EP
  162. * before the USB is completely ready, which sometimes causes
  163. * HardFaults.
  164. */
  165. wait_ms(50);
  166. print("USB configured.\n");
  167. /* init TMK modules */
  168. keyboard_init();
  169. host_set_driver(driver);
  170. #ifdef SLEEP_LED_ENABLE
  171. sleep_led_init();
  172. #endif
  173. print("Keyboard start.\n");
  174. /* Main loop */
  175. while (true) {
  176. usb_event_queue_task();
  177. #if !defined(NO_USB_STARTUP_CHECK)
  178. if (USB_DRIVER.state == USB_SUSPENDED) {
  179. print("[s]");
  180. # ifdef VISUALIZER_ENABLE
  181. visualizer_suspend();
  182. # endif
  183. while (USB_DRIVER.state == USB_SUSPENDED) {
  184. /* Do this in the suspended state */
  185. # ifdef SERIAL_LINK_ENABLE
  186. serial_link_update();
  187. # endif
  188. suspend_power_down(); // on AVR this deep sleeps for 15ms
  189. /* Remote wakeup */
  190. if (suspend_wakeup_condition()) {
  191. usbWakeupHost(&USB_DRIVER);
  192. restart_usb_driver(&USB_DRIVER);
  193. }
  194. }
  195. /* Woken up */
  196. // variables has been already cleared by the wakeup hook
  197. send_keyboard_report();
  198. # ifdef MOUSEKEY_ENABLE
  199. mousekey_send();
  200. # endif /* MOUSEKEY_ENABLE */
  201. # ifdef VISUALIZER_ENABLE
  202. visualizer_resume();
  203. # endif
  204. }
  205. #endif
  206. keyboard_task();
  207. #ifdef CONSOLE_ENABLE
  208. console_task();
  209. #endif
  210. #ifdef MIDI_ENABLE
  211. midi_ep_task();
  212. #endif
  213. #ifdef VIRTSER_ENABLE
  214. virtser_task();
  215. #endif
  216. #ifdef RAW_ENABLE
  217. raw_hid_task();
  218. #endif
  219. // Run housekeeping
  220. housekeeping_task();
  221. }
  222. }