]> git.friedersdorff.com Git - max/tmk_keyboard.git/blobdiff - tmk_core/protocol/chibios/main.c
lufa: Add debug print support with UART
[max/tmk_keyboard.git] / tmk_core / protocol / chibios / main.c
index 589675aa125e81568a338270beb247b97b79bf6c..9c5254fa6120165f14b5530628b1704b1deeaf4b 100644 (file)
@@ -26,6 +26,8 @@
 #include "host_driver.h"
 #include "keyboard.h"
 #include "action.h"
+#include "action_util.h"
+#include "mousekey.h"
 #include "led.h"
 #include "sendchar.h"
 #include "debug.h"
@@ -33,6 +35,7 @@
 #include "sleep_led.h"
 #endif
 #include "suspend.h"
+#include "hook.h"
 
 
 /* -------------------------
@@ -56,6 +59,22 @@ host_driver_t chibios_driver = {
   send_consumer
 };
 
+/* Default hooks definitions. */
+__attribute__((weak))
+void hook_early_init(void) {}
+
+__attribute__((weak))
+void hook_late_init(void) {}
+
+__attribute__((weak))
+void hook_usb_suspend_loop(void) {
+  /* Do this in the suspended state */
+  suspend_power_down(); // on AVR this deep sleeps for 15ms
+  /* Remote wakeup */
+  if((USB_DRIVER.status & 2) && suspend_wakeup_condition()) {
+    send_remote_wakeup(&USB_DRIVER);
+  }
+}
 
 /* TESTING
  * Amber LED blinker thread, times are in milliseconds.
@@ -69,9 +88,9 @@ host_driver_t chibios_driver = {
 //   while(true) {
 //     if(blinkLed) {
 //       blinkLed = 0;
-//       palSetPad(GPIOC, GPIOC_LED_ORANGE);
+//       palSetPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13);
 //       chThdSleepMilliseconds(100);
-//       palClearPad(GPIOC, GPIOC_LED_ORANGE);
+//       palClearPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13);
 //     }
 //     chThdSleepMilliseconds(100);
 //   }
@@ -86,13 +105,11 @@ int main(void) {
   halInit();
   chSysInit();
 
-  palSetPad(GPIOC, GPIOC_LED_BLUE);
-  chThdSleepMilliseconds(400);
-  palClearPad(GPIOC, GPIOC_LED_BLUE);
-
   // TESTING
   // chThdCreateStatic(waBlinkerThread, sizeof(waBlinkerThread), NORMALPRIO, blinkerThread, NULL);
 
+  hook_early_init();
+
   /* Init USB */
   init_usb_driver(&USB_DRIVER);
 
@@ -103,6 +120,13 @@ int main(void) {
   while(USB_DRIVER.state != USB_ACTIVE)
     chThdSleepMilliseconds(50);
 
+  /* Do need to wait here!
+   * Otherwise the next print might start a transfer on console EP
+   * before the USB is completely ready, which sometimes causes
+   * HardFaults.
+   */
+  chThdSleepMilliseconds(50);
+
   print("USB configured.\n");
 
   /* init TMK modules */
@@ -115,11 +139,24 @@ int main(void) {
 
   print("Keyboard start.\n");
 
+  hook_late_init();
+
   /* Main loop */
   while(true) {
-    /* TODO: check for suspended event */
+
+    if(USB_DRIVER.state == USB_SUSPENDED) {
+      print("[s]");
+      while(USB_DRIVER.state == USB_SUSPENDED) {
+        hook_usb_suspend_loop();
+      }
+      /* Woken up */
+      // variables have been already cleared
+      send_keyboard_report();
+#ifdef MOUSEKEY_ENABLE
+      mousekey_send();
+#endif /* MOUSEKEY_ENABLE */
+    }
 
     keyboard_task();
-    chThdSleepMilliseconds(5);
   }
 }