X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fdrv%2Fi2c.h;h=f527f359d19d375998633260ba90453e65ea600a;hb=ec1f9b136d8b9ec44272ba7ab8ddc4b729589af2;hp=60e40f18d0ff3e9151bd3d46959ad8929d1dd98c;hpb=76ee8a1f5f851680ee237f1a7064b03c9de48aa2;p=bertos.git diff --git a/bertos/drv/i2c.h b/bertos/drv/i2c.h index 60e40f18..f527f359 100644 --- a/bertos/drv/i2c.h +++ b/bertos/drv/i2c.h @@ -44,11 +44,20 @@ #define DRV_I2C_H #include "cfg/cfg_i2c.h" + #include +#include +#include + +#include #define I2C_READBIT BV(0) -#define i2c_init(FN_ARGS) CAT(fn ## _, COUNT_PARMS(FN_ARGS)) (FN_ARGS) +#if COMPILER_C99 + #define i2c_init(...) PP_CAT(i2c_init ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__) +#else + #define i2c_init(args...) PP_CAT(i2c_init ## _, COUNT_PARMS(args)) (args) +#endif /** * I2C Backends. @@ -62,6 +71,7 @@ #define I2C_BACKEND_BUILTIN 0 ///< Uses cpu builtin i2c driver #define I2C_BACKEND_BITBANG 1 ///< Uses emulated bitbang driver +#if 0 /** * I2c builtin prototypes. @@ -113,108 +123,172 @@ int i2c_bitbang_get(bool ack); #error Unsupported i2c backend. #endif + bool i2c_send(const void *_buf, size_t count); bool i2c_recv(void *_buf, size_t count); - - +#endif /* * I2c new api * */ -#include CPU_HEADER(i2c) +#define I2C_OK 0 +#define I2C_ERR BV(3) +#define I2C_ARB_LOST BV(2) +#define I2C_START_TIMEOUT BV(0) +#define I2C_NO_ACK BV(1) + + + +#define I2C_NOSTOP 0 +#define I2C_STOP BV(0) +#define I2C_START_R BV(1) +#define I2C_START_W 0 + + +#define I2C_TEST_START(flag) ((flag) & I2C_START_R) +#define I2C_TEST_STOP(flag) ((flag) & I2C_STOP) struct I2cHardware; -typedef int (*i2c_writeRope_t)(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len, ...); -typedef int (*i2c_readRope_t)(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len, ...); +struct I2c; -typedef struct I2c +typedef void (*i2c_start_t)(struct I2c *i2c, uint16_t slave_addr); +typedef uint8_t (*i2c_get_t)(struct I2c *i2c); +typedef void (*i2c_put_t)(struct I2c *i2c, uint8_t data); +typedef void (*i2c_send_t)(struct I2c *i2c, const void *_buf, size_t count); +typedef void (*i2c_recv_t)(struct I2c *i2c, void *_buf, size_t count); + +typedef struct I2cVT { - int dev; - i2c_writeRope_t write; - i2c_readRope_t read; + i2c_start_t start; + i2c_get_t get; + i2c_put_t put; + i2c_send_t send; + i2c_recv_t recv; +} I2cVT; +typedef struct I2c +{ + int errors; + int flags; + size_t xfer_size; struct I2cHardware* hw; + const struct I2cVT *vt; } I2c; -void i2c_init_3(I2c *i2c, int dev, uint32_t clock) -{ - i2c_hw_init(I2c *i2c, int dev, uint32_t clock); -} -#define i2c_write(FN_ARGS) CAT(fn ## _, COUNT_PARMS(FN_ARGS)) (FN_ARGS) -#define i2c_read(FN_ARGS) CAT(fn ## _, COUNT_PARMS(FN_ARGS)) (FN_ARGS) +#include CPU_HEADER(i2c) -/* - * Overloaded functions definition. - */ -int i2c_write_5(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len) +INLINE void i2c_start(I2c *i2c, uint16_t slave_addr, size_t size) { - return i2c->write(i2c, slave_addr, flags, buf, len, NULL); + 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); } -int i2c_read_5(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len); +INLINE void i2c_start_r(I2c *i2c, uint16_t slave_addr, size_t size, int flags) { - return i2c->read(i2c, slave_addr, flags, buf, len, NULL); + ASSERT(i2c); + i2c->flags = flags | I2C_START_R; + i2c_start(i2c, slave_addr, size); } - -int i2c_write_7(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len, - const void *buf1, size_t len1); +INLINE void i2c_start_w(I2c *i2c, uint16_t slave_addr, size_t size, int flags) { - return i2c->write(i2c, slave_addr, flags, buf, len, - buf1, len1, NULL); + ASSERT(i2c); + i2c->flags = flags & ~I2C_START_R; + i2c_start(i2c, slave_addr, size); } -int i2c_read_7(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len, - void *buf1, size_t len1); - +INLINE uint8_t i2c_get(I2c *i2c) { - return i2c->read(i2c, slave_addr, flags, buf, len, - buf1, len1, NULL); + ASSERT(i2c); + ASSERT(i2c->vt); + ASSERT(i2c->vt->get); + + ASSERT(i2c->xfer_size); + + ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R); + + if (!i2c->errors) + { + uint8_t data = i2c->vt->get(i2c); + i2c->xfer_size--; + return data; + } + else + return 0xFF; } - -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); +INLINE void i2c_put(I2c *i2c, uint8_t data) { - return i2c->write(i2c, slave_addr, flags, buf, len, - buf1, len1, - buf2, len2, NULL); + ASSERT(i2c); + ASSERT(i2c->vt); + ASSERT(i2c->vt->put); + + ASSERT(i2c->xfer_size); + + ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W); + + if (!i2c->errors) + { + i2c->vt->put(i2c, data); + i2c->xfer_size--; + } } -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); +INLINE void i2c_send(I2c *i2c, const void *_buf, size_t count) { - return i2c->read(i2c, slave_addr, flags, buf, len, - buf1, len1, - buf2, len2, NULL); + ASSERT(i2c); + ASSERT(i2c->vt); + ASSERT(i2c->vt->send); + + ASSERT(_buf); + ASSERT(count); + ASSERT(count <= i2c->xfer_size); + + ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W); + + if (!i2c->errors) + i2c->vt->send(i2c, _buf, count); } -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); + +INLINE void i2c_recv(I2c *i2c, void *_buf, size_t count) { - return i2c->write(i2c, slave_addr, flags, buf, len, - buf1, len1, - buf2, len2, - buf3, len3, NULL); + ASSERT(i2c); + ASSERT(i2c->vt); + ASSERT(i2c->vt->recv); + + ASSERT(_buf); + ASSERT(count); + ASSERT(count <= i2c->xfer_size); + + ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R); + + if (!i2c->errors) + i2c->vt->recv(i2c, _buf, count); } -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); +INLINE int i2c_error(I2c *i2c) +{ + ASSERT(i2c); + int err = i2c->errors; + i2c->errors = 0; + return err; +} +INLINE void i2c_init_3(I2c *i2c, int dev, uint32_t clock) { - return i2c->read(i2c, slave_addr, flags, buf, len, - buf1, len1, - buf2, len2, - buf3, len3, NULL); + i2c_hw_init(i2c, dev, clock); } #endif