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.cpp 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <avr/io.h>
  2. #include <avr/wdt.h>
  3. #include <avr/power.h>
  4. #include <util/delay.h>
  5. // USB HID host
  6. #include "Usb.h"
  7. #include "hid.h"
  8. #include "hidboot.h"
  9. #include "parser.h"
  10. #include "usbhub.h"
  11. // LUFA
  12. #include "lufa.h"
  13. #include "timer.h"
  14. #include "sendchar.h"
  15. #include "debug.h"
  16. #include "keyboard.h"
  17. /* LED ping configuration */
  18. #define TMK_LED
  19. //#define LEONARDO_LED
  20. #if defined(TMK_LED)
  21. // For TMK converter and Teensy
  22. #define LED_TX_INIT (DDRD |= (1<<6))
  23. #define LED_TX_ON (PORTD |= (1<<6))
  24. #define LED_TX_OFF (PORTD &= ~(1<<6))
  25. #define LED_TX_TOGGLE (PORTD ^= (1<<6))
  26. #elif defined(LEONARDO_LED)
  27. // For Leonardo(TX LED)
  28. #define LED_TX_INIT (DDRD |= (1<<5))
  29. #define LED_TX_ON (PORTD &= ~(1<<5))
  30. #define LED_TX_OFF (PORTD |= (1<<5))
  31. #define LED_TX_TOGGLE (PORTD ^= (1<<5))
  32. #else
  33. #define LED_TX_INIT
  34. #define LED_TX_ON
  35. #define LED_TX_OFF
  36. #define LED_TX_TOGGLE
  37. #endif
  38. static USB usb_host;
  39. static HIDBoot<HID_PROTOCOL_KEYBOARD> kbd(&usb_host);
  40. static KBDReportParser kbd_parser;
  41. static USBHub hub1(&usb_host); // one hub is enough for HHKB pro2
  42. /* may be needed for other device with more hub
  43. static USBHub hub2(&usb_host);
  44. static USBHub hub3(&usb_host);
  45. static USBHub hub4(&usb_host);
  46. static USBHub hub5(&usb_host);
  47. static USBHub hub6(&usb_host);
  48. static USBHub hub7(&usb_host);
  49. */
  50. static void LUFA_setup(void)
  51. {
  52. /* Disable watchdog if enabled by bootloader/fuses */
  53. MCUSR &= ~(1 << WDRF);
  54. wdt_disable();
  55. /* Disable clock division */
  56. clock_prescale_set(clock_div_1);
  57. // Leonardo needs. Without this USB device is not recognized.
  58. USB_Disable();
  59. USB_Init();
  60. // for Console_Task
  61. USB_Device_EnableSOFEvents();
  62. print_set_sendchar(sendchar);
  63. }
  64. static void HID_setup()
  65. {
  66. if (usb_host.Init() == -1) {
  67. LED_TX_OFF;
  68. }
  69. _delay_ms(200);
  70. kbd.SetReportParser(0, (HIDReportParser*)&kbd_parser);
  71. }
  72. int main(void)
  73. {
  74. // LED for debug
  75. LED_TX_INIT;
  76. LED_TX_ON;
  77. debug_enable = true;
  78. host_set_driver(&lufa_driver);
  79. keyboard_init();
  80. LUFA_setup();
  81. HID_setup();
  82. /* NOTE: Don't insert time consuming job here.
  83. * It'll cause unclear initialization failure when DFU reset(worm start).
  84. */
  85. sei();
  86. // wait for startup of sendchar routine
  87. while (USB_DeviceState != DEVICE_STATE_Configured) ;
  88. if (debug_enable) {
  89. _delay_ms(1000);
  90. }
  91. debug("init: done\n");
  92. uint16_t timer;
  93. for (;;) {
  94. keyboard_task();
  95. timer = timer_read();
  96. usb_host.Task();
  97. timer = timer_elapsed(timer);
  98. if (timer > 100) {
  99. debug("host.Task: "); debug_hex16(timer); debug("\n");
  100. }
  101. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  102. // LUFA Task for control request
  103. USB_USBTask();
  104. #endif
  105. }
  106. return 0;
  107. }