AP_BattMonitor_SMBus.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #include <AP_Common/AP_Common.h>
  3. #include <AP_Param/AP_Param.h>
  4. #include <AP_Math/AP_Math.h>
  5. #include "AP_BattMonitor_Backend.h"
  6. #include <utility>
  7. #define AP_BATTMONITOR_SMBUS_BUS_INTERNAL 0
  8. #define AP_BATTMONITOR_SMBUS_BUS_EXTERNAL 1
  9. #define AP_BATTMONITOR_SMBUS_I2C_ADDR 0x0B
  10. #define AP_BATTMONITOR_SMBUS_TIMEOUT_MICROS 5000000 // sensor becomes unhealthy if no successful readings for 5 seconds
  11. class AP_BattMonitor_SMBus : public AP_BattMonitor_Backend
  12. {
  13. public:
  14. // Smart Battery Data Specification Revision 1.1
  15. enum BATTMONITOR_SMBUS {
  16. BATTMONITOR_SMBUS_TEMP = 0x08, // Temperature
  17. BATTMONITOR_SMBUS_VOLTAGE = 0x09, // Voltage
  18. BATTMONITOR_SMBUS_CURRENT = 0x0A, // Current
  19. BATTMONITOR_SMBUS_REMAINING_CAPACITY = 0x0F, // Remaining Capacity
  20. BATTMONITOR_SMBUS_FULL_CHARGE_CAPACITY = 0x10, // Full Charge Capacity
  21. BATTMONITOR_SMBUS_SPECIFICATION_INFO = 0x1A, // Specification Info
  22. BATTMONITOR_SMBUS_SERIAL = 0x1C, // Serial Number
  23. BATTMONITOR_SMBUS_MANUFACTURE_NAME = 0x20, // Manufacture Name
  24. BATTMONITOR_SMBUS_MANUFACTURE_DATA = 0x23, // Manufacture Data
  25. };
  26. /// Constructor
  27. AP_BattMonitor_SMBus(AP_BattMonitor &mon,
  28. AP_BattMonitor::BattMonitor_State &mon_state,
  29. AP_BattMonitor_Params &params,
  30. AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev);
  31. // virtual destructor to reduce compiler warnings
  32. virtual ~AP_BattMonitor_SMBus() {}
  33. bool has_cell_voltages() const override { return _has_cell_voltages; }
  34. // all smart batteries are expected to provide current
  35. bool has_current() const override { return true; }
  36. // don't allow reset of remaining capacity for SMBus
  37. bool reset_remaining(float percentage) override { return false; }
  38. void init(void) override;
  39. protected:
  40. void read(void) override;
  41. // reads the pack full charge capacity
  42. // returns true if the read was successful, or if we already knew the pack capacity
  43. bool read_full_charge_capacity(void);
  44. // reads the remaining capacity
  45. // returns true if the read was succesful, which is only considered to be the
  46. // we know the full charge capacity
  47. bool read_remaining_capacity(void);
  48. // reads the temperature word from the battery
  49. // returns true if the read was successful
  50. bool read_temp(void);
  51. // reads the serial number if it's not already known
  52. // returns true if the read was successful, or the number was already known
  53. bool read_serial_number(void);
  54. // read word from register
  55. // returns true if read was successful, false if failed
  56. bool read_word(uint8_t reg, uint16_t& data) const;
  57. // get_PEC - calculate PEC for a read or write from the battery
  58. // buff is the data that was read or will be written
  59. uint8_t get_PEC(const uint8_t i2c_addr, uint8_t cmd, bool reading, const uint8_t buff[], uint8_t len) const;
  60. AP_HAL::OwnPtr<AP_HAL::I2CDevice> _dev;
  61. bool _pec_supported; // true if PEC is supported
  62. int32_t _serial_number = -1; // battery serial number
  63. uint16_t _full_charge_capacity; // full charge capacity, used to stash the value before setting the parameter
  64. bool _has_cell_voltages; // smbus backends flag this as true once they have recieved a valid cell voltage report
  65. virtual void timer(void) = 0; // timer function to read from the battery
  66. };
  67. // include specific implementations
  68. #include "AP_BattMonitor_SMBus_Solo.h"
  69. #include "AP_BattMonitor_SMBus_Maxell.h"