You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

main.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * (c) 2015 flabberast <[email protected]>
  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. #ifdef SLEEP_LED_ENABLE
  32. #include "sleep_led.h"
  33. #endif
  34. #include "suspend.h"
  35. #include "hook.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. /* Default hooks definitions. */
  55. __attribute__((weak))
  56. void hook_early_init(void) {}
  57. __attribute__((weak))
  58. void hook_late_init(void) {}
  59. __attribute__((weak))
  60. void hook_usb_suspend_loop(void) {
  61. /* Do this in the suspended state */
  62. suspend_power_down(); // on AVR this deep sleeps for 15ms
  63. /* Remote wakeup */
  64. if((USB_DRIVER.status & 2) && suspend_wakeup_condition()) {
  65. send_remote_wakeup(&USB_DRIVER);
  66. }
  67. }
  68. /* TESTING
  69. * Amber LED blinker thread, times are in milliseconds.
  70. */
  71. /* set this variable to non-zero anywhere to blink once */
  72. // uint8_t blinkLed = 0;
  73. // static THD_WORKING_AREA(waBlinkerThread, 128);
  74. // static THD_FUNCTION(blinkerThread, arg) {
  75. // (void)arg;
  76. // chRegSetThreadName("blinkOrange");
  77. // while(true) {
  78. // if(blinkLed) {
  79. // blinkLed = 0;
  80. // palSetPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13);
  81. // chThdSleepMilliseconds(100);
  82. // palClearPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13);
  83. // }
  84. // chThdSleepMilliseconds(100);
  85. // }
  86. // }
  87. /* Main thread
  88. */
  89. int main(void) {
  90. /* ChibiOS/RT init */
  91. halInit();
  92. chSysInit();
  93. // TESTING
  94. // chThdCreateStatic(waBlinkerThread, sizeof(waBlinkerThread), NORMALPRIO, blinkerThread, NULL);
  95. hook_early_init();
  96. /* Init USB */
  97. init_usb_driver(&USB_DRIVER);
  98. /* init printf */
  99. init_printf(NULL,sendchar_pf);
  100. /* Wait until the USB is active */
  101. while(USB_DRIVER.state != USB_ACTIVE)
  102. chThdSleepMilliseconds(50);
  103. /* Do need to wait here!
  104. * Otherwise the next print might start a transfer on console EP
  105. * before the USB is completely ready, which sometimes causes
  106. * HardFaults.
  107. */
  108. chThdSleepMilliseconds(50);
  109. print("USB configured.\n");
  110. /* init TMK modules */
  111. keyboard_init();
  112. host_set_driver(&chibios_driver);
  113. #ifdef SLEEP_LED_ENABLE
  114. sleep_led_init();
  115. #endif
  116. print("Keyboard start.\n");
  117. hook_late_init();
  118. /* Main loop */
  119. while(true) {
  120. if(USB_DRIVER.state == USB_SUSPENDED) {
  121. print("[s]");
  122. while(USB_DRIVER.state == USB_SUSPENDED) {
  123. hook_usb_suspend_loop();
  124. }
  125. /* Woken up */
  126. // variables have been already cleared
  127. send_keyboard_report();
  128. #ifdef MOUSEKEY_ENABLE
  129. mousekey_send();
  130. #endif /* MOUSEKEY_ENABLE */
  131. }
  132. keyboard_task();
  133. }
  134. }