1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
/* Copyright 2019 The ChromiumOS Authors
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "common.h"
#include "console.h"
#include "i2c.h"
#include "i2c_bitbang.h"
#include "test_util.h"
#include "util.h"
const struct i2c_port_t i2c_bitbang_ports[] = { { .name = "",
.port = 0,
.kbps = 100,
.scl = GPIO_I2C_SCL,
.sda = GPIO_I2C_SDA } };
const unsigned int i2c_bitbang_ports_used = 1;
struct pin_state {
int scl, sda;
} history[64];
int history_count;
void reset_state(void)
{
history[0] = (struct pin_state){ 1, 1 };
history_count = 1;
bitbang_set_started(0);
}
void gpio_set_level(enum gpio_signal signal, int level)
{
struct pin_state new = history[history_count - 1];
/* reject if stack is full */
if (history_count >= ARRAY_SIZE(history))
return;
if (signal == GPIO_I2C_SDA)
new.sda = level;
else if (signal == GPIO_I2C_SCL)
new.scl = level;
if (new.scl != history[history_count - 1].scl ||
new.sda != history[history_count - 1].sda)
history[history_count++] = new;
}
int gpio_get_level(enum gpio_signal signal)
{
if (signal == GPIO_I2C_SDA)
return history[history_count - 1].sda;
else if (signal == GPIO_I2C_SCL)
return history[history_count - 1].scl;
return 0;
}
static int test_i2c_start_stop(void)
{
struct pin_state expected[] = {
/* start */
{ 1, 1 },
{ 1, 0 },
{ 0, 0 },
/* stop */
{ 1, 0 },
{ 1, 1 },
};
int i;
reset_state();
bitbang_start_cond(&i2c_bitbang_ports[0]);
bitbang_stop_cond(&i2c_bitbang_ports[0]);
TEST_EQ((int)ARRAY_SIZE(expected), history_count, "%d");
for (i = 0; i < ARRAY_SIZE(expected); i++) {
TEST_EQ(expected[i].scl, history[i].scl, "%d");
TEST_EQ(expected[i].sda, history[i].sda, "%d");
}
return EC_SUCCESS;
}
static int test_i2c_repeated_start(void)
{
struct pin_state expected[] = {
/* start */
{ 1, 1 },
{ 1, 0 },
{ 0, 0 },
/* repeated start */
{ 0, 1 },
{ 1, 1 },
{ 1, 0 },
{ 0, 0 },
};
int i;
reset_state();
bitbang_start_cond(&i2c_bitbang_ports[0]);
bitbang_start_cond(&i2c_bitbang_ports[0]);
TEST_EQ((int)ARRAY_SIZE(expected), history_count, "%d");
for (i = 0; i < ARRAY_SIZE(expected); i++) {
TEST_EQ(expected[i].scl, history[i].scl, "%d");
TEST_EQ(expected[i].sda, history[i].sda, "%d");
}
return EC_SUCCESS;
}
static int test_i2c_write(void)
{
struct pin_state expected[] = {
/* start */
{ 1, 1 },
{ 1, 0 },
{ 0, 0 },
/* bit 7: 0 */
{ 1, 0 },
{ 0, 0 },
/* bit 6: 1 */
{ 0, 1 },
{ 1, 1 },
{ 0, 1 },
/* bit 5: 0 */
{ 0, 0 },
{ 1, 0 },
{ 0, 0 },
/* bit 4: 1 */
{ 0, 1 },
{ 1, 1 },
{ 0, 1 },
/* bit 3: 0 */
{ 0, 0 },
{ 1, 0 },
{ 0, 0 },
/* bit 2: 1 */
{ 0, 1 },
{ 1, 1 },
{ 0, 1 },
/* bit 1: 1 */
{ 1, 1 },
{ 0, 1 },
/* bit 0: 0 */
{ 0, 0 },
{ 1, 0 },
{ 0, 0 },
/* read bit */
{ 0, 1 },
{ 1, 1 },
{ 0, 1 },
/* stop */
{ 0, 0 },
{ 1, 0 },
{ 1, 1 },
};
int i, ret;
reset_state();
bitbang_start_cond(&i2c_bitbang_ports[0]);
ret = bitbang_write_byte(&i2c_bitbang_ports[0], 0x56);
/* expected to fail because no slave answering the nack bit */
TEST_EQ(EC_ERROR_BUSY, ret, "%d");
TEST_EQ((int)ARRAY_SIZE(expected), history_count, "%d");
for (i = 0; i < ARRAY_SIZE(expected); i++) {
TEST_EQ(expected[i].scl, history[i].scl, "%d");
TEST_EQ(expected[i].sda, history[i].sda, "%d");
}
return EC_SUCCESS;
}
void run_test(int argc, const char **argv)
{
test_reset();
RUN_TEST(test_i2c_start_stop);
RUN_TEST(test_i2c_repeated_start);
RUN_TEST(test_i2c_write);
test_print_result();
}
|