19
19
#error [NOT_SUPPORTED] I2C not supported for this target
20
20
#elif !COMPONENT_FPGA_CI_TEST_SHIELD
21
21
#error [NOT_SUPPORTED] FPGA CI Test Shield is needed to run this test
22
- #elif !defined(FULL_TEST_SHIELD)
23
- #error [NOT_SUPPORTED] The I2C test does not run on prototype hardware
24
22
#else
25
23
26
24
#include " utest/utest.h"
@@ -53,13 +51,16 @@ void test_i2c_init_free(PinName sda, PinName scl)
53
51
i2c_frequency (&obj, 100000 );
54
52
}
55
53
56
- void i2c_test_all (PinName sda, PinName scl)
54
+ void i2c_test_write (PinName sda, PinName scl)
57
55
{
58
56
// Remap pins for test
59
57
tester.reset ();
60
58
tester.pin_map_set (sda, MbedTester::LogicalPinI2CSda);
61
59
tester.pin_map_set (scl, MbedTester::LogicalPinI2CScl);
62
60
61
+ tester.pin_set_pull (sda, MbedTester::PullUp);
62
+ tester.pin_set_pull (scl, MbedTester::PullUp);
63
+
63
64
// Initialize mbed I2C pins
64
65
i2c_t i2c;
65
66
memset (&i2c, 0 , sizeof (i2c));
@@ -90,10 +91,6 @@ void i2c_test_all(PinName sda, PinName scl)
90
91
int num_dev_addr_matches;
91
92
int ack_nack;// 0 if NACK was received, 1 if ACK was received, 2 for timeout
92
93
93
- printf (" I2C complete write transaction test on sda=%s (%i), scl=%s (%i)\r\n " ,
94
- pinmap_ff_default_pin_to_string (sda), sda,
95
- pinmap_ff_default_pin_to_string (scl), scl);
96
-
97
94
// Reset tester stats and select I2C
98
95
tester.peripherals_reset ();
99
96
tester.select_peripheral (MbedTester::PeripheralI2C);
@@ -108,13 +105,13 @@ void i2c_test_all(PinName sda, PinName scl)
108
105
num_acks = 0 ;
109
106
num_nacks = 0 ;
110
107
checksum = 0 ;
111
- tester. io_metrics_start ();
108
+
112
109
num_writes = i2c_write (&i2c, I2C_DEV_ADDR, (char *)data_out, TRANSFER_COUNT, true ); // transaction ends with a stop condition
113
110
num_acks = num_writes + 1 ;
114
111
num_starts += 1 ;
115
112
num_stops += 1 ;
116
113
num_dev_addr_matches += 1 ;
117
- tester. io_metrics_stop ();
114
+
118
115
for (int i = 0 ; i < TRANSFER_COUNT; i++) {
119
116
checksum += data_out[i];
120
117
}
@@ -136,11 +133,50 @@ void i2c_test_all(PinName sda, PinName scl)
136
133
TEST_ASSERT_EQUAL (num_writes, tester.num_writes ());
137
134
TEST_ASSERT_EQUAL (num_reads, tester.num_reads ());
138
135
139
- printf (" Pin combination works\r\n " );
136
+ tester.reset ();
137
+ tester.pin_set_pull (sda, MbedTester::PullNone);
138
+ tester.pin_set_pull (scl, MbedTester::PullNone);
139
+ }
140
+
141
+ void i2c_test_read (PinName sda, PinName scl)
142
+ {
143
+ // Remap pins for test
144
+ tester.reset ();
145
+ tester.pin_map_set (sda, MbedTester::LogicalPinI2CSda);
146
+ tester.pin_map_set (scl, MbedTester::LogicalPinI2CScl);
147
+
148
+ tester.pin_set_pull (sda, MbedTester::PullUp);
149
+ tester.pin_set_pull (scl, MbedTester::PullUp);
150
+
151
+ // Initialize mbed I2C pins
152
+ i2c_t i2c;
153
+ memset (&i2c, 0 , sizeof (i2c));
154
+ i2c_init (&i2c, sda, scl);
155
+ i2c_frequency (&i2c, 100000 );
156
+
157
+ // Reset tester stats and select I2C
158
+ tester.peripherals_reset ();
159
+ tester.select_peripheral (I2CTester::PeripheralI2C);
160
+
161
+ // Data out and in buffers and initialization
162
+ uint8_t data_out[TRANSFER_COUNT];
163
+ for (int i = 0 ; i < TRANSFER_COUNT; i++) {
164
+ data_out[i] = i & 0xFF ;
165
+ }
166
+ uint8_t data_in[TRANSFER_COUNT];
167
+ for (int i = 0 ; i < TRANSFER_COUNT; i++) {
168
+ data_in[i] = 0 ;
169
+ }
140
170
141
- printf (" I2C complete read transaction test on sda=%s (%i), scl=%s (%i)\r\n " ,
142
- pinmap_ff_default_pin_to_string (sda), sda,
143
- pinmap_ff_default_pin_to_string (scl), scl);
171
+ int num_writes;
172
+ int num_reads;
173
+ int num_acks;
174
+ int num_nacks;
175
+ int num_starts;
176
+ int num_stops;
177
+ uint32_t checksum;
178
+ int num_dev_addr_matches;
179
+ int ack_nack;// 0 if NACK was received, 1 if ACK was received, 2 for timeout
144
180
145
181
// Reset tester stats and select I2C
146
182
tester.peripherals_reset ();
@@ -156,13 +192,15 @@ void i2c_test_all(PinName sda, PinName scl)
156
192
num_acks = 0 ;
157
193
num_nacks = 0 ;
158
194
checksum = 0 ;
159
- tester. io_metrics_start ();
195
+
160
196
num_reads = i2c_read (&i2c, (I2C_DEV_ADDR | 1 ), (char *)data_in, TRANSFER_COUNT, true ); // transaction ends with a stop condition
161
197
num_starts += 1 ;
162
198
num_stops += 1 ;
163
199
num_acks += 1 ;
200
+ num_acks += TRANSFER_COUNT - 1 ;
201
+ num_nacks += 1 ;
164
202
num_dev_addr_matches += 1 ;
165
- tester. io_metrics_stop ();
203
+
166
204
for (int i = 0 ; i < TRANSFER_COUNT; i++) {
167
205
checksum += data_in[i];
168
206
}
@@ -175,17 +213,56 @@ void i2c_test_all(PinName sda, PinName scl)
175
213
TEST_ASSERT_EQUAL (num_stops, tester.num_stops ());
176
214
TEST_ASSERT_EQUAL (num_acks, tester.num_acks ());
177
215
TEST_ASSERT_EQUAL (num_nacks, tester.num_nacks ());
178
- TEST_ASSERT_EQUAL (checksum, tester.get_receive_checksum ());
216
+ TEST_ASSERT_EQUAL (checksum, tester.get_send_checksum ());
179
217
TEST_ASSERT_EQUAL (0 , tester.state_num ());
180
- TEST_ASSERT_EQUAL ((TRANSFER_COUNT & 0xFF ), tester.get_next_from_slave ());
218
+ TEST_ASSERT_EQUAL ((( TRANSFER_COUNT + 1 ) & 0xFF ), tester.get_next_from_slave ());
181
219
TEST_ASSERT_EQUAL (num_writes, tester.num_writes ());
182
220
TEST_ASSERT_EQUAL (num_reads, tester.num_reads ());
183
221
184
- printf (" Pin combination works\r\n " );
222
+ tester.reset ();
223
+ tester.pin_set_pull (sda, MbedTester::PullNone);
224
+ tester.pin_set_pull (scl, MbedTester::PullNone);
225
+ }
226
+
227
+ void i2c_test_byte_write (PinName sda, PinName scl)
228
+ {
229
+ // Remap pins for test
230
+ tester.reset ();
231
+ tester.pin_map_set (sda, MbedTester::LogicalPinI2CSda);
232
+ tester.pin_map_set (scl, MbedTester::LogicalPinI2CScl);
233
+
234
+ tester.pin_set_pull (sda, MbedTester::PullUp);
235
+ tester.pin_set_pull (scl, MbedTester::PullUp);
185
236
186
- printf (" I2C write single bytes test on sda=%s (%i), scl=%s (%i)\r\n " ,
187
- pinmap_ff_default_pin_to_string (sda), sda,
188
- pinmap_ff_default_pin_to_string (scl), scl);
237
+ // Initialize mbed I2C pins
238
+ i2c_t i2c;
239
+ memset (&i2c, 0 , sizeof (i2c));
240
+ i2c_init (&i2c, sda, scl);
241
+ i2c_frequency (&i2c, 100000 );
242
+
243
+ // Reset tester stats and select I2C
244
+ tester.peripherals_reset ();
245
+ tester.select_peripheral (I2CTester::PeripheralI2C);
246
+
247
+ // Data out and in buffers and initialization
248
+ uint8_t data_out[TRANSFER_COUNT];
249
+ for (int i = 0 ; i < TRANSFER_COUNT; i++) {
250
+ data_out[i] = i & 0xFF ;
251
+ }
252
+ uint8_t data_in[TRANSFER_COUNT];
253
+ for (int i = 0 ; i < TRANSFER_COUNT; i++) {
254
+ data_in[i] = 0 ;
255
+ }
256
+
257
+ int num_writes;
258
+ int num_reads;
259
+ int num_acks;
260
+ int num_nacks;
261
+ int num_starts;
262
+ int num_stops;
263
+ uint32_t checksum;
264
+ int num_dev_addr_matches;
265
+ int ack_nack;// 0 if NACK was received, 1 if ACK was received, 2 for timeout
189
266
190
267
// Reset tester stats and select I2C
191
268
tester.peripherals_reset ();
@@ -201,7 +278,7 @@ void i2c_test_all(PinName sda, PinName scl)
201
278
num_acks = 0 ;
202
279
num_nacks = 0 ;
203
280
checksum = 0 ;
204
- tester. io_metrics_start ();
281
+
205
282
i2c_start (&i2c);// start condition
206
283
num_starts += 1 ;
207
284
i2c_byte_write (&i2c, I2C_DEV_ADDR);// send device address
@@ -221,7 +298,6 @@ void i2c_test_all(PinName sda, PinName scl)
221
298
}
222
299
i2c_stop (&i2c);
223
300
num_stops += 1 ;
224
- tester.io_metrics_stop ();
225
301
226
302
// Verify that the transfer was successful
227
303
TEST_ASSERT_EQUAL (num_dev_addr_matches, tester.num_dev_addr_matches ());
@@ -240,11 +316,50 @@ void i2c_test_all(PinName sda, PinName scl)
240
316
TEST_ASSERT_EQUAL (num_writes, tester.num_writes ());
241
317
TEST_ASSERT_EQUAL (num_reads, tester.num_reads ());
242
318
243
- printf (" Pin combination works\r\n " );
319
+ tester.reset ();
320
+ tester.pin_set_pull (sda, MbedTester::PullNone);
321
+ tester.pin_set_pull (scl, MbedTester::PullNone);
322
+ }
323
+
324
+ void i2c_test_byte_read (PinName sda, PinName scl)
325
+ {
326
+ // Remap pins for test
327
+ tester.reset ();
328
+ tester.pin_map_set (sda, MbedTester::LogicalPinI2CSda);
329
+ tester.pin_map_set (scl, MbedTester::LogicalPinI2CScl);
244
330
245
- printf (" I2C read single bytes test on sda=%s (%i), scl=%s (%i)\r\n " ,
246
- pinmap_ff_default_pin_to_string (sda), sda,
247
- pinmap_ff_default_pin_to_string (scl), scl);
331
+ tester.pin_set_pull (sda, MbedTester::PullUp);
332
+ tester.pin_set_pull (scl, MbedTester::PullUp);
333
+
334
+ // Initialize mbed I2C pins
335
+ i2c_t i2c;
336
+ memset (&i2c, 0 , sizeof (i2c));
337
+ i2c_init (&i2c, sda, scl);
338
+ i2c_frequency (&i2c, 100000 );
339
+
340
+ // Reset tester stats and select I2C
341
+ tester.peripherals_reset ();
342
+ tester.select_peripheral (I2CTester::PeripheralI2C);
343
+
344
+ // Data out and in buffers and initialization
345
+ uint8_t data_out[TRANSFER_COUNT];
346
+ for (int i = 0 ; i < TRANSFER_COUNT; i++) {
347
+ data_out[i] = i & 0xFF ;
348
+ }
349
+ uint8_t data_in[TRANSFER_COUNT];
350
+ for (int i = 0 ; i < TRANSFER_COUNT; i++) {
351
+ data_in[i] = 0 ;
352
+ }
353
+
354
+ int num_writes;
355
+ int num_reads;
356
+ int num_acks;
357
+ int num_nacks;
358
+ int num_starts;
359
+ int num_stops;
360
+ uint32_t checksum;
361
+ int num_dev_addr_matches;
362
+ int ack_nack;// 0 if NACK was received, 1 if ACK was received, 2 for timeout
248
363
249
364
// Reset tester stats and select I2C
250
365
tester.peripherals_reset ();
@@ -263,7 +378,7 @@ void i2c_test_all(PinName sda, PinName scl)
263
378
num_acks = 0 ;
264
379
num_nacks = 0 ;
265
380
checksum = 0 ;
266
- tester. io_metrics_start ();
381
+
267
382
i2c_start (&i2c);// start condition
268
383
num_starts += 1 ;
269
384
i2c_byte_write (&i2c, (I2C_DEV_ADDR | 1 ));// send device address for reading
@@ -274,16 +389,17 @@ void i2c_test_all(PinName sda, PinName scl)
274
389
data_in[i] = i2c_byte_read (&i2c, 1 );// send NACK
275
390
checksum += data_in[i];
276
391
num_reads += 1 ;
392
+ num_nacks += 1 ;
277
393
} else {
278
394
data_in[i] = i2c_byte_read (&i2c, 0 );// send ACK
279
395
checksum += data_in[i];
280
396
num_reads += 1 ;
397
+ num_acks += 1 ;
281
398
}
282
399
}
283
400
284
401
i2c_stop (&i2c);
285
402
num_stops += 1 ;
286
- tester.io_metrics_stop ();
287
403
288
404
// Verify that the transfer was successful
289
405
TEST_ASSERT_EQUAL (num_dev_addr_matches, tester.num_dev_addr_matches ());
@@ -293,23 +409,23 @@ void i2c_test_all(PinName sda, PinName scl)
293
409
TEST_ASSERT_EQUAL (num_stops, tester.num_stops ());
294
410
TEST_ASSERT_EQUAL (num_acks, tester.num_acks ());
295
411
TEST_ASSERT_EQUAL (num_nacks, tester.num_nacks ());
296
- TEST_ASSERT_EQUAL (checksum, tester.get_receive_checksum ());
412
+ TEST_ASSERT_EQUAL (checksum, tester.get_send_checksum ());
297
413
TEST_ASSERT_EQUAL (0 , tester.state_num ());
298
- TEST_ASSERT_EQUAL ((TRANSFER_COUNT & 0xFF ), tester.get_next_from_slave ());
414
+ TEST_ASSERT_EQUAL ((( TRANSFER_COUNT + 2 ) & 0xFF ), tester.get_next_from_slave ());
299
415
TEST_ASSERT_EQUAL (num_writes, tester.num_writes ());
300
416
TEST_ASSERT_EQUAL (num_reads, tester.num_reads ());
301
417
302
- printf (" Pin combination works\r\n " );
303
-
304
418
tester.reset ();
419
+ tester.pin_set_pull (sda, MbedTester::PullNone);
420
+ tester.pin_set_pull (scl, MbedTester::PullNone);
305
421
}
306
422
307
423
Case cases[] = {
308
424
Case (" i2c - init/free test all pins" , all_ports<I2CPort, DefaultFormFactor, test_i2c_init_free>),
309
- Case (" i2c - test all i2c APIs " , all_peripherals<I2CPort, DefaultFormFactor, i2c_test_all>)
310
- // Case("i2c - simple write test all peripherals ", all_peripherals<I2CPort, DefaultFormFactor, test_i2c_simple_write >),
311
- // Case("i2c - simple read test all peripherals ", all_peripherals<I2CPort, DefaultFormFactor, test_i2c_simple_read >),
312
- // Case("i2c - XXXXXXX test single pin", one_peripheral <I2CPort, DefaultFormFactor, test_i2c_XXXXXXX >)
425
+ Case (" i2c - test write i2c API " , all_peripherals<I2CPort, DefaultFormFactor, i2c_test_write>),
426
+ Case (" i2c - test read i2c API " , all_peripherals<I2CPort, DefaultFormFactor, i2c_test_read >),
427
+ Case (" i2c - test single byte write i2c API " , all_peripherals<I2CPort, DefaultFormFactor, i2c_test_byte_write >),
428
+ Case (" i2c - test single byte read i2c API " , all_peripherals <I2CPort, DefaultFormFactor, i2c_test_byte_read >)
313
429
};
314
430
315
431
utest::v1::status_t greentea_test_setup (const size_t number_of_cases)
0 commit comments