Battery monitor support (#404)

* Adding support for BQ769x0 Battery Monitor IC (Work In Progress)
* Additional changes to support BQ769x0
* Store cell count and chip type when setting up
* Added shutdown, limits, charge/discharge switch, and clear faults operations
* Added support for current measurement; some cleanup
* Updated temperature reporting.  Fixed negative current reporting.
* When setting safety limits, update limit structure with actual values used.
* Added note on battery limit structure
* Updates to BQ769x0.  Re-ordered fault reporting, added fault cache, added ordered fault clearing
This commit is contained in:
macman88
2020-02-28 18:17:27 -06:00
committed by Gregory Nutt
parent 922e67c7cc
commit 43dfbdb348
9 changed files with 3409 additions and 12 deletions
+28
View File
@@ -459,5 +459,33 @@ config I2C_BQ2429X
config I2C_MAX1704X
bool
default y if MAX1704X
config BATTERY_MONITOR
bool "Battery Monitor/Management support"
default n
config BQ769X0
bool "BQ769X0 Battery monitor support"
default n
select I2C
select I2C_BQ769X0
depends on BATTERY_MONITOR
---help---
The BQ76920/BQ76930/BQ76940 provide monitoring, balancing, and
protection features for up to 15 cells in series.
config I2C_BQ769X0
bool
default y if BQ769X0
if BQ769X0
config DEBUG_BQ769X0
bool "BQ769X0 debug features"
default n
config BQ769X0_USE_INTERNAL_TS
bool "Use internal temperature sensor on BQ769X0"
default n
endif
endif # POWER
+26
View File
@@ -166,6 +166,32 @@ POWER_CFLAGS := ${shell $(INCDIR) $(INCDIROPT) "$(CC)" $(TOPDIR)$(DELIM)drivers$
endif
# Add battery monitor drivers
ifeq ($(CONFIG_BATTERY_MONITOR),y)
CSRCS += battery_monitor.c
# Add I2C-based battery monitor drivers
ifeq ($(CONFIG_I2C),y)
# Add the BQ769x0 I2C-based battery monitor driver
ifeq ($(CONFIG_I2C_BQ769X0),y)
CSRCS += bq769x0.c
endif
endif
# Include battery suport in the build
POWER_DEPPATH := --dep-path power
POWER_VPATH := :power
POWER_CFLAGS := ${shell $(INCDIR) $(INCDIROPT) "$(CC)" $(TOPDIR)$(DELIM)drivers$(DELIM)power}
endif
# Include power management in the build
DEPPATH += $(POWER_DEPPATH)
+352
View File
@@ -0,0 +1,352 @@
/****************************************************************************
* drivers/power/battery_monitor.c
* Upper-half, character driver for battery manager & monitor ICs.
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdbool.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/semaphore.h>
#include <nuttx/fs/fs.h>
#include <nuttx/power/battery_monitor.h>
#include <nuttx/power/battery_ioctl.h>
/* This driver requires:
*
* CONFIG_BATTERY_MONITOR - Upper half battery driver support
*/
#if defined(CONFIG_BATTERY_MONITOR)
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* Character driver methods */
static int bat_monitor_open(FAR struct file *filep);
static int bat_monitor_close(FAR struct file *filep);
static ssize_t bat_monitor_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static ssize_t bat_monitor_write(FAR struct file *filep,
FAR const char *buffer, size_t buflen);
static int bat_monitor_ioctl(FAR struct file *filep, int cmd,
unsigned long arg);
/****************************************************************************
* Private Data
****************************************************************************/
static const struct file_operations g_batteryops =
{
bat_monitor_open,
bat_monitor_close,
bat_monitor_read,
bat_monitor_write,
NULL,
bat_monitor_ioctl,
NULL
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: bat_monitor_open
*
* Description:
* This function is called whenever the battery device is opened.
*
****************************************************************************/
static int bat_monitor_open(FAR struct file *filep)
{
return OK;
}
/****************************************************************************
* Name: bat_monitor_close
*
* Description:
* This routine is called when the battery device is closed.
*
****************************************************************************/
static int bat_monitor_close(FAR struct file *filep)
{
return OK;
}
/****************************************************************************
* Name: bat_monitor_read
****************************************************************************/
static ssize_t bat_monitor_read(FAR struct file *filep, FAR char *buffer,
size_t buflen)
{
/* Return nothing read */
return 0;
}
/****************************************************************************
* Name: bat_monitor_write
****************************************************************************/
static ssize_t bat_monitor_write(FAR struct file *filep,
FAR const char *buffer, size_t buflen)
{
/* Return nothing written */
return 0;
}
/****************************************************************************
* Name: bat_monitor_ioctl
****************************************************************************/
static int bat_monitor_ioctl(FAR struct file *filep, int cmd,
unsigned long arg)
{
FAR struct inode *inode = filep->f_inode;
FAR struct battery_monitor_dev_s *dev = inode->i_private;
int ret;
/* Enforce mutually exclusive access to the battery driver */
ret = nxsem_wait(&dev->batsem);
if (ret < 0)
{
return ret; /* Probably -EINTR */
}
/* Process the IOCTL command */
ret = -EINVAL; /* Assume a bad argument */
switch (cmd)
{
case BATIOC_STATE:
{
FAR int *ptr = (FAR int *)((uintptr_t)arg);
if (ptr)
{
ret = dev->ops->state(dev, ptr);
}
}
break;
case BATIOC_HEALTH:
{
FAR int *ptr = (FAR int *)((uintptr_t)arg);
if (ptr)
{
ret = dev->ops->health(dev, ptr);
}
}
break;
case BATIOC_ONLINE:
{
FAR bool *ptr = (FAR bool *)((uintptr_t)arg);
if (ptr)
{
ret = dev->ops->online(dev, ptr);
}
}
break;
case BATIOC_VOLTAGE:
{
FAR int *voltsp = (FAR int *)((uintptr_t)arg);
if (voltsp)
{
ret = dev->ops->voltage(dev, voltsp);
}
}
break;
case BATIOC_CURRENT:
{
FAR struct battery_monitor_current_s *current =
(FAR struct battery_monitor_current_s *)((uintptr_t)arg);
if (current)
{
ret = dev->ops->current(dev, current);
}
}
break;
case BATIOC_OPERATE:
{
FAR int *ptr = (FAR int *)((uintptr_t)arg);
if (ptr)
{
ret = dev->ops->operate(dev, (uintptr_t)arg);
}
}
break;
case BATIOC_CELLVOLTAGE:
{
FAR struct battery_monitor_voltage_s *ptr =
(FAR struct battery_monitor_voltage_s *)((uintptr_t)arg);
if (ptr)
{
ret = dev->ops->cell_voltage(dev, ptr);
}
}
break;
case BATIOC_BALANCE:
{
FAR struct battery_monitor_balance_s *ptr =
(FAR struct battery_monitor_balance_s *)((uintptr_t)arg);
if (ptr)
{
ret = dev->ops->balance(dev, ptr);
}
}
break;
case BATIOC_SHUTDOWN:
{
/* Options are not currently used by shutdown */
ret = dev->ops->shutdown(dev, (uintptr_t)arg);
}
break;
case BATIOC_SETLIMITS:
{
FAR struct battery_monitor_limits_s *ptr =
(FAR struct battery_monitor_limits_s *)((uintptr_t)arg);
if (ptr)
{
ret = dev->ops->setlimits(dev, ptr);
}
}
break;
case BATIOC_CHGDSG:
{
FAR struct battery_monitor_switches_s *ptr =
(FAR struct battery_monitor_switches_s *)((uintptr_t)arg);
if (ptr)
{
ret = dev->ops->chgdsg(dev, ptr);
}
}
break;
case BATIOC_CLEARFAULTS:
{
/* Options are not currently used by clear faults operation */
ret = dev->ops->clearfaults(dev, (uintptr_t)arg);
}
break;
case BATIOC_TEMPERATURE:
{
FAR struct battery_monitor_temperature_s *ptr =
(FAR struct battery_monitor_temperature_s *)((uintptr_t)arg);
if (ptr)
{
ret = dev->ops->temperature(dev, ptr);
}
}
break;
case BATIOC_COULOMBS:
{
FAR int *coulombp = (FAR int *)((uintptr_t)arg);
if (coulombp)
{
ret = dev->ops->coulombs(dev, coulombp);
}
}
break;
default:
_err("ERROR: Unrecognized cmd: %d\n", cmd);
ret = -ENOTTY;
break;
}
nxsem_post(&dev->batsem);
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: battery_monitor_register
*
* Description:
* Register a lower half battery driver with the common, upper-half
* battery driver.
*
* Input Parameters:
* devpath - The location in the pseudo-filesystem to create the driver.
* Recommended standard is "/dev/bat0", "/dev/bat1", etc.
* dev - An instance of the battery state structure .
*
* Returned Value:
* Zero on success or a negated errno value on failure.
*
****************************************************************************/
int battery_monitor_register(FAR const char *devpath,
FAR struct battery_monitor_dev_s *dev)
{
int ret;
/* Initialize the semaphore */
nxsem_init(&dev->batsem, 0, 1);
/* Register the character driver */
ret = register_driver(devpath, &g_batteryops, 0555, dev);
if (ret < 0)
{
_err("ERROR: Failed to register driver: %d\n", ret);
}
return ret;
}
#endif /* CONFIG_BATTERY_MONITOR */
File diff suppressed because it is too large Load Diff
+11
View File
@@ -88,6 +88,17 @@ uint8_t crc8(FAR const uint8_t *src, size_t len);
uint8_t crc8ccitt(FAR const uint8_t *src, size_t len);
/****************************************************************************
* Name: crc8part
*
* Description:
* Continue CRC calculation on a part of the buffer using the polynomial
* x^8+x^2+x^1+1 (aka "0x07" poly).
*
****************************************************************************/
uint8_t crc8ccittpart(FAR const uint8_t *src, size_t len, uint8_t crc8val);
#undef EXTERN
#ifdef __cplusplus
}
+8
View File
@@ -61,6 +61,14 @@
#define BATIOC_INPUT_CURRENT _BATIOC(0x0006)
#define BATIOC_CAPACITY _BATIOC(0x0007)
#define BATIOC_OPERATE _BATIOC(0x0008)
#define BATIOC_CELLVOLTAGE _BATIOC(0x0009)
#define BATIOC_TEMPERATURE _BATIOC(0x000A)
#define BATIOC_BALANCE _BATIOC(0x000B)
#define BATIOC_SHUTDOWN _BATIOC(0x000C)
#define BATIOC_SETLIMITS _BATIOC(0x000D)
#define BATIOC_CHGDSG _BATIOC(0x000E)
#define BATIOC_CLEARFAULTS _BATIOC(0x000F)
#define BATIOC_COULOMBS _BATIOC(0x0010)
/****************************************************************************
* Public Types
+431
View File
@@ -0,0 +1,431 @@
/****************************************************************************
* include/nuttx/power/battery_monitor.h
* NuttX Battery battery manager & monitor interfaces
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __INCLUDE_NUTTX_POWER_BATTERY_MONITOR_H
#define __INCLUDE_NUTTX_POWER_BATTERY_MONITOR_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/fs/ioctl.h>
#include <stdbool.h>
#include <stdint.h>
#include <semaphore.h>
#include <fixedmath.h>
#ifdef CONFIG_BATTERY_MONITOR
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* CONFIG_BATTERY_MONITOR - Upper half battery monitor driver support
*
* Specific, lower-half drivers will have other configuration requirements
* such as:
*
* CONFIG_I2C - I2C support *may* be needed
* CONFIG_I2C_BQ769X0 - The BQ769X0 driver must be explicitly selected.
*/
/* IOCTL Commands ***********************************************************/
/* The upper-half battery monitor driver provides a character driver
* "wrapper" around the lower-half battery monitor driver that does all of
* the real work. Since there is no real data transfer to/or from a battery,
* all of the driver interaction is through IOCTL commands. The IOCTL
* commands supported by the upper-half driver simply provide calls into the
* lower half as summarized below:
*
* BATIOC_STATE - Return the current state of the battery (see
* enum battery_monitor_status_e).
* Input value: A pointer to type int.
* BATIOC_HEALTH - Return the current health of the battery (see
* enum battery_monitor_health_e).
* Input value: A pointer to type int.
* BATIOC_ONLINE - Return 1 if the battery is online; 0 if offline.
* Input value: A pointer to type bool.
* BATIOC_VOLTAGE - Return the current battery pack voltage in microvolts.
* Input value: A pointer to type int.
* BATIOC_CELLVOLTAGE - Return the voltage of all cells in microvolts.
* Input value: A pointer to type battery_monitor_voltage_s.
* BATIOC_CURRENT - Return the current battery pack current in microamps.
* The returned data includes duration over which measurement was done, if
* provided by the hardware.
* Input value: A pointer to type battery_monitor_current_s.
* BATIOC_SOC - Return the state-of-charge of the battery, in percent.
* Input value: A pointer to type b16_t.
* BATIOC_COULOMBS - Return the value of the monitor's coulomb counter,
* if provided by the hardware
* Input value: A pointer to type int.
* BATIOC_TEMPERATURE - Return the value of any temperature sensors attached
* to the monitor, in microvolts.
* Input value: A pointer to type battery_monitor_temperature_s.
* BATIOC_BALANCE - Set the monitor's battery balancing switches.
* Input value: A pointer to type battery_monitor_balance_s.
* BATIOC_SHUTDOWN - Put the device into low-power shutdown mode.
* Input value: None.
* BATIOC_SETLIMITS - Set the device's safety limits for voltage, current,
* etc.
* Input value: A pointer to type battery_monitor_limits_s.
* BATIOC_CHGDSG - Set the device's charge and discharge switches.
* Input value: A pointer to type battery_monitor_switches_s.
* BATIOC_CLEARFAULTS - Clear the device's most recent fault.
* Input value: None.
* BATIOC_OPERATE - Perform miscellaneous, device-specific charger operation.
* Input value: An uintptr_t that can hold a pointer to struct
* batio_operate_msg_s.
*/
/* Special input values for BATIOC_INPUT_CURRENT that may optionally
* be supported by lower-half driver:
*/
#define BATTERY_INPUT_CURRENT_EXT_LIM (-1) /* External input current limit */
/****************************************************************************
* Public Types
****************************************************************************/
/* Battery status */
enum battery_monitor_status_e
{
BATTERY_UNKNOWN = 0, /* Battery state is not known */
BATTERY_FAULT, /* Charger reported a fault, get health for more info */
BATTERY_IDLE, /* Not full, not charging, not discharging */
BATTERY_FULL, /* Full, not discharging */
BATTERY_CHARGING, /* Not full, charging */
BATTERY_DISCHARGING /* Probably not full, discharging */
};
/* Battery Health status */
enum battery_monitor_health_e
{
BATTERY_HEALTH_UNKNOWN = 0, /* Battery health state is not known */
BATTERY_HEALTH_GOOD, /* Battery is in good condition */
BATTERY_HEALTH_DEAD, /* Battery is dead, nothing we can do */
BATTERY_HEALTH_OVERHEAT, /* Battery is over recommended temperature */
BATTERY_HEALTH_OVERVOLTAGE, /* Battery voltage is over recommended level */
BATTERY_HEALTH_UNDERVOLTAGE, /* Battery monitor reported an unspecified failure */
BATTERY_HEALTH_OVERCURRENT, /* Battery monitor reported an overcurrent event */
BATTERY_HEALTH_SHORT_CIRCUIT, /* Battery monitor reported a short circuit event */
BATTERY_HEALTH_UNSPEC_FAIL, /* Battery monitor reported an unspecified failure */
BATTERY_HEALTH_COLD, /* Battery is under recommended temperature */
BATTERY_HEALTH_WD_TMR_EXP, /* Battery WatchDog Timer Expired */
BATTERY_HEALTH_DISCONNECTED /* Battery is not connected */
};
struct battery_monitor_voltage_s
{
/* Before call, contains the desired number of cells to read.
* After call, contains the actual number of cells read.
*/
int cell_count;
/* Pointer to array where cell voltages should be stored.
* MUST contain at least cell_count elements.
* Cell voltages are stored in microvolts (uV)
* Cell voltages in this array should be ordered according to the
* physical layout of cells in the system. Driver should rearrange
* voltage values as necssary to present the user with a contiguous
* list of cell voltages in the expected order.
*/
uint32_t *cell_voltages;
};
struct battery_monitor_temperature_s
{
/* Before call, contains the desired number of temperature sensors to read.
* After call, contains the actual number of temperature sensors read.
*/
int sensor_count;
/* Pointer to array where temperature values should be stored.
* MUST contain at least sensor_count elements.
* Temperature values are stored in microvolts (uV)
* It is up to the application to convert these to
* actual temperature values, as the system could have
* any number of different types of sensors hooked up,
* each of which have a different conversion between voltage
* and current.
*/
uint32_t *temperatures;
};
struct battery_monitor_balance_s
{
int balance_count;
/* Pointer to array where balance switch values should be stored.
* MUST contain at least balance_count elements.
* Balance switch is turned on if true, off if false
* Array indices for balance switches should correspond to those
* for cell voltage. The driver must rearrange the balance values as
* necessary to make this happen.
*/
bool *balance;
};
struct battery_monitor_limits_s
{
/* The driver may overwrite any value in this structure to indicate the
* actual value that was set if the exact requested value was not
* available.
*
* All voltage limits are per-cell.
*/
uint32_t overvoltage_limit; /* Overvoltage trip threshold, in uV */
uint32_t undervoltage_limit; /* Undervoltage trip threshold, in uV */
uint32_t overcurrent_limit; /* Overcurrent trip threshold, in mA */
uint32_t shortcircuit_limit; /* Short circuit current limit, in mA */
uint32_t overvoltage_delay; /* Delay before overvoltage trips, in uS */
uint32_t undervoltage_delay; /* Delay before undervoltage trips, in uS */
uint32_t overcurrent_delay; /* Delay before overcurrent trips, in uS */
uint32_t shortcircuit_delay; /* Delay before short circuit trips, in uS */
};
struct battery_monitor_switches_s
{
/* Sets the state of the CHARGE switch, which allows the
* battery to accept current.
*/
bool charge;
/* Sets the state of the DISCHARGE switch, which allows the
* battery to supply current.
*/
bool discharge;
};
struct battery_monitor_current_s
{
/* The value of current measured by the monitor IC, in uA */
int32_t current;
/* The time over which the above current was measured,
* in uS. The application may request a specific time interval
* by filling in this field when calling the BATIOC_CURRENT function,
* and the driver may or may not choose to honor it. The driver
* will always overwrite this field with the actual measurement time.
* A value of 0 means that instantaneous current should be measured.
*/
uint32_t time;
};
/* This structure defines the lower half battery interface */
struct battery_monitor_dev_s;
struct battery_monitor_operations_s
{
/* Return the current battery state (see enum battery_monitor_status_e) */
int (*state)(struct battery_monitor_dev_s *dev, int *status);
/* Return the current battery health (see enum battery_monitor_health_e) */
int (*health)(struct battery_monitor_dev_s *dev, int *health);
/* Return true if the battery is online */
int (*online)(struct battery_monitor_dev_s *dev, bool *status);
/* Get the battery pack voltage */
int (*voltage)(struct battery_monitor_dev_s *dev, int *value);
/* Get the battery cell voltages */
int (*cell_voltage)(struct battery_monitor_dev_s *dev,
struct battery_monitor_voltage_s *cellv);
/* Get the battery pack current */
int (*current)(struct battery_monitor_dev_s *dev,
struct battery_monitor_current_s *current);
/* Get the battery pack state of charge */
int (*soc)(struct battery_monitor_dev_s *dev, b16_t *value);
/* Get the battery pack Couloumb count value*/
int (*coulombs)(struct battery_monitor_dev_s *dev, int *value);
/* Read battery pack temperature sensor(s) */
int (*temperature)(struct battery_monitor_dev_s *dev,
struct battery_monitor_temperature_s *temps);
/* Set balance switches on battery cells */
int (*balance)(struct battery_monitor_dev_s *dev,
struct battery_monitor_balance_s *bal);
/* Put monitor device into low-power shutdown mode */
int (*shutdown)(struct battery_monitor_dev_s *dev, uintptr_t param);
/* Configure safety limits for the device */
int (*setlimits)(struct battery_monitor_dev_s *dev,
struct battery_monitor_limits_s *limits);
/* Set the state of charge/discharge switches to allow battery to source/sink power */
int (*chgdsg)(struct battery_monitor_dev_s *dev,
struct battery_monitor_switches_s *sw);
/* Clear battery monitor faults */
int (*clearfaults)(struct battery_monitor_dev_s *dev, uintptr_t param);
/* Do device specific operation */
int (*operate)(struct battery_monitor_dev_s *dev, uintptr_t param);
};
/* This structure defines the battery driver state structure */
struct battery_monitor_dev_s
{
/* Fields required by the upper-half driver */
FAR const struct battery_monitor_operations_s *ops; /* Battery operations */
sem_t batsem; /* Enforce mutually exclusive access */
/* Data fields specific to the lower-half driver may follow */
};
/****************************************************************************
* Public Data
****************************************************************************/
#ifndef __ASSEMBLY__
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: battery_monitor_register
*
* Description:
* Register a lower half battery driver with the common, upper-half
* battery driver.
*
* Input Parameters:
* devpath - The location in the pseudo-filesystem to create the driver.
* Recommended standard is "/dev/bat0", "/dev/bat1", etc.
* dev - An instance of the battery state structure .
*
* Returned Value:
* Zero on success or a negated errno value on failure.
*
****************************************************************************/
int battery_monitor_register(FAR const char *devpath,
FAR struct battery_monitor_dev_s *dev);
/****************************************************************************
* Name: BQ769X0_initialize
*
* Description:
* Initialize the BQ769X0 battery driver and return an instance of the
* lower-half interface that may be used with battery_monitor_register().
*
* This is for:
* BQ7692000XXX
* BQ7693000XXX
* bq7694000XXX
*
* This driver requires:
*
* CONFIG_BATTERY_MONITOR - Upper half battery monitor driver support
* CONFIG_I2C - I2C support
* CONFIG_I2C_BQ769X0 - And the driver must be explictly selected.
*
* Input Parameters:
* i2c - An instance of the I2C interface to use to communicate with
* the BQ769X0
* addr - The I2C address of the BQ769X0 (Can be 0x08 or 0x18).
* frequency - The I2C frequency
* crc - True if the device has CRC enabled (see TI datasheet)
* cellcount - The number of battery cells attached to the BQ769X0. The
* mapping of the cells changes based on count - see datasheet.
* chip - The chip type (either CHIP_BQ76920, CHIP_BQ76930, or
* CHIP_BQ76940). This is used to map cell numbers when the
* full capacity of the chip is not used. See the TI datasheet
* for cell wiring information.
* sense_r - The value of the current sense resistor, in micro ohms.
* This value is used to calculate reported current, and when
* setting overcurrent thresholds.
* Returned Value:
* A pointer to the initialized battery driver instance. A NULL pointer
* is returned on a failure to initialize the BQ769X0 lower half.
*
****************************************************************************/
#if defined(CONFIG_I2C) && defined(CONFIG_I2C_BQ769X0)
struct i2c_master_s;
FAR struct battery_monitor_dev_s *bq769x0_initialize(FAR struct i2c_master_s *i2c,
uint8_t addr,
uint32_t frequency,
bool crc,
uint8_t cellcount,
uint8_t chip,
uint32_t sense_r);
#endif
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __ASSEMBLY__ */
#endif /* CONFIG_BATTERY_MONITOR */
#endif /* __INCLUDE_NUTTX_POWER_BATTERY_H */
+284
View File
@@ -0,0 +1,284 @@
/****************************************************************************
* drivers/power/bq769x0.h
* Lower half driver for 769x0 battery monitor
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __DRIVERS_POWER_BQ769X0_H
#define __DRIVERS_POWER_BQ769X0_H
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Chip variant definitions */
#define CHIP_BQ76920 0
#define CHIP_BQ76930 1
#define CHIP_BQ76940 2
/* Auxiliary Definitions */
#define BQ769X0_BASE_GAIN 365 /* uV/LSB */
#define BQ769X0_MAX_CELLS 15 /* No more than 15 cells per chip*/
#define BQ769X0_BAL_REG_COUNT 3 /* CELBAL1, CELBAL2, CELBAL3 */
#define BQ769X0_BAL_BITS_PER_REG 5
#define BQ76920_MIN_CELL_COUNT 3
#define BQ76920_MAX_CELL_COUNT 5
#define BQ76930_MIN_CELL_COUNT 6
#define BQ76930_MAX_CELL_COUNT 10
#define BQ76940_MIN_CELL_COUNT 9
#define BQ76940_MAX_CELL_COUNT 15
#define BQ76920_TEMP_COUNT 1
#define BQ76930_TEMP_COUNT 2
#define BQ76940_TEMP_COUNT 3
#define BQ769X0_CC_CFG_DEFAULT_VAL 0x19 /* Per Datasheet */
#define BQ769X0_CELLBAL_MASK 0x1f /* 5 LSBs in each register */
#define BQ769X0_CC_TIME 250 /* milliseconds per CC sample */
#define BQ769X0_CC_POLL_INTERVAL 50 /* milliseconds (arbitrary) */
#define BQ769X0_CC_SCALE 8440 /* nanovolts / LSB */
/* BQ769x0 Register Definitions *********************************************/
/* System Control & Configuration Registers */
#define BQ769X0_REG_SYS_STAT 0x00
#define BQ769X0_REG_CELLBAL1 0x01
#define BQ769X0_REG_CELLBAL2 0x02 /* Only on BQ76930 and BQ76940 */
#define BQ769X0_REG_CELLBAL3 0x03 /* Only on BQ76940 */
#define BQ769X0_REG_SYS_CTRL1 0x04
#define BQ769X0_REG_SYS_CTRL2 0x05
#define BQ769X0_REG_PROTECT1 0x06
#define BQ769X0_REG_PROTECT2 0x07
#define BQ769X0_REG_PROTECT3 0x08
#define BQ769X0_REG_OV_TRIP 0x09
#define BQ769X0_REG_UV_TRIP 0x0a
#define BQ769X0_REG_CC_CFG 0x0b
/* Cell Voltage Registers
* BQ76920 can use VC1 - VC5
* BQ76930 can use VC1 - VC10
* BQ76940 can use VC1 - VC15
*/
#define BQ769X0_REG_VC1_HI 0x0c
#define BQ769X0_REG_VC1_LO 0x0d
#define BQ769X0_REG_VC2_HI 0x0e
#define BQ769X0_REG_VC2_LO 0x0f
#define BQ769X0_REG_VC3_HI 0x10
#define BQ769X0_REG_VC3_LO 0x11
#define BQ769X0_REG_VC4_HI 0x12
#define BQ769X0_REG_VC4_LO 0x13
#define BQ769X0_REG_VC5_HI 0x14
#define BQ769X0_REG_VC5_LO 0x15
#define BQ769X0_REG_VC6_HI 0x16
#define BQ769X0_REG_VC6_LO 0x17
#define BQ769X0_REG_VC7_HI 0x18
#define BQ769X0_REG_VC7_LO 0x19
#define BQ769X0_REG_VC8_HI 0x1a
#define BQ769X0_REG_VC8_LO 0x1b
#define BQ769X0_REG_VC9_HI 0x1c
#define BQ769X0_REG_VC9_LO 0x1d
#define BQ769X0_REG_VC10_HI 0x1e
#define BQ769X0_REG_VC10_LO 0x1f
#define BQ769X0_REG_VC11_HI 0x20
#define BQ769X0_REG_VC11_LO 0x21
#define BQ769X0_REG_VC12_HI 0x22
#define BQ769X0_REG_VC12_LO 0x23
#define BQ769X0_REG_VC13_HI 0x24
#define BQ769X0_REG_VC13_LO 0x25
#define BQ769X0_REG_VC14_HI 0x26
#define BQ769X0_REG_VC14_LO 0x27
#define BQ769X0_REG_VC15_HI 0x28
#define BQ769X0_REG_VC15_LO 0x29
/* System Voltage, Temperature, and Current Registers */
#define BQ769X0_REG_BAT_HI 0x2a
#define BQ769X0_REG_BAT_LO 0x2b
#define BQ769X0_REG_TS1_HI 0x2c
#define BQ769X0_REG_TS1_LO 0x2d
#define BQ769X0_REG_TS2_HI 0x2e
#define BQ769X0_REG_TS2_LO 0x2f
#define BQ769X0_REG_TS3_HI 0x30
#define BQ769X0_REG_TS3_LO 0x31
#define BQ769X0_REG_CC_HI 0x32
#define BQ769X0_REG_CC_LO 0x33
/* ADC Calibration Registers */
#define BQ769X0_REG_ADCGAIN1 0x50
#define BQ769X0_REG_ADCOFFSET 0x51
#define BQ769X0_REG_ADCGAIN2 0x59
/* REG_SYS_STAT */
#define BQ769X0_CC_READY (1 << 7) /* Updated Coulomb counter reading is available */
/* Bit 6: Reserved */
#define BQ769X0_DEVICE_XREADY (1 << 5) /* Internal chip fault detected */
#define BQ769X0_OVRD_ALERT (1 << 4) /* External pull-up detected on ALERT pin */
#define BQ769X0_UV (1 << 3) /* Undervoltage fault */
#define BQ769X0_OV (1 << 2) /* Overvoltage fault */
#define BQ769X0_SCD (1 << 1) /* Short circuit in discharge fault */
#define BQ769X0_OCD (1 << 0) /* Over current in discharge fault */
#define BQ769X0_FAULT_MASK (0x3f) /* Bottom 6 bits */
/* REG_SYS_CTRL1 */
#define BQ769X0_LOAD_PRESENT (1 << 7) /* CHG pin is higher than VLOAD_DETECT, external load is present */
/* Bit 6: Reserved */
/* Bit 5: Reserved */
#define BQ769X0_ADC_EN (1 << 4) /* Enable voltage, temperature measurement and OV protection */
#define BQ769X0_TEMP_SEL (1 << 3) /* 0 = use onboard temp sensors, 1 = use external temp sensors */
/* Bit 2: Reserved */
#define BQ769X0_SHUT_A (1 << 1) /* Shutdown command, bit A */
#define BQ769X0_SHUT_B (1 << 0) /* Shutdown command, bit B */
#define BQ769X0_SYS_CTRL1_WRITE_MASK (0x1b) /* Bits 0, 1, 3, and 4 are writeable */
#define BQ769X0_SYS_CTRL1_SHUTDOWN_MASK (0x03) /* Bits 0 and 1 for shutdown */
/* REG_SYS_CTRL2 */
#define BQ769X0_DELAY_DIS (1 << 7) /* 1 = Disable protection delays for testing */
#define BQ769X0_CC_EN (1 << 6) /* 1 = Enable continuous Coulomb counter readings */
#define BQ769X0_CC_ONESHOT (1 << 5) /* Trigger a single 250ms Coulomb counter read */
/* Bit 4: Reserved */
/* Bit 3: Reserved */
/* Bit 2: Reserved */
#define BQ769X0_DSG_ON (1 << 1) /* DSG (Discharge FET) control */
#define BQ769X0_CHG_ON (1 << 0) /* CHG (Charge FET) control */
#define BQ769X0_SYS_CTRL2_WRITE_MASK (0xe3) /* Bits 0, 1, 5, 6, and 7 are writeable */
#define BQ769X0_SYS_CTRL2_CHGDSG_MASK (0x03) /* Bits 0 and 1 for charge/discharge */
/* REG_PROTECT1 */
#define BQ769X0_RSNS (1 << 7 ) /* Selects low or high input range for OCD/SCD */
/* Bit 6: Reserved */
/* Bit 5: Reserved */
#define BQ769X0_SCD_DELAY_SHIFT (3) /* Short circuit in discharge delay timer */
#define BQ769X0_SCD_DELAY_MASK (0x03 << BQ769X0_SCD_DELAY_SHIFT)
# define BQ769X0_SCD_DELAY_70US (0 << BQ769X0_SCD_DELAY_SHIFT)
# define BQ769X0_SCD_DELAY_100US (1 << BQ769X0_SCD_DELAY_SHIFT)
# define BQ769X0_SCD_DELAY_200US (2 << BQ769X0_SCD_DELAY_SHIFT)
# define BQ769X0_SCD_DELAY_400US (3 << BQ769X0_SCD_DELAY_SHIFT)
#define BQ769X0_SCD_THRESH_SHIFT (0) /* Short circuit in discharge threshold value */
#define BQ769X0_SCD_THRESH_MASK (0x07 << BQ769X0_SCD_THRESH_SHIFT)
/* These defines apply when RSNS is 0 */
# define BQ769X0_SCD_THRESH_0_22MV (0 << BQ769X0_SCD_THRESH_SHIFT)
# define BQ769X0_SCD_THRESH_0_33MV (1 << BQ769X0_SCD_THRESH_SHIFT)
# define BQ769X0_SCD_THRESH_0_44MV (2 << BQ769X0_SCD_THRESH_SHIFT)
# define BQ769X0_SCD_THRESH_0_56MV (3 << BQ769X0_SCD_THRESH_SHIFT)
# define BQ769X0_SCD_THRESH_0_67MV (4 << BQ769X0_SCD_THRESH_SHIFT)
# define BQ769X0_SCD_THRESH_0_78MV (5 << BQ769X0_SCD_THRESH_SHIFT)
# define BQ769X0_SCD_THRESH_0_89MV (6 << BQ769X0_SCD_THRESH_SHIFT)
# define BQ769X0_SCD_THRESH_0_100MV (7 << BQ769X0_SCD_THRESH_SHIFT)
/* These defines apply when RSNS is 1 */
# define BQ769X0_SCD_THRESH_1_44MV (0 << BQ769X0_SCD_THRESH_SHIFT)
# define BQ769X0_SCD_THRESH_1_67MV (1 << BQ769X0_SCD_THRESH_SHIFT)
# define BQ769X0_SCD_THRESH_1_89MV (2 << BQ769X0_SCD_THRESH_SHIFT)
# define BQ769X0_SCD_THRESH_1_111MV (3 << BQ769X0_SCD_THRESH_SHIFT)
# define BQ769X0_SCD_THRESH_1_133MV (4 << BQ769X0_SCD_THRESH_SHIFT)
# define BQ769X0_SCD_THRESH_1_155MV (5 << BQ769X0_SCD_THRESH_SHIFT)
# define BQ769X0_SCD_THRESH_1_178MV (6 << BQ769X0_SCD_THRESH_SHIFT)
# define BQ769X0_SCD_THRESH_1_200MV (7 << BQ769X0_SCD_THRESH_SHIFT)
/* REG_PROTECT2 */
/* Bit 7: Reserved */
#define BQ769X0_OCD_DELAY_SHIFT (4) /* Over current in discharge delay timer */
#define BQ769X0_OCD_DELAY_MASK (0x07 << BQ769X0_OCD_DELAY_SHIFT)
# define BQ769X0_OCD_DELAY_8MS (0 << BQ769X0_OCD_DELAY_SHIFT)
# define BQ769X0_OCD_DELAY_20MS (1 << BQ769X0_OCD_DELAY_SHIFT)
# define BQ769X0_OCD_DELAY_40MS (2 << BQ769X0_OCD_DELAY_SHIFT)
# define BQ769X0_OCD_DELAY_80MS (3 << BQ769X0_OCD_DELAY_SHIFT)
# define BQ769X0_OCD_DELAY_160MS (4 << BQ769X0_OCD_DELAY_SHIFT)
# define BQ769X0_OCD_DELAY_320MS (5 << BQ769X0_OCD_DELAY_SHIFT)
# define BQ769X0_OCD_DELAY_640MS (6 << BQ769X0_OCD_DELAY_SHIFT)
# define BQ769X0_OCD_DELAY_1280MS (7 << BQ769X0_OCD_DELAY_SHIFT)
#define BQ769X0_OCD_THRESH_SHIFT (0) /* Over current in discharge threshold value */
#define BQ769X0_OCD_THRESH_MASK (0x0f << BQ769X0_OCD_THRESH_SHIFT)
/* These defines apply when RSNS is 0 */
# define BQ769X0_OCD_THRESH_0_8MV (0 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_0_11MV (1 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_0_14MV (2 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_0_17MV (3 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_0_19MV (4 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_0_22MV (5 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_0_25MV (6 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_0_28MV (7 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_0_31MV (8 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_0_33MV (9 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_0_36MV (10 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_0_39MV (11 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_0_42MV (12 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_0_44MV (13 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_0_47MV (14 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_0_50MV (15 << BQ769X0_OCD_THRESH_SHIFT)
/* These defines apply when RSNS is 1 */
# define BQ769X0_OCD_THRESH_1_17MV (0 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_1_22MV (1 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_1_28MV (2 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_1_33MV (3 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_1_39MV (4 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_1_44MV (5 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_1_50MV (6 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_1_56MV (7 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_1_61MV (8 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_1_67MV (9 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_1_72MV (10 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_1_78MV (11 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_1_83MV (12 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_1_89MV (13 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_1_94MV (14 << BQ769X0_OCD_THRESH_SHIFT)
# define BQ769X0_OCD_THRESH_1_100MV (15 << BQ769X0_OCD_THRESH_SHIFT)
/* REG_PROTECT3 */
#define BQ769X0_UV_DELAY_SHIFT (6)
#define BQ769X0_UV_DELAY_MASK (0x03 << BQ769X0_UV_DELAY_SHIFT)
# define BQ769X0_UV_DELAY_1S (0 << BQ769X0_UV_DELAY_SHIFT)
# define BQ769X0_UV_DELAY_4S (1 << BQ769X0_UV_DELAY_SHIFT)
# define BQ769X0_UV_DELAY_8S (2 << BQ769X0_UV_DELAY_SHIFT)
# define BQ769X0_UV_DELAY_16S (3 << BQ769X0_UV_DELAY_SHIFT)
#define BQ769X0_OV_DELAY_SHIFT (4)
#define BQ769X0_OV_DELAY_MASK (0x03 << BQ769X0_OV_DELAY_SHIFT)
# define BQ769X0_OV_DELAY_1S (0 << BQ769X0_OV_DELAY_SHIFT)
# define BQ769X0_OV_DELAY_2S (1 << BQ769X0_OV_DELAY_SHIFT)
# define BQ769X0_OV_DELAY_4S (2 << BQ769X0_OV_DELAY_SHIFT)
# define BQ769X0_OV_DELAY_8S (3 << BQ769X0_OV_DELAY_SHIFT)
/* REG_ADCGAIN1 */
#define BQ769X0_ADCGAIN1_SHIFT (2)
#define BQ769X0_ADCGAIN1_MASK (0x03 << BQ769X0_ADCGAIN1_SHIFT)
/* REG_ADCGAIN2 */
#define BQ769X0_ADCGAIN2_SHIFT (5)
#define BQ769X0_ADCGAIN2_MASK (0x07 << BQ769X0_ADCGAIN2_SHIFT)
#endif /* __DRIVERS_POWER_BQ769X0_H */
+24 -12
View File
@@ -87,6 +87,29 @@ static const uint8_t crc8_tab[256] =
* Public Functions
***************************************************************************/
/***************************************************************************
* Name: crc8ccitt
*
* Description:
* Continue CRC calculation on a part of the buffer.
*
***************************************************************************/
uint8_t crc8ccittpart(FAR const uint8_t *src, size_t len, uint8_t crc8val)
{
uint8_t *pos = (uint8_t *) src;
uint8_t *end = pos + len;
while (pos < end)
{
crc8val = crc8_tab[crc8val ^ *pos];
pos++;
}
return crc8val;
}
/***************************************************************************
* Name: crc8ccitt
*
@@ -98,16 +121,5 @@ static const uint8_t crc8_tab[256] =
uint8_t crc8ccitt(FAR const uint8_t *src, size_t len)
{
uint8_t val = 0;
uint8_t *pos = (uint8_t *) src;
uint8_t *end = pos + len;
while (pos < end)
{
val = crc8_tab[val ^ *pos];
pos++;
}
return val;
return crc8ccittpart(src, len, 0);
}