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 // Test configuration block
26 const int ntests = 1000;
27 const int i2c_freq_hz = 400000;
28 const int i2c_delay_us = 0;
29 // const int EEPROM_24LC256_SIZE = (256 * 1024 / 8); // 256 kbit memory
32 // End of test configuration block
34 #if defined(TARGET_KL25Z)
37 #elif defined(TARGET_KL46Z)
40 #elif defined(TARGET_K64F)
41 I2C i2c(PTE25, PTE24);
43 #elif defined(TARGET_K22F)
46 #elif defined(TARGET_K20D50M)
49 #elif defined(TARGET_LPC812)
50 I2C i2c(P0_10, P0_11);
52 #elif defined(TARGET_LPC1549)
53 I2C i2c(P0_23, P0_22);
55 #elif defined(TARGET_LPC11U68)
58 #elif defined(TARGET_DELTA_DFCM_NNN40)
59 I2C i2c(I2C_SDA0, I2C_SCL0);
61 #elif defined(TARGET_NUCLEO_F030R8) || \
62 defined(TARGET_NUCLEO_F070RB) || \
63 defined(TARGET_NUCLEO_F072RB) || \
64 defined(TARGET_NUCLEO_F091RC) || \
65 defined(TARGET_NUCLEO_F103RB) || \
66 defined(TARGET_NUCLEO_F302R8) || \
67 defined(TARGET_NUCLEO_F303RE) || \
68 defined(TARGET_NUCLEO_F334R8) || \
69 defined(TARGET_NUCLEO_F401RE) || \
70 defined(TARGET_NUCLEO_F411RE) || \
71 defined(TARGET_NUCLEO_L053R8) || \
72 defined(TARGET_NUCLEO_L073RZ) || \
73 defined(TARGET_NUCLEO_L152RE) || \
74 defined(TARGET_FF_ARDUINO)
75 I2C i2c(I2C_SDA, I2C_SCL);
81 #define PATTERN_MASK 0x66, ~0x66, 0x00, 0xFF, 0xA5, 0x5A, 0xF0, 0x0F
84 MBED_HOSTTEST_TIMEOUT(15);
85 MBED_HOSTTEST_SELECT(default_auto);
86 MBED_HOSTTEST_DESCRIPTION(I2C EEPROM line read write test);
87 MBED_HOSTTEST_START("MBED_A25");
89 const int EEPROM_MEM_ADDR = 0xA0;
92 i2c.frequency(i2c_freq_hz);
93 printf("I2C: I2C Frequency: %d Hz\r\n", i2c_freq_hz);
95 printf("I2C: Lines pattern write test ... ");
97 for (int i = 0; i < ntests; i++) {
98 char data[] = { 0 /*MSB*/, 0 /*LSB*/, PATTERN_MASK };
99 const int addr = i * 8; // 8 bytes of data in data array
100 data[0] = ((0xFF00 & addr) >> 8) & 0x00FF;
101 data[1] = (addr & 0x00FF);
103 if (i2c.write(EEPROM_MEM_ADDR, data, sizeof(data)) != 0) {
107 while (i2c.write(EEPROM_MEM_ADDR, NULL, 0)) ; // wait to complete
109 // us delay if specified
110 if (i2c_delay_us != 0) {
111 wait_us(i2c_delay_us);
115 printf("[%s]\r\n", write_errors ? "FAIL" : "OK");
116 printf("I2C: Write errors: %d ... [%s]\r\n", write_errors, write_errors ? "FAIL" : "OK");
118 printf("I2C: Lines pattern read test ... ");
120 int pattern_errors = 0;
121 for (int i = 0; i < ntests; i++) {
122 char data[8] = { 0 }; // General puspose buffer
123 const int addr = i * 8; // 8 bytes of data in data array
124 data[0] = ((0xFF00 & addr) >> 8) & 0x00FF;
125 data[1] = (addr & 0x00FF);
127 // Set address for read
128 if (i2c.write(EEPROM_MEM_ADDR, data, 2, true) != 0) {
131 if (i2c.read(EEPROM_MEM_ADDR, data, 8) != 0) {
135 static char pattern[] = { PATTERN_MASK };
136 if (memcmp(pattern, data, sizeof(data))) {
141 printf("[%s]\r\n", read_errors ? "FAIL" : "OK");
142 printf("I2C: Read errors: %d/%d ... [%s]\r\n", read_errors, ntests, read_errors ? "FAIL" : "OK");
143 printf("EEPROM: Pattern match errors: %d/%d ... [%s]\r\n", pattern_errors, ntests, pattern_errors ? "FAIL" : "OK");
145 result = write_errors == 0 && read_errors == 0;
146 MBED_HOSTTEST_RESULT(result);