chibios.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_programmable_button(uint32_t data);
  57. void send_digitizer(report_digitizer_t *report);
  58. /* host struct */
  59. host_driver_t chibios_driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer, send_programmable_button};
  60. #ifdef VIRTSER_ENABLE
  61. void virtser_task(void);
  62. #endif
  63. #ifdef RAW_ENABLE
  64. void raw_hid_task(void);
  65. #endif
  66. #ifdef CONSOLE_ENABLE
  67. void console_task(void);
  68. #endif
  69. #ifdef MIDI_ENABLE
  70. void midi_ep_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. static host_driver_t *driver = NULL;
  120. void protocol_pre_init(void) {
  121. /* Init USB */
  122. usb_event_queue_init();
  123. init_usb_driver(&USB_DRIVER);
  124. #ifdef MIDI_ENABLE
  125. setup_midi();
  126. #endif
  127. /* Wait until USB is active */
  128. while (true) {
  129. #if defined(WAIT_FOR_USB)
  130. if (USB_DRIVER.state == USB_ACTIVE) {
  131. driver = &chibios_driver;
  132. break;
  133. }
  134. #else
  135. driver = &chibios_driver;
  136. break;
  137. #endif
  138. wait_ms(50);
  139. }
  140. /* Do need to wait here!
  141. * Otherwise the next print might start a transfer on console EP
  142. * before the USB is completely ready, which sometimes causes
  143. * HardFaults.
  144. */
  145. wait_ms(50);
  146. print("USB configured.\n");
  147. }
  148. void protocol_post_init(void) {
  149. host_set_driver(driver);
  150. }
  151. void protocol_pre_task(void) {
  152. usb_event_queue_task();
  153. #if !defined(NO_USB_STARTUP_CHECK)
  154. if (USB_DRIVER.state == USB_SUSPENDED) {
  155. print("[s]");
  156. while (USB_DRIVER.state == USB_SUSPENDED) {
  157. /* Do this in the suspended state */
  158. suspend_power_down(); // on AVR this deep sleeps for 15ms
  159. /* Remote wakeup */
  160. if (suspend_wakeup_condition()) {
  161. usbWakeupHost(&USB_DRIVER);
  162. restart_usb_driver(&USB_DRIVER);
  163. }
  164. }
  165. /* Woken up */
  166. // variables has been already cleared by the wakeup hook
  167. send_keyboard_report();
  168. # ifdef MOUSEKEY_ENABLE
  169. mousekey_send();
  170. # endif /* MOUSEKEY_ENABLE */
  171. }
  172. #endif
  173. }
  174. void protocol_post_task(void) {
  175. #ifdef CONSOLE_ENABLE
  176. console_task();
  177. #endif
  178. #ifdef MIDI_ENABLE
  179. midi_ep_task();
  180. #endif
  181. #ifdef VIRTSER_ENABLE
  182. virtser_task();
  183. #endif
  184. #ifdef RAW_ENABLE
  185. raw_hid_task();
  186. #endif
  187. }