Keyboard firmwares for Atmel AVR and Cortex-M
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.

main.c 3.3KB

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