Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
Namespaces | Typedefs | Functions | Variables
lampda::logic::power Namespace Reference

Main entry point of the power handling of the board. Responsible the main state of the power handling. More...

Namespaces

namespace  __private
 Internal implementations.
 

Typedefs

using PowerStates = enum class power_states_t { STARTUP, IDLE, CLEAR_POWER_RAILS, CHARGING_MODE, OUTPUT_VOLTAGE_MODE, OTG_MODE, SHUTDOWN, ERROR, }
 Define the state for the power state machine.
 

Functions

void power_loop ()
 main loop of the power, running in another thread.
 
std::string get_error_string ()
 Return the error string if set, or "x".
 
void set_error_state_message (const std::string &errorMsg)
 Set the error message, if not already set.
 
bool is_output_mode_ready ()
 Return true when the current state is OUTPUT_VOLTAGE_MODE, and the desired voltage are set and gates are ready. Basically, it returns true when the output is being powered.
 
uint32_t get_vbus_rail_voltage ()
 
uint32_t get_power_rail_voltage ()
 
void set_otg_parameters (uint16_t voltage_mV, uint16_t current_mA)
 
void handle_clear_power_rails ()
 
void handle_charging_mode ()
 
void handle_output_voltage_mode ()
 
void handle_otg_mode ()
 
void handle_shutdown ()
 
void handle_startup ()
 
void handle_error_state ()
 
bool is_in_error_state ()
 Return true if the power handling is locked in an ERROR state. No power actions can be made.
 
bool go_to_output_mode ()
 Set the power handler to main OUTPUT_VOLTAGE_MODE.
 
bool go_to_charger_mode ()
 Set the power handler to battery CHARGING_MODE.
 
bool go_to_otg_mode ()
 Set the power handler to OTG_MODE (eg: external battery mode)
 
bool go_to_idle ()
 Set the power handler to IDLE mode (no power output/input)
 
bool go_to_shutdown ()
 Set the power handler to SHUTDOWN mode (last mode before shuting system down)
 
bool go_to_error ()
 Set the power handler to ERROR mode (locked safety state)
 
void set_output_voltage_mv (const uint16_t outputVoltage_mV)
 Set the desired output voltage of the charger.
 
void set_output_max_current_mA (const uint16_t outputCurrent_mA)
 Set the desired maximum output current of the charger. This is just an information, that will be used when the state machine reaches the OUTPUT_VOLTAGE_MODE state.
 
void set_temporary_output (const uint16_t outputVoltage_mV, const uint16_t outputCurrent_mA, const uint16_t timeout_ms)
 Set a new output with a time limit, after wich the output will go back to the original values. It is canceled at any point by a call to set_output_voltage_mv. This is just an information, that will be used when the state machine reaches the OUTPUT_VOLTAGE_MODE state.
 
bool enable_charge (const bool)
 Block or allow the charging of the battery. Note that the system can still be in CHARGING_MODE mode if you set this to false, but the battery just wont charge.
 
std::string get_state ()
 Return the current state, as a string.
 
bool is_in_output_mode ()
 Return true if the current state is OUTPUT_VOLTAGE_MODE.
 
bool is_in_otg_mode ()
 Return true if the current state is OTG_MODE.
 
bool was_started_in_battery_recovery ()
 Indicates if the charger tried the battery recovery system.
 
bool is_setup ()
 Return true if the power handler system is effectivly started.
 
bool is_started ()
 Return true when power machine finally exits the STARTUP.
 
void init ()
 Call once at system startup. This can fail, and it would set the state to ERROR.
 

Variables

bool _isShutdownCompleted = false
 Set to true if the shutdown process is finished cleanly.
 
static constexpr uint32_t clearPowerRailMinDelay_ms = 10
 Minimum allowed power rail clear delay.
 
static constexpr uint32_t clearPowerRailFailureDelay_ms = 3000
 Minimum power rail clear delay after which a failure is signaled. In some rare case the rail can take up to 2 seconds to clear itself.
 
static constexpr uint32_t otgNoUseTimeToDisconnect_ms = 1000
 Delay after which the OTG_MODE will auto disable if a disconnection is detected.
 
static constexpr uint8_t otgTurnOffTimeMinutes = 5
 Delay after which the OTG_MODE will auto disable if not used.
 
static constexpr uint32_t otgNoUseExtBatTimeToDisconnect_ms = 1000 * 60 * otgTurnOffTimeMinutes
 Delay after which the OTG_MODE will auto disable if not used, in milliseconds.
 
static constexpr uint32_t startupFailTimeout_ms = 3000
 Delay after which the STARTUP state will auto switch to error.
 
static uint16_t _outputVoltage_mV = 0
 true voltage output
 
static uint16_t _outputCurrent_mA = 0
 true current output limit
 
static uint16_t _temporaryOutputVoltage_mV = 0
 temporary voltage
 
