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