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