This commit is contained in:
Thomas Klaehn 2019-07-25 08:25:10 +02:00
parent 695bf90c03
commit bcc4cb8291

View File

@ -24,6 +24,8 @@ int i2c_open(const struct i2c_bb *i2c)
res = gpio_open(i2c->sda);
res |= gpio_open(i2c->scl);
res |= gpio_write(i2c->sda, 1);
res |= gpio_write(i2c->scl, 1);
return res;
}
@ -51,7 +53,7 @@ int i2c_read(const struct i2c_bb *i2c, uint8_t slave_addr, char *buffer, unsigne
}
start_condition(i2c);
write_byte(i2c, (slave_addr & 0x80));
write_byte(i2c, (slave_addr << 1) | 1);
for(i = 0; i < len; i++) {
buffer[i] = read_byte(i2c, true);
}
@ -72,7 +74,7 @@ int i2c_write(const struct i2c_bb *i2c, uint8_t slave_addr, const char *buffer,
}
start_condition(i2c);
write_byte(i2c, (slave_addr & 0x7F));
write_byte(i2c, slave_addr << 1);
for(i = 0; i < len; i++) {
write_byte(i2c, buffer[i]);
}
@ -136,7 +138,7 @@ static uint8_t read_byte(const struct i2c_bb *i2c, bool ack)
if(ack) {
write_bit(i2c, 0);
} else {
write_bit(i2c, 0);
write_bit(i2c, 1);
}
return res;
@ -146,15 +148,15 @@ static void write_bit(const struct i2c_bb *i2c, uint8_t bit)
{
assert( NULL != i2c);
if((bit & 0x01) > 0) {
if(bit > 0) {
gpio_write(i2c->sda, 1);
} else {
gpio_write(i2c->sda, 1);
gpio_write(i2c->sda, 0);
}
usleep(5);
gpio_write(i2c->scl, 1);
usleep(5);
gpio_write(i2c->scl, 1);
gpio_write(i2c->scl, 0);
}
static uint8_t read_bit(const struct i2c_bb *i2c)
@ -168,7 +170,7 @@ static uint8_t read_bit(const struct i2c_bb *i2c)
gpio_write(i2c->scl, 1);
usleep(5);
gpio_read(i2c->sda, &res);
gpio_write(i2c->scl, 1);
gpio_write(i2c->scl, 0);
return (uint8_t) res;
}