chibios.c 5.5 KB

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