Last active
November 30, 2018 11:34
-
-
Save nanpuyue/576ba3b61ae220dfdca890258b80aac9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| from smbus import SMBus | |
| # Use /dev/i2c-0 | |
| smbus = SMBus(0) | |
| # Use PEC | |
| smbus.pec = True | |
| ADDR1 = 0x34 | |
| ADDR2 = 0x36 | |
| class C(object): | |
| OPERATION = 0x01 | |
| ON_OFF_CONFIG = 0x02 | |
| CLEAR_FAULTS = 0x03 | |
| WRITE_PROTECT = 0x10 | |
| STORE_DEFAULT_ALL = 0x11 | |
| RESTORE_DEFAULT_ALL = 0x12 | |
| STORE_USER_ALL = 0x15 | |
| RESTORE_USER_ALL = 0x16 | |
| CAPABILITY = 0x19 | |
| SMBALERT_MASK = 0x1B | |
| VOUT_MODE = 0x20 | |
| VOUT_COMMAND = 0x21 | |
| VOUT_MAX = 0x24 | |
| VOUT_TRANSITION_RATE = 0x27 | |
| VOUT_SCALE_LOOP = 0x29 | |
| VOUT_MIN = 0x2B | |
| VIN_ON = 0x35 | |
| VIN_OFF = 0x36 | |
| IOUT_CAL_OFFSET = 0x39 | |
| VOUT_OV_FAULT_RESPONSE = 0x41 | |
| VOUT_UV_FAULT_RESPONSE = 0x45 | |
| IOUT_OC_FAULT_LIMIT = 0x46 | |
| IOUT_OC_FAULT_RESPONSE = 0x47 | |
| IOUT_OC_WARN_LIMIT = 0x4A | |
| OT_FAULT_LIMIT = 0x4F | |
| OT_FAULT_RESPONSE = 0x50 | |
| OT_WARN_LIMIT = 0x51 | |
| TON_DELAY = 0x60 | |
| TON_RISE = 0x61 | |
| TON_MAX_FAULT_LIMIT = 0x62 | |
| TON_MAX_FAULT_RESPONSE = 0x63 | |
| TOFF_DELAY = 0x64 | |
| TOFF_FALL = 0x65 | |
| STATUS_BYTE = 0x78 | |
| STATUS_WORD = 0x79 | |
| STATUS_VOUT = 0x7A | |
| STATUS_IOUT = 0x7B | |
| STATUS_INPUT = 0x7C | |
| STATUS_TEMPERATURE = 0x7D | |
| STATUS_CML = 0x7E | |
| STATUS_MFR_SPECIFIC = 0x80 | |
| READ_VOUT = 0x8B | |
| READ_IOUT = 0x8C | |
| READ_TEMPERATURE_1 = 0x8D | |
| PMBUS_REVISION = 0x98 | |
| IC_DEVICE_ID = 0xAD | |
| IC_DEVICE_REV = 0xAE | |
| MFR_SPECIFIC_00 = 0xD0 | |
| VREF_TRIM = 0xD4 | |
| STEP_VREF_MARGIN_HIGH = 0xD5 | |
| STEP_VREF_MARGIN_LOW = 0xD6 | |
| PCT_OV_UV_WRN_FLT_LIMITS = 0xD7 | |
| OPTIONS = 0xE5 | |
| MISC_CONFIG_OPTIONS = 0xF0 | |
| def store_default_all(): | |
| smbus.write_byte(ADDR2, C.STORE_DEFAULT_ALL) | |
| def restore_default_all(): | |
| smbus.write_byte(ADDR2, C.RESTORE_DEFAULT_ALL) | |
| def set_voltage(vol: float, verify: bool=False): | |
| data = int(vol * 512) | |
| smbus.write_word_data(ADDR2, C.VOUT_COMMAND, data) | |
| if verify and smbus.read_word_data(ADDR2, C.VOUT_COMMAND) != data: | |
| return False | |
| return True | |
| def get_voltage(): | |
| data = smbus.read_word_data(ADDR2, C.READ_VOUT) | |
| return data / 512 | |
| def get_current(): | |
| mask = 0x3ff | |
| data1 = smbus.read_word_data(ADDR1, C.READ_IOUT) | |
| data2 = smbus.read_word_data(ADDR2, C.READ_IOUT) | |
| return ((data1 & mask) + (data2 & mask)) / 16 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment