Fixes and improvements (#185)

- unPhone improvements related to power and boot (add boot count logging)
- Cleanup of Mutex acquire/release
- Removed `tt_assert()` in favour of `assert()`
- Fix sim build (likely failed due to migration of GitHub Actions to Ubuntu 24.04)
This commit is contained in:
Ken Van Hoeylandt
2025-01-24 22:49:29 +01:00
committed by GitHub
parent 3be251d8fb
commit d86dc40472
47 changed files with 223 additions and 177 deletions
+68 -2
View File
@@ -1,3 +1,4 @@
#include "Preferences.h"
#include "TactilityCore.h"
#include "UnPhoneFeatures.h"
#include <esp_sleep.h>
@@ -8,6 +9,55 @@ extern UnPhoneFeatures unPhoneFeatures;
static std::unique_ptr<tt::Thread> powerThread;
static const char* bootCountKey = "boot_count";
static const char* powerOffCountKey = "power_off_count";
static const char* powerSleepKey = "power_sleep_key";
class DeviceStats {
private:
tt::Preferences preferences = tt::Preferences("unphone");
int32_t getValue(const char* key) {
int32_t value = 0;
preferences.optInt32(key, value);
return value;
}
void setValue(const char* key, int32_t value) {
preferences.putInt32(key, value);
}
void increaseValue(const char* key) {
int32_t new_value = getValue(key) + 1;
setValue(key, new_value);
}
public:
void notifyBootStart() {
increaseValue(bootCountKey);
}
void notifyPowerOff() {
increaseValue(powerOffCountKey);
}
void notifyPowerSleep() {
increaseValue(powerSleepKey);
}
void printInfo() {
TT_LOG_I("TAG", "Device stats:");
TT_LOG_I("TAG", " boot: %ld", getValue(bootCountKey));
TT_LOG_I("TAG", " power off: %ld", getValue(powerOffCountKey));
TT_LOG_I("TAG", " power sleep: %ld", getValue(powerSleepKey));
}
};
DeviceStats bootStats;
enum class PowerState {
Initial,
On,
@@ -16,6 +66,7 @@ enum class PowerState {
#define DEBUG_POWER_STATES false
#if DEBUG_POWER_STATES
/** Helper method to use the buzzer to signal the different power stages */
static void powerInfoBuzz(uint8_t count) {
if (DEBUG_POWER_STATES) {
@@ -33,6 +84,7 @@ static void powerInfoBuzz(uint8_t count) {
}
}
}
#endif
static void updatePowerSwitch() {
static PowerState last_state = PowerState::Initial;
@@ -46,19 +98,28 @@ static void updatePowerSwitch() {
if (!unPhoneFeatures.isUsbPowerConnected()) { // and usb unplugged we go into shipping mode
TT_LOG_W(TAG, "Shipping mode until USB connects");
#if DEBUG_POWER_STATES
unPhoneFeatures.setExpanderPower(true);
powerInfoBuzz(3);
unPhoneFeatures.setExpanderPower(false);
#endif
unPhoneFeatures.turnPeripheralsOff();
bootStats.notifyPowerOff();
unPhoneFeatures.setShipping(true); // tell BM to stop supplying power until USB connects
} else { // When power switch is off, but USB is plugged in, we wait (deep sleep) until USB is unplugged.
TT_LOG_W(TAG, "Waiting for USB disconnect to power off");
#if DEBUG_POWER_STATES
powerInfoBuzz(2);
#endif
unPhoneFeatures.turnPeripheralsOff();
bootStats.notifyPowerSleep();
// Deep sleep for 1 minute, then awaken to check power state again
// GPIO trigger from power switch also awakens the device
unPhoneFeatures.wakeOnPowerSwitch();
@@ -70,9 +131,9 @@ static void updatePowerSwitch() {
last_state = PowerState::On;
TT_LOG_W(TAG, "Power on");
unPhoneFeatures.setShipping(false);
unPhoneFeatures.setExpanderPower(true);
#if DEBUG_POWER_STATES
powerInfoBuzz(1);
#endif
}
}
}
@@ -95,6 +156,11 @@ static void startPowerSwitchThread() {
}
static bool unPhonePowerOn() {
// Print early, in case of early crash (info will be from previous boot)
bootStats.printInfo();
bootStats.notifyBootStart();
if (!unPhoneFeatures.init()) {
TT_LOG_E(TAG, "UnPhoneFeatures init failed");
return false;
+2 -4
View File
@@ -283,13 +283,11 @@ bool UnPhoneFeatures::setShipping(bool on) const {
if (on) {
TT_LOG_W(TAG, "setShipping: on");
batteryManagement.setWatchDogTimer(Bq24295::WatchDogTimer::Disabled);
// Set bit 5 to disable
batteryManagement.setOperationControlBitOn(1 << 5);
batteryManagement.setBatFetOn(false);
} else {
TT_LOG_W(TAG, "setShipping: off");
batteryManagement.setWatchDogTimer(Bq24295::WatchDogTimer::Enabled40s);
// Clear bit 5 to enable
batteryManagement.setOperationControlBitOff(1 << 5);
batteryManagement.setBatFetOn(true);
}
return true;
}
+9 -11
View File
@@ -60,18 +60,16 @@ bool Bq24295::setWatchDogTimer(WatchDogTimer in) const {
// endregoin
// region Operation Control
// region Operation Control (REG07)
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);
bool Bq24295::setBatFetOn(bool on) const {
if (on) {
// bit 5 low means bat fet is on
return bitOff(registers::OPERATION_CONTROL, BIT(5));
} else {
// bit 5 high means bat fet is off
return bitOn(registers::OPERATION_CONTROL, BIT(5));
}
}
// endregion
+1 -3
View File
@@ -26,9 +26,7 @@ public:
bool isUsbPowerConnected() const;
bool getOperationControl(uint8_t value) const;
bool setOperationControlBitOn(uint8_t mask) const;
bool setOperationControlBitOff(uint8_t mask) const;
bool setBatFetOn(bool on) const;
bool getStatus(uint8_t& value) const;
bool getVersion(uint8_t& value) const;
+1 -1
View File
@@ -56,7 +56,7 @@ bool UnPhoneDisplay::start() {
}
bool UnPhoneDisplay::stop() {
tt_assert(displayHandle != nullptr);
assert(displayHandle != nullptr);
lv_display_delete(displayHandle);
displayHandle = nullptr;