board-logic.mjs 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006
  1. const VERIFIED = "cross_verified";
  2. const POWER_NET = /^(?:GND|AGND|DGND|PGND|EARTH|VCC|VDD|VBAT|PWR|\+)/i;
  3. const RTL_PORT = /^[A-Za-z_][A-Za-z0-9_$]*(?:\[[0-9]+\])?$/;
  4. const RESISTOR_REF = /^r(\d+)$/i;
  5. const RESISTOR_PAD = /^r(\d+)\.(\d+)$/i;
  6. const BALL_PREFIX = /^ball:(.+)$/i;
  7. const NET_PREFIX = /^net:(.+)$/i;
  8. const VALUE_PREFIX = /^value:(.+)$/i;
  9. const ENDPOINT = /^u\d+\.[a-z]+\d+$/i;
  10. const ANNOTATION_RESISTOR = /(?<![A-Za-z0-9.])R(\d+)\b/g;
  11. const VALUE_GRAMMAR = /^(?:\d+(?:\.\d+)?[rRkKmM]\d*|\d+[rRkKmM]|\d+(?:\.\d+)?(?:ohm|ohms|Ω)?)$/i;
  12. const PACKAGE_OR_DNP = /^(?:0603|0805|0402|1206|1210|2010|2512|dnp|nc|n\/?c)$/i;
  13. const CAPABILITY_ALIASES = {
  14. pwm: ["pwm", "pwm_output"],
  15. gpio_output: ["gpio_output", "digital_output"],
  16. gpio_input: ["gpio_input", "digital_input"],
  17. tach: ["tach", "tach_input"],
  18. fan_tach: ["fan_tach", "tach_input"],
  19. presence: ["presence", "presence_input"],
  20. };
  21. function text(value) {
  22. return value == null ? "" : String(value);
  23. }
  24. function normalized(value) {
  25. return text(value).trim().toLocaleLowerCase();
  26. }
  27. function naturalCompare(left, right) {
  28. return text(left).localeCompare(text(right), undefined, {numeric: true, sensitivity: "base"});
  29. }
  30. function capabilities(pin) {
  31. return Array.isArray(pin?.fpga?.capabilities) ? pin.fpga.capabilities.map(text) : [];
  32. }
  33. function baseBlockers(pin) {
  34. const blockers = [];
  35. if (pin?.evidence_tier !== VERIFIED || pin?.fpga?.evidence_tier !== VERIFIED) {
  36. blockers.push("unverified");
  37. }
  38. if (!pin?.fpga?.ball || !pin?.fpga?.io_standard) blockers.push("missing_fpga_assignment");
  39. if (pin?.mechanical === true || pin?.electrical !== true) blockers.push("mechanical_or_non_electrical");
  40. if (pin?.pad_role !== "contact") blockers.push("not_connector_contact");
  41. if (["power", "ground"].includes(pin?.function_category) || POWER_NET.test(text(pin?.net))) {
  42. blockers.push("power_or_ground");
  43. }
  44. if (pin?.reserved === true) blockers.push("reserved");
  45. if (pin?.fixed_load_conflict === true) blockers.push("fixed_load_conflict");
  46. if (["", "unknown"].includes(normalized(pin?.external_level))) blockers.push("external_level_unknown");
  47. return blockers;
  48. }
  49. export function connectorPinGroups(touchpoint) {
  50. const pins = Array.isArray(touchpoint?.pins) ? touchpoint.pins : [];
  51. const connector = touchpoint?.connector;
  52. const reviewed = connector?.review_status === VERIFIED;
  53. const grid = reviewed && Array.isArray(connector?.contact_grid) ? connector.contact_grid : [];
  54. const byNumber = new Map(
  55. pins
  56. .filter((pin) => pin?.pad_role === "contact" && text(pin?.number))
  57. .map((pin) => [text(pin.number), pin]),
  58. );
  59. const contactRows = grid.map((row) =>
  60. (Array.isArray(row) ? row : []).map((number) => byNumber.get(text(number))).filter(Boolean),
  61. );
  62. return {
  63. contactRows,
  64. contacts: contactRows.flat(),
  65. auxiliary: pins.filter((pin) => pin?.pad_role !== "contact"),
  66. };
  67. }
  68. function pinSearchFields(touchpoint, pin, boardVersion = "v2_0") {
  69. const programming = pin?.programming ?? {};
  70. const logical = resolveLogicalPin(touchpoint, pin, boardVersion);
  71. return [
  72. ["pin", `${touchpoint.reference}.${pin.number}`],
  73. ["reference", touchpoint.reference],
  74. ["pin_number", pin.number],
  75. ["net", pin.net],
  76. ["summary", pin.summary],
  77. ["function", pin.function_category],
  78. ["direction", pin.direction],
  79. ["external_level", pin.external_level],
  80. ["fpga_ball", pin.fpga?.ball],
  81. ["board_signal", pin.fpga?.board_signal],
  82. ["capability", capabilities(pin).join(" ")],
  83. ["mcu_device", pin.mcu?.device],
  84. ["mcu_family", pin.mcu?.family],
  85. ["mcu_pin", pin.mcu?.device && pin.mcu?.pin ? `${pin.mcu.device}.${pin.mcu.pin}` : ""],
  86. ["mcu_signal", pin.mcu?.signal],
  87. ["mcu_capability", Array.isArray(pin.mcu?.capabilities) ? pin.mcu.capabilities.join(" ") : ""],
  88. ["path", Array.isArray(pin.path) ? pin.path.join(" ") : ""],
  89. ["programming_conclusion", programming.conclusion],
  90. ["network_explanation", programming.network_explanation],
  91. ["hdl_guidance", programming.hdl_guidance],
  92. ["connection_guidance", Array.isArray(programming.connection_guidance) ? programming.connection_guidance.join(" ") : ""],
  93. ["programming_constraints", Array.isArray(programming.constraints) ? programming.constraints.join(" ") : ""],
  94. ["programming_risks", Array.isArray(programming.risks) ? programming.risks.join(" ") : ""],
  95. ["peripheral_tag", Array.isArray(logical?.tags) ? logical.tags.join(" ") : ""],
  96. ["logical_pin", logical?.displayBadge],
  97. ["peripheral_role", logical?.peripheralRole],
  98. ["pin_definition", logical?.pinDefinition],
  99. ];
  100. }
  101. /**
  102. * Safe reference / identity match: exact, or prefix only when the next char is not a digit.
  103. * Prevents R12 ⊆ R1225 false positives.
  104. */
  105. export function identityMatch(candidate, query) {
  106. const value = normalized(candidate);
  107. const needle = normalized(query);
  108. if (!value || !needle) return 0;
  109. if (value === needle) return 3;
  110. if (value.startsWith(needle)) {
  111. const next = value.charAt(needle.length);
  112. if (!next || !/\d/.test(next)) return 2;
  113. return 0;
  114. }
  115. return 0;
  116. }
  117. function fieldScore(field, value, query, {allowIncludes = true} = {}) {
  118. const candidate = normalized(value);
  119. if (!candidate) return 0;
  120. if (field === "peripheral_tag") {
  121. const tokens = candidate.split(/\s+/).filter(Boolean);
  122. if (tokens.includes(query)) return 960;
  123. return 0;
  124. }
  125. const weights = {
  126. pin: 1200,
  127. reference: 1000,
  128. resistor: 1600,
  129. resistor_pad: 1500,
  130. value: 1400,
  131. fpga_ball: 900,
  132. mcu_pin: 900,
  133. mcu_signal: 840,
  134. mcu_device: 800,
  135. mcu_family: 780,
  136. mcu_capability: 720,
  137. net: 850,
  138. board_signal: 820,
  139. function: 760,
  140. logical_pin: 740,
  141. capability: 720,
  142. peripheral_role: 710,
  143. pin_number: 620,
  144. pin_definition: 520,
  145. summary: 500,
  146. path: 460,
  147. programming_conclusion: 700,
  148. network_explanation: 610,
  149. hdl_guidance: 590,
  150. connection_guidance: 570,
  151. programming_constraints: 540,
  152. programming_risks: 480,
  153. external_level: 420,
  154. direction: 360,
  155. };
  156. const weight = weights[field] ?? 200;
  157. if (field === "reference" || field === "resistor" || field === "resistor_pad" || field === "pin") {
  158. const grade = identityMatch(candidate, query);
  159. if (grade === 3) return weight;
  160. if (grade === 2) return weight - 120;
  161. return 0;
  162. }
  163. if (field === "fpga_ball") {
  164. // Identifiers: exact / token boundary only — never bare includes.
  165. if (candidate === query) return weight;
  166. return 0;
  167. }
  168. if (candidate === query) return weight;
  169. if (candidate.startsWith(query)) return weight - 120;
  170. if (allowIncludes && candidate.includes(query)) return weight - 240;
  171. return 0;
  172. }
  173. function resultId(type, reference, pinNumber = null) {
  174. return pinNumber == null ? `${type}:${reference}` : `${type}:${reference}.${pinNumber}`;
  175. }
  176. export function buildBoardIndex(data) {
  177. const resistorByReference = new Map();
  178. const resistorPadById = new Map();
  179. const valueTokenIndex = new Map();
  180. const netMembers = new Map();
  181. const relatedByPinId = new Map();
  182. const pushNet = (net, member) => {
  183. const key = text(net);
  184. if (!key) return;
  185. if (!netMembers.has(key)) netMembers.set(key, []);
  186. netMembers.get(key).push(member);
  187. };
  188. for (const resistor of data?.resistors ?? []) {
  189. const reference = text(resistor.reference);
  190. resistorByReference.set(reference, resistor);
  191. resistorByReference.set(normalized(reference), resistor);
  192. for (const token of resistor.value_tokens ?? []) {
  193. const key = normalized(token);
  194. if (!key) continue;
  195. if (!valueTokenIndex.has(key)) valueTokenIndex.set(key, []);
  196. valueTokenIndex.get(key).push(resistor);
  197. }
  198. for (const pad of resistor.pads ?? []) {
  199. const padId = text(pad.id) || `${reference}.pad-${pad.number}`;
  200. resistorPadById.set(padId, {resistor, pad});
  201. resistorPadById.set(`${reference}.${pad.number}`, {resistor, pad});
  202. pushNet(pad.net, {
  203. kind: "resistor_pad",
  204. reference,
  205. pinNumber: text(pad.number),
  206. touchpoint: null,
  207. resistor,
  208. pin: pad,
  209. });
  210. }
  211. }
  212. for (const touchpoint of data?.touchpoints ?? []) {
  213. for (const pin of touchpoint.pins ?? []) {
  214. pushNet(pin.net, {
  215. kind: "touchpoint",
  216. reference: touchpoint.reference,
  217. pinNumber: text(pin.number),
  218. touchpoint,
  219. resistor: null,
  220. pin,
  221. });
  222. if (Array.isArray(pin.related_resistors) && pin.related_resistors.length) {
  223. relatedByPinId.set(text(pin.id), pin.related_resistors);
  224. }
  225. }
  226. }
  227. for (const members of netMembers.values()) {
  228. members.sort((left, right) => {
  229. const referenceOrder = naturalCompare(left.reference, right.reference);
  230. return referenceOrder || naturalCompare(left.pinNumber, right.pinNumber);
  231. });
  232. }
  233. return {
  234. resistorByReference,
  235. resistorPadById,
  236. valueTokenIndex,
  237. netMembers,
  238. relatedByPinId,
  239. evidenceConflicts: Array.isArray(data?.evidence_conflicts) ? data.evidence_conflicts : [],
  240. };
  241. }
  242. function looksLikeResistanceToken(raw) {
  243. const needle = text(raw).trim();
  244. if (!needle) return false;
  245. if (PACKAGE_OR_DNP.test(needle)) return false;
  246. if (!VALUE_GRAMMAR.test(needle)) return false;
  247. if (/^\d{1,2}$/.test(needle)) return false; // short bare numbers need value: or 22R
  248. // Bare package-sized integers like 0805 already rejected; reject 4-digit bare
  249. // numbers that look like imperial package codes when no unit suffix present.
  250. if (/^\d{4}$/.test(needle) && !/[rRkKmM]|ohm|Ω/i.test(needle)) return false;
  251. return true;
  252. }
  253. /** Resolve exact net name from board index; case-sensitive first, then CI. */
  254. export function resolveExactNetName(data, query) {
  255. const raw = text(query).trim();
  256. if (!raw) return null;
  257. const index = buildBoardIndex(data);
  258. if (index.netMembers.has(raw)) return raw;
  259. const needle = normalized(raw);
  260. for (const name of index.netMembers.keys()) {
  261. if (normalized(name) === needle) return name;
  262. }
  263. return null;
  264. }
  265. export function parseBoardQuery(query) {
  266. const raw = text(query).trim();
  267. const needle = normalized(raw);
  268. if (!needle) return {kind: "empty", raw, needle};
  269. const ball = BALL_PREFIX.exec(raw);
  270. if (ball) return {kind: "ball", raw, needle: normalized(ball[1]), value: ball[1].trim()};
  271. const net = NET_PREFIX.exec(raw);
  272. if (net) return {kind: "net", raw, needle: normalized(net[1]), value: net[1].trim()};
  273. const value = VALUE_PREFIX.exec(raw);
  274. if (value) return {kind: "value", raw, needle: normalized(value[1]), value: value[1].trim()};
  275. const pad = RESISTOR_PAD.exec(needle);
  276. if (pad) {
  277. return {
  278. kind: "resistor_pad",
  279. raw,
  280. needle,
  281. reference: `R${pad[1]}`,
  282. padNumber: pad[2],
  283. };
  284. }
  285. const ref = RESISTOR_REF.exec(needle);
  286. if (ref) {
  287. return {
  288. kind: "resistor_ref",
  289. raw,
  290. needle,
  291. reference: `R${ref[1]}`,
  292. };
  293. }
  294. if (ENDPOINT.test(needle)) {
  295. return {kind: "endpoint", raw, needle, value: raw};
  296. }
  297. if (looksLikeResistanceToken(raw)) {
  298. return {kind: "value", raw, needle, value: raw};
  299. }
  300. return {kind: "text", raw, needle};
  301. }
  302. function sortResults(results) {
  303. const typeOrder = {
  304. resistor: 0,
  305. resistor_pad: 1,
  306. evidence_conflict: 2,
  307. touchpoint: 3,
  308. pin: 4,
  309. };
  310. return results.sort((left, right) => {
  311. if (right.score !== left.score) return right.score - left.score;
  312. const leftType = typeOrder[left.type] ?? 9;
  313. const rightType = typeOrder[right.type] ?? 9;
  314. if (leftType !== rightType) return leftType - rightType;
  315. const referenceOrder = naturalCompare(left.reference, right.reference);
  316. if (referenceOrder) return referenceOrder;
  317. return naturalCompare(left.pinNumber, right.pinNumber);
  318. });
  319. }
  320. function searchTouchpoints(data, needle, {ballOnly = false, endpoint = null} = {}) {
  321. const results = [];
  322. const boardVersion = normalizeBoardVersion(null, data);
  323. for (const touchpoint of data?.touchpoints ?? []) {
  324. if (!ballOnly && !endpoint) {
  325. const touchpointFields = [
  326. ["reference", touchpoint.reference],
  327. ["summary", touchpoint.summary],
  328. ["function", touchpoint.category],
  329. ];
  330. let touchpointScore = 0;
  331. let touchpointField = "";
  332. for (const [field, value] of touchpointFields) {
  333. const score = fieldScore(field, value, needle);
  334. if (score > touchpointScore) {
  335. touchpointScore = score;
  336. touchpointField = field;
  337. }
  338. }
  339. if (touchpointScore) {
  340. results.push({
  341. id: resultId("touchpoint", touchpoint.reference),
  342. type: "touchpoint",
  343. reference: touchpoint.reference,
  344. pinNumber: null,
  345. touchpoint,
  346. resistor: null,
  347. pin: null,
  348. matchedField: touchpointField,
  349. score: touchpointScore + 20,
  350. });
  351. }
  352. }
  353. for (const pin of touchpoint.pins ?? []) {
  354. let score = 0;
  355. let matchedField = "";
  356. if (ballOnly) {
  357. const ballScore = fieldScore("fpga_ball", pin.fpga?.ball, needle, {allowIncludes: false});
  358. if (!ballScore) continue;
  359. score = ballScore;
  360. matchedField = "fpga_ball";
  361. } else if (endpoint) {
  362. const endpointNeedle = normalized(endpoint);
  363. const candidates = [
  364. ["fpga_ball", `u3.${text(pin.fpga?.ball)}`],
  365. ["path", Array.isArray(pin.path) ? pin.path.join(" ") : ""],
  366. ["board_signal", pin.fpga?.board_signal],
  367. ["pin", `${touchpoint.reference}.${pin.number}`],
  368. ];
  369. for (const [field, value] of candidates) {
  370. const candidate = normalized(value);
  371. if (!candidate) continue;
  372. if (candidate === endpointNeedle || candidate.split(/[^a-z0-9_.]+/).includes(endpointNeedle)) {
  373. const candidateScore = field === "fpga_ball" ? 950 : 700;
  374. if (candidateScore > score) {
  375. score = candidateScore;
  376. matchedField = field;
  377. }
  378. }
  379. }
  380. if (!score) continue;
  381. } else {
  382. for (const [field, value] of pinSearchFields(touchpoint, pin, boardVersion)) {
  383. const candidateScore = fieldScore(field, value, needle, {
  384. allowIncludes: field !== "fpga_ball" && field !== "reference" && field !== "pin",
  385. });
  386. if (candidateScore > score) {
  387. score = candidateScore;
  388. matchedField = field;
  389. }
  390. }
  391. if (!score) continue;
  392. if (baseBlockers(pin).length === 0) score += 40;
  393. }
  394. results.push({
  395. id: resultId("pin", touchpoint.reference, text(pin.number)),
  396. type: "pin",
  397. reference: touchpoint.reference,
  398. pinNumber: text(pin.number),
  399. touchpoint,
  400. resistor: null,
  401. pin,
  402. matchedField,
  403. score,
  404. });
  405. }
  406. }
  407. return results;
  408. }
  409. function pushNetMemberResults(results, members, score = 1400) {
  410. for (const member of members) {
  411. const isResistor = member.kind === "resistor_pad";
  412. results.push({
  413. id: resultId(isResistor ? "resistor_pad" : "pin", member.reference, member.pinNumber),
  414. type: isResistor ? "resistor_pad" : "pin",
  415. reference: member.reference,
  416. pinNumber: member.pinNumber,
  417. touchpoint: member.touchpoint,
  418. resistor: member.resistor,
  419. pin: member.pin,
  420. matchedField: "net",
  421. score,
  422. assemblyStatus: member.resistor?.assembly_status || null,
  423. });
  424. }
  425. }
  426. export function searchBoard(data, query) {
  427. let parsed = parseBoardQuery(query);
  428. if (parsed.kind === "empty") return [];
  429. const index = buildBoardIndex(data);
  430. let results = [];
  431. // Bare string that exactly equals an existing net → exact net query (same as net:).
  432. if (parsed.kind === "text") {
  433. const exactNet = resolveExactNetName(data, parsed.raw);
  434. if (exactNet) {
  435. parsed = {kind: "net", raw: parsed.raw, needle: normalized(exactNet), value: exactNet};
  436. }
  437. }
  438. if (parsed.kind === "resistor_pad") {
  439. const hit = index.resistorPadById.get(`${parsed.reference}.${parsed.padNumber}`);
  440. if (hit) {
  441. results.push({
  442. id: resultId("resistor_pad", parsed.reference, parsed.padNumber),
  443. type: "resistor_pad",
  444. reference: parsed.reference,
  445. pinNumber: parsed.padNumber,
  446. touchpoint: null,
  447. resistor: hit.resistor,
  448. pin: hit.pad,
  449. matchedField: "resistor_pad",
  450. score: 1550,
  451. });
  452. }
  453. return sortResults(results);
  454. }
  455. if (parsed.kind === "resistor_ref") {
  456. const resistor = index.resistorByReference.get(parsed.reference)
  457. || index.resistorByReference.get(normalized(parsed.reference));
  458. if (resistor) {
  459. results.push({
  460. id: resultId("resistor", parsed.reference),
  461. type: "resistor",
  462. reference: parsed.reference,
  463. pinNumber: null,
  464. touchpoint: null,
  465. resistor,
  466. pin: null,
  467. matchedField: "resistor",
  468. score: 1600,
  469. });
  470. }
  471. return sortResults(results);
  472. }
  473. if (parsed.kind === "ball") {
  474. return sortResults(searchTouchpoints(data, parsed.needle, {ballOnly: true}));
  475. }
  476. if (parsed.kind === "endpoint") {
  477. return sortResults(searchTouchpoints(data, parsed.needle, {endpoint: parsed.value}));
  478. }
  479. if (parsed.kind === "net") {
  480. const members =
  481. index.netMembers.get(text(parsed.value))
  482. || index.netMembers.get(parsed.value)
  483. || [];
  484. pushNetMemberResults(results, members, 1400);
  485. if (!results.length) {
  486. for (const [net, rows] of index.netMembers.entries()) {
  487. if (normalized(net) !== parsed.needle) continue;
  488. pushNetMemberResults(results, rows, 1400);
  489. }
  490. }
  491. return sortResults(results);
  492. }
  493. if (parsed.kind === "value") {
  494. const seen = new Set();
  495. const addResistor = (resistor, score = 1400) => {
  496. const reference = text(resistor.reference);
  497. if (seen.has(reference)) return;
  498. seen.add(reference);
  499. const notFitted = resistor.assembly_status === "not_fitted";
  500. results.push({
  501. id: resultId("resistor", reference),
  502. type: "resistor",
  503. reference,
  504. pinNumber: null,
  505. touchpoint: null,
  506. resistor,
  507. pin: null,
  508. matchedField: "value",
  509. score: notFitted ? score - 200 : score,
  510. assemblyStatus: resistor.assembly_status || "unknown",
  511. notFitted,
  512. });
  513. };
  514. for (const resistor of index.valueTokenIndex.get(parsed.needle) ?? []) {
  515. addResistor(resistor, 1450);
  516. }
  517. for (const resistor of data?.resistors ?? []) {
  518. for (const token of resistor.value_tokens ?? []) {
  519. if (normalized(token) === parsed.needle) addResistor(resistor, 1450);
  520. }
  521. if (normalized(resistor.component_value) === parsed.needle) addResistor(resistor, 1400);
  522. }
  523. return sortResults(results);
  524. }
  525. // Free-text (non-exact-net): touchpoints/pins; value tokens; never bare R# here.
  526. results = searchTouchpoints(data, parsed.needle);
  527. for (const resistor of index.valueTokenIndex.get(parsed.needle) ?? []) {
  528. results.push({
  529. id: resultId("resistor", resistor.reference),
  530. type: "resistor",
  531. reference: resistor.reference,
  532. pinNumber: null,
  533. touchpoint: null,
  534. resistor,
  535. pin: null,
  536. matchedField: "value",
  537. score: resistor.assembly_status === "not_fitted" ? 1100 : 1300,
  538. assemblyStatus: resistor.assembly_status || "unknown",
  539. notFitted: resistor.assembly_status === "not_fitted",
  540. });
  541. }
  542. for (const conflict of index.evidenceConflicts) {
  543. const hay = normalized([
  544. conflict.subject_reference,
  545. conflict.summary,
  546. ...(conflict.claims || []).map((claim) => JSON.stringify(claim)),
  547. ].join(" "));
  548. if (hay.includes(parsed.needle) && conflict.status === "unresolved") {
  549. results.push({
  550. id: resultId("evidence_conflict", conflict.subject_reference || conflict.id),
  551. type: "evidence_conflict",
  552. reference: conflict.subject_reference || conflict.id,
  553. pinNumber: null,
  554. touchpoint: null,
  555. resistor: index.resistorByReference.get(conflict.subject_reference) || null,
  556. pin: null,
  557. matchedField: "evidence_conflict",
  558. score: 200,
  559. evidenceConflict: conflict,
  560. });
  561. }
  562. }
  563. return sortResults(results);
  564. }
  565. /** Compatibility: touchpoint pins only (does not include resistor pads). */
  566. export function sameNetPins(data, net) {
  567. const wanted = text(net);
  568. if (!wanted) return [];
  569. const matches = [];
  570. for (const touchpoint of data?.touchpoints ?? []) {
  571. for (const pin of touchpoint.pins ?? []) {
  572. if (pin.net === wanted) {
  573. matches.push({
  574. kind: "touchpoint",
  575. reference: touchpoint.reference,
  576. pinNumber: text(pin.number),
  577. touchpoint,
  578. resistor: null,
  579. pin,
  580. });
  581. }
  582. }
  583. }
  584. return matches.sort((left, right) => {
  585. const referenceOrder = naturalCompare(left.reference, right.reference);
  586. return referenceOrder || naturalCompare(left.pinNumber, right.pinNumber);
  587. });
  588. }
  589. /** Touchpoint pins + resistor pads sharing the PCB net. */
  590. export function sameNetMembers(data, net) {
  591. const wanted = text(net);
  592. if (!wanted) return [];
  593. const index = buildBoardIndex(data);
  594. const members = index.netMembers.get(wanted) || [];
  595. if (members.length) return members.map((item) => ({...item}));
  596. // Case-insensitive fallback
  597. for (const [name, rows] of index.netMembers.entries()) {
  598. if (normalized(name) === normalized(wanted)) {
  599. return rows.map((item) => ({...item}));
  600. }
  601. }
  602. return [];
  603. }
  604. export function extractPathResistors(data, ...texts) {
  605. const index = new Map((data?.resistors ?? []).map((item) => [text(item.reference), item]));
  606. const ordered = [];
  607. const seen = new Set();
  608. for (const value of texts) {
  609. const chunks = Array.isArray(value) ? value : [value];
  610. for (const chunk of chunks) {
  611. const source = text(chunk);
  612. if (!source) continue;
  613. ANNOTATION_RESISTOR.lastIndex = 0;
  614. let match;
  615. while ((match = ANNOTATION_RESISTOR.exec(source))) {
  616. const reference = `R${match[1]}`;
  617. if (seen.has(reference)) continue;
  618. seen.add(reference);
  619. const resistor = index.get(reference);
  620. if (!resistor) {
  621. throw new Error(`annotation resistor ${reference} missing from resistor index`);
  622. }
  623. ordered.push(resistor);
  624. }
  625. }
  626. }
  627. return ordered;
  628. }
  629. export function findCandidates(data, capability) {
  630. const wanted = normalized(capability);
  631. if (!wanted) return [];
  632. const acceptedCapabilities = new Set(
  633. (CAPABILITY_ALIASES[wanted] ?? [wanted]).map(normalized),
  634. );
  635. const candidates = [];
  636. for (const touchpoint of data?.touchpoints ?? []) {
  637. for (const pin of touchpoint.pins ?? []) {
  638. const pinCapabilities = capabilities(pin).map(normalized);
  639. if (!pinCapabilities.some((item) => acceptedCapabilities.has(item))) continue;
  640. if (wanted === "gpio" && !["gpio", "direct_gpio"].includes(normalized(pin.function_category))) {
  641. continue;
  642. }
  643. if (baseBlockers(pin).length) continue;
  644. const purpose = text(pin.summary).trim();
  645. const polarity = normalized(pin.polarity);
  646. const details = [];
  647. if (purpose) details.push(`用途 ${purpose}`);
  648. if (polarity && polarity !== "unknown") details.push(`极性 ${pin.polarity}`);
  649. candidates.push({
  650. reference: touchpoint.reference,
  651. touchpoint,
  652. pin,
  653. reason: `${details.length ? `${details.join(";")};` : ""}能力 ${capability} 匹配;已验证 FPGA 路径至 ${pin.fpga.ball};外部电平 ${pin.external_level}。`,
  654. warning: "只证明物理可用性,仍需检查当前工程约束冲突。",
  655. });
  656. }
  657. }
  658. return candidates.sort((left, right) => {
  659. const categoryMatchLeft = normalized(left.pin.function_category) === wanted ? 1 : 0;
  660. const categoryMatchRight = normalized(right.pin.function_category) === wanted ? 1 : 0;
  661. if (categoryMatchRight !== categoryMatchLeft) return categoryMatchRight - categoryMatchLeft;
  662. const referenceOrder = naturalCompare(left.reference, right.reference);
  663. return referenceOrder || naturalCompare(left.pin.number, right.pin.number);
  664. });
  665. }
  666. export function isValidRtlPortName(name) {
  667. return RTL_PORT.test(text(name));
  668. }
  669. /** Back SVG export uses KiCad --mirror about the vertical/Y axis (left-right flip). */
  670. export const BACK_MIRROR_AXIS = "y";
  671. export function mirrorBoardX(x, boardWidth) {
  672. const width = Number(boardWidth);
  673. const value = Number(x);
  674. if (!Number.isFinite(width) || !Number.isFinite(value)) return value;
  675. return width - value;
  676. }
  677. export function displayPoint(point, boardWidth, viewingSide) {
  678. const x = Number(point?.x);
  679. const y = Number(point?.y);
  680. if (viewingSide !== "back") return {x, y};
  681. return {x: mirrorBoardX(x, boardWidth), y};
  682. }
  683. export function displayBounds(bounds, boardWidth, viewingSide) {
  684. const x = Number(bounds?.x);
  685. const y = Number(bounds?.y);
  686. const width = Number(bounds?.width);
  687. const height = Number(bounds?.height);
  688. if (viewingSide !== "back") {
  689. return {x, y, width, height};
  690. }
  691. return {
  692. x: mirrorBoardX(x + width, boardWidth),
  693. y,
  694. width,
  695. height,
  696. };
  697. }
  698. export function itemVisibleOnSide(item, viewingSide) {
  699. return (item?.side || "front") === viewingSide;
  700. }
  701. export function generateAdcSnippet(pin, rtlPortName, runtimeStatus = {}) {
  702. if (runtimeStatus.adcCopyEnabled !== true) {
  703. throw new Error("源数据已过期,ADC 复制已关闭。");
  704. }
  705. if (!isValidRtlPortName(rtlPortName)) {
  706. throw new Error("生成 ADC 前请输入合法的 RTL 端口名。");
  707. }
  708. if (baseBlockers(pin).length) {
  709. throw new Error("此针脚不具备 ADC 生成资格。");
  710. }
  711. const ball = text(pin.fpga.ball);
  712. const ioStandard = text(pin.fpga.io_standard);
  713. if (!/^[A-Z]+[0-9]+$/.test(ball) || !/^[A-Za-z0-9_.+-]+$/.test(ioStandard)) {
  714. throw new Error("已验证 FPGA 分配包含不安全的 ADC 值。");
  715. }
  716. const assignments = [`LOCATION = ${ball}`, `IOSTANDARD = ${ioStandard}`];
  717. if (pin.adc_pulltype != null) {
  718. const pulltype = text(pin.adc_pulltype);
  719. if (!/^[A-Z][A-Z0-9_]*$/.test(pulltype)) {
  720. throw new Error("人工注释的 PULLTYPE 包含不安全值。");
  721. }
  722. assignments.push(`PULLTYPE = ${pulltype}`);
  723. }
  724. return `set_pin_assignment { ${rtlPortName} } { ${assignments.join("; ")}; }`;
  725. }
  726. export const PERIPHERAL_TYPE_ORDER = [
  727. "ALL",
  728. "PTC",
  729. "FAN",
  730. "UART",
  731. "JTAG",
  732. "MOTOR",
  733. "LIMIT",
  734. "TEMP",
  735. "SSR",
  736. "GPIO",
  737. "MCU",
  738. "POWER",
  739. ];
  740. export const PERIPHERAL_TYPE_LABELS = {
  741. ALL: "全部外设",
  742. PTC: "PTC 加热",
  743. FAN: "FAN 风扇",
  744. UART: "UART 串口",
  745. JTAG: "JTAG / SWD",
  746. MOTOR: "步进电机",
  747. LIMIT: "限位开关",
  748. TEMP: "温度探头",
  749. SSR: "SSR 继电器",
  750. GPIO: "GPIO 扩展",
  751. MCU: "MCU 接口",
  752. POWER: "电源 / 测点",
  753. };
  754. const V12_V11_PTC_CONNECTOR_MAP = {
  755. "CON18.1": {tag: "PTC1", idx: 0, ball: "F16", heat: "HEAT1", v11Role: "PTC1 (通道0)"},
  756. "CON18.3": {tag: "PTC2", idx: 1, ball: "G16", heat: "HEAT2", v11Role: "PTC2 (通道1)"},
  757. "CON18.5": {tag: "PTC3", idx: 2, ball: "T4", heat: "HEAT3", v11Role: "PTC3 (通道2)"},
  758. "CON18.7": {tag: "PTC4", idx: 3, ball: "R5", heat: "HEAT4", v11Role: "PTC4 (通道3)"},
  759. "CON18.9": {tag: "PTC5", idx: 4, ball: "T6", heat: "HEAT5", v11Role: "PTC5 (通道4)"},
  760. "CON18.11": {tag: "PTC6", idx: 5, ball: "R7", heat: "HEAT6", v11Role: "PTC6 (通道5)"},
  761. "CON18.13": {tag: "PTC6", idx: 5, ball: "R7", heat: "HEAT6", v11Role: "PTC6 (通道5 并联针)"},
  762. "CON27.1": {tag: "PTC7", idx: 6, ball: "M1", heat: "HEAT7", v11Role: "PTC7 (通道6)"},
  763. "CON27.3": {tag: "PTC8", idx: 7, ball: "N1", heat: "HEAT8", v11Role: "PTC8 (通道7)"},
  764. "CON27.5": {tag: "PTC9", idx: 8, ball: "P1", heat: "HEAT9", v11Role: "PTC9 (通道8)"},
  765. "CON27.7": {tag: "PTC10", idx: 9, ball: "R1", heat: "HEAT10", v11Role: "PTC10 (通道9)"},
  766. "CON27.9": {tag: "PTC11", idx: 10, ball: "T2", heat: "HEAT11", v11Role: "PTC11 (通道10)"},
  767. "CON27.11": {tag: "PTC12", idx: 11, ball: "R3", heat: "HEAT12", v11Role: "PTC12 (通道11)"},
  768. "CON27.13": {tag: "PTC12", idx: 11, ball: "R3", heat: "HEAT12", v11Role: "PTC12 (通道11 并联针)"},
  769. "CON28.1": {tag: "PTC13", idx: 12, ball: "P16", heat: "HEAT13", v11Role: "PTC13 (通道12)"},
  770. "CON28.3": {tag: "PTC14", idx: 13, ball: "R16", heat: "HEAT14", v11Role: "PTC14 (通道13)"},
  771. "CON28.5": {tag: "PTC15", idx: 14, ball: "M16", heat: "HEAT15", v11Role: "PTC15 (通道14)"},
  772. "CON28.7": {tag: "PTC16", idx: 15, ball: "N16", heat: "HEAT16", v11Role: "PTC16 (通道15)"},
  773. "CON28.9": {tag: "PTC17", idx: 16, ball: "P8", heat: "HEAT17", v11Role: "PTC17 (通道16)"},
  774. "CON28.11": {tag: "PTC18", idx: 17, ball: "P7", heat: "HEAT18", v11Role: "PTC18 (通道17)"},
  775. "CON28.13": {tag: "PTC18", idx: 17, ball: "P7", heat: "HEAT18", v11Role: "PTC18 (通道17 并联针)"},
  776. "CON29.1": {tag: "PTC19", idx: 18, ball: "T9", heat: "HEAT19", v11Role: "PTC19 (通道18)"},
  777. "CON29.3": {tag: "PTC20", idx: 19, ball: "P9", heat: "HEAT20", v11Role: "PTC20 (通道19)"},
  778. "CON29.5": {tag: "PTC21", idx: 20, ball: "P10", heat: "HEAT21", v11Role: "PTC21 (通道20)"},
  779. "CON29.7": {tag: "PTC22", idx: 21, ball: "R10", heat: "HEAT22", v11Role: "PTC22 (通道21)"},
  780. "CON29.9": {tag: "PTC23", idx: 22, ball: "T13", heat: "HEAT23", v11Role: "PTC23 (通道22)"},
  781. "CON29.11": {tag: "PTC24", idx: 23, ball: "T12", heat: "HEAT24", v11Role: "PTC24 (通道23)"},
  782. "CON29.13": {tag: "PTC24", idx: 23, ball: "T12", heat: "HEAT24", v11Role: "PTC24 (通道23 并联针)"},
  783. };
  784. const V20_PTC_CONNECTOR_MAP = {
  785. "CON147.1": {tag: "PTC1", idx: 0, ball: "F16", net: "PTC1", role: "1#腔 PTC1 (通道0)"},
  786. "CON147.3": {tag: "PTC2", idx: 1, ball: "G16", net: "PTC2", role: "1#腔 PTC2 (通道1)"},
  787. "CON147.5": {tag: "PTC3", idx: 2, ball: "T4", net: "PTC3", role: "1#腔 PTC3 (通道2)"},
  788. "CON147.7": {tag: "PTC4", idx: 3, ball: "R5", net: "PTC4", role: "1#腔 PTC4 (通道3)"},
  789. "CON147.9": {tag: "PTC5", idx: 4, ball: "T6", net: "PTC5", role: "1#腔 PTC5 (通道4)"},
  790. "CON147.11": {tag: "PTC6", idx: 5, ball: "R7", net: "PTC6", role: "1#腔 PTC6 (通道5)"},
  791. "CON147.13": {tag: "PTC7_SPARE", idx: 26, ball: "M1", net: "PTC7", role: "1#腔预留 PTC7 (U3.M1 空余)"},
  792. "CON147.15": {tag: "PTC8_SPARE", idx: 27, ball: "N1", net: "PTC8", role: "1#腔预留 PTC8 (U3.N1 空余)"},
  793. "CON147.17": {tag: "PTC9_SPARE", idx: 28, ball: "P1", net: "PTC9", role: "1#腔预留 PTC9 (U3.P1 空余)"},
  794. "CON147.19": {tag: "PTC10_SPARE", idx: 29, ball: "R1", net: "PTC10", role: "1#腔预留 PTC10 (U3.R1 空余)"},
  795. "CON148.1": {tag: "PTC7", altTag: "PTC11", idx: 6, ball: "T2", net: "PTC11", role: "2#腔 PTC11 (逻辑PTC7 / 通道6)"},
  796. "CON148.3": {tag: "PTC8", altTag: "PTC12", idx: 7, ball: "R3", net: "PTC12", role: "2#腔 PTC12 (逻辑PTC8 / 通道7)"},
  797. "CON148.5": {tag: "PTC9", altTag: "PTC13", idx: 8, ball: "P16", net: "PTC13", role: "2#腔 PTC13 (逻辑PTC9 / 通道8)"},
  798. "CON148.7": {tag: "PTC10", altTag: "PTC14", idx: 9, ball: "R16", net: "PTC14", role: "2#腔 PTC14 (逻辑PTC10 / 通道9)"},
  799. "CON148.9": {tag: "PTC11", altTag: "PTC15", idx: 10, ball: "M16", net: "PTC15", role: "2#腔 PTC15 (逻辑PTC11 / 通道10)"},
  800. "CON148.11": {tag: "PTC12", altTag: "PTC16", idx: 11, ball: "N16", net: "PTC16", role: "2#腔 PTC16 (逻辑PTC12 / 通道11)"},
  801. "CON148.13": {tag: "PTC17_SPARE", idx: 30, ball: "P8", net: "PTC17", role: "2#腔预留 PTC17 (U3.P8 空余)"},
  802. "CON148.15": {tag: "PTC18_SPARE", idx: 31, ball: "P7", net: "PTC18", role: "2#腔预留 PTC18 (U3.P7 空余)"},
  803. "CON148.17": {tag: "PTC19_SPARE", idx: 32, ball: "T9", net: "PTC19", role: "2#腔预留 PTC19 (U3.T9 空余)"},
  804. "CON148.19": {tag: "PTC20_SPARE", idx: 33, ball: "P9", net: "PTC20", role: "2#腔预留 PTC20 (U3.P9 空余)"},
  805. "CON149.1": {tag: "PTC13", altTag: "PTC21", idx: 12, ball: "P10", net: "PTC21", role: "3#腔 PTC21 (逻辑PTC13 / 通道12)"},
  806. "CON149.3": {tag: "PTC14", altTag: "PTC22", idx: 13, ball: "R10", net: "PTC22", role: "3#腔 PTC22 (逻辑PTC14 / 通道13)"},
  807. "CON149.5": {tag: "PTC15", altTag: "PTC23", idx: 14, ball: "T13", net: "PTC23", role: "3#腔 PTC23 (逻辑PTC15 / 通道14)"},
  808. "CON149.7": {tag: "PTC16", altTag: "PTC24", idx: 15, ball: "T12", net: "PTC24", role: "3#腔 PTC24 (逻辑PTC16 / 通道15)"},
  809. "CON149.9": {tag: "PTC17", altTag: "PTC25", idx: 16, ball: "L3", net: "PTC25", role: "3#腔 PTC25 (逻辑PTC17 / 通道16)"},
  810. "CON149.11": {tag: "PTC18", altTag: "PTC26", idx: 17, ball: "N2", net: "PTC26", role: "3#腔 PTC26 (逻辑PTC18 / 通道17)"},
  811. "CON149.13": {tag: "PTC27_SPARE", idx: 34, ball: "P2", net: "PTC27", role: "预留 PTC27(当前固件未绑定)"},
  812. "CON149.15": {tag: "PTC28_SPARE", idx: 35, ball: "M3", net: "PTC28", role: "3#腔预留 PTC28 (U3.M3 空余)"},
  813. "CON149.17": {tag: "PTC29_SPARE", idx: 36, ball: "L5", net: "PTC29", role: "3#腔预留 PTC29 (U3.L5 空余)"},
  814. "CON149.19": {tag: "PTC19", altTag: "PTC30", idx: 18, ball: "K5", net: "PTC30", role: "4#腔 PTC30(通道18)"},
  815. "CON150.1": {tag: "PTC20", altTag: "PTC31", idx: 19, ball: "L4", net: "PTC31", role: "4#腔 PTC31(通道19)"},
  816. "CON150.3": {tag: "PTC20", altTag: "PTC31", idx: 19, ball: "L4", net: "PTC31", role: "4#腔 PTC31(通道19)"},
  817. "CON150.5": {tag: "PTC21", altTag: "PTC32", idx: 20, ball: "H4", net: "PTC32", role: "4#腔 PTC32(通道20)"},
  818. "CON150.7": {tag: "PTC21", altTag: "PTC32", idx: 20, ball: "H4", net: "PTC32", role: "4#腔 PTC32(通道20)"},
  819. "CON150.9": {tag: "PTC22", altTag: "PTC33", idx: 21, ball: "J6", net: "PTC33", role: "4#腔 PTC33(通道21)"},
  820. "CON150.11": {tag: "PTC22", altTag: "PTC33", idx: 21, ball: "J6", net: "PTC33", role: "4#腔 PTC33(通道21)"},
  821. "CON150.13": {tag: "PTC23", altTag: "PTC34", idx: 22, ball: "H5", net: "PTC34", role: "4#腔 PTC34(通道22)"},
  822. "CON150.15": {tag: "PTC23", altTag: "PTC34", idx: 22, ball: "H5", net: "PTC34", role: "4#腔 PTC34(通道22)"},
  823. "CON150.17": {tag: "PTC24", altTag: "PTC35", idx: 23, ball: "C15", net: "PTC35", role: "4#腔 PTC35(通道23)"},
  824. "CON150.19": {tag: "PTC24", altTag: "PTC35", idx: 23, ball: "C15", net: "PTC35", role: "4#腔 PTC35(通道23)"},
  825. };
  826. const V12_MOTOR_MAP = {
  827. "CON104": {"axis": 0, "tag": "M1", "stepPin": "2", "stepBall": "C12", "dirPin": "3", "dirBall": "B12", "stepNet": "PUL1", "dirNet": "DIR1"},
  828. "CON105": {"axis": 1, "tag": "M2", "stepPin": "2", "stepBall": "L12", "dirPin": "3", "dirBall": "J11", "stepNet": "PUL2", "dirNet": "DIR2"},
  829. "CON106": {"axis": 2, "tag": "M3", "stepPin": "2", "stepBall": "L14", "dirPin": "3", "dirBall": "K14", "stepNet": "PUL3", "dirNet": "DIR3"},
  830. "CON113": {"axis": 3, "tag": "M4", "stepPin": "2", "stepBall": "G14", "dirPin": "3", "dirBall": "G15", "stepNet": "PUL10", "dirNet": "DIR10"},
  831. "CON107": {"axis": 4, "tag": "M5", "stepPin": "2", "stepBall": "L13", "dirPin": "3", "dirBall": "K11", "stepNet": "PUL4", "dirNet": "DIR4"},
  832. "CON108": {"axis": 5, "tag": "M6", "stepPin": "2", "stepBall": "K12", "dirPin": "3", "dirBall": "K13", "stepNet": "PUL5", "dirNet": "DIR5"},
  833. "CON109": {"axis": 6, "tag": "M7", "stepPin": "2", "stepBall": "J14", "dirPin": "3", "dirBall": "J12", "stepNet": "PUL6", "dirNet": "DIR6"},
  834. "CON114": {"axis": 7, "tag": "M8", "stepPin": "2", "stepBall": "G13", "dirPin": "3", "dirBall": "F14", "stepNet": "PUL11", "dirNet": "DIR11"},
  835. "CON110": {"axis": 8, "tag": "M9", "stepPin": "2", "stepBall": "H13", "dirPin": "3", "dirBall": "J13", "stepNet": "PUL7", "dirNet": "DIR7"},
  836. "CON111": {"axis": 9, "tag": "M10", "stepPin": "2", "stepBall": "H11", "dirPin": "3", "dirBall": "H12", "stepNet": "PUL8", "dirNet": "DIR8"},
  837. "CON112": {"axis": 10, "tag": "M11", "stepPin": "2", "stepBall": "G11", "dirPin": "3", "dirBall": "H14", "stepNet": "PUL9", "dirNet": "DIR9"},
  838. "CON115": {"axis": 11, "tag": "M12", "stepPin": "2", "stepBall": "F12", "dirPin": "3", "dirBall": "D15", "stepNet": "PUL12", "dirNet": "DIR12"},
  839. };
  840. const V12_LIMIT_MAP = {
  841. "CON12.1": {"axis": 0, "tag": "LS1", "rtl": "limit_lower_raw[0]", "ball": "A5", "role": "轴0(M1) 下限位 LS1"},
  842. "CON12.3": {"axis": 1, "tag": "LS2", "rtl": "limit_lower_raw[1]", "ball": "C8", "role": "轴1(M2) 下限位 LS2"},
  843. "CON126.1": {"axis": 2, "tag": "LS3", "rtl": "limit_lower_raw[2]", "ball": "D7", "role": "轴2(M3) 下限位 LS3"},
  844. "CON126.3": {"axis": 3, "tag": "LS4", "rtl": "limit_lower_raw[3]", "ball": "A4", "role": "轴3(M4) 下限位 LS4"},
  845. "CON127.1": {"axis": 4, "tag": "LS5", "rtl": "limit_lower_raw[4]", "ball": "A3", "role": "轴4(M5) 下限位 LS5"},
  846. "CON127.3": {"axis": 5, "tag": "LS6", "rtl": "limit_lower_raw[5]", "ball": "A12", "role": "轴5(M6) 下限位 LS6"},
  847. "CON128.1": {"axis": 6, "tag": "LS7", "rtl": "limit_lower_raw[6]", "ball": "L16", "role": "轴6(M7) 下限位 LS7"},
  848. "CON128.3": {"axis": 7, "tag": "LS8", "rtl": "limit_lower_raw[7]", "ball": "K15", "role": "轴7(M8) 下限位 LS8"},
  849. "CON129.1": {"axis": 8, "tag": "LS9", "rtl": "limit_lower_raw[8]", "ball": "K16", "role": "轴8(M9) 下限位 LS9"},
  850. "CON129.3": {"axis": 9, "tag": "LS10", "rtl": "limit_lower_raw[9]", "ball": "H15", "role": "轴9(M10) 下限位 LS10"},
  851. "CON138.1": {"axis": 10, "tag": "LS11", "rtl": "limit_lower_raw[10]", "ball": "T3", "role": "轴10(M11) 下限位 LS11"},
  852. "CON138.3": {"axis": 11, "tag": "LS12", "rtl": "limit_lower_raw[11]", "ball": "P4", "role": "轴11(M12) 下限位 LS12"},
  853. "CON12.2": {"axis": 0, "tag": "HS1", "rtl": "limit_upper_raw[0]", "ball": "B6", "role": "轴0(M1) 上限位 HS1"},
  854. "CON12.4": {"axis": 1, "tag": "HS2", "rtl": "limit_upper_raw[1]", "ball": "A8", "role": "轴1(M2) 上限位 HS2"},
  855. "CON126.2": {"axis": 2, "tag": "HS3", "rtl": "limit_upper_raw[2]", "ball": "E7", "role": "轴2(M3) 上限位 HS3"},
  856. "CON126.4": {"axis": 3, "tag": "HS4", "rtl": "limit_upper_raw[3]", "ball": "C5", "role": "轴3(M4) 上限位 HS4"},
  857. "CON127.2": {"axis": 4, "tag": "HS5", "rtl": "limit_upper_raw[4]", "ball": "B4", "role": "轴4(M5) 上限位 HS5"},
  858. "CON127.4": {"axis": 5, "tag": "HS6", "rtl": "limit_upper_raw[5]", "ball": "B11", "role": "轴5(M6) 上限位 HS6"},
  859. "CON128.2": {"axis": 6, "tag": "HS7", "rtl": "limit_upper_raw[6]", "ball": "L15", "role": "轴6(M7) 上限位 HS7"},
  860. "CON128.4": {"axis": 7, "tag": "HS8", "rtl": "limit_upper_raw[7]", "ball": "J16", "role": "轴7(M8) 上限位 HS8"},
  861. "CON129.2": {"axis": 8, "tag": "HS9", "rtl": "limit_upper_raw[8]", "ball": "J15", "role": "轴8(M9) 上限位 HS9"},
  862. "CON129.4": {"axis": 9, "tag": "HS10", "rtl": "limit_upper_raw[9]", "ball": "H16", "role": "轴9(M10) 上限位 HS10"},
  863. "CON138.2": {"axis": 10, "tag": "HS11", "rtl": "limit_upper_raw[10]", "ball": "R4", "role": "轴10(M11) 上限位 HS11"},
  864. "CON138.4": {"axis": 11, "tag": "HS12", "rtl": "limit_upper_raw[11]", "ball": "T5", "role": "轴11(M12) 上限位 HS12"},
  865. };
  866. function normalizeBoardVersion(boardVersion, data) {
  867. const v = text(boardVersion).trim().toLowerCase();
  868. if (v === "v2_0" || v === "v2_0_fan" || v === "v1_2" || v === "v1_1") return v;
  869. const id = `${text(data?.board?.id)} ${text(data?.board?.name)}`.toLowerCase();
  870. if (id.includes("fan")) return "v2_0_fan";
  871. if (id.includes("v2")) return "v2_0";
  872. if (id.includes("v1.2") || id.includes("v1_2") || id.includes("20260717")) return "v1_2";
  873. return "v1_1";
  874. }
  875. /**
  876. * Add reviewed project roles to board JSON facts. Inferred paths remain inferred; this is not a continuity measurement.
  877. */
  878. export function resolveLogicalPin(touchpoint, pin, boardVersion = "v2_0") {
  879. const result = describeLogicalPin(touchpoint, pin, boardVersion);
  880. // The connector net remains a board JSON fact, independent of project role labels.
  881. result.boardSignal = text(pin?.net).trim() || result.boardSignal;
  882. const nativeBall = text(pin?.fpga?.ball).trim();
  883. const describedBall = /^U3\.([A-Z]+[0-9]+)$/.exec(result.physicalPin || "")?.[1];
  884. if (nativeBall && describedBall && nativeBall !== describedBall) {
  885. return {
  886. ...result,
  887. physicalPin: `U3.${nativeBall}`,
  888. logicalSignal: "板数据与项目角色表冲突,未绑定角色",
  889. pinDefinition: `板数据标注 U3.${nativeBall};项目角色表预期 U3.${describedBall},请核对当前 ADC 与原生网表。`,
  890. displayBadge: `FPGA U3.${nativeBall} · 映射冲突`,
  891. isUsed: false,
  892. };
  893. }
  894. return result;
  895. }
  896. function describeLogicalPin(touchpoint, pin, boardVersion = "v2_0") {
  897. const ver = normalizeBoardVersion(boardVersion);
  898. const ref = text(touchpoint?.reference).trim();
  899. const pnum = text(pin?.number).trim();
  900. const key = `${ref}.${pnum}`;
  901. const ball = text(pin?.fpga?.ball).trim();
  902. const boardSig = text(pin?.fpga?.board_signal).trim();
  903. const mcuDev = text(pin?.mcu?.device).trim();
  904. const mcuPin = text(pin?.mcu?.pin).trim();
  905. const mcuSig = text(pin?.mcu?.signal).trim();
  906. const net = text(pin?.net).trim();
  907. // Mechanical / Shield pads
  908. if (pin?.mechanical === true || pin?.pad_role === "shield" || pin?.pad_role === "mechanical" || pnum.startsWith("SH")) {
  909. return {
  910. chip: "MECHANICAL",
  911. physicalPin: "SHIELD/GND",
  912. logicalSignal: "GND_SHIELD (外壳固定焊盘)",
  913. boardSignal: net || "GND",
  914. peripheralRole: "机械屏蔽固定脚",
  915. pinDefinition: text(pin?.summary) || `${key} 机械固定与屏蔽接地焊盘`,
  916. displayBadge: "非逻辑管脚 · 机械屏蔽/GND",
  917. tags: ["GND"],
  918. isUsed: false,
  919. };
  920. }
  921. // JTAG (J3 across V1.2 / V1.1 / V2.0)
  922. if (ref === "J3") {
  923. const j3Map = {
  924. "1": {ball: "A7", rtl: "jtag_tck", sig: "FPGA_TCK", def: "FPGA JTAG 时钟输入 (TCK, R63 串联)"},
  925. "3": {ball: "C6", rtl: "jtag_tdo", sig: "FPGA_TDO", def: "FPGA JTAG 数据输出 (TDO, R65 串联)"},
  926. "5": {ball: "B8", rtl: "jtag_tms", sig: "FPGA_TMS", def: "FPGA JTAG 模式选择 (TMS, R67 串联)"},
  927. "9": {ball: "A6", rtl: "jtag_tdi", sig: "FPGA_TDI", def: "FPGA JTAG 数据输入 (TDI, R69 串联)"},
  928. "4": {ball: "", rtl: "VREF_+3V3", sig: "+3V3_VREF", def: "JTAG 下载器目标电平参考 (+3.3V 经 R61/R62)"},
  929. "2": {ball: "", rtl: "GND", sig: "GND", def: "JTAG 信号参考地 (0V GND)"},
  930. "10": {ball: "", rtl: "GND", sig: "GND", def: "JTAG 信号参考地 (0V GND)"},
  931. }[pnum];
  932. if (j3Map?.ball) {
  933. return {
  934. chip: "FPGA U3",
  935. physicalPin: `U3.${j3Map.ball}`,
  936. logicalSignal: `${j3Map.rtl} (${j3Map.sig})`,
  937. boardSignal: j3Map.sig,
  938. peripheralRole: `JTAG ${j3Map.rtl.replace("jtag_", "").toUpperCase()}`,
  939. pinDefinition: j3Map.def,
  940. displayBadge: `FPGA U3.${j3Map.ball} · ${j3Map.rtl} (${j3Map.sig})`,
  941. tags: ["JTAG", j3Map.rtl.toUpperCase()],
  942. isUsed: true,
  943. };
  944. }
  945. return {
  946. chip: pnum === "4" ? "POWER" : (["2", "10"].includes(pnum) ? "GND" : "NC"),
  947. physicalPin: pnum === "4" ? "+3.3V" : (["2", "10"].includes(pnum) ? "GND" : "NC"),
  948. logicalSignal: j3Map ? j3Map.rtl : "NC (空脚)",
  949. boardSignal: net,
  950. peripheralRole: "JTAG 辅助针脚",
  951. pinDefinition: j3Map ? j3Map.def : `${key} JTAG 空脚 (NC)`,
  952. displayBadge: j3Map ? `板级参考 · ${j3Map.rtl}` : "NC · 未连接",
  953. tags: ["JTAG"],
  954. isUsed: true,
  955. };
  956. }
  957. // J25 MCU SWD (V1.2, V1.1, V2.0 - 5-pin header)
  958. if (ref === "J25") {
  959. const j25Map = {
  960. "1": {chip: "GND", phys: "GND", rtl: "GND", def: "板载 MCU SWD 调试参考地 (0V)"},
  961. "2": {chip: "MCU U27", phys: "U27.37", rtl: "PA14 / JTCK_SWCLK", def: "MCU SWD 时钟输入 (SWCLK, 经 R798)"},
  962. "3": {chip: "MCU U27", phys: "U27.34", rtl: "PA13 / JTMS_SWDIO", def: "MCU SWD 双向数据 (SWDIO, 经 R797)"},
  963. "4": {chip: "POWER", phys: "+3.3V", rtl: "VCC_3V3", def: "MCU SWD 目标参考电压 (+3.3V)"},
  964. "5": {chip: "MCU U27", phys: "U27.7", rtl: "NRST (MCU_RST#)", def: "MCU 硬件复位脚 (低有效, 经 R796)"},
  965. };
  966. const entry = j25Map[pnum];
  967. if (entry) {
  968. return {
  969. chip: entry.chip,
  970. physicalPin: entry.phys,
  971. logicalSignal: entry.rtl,
  972. boardSignal: net,
  973. peripheralRole: "MCU SWD 调试",
  974. pinDefinition: entry.def,
  975. displayBadge: `${entry.chip} ${entry.phys} · ${entry.rtl}`,
  976. tags: ["JTAG", "SWD", "MCU"],
  977. isUsed: false,
  978. };
  979. }
  980. }
  981. // Board-specific active peripheral mappings
  982. if (ver === "v2_0") {
  983. if (key === "CON12.3") {
  984. return {
  985. chip: "FPGA U3",
  986. physicalPin: "U3.K2",
  987. logicalSignal: "host_ttl_tx (EX_GPIO1)",
  988. boardSignal: "EX_GPIO1_TX",
  989. peripheralRole: "Host TTL TX (6400 V2.0 主通信口)",
  990. pinDefinition: "CPLD TTL 串口发送端 (3.3V LVCMOS, 经 R1875 0R 到 CON12.3;外部接收端须另接线)",
  991. displayBadge: "FPGA U3.K2 · host_ttl_tx (EX_GPIO1)",
  992. tags: ["UART", "HOST_TTL", "TTL"],
  993. isUsed: true,
  994. };
  995. }
  996. if (key === "CON12.4") {
  997. return {
  998. chip: "FPGA U3",
  999. physicalPin: "U3.J4",
  1000. logicalSignal: "host_ttl_rx (EX_GPIO2)",
  1001. boardSignal: "EX_GPIO2_RX",
  1002. peripheralRole: "Host TTL RX (6400 V2.0 主通信口)",
  1003. pinDefinition: "CPLD TTL 串口接收端 (3.3V LVCMOS, 经 R1876 0R 到 CON12.4;外部发送端须另接线)",
  1004. displayBadge: "FPGA U3.J4 · host_ttl_rx (EX_GPIO2)",
  1005. tags: ["UART", "HOST_TTL", "TTL"],
  1006. isUsed: true,
  1007. };
  1008. }
  1009. if (key === "CON103.1" || key === "CON103.2") {
  1010. const isTx = pnum === "1";
  1011. return {
  1012. chip: "MCU U27 & CP2102 U33",
  1013. physicalPin: isTx ? "U27.12 (PA2)" : "U27.13 (PA3)",
  1014. logicalSignal: isTx ? "MCU_UART1_TX (PA2 / 电气图 TX1)" : "MCU_UART1_RX (PA3 / 电气图 RX1)",
  1015. boardSignal: isTx ? "MCU_UART1_TX_CON" : "MCU_UART1_RX_CON",
  1016. peripheralRole: isTx ? "板载 STM32 MCU 串口 TX (电气图标 -CON103)" : "板载 STM32 MCU 串口 RX (电气图标 -CON103)",
  1017. pinDefinition: isTx
  1018. ? "原生 BRD 网表经 R1029 接板载 STM32 U27.12 (PA2) 及经 R1921 接 CP2102 U33(电气图第4/9页标为 -CON103 TX1 接 -U6-2 P1.26 RX1;直连 FPGA U3.K2 的为 CON12.3)"
  1019. : "原生 BRD 网表经 R1030 接板载 STM32 U27.13 (PA3) 及经 R1920 接 CP2102 U33(电气图第4/9页标为 -CON103 RX1 接 -U6-2 P1.25 TX1;直连 FPGA U3.J4 的为 CON12.4)",
  1020. displayBadge: `MCU U27.${isTx ? "12 (PA2)" : "13 (PA3)"} · ${isTx ? "MCU_UART1_TX" : "MCU_UART1_RX"} [板载MCU]`,
  1021. tags: ["UART", "MCU", "CON103"],
  1022. isUsed: false,
  1023. };
  1024. }
  1025. if (key === "CON134.1" || key === "CON134.2") {
  1026. const isTx = pnum === "1";
  1027. return {
  1028. chip: "FPGA U3",
  1029. physicalPin: isTx ? "U3.T14" : "U3.R13",
  1030. logicalSignal: isTx ? "host_rs232_tx (RS232_TX)" : "host_rs232_rx (RS232_RX)",
  1031. boardSignal: isTx ? "HOST_RS232_TX" : "HOST_RS232_RX",
  1032. peripheralRole: isTx ? "Host RS232 TX" : "Host RS232 RX",
  1033. pinDefinition: isTx ? "Host RS232 发送端 (经 U22 TRSF3232E 电平转换, 921600 8N1)" : "Host RS232 接收端 (经 U22 TRSF3232E 电平转换, 921600 8N1)",
  1034. displayBadge: `FPGA U3.${isTx ? "T14" : "R13"} · ${isTx ? "host_rs232_tx" : "host_rs232_rx"}`,
  1035. tags: ["UART", "HOST_RS232", "RS232"],
  1036. isUsed: true,
  1037. };
  1038. }
  1039. if (key === "CON77.1" || key === "CON77.2") {
  1040. const isTx = pnum === "1";
  1041. return {
  1042. chip: "FPGA U3",
  1043. physicalPin: isTx ? "U3.L10" : "U3.M9",
  1044. logicalSignal: isTx ? "bmc_tx (BMC_TX)" : "bmc_rx (BMC_RX)",
  1045. boardSignal: isTx ? "BMC_RS232_TX" : "BMC_RS232_RX",
  1046. peripheralRole: isTx ? "BMC RS232 TX" : "BMC RS232 RX",
  1047. pinDefinition: isTx ? "BMC 测温轮询串口发送端 (经 U22, 115200 8N1)" : "BMC 测温轮询串口接收端 (经 U22, 115200 8N1)",
  1048. displayBadge: `FPGA U3.${isTx ? "L10" : "M9"} · ${isTx ? "bmc_tx (BMC_TX)" : "bmc_rx (BMC_RX)"}`,
  1049. tags: ["UART", "BMC", "BMC_RS232"],
  1050. isUsed: true,
  1051. };
  1052. }
  1053. if (ref === "CON170" && (pnum === "3" || pnum === "6")) {
  1054. const isTx = pnum === "3";
  1055. return {
  1056. chip: "FPGA U3",
  1057. physicalPin: isTx ? "U3.L10" : "U3.M9",
  1058. logicalSignal: isTx ? "bmc_tx (UART1_TX / RJ45备用)" : "bmc_rx (UART1_RX / RJ45备用)",
  1059. boardSignal: isTx ? "RS232_TX1_RJ45" : "RS232_RX1_RJ45",
  1060. peripheralRole: isTx ? "RJ45 BMC RS232 TX 备用口 (CON170.3)" : "RJ45 BMC RS232 RX 备用口 (CON170.6)",
  1061. pinDefinition: isTx
  1062. ? "RJ45 备用 BMC RS232 发送端 (经 R1898 并联 CON77.1 ↔ U22 ↔ R818 ↔ U3.L10)"
  1063. : "RJ45 备用 BMC RS232 接收端 (经 R1899 并联 CON77.2 ↔ U22 ↔ R819 ↔ U3.M9)",
  1064. displayBadge: `FPGA U3.${isTx ? "L10" : "M9"} · ${isTx ? "bmc_tx" : "bmc_rx"} [RJ45备用]`,
  1065. tags: ["UART", "BMC", "CON170"],
  1066. isUsed: false,
  1067. };
  1068. }
  1069. if (key in V20_PTC_CONNECTOR_MAP) {
  1070. const ptc = V20_PTC_CONNECTOR_MAP[key];
  1071. const rtlPort = ptc.idx < 24 ? `ptc_pwm_out[${ptc.idx}]` : "未绑定输出";
  1072. const tags = ["PTC", ptc.tag];
  1073. if (ptc.altTag) tags.push(ptc.altTag);
  1074. return {
  1075. chip: "FPGA U3",
  1076. physicalPin: `U3.${ptc.ball}`,
  1077. logicalSignal: `${rtlPort} (${ptc.net})`,
  1078. boardSignal: ptc.net,
  1079. peripheralRole: ptc.role,
  1080. pinDefinition: `24V PTC 加热功率输出 (${ptc.role}, 20Hz 统一高有效 PWM, 50ms周期)`,
  1081. displayBadge: `FPGA U3.${ptc.ball} · ${rtlPort} (${ptc.tag}/${ptc.net})`,
  1082. tags,
  1083. isUsed: ptc.idx < 24,
  1084. };
  1085. }
  1086. const v20FanMap = {
  1087. "CON154.5": {tag: "FAN1", alt: "Front0", rtl: "fan_front_out[0]", ball: "C4", role: "1#腔新风/进风双风扇 PWM (Front0)"},
  1088. "CON154.6": {tag: "FAN5", alt: "Rear0", rtl: "fan_rear_out[0]", ball: "B5", role: "1#腔排风/出风单风扇 PWM (Rear0)"},
  1089. "CON154.11": {tag: "FAN2", alt: "Front1", rtl: "fan_front_out[1]", ball: "B1", role: "2#腔新风/进风双风扇 PWM (Front1)"},
  1090. "CON154.12": {tag: "FAN6", alt: "Rear1", rtl: "fan_rear_out[1]", ball: "A14", role: "2#腔排风/出风单风扇 PWM (Rear1)"},
  1091. "CON155.5": {tag: "FAN3", alt: "Front2", rtl: "fan_front_out[2]", ball: "P12", role: "3#腔新风/进风双风扇 PWM (Front2)"},
  1092. "CON155.6": {tag: "FAN7", alt: "Rear2", rtl: "fan_rear_out[2]", ball: "A10", role: "3#腔排风/出风单风扇 PWM (Rear2)"},
  1093. "CON155.11": {tag: "FAN4", alt: "Front3", rtl: "fan_front_out[3]", ball: "D8", role: "4#腔新风/进风双风扇 PWM (Front3)"},
  1094. "CON155.12": {tag: "FAN8", alt: "Rear3", rtl: "fan_rear_out[3]", ball: "C7", role: "4#腔排风/出风单风扇 PWM (Rear3)"},
  1095. };
  1096. if (key in v20FanMap) {
  1097. const f = v20FanMap[key];
  1098. return {
  1099. chip: "FPGA U3",
  1100. physicalPin: `U3.${f.ball}`,
  1101. logicalSignal: `${f.rtl} (${f.alt})`,
  1102. boardSignal: f.alt,
  1103. peripheralRole: f.role,
  1104. pinDefinition: `${f.role} (20Hz 统一 PWM;连接器高电平为ON,FPGA球位低电平为ON,经主板 2N7002 NMOS 反相, FAN_BOARD_INVERT_MASK=8'hFF)`,
  1105. displayBadge: `FPGA U3.${f.ball} · ${f.rtl} (${f.alt}/${f.tag})`,
  1106. tags: ["FAN", f.tag, f.alt.toUpperCase()],
  1107. isUsed: true,
  1108. };
  1109. }
  1110. const v20SsrMap = {
  1111. "CON165.14": {tag: "SSR1", rtl: "ssr1_out", ball: "E14", role: "SSR1 腔1加热布 20Hz PWM 控制 (-1-7E 110W/24V,CON_PE14→R1698/R1860→CPLD_GPIO64)"},
  1112. "CON165.13": {tag: "SSR2", rtl: "ssr2_out", ball: "E8", role: "SSR2 腔2+3共用加热布 20Hz PWM 控制 (-2-7E 220W/24V,CON_PE13→R1695/R1859→ADC0_CH7)"},
  1113. "CON165.11": {tag: "SSR4", rtl: "ssr4_out", ball: "D9", role: "SSR4 腔4加热布 20Hz PWM 控制 (-4-7E 110W/24V,CON_PE11→R1694/R1857→ADC0_CH5)"},
  1114. };
  1115. if (key in v20SsrMap) {
  1116. const s = v20SsrMap[key];
  1117. return {
  1118. chip: "FPGA U3",
  1119. physicalPin: `U3.${s.ball}`,
  1120. logicalSignal: `${s.rtl} (${s.tag})`,
  1121. boardSignal: `${s.tag}_CTRL`,
  1122. peripheralRole: s.role,
  1123. pinDefinition: `${s.role} (电气图 V3.0 第4页对齐;20Hz 统一 PWM 50ms周期,默认/看门狗输出 0%)`,
  1124. displayBadge: `FPGA U3.${s.ball} · ${s.rtl} (${s.tag})`,
  1125. tags: ["SSR", s.tag],
  1126. isUsed: true,
  1127. };
  1128. }
  1129. const v20BtnMap = {
  1130. "CON165.10": {tag: "EPB1", rtl: "estop_btn_in", ball: "F8", byteIdx: 86, role: "急停按钮 EPB1 检测 (IO10/CON_PE10→R1696/R1856→ADC0_CH0)", extra: "无 CPLD 安全联锁(外部电路直接失效开关罩),仅透传"},
  1131. "CON165.9": {tag: "SB1", rtl: "hood_open_btn_in", ball: "C11", byteIdx: 87, role: "开罩按钮 SB1 检测 (IO9/CON_PE9→R1693/R1855→BANK0_GPIO2)", extra: "不主动上报"},
  1132. "CON165.8": {tag: "SB2", rtl: "hood_close_btn_in", ball: "A11", byteIdx: 88, role: "合罩按钮 SB2 检测 (IO8/CON_PE8→R1692/R1854→BANK0_GPIO1)", extra: "不主动上报"},
  1133. };
  1134. if (key in v20BtnMap) {
  1135. const b = v20BtnMap[key];
  1136. return {
  1137. chip: "FPGA U3",
  1138. physicalPin: `U3.${b.ball}`,
  1139. logicalSignal: `${b.rtl} (${b.tag})`,
  1140. boardSignal: net || b.tag,
  1141. peripheralRole: b.role,
  1142. pinDefinition: `${b.role};外部上拉电阻未贴装,约束配 LVCMOS33 + 内部 PULLDOWN(未按下/无电=0x00,按下高电平=0x01),仅 GET_ALL 写入回包第 ${b.byteIdx} 字节(${b.extra})`,
  1143. displayBadge: `FPGA U3.${b.ball} · ${b.rtl} (${b.tag})`,
  1144. tags: ["SSR", "GPIO", b.tag, "BUTTON"],
  1145. isUsed: true,
  1146. };
  1147. }
  1148. if (key === "CON165.12") {
  1149. return {
  1150. chip: "NC",
  1151. physicalPin: "NC (V3.0 空脚)",
  1152. logicalSignal: "未连接 (已取消旧版暂绑 U3.F7)",
  1153. boardSignal: net || "CON_PE12",
  1154. peripheralRole: "CON165.12 空脚 (NC)",
  1155. pinDefinition: "电气图 V3.0 第4页确认 CON165.12 为空脚,不再沿用旧版暂绑的 U3.F7。",
  1156. displayBadge: "NC · V3.0 空脚",
  1157. tags: ["SSR"],
  1158. isUsed: false,
  1159. };
  1160. }
  1161. if (key === "CON165.1" || key === "CON165.2" || key === "CON165.4") {
  1162. const retiredBall = key === "CON165.1" ? "F3" : (key === "CON165.2" ? "F1" : "G4");
  1163. const idx = key === "CON165.1" ? 0 : (key === "CON165.2" ? 1 : 2);
  1164. return {
  1165. chip: "FPGA U3",
  1166. physicalPin: `U3.${retiredBall}`,
  1167. logicalSignal: `ssr_unused_zero[${idx}] (固定低电平)`,
  1168. boardSignal: net,
  1169. peripheralRole: "旧版废弃 SSR 引脚 (钳位低电平)",
  1170. pinDefinition: `旧版 SSR 引脚 U3.${retiredBall},现由 ssr_unused_zero[${idx}] 固定输出 0。`,
  1171. displayBadge: `FPGA U3.${retiredBall} · ssr_unused_zero[${idx}]`,
  1172. tags: ["SSR"],
  1173. isUsed: false,
  1174. };
  1175. }
  1176. if (key === "CON146.1") {
  1177. return {
  1178. chip: "FPGA U3",
  1179. physicalPin: "U3.H6",
  1180. logicalSignal: "buzzer_out (KEYLED_V3 +24V 蜂鸣器)",
  1181. boardSignal: "KEYLED_V3",
  1182. peripheralRole: "MCU 触发蜂鸣器 +24V 高边驱动 (4000Hz 80% 占空比)",
  1183. pinDefinition: "CON146.1 KEYLED_V3 (+24V 高边输出) ← Q10620/Q10621 ← R1297/R1301 ← U3.H6;SET_ALL 第 35 字节控制(0x00 关闭、0x01 开启输出 4000Hz 80% 占空比方波:6250拍周期/5000高/1250低),GET_DATA 第 89 字节回读,复位/看门狗保持低电平;非 TTL。",
  1184. displayBadge: "FPGA U3.H6 · buzzer_out (4000Hz 80%)",
  1185. tags: ["BUZZER", "KEYLED", "CON146"],
  1186. isUsed: true,
  1187. };
  1188. }
  1189. const v20Con159Map = {
  1190. "CON159.1": {tag: "M1", net: "PUL1 (X1-S)", ball: "C12", role: "1轴 -M1 (1#前部风门) STEP 脉冲 (X1-S → -U1 X-STEP)"},
  1191. "CON159.2": {tag: "M1", net: "DIR1 (X1-D)", ball: "B12", role: "1轴 -M1 (1#前部风门) DIR 方向 (X1-D → -U1 X-DIR)"},
  1192. "CON159.3": {tag: "M2", net: "PUL2 (Y1-S)", ball: "L12", role: "2轴 -M2 (1#后部风门) STEP 脉冲 (Y1-S → -U1 Y-STEP)"},
  1193. "CON159.4": {tag: "M2", net: "DIR2 (Y1-D)", ball: "J11", role: "2轴 -M2 (1#后部风门) DIR 方向 (Y1-D → -U1 Y-DIR)"},
  1194. "CON159.5": {tag: "M3", net: "PUL3 (Z1-S)", ball: "L14", role: "3轴 -M3 (1#顶部风门) STEP 脉冲 (Z1-S → -U1 Z-STEP)"},
  1195. "CON159.6": {tag: "M3", net: "DIR3 (Z1-D)", ball: "K14", role: "3轴 -M3 (1#顶部风门) DIR 方向 (Z1-D → -U1 Z-DIR)"},
  1196. "CON159.7": {tag: "M4", net: "PUL4 (A1-S)", ball: "L13", role: "轴4 -M4 (2#前部风门) STEP 脉冲 (A1-S → -U1 A-STEP)"},
  1197. "CON159.8": {tag: "M4", net: "DIR4 (A1-D)", ball: "K11", role: "轴4 -M4 (2#前部风门) DIR 方向 (A1-D → -U1 A-DIR)"},
  1198. "CON159.9": {tag: "MOTOR_EN", net: "ENA1", ball: "F3", role: "-U1 步进驱动板使能 ENA1 (与 CON165.1 SSR1 共用 U3.F3)"},
  1199. };
  1200. if (key in v20Con159Map) {
  1201. const m = v20Con159Map[key];
  1202. return {
  1203. chip: "FPGA U3",
  1204. physicalPin: `U3.${m.ball}`,
  1205. logicalSignal: `${m.net} (6400 协议空余 / 电气图第3页 -U1)`,
  1206. boardSignal: net || m.net,
  1207. peripheralRole: m.role,
  1208. pinDefinition: `电气图第3/4页 ${m.role} ↔ FPGA U3.${m.ball}(6400 32B 热控协议未启用步进轴)`,
  1209. displayBadge: `FPGA U3.${m.ball} · ${m.net} [第3页风门步进 / 协议空余]`,
  1210. tags: ["MOTOR", m.tag, "CON159"],
  1211. isUsed: false,
  1212. };
  1213. }
  1214. }
  1215. // V2.0 Fan Expansion Board
  1216. if (ver === "v2_0_fan") {
  1217. if (ball) {
  1218. return {
  1219. chip: "主板 FPGA U3 (直通)",
  1220. physicalPin: `U3.${ball}`,
  1221. logicalSignal: `${boardSig} (经 CON168 无源直通)`,
  1222. boardSignal: net,
  1223. peripheralRole: `${ref}.${pnum} 风扇 PWM 信号`,
  1224. pinDefinition: `风扇子板无源直通 PWM 信号 (${ref}.${pnum} ↔ 主板 FPGA U3.${ball})`,
  1225. displayBadge: `主板 FPGA U3.${ball} · ${boardSig} (无源直通)`,
  1226. tags: ["FAN", ref],
  1227. isUsed: true,
  1228. };
  1229. }
  1230. }
  1231. // V1.2 (MRDIMM / 8000) & V1.1 (6400)
  1232. if (ver === "v1_2" || ver === "v1_1") {
  1233. // MRDIMM dev 2.1.0: direct 3.3 V GPIO pins, not the 12 V KEYLED power output.
  1234. if (ver === "v1_2" && ref === "CON140" && (pnum === "1" || pnum === "2")) {
  1235. const isTx = pnum === "1";
  1236. const b = isTx ? "T11" : "P13";
  1237. const signal = isTx ? "host_ttl_tx" : "host_ttl_rx";
  1238. return {
  1239. chip: "FPGA U3",
  1240. physicalPin: `U3.${b}`,
  1241. logicalSignal: signal,
  1242. boardSignal: net,
  1243. peripheralRole: `MRDIMM Host TTL ${isTx ? "TX" : "RX"}`,
  1244. pinDefinition: `CON140.${pnum} 经 ${isTx ? "R1261" : "R1262"} 0Ω 直连 U3.${b},3.3V;3/4脚仍为GPIO,无信号地。共地另接 CON77.3/4。`,
  1245. displayBadge: `FPGA U3.${b} · ${signal}`,
  1246. tags: ["UART", "HOST_TTL", "TTL", "CON140"],
  1247. isUsed: true,
  1248. };
  1249. }
  1250. if (ver === "v1_2" && ref === "CON140" && pnum === "3") {
  1251. return {
  1252. chip: "FPGA U3",
  1253. physicalPin: "U3.L3",
  1254. logicalSignal: "hood_switch_in (EX_GPIO31)",
  1255. boardSignal: net,
  1256. peripheralRole: "MRDIMM 高温罩开关输入 (GET_ALL 字节63)",
  1257. pinDefinition: "CON140.3 经 R1263 接 U3.L3 (LVCMOS33 + PULLDOWN, 非 GND);仅 GET_ALL 时采样放进第 63 字节。",
  1258. displayBadge: "FPGA U3.L3 · hood_switch_in",
  1259. tags: ["GPIO", "CON140"],
  1260. isUsed: true,
  1261. };
  1262. }
  1263. if (ver === "v1_2" && ref === "CON146" && (pnum === "1" || pnum === "3")) {
  1264. if (pnum === "1") {
  1265. return {
  1266. chip: "FPGA U3",
  1267. physicalPin: "U3.H6",
  1268. logicalSignal: "buzzer_out (12V KEYLED_V3 蜂鸣器)",
  1269. boardSignal: net,
  1270. peripheralRole: "MCU 触发蜂鸣器 12V 高边输出(不能用作 Host TTL)",
  1271. pinDefinition: "U3.H6 经 NMOS+PMOS 控制 +12V KEYLED_V3(4000Hz 80% 占空比方波:6250拍周期/5000高/1250低;SET_ALL 字节68 / GET_DATA 字节64),输出有1µF和100nF电容;不是3.3V TTL。",
  1272. displayBadge: "FPGA U3.H6 · buzzer_out (12V 蜂鸣器 / 非TTL)",
  1273. tags: ["KEYLED", "BUZZER"],
  1274. isUsed: true,
  1275. };
  1276. }
  1277. return {
  1278. chip: "BOARD",
  1279. physicalPin: "KEY3按键输入",
  1280. logicalSignal: "当前固件未绑定",
  1281. boardSignal: net,
  1282. peripheralRole: "KEY/LED座,不能用作Host TTL",
  1283. pinDefinition: "N7 按键输入含10kΩ上拉及100nF电容;当前Host RX使用CON140.2。",
  1284. displayBadge: "CON146 · 非Host串口",
  1285. tags: ["KEYLED"],
  1286. isUsed: false,
  1287. };
  1288. }
  1289. // V1.1 Host TTL on CON30
  1290. if (ver === "v1_1" && ref === "CON30" && (pnum === "3" || pnum === "4")) {
  1291. const isTx = pnum === "4";
  1292. return {
  1293. chip: "FPGA U3",
  1294. physicalPin: isTx ? "U3.A10" : "U3.B9",
  1295. logicalSignal: isTx ? "host_ttl_tx (FAN_GPIO4)" : "host_ttl_rx (FAN_GPIO3)",
  1296. boardSignal: isTx ? "MOTOR_EN_N / FAN_GPIO4" : "FAN_GPIO3",
  1297. peripheralRole: isTx ? "6400 V1.1 Host TTL TX (活动串口)" : "6400 V1.1 Host TTL RX (活动串口)",
  1298. pinDefinition: isTx
  1299. ? "6400 V1.1 Host TTL 串口发送端 (FPGA U3.A10 → CON30.4, 3.3V LVCMOS)"
  1300. : "6400 V1.1 Host TTL 串口接收端 (CON30.3 → FPGA U3.B9, 3.3V LVCMOS)",
  1301. displayBadge: isTx
  1302. ? "FPGA U3.A10 · host_ttl_tx (FAN_GPIO4)"
  1303. : "FPGA U3.B9 · host_ttl_rx (FAN_GPIO3)",
  1304. tags: ["UART", "HOST_TTL", "TTL", "CON30"],
  1305. isUsed: true,
  1306. };
  1307. }
  1308. // Host RS232 CON78 & BMC RS232 CON77
  1309. if (ref === "CON78" && (pnum === "1" || pnum === "2")) {
  1310. const isTx = pnum === "1";
  1311. return {
  1312. chip: "FPGA U3",
  1313. physicalPin: isTx ? "U3.M10" : "U3.N11",
  1314. logicalSignal: isTx ? "host_rs232_tx (UART2_TX)" : "host_rs232_rx (UART2_RX)",
  1315. boardSignal: isTx ? "UART2_TX" : "UART2_RX",
  1316. peripheralRole: isTx ? "Host RS232 TX (CON78.1)" : "Host RS232 RX (CON78.2)",
  1317. pinDefinition: isTx
  1318. ? "Host RS232 发送端 (U3.M10 → U23 RS232 收发器(V1.2 BOM: TRSF3232E) → CON78.1)"
  1319. : "Host RS232 接收端 (CON78.2 → U23 RS232 收发器(V1.2 BOM: TRSF3232E) → U3.N11)",
  1320. displayBadge: `FPGA U3.${isTx ? "M10" : "N11"} · ${isTx ? "host_rs232_tx (UART2_TX)" : "host_rs232_rx (UART2_RX)"}`,
  1321. tags: ["UART", "HOST_RS232", "RS232"],
  1322. isUsed: true,
  1323. };
  1324. }
  1325. if (ref === "CON77" && (pnum === "1" || pnum === "2")) {
  1326. const isTx = pnum === "1";
  1327. return {
  1328. chip: "FPGA U3",
  1329. physicalPin: isTx ? "U3.L10" : "U3.M9",
  1330. logicalSignal: isTx ? "bmc_tx (UART1_TX)" : "bmc_rx (UART1_RX)",
  1331. boardSignal: isTx ? "UART1_TX" : "UART1_RX",
  1332. peripheralRole: isTx ? "BMC RS232 TX (CON77.1)" : "BMC RS232 RX (CON77.2)",
  1333. pinDefinition: isTx
  1334. ? "BMC 测温轮询串口发送端 (U3.L10 → U22 RS232 收发器(V1.2 BOM: TRSF3232E) → CON77.1, 115200 8N1)"
  1335. : "BMC 测温轮询串口接收端 (CON77.2 → U22 RS232 收发器(V1.2 BOM: TRSF3232E) → U3.M9, 115200 8N1)",
  1336. displayBadge: `FPGA U3.${isTx ? "L10" : "M9"} · ${isTx ? "bmc_tx (UART1_TX)" : "bmc_rx (UART1_RX)"}`,
  1337. tags: ["UART", "BMC", "BMC_RS232"],
  1338. isUsed: true,
  1339. };
  1340. }
  1341. // PTC on V1.2 / V1.1 (CON18, CON27, CON28, CON29)
  1342. if (key in V12_V11_PTC_CONNECTOR_MAP) {
  1343. const ptc = V12_V11_PTC_CONNECTOR_MAP[key];
  1344. const isUsed = true;
  1345. const rtlPort = ver === "v1_1"
  1346. ? `ptc_pwm_out[${ptc.idx}]`
  1347. : `ptc_pwm_out[${ptc.idx}] (逻辑PTC${Math.floor(ptc.idx / 6)})`;
  1348. const role = ver === "v1_1" ? ptc.v11Role : `PTC${ptc.idx + 1} (腔${Math.floor(ptc.idx / 6) + 1},逻辑PTC${Math.floor(ptc.idx / 6)})`;
  1349. return {
  1350. chip: "FPGA U3",
  1351. physicalPin: `U3.${ptc.ball}`,
  1352. logicalSignal: `${rtlPort} (${ptc.heat})`,
  1353. boardSignal: ptc.heat,
  1354. peripheralRole: role,
  1355. pinDefinition: `+12V PTC 高边 MOS 功率输出 (${role}, 20Hz 统一高有效 PWM)`,
  1356. displayBadge: `FPGA U3.${ptc.ball} · ${rtlPort}`,
  1357. tags: ["PTC", ptc.tag],
  1358. isUsed,
  1359. };
  1360. }
  1361. // Current MRDIMM four buffered PWM channels; FG stays a board fact, not sampled by this protocol.
  1362. const v12ActiveFans = {
  1363. "CON25.7": {axis: 0, ball: "B1"},
  1364. "CON8.7": {axis: 1, ball: "C4"},
  1365. "CON24.7": {axis: 2, ball: "B5"},
  1366. "CON26.7": {axis: 3, ball: "A14"},
  1367. };
  1368. if (ver === "v1_2" && key in v12ActiveFans) {
  1369. const f = v12ActiveFans[key];
  1370. const rtl = `fan_pwm_out[${f.axis}]`;
  1371. return {
  1372. chip: "FPGA U3",
  1373. physicalPin: `U3.${f.ball}`,
  1374. logicalSignal: rtl,
  1375. boardSignal: net,
  1376. peripheralRole: `MRDIMM 腔${f.axis + 1} FAN${f.axis + 1} PWM`,
  1377. pinDefinition: "20Hz 统一 PWM;主板MMBT2222 NPN反相,连接器高电平为ON、FPGA球位低电平为ON。",
  1378. displayBadge: `FPGA U3.${f.ball} · ${rtl}`,
  1379. tags: ["FAN", `FAN${f.axis + 1}`],
  1380. isUsed: true,
  1381. };
  1382. }
  1383. // V1.1 Active Fans (Front0..3 on CON25/CON8/CON24/CON26; Rear0..3 on CON64/CON66/CON62/CON60)
  1384. const v11ActiveFans = {
  1385. "CON25.7": {tag: "FAN1", alt: "Front0", rtl: "fan_front_out[0]", ball: "B1", sig: "FAN_PWM3", role: "6400 V1.1 Front0 前风扇 PWM (板级反相)"},
  1386. "CON8.7": {tag: "FAN2", alt: "Front1", rtl: "fan_front_out[1]", ball: "C4", sig: "FAN_PWM1", role: "6400 V1.1 Front1 前风扇 PWM (板级反相)"},
  1387. "CON24.7": {tag: "FAN3", alt: "Front2", rtl: "fan_front_out[2]", ball: "B5", sig: "FAN_PWM2", role: "6400 V1.1 Front2 前风扇 PWM (板级反相)"},
  1388. "CON26.7": {tag: "FAN4", alt: "Front3", rtl: "fan_front_out[3]", ball: "A14", sig: "FAN_PWM4", role: "6400 V1.1 Front3 前风扇 PWM (板级反相)"},
  1389. "CON64.4": {tag: "FAN5", alt: "Rear0", rtl: "fan_rear_out[0]", ball: "P15", sig: "FAN_GPIO44", role: "6400 V1.1 Rear0 后风扇 PWM (同相)"},
  1390. "CON66.4": {tag: "FAN6", alt: "Rear1", rtl: "fan_rear_out[1]", ball: "N15", sig: "FAN_GPIO46", role: "6400 V1.1 Rear1 后风扇 PWM (同相)"},
  1391. "CON62.4": {tag: "FAN7", alt: "Rear2", rtl: "fan_rear_out[2]", ball: "M14", sig: "FAN_GPIO42", role: "6400 V1.1 Rear2 后风扇 PWM (同相)"},
  1392. "CON60.4": {tag: "FAN8", alt: "Rear3", rtl: "fan_rear_out[3]", ball: "P5", sig: "FAN_GPIO40", role: "6400 V1.1 Rear3 后风扇 PWM (同相)"},
  1393. };
  1394. if (ver === "v1_1" && key in v11ActiveFans) {
  1395. const f = v11ActiveFans[key];
  1396. return {
  1397. chip: "FPGA U3",
  1398. physicalPin: `U3.${f.ball}`,
  1399. logicalSignal: `${f.rtl} (${f.sig})`,
  1400. boardSignal: f.sig,
  1401. peripheralRole: f.role,
  1402. pinDefinition: `${f.role} (500Hz PWM, FAN_BOARD_INVERT_MASK=8'h0F)`,
  1403. displayBadge: `FPGA U3.${f.ball} · ${f.rtl} (${f.alt}/${f.tag})`,
  1404. tags: ["FAN", f.tag, f.alt.toUpperCase()],
  1405. isUsed: true,
  1406. };
  1407. }
  1408. // V1.2 12 buffered axes in generator order, plus global enable CON120
  1409. if (ver === "v1_2" && ref in V12_MOTOR_MAP) {
  1410. const m = V12_MOTOR_MAP[ref];
  1411. if (pnum === m.stepPin || pnum === m.dirPin) {
  1412. const isStep = pnum === m.stepPin;
  1413. const b = isStep ? m.stepBall : m.dirBall;
  1414. const rtl = isStep ? `motor_step_gate[${m.axis}]` : `motor_dir_gate[${m.axis}]`;
  1415. const snet = isStep ? m.stepNet : m.dirNet;
  1416. return {
  1417. chip: "FPGA U3",
  1418. physicalPin: `U3.${b}`,
  1419. logicalSignal: `${rtl} (${snet})`,
  1420. boardSignal: snet,
  1421. peripheralRole: `${m.tag} (轴${m.axis}) ${isStep ? "STEP 脉冲" : "DIR 方向"}`,
  1422. pinDefinition: `步进电机 ${m.tag} (轴${m.axis}) ${isStep ? "STEP 输出(2N7002 NMOS开漏;FPGA上升沿计步,对应座端下拉)" : "DIR 输出(2N7002 NMOS开漏反相)"}`,
  1423. displayBadge: `FPGA U3.${b} · ${rtl} (${m.tag}_${isStep ? "STEP" : "DIR"})`,
  1424. tags: ["MOTOR", m.tag],
  1425. isUsed: true,
  1426. };
  1427. }
  1428. }
  1429. if (ver === "v1_2" && key === "CON120.2") {
  1430. return {
  1431. chip: "FPGA U3",
  1432. physicalPin: "U3.C11",
  1433. logicalSignal: "motor_en_n (MOTOR_EN_N)",
  1434. boardSignal: "MOTOR_EN_N",
  1435. peripheralRole: "步进电机全局使能 (MOTOR_EN)",
  1436. pinDefinition: "电机驱动使能控制 (FPGA U3.C11 常驱高 1'b1 → Q145 2N7002 NMOS拉低座端 EN 保持有效)",
  1437. displayBadge: "FPGA U3.C11 · motor_en_n (MOTOR_EN_N)",
  1438. tags: ["MOTOR", "MOTOR_EN"],
  1439. isUsed: true,
  1440. };
  1441. }
  1442. // CON126/127 now carry active limit inputs; do not overwrite them as old X/Y/Z motor outputs.
  1443. // V1.2 active limits: CON12/126/127/128/129/138
  1444. if (ver === "v1_2" && key in V12_LIMIT_MAP) {
  1445. const lim = V12_LIMIT_MAP[key];
  1446. return {
  1447. chip: "FPGA U3",
  1448. physicalPin: `U3.${lim.ball}`,
  1449. logicalSignal: `${lim.rtl} (${lim.tag})`,
  1450. boardSignal: boardSig || net,
  1451. peripheralRole: lim.role,
  1452. pinDefinition: `${lim.role} (3.3V 限位输入, 2FF 同步, 高电平有效阻断对应方向 STEP)`,
  1453. displayBadge: `FPGA U3.${lim.ball} · ${lim.rtl} (${lim.tag})`,
  1454. tags: ["LIMIT", lim.tag, `M${lim.axis + 1}`],
  1455. isUsed: true,
  1456. };
  1457. }
  1458. }
  1459. // CT7428 Temperature Sensor Connectors (CON53, 55, 56, 57, 79..98 across all boards)
  1460. if (/^CON(53|55|56|57|79|8[0-9]|9[0-8])$/.test(ref)) {
  1461. const chOrder = [
  1462. "CON53", "CON55", "CON56", "CON57", "CON79", "CON80", "CON81", "CON82",
  1463. "CON83", "CON85", "CON87", "CON89", "CON84", "CON86", "CON88", "CON90",
  1464. "CON91", "CON93", "CON95", "CON97", "CON92", "CON94", "CON96", "CON98",
  1465. ];
  1466. const v20ProbeRole = {
  1467. CON53: "1#腔室进风温度",
  1468. CON55: "1#腔室出风温度",
  1469. CON56: "2#腔室进风温度",
  1470. CON57: "2#腔室出风温度",
  1471. CON79: "3#腔室进风温度",
  1472. CON80: "3#腔室出风温度",
  1473. CON81: "4#腔室进风温度",
  1474. CON82: "4#腔室出风温度",
  1475. }[ref];
  1476. const chIdx = chOrder.indexOf(ref) + 1;
  1477. const ctBalls = ver === "v1_1" ? "M8/N9(历史profile)" : "B14/A15";
  1478. const chipName = chIdx <= 8 ? "U6" : (chIdx <= 16 ? "U24" : "U25");
  1479. const upperNet = net.toUpperCase();
  1480. const isGndPin = upperNet.includes("GND") || pin?.function_category === "ground" || text(pin?.external_level) === "0V";
  1481. if (!isGndPin && (pnum === "1" || pnum === "2" || pnum === "3")) {
  1482. const isDp = pnum === "1";
  1483. const rolePrefix = ver === "v2_0" && v20ProbeRole ? `${v20ProbeRole} · ` : "";
  1484. return {
  1485. chip: `CT7428 ${chipName} ↔ FPGA U3`,
  1486. physicalPin: `U3.${ctBalls} (SCL/SDA)`,
  1487. logicalSignal: `temp_scl/temp_sda (${ctBalls}) → ${chipName}_CH${(chIdx-1)%8+1}_${isDp ? "DP" : "DN"}`,
  1488. boardSignal: net || `${chipName}_CH${chIdx}_${isDp ? "DP" : "DN"}`,
  1489. peripheralRole: `${rolePrefix}温度探头 CH${chIdx} ${isDp ? "DP+" : "DN-"}`,
  1490. pinDefinition: `${rolePrefix}CT7428 (${chipName}) 远端二极管测温差分输入 ${isDp ? "DP+" : "DN-"} (经 I2C U3.${ctBalls};MRDIMM板载MCU须将I2C0 GPIO设高阻)`,
  1491. displayBadge: `FPGA U3.${ctBalls} (temp_scl/temp_sda) ← ${chipName} CH${(chIdx-1)%8+1}`,
  1492. tags: ["TEMP", `TEMP${chIdx}`],
  1493. isUsed: true,
  1494. };
  1495. }
  1496. }
  1497. // Generic FPGA-mapped pin fallback
  1498. if (ball) {
  1499. const sig = boardSig || net || `${ref}_${pnum}`;
  1500. const isSpare = text(touchpoint?.summary).includes("空余") || text(pin?.summary).includes("空余") || text(touchpoint?.summary).includes("非产品");
  1501. return {
  1502. chip: "FPGA U3",
  1503. physicalPin: `U3.${ball}`,
  1504. logicalSignal: `${sig} (板级映射;以当前ADC为准)`,
  1505. boardSignal: sig,
  1506. peripheralRole: text(pin?.summary) || `${key} (${sig})`,
  1507. pinDefinition: text(pin?.programming?.conclusion) || text(pin?.summary) || `${key} → FPGA U3.${ball} (${sig})`,
  1508. displayBadge: `FPGA U3.${ball} · ${sig}${isSpare ? " [空余]" : ""}`,
  1509. tags: ["GPIO"],
  1510. isUsed: false,
  1511. };
  1512. }
  1513. // Generic MCU-mapped pin fallback
  1514. if (mcuDev && mcuPin) {
  1515. return {
  1516. chip: `MCU ${mcuDev}`,
  1517. physicalPin: `${mcuDev}.${mcuPin}`,
  1518. logicalSignal: `${mcuSig || net} (${text(pin?.mcu?.family) || "板载MCU"})`,
  1519. boardSignal: mcuSig || net,
  1520. peripheralRole: text(pin?.summary) || `${key} → MCU ${mcuDev}.${mcuPin}`,
  1521. pinDefinition: text(pin?.programming?.conclusion) || text(pin?.summary) || `直连板载 MCU ${mcuDev}.${mcuPin} (${mcuSig}),非 FPGA 管脚`,
  1522. displayBadge: `MCU ${mcuDev}.${mcuPin} · ${mcuSig || net} [板载MCU]`,
  1523. tags: ["MCU"],
  1524. isUsed: false,
  1525. };
  1526. }
  1527. // Power / Ground fallback
  1528. const upperNet = net.toUpperCase();
  1529. const isGnd = upperNet.includes("GND") || pin?.function_category === "ground" || text(pin?.external_level) === "0V";
  1530. return {
  1531. chip: isGnd ? "GND" : "POWER/BOARD",
  1532. physicalPin: isGnd ? "GND (0V)" : (net || text(pin?.external_level) || "BOARD"),
  1533. logicalSignal: isGnd ? "GND (参考地)" : `${net || "BOARD_NET"} (${text(pin?.external_level) || "板级网络"})`,
  1534. boardSignal: net,
  1535. peripheralRole: isGnd ? "电源/信号地 (GND)" : (text(pin?.summary) || `${key} (${net})`),
  1536. pinDefinition: text(pin?.summary) || (isGnd ? `${key} 板级公共参考地 (0V GND)` : `${key} 板级电源或外围网络 (${net})`),
  1537. displayBadge: isGnd ? "GND · 0V 参考地" : `板级网络 · ${net || text(pin?.external_level) || "电源"}`,
  1538. tags: [isGnd ? "GND" : "POWER"],
  1539. isUsed: false,
  1540. };
  1541. }
  1542. /**
  1543. * Classify a touchpoint/connector into its peripheral type, usage status (used vs spare),
  1544. * quick-filter tags, and enriched pin rows with explicit FPGA/MCU logical pin mappings.
  1545. */
  1546. export function classifyTouchpointPeripheral(touchpoint, boardVersion = "v2_0") {
  1547. const ver = normalizeBoardVersion(boardVersion);
  1548. const ref = text(touchpoint?.reference).trim();
  1549. const summary = text(touchpoint?.summary).trim();
  1550. const rawPins = (touchpoint?.pins ?? []).filter(
  1551. (p) => p?.mechanical !== true && p?.pad_role !== "shield" && p?.pad_role !== "mechanical" && !text(p?.number).startsWith("SH"),
  1552. );
  1553. const resolvedPins = rawPins.map((pin) => {
  1554. const logical = resolveLogicalPin(touchpoint, pin, ver);
  1555. return {
  1556. pin,
  1557. pinNumber: text(pin?.number),
  1558. net: text(pin?.net),
  1559. direction: text(pin?.direction),
  1560. level: text(pin?.external_level),
  1561. logical,
  1562. };
  1563. });
  1564. const tags = new Set([ref]);
  1565. for (const rp of resolvedPins) {
  1566. for (const t of rp.logical.tags ?? []) {
  1567. tags.add(t);
  1568. }
  1569. }
  1570. let peripheralType = "GPIO";
  1571. let isUsed = false;
  1572. let roleTitle = summary || ref;
  1573. if (ver === "v2_0") {
  1574. if (["CON147", "CON148", "CON149", "CON150"].includes(ref)) {
  1575. peripheralType = "PTC";
  1576. isUsed = true;
  1577. } else if (["CON154", "CON155"].includes(ref)) {
  1578. peripheralType = "FAN";
  1579. isUsed = true;
  1580. } else if (["CON156", "CON157", "CON158"].includes(ref)) {
  1581. peripheralType = "FAN";
  1582. isUsed = false;
  1583. } else if (["CON12", "CON134", "CON77"].includes(ref)) {
  1584. peripheralType = "UART";
  1585. isUsed = true;
  1586. } else if (["CON70", "CON103", "CON170"].includes(ref)) {
  1587. peripheralType = "UART";
  1588. isUsed = false;
  1589. } else if (["CON165", "CON164"].includes(ref)) {
  1590. peripheralType = "SSR";
  1591. isUsed = true;
  1592. } else if (ref === "CON146") {
  1593. peripheralType = "GPIO";
  1594. isUsed = true;
  1595. } else if (/^CON(53|55|56|57|79|80|81|82)$/.test(ref)) {
  1596. peripheralType = "TEMP";
  1597. isUsed = true;
  1598. } else if (["CON159", "CON160", "CON161", "CON162", "CON163"].includes(ref)) {
  1599. peripheralType = "MOTOR";
  1600. isUsed = false;
  1601. } else if (["J3", "J19", "J25"].includes(ref)) {
  1602. peripheralType = "JTAG";
  1603. isUsed = ref !== "J25";
  1604. } else if (["CON168", "CON172", "J29", "J30"].includes(ref)) {
  1605. peripheralType = "MCU";
  1606. isUsed = false;
  1607. } else if (["CON173", "CON174"].includes(ref) || ref.startsWith("PL") || ref.startsWith("TP")) {
  1608. peripheralType = "POWER";
  1609. isUsed = true;
  1610. } else {
  1611. peripheralType = "GPIO";
  1612. isUsed = false;
  1613. }
  1614. } else if (ver === "v2_0_fan") {
  1615. if (["CON168", "CON8", "CON169", "CON170", "CON171"].includes(ref)) {
  1616. peripheralType = "FAN";
  1617. isUsed = true;
  1618. } else {
  1619. peripheralType = "POWER";
  1620. isUsed = true;
  1621. }
  1622. } else if (ver === "v1_2") {
  1623. if (["CON18", "CON27", "CON28", "CON29"].includes(ref)) {
  1624. peripheralType = "PTC";
  1625. isUsed = true;
  1626. } else if (["CON25", "CON8", "CON24", "CON26"].includes(ref)) {
  1627. peripheralType = "FAN";
  1628. isUsed = true;
  1629. } else if (["CON31", "CON32", "CON33", "CON34", "CON35", "CON68"].includes(ref)) {
  1630. peripheralType = "FAN";
  1631. isUsed = false;
  1632. } else if (["CON140", "CON78", "CON77"].includes(ref)) {
  1633. peripheralType = "UART";
  1634. isUsed = true;
  1635. } else if (["CON70", "CON102", "CON103", "CON134", "CON135"].includes(ref)) {
  1636. peripheralType = "UART";
  1637. isUsed = false;
  1638. } else if (ref in V12_MOTOR_MAP || ref === "CON120") {
  1639. peripheralType = "MOTOR";
  1640. isUsed = true;
  1641. } else if (["CON116", "CON117", "CON118", "CON119", "CON124", "CON125", "CON142"].includes(ref)) {
  1642. peripheralType = "MOTOR";
  1643. isUsed = false;
  1644. } else if (["CON12", "CON126", "CON127", "CON128", "CON129", "CON138"].includes(ref)) {
  1645. peripheralType = "LIMIT";
  1646. isUsed = true;
  1647. } else if (["J3", "J19", "J25"].includes(ref)) {
  1648. peripheralType = "JTAG";
  1649. isUsed = ref !== "J25";
  1650. } else if (/^CON(53|55|56|57|79|8[0-9]|9[0-8])$/.test(ref)) {
  1651. peripheralType = "TEMP";
  1652. isUsed = true;
  1653. } else if (["CON101", "CON130", "CON131", "CON132", "CON133", "J29", "J30"].includes(ref)) {
  1654. peripheralType = "MCU";
  1655. isUsed = false;
  1656. } else if (ref === "CON146") {
  1657. peripheralType = "GPIO";
  1658. isUsed = true;
  1659. } else if (["CON2", "CON23"].includes(ref) || ref.startsWith("PL") || ref.startsWith("TP")) {
  1660. peripheralType = "POWER";
  1661. isUsed = true;
  1662. } else {
  1663. peripheralType = "GPIO";
  1664. isUsed = false;
  1665. }
  1666. } else {
  1667. // v1_1 (6400 V1.1)
  1668. if (["CON18", "CON27", "CON28", "CON29"].includes(ref)) {
  1669. peripheralType = "PTC";
  1670. isUsed = true;
  1671. } else if (["CON25", "CON8", "CON24", "CON26", "CON64", "CON66", "CON62", "CON60"].includes(ref)) {
  1672. peripheralType = "FAN";
  1673. isUsed = true;
  1674. } else if (["CON31", "CON32", "CON33", "CON34", "CON35", "CON68"].includes(ref)) {
  1675. peripheralType = "FAN";
  1676. isUsed = false;
  1677. } else if (["CON30", "CON78", "CON77"].includes(ref)) {
  1678. peripheralType = "UART";
  1679. isUsed = true;
  1680. } else if (["CON70"].includes(ref)) {
  1681. peripheralType = "UART";
  1682. isUsed = false;
  1683. } else if (["J3", "J19", "J25"].includes(ref)) {
  1684. peripheralType = "JTAG";
  1685. isUsed = ref !== "J25";
  1686. } else if (/^CON(53|55|56|57|79|8[0-9]|9[0-8])$/.test(ref)) {
  1687. peripheralType = "TEMP";
  1688. isUsed = true;
  1689. } else if (ref === "CON146") {
  1690. peripheralType = "GPIO";
  1691. isUsed = true;
  1692. } else if (["CON2", "CON23"].includes(ref) || ref.startsWith("PL") || ref.startsWith("TP")) {
  1693. peripheralType = "POWER";
  1694. isUsed = true;
  1695. } else {
  1696. peripheralType = "GPIO";
  1697. isUsed = false;
  1698. }
  1699. }
  1700. // Mark mechanical mounting holes (J8, J9, J11..18, J20..22, J26..27) as non-used mechanical
  1701. if (/^J(8|9|11|12|13|14|17|18|20|21|22|26|27)$/.test(ref)) {
  1702. peripheralType = "POWER";
  1703. isUsed = false;
  1704. }
  1705. const isSpareConnector = !isUsed && (ref.startsWith("CON") || ref.startsWith("J"));
  1706. if (isSpareConnector) {
  1707. tags.add("SPARE");
  1708. tags.add("空余插座");
  1709. }
  1710. return {
  1711. reference: ref,
  1712. touchpoint,
  1713. peripheralType,
  1714. peripheralLabel: PERIPHERAL_TYPE_LABELS[peripheralType] ?? peripheralType,
  1715. isUsed,
  1716. isSpareConnector,
  1717. roleTitle,
  1718. tags: Array.from(tags),
  1719. pins: resolvedPins,
  1720. };
  1721. }
  1722. /**
  1723. * Build the complete peripheral & logical pin catalog for a board dataset.
  1724. */
  1725. export function buildPeripheralIndex(data, boardVersion = "v2_0") {
  1726. const ver = normalizeBoardVersion(boardVersion, data);
  1727. const items = (data?.touchpoints ?? [])
  1728. .map((tp) => classifyTouchpointPeripheral(tp, ver))
  1729. .sort((a, b) => {
  1730. if (a.isUsed !== b.isUsed) return a.isUsed ? -1 : 1;
  1731. const typeIdxA = PERIPHERAL_TYPE_ORDER.indexOf(a.peripheralType);
  1732. const typeIdxB = PERIPHERAL_TYPE_ORDER.indexOf(b.peripheralType);
  1733. if (typeIdxA !== typeIdxB) return typeIdxA - typeIdxB;
  1734. return naturalCompare(a.reference, b.reference);
  1735. });
  1736. const categoryCounts = {ALL: items.length};
  1737. for (const cat of PERIPHERAL_TYPE_ORDER) {
  1738. if (cat !== "ALL") categoryCounts[cat] = 0;
  1739. }
  1740. let usedCount = 0;
  1741. let spareCount = 0;
  1742. for (const item of items) {
  1743. categoryCounts[item.peripheralType] = (categoryCounts[item.peripheralType] ?? 0) + 1;
  1744. if (item.isUsed) usedCount += 1;
  1745. if (item.isSpareConnector) spareCount += 1;
  1746. }
  1747. // Build quick-jump chips tailored to the board
  1748. const quickChips = [];
  1749. if (ver === "v1_2") {
  1750. quickChips.push(
  1751. {label: "HOST TTL (CON140)", category: "UART", query: "CON140"},
  1752. {label: "HOST RS232 (CON78)", category: "UART", query: "CON78"},
  1753. {label: "BMC RS232 (CON77)", category: "UART", query: "CON77"},
  1754. {label: "JTAG (J3)", category: "JTAG", query: "J3"},
  1755. {label: "PTC1..6 (CON18)", category: "PTC", query: "CON18"},
  1756. {label: "PTC7..12 (CON27)", category: "PTC", query: "CON27"},
  1757. {label: "FAN1..4 (全部风扇)", category: "FAN", usage: "used", query: ""},
  1758. {label: "电机 M1..M12", category: "MOTOR", usage: "used", query: ""},
  1759. {label: "限位 LS1..12/HS1..12", category: "LIMIT", usage: "used", query: ""},
  1760. {label: "空余插座", category: "ALL", usage: "spare", query: ""},
  1761. );
  1762. } else if (ver === "v2_0") {
  1763. quickChips.push(
  1764. {label: "HOST TTL (CON12)", category: "UART", query: "CON12"},
  1765. {label: "HOST RS232 (CON134)", category: "UART", query: "CON134"},
  1766. {label: "BMC RS232 (CON77)", category: "UART", query: "CON77"},
  1767. {label: "JTAG (J3)", category: "JTAG", query: "J3"},
  1768. {label: "PTC1", category: "PTC", query: "PTC1"},
  1769. {label: "PTC 1#腔 (CON147)", category: "PTC", query: "CON147"},
  1770. {label: "PTC 2#腔 (CON148)", category: "PTC", query: "CON148"},
  1771. {label: "PTC 3#腔 (CON149)", category: "PTC", query: "CON149"},
  1772. {label: "PTC 4#腔 (CON150)", category: "PTC", query: "CON150"},
  1773. {label: "FAN 全部风扇", category: "FAN", usage: "used", query: ""},
  1774. {label: "SSR/急停/开关罩 (CON165)", category: "SSR", query: "CON165"},
  1775. {label: "蜂鸣器 4kHz (CON146)", category: "GPIO", query: "CON146"},
  1776. {label: "JTAG / SWD", category: "JTAG", query: ""},
  1777. {label: "空余插座", category: "ALL", usage: "spare", query: ""},
  1778. );
  1779. } else if (ver === "v2_0_fan") {
  1780. quickChips.push(
  1781. {label: "控制输入 (CON168)", category: "FAN", query: "CON168"},
  1782. {label: "FAN1 (CON8)", category: "FAN", query: "CON8"},
  1783. {label: "FAN2 (CON169)", category: "FAN", query: "CON169"},
  1784. {label: "FAN3 (CON170)", category: "FAN", query: "CON170"},
  1785. {label: "FAN4 (CON171)", category: "FAN", query: "CON171"},
  1786. {label: "+12V 电源 (CON2/172)", category: "POWER", query: ""},
  1787. );
  1788. } else {
  1789. quickChips.push(
  1790. {label: "HOST TTL (CON30)", category: "UART", query: "CON30"},
  1791. {label: "HOST RS232 (CON78)", category: "UART", query: "CON78"},
  1792. {label: "BMC RS232 (CON77)", category: "UART", query: "CON77"},
  1793. {label: "JTAG (J3)", category: "JTAG", query: "J3"},
  1794. {label: "PTC1", category: "PTC", query: "PTC1"},
  1795. {label: "PTC1..24 全部", category: "PTC", usage: "used", query: ""},
  1796. {label: "FAN 前/后全部", category: "FAN", usage: "used", query: ""},
  1797. {label: "空余插座", category: "ALL", usage: "spare", query: ""},
  1798. );
  1799. }
  1800. return {
  1801. boardVersion: ver,
  1802. items,
  1803. categoryCounts,
  1804. usedCount,
  1805. spareCount,
  1806. quickChips,
  1807. };
  1808. }
  1809. /**
  1810. * Filter the peripheral catalog by category, usage ("all" | "used" | "spare"), and query string.
  1811. */
  1812. export function filterPeripheralCatalog(data, boardVersion = "v2_0", options = {}) {
  1813. const index = buildPeripheralIndex(data, boardVersion);
  1814. const category = text(options.category || "ALL").toUpperCase();
  1815. const usage = text(options.usage || "all").toLowerCase();
  1816. const rawQuery = text(options.query || "").trim();
  1817. const needle = normalized(rawQuery);
  1818. const exactTag = rawQuery.toUpperCase();
  1819. const filtered = index.items.filter((item) => {
  1820. if (category !== "ALL" && item.peripheralType !== category) return false;
  1821. if (usage === "used" && !item.isUsed) return false;
  1822. if (usage === "spare" && !item.isSpareConnector) return false;
  1823. if (!needle) return true;
  1824. // Check exact tag match first (e.g., "PTC1" should not match "PTC10" if exact tag exists)
  1825. if (/^(PTC|FAN|M|LS|HS|SSR|TEMP)\d+$/i.test(rawQuery)) {
  1826. const hasExactTag = item.tags.some((t) => t.toUpperCase() === exactTag) ||
  1827. item.pins.some((p) => (p.logical.tags ?? []).some((t) => t.toUpperCase() === exactTag));
  1828. if (hasExactTag) return true;
  1829. return false;
  1830. }
  1831. const haystack = normalized([
  1832. item.reference,
  1833. item.peripheralType,
  1834. item.peripheralLabel,
  1835. item.roleTitle,
  1836. ...item.tags,
  1837. ...item.pins.flatMap((p) => [
  1838. `${item.reference}.${p.pinNumber}`,
  1839. p.net,
  1840. p.logical.physicalPin,
  1841. p.logical.logicalSignal,
  1842. p.logical.boardSignal,
  1843. p.logical.peripheralRole,
  1844. p.logical.pinDefinition,
  1845. p.logical.displayBadge,
  1846. ]),
  1847. ].join(" "));
  1848. return haystack.includes(needle);
  1849. });
  1850. // If filtering by specific pin tag (like PTC1, M1, LS1), highlight matching pins
  1851. const matchingItems = filtered.map((item) => {
  1852. if (!needle) return {...item, matchedPinNumbers: []};
  1853. const matchedPinNumbers = item.pins
  1854. .filter((p) => {
  1855. if (/^(PTC|FAN|M|LS|HS|SSR|TEMP)\d+$/i.test(rawQuery)) {
  1856. return (p.logical.tags ?? []).some((t) => t.toUpperCase() === exactTag);
  1857. }
  1858. const pinHaystack = normalized([
  1859. `${item.reference}.${p.pinNumber}`,
  1860. p.net,
  1861. p.logical.physicalPin,
  1862. p.logical.logicalSignal,
  1863. p.logical.boardSignal,
  1864. p.logical.peripheralRole,
  1865. p.logical.pinDefinition,
  1866. p.logical.displayBadge,
  1867. ].join(" "));
  1868. return pinHaystack.includes(needle);
  1869. })
  1870. .map((p) => p.pinNumber);
  1871. return {...item, matchedPinNumbers};
  1872. });
  1873. return {
  1874. ...index,
  1875. filteredItems: matchingItems,
  1876. };
  1877. }