MustafaMulla29/rp2040-bldc-motor-controller

Hardware assembly metadata and PCB geometry for a controller board with a snap-off rotary-encoder section, plus four parallel 150 µF/50 V bulk electrolytic capacitors filtering the motor supply.

Version
2.0.40
License
unset
Stars
0

Use with the tscircuit CLI

tsci is the tscircuit CLI. Install it with npm install -g tscircuit.

tsci clone MustafaMulla29/rp2040-bldc-motor-controller  # download the package files into ./rp2040-bldc-motor-controller
tsci add MustafaMulla29/rp2040-bldc-motor-controller    # add it to your project as @tsci/MustafaMulla29.rp2040-bldc-motor-controller

In a cloned or new (tsci init) project: tsci dev starts a local preview, tsci build outputs Circuit JSON, and tsci push publishes to tscircuit.com.

firmware/power_policy.py

"""Configurable-PD prototype policy. Units: mV/mA; no hardware side effects.

Input-current limits below are conservative bring-up ceilings, not motor phase
ratings or a validated thermal specification. Calibrate ADCs before loading.
"""
VOLTAGES_MV = (5000, 9000, 15000, 20000)
DEFAULT_MV = 9000
CONTRACT_MA = 3000  # 3 A: highest current a non-e-marked USB-C cable may carry
# Measured at the board, after cable loss. Motor UV margin is checked separately.
# USB-PD sources may sit 5% low; at up to 3 A a typical cable loop adds a further
# 0.5-0.75 V, so 9/15/20 V minimums are 95% of nominal minus 0.75 V. (The former
# 95%-only 19.0 V minimum could stop a healthy 20 V supply under load.)
VBUS_WINDOWS = {5000: (4500, 5250), 9000: (7800, 9450),
                15000: (13500, 15750), 20000: (18250, 21000)}
# Board path (PMOS pair, R13, F1, Q7, hot U11) drops ~0.6 V at 2.3 A and ~0.8 V at 2.7 A,
# inside the 1.0 V allowed here.
MOTOR_MIN_MV = {5000: 4650, 9000: 6800, 15000: 12500, 20000: 17250}
# Motor-feed measurement only: the 3.3 V logic branch bypasses R13.
# knob_runtime uses these limits with commissioned calibration. The separate
# power_manager diagnostic still keeps drive off. Limits need physical testing.
# No resistor-set motor limit: R142 = 680 Ω puts U11's guard at 3.63 A nominal (3.06-4.22 A
# with TI's ±14% and a 2% resistor allowance), above the 3 A contract. These software
# ceilings are the working limit: the 3 A contract minus the logic allowance in
# manufacturing/current-budget.json. Change them here; no board change is needed.
INPUT_LIMIT_MA = {5000: 800, 9000: 2400, 15000: 2600, 20000: 2700}
# U11 signature: while it current-limits, VBUS-VMOTOR jumps well above the normal path
# drop (field log with the old R142 = 2 kΩ: 0.25 V at 1.03 A, then 1.31 V at 1.21 A).
# U11 cannot limit below 3.06 A now; 2.8 A sits between the highest ceiling and that.
SWITCH_LIMIT_MIN_MA = 2800
SWITCH_LIMIT_DROP_MV = 800


def fixed_sink_pdo(millivolts, milliamps, usb_data=False):
    if millivolts not in VOLTAGES_MV or not 100 <= milliamps <= CONTRACT_MA:
        raise ValueError("Supported PD profiles: 5, 9, 15 and 20 V; at most 3 A")
    if millivolts % 50 or milliamps % 10:
        raise ValueError("PDO values must use 50 mV and 10 mA steps")
    return ((1 << 26) if usb_data else 0) | ((millivolts // 50) << 10) | (milliamps // 10)


def adc_millivolts(raw_u16, reference_mv=3300):
    """R11/R30=220k, R12/R31=15k. Nominal transfer; calibrate on hardware."""
    if not 0 <= raw_u16 <= 65535:
        raise ValueError("ADC sample out of range")
    return raw_u16 * reference_mv * 235 // (65535 * 15)


def valid_motor_contract(vbus_mv, rdo, pe_state, pd_ok2_low, selected_mv=DEFAULT_MV):
    """Require explicit selected PDO2, including PDO2=5 V (not fallback PDO1).

    STUSB4500 sets operating current to our request and maximum current to the
    SOURCE capability. A 5 A source is valid for our 3 A request. RDO object
    position indexes source capabilities, not our sink-PDO index.
    """
    if selected_mv not in VOLTAGES_MV:
        return False
    lo, hi = VBUS_WINDOWS[selected_mv]
    return (pd_ok2_low and pe_state == 0x18 and 1 <= ((rdo >> 28) & 7) <= 7
            and (rdo & (1 << 26)) == 0 and (rdo & (1 << 27)) == 0
            and ((rdo >> 10) & 1023) == CONTRACT_MA // 10
            and CONTRACT_MA // 10 <= (rdo & 1023) <= 500
            and lo <= vbus_mv <= hi)


def motor_permitted(vbus_mv, vmotor_mv, input_ma, rdo, pe_state, pd_ok2_low,
                    armed, fault_n, selected_mv=DEFAULT_MV):
    return (valid_motor_contract(vbus_mv, rdo, pe_state, pd_ok2_low, selected_mv)
            and MOTOR_MIN_MV[selected_mv] <= vmotor_mv <= VBUS_WINDOWS[selected_mv][1]
            and 0 <= input_ma < INPUT_LIMIT_MA[selected_mv]
            and armed and fault_n)


def stop_reason(vbus_mv, vmotor_mv, input_ma, rdo, pe_state, pd_ok2_low,
                fault_n, selected_mv=DEFAULT_MV):
    """Name the first failed power check, or None. Mirrors motor_permitted."""
    if not valid_motor_contract(vbus_mv, rdo, pe_state, pd_ok2_low, selected_mv):
        lo, hi = VBUS_WINDOWS.get(selected_mv, (0, 0))
        if pd_ok2_low and pe_state == 0x18 and not lo <= vbus_mv <= hi:
            return "USB-PD input %d mV outside %d-%d mV" % (vbus_mv, lo, hi)
        return "USB-PD contract not valid for %d V / %d A" % (selected_mv // 1000, CONTRACT_MA // 1000)
    if input_ma >= SWITCH_LIMIT_MIN_MA and vbus_mv - vmotor_mv >= SWITCH_LIMIT_DROP_MV:
        return "U11 power-switch current limit reached (input %d mA, drop %d mV)" % (input_ma, vbus_mv - vmotor_mv)
    if not 0 <= input_ma < INPUT_LIMIT_MA[selected_mv]:
        return "motor-feed current %d mA at or above %d mA limit" % (input_ma, INPUT_LIMIT_MA[selected_mv])
    if not MOTOR_MIN_MV[selected_mv] <= vmotor_mv <= VBUS_WINDOWS[selected_mv][1]:
        return "motor supply %d mV outside %d-%d mV" % (vmotor_mv, MOTOR_MIN_MV[selected_mv], VBUS_WINDOWS[selected_mv][1])
    if not fault_n:
        return "motor driver nFAULT asserted"
    return None