Line data Source code
1 : #pragma once 2 : 3 : #include "AP_BattMonitor_Backend.h" 4 : 5 : #if AP_BATTERY_SMBUS_ENABLED 6 : 7 : #include <AP_Common/AP_Common.h> 8 : #include <AP_Param/AP_Param.h> 9 : #include <AP_Math/AP_Math.h> 10 : #include <AP_HAL/I2CDevice.h> 11 : #include <utility> 12 : 13 : #define AP_BATTMONITOR_SMBUS_BUS_INTERNAL 0 14 : #define AP_BATTMONITOR_SMBUS_BUS_EXTERNAL 1 15 : #define AP_BATTMONITOR_SMBUS_I2C_ADDR 0x0B 16 : #define AP_BATTMONITOR_SMBUS_TIMEOUT_MICROS 5000000 // sensor becomes unhealthy if no successful readings for 5 seconds 17 : #define AP_BATTMONITOR_SMBUS_READ_BLOCK_MAXIMUM_TRANSFER 0x20 // A Block Read or Write is allowed to transfer a maximum of 32 data bytes. 18 : 19 : class AP_BattMonitor_SMBus : public AP_BattMonitor_Backend 20 : { 21 : public: 22 : 23 : // Smart Battery Data Specification Revision 1.1 24 : enum BATTMONITOR_SMBUS { 25 : BATTMONITOR_SMBUS_TEMP = 0x08, // Temperature 26 : BATTMONITOR_SMBUS_VOLTAGE = 0x09, // Voltage 27 : BATTMONITOR_SMBUS_CURRENT = 0x0A, // Current 28 : BATTMONITOR_SMBUS_REMAINING_CAPACITY = 0x0F, // Remaining Capacity 29 : BATTMONITOR_SMBUS_FULL_CHARGE_CAPACITY = 0x10, // Full Charge Capacity 30 : BATTMONITOR_SMBUS_CYCLE_COUNT = 0x17, // Cycle Count 31 : BATTMONITOR_SMBUS_SPECIFICATION_INFO = 0x1A, // Specification Info 32 : BATTMONITOR_SMBUS_SERIAL = 0x1C, // Serial Number 33 : BATTMONITOR_SMBUS_MANUFACTURE_NAME = 0x20, // Manufacture Name 34 : BATTMONITOR_SMBUS_MANUFACTURE_DATA = 0x23, // Manufacture Data 35 : }; 36 : 37 : /// Constructor 38 : AP_BattMonitor_SMBus(AP_BattMonitor &mon, 39 : AP_BattMonitor::BattMonitor_State &mon_state, 40 : AP_BattMonitor_Params ¶ms, 41 : uint8_t i2c_bus); 42 : 43 : // virtual destructor to reduce compiler warnings 44 0 : virtual ~AP_BattMonitor_SMBus() {} 45 : 46 602 : bool has_cell_voltages() const override { return _has_cell_voltages; } 47 : 48 602 : bool has_temperature() const override { return _has_temperature; } 49 : 50 : // all smart batteries are expected to provide current 51 2628 : bool has_current() const override { return true; } 52 : 53 : // don't allow reset of remaining capacity for SMBus 54 0 : bool reset_remaining(float percentage) override { return false; } 55 : 56 : // return true if cycle count can be provided and fills in cycles argument 57 : bool get_cycle_count(uint16_t &cycles) const override; 58 : 59 : virtual void init(void) override; 60 : 61 : static const struct AP_Param::GroupInfo var_info[]; 62 : 63 : protected: 64 : 65 : void read(void) override; 66 : 67 : // reads the pack full charge capacity 68 : void read_full_charge_capacity(void); 69 : 70 : // reads the remaining capacity 71 : // which will only be read if we know the full charge capacity (accounting for battery degradation) 72 : void read_remaining_capacity(void); 73 : 74 : // return a scaler that should be multiplied by the battery's reported capacity numbers to arrive at the actual capacity in mAh 75 0 : virtual uint16_t get_capacity_scaler() const { return 1; } 76 : 77 : // reads the temperature word from the battery 78 : virtual void read_temp(void); 79 : 80 : // reads the serial number if it's not already known 81 : // returns if the serial number was already known 82 : void read_serial_number(void); 83 : 84 : // reads the battery's cycle count 85 : void read_cycle_count(); 86 : 87 : // read word from register 88 : // returns true if read was successful, false if failed 89 : bool read_word(uint8_t reg, uint16_t& data) const; 90 : 91 : // read_block - returns number of characters read if successful, zero if unsuccessful 92 : uint8_t read_block(uint8_t reg, uint8_t* data, uint8_t len) const; 93 : 94 : // get_PEC - calculate PEC for a read or write from the battery 95 : // buff is the data that was read or will be written 96 : uint8_t get_PEC(const uint8_t i2c_addr, uint8_t cmd, bool reading, const uint8_t buff[], uint8_t len) const; 97 : 98 : AP_HAL::I2CDevice *_dev; 99 : bool _pec_supported; // true if PEC is supported 100 : 101 : int32_t _serial_number = -1; // battery serial number 102 : uint16_t _full_charge_capacity; // full charge capacity, used to stash the value before setting the parameter 103 : bool _has_cell_voltages; // smbus backends flag this as true once they have received a valid cell voltage report 104 : uint16_t _cycle_count = 0; // number of cycles the battery has experienced. An amount of discharge approximately equal to the value of DesignCapacity. 105 : bool _has_cycle_count; // true if cycle count has been retrieved from the battery 106 : bool _has_temperature; 107 : 108 : virtual void timer(void) = 0; // timer function to read from the battery 109 : 110 : AP_HAL::Device::PeriodicHandle timer_handle; 111 : 112 : // Parameters 113 : AP_Int8 _bus; // I2C bus number 114 : AP_Int8 _address; // I2C address 115 : 116 : }; 117 : 118 : #endif // AP_BATTERY_SMBUS_ENABLED