chibios.c 5.2 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_extra(report_extra_t *report);
  55. /* host struct */
  56. host_driver_t chibios_driver = {keyboard_leds, send_keyboard, send_mouse, send_extra};
  57. #ifdef VIRTSER_ENABLE
  58. void virtser_task(void);
  59. #endif
  60. #ifdef RAW_ENABLE
  61. void raw_hid_task(void);
  62. #endif
  63. #ifdef CONSOLE_ENABLE
  64. void console_task(void);
  65. #endif
  66. #ifdef MIDI_ENABLE
  67. void midi_ep_task(void);
  68. #endif
  69. /* TESTING
  70. * Amber LED blinker thread, times are in milliseconds.
  71. */
  72. /* set this variable to non-zero anywhere to blink once */
  73. // static THD_WORKING_AREA(waThread1, 128);
  74. // static THD_FUNCTION(Thread1, arg) {
  75. // (void)arg;
  76. // chRegSetThreadName("blinker");
  77. // while (true) {
  78. // systime_t time;
  79. // time = USB_DRIVER.state == USB_ACTIVE ? 250 : 500;
  80. // palClearLine(LINE_CAPS_LOCK);
  81. // chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
  82. // palSetLine(LINE_CAPS_LOCK);
  83. // chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
  84. // }
  85. // }
  86. /* Early initialisation
  87. */
  88. __attribute__((weak)) void early_hardware_init_pre(void) {
  89. #if EARLY_INIT_PERFORM_BOOTLOADER_JUMP
  90. void enter_bootloader_mode_if_requested(void);
  91. enter_bootloader_mode_if_requested();
  92. #endif // EARLY_INIT_PERFORM_BOOTLOADER_JUMP
  93. }
  94. __attribute__((weak)) void early_hardware_init_post(void) {}
  95. __attribute__((weak)) void board_init(void) {}
  96. // This overrides what's normally in ChibiOS board definitions
  97. void __early_init(void) {
  98. early_hardware_init_pre();
  99. // This is the renamed equivalent of __early_init in the board.c file
  100. void __chibios_override___early_init(void);
  101. __chibios_override___early_init();
  102. early_hardware_init_post();
  103. }
  104. // This overrides what's normally in ChibiOS board definitions
  105. void boardInit(void) {
  106. // This is the renamed equivalent of boardInit in the board.c file
  107. void __chibios_override_boardInit(void);
  108. __chibios_override_boardInit();
  109. board_init();
  110. }
  111. void protocol_setup(void) {
  112. usb_device_state_init();
  113. // TESTING
  114. // chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  115. }
  116. static host_driver_t *driver = NULL;
  117. void protocol_pre_init(void) {
  118. /* Init USB */
  119. usb_event_queue_init();
  120. init_usb_driver(&USB_DRIVER);
  121. #ifdef MIDI_ENABLE
  122. setup_midi();
  123. #endif
  124. /* Wait until USB is active */
  125. while (true) {
  126. #if defined(WAIT_FOR_USB)
  127. if (USB_DRIVER.state == USB_ACTIVE) {
  128. driver = &chibios_driver;
  129. break;
  130. }
  131. #else
  132. driver = &chibios_driver;
  133. break;
  134. #endif
  135. wait_ms(50);
  136. }
  137. /* Do need to wait here!
  138. * Otherwise the next print might start a transfer on console EP
  139. * before the USB is completely ready, which sometimes causes
  140. * HardFaults.
  141. */
  142. wait_ms(50);
  143. print("USB configured.\n");
  144. }
  145. void protocol_post_init(void) {
  146. host_set_driver(driver);
  147. }
  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. }