Skip to content

Commit 6b93dbb

Browse files
committed
Improve documentation for Windows
1 parent 5c251bf commit 6b93dbb

File tree

5 files changed

+98
-36
lines changed

5 files changed

+98
-36
lines changed

cros_ec_python/baseclass.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
It doesn't do anything on its own, but it is used as a base for all CrosEc classes to inherit from.
55
6-
See `cros_ec_python.devices` for a few examples of a classes that inherits from this class.
6+
See `cros_ec_python.devices` for a few examples of a class that inherits from this class.
77
"""
88

99
import abc

cros_ec_python/devices/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
If you don't understand what this does, just use `cros_ec_python.cros_ec.get_cros_ec()` to pick an interface.
77
88
All device class implementations should inherit from `cros_ec_python.baseclass.CrosEcClass`,
9-
and implement the methods described in that class. This allows everyone to use the standardised
9+
and implement the methods described in that class. This allows everything to use the standardised
1010
`cros_ec_python.baseclass.CrosEcClass.command` method to send commands to the EC, and
1111
`cros_ec_python.baseclass.CrosEcClass.memmap` to read memory from the EC.
1212
@@ -17,17 +17,18 @@
1717
**Initialisation**
1818
1919
```python
20-
from cros_ec_python import get_cros_ec, CrosEcLpc, CrosEcDev
21-
2220
# Pick one of the following:
2321
2422
# Automatically pick the right class
23+
from cros_ec_python import get_cros_ec
2524
ec = get_cros_ec()
2625
2726
# Manually pick LinuxDev
27+
from cros_ec_python import CrosEcDev
2828
ec = CrosEcDev()
2929
3030
# Manually pick LPC
31+
from cros_ec_python import CrosEcLpc
3132
ec = CrosEcLpc()
3233
3334
# Manually pick LPC with a specific address

cros_ec_python/ioports/__init__.py

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,70 @@
11
"""
2-
This module is used to import the correct portio module based on the OS.
2+
# Description
3+
4+
This submodule contains the classes for the different IO port interfaces supported by the library.
5+
6+
If you don't understand what this does, just use `cros_ec_python.ioports.PortIO()` to import the correct class.
7+
8+
All IO port classes should inherit from `cros_ec_python.ioports.baseportio.PortIOClass`,
9+
and implement the methods described in that class. This allows everything to use the standardised
10+
methods to interact with the IO ports in a platform-independent way.
11+
12+
## Examples
13+
14+
**Initialisation**
15+
16+
```python
17+
# Automatically pick the right class (Recommended)
18+
from cros_ec_python.ioports import PortIO
19+
portio = PortIO()
20+
21+
# Manually pick portio library (Linux, preferred)
22+
from cros_ec_python.ioports.x86portio import IoPortIo
23+
portio = IoPortIo()
24+
25+
# Manually pick `/dev/port` (Linux, fallback)
26+
from cros_ec_python.ioports.devportio import DevPortIO
27+
portio = DevPortIO()
28+
29+
# Manually pick WinRing0 (Windows)
30+
from cros_ec_python.ioports.winportio import WinPortIO
31+
portio = WinPortIO()
32+
```
33+
34+
**Reading from a port**
35+
36+
```python
37+
# Assuming `portio` is already initialised
38+
data = portio.inb(0x80)
39+
print(data)
40+
```
41+
42+
**Writing to a port**
43+
44+
```python
45+
# Assuming `portio` is already initialised
46+
portio.outb(0x55, 0x80)
47+
```
48+
49+
See `cros_ec_python.ioports.baseportio.PortIOClass` for the full list of common methods.
50+
51+
Some classes may have additional methods, such as `cros_ec_python.ioports.x86portio.IoPortIo`.
352
"""
453

554
import os
655

7-
if os.name == "nt":
8-
from .winportio import WinPortIO as PortIO
9-
elif os.name == "posix":
56+
if os.name == "posix":
1057
try:
1158
from .x86portio import IoPortIo as PortIO
1259
except ImportError:
1360
from .devportio import DevPortIO as PortIO
61+
elif os.name == "nt":
62+
from .winportio import WinPortIO as PortIO
1463
else:
1564
raise Exception("Unsupported OS")
65+
66+
# This is for pdoc
67+
from .baseportio import PortIOClass
68+
69+
PortIO: PortIOClass
70+
"Automatically picked IO port class."

cros_ec_python/ioports/baseportio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
It doesn't do anything on its own, but it is used as a base for all port I/O backends to inherit from.
55
6-
See `cros_ec_python.io` for a few examples of a classes that inherits from this class.
6+
See `cros_ec_python.ioports` for a few examples of a class that inherits from this class.
77
"""
88

99
import abc

cros_ec_python/ioports/winportio.py

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from enum import Enum
99
import ctypes
10+
from ctypes import wintypes
1011

1112
from .baseportio import PortIOClass
1213

