23
23
#define NODE_EP0 DT_NODELABEL(eeprom0)
24
24
#define NODE_EP1 DT_NODELABEL(eeprom1)
25
25
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 ];
29
31
static uint8_t i2c_buffer [TEST_DATA_SIZE ];
30
32
31
33
/*
@@ -35,6 +37,23 @@ static uint8_t i2c_buffer[TEST_DATA_SIZE];
35
37
uint8_t buffer_print_eeprom [TEST_DATA_SIZE * 5 + 1 ];
36
38
uint8_t buffer_print_i2c [TEST_DATA_SIZE * 5 + 1 ];
37
39
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
+
38
57
static void to_display_format (const uint8_t * src , size_t size , char * dst )
39
58
{
40
59
size_t i ;
@@ -123,7 +142,7 @@ static int run_program_read(const struct device *i2c, uint8_t addr,
123
142
i2c -> name , addr , offset );
124
143
125
144
for (i = 0 ; i < TEST_DATA_SIZE - offset ; ++ i ) {
126
- i2c_buffer [i ] = i ;
145
+ i2c_buffer [i ] = i & 0xFF ;
127
146
}
128
147
129
148
switch (addr_width ) {
@@ -155,11 +174,11 @@ static int run_program_read(const struct device *i2c, uint8_t addr,
155
174
zassert_equal (ret , 0 , "Failed to read EEPROM" );
156
175
157
176
for (i = 0 ; i < TEST_DATA_SIZE - offset ; ++ i ) {
158
- if (i2c_buffer [i ] != i ) {
177
+ if (i2c_buffer [i ] != ( i & 0xFF ) ) {
159
178
to_display_format (i2c_buffer , TEST_DATA_SIZE - offset ,
160
179
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 );
163
182
return - EIO ;
164
183
}
165
184
}
@@ -235,6 +254,8 @@ ZTEST(i2c_eeprom_target, test_eeprom_target)
235
254
uint8_t addr_1_width = DT_PROP_OR (NODE_EP1 , address_width , 8 );
236
255
int ret , offset ;
237
256
257
+ init_eeprom_test_data ();
258
+
238
259
zassert_not_null (i2c_0 , "EEPROM 0 - I2C bus not found" );
239
260
zassert_not_null (eeprom_0 , "EEPROM 0 device not found" );
240
261
0 commit comments