]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/tests/mbed/i2c_eeprom/main.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / tests / mbed / i2c_eeprom / main.cpp
1 #include "test_env.h"
2
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
7 * reported here:
8 *
9 * https://mbed.org/forum/bugs-suggestions/topic/4128/
10 *
11 * Test configuration:
12 *
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
19 *
20 * The test ran with a 24LC256 external EEPROM memory, but any I2C EEPROM memory
21 * that uses two byte addresses should work.
22 ******************************************************************************/
23
24 #if defined(TARGET_KL25Z)
25 I2C i2c(PTC9, PTC8);
26
27 #elif defined(TARGET_KL46Z)
28 I2C i2c(PTC9, PTC8);
29
30 #elif defined(TARGET_K64F)
31 I2C i2c(PTE25, PTE24);
32
33 #elif defined(TARGET_K22F)
34 I2C i2c(PTE0, PTE1);
35
36 #elif defined(TARGET_K20D50M)
37 I2C i2c(PTB3, PTB2);
38
39 #elif defined(TARGET_LPC812)
40 I2C i2c(P0_10, P0_11);
41
42 #elif defined(TARGET_LPC1549)
43 I2C i2c(P0_23, P0_22);
44
45 #elif defined(TARGET_LPC11U68)
46 I2C i2c(SDA, SCL);
47
48 #elif defined(TARGET_DELTA_DFCM_NNN40)
49 I2C i2c(I2C_SDA0, I2C_SCL0);
50
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);
66
67 #else
68 I2C i2c(p28, p27);
69 #endif
70
71 namespace {
72 const int ntests = 10000;
73 const int i2c_freq_hz = 400000;
74 const int i2c_delay_us = 0;
75 }
76
77 int main() {
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");
82
83 const int EEPROM_MEM_ADDR = 0xA0;
84 const char MARK = 0x66;
85 int fw = 0;
86 int fr = 0;
87 int fc = 0;
88 int i2c_stat = 0;
89 bool result = true;
90
91 i2c.frequency(i2c_freq_hz);
92 printf("I2C: I2C Frequency: %d Hz\r\n", i2c_freq_hz);
93
94 printf("I2C: Write 0x%2X at address 0x0000 test ... \r\n", MARK);
95 // Data write
96 {
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);
101 return 1;
102 }
103
104 // ACK polling (assumes write will be successful eventually)
105 while (i2c.write(EEPROM_MEM_ADDR, data, 0) != 0)
106 ;
107 }
108
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
113 {
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);
117 fw++;
118 continue;
119 }
120 }
121
122 // us delay if specified
123 if (i2c_delay_us != 0)
124 wait_us(i2c_delay_us);
125
126 // Read data
127 {
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);
131 fr++;
132 continue;
133 }
134
135 if (data[0] != MARK) {
136 printf("Test %d failed at data match\r\n", i);
137 fc++;
138 }
139 }
140 }
141
142 result = (fw + fr + fc == 0);
143 printf("EEPROM: Test result ... [%s]\r\n", result ? "OK" : "FAIL");
144
145 if (!result) {
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);
152 }
153
154 MBED_HOSTTEST_RESULT(result);
155 }
Imprint / Impressum