]> git.donarmstrong.com Git - qmk_firmware.git/blob - converter/usb_usb/main.cpp
Updated readme to fix a typo and list out hotkey shortcuts
[qmk_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 "usbhub.h"
9 #include "hid.h"
10 #include "hidboot.h"
11 #include "parser.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 #include "led.h"
21
22
23 /* LED ping configuration */
24 #define TMK_LED
25 //#define LEONARDO_LED
26 #if defined(TMK_LED)
27 // For TMK converter and Teensy
28 #define LED_TX_INIT    (DDRD  |=  (1<<6))
29 #define LED_TX_ON      (PORTD |=  (1<<6))
30 #define LED_TX_OFF     (PORTD &= ~(1<<6))
31 #define LED_TX_TOGGLE  (PORTD ^=  (1<<6))
32 #elif defined(LEONARDO_LED)
33 // For Leonardo(TX LED)
34 #define LED_TX_INIT    (DDRD  |=  (1<<5))
35 #define LED_TX_ON      (PORTD &= ~(1<<5))
36 #define LED_TX_OFF     (PORTD |=  (1<<5))
37 #define LED_TX_TOGGLE  (PORTD ^=  (1<<5))
38 #else
39 #define LED_TX_INIT
40 #define LED_TX_ON
41 #define LED_TX_OFF
42 #define LED_TX_TOGGLE
43 #endif
44
45
46 static void LUFA_setup(void)
47 {
48     /* Disable watchdog if enabled by bootloader/fuses */
49     MCUSR &= ~(1 << WDRF);
50     wdt_disable();
51
52     /* Disable clock division */
53     clock_prescale_set(clock_div_1);
54
55     // Leonardo needs. Without this USB device is not recognized.
56     USB_Disable();
57
58     USB_Init();
59
60     // for Console_Task
61     USB_Device_EnableSOFEvents();
62     print_set_sendchar(sendchar);
63 }
64
65
66
67 /*
68  * USB Host Shield HID keyboard
69  */
70 USB usb_host;
71 USBHub hub1(&usb_host);
72 HIDBoot<HID_PROTOCOL_KEYBOARD>    kbd(&usb_host);
73 KBDReportParser kbd_parser;
74
75
76 void led_set(uint8_t usb_led)
77 {
78     kbd.SetReport(0, 0, 2, 0, 1, &usb_led);
79 }
80
81
82
83 int main(void)
84 {
85     // LED for debug
86     LED_TX_INIT;
87     LED_TX_ON;
88
89     debug_enable = true;
90     debug_keyboard = true;
91
92     host_set_driver(&lufa_driver);
93     keyboard_init();
94
95     LUFA_setup();
96
97     // USB Host Shield setup
98     usb_host.Init();
99     kbd.SetReportParser(0, (HIDReportParser*)&kbd_parser);
100
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 }