feat(battery)!: Add chosen node for battery

battery.c now uses the zmk,battery chosen node to select a battery
sensor. Using the node labeled "BATTERY" is maintained for backwards
compatibility but is now deprecated. Custom boards should switch to
using the chosen node.

# Conflicts:
#	app/boards/arm/bluemicro840/bluemicro840_v1.dts
#	app/boards/arm/nice60/nice60.dts
#	app/boards/arm/nrfmicro/nrfmicro_13.dts

# Conflicts:
#	app/boards/arm/bluemicro840/bluemicro840_v1.dts
This commit is contained in:
Joel Spadin 2021-07-17 17:49:37 -05:00 committed by Pete Johanson
parent d08463e483
commit 388e345c28
10 changed files with 40 additions and 12 deletions

View file

@ -17,6 +17,7 @@
zephyr,sram = &sram0;
zephyr,flash = &flash0;
zephyr,console = &cdc_acm_uart;
zmk,battery = &vbatt;
};
leds {
@ -34,7 +35,7 @@
control-gpios = <&gpio0 12 GPIO_ACTIVE_HIGH>;
};
vbatt {
vbatt: vbatt {
compatible = "zmk,battery-voltage-divider";
label = "BATTERY";
io-channels = <&adc 7>;

View file

@ -17,6 +17,7 @@
zephyr,sram = &sram0;
zephyr,flash = &flash0;
zephyr,console = &cdc_acm_uart;
zmk,battery = &vbatt;
zmk,kscan = &kscan0;
zmk,matrix_transform = &default_transform;
};
@ -46,7 +47,7 @@
};
};
vbatt {
vbatt: vbatt {
compatible = "zmk,battery-voltage-divider";
label = "BATTERY";
io-channels = <&adc 2>;

View file

@ -17,6 +17,7 @@
zephyr,sram = &sram0;
zephyr,flash = &flash0;
zephyr,console = &cdc_acm_uart;
zmk,battery = &vbatt;
};
leds {
@ -34,7 +35,7 @@
init-delay-ms = <50>;
};
vbatt {
vbatt: vbatt {
compatible = "zmk,battery-voltage-divider";
label = "BATTERY";
io-channels = <&adc 2>;

View file

@ -19,6 +19,7 @@
zephyr,sram = &sram0;
zephyr,flash = &flash0;
zephyr,console = &cdc_acm_uart;
zmk,battery = &vbatt;
zmk,kscan = &kscan0;
zmk,matrix_transform = &default_transform;
zmk,underglow = &led_strip;
@ -81,7 +82,7 @@ RC(4,0) RC(4,1) RC(4,2) RC(4,5) R
control-gpios = <&gpio0 5 GPIO_ACTIVE_LOW>;
};
vbatt {
vbatt: vbatt {
compatible = "zmk,battery-voltage-divider";
label = "BATTERY";
io-channels = <&adc 2>;

View file

@ -8,13 +8,17 @@
#include "nice_nano.dtsi"
/ {
chosen {
zmk,battery = &vbatt;
};
ext-power {
compatible = "zmk,ext-power-generic";
label = "EXT_POWER";
control-gpios = <&gpio0 13 GPIO_ACTIVE_LOW>;
};
vbatt {
vbatt: vbatt {
compatible = "zmk,battery-voltage-divider";
label = "BATTERY";
io-channels = <&adc 2>;

View file

@ -8,6 +8,10 @@
#include "nice_nano.dtsi"
/ {
chosen {
zmk,battery = &vbatt;
};
ext-power {
compatible = "zmk,ext-power-generic";
label = "EXT_POWER";
@ -15,7 +19,7 @@
init-delay-ms = <50>;
};
vbatt {
vbatt: vbatt {
compatible = "zmk,battery-nrf-vddh";
label = "BATTERY";
};

View file

@ -17,6 +17,7 @@
zephyr,sram = &sram0;
zephyr,flash = &flash0;
zephyr,console = &cdc_acm_uart;
zmk,battery = &vbatt;
};
leds {
@ -33,7 +34,7 @@
control-gpios = <&gpio1 9 GPIO_ACTIVE_LOW>;
};
vbatt {
vbatt: vbatt {
compatible = "zmk,battery-voltage-divider";
label = "BATTERY";
io-channels = <&adc 2>;

View file

@ -17,6 +17,7 @@
zephyr,sram = &sram0;
zephyr,flash = &flash0;
zephyr,console = &cdc_acm_uart;
zmk,battery = &vbatt;
zmk,kscan = &kscan0;
zmk,matrix_transform = &default_transform;
};
@ -69,7 +70,7 @@
};
};
vbatt {
vbatt: vbatt {
compatible = "zmk,battery-voltage-divider";
label = "BATTERY";
io-channels = <&adc 2>;

View file

@ -8,9 +8,10 @@
/ {
chosen {
zephyr,console = &cdc_acm_uart;
zmk,battery = &vbatt;
};
vbatt {
vbatt: vbatt {
compatible = "zmk,battery-voltage-divider";
label = "BATTERY";
io-channels = <&adc 7>;

View file

@ -5,6 +5,7 @@
*/
#include <device.h>
#include <devicetree.h>
#include <init.h>
#include <kernel.h>
#include <drivers/sensor.h>
@ -18,12 +19,16 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/battery.h>
#include <zmk/events/battery_state_changed.h>
const struct device *battery;
static uint8_t last_state_of_charge = 0;
uint8_t zmk_battery_state_of_charge() { return last_state_of_charge; }
#if DT_HAS_CHOSEN(zmk_battery)
static const struct device *const battery = DEVICE_DT_GET(DT_CHOSEN(zmk_battery));
#else
static const struct device *battery;
#endif
static int zmk_battery_update(const struct device *battery) {
struct sensor_value state_of_charge;
@ -75,10 +80,18 @@ static void zmk_battery_timer(struct k_timer *timer) { k_work_submit(&battery_wo
K_TIMER_DEFINE(battery_timer, zmk_battery_timer, NULL);
static int zmk_battery_init(const struct device *_arg) {
#if !DT_HAS_CHOSEN(zmk_battery)
battery = device_get_binding("BATTERY");
if (battery == NULL) {
LOG_DBG("No battery device labelled BATTERY found.");
return -ENODEV;
}
LOG_WRN("Finding battery device labeled BATTERY is deprecated. Use zmk,battery chosen node.");
#endif
if (!device_is_ready(battery)) {
LOG_ERR("Battery device \"%s\" is not ready", battery->name);
return -ENODEV;
}