]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/sirius/unigo66/main.c
cleanup
[qmk_firmware.git] / keyboards / sirius / unigo66 / main.c
1 #include <avr/io.h>
2 #include <avr/wdt.h>
3 #include <avr/power.h>
4 #include <util/delay.h>
5
6 // LUFA
7 #include "lufa.h"
8
9 #include "sendchar.h"
10 #include "debug.h"
11 #include "keyboard.h"
12 #include "led.h"
13
14 /* LED ping configuration */
15 #define TMK_LED
16 //#define LEONARDO_LED
17 #if defined(TMK_LED)
18 // For TMK converter and Teensy
19 #define LED_TX_INIT    (DDRD  |=  (1<<6))
20 #define LED_TX_ON      (PORTD |=  (1<<6))
21 #define LED_TX_OFF     (PORTD &= ~(1<<6))
22 #define LED_TX_TOGGLE  (PORTD ^=  (1<<6))
23 #elif defined(LEONARDO_LED)
24 // For Leonardo(TX LED)
25 #define LED_TX_INIT    (DDRD  |=  (1<<5))
26 #define LED_TX_ON      (PORTD &= ~(1<<5))
27 #define LED_TX_OFF     (PORTD |=  (1<<5))
28 #define LED_TX_TOGGLE  (PORTD ^=  (1<<5))
29 #else
30 #define LED_TX_INIT
31 #define LED_TX_ON
32 #define LED_TX_OFF
33 #define LED_TX_TOGGLE
34 #endif
35
36
37 static void LUFA_setup(void)
38 {
39     /* Disable watchdog if enabled by bootloader/fuses */
40     MCUSR &= ~(1 << WDRF);
41     wdt_disable();
42
43     /* Disable clock division */
44 #if (F_CPU == 8000000)
45     clock_prescale_set(clock_div_2);    // 16MHz crystal divided by 2
46 #else
47     clock_prescale_set(clock_div_1);
48 #endif
49
50     // Leonardo needs. Without this USB device is not recognized.
51     USB_Disable();
52
53     USB_Init();
54
55     // for Console_Task
56     USB_Device_EnableSOFEvents();
57     print_set_sendchar(sendchar);
58 }
59
60
61
62 int main(void)
63 {
64     // LED for debug
65     LED_TX_INIT;
66     LED_TX_ON;
67
68     debug_enable = true;
69     debug_keyboard = true;
70
71     host_set_driver(&lufa_driver);
72     keyboard_init();
73
74     LUFA_setup();
75
76     /* NOTE: Don't insert time consuming job here.
77      * It'll cause unclear initialization failure when DFU reset(worm start).
78      */
79     sei();
80
81 /* Some keyboards bootup quickly and cannot be initialized with this startup wait.
82     // wait for startup of sendchar routine
83     while (USB_DeviceState != DEVICE_STATE_Configured) ;
84     if (debug_enable) {
85         _delay_ms(1000);
86     }
87 */
88
89     /* wait for USB startup to get ready for debug output */
90     uint8_t timeout = 255;  // timeout when USB is not available(Bluetooth)
91     while (timeout-- && USB_DeviceState != DEVICE_STATE_Configured) {
92     _delay_ms(4);
93     }
94
95     debug("init: done\n");
96
97     for (;;) {
98         keyboard_task();
99
100 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
101         // LUFA Task for control request
102         USB_USBTask();
103 #endif
104     }
105
106     return 0;
107 }