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.

100 lines
2.1 KiB

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