|
@@ -0,0 +1,124 @@
|
|
|
|
|
+#!/usr/bin/env python3
|
|
|
|
|
+"""Validate a project.json and emit deterministic compile-time artifacts."""
|
|
|
|
|
+
|
|
|
|
|
+from __future__ import annotations
|
|
|
|
|
+
|
|
|
|
|
+import argparse
|
|
|
|
|
+import json
|
|
|
|
|
+import re
|
|
|
|
|
+from pathlib import Path
|
|
|
|
|
+from typing import Any, Mapping
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+PROFILE = "prod2u-normal-multiproject-115200-v1"
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def _require(condition: bool, message: str) -> None:
|
|
|
|
|
+ if not condition:
|
|
|
|
|
+ raise ValueError(message)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def load_project(path: Path) -> dict[str, Any]:
|
|
|
|
|
+ project_path = Path(path)
|
|
|
|
|
+ payload = json.loads(project_path.read_text(encoding="utf-8"))
|
|
|
|
|
+ _require(isinstance(payload, dict), "project root must be an object")
|
|
|
|
|
+ _require(re.fullmatch(r"[a-z0-9-]+", str(payload.get("project_id", ""))) is not None, "invalid project_id")
|
|
|
|
|
+ _require(payload.get("profile") == PROFILE, "unsupported project profile")
|
|
|
|
|
+ device = payload.get("device")
|
|
|
|
|
+ uart = payload.get("uart")
|
|
|
|
|
+ outputs = payload.get("outputs")
|
|
|
|
|
+ motors = payload.get("motors")
|
|
|
|
|
+ temperature = payload.get("temperature")
|
|
|
|
|
+ protocol = payload.get("protocol")
|
|
|
|
|
+ for name, value in (("device", device), ("uart", uart), ("outputs", outputs), ("motors", motors), ("temperature", temperature), ("protocol", protocol)):
|
|
|
|
|
+ _require(isinstance(value, dict), f"{name} must be an object")
|
|
|
|
|
+ _require(device["part"] == "EF2L45BG256B" and device["clock_hz"] == 25_000_000, "device constants mismatch")
|
|
|
|
|
+ _require(uart == {**uart, "baud": 115200, "data_bits": 8, "parity": "N", "stop_bits": 1, "flow_control": "none", "clks_per_bit": 217}, "UART constants mismatch")
|
|
|
|
|
+ _require(outputs["ptc_count"] == 24 and outputs["fan_count"] == 4 and outputs["pwm_bits"] == 8, "output counts mismatch")
|
|
|
|
|
+ _require(motors["count"] == 2 and motors["fixed_steps"] == 450 and motors["execution"] == "serial", "motor constants mismatch")
|
|
|
|
|
+ _require(motors["order"] == ["inlet", "outlet"] and motors["direction_values"] == [0, 1], "motor order mismatch")
|
|
|
|
|
+ _require(temperature["value_count"] == 27 and temperature["device_addresses"] == [73, 72, 75], "temperature constants mismatch")
|
|
|
|
|
+ _require(temperature["registers"] == list(range(9)), "temperature register order mismatch")
|
|
|
|
|
+ _require(protocol["commands"] == {"set_all": 1, "get_temperature": 2, "temperature_data": 130}, "protocol type mismatch")
|
|
|
|
|
+ _require(protocol["crc"]["polynomial"] == 0x1021 and protocol["crc"]["initial"] == 0xFFFF, "CRC constants mismatch")
|
|
|
|
|
+ _require(len(payload["pins"]["ptc_pwm"]) == 24 and len(payload["pins"]["fan_pwm"]) == 4, "pin count mismatch")
|
|
|
|
|
+ return payload
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def project_summary(path: Path) -> dict[str, Any]:
|
|
|
|
|
+ project = load_project(path)
|
|
|
|
|
+ ptc_count = project["outputs"]["ptc_count"]
|
|
|
|
|
+ fan_count = project["outputs"]["fan_count"]
|
|
|
|
|
+ temperature_count = project["temperature"]["value_count"]
|
|
|
|
|
+ return {
|
|
|
|
|
+ "project_id": project["project_id"],
|
|
|
|
|
+ "profile": project["profile"],
|
|
|
|
|
+ "uart_clks_per_bit": project["uart"]["clks_per_bit"],
|
|
|
|
|
+ "set_all_payload_len": ptc_count + fan_count + 2,
|
|
|
|
|
+ "set_all_frame_len": ptc_count + fan_count + 8,
|
|
|
|
|
+ "temperature_payload_len": temperature_count,
|
|
|
|
|
+ "temperature_frame_len": temperature_count + 6,
|
|
|
|
|
+ "temperature_addresses": tuple(project["temperature"]["device_addresses"]),
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def _write_json(path: Path, payload: Mapping[str, Any]) -> None:
|
|
|
|
|
+ path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
+ path.write_text(json.dumps(dict(payload), indent=2, sort_keys=True) + "\n", encoding="utf-8", newline="\n")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def _render_params(project: Mapping[str, Any]) -> str:
|
|
|
|
|
+ outputs = project["outputs"]
|
|
|
|
|
+ motors = project["motors"]
|
|
|
|
|
+ temperature = project["temperature"]
|
|
|
|
|
+ uart = project["uart"]
|
|
|
|
|
+ return "\n".join([
|
|
|
|
|
+ "// Generated from project.json. Do not edit.",
|
|
|
|
|
+ f"`define P4_CLOCK_HZ {project['device']['clock_hz']}",
|
|
|
|
|
+ f"`define P4_UART_BAUD {uart['baud']}",
|
|
|
|
|
+ f"`define P4_UART_CLKS_PER_BIT {uart['clks_per_bit']}",
|
|
|
|
|
+ f"`define P4_PTC_COUNT {outputs['ptc_count']}",
|
|
|
|
|
+ f"`define P4_FAN_COUNT {outputs['fan_count']}",
|
|
|
|
|
+ f"`define P4_FIXED_MOTOR_STEPS {motors['fixed_steps']}",
|
|
|
|
|
+ f"`define P4_MOTOR_STEP_HALF_CYCLES {motors['step_half_cycles']}",
|
|
|
|
|
+ f"`define P4_MOTOR_DIR_SETUP_CYCLES {motors['dir_setup_cycles']}",
|
|
|
|
|
+ f"`define P4_TEMPERATURE_COUNT {temperature['value_count']}",
|
|
|
|
|
+ "",
|
|
|
|
|
+ ])
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def generate_project(project_path: Path, output_root: Path) -> dict[str, Any]:
|
|
|
|
|
+ project = load_project(project_path)
|
|
|
|
|
+ output = Path(output_root) / "generated"
|
|
|
|
|
+ output.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
+ (output / f"{project['project_id'].replace('-', '_')}_params.vh").write_text(_render_params(project), encoding="ascii", newline="\n")
|
|
|
|
|
+ summary = project_summary(project_path)
|
|
|
|
|
+ contract = {
|
|
|
|
|
+ "schema": "prod2u-simple-host-contract-v1",
|
|
|
|
|
+ "project_id": project["project_id"],
|
|
|
|
|
+ "uart": project["uart"],
|
|
|
|
|
+ "commands": {
|
|
|
|
|
+ "set_all": {"type": "0x01", "payload_length": summary["set_all_payload_len"], "response": "none"},
|
|
|
|
|
+ "get_temperature": {"type": "0x02", "payload_length": 0, "response": "TEMPERATURE_DATA"},
|
|
|
|
|
+ "temperature_data": {"type": "0x82", "payload_length": summary["temperature_payload_len"], "frame_length": summary["temperature_frame_len"]},
|
|
|
|
|
+ },
|
|
|
|
|
+ "crc": project["protocol"]["crc"],
|
|
|
|
|
+ "motors": project["motors"],
|
|
|
|
|
+ "temperature": project["temperature"],
|
|
|
|
|
+ }
|
|
|
|
|
+ _write_json(output / "host-contract.json", contract)
|
|
|
|
|
+ _write_json(output / "project-summary.json", summary)
|
|
|
|
|
+ return summary
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def main() -> int:
|
|
|
|
|
+ parser = argparse.ArgumentParser()
|
|
|
|
|
+ parser.add_argument("project", type=Path)
|
|
|
|
|
+ parser.add_argument("output", type=Path)
|
|
|
|
|
+ args = parser.parse_args()
|
|
|
|
|
+ generate_project(args.project, args.output)
|
|
|
|
|
+ return 0
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == "__main__":
|
|
|
|
|
+ raise SystemExit(main())
|