Skip to content

Commit 0276d13

Browse files
committed
Major refactor, use a class for each device type
1 parent 8862d2b commit 0276d13

File tree

24 files changed

+483
-440
lines changed

24 files changed

+483
-440
lines changed

cros_ec_python/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
from .cros_ec import CrOS_EC, DeviceTypes
1+
from .cros_ec import get_cros_ec, DeviceTypes
2+
from .baseclass import CrosEcClass
3+
from .devices.dev import CrosEcDev
4+
from .devices.lpc import CrosEcLpc
25
from .commands import memmap, general, features, pwm, leds, thermal
36
from .exceptions import ECError

cros_ec_python/baseclass.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import abc
2+
from .constants.COMMON import *
3+
4+
5+
class CrosEcClass(metaclass=abc.ABCMeta):
6+
@staticmethod
7+
@abc.abstractmethod
8+
def detect() -> bool:
9+
"""
10+
Detect the EC type.
11+
"""
12+
pass
13+
14+
@abc.abstractmethod
15+
def ec_init(self) -> None:
16+
"""
17+
Initialise the EC, this shouldn't need to be called unless the keyword arg init was set to false.
18+
"""
19+
pass
20+
21+
@abc.abstractmethod
22+
def ec_exit(self) -> None:
23+
"""
24+
Close connection to the EC.
25+
"""
26+
pass
27+
28+
@abc.abstractmethod
29+
def command(self, version: Int32, command: Int32, outsize: Int32, insize: Int32, data: bytes = None,
30+
warn: bool = True) -> bytes:
31+
"""
32+
Send a command to the EC and return the response.
33+
@param version: Command version number (often 0).
34+
@param command: Command to send (EC_CMD_...).
35+
@param outsize: Outgoing length in bytes.
36+
@param insize: Max number of bytes to accept from the EC.
37+
@param data: Outgoing data to EC.
38+
@param warn: Whether to warn if the response size is not as expected. Default is True.
39+
@return: Response from the EC.
40+
"""
41+
pass
42+
43+
@abc.abstractmethod
44+
def memmap(self, offset: Int32, num_bytes: Int32) -> bytes:
45+
"""
46+
Read memory from the EC.
47+
@param offset: Offset to read from.
48+
@param num_bytes: Number of bytes to read.
49+
@return: Bytes read from the EC.
50+
"""
51+
pass

cros_ec_python/commands/features.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Final
22
from enum import Enum
33
import struct
4-
from ..cros_ec import CrOS_EC
4+
from ..baseclass import CrosEcClass
55
from ..constants.COMMON import *
66

77
EC_CMD_GET_FEATURES: Final = 0x000D
@@ -139,7 +139,7 @@ class EcFeatureCode(Enum):
139139
EC_FEATURE_TYPEC_AP_VDM_SEND = 46
140140

141141

142-
def get_features(ec: CrOS_EC) -> UInt64:
142+
def get_features(ec: CrosEcClass) -> UInt64:
143143
"""
144144
List the features supported by the firmware
145145
@param ec: The CrOS_EC object.

cros_ec_python/commands/general.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from typing import Final, Literal
22
import struct
3-
from ..cros_ec import CrOS_EC
3+
from ..baseclass import CrosEcClass
44
from ..constants.COMMON import *
55

66
EC_CMD_PROTO_VERSION: Final = 0x0000
77

88

9-
def proto_version(ec: CrOS_EC) -> UInt32:
9+
def proto_version(ec: CrosEcClass) -> UInt32:
1010
"""
1111
Get protocol version, used to deal with non-backward compatible protocol changes.
1212
@param ec: The CrOS_EC object.
@@ -19,7 +19,7 @@ def proto_version(ec: CrOS_EC) -> UInt32:
1919
EC_CMD_HELLO: Final = 0x0001
2020

2121

22-
def hello(ec: CrOS_EC, in_data: UInt32) -> UInt32:
22+
def hello(ec: CrosEcClass, in_data: UInt32) -> UInt32:
2323
"""
2424
Hello. This is a simple command to test the EC is responsive to commands.
2525
@param ec: The CrOS_EC object.
@@ -34,7 +34,7 @@ def hello(ec: CrOS_EC, in_data: UInt32) -> UInt32:
3434
EC_CMD_GET_VERSION: Final = 0x0002
3535

3636

37-
def get_version(ec: CrOS_EC, version: Literal[0, 1] = 0) -> dict[str, str | int]:
37+
def get_version(ec: CrosEcClass, version: Literal[0, 1] = 0) -> dict[str, str | int]:
3838
"""
3939
Get version number
4040
@param ec: The CrOS_EC object.
@@ -71,7 +71,7 @@ def get_version(ec: CrOS_EC, version: Literal[0, 1] = 0) -> dict[str, str | int]
7171
EC_CMD_GET_BUILD_INFO: Final = 0x0004
7272

