main
John Lauer
Publish compact v5 electrical checks and six captioned progression review scenes; cooling acceptance remains open
ca514b5
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
#!/usr/bin/env python3
"""Build the output-locked STM32 image from exact pinned dependencies. Never flash."""
import argparse, hashlib, json, os, subprocess
from pathlib import Path
P = Path(__file__).resolve().parents[1]
a = argparse.ArgumentParser(description=__doc__)
a.add_argument('--firmware-root', type=Path, default=P/'firmware', help='Isolated firmware tree inside this project')
a.add_argument('--tinyusb', type=Path, required=True)
a.add_argument('--toolchain-root', type=Path, required=True, help='Relocated Debian package root')
a.add_argument('--vid', type=lambda s:int(s,0), required=True)
a.add_argument('--pid', type=lambda s:int(s,0), required=True)
a.add_argument('--lab-boot-id', required=True, help='Lab session only; no reset freshness guarantee')
a.add_argument('--out', type=Path, default=P/'build/stm32-bringup')
r = a.parse_args()
assert 0 < r.vid <= 65535 and 0 < r.pid <= 65535
assert 8 <= len(r.lab_boot_id) <= 32 and all(c.isascii() and (c.isalnum() or c in '_-') for c in r.lab_boot_id)
F = r.firmware_root.resolve()
assert F.is_relative_to(P), 'Firmware source must stay within the project for provenance'
t = r.tinyusb.resolve(); root = r.toolchain_root.resolve(); out = r.out.resolve(); out.mkdir(parents=True, exist_ok=True)
paths = {'tinyusb':t,'cmsis_device_g0':t/'hw/mcu/st/cmsis_device_g0',
'stm32g0xx_hal_driver':t/'hw/mcu/st/stm32g0xx_hal_driver','CMSIS_5':t/'lib/CMSIS_5'}
pins=json.loads((F/'stm32/dependencies.json').read_text())
for name,path in paths.items():
head=subprocess.check_output(['git','-C',str(path),'rev-parse','HEAD'],text=True).strip()
assert head==pins[name]['commit'], f'{name}: dependency commit mismatch'
assert not subprocess.check_output(['git','-C',str(path),'status','--porcelain','--untracked-files=no'],text=True).strip(), f'{name}: modified tracked files'
cc=root/'usr/bin/arm-none-eabi-gcc'; prefix=root/'usr/bin/arm-none-eabi-'
newlib=root/'usr/lib/arm-none-eabi/newlib/thumb/v6-m/nofp'
h=paths['stm32g0xx_hal_driver']; c=paths['cmsis_device_g0']
sources=[F/s for s in ['load_core.c','adc_conversion.c','ntcle428.c','stm32/adc_input.c','instruments.c','measurement_cache.c','stm32/instrument_input.c','load_protocol.c','fan_tach.c','stm32/fan_input.c','stm32/main.c','stm32/clock.c','stm32/usb_descriptors.c','stm32/syscalls.c']]
sources += [t/'src'/s for s in ['tusb.c','common/tusb_fifo.c','device/usbd.c','class/cdc/cdc_device.c','portable/st/stm32_fsdev/dcd_stm32_fsdev.c','portable/st/stm32_fsdev/fsdev_common.c']]
sources += [h/'Src'/('stm32g0xx_hal'+s+'.c') for s in ['', '_cortex','_pwr','_pwr_ex','_rcc','_rcc_ex','_gpio','_i2c','_i2c_ex','_dma','_adc','_adc_ex']]
sources += [c/'Source/Templates/system_stm32g0xx.c',c/'Source/Templates/gcc/startup_stm32g0b1xx.s']
includes=[F,F/'stm32',t/'src',paths['CMSIS_5']/'CMSIS/Core/Include',c/'Include',h/'Inc',root/'usr/include/newlib']
flags=['-mcpu=cortex-m0plus','-mthumb','-Os','-g3','-ffunction-sections','-fdata-sections','-fno-common','-fstack-usage','-Wall','-Wextra',
'-DSTM32G0B1xx','-DUSE_HAL_DRIVER','-DADOM_BRINGUP_OUTPUT_LOCK=1',f'-DADOM_USB_VID={r.vid}',f'-DADOM_USB_PID={r.pid}',f'-DADOM_LAB_BOOT_ID="{r.lab_boot_id}"']
flags += ['-I'+str(i) for i in includes]
commands=[]
def run(cmd):
commands.append([str(s) for s in cmd]); subprocess.run(cmd,check=True)
objects=[]
for i,src in enumerate(sources):
obj=out/f'{i:02d}-{src.stem}.o'; objects.append(obj)
extra=['-std=c11','-Werror'] if src.is_relative_to(P) else []
run([cc,*flags,*extra,'-c',src,'-o',obj])
elf=out/'adom-load-bringup.elf'
run([cc,'-mcpu=cortex-m0plus','-mthumb','-nostartfiles',*objects,'-T',F/'stm32/stm32g0b1kc.ld',
'-Wl,--gc-sections,--print-memory-usage',f'-Wl,-Map={out}/adom-load-bringup.map','-L'+str(newlib),
'-Wl,--start-group','-lc','-lm','-lnosys','-lgcc','-Wl,--end-group','-o',elf])
for fmt,ext in [('ihex','hex'),('binary','bin')]: run([str(prefix)+'objcopy','-O',fmt,elf,out/f'adom-load-bringup.{ext}'])
run([str(prefix)+'size',elf])
sha=lambda p:hashlib.sha256(p.read_bytes()).hexdigest()
manifest={'status':'compiled-only-output-locked','flashed':False,'physical_usb_verified':False,'load_enable_supported':False,
'production_boot_freshness':False,'lab_boot_id':r.lab_boot_id,'vid':r.vid,'pid':r.pid,
'toolchain':subprocess.check_output([cc,'--version'],text=True).splitlines()[0], 'dependencies':pins,
'sources':{str(p.relative_to(P)):sha(p) for p in sorted(set([s for s in sources if s.is_relative_to(P)] + list(F.glob('*.h')) + [s for s in (F/'stm32').rglob('*') if s.is_file()] + [Path(__file__).resolve()]))},
'artifacts':{p.name:sha(p) for p in out.glob('adom-load-bringup.*')},'commands':commands}
(out/'build-manifest.json').write_text(json.dumps(manifest,indent=2)+'\n')
print('COMPILED ONLY: output permanently locked OFF; no flash or hardware validation performed.')