3 /******************************************************************************
4 * This will test an I2C EEPROM connected to mbed by writing a predefined byte at
5 * address 0 and then reading it back and comparing it with the known byte value a
6 * number of times. This test was written specifically for reproducing the bug
9 * https://mbed.org/forum/bugs-suggestions/topic/4128/
13 * set 'ntests' to the number of iterations
14 * set 'i2c_speed_hz' to the desired speed of the I2C interface
15 * set 'i2c_delay_us' to the delay that will be inserted between 'write' and
16 * 'read' I2C operations (https://mbed.org/users/mbed_official/code/mbed/issues/1
17 * for more details). '0' disables the delay.
18 * define I2C_EEPROM_VERBOSE to get verbose output
20 * The test ran with a 24LC256 external EEPROM memory, but any I2C EEPROM memory
21 * that uses two byte addresses should work.
22 ******************************************************************************/
24 #if defined(TARGET_KL25Z)
27 #elif defined(TARGET_KL46Z)
30 #elif defined(TARGET_K64F)
31 I2C
i2c(PTE25
, PTE24
);
33 #elif defined(TARGET_K22F)
36 #elif defined(TARGET_K20D50M)
39 #elif defined(TARGET_LPC812)
40 I2C
i2c(P0_10
, P0_11
);
42 #elif defined(TARGET_LPC1549)
43 I2C
i2c(P0_23
, P0_22
);
45 #elif defined(TARGET_LPC11U68)
48 #elif defined(TARGET_DELTA_DFCM_NNN40)
49 I2C
i2c(I2C_SDA0
, I2C_SCL0
);
51 #elif defined(TARGET_NUCLEO_F030R8) || \
52 defined(TARGET_NUCLEO_F070RB) || \
53 defined(TARGET_NUCLEO_F072RB) || \
54 defined(TARGET_NUCLEO_F091RC) || \
55 defined(TARGET_NUCLEO_F103RB) || \
56 defined(TARGET_NUCLEO_F302R8) || \
57 defined(TARGET_NUCLEO_F303RE) || \
58 defined(TARGET_NUCLEO_F334R8) || \
59 defined(TARGET_NUCLEO_F401RE) || \
60 defined(TARGET_NUCLEO_F411RE) || \
61 defined(TARGET_NUCLEO_L053R8) || \
62 defined(TARGET_NUCLEO_L073RZ) || \
63 defined(TARGET_NUCLEO_L152RE) || \
64 defined(TARGET_FF_ARDUINO)
65 I2C
i2c(I2C_SDA
, I2C_SCL
);
72 const int ntests
= 10000;
73 const int i2c_freq_hz
= 400000;
74 const int i2c_delay_us
= 0;
78 MBED_HOSTTEST_TIMEOUT(15);
79 MBED_HOSTTEST_SELECT(default_auto
);
80 MBED_HOSTTEST_DESCRIPTION(I2C EEPROM read write test
);
81 MBED_HOSTTEST_START("MBED_A19");
83 const int EEPROM_MEM_ADDR
= 0xA0;
84 const char MARK
= 0x66;
91 i2c
.frequency(i2c_freq_hz
);
92 printf("I2C: I2C Frequency: %d Hz\r\n", i2c_freq_hz
);
94 printf("I2C: Write 0x%2X at address 0x0000 test ... \r\n", MARK
);
97 char data
[] = { 0, 0, MARK
};
98 if ((i2c_stat
= i2c
.write(EEPROM_MEM_ADDR
, data
, sizeof(data
))) != 0) {
99 printf("Unable to write data to EEPROM (i2c_stat = 0x%02X), aborting\r\n", i2c_stat
);
100 notify_completion(false);
104 // ACK polling (assumes write will be successful eventually)
105 while (i2c
.write(EEPROM_MEM_ADDR
, data
, 0) != 0)
109 printf("I2C: Read data from address 0x0000 test ... \r\n");
110 // Data read (actual test)
111 for (int i
= 0; i
< ntests
; i
++) {
112 // Write data to EEPROM memory
114 char data
[] = { 0, 0 };
115 if ((i2c_stat
= i2c
.write(EEPROM_MEM_ADDR
, data
, 2, true)) != 0) {
116 printf("Test %d failed at write, i2c_stat is 0x%02X\r\n", i
, i2c_stat
);
122 // us delay if specified
123 if (i2c_delay_us
!= 0)
124 wait_us(i2c_delay_us
);
128 char data
[1] = { 0 };
129 if ((i2c_stat
= i2c
.read(EEPROM_MEM_ADDR
, data
, 1)) != 0) {
130 printf("Test %d failed at read, i2c_stat is 0x%02X\r\n", i
, i2c_stat
);
135 if (data
[0] != MARK
) {
136 printf("Test %d failed at data match\r\n", i
);
142 result
= (fw
+ fr
+ fc
== 0);
143 printf("EEPROM: Test result ... [%s]\r\n", result
? "OK" : "FAIL");
146 printf("Test Statistics:\r\n");
147 printf("\tTotal tests: %d\r\n", ntests
);
148 printf("\tFailed at write: %d\r\n", fw
);
149 printf("\tFailed at read: %d\r\n", fr
);
150 printf("\tData mismatch: %d\r\n", fc
);
151 printf("\tTotal failures: %d\r\n", fw
+ fr
+ fc
);
154 MBED_HOSTTEST_RESULT(result
);