ShiboSoftwareDev/am69-pi5

A 85×56 mm 10-layer AM69A single-board computer carrier integrates 8 GiB LPDDR4, 32 GB eMMC, Pi-style expansion and multimedia/USB/Ethernet connectors, multi-rail PMIC power, STM32 boot/power control, USB-C PD, SD/fan protection, and HDI-controlled high-speed routing.

Version
0.0.8-engineering-draft-20261107-dqs19
License
unset
Stars
0

Use with the tscircuit CLI

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

tsci clone ShiboSoftwareDev/am69-pi5  # download the package files into ./am69-pi5
tsci add ShiboSoftwareDev/am69-pi5    # add it to your project as @tsci/ShiboSoftwareDev.am69-pi5

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/usb-pd/pd-nvm.py

#!/usr/bin/env python3
"""STUSB4500 single-5V/5A production NVM patch, preserving device calibration.
Byte fields cross-checked against SparkFun's MIT STUSB4500 library (Alex Wende,
2020; licenses/SPARKFUN-LICENSE.md). FTP sequence: ST public NVM API at
usb-c/STUSB4500 commit55d1109e7ef0cffc2ffef2494095d23306594601.
Use a fixed5V fixture, hold EC NRST low, and connect open-drain3.3V I2C on
TP1600/TP1601. Never use a PD-capable source during initial programming.
"""
import argparse,hashlib,json,time
from pathlib import Path

def decode(data):
    if len(data)!=40:raise ValueError('Expected five8-byte customer NVM sectors')
    b=data[26];code=b>>4
    current=0 if code==0 else code*250+250 if code<11 else code*500-2500
    return {'pdo_count':(b>>1)&3,'pdo1_mv':5000,'pdo1_ma':current,
            'usb_communications':bool(b&1),'externally_powered':bool(b&8),
            'above_5v_only':bool(data[38]&8),'request_source_current':bool(data[38]&16)}
def patch(data):
    decode(data);out=bytearray(data)
    out[26]=0xf3 # Current code15=5A; PDO count1; USB data yes; unconstrained power no.
    out[38]&=~0x18 # Permit5V; request exactly configured current, not arbitrary source maximum.
    assert decode(out)=={'pdo_count':1,'pdo1_mv':5000,'pdo1_ma':5000,'usb_communications':True,'externally_powered':False,'above_5v_only':False,'request_source_current':False}
    return bytes(out)
class Device:
    def __init__(self,bus):
        from smbus2 import SMBus,i2c_msg
        self.bus=SMBus(bus);self.msg=i2c_msg;self.address=0x28
    def write(self,reg,data):
        if isinstance(data,int):data=bytes([data])
        self.bus.i2c_rdwr(self.msg.write(self.address,bytes([reg])+bytes(data)))
    def read(self,reg,count=1):
        request=self.msg.read(self.address,count)
        self.bus.i2c_rdwr(self.msg.write(self.address,[reg]),request)
        return bytes(request)
    def request(self,ctrl):
        self.write(0x9e,ctrl);deadline=time.monotonic()+1
        while self.read(0x9e)[0]&0x10:
            if time.monotonic()>deadline:raise TimeoutError('STUSB4500 FTP request did not complete')
            time.sleep(.001)
    def exit(self):
        self.write(0x9e,bytes([0x40,0]));self.write(0x95,0)
    def dump(self):
        self.write(0x95,0x47);out=bytearray()
        try:
            for sector in range(5):
                self.write(0x9e,0);self.write(0x9e,0xc0);self.write(0x9f,0)
                self.request(0xd0|sector);out+=self.read(0x53,8);self.write(0x9e,0)
        finally:self.exit()
        return bytes(out)
    def program(self,target):
        # Erase only sectors3 and4. Leave sectors0..2 calibration/timing untouched.
        self.write(0x95,0x47)
        try:
            self.write(0x53,0);self.write(0x9e,0);self.write(0x9e,0xc0)
            self.write(0x9f,((0x18<<3)&0xf8)|2);self.request(0xd0)
            self.write(0x9f,7);self.request(0xd0)
            self.write(0x9f,5);self.request(0xd0)
            for sector in [3,4]:
                self.write(0x53,target[sector*8:sector*8+8]);self.write(0x9e,0xc0)
                self.write(0x9f,1);self.request(0xd0)
                self.write(0x9f,6);self.request(0xd0|sector)
        finally:self.exit()

def digest(data):return hashlib.sha256(data).hexdigest()
def main():
    parser=argparse.ArgumentParser(description=__doc__)
    sub=parser.add_subparsers(dest='action',required=True)
    p=sub.add_parser('plan');p.add_argument('original');p.add_argument('target')
    p=sub.add_parser('verify');p.add_argument('original');p.add_argument('readback')
    p=sub.add_parser('program');p.add_argument('--bus',type=int,required=True);p.add_argument('--backup',required=True)
    p.add_argument('--fixed-5v-fixture',action='store_true',required=True)
    p=sub.add_parser('verify-live');p.add_argument('--bus',type=int,required=True)
    args=parser.parse_args()
    if args.action=='plan':
        original=Path(args.original).read_bytes();target=patch(original);Path(args.target).write_bytes(target)
        print(json.dumps({'original_sha256':digest(original),'target_sha256':digest(target),'profile':decode(target),'changed_offsets':[i for i,(a,b)in enumerate(zip(original,target))if a!=b]},indent=2));return
    if args.action=='verify':
        expected=patch(Path(args.original).read_bytes());actual=Path(args.readback).read_bytes()
        if actual!=expected:raise ValueError('NVM readback differs from the calibrated target')
        print(json.dumps({'status':'PASS_NVM_READBACK','sha256':digest(actual),'profile':decode(actual)},indent=2));return
    dev=Device(args.bus)
    if args.action=='program':
        backup=Path(args.backup)
        if backup.exists():raise FileExistsError('Refusing to overwrite the unique per-device calibration backup')
        time.sleep(.1);original=dev.dump();backup.write_bytes(original);target=patch(original)
        backup.with_suffix(backup.suffix+'.target').write_bytes(target)
        if target!=original:dev.program(target)
        if dev.dump()!=target:raise ValueError('NVM write/readback mismatch; reject device')
        print('PASS_NVM_READBACK: remove fixture power, release EC reset, power-cycle, then run verify-live')
    else:
        count=dev.read(0x70)[0]&7;pdo=int.from_bytes(dev.read(0x85,4),'little')
        if count!=1 or pdo>>30 or (pdo>>10)&1023!=100 or pdo&1023!=500:raise ValueError('Post-reset sink profile is not fixed5V/5A only')
        print(json.dumps({'status':'PASS_POST_RESET_PROFILE','pdo_count':count,'pdo1':hex(pdo)},indent=2))
if __name__=='__main__':main()