]> git.gir.st - tmk_keyboard.git/blob - converter/usb_usb/main.cpp
Merge pull request #148 from cub-uanic/patch-1
[tmk_keyboard.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 /*
76 debug_matrix = true;
77 debug_keyboard = true;
78 debug_mouse = true;
79 */
80
81 host_set_driver(&lufa_driver);
82 keyboard_init();
83
84 LUFA_setup();
85 sei();
86
87 uint8_t ret;
88 // wait for startup of sendchar routine
89 while (USB_DeviceState != DEVICE_STATE_Configured) ;
90 if (debug_enable) {
91 _delay_ms(1000);
92 }
93
94 debug("init: start\n");
95 HID_setup();
96
97 debug("init: done\n");
98
99 uint16_t timer;
100 // to see loop pulse with oscillo scope
101 DDRF = (1<<7);
102 for (;;) {
103 PORTF ^= (1<<7);
104 keyboard_task();
105
106 timer = timer_read();
107 usb_host.Task();
108 timer = timer_elapsed(timer);
109 if (timer > 100) {
110 debug("host.Task: "); debug_hex16(timer); debug("\n");
111 }
112
113 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
114 // LUFA Task for control request
115 USB_USBTask();
116 #endif
117 }
118
119 return 0;
120 }
Imprint / Impressum