Skip to content

Add generic functions to write to or read from internal registers + use Wire1 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 15, 2020
40 changes: 40 additions & 0 deletions examples/ReadWriteRegs/ReadWriteRegs.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "Arduino_PMIC.h"
#include "PF1550/PF1550_Register.h"

#define Serial Serial1

uint8_t regVal;

void setup() {
Serial.begin(115200);
while(!Serial);

PMIC.begin();

PMIC.debug(Serial);

}


void loop() {
PMIC.writePMICreg(Register::CHARGER_CHG_EOC_CNFG, 0x41);
regVal = PMIC.readPMICreg(Register::CHARGER_CHG_EOC_CNFG);

Serial.print("CHARGER_CHG_SNS register value: ");
Serial.println(regVal,HEX);

regVal = PMIC.readPMICreg(Register::CHARGER_VBUS_SNS);

Serial.print("CHARGER_VBUS_SNS register value: ");
Serial.println(regVal,HEX);

regVal = PMIC.readPMICreg(Register::CHARGER_CHG_INT_OK);

Serial.print("CHARGER_CHG_INT_OK register value: ");
Serial.println(regVal,HEX);
Serial.println();
Serial.println();

delay(5000);

}
44 changes: 43 additions & 1 deletion src/PF1550.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
******************************************************************************/

PF1550::PF1550(interface::PF1550_Io & io)
: _control(io)
: _control(io),
_debug(NULL)
{

}
Expand All @@ -40,9 +41,50 @@ PF1550::PF1550(interface::PF1550_Io & io)

int PF1550::begin()
{
if (_debug) {
_debug->println("PF1550 begin");
}
return _control.begin();
}

void PF1550::debug(Stream& stream)
{
_debug = &stream;
_control.debug(stream);
}

void PF1550::setPMICbit(Register const reg_addr, uint8_t posBit)
{
_control.setBit(reg_addr, posBit);
}

void PF1550::writePMICreg(Register const reg_addr, uint8_t val)
{
if (_debug) {
_debug->print("PF1550::writePMICreg at address=");
_debug->print((uint8_t)reg_addr, HEX);
_debug->print(" data=");
_debug->println(val, HEX);
}

_control.writeReg(reg_addr, val);
}

uint8_t PF1550::readPMICreg(Register const reg_addr)
{
uint8_t reg_val;
_control.readReg(reg_addr, &reg_val);

if (_debug) {
_debug->print("PF1550::readPMICreg from address=");
_debug->print((uint8_t)reg_addr, HEX);
_debug->print(" data=");
_debug->println(reg_val, HEX);
}

return reg_val;
}

void PF1550::configLDO1(Ldo1Voltage const ldo_1_volt, bool const enable, bool const enable_in_standby, bool const enable_in_sleep)
{
_control.setLDO1Voltage(ldo_1_volt);
Expand Down
13 changes: 12 additions & 1 deletion src/PF1550.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
#ifndef ARDUINO_PF1550_H_
#define ARDUINO_PF1550_H_

#define PF1550_I2C_ADDR 0x08

/******************************************************************************
INCLUDE
******************************************************************************/

#include "Arduino.h"
#include "PF1550/interface/PF1550_Io.h"
#include "PF1550/PF1550_Types.h"
#include "PF1550/PF1550_Control.h"
#include "PF1550/PF1550_Register.h"

/******************************************************************************
EXTERN DECLARATION
Expand All @@ -46,6 +49,12 @@ class PF1550

int begin();

void debug(Stream& stream);

void setPMICbit(Register const reg_addr, uint8_t posBit);
void writePMICreg(Register const reg_addr, uint8_t val);
uint8_t readPMICreg(Register const reg_addr);

void configLDO1(Ldo1Voltage const ldo_1_volt, bool const enable, bool const enable_in_standby, bool const enable_in_sleep);
void configLDO2(Ldo2Voltage const ldo_2_volt, bool const enable, bool const enable_in_standby, bool const enable_in_sleep);
void configLDO3(Ldo3Voltage const ldo_3_volt, bool const enable, bool const enable_in_standby, bool const enable_in_sleep);
Expand All @@ -72,6 +81,8 @@ class PF1550

PF1550_Control _control;

Stream* _debug;

};

#endif /* ARDUINO_PF1550_H_ */
84 changes: 66 additions & 18 deletions src/PF1550/PF1550_Control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include "util/PF1550_Util.h"

#include "PF1550.h"

/******************************************************************************
CTOR/DTOR
******************************************************************************/
Expand All @@ -43,9 +45,42 @@ int PF1550_Control::begin()
return _io.begin();
}

