main.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. #ifdef STM32_EEPROM_ENABLE
  50. # include "eeprom_stm32.h"
  51. #endif
  52. #ifdef EEPROM_DRIVER
  53. # include "eeprom_driver.h"
  54. #endif
  55. #include "suspend.h"
  56. #include "wait.h"
  57. /* -------------------------
  58. * TMK host driver defs
  59. * -------------------------
  60. */
  61. /* declarations */
  62. uint8_t keyboard_leds(void);
  63. void send_keyboard(report_keyboard_t *report);
  64. void send_mouse(report_mouse_t *report);
  65. void send_system(uint16_t data);
  66. void send_consumer(uint16_t data);
  67. /* host struct */
  68. host_driver_t chibios_driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer};
  69. #ifdef VIRTSER_ENABLE
  70. void virtser_task(void);
  71. #endif
  72. #ifdef RAW_ENABLE
  73. void raw_hid_task(void);
  74. #endif
  75. #ifdef CONSOLE_ENABLE
  76. void console_task(void);
  77. #endif
  78. #ifdef MIDI_ENABLE
  79. void midi_ep_task(void);
  80. #endif
  81. #ifdef GAMEPAD_ENABLE
  82. void gamepad_ep_task(void);
  83. #endif
  84. /* TESTING
  85. * Amber LED blinker thread, times are in milliseconds.
  86. */
  87. /* set this variable to non-zero anywhere to blink once */
  88. // static THD_WORKING_AREA(waThread1, 128);
  89. // static THD_FUNCTION(Thread1, arg) {
  90. // (void)arg;
  91. // chRegSetThreadName("blinker");
  92. // while (true) {
  93. // systime_t time;
  94. // time = USB_DRIVER.state == USB_ACTIVE ? 250 : 500;
  95. // palClearLine(LINE_CAPS_LOCK);
  96. // chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
  97. // palSetLine(LINE_CAPS_LOCK);
  98. // chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
  99. // }
  100. // }
  101. /* Early initialisation
  102. */
  103. __attribute__((weak)) void early_hardware_init_pre(void) {
  104. #if EARLY_INIT_PERFORM_BOOTLOADER_JUMP
  105. void enter_bootloader_mode_if_requested(void);
  106. enter_bootloader_mode_if_requested();
  107. #endif // EARLY_INIT_PERFORM_BOOTLOADER_JUMP
  108. }
  109. __attribute__((weak)) void early_hardware_init_post(void) {}
  110. __attribute__((weak)) void board_init(void) {}
  111. // This overrides what's normally in ChibiOS board definitions
  112. void __early_init(void) {
  113. early_hardware_init_pre();
  114. // This is the renamed equivalent of __early_init in the board.c file
  115. void __chibios_override___early_init(void);
  116. __chibios_override___early_init();
  117. early_hardware_init_post();
  118. }
  119. // This overrides what's normally in ChibiOS board definitions
  120. void boardInit(void) {
  121. // This is the renamed equivalent of boardInit in the board.c file
  122. void __chibios_override_boardInit(void);
  123. __chibios_override_boardInit();
  124. board_init();
  125. }
  126. /* Main thread
  127. */
  128. int main(void) {
  129. /* ChibiOS/RT init */
  130. halInit();
  131. chSysInit();
  132. #ifdef STM32_EEPROM_ENABLE
  133. EEPROM_Init();
  134. #endif
  135. #ifdef EEPROM_DRIVER
  136. eeprom_driver_init();
  137. #endif
  138. // TESTING
  139. // chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  140. keyboard_setup();
  141. /* Init USB */
  142. init_usb_driver(&USB_DRIVER);
  143. #ifdef MIDI_ENABLE
  144. setup_midi();
  145. #endif
  146. #ifdef SERIAL_LINK_ENABLE
  147. init_serial_link();
  148. #endif
  149. #ifdef VISUALIZER_ENABLE
  150. visualizer_init();
  151. #endif
  152. host_driver_t *driver = NULL;
  153. /* Wait until the USB or serial link is active */
  154. while (true) {
  155. #if defined(WAIT_FOR_USB) || defined(SERIAL_LINK_ENABLE)
  156. if (USB_DRIVER.state == USB_ACTIVE) {
  157. driver = &chibios_driver;
  158. break;
  159. }
  160. #else
  161. driver = &chibios_driver;
  162. break;
  163. #endif
  164. #ifdef SERIAL_LINK_ENABLE
  165. if (is_serial_link_connected()) {
  166. driver = get_serial_link_driver();
  167. break;
  168. }
  169. serial_link_update();
  170. #endif
  171. wait_ms(50);
  172. }
  173. /* Do need to wait here!
  174. * Otherwise the next print might start a transfer on console EP
  175. * before the USB is completely ready, which sometimes causes
  176. * HardFaults.
  177. */
  178. wait_ms(50);
  179. print("USB configured.\n");
  180. /* init TMK modules */
  181. keyboard_init();
  182. host_set_driver(driver);
  183. #ifdef SLEEP_LED_ENABLE
  184. sleep_led_init();
  185. #endif
  186. print("Keyboard start.\n");
  187. /* Main loop */
  188. while (true) {
  189. #if !defined(NO_USB_STARTUP_CHECK)
  190. if (USB_DRIVER.state == USB_SUSPENDED) {
  191. print("[s]");
  192. # ifdef VISUALIZER_ENABLE
  193. visualizer_suspend();
  194. # endif
  195. while (USB_DRIVER.state == USB_SUSPENDED) {
  196. /* Do this in the suspended state */
  197. # ifdef SERIAL_LINK_ENABLE
  198. serial_link_update();
  199. # endif
  200. suspend_power_down(); // on AVR this deep sleeps for 15ms
  201. /* Remote wakeup */
  202. if (suspend_wakeup_condition()) {
  203. usbWakeupHost(&USB_DRIVER);
  204. restart_usb_driver(&USB_DRIVER);
  205. }
  206. }
  207. /* Woken up */
  208. // variables has been already cleared by the wakeup hook
  209. send_keyboard_report();
  210. # ifdef MOUSEKEY_ENABLE
  211. mousekey_send();
  212. # endif /* MOUSEKEY_ENABLE */
  213. # ifdef VISUALIZER_ENABLE
  214. visualizer_resume();
  215. # endif
  216. }
  217. #endif
  218. keyboard_task();
  219. #ifdef CONSOLE_ENABLE
  220. console_task();
  221. #endif
  222. #ifdef MIDI_ENABLE
  223. midi_ep_task();
  224. #endif
  225. #ifdef GAMEPAD_ENABLE
  226. gamepad_ep_task();
  227. #endif
  228. #ifdef VIRTSER_ENABLE
  229. virtser_task();
  230. #endif
  231. #ifdef RAW_ENABLE
  232. raw_hid_task();
  233. #endif
  234. // Run housekeeping
  235. housekeeping_task_kb();
  236. housekeeping_task_user();
  237. }
  238. }