]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/chibios/main.c
Add kb and user level keyboard initialization functions (#3113)
[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 #ifdef VISUALIZER_ENABLE
42 #include "visualizer/visualizer.h"
43 #endif
44 #ifdef MIDI_ENABLE
45 #include "qmk_midi.h"
46 #endif
47 #ifdef STM32_EEPROM_ENABLE
48 #include "eeprom_stm32.h"
49 #endif
50 #include "suspend.h"
51 #include "wait.h"
52
53 /* -------------------------
54  *   TMK host driver defs
55  * -------------------------
56  */
57
58 /* declarations */
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);
64
65 /* host struct */
66 host_driver_t chibios_driver = {
67   keyboard_leds,
68   send_keyboard,
69   send_mouse,
70   send_system,
71   send_consumer
72 };
73
74 #ifdef VIRTSER_ENABLE
75 void virtser_task(void);
76 #endif
77
78 #ifdef RAW_ENABLE
79 void raw_hid_task(void);
80 #endif
81
82 #ifdef CONSOLE_ENABLE
83 void console_task(void);
84 #endif
85
86 /* TESTING
87  * Amber LED blinker thread, times are in milliseconds.
88  */
89 /* set this variable to non-zero anywhere to blink once */
90 // static THD_WORKING_AREA(waThread1, 128);
91 // static THD_FUNCTION(Thread1, arg) {
92
93 //   (void)arg;
94 //   chRegSetThreadName("blinker");
95 //   while (true) {
96 //     systime_t time;
97
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));
103 //   }
104 // }
105
106
107
108 /* Main thread
109  */
110 int main(void) {
111   /* ChibiOS/RT init */
112   halInit();
113   chSysInit();
114
115 #ifdef STM32_EEPROM_ENABLE
116   EEPROM_Init();
117 #endif
118
119   // TESTING
120   // chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
121
122   keyboard_setup();
123
124   /* Init USB */
125   init_usb_driver(&USB_DRIVER);
126
127   /* init printf */
128   init_printf(NULL,sendchar_pf);
129
130 #ifdef MIDI_ENABLE
131   setup_midi();
132 #endif
133
134 #ifdef SERIAL_LINK_ENABLE
135   init_serial_link();
136 #endif
137
138 #ifdef VISUALIZER_ENABLE
139   visualizer_init();
140 #endif
141
142
143   host_driver_t* driver = NULL;
144
145   /* Wait until the USB or serial link is active */
146   while (true) {
147 #if defined(WAIT_FOR_USB) || defined(SERIAL_LINK_ENABLE)
148     if(USB_DRIVER.state == USB_ACTIVE) {
149       driver = &chibios_driver;
150       break;
151     }
152 #else
153     driver = &chibios_driver;
154     break;
155 #endif
156 #ifdef SERIAL_LINK_ENABLE
157     if(is_serial_link_connected()) {
158       driver = get_serial_link_driver();
159       break;
160     }
161     serial_link_update();
162 #endif
163     wait_ms(50);
164   }
165
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
169    * HardFaults.
170    */
171   wait_ms(50);
172
173   print("USB configured.\n");
174
175   /* init TMK modules */
176   keyboard_init();
177   host_set_driver(driver);
178
179 #ifdef SLEEP_LED_ENABLE
180   sleep_led_init();
181 #endif
182
183   print("Keyboard start.\n");
184
185   /* Main loop */
186   while(true) {
187
188 #if !defined(NO_USB_STARTUP_CHECK)
189     if(USB_DRIVER.state == USB_SUSPENDED) {
190       print("[s]");
191 #ifdef VISUALIZER_ENABLE
192       visualizer_suspend();
193 #endif
194       while(USB_DRIVER.state == USB_SUSPENDED) {
195         /* Do this in the suspended state */
196 #ifdef SERIAL_LINK_ENABLE
197         serial_link_update();
198 #endif
199         suspend_power_down(); // on AVR this deep sleeps for 15ms
200         /* Remote wakeup */
201         if(suspend_wakeup_condition()) {
202           usbWakeupHost(&USB_DRIVER);
203         }
204       }
205       /* Woken up */
206       // variables has been already cleared by the wakeup hook
207       send_keyboard_report();
208 #ifdef MOUSEKEY_ENABLE
209       mousekey_send();
210 #endif /* MOUSEKEY_ENABLE */
211
212 #ifdef VISUALIZER_ENABLE
213       visualizer_resume();
214 #endif
215     }
216 #endif
217
218     keyboard_task();
219 #ifdef CONSOLE_ENABLE
220     console_task();
221 #endif
222 #ifdef VIRTSER_ENABLE
223     virtser_task();
224 #endif
225 #ifdef RAW_ENABLE
226     raw_hid_task();
227 #endif
228   }
229 }