]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/chibios/main.c
Merge commit '657d9f23fe47fb88cf221adb23095082f191ba6a'
[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
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   print("USB configured.\n");
105
106   /* init TMK modules */
107   keyboard_init();
108   host_set_driver(&chibios_driver);
109
110 #ifdef SLEEP_LED_ENABLE
111   sleep_led_init();
112 #endif
113
114   print("Keyboard start.\n");
115
116   /* Main loop */
117   while(true) {
118
119     if(USB_DRIVER.state == USB_SUSPENDED) {
120       print("[s]");
121       while(USB_DRIVER.state == USB_SUSPENDED) {
122         /* Do this in the suspended state */
123         suspend_power_down(); // on AVR this deep sleeps for 15ms
124         /* Remote wakeup */
125         if((USB_DRIVER.status & 2) && suspend_wakeup_condition()) {
126           send_remote_wakeup(&USB_DRIVER);
127         }
128       }
129       /* Woken up */
130       // variables has been already cleared by the wakeup hook
131       send_keyboard_report();
132 #ifdef MOUSEKEY_ENABLE
133       mousekey_send();
134 #endif /* MOUSEKEY_ENABLE */
135     }
136
137     keyboard_task();
138   }
139 }