@@ -16,27 +17,28 @@ class WinPortIO(PortIOClass):
1617
A class to interact with the WinRing0 library for port I/O on Windows.
1718
"""
1819

19-
winring0 = None
20+
winring0: "ctypes.WinDLL | None" = None
21+
"The WinRing0 library instance."
2022

2123
def __init__(self):
2224
"""
2325
Load and initialize the WinRing0 library.
2426
"""
2527
self.winring0 = ctypes.WinDLL("WinRing0x64.dll")
26-
self.winring0.InitializeOls.restype = ctypes.c_bool
27-
self.winring0.GetDllStatus.restype = ctypes.c_ulong
28+
self.winring0.InitializeOls.restype = wintypes.BOOL
29+
self.winring0.GetDllStatus.restype = wintypes.DWORD
2830
self.winring0.DeinitializeOls.restype = None
2931
# ReadIoPort (port)
30-
self.winring0.ReadIoPortByte.restype = ctypes.c_ubyte
31-
self.winring0.ReadIoPortByte.argtypes = [ctypes.c_ushort]
32-
self.winring0.ReadIoPortWord.restype = ctypes.c_ushort
33-
self.winring0.ReadIoPortWord.argtypes = [ctypes.c_ushort]
34-
self.winring0.ReadIoPortDword.restype = ctypes.c_ulong
35-
self.winring0.ReadIoPortDword.argtypes = [ctypes.c_ushort]
32+
self.winring0.ReadIoPortByte.restype = wintypes.BYTE
33+
self.winring0.ReadIoPortByte.argtypes = [wintypes.WORD]
34+
self.winring0.ReadIoPortWord.restype = wintypes.WORD
35+
self.winring0.ReadIoPortWord.argtypes = [wintypes.WORD]
36+
self.winring0.ReadIoPortDword.restype = wintypes.DWORD
37+
self.winring0.ReadIoPortDword.argtypes = [wintypes.WORD]
3638
# WriteIoPort (port, data)
37-
self.winring0.WriteIoPortByte.argtypes = [ctypes.c_ushort, ctypes.c_ubyte]
38-
self.winring0.WriteIoPortWord.argtypes = [ctypes.c_ushort, ctypes.c_ushort]
39-
self.winring0.WriteIoPortDword.argtypes = [ctypes.c_ushort, ctypes.c_ulong]
39+
self.winring0.WriteIoPortByte.argtypes = [wintypes.WORD, wintypes.BYTE]
40+
self.winring0.WriteIoPortWord.argtypes = [wintypes.WORD, wintypes.WORD]
41+
self.winring0.WriteIoPortDword.argtypes = [wintypes.WORD, wintypes.DWORD]
4042

4143
self.winring0.InitializeOls()
4244
if error := self.winring0.GetDllStatus():
@@ -49,21 +51,6 @@ def __del__(self):
4951
if self.winring0:
5052
self.winring0.DeinitializeOls()
5153

52-
class Status(Enum):
53-
OLS_DLL_NO_ERROR = 0
54-
OLS_DLL_UNSUPPORTED_PLATFORM = 1
55-
OLS_DLL_DRIVER_NOT_LOADED = 2
56-
OLS_DLL_DRIVER_NOT_FOUND = 3
57-
OLS_DLL_DRIVER_UNLOADED = 4
58-
OLS_DLL_DRIVER_NOT_LOADED_ON_NETWORK = 5
59-
OLS_DLL_UNKNOWN_ERROR = 9
60-
61-
OLS_DLL_DRIVER_INVALID_PARAM = 10
62-
OLS_DLL_DRIVER_SC_MANAGER_NOT_OPENED = 11
63-
OLS_DLL_DRIVER_SC_DRIVER_NOT_INSTALLED = 12
64-
OLS_DLL_DRIVER_SC_DRIVER_NOT_STARTED = 13
65-
OLS_DLL_DRIVER_SC_DRIVER_NOT_REMOVED = 14
66-
6754
def outb(self, data: int, port: int) -> None:
6855
"""
6956
Write a byte (8 bit) to the specified port.
@@ -123,3 +110,22 @@ def iopl(self, level: int) -> None:
123110
`iopl` stub function. It's not required for WinRing0.
124111
"""
125112
pass
113+
114+
class Status(Enum):
115+
"""
116+
WinRing0 status codes.
117+
"""
118+
119+
OLS_DLL_NO_ERROR = 0
120+
OLS_DLL_UNSUPPORTED_PLATFORM = 1
121+
OLS_DLL_DRIVER_NOT_LOADED = 2
122+
OLS_DLL_DRIVER_NOT_FOUND = 3
123+
OLS_DLL_DRIVER_UNLOADED = 4
124+
OLS_DLL_DRIVER_NOT_LOADED_ON_NETWORK = 5
125+
OLS_DLL_UNKNOWN_ERROR = 9
126+
127+
OLS_DLL_DRIVER_INVALID_PARAM = 10
128+
OLS_DLL_DRIVER_SC_MANAGER_NOT_OPENED = 11
129+
OLS_DLL_DRIVER_SC_DRIVER_NOT_INSTALLED = 12
130+
OLS_DLL_DRIVER_SC_DRIVER_NOT_STARTED = 13
131+
OLS_DLL_DRIVER_SC_DRIVER_NOT_REMOVED = 14

0 commit comments

Comments
 (0)