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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. debug("HID init: failed\n");
  48. LED_TX_OFF;
  49. }
  50. _delay_ms(200);
  51. kbd.SetReportParser(0, (HIDReportParser*)&kbd_parser);
  52. }
  53. int main(void)
  54. {
  55. // LED for debug
  56. LED_TX_INIT;
  57. LED_TX_ON;
  58. debug_enable = true;
  59. debug_matrix = true;
  60. debug_keyboard = true;
  61. debug_mouse = true;
  62. host_set_driver(&lufa_driver);
  63. keyboard_init();
  64. LUFA_setup();
  65. sei();
  66. uint8_t ret;
  67. // wait for startup of sendchar routine
  68. while (USB_DeviceState != DEVICE_STATE_Configured) ;
  69. if (debug_enable) {
  70. _delay_ms(1000);
  71. }
  72. debug("init: start\n");
  73. HID_setup();
  74. debug("init: done\n");
  75. uint16_t timer;
  76. // to see loop pulse with oscillo scope
  77. DDRF = (1<<7);
  78. for (;;) {
  79. PORTF ^= (1<<7);
  80. keyboard_task();
  81. timer = timer_read();
  82. usb_host.Task();
  83. timer = timer_elapsed(timer);
  84. if (timer > 100) {
  85. debug("host.Task: "); debug_hex16(timer); debug("\n");
  86. }
  87. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  88. // LUFA Task for control request
  89. USB_USBTask();
  90. #endif
  91. }
  92. return 0;
  93. }