]> git.donarmstrong.com Git - tmk_firmware.git/blob - converter/usb_usb/main.cpp
20b7af66a25a56b61f2db2f320a9383e3a872aaf
[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
12 // LUFA
13 #include "lufa.h"
14
15 #include "timer.h"
16 #include "debug.h"
17 #include "keyboard.h"
18
19 #include "leonardo_led.h"
20
21
22 static USB     usb_host;
23 static HIDBoot<HID_PROTOCOL_KEYBOARD>    kbd(&usb_host);
24 static KBDReportParser kbd_parser;
25
26 static void LUFA_setup(void)
27 {
28     /* Disable watchdog if enabled by bootloader/fuses */
29     MCUSR &= ~(1 << WDRF);
30     wdt_disable();
31
32     /* Disable clock division */
33     clock_prescale_set(clock_div_1);
34
35     // Leonardo needs. Without this USB device is not recognized.
36     USB_Disable();
37
38     USB_Init();
39
40     // for Console_Task
41     USB_Device_EnableSOFEvents();
42 }
43
44 static void HID_setup()
45 {
46     if (usb_host.Init() == -1) {
47         debug("HID init: failed\n");
48         LED_TX_OFF;
49     }
50   
51     _delay_ms(200);
52       
53     kbd.SetReportParser(0, (HIDReportParser*)&kbd_parser);
54 }
55
56 int main(void)
57 {
58     // LED for debug
59     LED_TX_INIT;
60     LED_TX_ON;
61
62     debug_enable = true;
63     debug_matrix = true;
64     debug_keyboard = true;
65     debug_mouse = true;
66
67     host_set_driver(&lufa_driver);
68     keyboard_init();
69
70     LUFA_setup();
71     sei();
72
73 uint8_t ret;
74     // wait for startup of sendchar routine
75     while (USB_DeviceState != DEVICE_STATE_Configured) ;
76     if (debug_enable) {
77         _delay_ms(1000);
78     }
79
80     debug("init: start\n");
81     HID_setup();
82     
83     debug("init: done\n");
84
85 uint16_t timer;
86 // to see loop pulse with oscillo scope
87 DDRF = (1<<7);
88     for (;;) {
89 PORTF ^= (1<<7);
90         keyboard_task();
91
92 timer = timer_read();
93         usb_host.Task();
94 timer = timer_elapsed(timer);
95 if (timer > 100) {
96     debug("host.Task: "); debug_hex16(timer);  debug("\n");
97 }
98
99 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
100         // LUFA Task for control request
101         USB_USBTask();
102 #endif
103     }
104         
105     return 0;
106 }