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 /* -------------------------
48 * TMK host driver defs
49 * -------------------------
53 uint8_t keyboard_leds(void);
54 void send_keyboard(report_keyboard_t *report);
55 void send_mouse(report_mouse_t *report);
56 void send_system(uint16_t data);
57 void send_consumer(uint16_t data);
60 host_driver_t chibios_driver = {
70 * Amber LED blinker thread, times are in milliseconds.
72 /* set this variable to non-zero anywhere to blink once */
73 // uint8_t blinkLed = 0;
74 // static THD_WORKING_AREA(waBlinkerThread, 128);
75 // static THD_FUNCTION(blinkerThread, arg) {
77 // chRegSetThreadName("blinkOrange");
81 // palSetPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13);
82 // chThdSleepMilliseconds(100);
83 // palClearPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13);
85 // chThdSleepMilliseconds(100);
99 // chThdCreateStatic(waBlinkerThread, sizeof(waBlinkerThread), NORMALPRIO, blinkerThread, NULL);
102 init_usb_driver(&USB_DRIVER);
105 init_printf(NULL,sendchar_pf);
107 #ifdef SERIAL_LINK_ENABLE
111 #ifdef VISUALIZER_ENABLE
116 host_driver_t* driver = NULL;
118 /* Wait until the USB or serial link is active */
120 if(USB_DRIVER.state == USB_ACTIVE) {
121 driver = &chibios_driver;
124 #ifdef SERIAL_LINK_ENABLE
125 if(is_serial_link_connected()) {
126 driver = get_serial_link_driver();
129 serial_link_update();
131 chThdSleepMilliseconds(50);
134 /* Do need to wait here!
135 * Otherwise the next print might start a transfer on console EP
136 * before the USB is completely ready, which sometimes causes
139 chThdSleepMilliseconds(50);
141 print("USB configured.\n");
143 /* init TMK modules */
145 host_set_driver(driver);
147 #ifdef SLEEP_LED_ENABLE
151 print("Keyboard start.\n");
156 if(USB_DRIVER.state == USB_SUSPENDED) {
158 #ifdef VISUALIZER_ENABLE
159 visualizer_suspend();
161 while(USB_DRIVER.state == USB_SUSPENDED) {
162 /* Do this in the suspended state */
163 #ifdef SERIAL_LINK_ENABLE
164 serial_link_update();
166 suspend_power_down(); // on AVR this deep sleeps for 15ms
168 if((USB_DRIVER.status & 2) && suspend_wakeup_condition()) {
169 send_remote_wakeup(&USB_DRIVER);
173 // variables has been already cleared by the wakeup hook
174 send_keyboard_report();
175 #ifdef MOUSEKEY_ENABLE
177 #endif /* MOUSEKEY_ENABLE */
179 #ifdef VISUALIZER_ENABLE