main.c 3.3 KB

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