Skip to content

Commit

Permalink
Add charging detection to vddh driver
Browse files Browse the repository at this point in the history
  • Loading branch information
ReFil committed Mar 21, 2024
1 parent 74db777 commit 8b0f6fd
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* SPDX-License-Identifier: MIT
*/

#ifndef ZEPHYR_INCLUDE_DRIVERS_SENSOR_BATTERY_BATTERY_VOLTAGE_DIVIDER_H_
#define ZEPHYR_INCLUDE_DRIVERS_SENSOR_BATTERY_BATTERY_VOLTAGE_DIVIDER_H_
#ifndef ZEPHYR_INCLUDE_DRIVERS_SENSOR_BATTERY_BATTERY_CHARGING_H_
#define ZEPHYR_INCLUDE_DRIVERS_SENSOR_BATTERY_BATTERY_CHARGING_H_

#ifdef __cplusplus
extern "C" {
Expand Down
6 changes: 1 addition & 5 deletions app/module/drivers/sensor/battery/battery_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
#include <errno.h>
#include <zephyr/drivers/sensor.h>

#if CONFIG_ZMK_BATTERY_VOLTAGE_DIVIDER
#include <drivers/sensor/battery/battery_voltage_divider.h>
#endif
#include <drivers/sensor/battery/battery_charging.h>

#include "battery_common.h"

Expand All @@ -26,12 +24,10 @@ int battery_channel_get(const struct battery_value *value, enum sensor_channel c
val_out->val2 = 0;
break;

#if CONFIG_ZMK_BATTERY_VOLTAGE_DIVIDER
case SENSOR_CHAN_CHARGING:
val_out->val1 = value->charging;
val_out->val2 = 0;
break;
#endif

default:
return -ENOTSUP;
Expand Down
44 changes: 41 additions & 3 deletions app/module/drivers/sensor/battery/battery_nrf_vddh.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/adc.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/logging/log.h>

#include <drivers/sensor/battery/battery_charging.h>
#include "battery_common.h"

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
Expand All @@ -23,6 +25,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

static const struct device *adc = DEVICE_DT_GET(DT_NODELABEL(adc));

struct vddh_config {
struct gpio_dt_spec chg;
};

struct vddh_data {
struct adc_channel_cfg acc;
struct adc_sequence as;
Expand All @@ -32,12 +38,13 @@ struct vddh_data {
static int vddh_sample_fetch(const struct device *dev, enum sensor_channel chan) {
// Make sure selected channel is supported
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);
return -ENOTSUP;
}

struct vddh_data *drv_data = dev->data;
const struct vddh_config *drv_cfg = dev->config;
struct adc_sequence *as = &drv_data->as;

int rc = adc_read(adc, as);
Expand All @@ -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,
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;
}

Expand All @@ -77,6 +96,7 @@ static const struct sensor_driver_api vddh_api = {

static int vddh_init(const struct device *dev) {
struct vddh_data *drv_data = dev->data;
const struct vddh_config *drv_cfg = dev->config;

if (!device_is_ready(adc)) {
LOG_ERR("ADC device is not ready %s", adc->name);
Expand Down Expand Up @@ -104,13 +124,31 @@ static int vddh_init(const struct device *dev) {
#error Unsupported ADC
#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);

#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;
}

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);
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <zephyr/logging/log.h>

#include "battery_common.h"
#include <drivers/sensor/battery/battery_voltage_divider.h>
#include <drivers/sensor/battery/battery_charging.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

Expand Down
6 changes: 6 additions & 0 deletions app/module/dts/bindings/sensor/zmk,battery-nrf-vddh.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
description: Battery SoC monitoring using nRF VDDH

compatible: "zmk,battery-nrf-vddh"

properties:
chg-gpios:
required: false
type: phandle-array
description: "A GPIO pin to report charging state to"
4 changes: 1 addition & 3 deletions app/src/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/activity.h>
#include <zmk/workqueue.h>

#if CONFIG_ZMK_BATTERY_VOLTAGE_DIVIDER
#include <drivers/sensor/battery/battery_voltage_divider.h>
#endif
#include <drivers/sensor/battery/battery_charging.h>

static uint8_t last_state_of_charge = 0;
static bool charging = 0;
Expand Down

0 comments on commit 8b0f6fd

Please sign in to comment.