Move cm3 type to generic types header.
[bertos.git] / bertos / drv / i2c.h
index 361bd49d6beb1a5664013e1c5061aa1000350a94..268e2451e89c9b1d0c2110d38821ae6b80f9f445 100644 (file)
 
 #include "cfg/cfg_i2c.h"
 
+#define LOG_LEVEL  I2C_LOG_LEVEL
+#define LOG_FORMAT I2C_LOG_FORMAT
+
+#include <cfg/log.h>
+
 #include <cfg/compiler.h>
 #include <cfg/macros.h>
 #include <cfg/debug.h>
@@ -131,21 +136,28 @@ bool i2c_recv(void *_buf, size_t count);
 
 /*
  * I2c new api
- *
  */
-#define I2C_OK               0
-#define I2C_START_ERR     BV(0)
-#define I2C_NO_ACK        BV(1)
-#define I2C_ARBLST        BV(2)
 
+ /*
+  * I2C error flags
+  */
+#define I2C_OK               0     ///< I2C no errors flag
+#define I2C_ERR           BV(3)    ///< I2C generic error
+#define I2C_ARB_LOST      BV(2)    ///< I2C arbitration lost error
+#define I2C_START_TIMEOUT BV(0)    ///< I2C timeout error on start
+#define I2C_NO_ACK        BV(1)    ///< I2C no ack for sla start
 
-#define I2C_NOSTOP           0
-#define I2C_STOP          BV(0)
-#define I2C_START_R       BV(1)
-#define I2C_START_W          0
+/*
+ * I2C command flags
+ */
+#define I2C_NOSTOP           0    ///< Do not program the stop for current transition
+#define I2C_STOP          BV(0)   ///< Program the stop for current transition
+#define I2C_START_R       BV(1)   ///< Start read command
+#define I2C_START_W          0    ///< Start write command
 
 
 #define I2C_TEST_START(flag)  ((flag) & I2C_START_R)
+#define I2C_TEST_STOP(flag)   ((flag) & I2C_STOP)
 
 struct I2cHardware;
 struct I2c;
@@ -177,11 +189,16 @@ typedef struct I2c
 
 #include CPU_HEADER(i2c)
 
+void i2c_swSend(struct I2c *i2c, const void *_buf, size_t count);
+void i2c_swRecv(struct 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);
-       ASSERT(i2c->xfer_size == 0);
+
+       if (!i2c->errors)
+               ASSERT(i2c->xfer_size == 0);
 
        i2c->errors = 0;
        i2c->xfer_size = size;
@@ -209,12 +226,16 @@ INLINE uint8_t i2c_get(I2c *i2c)
        ASSERT(i2c->vt);
        ASSERT(i2c->vt->get);
 
-       ASSERT(i2c->xfer_size >=  1);
+       ASSERT(i2c->xfer_size);
 
        ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
 
        if (!i2c->errors)
-               return i2c->vt->get(i2c);
+       {
+               uint8_t data = i2c->vt->get(i2c);
+               i2c->xfer_size--;
+               return data;
+       }
        else
                return 0xFF;
 }
@@ -225,12 +246,15 @@ INLINE void i2c_put(I2c *i2c, uint8_t data)
        ASSERT(i2c->vt);
        ASSERT(i2c->vt->put);
 
-       ASSERT(i2c->xfer_size >=  1);
+       ASSERT(i2c->xfer_size);
 
        ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
 
        if (!i2c->errors)
+       {
                i2c->vt->put(i2c, data);
+               i2c->xfer_size--;
+       }
 }
 
 INLINE void i2c_send(I2c *i2c, const void *_buf, size_t count)
@@ -271,6 +295,12 @@ INLINE int i2c_error(I2c *i2c)
        ASSERT(i2c);
        int err = i2c->errors;
        i2c->errors = 0;
+
+       LOG_ERRB(
+               if (err)
+                       LOG_ERR("err[%02x]\n", err);
+       );
+
        return err;
 }