static uint16_t _temporaryOutputCurrent_mA = 0
 temporary current limits
 
static uint32_t _temporaryOutputTimeOut = 0
 temporary output timeout time
 
static bool _isChargeEnabled = false
 True if battery charging is allowed.
 
static std::string _errorStr = ""
 Store the state error description.
 
static bool _hasAutoSwitchedToOTG = false
 True if the state changed to OTG_MODE on it's own (via USB-PD requests).
 
static bool _isInBatteryRecoveryMode = false
 in this mode, the battery is too low to power the components. They will be powered through the vbus gate.
 
static bool _wasStartedInBatteryRecoveryMode = false
 
const char *const PowerStatesStr []
 Name of the states as strings.
 
static bool s_isOutputModeReady = false
 True if the OUTPUT_MODE state is ready, ie if the mode is active, the power is as requested and the output gate is enabled.
 
static uint32_t timeSinceOTGNoCurrentUse
 
static uint32_t timeSinceOTGCurrentUse
 
static bool isSetup = false
 

Detailed Description

Main entry point of the power handling of the board. Responsible the main state of the power handling.

Function Documentation

◆ enable_charge()

bool lampda::logic::power::enable_charge ( const bool  )

Block or allow the charging of the battery. Note that the system can still be in CHARGING_MODE mode if you set this to false, but the battery just wont charge.

Returns
True if accepted

◆ get_error_string()

std::string lampda::logic::power::get_error_string ( )

Return the error string if set, or "x".

Return the current error message, as a string. If not errors are present, will return "x".

◆ go_to_charger_mode()

bool lampda::logic::power::go_to_charger_mode ( )

Set the power handler to battery CHARGING_MODE.

Returns
true if the mode switch is accepted

◆ go_to_error()

bool lampda::logic::power::go_to_error ( )

Set the power handler to ERROR mode (locked safety state)

Returns
true if the mode switch is accepted

◆ go_to_idle()

bool lampda::logic::power::go_to_idle ( )

Set the power handler to IDLE mode (no power output/input)

Returns
true if the mode switch is accepted

◆ go_to_otg_mode()

bool lampda::logic::power::go_to_otg_mode ( )

Set the power handler to OTG_MODE (eg: external battery mode)

Returns
true if the mode switch is accepted

◆ go_to_output_mode()

bool lampda::logic::power::go_to_output_mode ( )

Set the power handler to main OUTPUT_VOLTAGE_MODE.

Returns
true if the mode switch is accepted

◆ go_to_shutdown()

bool lampda::logic::power::go_to_shutdown ( )

Set the power handler to SHUTDOWN mode (last mode before shuting system down)

Returns
true if the mode switch is accepted

◆ handle_startup()

void lampda::logic::power::handle_startup ( )

Component initialization

◆ init()

void lampda::logic::power::init ( )

Call once at system startup. This can fail, and it would set the state to ERROR.

TODO: issue #326 handle the VBUS gate fault cleanly

◆ is_setup()

bool lampda::logic::power::is_setup ( )

Return true if the power handler system is effectivly started.

Returns
False if the init() call failed.

◆ set_output_max_current_mA()

void lampda::logic::power::set_output_max_current_mA ( const uint16_t  outputCurrent_mA)

Set the desired maximum output current of the charger. This is just an information, that will be used when the state machine reaches the OUTPUT_VOLTAGE_MODE state.

Parameters
[in]outputCurrent_mADesired maximum allowed output current, in milliamps

◆ set_output_voltage_mv()

void lampda::logic::power::set_output_voltage_mv ( const uint16_t  outputVoltage_mV)

Set the desired output voltage of the charger.

This is just an information, that will be used when the state machine reaches the OUTPUT_VOLTAGE_MODE state.

Parameters
[in]outputVoltage_mVDesired output voltage, in millivolts

◆ set_temporary_output()

void lampda::logic::power::set_temporary_output ( const uint16_t  outputVoltage_mV,
const uint16_t  outputCurrent_mA,
const uint16_t  timeout_ms 
)

Set a new output with a time limit, after wich the output will go back to the original values. It is canceled at any point by a call to set_output_voltage_mv. This is just an information, that will be used when the state machine reaches the OUTPUT_VOLTAGE_MODE state.

Parameters
[in]outputVoltage_mVdesired temporary output voltage, in millivolts
[in]outputCurrent_mAdesired temporary maximum output current, in milliamps
[in]timeout_msTime delay after which the temporary mode will be disabled

Variable Documentation

◆ PowerStatesStr

const char* const lampda::logic::power::PowerStatesStr[]
Initial value:
= {
"STARTUP",
"IDLE",
"CLEAR_POWER_RAILS",
"CHARGING_MODE",
"OUTPUT_VOLTAGE_MODE",
"OTG_MODE",
"SHUTDOWN",
"ERROR",
}

Name of the states as strings.