]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - converter/usb_usb/main.cpp
ps2_usb: Fix for mbed build
[max/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
22 /* LED ping configuration */
23 #define TMK_LED
24 //#define LEONARDO_LED
25 #if defined(TMK_LED)
26 // For TMK converter and Teensy
27 #define LED_TX_INIT    (DDRD  |=  (1<<6))
28 #define LED_TX_ON      (PORTD |=  (1<<6))
29 #define LED_TX_OFF     (PORTD &= ~(1<<6))
30 #define LED_TX_TOGGLE  (PORTD ^=  (1<<6))
31 #elif defined(LEONARDO_LED)
32 // For Leonardo(TX LED)
33 #define LED_TX_INIT    (DDRD  |=  (1<<5))
34 #define LED_TX_ON      (PORTD &= ~(1<<5))
35 #define LED_TX_OFF     (PORTD |=  (1<<5))
36 #define LED_TX_TOGGLE  (PORTD ^=  (1<<5))
37 #else
38 #define LED_TX_INIT
39 #define LED_TX_ON
40 #define LED_TX_OFF
41 #define LED_TX_TOGGLE
42 #endif
43
44
45 static USB     usb_host;
46 static HIDBoot<HID_PROTOCOL_KEYBOARD>    kbd(&usb_host);
47 static KBDReportParser kbd_parser;
48 static USBHub hub1(&usb_host);  // one hub is enough for HHKB pro2
49 /* may be needed  for other device with more hub
50 static USBHub hub2(&usb_host);
51 static USBHub hub3(&usb_host);
52 static USBHub hub4(&usb_host);
53 static USBHub hub5(&usb_host);
54 static USBHub hub6(&usb_host);
55 static USBHub hub7(&usb_host);
56 */
57
58 static void LUFA_setup(void)
59 {
60     /* Disable watchdog if enabled by bootloader/fuses */
61     MCUSR &= ~(1 << WDRF);
62     wdt_disable();
63
64     /* Disable clock division */
65     clock_prescale_set(clock_div_1);
66
67     // Leonardo needs. Without this USB device is not recognized.
68     USB_Disable();
69
70     USB_Init();
71
72     // for Console_Task
73     USB_Device_EnableSOFEvents();
74     print_set_sendchar(sendchar);
75 }
76
77 static void HID_setup()
78 {
79     if (usb_host.Init() == -1) {
80         LED_TX_OFF;
81     }
82
83     _delay_ms(200);
84
85     kbd.SetReportParser(0, (HIDReportParser*)&kbd_parser);
86 }
87
88 int main(void)
89 {
90     // LED for debug
91     LED_TX_INIT;
92     LED_TX_ON;
93
94     debug_enable = true;
95
96     host_set_driver(&lufa_driver);
97     keyboard_init();
98
99     LUFA_setup();
100     HID_setup();
101     /* NOTE: Don't insert time consuming job here.
102      * It'll cause unclear initialization failure when DFU reset(worm start).
103      */
104     sei();
105
106     // wait for startup of sendchar routine
107     while (USB_DeviceState != DEVICE_STATE_Configured) ;
108     if (debug_enable) {
109         _delay_ms(1000);
110     }
111
112     debug("init: done\n");
113
114 uint16_t timer;
115     for (;;) {
116         keyboard_task();
117
118 timer = timer_read();
119         usb_host.Task();
120 timer = timer_elapsed(timer);
121 if (timer > 100) {
122     debug("host.Task: "); debug_hex16(timer);  debug("\n");
123 }
124
125 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
126         // LUFA Task for control request
127         USB_USBTask();
128 #endif
129     }
130
131     return 0;
132 }