Commit b08fbb30 authored by Benoit Rat's avatar Benoit Rat

userspace: correct i2c scanning with pulldown line

When we had a pulldown on the i2c bus the device was okay because we
only check ack (0), now we first check if we have a pullup and then a
ACK.
parent e4f7934c
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
#define I2C_WRITE 0 #define I2C_WRITE 0
#define I2C_READ 1 #define I2C_READ 1
typedef struct i2c_bus typedef struct i2c_bus
{ {
const char *name; const char *name;
......
...@@ -123,19 +123,35 @@ static uint8_t mi2c_get_byte(struct i2c_bitbang *bus, int ack) ...@@ -123,19 +123,35 @@ static uint8_t mi2c_get_byte(struct i2c_bitbang *bus, int ack)
} }
/**
* Scan if an i2c chip reply with the given address
*
* All i2c chip are pullup, so we first check if we have a pullup connected, then we send
* the address and wait for acknowledge (pull down)
*
* \input bus Generic i2c bus
* \input address chip address on the bus
* \output Return 1 (true) or 0 (false) if the bus has replied correctly
*/
int32_t i2c_bitbang_scan(struct i2c_bus* bus, uint32_t address) int32_t i2c_bitbang_scan(struct i2c_bus* bus, uint32_t address)
{ {
if (!bus) if (!bus)
return I2C_NULL_PARAM; return I2C_NULL_PARAM;
uint8_t state;
struct i2c_bitbang* ts = (struct i2c_bitbang*)bus->type_specific; struct i2c_bitbang* ts = (struct i2c_bitbang*)bus->type_specific;
mi2c_start(ts); //Check if we have pull up on the data line of iic bus
int ack = mi2c_write_byte(ts, (address << 1) | 0); shw_pio_setdir(ts->sda, PIO_IN);
state=shw_pio_get(ts->sda);
if(state!=1) return 0;
//Then write address and check acknowledge
mi2c_start(ts);
state = mi2c_write_byte(ts, (address << 1) | 0);
mi2c_stop(ts); mi2c_stop(ts);
return ack ? 1: 0; return state ? 1: 0;
} }
int i2c_bitbang_transfer(i2c_bus_t* bus, uint32_t address, uint32_t to_write, uint32_t to_read, uint8_t* data) int i2c_bitbang_transfer(i2c_bus_t* bus, uint32_t address, uint32_t to_write, uint32_t to_read, uint8_t* data)
......
...@@ -43,14 +43,14 @@ ...@@ -43,14 +43,14 @@
pio_pin_t wr_i2c_io_sda = { pio_pin_t wr_i2c_io_sda = {
.port = PIOB, .port = PIOB,
.pin = 24, .pin = 24,
.mode = PIO_MODE_PULLUP | PIO_MODE_GPIO, .mode = PIO_MODE_GPIO, //PullUp by i2c when miniBP >v3.3, PullDown in miniBP v3.2
.dir = PIO_OUT_0, .dir = PIO_OUT_0,
}; };
//Connected to miniBP to PB7>PB0>PB20 //Connected to miniBP to PB7>PB0>PB20
pio_pin_t wr_i2c_io_scl = { pio_pin_t wr_i2c_io_scl = {
.port = PIOB, .port = PIOB,
.pin = 20, .pin = 20,
.mode = PIO_MODE_PULLUP | PIO_MODE_GPIO, .mode = PIO_MODE_GPIO, //PullUp by i2c when miniBP >v3.3, PullDown in miniBP v3.2
.dir = PIO_OUT_0, .dir = PIO_OUT_0,
}; };
struct i2c_bitbang wr_i2c_io_reg = { struct i2c_bitbang wr_i2c_io_reg = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment