chibios.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #define USB_GETSTATUS_REMOTE_WAKEUP_ENABLED (2U)
  47. #ifdef WAIT_FOR_USB
  48. // TODO: Remove backwards compatibility with old define
  49. # define USB_WAIT_FOR_ENUMERATION
  50. #endif
  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_nkro(report_nkro_t *report);
  59. void send_mouse(report_mouse_t *report);
  60. void send_extra(report_extra_t *report);
  61. /* host struct */
  62. host_driver_t chibios_driver = {keyboard_leds, send_keyboard, send_nkro, send_mouse, send_extra};
  63. #ifdef VIRTSER_ENABLE
  64. void virtser_task(void);
  65. #endif
  66. /* TESTING
  67. * Amber LED blinker thread, times are in milliseconds.
  68. */
  69. /* set this variable to non-zero anywhere to blink once */
  70. // static THD_WORKING_AREA(waThread1, 128);
  71. // static THD_FUNCTION(Thread1, arg) {
  72. // (void)arg;
  73. // chRegSetThreadName("blinker");
  74. // while (true) {
  75. // systime_t time;
  76. // time = USB_DRIVER.state == USB_ACTIVE ? 250 : 500;
  77. // palClearLine(LINE_CAPS_LOCK);
  78. // chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
  79. // palSetLine(LINE_CAPS_LOCK);
  80. // chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
  81. // }
  82. // }
  83. /* Early initialisation
  84. */
  85. __attribute__((weak)) void early_hardware_init_pre(void) {
  86. #if EARLY_INIT_PERFORM_BOOTLOADER_JUMP
  87. void enter_bootloader_mode_if_requested(void);
  88. enter_bootloader_mode_if_requested();
  89. #endif // EARLY_INIT_PERFORM_BOOTLOADER_JUMP
  90. }
  91. __attribute__((weak)) void early_hardware_init_post(void) {}
  92. __attribute__((weak)) void board_init(void) {}
  93. // This overrides what's normally in ChibiOS board definitions
  94. void __early_init(void) {
  95. early_hardware_init_pre();
  96. // This is the renamed equivalent of __early_init in the board.c file
  97. void __chibios_override___early_init(void);
  98. __chibios_override___early_init();
  99. early_hardware_init_post();
  100. }
  101. // This overrides what's normally in ChibiOS board definitions
  102. void boardInit(void) {
  103. // This is the renamed equivalent of boardInit in the board.c file
  104. void __chibios_override_boardInit(void);
  105. __chibios_override_boardInit();
  106. board_init();
  107. }
  108. void protocol_setup(void) {
  109. usb_device_state_init();
  110. // TESTING
  111. // chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  112. }
  113. static host_driver_t *driver = NULL;
  114. void protocol_pre_init(void) {
  115. /* Init USB */
  116. usb_event_queue_init();
  117. init_usb_driver(&USB_DRIVER);
  118. #ifdef MIDI_ENABLE
  119. setup_midi();
  120. #endif
  121. /* Wait until USB is active */
  122. while (true) {
  123. #if defined(USB_WAIT_FOR_ENUMERATION)
  124. if (USB_DRIVER.state == USB_ACTIVE) {
  125. driver = &chibios_driver;
  126. break;
  127. }
  128. #else
  129. driver = &chibios_driver;
  130. break;
  131. #endif
  132. wait_ms(50);
  133. }
  134. /* Do need to wait here!
  135. * Otherwise the next print might start a transfer on console EP
  136. * before the USB is completely ready, which sometimes causes
  137. * HardFaults.
  138. */
  139. wait_ms(50);
  140. print("USB configured.\n");
  141. }
  142. void protocol_post_init(void) {
  143. host_set_driver(driver);
  144. }
  145. void protocol_pre_task(void) {
  146. usb_event_queue_task();
  147. #if !defined(NO_USB_STARTUP_CHECK)
  148. if (USB_DRIVER.state == USB_SUSPENDED) {
  149. dprintln("suspending keyboard");
  150. while (USB_DRIVER.state == USB_SUSPENDED) {
  151. /* Do this in the suspended state */
  152. suspend_power_down(); // on AVR this deep sleeps for 15ms
  153. /* Remote wakeup */
  154. if ((USB_DRIVER.status & USB_GETSTATUS_REMOTE_WAKEUP_ENABLED) && suspend_wakeup_condition()) {
  155. usbWakeupHost(&USB_DRIVER);
  156. # if USB_SUSPEND_WAKEUP_DELAY > 0
  157. // Some hubs, kvm switches, and monitors do
  158. // weird things, with USB device state bouncing
  159. // around wildly on wakeup, yielding race
  160. // conditions that can corrupt the keyboard state.
  161. //
  162. // Pause for a while to let things settle...
  163. wait_ms(USB_SUSPEND_WAKEUP_DELAY);
  164. # endif
  165. }
  166. }
  167. /* Woken up */
  168. }
  169. #endif
  170. }
  171. void protocol_post_task(void) {
  172. #ifdef VIRTSER_ENABLE
  173. virtser_task();
  174. #endif
  175. usb_idle_task();
  176. }