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