cros_ec_python.commands.pwm

PWM commands

  1"""
  2PWM commands
  3"""
  4
  5from typing import Final
  6from enum import Enum, auto
  7import struct
  8from ..baseclass import CrosEcClass
  9from ..constants.COMMON import *
 10
 11EC_CMD_PWM_GET_FAN_TARGET_RPM: Final = 0x0020
 12
 13
 14def pwm_get_fan_rpm(ec: CrosEcClass) -> UInt32:
 15    """
 16    Get fan target RPM
 17    :param ec: The CrOS_EC object.
 18    :return: The current fan target RPM.
 19    """
 20    resp = ec.command(0, EC_CMD_PWM_GET_FAN_TARGET_RPM, 0, 4)
 21    return struct.unpack("<I", resp)[0]
 22
 23
 24EC_CMD_PWM_SET_FAN_TARGET_RPM: Final = 0x0021
 25
 26
 27def pwm_set_fan_rpm(ec: CrosEcClass, rpm: UInt32, idx: UInt8 | None = None) -> None:
 28    """
 29    Set target fan RPM
 30    :param ec: The CrOS_EC object.
 31    :param rpm: The RPM to set the fan to.
 32    :param idx: The index of the fan to set the RPM for (v1 command). If None, it will set all fans (v0 command).
 33    :return: The current fan target RPM.
 34    """
 35    if idx is None:
 36        data = struct.pack("<I", rpm)
 37        ec.command(0, EC_CMD_PWM_SET_FAN_TARGET_RPM, 4, 0, data)
 38    else:
 39        data = struct.pack("<IB", rpm, idx)
 40        ec.command(1, EC_CMD_PWM_SET_FAN_TARGET_RPM, 5, 0, data)
 41
 42
 43EC_CMD_PWM_GET_KEYBOARD_BACKLIGHT: Final = 0x0022
 44
 45
 46def pwm_get_keyboard_backlight(ec: CrosEcClass) -> dict[str, UInt8]:
 47    """
 48    Get keyboard backlight
 49    OBSOLETE - Use EC_CMD_PWM_SET_DUTY
 50    :param ec: The CrOS_EC object.
 51    :return: The current keyboard backlight percentage and state.
 52    """
 53    resp = ec.command(0, EC_CMD_PWM_GET_KEYBOARD_BACKLIGHT, 0, 2)
 54    unpacked = struct.unpack("<BB", resp)
 55    return {
 56        "percent": unpacked[0],
 57        "enabled": unpacked[1]
 58    }
 59
 60
 61EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT: Final = 0x0023
 62
 63
 64def pwm_set_keyboard_backlight(ec: CrosEcClass, percent: UInt8) -> None:
 65    """
 66    Set keyboard backlight
 67    OBSOLETE - Use EC_CMD_PWM_SET_DUTY
 68    :param ec: The CrOS_EC object.
 69    :param percent: The percentage to set the keyboard backlight to.
 70    :return: None
 71    """
 72    data = struct.pack("<B", percent)
 73    ec.command(0, EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT, 1, 0, data)
 74
 75
 76EC_CMD_PWM_SET_FAN_DUTY: Final = 0x0024
 77
 78
 79def pwm_set_fan_duty(ec: CrosEcClass, percent: UInt32, idx: UInt8 | None = None) -> None:
 80    """
 81    Set target fan PWM duty cycle
 82    :param ec: The CrOS_EC object.
 83    :param percent: The duty cycle to set the fan to. Out of 100.
 84    :param idx: The index of the fan to set the duty cycle for (v1 command). If None, it will set all fans (v0 command).
 85    :return: None
 86    """
 87    if idx is None:
 88        data = struct.pack("<I", percent)
 89        ec.command(0, EC_CMD_PWM_SET_FAN_DUTY, 1, 0, data)
 90    else:
 91        data = struct.pack("<IB", percent, idx)
 92        ec.command(1, EC_CMD_PWM_SET_FAN_DUTY, 2, 0, data)
 93
 94
 95EC_CMD_PWM_SET_DUTY: Final = 0x0025
 96# 16 bit duty cycle, 0xffff = 100%
 97EC_PWM_MAX_DUTY: Final = 0xffff
 98
 99
100class EcPwmType(Enum):
101    # All types, indexed by board-specific enum pwm_channel
102    EC_PWM_TYPE_GENERIC = 0
103    # Keyboard backlight
104    EC_PWM_TYPE_KB_LIGHT = auto()
105    # Display backlight
106    EC_PWM_TYPE_DISPLAY_LIGHT = auto()
107    EC_PWM_TYPE_COUNT = auto()
108
109
110def pwm_set_duty(ec: CrosEcClass, duty: UInt16, pwm_type: EcPwmType, index: UInt8 = 0) -> None:
111    """
112    Set PWM duty cycle
113    :param ec: The CrOS_EC object.
114    :param duty: The duty cycle to set the PWM to. Out of EC_PWM_MAX_DUTY (0xffff).
115    :param pwm_type: The type of PWM to set.
116    :param index: The index of the PWM to set the duty cycle for.
117    :return: None
118    """
119    data = struct.pack("<HBB", duty, pwm_type.value, index)
120    ec.command(0, EC_CMD_PWM_SET_DUTY, 4, 0, data)
121
122
123EC_CMD_PWM_GET_DUTY: Final = 0x0026
124
125
126def pwm_get_duty(ec: CrosEcClass, pwm_type: EcPwmType, index: UInt8 = 0) -> UInt16:
127    """
128    Get PWM duty cycle
129    :param ec: The CrOS_EC object.
130    :param pwm_type: The type of PWM to get.
131    :param index: The index of the PWM to get the duty cycle for.
132    :return: The current PWM duty cycle. Out of EC_PWM_MAX_DUTY (0xffff).
133    """
134    data = struct.pack("<BB", pwm_type.value, index)
135    resp = ec.command(0, EC_CMD_PWM_GET_DUTY, 2, 2, data)
136    return struct.unpack("<H", resp)[0]
EC_CMD_PWM_GET_FAN_TARGET_RPM: Final = 32
def pwm_get_fan_rpm(ec: cros_ec_python.baseclass.CrosEcClass) -> int:
15def pwm_get_fan_rpm(ec: CrosEcClass) -> UInt32:
16    """
17    Get fan target RPM
18    :param ec: The CrOS_EC object.
19    :return: The current fan target RPM.
20    """
21    resp = ec.command(0, EC_CMD_PWM_GET_FAN_TARGET_RPM, 0, 4)
22    return struct.unpack("<I", resp)[0]

Get fan target RPM

Parameters
  • ec: The CrOS_EC object.
Returns

The current fan target RPM.

EC_CMD_PWM_SET_FAN_TARGET_RPM: Final = 33
def pwm_set_fan_rpm( ec: cros_ec_python.baseclass.CrosEcClass, rpm: int, idx: int | None = None) -> None:
28def pwm_set_fan_rpm(ec: CrosEcClass, rpm: UInt32, idx: UInt8 | None = None) -> None:
29    """
30    Set target fan RPM
31    :param ec: The CrOS_EC object.
32    :param rpm: The RPM to set the fan to.
33    :param idx: The index of the fan to set the RPM for (v1 command). If None, it will set all fans (v0 command).
34    :return: The current fan target RPM.
35    """
36    if idx is None:
37        data = struct.pack("<I", rpm)
38        ec.command(0, EC_CMD_PWM_SET_FAN_TARGET_RPM, 4, 0, data)
39    else:
40        data = struct.pack("<IB", rpm, idx)
41        ec.command(1, EC_CMD_PWM_SET_FAN_TARGET_RPM, 5, 0, data)

Set target fan RPM

Parameters
  • ec: The CrOS_EC object.
  • rpm: The RPM to set the fan to.
  • idx: The index of the fan to set the RPM for (v1 command). If None, it will set all fans (v0 command).
Returns

The current fan target RPM.

EC_CMD_PWM_GET_KEYBOARD_BACKLIGHT: Final = 34
def pwm_get_keyboard_backlight(ec: cros_ec_python.baseclass.CrosEcClass) -> dict[str, int]:
47def pwm_get_keyboard_backlight(ec: CrosEcClass) -> dict[str, UInt8]:
48    """
49    Get keyboard backlight
50    OBSOLETE - Use EC_CMD_PWM_SET_DUTY
51    :param ec: The CrOS_EC object.
52    :return: The current keyboard backlight percentage and state.
53    """
54    resp = ec.command(0, EC_CMD_PWM_GET_KEYBOARD_BACKLIGHT, 0, 2)
55    unpacked = struct.unpack("<BB", resp)
56    return {
57        "percent": unpacked[0],
58        "enabled": unpacked[1]
59    }

Get keyboard backlight OBSOLETE - Use EC_CMD_PWM_SET_DUTY

Parameters
  • ec: The CrOS_EC object.
Returns

The current keyboard backlight percentage and state.

EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT: Final = 35
def pwm_set_keyboard_backlight(ec: cros_ec_python.baseclass.CrosEcClass, percent: int) -> None:
65def pwm_set_keyboard_backlight(ec: CrosEcClass, percent: UInt8) -> None:
66    """
67    Set keyboard backlight
68    OBSOLETE - Use EC_CMD_PWM_SET_DUTY
69    :param ec: The CrOS_EC object.
70    :param percent: The percentage to set the keyboard backlight to.
71    :return: None
72    """
73    data = struct.pack("<B", percent)
74    ec.command(0, EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT, 1, 0, data)

