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:
parent
d08463e483
commit
388e345c28
10 changed files with 40 additions and 12 deletions
|
@ -17,6 +17,7 @@
|
||||||
zephyr,sram = &sram0;
|
zephyr,sram = &sram0;
|
||||||
zephyr,flash = &flash0;
|
zephyr,flash = &flash0;
|
||||||
zephyr,console = &cdc_acm_uart;
|
zephyr,console = &cdc_acm_uart;
|
||||||
|
zmk,battery = &vbatt;
|
||||||
};
|
};
|
||||||
|
|
||||||
leds {
|
leds {
|
||||||
|
@ -34,7 +35,7 @@
|
||||||
control-gpios = <&gpio0 12 GPIO_ACTIVE_HIGH>;
|
control-gpios = <&gpio0 12 GPIO_ACTIVE_HIGH>;
|
||||||
};
|
};
|
||||||
|
|
||||||
vbatt {
|
vbatt: vbatt {
|
||||||
compatible = "zmk,battery-voltage-divider";
|
compatible = "zmk,battery-voltage-divider";
|
||||||
label = "BATTERY";
|
label = "BATTERY";
|
||||||
io-channels = <&adc 7>;
|
io-channels = <&adc 7>;
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
zephyr,sram = &sram0;
|
zephyr,sram = &sram0;
|
||||||
zephyr,flash = &flash0;
|
zephyr,flash = &flash0;
|
||||||
zephyr,console = &cdc_acm_uart;
|
zephyr,console = &cdc_acm_uart;
|
||||||
|
zmk,battery = &vbatt;
|
||||||
zmk,kscan = &kscan0;
|
zmk,kscan = &kscan0;
|
||||||
zmk,matrix_transform = &default_transform;
|
zmk,matrix_transform = &default_transform;
|
||||||
};
|
};
|
||||||
|
@ -46,7 +47,7 @@
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
vbatt {
|
vbatt: vbatt {
|
||||||
compatible = "zmk,battery-voltage-divider";
|
compatible = "zmk,battery-voltage-divider";
|
||||||
label = "BATTERY";
|
label = "BATTERY";
|
||||||
io-channels = <&adc 2>;
|
io-channels = <&adc 2>;
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
zephyr,sram = &sram0;
|
zephyr,sram = &sram0;
|
||||||
zephyr,flash = &flash0;
|
zephyr,flash = &flash0;
|
||||||
zephyr,console = &cdc_acm_uart;
|
zephyr,console = &cdc_acm_uart;
|
||||||
|
zmk,battery = &vbatt;
|
||||||
};
|
};
|
||||||
|
|
||||||
leds {
|
leds {
|
||||||
|
@ -34,7 +35,7 @@
|
||||||
init-delay-ms = <50>;
|
init-delay-ms = <50>;
|
||||||
};
|
};
|
||||||
|
|
||||||
vbatt {
|
vbatt: vbatt {
|
||||||
compatible = "zmk,battery-voltage-divider";
|
compatible = "zmk,battery-voltage-divider";
|
||||||
label = "BATTERY";
|
label = "BATTERY";
|
||||||
io-channels = <&adc 2>;
|
io-channels = <&adc 2>;
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
zephyr,sram = &sram0;
|
zephyr,sram = &sram0;
|
||||||
zephyr,flash = &flash0;
|
zephyr,flash = &flash0;
|
||||||
zephyr,console = &cdc_acm_uart;
|
zephyr,console = &cdc_acm_uart;
|
||||||
|
zmk,battery = &vbatt;
|
||||||
zmk,kscan = &kscan0;
|
zmk,kscan = &kscan0;
|
||||||
zmk,matrix_transform = &default_transform;
|
zmk,matrix_transform = &default_transform;
|
||||||
zmk,underglow = &led_strip;
|
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>;
|
control-gpios = <&gpio0 5 GPIO_ACTIVE_LOW>;
|
||||||
};
|
};
|
||||||
|
|
||||||
vbatt {
|
vbatt: vbatt {
|
||||||
compatible = "zmk,battery-voltage-divider";
|
compatible = "zmk,battery-voltage-divider";
|
||||||
label = "BATTERY";
|
label = "BATTERY";
|
||||||
io-channels = <&adc 2>;
|
io-channels = <&adc 2>;
|
||||||
|
|
|
@ -8,13 +8,17 @@
|
||||||
#include "nice_nano.dtsi"
|
#include "nice_nano.dtsi"
|
||||||
|
|
||||||
/ {
|
/ {
|
||||||
|
chosen {
|
||||||
|
zmk,battery = &vbatt;
|
||||||
|
};
|
||||||
|
|
||||||
ext-power {
|
ext-power {
|
||||||
compatible = "zmk,ext-power-generic";
|
compatible = "zmk,ext-power-generic";
|
||||||
label = "EXT_POWER";
|
label = "EXT_POWER";
|
||||||
control-gpios = <&gpio0 13 GPIO_ACTIVE_LOW>;
|
control-gpios = <&gpio0 13 GPIO_ACTIVE_LOW>;
|
||||||
};
|
};
|
||||||
|
|
||||||
vbatt {
|
vbatt: vbatt {
|
||||||
compatible = "zmk,battery-voltage-divider";
|
compatible = "zmk,battery-voltage-divider";
|
||||||
label = "BATTERY";
|
label = "BATTERY";
|
||||||
io-channels = <&adc 2>;
|
io-channels = <&adc 2>;
|
||||||
|
|
|
@ -8,6 +8,10 @@
|
||||||
#include "nice_nano.dtsi"
|
#include "nice_nano.dtsi"
|
||||||
|
|
||||||
/ {
|
/ {
|
||||||
|
chosen {
|
||||||
|
zmk,battery = &vbatt;
|
||||||
|
};
|
||||||
|
|
||||||
ext-power {
|
ext-power {
|
||||||
compatible = "zmk,ext-power-generic";
|
compatible = "zmk,ext-power-generic";
|
||||||
label = "EXT_POWER";
|
label = "EXT_POWER";
|
||||||
|
@ -15,7 +19,7 @@
|
||||||
init-delay-ms = <50>;
|
init-delay-ms = <50>;
|
||||||
};
|
};
|
||||||
|
|
||||||
vbatt {
|
vbatt: vbatt {
|
||||||
compatible = "zmk,battery-nrf-vddh";
|
compatible = "zmk,battery-nrf-vddh";
|
||||||
label = "BATTERY";
|
label = "BATTERY";
|
||||||
};
|
};
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
zephyr,sram = &sram0;
|
zephyr,sram = &sram0;
|
||||||
zephyr,flash = &flash0;
|
zephyr,flash = &flash0;
|
||||||
zephyr,console = &cdc_acm_uart;
|
zephyr,console = &cdc_acm_uart;
|
||||||
|
zmk,battery = &vbatt;
|
||||||
};
|
};
|
||||||
|
|
||||||
leds {
|
leds {
|
||||||
|
@ -33,7 +34,7 @@
|
||||||
control-gpios = <&gpio1 9 GPIO_ACTIVE_LOW>;
|
control-gpios = <&gpio1 9 GPIO_ACTIVE_LOW>;
|
||||||
};
|
};
|
||||||
|
|
||||||
vbatt {
|
vbatt: vbatt {
|
||||||
compatible = "zmk,battery-voltage-divider";
|
compatible = "zmk,battery-voltage-divider";
|
||||||
label = "BATTERY";
|
label = "BATTERY";
|
||||||
io-channels = <&adc 2>;
|
io-channels = <&adc 2>;
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
zephyr,sram = &sram0;
|
zephyr,sram = &sram0;
|
||||||
zephyr,flash = &flash0;
|
zephyr,flash = &flash0;
|
||||||
zephyr,console = &cdc_acm_uart;
|
zephyr,console = &cdc_acm_uart;
|
||||||
|
zmk,battery = &vbatt;
|
||||||
zmk,kscan = &kscan0;
|
zmk,kscan = &kscan0;
|
||||||
zmk,matrix_transform = &default_transform;
|
zmk,matrix_transform = &default_transform;
|
||||||
};
|
};
|
||||||
|
@ -69,7 +70,7 @@
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
vbatt {
|
vbatt: vbatt {
|
||||||
compatible = "zmk,battery-voltage-divider";
|
compatible = "zmk,battery-voltage-divider";
|
||||||
label = "BATTERY";
|
label = "BATTERY";
|
||||||
io-channels = <&adc 2>;
|
io-channels = <&adc 2>;
|
||||||
|
|
|
@ -8,9 +8,10 @@
|
||||||
/ {
|
/ {
|
||||||
chosen {
|
chosen {
|
||||||
zephyr,console = &cdc_acm_uart;
|
zephyr,console = &cdc_acm_uart;
|
||||||
|
zmk,battery = &vbatt;
|
||||||
};
|
};
|
||||||
|
|
||||||
vbatt {
|
vbatt: vbatt {
|
||||||
compatible = "zmk,battery-voltage-divider";
|
compatible = "zmk,battery-voltage-divider";
|
||||||
label = "BATTERY";
|
label = "BATTERY";
|
||||||
io-channels = <&adc 7>;
|
io-channels = <&adc 7>;
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <device.h>
|
#include <device.h>
|
||||||
|
#include <devicetree.h>
|
||||||
#include <init.h>
|
#include <init.h>
|
||||||
#include <kernel.h>
|
#include <kernel.h>
|
||||||
#include <drivers/sensor.h>
|
#include <drivers/sensor.h>
|
||||||
|
@ -18,12 +19,16 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
#include <zmk/battery.h>
|
#include <zmk/battery.h>
|
||||||
#include <zmk/events/battery_state_changed.h>
|
#include <zmk/events/battery_state_changed.h>
|
||||||
|
|
||||||
const struct device *battery;
|
|
||||||
|
|
||||||
static uint8_t last_state_of_charge = 0;
|
static uint8_t last_state_of_charge = 0;
|
||||||
|
|
||||||
uint8_t zmk_battery_state_of_charge() { return last_state_of_charge; }
|
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) {
|
static int zmk_battery_update(const struct device *battery) {
|
||||||
struct sensor_value state_of_charge;
|
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);
|
K_TIMER_DEFINE(battery_timer, zmk_battery_timer, NULL);
|
||||||
|
|
||||||
static int zmk_battery_init(const struct device *_arg) {
|
static int zmk_battery_init(const struct device *_arg) {
|
||||||
|
#if !DT_HAS_CHOSEN(zmk_battery)
|
||||||
battery = device_get_binding("BATTERY");
|
battery = device_get_binding("BATTERY");
|
||||||
|
|
||||||
if (battery == NULL) {
|
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;
|
return -ENODEV;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue