Skip to content

Commit 43464be

Browse files
committed
Add portio alternative
1 parent 0276d13 commit 43464be

File tree

2 files changed

+192
-1
lines changed

2 files changed

+192
-1
lines changed

cros_ec_python/devices/lpc.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import struct
2-
import portio
32
import warnings
43
import errno
54
from ..baseclass import CrosEcClass
@@ -9,6 +8,13 @@
98
from ..exceptions import ECError
109

1110

11+
try:
12+
import portio
13+
except ImportError as e:
14+
warnings.warn(f"Failed to import portio: {e}, using /dev/port instead.", ImportWarning)
15+
from ..utils import devportio as portio
16+
17+
1218
class CrosEcLpc(CrosEcClass):
1319
def __init__(self, init: bool = True, address: Int32 = None):
1420
self.address = address

cros_ec_python/utils/devportio.py

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# This file provides a way to interact with the /dev/port device file as an alternative to the portio library.
2+
# This is a lot slower than the portio library, but it doesn't require an extra package.
3+
4+
def out_bytes(data: bytes, port: int) -> None:
5+
"""
6+
Write data to the specified port.
7+
@param data: Data to write.
8+
@param port: Port to write to.
9+
"""
10+
with open("/dev/port", "wb") as f:
11+
f.seek(port)
12+
f.write(data)
13+
14+
15+
def outb(data: int, port: int) -> None:
16+
"""
17+
Write a byte to the specified port.
18+
@param data: Byte to write.
19+
@param port: Port to write to.
20+
"""
21+
out_bytes(data.to_bytes(1, "little"), port)
22+
23+
24+
outb_p = outb
25+
26+
27+
def outw(data: int, port: int) -> None:
28+
"""
29+
Write a word to the specified port.
30+
@param data: Word to write.
31+
@param port: Port to write to.
32+
"""
33+
out_bytes(data.to_bytes(2, "little"), port)
34+
35+
36+
outw_p = outw
37+
38+
39+
def outl(data: int, port: int) -> None:
40+
"""
41+
Write a long to the specified port.
42+
@param data: Long to write.
43+
@param port: Port to write to.
44+
"""
45+
out_bytes(data.to_bytes(4, "little"), port)
46+
47+
48+
outl_p = outl
49+
50+
51+
def outsb(data: int, port: int, count: int) -> None:
52+
"""
53+
Write a byte to the specified port, multiple times.
54+
@param data: Byte to write.
55+
@param port: Port to write to.
56+
@param count: Number of times to write.
57+
"""
58+
for i in range(count):
59+
outb(data, port)
60+
61+
62+
def outsw(data: int, port: int, count: int) -> None:
63+
"""
64+
Write a word to the specified port, multiple times.
65+
@param data: Word to write.
66+
@param port: Port to write to.
67+
@param count: Number of times to write.
68+
"""
69+
for i in range(count):
70+
outw(data, port)
71+
72+
73+
def outsl(data: int, port: int, count: int) -> None:
74+
"""
75+
Write a long to the specified port, multiple times.
76+
@param data: Long to write.
77+
@param port: Port to write to.
78+
@param count: Number of times to write.
79+
"""
80+
for i in range(count):
81+
outl(data, port)
82+
83+
84+
def in_bytes(port: int, num: int) -> bytes:
85+
"""
86+
Read data from the specified port.
87+
@param port: Port to read from.
88+
@param num: Number of bytes to read.
89+
@return: Data read.
90+
"""
91+
with open("/dev/port", "rb") as f:
92+
f.seek(port)
93+
return f.read(num)
94+
95+
96+
def inb(port: int) -> int:
97+
"""
98+
Read a byte from the specified port.
99+
@param port: Port to read from.
100+
@return: Byte read.
101+
"""
102+
return int.from_bytes(in_bytes(port, 1), "little")
103+
104+
105+
inb_p = inb
106+
107+
108+
def inw(port: int) -> int:
109+
"""
110+
Read a word from the specified port.
111+
@param port: Port to read from.
112+
@return: Word read.
113+
"""
114+
return int.from_bytes(in_bytes(port, 2), "little")
115+
116+
117+
inw_p = inw
118+
119+
120+
def inl(port: int) -> int:
121+
"""
122+
Read a long from the specified port.
123+
@param port: Port to read from.
124+
@return: Long read.
125+
"""
126+
return int.from_bytes(in_bytes(port, 4), "little")
127+
128+
129+
inl_p = inl
130+
131+
132+
def insb(port: int, data: bytearray, count: int) -> None:
133+
"""
134+
Read a byte from the specified port, multiple times.
135+
@param port: Port to read from.
136+
@param data: Buffer to read into.
137+
@param count: Number of times to read.
138+
"""
139+
for i in range(count):
140+
if i >= len(data):
141+
data.append(inb(port))
142+
else:
143+
data[i] = inb(port)
144+
145+
146+
def insw(port: int, data: bytearray, count: int) -> None:
147+
"""
148+
Read a word from the specified port, multiple times.
149+
@param port: Port to read from.
150+
@param data: Buffer to read into.
151+
@param count: Number of times to read.
152+
"""
153+
for i in range(count):
154+
if i >= len(data):
155+
data.append(inw(port))
156+
else:
157+
data[i] = inw(port)
158+
159+
160+
def insl(port: int, data: bytearray, count: int) -> None:
161+
"""
162+
Read a long from the specified port, multiple times.
163+
@param port: Port to read from.
164+
@param data: Buffer to read into.
165+
@param count: Number of times to read.
166+
"""
167+
for i in range(count):
168+
if i >= len(data):
169+
data.append(inl(port))
170+
else:
171+
data[i] = inl(port)
172+
173+
174+
def ioperm(port: int, num: int, turn_on: bool) -> None:
175+
"""
176+
ioperm stub function. It's not required for /dev/port.
177+
"""
178+
pass
179+
180+
181+
def iopl(level: int) -> None:
182+
"""
183+
iopl stub function. It's not required for /dev/port.
184+
"""
185+
pass

0 commit comments

Comments
 (0)