]> git.friedersdorff.com Git - max/tmk_keyboard.git/commitdiff
Chibios: use WFI in idle. WIP suspend stuff.
authorflabbergast <s3+flabbergast@sdfeu.org>
Wed, 14 Oct 2015 09:13:49 +0000 (10:13 +0100)
committerflabbergast <s3+flabbergast@sdfeu.org>
Wed, 14 Oct 2015 09:13:49 +0000 (10:13 +0100)
keyboard/stm32_onekey/chconf.h
keyboard/stm32_onekey/keymap_plain.c
keyboard/teensy_lc_onekey/chconf.h
keyboard/teensy_lc_onekey/instructions.md
keyboard/teensy_lc_onekey/keymap_plain.c
tmk_core/common/chibios/sleep_led.c
tmk_core/common/chibios/suspend.c
tmk_core/protocol/chibios/main.c
tmk_core/protocol/chibios/usb_main.c

index da4160b6ea82f26bc7c053dfaadcf7d7ae297143..be362a95f236185e051fc35eb8da762270f955e2 100644 (file)
  */
 #define CH_CFG_NO_IDLE_THREAD               FALSE
 
+/* Use __WFI in the idle thread for waiting. Does lower the power
+ * consumption. */
+#define CORTEX_ENABLE_WFI_IDLE              TRUE
+
 /** @} */
 
 /*===========================================================================*/
index ea17393582b371f9340d007a1ad65837e5071e68..8de1c0279e0190b820c15df7cbd262b7a23a4de1 100644 (file)
@@ -25,7 +25,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include "keymap.h"
 
 static const uint8_t keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
