feat(battery): Initial battery charging state setection
feat(battery): Initial battery charging state setection
This commit is contained in:
parent
2adaa00d10
commit
b82be6b485
10 changed files with 135 additions and 4 deletions
21
app/include/drivers/sensor/battery/battery_charging.h
Normal file
21
app/include/drivers/sensor/battery/battery_charging.h
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022 The ZMK Contributors
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ZEPHYR_INCLUDE_DRIVERS_SENSOR_BATTERY_BATTERY_CHARGING_H_
|
||||||
|
#define ZEPHYR_INCLUDE_DRIVERS_SENSOR_BATTERY_BATTERY_CHARGING_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <zephyr/drivers/sensor.h>
|
||||||
|
|
||||||
|
enum sensor_channel_bvd {
|
||||||
|
/** Charging state, bool **/
|
||||||
|
SENSOR_CHAN_CHARGING = SENSOR_CHAN_PRIV_START,
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
|
@ -7,3 +7,4 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
uint8_t zmk_battery_state_of_charge(void);
|
uint8_t zmk_battery_state_of_charge(void);
|
||||||
|
bool zmk_battery_charging(void);
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
# SPDX-License-Identifier: MIT
|
# SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
zephyr_include_directories(.)
|
zephyr_include_directories(.)
|
||||||
|
zephyr_include_directories(${CMAKE_SOURCE_DIR}/include)
|
||||||
|
|
||||||
zephyr_library()
|
zephyr_library()
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,8 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <zephyr/drivers/sensor.h>
|
#include <zephyr/drivers/sensor.h>
|
||||||
|
|
||||||
|
#include <drivers/sensor/battery/battery_charging.h>
|
||||||
|
|
||||||
#include "battery_common.h"
|
#include "battery_common.h"
|
||||||
|
|
||||||
int battery_channel_get(const struct battery_value *value, enum sensor_channel chan,
|
int battery_channel_get(const struct battery_value *value, enum sensor_channel chan,
|
||||||
|
@ -22,6 +24,11 @@ int battery_channel_get(const struct battery_value *value, enum sensor_channel c
|
||||||
val_out->val2 = 0;
|
val_out->val2 = 0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case SENSOR_CHAN_CHARGING:
|
||||||
|
val_out->val1 = value->charging;
|
||||||
|
val_out->val2 = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return -ENOTSUP;
|
return -ENOTSUP;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ struct battery_value {
|
||||||
uint16_t adc_raw;
|
uint16_t adc_raw;
|
||||||
uint16_t millivolts;
|
uint16_t millivolts;
|
||||||
uint8_t state_of_charge;
|
uint8_t state_of_charge;
|
||||||
|
bool charging;
|
||||||
};
|
};
|
||||||
|
|
||||||
int battery_channel_get(const struct battery_value *value, enum sensor_channel chan,
|
int battery_channel_get(const struct battery_value *value, enum sensor_channel chan,
|
||||||
|
|
|
@ -11,10 +11,12 @@
|
||||||
|
|
||||||
#include <zephyr/device.h>
|
#include <zephyr/device.h>
|
||||||
#include <zephyr/devicetree.h>
|
#include <zephyr/devicetree.h>
|
||||||
|
#include <zephyr/drivers/gpio.h>
|
||||||
#include <zephyr/drivers/adc.h>
|
#include <zephyr/drivers/adc.h>
|
||||||
#include <zephyr/drivers/sensor.h>
|
#include <zephyr/drivers/sensor.h>
|
||||||
#include <zephyr/logging/log.h>
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
|
#include <drivers/sensor/battery/battery_charging.h>
|
||||||
#include "battery_common.h"
|
#include "battery_common.h"
|
||||||
|
|
||||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
@ -23,6 +25,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
static const struct device *adc = DEVICE_DT_GET(DT_NODELABEL(adc));
|
static const struct device *adc = DEVICE_DT_GET(DT_NODELABEL(adc));
|
||||||
|
|
||||||
|
struct vddh_config {
|
||||||
|
struct gpio_dt_spec chg;
|
||||||
|
};
|
||||||
|
|
||||||
struct vddh_data {
|
struct vddh_data {
|
||||||
struct adc_channel_cfg acc;
|
struct adc_channel_cfg acc;
|
||||||
struct adc_sequence as;
|
struct adc_sequence as;
|
||||||
|
@ -32,12 +38,13 @@ struct vddh_data {
|
||||||
static int vddh_sample_fetch(const struct device *dev, enum sensor_channel chan) {
|
static int vddh_sample_fetch(const struct device *dev, enum sensor_channel chan) {
|
||||||
// Make sure selected channel is supported
|
// Make sure selected channel is supported
|
||||||
if (chan != SENSOR_CHAN_GAUGE_VOLTAGE && chan != SENSOR_CHAN_GAUGE_STATE_OF_CHARGE &&
|
if (chan != SENSOR_CHAN_GAUGE_VOLTAGE && chan != SENSOR_CHAN_GAUGE_STATE_OF_CHARGE &&
|
||||||
chan != SENSOR_CHAN_ALL) {
|
(enum sensor_channel_bvd)chan != SENSOR_CHAN_CHARGING && chan != SENSOR_CHAN_ALL) {
|
||||||
LOG_DBG("Selected channel is not supported: %d.", chan);
|
LOG_DBG("Selected channel is not supported: %d.", chan);
|
||||||
return -ENOTSUP;
|
return -ENOTSUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct vddh_data *drv_data = dev->data;
|
struct vddh_data *drv_data = dev->data;
|
||||||
|
const struct vddh_config *drv_cfg = dev->config;
|
||||||
struct adc_sequence *as = &drv_data->as;
|
struct adc_sequence *as = &drv_data->as;
|
||||||
|
|
||||||
int rc = adc_read(adc, as);
|
int rc = adc_read(adc, as);
|
||||||
|
@ -61,6 +68,18 @@ static int vddh_sample_fetch(const struct device *dev, enum sensor_channel chan)
|
||||||
LOG_DBG("ADC raw %d ~ %d mV => %d%%", drv_data->value.adc_raw, drv_data->value.millivolts,
|
LOG_DBG("ADC raw %d ~ %d mV => %d%%", drv_data->value.adc_raw, drv_data->value.millivolts,
|
||||||
drv_data->value.state_of_charge);
|
drv_data->value.state_of_charge);
|
||||||
|
|
||||||
|
#if DT_INST_NODE_HAS_PROP(0, chg_gpios)
|
||||||
|
int raw = gpio_pin_get_dt(&drv_cfg->chg);
|
||||||
|
if (raw == -EIO || raw == -EWOULDBLOCK) {
|
||||||
|
LOG_DBG("Failed to read chg status: %d", raw);
|
||||||
|
return raw;
|
||||||
|
} else {
|
||||||
|
bool charging = raw;
|
||||||
|
LOG_DBG("Charging state: %d", raw);
|
||||||
|
drv_data->value.charging = charging;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,6 +96,7 @@ static const struct sensor_driver_api vddh_api = {
|
||||||
|
|
||||||
static int vddh_init(const struct device *dev) {
|
static int vddh_init(const struct device *dev) {
|
||||||
struct vddh_data *drv_data = dev->data;
|
struct vddh_data *drv_data = dev->data;
|
||||||
|
const struct vddh_config *drv_cfg = dev->config;
|
||||||
|
|
||||||
if (!device_is_ready(adc)) {
|
if (!device_is_ready(adc)) {
|
||||||
LOG_ERR("ADC device is not ready %s", adc->name);
|
LOG_ERR("ADC device is not ready %s", adc->name);
|
||||||
|
@ -104,13 +124,31 @@ static int vddh_init(const struct device *dev) {
|
||||||
#error Unsupported ADC
|
#error Unsupported ADC
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
const int rc = adc_channel_setup(adc, &drv_data->acc);
|
int rc = adc_channel_setup(adc, &drv_data->acc);
|
||||||
LOG_DBG("VDDHDIV5 setup returned %d", rc);
|
LOG_DBG("VDDHDIV5 setup returned %d", rc);
|
||||||
|
|
||||||
|
#if DT_INST_NODE_HAS_PROP(0, chg_gpios)
|
||||||
|
if (!device_is_ready(drv_cfg->chg.port)) {
|
||||||
|
LOG_ERR("GPIO port for chg reading is not ready");
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
|
rc = gpio_pin_configure_dt(&drv_cfg->chg, GPIO_INPUT);
|
||||||
|
if (rc != 0) {
|
||||||
|
LOG_ERR("Failed to set chg feed %u: %d", drv_cfg->chg.pin, rc);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
#endif // DT_INST_NODE_HAS_PROP(0, chg_gpios)
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct vddh_data vddh_data;
|
static struct vddh_data vddh_data;
|
||||||
|
|
||||||
DEVICE_DT_INST_DEFINE(0, &vddh_init, NULL, &vddh_data, NULL, POST_KERNEL,
|
static const struct vddh_config vddh_cfg = {
|
||||||
|
#if DT_INST_NODE_HAS_PROP(0, chg_gpios)
|
||||||
|
.chg = GPIO_DT_SPEC_INST_GET(0, chg_gpios),
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
DEVICE_DT_INST_DEFINE(0, &vddh_init, NULL, &vddh_data, &vddh_cfg, POST_KERNEL,
|
||||||
CONFIG_SENSOR_INIT_PRIORITY, &vddh_api);
|
CONFIG_SENSOR_INIT_PRIORITY, &vddh_api);
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
#include <zephyr/logging/log.h>
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
#include "battery_common.h"
|
#include "battery_common.h"
|
||||||
|
#include <drivers/sensor/battery/battery_charging.h>
|
||||||
|
|
||||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
|
@ -24,6 +25,7 @@ struct io_channel_config {
|
||||||
struct bvd_config {
|
struct bvd_config {
|
||||||
struct io_channel_config io_channel;
|
struct io_channel_config io_channel;
|
||||||
struct gpio_dt_spec power;
|
struct gpio_dt_spec power;
|
||||||
|
struct gpio_dt_spec chg;
|
||||||
uint32_t output_ohm;
|
uint32_t output_ohm;
|
||||||
uint32_t full_ohm;
|
uint32_t full_ohm;
|
||||||
};
|
};
|
||||||
|
@ -42,7 +44,7 @@ static int bvd_sample_fetch(const struct device *dev, enum sensor_channel chan)
|
||||||
|
|
||||||
// Make sure selected channel is supported
|
// Make sure selected channel is supported
|
||||||
if (chan != SENSOR_CHAN_GAUGE_VOLTAGE && chan != SENSOR_CHAN_GAUGE_STATE_OF_CHARGE &&
|
if (chan != SENSOR_CHAN_GAUGE_VOLTAGE && chan != SENSOR_CHAN_GAUGE_STATE_OF_CHARGE &&
|
||||||
chan != SENSOR_CHAN_ALL) {
|
(enum sensor_channel_bvd)chan != SENSOR_CHAN_CHARGING && chan != SENSOR_CHAN_ALL) {
|
||||||
LOG_DBG("Selected channel is not supported: %d.", chan);
|
LOG_DBG("Selected channel is not supported: %d.", chan);
|
||||||
return -ENOTSUP;
|
return -ENOTSUP;
|
||||||
}
|
}
|
||||||
|
@ -93,6 +95,18 @@ static int bvd_sample_fetch(const struct device *dev, enum sensor_channel chan)
|
||||||
}
|
}
|
||||||
#endif // DT_INST_NODE_HAS_PROP(0, power_gpios)
|
#endif // DT_INST_NODE_HAS_PROP(0, power_gpios)
|
||||||
|
|
||||||
|
#if DT_INST_NODE_HAS_PROP(0, chg_gpios)
|
||||||
|
int raw = gpio_pin_get_dt(&drv_cfg->chg);
|
||||||
|
if (raw == -EIO || raw == -EWOULDBLOCK) {
|
||||||
|
LOG_DBG("Failed to read chg status: %d", raw);
|
||||||
|
return raw;
|
||||||
|
} else {
|
||||||
|
bool charging = raw;
|
||||||
|
LOG_DBG("Charging state: %d", raw);
|
||||||
|
drv_data->value.charging = charging;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,6 +144,18 @@ static int bvd_init(const struct device *dev) {
|
||||||
}
|
}
|
||||||
#endif // DT_INST_NODE_HAS_PROP(0, power_gpios)
|
#endif // DT_INST_NODE_HAS_PROP(0, power_gpios)
|
||||||
|
|
||||||
|
#if DT_INST_NODE_HAS_PROP(0, chg_gpios)
|
||||||
|
if (!device_is_ready(drv_cfg->chg.port)) {
|
||||||
|
LOG_ERR("GPIO port for chg reading is not ready");
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
|
rc = gpio_pin_configure_dt(&drv_cfg->chg, GPIO_INPUT);
|
||||||
|
if (rc != 0) {
|
||||||
|
LOG_ERR("Failed to set chg feed %u: %d", drv_cfg->chg.pin, rc);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
#endif // DT_INST_NODE_HAS_PROP(0, chg_gpios)
|
||||||
|
|
||||||
drv_data->as = (struct adc_sequence){
|
drv_data->as = (struct adc_sequence){
|
||||||
.channels = BIT(0),
|
.channels = BIT(0),
|
||||||
.buffer = &drv_data->value.adc_raw,
|
.buffer = &drv_data->value.adc_raw,
|
||||||
|
@ -166,6 +192,9 @@ static const struct bvd_config bvd_cfg = {
|
||||||
},
|
},
|
||||||
#if DT_INST_NODE_HAS_PROP(0, power_gpios)
|
#if DT_INST_NODE_HAS_PROP(0, power_gpios)
|
||||||
.power = GPIO_DT_SPEC_INST_GET(0, power_gpios),
|
.power = GPIO_DT_SPEC_INST_GET(0, power_gpios),
|
||||||
|
#endif
|
||||||
|
#if DT_INST_NODE_HAS_PROP(0, chg_gpios)
|
||||||
|
.chg = GPIO_DT_SPEC_INST_GET(0, chg_gpios),
|
||||||
#endif
|
#endif
|
||||||
.output_ohm = DT_INST_PROP(0, output_ohms),
|
.output_ohm = DT_INST_PROP(0, output_ohms),
|
||||||
.full_ohm = DT_INST_PROP(0, full_ohms),
|
.full_ohm = DT_INST_PROP(0, full_ohms),
|
||||||
|
|
|
@ -4,3 +4,9 @@
|
||||||
description: Battery SoC monitoring using nRF VDDH
|
description: Battery SoC monitoring using nRF VDDH
|
||||||
|
|
||||||
compatible: "zmk,battery-nrf-vddh"
|
compatible: "zmk,battery-nrf-vddh"
|
||||||
|
|
||||||
|
properties:
|
||||||
|
chg-gpios:
|
||||||
|
required: false
|
||||||
|
type: phandle-array
|
||||||
|
description: "A GPIO pin to report charging state to"
|
||||||
|
|
|
@ -6,3 +6,9 @@ description: Battery SoC monitoring using voltage divider
|
||||||
compatible: "zmk,battery-voltage-divider"
|
compatible: "zmk,battery-voltage-divider"
|
||||||
|
|
||||||
include: voltage-divider.yaml
|
include: voltage-divider.yaml
|
||||||
|
|
||||||
|
properties:
|
||||||
|
chg-gpios:
|
||||||
|
required: false
|
||||||
|
type: phandle-array
|
||||||
|
description: "A GPIO pin to report charging state to"
|
||||||
|
|
|
@ -22,9 +22,13 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
#include <zmk/activity.h>
|
#include <zmk/activity.h>
|
||||||
#include <zmk/workqueue.h>
|
#include <zmk/workqueue.h>
|
||||||
|
|
||||||
|
#include <drivers/sensor/battery/battery_charging.h>
|
||||||
|
|
||||||
static uint8_t last_state_of_charge = 0;
|
static uint8_t last_state_of_charge = 0;
|
||||||
|
static bool charging = 0;
|
||||||
|
|
||||||
uint8_t zmk_battery_state_of_charge(void) { return last_state_of_charge; }
|
uint8_t zmk_battery_state_of_charge(void) { return last_state_of_charge; }
|
||||||
|
bool zmk_battery_charging(void) { return charging; }
|
||||||
|
|
||||||
#if DT_HAS_CHOSEN(zmk_battery)
|
#if DT_HAS_CHOSEN(zmk_battery)
|
||||||
static const struct device *const battery = DEVICE_DT_GET(DT_CHOSEN(zmk_battery));
|
static const struct device *const battery = DEVICE_DT_GET(DT_CHOSEN(zmk_battery));
|
||||||
|
@ -67,6 +71,23 @@ static int zmk_battery_update(const struct device *battery) {
|
||||||
(struct zmk_battery_state_changed){.state_of_charge = last_state_of_charge});
|
(struct zmk_battery_state_changed){.state_of_charge = last_state_of_charge});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if DT_NODE_HAS_PROP(DT_CHOSEN(zmk_battery), chg_gpios)
|
||||||
|
|
||||||
|
rc = sensor_sample_fetch_chan(battery, SENSOR_CHAN_CHARGING);
|
||||||
|
|
||||||
|
if (rc != 0) {
|
||||||
|
LOG_DBG("Failed to fetch battery values: %d", rc);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
struct sensor_value charging_state;
|
||||||
|
rc = sensor_channel_get(battery, SENSOR_CHAN_CHARGING, &charging_state);
|
||||||
|
if (rc != 0) {
|
||||||
|
LOG_DBG("Failed to get battery charging status: %d", rc);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
charging = charging_state.val1;
|
||||||
|
#endif
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue