Skip to content

Commit 5ae941d

Browse files
authored
feat: cleanup interface and leverage inheritance (#51)
The initial implementation tried to separate the low-level interface to the EMC2101's functionality from the high-level interfaces (PWM & DAC). This lead to significant code duplication between EMC2101_Core and EMC2101_PWM. This duplication would only get worse once EMC2101_DAC is added. The redesigned interface scraps that distinction and instead leverages inheritance to remove code duplication. This is expected to result in a cleaner and easier to manage code base. notable changes: - remove code duplication between EMC2101_Core and EMC2101_PWM - rework the initialization logic for the low-level implementation - rename 'chip' and 'external' sensore to 'its' and 'ets' (the external temperature sensor had many different names - these have been cleaned up) - redesign lookup table into a low-level and an extended implementation (The low-level implementation exclusively supports steps and has no concept of a percentage or RPM. PWM and DAC implementation need to provide the required logic to convert Percentage and RPM into steps.) - eliminate no longer required intermediate functionality
1 parent cb6357e commit 5ae941d

16 files changed

+1201
-972
lines changed

feeph/emc2101/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
# the following imports are provided for user convenience
4848
# flake8: noqa: F401
4949
from feeph.emc2101.calibration import calibrate_pwm_fan
50+
from feeph.emc2101.config_register import ConfigRegister
5051
from feeph.emc2101.core import CONVERSIONS_PER_SECOND, DEFAULTS, ExternalSensorStatus, SpinUpDuration, SpinUpStrength
52+
from feeph.emc2101.ets_config import ExternalTemperatureSensorConfig, ets_2n3904, ets_2n3906
5153
from feeph.emc2101.fan_configs import FanConfig, RpmControlMode, Steps, export_fan_config, generic_pwm_fan
52-
from feeph.emc2101.pwm import DeviceConfig, Emc2101_PWM, ExternalTemperatureSensorConfig, FanSpeedUnit, PinSixMode, TemperatureLimitType, emc2101_default_config, ets_2n3904, ets_2n3906
54+
from feeph.emc2101.pwm import DeviceConfig, Emc2101_PWM, FanSpeedUnit, PinSixMode, emc2101_default_config

feeph/emc2101/calibration.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ def calibrate_pwm_fan(i2c_bus: busio.I2C, model: str, pwm_frequency: int = 22500
2727
LH.info("Calibrating fan parameters.")
2828
pwm_d, pwm_f = feeph.emc2101.utilities.calculate_pwm_factors(pwm_frequency=pwm_frequency)
2929
steps_list = list(range(pwm_f * 2))
30-
emc2101 = feeph.emc2101.core.Emc2101_core(i2c_bus=i2c_bus)
31-
emc2101.configure_pin_six_as_tacho()
30+
# tacho signal on pin 6, device uses PWM control
31+
config = feeph.emc2101.core.ConfigRegister(alt_tach=True, dac=False)
32+
emc2101 = feeph.emc2101.core.Emc2101(i2c_bus=i2c_bus, config=config)
3233
emc2101.configure_pwm_control(pwm_d=pwm_d, pwm_f=pwm_f, step_max=max(steps_list))
3334
# -----------------------------------------------------------------
3435
LH.debug("Disabling gradual speed rampup.")

feeph/emc2101/config_register.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env
2+
3+
from attrs import define
4+
5+
6+
@define(eq=True)
7+
class ConfigRegister:
8+
"""
9+
a representation of the EMC2101's config register (0x03)
10+
11+
this is not the entire configuration, there are additional registers
12+
which configure different aspects of this chip, e.g. fan configuration
13+
register (0x4A)
14+
15+
for an exhaustive description refer to EMC2101 datasheet section 6.5
16+
"""
17+
# the comment describes what happens if the value is set to True
18+
mask: bool = False # disable ALERT/TACH when in interrupt mode
19+
standby: bool = False # enable low power standby mode
20+
fan_standby: bool = False # disable fan output while in standby
21+
dac: bool = False # enable DAC output on FAN pin
22+
dis_to: bool = False # disable SMBUS timeout
23+
alt_tach: bool = False # configure pin six as tacho input
24+
trcit_ovrd: bool = False # unlock tcrit limit and allow one-time write
25+
queue: bool = False # alert after 3 critical temperature readings
26+
27+
def as_int(self):
28+
"""
29+
compute the config register's value
30+
"""
31+
config = 0x00
32+
if self.mask:
33+
config |= 0b1000_0000
34+
if self.standby:
35+
config |= 0b0100_0000
36+
if self.fan_standby:
37+
config |= 0b0010_0000
38+
if self.dac:
39+
config |= 0b0001_0000
40+
if self.dis_to:
41+
config |= 0b0000_1000
42+
if self.alt_tach:
43+
config |= 0b0000_0100
44+
if self.trcit_ovrd:
45+
config |= 0b0000_0010
46+
if self.queue:
47+
config |= 0b0000_0001
48+
return config
49+
50+
51+
def parse_config_register(value: int) -> ConfigRegister:
52+
"""
53+
parse the config register's value
54+
"""
55+
params = dict()
56+
if value & 0b1000_0000:
57+
params['mask'] = True
58+
if value & 0b0100_0000:
59+
params['standby'] = True
60+
if value & 0b0010_0000:
61+
params['fan_standby'] = True
62+
if value & 0b0001_0000:
63+
params['dac'] = True
64+
if value & 0b0000_1000:
65+
params['dis_to'] = True
66+
if value & 0b0000_0100:
67+
params['alt_tach'] = True
68+
if value & 0b0000_0010:
69+
params['trcit_ovrd'] = True
70+
if value & 0b0000_0001:
71+
params['queue'] = True
72+
return ConfigRegister(**params)

0 commit comments

Comments
 (0)