Andrew Jeffery | 29b5b2d | 2020-09-09 23:21:53 +0930 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # Copyright 2020 IBM Corp. |
| 5 | |
| 6 | import csv |
| 7 | import sys |
| 8 | from collections import namedtuple |
| 9 | from pprint import pprint |
| 10 | from datetime import time, timedelta |
| 11 | import argparse |
| 12 | import enum |
| 13 | import crc8 |
| 14 | |
| 15 | class UCD90320Command(bytes, enum.Enum): |
| 16 | def __new__(cls, value, width): |
| 17 | obj = bytes.__new__(cls, [value]) |
| 18 | obj._value_ = value |
| 19 | obj.width = width |
| 20 | return obj |
| 21 | |
| 22 | MONITOR_CONFIG = (0xd5, -1) |
| 23 | NUM_PAGES = (0xd6, 1) |
| 24 | GPIO_SELECT = (0xfa, 1) |
| 25 | GPIO_CONFIG = (0xfb, 1) |
| 26 | DEVICE_ID = (0xfd, -1) |
| 27 | |
| 28 | class PMBusCommand(bytes, enum.Enum): |
| 29 | def __new__(cls, value, width): |
| 30 | obj = bytes.__new__(cls, [value]) |
| 31 | obj._value_ = value |
| 32 | obj.width = width |
| 33 | return obj |
| 34 | |
| 35 | PAGE = (0x00, 1) |
| 36 | OPERATION = (0x01, 1) |
| 37 | ON_OFF_CONFIG = (0x02, 1) |
| 38 | CLEAR_FAULTS = (0x03, 0) |
| 39 | PHASE = (0x04, 1) |
| 40 | PAGE_PLUS_WRITE = (0x05, -1) |
| 41 | PAGE_PLUS_READ = (0x06, -1) |
| 42 | WRITE_PROTECT = (0x10, 1) |
| 43 | STORE_DEFAULT_ALL = (0x11, 0) |
| 44 | RESTORE_DEFAULT_ALL = (0x12, 0) |
| 45 | STORE_DEFAULT_CODE = (0x13, 1) |
| 46 | RESTORE_DEFAULT_CODE = (0x14, 1) |
| 47 | STORE_USER_ALL = (0x15, 0) |
| 48 | RESTORE_USER_ALL = (0x16, 0) |
| 49 | STORE_USER_CODE = (0x17, 1) |
| 50 | RESTORE_USER_CODE = (0x18, 1) |
| 51 | CAPABILITY = (0x19, 1) |
| 52 | QUERY = (0x1a, 1) |
| 53 | SMBALERT_MASK = (0x1b, 2) |
| 54 | VOUT_MODE = (0x20, 1) |
| 55 | VOUT_COMMAND = (0x21, 2) |
| 56 | VOUT_CAL_OFFSET = (0x23, 2) |
| 57 | POUT_MAX = (0x31, 2) |
| 58 | FREQUENCY_SWITCH = (0x33, 2) |
| 59 | VIN_OFF = (0x36, 2) |
| 60 | FAN_COMMAND_1 = (0x3b, 2) |
| 61 | FAN_COMMAND_4 = (0x3f, 2) |
| 62 | VOUT_OV_FAULT_LIMIT = (0x40, 2) |
| 63 | VOUT_OV_WARN_LIMIT = (0x42, 2) |
| 64 | VOUT_UV_WARN_LIMIT = (0x43, 2) |
| 65 | VOUT_UV_FAULT_LIMIT = (0x44, 2) |
| 66 | IOUT_OC_LV_FAULT_LIMIT = (0x48, 2) |
| 67 | IOUT_OC_LV_FAULT_RESPONSE = (0x49, 1) |
| 68 | IOUT_UC_FAULT_RESPONSE = (0x4c, 1) |
| 69 | OT_FAULT_LIMIT = (0x4f, 2) |
| 70 | OT_WARN_LIMIT = (0x51, 2) |
| 71 | UT_WARN_LIMIT = (0x52, 2) |
| 72 | UT_FAULT_LIMIT = (0x53, 2) |
| 73 | VIN_UV_FAULT_LIMIT = (0x59, 2) |
| 74 | IIN_OC_FAULT_RESPONSE = (0x5c, 1) |
| 75 | TOFF_MAX_WARN_LIMIT = (0x66, 2) |
| 76 | STATUS_WORD = (0x79, 2) |
| 77 | STATUS_CML = (0x7e, 1) |
| 78 | STATUS_OTHER = (0x7f, 1) |
| 79 | STATUS_MFR_SPECIFIC = (0x80, 1) |
| 80 | READ_TEMPERATURE_3 = (0x8f, 2) |
| 81 | PMBUS_REVISION = (0x98, 1) |
| 82 | MFR_MODEL = (0x9a, -1) |
| 83 | IC_DEVICE_REV = (0xae, -1) |
| 84 | USER_DATA_00 = (0xb0, -1) |
| 85 | USER_DATA_08 = (0xb8, -1) |
| 86 | MFR_SPECIFIC_05 = (0xd5, None) |
| 87 | MFR_SPECIFIC_06 = (0xd6, None) |
| 88 | MFR_SPECIFIC_42 = (0xfa, None) |
| 89 | MFR_SPECIFIC_43 = (0xfb, None) |
| 90 | MFR_SPECIFIC_45 = (0xfd, None) |
| 91 | |
| 92 | |
| 93 | class I2CCondition(enum.Enum): |
| 94 | START = 0 |
| 95 | STOP = 1 |
| 96 | |
| 97 | class I2CRecord(enum.Enum): |
| 98 | WRITE = 0 |
| 99 | READ = 1 |
| 100 | |
| 101 | class I2CResponse(enum.Enum): |
| 102 | ACK = 0 |
| 103 | NACK = 1 |
| 104 | |
| 105 | # Level,Index,m:s.ms.us,Dur,Len,Err,S/P,Addr,Record,Data |
| 106 | # 0,1,0:29.722.525,210.600 us,1 B,,S,32,Write Transaction,0E |
| 107 | I2CTransfer = namedtuple("I2CTransfer", ("level", "index", "timestamp", "duration", "length", "error", "conditions", "address", "record", "data")) |
| 108 | Timestamp = namedtuple("Timestamp", ["minutes", "seconds", "milliseconds", "microseconds"]) |
| 109 | I2CData = namedtuple("I2CData", ["response", "data"]) |
| 110 | |
| 111 | SMBusTransfer = namedtuple("SMBusTransfer", ["command", "response"]) |
| 112 | |
| 113 | def to_duration(field): |
| 114 | if field.endswith("us"): |
| 115 | if "." in field: |
| 116 | ms, us, _ = 0, *field.split(".") |
| 117 | else: |
| 118 | ms, us = 0, int(field.rstrip("us")) |
| 119 | elif field.endswith("ms"): |
| 120 | if "." in field: |
| 121 | ms, us, _ = field.split(".") |
| 122 | else: |
| 123 | ms, us = int(field.rstrip("ms")), 0 |
| 124 | else: |
| 125 | raise ValueError |
| 126 | return timedelta(milliseconds=int(ms), microseconds=int(us)) |
| 127 | |
| 128 | def to_timestamp(field): |
| 129 | ts = Timestamp(*list(int(v) for v in field.replace(":", ".").split("."))) |
| 130 | return time(0, ts.minutes, ts.seconds, ts.milliseconds * 1000 + ts.microseconds) |
| 131 | |
| 132 | def to_i2cdata(field): |
| 133 | resp = I2CResponse.NACK if field.endswith("*") else I2CResponse.ACK |
| 134 | return I2CData(resp, bytes(int(v, 16) for v in field.rstrip("*").split())) |
| 135 | |
| 136 | def to_address(field): |
| 137 | return int(field, 16) |
| 138 | |
| 139 | def to_i2cconditions(field): |
| 140 | if "S" == field: |
| 141 | return { I2CCondition.START } |
| 142 | elif "SP" == field: |
| 143 | return { I2CCondition.START, I2CCondition.STOP } |
| 144 | raise ValueError |
| 145 | |
| 146 | def to_i2crecord(field): |
| 147 | if "Write Transaction" == field: |
| 148 | return I2CRecord.WRITE |
| 149 | if "Read Transaction" == field: |
| 150 | return I2CRecord.READ |
| 151 | raise ValueError |
| 152 | |
| 153 | def to_i2ctransfer(line): |
| 154 | return I2CTransfer(*line[:2], |
| 155 | to_timestamp(line[2]), |
| 156 | to_duration(line[3]), |
| 157 | *line[4:6], |
| 158 | to_i2cconditions(line[6]), |
| 159 | to_address(line[7]), |
| 160 | to_i2crecord(line[8]), |
| 161 | to_i2cdata(line[9])) |
| 162 | |
| 163 | def pmbuscommand_style(xfer): |
| 164 | return PMBusCommand(xfer.data.data[0]) |
| 165 | |
| 166 | def ucd90320command_style(xfer): |
| 167 | try: |
| 168 | return UCD90320Command(xfer.data.data[0]) |
| 169 | except: |
| 170 | return pmbuscommand_style(xfer) |
| 171 | |
| 172 | def as_smbustransfers(i2cxfers, style): |
| 173 | command = None |
| 174 | for i2cxfer in i2cxfers: |
| 175 | if i2cxfer.conditions == { I2CCondition.START }: |
| 176 | assert not command |
| 177 | command = i2cxfer |
| 178 | if i2cxfer.conditions == { I2CCondition.START, I2CCondition.STOP }: |
| 179 | if command: |
| 180 | yield PMBusRead(style(command), command, i2cxfer) |
| 181 | command = None |
| 182 | else: |
| 183 | yield PMBusWrite(style(i2cxfer), i2cxfer) |
| 184 | |
| 185 | def smbus_pec(data): |
| 186 | hash = crc8.crc8() |
| 187 | hash.update(data) |
| 188 | return hash.digest()[0] |
| 189 | |
| 190 | def smbus_pec_pack_address(address, record): |
| 191 | return (address << 1) | record.value |
| 192 | |
| 193 | class PMBusTransfer(object): |
| 194 | def calculate_pec(self): |
| 195 | raise NotImplemented |
| 196 | |
| 197 | def validate_pec(self): |
| 198 | if self.pec is None: |
| 199 | return True |
| 200 | derived = self.calculate_pec() |
| 201 | provided = self.pec |
| 202 | return provided == derived |
| 203 | |
| 204 | def validate_xfer(self): |
| 205 | raise NotImplemented |
| 206 | |
| 207 | def valid(self): |
| 208 | return self.validate_xfer() and self.validate_pec() |
| 209 | |
| 210 | class PMBusWrite(PMBusTransfer): |
| 211 | def __init__(self, command, xfer): |
| 212 | assert xfer.record == I2CRecord.WRITE |
| 213 | self.command = command |
| 214 | self.xfer = xfer |
| 215 | |
| 216 | if command.width is None: |
| 217 | start, end = 1, len(xfer.data.data) |
| 218 | elif command.width == -1: |
| 219 | start, end = 1, xfer.data.data[0] + 1 |
| 220 | else: |
| 221 | start, end = 1, command.width + 1 |
| 222 | |
| 223 | self.data = xfer.data.data[start:end] |
| 224 | tail = self.data[end:] |
| 225 | |
| 226 | if len(tail) == 1: |
| 227 | self.pec, = tail |
| 228 | else: |
| 229 | self.pec = None |
| 230 | |
| 231 | self.response = xfer.data.response |
| 232 | |
| 233 | def calculate_pec(self): |
| 234 | data = (smbus_pec_pack_address(self.xfer.address, self.xfer.record), |
| 235 | *self.xfer.data.data[:-1]) |
| 236 | return smbus_pec(bytes(data)) |
| 237 | |
| 238 | def validate_xfer(self): |
| 239 | return self.response == I2CResponse.ACK |
| 240 | |
| 241 | def __str__(self): |
| 242 | timestamp = self.xfer.timestamp.strftime("%M:%S.%f") |
| 243 | duration = self.xfer.duration.total_seconds() |
| 244 | data = "[ " + " ".join("{:02x}".format(v) for v in self.data) + " ]" |
| 245 | |
| 246 | status = [] |
| 247 | if self.response != I2CResponse.ACK: |
| 248 | status.append(self.response.name) |
| 249 | |
| 250 | if not self.validate_pec(): |
| 251 | status.append("!PEC") |
| 252 | |
| 253 | if status: |
| 254 | status = " ".join(status) |
| 255 | fmt = "{0} {1:.6f} 0x{2.xfer.address:x} {3.name} {2.command.name} {4} {5}" |
| 256 | return fmt.format(timestamp, duration, self, I2CRecord.WRITE, data, status) |
| 257 | |
| 258 | fmt = "{0} {1:.6f} 0x{2.xfer.address:x} {3.name} {2.command.name} {4}" |
| 259 | return fmt.format(timestamp, duration, self, I2CRecord.WRITE, data) |
| 260 | |
| 261 | class PMBusRead(PMBusTransfer): |
| 262 | def __init__(self, command, start, repeat): |
| 263 | assert repeat.record == I2CRecord.READ |
| 264 | self.command = command |
| 265 | self.start = start |
| 266 | self.repeat = repeat |
| 267 | assert start.address == repeat.address |
| 268 | self.address = start.address |
| 269 | |
| 270 | if self.command.width is None: |
| 271 | start, end = 0, len(repeat.data.data) |
| 272 | elif self.command.width == -1: |
| 273 | start, end = 1, repeat.data.data[0] + 1 |
| 274 | else: |
| 275 | start, end = 0, command.width |
| 276 | |
| 277 | self.data = repeat.data.data[start:end] |
| 278 | tail = repeat.data.data[end:] |
| 279 | |
| 280 | if len(tail) == 1: |
| 281 | self.pec, = tail |
| 282 | else: |
| 283 | self.pec = None |
| 284 | |
| 285 | self.response = repeat.data.response |
| 286 | |
| 287 | def calculate_pec(self): |
| 288 | data = (smbus_pec_pack_address(self.start.address, self.start.record), |
| 289 | *self.start.data.data, |
| 290 | smbus_pec_pack_address(self.repeat.address, self.repeat.record), |
| 291 | *self.repeat.data.data[:-1]) |
| 292 | return smbus_pec(bytes(data)) |
| 293 | |
| 294 | def validate_xfer(self): |
| 295 | return (self.start.data.response == I2CResponse.ACK and |
| 296 | self.repeat.data.response == I2CResponse.NACK) |
| 297 | |
| 298 | def __str__(self): |
| 299 | timestamp = self.start.timestamp.strftime("%M:%S.%f") |
| 300 | duration = self.start.duration.total_seconds() |
| 301 | |
| 302 | status = [] |
| 303 | if self.start.data.response != I2CResponse.ACK: |
| 304 | status.append(self.start.data.response.name) |
| 305 | |
| 306 | if status: |
| 307 | status = " ".join(status) |
| 308 | fmt = "{0} {1:.6f} 0x{2.address:x} {3.name} {2.command.name} {4}" |
| 309 | start = fmt.format(timestamp, duration, self, I2CRecord.READ, status) |
| 310 | else: |
| 311 | fmt = "{0} {1:.6f} 0x{2.address:x} {3.name} {2.command.name}" |
| 312 | start = fmt.format(timestamp, duration, self, I2CRecord.READ) |
| 313 | |
| 314 | timestamp = self.repeat.timestamp.strftime("%M:%S.%f") |
| 315 | duration = self.repeat.duration.total_seconds() |
| 316 | data = " ".join("{:02x}".format(v) for v in self.data) |
| 317 | data = "[ " + data + " ]" |
| 318 | |
| 319 | status = [] |
| 320 | if self.repeat.data.response != I2CResponse.NACK: |
| 321 | status.append(self.repeat.data.response.name) |
| 322 | |
| 323 | if not self.validate_pec(): |
| 324 | status.append("!PEC") |
| 325 | |
| 326 | status = " ".join(status) |
| 327 | fmt = "{0} {1:.6f} {2} {3}" |
| 328 | repeat = fmt.format(timestamp, duration, data, status) |
| 329 | |
| 330 | return start + " | " + repeat |
| 331 | |
| 332 | def filter_source(src): |
| 333 | for line in src: |
| 334 | if not line: |
| 335 | continue |
| 336 | if line.startswith("#"): |
| 337 | continue |
| 338 | if "Capture started" in line: |
| 339 | continue |
| 340 | if "Capture stopped" in line: |
| 341 | continue |
| 342 | yield line |
| 343 | |
| 344 | def main(): |
| 345 | parser = argparse.ArgumentParser() |
| 346 | parser.add_argument("--after", type=str) |
| 347 | parser.add_argument("--before", type=str) |
| 348 | parser.add_argument("--longer-than", type=str) |
| 349 | parser.add_argument("--address", type=lambda x: int(x, 0)) |
| 350 | parser.add_argument("--pmbus", action="store_true") |
| 351 | parser.add_argument("--ucd90320", action="store_true") |
| 352 | parser.add_argument("--bad-transfers", action="store_true") |
| 353 | parser.add_argument("file", type=str) |
| 354 | args = parser.parse_args() |
| 355 | with open(args.file, "r") as src: |
| 356 | data = (line for line in filter_source(src.readlines())) |
| 357 | xfers = (to_i2ctransfer(e) for e in csv.reader(data)) |
| 358 | if args.after: |
| 359 | after = to_timestamp(args.after) |
| 360 | xfers = (e for e in xfers if e.timestamp > after) |
| 361 | if args.before: |
| 362 | before = to_timestamp(args.before) |
| 363 | xfers = (e for e in xfers if e.timestamp < before) |
| 364 | if args.longer_than: |
| 365 | minimum = to_duration(args.longer_than) |
| 366 | xfers = (e for e in xfers if e.duration > minimum) |
| 367 | if args.address is not None: |
| 368 | xfers = (e for e in xfers if e.address == args.address) |
| 369 | if args.ucd90320 or args.pmbus: |
| 370 | if args.ucd90320: |
| 371 | style = ucd90320command_style |
| 372 | else: |
| 373 | style = pmbuscommand_style |
| 374 | for xfer in as_smbustransfers(xfers, style): |
| 375 | if args.bad_transfers and xfer.valid(): |
| 376 | continue |
| 377 | print(xfer) |
| 378 | else: |
| 379 | for xfer in xfers: |
| 380 | timestamp = xfer.timestamp.strftime("%M:%S.%f") |
| 381 | duration = xfer.duration.total_seconds() |
| 382 | data = "[ " + " ".join("{:02x}".format(v) for v in xfer.data.data) + " ]" |
| 383 | res = xfer.data.response.name |
| 384 | fmt = "{0} {1:.6f} 0x{2.address:x} {2.record.name} {3} {4}" |
| 385 | print(fmt.format(timestamp, duration, xfer, data, res)) |
| 386 | |
| 387 | if __name__ == "__main__": |
| 388 | main() |