|
|
@@ -1,5 +1,5 @@
|
|
|
#!/usr/bin/env python3
|
|
|
-"""Validate a project.json and emit deterministic compile-time artifacts."""
|
|
|
+"""Validate a project configuration and emit deterministic contract artifacts."""
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
@@ -33,80 +33,407 @@ ACTIVE_SOURCE_PATHS = (
|
|
|
"src/top/prod2u_p4_normal_v3_top.v",
|
|
|
)
|
|
|
|
|
|
+EXPECTED_CHANNEL_METADATA = {
|
|
|
+ "ptc": (
|
|
|
+ ("HEAT2", "G16", "CON18", 3, "active_high"),
|
|
|
+ ("HEAT1", "F16", "CON18", 1, "active_high"),
|
|
|
+ ("HEAT8", "N1", "CON27", 3, "active_high"),
|
|
|
+ ("HEAT7", "M1", "CON27", 1, "active_high"),
|
|
|
+ ("HEAT14", "R16", "CON28", 3, "active_high"),
|
|
|
+ ("HEAT13", "P16", "CON28", 1, "active_high"),
|
|
|
+ ("HEAT20", "P9", "CON29", 3, "active_high"),
|
|
|
+ ("HEAT19", "T9", "CON29", 1, "active_high"),
|
|
|
+ ),
|
|
|
+ "fan": (
|
|
|
+ ("FAN_PWM3", "B1", "CON25", 7, "active_low"),
|
|
|
+ ("FAN_PWM1", "C4", "CON8", 7, "active_low"),
|
|
|
+ ("FAN_PWM2", "B5", "CON24", 7, "active_low"),
|
|
|
+ ("FAN_PWM4", "A14", "CON26", 7, "active_low"),
|
|
|
+ ),
|
|
|
+}
|
|
|
+
|
|
|
+_TOP_LEVEL_KEYS = {
|
|
|
+ "project_id",
|
|
|
+ "profile",
|
|
|
+ "display_name",
|
|
|
+ "device",
|
|
|
+ "uart",
|
|
|
+ "outputs",
|
|
|
+ "motors",
|
|
|
+ "limits",
|
|
|
+ "temperature",
|
|
|
+ "pins",
|
|
|
+ "resources",
|
|
|
+ "protocol",
|
|
|
+ "timeouts",
|
|
|
+ "watchdog",
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
def _require(condition: bool, message: str) -> None:
|
|
|
if not condition:
|
|
|
raise ValueError(message)
|
|
|
|
|
|
|
|
|
+def _object(value: Any, name: str) -> Mapping[str, Any]:
|
|
|
+ _require(isinstance(value, dict), f"{name} must be an object")
|
|
|
+ return value
|
|
|
+
|
|
|
+
|
|
|
+def _keys(value: Mapping[str, Any], expected: set[str], name: str) -> None:
|
|
|
+ _require(set(value) == expected, f"{name} keys mismatch")
|
|
|
+
|
|
|
+
|
|
|
+def _integer(value: Any, name: str, *, minimum: int | None = None) -> None:
|
|
|
+ _require(isinstance(value, int) and not isinstance(value, bool), f"{name} must be an integer")
|
|
|
+ if minimum is not None:
|
|
|
+ _require(value >= minimum, f"{name} is below the minimum")
|
|
|
+
|
|
|
+
|
|
|
+def _number(value: Any, name: str) -> None:
|
|
|
+ _require(isinstance(value, (int, float)) and not isinstance(value, bool), f"{name} must be numeric")
|
|
|
+
|
|
|
+
|
|
|
+def _list(value: Any, name: str, length: int | None = None) -> list[Any]:
|
|
|
+ _require(isinstance(value, list), f"{name} must be an array")
|
|
|
+ if length is not None:
|
|
|
+ _require(len(value) == length, f"{name} must contain {length} items")
|
|
|
+ return value
|
|
|
+
|
|
|
+
|
|
|
+def _validate_channel(channel: Any, index: int, kind: str) -> None:
|
|
|
+ item = _object(channel, f"{kind}_channels[{index}]")
|
|
|
+ _keys(item, {"index", "name", "signal", "board_signal", "ball", "connector", "connector_pin", "polarity"}, f"{kind}_channels[{index}]")
|
|
|
+ _require(item["index"] == index, f"{kind} channel index mismatch at {index}")
|
|
|
+ _require(item["name"] == f"{kind}{index + 1}", f"{kind} channel name mismatch at {index}")
|
|
|
+ _require(item["signal"] == f"{kind}_pwm_out[{index}]", f"{kind} channel signal mismatch at {index}")
|
|
|
+ _require(isinstance(item["board_signal"], str) and item["board_signal"], f"invalid {kind} board signal at {index}")
|
|
|
+ _require(item["polarity"] in {"active_high", "active_low"}, f"invalid {kind} polarity at {index}")
|
|
|
+ _require(re.fullmatch(r"[A-Z][0-9]+", str(item["ball"])) is not None, f"invalid {kind} ball at {index}")
|
|
|
+ _require(re.fullmatch(r"CON[0-9]+", str(item["connector"])) is not None, f"invalid {kind} connector at {index}")
|
|
|
+ _integer(item["connector_pin"], f"{kind} channel connector pin", minimum=1)
|
|
|
+ expected = EXPECTED_CHANNEL_METADATA[kind][index]
|
|
|
+ _require(
|
|
|
+ (item["board_signal"], item["ball"], item["connector"], item["connector_pin"], item["polarity"]) == expected,
|
|
|
+ f"{kind} channel metadata mismatch at {index}",
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+def _validate_project(payload: Mapping[str, Any]) -> None:
|
|
|
+ _keys(payload, _TOP_LEVEL_KEYS, "project")
|
|
|
+ _require(re.fullmatch(r"[a-z0-9-]+", str(payload["project_id"])) is not None, "invalid project_id")
|
|
|
+ _require(payload["profile"] == PROFILE, "unsupported project profile")
|
|
|
+ _require(isinstance(payload["display_name"], str) and payload["display_name"], "invalid display_name")
|
|
|
+
|
|
|
+ device = _object(payload["device"], "device")
|
|
|
+ _keys(device, {"family", "part", "clock_hz", "clock_ball"}, "device")
|
|
|
+ _require(device["family"] == "EF2" and device["part"] == "EF2L45BG256B", "device identity mismatch")
|
|
|
+ _require(device["clock_hz"] == 25_000_000 and device["clock_ball"] == "J1", "clock contract mismatch")
|
|
|
+
|
|
|
+ uart = _object(payload["uart"], "uart")
|
|
|
+ _keys(uart, {"baud", "data_bits", "parity", "stop_bits", "flow_control", "clks_per_bit", "inter_byte_timeout_ms", "rx_ball", "tx_ball"}, "uart")
|
|
|
+ _require(
|
|
|
+ uart["baud"] == 115200
|
|
|
+ and uart["data_bits"] == 8
|
|
|
+ and uart["parity"] == "N"
|
|
|
+ and uart["stop_bits"] == 1
|
|
|
+ and uart["flow_control"] == "none"
|
|
|
+ and uart["clks_per_bit"] == 217,
|
|
|
+ "UART contract mismatch",
|
|
|
+ )
|
|
|
+ _require(uart["rx_ball"] == "N11" and uart["tx_ball"] == "M10", "UART pin mapping mismatch")
|
|
|
+
|
|
|
+ outputs = _object(payload["outputs"], "outputs")
|
|
|
+ _keys(
|
|
|
+ outputs,
|
|
|
+ {
|
|
|
+ "ptc_count",
|
|
|
+ "fan_count",
|
|
|
+ "ptc_channels",
|
|
|
+ "fan_channels",
|
|
|
+ "pwm_bits",
|
|
|
+ "pwm_internal_width",
|
|
|
+ "pwm_max_duty",
|
|
|
+ "pwm_phase_count",
|
|
|
+ "pwm_slot_clks",
|
|
|
+ "pwm_frequency_hz",
|
|
|
+ "pwm_actual_frequency_hz",
|
|
|
+ "ptc_polarity",
|
|
|
+ "fan_polarity",
|
|
|
+ "ptc_default",
|
|
|
+ "fan_default",
|
|
|
+ "fan_connector_active_level",
|
|
|
+ "fan_board_inverts",
|
|
|
+ "boot_targets",
|
|
|
+ },
|
|
|
+ "outputs",
|
|
|
+ )
|
|
|
+ _require(outputs["ptc_count"] == len(outputs["ptc_channels"]) == 8, "PTC count/list mismatch")
|
|
|
+ _require(outputs["fan_count"] == len(outputs["fan_channels"]) == 4, "FAN count/list mismatch")
|
|
|
+ for index, channel in enumerate(outputs["ptc_channels"]):
|
|
|
+ _validate_channel(channel, index, "ptc")
|
|
|
+ for index, channel in enumerate(outputs["fan_channels"]):
|
|
|
+ _validate_channel(channel, index, "fan")
|
|
|
+ _require(outputs["pwm_bits"] == 8, "PWM protocol width mismatch")
|
|
|
+ _require(outputs["pwm_internal_width"] == 7, "PWM internal width mismatch")
|
|
|
+ _require(outputs["pwm_max_duty"] == 100, "PWM maximum duty mismatch")
|
|
|
+ _require(outputs["pwm_phase_count"] == 100 and outputs["pwm_slot_clks"] == 1042, "PWM phase contract mismatch")
|
|
|
+ _require(outputs["pwm_frequency_hz"] == 20, "PWM target frequency mismatch")
|
|
|
+ _require(abs(outputs["pwm_actual_frequency_hz"] - 19.993602047) < 1e-12, "PWM actual frequency mismatch")
|
|
|
+ _require(outputs["ptc_polarity"] == "active_high" and outputs["fan_polarity"] == "active_low", "PWM polarity mismatch")
|
|
|
+ _require(outputs["ptc_default"] == 0 and outputs["fan_default"] == 10, "PWM boot target mismatch")
|
|
|
+ _require(outputs["fan_connector_active_level"] == "high" and outputs["fan_board_inverts"] is True, "FAN board metadata mismatch")
|
|
|
+ boot_targets = _object(outputs["boot_targets"], "outputs.boot_targets")
|
|
|
+ _keys(boot_targets, {"ptc", "fan"}, "outputs.boot_targets")
|
|
|
+ _require(boot_targets == {"ptc": 0, "fan": 10}, "boot target mapping mismatch")
|
|
|
+
|
|
|
+ motors = _object(payload["motors"], "motors")
|
|
|
+ _keys(
|
|
|
+ motors,
|
|
|
+ {
|
|
|
+ "count",
|
|
|
+ "order",
|
|
|
+ "role",
|
|
|
+ "fixed_steps",
|
|
|
+ "execution",
|
|
|
+ "busy_wait_ms",
|
|
|
+ "step_rate_hz",
|
|
|
+ "step_half_cycles",
|
|
|
+ "dir_setup_cycles",
|
|
|
+ "command_values",
|
|
|
+ "heartbeat_value",
|
|
|
+ "mapping",
|
|
|
+ "enable_n",
|
|
|
+ },
|
|
|
+ "motors",
|
|
|
+ )
|
|
|
+ _require(motors["count"] == 2 and motors["order"] == ["x", "y"] and motors["role"] == "exhaust", "motor identity mismatch")
|
|
|
+ _require(motors["execution"] == "overlap" and motors["busy_wait_ms"] >= 600, "motor overlap/busy contract mismatch")
|
|
|
+ _require(motors["fixed_steps"] == 450 and motors["step_rate_hz"] == 1000, "motor speed/step mismatch")
|
|
|
+ _require(motors["step_half_cycles"] == 12_500 and motors["dir_setup_cycles"] == 25_000, "motor timing mismatch")
|
|
|
+ _require(motors["command_values"] == [0, 1, 2] and motors["heartbeat_value"] == 0, "motor command mismatch")
|
|
|
+ mapping = _object(motors["mapping"], "motors.mapping")
|
|
|
+ _keys(mapping, {"x", "y"}, "motors.mapping")
|
|
|
+ for axis in ("x", "y"):
|
|
|
+ item = _object(mapping[axis], f"motors.mapping.{axis}")
|
|
|
+ _keys(item, {"axis", "role", "step_ball", "dir_ball"}, f"motors.mapping.{axis}")
|
|
|
+ _require(item["axis"] == axis and item["role"] == "exhaust", f"motor {axis} role mismatch")
|
|
|
+ enable = _object(motors["enable_n"], "motors.enable_n")
|
|
|
+ _keys(enable, {"ball", "active_level", "policy"}, "motors.enable_n")
|
|
|
+ _require(enable == {"ball": "A10", "active_level": "low", "policy": "shared_constant_low"}, "motor enable mapping mismatch")
|
|
|
+
|
|
|
+ limits = _object(payload["limits"], "limits")
|
|
|
+ _keys(limits, {"limit_count", "active_level", "functional_use", "channels"}, "limits")
|
|
|
+ _require(limits["limit_count"] == len(limits["channels"]) == 2, "limit count/list mismatch")
|
|
|
+ _require(limits["active_level"] == "high" and limits["functional_use"] is False, "limit policy mismatch")
|
|
|
+ for index, item in enumerate(limits["channels"]):
|
|
|
+ entry = _object(item, f"limits.channels[{index}]")
|
|
|
+ _keys(entry, {"name", "ball"}, f"limits.channels[{index}]")
|
|
|
+ _require(entry["name"] in {"x_limit_raw", "y_limit_raw"}, "invalid limit name")
|
|
|
+
|
|
|
+ temperature = _object(payload["temperature"], "temperature")
|
|
|
+ _keys(
|
|
|
+ temperature,
|
|
|
+ {
|
|
|
+ "value_count",
|
|
|
+ "device_addresses",
|
|
|
+ "registers",
|
|
|
+ "scl_ball",
|
|
|
+ "sda_ball",
|
|
|
+ "bus",
|
|
|
+ "device",
|
|
|
+ "invalid_value",
|
|
|
+ "maximum_celsius",
|
|
|
+ "conversion",
|
|
|
+ },
|
|
|
+ "temperature",
|
|
|
+ )
|
|
|
+ _require(temperature["value_count"] == 27, "temperature count mismatch")
|
|
|
+ _require(temperature["device_addresses"] == [73, 72, 75] and temperature["registers"] == list(range(9)), "temperature mapping mismatch")
|
|
|
+ _require(temperature["scl_ball"] == "M8" and temperature["sda_ball"] == "N9" and temperature["device"] == "CT7428", "I2C mapping mismatch")
|
|
|
+ conversion = _object(temperature["conversion"], "temperature.conversion")
|
|
|
+ _keys(conversion, {"profile", "raw_shift", "raw_width", "reject_sign_bit"}, "temperature.conversion")
|
|
|
+ _require(conversion == {"profile": "ct7428-msb-integer", "raw_shift": 7, "raw_width": 9, "reject_sign_bit": True}, "temperature conversion mismatch")
|
|
|
+
|
|
|
+ pins = _object(payload["pins"], "pins")
|
|
|
+ _keys(
|
|
|
+ pins,
|
|
|
+ {
|
|
|
+ "host_uart_rx",
|
|
|
+ "host_uart_tx",
|
|
|
+ "temp_scl",
|
|
|
+ "temp_sda",
|
|
|
+ "ptc_pwm",
|
|
|
+ "fan_pwm",
|
|
|
+ "motor_en_n",
|
|
|
+ "x_step",
|
|
|
+ "x_dir",
|
|
|
+ "y_step",
|
|
|
+ "y_dir",
|
|
|
+ "x_limit_raw",
|
|
|
+ "y_limit_raw",
|
|
|
+ },
|
|
|
+ "pins",
|
|
|
+ )
|
|
|
+ _require(pins["host_uart_rx"] == uart["rx_ball"] and pins["host_uart_tx"] == uart["tx_ball"], "UART pin duplication mismatch")
|
|
|
+ _require(pins["temp_scl"] == temperature["scl_ball"] and pins["temp_sda"] == temperature["sda_ball"], "I2C pin duplication mismatch")
|
|
|
+ _require(pins["ptc_pwm"] == [item["ball"] for item in outputs["ptc_channels"]], "PTC pin/list mismatch")
|
|
|
+ _require(
|
|
|
+ len(pins["fan_pwm"]) == outputs["fan_count"]
|
|
|
+ and set(pins["fan_pwm"]) == {item["ball"] for item in outputs["fan_channels"]},
|
|
|
+ "FAN pin/list mismatch",
|
|
|
+ )
|
|
|
+ _require(pins["motor_en_n"] == enable["ball"], "motor enable pin mismatch")
|
|
|
+ _require(pins["x_step"] == mapping["x"]["step_ball"] and pins["x_dir"] == mapping["x"]["dir_ball"], "X motor pin mismatch")
|
|
|
+ _require(pins["y_step"] == mapping["y"]["step_ball"] and pins["y_dir"] == mapping["y"]["dir_ball"], "Y motor pin mismatch")
|
|
|
+ _require([pins["x_limit_raw"], pins["y_limit_raw"]] == [item["ball"] for item in limits["channels"]], "limit pin mismatch")
|
|
|
+
|
|
|
+ resources = _object(payload["resources"], "resources")
|
|
|
+ _keys(
|
|
|
+ resources,
|
|
|
+ {
|
|
|
+ "target_ram_blocks",
|
|
|
+ "request_ram_blocks",
|
|
|
+ "temperature_ram_blocks",
|
|
|
+ "dsp",
|
|
|
+ "shared_pwm_comparators",
|
|
|
+ "shared_motor_executors",
|
|
|
+ "shared_i2c_readers",
|
|
|
+ },
|
|
|
+ "resources",
|
|
|
+ )
|
|
|
+ _require(resources["shared_pwm_comparators"] == 1 and resources["shared_motor_executors"] == 1 and resources["shared_i2c_readers"] == 1, "shared resource contract mismatch")
|
|
|
+
|
|
|
+ protocol = _object(payload["protocol"], "protocol")
|
|
|
+ _keys(protocol, {"header_hex", "crc", "commands", "set_all", "temperature_data"}, "protocol")
|
|
|
+ _require(protocol["header_hex"] == "A55A", "frame header mismatch")
|
|
|
+ crc = _object(protocol["crc"], "protocol.crc")
|
|
|
+ _keys(crc, {"name", "polynomial", "initial", "reflect_input", "reflect_output", "xor_output", "coverage", "wire_order"}, "protocol.crc")
|
|
|
+ _require(
|
|
|
+ crc
|
|
|
+ == {
|
|
|
+ "name": "CRC-16/CCITT-FALSE",
|
|
|
+ "polynomial": 0x1021,
|
|
|
+ "initial": 0xFFFF,
|
|
|
+ "reflect_input": False,
|
|
|
+ "reflect_output": False,
|
|
|
+ "xor_output": 0,
|
|
|
+ "coverage": "type_length_payload",
|
|
|
+ "wire_order": "little",
|
|
|
+ },
|
|
|
+ "CRC contract mismatch",
|
|
|
+ )
|
|
|
+ _require(protocol["commands"] == {"set_all": 1, "get_temperature": 2, "temperature_data": 130}, "protocol type mismatch")
|
|
|
+ set_all = _object(protocol["set_all"], "protocol.set_all")
|
|
|
+ _keys(set_all, {"payload_length", "ptc_offset", "fan_offset", "motor_x_offset", "motor_y_offset", "duty_min", "duty_max", "motor_values", "heartbeat", "response"}, "protocol.set_all")
|
|
|
+ _require(
|
|
|
+ set_all
|
|
|
+ == {
|
|
|
+ "payload_length": 14,
|
|
|
+ "ptc_offset": 0,
|
|
|
+ "fan_offset": 8,
|
|
|
+ "motor_x_offset": 12,
|
|
|
+ "motor_y_offset": 13,
|
|
|
+ "duty_min": 0,
|
|
|
+ "duty_max": 100,
|
|
|
+ "motor_values": [0, 1, 2],
|
|
|
+ "heartbeat": [0, 0],
|
|
|
+ "response": "none",
|
|
|
+ },
|
|
|
+ "SET_ALL contract mismatch",
|
|
|
+ )
|
|
|
+ temperature_data = _object(protocol["temperature_data"], "protocol.temperature_data")
|
|
|
+ _keys(temperature_data, {"payload_length", "frame_length"}, "protocol.temperature_data")
|
|
|
+ _require(temperature_data == {"payload_length": 27, "frame_length": 33}, "temperature frame contract mismatch")
|
|
|
+
|
|
|
+ timeouts = _object(payload["timeouts"], "timeouts")
|
|
|
+ _keys(timeouts, {"frame_inter_byte_ms", "host_get_response_ms"}, "timeouts")
|
|
|
+ _require(timeouts["frame_inter_byte_ms"] == 10, "frame timeout mismatch")
|
|
|
+
|
|
|
+ watchdog = _object(payload["watchdog"], "watchdog")
|
|
|
+ _keys(watchdog, {"timeout_seconds", "tick_us", "tick_clks", "timeout_ticks", "target"}, "watchdog")
|
|
|
+ _require(watchdog["timeout_seconds"] == 10 and watchdog["tick_us"] == 500, "watchdog duration mismatch")
|
|
|
+ _require(watchdog["tick_clks"] == device["clock_hz"] * watchdog["tick_us"] // 1_000_000, "watchdog tick clock mismatch")
|
|
|
+ _require(watchdog["timeout_ticks"] == watchdog["timeout_seconds"] * 1_000_000 // watchdog["tick_us"], "watchdog tick count mismatch")
|
|
|
+ target = _object(watchdog["target"], "watchdog.target")
|
|
|
+ _keys(target, {"ptc", "fan"}, "watchdog.target")
|
|
|
+ _require(target == {"ptc": 0, "fan": 20}, "watchdog target mismatch")
|
|
|
+
|
|
|
+
|
|
|
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(outputs["pwm_frequency_hz"] == 20, "PWM frequency mismatch")
|
|
|
- _require(outputs["ptc_polarity"] == "active_high" and outputs["fan_polarity"] == "active_low", "PWM polarity mismatch")
|
|
|
- _require(outputs["ptc_default"] == 0 and outputs["fan_default"] == 26, "PWM default mismatch")
|
|
|
- _require(outputs["fan_connector_active_level"] == "high" and outputs["fan_board_inverts"] is True, "FAN board polarity metadata 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")
|
|
|
+ _validate_project(payload)
|
|
|
return payload
|
|
|
|
|
|
|
|
|
+def _active_interface(project: Mapping[str, Any]) -> tuple[tuple[str, str], ...]:
|
|
|
+ outputs = project["outputs"]
|
|
|
+ pins = project["pins"]
|
|
|
+ motors = project["motors"]
|
|
|
+ limits = project["limits"]
|
|
|
+ return (
|
|
|
+ ("sys_clk", project["device"]["clock_ball"]),
|
|
|
+ ("host_uart_rx", pins["host_uart_rx"]),
|
|
|
+ ("host_uart_tx", pins["host_uart_tx"]),
|
|
|
+ ("temp_scl", pins["temp_scl"]),
|
|
|
+ ("temp_sda", pins["temp_sda"]),
|
|
|
+ *tuple((item["signal"], item["ball"]) for item in outputs["ptc_channels"]),
|
|
|
+ *tuple((item["signal"], item["ball"]) for item in outputs["fan_channels"]),
|
|
|
+ ("motor_en_n", pins["motor_en_n"]),
|
|
|
+ ("x_step", motors["mapping"]["x"]["step_ball"]),
|
|
|
+ ("x_dir", motors["mapping"]["x"]["dir_ball"]),
|
|
|
+ ("y_step", motors["mapping"]["y"]["step_ball"]),
|
|
|
+ ("y_dir", motors["mapping"]["y"]["dir_ball"]),
|
|
|
+ *tuple((item["name"], item["ball"]) for item in limits["channels"]),
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+def render_adc(project: Mapping[str, Any]) -> tuple[tuple[str, str], ...]:
|
|
|
+ """Return the ordered active interface as (signal, ball) pairs."""
|
|
|
+ return _active_interface(project)
|
|
|
+
|
|
|
+
|
|
|
def project_summary(path: Path) -> dict[str, Any]:
|
|
|
project = load_project(path)
|
|
|
- ptc_count = project["outputs"]["ptc_count"]
|
|
|
- fan_count = project["outputs"]["fan_count"]
|
|
|
- pwm_bits = project["outputs"]["pwm_bits"]
|
|
|
- target_frequency_hz = project["outputs"]["pwm_frequency_hz"]
|
|
|
- temperature_count = project["temperature"]["value_count"]
|
|
|
- total_channels = ptc_count + fan_count
|
|
|
- phase_count = 1 << pwm_bits
|
|
|
- ideal_slot = project["device"]["clock_hz"] / (target_frequency_hz * total_channels * phase_count)
|
|
|
- pwm_slot_clks = int(round(ideal_slot))
|
|
|
- pwm_period_clks = pwm_slot_clks * total_channels * phase_count
|
|
|
+ outputs = project["outputs"]
|
|
|
+ temperature = project["temperature"]
|
|
|
+ watchdog = project["watchdog"]
|
|
|
+ total_channels = outputs["ptc_count"] + outputs["fan_count"]
|
|
|
+ pwm_period_clks = outputs["pwm_slot_clks"] * total_channels * outputs["pwm_phase_count"]
|
|
|
actual_frequency_hz = project["device"]["clock_hz"] / pwm_period_clks
|
|
|
- frequency_error_percent = abs(actual_frequency_hz - target_frequency_hz) / target_frequency_hz * 100.0
|
|
|
+ frequency_error_percent = abs(actual_frequency_hz - outputs["pwm_frequency_hz"]) / outputs["pwm_frequency_hz"] * 100.0
|
|
|
return {
|
|
|
"project_id": project["project_id"],
|
|
|
"profile": project["profile"],
|
|
|
"uart_clks_per_bit": project["uart"]["clks_per_bit"],
|
|
|
- "pwm_bits": pwm_bits,
|
|
|
- "pwm_phase_count": phase_count,
|
|
|
- "pwm_frequency_hz": target_frequency_hz,
|
|
|
- "pwm_slot_clks": pwm_slot_clks,
|
|
|
+ "pwm_bits": outputs["pwm_bits"],
|
|
|
+ "pwm_internal_width": outputs["pwm_internal_width"],
|
|
|
+ "pwm_max_duty": outputs["pwm_max_duty"],
|
|
|
+ "pwm_phase_count": outputs["pwm_phase_count"],
|
|
|
+ "pwm_frequency_hz": outputs["pwm_frequency_hz"],
|
|
|
+ "pwm_slot_clks": outputs["pwm_slot_clks"],
|
|
|
"pwm_period_clks": pwm_period_clks,
|
|
|
"pwm_period_seconds": round(pwm_period_clks / project["device"]["clock_hz"], 9),
|
|
|
"actual_frequency_hz": round(actual_frequency_hz, 9),
|
|
|
"frequency_error_percent": round(frequency_error_percent, 9),
|
|
|
- "ptc_default": project["outputs"]["ptc_default"],
|
|
|
- "fan_default": project["outputs"]["fan_default"],
|
|
|
- "ptc_polarity": project["outputs"]["ptc_polarity"],
|
|
|
- "fan_polarity": project["outputs"]["fan_polarity"],
|
|
|
- "fan_connector_active_level": project["outputs"]["fan_connector_active_level"],
|
|
|
- "fan_board_inverts": project["outputs"]["fan_board_inverts"],
|
|
|
- "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"]),
|
|
|
+ "ptc_default": outputs["ptc_default"],
|
|
|
+ "fan_default": outputs["fan_default"],
|
|
|
+ "ptc_polarity": outputs["ptc_polarity"],
|
|
|
+ "fan_polarity": outputs["fan_polarity"],
|
|
|
+ "fan_connector_active_level": outputs["fan_connector_active_level"],
|
|
|
+ "fan_board_inverts": outputs["fan_board_inverts"],
|
|
|
+ "set_all_payload_len": project["protocol"]["set_all"]["payload_length"],
|
|
|
+ "set_all_frame_len": project["protocol"]["set_all"]["payload_length"] + 6,
|
|
|
+ "temperature_payload_len": temperature["value_count"],
|
|
|
+ "temperature_frame_len": temperature["value_count"] + 6,
|
|
|
+ "temperature_addresses": list(temperature["device_addresses"]),
|
|
|
+ "boot_targets": dict(outputs["boot_targets"]),
|
|
|
+ "watchdog_timeout_seconds": watchdog["timeout_seconds"],
|
|
|
+ "watchdog_tick_clks": watchdog["tick_clks"],
|
|
|
+ "watchdog_timeout_ticks": watchdog["timeout_ticks"],
|
|
|
+ "watchdog_target": dict(watchdog["target"]),
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -135,11 +462,7 @@ def _render_params(project: Mapping[str, Any]) -> str:
|
|
|
motors = project["motors"]
|
|
|
temperature = project["temperature"]
|
|
|
uart = project["uart"]
|
|
|
- total_channels = outputs["ptc_count"] + outputs["fan_count"]
|
|
|
- phase_count = 1 << outputs["pwm_bits"]
|
|
|
- pwm_slot_clks = int(round(project["device"]["clock_hz"] / (
|
|
|
- outputs["pwm_frequency_hz"] * total_channels * phase_count
|
|
|
- )))
|
|
|
+ watchdog = project["watchdog"]
|
|
|
lines = [
|
|
|
"// Generated from project.json. Do not edit.",
|
|
|
f"`define P4_CLOCK_HZ {project['device']['clock_hz']}",
|
|
|
@@ -147,11 +470,17 @@ def _render_params(project: Mapping[str, Any]) -> str:
|
|
|
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_PWM_PROTOCOL_WIDTH {outputs['pwm_bits']}",
|
|
|
+ f"`define P4_PWM_INTERNAL_WIDTH {outputs['pwm_internal_width']}",
|
|
|
+ f"`define P4_PWM_MAX_DUTY {outputs['pwm_max_duty']}",
|
|
|
+ f"`define P4_PWM_PHASE_COUNT {outputs['pwm_phase_count']}",
|
|
|
f"`define P4_PWM_FREQUENCY_HZ {outputs['pwm_frequency_hz']}",
|
|
|
- f"`define P4_PWM_SLOT_CLKS {pwm_slot_clks}",
|
|
|
+ f"`define P4_PWM_SLOT_CLKS {outputs['pwm_slot_clks']}",
|
|
|
f"`define P4_FIXED_MOTOR_STEPS {motors['fixed_steps']}",
|
|
|
+ f"`define P4_MOTOR_STEP_RATE_HZ {motors['step_rate_hz']}",
|
|
|
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_MOTOR_HEARTBEAT_COMMAND 2'd{motors['heartbeat_value']}",
|
|
|
f"`define P4_TEMPERATURE_COUNT {temperature['value_count']}",
|
|
|
f"`define P4_TEMPERATURE_COUNT_BYTE 8'h{temperature['value_count']:02X}",
|
|
|
f"`define P4_TEMP_MAX_CELSIUS {temperature['maximum_celsius']}",
|
|
|
@@ -159,6 +488,12 @@ def _render_params(project: Mapping[str, Any]) -> str:
|
|
|
f"`define P4_FAN_ACTIVE_HIGH {1 if outputs['fan_polarity'] == 'active_high' else 0}",
|
|
|
f"`define P4_PTC_DEFAULT_TARGET 8'h{outputs['ptc_default']:02X}",
|
|
|
f"`define P4_FAN_DEFAULT_TARGET 8'h{outputs['fan_default']:02X}",
|
|
|
+ f"`define P4_WATCHDOG_TIMEOUT_SECONDS {watchdog['timeout_seconds']}",
|
|
|
+ f"`define P4_WATCHDOG_TICK_US {watchdog['tick_us']}",
|
|
|
+ f"`define P4_WATCHDOG_TICK_CLKS {watchdog['tick_clks']}",
|
|
|
+ f"`define P4_WATCHDOG_TIMEOUT_TICKS {watchdog['timeout_ticks']}",
|
|
|
+ f"`define P4_WATCHDOG_PTC_TARGET 8'h{watchdog['target']['ptc']:02X}",
|
|
|
+ f"`define P4_WATCHDOG_FAN_TARGET 8'h{watchdog['target']['fan']:02X}",
|
|
|
]
|
|
|
lines.extend(
|
|
|
f"`define P4_TEMP_DEVICE_ADDR_{index} 7'h{address:02X}"
|
|
|
@@ -172,33 +507,60 @@ def _render_params(project: Mapping[str, Any]) -> str:
|
|
|
return "\n".join(lines)
|
|
|
|
|
|
|
|
|
+def render_adc_text(project: Mapping[str, Any]) -> str:
|
|
|
+ lines = [
|
|
|
+ "# Generated from project.json. Do not edit.",
|
|
|
+ "# Active interface only; reserved board loads are intentionally absent.",
|
|
|
+ ]
|
|
|
+ active_interface = list(render_adc(project))
|
|
|
+ for signal, ball in active_interface:
|
|
|
+ lines.append(f"set_pin_assignment {{ {signal} }} {{ LOCATION = {ball}; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }}")
|
|
|
+ return "\n".join(lines) + "\n"
|
|
|
+
|
|
|
+
|
|
|
def generate_project(project_path: Path, output_root: Path) -> dict[str, Any]:
|
|
|
project = load_project(project_path)
|
|
|
- temperature_count = project["temperature"]["value_count"]
|
|
|
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")
|
|
|
+ (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"],
|
|
|
+ "protocol": project["protocol"],
|
|
|
"commands": {
|
|
|
- "set_all": {"type": "0x01", "payload_length": summary["set_all_payload_len"], "response": "none"},
|
|
|
+ "set_all": {
|
|
|
+ "type": "0x01",
|
|
|
+ "payload_length": summary["set_all_payload_len"],
|
|
|
+ "response": "none",
|
|
|
+ "duty_range": [0, 100],
|
|
|
+ "motor_values": [0, 1, 2],
|
|
|
+ "heartbeat": [0, 0],
|
|
|
+ },
|
|
|
"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"]},
|
|
|
+ "temperature_data": {
|
|
|
+ "type": "0x82",
|
|
|
+ "payload_length": summary["temperature_payload_len"],
|
|
|
+ "frame_length": summary["temperature_frame_len"],
|
|
|
+ },
|
|
|
},
|
|
|
- "crc": project["protocol"]["crc"],
|
|
|
+ "outputs": project["outputs"],
|
|
|
"motors": project["motors"],
|
|
|
+ "limits": project["limits"],
|
|
|
"temperature": project["temperature"],
|
|
|
+ "watchdog": project["watchdog"],
|
|
|
}
|
|
|
_write_json(output / "host-contract.json", contract)
|
|
|
_write_json(output / "project-summary.json", summary)
|
|
|
_write_json(
|
|
|
output / "golden-vectors.json",
|
|
|
{
|
|
|
+ "set_all": _frame_hex(0x01, bytes(range(8)) + bytes((10, 20, 30, 40, 1, 2))),
|
|
|
"get_temperature": _frame_hex(0x02, b""),
|
|
|
- "all_invalid_temperature": _frame_hex(0x82, bytes([0xFF] * temperature_count)),
|
|
|
+ "all_invalid_temperature": _frame_hex(0x82, bytes([0xFF] * project["temperature"]["value_count"])),
|
|
|
},
|
|
|
)
|
|
|
_write_json(
|
|
|
@@ -208,8 +570,21 @@ def generate_project(project_path: Path, output_root: Path) -> dict[str, Any]:
|
|
|
"project_id": project["project_id"],
|
|
|
"sources": list(ACTIVE_SOURCE_PATHS),
|
|
|
"generated_from": "project.json",
|
|
|
+ "contract": {
|
|
|
+ "uart": project["uart"],
|
|
|
+ "protocol": project["protocol"],
|
|
|
+ "set_all_payload_length": summary["set_all_payload_len"],
|
|
|
+ "temperature_frame_length": summary["temperature_frame_len"],
|
|
|
+ },
|
|
|
+ "active_interface": [
|
|
|
+ {"signal": signal, "ball": ball}
|
|
|
+ for signal, ball in render_adc(project)
|
|
|
+ ],
|
|
|
},
|
|
|
)
|
|
|
+ (output / f"{project['project_id'].replace('-', '_')}.adc").write_text(
|
|
|
+ render_adc_text(project), encoding="ascii", newline="\n"
|
|
|
+ )
|
|
|
return summary
|
|
|
|
|
|
|