I2C improvements and fixes (#201)

- Show I2C device name in I2C Scanner app
- Register various I2C devices from board implementations
- Fix M5Stack Core2 power status
- Fix pre-allocation issue in `hal::Device`
This commit is contained in:
Ken Van Hoeylandt
2025-02-02 17:54:36 +01:00
committed by GitHub
parent 2e61aea93c
commit c0f4738abe
13 changed files with 133 additions and 59 deletions
+20 -15
View File
@@ -5,8 +5,7 @@
#define TAG "unphone"
extern UnPhoneFeatures unPhoneFeatures;
std::shared_ptr<UnPhoneFeatures> unPhoneFeatures;
static std::unique_ptr<tt::Thread> powerThread;
static const char* bootCountKey = "boot_count";
@@ -89,13 +88,13 @@ static void powerInfoBuzz(uint8_t count) {
static void updatePowerSwitch() {
static PowerState last_state = PowerState::Initial;
if (!unPhoneFeatures.isPowerSwitchOn()) {
if (!unPhoneFeatures->isPowerSwitchOn()) {
if (last_state != PowerState::Off) {
last_state = PowerState::Off;
TT_LOG_W(TAG, "Power off");
}
if (!unPhoneFeatures.isUsbPowerConnected()) { // and usb unplugged we go into shipping mode
if (!unPhoneFeatures->isUsbPowerConnected()) { // and usb unplugged we go into shipping mode
TT_LOG_W(TAG, "Shipping mode until USB connects");
#if DEBUG_POWER_STATES
@@ -104,11 +103,11 @@ static void updatePowerSwitch() {
unPhoneFeatures.setExpanderPower(false);
#endif
unPhoneFeatures.turnPeripheralsOff();
unPhoneFeatures->turnPeripheralsOff();
bootStats.notifyPowerOff();
unPhoneFeatures.setShipping(true); // tell BM to stop supplying power until USB connects
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");
@@ -116,13 +115,13 @@ static void updatePowerSwitch() {
powerInfoBuzz(2);
#endif
unPhoneFeatures.turnPeripheralsOff();
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();
unPhoneFeatures->wakeOnPowerSwitch();
esp_sleep_enable_timer_wakeup(60000000);
esp_deep_sleep_start();
}
@@ -155,23 +154,29 @@ static void startPowerSwitchThread() {
powerThread->start();
}
std::shared_ptr<Bq24295> bq24295;
static bool unPhonePowerOn() {
// Print early, in case of early crash (info will be from previous boot)
bootStats.printInfo();
bootStats.notifyBootStart();
if (!unPhoneFeatures.init()) {
bq24295 = std::make_shared<Bq24295>(I2C_NUM_0);
tt::hal::registerDevice(bq24295);
unPhoneFeatures = std::make_shared<UnPhoneFeatures>(bq24295);
if (!unPhoneFeatures->init()) {
TT_LOG_E(TAG, "UnPhoneFeatures init failed");
return false;
}
unPhoneFeatures.printInfo();
unPhoneFeatures->printInfo();
unPhoneFeatures.setBacklightPower(false);
unPhoneFeatures.setVibePower(false);
unPhoneFeatures.setIrPower(false);
unPhoneFeatures.setExpanderPower(false);
unPhoneFeatures->setBacklightPower(false);
unPhoneFeatures->setVibePower(false);
unPhoneFeatures->setIrPower(false);
unPhoneFeatures->setExpanderPower(false);
// Turn off the device if power switch is on off state,
// instead of waiting for the Thread to start and continue booting
-3
View File
@@ -8,9 +8,6 @@ bool unPhoneInitPower();
bool unPhoneInitHardware();
bool unPhoneInitLvgl();
// Shared object, used in PowerOn and UnPhoneDisplay
UnPhoneFeatures unPhoneFeatures;
extern const tt::hal::Configuration unPhone = {
.initBoot = unPhoneInitPower,
.initHardware = unPhoneInitHardware,
+6 -6
View File
@@ -223,7 +223,7 @@ bool UnPhoneFeatures::init() {
void UnPhoneFeatures::printInfo() const {
esp_io_expander_print_state(ioExpander);
batteryManagement.printInfo();
batteryManagement->printInfo();
bool backlight_power;
const char* backlight_power_state = getBacklightPower(backlight_power) && backlight_power ? "on" : "off";
TT_LOG_I(TAG, "Backlight: %s", backlight_power_state);
@@ -282,12 +282,12 @@ void UnPhoneFeatures::turnPeripheralsOff() const {
bool UnPhoneFeatures::setShipping(bool on) const {
if (on) {
TT_LOG_W(TAG, "setShipping: on");
batteryManagement.setWatchDogTimer(Bq24295::WatchDogTimer::Disabled);
batteryManagement.setBatFetOn(false);
batteryManagement->setWatchDogTimer(Bq24295::WatchDogTimer::Disabled);
batteryManagement->setBatFetOn(false);
} else {
TT_LOG_W(TAG, "setShipping: off");
batteryManagement.setWatchDogTimer(Bq24295::WatchDogTimer::Enabled40s);
batteryManagement.setBatFetOn(true);
batteryManagement->setWatchDogTimer(Bq24295::WatchDogTimer::Enabled40s);
batteryManagement->setBatFetOn(true);
}
return true;
}
@@ -297,5 +297,5 @@ void UnPhoneFeatures::wakeOnPowerSwitch() const {
}
bool UnPhoneFeatures::isUsbPowerConnected() const {
return batteryManagement.isUsbPowerConnected();
return batteryManagement->isUsbPowerConnected();
}
+7 -3
View File
@@ -7,12 +7,11 @@
/**
* Easy access to GPIO pins
*/
class UnPhoneFeatures {
class UnPhoneFeatures final {
private:
esp_io_expander_handle_t ioExpander = nullptr;
Bq24295 batteryManagement = Bq24295(I2C_NUM_0);
tt::Thread buttonHandlingThread;
bool buttonHandlingThreadInterruptRequest = false;
@@ -21,9 +20,14 @@ private:
static bool initPowerSwitch();
bool initGpioExpander();
std::shared_ptr<Bq24295> batteryManagement;
public:
UnPhoneFeatures() = default;
explicit UnPhoneFeatures(std::shared_ptr<Bq24295> bq24295) : batteryManagement(std::move(bq24295)) {
assert(batteryManagement != nullptr);
}
~UnPhoneFeatures();
bool init();
+2 -2
View File
@@ -13,7 +13,7 @@
#define TAG "unphone_display"
#define BUFFER_SIZE (UNPHONE_LCD_HORIZONTAL_RESOLUTION * UNPHONE_LCD_DRAW_BUFFER_HEIGHT * LV_COLOR_DEPTH / 8)
extern UnPhoneFeatures unPhoneFeatures;
extern std::shared_ptr<UnPhoneFeatures> unPhoneFeatures;
bool UnPhoneDisplay::start() {
TT_LOG_I(TAG, "Starting");
@@ -47,7 +47,7 @@ bool UnPhoneDisplay::start() {
if (displayHandle != nullptr) {
TT_LOG_I(TAG, "Finished");
unPhoneFeatures.setBacklightPower(true);
unPhoneFeatures->setBacklightPower(true);
return true;
} else {
TT_LOG_I(TAG, "Failed");