chibios.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "usb_device_state.h"
  28. #include "mousekey.h"
  29. #include "led.h"
  30. #include "sendchar.h"
  31. #include "debug.h"
  32. #include "print.h"
  33. #ifndef EARLY_INIT_PERFORM_BOOTLOADER_JUMP
  34. // Change this to be TRUE once we've migrated keyboards to the new init system
  35. // Remember to change docs/platformdev_chibios_earlyinit.md as well.
  36. # define EARLY_INIT_PERFORM_BOOTLOADER_JUMP FALSE
  37. #endif
  38. #ifdef SLEEP_LED_ENABLE
  39. # include "sleep_led.h"
  40. #endif
  41. #ifdef MIDI_ENABLE
  42. # include "qmk_midi.h"
  43. #endif
  44. #include "suspend.h"
  45. #include "wait.h"
  46. /* -------------------------
  47. * TMK host driver defs
  48. * -------------------------
  49. */
  50. /* declarations */
  51. uint8_t keyboard_leds(void);
  52. void send_keyboard(report_keyboard_t *report);
  53. void send_mouse(report_mouse_t *report);
  54. void send_system(uint16_t data);
  55. void send_consumer(uint16_t data);
  56. void send_digitizer(report_digitizer_t *report);
  57. /* host struct */
  58. host_driver_t chibios_driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer};
  59. #ifdef VIRTSER_ENABLE
  60. void virtser_task(void);
  61. #endif
  62. #ifdef RAW_ENABLE
  63. void raw_hid_task(void);
  64. #endif
  65. #ifdef CONSOLE_ENABLE
  66. void console_task(void);
  67. #endif
  68. #ifdef MIDI_ENABLE
  69. void midi_ep_task(void);
  70. #endif
  71. /* TESTING
  72. * Amber LED blinker thread, times are in milliseconds.
  73. */
  74. /* set this variable to non-zero anywhere to blink once */
  75. // static THD_WORKING_AREA(waThread1, 128);
  76. // static THD_FUNCTION(Thread1, arg) {
  77. // (void)arg;
  78. // chRegSetThreadName("blinker");
  79. // while (true) {
  80. // systime_t time;
  81. // time = USB_DRIVER.state == USB_ACTIVE ? 250 : 500;
  82. // palClearLine(LINE_CAPS_LOCK);
  83. // chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
  84. // palSetLine(LINE_CAPS_LOCK);
  85. // chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
  86. // }
  87. // }
  88. /* Early initialisation
  89. */
  90. __attribute__((weak)) void early_hardware_init_pre(void) {
  91. #if EARLY_INIT_PERFORM_BOOTLOADER_JUMP
  92. void enter_bootloader_mode_if_requested(void);
  93. enter_bootloader_mode_if_requested();
  94. #endif // EARLY_INIT_PERFORM_BOOTLOADER_JUMP
  95. }
  96. __attribute__((weak)) void early_hardware_init_post(void) {}
  97. __attribute__((weak)) void board_init(void) {}
  98. // This overrides what's normally in ChibiOS board definitions
  99. void __early_init(void) {
  100. early_hardware_init_pre();
  101. // This is the renamed equivalent of __early_init in the board.c file
  102. void __chibios_override___early_init(void);
  103. __chibios_override___early_init();
  104. early_hardware_init_post();
  105. }
  106. // This overrides what's normally in ChibiOS board definitions
  107. void boardInit(void) {
  108. // This is the renamed equivalent of boardInit in the board.c file
  109. void __chibios_override_boardInit(void);
  110. __chibios_override_boardInit();
  111. board_init();
  112. }
  113. void protocol_setup(void) {
  114. usb_device_state_init();
  115. // TESTING
  116. // chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  117. }
  118. static host_driver_t *driver = NULL;
  119. void protocol_pre_init(void) {
  120. /* Init USB */
  121. usb_event_queue_init();
  122. init_usb_driver(&USB_DRIVER);
  123. #ifdef MIDI_ENABLE
  124. setup_midi();
  125. #endif
  126. /* Wait until USB is active */
  127. while (true) {
  128. #if defined(WAIT_FOR_USB)
  129. if (USB_DRIVER.state == USB_ACTIVE) {
  130. driver = &chibios_driver;
  131. break;
  132. }
  133. #else
  134. driver = &chibios_driver;
  135. break;
  136. #endif
  137. wait_ms(50);
  138. }
  139. /* Do need to wait here!
  140. * Otherwise the next print might start a transfer on console EP
  141. * before the USB is completely ready, which sometimes causes
  142. * HardFaults.
  143. */
  144. wait_ms(50);
  145. print("USB configured.\n");
  146. }
  147. void protocol_post_init(void) { host_set_driver(driver); }
  148. void protocol_pre_task(void) {
  149. usb_event_queue_task();
  150. #if !defined(NO_USB_STARTUP_CHECK)
  151. if (USB_DRIVER.state == USB_SUSPENDED) {
  152. print("[s]");
  153. while (USB_DRIVER.state == USB_SUSPENDED) {
  154. /* Do this in the suspended state */
  155. suspend_power_down(); // on AVR this deep sleeps for 15ms
  156. /* Remote wakeup */
  157. if (suspend_wakeup_condition()) {
  158. usbWakeupHost(&USB_DRIVER);
  159. restart_usb_driver(&USB_DRIVER);
  160. }
  161. }
  162. /* Woken up */
  163. // variables has been already cleared by the wakeup hook
  164. send_keyboard_report();
  165. # ifdef MOUSEKEY_ENABLE
  166. mousekey_send();
  167. # endif /* MOUSEKEY_ENABLE */
  168. }
  169. #endif
  170. }
  171. void protocol_post_task(void) {
  172. #ifdef CONSOLE_ENABLE
  173. console_task();
  174. #endif
  175. #ifdef MIDI_ENABLE
  176. midi_ep_task();
  177. #endif
  178. #ifdef VIRTSER_ENABLE
  179. virtser_task();
  180. #endif
  181. #ifdef RAW_ENABLE
  182. raw_hid_task();
  183. #endif
  184. }