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

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