Set keyboard backlight OBSOLETE - Use EC_CMD_PWM_SET_DUTY

Parameters
  • ec: The CrOS_EC object.
  • percent: The percentage to set the keyboard backlight to.
Returns

None

EC_CMD_PWM_SET_FAN_DUTY: Final = 36
def pwm_set_fan_duty( ec: cros_ec_python.baseclass.CrosEcClass, percent: int, idx: int | None = None) -> None:
80def pwm_set_fan_duty(ec: CrosEcClass, percent: UInt32, idx: UInt8 | None = None) -> None:
81    """
82    Set target fan PWM duty cycle
83    :param ec: The CrOS_EC object.
84    :param percent: The duty cycle to set the fan to. Out of 100.
85    :param idx: The index of the fan to set the duty cycle for (v1 command). If None, it will set all fans (v0 command).
86    :return: None
87    """
88    if idx is None:
89        data = struct.pack("<I", percent)
90        ec.command(0, EC_CMD_PWM_SET_FAN_DUTY, 1, 0, data)
91    else:
92        data = struct.pack("<IB", percent, idx)
93        ec.command(1, EC_CMD_PWM_SET_FAN_DUTY, 2, 0, data)

Set target fan PWM duty cycle

Parameters
  • ec: The CrOS_EC object.
  • percent: The duty cycle to set the fan to. Out of 100.
  • idx: The index of the fan to set the duty cycle for (v1 command). If None, it will set all fans (v0 command).
Returns

None

EC_CMD_PWM_SET_DUTY: Final = 37
EC_PWM_MAX_DUTY: Final = 65535
class EcPwmType(enum.Enum):
101class EcPwmType(Enum):
102    # All types, indexed by board-specific enum pwm_channel
103    EC_PWM_TYPE_GENERIC = 0
104    # Keyboard backlight
105    EC_PWM_TYPE_KB_LIGHT = auto()
106    # Display backlight
107    EC_PWM_TYPE_DISPLAY_LIGHT = auto()
108    EC_PWM_TYPE_COUNT = auto()
EC_PWM_TYPE_GENERIC = <EcPwmType.EC_PWM_TYPE_GENERIC: 0>
EC_PWM_TYPE_KB_LIGHT = <EcPwmType.EC_PWM_TYPE_KB_LIGHT: 1>
EC_PWM_TYPE_DISPLAY_LIGHT = <EcPwmType.EC_PWM_TYPE_DISPLAY_LIGHT: 2>
EC_PWM_TYPE_COUNT = <EcPwmType.EC_PWM_TYPE_COUNT: 3>
def pwm_set_duty( ec: cros_ec_python.baseclass.CrosEcClass, duty: int, pwm_type: EcPwmType, index: int = 0) -> None:
111def pwm_set_duty(ec: CrosEcClass, duty: UInt16, pwm_type: EcPwmType, index: UInt8 = 0) -> None:
112    """
113    Set PWM duty cycle
114    :param ec: The CrOS_EC object.
115    :param duty: The duty cycle to set the PWM to. Out of EC_PWM_MAX_DUTY (0xffff).
116    :param pwm_type: The type of PWM to set.
117    :param index: The index of the PWM to set the duty cycle for.
118    :return: None
119    """
120    data = struct.pack("<HBB", duty, pwm_type.value, index)
121    ec.command(0, EC_CMD_PWM_SET_DUTY, 4, 0, data)

Set PWM duty cycle

Parameters
  • ec: The CrOS_EC object.
  • duty: The duty cycle to set the PWM to. Out of EC_PWM_MAX_DUTY (0xffff).
  • pwm_type: The type of PWM to set.
  • index: The index of the PWM to set the duty cycle for.
Returns

None

EC_CMD_PWM_GET_DUTY: Final = 38
def pwm_get_duty( ec: cros_ec_python.baseclass.CrosEcClass, pwm_type: EcPwmType, index: int = 0) -> int:
127def pwm_get_duty(ec: CrosEcClass, pwm_type: EcPwmType, index: UInt8 = 0) -> UInt16:
128    """
129    Get PWM duty cycle
130    :param ec: The CrOS_EC object.
131    :param pwm_type: The type of PWM to get.
132    :param index: The index of the PWM to get the duty cycle for.
133    :return: The current PWM duty cycle. Out of EC_PWM_MAX_DUTY (0xffff).
134    """
135    data = struct.pack("<BB", pwm_type.value, index)
136    resp = ec.command(0, EC_CMD_PWM_GET_DUTY, 2, 2, data)
137    return struct.unpack("<H", resp)[0]

Get PWM duty cycle

Parameters
  • ec: The CrOS_EC object.
  • pwm_type: The type of PWM to get.
  • index: The index of the PWM to get the duty cycle for.
Returns

The current PWM duty cycle. Out of EC_PWM_MAX_DUTY (0xffff).