Skip to content

Commit 261e67b

Browse files
committed
FPGA I2C test update.
Divide one test case to four test cases to increase readability. Adapted to the last version of the FPGA CI Test Shield API.
1 parent cd0ad51 commit 261e67b

File tree

1 file changed

+153
-37
lines changed
  • TESTS/mbed_hal_fpga_ci_test_shield/i2c

1 file changed

+153
-37
lines changed

TESTS/mbed_hal_fpga_ci_test_shield/i2c/main.cpp

Lines changed: 153 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#error [NOT_SUPPORTED] I2C not supported for this target
2020
#elif !COMPONENT_FPGA_CI_TEST_SHIELD
2121
#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
2422
#else
2523

2624
#include "utest/utest.h"
@@ -53,13 +51,16 @@ void test_i2c_init_free(PinName sda, PinName scl)
5351
i2c_frequency(&obj, 100000);
5452
}
5553

56-
void i2c_test_all(PinName sda, PinName scl)
54+
void i2c_test_write(PinName sda, PinName scl)
5755
{
5856
// Remap pins for test
5957
tester.reset();
6058
tester.pin_map_set(sda, MbedTester::LogicalPinI2CSda);
6159
tester.pin_map_set(scl, MbedTester::LogicalPinI2CScl);
6260

61+
tester.pin_set_pull(sda, MbedTester::PullUp);
62+
tester.pin_set_pull(scl, MbedTester::PullUp);
63+
6364
// Initialize mbed I2C pins
6465
i2c_t i2c;
6566
memset(&i2c, 0, sizeof(i2c));
@@ -90,10 +91,6 @@ void i2c_test_all(PinName sda, PinName scl)
9091
int num_dev_addr_matches;
9192
int ack_nack;//0 if NACK was received, 1 if ACK was received, 2 for timeout
9293

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-
9794
// Reset tester stats and select I2C
9895
tester.peripherals_reset();
9996
tester.select_peripheral(MbedTester::PeripheralI2C);
@@ -108,13 +105,13 @@ void i2c_test_all(PinName sda, PinName scl)
108105
num_acks = 0;
109106
num_nacks = 0;
110107
checksum = 0;
111-
tester.io_metrics_start();
108+
112109
num_writes = i2c_write(&i2c, I2C_DEV_ADDR, (char *)data_out, TRANSFER_COUNT, true); //transaction ends with a stop condition
113110
num_acks = num_writes + 1;
114111
num_starts += 1;
115112
num_stops += 1;
116113
num_dev_addr_matches += 1;
117-
tester.io_metrics_stop();
114+
118115
for (int i = 0; i < TRANSFER_COUNT; i++) {
119116
checksum += data_out[i];
120117
}
@@ -136,11 +133,50 @@ void i2c_test_all(PinName sda, PinName scl)
136133
TEST_ASSERT_EQUAL(num_writes, tester.num_writes());
137134
TEST_ASSERT_EQUAL(num_reads, tester.num_reads());
138135

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+
}
140170

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
144180

145181
// Reset tester stats and select I2C
146182
tester.peripherals_reset();
@@ -156,13 +192,15 @@ void i2c_test_all(PinName sda, PinName scl)
156192
num_acks = 0;
157193
num_nacks = 0;
158194
checksum = 0;
159-
tester.io_metrics_start();
195+
160196
num_reads = i2c_read(&i2c, (I2C_DEV_ADDR | 1), (char *)data_in, TRANSFER_COUNT, true); //transaction ends with a stop condition
161197
num_starts += 1;
162198
num_stops += 1;
163199
num_acks += 1;
200+
num_acks += TRANSFER_COUNT - 1;
201+
num_nacks += 1;
164202
num_dev_addr_matches += 1;
165-
tester.io_metrics_stop();
203+
166204
for (int i = 0; i < TRANSFER_COUNT; i++) {
167205
checksum += data_in[i];
168206
}
@@ -175,17 +213,56 @@ void i2c_test_all(PinName sda, PinName scl)
175213
TEST_ASSERT_EQUAL(num_stops, tester.num_stops());
176214
TEST_ASSERT_EQUAL(num_acks, tester.num_acks());
177215
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());
179217
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());
181219
TEST_ASSERT_EQUAL(num_writes, tester.num_writes());
182220
TEST_ASSERT_EQUAL(num_reads, tester.num_reads());
183221

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);
185236

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
189266

190267
// Reset tester stats and select I2C
191268
tester.peripherals_reset();
@@ -201,7 +278,7 @@ void i2c_test_all(PinName sda, PinName scl)
201278
num_acks = 0;
202279
num_nacks = 0;
203280
checksum = 0;
204-
tester.io_metrics_start();
281+
205282
i2c_start(&i2c);//start condition
206283
num_starts += 1;
207284
i2c_byte_write(&i2c, I2C_DEV_ADDR);//send device address
@@ -221,7 +298,6 @@ void i2c_test_all(PinName sda, PinName scl)
221298
}
222299
i2c_stop(&i2c);
223300
num_stops += 1;
224-
tester.io_metrics_stop();
225301

226302
// Verify that the transfer was successful
227303
TEST_ASSERT_EQUAL(num_dev_addr_matches, tester.num_dev_addr_matches());
@@ -240,11 +316,50 @@ void i2c_test_all(PinName sda, PinName scl)
240316
TEST_ASSERT_EQUAL(num_writes, tester.num_writes());
241317
TEST_ASSERT_EQUAL(num_reads, tester.num_reads());
242318

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);
244330

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
248363

249364
// Reset tester stats and select I2C
250365
tester.peripherals_reset();
@@ -263,7 +378,7 @@ void i2c_test_all(PinName sda, PinName scl)
263378
num_acks = 0;
264379
num_nacks = 0;
265380
checksum = 0;
266-
tester.io_metrics_start();
381+
267382
i2c_start(&i2c);//start condition
268383
num_starts += 1;
269384
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)
274389
data_in[i] = i2c_byte_read(&i2c, 1);//send NACK
275390
checksum += data_in[i];
276391
num_reads += 1;
392+
num_nacks += 1;
277393
} else {
278394
data_in[i] = i2c_byte_read(&i2c, 0);//send ACK
279395
checksum += data_in[i];
280396
num_reads += 1;
397+
num_acks += 1;
281398
}
282399
}
283400

284401
i2c_stop(&i2c);
285402
num_stops += 1;
286-
tester.io_metrics_stop();
287403

288404
// Verify that the transfer was successful
289405
TEST_ASSERT_EQUAL(num_dev_addr_matches, tester.num_dev_addr_matches());
@@ -293,23 +409,23 @@ void i2c_test_all(PinName sda, PinName scl)
293409
TEST_ASSERT_EQUAL(num_stops, tester.num_stops());
294410
TEST_ASSERT_EQUAL(num_acks, tester.num_acks());
295411
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());
297413
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());
299415
TEST_ASSERT_EQUAL(num_writes, tester.num_writes());
300416
TEST_ASSERT_EQUAL(num_reads, tester.num_reads());
301417

302-
printf(" Pin combination works\r\n");
303-
304418
tester.reset();
419+
tester.pin_set_pull(sda, MbedTester::PullNone);
420+
tester.pin_set_pull(scl, MbedTester::PullNone);
305421
}
306422

307423
Case cases[] = {
308424
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>)
313429
};
314430

315431
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)

0 commit comments

Comments
 (0)