]> git.friedersdorff.com Git - max/tmk_keyboard.git/blobdiff - tmk_core/protocol/chibios/main.c
Chibios: Fix a HardFault bug (wait after start).
[max/tmk_keyboard.git] / tmk_core / protocol / chibios / main.c
index 589675aa125e81568a338270beb247b97b79bf6c..d588e4de06f9beaab67e8fa82dae5755d1bc2a13 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"
@@ -69,9 +71,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,10 +88,6 @@ 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);
 
@@ -103,6 +101,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 */
@@ -117,9 +122,25 @@ int main(void) {
 
   /* Main loop */
   while(true) {
-    /* TODO: check for suspended event */
+
+    if(USB_DRIVER.state == USB_SUSPENDED) {
+      print("[s]");
+      while(USB_DRIVER.state == USB_SUSPENDED) {
+        /* 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);
+        }
+      }
+      /* Woken up */
+      // variables has been already cleared by the wakeup hook
+      send_keyboard_report();
+#ifdef MOUSEKEY_ENABLE
+      mousekey_send();
+#endif /* MOUSEKEY_ENABLE */
+    }
 
     keyboard_task();
-    chThdSleepMilliseconds(5);
   }
 }