-int i2c_write_5(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len);
-int i2c_read_5(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len);
-
-int i2c_write_7(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len,
- const void *buf1, size_t len1);
-int i2c_read_7(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len,
- void *buf1, size_t len1);
-
-int i2c_write_9(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len,
- const void *buf1, size_t len1,
- const void *buf2, size_t len2);
-int i2c_read_9(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len,
- void *buf1, size_t len1,
- void *buf2, size_t len2);
-
-int i2c_write_11(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len,
- const void *buf1, size_t len1,
- const void *buf2, size_t len2
- const void *buf3, size_t len3);
-int i2c_read_11(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len,
- void *buf1, size_t len1,
- void *buf2, size_t len2
- void *buf3, size_t len3);
+void i2c_hw_init(I2c *i2c, int dev, uint32_t clock);
+void i2c_hw_bitbangInit(I2c *i2c, int dev);
+
+void i2c_genericWrite(I2c *i2c, const void *_buf, size_t count);
+void i2c_genericRead(I2c *i2c, void *_buf, size_t count);
+
+INLINE void i2c_start(I2c *i2c, uint16_t slave_addr, size_t size)
+{
+ ASSERT(i2c->vt);
+ ASSERT(i2c->vt->start);
+
+ if (!i2c->errors)
+ ASSERT(i2c->xfer_size == 0);
+
+ i2c->errors = 0;
+ i2c->xfer_size = size;
+
+ i2c->vt->start(i2c, slave_addr);
+}
+
+INLINE void i2c_start_r_4(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
+{
+ ASSERT(i2c);
+ i2c->flags = flags | I2C_START_R;
+ i2c_start(i2c, slave_addr, size);
+}
+
+INLINE void i2c_start_w_4(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
+{
+ ASSERT(i2c);
+ i2c->flags = flags & ~I2C_START_R;
+ i2c_start(i2c, slave_addr, size);
+}
+
+INLINE uint8_t i2c_getc(I2c *i2c)
+{
+ ASSERT(i2c);
+ ASSERT(i2c->vt);
+ ASSERT(i2c->vt->getc);
+
+ ASSERT(i2c->xfer_size);
+
+ ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
+
+ if (!i2c->errors)
+ {
+ uint8_t data = i2c->vt->getc(i2c);
+ i2c->xfer_size--;
+ return data;
+ }
+ else
+ return 0xFF;
+}
+
+INLINE void i2c_putc(I2c *i2c, uint8_t data)
+{
+ ASSERT(i2c);
+ ASSERT(i2c->vt);
+ ASSERT(i2c->vt->putc);
+
+ ASSERT(i2c->xfer_size);
+
+ ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
+
+ if (!i2c->errors)
+ {
+ i2c->vt->putc(i2c, data);
+ i2c->xfer_size--;
+ }
+}
+
+INLINE void i2c_write(I2c *i2c, const void *_buf, size_t count)
+{
+ ASSERT(i2c);
+ ASSERT(i2c->vt);
+ ASSERT(i2c->vt->write);
+
+ ASSERT(_buf);
+ ASSERT(count);
+ ASSERT(count <= i2c->xfer_size);
+
+ ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
+
+ if (!i2c->errors)
+ i2c->vt->write(i2c, _buf, count);
+}
+
+
+INLINE void i2c_read(I2c *i2c, void *_buf, size_t count)
+{
+ ASSERT(i2c);
+ ASSERT(i2c->vt);
+ ASSERT(i2c->vt->read);
+
+ ASSERT(_buf);
+ ASSERT(count);
+ ASSERT(count <= i2c->xfer_size);
+
+ ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
+
+ if (!i2c->errors)
+ i2c->vt->read(i2c, _buf, count);
+}
+
+INLINE int i2c_error(I2c *i2c)
+{
+ ASSERT(i2c);
+ int err = i2c->errors;
+ i2c->errors = 0;
+
+ return err;
+}
+
+#define i2c_init_3(i2c, dev, clock) (dev > I2C_BITBANG0) ? i2c_hw_bitbangInit(i2c, dev) : i2c_hw_init(i2c, dev, clock)