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