2 * (c) 2015 flabberast <s3+flabbergast@sdfeu.org>
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
14 * Since some GPL'd code is used, this work is licensed under
26 #include "host_driver.h"
29 #include "action_util.h"
35 #ifdef SLEEP_LED_ENABLE
36 #include "sleep_led.h"
38 #ifdef SERIAL_LINK_ENABLE
39 #include "serial_link/system/serial_link.h"
41 #ifdef VISUALIZER_ENABLE
42 #include "visualizer/visualizer.h"
47 #ifdef STM32_EEPROM_ENABLE
48 #include "eeprom_stm32.h"
53 /* -------------------------
54 * TMK host driver defs
55 * -------------------------
59 uint8_t keyboard_leds(void);
60 void send_keyboard(report_keyboard_t *report);
61 void send_mouse(report_mouse_t *report);
62 void send_system(uint16_t data);
63 void send_consumer(uint16_t data);
66 host_driver_t chibios_driver = {
75 void virtser_task(void);
79 void raw_hid_task(void);
83 void console_task(void);
87 * Amber LED blinker thread, times are in milliseconds.
89 /* set this variable to non-zero anywhere to blink once */
90 // static THD_WORKING_AREA(waThread1, 128);
91 // static THD_FUNCTION(Thread1, arg) {
94 // chRegSetThreadName("blinker");
98 // time = USB_DRIVER.state == USB_ACTIVE ? 250 : 500;
99 // palClearLine(LINE_CAPS_LOCK);
100 // chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
101 // palSetLine(LINE_CAPS_LOCK);
102 // chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
111 /* ChibiOS/RT init */
115 #ifdef STM32_EEPROM_ENABLE
120 // chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
125 init_usb_driver(&USB_DRIVER);
128 init_printf(NULL,sendchar_pf);
134 #ifdef SERIAL_LINK_ENABLE
138 #ifdef VISUALIZER_ENABLE
143 host_driver_t* driver = NULL;
145 /* Wait until the USB or serial link is active */
147 #if defined(WAIT_FOR_USB) || defined(SERIAL_LINK_ENABLE)
148 if(USB_DRIVER.state == USB_ACTIVE) {
149 driver = &chibios_driver;
153 driver = &chibios_driver;
156 #ifdef SERIAL_LINK_ENABLE
157 if(is_serial_link_connected()) {
158 driver = get_serial_link_driver();
161 serial_link_update();
166 /* Do need to wait here!
167 * Otherwise the next print might start a transfer on console EP
168 * before the USB is completely ready, which sometimes causes
173 print("USB configured.\n");
175 /* init TMK modules */
177 host_set_driver(driver);
179 #ifdef SLEEP_LED_ENABLE
183 print("Keyboard start.\n");
188 #if !defined(NO_USB_STARTUP_CHECK)
189 if(USB_DRIVER.state == USB_SUSPENDED) {
191 #ifdef VISUALIZER_ENABLE
192 visualizer_suspend();
194 while(USB_DRIVER.state == USB_SUSPENDED) {
195 /* Do this in the suspended state */
196 #ifdef SERIAL_LINK_ENABLE
197 serial_link_update();
199 suspend_power_down(); // on AVR this deep sleeps for 15ms
201 if(suspend_wakeup_condition()) {
202 usbWakeupHost(&USB_DRIVER);
206 // variables has been already cleared by the wakeup hook
207 send_keyboard_report();
208 #ifdef MOUSEKEY_ENABLE
210 #endif /* MOUSEKEY_ENABLE */
212 #ifdef VISUALIZER_ENABLE
219 #ifdef CONSOLE_ENABLE
222 #ifdef VIRTSER_ENABLE