main.c 3.9 KB

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