]> git.donarmstrong.com Git - tmk_firmware.git/blob - converter/usb_usb/main.cpp
0b536b05aa80492bb8c60a8bb4570b70843332d0
[tmk_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         debug("HID init: failed\n");
60         LED_TX_OFF;
61     }
62   
63     _delay_ms(200);
64       
65     kbd.SetReportParser(0, (HIDReportParser*)&kbd_parser);
66 }
67
68 int main(void)
69 {
70     // LED for debug
71     LED_TX_INIT;
72     LED_TX_ON;
73
74     debug_enable = true;
75     debug_matrix = true;
76     debug_keyboard = true;
77     debug_mouse = true;
78
79     host_set_driver(&lufa_driver);
80     keyboard_init();
81
82     LUFA_setup();
83     sei();
84
85 uint8_t ret;
86     // wait for startup of sendchar routine
87     while (USB_DeviceState != DEVICE_STATE_Configured) ;
88     if (debug_enable) {
89         _delay_ms(1000);
90     }
91
92     debug("init: start\n");
93     HID_setup();
94     
95     debug("init: done\n");
96
97 uint16_t timer;
98 // to see loop pulse with oscillo scope
99 DDRF = (1<<7);
100     for (;;) {
101 PORTF ^= (1<<7);
102         keyboard_task();
103
104 timer = timer_read();
105         usb_host.Task();
106 timer = timer_elapsed(timer);
107 if (timer > 100) {
108     debug("host.Task: "); debug_hex16(timer);  debug("\n");
109 }
110
111 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
112         // LUFA Task for control request
113         USB_USBTask();
114 #endif
115     }
116         
117     return 0;
118 }