main.c 6.1 KB

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