logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

chibios.c (5573B)


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