Commit | Line | Data |
---|---|---|
4d4f7684 | 1 | /* |
2 | * (c) 2015 flabberast <s3+flabbergast@sdfeu.org> | |
3 | * | |
4 | * Based on the following work: | |
5 | * - Guillaume Duc's raw hid example (MIT License) | |
6 | * https://github.com/guiduc/usb-hid-chibios-example | |
7 | * - PJRC Teensy examples (MIT License) | |
8 | * https://www.pjrc.com/teensy/usb_keyboard.html | |
9 | * - hasu's TMK keyboard code (GPL v2 and some code Modified BSD) | |
10 | * https://github.com/tmk/tmk_keyboard/ | |
11 | * - ChibiOS demo code (Apache 2.0 License) | |
12 | * http://www.chibios.org | |
13 | * | |
14 | * Since some GPL'd code is used, this work is licensed under | |
15 | * GPL v2 or later. | |
16 | */ | |
17 | ||
18 | #include "ch.h" | |
19 | #include "hal.h" | |
20 | ||
21 | #include "usb_main.h" | |
22 | ||
23 | /* TMK includes */ | |
24 | #include "report.h" | |
25 | #include "host.h" | |
26 | #include "host_driver.h" | |
27 | #include "keyboard.h" | |
28 | #include "action.h" | |
29 | #include "action_util.h" | |
30 | #include "mousekey.h" | |
31 | #include "led.h" | |
32 | #include "sendchar.h" | |
33 | #include "debug.h" | |
34 | #include "printf.h" | |
1ea0cac9 JC |
35 | #include "rgblight_reconfig.h" |
36 | ||
37 | #if (defined(RGB_MIDI) || defined(RGBLIGHT_ANIMATIONS)) && defined(RGBLIGHT_ENABLE) | |
38 | # include "rgblight.h" | |
39 | #endif | |
4d4f7684 | 40 | #ifdef SLEEP_LED_ENABLE |
b624f32f | 41 | # include "sleep_led.h" |
4d4f7684 | 42 | #endif |
4b45deb6 | 43 | #ifdef SERIAL_LINK_ENABLE |
b624f32f | 44 | # include "serial_link/system/serial_link.h" |
4b45deb6 | 45 | #endif |
07d0d5cb | 46 | #ifdef VISUALIZER_ENABLE |
b624f32f | 47 | # include "visualizer/visualizer.h" |
07d0d5cb | 48 | #endif |
53ff8a31 | 49 | #ifdef MIDI_ENABLE |
b624f32f | 50 | # include "qmk_midi.h" |
53ff8a31 | 51 | #endif |
f4094930 | 52 | #ifdef STM32_EEPROM_ENABLE |
b624f32f | 53 | # include "eeprom_stm32.h" |
621ce29a | 54 | #endif |
4d4f7684 | 55 | #include "suspend.h" |
5fd68266 | 56 | #include "wait.h" |
4d4f7684 | 57 | |
58 | /* ------------------------- | |
59 | * TMK host driver defs | |
60 | * ------------------------- | |
61 | */ | |
62 | ||
63 | /* declarations */ | |
64 | uint8_t keyboard_leds(void); | |
b624f32f | 65 | void send_keyboard(report_keyboard_t *report); |
66 | void send_mouse(report_mouse_t *report); | |
67 | void send_system(uint16_t data); | |
68 | void send_consumer(uint16_t data); | |
4d4f7684 | 69 | |
70 | /* host struct */ | |
b624f32f | 71 | host_driver_t chibios_driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer}; |
4d4f7684 | 72 | |
53ff8a31 | 73 | #ifdef VIRTSER_ENABLE |
74 | void virtser_task(void); | |
75 | #endif | |
76 | ||
87f06e72 | 77 | #ifdef RAW_ENABLE |
53ff8a31 | 78 | void raw_hid_task(void); |
79 | #endif | |
80 | ||
81 | #ifdef CONSOLE_ENABLE | |
82 | void console_task(void); | |
83 | #endif | |
4d4f7684 | 84 | |
85 | /* TESTING | |
86 | * Amber LED blinker thread, times are in milliseconds. | |
87 | */ | |
88 | /* set this variable to non-zero anywhere to blink once */ | |
5fd68266 | 89 | // static THD_WORKING_AREA(waThread1, 128); |
90 | // static THD_FUNCTION(Thread1, arg) { | |
91 | ||
4d4f7684 | 92 | // (void)arg; |
5fd68266 | 93 | // chRegSetThreadName("blinker"); |
94 | // while (true) { | |
95 | // systime_t time; | |
96 | ||
97 | // time = USB_DRIVER.state == USB_ACTIVE ? 250 : 500; | |
98 | // palClearLine(LINE_CAPS_LOCK); | |
99 | // chSysPolledDelayX(MS2RTC(STM32_HCLK, time)); | |
100 | // palSetLine(LINE_CAPS_LOCK); | |
101 | // chSysPolledDelayX(MS2RTC(STM32_HCLK, time)); | |
4d4f7684 | 102 | // } |
103 | // } | |
104 | ||
4d4f7684 | 105 | /* Main thread |
106 | */ | |
107 | int main(void) { | |
b624f32f | 108 | /* ChibiOS/RT init */ |
109 | halInit(); | |
110 | chSysInit(); | |
4d4f7684 | 111 | |
f4094930 | 112 | #ifdef STM32_EEPROM_ENABLE |
b624f32f | 113 | EEPROM_Init(); |
621ce29a | 114 | #endif |
115 | ||
b624f32f | 116 | // TESTING |
117 | // chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); | |
4d4f7684 | 118 | |
b624f32f | 119 | keyboard_setup(); |
cc5c6b44 | 120 | |
b624f32f | 121 | /* Init USB */ |
122 | init_usb_driver(&USB_DRIVER); | |
4d4f7684 | 123 | |
b624f32f | 124 | /* init printf */ |
125 | init_printf(NULL, sendchar_pf); | |
4d4f7684 | 126 | |
53ff8a31 | 127 | #ifdef MIDI_ENABLE |
b624f32f | 128 | setup_midi(); |
53ff8a31 | 129 | #endif |
130 | ||
7229751b | 131 | #ifdef SERIAL_LINK_ENABLE |
b624f32f | 132 | init_serial_link(); |
7229751b FS |
133 | #endif |
134 | ||
07d0d5cb | 135 | #ifdef VISUALIZER_ENABLE |
b624f32f | 136 | visualizer_init(); |
07d0d5cb FS |
137 | #endif |
138 | ||
b624f32f | 139 | host_driver_t *driver = NULL; |
4b45deb6 | 140 | |
b624f32f | 141 | /* Wait until the USB or serial link is active */ |
142 | while (true) { | |
73a3399d | 143 | #if defined(WAIT_FOR_USB) || defined(SERIAL_LINK_ENABLE) |
b624f32f | 144 | if (USB_DRIVER.state == USB_ACTIVE) { |
145 | driver = &chibios_driver; | |
146 | break; | |
147 | } | |
73a3399d | 148 | #else |
b624f32f | 149 | driver = &chibios_driver; |
150 | break; | |
73a3399d | 151 | #endif |
4b45deb6 | 152 | #ifdef SERIAL_LINK_ENABLE |
b624f32f | 153 | if (is_serial_link_connected()) { |
154 | driver = get_serial_link_driver(); | |
155 | break; | |
156 | } | |
157 | serial_link_update(); | |
4b45deb6 | 158 | #endif |
b624f32f | 159 | wait_ms(50); |
160 | } | |
4d4f7684 | 161 | |
b624f32f | 162 | /* Do need to wait here! |
163 | * Otherwise the next print might start a transfer on console EP | |
164 | * before the USB is completely ready, which sometimes causes | |
165 | * HardFaults. | |
166 | */ | |
167 | wait_ms(50); | |
4d4f7684 | 168 | |
b624f32f | 169 | print("USB configured.\n"); |
4d4f7684 | 170 | |
b624f32f | 171 | /* init TMK modules */ |
172 | keyboard_init(); | |
173 | host_set_driver(driver); | |
4d4f7684 | 174 | |
175 | #ifdef SLEEP_LED_ENABLE | |
b624f32f | 176 | sleep_led_init(); |
4d4f7684 | 177 | #endif |
178 | ||
b624f32f | 179 | print("Keyboard start.\n"); |
4d4f7684 | 180 | |
b624f32f | 181 | /* Main loop */ |
182 | while (true) { | |
73a3399d | 183 | #if !defined(NO_USB_STARTUP_CHECK) |
b624f32f | 184 | if (USB_DRIVER.state == USB_SUSPENDED) { |
185 | print("[s]"); | |
186 | # ifdef VISUALIZER_ENABLE | |
187 | visualizer_suspend(); | |
188 | # endif | |
189 | while (USB_DRIVER.state == USB_SUSPENDED) { | |
190 | /* Do this in the suspended state */ | |
191 | # ifdef SERIAL_LINK_ENABLE | |
192 | serial_link_update(); | |
193 | # endif | |
194 | suspend_power_down(); // on AVR this deep sleeps for 15ms | |
195 | /* Remote wakeup */ | |
196 | if (suspend_wakeup_condition()) { | |
197 | usbWakeupHost(&USB_DRIVER); | |
198 | } | |
199 | } | |
200 | /* Woken up */ | |
201 | // variables has been already cleared by the wakeup hook | |
202 | send_keyboard_report(); | |
203 | # ifdef MOUSEKEY_ENABLE | |
204 | mousekey_send(); | |
205 | # endif /* MOUSEKEY_ENABLE */ | |
206 | ||
207 | # ifdef VISUALIZER_ENABLE | |
208 | visualizer_resume(); | |
209 | # endif | |
4d4f7684 | 210 | } |
73a3399d | 211 | #endif |
4d4f7684 | 212 | |
b624f32f | 213 | keyboard_task(); |
53ff8a31 | 214 | #ifdef CONSOLE_ENABLE |
b624f32f | 215 | console_task(); |
53ff8a31 | 216 | #endif |
217 | #ifdef VIRTSER_ENABLE | |
b624f32f | 218 | virtser_task(); |
53ff8a31 | 219 | #endif |
87f06e72 | 220 | #ifdef RAW_ENABLE |
b624f32f | 221 | raw_hid_task(); |
53ff8a31 | 222 | #endif |
b624f32f | 223 | } |
4d4f7684 | 224 | } |