Skip to content

Commit 9c3e539

Browse files
authored
Merge pull request #374 from ainyan03/power_output_result
Power: output setters return bool, add getPowerOutputCaps()
2 parents 9e3f914 + 52fc4ee commit 9c3e539

11 files changed

Lines changed: 436 additions & 217 deletions

File tree

examples/Test/build_test/main/main.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,24 @@ static void test_power(void)
7070
M5.Power.setLed(64);
7171
M5.Power.setExtOutput(true);
7272
M5.Power.setExtPower(true);
73+
(void)M5.Power.getPowerOutputCaps();
74+
(void)M5.Power.setUsbOutput(true);
75+
(void)M5.Power.setVibration(1);
76+
(void)M5.Power.setExtPortBusConfig({});
7377
M5.Power.setBatteryCharge(true);
7478
M5.Power.setChargeCurrent(500);
79+
#if !defined(CONFIG_IDF_TARGET_ESP32S3) && !defined(CONFIG_IDF_TARGET_ESP32C3) && !defined(CONFIG_IDF_TARGET_ESP32C6) \
80+
&& !defined(CONFIG_IDF_TARGET_ESP32C61) && !defined(CONFIG_IDF_TARGET_ESP32P4)
81+
int8_t battery_level;
82+
int32_t shunt_mv;
83+
(void)M5.Power.Axp192.getBatteryLevel(&battery_level);
84+
(void)M5.Power.Ina3221[0].getShuntMilliVoltage(0, &shunt_mv);
85+
#endif
86+
#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C5) \
87+
|| defined(CONFIG_IDF_TARGET_ESP32C61) || defined(CONFIG_IDF_TARGET_ESP32P4)
88+
bool gpio_high;
89+
(void)M5.Power.M5pm1.getGPIOOutputLatch(m5::M5PM1_Class::gpio0, &gpio_high);
90+
#endif
7591
(void)M5.Power.getVBUSVoltage();
7692
(void)M5.Power.getBatteryLevel();
7793
(void)M5.Power.getBatteryVoltage();

src/utility/Power_Class.hpp

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,17 +205,44 @@ namespace m5
205205
, cap_set_charge_voltage = 1u << 2
206206
};
207207

208+
/// The power output paths a model provides. (see getPowerOutputCaps)
209+
/// @note This mask is deliberately separate from ext_port_mask_t, so that a
210+
/// port bit never gets tied to a capability bit.
211+
enum power_output_capability_t : std::uint8_t
212+
{ cap_set_ext_output = 1u << 0
213+
, cap_set_usb_output = 1u << 1
214+
, cap_set_ext_port_bus = 1u << 2
215+
, cap_set_vibration = 1u << 3
216+
};
217+
208218
bool begin(void);
209219

210220
/// Set power output of the external ports.
211221
/// @param enable true=output / false=input
212-
/// @param port_mask for M5Station. ext_port (bitmask).
222+
/// @param port_mask for M5Station / M5PowerHub / Tab5 family / CoreP4X / ChainCaptain. ext_port (bitmask).
223+
/// @return true if the requested state was reached on every selected port.
224+
/// @note A successful return means the writes were accepted; state is not read back
225+
/// (except when disabling the CoreS3 family output).
226+
/// @note false means a model without the path (see getPowerOutputCaps()),
227+
/// an I2C failure, or a protection check that cancelled the request
228+
/// (Core2 / Tough on a low battery, the CoreS3 family without a battery).
229+
/// Core2 / Tough with AXP2101: when the INA3221 did not respond at begin(),
230+
/// the current-direction part of that check is skipped.
231+
/// On the CoreS3 family it is also false when the request could not take the
232+
/// AW9523 lock or was superseded by a later request while it waited.
233+
/// Every selected port is still attempted; false says at least one
234+
/// did not take effect, not which one.
235+
/// @note port_mask is honoured only on models with individually switched ports
236+
/// (M5Station, PowerHub, Tab5 family, CoreP4X, ChainCaptain); there a mask
237+
/// that selects none of the model's ports returns false without touching
238+
/// anything. Models with a single output ignore the mask, except that
239+
/// ext_none returns false on every model.
213240
/// @note On the CoreS3 family (CoreS3 / CoreS3 SE / StackChan), disabling an enabled output blocks for
214241
/// about 200 ms (the boost converter is stopped first and the bus is left to discharge before the
215242
/// switch-over), and enabling without a battery may block for up to 1 s while the protection check
216243
/// waits for the TS reading to settle. The switch-over is serialized with setUsbOutput and the
217244
/// internal speaker enable, so those may wait for it as well.
218-
void setExtOutput(bool enable, ext_port_mask_t port_mask = (ext_port_mask_t)0xFF);
245+
bool setExtOutput(bool enable, ext_port_mask_t port_mask = (ext_port_mask_t)0xFF);
219246

220247
/// deprecated : Change to "setExtOutput"
221248
[[deprecated("Change to setExtOutput")]]
@@ -227,9 +254,12 @@ namespace m5
227254

228255
/// Set power output of the main USB port.
229256
/// @param enable true=output / false=input
257+
/// @return true if the requested state was reached.
258+
/// @note false means a model without the path (see getPowerOutputCaps()), an I2C failure,
259+
/// or, on the CoreS3 family, failure to acquire the AW9523 lock.
230260
/// @attention for M5Stack CoreS3 main USB port.
231261
/// @attention ※ Not for M5Station/M5Tab external USB.
232-
void setUsbOutput(bool enable);
262+
bool setUsbOutput(bool enable);
233263

234264
/// Get power output of the main USB port.
235265
/// @return true=output enabled / false=output disabled
@@ -439,12 +469,24 @@ namespace m5
439469

440470
/// Set the configuration of the external port bus.
441471
/// @param config Configuration of the external port bus.
472+
/// @return true if the configuration was written.
473+
/// @note false means a model without the path (see getPowerOutputCaps()) or an I2C failure.
442474
/// @attention for M5PowerHub.
443-
void setExtPortBusConfig(const ext_port_bus_t& config);
475+
bool setExtPortBusConfig(const ext_port_bus_t& config);
444476

445477
/// Operate the vibration motor
446478
/// @param level Vibration strength of the motor. (0=stop)
447-
void setVibration(uint8_t level);
479+
/// @return true if the requested level was applied.
480+
/// @note false means a model without a motor (see getPowerOutputCaps()) or an I2C failure.
481+
/// @note A nonzero level is clamped to the rail minimum: 1800 mV on AXP192
482+
/// LDO3 and 500 mV on AXP2101 DLDO1.
483+
bool setVibration(uint8_t level);
484+
485+
/// Get which power output paths this model provides.
486+
/// @return bitmask of power_output_capability_t. 0 = no output path, or M5.begin() has not completed yet.
487+
/// @note A set bit says the path exists, not that a call on it will succeed:
488+
/// the setters still return false on an I2C failure or a cancelled request.
489+
std::uint8_t getPowerOutputCaps(void);
448490

449491
pmic_t getType(void) const { return _pmic; }
450492

0 commit comments

Comments
 (0)