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 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // LUFA
  11. #include "lufa.h"
  12. #include "timer.h"
  13. #include "debug.h"
  14. #include "keyboard.h"
  15. #include "leonardo_led.h"
  16. static USB usb_host;
  17. static HIDBoot<HID_PROTOCOL_KEYBOARD> kbd(&usb_host);
  18. static KBDReportParser kbd_parser;
  19. static void LUFA_setup(void)
  20. {
  21. /* Disable watchdog if enabled by bootloader/fuses */
  22. MCUSR &= ~(1 << WDRF);
  23. wdt_disable();
  24. /* Disable clock division */
  25. clock_prescale_set(clock_div_1);
  26. // Leonardo needs. Without this USB device is not recognized.
  27. USB_Disable();
  28. USB_Init();
  29. // for Console_Task
  30. USB_Device_EnableSOFEvents();
  31. }
  32. static void HID_setup()
  33. {
  34. if (usb_host.Init() == -1) {
  35. debug("HID init: failed\n");
  36. LED_TX_OFF;
  37. }
  38. _delay_ms(200);
  39. kbd.SetReportParser(0, (HIDReportParser*)&kbd_parser);
  40. }
  41. int main(void)
  42. {
  43. // LED for debug
  44. LED_TX_INIT;
  45. LED_TX_ON;
  46. debug_enable = true;
  47. debug_matrix = true;
  48. debug_keyboard = true;
  49. debug_mouse = true;
  50. host_set_driver(&lufa_driver);
  51. keyboard_init();
  52. LUFA_setup();
  53. sei();
  54. uint8_t ret;
  55. // wait for startup of sendchar routine
  56. while (USB_DeviceState != DEVICE_STATE_Configured) ;
  57. if (debug_enable) {
  58. _delay_ms(1000);
  59. }
  60. debug("init: start\n");
  61. HID_setup();
  62. debug("init: done\n");
  63. uint16_t timer;
  64. // to see loop pulse with oscillo scope
  65. DDRF = (1<<7);
  66. for (;;) {
  67. PORTF ^= (1<<7);
  68. keyboard_task();
  69. timer = timer_read();
  70. usb_host.Task();
  71. timer = timer_elapsed(timer);
  72. if (timer > 100) {
  73. debug("host.Task: "); debug_hex16(timer); debug("\n");
  74. }
  75. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  76. // LUFA Task for control request
  77. USB_USBTask();
  78. #endif
  79. }
  80. return 0;
  81. }