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