]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/chibios/main.c
7aec7e3e107f8ba1e82cf572ebd1557c10018c92
[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 host_driver_t chibios_driver = {
44   keyboard_leds,
45   send_keyboard,
46   send_mouse,
47   send_system,
48   send_consumer
49 };
50
51
52 /* TESTING
53  * Amber LED blinker thread, times are in milliseconds.
54  */
55 // uint8_t blinkLedState = 0;
56 // static THD_WORKING_AREA(waThread1, 128);
57 // static THD_FUNCTION(Thread1, arg) {
58 //   (void)arg;
59 //   chRegSetThreadName("blinker1");
60 //   while(true) {
61 //     if(blinkLedState) {
62 //       blinkLedState = 0;
63 //       palSetPad(GPIOC, GPIOC_LED_ORANGE);
64 //       chThdSleepMilliseconds(100);
65 //       palClearPad(GPIOC, GPIOC_LED_ORANGE);
66 //     }
67 //     chThdSleepMilliseconds(100);
68 //   }
69 // }
70
71
72
73 /* Main thread
74  */
75 int main(void) {
76   /* ChibiOS/RT init */
77   halInit();
78   chSysInit();
79
80   palSetPad(GPIOC, GPIOC_LED_BLUE);
81   chThdSleepMilliseconds(400);
82   palClearPad(GPIOC, GPIOC_LED_BLUE);
83
84   // TESTING
85   // chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
86
87   /* Init USB */
88   init_usb_driver(&USB_DRIVER);
89
90   /* init printf */
91   init_printf(NULL,sendchar_pf);
92
93   /* Wait until the USB is active */
94   while(USB_DRIVER.state != USB_ACTIVE)
95     chThdSleepMilliseconds(50);
96
97   print("USB configured.\n");
98
99   /* init TMK modules */
100   keyboard_init();
101   host_set_driver(&chibios_driver);
102
103 #ifdef SLEEP_LED_ENABLE
104   sleep_led_init();
105 #endif
106
107   print("Keyboard start.\n");
108
109   /* Main loop */
110   while(true) {
111     /* TODO: check for suspended event */
112
113     keyboard_task();
114     chThdSleepMilliseconds(5);
115   }
116 }