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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

main.cpp 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <avr/io.h>
  2. #include <avr/wdt.h>
  3. #include <avr/power.h>
  4. #include <util/delay.h>
  5. // LUFA
  6. #include "lufa.h"
  7. #include "sendchar.h"
  8. #include "debug.h"
  9. #include "keyboard.h"
  10. /* LED ping configuration */
  11. //#define TMK_LED
  12. //#define LEONARDO_LED
  13. #if defined(TMK_LED)
  14. // For TMK converter and Teensy
  15. #define LED_TX_INIT (DDRD |= (1<<6))
  16. #define LED_TX_ON (PORTD |= (1<<6))
  17. #define LED_TX_OFF (PORTD &= ~(1<<6))
  18. #define LED_TX_TOGGLE (PORTD ^= (1<<6))
  19. #elif defined(LEONARDO_LED)
  20. // For Leonardo(TX LED)
  21. #define LED_TX_INIT (DDRD |= (1<<5))
  22. #define LED_TX_ON (PORTD &= ~(1<<5))
  23. #define LED_TX_OFF (PORTD |= (1<<5))
  24. #define LED_TX_TOGGLE (PORTD ^= (1<<5))
  25. #else
  26. #define LED_TX_INIT
  27. #define LED_TX_ON
  28. #define LED_TX_OFF
  29. #define LED_TX_TOGGLE
  30. #endif
  31. static void LUFA_setup(void)
  32. {
  33. /* Disable watchdog if enabled by bootloader/fuses */
  34. MCUSR &= ~(1 << WDRF);
  35. wdt_disable();
  36. /* Disable clock division */
  37. clock_prescale_set(clock_div_1);
  38. // Leonardo needs. Without this USB device is not recognized.
  39. USB_Disable();
  40. USB_Init();
  41. // for Console_Task
  42. USB_Device_EnableSOFEvents();
  43. print_set_sendchar(sendchar);
  44. }
  45. bool kbd_init = false;
  46. int main(void)
  47. {
  48. // LED for debug
  49. LED_TX_INIT;
  50. LED_TX_ON;
  51. debug_enable = true;
  52. debug_keyboard = true;
  53. host_set_driver(&lufa_driver);
  54. keyboard_init();
  55. LUFA_setup();
  56. /* NOTE: Don't insert time consuming job here.
  57. * It'll cause unclear initialization failure when DFU reset(worm start).
  58. */
  59. sei();
  60. // wait for startup of sendchar routine
  61. while (USB_DeviceState != DEVICE_STATE_Configured) ;
  62. if (debug_enable) {
  63. _delay_ms(1000);
  64. }
  65. kbd_init = true;
  66. debug("init: done\n");
  67. for (;;) {
  68. keyboard_task();
  69. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  70. // LUFA Task for control request
  71. USB_USBTask();
  72. #endif
  73. }
  74. return 0;
  75. }