From 2b1c108819b197db8a8a93dea2b79465dab4200e Mon Sep 17 00:00:00 2001 From: yezhonghui Date: Mon, 6 Jan 2025 18:07:55 +0800 Subject: [PATCH] power/battery: set interval to reduce report via ioctl set time interval to control report frequency Signed-off-by: yezhonghui --- drivers/power/battery/battery_charger.c | 48 +++++++++++++++++++----- drivers/power/battery/battery_gauge.c | 48 +++++++++++++++++++----- drivers/power/battery/battery_monitor.c | 49 ++++++++++++++++++++----- include/nuttx/power/battery_ioctl.h | 1 + 4 files changed, 119 insertions(+), 27 deletions(-) diff --git a/drivers/power/battery/battery_charger.c b/drivers/power/battery/battery_charger.c index 6f3bda6f974..418e476fbd0 100644 --- a/drivers/power/battery/battery_charger.c +++ b/drivers/power/battery/battery_charger.c @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -61,7 +62,9 @@ struct battery_charger_priv_s mutex_t lock; sem_t wait; uint32_t mask; + clock_t interval; /* tick unit */ FAR struct pollfd *fds; + struct work_s work; }; /**************************************************************************** @@ -102,11 +105,28 @@ static const struct file_operations g_batteryops = * Private Functions ****************************************************************************/ +static void battery_charger_work(FAR void *arg) +{ + FAR struct battery_charger_priv_s *priv = + (FAR struct battery_charger_priv_s *)arg; + FAR struct pollfd *fds = priv->fds; + int semcnt; + + if (priv->mask != 0) + { + poll_notify(&fds, 1, POLLIN); + + nxsem_get_value(&priv->wait, &semcnt); + if (semcnt < 1) + { + nxsem_post(&priv->wait); + } + } +} + static int battery_charger_notify(FAR struct battery_charger_priv_s *priv, uint32_t mask) { - FAR struct pollfd *fds = priv->fds; - int semcnt; int ret; ret = nxmutex_lock(&priv->lock); @@ -116,14 +136,16 @@ static int battery_charger_notify(FAR struct battery_charger_priv_s *priv, } priv->mask |= mask; - if (priv->mask) + if (priv->mask != 0) { - poll_notify(&fds, 1, POLLIN); - - nxsem_get_value(&priv->wait, &semcnt); - if (semcnt < 1) + if (priv->interval > 0) { - nxsem_post(&priv->wait); + work_queue(LPWORK, &priv->work, battery_charger_work, priv, + priv->interval); + } + else + { + battery_charger_work(priv); } } @@ -267,6 +289,7 @@ static int bat_charger_ioctl(FAR struct file *filep, int cmd, { FAR struct inode *inode = filep->f_inode; FAR struct battery_charger_dev_s *dev = inode->i_private; + FAR struct battery_charger_priv_s *priv = filep->f_priv; int ret; /* Enforce mutually exclusive access to the battery driver */ @@ -406,6 +429,13 @@ static int bat_charger_ioctl(FAR struct file *filep, int cmd, } break; + case BATIOC_SET_DEBOUNCE: + { + priv->interval = arg; + ret = OK; + } + break; + default: batinfo("ERROR: Unrecognized cmd: %d\n", cmd); ret = -ENOTTY; @@ -438,7 +468,7 @@ static int bat_charger_poll(FAR struct file *filep, { priv->fds = fds; fds->priv = &priv->fds; - if (priv->mask) + if (priv->mask != 0) { poll_notify(&fds, 1, POLLIN); } diff --git a/drivers/power/battery/battery_gauge.c b/drivers/power/battery/battery_gauge.c index beb63bf6012..a8b55081720 100644 --- a/drivers/power/battery/battery_gauge.c +++ b/drivers/power/battery/battery_gauge.c @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -61,7 +62,9 @@ struct battery_gauge_priv_s mutex_t lock; sem_t wait; uint32_t mask; + clock_t interval; /* tick unit */ FAR struct pollfd *fds; + struct work_s work; }; /**************************************************************************** @@ -104,11 +107,28 @@ static const struct file_operations g_batteryops = * Private Functions ****************************************************************************/ +static void battery_gauge_work(FAR void *arg) +{ + FAR struct battery_gauge_priv_s *priv = + (FAR struct battery_gauge_priv_s *)arg; + FAR struct pollfd *fds = priv->fds; + int semcnt; + + if (priv->mask != 0) + { + poll_notify(&fds, 1, POLLIN); + + nxsem_get_value(&priv->wait, &semcnt); + if (semcnt < 1) + { + nxsem_post(&priv->wait); + } + } +} + static int battery_gauge_notify(FAR struct battery_gauge_priv_s *priv, uint32_t mask) { - FAR struct pollfd *fds = priv->fds; - int semcnt; int ret; ret = nxmutex_lock(&priv->lock); @@ -118,14 +138,16 @@ static int battery_gauge_notify(FAR struct battery_gauge_priv_s *priv, } priv->mask |= mask; - if (priv->mask) + if (priv->mask != 0) { - poll_notify(&fds, 1, POLLIN); - - nxsem_get_value(&priv->wait, &semcnt); - if (semcnt < 1) + if (priv->interval > 0) { - nxsem_post(&priv->wait); + work_queue(LPWORK, &priv->work, battery_gauge_work, priv, + priv->interval); + } + else + { + battery_gauge_work(priv); } } @@ -271,6 +293,7 @@ static int bat_gauge_ioctl(FAR struct file *filep, { FAR struct inode *inode = filep->f_inode; FAR struct battery_gauge_dev_s *dev = inode->i_private; + FAR struct battery_gauge_priv_s *priv = filep->f_priv; int ret; /* Enforce mutually exclusive access to the battery driver */ @@ -366,6 +389,13 @@ static int bat_gauge_ioctl(FAR struct file *filep, } break; + case BATIOC_SET_DEBOUNCE: + { + priv->interval = arg; + ret = OK; + } + break; + default: batinfo("ERROR: Unrecognized cmd: %d\n", cmd); ret = -ENOTTY; @@ -398,7 +428,7 @@ static int bat_gauge_poll(FAR struct file *filep, { priv->fds = fds; fds->priv = &priv->fds; - if (priv->mask) + if (priv->mask != 0) { poll_notify(&fds, 1, POLLIN); } diff --git a/drivers/power/battery/battery_monitor.c b/drivers/power/battery/battery_monitor.c index 825b316b7a4..4a9bdd3ab32 100644 --- a/drivers/power/battery/battery_monitor.c +++ b/drivers/power/battery/battery_monitor.c @@ -37,6 +37,7 @@ #include #include +#include #include #include #include @@ -62,7 +63,9 @@ struct battery_monitor_priv_s mutex_t lock; sem_t wait; uint32_t mask; + clock_t interval; /* tick unit */ FAR struct pollfd *fds; + struct work_s work; }; /**************************************************************************** @@ -103,11 +106,28 @@ static const struct file_operations g_batteryops = * Private Functions ****************************************************************************/ +static void battery_monitor_work(FAR void *arg) +{ + FAR struct battery_monitor_priv_s *priv = + (FAR struct battery_monitor_priv_s *)arg; + FAR struct pollfd *fds = priv->fds; + int semcnt; + + if (priv->mask != 0) + { + poll_notify(&fds, 1, POLLIN); + + nxsem_get_value(&priv->wait, &semcnt); + if (semcnt < 1) + { + nxsem_post(&priv->wait); + } + } +} + static int battery_monitor_notify(FAR struct battery_monitor_priv_s *priv, uint32_t mask) { - FAR struct pollfd *fds = priv->fds; - int semcnt; int ret; ret = nxmutex_lock(&priv->lock); @@ -117,14 +137,16 @@ static int battery_monitor_notify(FAR struct battery_monitor_priv_s *priv, } priv->mask |= mask; - if (priv->mask) + if (priv->mask != 0) { - poll_notify(&fds, 1, POLLIN); - - nxsem_get_value(&priv->wait, &semcnt); - if (semcnt < 1) + if (priv->interval != 0) { - nxsem_post(&priv->wait); + work_queue(LPWORK, &priv->work, battery_monitor_work, priv, + priv->interval); + } + else + { + battery_monitor_work(priv); } } @@ -268,6 +290,7 @@ static int bat_monitor_ioctl(FAR struct file *filep, int cmd, { FAR struct inode *inode = filep->f_inode; FAR struct battery_monitor_dev_s *dev = inode->i_private; + FAR struct battery_monitor_priv_s *priv = filep->f_priv; int ret; /* Enforce mutually exclusive access to the battery driver */ @@ -435,6 +458,13 @@ static int bat_monitor_ioctl(FAR struct file *filep, int cmd, } break; + case BATIOC_SET_DEBOUNCE: + { + priv->interval = arg; + ret = OK; + } + break; + default: batinfo("ERROR: Unrecognized cmd: %d\n", cmd); ret = -ENOTTY; @@ -467,7 +497,7 @@ static int bat_monitor_poll(FAR struct file *filep, { priv->fds = fds; fds->priv = &priv->fds; - if (priv->mask) + if (priv->mask != 0) { poll_notify(&fds, 1, POLLIN); } @@ -518,6 +548,7 @@ int battery_monitor_changed(FAR struct battery_monitor_dev_s *dev, struct battery_monitor_priv_s, node) { battery_monitor_notify(priv, mask); + priv->mask |= mask; } out: diff --git a/include/nuttx/power/battery_ioctl.h b/include/nuttx/power/battery_ioctl.h index 90bf3a87d6a..fa873bf7bef 100644 --- a/include/nuttx/power/battery_ioctl.h +++ b/include/nuttx/power/battery_ioctl.h @@ -59,6 +59,7 @@ #define BATIOC_GET_VOLTAGE _BATIOC(0x0012) #define BATIOC_VOLTAGE_INFO _BATIOC(0x0013) #define BATIOC_GET_PROTOCOL _BATIOC(0x0014) +#define BATIOC_SET_DEBOUNCE _BATIOC(0x0015) /* Special input values for BATIOC_INPUT_CURRENT that may optionally * be supported by lower-half driver: