Skip to content

Commit 49da1b8

Browse files
mateusz-holenkonashif
authored andcommitted
drivers: i2c_bitbang: read SDA on high clock
According to the I2C spec, the SDA signal must be stable as long as the SCL signal is high (which means it can change only when clock is low). This commit reworks clock signal handling in such a way that all reads are done when SCL is high and SDA is stable. Signed-off-by: Mateusz Holenko <[email protected]>
1 parent 382e6fb commit 49da1b8

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

drivers/i2c/i2c_bitbang.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ static void i2c_start(struct i2c_bitbang *context)
101101
}
102102
i2c_set_sda(context, 0);
103103
i2c_delay(context->delays[T_HD_STA]);
104+
105+
i2c_set_scl(context, 0);
106+
i2c_delay(context->delays[T_LOW]);
104107
}
105108

106109
static void i2c_repeated_start(struct i2c_bitbang *context)
@@ -111,6 +114,9 @@ static void i2c_repeated_start(struct i2c_bitbang *context)
111114

112115
static void i2c_stop(struct i2c_bitbang *context)
113116
{
117+
i2c_set_scl(context, 1);
118+
i2c_delay(context->delays[T_HIGH]);
119+
114120
if (i2c_get_sda(context)) {
115121
/*
116122
* SDA is already high, so we need to make it low so that
@@ -128,25 +134,28 @@ static void i2c_stop(struct i2c_bitbang *context)
128134

129135
static void i2c_write_bit(struct i2c_bitbang *context, int bit)
130136
{
131-
i2c_set_scl(context, 0);
132137
/* SDA hold time is zero, so no need for a delay here */
133138
i2c_set_sda(context, bit);
134-
i2c_delay(context->delays[T_LOW]);
135139
i2c_set_scl(context, 1);
136140
i2c_delay(context->delays[T_HIGH]);
141+
i2c_set_scl(context, 0);
142+
i2c_delay(context->delays[T_LOW]);
137143
}
138144

139145
static bool i2c_read_bit(struct i2c_bitbang *context)
140146
{
141147
bool bit;
142148

143-
i2c_set_scl(context, 0);
144149
/* SDA hold time is zero, so no need for a delay here */
145150
i2c_set_sda(context, 1); /* Stop driving low, so slave has control */
146-
i2c_delay(context->delays[T_LOW]);
147-
bit = i2c_get_sda(context);
151+
148152
i2c_set_scl(context, 1);
149153
i2c_delay(context->delays[T_HIGH]);
154+
155+
bit = i2c_get_sda(context);
156+
157+
i2c_set_scl(context, 0);
158+
i2c_delay(context->delays[T_LOW]);
150159
return bit;
151160
}
152161

0 commit comments

Comments
 (0)