]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/tests/mbed/i2c_eeprom_line/main.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / tests / mbed / i2c_eeprom_line / 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 // Test configuration block
25 namespace {
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
30 }
31
32 // End of test configuration block
33
34 #if defined(TARGET_KL25Z)
35 I2C i2c(PTC9, PTC8);
36
37 #elif defined(TARGET_KL46Z)
38 I2C i2c(PTC9, PTC8);
39
40 #elif defined(TARGET_K64F)
41 I2C i2c(PTE25, PTE24);
42
43 #elif defined(TARGET_K22F)
44 I2C i2c(PTE0, PTE1);
45
46 #elif defined(TARGET_K20D50M)
47 I2C i2c(PTB3, PTB2);
48
49 #elif defined(TARGET_LPC812)
50 I2C i2c(P0_10, P0_11);
51
52 #elif defined(TARGET_LPC1549)
53 I2C i2c(P0_23, P0_22);
54
55 #elif defined(TARGET_LPC11U68)
56 I2C i2c(SDA, SCL);
57
58 #elif defined(TARGET_DELTA_DFCM_NNN40)
59 I2C i2c(I2C_SDA0, I2C_SCL0);
60
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);
76
77 #else
78 I2C i2c(p28, p27);
79 #endif
80
81 #define PATTERN_MASK 0x66, ~0x66, 0x00, 0xFF, 0xA5, 0x5A, 0xF0, 0x0F
82
83 int main() {
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");
88
89 const int EEPROM_MEM_ADDR = 0xA0;
90 bool result = true;
91
92 i2c.frequency(i2c_freq_hz);
93 printf("I2C: I2C Frequency: %d Hz\r\n", i2c_freq_hz);
94
95 printf("I2C: Lines pattern write test ... ");
96 int write_errors = 0;
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);
102
103 if (i2c.write(EEPROM_MEM_ADDR, data, sizeof(data)) != 0) {
104 write_errors++;
105 }
106
107 while (i2c.write(EEPROM_MEM_ADDR, NULL, 0)) ; // wait to complete
108
109 // us delay if specified
110 if (i2c_delay_us != 0) {
111 wait_us(i2c_delay_us);
112 }
113 }
114
115 printf("[%s]\r\n", write_errors ? "FAIL" : "OK");
116 printf("I2C: Write errors: %d ... [%s]\r\n", write_errors, write_errors ? "FAIL" : "OK");
117
118 printf("I2C: Lines pattern read test ... ");
119 int read_errors = 0;
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);
126
127 // Set address for read
128 if (i2c.write(EEPROM_MEM_ADDR, data, 2, true) != 0) {
129 }
130
131 if (i2c.read(EEPROM_MEM_ADDR, data, 8) != 0) {
132 read_errors++;
133 }
134
135 static char pattern[] = { PATTERN_MASK };
136 if (memcmp(pattern, data, sizeof(data))) {
137 pattern_errors++;
138 }
139 }
140
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");
144
145 result = write_errors == 0 && read_errors == 0;
146 MBED_HOSTTEST_RESULT(result);
147 }
Imprint / Impressum