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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #include "leonardo_led.h"
  18. static USB usb_host;
  19. static HIDBoot<HID_PROTOCOL_KEYBOARD> kbd(&usb_host);
  20. static KBDReportParser kbd_parser;
  21. static USBHub hub1(&usb_host); // one hub is enough for HHKB pro2
  22. /* may be needed for other device with more hub
  23. static USBHub hub2(&usb_host);
  24. static USBHub hub3(&usb_host);
  25. static USBHub hub4(&usb_host);
  26. static USBHub hub5(&usb_host);
  27. static USBHub hub6(&usb_host);
  28. static USBHub hub7(&usb_host);
  29. */
  30. static void LUFA_setup(void)
  31. {
  32. /* Disable watchdog if enabled by bootloader/fuses */
  33. MCUSR &= ~(1 << WDRF);
  34. wdt_disable();
  35. /* Disable clock division */
  36. clock_prescale_set(clock_div_1);
  37. // Leonardo needs. Without this USB device is not recognized.
  38. USB_Disable();
  39. USB_Init();
  40. // for Console_Task
  41. USB_Device_EnableSOFEvents();
  42. print_set_sendchar(sendchar);
  43. }
  44. static void HID_setup()
  45. {
  46. if (usb_host.Init() == -1) {
  47. LED_TX_OFF;
  48. }
  49. _delay_ms(200);
  50. kbd.SetReportParser(0, (HIDReportParser*)&kbd_parser);
  51. }
  52. int main(void)
  53. {
  54. // LED for debug
  55. LED_TX_INIT;
  56. LED_TX_ON;
  57. debug_enable = true;
  58. host_set_driver(&lufa_driver);
  59. keyboard_init();
  60. LUFA_setup();
  61. HID_setup();
  62. /* NOTE: Don't insert time consuming job here.
  63. * It'll cause unclear initialization failure when DFU reset(worm start).
  64. */
  65. sei();
  66. // wait for startup of sendchar routine
  67. while (USB_DeviceState != DEVICE_STATE_Configured) ;
  68. if (debug_enable) {
  69. _delay_ms(1000);
  70. }
  71. debug("init: done\n");
  72. uint16_t timer;
  73. for (;;) {
  74. keyboard_task();
  75. timer = timer_read();
  76. usb_host.Task();
  77. timer = timer_elapsed(timer);
  78. if (timer > 100) {
  79. debug("host.Task: "); debug_hex16(timer); debug("\n");
  80. }
  81. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  82. // LUFA Task for control request
  83. USB_USBTask();
  84. #endif
  85. }
  86. return 0;
  87. }