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