Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions src/MP2722.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,36 @@
// Initialization of MP2722 Class
MP2722 PowerSystem;

MP2722::MP2722(TwoWire& wirePort) : _wire(&wirePort) {}
MP2722::MP2722(TwoWire& wirePort, uint8_t address, int8_t muxPin)
: _wire(&wirePort), _address(address), _muxPin(muxPin) {}

void MP2722::setAddress(uint8_t address) { _address = address; }

void MP2722::setMuxPin(int8_t muxPin) { _muxPin = muxPin; }

bool MP2722::begin() {
_wire->begin();
return isConnected();
}

bool MP2722::isConnected() {
_wire->beginTransmission(MP2722_ADDR);
_wire->beginTransmission(_address);
return (_wire->endTransmission() == 0);
}

bool MP2722::writeReg(uint8_t reg, uint8_t value) {
_wire->beginTransmission(MP2722_ADDR);
_wire->beginTransmission(_address);
_wire->write(reg);
_wire->write(value);
return (_wire->endTransmission() == 0);
}

bool MP2722::readReg(uint8_t reg, uint8_t &value) {
_wire->beginTransmission(MP2722_ADDR);
_wire->beginTransmission(_address);
_wire->write(reg);
if (_wire->endTransmission(false) != 0) return false;

if (_wire->requestFrom(static_cast<uint8_t>(MP2722_ADDR), static_cast<uint8_t>(1)) != 1) return false;
if (_wire->requestFrom(static_cast<uint8_t>(_address), static_cast<uint8_t>(1)) != 1) return false;
value = _wire->read();
return true;
}
Expand Down Expand Up @@ -148,12 +153,14 @@ bool MP2722::getBoostState(bool &enabled) {
}

void MP2722::setUSBControlESP() {
digitalWrite(USB_MUX_PIN, HIGH);
if (_muxPin < 0) return;
digitalWrite(_muxPin, HIGH);
}


void MP2722::setUSBControlBMS() {
digitalWrite(USB_MUX_PIN, LOW);
if (_muxPin < 0) return;
digitalWrite(_muxPin, LOW);
}

bool MP2722::getDPDMStatus(DPDMResult &out) {
Expand Down Expand Up @@ -258,16 +265,20 @@ void MP2722::printDiagnostics() {
}

// ----- INIT ----- //
bool MP2722::init(uint8_t sda, uint8_t scl) {
bool MP2722::init(uint8_t sda, uint8_t scl, uint8_t address, int8_t muxPin) {
_address = address;
_muxPin = muxPin;

// Start I2C
Wire.begin(sda, scl);
if (!begin()) return false;

// Designate Multiplexer pin as output
pinMode(USB_MUX_PIN, OUTPUT);

// Give USB control to BMS
setUSBControlBMS();
// Boards without a USB mux skip the pin setup and control handoff
if (_muxPin >= 0) {
pinMode(_muxPin, OUTPUT);
// Give USB control to BMS
setUSBControlBMS();
}

// Set fast charge current limit to 1200mA
if (!setFastChargeCurrent(1200)) return false;
Expand Down
21 changes: 18 additions & 3 deletions src/MP2722.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@

#include <Arduino.h>
#include <Wire.h>
#include <config.h>

class MP2722 {
public:
explicit MP2722(TwoWire& wirePort = Wire);
// Datasheet I2C address. Override through the constructor or init() when the
// board has a different address.
static constexpr uint8_t DEFAULT_ADDRESS = 0x3F;

// Pass as muxPin when the board has no USB data-line mux; the mux helpers
// then become no-ops.
static constexpr int8_t NO_MUX_PIN = -1;

explicit MP2722(TwoWire& wirePort = Wire,
uint8_t address = DEFAULT_ADDRESS,
int8_t muxPin = NO_MUX_PIN);

bool begin();
bool isConnected();
Expand Down Expand Up @@ -34,7 +43,11 @@ class MP2722 {
bool readReg(uint8_t reg, uint8_t &value);

// Helper functions
bool init(uint8_t sda, uint8_t scl);
bool init(uint8_t sda, uint8_t scl,
uint8_t address = DEFAULT_ADDRESS,
int8_t muxPin = NO_MUX_PIN);
void setAddress(uint8_t address);
void setMuxPin(int8_t muxPin);
void printDiagnostics();
bool setCCMode(uint8_t cc_cfg);
bool setBoostCurrentLimit(float amps);
Expand All @@ -52,6 +65,8 @@ class MP2722 {

private:
TwoWire* _wire;
uint8_t _address;
int8_t _muxPin;
};

// Initialization of MP2722 Class
Expand Down
Loading