+
+
+static void i2c_bitbang_start_2(struct I2c *i2c, uint16_t slave_addr)
+{
+ if (i2c->flags & I2C_START_R)
+ slave_addr |= I2C_READBIT;
+ else
+ slave_addr &= ~I2C_READBIT;
+
+ /*
+ * Loop on the select write sequence: when the device is busy
+ * writing previously sent data it will reply to the SLA_W
+ * control byte with a NACK. In this case, we must
+ * keep trying until the deveice responds with an ACK.
+ */
+ ticks_t start = timer_clock();
+ while (i2c_bitbang_start_1(i2c))
+ {
+ i2c_bitbang_putc(i2c, slave_addr);
+
+ if (!(i2c->errors & I2C_NO_ACK))
+ return;
+ else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
+ {
+ LOG_ERR("Timeout on I2C start\n");
+ i2c->errors |= I2C_START_TIMEOUT;
+ i2c_bitbang_stop_1(i2c);
+ return;
+ }
+ }
+
+ LOG_ERR("START arbitration lost\n");
+ i2c->errors |= I2C_ARB_LOST;
+ i2c_bitbang_stop_1(i2c);
+ return;
+}
+
+
+static const I2cVT i2c_bitbang_vt =
+{
+ .start = i2c_bitbang_start_2,
+ .getc = i2c_bitbang_getc,
+ .putc = i2c_bitbang_putc,
+ .write = i2c_genericWrite,
+ .read = i2c_genericRead,
+};
+
+
+/**
+ * Initialize i2c module.
+ */
+void i2c_hw_bitbangInit(I2c *i2c, int dev)
+{
+ MOD_CHECK(timer);
+ i2c->hw = (struct I2cHardware *)(dev - I2C_BITBANG0);
+ i2c->vt = &i2c_bitbang_vt;
+
+ i2c_bitbang_init(I2C_DEV(i2c));
+ i2c_sdaHi(I2C_DEV(i2c));
+ i2c_sclHi(I2C_DEV(i2c));
+}
+