Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
input_power_policy.h
Go to the documentation of this file.
1
7#ifndef BSP_PD_INPUT_POWER_POLICY_H
8#define BSP_PD_INPUT_POWER_POLICY_H
9
10#include <cstdint>
11
12namespace lampda {
13namespace bsp {
14namespace powerDelivery {
16namespace policy {
17
19static constexpr uint32_t standardPortDetectionDelay_ms = 1500;
21static constexpr uint32_t pdLostDelay_ms = 1000;
28static constexpr uint32_t maxPdNegociationWait_ms = 8000;
30static constexpr uint16_t standardPortCurrent_mA = 800;
32static constexpr uint32_t pdSourceTolerance_percent = 5;
34static constexpr uint32_t pdCableDrop_mV = 500;
36static constexpr uint32_t vbusMeasureResolution_mV = 420;
37
42inline uint32_t pd_min_established_vbus_mV(const uint32_t negotiatedVoltage_mV)
43{
44 const uint32_t margin_mV =
46 return negotiatedVoltage_mV > margin_mV ? negotiatedVoltage_mV - margin_mV : 0;
47}
49static constexpr float pdCurrentRatio = 0.90f;
50
52static constexpr uint32_t unplugDeglitch_ms = 1000;
59static constexpr uint32_t hardResetUnplugDeglitch_ms = 3000;
60
68inline bool is_vbus_from_own_output(const bool isSwitchingToOtg,
69 const bool isChargerInOtg,
70 const bool isOutputGateEnabled)
71{
72 return isSwitchingToOtg or (isChargerInOtg and not isOutputGateEnabled);
73}
74
76inline bool is_unplugged(const uint32_t timeSinceVbusValid_ms, const bool isSinkHardResetting)
77{
78 return timeSinceVbusValid_ms > (isSinkHardResetting ? hardResetUnplugDeglitch_ms : unplugDeglitch_ms);
79}
80
88{
89public:
91 void on_vbus_valid(const uint32_t time_ms)
92 {
93 lastVbusValid_ms_ = time_ms;
94 wasHardResetSeen_ = false;
95 }
96
98 bool is_unplugged(const uint32_t time_ms, const bool isSinkHardResetting)
99 {
100 wasHardResetSeen_ = wasHardResetSeen_ or isSinkHardResetting;
101 return policy::is_unplugged(time_ms - lastVbusValid_ms_, wasHardResetSeen_);
102 }
103
104private:
105 uint32_t lastVbusValid_ms_ = 0;
106 bool wasHardResetSeen_ = false;
107};
108
111{
119 bool isUsbPd = false;
121 bool isSinkReady = false;
123 bool isSinkNegotiating = false;
126 uint32_t negotiatedCurrent_mA = 0;
128 uint32_t vbusVoltage_mV = 0;
129};
130
133{
134 return s.negotiatedVoltage_mV > 0 and s.isSinkReady and
135 // the voltage/current rise should be over
137}
138
140inline bool is_standard_port(const SourceStatus& s)
141{
142 return s.isPowerSourceDetected and
143 // let some time pass after a new detection
145 // last pd detected was some time ago
147 // a late PD negociation (soft/hard reset when the source caps were missed) can take seconds.
148 // Pulling current from a fast charger before it ends can make it crash (issue #633)
150}
151
153inline bool can_use_power(const SourceStatus& s)
154{
156}
157
159inline uint16_t max_input_current_mA(const SourceStatus& s)
160{
161 // safety
162 if (not s.isPowerSourceDetected)
163 return 0;
164
165 // power delivery detected
166 if (s.isUsbPd)
167 {
169 return static_cast<uint16_t>(s.negotiatedCurrent_mA * pdCurrentRatio);
170 // no contract yet
171 return 0;
172 }
173 // no usb pd since some time, and vbus seems stable so try to use it
174 if (is_standard_port(s))
175 // maximum USB current (hacky, would prefer USB type detection)
177
178 // we dont known for now the type of connection
179 return 0;
180}
181
182} // namespace policy
183} // namespace powerDelivery
184} // namespace bsp
185} // namespace lampda
186
187#endif
Definition: input_power_policy.h:88
bool is_unplugged(const uint32_t time_ms, const bool isSinkHardResetting)
VBUS is not valid: return true if this loss is an unplug.
Definition: input_power_policy.h:98
void on_vbus_valid(const uint32_t time_ms)
VBUS is valid: a new loss starts from this time.
Definition: input_power_policy.h:91
static constexpr uint32_t hardResetUnplugDeglitch_ms
Definition: input_power_policy.h:59
uint16_t max_input_current_mA(const SourceStatus &s)
Maximum current that can be pulled from the source.
Definition: input_power_policy.h:159
bool is_unplugged(const uint32_t timeSinceVbusValid_ms, const bool isSinkHardResetting)
VBUS was lost long enough to consider the source unplugged.
Definition: input_power_policy.h:76
static constexpr uint16_t standardPortCurrent_mA
Current pulled from a standard port (ICO finds the real maximum)
Definition: input_power_policy.h:30
static constexpr uint32_t pdLostDelay_ms
Delay after the last PD source detection before considering it a standard port.
Definition: input_power_policy.h:21
bool can_use_pd_full_power(const SourceStatus &s)
The PD contract is established, and VBUS reached the negociated voltage.
Definition: input_power_policy.h:132
static constexpr uint32_t pdCableDrop_mV
Cable and connectors voltage drop allowed on VBUS (USB-C, at 3A)
Definition: input_power_policy.h:34
static constexpr uint32_t unplugDeglitch_ms
VBUS can be lost this long before the source is considered unplugged (flicker, ICO algorithm)
Definition: input_power_policy.h:52
static constexpr uint32_t standardPortDetectionDelay_ms
Delay after a source detection (or a PD resume) before considering it a standard (non PD) port.
Definition: input_power_policy.h:19
uint32_t pd_min_established_vbus_mV(const uint32_t negotiatedVoltage_mV)
Lowest VBUS measure for which the negociated voltage is considered established A fixed 1V margin bloc...
Definition: input_power_policy.h:42
static constexpr float pdCurrentRatio
Do not use the whole PD current, or the source will cut us off.
Definition: input_power_policy.h:49
bool is_vbus_from_own_output(const bool isSwitchingToOtg, const bool isChargerInOtg, const bool isOutputGateEnabled)
True when the VBUS measure can come from our own OTG output, and is not a power source The charger OT...
Definition: input_power_policy.h:68
static constexpr uint32_t pdSourceTolerance_percent
PD source output tolerance (vSrcNew), in percents of the negociated voltage.
Definition: input_power_policy.h:32
static constexpr uint32_t maxPdNegociationWait_ms
Definition: input_power_policy.h:28
static constexpr uint32_t vbusMeasureResolution_mV
VBUS measure resolution: the FUSB302 measure has 420mV steps, and rounds down.
Definition: input_power_policy.h:36
bool can_use_power()
Return true if we can use this source as power entry.
Definition: power_delivery.cpp:619
bool is_standard_port()
return true if this voltage source is from a standard non pd port
Definition: power_delivery.cpp:130
Program scope.
Definition: control_fixed_modes.hpp:14
Snapshot of the input source status.
Definition: input_power_policy.h:111
uint32_t timeSinceSourceDetected_ms
Time since the source detection, or since the PD state machine resumed.
Definition: input_power_policy.h:115
bool isPowerSourceDetected
VBUS was detected (deglitched)
Definition: input_power_policy.h:113
bool isUsbPd
Source capabilities are known.
Definition: input_power_policy.h:119
bool isSinkNegotiating
The PD state machine is still negociating (waiting for capabilities, resetting, requesting)
Definition: input_power_policy.h:123
uint32_t vbusVoltage_mV
Measured VBUS voltage.
Definition: input_power_policy.h:128
uint32_t negotiatedVoltage_mV
Negociated contract (0 when no contract)
Definition: input_power_policy.h:125
bool isSinkReady
The PD state machine has an established contract.
Definition: input_power_policy.h:121
uint32_t timeSinceLastPdDetected_ms
Time since the source capabilities were last known.
Definition: input_power_policy.h:117