-    {{KC_A}},
+    {{KC_CAPS}}, // test with KC_CAPS, KC_A, KC_BTLD
 };
 
 static const uint16_t fn_actions[] = {
index 50cb1be45304cfa0957c7041013f0ccba64f206d..ee527d47004eb287fe74a1142fe704d85583baae 100644 (file)
  */\r
 #define CH_CFG_NO_IDLE_THREAD               FALSE\r
 \r
+/* Use __WFI in the idle thread for waiting. Does lower the power\r
+ * consumption. */\r
+#define CORTEX_ENABLE_WFI_IDLE              TRUE\r
+\r
 /** @} */\r
 \r
 /*===========================================================================*/\r
index b19b89848666efc95ea9440a58f2418dd4c3ed60..9a7aeecef3542546ef5090e0dcc14b083261ad51 100644 (file)
@@ -16,6 +16,8 @@ It's set up for Teensy LC. To use 3.x, you'll need to edit the `Makefile` (and c
 
 ## Credits
 
+TMK itself is written by hasu, original sources [here](https://github.com/tmk/tmk_keyboard).
+
 The USB support for Kinetis MCUs is due to RedoX. His ChibiOS fork is also [on github](https://github.com/RedoXyde/ChibiOS); but it doesn't include Teensy LC definitions.
 
 ## Features that are not implemented yet
index a83574896b66dd2c4d2d714c1747a8fd17e7ef2f..922fa5379cdcec48b99ad1653aaf48df2aa9c68b 100644 (file)
@@ -25,8 +25,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include "keymap.h"
 
 static const uint8_t keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
-    {{KC_BTLD}},
-};  // to test: KC_CAPS, KT_BTLD
+    {{KC_A}},
+};  // to test: KC_CAPS, KT_BTLD, KC_A
 
 static const uint16_t fn_actions[] = {
 };
index b08c9c2aa9a93e8cbc4af18825233924eebf5590..fad7ac043a91adf09c4b0e8090521ac14dc46f32 100644 (file)
@@ -4,6 +4,10 @@
 #include "sleep_led.h"
 
 void sleep_led_init(void) {
+       // we could go the 'software way' -- just enable *some* timer
+       // and go with callbacks
+       // or we could go the 'hardware way' -- and use timer output to
+       // pins directly
 }
 
 void sleep_led_enable(void) {
index 49ecad43920a4eaf7cc97a8c6d20d0c53f1ba221..6ca16034f3840f1b9230476ed2790af1d9f0a04f 100644 (file)
@@ -1,8 +1,65 @@
 /* TODO */
 
-#include <stdbool.h>
+#include "ch.h"
+#include "hal.h"
 
+#include "matrix.h"
+#include "action.h"
+#include "action_util.h"
+#include "mousekey.h"
+#include "host.h"
+#include "backlight.h"
+#include "suspend.h"
 
-void suspend_power_down(void) {}
-bool suspend_wakeup_condition(void) { return true; }
-void suspend_wakeup_init(void) {}
+void suspend_idle(uint8_t time) {
+       // TODO: this is not used anywhere - what units is 'time' in?
+       chThdSleepMilliseconds(time);
+}
+
+void suspend_power_down(void) {
+       // TODO: figure out what to power down and how
+       // shouldn't power down TPM/FTM if we want a breathing LED
+       // also shouldn't power down USB
+
+       // on AVR, this enables the watchdog for 15ms (max), and goes to
+       // SLEEP_MODE_PWR_DOWN
+
+       chThdSleepMilliseconds(17);
+}
+
+__attribute__ ((weak)) void matrix_power_up(void) {}
+__attribute__ ((weak)) void matrix_power_down(void) {}
+bool suspend_wakeup_condition(void)
+{
+    matrix_power_up();
+    matrix_scan();
+    matrix_power_down();
+    for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
+        if (matrix_get_row(r)) return true;
+    }
+    return false;
+}
+
+// run immediately after wakeup
+void suspend_wakeup_init(void)
+{
+    // clear keyboard state
+    // need to do it manually, because we're running from ISR
+    //  and clear_keyboard() calls print
+    // so only clear the variables in memory
+    // the reports will be sent from main.c afterwards
+    // or if the PC asks for GET_REPORT
+    clear_mods();
+    clear_weak_mods();
+    clear_keys();
+#ifdef MOUSEKEY_ENABLE
+    mousekey_clear();
+#endif /* MOUSEKEY_ENABLE */
+#ifdef EXTRAKEY_ENABLE
+    host_system_send(0);
+    host_consumer_send(0);
+#endif /* EXTRAKEY_ENABLE */
+#ifdef BACKLIGHT_ENABLE
+    backlight_init();
+#endif /* BACKLIGHT_ENABLE */
+}
index b2526d14280723e788360bbfaa1d9c459513bc3a..c4666ebd01bfe0d1f89f6daa69af351b92622e14 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"
@@ -113,7 +115,24 @@ 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
+        // TODO: remote wakeup
+        // if(USB_Device_RemoteWakeupEnabled (USB_DRIVER.status & 2) && suspend_wakeup_condition()) {
+          // USB_Device_SendRemoteWakeup();
+        // }
+      }
+      /* 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);
index 2625c42575d6c64d35fe7b05882cb307b96c5526..872d1c65d0a24eb6d3c9856e61771e0f12931ff3 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "host.h"
 #include "debug.h"
+#include "suspend.h"
 #ifdef SLEEP_LED_ENABLE
 #include "sleep_led.h"
 #include "led.h"
@@ -356,8 +357,8 @@ static const uint8_t hid_configuration_descriptor_data[] = {
                          NUM_INTERFACES,    // bNumInterfaces
                          1,    // bConfigurationValue
                          0,    // iConfiguration
-                         0xA0, // bmAttributes
-                         50),  // bMaxPower (100mA)
+                         0xA0, // bmAttributes (RESERVED|REMOTEWAKEUP)
+                         50),  // bMaxPower (50mA)
 
   /* Interface Descriptor (9 bytes) USB spec 9.6.5, page 267-269, Table 9-12 */
   USB_DESC_INTERFACE(KBD_INTERFACE,        // bInterfaceNumber
@@ -793,7 +794,6 @@ static void usb_event_cb(USBDriver *usbp, usbevent_t event) {
 
   case USB_EVENT_SUSPEND:
     //TODO: from ISR! print("[S]");
-    //TODO: signal suspend?
 #ifdef SLEEP_LED_ENABLE
     sleep_led_enable();
 #endif /* SLEEP_LED_ENABLE */
@@ -801,7 +801,7 @@ static void usb_event_cb(USBDriver *usbp, usbevent_t event) {
 
   case USB_EVENT_WAKEUP:
     //TODO: from ISR! print("[W]");
-    //TODO: suspend_wakeup_init();
+    suspend_wakeup_init();
 #ifdef SLEEP_LED_ENABLE
     sleep_led_disable();
     // NOTE: converters may not accept this