7373

74-
def get_build_info(ec: CrOS_EC) -> str:
74+
def get_build_info(ec: CrosEcClass) -> str:
7575
"""
7676
Get build information
7777
@param ec: The CrOS_EC object.
@@ -84,7 +84,7 @@ def get_build_info(ec: CrOS_EC) -> str:
8484
EC_CMD_GET_CHIP_INFO: Final = 0x0005
8585

8686

87-
def get_chip_info(ec: CrOS_EC) -> dict[str, str]:
87+
def get_chip_info(ec: CrosEcClass) -> dict[str, str]:
8888
"""
8989
Get chip info
9090
@param ec: The CrOS_EC object.
@@ -102,7 +102,7 @@ def get_chip_info(ec: CrOS_EC) -> dict[str, str]:
102102
EC_CMD_GET_BOARD_VERSION: Final = 0x0006
103103

104104

105-
def get_board_version(ec: CrOS_EC) -> UInt16:
105+
def get_board_version(ec: CrosEcClass) -> UInt16:
106106
"""
107107
Get board HW version
108108
@param ec: The CrOS_EC object.
@@ -118,7 +118,7 @@ def get_board_version(ec: CrOS_EC) -> UInt16:
118118
EC_CMD_GET_CMD_VERSIONS: Final = 0x0008
119119

120120

121-
def get_cmd_versions(ec: CrOS_EC, cmd: UInt8 | UInt16, version: Literal[0, 1] | None = None) -> UInt32:
121+
def get_cmd_versions(ec: CrosEcClass, cmd: UInt8 | UInt16, version: Literal[0, 1] | None = None) -> UInt32:
122122
"""
123123
Read versions supported for a command.
124124
@param ec: The CrOS_EC object.
@@ -144,7 +144,7 @@ def get_cmd_versions(ec: CrOS_EC, cmd: UInt8 | UInt16, version: Literal[0, 1] |
144144
EC_CMD_TEST_PROTOCOL: Final = 0x000A
145145

146146

147-
def test_protocol(ec: CrOS_EC, result: UInt32, ret_len: UInt32, buf: bytes, in_size: Int32 | None = None) -> bytes:
147+
def test_protocol(ec: CrosEcClass, result: UInt32, ret_len: UInt32, buf: bytes, in_size: Int32 | None = None) -> bytes:
148148
"""
149149
Fake a variety of responses, purely for testing purposes.
150150
@param ec: The CrOS_EC object.
@@ -162,7 +162,7 @@ def test_protocol(ec: CrOS_EC, result: UInt32, ret_len: UInt32, buf: bytes, in_s
162162
EC_CMD_GET_PROTOCOL_INFO: Final = 0x000B
163163

164164

165-
def get_protocol_info(ec: CrOS_EC) -> dict[str, int]:
165+
def get_protocol_info(ec: CrosEcClass) -> dict[str, int]:
166166
"""
167167
Get protocol info
168168
@param ec: The CrOS_EC object.

cros_ec_python/commands/leds.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Final
22
from enum import Enum, auto
33
import struct
4-
from ..cros_ec import CrOS_EC
4+
from ..baseclass import CrosEcClass
55
from ..constants.COMMON import *
66

77
EC_CMD_LED_CONTROL: Final = 0x0029
@@ -45,7 +45,7 @@ class EcLedColors(Enum):
4545
EC_LED_COLOR_COUNT = auto()
4646

4747

48-
def led_control(ec: CrOS_EC, led_id: EcLedId, flags: UInt8, brightnesses: list[UInt8]) -> list[UInt8]:
48+
def led_control(ec: CrosEcClass, led_id: EcLedId, flags: UInt8, brightnesses: list[UInt8]) -> list[UInt8]:
4949
"""
5050
Control an LED
5151
@param ec: The CrOS_EC object.
@@ -59,7 +59,7 @@ def led_control(ec: CrOS_EC, led_id: EcLedId, flags: UInt8, brightnesses: list[U
5959
return list(resp)
6060

6161

62-
def led_control_set_color(ec: CrOS_EC, led_id: EcLedId, brightness: UInt8, color: EcLedColors) -> list[UInt8]:
62+
def led_control_set_color(ec: CrosEcClass, led_id: EcLedId, brightness: UInt8, color: EcLedColors) -> list[UInt8]:
6363
"""
6464
Control an LED
6565
@param ec: The CrOS_EC object.
@@ -73,7 +73,7 @@ def led_control_set_color(ec: CrOS_EC, led_id: EcLedId, brightness: UInt8, color
7373
return led_control(ec, led_id, 0, brightnesses)
7474

7575

76-
def led_control_get_max_values(ec: CrOS_EC, led_id: EcLedId) -> list[UInt8]:
76+
def led_control_get_max_values(ec: CrosEcClass, led_id: EcLedId) -> list[UInt8]:
7777
"""
7878
Get the current brightness values of an LED
7979
@param ec: The CrOS_EC object.
@@ -83,7 +83,7 @@ def led_control_get_max_values(ec: CrOS_EC, led_id: EcLedId) -> list[UInt8]:
8383
return led_control(ec, led_id, EC_LED_FLAGS_QUERY, [0] * EcLedColors.EC_LED_COLOR_COUNT.value)
8484

8585

86-
def led_control_set_auto(ec: CrOS_EC, led_id: EcLedId) -> list[UInt8]:
86+
def led_control_set_auto(ec: CrosEcClass, led_id: EcLedId) -> list[UInt8]:
8787
"""
8888
Get the current brightness values of an LED
8989
@param ec: The CrOS_EC object.

cros_ec_python/commands/memmap.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import struct
2-
from ..cros_ec import CrOS_EC
2+
from ..baseclass import CrosEcClass
33
from ..constants.COMMON import *
44
from ..constants.MEMMAP import *
55

66

7-
def get_temps(ec: CrOS_EC, adjust: int | float = -273) -> list[int | float]:
7+
def get_temps(ec: CrosEcClass, adjust: int | float = -273) -> list[int | float]:
88
"""
99
Get the temperature of all temp sensors.
1010
@param ec: The CrOS_EC object.
@@ -30,7 +30,7 @@ def get_temps(ec: CrOS_EC, adjust: int | float = -273) -> list[int | float]:
3030
return ret
3131

3232

33-
def get_fans(ec: CrOS_EC) -> list[int | None]:
33+
def get_fans(ec: CrosEcClass) -> list[int | None]:
3434
"""
3535
Get the speed of all fans.
3636
@param ec: The CrOS_EC object.
@@ -46,7 +46,7 @@ def get_fans(ec: CrOS_EC) -> list[int | None]:
4646
return [None if fan is EC_FAN_SPEED_STALLED else fan for fan in fans if fan < EC_FAN_SPEED_NOT_PRESENT]
4747

4848

49-
def get_switches(ec: CrOS_EC) -> dict[str, bool]:
49+
def get_switches(ec: CrosEcClass) -> dict[str, bool]:
5050
"""
5151
Get the state of the switches.
5252
@param ec: The CrOS_EC object.
@@ -66,7 +66,7 @@ def get_switches(ec: CrOS_EC) -> dict[str, bool]:
6666
}
6767

6868

69-
def get_battery_values(ec: CrOS_EC) -> dict[str, int | bool | str]:
69+
def get_battery_values(ec: CrosEcClass) -> dict[str, int | bool | str]:
7070
"""
7171
Get the values of the battery.
7272
@param ec: The CrOS_EC object.
@@ -102,7 +102,7 @@ def get_battery_values(ec: CrOS_EC) -> dict[str, int | bool | str]:
102102
}
103103

104104

105-
def get_als(ec: CrOS_EC) -> list[int | None]:
105+
def get_als(ec: CrosEcClass) -> list[int | None]:
106106
"""
107107
Get the current value from all Ambient Light Sensors.
108108
@param ec: The CrOS_EC object.
@@ -113,7 +113,7 @@ def get_als(ec: CrOS_EC) -> list[int | None]:
113113
return [val for val in als]
114114

115115

116-
def get_accel(ec: CrOS_EC) -> list[int | None]:
116+
def get_accel(ec: CrosEcClass) -> list[int | None]:
117117
"""
118118
Get the current value from all accelerometers.
119119
@param ec: The CrOS_EC object.

cros_ec_python/commands/pwm.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from typing import Final
22
from enum import Enum, auto
33
import struct
4-
from ..cros_ec import CrOS_EC
4+
from ..baseclass import CrosEcClass
55
from ..constants.COMMON import *
66

77
EC_CMD_PWM_GET_FAN_TARGET_RPM: Final = 0x0020
88

99

10-
def pwm_get_fan_rpm(ec: CrOS_EC) -> UInt32:
10+
def pwm_get_fan_rpm(ec: CrosEcClass) -> UInt32:
1111
"""
1212
Get fan target RPM
1313
@param ec: The CrOS_EC object.
@@ -20,7 +20,7 @@ def pwm_get_fan_rpm(ec: CrOS_EC) -> UInt32:
2020
EC_CMD_PWM_SET_FAN_TARGET_RPM: Final = 0x0021
2121

2222

23-
def pwm_set_fan_rpm(ec: CrOS_EC, rpm: UInt32, idx: UInt8 | None = None) -> None:
23+
def pwm_set_fan_rpm(ec: CrosEcClass, rpm: UInt32, idx: UInt8 | None = None) -> None:
2424
"""
2525
Set target fan RPM
2626
@param ec: The CrOS_EC object.
@@ -39,7 +39,7 @@ def pwm_set_fan_rpm(ec: CrOS_EC, rpm: UInt32, idx: UInt8 | None = None) -> None:
3939
EC_CMD_PWM_GET_KEYBOARD_BACKLIGHT: Final = 0x0022
4040

4141

42-
def pwm_get_keyboard_backlight(ec: CrOS_EC) -> dict[str, UInt8]:
42+
def pwm_get_keyboard_backlight(ec: CrosEcClass) -> dict[str, UInt8]:
4343
"""
4444
Get keyboard backlight
4545
OBSOLETE - Use EC_CMD_PWM_SET_DUTY
@@ -57,7 +57,7 @@ def pwm_get_keyboard_backlight(ec: CrOS_EC) -> dict[str, UInt8]:
5757
EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT: Final = 0x0023
5858

5959

60-
def pwm_set_keyboard_backlight(ec: CrOS_EC, percent: UInt8) -> None:
60+
def pwm_set_keyboard_backlight(ec: CrosEcClass, percent: UInt8) -> None:
6161
"""
6262
Set keyboard backlight
6363
OBSOLETE - Use EC_CMD_PWM_SET_DUTY
@@ -72,7 +72,7 @@ def pwm_set_keyboard_backlight(ec: CrOS_EC, percent: UInt8) -> None:
7272
EC_CMD_PWM_SET_FAN_DUTY: Final = 0x0024
7373

7474

75-
def pwm_set_fan_duty(ec: CrOS_EC, percent: UInt32, idx: UInt8 | None = None) -> None:
75+
def pwm_set_fan_duty(ec: CrosEcClass, percent: UInt32, idx: UInt8 | None = None) -> None:
7676
"""
7777
Set target fan PWM duty cycle
7878
@param ec: The CrOS_EC object.
@@ -103,7 +103,7 @@ class EcPwmType(Enum):
103103
EC_PWM_TYPE_COUNT = auto()
104104

105105

106-
def pwm_set_duty(ec: CrOS_EC, duty: UInt16, pwm_type: EcPwmType, index: UInt8 = 0) -> None:
106+
def pwm_set_duty(ec: CrosEcClass, duty: UInt16, pwm_type: EcPwmType, index: UInt8 = 0) -> None:
107107
"""
108108
Set PWM duty cycle
109109
@param ec: The CrOS_EC object.
@@ -119,7 +119,7 @@ def pwm_set_duty(ec: CrOS_EC, duty: UInt16, pwm_type: EcPwmType, index: UInt8 =
119119
EC_CMD_PWM_GET_DUTY: Final = 0x0026
120120

121121

122-
def pwm_get_duty(ec: CrOS_EC, pwm_type: EcPwmType, index: UInt8 = 0) -> UInt16:
122+
def pwm_get_duty(ec: CrosEcClass, pwm_type: EcPwmType, index: UInt8 = 0) -> UInt16:
123123
"""
124124
Get PWM duty cycle
125125
@param ec: The CrOS_EC object.

cros_ec_python/commands/thermal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Final
22
import struct
3-
from ..cros_ec import CrOS_EC
3+
from ..baseclass import CrosEcClass
44
from ..constants.COMMON import *
55

66
EC_CMD_THERMAL_SET_THRESHOLD: Final = 0x0050
@@ -9,7 +9,7 @@
99
EC_CMD_THERMAL_AUTO_FAN_CTRL: Final = 0x0052
1010

1111

12-
def thermal_auto_fan_ctrl(ec: CrOS_EC, fan_idx: UInt8 | None = None) -> None:
12+
def thermal_auto_fan_ctrl(ec: CrosEcClass, fan_idx: UInt8 | None = None) -> None:
1313
"""
1414
Toggle automatic fan control.
1515
@param ec: The CrOS_EC object.

cros_ec_python/constants/LPC.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
EC_LPC_CMDR_SMI : Final = BIT(6) # SMI event is pending
4242

4343
EC_LPC_ADDR_MEMMAP : Final = 0x900
44+
EC_LPC_ADDR_MEMMAP_FWAMD : Final = 0xE00 # Address on AMD Framework Laptops
4445

4546

4647
# Value written to legacy command port / prefix byte to indicate protocol

0 commit comments

Comments
 (0)