-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsetup-gpio-sim.py
More file actions
83 lines (63 loc) · 1.87 KB
/
setup-gpio-sim.py
File metadata and controls
83 lines (63 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import json
import pathlib
import sys
mounts = pathlib.Path("/proc/mounts")
for dtype, mpath, fstype, *_ in mounts.read_text().splitlines():
if dtype == "configfs" and fstype == "configfs":
CONFIGFS_PATH = pathlib.Path(mpath)
else:
CONFIGFS_PATH = pathlib.Path("/sys/kernel/config/gpio-sim")
def Line(name, direction=None):
result = {"name": name}
if direction:
result["hog"] = {"name": f"{name}-hog", "direction": direction}
return result
DEFAULT = {
"name": "chip99",
"banks": [
{
"lines": [
Line("L-I0"),
Line("L-I1"),
Line("L-I2", "input"),
Line("L-O0", "output-high"),
Line("L-O1", "output-low"),
Line("L-O2"),
]
},
],
}
if len(sys.argv) > 1:
cfg = json.load(sys.argv[-1])
else:
cfg = DEFAULT
def mkdir(path):
print(f"Creating {path}")
path.mkdir()
def cleanup(path):
# clean up first
if path.exists():
live.write_text("0")
for directory, _, _ in path.walk(top_down=False):
directory.rmdir()
path = CONFIGFS_PATH / cfg["name"]
live = path / "live"
cleanup(path)
mkdir(path)
for bank_id, bank in enumerate(cfg["banks"]):
lines = bank["lines"]
bpath = path / f"gpio-bank{bank_id}"
mkdir(bpath)
blabel = bank.get("name", f"gpio-sim-bank{bank_id}")
(bpath / "num_lines").write_text("16")
(bpath / "label").write_text(blabel)
for line_id, line in enumerate(lines):
lpath = bpath / f"line{line_id}"
mkdir(lpath)
(lpath / "name").write_text(line.get("name", f"L-{line_id}"))
if hog := line.get("hog"):
hpath = lpath / "hog"
mkdir(hpath)
(hpath / "name").write_text(hog["name"])
(hpath / "direction").write_text(hog["direction"])
live.write_text("1")