]> git.friedersdorff.com Git - max/tmk_keyboard.git/commitdiff
Monitor battery and alert low voltage
authortmk <nobody@nowhere>
Thu, 4 Sep 2014 08:08:23 +0000 (17:08 +0900)
committertmk <nobody@nowhere>
Thu, 4 Sep 2014 08:08:23 +0000 (17:08 +0900)
keyboard/hhkb_rn42/rn42/battery.c
keyboard/hhkb_rn42/rn42/battery.h
keyboard/hhkb_rn42/rn42/rn42_task.c

index 32de86448136bca3fd3539631ae87413d0aebe60..c6988fe33645b40ce54b0cf3e7965ed7ad77c068 100644 (file)
@@ -49,10 +49,11 @@ bool battery_charging(void)
 {
     if (!(USBSTA&(1<<VBUS))) return false;
 
-    // MCP73831:STAT
-    //   HiZ:    Shutdown/No Battery
-    //   Low:    Charging
-    //   Hi:     Charged
+    // Charger Status:
+    //   MCP73831   MCP73832   LTC4054  Status
+    //   Hi-Z       Hi-Z       Hi-Z     Shutdown/No Battery
+    //   Low        Low        Low      Charging
+    //   Hi         Hi-Z       Hi-Z     Charged
 
     // preserve last register status
     uint8_t ddrf_prev  = DDRF;
@@ -68,6 +69,10 @@ bool battery_charging(void)
     DDRF  = (DDRF&~(1<<5))  | (ddrf_prev&(1<<5));
     PORTF = (PORTF&~(1<<5)) | (portf_prev&(1<<5));
 
+    // TODO: With MCP73831 this can not get stable status when charging.
+    // LED is powered from PSEL line(USB or Lipo)
+    // due to weak low output of STAT pin?
+    // due to pull-up'd via resitor and LED?
     return charging;
 }
 
index 60fc8adfcb17c814bc25406e155604d179ce8977..180d4dcfaaaac19c34da7a753f592b79e9be04d6 100644 (file)
@@ -9,6 +9,7 @@ typedef enum {
     CHARGING,
     DISCHARGING,
     LOW_VOLTAGE,
+    UNKNOWN,
 } battery_status_t;
 
 typedef enum {
index 07b34e111a4317f8b15382fb1f77cdc9d479f4ce..30914452e530296afcac18e7c45c3da5b2eb7b25 100644 (file)
@@ -81,13 +81,50 @@ void rn42_task(void)
         }
     }
 
-    /* Low voltage alert */
-    if (battery_status() == LOW_VOLTAGE) {
-        battery_led(LED_ON);
-    } else {
-        battery_led(LED_CHARGER);
+
+    static uint16_t prev_timer = 0;
+    static uint8_t sec = 0;
+    // NOTE: not exact 1 sec
+    if (timer_elapsed(prev_timer) > 1000) {
+        /* every second */
+        prev_timer = timer_read();
+
+        /* Low voltage alert */
+        uint8_t bs = battery_status();
+        if (bs == LOW_VOLTAGE) {
+            battery_led(LED_ON);
+        } else {
+            battery_led(LED_CHARGER);
+        }
+
+        static uint8_t prev_status = UNKNOWN;
+        if (bs != prev_status) {
+            prev_status = bs;
+            switch (bs) {
+                case FULL_CHARGED:  xprintf("FULL_CHARGED\n"); break;
+                case CHARGING:      xprintf("CHARGING\n"); break;
+                case DISCHARGING:   xprintf("DISCHARGING\n"); break;
+                case LOW_VOLTAGE:   xprintf("LOW_VOLTAGE\n"); break;
+                default:            xprintf("UNKNOWN STATUS\n"); break;
+            };
+        }
+
+        /* every minute */
+        if (sec == 0) {
+            uint32_t t = timer_read32()/1000;
+            uint16_t v = battery_voltage();
+            uint8_t h = t/3600;
+            uint8_t m = t%3600/60;
+            uint8_t s = t%60;
+            xprintf("%02u:%02u:%02u\t%umV\n", h, m, s, v);
+            /* TODO: xprintf doesn't work for this.
+            xprintf("%02u:%02u:%02u\t%umV\n", (t/3600), (t%3600/60), (t%60), v);
+            */
+        }
+        sec++; sec = sec%60;
     }
 
+
     /* Connection monitor */
     if (rn42_linked()) {
         status_led(true);