mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-31 10:41:35 +08:00
SMBus battery driver: Stylize according to style guide
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
*
|
*
|
||||||
* Copyright (C) 2012, 2013 PX4 Development Team. All rights reserved.
|
* Copyright (c) 2012-2015 PX4 Development Team. All rights reserved.
|
||||||
* Author: Randy Mackay <rmackay9@yahoo.com>
|
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -37,6 +36,7 @@
|
|||||||
*
|
*
|
||||||
* Driver for a battery monitor connected via SMBus (I2C).
|
* Driver for a battery monitor connected via SMBus (I2C).
|
||||||
*
|
*
|
||||||
|
* @author Randy Mackay <rmackay9@yahoo.com>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <nuttx/config.h>
|
#include <nuttx/config.h>
|
||||||
@@ -77,22 +77,22 @@
|
|||||||
#include <drivers/drv_batt_smbus.h>
|
#include <drivers/drv_batt_smbus.h>
|
||||||
#include <drivers/device/ringbuffer.h>
|
#include <drivers/device/ringbuffer.h>
|
||||||
|
|
||||||
#define BATT_SMBUS_ADDR_MIN 0x08 /* lowest possible address */
|
#define BATT_SMBUS_ADDR_MIN 0x08 ///< lowest possible address
|
||||||
#define BATT_SMBUS_ADDR_MAX 0x7F /* highest possible address */
|
#define BATT_SMBUS_ADDR_MAX 0x7F ///< highest possible address
|
||||||
|
|
||||||
#define BATT_SMBUS_I2C_BUS PX4_I2C_BUS_EXPANSION
|
#define BATT_SMBUS_I2C_BUS PX4_I2C_BUS_EXPANSION
|
||||||
#define BATT_SMBUS_ADDR 0x0B /* I2C address */
|
#define BATT_SMBUS_ADDR 0x0B ///< I2C address
|
||||||
#define BATT_SMBUS_TEMP 0x08 /* temperature register */
|
#define BATT_SMBUS_TEMP 0x08 ///< temperature register
|
||||||
#define BATT_SMBUS_VOLTAGE 0x09 /* voltage register */
|
#define BATT_SMBUS_VOLTAGE 0x09 ///< voltage register
|
||||||
#define BATT_SMBUS_DESIGN_CAPACITY 0x18 /* design capacity register */
|
#define BATT_SMBUS_DESIGN_CAPACITY 0x18 ///< design capacity register
|
||||||
#define BATT_SMBUS_DESIGN_VOLTAGE 0x19 /* design voltage register */
|
#define BATT_SMBUS_DESIGN_VOLTAGE 0x19 ///< design voltage register
|
||||||
#define BATT_SMBUS_SERIALNUM 0x1c /* serial number register */
|
#define BATT_SMBUS_SERIALNUM 0x1c ///< serial number register
|
||||||
#define BATT_SMBUS_MANUFACTURE_NAME 0x20 /* manufacturer name */
|
#define BATT_SMBUS_MANUFACTURE_NAME 0x20 ///< manufacturer name
|
||||||
#define BATT_SMBUS_MANUFACTURE_INFO 0x25 /* cell voltage register */
|
#define BATT_SMBUS_MANUFACTURE_INFO 0x25 ///< cell voltage register
|
||||||
#define BATT_SMBUS_CURRENT 0x2a /* current register */
|
#define BATT_SMBUS_CURRENT 0x2a ///< current register
|
||||||
#define BATT_SMBUS_MEASUREMENT_INTERVAL_MS (1000000 / 10) /* time in microseconds, measure at 10hz */
|
#define BATT_SMBUS_MEASUREMENT_INTERVAL_MS (1000000 / 10) ///< time in microseconds, measure at 10hz
|
||||||
|
|
||||||
#define BATT_SMBUS_PEC_POLYNOMIAL 0x07 /* Polynomial for calculating PEC */
|
#define BATT_SMBUS_PEC_POLYNOMIAL 0x07 ///< Polynomial for calculating PEC
|
||||||
|
|
||||||
#ifndef CONFIG_SCHED_WORKQUEUE
|
#ifndef CONFIG_SCHED_WORKQUEUE
|
||||||
# error This requires CONFIG_SCHED_WORKQUEUE.
|
# error This requires CONFIG_SCHED_WORKQUEUE.
|
||||||
@@ -106,41 +106,62 @@ public:
|
|||||||
|
|
||||||
virtual int init();
|
virtual int init();
|
||||||
virtual int test();
|
virtual int test();
|
||||||
int search(); /* search all possible slave addresses for a smart battery */
|
/**
|
||||||
|
* Search all possible slave addresses for a smart battery
|
||||||
|
*/
|
||||||
|
int search();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual int probe(); // check if the device can be contacted
|
/**
|
||||||
|
* Check if the device can be contacted
|
||||||
|
*/
|
||||||
|
virtual int probe();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
// start periodic reads from the battery
|
/**
|
||||||
|
* Start periodic reads from the battery
|
||||||
|
*/
|
||||||
void start();
|
void start();
|
||||||
|
|
||||||
// stop periodic reads from the battery
|
/**
|
||||||
|
* Stop periodic reads from the battery
|
||||||
|
*/
|
||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
// static function that is called by worker queue
|
/**
|
||||||
|
* static function that is called by worker queue
|
||||||
|
*/
|
||||||
static void cycle_trampoline(void *arg);
|
static void cycle_trampoline(void *arg);
|
||||||
|
|
||||||
// perform a read from the battery
|
/**
|
||||||
|
* perform a read from the battery
|
||||||
|
*/
|
||||||
void cycle();
|
void cycle();
|
||||||
|
|
||||||
// read_reg - read a word from specified register
|
/**
|
||||||
|
* Read a word from specified register
|
||||||
|
*/
|
||||||
int read_reg(uint8_t reg, uint16_t &val);
|
int read_reg(uint8_t reg, uint16_t &val);
|
||||||
|
|
||||||
// read_block - returns number of characters read if successful, zero if unsuccessful
|
/**
|
||||||
|
* Read block from bus
|
||||||
|
* @return returns number of characters read if successful, zero if unsuccessful
|
||||||
|
*/
|
||||||
uint8_t read_block(uint8_t reg, uint8_t* data, uint8_t max_len, bool append_zero);
|
uint8_t read_block(uint8_t reg, uint8_t* data, uint8_t max_len, bool append_zero);
|
||||||
|
|
||||||
// get_PEC - calculate PEC for a read or write from the battery
|
/**
|
||||||
// buff is the data that was read or will be written
|
* Calculate PEC for a read or write from the battery
|
||||||
|
* @param buff is the data that was read or will be written
|
||||||
|
*/
|
||||||
uint8_t get_PEC(uint8_t cmd, bool reading, const uint8_t buff[], uint8_t len) const;
|
uint8_t get_PEC(uint8_t cmd, bool reading, const uint8_t buff[], uint8_t len) const;
|
||||||
|
|
||||||
// internal variables
|
// internal variables
|
||||||
work_s _work; // work queue for scheduling reads
|
work_s _work; ///< work queue for scheduling reads
|
||||||
RingBuffer *_reports; // buffer of recorded voltages, currents
|
RingBuffer *_reports; ///< buffer of recorded voltages, currents
|
||||||
struct battery_status_s _last_report; // last published report, used for test()
|
struct battery_status_s _last_report; ///< last published report, used for test()
|
||||||
orb_advert_t _batt_topic;
|
orb_advert_t _batt_topic; ///< uORB battery topic
|
||||||
orb_id_t _batt_orb_id;
|
orb_id_t _batt_orb_id; ///< uORB battery topic ID
|
||||||
};
|
};
|
||||||
|
|
||||||
/* for now, we only support one BATT_SMBUS */
|
/* for now, we only support one BATT_SMBUS */
|
||||||
@@ -153,7 +174,6 @@ void batt_smbus_usage();
|
|||||||
|
|
||||||
extern "C" __EXPORT int batt_smbus_main(int argc, char *argv[]);
|
extern "C" __EXPORT int batt_smbus_main(int argc, char *argv[]);
|
||||||
|
|
||||||
// constructor
|
|
||||||
BATT_SMBUS::BATT_SMBUS(int bus, uint16_t batt_smbus_addr) :
|
BATT_SMBUS::BATT_SMBUS(int bus, uint16_t batt_smbus_addr) :
|
||||||
I2C("batt_smbus", BATT_SMBUS_DEVICE_PATH, bus, batt_smbus_addr, 400000),
|
I2C("batt_smbus", BATT_SMBUS_DEVICE_PATH, bus, batt_smbus_addr, 400000),
|
||||||
_work{},
|
_work{},
|
||||||
@@ -165,10 +185,9 @@ BATT_SMBUS::BATT_SMBUS(int bus, uint16_t batt_smbus_addr) :
|
|||||||
memset(&_work, 0, sizeof(_work));
|
memset(&_work, 0, sizeof(_work));
|
||||||
}
|
}
|
||||||
|
|
||||||
// destructor
|
|
||||||
BATT_SMBUS::~BATT_SMBUS()
|
BATT_SMBUS::~BATT_SMBUS()
|
||||||
{
|
{
|
||||||
/* make sure we are truly inactive */
|
// make sure we are truly inactive
|
||||||
stop();
|
stop();
|
||||||
|
|
||||||
if (_reports != nullptr) {
|
if (_reports != nullptr) {
|
||||||
@@ -188,7 +207,7 @@ BATT_SMBUS::init()
|
|||||||
errx(1,"failed to init I2C");
|
errx(1,"failed to init I2C");
|
||||||
return ret;
|
return ret;
|
||||||
} else {
|
} else {
|
||||||
/* allocate basic report buffers */
|
// allocate basic report buffers
|
||||||
_reports = new RingBuffer(2, sizeof(struct battery_status_s));
|
_reports = new RingBuffer(2, sizeof(struct battery_status_s));
|
||||||
if (_reports == nullptr) {
|
if (_reports == nullptr) {
|
||||||
ret = ENOTTY;
|
ret = ENOTTY;
|
||||||
@@ -230,7 +249,6 @@ BATT_SMBUS::test()
|
|||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* search all possible slave addresses for a smart battery */
|
|
||||||
int
|
int
|
||||||
BATT_SMBUS::search()
|
BATT_SMBUS::search()
|
||||||
{
|
{
|
||||||
@@ -265,25 +283,22 @@ BATT_SMBUS::probe()
|
|||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
// start periodic reads from the battery
|
|
||||||
void
|
void
|
||||||
BATT_SMBUS::start()
|
BATT_SMBUS::start()
|
||||||
{
|
{
|
||||||
/* reset the report ring and state machine */
|
// reset the report ring and state machine
|
||||||
_reports->flush();
|
_reports->flush();
|
||||||
|
|
||||||
/* schedule a cycle to start things */
|
// schedule a cycle to start things
|
||||||
work_queue(HPWORK, &_work, (worker_t)&BATT_SMBUS::cycle_trampoline, this, 1);
|
work_queue(HPWORK, &_work, (worker_t)&BATT_SMBUS::cycle_trampoline, this, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// stop periodic reads from the battery
|
|
||||||
void
|
void
|
||||||
BATT_SMBUS::stop()
|
BATT_SMBUS::stop()
|
||||||
{
|
{
|
||||||
work_cancel(HPWORK, &_work);
|
work_cancel(HPWORK, &_work);
|
||||||
}
|
}
|
||||||
|
|
||||||
// static function that is called by worker queue
|
|
||||||
void
|
void
|
||||||
BATT_SMBUS::cycle_trampoline(void *arg)
|
BATT_SMBUS::cycle_trampoline(void *arg)
|
||||||
{
|
{
|
||||||
@@ -292,7 +307,6 @@ BATT_SMBUS::cycle_trampoline(void *arg)
|
|||||||
dev->cycle();
|
dev->cycle();
|
||||||
}
|
}
|
||||||
|
|
||||||
// perform a read from the battery
|
|
||||||
void
|
void
|
||||||
BATT_SMBUS::cycle()
|
BATT_SMBUS::cycle()
|
||||||
{
|
{
|
||||||
@@ -331,14 +345,14 @@ BATT_SMBUS::cycle()
|
|||||||
// copy report for test()
|
// copy report for test()
|
||||||
_last_report = new_report;
|
_last_report = new_report;
|
||||||
|
|
||||||
/* post a report to the ring */
|
// post a report to the ring
|
||||||
_reports->force(&new_report);
|
_reports->force(&new_report);
|
||||||
|
|
||||||
/* notify anyone waiting for data */
|
// notify anyone waiting for data
|
||||||
poll_notify(POLLIN);
|
poll_notify(POLLIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* schedule a fresh cycle call when the measurement is done */
|
// schedule a fresh cycle call when the measurement is done
|
||||||
work_queue(HPWORK, &_work, (worker_t)&BATT_SMBUS::cycle_trampoline, this, USEC2TICK(BATT_SMBUS_MEASUREMENT_INTERVAL_MS));
|
work_queue(HPWORK, &_work, (worker_t)&BATT_SMBUS::cycle_trampoline, this, USEC2TICK(BATT_SMBUS_MEASUREMENT_INTERVAL_MS));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -363,8 +377,8 @@ BATT_SMBUS::read_reg(uint8_t reg, uint16_t &val)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
// read_block - returns number of characters read if successful, zero if unsuccessful
|
uint8_t
|
||||||
uint8_t BATT_SMBUS::read_block(uint8_t reg, uint8_t* data, uint8_t max_len, bool append_zero)
|
BATT_SMBUS::read_block(uint8_t reg, uint8_t* data, uint8_t max_len, bool append_zero)
|
||||||
{
|
{
|
||||||
uint8_t buff[max_len+2]; // buffer to hold results
|
uint8_t buff[max_len+2]; // buffer to hold results
|
||||||
|
|
||||||
@@ -408,9 +422,8 @@ uint8_t BATT_SMBUS::read_block(uint8_t reg, uint8_t* data, uint8_t max_len, bool
|
|||||||
return bufflen;
|
return bufflen;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get_PEC - calculate PEC for a read or write from the battery
|
uint8_t
|
||||||
// buff is the data that was read or will be written
|
BATT_SMBUS::get_PEC(uint8_t cmd, bool reading, const uint8_t buff[], uint8_t len) const
|
||||||
uint8_t BATT_SMBUS::get_PEC(uint8_t cmd, bool reading, const uint8_t buff[], uint8_t len) const
|
|
||||||
{
|
{
|
||||||
// exit immediately if no data
|
// exit immediately if no data
|
||||||
if (len <= 0) {
|
if (len <= 0) {
|
||||||
@@ -463,11 +476,11 @@ int
|
|||||||
batt_smbus_main(int argc, char *argv[])
|
batt_smbus_main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int i2cdevice = BATT_SMBUS_I2C_BUS;
|
int i2cdevice = BATT_SMBUS_I2C_BUS;
|
||||||
int batt_smbusadr = BATT_SMBUS_ADDR; /* 7bit */
|
int batt_smbusadr = BATT_SMBUS_ADDR; // 7bit address
|
||||||
|
|
||||||
int ch;
|
int ch;
|
||||||
|
|
||||||
/* jump over start/off/etc and look at options first */
|
// jump over start/off/etc and look at options first
|
||||||
while ((ch = getopt(argc, argv, "a:b:")) != EOF) {
|
while ((ch = getopt(argc, argv, "a:b:")) != EOF) {
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case 'a':
|
case 'a':
|
||||||
@@ -512,7 +525,7 @@ batt_smbus_main(int argc, char *argv[])
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* need the driver past this point */
|
// need the driver past this point
|
||||||
if (g_batt_smbus == nullptr) {
|
if (g_batt_smbus == nullptr) {
|
||||||
warnx("not started");
|
warnx("not started");
|
||||||
batt_smbus_usage();
|
batt_smbus_usage();
|
||||||
|
|||||||
Reference in New Issue
Block a user