]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/chibios/main.c
Add 'quantum/visualizer/' from commit 'bde869aa7ec8601459bc63b9636081d21108d1be'
[qmk_firmware.git] / tmk_core / protocol / chibios / main.c
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"
35 #ifdef SLEEP_LED_ENABLE
36 #include "sleep_led.h"
37 #endif
38 #ifdef SERIAL_LINK_ENABLE
39 #include "serial_link/system/serial_link.h"
40 #endif
41 #include "suspend.h"
42
43
44 /* -------------------------
45  *   TMK host driver defs
46  * -------------------------
47  */
48
49 /* declarations */
50 uint8_t keyboard_leds(void);
51 void send_keyboard(report_keyboard_t *report);
52 void send_mouse(report_mouse_t *report);
53 void send_system(uint16_t data);
54 void send_consumer(uint16_t data);
55
56 /* host struct */
57 host_driver_t chibios_driver = {
58   keyboard_leds,
59   send_keyboard,
60   send_mouse,
61   send_system,
62   send_consumer
63 };
64
65
66 /* TESTING
67  * Amber LED blinker thread, times are in milliseconds.
68  */
69 /* set this variable to non-zero anywhere to blink once */
70 // uint8_t blinkLed = 0;
71 // static THD_WORKING_AREA(waBlinkerThread, 128);
72 // static THD_FUNCTION(blinkerThread, arg) {
73 //   (void)arg;
74 //   chRegSetThreadName("blinkOrange");
75 //   while(true) {
76 //     if(blinkLed) {
77 //       blinkLed = 0;
78 //       palSetPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13);
79 //       chThdSleepMilliseconds(100);
80 //       palClearPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13);
81 //     }
82 //     chThdSleepMilliseconds(100);
83 //   }
84 // }
85
86
87
88 /* Main thread
89  */
90 int main(void) {
91   /* ChibiOS/RT init */
92   halInit();
93   chSysInit();
94
95   // TESTING
96   // chThdCreateStatic(waBlinkerThread, sizeof(waBlinkerThread), NORMALPRIO, blinkerThread, NULL);
97
98   /* Init USB */
99   init_usb_driver(&USB_DRIVER);
100
101   /* init printf */
102   init_printf(NULL,sendchar_pf);
103
104 #ifdef SERIAL_LINK_ENABLE
105   init_serial_link();
106 #endif
107
108   host_driver_t* driver = NULL;
109
110   /* Wait until the USB or serial link is active */
111   while (true) {
112     if(USB_DRIVER.state == USB_ACTIVE) {
113       driver = &chibios_driver;
114       break;
115     }
116 #ifdef SERIAL_LINK_ENABLE
117     if(is_serial_link_connected()) {
118       driver = get_serial_link_driver();
119       break;
120     }
121     serial_link_update();
122 #endif
123     chThdSleepMilliseconds(50);
124   }
125
126   /* Do need to wait here!
127    * Otherwise the next print might start a transfer on console EP
128    * before the USB is completely ready, which sometimes causes
129    * HardFaults.
130    */
131   chThdSleepMilliseconds(50);
132
133   print("USB configured.\n");
134
135   /* init TMK modules */
136   keyboard_init();
137   host_set_driver(driver);
138
139 #ifdef SLEEP_LED_ENABLE
140   sleep_led_init();
141 #endif
142
143   print("Keyboard start.\n");
144
145   /* Main loop */
146   while(true) {
147
148     if(USB_DRIVER.state == USB_SUSPENDED) {
149       print("[s]");
150       while(USB_DRIVER.state == USB_SUSPENDED) {
151         /* Do this in the suspended state */
152 #ifdef SERIAL_LINK_ENABLE
153         serial_link_update();
154 #endif
155         suspend_power_down(); // on AVR this deep sleeps for 15ms
156         /* Remote wakeup */
157         if((USB_DRIVER.status & 2) && suspend_wakeup_condition()) {
158           send_remote_wakeup(&USB_DRIVER);
159         }
160       }
161       /* Woken up */
162       // variables has been already cleared by the wakeup hook
163       send_keyboard_report();
164 #ifdef MOUSEKEY_ENABLE
165       mousekey_send();
166 #endif /* MOUSEKEY_ENABLE */
167     }
168
169     keyboard_task();
170   }
171 }