|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Tenstorrent AI ULC |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/ztest.h> |
| 8 | +#include <zephyr/sys/util.h> |
| 9 | + |
| 10 | +#include <smbus_utils.h> |
| 11 | + |
| 12 | +ZTEST(smbus_pec, test_smbus_pec) |
| 13 | +{ |
| 14 | + uint8_t addr = 0x42; |
| 15 | + /* Write Block with PEC (SMBus spec v3.1, Section 6.5.7) */ |
| 16 | + uint8_t write_data[] = { |
| 17 | + 0x73, /* command */ |
| 18 | + 4, /* len */ |
| 19 | + 0xde, 0xad, 0xbe, 0xef, /* data */ |
| 20 | + }; |
| 21 | + struct i2c_msg msgs[] = { |
| 22 | + { |
| 23 | + .buf = write_data, |
| 24 | + .len = sizeof(write_data), |
| 25 | + .flags = I2C_MSG_WRITE | I2C_MSG_STOP, |
| 26 | + }, |
| 27 | + /* driver would add a PEC message here */ |
| 28 | + }; |
| 29 | + |
| 30 | + uint8_t actual_pec = smbus_pec(addr, msgs, ARRAY_SIZE(msgs)); |
| 31 | + uint8_t expected_pec = 0x12; |
| 32 | + |
| 33 | + zexpect_equal(expected_pec, actual_pec, "expected: %02x actual: %02x", |
| 34 | + expected_pec, actual_pec); |
| 35 | +} |
| 36 | + |
| 37 | +ZTEST(smbus_pec, test_smbus_read_check_pec) |
| 38 | +{ |
| 39 | + uint8_t addr = 0xa; |
| 40 | + |
| 41 | + { |
| 42 | + /* Read Byte with PEC (SMBus spec v3.1, Section 6.5.5) */ |
| 43 | + uint8_t data[] = { |
| 44 | + 0x10, /* command */ |
| 45 | + 0x05, /* data */ |
| 46 | + 0x90, /* PEC */ |
| 47 | + }; |
| 48 | + struct i2c_msg msgs[] = { |
| 49 | + { |
| 50 | + .buf = &data[0], /* command */ |
| 51 | + .len = 1, |
| 52 | + .flags = I2C_MSG_WRITE, |
| 53 | + }, |
| 54 | + { |
| 55 | + .buf = &data[1], /* data */ |
| 56 | + .len = 1, |
| 57 | + .flags = I2C_MSG_READ, |
| 58 | + }, |
| 59 | + { |
| 60 | + .buf = &data[2], /* PEC */ |
| 61 | + .len = 1, |
| 62 | + .flags = I2C_MSG_READ, |
| 63 | + }, |
| 64 | + }; |
| 65 | + |
| 66 | + zexpect_ok(smbus_read_check_pec(SMBUS_MODE_PEC, addr, msgs, ARRAY_SIZE(msgs))); |
| 67 | + } |
| 68 | + |
| 69 | + { |
| 70 | + /* Read Word with PEC (SMBus spec v3.1, Section 6.5.5) */ |
| 71 | + uint8_t data[] = { |
| 72 | + 0x10, /* command */ |
| 73 | + 0x05, /* data byte (low) */ |
| 74 | + 0x0a, /* data byte (high) */ |
| 75 | + 0xcf, /* PEC */ |
| 76 | + }; |
| 77 | + struct i2c_msg msgs[] = { |
| 78 | + { |
| 79 | + .buf = &data[0], /* command */ |
| 80 | + .len = 1, |
| 81 | + .flags = I2C_MSG_WRITE, |
| 82 | + }, |
| 83 | + { |
| 84 | + .buf = &data[1], /* data */ |
| 85 | + .len = 2, |
| 86 | + .flags = I2C_MSG_READ, |
| 87 | + }, |
| 88 | + { |
| 89 | + .buf = &data[3], /* PEC */ |
| 90 | + .len = 1, |
| 91 | + .flags = I2C_MSG_READ, |
| 92 | + }, |
| 93 | + }; |
| 94 | + |
| 95 | + zexpect_ok(smbus_read_check_pec(SMBUS_MODE_PEC, addr, msgs, ARRAY_SIZE(msgs))); |
| 96 | + } |
| 97 | + |
| 98 | + { |
| 99 | + /* Block read (SMBus spec v3.1, Section 6.5.7) */ |
| 100 | + uint8_t data[] = { |
| 101 | + 0x10, /* command */ |
| 102 | + 0x06, /* block count */ |
| 103 | + 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, /* data */ |
| 104 | + 0x99, /* PEC */ |
| 105 | + }; |
| 106 | + struct i2c_msg msgs[] = { |
| 107 | + { |
| 108 | + .buf = &data[0], /* command */ |
| 109 | + .len = 1, |
| 110 | + .flags = I2C_MSG_WRITE, |
| 111 | + }, |
| 112 | + { |
| 113 | + .buf = &data[1], /* block count */ |
| 114 | + .len = 1, |
| 115 | + .flags = I2C_MSG_READ | I2C_MSG_RESTART, |
| 116 | + }, |
| 117 | + { |
| 118 | + .buf = &data[2], /* data */ |
| 119 | + .len = data[1], |
| 120 | + .flags = I2C_MSG_READ, |
| 121 | + }, |
| 122 | + { |
| 123 | + .buf = &data[2] + data[1], /* PEC */ |
| 124 | + .len = 1, |
| 125 | + .flags = I2C_MSG_READ, |
| 126 | + }, |
| 127 | + }; |
| 128 | + |
| 129 | + zexpect_ok(smbus_read_check_pec(SMBUS_MODE_PEC, addr, msgs, ARRAY_SIZE(msgs))); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +ZTEST_SUITE(smbus_pec, NULL, NULL, NULL, NULL, NULL); |
0 commit comments