]> git.donarmstrong.com Git - qmk_firmware.git/blob - converter/usb_usb/main.cpp
usb_usb: Fix initialize procedure
[qmk_firmware.git] / converter / usb_usb / main.cpp
1 #include <avr/io.h>
2 #include <avr/wdt.h>
3 #include <avr/power.h>
4 #include <util/delay.h>
5
6 // USB HID host
7 #include "Usb.h"
8 #include "hid.h"
9 #include "hidboot.h"
10 #include "parser.h"
11 #include "usbhub.h"
12
13 // LUFA
14 #include "lufa.h"
15
16 #include "timer.h"
17 #include "sendchar.h"
18 #include "debug.h"
19 #include "keyboard.h"
20
21 #include "leonardo_led.h"
22
23
24 static USB     usb_host;
25 static HIDBoot<HID_PROTOCOL_KEYBOARD>    kbd(&usb_host);
26 static KBDReportParser kbd_parser;
27 static USBHub hub1(&usb_host);  // one hub is enough for HHKB pro2
28 /* may be needed  for other device with more hub
29 static USBHub hub2(&usb_host);
30 static USBHub hub3(&usb_host);
31 static USBHub hub4(&usb_host);
32 static USBHub hub5(&usb_host);
33 static USBHub hub6(&usb_host);
34 static USBHub hub7(&usb_host);
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     clock_prescale_set(clock_div_1);
45
46     // Leonardo needs. Without this USB device is not recognized.
47     USB_Disable();
48
49     USB_Init();
50
51     // for Console_Task
52     USB_Device_EnableSOFEvents();
53     print_set_sendchar(sendchar);
54 }
55
56 static void HID_setup()
57 {
58     if (usb_host.Init() == -1) {
59         LED_TX_OFF;
60     }
61
62     _delay_ms(200);
63
64     kbd.SetReportParser(0, (HIDReportParser*)&kbd_parser);
65 }
66
67 int main(void)
68 {
69     // LED for debug
70     LED_TX_INIT;
71     LED_TX_ON;
72
73     debug_enable = true;
74
75     host_set_driver(&lufa_driver);
76     keyboard_init();
77
78     LUFA_setup();
79     HID_setup();
80     /* NOTE: Don't insert time consuming job here.
81      * It'll cause unclear initialization failure when DFU reset(worm start).
82      */
83     sei();
84
85     // wait for startup of sendchar routine
86     while (USB_DeviceState != DEVICE_STATE_Configured) ;
87     if (debug_enable) {
88         _delay_ms(1000);
89     }
90
91     debug("init: done\n");
92
93 uint16_t timer;
94     for (;;) {
95         keyboard_task();
96
97 timer = timer_read();
98         usb_host.Task();
99 timer = timer_elapsed(timer);
100 if (timer > 100) {
101     debug("host.Task: "); debug_hex16(timer);  debug("\n");
102 }
103
104 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
105         // LUFA Task for control request
106         USB_USBTask();
107 #endif
108     }
109
110     return 0;
111 }