Skip to content

Commit 81b76e0

Browse files
etienne-lmskartben
authored andcommitted
tests: drivers: i2c: i2c_target_api: configure transfer byte size
Add CONFIG_I2C_TEST_DATA_MAX_SIZE to I2C driver i2c_target_api tests to set the max data size of tested I2C transfer. The effective I2C transfer data size depends on that value and the size of the EEPROM defined in the board DTS file. Signed-off-by: Etienne Carriere <[email protected]>
1 parent 2560dca commit 81b76e0

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

tests/drivers/i2c/i2c_target_api/README.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,9 @@ Presence of this required hardware configuration is identified by the
3939
`i2c_bus_short` fixture. If the buses are not connected as required,
4040
or the controller driver has bugs, the test will fail one or more I2C
4141
transactions.
42+
43+
Transfer data size
44+
******************
45+
46+
One can tune the number of data bytes echanged during the tests using
47+
configuration option ``CONFIG_I2C_TEST_DATA_MAX_SIZE``.

tests/drivers/i2c/i2c_target_api/common/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ config I2C_VIRTUAL_NAME
1717

1818
config APP_DUAL_ROLE_I2C
1919
bool "Test with combined controller/target behavior"
20+
21+
config I2C_TEST_DATA_MAX_SIZE
22+
int "Max tested data size for the I2C transfers"
23+
default 20

tests/drivers/i2c/i2c_target_api/src/main.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
#define NODE_EP0 DT_NODELABEL(eeprom0)
2424
#define NODE_EP1 DT_NODELABEL(eeprom1)
2525

26-
#define TEST_DATA_SIZE 20
27-
static const uint8_t eeprom_0_data[TEST_DATA_SIZE] = "0123456789abcdefghij";
28-
static const uint8_t eeprom_1_data[TEST_DATA_SIZE] = "jihgfedcba9876543210";
26+
#define TEST_DATA_SIZE MIN(CONFIG_I2C_TEST_DATA_MAX_SIZE, \
27+
MIN(DT_PROP(NODE_EP0, size), DT_PROP(NODE_EP1, size)))
28+
29+
static uint8_t eeprom_0_data[TEST_DATA_SIZE];
30+
static uint8_t eeprom_1_data[TEST_DATA_SIZE];
2931
static uint8_t i2c_buffer[TEST_DATA_SIZE];
3032

3133
/*
@@ -35,6 +37,23 @@ static uint8_t i2c_buffer[TEST_DATA_SIZE];
3537
uint8_t buffer_print_eeprom[TEST_DATA_SIZE * 5 + 1];
3638
uint8_t buffer_print_i2c[TEST_DATA_SIZE * 5 + 1];
3739

40+
static void init_eeprom_test_data(void)
41+
{
42+
size_t n;
43+
44+
/*
45+
* Initialize EEPROM data with printable ASCII value (range [32 126]).
46+
* Make sure content differs between eeprom_0_data[] and eeprom_1_data[].
47+
*/
48+
for (n = 0; n < sizeof(eeprom_0_data); n++) {
49+
eeprom_0_data[n] = 32 + (n % (126 - 32));
50+
}
51+
52+
for (n = 0; n < sizeof(eeprom_1_data); n++) {
53+
eeprom_1_data[n] = 32 + (((n + 10) * 3) % (126 - 32));
54+
}
55+
}
56+
3857
static void to_display_format(const uint8_t *src, size_t size, char *dst)
3958
{
4059
size_t i;
@@ -123,7 +142,7 @@ static int run_program_read(const struct device *i2c, uint8_t addr,
123142
i2c->name, addr, offset);
124143

125144
for (i = 0 ; i < TEST_DATA_SIZE-offset ; ++i) {
126-
i2c_buffer[i] = i;
145+
i2c_buffer[i] = i & 0xFF;
127146
}
128147

129148
switch (addr_width) {
@@ -155,11 +174,11 @@ static int run_program_read(const struct device *i2c, uint8_t addr,
155174
zassert_equal(ret, 0, "Failed to read EEPROM");
156175

157176
for (i = 0 ; i < TEST_DATA_SIZE-offset ; ++i) {
158-
if (i2c_buffer[i] != i) {
177+
if (i2c_buffer[i] != (i & 0xFF)) {
159178
to_display_format(i2c_buffer, TEST_DATA_SIZE-offset,
160179
buffer_print_i2c);
161-
TC_PRINT("Error: Unexpected buffer content: %s\n",
162-
buffer_print_i2c);
180+
TC_PRINT("Error: Unexpected %u (%02x) buffer content: %s\n",
181+
i, i2c_buffer[i], buffer_print_i2c);
163182
return -EIO;
164183
}
165184
}
@@ -235,6 +254,8 @@ ZTEST(i2c_eeprom_target, test_eeprom_target)
235254
uint8_t addr_1_width = DT_PROP_OR(NODE_EP1, address_width, 8);
236255
int ret, offset;
237256

257+
init_eeprom_test_data();
258+
238259
zassert_not_null(i2c_0, "EEPROM 0 - I2C bus not found");
239260
zassert_not_null(eeprom_0, "EEPROM 0 device not found");
240261

0 commit comments

Comments
 (0)