void PF1550_Control::debug(Stream& stream)
{
_debug = &stream;
_io.debug(stream);
}

void PF1550_Control::setBit(Register const reg, uint8_t const bit_pos)
{
_io.setBit(reg, bit_pos);
}

void PF1550_Control::writeReg(Register const reg_addr, uint8_t val)
{
uint8_t i2c_data[2];

i2c_data[0] = (uint8_t)reg_addr;
i2c_data[1] = val;

if (_debug) {
_debug->print("PF1550_Control::writeReg at address=");
_debug->print(i2c_data[0], HEX);
_debug->print(" data=");
_debug->println(i2c_data[1], HEX);
}

_io.writeRegister(PF1550_I2C_ADDR, i2c_data, 2, 0);
}

void PF1550_Control::readReg(Register const reg_addr, uint8_t *data)
{
_io.readRegister(reg_addr, data);
}

void PF1550_Control::setLDO1Voltage(Ldo1Voltage const ldo1_volt)
{
_io.writeRegister(Register::PMIC_LDO1_VOLT, static_cast<uint8_t>(ldo1_volt));
writeReg(Register::PMIC_LDO1_VOLT, static_cast<uint8_t>(ldo1_volt));
}

void PF1550_Control::turnLDO1On(Ldo1Mode const mode)
Expand All @@ -60,7 +95,7 @@ void PF1550_Control::turnLDO1Off(Ldo1Mode const mode)

void PF1550_Control::setLDO2Voltage(Ldo2Voltage const ldo2_volt)
{
_io.writeRegister(Register::PMIC_LDO2_VOLT, static_cast<uint8_t>(ldo2_volt));
writeReg(Register::PMIC_LDO2_VOLT, static_cast<uint8_t>(ldo2_volt));
}

void PF1550_Control::turnLDO2On(Ldo2Mode const mode)
Expand All @@ -75,7 +110,7 @@ void PF1550_Control::turnLDO2Off(Ldo2Mode const mode)

void PF1550_Control::setLDO3Voltage(Ldo3Voltage const ldo3_volt)
{
_io.writeRegister(Register::PMIC_LDO3_VOLT, static_cast<uint8_t>(ldo3_volt));
writeReg(Register::PMIC_LDO3_VOLT, static_cast<uint8_t>(ldo3_volt));
}

void PF1550_Control::turnLDO3On(Ldo3Mode const mode)
Expand All @@ -90,25 +125,27 @@ void PF1550_Control::turnLDO3Off(Ldo3Mode const mode)

void PF1550_Control::setSw2Voltage(Sw2Voltage const sw2_volt)
{
_io.writeRegister(Register::PMIC_SW2_VOLT, static_cast<uint8_t>(sw2_volt));
writeReg(Register::PMIC_SW2_VOLT, static_cast<uint8_t>(sw2_volt));
}

void PF1550_Control::setSw2VoltageStandby(Sw2Voltage const sw2_volt_standby)
{
_io.writeRegister(Register::PMIC_SW2_STBY_VOLT, static_cast<uint8_t>(sw2_volt_standby));
writeReg(Register::PMIC_SW2_STBY_VOLT, static_cast<uint8_t>(sw2_volt_standby));
}

void PF1550_Control::setSw2VoltageSleep(Sw2Voltage const sw2_volt_sleep)
{
_io.writeRegister(Register::PMIC_SW2_SLP_VOLT, static_cast<uint8_t>(sw2_volt_sleep));
writeReg(Register::PMIC_SW2_SLP_VOLT, static_cast<uint8_t>(sw2_volt_sleep));
}

void PF1550_Control::setSw2CurrentLimit(Sw2CurrentLimit const sw2_current_limit)
{
uint8_t sw2_ctrl1_reg = _io.readRegister(Register::PMIC_SW2_CTRL1);
uint8_t sw2_ctrl1_reg;
_io.readRegister(Register::PMIC_SW2_CTRL1, &sw2_ctrl1_reg);
sw2_ctrl1_reg &= ~REG_SW2_CTRL1_SW2_ILIM_mask;
sw2_ctrl1_reg |= static_cast<uint8_t>(sw2_current_limit);
_io.writeRegister(Register::PMIC_SW2_CTRL1, sw2_ctrl1_reg);

writeReg(Register::PMIC_SW2_CTRL1, sw2_ctrl1_reg);
}

void PF1550_Control::turnSw2On(Sw2Mode const mode)
Expand All @@ -123,45 +160,56 @@ void PF1550_Control::turnSw2Off(Sw2Mode const mode)

