From ed40a238e3a5f383ab17784279bba5e91112a46a Mon Sep 17 00:00:00 2001 From: Suke0811 <49264928+Suke0811@users.noreply.github.com> Date: Wed, 3 Apr 2024 13:07:50 -0700 Subject: [PATCH] Add: pwr_in Fix: refactor the port names Add: docstring on the ports Update: mp1/mp2 to be based on the max on the voltage/current in both direction --- electronics_lib/PowerConditioning.py | 76 ++++++++++++------- .../PmosChargerReverseProtection.kicad_sch | 2 +- examples/test_protected_charger.py | 5 +- 3 files changed, 52 insertions(+), 31 deletions(-) diff --git a/electronics_lib/PowerConditioning.py b/electronics_lib/PowerConditioning.py index d82fdb2b2..4eae8c265 100644 --- a/electronics_lib/PowerConditioning.py +++ b/electronics_lib/PowerConditioning.py @@ -1,7 +1,7 @@ from typing import Optional, cast from electronics_abstract_parts import * -from electronics_model.PassivePort import PassiveAdapterVoltageSink +from electronics_model.PassivePort import PassiveAdapterVoltageSink, PassiveAdapterVoltageSource class Supercap(DiscreteComponent, FootprintBlock): # TODO actually model supercaps and parts selection @@ -316,22 +316,36 @@ def contents(self): class PmosChargerReverseProtection(PowerConditioner, KiCadSchematicBlock, Block): - """ - - PMOS transistors are used in charging circuits to prevent reverse voltage from damaging the battery or the circuit. - They act as a switch that allows current flow only in the correct direction. Regular PMOS reverse protection may not be suitable for bidirectional circuit - - Limitation: the highest Vgs/Vds are bounded by the transistors spec. - - There is also a rare case when this circuit being disconnected when a charger is connected first. But always reverse protect. - - More info in https://www.edn.com/reverse-voltage-protection-for-battery-chargers/ - - R1 and R2 are the pullup bias resistors for mp1 and mp2 PFet. - """ + class PmosChargerReverseProtection(PowerConditioner, KiCadSchematicBlock, Block): + """PMOS Charger Reverse Protection. + + PMOS transistors are used in charging circuits to prevent reverse voltage from damaging the battery or the circuit. + They act as a switch that allows current flow only in the correct direction. Regular PMOS reverse protection may not be suitable for bidirectional circuit. + + Ports: + - pwr_in: Power in from the battery + - pwr_out: Power out for the load + - chg_in: Charger input to charge the battery + - chg_out: Charging output for the battery + - gnd: Ground + + Note: + - Limitation: The highest Vgs/Vds are bounded by the transistors' specifications. + - There is also a rare case when this circuit being disconnected when a charger is connected first. But always reverse protect. + - R1 and R2 are the pullup bias resistors for mp1 and mp2 PFet. + + More info at: https://www.edn.com/reverse-voltage-protection-for-battery-chargers/ + """ + @init_in_parent def __init__(self, r1_val: RangeLike = 100*kOhm(tol=0.01), r2_val: RangeLike = 100*kOhm(tol=0.01), rds_on: RangeLike =(0, 0.1)*Ohm): super().__init__() self.gnd = self.Port(Ground.empty(), [Common]) self.pwr_out = self.Port(VoltageSource.empty(),) # Load - self.vbatt = self.Port(VoltageSink.empty(),) # Battery - self.chg_in = self.Port(VoltageSink.empty(), ) # Charger + self.pwr_in = self.Port(VoltageSink.empty(), ) # For connecting battery pwr output + self.chg_in = self.Port(VoltageSink.empty(), ) # Charger input + self.chg_out = self.Port(VoltageSource.empty(),) # Charging output to lipo self.r1_val = self.ArgParameter(r1_val) self.r2_val = self.ArgParameter(r2_val) @@ -342,38 +356,44 @@ def contents(self): self.r1 = self.Block(Resistor(resistance=self.r1_val)) self.r2 = self.Block(Resistor(resistance=self.r2_val)) - max_vcharge_voltage = self.chg_in.link().voltage.upper() - max_vcharge_current = self.chg_in.link().current_drawn.upper() - max_vbatt_voltage = self.vbatt.link().voltage.upper() - max_vbatt_current = self.vbatt.link().current_limits.upper() # TODO: check if we should use current_limit or current_draw + batt_voltage = self.pwr_in.link().voltage.hull(self.chg_out.link().voltage).hull((0, 0)) + + # taking the max of the current for the both direction. 0 lower bound + batt_current = self.pwr_in.link().current_limits.hull(self.chg_out.link().current_drawn).hull((0, 0)) + power = batt_current * batt_current * self.rds_on + r1_current = batt_voltage / self.r1.resistance + # Create the PMOS transistors and resistors based on the provided schematic self.mp1 = self.Block(Fet.PFet( - drain_voltage=(0, max_vcharge_voltage), drain_current=(0, max_vcharge_current), - gate_voltage=(- max_vbatt_voltage, max_vbatt_voltage), + drain_voltage=batt_voltage, drain_current=r1_current, + gate_voltage=(-batt_voltage.upper(), batt_voltage.upper()), rds_on=self.rds_on, - power=(0, max_vcharge_voltage * max_vcharge_current) )) self.mp2 = self.Block(Fet.PFet( - drain_voltage=(0, max_vbatt_voltage), drain_current=(0, max_vbatt_current), - gate_voltage=(0, max_vcharge_voltage), + drain_voltage=batt_voltage, drain_current=batt_current, + gate_voltage=(- batt_voltage.upper(), batt_voltage.upper()), rds_on=self.rds_on, - power=(0, max_vbatt_current * max_vbatt_current * self.rds_on.upper()) + power=power )) + chg_in_adapter = self.Block(PassiveAdapterVoltageSink()) + setattr(self, '(adapter)chg_in', chg_in_adapter) # hack so the netlister recognizes this as an adapter + self.connect(self.mp1.source, chg_in_adapter.src) + self.connect(self.chg_in, chg_in_adapter.dst) - chg_adapter = self.Block(PassiveAdapterVoltageSink()) - setattr(self, '(adapter)chg_in', chg_adapter) # hack so the netlister recognizes this as an adapter - self.connect(self.mp1.source, chg_adapter.src) - self.connect(self.chg_in, chg_adapter.dst) + chg_out_adapter = self.Block(PassiveAdapterVoltageSource()) + setattr(self, '(adapter)chg_out', chg_out_adapter) # hack so the netlister recognizes this as an adapter + self.connect(self.r2.b, chg_out_adapter.src) + self.connect(self.chg_out, chg_out_adapter.dst) self.import_kicad( self.file_path("resources", f"{self.__class__.__name__}.kicad_sch"), conversions={ - 'vbatt': VoltageSink( - current_draw=self.chg_in.link().current_drawn + 'pwr_in': VoltageSink( + current_draw=batt_current ), 'pwr_out': VoltageSource( - voltage_out=self.chg_in.link().voltage), + voltage_out=batt_voltage), 'gnd': Ground(), }) diff --git a/electronics_lib/resources/PmosChargerReverseProtection.kicad_sch b/electronics_lib/resources/PmosChargerReverseProtection.kicad_sch index 6e57b5269..36f933f06 100644 --- a/electronics_lib/resources/PmosChargerReverseProtection.kicad_sch +++ b/electronics_lib/resources/PmosChargerReverseProtection.kicad_sch @@ -1639,7 +1639,7 @@ (effects (font (size 1.27 1.27)) (justify left)) (uuid 7e701471-7566-4ec7-bc5d-01c4b5f88ebf) ) - (hierarchical_label "vbatt" (shape output) (at 187.96 83.82 0) (fields_autoplaced) + (hierarchical_label "pwr_in" (shape output) (at 187.96 83.82 0) (fields_autoplaced) (effects (font (size 1.27 1.27)) (justify left)) (uuid a47934db-1933-4fca-a88e-84db51cda995) ) diff --git a/examples/test_protected_charger.py b/examples/test_protected_charger.py index c0e4db415..89b72956d 100644 --- a/examples/test_protected_charger.py +++ b/examples/test_protected_charger.py @@ -34,14 +34,15 @@ def contents(self) -> None: (self.charger, ), _ = self.chain( self.vusb, imp.Block(Mcp73831(200*mAmp(tol=0.2))), self.pmos.chg_in ) - self.connect(self.pmos.vbatt, self.batt.pwr) + self.connect(self.pmos.pwr_in, self.batt.pwr) + self.connect(self.pmos.chg_out, self.batt.chg) (self.charge_led, ), _ = self.chain( self.Block(IndicatorSinkLed(Led.Yellow)), self.charger.stat ) self.connect(self.vusb, self.charge_led.pwr) - self.pmos_load = imp.Block(PmosReverseProtection(clamp_voltage=(2.4, 4.2)*Volt, gate_resistor=300*Ohm(tol=0.05))) + self.pmos_load = imp.Block(PmosReverseProtection(gate_resistor=300*Ohm(tol=0.05))) self.pwr_pins = self.Block(PassiveConnector(length=3)) self.connect(self.pwr_pins.pins.request('1').adapt_to(Ground()), self.gnd)