regression.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. """Product-specific regression checks for the current FCB board."""
  2. from __future__ import annotations
  3. from collections import Counter
  4. from collections.abc import Mapping
  5. class BoardRegressionError(RuntimeError):
  6. """Raised when generated board facts drift from the reviewed board."""
  7. EXPECTED_COUNTS = {
  8. "footprints": 1052,
  9. "touchpoints": 110,
  10. "pads": 472,
  11. "connector_contacts": 288,
  12. "connector_auxiliary_pads": 116,
  13. }
  14. EXPECTED_CATEGORIES = {"CON": 62, "J": 16, "PL": 3, "TP": 29}
  15. FOUR_CONTACT_REFS = {
  16. "CON12", "CON30", "CON31", "CON32", "CON33", "CON34", "CON35", "CON36",
  17. "CON37", "CON38", "CON39", "CON40", "CON54", "CON58", "CON59", "CON60",
  18. "CON61", "CON62", "CON63", "CON64", "CON65", "CON66", "CON67", "CON68",
  19. "CON69", "CON70", "CON77", "CON78",
  20. }
  21. THREE_CONTACT_REFS = {
  22. "CON53", "CON55", "CON56", "CON57", "CON79", "CON80", "CON81", "CON82",
  23. "CON83", "CON84", "CON85", "CON86", "CON87", "CON88", "CON89", "CON90",
  24. "CON91", "CON92", "CON93", "CON94", "CON95", "CON96", "CON97", "CON98",
  25. }
  26. FOURTEEN_CONTACT_REFS = {"CON18", "CON27", "CON28", "CON29"}
  27. POWER_CONTACT_REFS = {"CON2", "CON23"}
  28. FAN_CONTACT_REFS = {"CON8", "CON24", "CON25", "CON26"}
  29. EXPECTED_CONNECTOR_REFERENCES = (
  30. FOUR_CONTACT_REFS
  31. | THREE_CONTACT_REFS
  32. | FOURTEEN_CONTACT_REFS
  33. | POWER_CONTACT_REFS
  34. | FAN_CONTACT_REFS
  35. )
  36. def _category(reference: str) -> str:
  37. return next((prefix for prefix in ("CON", "J", "PL", "TP") if reference.startswith(prefix)), "OTHER")
  38. def _pin_index(touchpoint: Mapping[str, object]) -> dict[str, Mapping[str, object]]:
  39. pins = touchpoint.get("pins", [])
  40. return {
  41. str(pin.get("number")): pin
  42. for pin in pins
  43. if isinstance(pin, Mapping) and pin.get("number") is not None
  44. }
  45. def _validate_recommendable_pins(touchpoints: list[Mapping[str, object]]) -> None:
  46. for touchpoint in touchpoints:
  47. for pin in touchpoint.get("pins", []):
  48. if not isinstance(pin, Mapping):
  49. continue
  50. recommendation = pin.get("recommendation")
  51. if not isinstance(recommendation, Mapping) or recommendation.get("eligible") is not True:
  52. continue
  53. fpga = pin.get("fpga")
  54. valid = (
  55. pin.get("evidence_tier") == "cross_verified"
  56. and isinstance(fpga, Mapping)
  57. and fpga.get("evidence_tier") == "cross_verified"
  58. and bool(fpga.get("ball"))
  59. and pin.get("electrical") is True
  60. and pin.get("mechanical") is not True
  61. and pin.get("pad_role") == "contact"
  62. and pin.get("reserved") is not True
  63. and pin.get("fixed_load_conflict") is not True
  64. and pin.get("function_category") not in {"power", "ground"}
  65. and str(pin.get("net") or "").upper() not in {"GND", "AGND", "DGND", "PGND"}
  66. and pin.get("external_level") not in {None, "", "unknown"}
  67. )
  68. if not valid:
  69. raise BoardRegressionError(
  70. f"recommendable pin violates contract: {touchpoint.get('reference')}.{pin.get('number')}"
  71. )
  72. def _expected_connector_contract(reference: str) -> tuple[str, list[list[str]], set[str], int]:
  73. if reference in FOUR_CONTACT_REFS:
  74. order = ["1", "2", "3", "4"] if reference in {"CON54", "CON77", "CON78"} else ["4", "3", "2", "1"]
  75. return "CON_11251W00_NPX_SX_4P", [[number] for number in order], {"5", "6"}, 0
  76. if reference in THREE_CONTACT_REFS:
  77. return "CON_51541_003_P1D25", [["3", "2", "1"]], {"4", "5"}, 0
  78. if reference in FOURTEEN_CONTACT_REFS:
  79. return (
  80. "CON_3110_14P_P2D54",
  81. [
  82. ["13", "11", "9", "7", "5", "3", "1"],
  83. ["14", "12", "10", "8", "6", "4", "2"],
  84. ],
  85. set(),
  86. 0,
  87. )
  88. if reference == "CON23":
  89. return "CON_C22930_1084N_X", [["5", "1"], ["6", "2"], ["7", "3"], ["8", "4"]], {"9", "10"}, 2
  90. if reference == "CON2":
  91. return "CON_C22930_1084N_X", [["4", "8"], ["3", "7"], ["2", "6"], ["1", "5"]], {"9", "10"}, 2
  92. if reference in FAN_CONTACT_REFS:
  93. return "CON_PZGM_B03_08P", [[str(number)] for number in range(8, 0, -1)], set(), 1
  94. raise BoardRegressionError(f"missing connector regression contract for {reference}")
  95. def _validate_connector_contracts(by_reference: Mapping[str, Mapping[str, object]]) -> None:
  96. actual_references = {reference for reference in by_reference if reference.startswith("CON")}
  97. if actual_references != EXPECTED_CONNECTOR_REFERENCES:
  98. missing = sorted(EXPECTED_CONNECTOR_REFERENCES.difference(actual_references))
  99. extra = sorted(actual_references.difference(EXPECTED_CONNECTOR_REFERENCES))
  100. raise BoardRegressionError(f"connector reference set drifted; missing={missing} extra={extra}")
  101. for reference in sorted(EXPECTED_CONNECTOR_REFERENCES):
  102. touchpoint = by_reference[reference]
  103. connector = touchpoint.get("connector")
  104. if not isinstance(connector, Mapping):
  105. raise BoardRegressionError(f"{reference} connector contract missing")
  106. profile, grid, shield_numbers, mechanical_count = _expected_connector_contract(reference)
  107. order = [number for row in grid for number in row]
  108. if connector.get("review_status") != "cross_verified":
  109. raise BoardRegressionError(f"{reference} connector layout is not cross-verified")
  110. if connector.get("profile") != profile:
  111. raise BoardRegressionError(f"{reference} profile={connector.get('profile')} expected {profile}")
  112. if connector.get("view") != "board_front":
  113. raise BoardRegressionError(f"{reference} connector view must be board_front")
  114. if connector.get("contact_grid") != grid or connector.get("contact_order") != order:
  115. raise BoardRegressionError(f"{reference} contact order or grid drifted")
  116. expected_columns = len(grid[0]) if grid else 0
  117. expected_auxiliary = len(shield_numbers) + mechanical_count
  118. if (
  119. connector.get("contact_count") != len(order)
  120. or connector.get("auxiliary_pad_count") != expected_auxiliary
  121. or connector.get("rows") != len(grid)
  122. or connector.get("columns") != expected_columns
  123. ):
  124. raise BoardRegressionError(f"{reference} connector dimensions or pad counts drifted")
  125. raw_pins = touchpoint.get("pins", [])
  126. pins = [pin for pin in raw_pins if isinstance(pin, Mapping)] if isinstance(raw_pins, list) else []
  127. pin_ids = [str(pin.get("id") or "") for pin in pins]
  128. if not all(pin_ids) or len(pin_ids) != len(set(pin_ids)):
  129. raise BoardRegressionError(f"{reference} pin ids are missing or duplicated")
  130. numbered = _pin_index(touchpoint)
  131. for board_order, number in enumerate(order, start=1):
  132. pin = numbered.get(number)
  133. if pin is None or pin.get("pad_role") != "contact":
  134. raise BoardRegressionError(f"{reference}.{number} contact pin role drifted")
  135. row = next(index for index, values in enumerate(grid, start=1) if number in values)
  136. column = grid[row - 1].index(number) + 1
  137. if (
  138. pin.get("board_order") != board_order
  139. or pin.get("row") != row
  140. or pin.get("column") != column
  141. ):
  142. raise BoardRegressionError(f"{reference}.{number} board-front position drifted")
  143. actual_shields = {
  144. str(pin.get("number")) for pin in pins if pin.get("pad_role") == "shield"
  145. }
  146. if actual_shields != shield_numbers:
  147. raise BoardRegressionError(f"{reference} shield pad classification drifted")
  148. actual_mechanical = sum(pin.get("pad_role") == "mechanical" for pin in pins)
  149. if actual_mechanical != mechanical_count:
  150. raise BoardRegressionError(f"{reference} mechanical pad classification drifted")
  151. if any(pin.get("pad_role") == "unknown" for pin in pins):
  152. raise BoardRegressionError(f"{reference} contains an unknown pad role")
  153. def _validate_j3_jtag(by_reference: Mapping[str, Mapping[str, object]]) -> None:
  154. touchpoint = by_reference.get("J3")
  155. if not isinstance(touchpoint, Mapping):
  156. raise BoardRegressionError("J3 FPGA JTAG header missing")
  157. if "FPGA JTAG" not in str(touchpoint.get("summary") or ""):
  158. raise BoardRegressionError("J3 FPGA JTAG purpose missing")
  159. connector = touchpoint.get("connector")
  160. grid = [["6", "1"], ["7", "2"], ["8", "3"], ["9", "4"], ["10", "5"]]
  161. order = [number for row in grid for number in row]
  162. if not isinstance(connector, Mapping):
  163. raise BoardRegressionError("J3 connector layout missing")
  164. if (
  165. connector.get("profile") != "J3_FPGA_JTAG_2X5"
  166. or connector.get("review_status") != "cross_verified"
  167. or connector.get("view") != "board_front"
  168. or connector.get("contact_grid") != grid
  169. or connector.get("contact_order") != order
  170. or connector.get("contact_count") != 10
  171. or connector.get("auxiliary_pad_count") != 0
  172. or connector.get("rows") != 5
  173. or connector.get("columns") != 2
  174. ):
  175. raise BoardRegressionError("J3 board-front contact grid drifted")
  176. pins = _pin_index(touchpoint)
  177. expected_nets = {
  178. "1": "N9532014",
  179. "2": "GND",
  180. "3": "N9532092",
  181. "4": "N9532034",
  182. "5": "N9532142",
  183. "6": "Net_649",
  184. "7": "Net_650",
  185. "8": "Net_648",
  186. "9": "N9532240",
  187. "10": "GND",
  188. }
  189. if set(pins) != set(expected_nets):
  190. raise BoardRegressionError("J3 pin set drifted")
  191. for board_order, number in enumerate(order, start=1):
  192. pin = pins[number]
  193. row = next(index for index, values in enumerate(grid, start=1) if number in values)
  194. column = grid[row - 1].index(number) + 1
  195. if (
  196. pin.get("net") != expected_nets[number]
  197. or pin.get("pad_role") != "contact"
  198. or pin.get("board_order") != board_order
  199. or pin.get("row") != row
  200. or pin.get("column") != column
  201. ):
  202. raise BoardRegressionError(f"J3.{number} net or board-front position drifted")
  203. signal_specs = {
  204. "1": ("FPGA_TCK", "A7", "input", "jtag_tck", "R63"),
  205. "3": ("FPGA_TDO", "C6", "output", "jtag_tdo", "R65"),
  206. "5": ("FPGA_TMS", "B8", "input", "jtag_tms", "R67"),
  207. "9": ("FPGA_TDI", "A6", "input", "jtag_tdi", "R69"),
  208. }
  209. for number, (signal, ball, direction, category, series_resistor) in signal_specs.items():
  210. pin = pins[number]
  211. fpga = pin.get("fpga")
  212. programming = pin.get("programming")
  213. path_text = " ".join(str(value) for value in pin.get("path", []))
  214. programming_text = " ".join(
  215. str(value)
  216. for value in (
  217. programming.get("conclusion", "") if isinstance(programming, Mapping) else "",
  218. programming.get("network_explanation", "") if isinstance(programming, Mapping) else "",
  219. programming.get("hdl_guidance", "") if isinstance(programming, Mapping) else "",
  220. *(programming.get("connection_guidance", []) if isinstance(programming, Mapping) else []),
  221. *(programming.get("risks", []) if isinstance(programming, Mapping) else []),
  222. )
  223. )
  224. if (
  225. pin.get("direction") != direction
  226. or pin.get("function_category") != category
  227. or pin.get("reserved") is not True
  228. or pin.get("evidence_tier") != "cross_verified"
  229. or not isinstance(fpga, Mapping)
  230. or fpga.get("ball") != ball
  231. or fpga.get("board_signal") != signal
  232. or fpga.get("evidence_tier") != "cross_verified"
  233. or signal not in path_text
  234. or f"U3.{ball}" not in path_text
  235. or series_resistor not in path_text
  236. or not isinstance(programming, Mapping)
  237. or programming.get("status") != "verified_fpga"
  238. or ("专用" not in programming_text and "dedicated" not in programming_text.lower())
  239. ):
  240. raise BoardRegressionError(f"J3.{number} dedicated JTAG path drifted")
  241. vref = pins["4"]
  242. vref_programming = vref.get("programming")
  243. vref_text = " ".join(
  244. str(value)
  245. for value in (
  246. vref.get("summary", ""),
  247. *vref.get("path", []),
  248. vref_programming.get("conclusion", "") if isinstance(vref_programming, Mapping) else "",
  249. vref_programming.get("network_explanation", "") if isinstance(vref_programming, Mapping) else "",
  250. *(vref_programming.get("connection_guidance", []) if isinstance(vref_programming, Mapping) else []),
  251. )
  252. )
  253. if (
  254. vref.get("function_category") != "reference_voltage"
  255. or vref.get("fpga") is not None
  256. or "R61" not in vref_text
  257. or "+3V3" not in vref_text
  258. or not isinstance(vref_programming, Mapping)
  259. or vref_programming.get("status") != "not_programmable"
  260. or ("不是给板子供电" not in vref_text and "not board power" not in vref_text.lower())
  261. ):
  262. raise BoardRegressionError("J3.4 VREF purpose drifted")
  263. for number in ("6", "7", "8"):
  264. pin = pins[number]
  265. programming = pin.get("programming")
  266. if (
  267. pin.get("function_category") != "not_connected"
  268. or pin.get("fpga") is not None
  269. or pin.get("reserved") is not True
  270. or not isinstance(programming, Mapping)
  271. or programming.get("status") != "not_programmable"
  272. ):
  273. raise BoardRegressionError(f"J3.{number} NC status drifted")
  274. def _validate_programming_guidance(by_reference: Mapping[str, Mapping[str, object]]) -> None:
  275. for reference, touchpoint in by_reference.items():
  276. pins = touchpoint.get("pins", [])
  277. for pin in pins if isinstance(pins, list) else []:
  278. if not isinstance(pin, Mapping):
  279. continue
  280. programming = pin.get("programming")
  281. if not isinstance(programming, Mapping):
  282. raise BoardRegressionError(f"{reference}.{pin.get('number')} programming guidance missing")
  283. if programming.get("status") not in {
  284. "verified_fpga",
  285. "verified_mcu",
  286. "board_only",
  287. "not_programmable",
  288. "unknown",
  289. }:
  290. raise BoardRegressionError(f"{reference}.{pin.get('number')} programming status invalid")
  291. for field in ("conclusion", "network_explanation", "hdl_guidance"):
  292. if not str(programming.get(field) or "").strip():
  293. raise BoardRegressionError(
  294. f"{reference}.{pin.get('number')} programming {field} missing"
  295. )
  296. for field in ("connection_guidance", "constraints", "risks", "sources"):
  297. if not isinstance(programming.get(field), list):
  298. raise BoardRegressionError(
  299. f"{reference}.{pin.get('number')} programming {field} must be an array"
  300. )
  301. if programming.get("status") == "verified_fpga":
  302. fpga = pin.get("fpga")
  303. if (
  304. pin.get("evidence_tier") != "cross_verified"
  305. or not isinstance(fpga, Mapping)
  306. or fpga.get("evidence_tier") != "cross_verified"
  307. or not fpga.get("ball")
  308. ):
  309. raise BoardRegressionError(
  310. f"{reference}.{pin.get('number')} programming claims an unverified FPGA path"
  311. )
  312. if programming.get("status") == "verified_mcu":
  313. mcu = pin.get("mcu")
  314. if (
  315. pin.get("evidence_tier") != "cross_verified"
  316. or not isinstance(mcu, Mapping)
  317. or mcu.get("evidence_tier") != "cross_verified"
  318. or not mcu.get("device")
  319. or not mcu.get("pin")
  320. or not mcu.get("signal")
  321. or pin.get("fpga") is not None
  322. ):
  323. raise BoardRegressionError(
  324. f"{reference}.{pin.get('number')} programming claims an invalid MCU path"
  325. )
  326. if mcu.get("device") == "U27" and mcu.get("family") != "GD32F303CGT6":
  327. raise BoardRegressionError(
  328. f"{reference}.{pin.get('number')} U27 family must remain GD32F303CGT6"
  329. )
  330. con78 = _pin_index(by_reference["CON78"])
  331. for number, signal, ball, peer in (
  332. ("1", "UART2_TX", "M10", "RX"),
  333. ("2", "UART2_RX", "N11", "TX"),
  334. ):
  335. pin = con78[number]
  336. programming = pin.get("programming")
  337. text = " ".join(
  338. str(value)
  339. for value in (
  340. programming.get("conclusion", ""),
  341. programming.get("network_explanation", ""),
  342. programming.get("hdl_guidance", ""),
  343. *programming.get("connection_guidance", []),
  344. *programming.get("constraints", []),
  345. )
  346. ) if isinstance(programming, Mapping) else ""
  347. required = (signal, ball, "U23", peer, "TTL", f"LOCATION = {ball}", "IOSTANDARD = LVCMOS33")
  348. if not isinstance(programming, Mapping) or programming.get("status") != "verified_fpga" or any(
  349. value not in text for value in required
  350. ):
  351. raise BoardRegressionError(f"CON78.{number} programming guidance drifted")
  352. con30_pin4 = _pin_index(by_reference["CON30"])["4"]
  353. con30_fpga = con30_pin4.get("fpga")
  354. con30_programming = con30_pin4.get("programming")
  355. con30_text = " ".join(
  356. str(value)
  357. for value in (
  358. con30_programming.get("conclusion", ""),
  359. con30_programming.get("network_explanation", ""),
  360. con30_programming.get("hdl_guidance", ""),
  361. *con30_programming.get("constraints", []),
  362. )
  363. ) if isinstance(con30_programming, Mapping) else ""
  364. if (
  365. con30_pin4.get("net") != "FAN_GPIO4"
  366. or con30_pin4.get("summary") != "MOTOR_EN_N"
  367. or con30_pin4.get("external_level") != "3.3V LVCMOS"
  368. or "R937 4.7K to +3V3" not in str(con30_pin4.get("board_pull") or "")
  369. or not isinstance(con30_fpga, Mapping)
  370. or con30_fpga.get("ball") != "A10"
  371. or con30_fpga.get("board_signal") != "MOTOR_EN_N"
  372. or not isinstance(con30_programming, Mapping)
  373. or con30_programming.get("status") != "verified_fpga"
  374. or any(value not in con30_text for value in ("MOTOR_EN_N", "FAN_GPIO4", "A10"))
  375. ):
  376. raise BoardRegressionError("CON30.4 MOTOR_EN_N programming guidance drifted")
  377. if con30_pin4.get("active_level") != "low" or (
  378. "低电平" not in con30_text and "low active" not in con30_text
  379. ):
  380. raise BoardRegressionError("CON30.4 programming must remain low active")
  381. con31_pin4 = _pin_index(by_reference["CON31"])["4"]
  382. con31_fpga = con31_pin4.get("fpga")
  383. con31_programming = con31_pin4.get("programming")
  384. con31_text = " ".join(
  385. str(value)
  386. for value in (
  387. *con31_pin4.get("path", []),
  388. con31_pin4.get("polarity", ""),
  389. con31_programming.get("conclusion", ""),
  390. con31_programming.get("network_explanation", ""),
  391. con31_programming.get("hdl_guidance", ""),
  392. *con31_programming.get("constraints", []),
  393. )
  394. ) if isinstance(con31_programming, Mapping) else ""
  395. if (
  396. con31_pin4.get("net") != "N9971986"
  397. or con31_pin4.get("function_category") != "pwm"
  398. or con31_pin4.get("evidence_tier") != "cross_verified"
  399. or not isinstance(con31_fpga, Mapping)
  400. or con31_fpga.get("ball") != "E10"
  401. or con31_fpga.get("board_signal") != "FAN_GPIO6"
  402. or con31_fpga.get("evidence_tier") != "cross_verified"
  403. or not isinstance(con31_programming, Mapping)
  404. or con31_programming.get("status") != "verified_fpga"
  405. or con31_programming.get("evidence_tier") != "cross_verified"
  406. or any(value not in con31_text for value in ("N9971986", "Q105", "FAN_GPIO6", "E10"))
  407. or ("拉低" not in con31_text and "pulls connector low" not in con31_text)
  408. ):
  409. raise BoardRegressionError("CON31.4 programming verified open-collector path drifted")
  410. con58_pin4 = _pin_index(by_reference["CON58"])["4"]
  411. con58_fpga = con58_pin4.get("fpga")
  412. con58_programming = con58_pin4.get("programming")
  413. con58_text = " ".join(
  414. str(value)
  415. for value in (
  416. con58_pin4.get("summary", ""),
  417. con58_programming.get("conclusion", ""),
  418. con58_programming.get("network_explanation", ""),
  419. con58_programming.get("hdl_guidance", ""),
  420. *con58_programming.get("constraints", []),
  421. )
  422. ) if isinstance(con58_programming, Mapping) else ""
  423. if (
  424. con58_pin4.get("net") != "FAN_GPIO38"
  425. or con58_pin4.get("function_category") != "motor_step"
  426. or con58_pin4.get("active_level") != "rising_edge"
  427. or not isinstance(con58_fpga, Mapping)
  428. or con58_fpga.get("ball") != "D15"
  429. or con58_fpga.get("board_signal") != "MOTOR_STEP"
  430. or not isinstance(con58_programming, Mapping)
  431. or con58_programming.get("status") != "verified_fpga"
  432. or not any(marker in con58_text for marker in ("上升沿", "rising edge", "rising-edge"))
  433. ):
  434. raise BoardRegressionError("CON58.4 MOTOR_STEP rising-edge semantics drifted")
  435. con65_pin4 = _pin_index(by_reference["CON65"])["4"]
  436. con65_fpga = con65_pin4.get("fpga")
  437. con65_programming = con65_pin4.get("programming")
  438. con65_text = " ".join(
  439. str(value)
  440. for value in (
  441. con65_pin4.get("summary", ""),
  442. con65_programming.get("conclusion", ""),
  443. con65_programming.get("network_explanation", ""),
  444. con65_programming.get("hdl_guidance", ""),
  445. *con65_programming.get("constraints", []),
  446. )
  447. ) if isinstance(con65_programming, Mapping) else ""
  448. if (
  449. con65_pin4.get("net") != "FAN_GPIO32"
  450. or con65_pin4.get("function_category") != "motor_direction"
  451. or con65_pin4.get("active_level") != "state"
  452. or not isinstance(con65_fpga, Mapping)
  453. or con65_fpga.get("ball") != "H14"
  454. or con65_fpga.get("board_signal") != "MOTOR_DIR"
  455. or not isinstance(con65_programming, Mapping)
  456. or con65_programming.get("status") != "verified_fpga"
  457. or not any(marker in con65_text for marker in ("方向状态", "direction state"))
  458. ):
  459. raise BoardRegressionError("CON65.4 MOTOR_DIR state semantics drifted")
  460. con53_pin1 = _pin_index(by_reference["CON53"])["1"]
  461. con53_programming = con53_pin1.get("programming")
  462. con53_text = " ".join(
  463. str(value)
  464. for value in (
  465. con53_programming.get("hdl_guidance", ""),
  466. *con53_programming.get("risks", []),
  467. )
  468. ) if isinstance(con53_programming, Mapping) else ""
  469. if (
  470. con53_pin1.get("function_category") != "remote_temperature"
  471. or not isinstance(con53_programming, Mapping)
  472. or con53_programming.get("status") != "board_only"
  473. or "open-drain" not in con53_text
  474. or not any(marker in con53_text for marker in ("只拉低或释放", "drive low or release"))
  475. or "R811/R812" not in con53_text
  476. ):
  477. raise BoardRegressionError("CON53.1 CT7428 open-drain programming guidance drifted")
  478. V12_EXPECTED_COUNTS = {
  479. "footprints": 1317,
  480. "touchpoints": 118,
  481. "pads": 629,
  482. "resistors": 644,
  483. "resistor_pads": 1288,
  484. }
  485. V12_EXPECTED_CATEGORIES = {"CON": 88, "J": 18, "PL": 4, "TP": 8}
  486. V12_EXPECTED_RESISTORS = 644
  487. def _board_identity(payload: Mapping[str, object]) -> str:
  488. board = payload.get("board")
  489. if not isinstance(board, Mapping):
  490. return ""
  491. return f"{board.get('id') or ''} {board.get('name') or ''}".lower()
  492. def _validate_v12_product_regression(payload: Mapping[str, object]) -> None:
  493. """Lightweight contract for FCB V1.2 until full cross-verification catches up."""
  494. counts = payload.get("counts")
  495. if not isinstance(counts, Mapping):
  496. raise BoardRegressionError("counts missing")
  497. for name, expected in V12_EXPECTED_COUNTS.items():
  498. if counts.get(name) != expected:
  499. raise BoardRegressionError(f"{name}={counts.get(name)} expected {expected}")
  500. categories = counts.get("categories")
  501. if not isinstance(categories, Mapping):
  502. raise BoardRegressionError("category counts missing")
  503. for name, expected in V12_EXPECTED_CATEGORIES.items():
  504. if categories.get(name) != expected:
  505. raise BoardRegressionError(f"{name}={categories.get(name)} expected {expected}")
  506. if categories.get("R") != V12_EXPECTED_RESISTORS:
  507. raise BoardRegressionError(f"R={categories.get('R')} expected {V12_EXPECTED_RESISTORS}")
  508. sides = counts.get("sides")
  509. if not isinstance(sides, Mapping) or sides.get("front") != 118 or sides.get("back") != 0:
  510. back = sides.get("back") if isinstance(sides, Mapping) else "missing"
  511. raise BoardRegressionError(f"all touchpoints must be front-side; back={back}")
  512. raw_touchpoints = payload.get("touchpoints", [])
  513. touchpoints = [item for item in raw_touchpoints if isinstance(item, Mapping)] if isinstance(raw_touchpoints, list) else []
  514. references = [str(item.get("reference") or "") for item in touchpoints]
  515. if "CON5" in references:
  516. raise BoardRegressionError("CON5 must not appear as a footprint hotspot")
  517. if len(touchpoints) != 118 or len(set(references)) != 118:
  518. raise BoardRegressionError(
  519. f"touchpoint list has {len(touchpoints)} rows and {len(set(references))} unique references"
  520. )
  521. actual_categories = Counter(_category(reference) for reference in references)
  522. if dict(actual_categories) != V12_EXPECTED_CATEGORIES:
  523. raise BoardRegressionError(f"touchpoint reference categories drifted: {dict(actual_categories)}")
  524. if any(item.get("side") != "front" for item in touchpoints):
  525. raise BoardRegressionError("touchpoint list contains a non-front item")
  526. raw_resistors = payload.get("resistors", [])
  527. resistors = [item for item in raw_resistors if isinstance(item, Mapping)] if isinstance(raw_resistors, list) else []
  528. resistor_refs = [str(item.get("reference") or "") for item in resistors]
  529. if len(resistors) != V12_EXPECTED_RESISTORS or len(set(resistor_refs)) != V12_EXPECTED_RESISTORS:
  530. raise BoardRegressionError(
  531. f"resistor list has {len(resistors)} rows and {len(set(resistor_refs))} unique references"
  532. )
  533. for required in ("R7", "R12", "R63", "R674", "R1225"):
  534. if required not in set(resistor_refs):
  535. raise BoardRegressionError(f"{required} missing from resistor index")
  536. by_reference = {str(item.get("reference")): item for item in touchpoints}
  537. for required in ("CON12", "CON78", "J3"):
  538. if required not in by_reference:
  539. raise BoardRegressionError(f"{required} missing on V1.2 board")
  540. con78 = by_reference["CON78"]
  541. pins = _pin_index(con78)
  542. for number, net in (("1", "RS232_TX2"), ("2", "RS232_RX2")):
  543. if number not in pins or pins[number].get("net") != net:
  544. actual = pins.get(number, {}).get("net") if number in pins else "missing"
  545. raise BoardRegressionError(f"CON78.{number} net={actual} expected {net}")
  546. _validate_recommendable_pins(touchpoints)
  547. from .data import validate_resistor_index
  548. try:
  549. validate_resistor_index(payload)
  550. except ValueError as exc:
  551. raise BoardRegressionError(str(exc)) from exc
  552. def validate_product_regression(payload: Mapping[str, object]) -> None:
  553. identity = _board_identity(payload)
  554. if "v1.2" in identity or "20260717" in identity or "fcb_fpga_mcu_v1_2" in identity:
  555. _validate_v12_product_regression(payload)
  556. return
  557. counts = payload.get("counts")
  558. if not isinstance(counts, Mapping):
  559. raise BoardRegressionError("counts missing")
  560. for name, expected in EXPECTED_COUNTS.items():
  561. if counts.get(name) != expected:
  562. raise BoardRegressionError(f"{name}={counts.get(name)} expected {expected}")
  563. categories = counts.get("categories")
  564. if not isinstance(categories, Mapping):
  565. raise BoardRegressionError("category counts missing")
  566. for name, expected in EXPECTED_CATEGORIES.items():
  567. if categories.get(name) != expected:
  568. raise BoardRegressionError(f"{name}={categories.get(name)} expected {expected}")
  569. sides = counts.get("sides")
  570. if not isinstance(sides, Mapping) or sides.get("front") != 110 or sides.get("back") != 0:
  571. back = sides.get("back") if isinstance(sides, Mapping) else "missing"
  572. raise BoardRegressionError(f"all touchpoints must be front-side; back={back}")
  573. raw_touchpoints = payload.get("touchpoints", [])
  574. touchpoints = [item for item in raw_touchpoints if isinstance(item, Mapping)] if isinstance(raw_touchpoints, list) else []
  575. references = [str(item.get("reference") or "") for item in touchpoints]
  576. if "CON5" in references:
  577. raise BoardRegressionError("CON5 must not appear as a footprint hotspot")
  578. if len(touchpoints) != 110 or len(set(references)) != 110:
  579. raise BoardRegressionError(f"touchpoint list has {len(touchpoints)} rows and {len(set(references))} unique references")
  580. actual_categories = Counter(_category(reference) for reference in references)
  581. if dict(actual_categories) != EXPECTED_CATEGORIES:
  582. raise BoardRegressionError(f"touchpoint reference categories drifted: {dict(actual_categories)}")
  583. if any(item.get("side") != "front" for item in touchpoints):
  584. raise BoardRegressionError("touchpoint list contains a non-front item")
  585. by_reference = {str(item.get("reference")): item for item in touchpoints}
  586. _validate_connector_contracts(by_reference)
  587. _validate_j3_jtag(by_reference)
  588. con78 = by_reference.get("CON78")
  589. if con78 is None:
  590. raise BoardRegressionError("CON78 missing")
  591. pins = _pin_index(con78)
  592. expected_nets = {
  593. "1": "RS232_TX2",
  594. "2": "RS232_RX2",
  595. "3": "GND",
  596. "4": "GND",
  597. "5": "GND",
  598. "6": "GND",
  599. }
  600. for number, net in expected_nets.items():
  601. if number not in pins or pins[number].get("net") != net:
  602. actual = pins.get(number, {}).get("net") if number in pins else "missing"
  603. raise BoardRegressionError(f"CON78.{number} net={actual} expected {net}")
  604. for number, ball in (("1", "M10"), ("2", "N11")):
  605. pin = pins[number]
  606. fpga = pin.get("fpga")
  607. if not isinstance(fpga, Mapping) or fpga.get("ball") != ball:
  608. actual = fpga.get("ball") if isinstance(fpga, Mapping) else "missing"
  609. raise BoardRegressionError(f"CON78.{number} FPGA ball={actual} expected {ball}")
  610. if "U23" not in " ".join(str(item) for item in pin.get("path", [])):
  611. raise BoardRegressionError(f"CON78.{number} path must include U23")
  612. _validate_recommendable_pins(touchpoints)
  613. _validate_programming_guidance(by_reference)