void PF1550_Control::setFastChargeCurrent(IFastCharge const i_fast_charge)
{
uint8_t chg_curr_reg = _io.readRegister(Register::CHARGER_CHG_CURR_CFG);
uint8_t chg_curr_reg;
_io.readRegister(Register::CHARGER_CHG_CURR_CFG, &chg_curr_reg);
chg_curr_reg &= ~REG_CHG_CURR_CFG_CHG_CC_mask;
chg_curr_reg |= static_cast<uint8_t>(i_fast_charge);
_io.writeRegister(Register::CHARGER_CHG_CURR_CFG, chg_curr_reg);

writeReg(Register::CHARGER_CHG_CURR_CFG, chg_curr_reg);
}

void PF1550_Control::setFastChargeVoltage(VFastCharge const v_fast_charge)
{
uint8_t batt_reg = _io.readRegister(Register::CHARGER_BATT_REG);
uint8_t batt_reg;
_io.readRegister(Register::CHARGER_BATT_REG, &batt_reg);
batt_reg &= ~REG_BATT_REG_CHCCV_mask;
batt_reg |= static_cast<uint8_t>(v_fast_charge);
_io.writeRegister(Register::CHARGER_BATT_REG, batt_reg);

writeReg(Register::CHARGER_BATT_REG, batt_reg);
}

void PF1550_Control::setEndOfChargeCurrent(IEndOfCharge const i_end_of_charge)
{
uint8_t chg_eoc_cnfg = _io.readRegister(Register::CHARGER_CHG_EOC_CNFG);
uint8_t chg_eoc_cnfg;
_io.readRegister(Register::CHARGER_CHG_EOC_CNFG, &chg_eoc_cnfg);
chg_eoc_cnfg &= ~REG_CHG_EOC_CNFG_IEOC_mask;
chg_eoc_cnfg |= static_cast<uint8_t>(i_end_of_charge);
_io.writeRegister(Register::CHARGER_CHG_EOC_CNFG, chg_eoc_cnfg);

writeReg(Register::CHARGER_CHG_EOC_CNFG, chg_eoc_cnfg);
}

void PF1550_Control::setInputCurrentLimit(IInputCurrentLimit const i_input_current_limit)
{
uint8_t vbus_inlim_cnfg = _io.readRegister(Register::CHARGER_VBUS_INLIM_CNFG);
uint8_t vbus_inlim_cnfg;
_io.readRegister(Register::CHARGER_VBUS_INLIM_CNFG, &vbus_inlim_cnfg);
vbus_inlim_cnfg &= ~REG_VBUS_INLIM_CNFG_VBUS_LIN_INLIM_mask;
vbus_inlim_cnfg |= static_cast<uint8_t>(i_input_current_limit);
_io.writeRegister(Register::CHARGER_VBUS_INLIM_CNFG, vbus_inlim_cnfg);

writeReg(Register::CHARGER_VBUS_INLIM_CNFG, vbus_inlim_cnfg);
}

uint8_t PF1550_Control::getDeviceId()
{
return _io.readRegister(Register::PMIC_DEVICE_ID);
uint8_t deviceId;
_io.readRegister(Register::PMIC_DEVICE_ID, &deviceId);
return deviceId;
}

void PF1550_Control::onPMICEvent()
{
/* Retrieve the source of the interrupt */
uint8_t const int_category = _io.readRegister(Register::PMIC_INT_CATEGORY);
uint8_t int_category;
_io.readRegister(Register::PMIC_INT_CATEGORY, &int_category);

/* Call the appopriate event handler */
if(isBitSet(int_category, REG_INT_CATEGORY_CHG_INT_bp )) onChargerEvent ();
Expand Down
7 changes: 7 additions & 0 deletions src/PF1550/PF1550_Control.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "PF1550_Types.h"
#include "interface/PF1550_Io.h"
#include "Arduino.h"

/******************************************************************************
CONSTANT
Expand All @@ -45,7 +46,11 @@ class PF1550_Control


int begin();
void debug(Stream& stream);

void setBit(Register const reg, uint8_t const bit_pos);
void writeReg(Register const reg_addr, uint8_t val);
void readReg(Register const reg_addr, uint8_t *data);

/* LDO1 Configuration *******************************************************/
void setLDO1Voltage (Ldo1Voltage const ldo1_volt);
Expand Down Expand Up @@ -93,6 +98,8 @@ class PF1550_Control
void onTemperatureMonitorEvent();
void onMiscellaneousEvent();

Stream* _debug;

};

#endif /* PF1550_CONTROL_H_ */
Loading