|
| 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