aboutsummaryrefslogtreecommitdiff
path: root/keyboard/hhkb_rn42/rn42/rn42_task.c
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2014-09-04 05:30:00 +0900
committertmk <nobody@nowhere>2014-09-04 05:30:00 +0900
commit02939ab1d831ab7bb02edb28cb0b21fb61bced56 (patch)
treefe55ff730af3c2b4e13a4077725f965d651e3ad5 /keyboard/hhkb_rn42/rn42/rn42_task.c
parent807ed33a9a29ee3c5248226d0e865d0580d7eebc (diff)
downloadqmk_firmware-02939ab1d831ab7bb02edb28cb0b21fb61bced56.tar.gz
qmk_firmware-02939ab1d831ab7bb02edb28cb0b21fb61bced56.zip
Add battery management
Diffstat (limited to 'keyboard/hhkb_rn42/rn42/rn42_task.c')
-rw-r--r--keyboard/hhkb_rn42/rn42/rn42_task.c71
1 files changed, 11 insertions, 60 deletions
diff --git a/keyboard/hhkb_rn42/rn42/rn42_task.c b/keyboard/hhkb_rn42/rn42/rn42_task.c
index 2813b1c5f..07b34e111 100644
--- a/keyboard/hhkb_rn42/rn42/rn42_task.c
+++ b/keyboard/hhkb_rn42/rn42/rn42_task.c
@@ -9,6 +9,7 @@
9#include "print.h" 9#include "print.h"
10#include "timer.h" 10#include "timer.h"
11#include "command.h" 11#include "command.h"
12#include "battery.h"
12 13
13static bool config_mode = false; 14static bool config_mode = false;
14static bool force_usb = false; 15static bool force_usb = false;
@@ -24,65 +25,9 @@ static void status_led(bool on)
24 } 25 }
25} 26}
26 27
27static void battery_adc_init(void)
28{
29 ADMUX = (1<<REFS1) | (1<<REFS0); // Ref:2.56V band-gap, Input:ADC0(PF0)
30 ADCSRA = (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); // Prescale:128 16MHz/128=125KHz
31 ADCSRA |= (1<<ADEN); // enable ADC
32}
33
34static uint16_t battery_adc(void)
35{
36 volatile uint16_t bat;
37 ADCSRA |= (1<<ADEN);
38
39 // discard first result
40 ADCSRA |= (1<<ADSC);
41 while (ADCSRA & (1<<ADSC)) ;
42 bat = ADC;
43
44 // discard second result
45 ADCSRA |= (1<<ADSC);
46 while (ADCSRA & (1<<ADSC)) ;
47 bat = ADC;
48
49 ADCSRA |= (1<<ADSC);
50 while (ADCSRA & (1<<ADSC)) ;
51 bat = ADC;
52
53 ADCSRA &= ~(1<<ADEN);
54 return bat;
55}
56
57static void battery_led(bool on)
58{
59 if (on) {
60 DDRF |= (1<<5);
61 PORTF &= ~(1<<5); // Low
62 } else {
63 DDRF &= ~(1<<5);
64 PORTF &= ~(1<<5); // HiZ
65 }
66}
67
68static bool battery_charging(void)
69{
70 // MCP73831:STAT
71 // Hi-Z: Shutdown/No Battery
72 // Low: Charging
73 // Hi: Charged
74 DDRF &= ~(1<<5);
75 PORTF |= (1<<5);
76 return PINF&(1<<5) ? false : true;
77}
78
79void rn42_task_init(void) 28void rn42_task_init(void)
80{ 29{
81 battery_adc_init(); 30 battery_init();
82
83 // battery charging(HiZ)
84 DDRF &= ~(1<<5);
85 PORTF &= ~(1<<5);
86} 31}
87 32
88void rn42_task(void) 33void rn42_task(void)
@@ -136,7 +81,12 @@ void rn42_task(void)
136 } 81 }
137 } 82 }
138 83
139 /* Battery monitor */ 84 /* Low voltage alert */
85 if (battery_status() == LOW_VOLTAGE) {
86 battery_led(LED_ON);
87 } else {
88 battery_led(LED_CHARGER);
89 }
140 90
141 /* Connection monitor */ 91 /* Connection monitor */
142 if (rn42_linked()) { 92 if (rn42_linked()) {
@@ -214,12 +164,13 @@ bool command_extra(uint8_t code)
214 xprintf("config_mode: %X\n", config_mode); 164 xprintf("config_mode: %X\n", config_mode);
215 xprintf("VBUS: %X\n", USBSTA&(1<<VBUS)); 165 xprintf("VBUS: %X\n", USBSTA&(1<<VBUS));
216 xprintf("battery_charging: %X\n", battery_charging()); 166 xprintf("battery_charging: %X\n", battery_charging());
167 xprintf("battery_status: %X\n", battery_status());
217 return true; 168 return true;
218 case KC_B: 169 case KC_B:
219 // battery monitor 170 // battery monitor
220 t = timer_read32()/1000; 171 t = timer_read32()/1000;
221 b = battery_adc(); 172 b = battery_voltage();
222 xprintf("BAT: %umV(%04X)\t", (b-16)*5, b); 173 xprintf("BAT: %umV\t", b);
223 xprintf("%02u:", t/3600); 174 xprintf("%02u:", t/3600);
224 xprintf("%02u:", t%3600/60); 175 xprintf("%02u:", t%3600/60);
225 xprintf("%02u\n", t%60); 176 xprintf("%02u\n", t%60);