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"
50 /* -------------------------
51 * TMK host driver defs
52 * -------------------------
56 uint8_t keyboard_leds(void);
57 void send_keyboard(report_keyboard_t *report);
58 void send_mouse(report_mouse_t *report);
59 void send_system(uint16_t data);
60 void send_consumer(uint16_t data);
63 host_driver_t chibios_driver = {
72 void virtser_task(void);
76 void raw_hid_task(void);
80 void console_task(void);
84 * Amber LED blinker thread, times are in milliseconds.
86 /* set this variable to non-zero anywhere to blink once */
87 // static THD_WORKING_AREA(waThread1, 128);
88 // static THD_FUNCTION(Thread1, arg) {
91 // chRegSetThreadName("blinker");
95 // time = USB_DRIVER.state == USB_ACTIVE ? 250 : 500;
96 // palClearLine(LINE_CAPS_LOCK);
97 // chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
98 // palSetLine(LINE_CAPS_LOCK);
99 // chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
108 /* ChibiOS/RT init */
113 // chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
116 init_usb_driver(&USB_DRIVER);
119 init_printf(NULL,sendchar_pf);
125 #ifdef SERIAL_LINK_ENABLE
129 #ifdef VISUALIZER_ENABLE
134 host_driver_t* driver = NULL;
136 /* Wait until the USB or serial link is active */
138 if(USB_DRIVER.state == USB_ACTIVE) {
139 driver = &chibios_driver;
142 #ifdef SERIAL_LINK_ENABLE
143 if(is_serial_link_connected()) {
144 driver = get_serial_link_driver();
147 serial_link_update();
152 /* Do need to wait here!
153 * Otherwise the next print might start a transfer on console EP
154 * before the USB is completely ready, which sometimes causes
159 print("USB configured.\n");
161 /* init TMK modules */
163 host_set_driver(driver);
165 #ifdef SLEEP_LED_ENABLE
169 print("Keyboard start.\n");
174 if(USB_DRIVER.state == USB_SUSPENDED) {
176 #ifdef VISUALIZER_ENABLE
177 visualizer_suspend();
179 while(USB_DRIVER.state == USB_SUSPENDED) {
180 /* Do this in the suspended state */
181 #ifdef SERIAL_LINK_ENABLE
182 serial_link_update();
184 suspend_power_down(); // on AVR this deep sleeps for 15ms
186 if(suspend_wakeup_condition()) {
187 usbWakeupHost(&USB_DRIVER);
191 // variables has been already cleared by the wakeup hook
192 send_keyboard_report();
193 #ifdef MOUSEKEY_ENABLE
195 #endif /* MOUSEKEY_ENABLE */
197 #ifdef VISUALIZER_ENABLE
203 #ifdef CONSOLE_ENABLE
206 #ifdef VIRTSER_ENABLE
209 #ifdef RAW_HID_ENABLE