power/battery: set interval to reduce report via ioctl

set time interval to control report frequency

Signed-off-by: yezhonghui <yezhonghui@xiaomi.com>
This commit is contained in:
yezhonghui
2025-01-06 18:07:55 +08:00
committed by Alan C. Assis
parent 6df4c7c009
commit 2b1c108819
4 changed files with 119 additions and 27 deletions

View File

@@ -36,6 +36,7 @@
#include <fcntl.h>
#include <nuttx/fs/fs.h>
#include <nuttx/wqueue.h>
#include <nuttx/kmalloc.h>
#include <nuttx/power/battery_charger.h>
#include <nuttx/power/battery_ioctl.h>
@@ -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);
}

View File

@@ -36,6 +36,7 @@
#include <fcntl.h>
#include <nuttx/fs/fs.h>
#include <nuttx/wqueue.h>
#include <nuttx/kmalloc.h>
#include <nuttx/power/battery_gauge.h>
#include <nuttx/power/battery_ioctl.h>
@@ -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);
}

View File

@@ -37,6 +37,7 @@
#include <nuttx/semaphore.h>
#include <nuttx/fs/fs.h>
#include <nuttx/wqueue.h>
#include <nuttx/kmalloc.h>
#include <nuttx/power/battery_monitor.h>
#include <nuttx/power/battery_ioctl.h>
@@ -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:

View File

@@ -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: