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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "usbhub.h"
  8. #include "hid.h"
  9. #include "hidboot.h"
  10. #include "parser.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 "led.h"
  18. /* LED ping configuration */
  19. #define TMK_LED
  20. //#define LEONARDO_LED
  21. #if defined(TMK_LED)
  22. // For TMK converter and Teensy
  23. #define LED_TX_INIT (DDRD |= (1<<6))
  24. #define LED_TX_ON (PORTD |= (1<<6))
  25. #define LED_TX_OFF (PORTD &= ~(1<<6))
  26. #define LED_TX_TOGGLE (PORTD ^= (1<<6))
  27. #elif defined(LEONARDO_LED)
  28. // For Leonardo(TX LED)
  29. #define LED_TX_INIT (DDRD |= (1<<5))
  30. #define LED_TX_ON (PORTD &= ~(1<<5))
  31. #define LED_TX_OFF (PORTD |= (1<<5))
  32. #define LED_TX_TOGGLE (PORTD ^= (1<<5))
  33. #else
  34. #define LED_TX_INIT
  35. #define LED_TX_ON
  36. #define LED_TX_OFF
  37. #define LED_TX_TOGGLE
  38. #endif
  39. static void LUFA_setup(void)
  40. {
  41. /* Disable watchdog if enabled by bootloader/fuses */
  42. MCUSR &= ~(1 << WDRF);
  43. wdt_disable();
  44. /* Disable clock division */
  45. clock_prescale_set(clock_div_1);
  46. // Leonardo needs. Without this USB device is not recognized.
  47. USB_Disable();
  48. USB_Init();
  49. // for Console_Task
  50. USB_Device_EnableSOFEvents();
  51. print_set_sendchar(sendchar);
  52. }
  53. /*
  54. * USB Host Shield HID keyboard
  55. */
  56. USB usb_host;
  57. USBHub hub1(&usb_host);
  58. HIDBoot<HID_PROTOCOL_KEYBOARD> kbd(&usb_host);
  59. KBDReportParser kbd_parser;
  60. void led_set(uint8_t usb_led)
  61. {
  62. kbd.SetReport(0, 0, 2, 0, 1, &usb_led);
  63. }
  64. int main(void)
  65. {
  66. // LED for debug
  67. LED_TX_INIT;
  68. LED_TX_ON;
  69. debug_enable = true;
  70. debug_keyboard = true;
  71. host_set_driver(&lufa_driver);
  72. keyboard_init();
  73. LUFA_setup();
  74. // USB Host Shield setup
  75. usb_host.Init();
  76. kbd.SetReportParser(0, (HIDReportParser*)&kbd_parser);
  77. /* NOTE: Don't insert time consuming job here.
  78. * It'll cause unclear initialization failure when DFU reset(worm start).
  79. */
  80. sei();
  81. // wait for startup of sendchar routine
  82. while (USB_DeviceState != DEVICE_STATE_Configured) ;
  83. if (debug_enable) {
  84. _delay_ms(1000);
  85. }
  86. debug("init: done\n");
  87. uint16_t timer;
  88. for (;;) {
  89. keyboard_task();
  90. timer = timer_read();
  91. usb_host.Task();
  92. timer = timer_elapsed(timer);
  93. if (timer > 100) {
  94. debug("host.Task: "); debug_hex16(timer); debug("\n");
  95. }
  96. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  97. // LUFA Task for control request
  98. USB_USBTask();
  99. #endif
  100. }
  101. return 0;
  102. }