main.c 6.1 KB

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