-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add DRC check to prevent I2C SDA and SCL from connecting #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
techmannih
wants to merge
9
commits into
tscircuit:main
Choose a base branch
from
techmannih:jk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bb11088
feat: add DRC constraint to prevent I2C SDA and SCL from connecting
techmannih 5e375a7
j
techmannih fac8b37
j
techmannih e2407e2
update
techmannih a588b68
Rename function to checkI2cSdaConnectedToSclMisconfigured and improve…
techmannih f82f71d
Further refine error message for I2C SDA connected to SCL
techmannih f78a1c2
up
techmannih 4df7974
Fix duplicate port IDs in conflictingPortIds array
techmannih 1270064
up
techmannih File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import type { | ||
| AnyCircuitElement, | ||
| SourceI2cMisconfiguredError, | ||
| } from "circuit-json" | ||
| import { getSourcePortConnectivityMapFromCircuitJson } from "circuit-json-to-connectivity-map" | ||
|
|
||
| export function checkI2cMisconfigured( | ||
| circuitJson: AnyCircuitElement[], | ||
| ): SourceI2cMisconfiguredError[] { | ||
| const errors: SourceI2cMisconfiguredError[] = [] | ||
|
|
||
| // Get all source ports to easily look up their attributes | ||
| const sourcePorts = circuitJson.filter( | ||
| (el): el is Extract<AnyCircuitElement, { type: "source_port" }> => | ||
| el.type === "source_port", | ||
| ) | ||
| const portMap = new Map(sourcePorts.map((p) => [p.source_port_id, p])) | ||
|
|
||
| const connMap = getSourcePortConnectivityMapFromCircuitJson(circuitJson) | ||
|
|
||
| for (const [netId, connectedPortIds] of Object.entries(connMap.netMap)) { | ||
| let hasSda = false | ||
| let hasScl = false | ||
|
|
||
| const conflictingPortIds: string[] = [] | ||
|
|
||
| for (const portId of connectedPortIds) { | ||
| const port = portMap.get(portId) | ||
| if (!port) continue | ||
|
|
||
| if (port.is_configured_for_i2c_sda) { | ||
| hasSda = true | ||
| conflictingPortIds.push(portId) | ||
| } | ||
| if (port.is_configured_for_i2c_scl) { | ||
| hasScl = true | ||
| conflictingPortIds.push(portId) | ||
| } | ||
| } | ||
|
|
||
| if (hasSda && hasScl) { | ||
| errors.push({ | ||
| type: "source_i2c_misconfigured_error", | ||
| source_i2c_misconfigured_error_id: `source_i2c_misconfigured_error_${netId}`, | ||
techmannih marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| error_type: "source_i2c_misconfigured_error", | ||
| message: "I2C SDA and SCL pins are connected together", | ||
techmannih marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| source_port_ids: conflictingPortIds, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| return errors | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { expect, test } from "bun:test" | ||
| import { checkI2cMisconfigured } from "../../lib/check-i2c-misconfigured" | ||
| import type { AnyCircuitElement } from "circuit-json" | ||
|
|
||
| test("checkI2cMisconfigured detects SDA connected to SCL", () => { | ||
| const circuitJson: AnyCircuitElement[] = [ | ||
| { | ||
| type: "source_port", | ||
| source_port_id: "port_sda", | ||
| name: "SDA", | ||
| is_configured_for_i2c_sda: true, | ||
| }, | ||
| { | ||
| type: "source_port", | ||
| source_port_id: "port_scl", | ||
| name: "SCL", | ||
| is_configured_for_i2c_scl: true, | ||
| }, | ||
| { | ||
| type: "source_trace", | ||
| source_trace_id: "trace_1", | ||
| connected_source_port_ids: ["port_sda", "port_scl"], | ||
| connected_source_net_ids: [], | ||
| }, | ||
| ] as AnyCircuitElement[] | ||
|
|
||
| const errors = checkI2cMisconfigured(circuitJson) | ||
| expect(errors).toHaveLength(1) | ||
| expect(errors[0].message).toEqual(expect.stringContaining("I2C")) | ||
| expect(errors[0].source_port_ids).toContain("port_sda") | ||
| expect(errors[0].source_port_ids).toContain("port_scl") | ||
| }) | ||
|
|
||
| test("checkI2cMisconfigured allows SDA connected to SDA", () => { | ||
| const circuitJson: AnyCircuitElement[] = [ | ||
| { | ||
| type: "source_port", | ||
| source_port_id: "port_sda1", | ||
| name: "SDA1", | ||
| is_configured_for_i2c_sda: true, | ||
| }, | ||
| { | ||
| type: "source_port", | ||
| source_port_id: "port_sda2", | ||
| name: "SDA2", | ||
| is_configured_for_i2c_sda: true, | ||
| }, | ||
| { | ||
| type: "source_trace", | ||
| source_trace_id: "trace_1", | ||
| connected_source_port_ids: ["port_sda1", "port_sda2"], | ||
| connected_source_net_ids: [], | ||
| }, | ||
| ] as AnyCircuitElement[] | ||
|
|
||
| const errors = checkI2cMisconfigured(circuitJson) | ||
| expect(errors).toHaveLength(0) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.