diff --git a/keyboard/hhkb_rn42/rn42.mk b/keyboard/hhkb_rn42/rn42.mk index 653a8092426..fd6f7db1a8e 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 00000000000..32de8644813 --- /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 2813b1c5ff4..07b34e111a4 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<