Ken Van Hoeylandt bb7e79886f
Various fixes and improvements (#182)
- Fix for `logMutex` bug where the `Mutex` constructor is not called when doing early boot logging (with `DEBUG` level logging) . The only way to make it work is to explicitly call the constructor in the logging wrapper function.
- Fix for unPhone power states
2025-01-23 21:17:33 +01:00

107 lines
3.1 KiB
C++

#include "Bq24295.h"
#include "Log.h"
#define TAG "bq24295"
/** Reference:
* https://www.ti.com/lit/ds/symlink/bq24295.pdf
* https://gitlab.com/hamishcunningham/unphonelibrary/-/blob/main/unPhone.h?ref_type=heads
*/
namespace registers {
static const uint8_t CHARGE_TERMINATION = 0x05U; // Datasheet page 35: Charge end/timer cntrl
static const uint8_t OPERATION_CONTROL = 0x07U; // Datasheet page 37: Misc operation control
static const uint8_t STATUS = 0x08U; // Datasheet page 38: System status
static const uint8_t VERSION = 0x0AU; // Datasheet page 38: Vendor/part/revision status
} // namespace registers
bool Bq24295::readChargeTermination(uint8_t& out) const {
return readRegister8(registers::CHARGE_TERMINATION, out);
}
// region Watchdog
bool Bq24295::getWatchDogTimer(WatchDogTimer& out) const {
uint8_t value;
if (readChargeTermination(value)) {
uint8_t relevant_bits = value & (BIT(4) | BIT(5));
switch (relevant_bits) {
case 0b000000:
out = WatchDogTimer::Disabled;
return true;
case 0b010000:
out = WatchDogTimer::Enabled40s;
return true;
case 0b100000:
out = WatchDogTimer::Enabled80s;
return true;
case 0b110000:
out = WatchDogTimer::Enabled160s;
return true;
default:
return false;
}
}
return false;
}
bool Bq24295::setWatchDogTimer(WatchDogTimer in) const {
uint8_t value;
if (readChargeTermination(value)) {
uint8_t bits_to_set = 0b00110000 & static_cast<uint8_t>(in);
uint8_t value_cleared = value & 0b11001111;
uint8_t to_set = bits_to_set & value_cleared;
TT_LOG_I(TAG, "WatchDogTimer: %02x -> %02x", value, to_set);
return writeRegister8(registers::CHARGE_TERMINATION, to_set);
}
return false;
}
// endregoin
// region Operation Control
bool Bq24295::getOperationControl(uint8_t value) const {
return readRegister8(registers::OPERATION_CONTROL, value);
}
bool Bq24295::setOperationControlBitOn(uint8_t mask) const {
return bitOn(registers::OPERATION_CONTROL, mask);
}
bool Bq24295::setOperationControlBitOff(uint8_t mask) const {
return bitOff(registers::OPERATION_CONTROL, mask);
}
// endregion
// region Other
bool Bq24295::getStatus(uint8_t& value) const {
return readRegister8(registers::STATUS, value);
}
bool Bq24295::isUsbPowerConnected() const {
uint8_t status;
if (getStatus(status)) {
return (status & BIT(2)) != 0U;
} else {
return false;
}
}
bool Bq24295::getVersion(uint8_t& value) const {
return readRegister8(registers::VERSION, value);
}
void Bq24295::printInfo() const {
uint8_t version, status, charge_termination;
if (getStatus(status) && getVersion(version) && readChargeTermination(charge_termination)) {
TT_LOG_I(TAG, "Version %d, status %02x, charge termination %02x", version, status, charge_termination);
} else {
TT_LOG_E(TAG, "Failed to retrieve version and/or status");
}
}
// endregion