]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/chibios/main.c
Merge branch 'master' into chibios
[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 "led.h"
30 #include "sendchar.h"
31 #include "debug.h"
32 #ifdef SLEEP_LED_ENABLE
33 #include "sleep_led.h"
34 #endif
35 #include "suspend.h"
36
37
38 /* -------------------------
39  *   TMK host driver defs
40  * -------------------------
41  */
42
43 /* declarations */
44 uint8_t keyboard_leds(void);
45 void send_keyboard(report_keyboard_t *report);
46 void send_mouse(report_mouse_t *report);
47 void send_system(uint16_t data);
48 void send_consumer(uint16_t data);
49
50 /* host struct */
51 host_driver_t chibios_driver = {
52   keyboard_leds,
53   send_keyboard,
54   send_mouse,
55   send_system,
56   send_consumer
57 };
58
59
60 /* TESTING
61  * Amber LED blinker thread, times are in milliseconds.
62  */
63 /* set this variable to non-zero anywhere to blink once */
64 // uint8_t blinkLed = 0;
65 // static THD_WORKING_AREA(waBlinkerThread, 128);
66 // static THD_FUNCTION(blinkerThread, arg) {
67 //   (void)arg;
68 //   chRegSetThreadName("blinkOrange");
69 //   while(true) {
70 //     if(blinkLed) {
71 //       blinkLed = 0;
72 //       palSetPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13);
73 //       chThdSleepMilliseconds(100);
74 //       palClearPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13);
75 //     }
76 //     chThdSleepMilliseconds(100);
77 //   }
78 // }
79
80
81
82 /* Main thread
83  */
84 int main(void) {
85   /* ChibiOS/RT init */
86   halInit();
87   chSysInit();
88
89   // TESTING
90   // chThdCreateStatic(waBlinkerThread, sizeof(waBlinkerThread), NORMALPRIO, blinkerThread, NULL);
91
92   /* Init USB */
93   init_usb_driver(&USB_DRIVER);
94
95   /* init printf */
96   init_printf(NULL,sendchar_pf);
97
98   /* Wait until the USB is active */
99   while(USB_DRIVER.state != USB_ACTIVE)
100     chThdSleepMilliseconds(50);
101
102   print("USB configured.\n");
103
104   /* init TMK modules */
105   keyboard_init();
106   host_set_driver(&chibios_driver);
107
108 #ifdef SLEEP_LED_ENABLE
109   sleep_led_init();
110 #endif
111
112   print("Keyboard start.\n");
113
114   /* Main loop */
115   while(true) {
116     /* TODO: check for suspended event */
117
118     keyboard_task();
119     chThdSleepMilliseconds(5);
120   }
121 }