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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. print_enable = true;
  47. debug_enable = true;
  48. debug_matrix = true;
  49. debug_keyboard = true;
  50. debug_mouse = true;
  51. host_set_driver(&lufa_driver);
  52. keyboard_init();
  53. LUFA_setup();
  54. sei();
  55. uint8_t ret;
  56. // wait for startup of sendchar routine
  57. while (USB_DeviceState != DEVICE_STATE_Configured) ;
  58. if (debug_enable) {
  59. _delay_ms(1000);
  60. }
  61. debug("init: start\n");
  62. HID_setup();
  63. debug("init: done\n");
  64. uint16_t timer;
  65. // to see loop pulse with oscillo scope
  66. DDRF = (1<<7);
  67. for (;;) {
  68. PORTF ^= (1<<7);
  69. keyboard_proc();
  70. timer = timer_read();
  71. usb_host.Task();
  72. timer = timer_elapsed(timer);
  73. if (timer > 100) {
  74. debug("host.Task: "); debug_hex16(timer); debug("\n");
  75. }
  76. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  77. // LUFA Task for control request
  78. USB_USBTask();
  79. #endif
  80. }
  81. return 0;
  82. }