main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "mousekey.h"
  28. #include "led.h"
  29. #include "sendchar.h"
  30. #include "debug.h"
  31. #include "printf.h"
  32. #ifdef SLEEP_LED_ENABLE
  33. #include "sleep_led.h"
  34. #endif
  35. #ifdef SERIAL_LINK_ENABLE
  36. #include "serial_link/system/serial_link.h"
  37. #endif
  38. #ifdef VISUALIZER_ENABLE
  39. #include "visualizer/visualizer.h"
  40. #endif
  41. #include "suspend.h"
  42. #include "wait.h"
  43. /* -------------------------
  44. * TMK host driver defs
  45. * -------------------------
  46. */
  47. /* declarations */
  48. uint8_t keyboard_leds(void);
  49. void send_keyboard(report_keyboard_t *report);
  50. void send_mouse(report_mouse_t *report);
  51. void send_system(uint16_t data);
  52. void send_consumer(uint16_t data);
  53. /* host struct */
  54. host_driver_t chibios_driver = {
  55. keyboard_leds,
  56. send_keyboard,
  57. send_mouse,
  58. send_system,
  59. send_consumer
  60. };
  61. /* TESTING
  62. * Amber LED blinker thread, times are in milliseconds.
  63. */
  64. /* set this variable to non-zero anywhere to blink once */
  65. // static THD_WORKING_AREA(waThread1, 128);
  66. // static THD_FUNCTION(Thread1, arg) {
  67. // (void)arg;
  68. // chRegSetThreadName("blinker");
  69. // while (true) {
  70. // systime_t time;
  71. // time = USB_DRIVER.state == USB_ACTIVE ? 250 : 500;
  72. // palClearLine(LINE_CAPS_LOCK);
  73. // chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
  74. // palSetLine(LINE_CAPS_LOCK);
  75. // chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
  76. // }
  77. // }
  78. /* Main thread
  79. */
  80. int main(void) {
  81. /* ChibiOS/RT init */
  82. halInit();
  83. chSysInit();
  84. // TESTING
  85. // chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  86. /* Init USB */
  87. init_usb_driver(&USB_DRIVER);
  88. /* init printf */
  89. init_printf(NULL,sendchar_pf);
  90. #ifdef SERIAL_LINK_ENABLE
  91. init_serial_link();
  92. #endif
  93. #ifdef VISUALIZER_ENABLE
  94. visualizer_init();
  95. #endif
  96. host_driver_t* driver = NULL;
  97. /* Wait until the USB or serial link is active */
  98. while (true) {
  99. if(USB_DRIVER.state == USB_ACTIVE) {
  100. driver = &chibios_driver;
  101. break;
  102. }
  103. #ifdef SERIAL_LINK_ENABLE
  104. if(is_serial_link_connected()) {
  105. driver = get_serial_link_driver();
  106. break;
  107. }
  108. serial_link_update();
  109. #endif
  110. wait_ms(50);
  111. }
  112. /* Do need to wait here!
  113. * Otherwise the next print might start a transfer on console EP
  114. * before the USB is completely ready, which sometimes causes
  115. * HardFaults.
  116. */
  117. wait_ms(50);
  118. print("USB configured.\n");
  119. /* init TMK modules */
  120. keyboard_init();
  121. host_set_driver(driver);
  122. #ifdef SLEEP_LED_ENABLE
  123. sleep_led_init();
  124. #endif
  125. print("Keyboard start.\n");
  126. /* Main loop */
  127. while(true) {
  128. if(USB_DRIVER.state == USB_SUSPENDED) {
  129. print("[s]");
  130. #ifdef VISUALIZER_ENABLE
  131. visualizer_suspend();
  132. #endif
  133. while(USB_DRIVER.state == USB_SUSPENDED) {
  134. /* Do this in the suspended state */
  135. #ifdef SERIAL_LINK_ENABLE
  136. serial_link_update();
  137. #endif
  138. suspend_power_down(); // on AVR this deep sleeps for 15ms
  139. /* Remote wakeup */
  140. if(suspend_wakeup_condition()) {
  141. usbWakeupHost(&USB_DRIVER);
  142. }
  143. }
  144. /* Woken up */
  145. // variables has been already cleared by the wakeup hook
  146. send_keyboard_report();
  147. #ifdef MOUSEKEY_ENABLE
  148. mousekey_send();
  149. #endif /* MOUSEKEY_ENABLE */
  150. #ifdef VISUALIZER_ENABLE
  151. visualizer_resume();
  152. #endif
  153. }
  154. keyboard_task();
  155. }
  156. }