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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include <avr/io.h>
  2. #include <avr/wdt.h>
  3. #include <avr/power.h>
  4. #include <util/delay.h>
  5. #include <Arduino.h>
  6. // USB HID host
  7. #include "Usb.h"
  8. #include "hid.h"
  9. #include "hidboot.h"
  10. #include "parser.h"
  11. // LUFA
  12. #include "lufa.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. // wait for startup of sendchar routine
  56. while (USB_DeviceState != DEVICE_STATE_Configured) ;
  57. if (debug_enable) {
  58. _delay_ms(1000);
  59. }
  60. HID_setup();
  61. debug("init: done\n");
  62. for (;;) {
  63. keyboard_proc();
  64. usb_host.Task();
  65. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  66. // LUFA Task for control request
  67. USB_USBTask();
  68. #endif
  69. }
  70. return 0;
  71. }