From 02939ab1d831ab7bb02edb28cb0b21fb61bced56 Mon Sep 17 00:00:00 2001 From: tmk Date: Thu, 4 Sep 2014 05:30:00 +0900 Subject: Add battery management --- keyboard/hhkb_rn42/rn42.mk | 1 + keyboard/hhkb_rn42/rn42/battery.c | 119 ++++++++++++++++++++++++++++++++++++ keyboard/hhkb_rn42/rn42/battery.h | 34 +++++++++++ keyboard/hhkb_rn42/rn42/rn42_task.c | 71 ++++----------------- 4 files changed, 165 insertions(+), 60 deletions(-) create mode 100644 keyboard/hhkb_rn42/rn42/battery.c create mode 100644 keyboard/hhkb_rn42/rn42/battery.h diff --git a/keyboard/hhkb_rn42/rn42.mk b/keyboard/hhkb_rn42/rn42.mk index 653a809242..fd6f7db1a8 100644 --- a/keyboard/hhkb_rn42/rn42.mk +++ b/keyboard/hhkb_rn42/rn42.mk @@ -4,6 +4,7 @@ SRC += serial_uart.c \ rn42/suart.S \ rn42/rn42.c \ rn42/rn42_task.c \ + rn42/battery.c \ rn42/main.c OPT_DEFS += -DPROTOCOL_RN42 diff --git a/keyboard/hhkb_rn42/rn42/battery.c b/keyboard/hhkb_rn42/rn42/battery.c new file mode 100644 index 0000000000..32de864481 --- /dev/null +++ b/keyboard/hhkb_rn42/rn42/battery.c @@ -0,0 +1,119 @@ +#include +#include +#include "battery.h" + + +/* + * Battery + */ +void battery_init(void) +{ + // blink + battery_led(LED_ON); _delay_ms(500); + battery_led(LED_OFF); _delay_ms(500); + battery_led(LED_ON); _delay_ms(500); + battery_led(LED_OFF); _delay_ms(500); + // LED indicates charger status + battery_led(LED_CHARGER); + + // ADC setting for voltage monitor + // Ref:2.56V band-gap, Input:ADC0(PF0), Prescale:128(16MHz/128=125KHz) + ADMUX = (1< BATTERY_VOLTAGE_LOW_RECOVERY) { + low = false; + } + return low; +} + +battery_status_t battery_status(void) +{ + if (USBSTA&(1< +#include + +typedef enum { + FULL_CHARGED, + CHARGING, + DISCHARGING, + LOW_VOLTAGE, +} battery_status_t; + +typedef enum { + LED_CHARGER = 0, + LED_ON, + LED_OFF, + LED_TOGGLE, +} battery_led_t; + +/* Battery API */ +void battery_init(void); +void battery_led(battery_led_t val); +bool battery_charging(void); +uint16_t battery_voltage(void); +battery_status_t battery_status(void); + +#define BATTERY_VOLTAGE_LOW_LIMIT 3500 +#define BATTERY_VOLTAGE_LOW_RECOVERY 3700 +// ADC offset:16, resolution:5mV +#define BATTERY_ADC_OFFSET 16 +#define BATTERY_ADC_RESOLUTION 5 + +#endif diff --git a/keyboard/hhkb_rn42/rn42/rn42_task.c b/keyboard/hhkb_rn42/rn42/rn42_task.c index 2813b1c5ff..07b34e111a 100644 --- a/keyboard/hhkb_rn42/rn42/rn42_task.c +++ b/keyboard/hhkb_rn42/rn42/rn42_task.c @@ -9,6 +9,7 @@ #include "print.h" #include "timer.h" #include "command.h" +#include "battery.h" static bool config_mode = false; static bool force_usb = false; @@ -24,65 +25,9 @@ static void status_led(bool on) } } -static void battery_adc_init(void) -{ - ADMUX = (1<