Typo.
[bertos.git] / bertos / drv / i2c.h
index 75fb54a51ed1b2e7e1cec744c3afdc10cb9b8dbe..d934497bc684d470df6d0f572425c30d55130210 100644 (file)
@@ -37,6 +37,7 @@
  * $WIZ$ module_name = "i2c"
  * $WIZ$ module_configuration = "bertos/cfg/cfg_i2c.h"
  * $WIZ$ module_hw = "bertos/hw/hw_i2c_bitbang.h"
+ * $WIZ$ module_depends = "i2c_bitbang"
  * $WIZ$ module_supports = "not atmega103 and not atmega168 and not at91"
  */
 
 
 #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>
 #define I2C_READBIT BV(0)
 
 #if COMPILER_C99
-       #define i2c_init(...)     PP_CAT(i2c_init ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
+       #define i2c_init(...)           PP_CAT(i2c_init ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
+       #define i2c_start_w(...)        PP_CAT(i2c_start_w ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
+       #define i2c_start_r(...)        PP_CAT(i2c_start_r ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
 #else
-       #define i2c_init(args...)     PP_CAT(i2c_init ## _, COUNT_PARMS(args)) (args)
+       #define i2c_init(args...)       PP_CAT(i2c_init ## _, COUNT_PARMS(args)) (args)
+       #define i2c_start_w(args...)    PP_CAT(i2c_start_w ## _, COUNT_PARMS(args)) (args)
+       #define i2c_start_r(args...)    PP_CAT(i2c_start_r ## _, COUNT_PARMS(args)) (args)
 #endif
 
+
+#if !CONFIG_I2C_DISABLE_OLD_API
 /**
  * I2C Backends.
  * Sometimes your cpu does not have a builtin
  * i2c driver or you don't want, for some reason, to
  * use that.
  * With this you can choose, at compile time, which backend to use.
- *
- * $WIZ$ i2c_backend = "I2C_BACKEND_BUILTIN", "I2C_BACKEND_BITBANG"
  */
 #define I2C_BACKEND_BUILTIN 0 ///< Uses cpu builtin i2c driver
 #define I2C_BACKEND_BITBANG 1 ///< Uses emulated bitbang driver
 
-#if 0
-
 /**
  * I2c builtin prototypes.
  * Do NOT use these function directly, instead,
@@ -110,20 +108,24 @@ bool i2c_bitbang_put(uint8_t _data);
 int i2c_bitbang_get(bool ack);
 /*\}*/
 
+#ifndef CONFIG_I2C_BACKEND
+#define  CONFIG_I2C_BACKEND  I2C_BACKEND_BUILTIN
+#endif
+
 #if CONFIG_I2C_BACKEND == I2C_BACKEND_BUILTIN
-       #define i2c_init_0    i2c_builtin_init
-       #define i2c_start_w   i2c_builtin_start_w
-       #define i2c_start_r   i2c_builtin_start_r
-       #define i2c_stop      i2c_builtin_stop
-       #define i2c_put       i2c_builtin_put
-       #define i2c_get       i2c_builtin_get
+       #define i2c_init_0      i2c_builtin_init
+       #define i2c_start_w_1   i2c_builtin_start_w
+       #define i2c_start_r_1   i2c_builtin_start_r
+       #define i2c_stop        i2c_builtin_stop
+       #define i2c_put         i2c_builtin_put
+       #define i2c_get         i2c_builtin_get
 #elif CONFIG_I2C_BACKEND == I2C_BACKEND_BITBANG
-       #define i2c_init_0    i2c_bitbang_init
-       #define i2c_start_w   i2c_bitbang_start_w
-       #define i2c_start_r   i2c_bitbang_start_r
-       #define i2c_stop      i2c_bitbang_stop
-       #define i2c_put       i2c_bitbang_put
-       #define i2c_get       i2c_bitbang_get
+       #define i2c_init_0      i2c_bitbang_init
+       #define i2c_start_w_1   i2c_bitbang_start_w
+       #define i2c_start_r_1   i2c_bitbang_start_r
+       #define i2c_stop        i2c_bitbang_stop
+       #define i2c_put         i2c_bitbang_put
+       #define i2c_get         i2c_bitbang_get
 #else
        #error Unsupported i2c backend.
 #endif
@@ -136,20 +138,44 @@ bool i2c_recv(void *_buf, size_t count);
 
 /*
  * I2c new api
- *
  */
-#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)
-
 
+/**
+ * \name I2C bitbang devices enum
+ */
+enum
+{
+       I2C_BITBANG0 = 1000,
+       I2C_BITBANG1,
+       I2C_BITBANG2,
+       I2C_BITBANG3,
+       I2C_BITBANG4,
+       I2C_BITBANG5,
+       I2C_BITBANG6,
+       I2C_BITBANG7,
+       I2C_BITBANG8,
+       I2C_BITBANG9,
+
+       I2C_BITBANG_CNT  /**< Number of serial ports */
+};
+
+ /*
+  * I2C error flags
+  */
+#define I2C_OK               0     ///< I2C no errors flag
+#define I2C_DATA_NACK     BV(4)    ///< I2C generic error
+#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)
@@ -159,18 +185,18 @@ struct I2cHardware;
 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 uint8_t (*i2c_getc_t)(struct I2c *i2c);
+typedef void (*i2c_putc_t)(struct I2c *i2c, uint8_t data);
+typedef void (*i2c_write_t)(struct I2c *i2c, const void *_buf, size_t count);
+typedef void (*i2c_read_t)(struct I2c *i2c, void *_buf, size_t count);
 
 typedef struct I2cVT
 {
        i2c_start_t start;
-       i2c_get_t   get;
-       i2c_put_t   put;
-       i2c_send_t  send;
-       i2c_recv_t  recv;
+       i2c_getc_t   getc;
+       i2c_putc_t   putc;
+       i2c_write_t  write;
+       i2c_read_t   read;
 } I2cVT;
 
 typedef struct I2c
@@ -185,8 +211,14 @@ 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);
+/*
+ * Low level i2c  init implementation prototype.
+ */
+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)
 {
@@ -202,25 +234,25 @@ INLINE void i2c_start(I2c *i2c, uint16_t slave_addr, size_t size)
        i2c->vt->start(i2c, slave_addr);
 }
 
-INLINE void i2c_start_r(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
+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(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
+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_get(I2c *i2c)
+INLINE uint8_t i2c_getc(I2c *i2c)
 {
        ASSERT(i2c);
        ASSERT(i2c->vt);
-       ASSERT(i2c->vt->get);
+       ASSERT(i2c->vt->getc);
 
        ASSERT(i2c->xfer_size);
 
@@ -228,7 +260,7 @@ INLINE uint8_t i2c_get(I2c *i2c)
 
        if (!i2c->errors)
        {
-               uint8_t data = i2c->vt->get(i2c);
+               uint8_t data = i2c->vt->getc(i2c);
                i2c->xfer_size--;
                return data;
        }
@@ -236,11 +268,11 @@ INLINE uint8_t i2c_get(I2c *i2c)
                return 0xFF;
 }
 
-INLINE void i2c_put(I2c *i2c, uint8_t data)
+INLINE void i2c_putc(I2c *i2c, uint8_t data)
 {
        ASSERT(i2c);
        ASSERT(i2c->vt);
-       ASSERT(i2c->vt->put);
+       ASSERT(i2c->vt->putc);
 
        ASSERT(i2c->xfer_size);
 
@@ -248,16 +280,16 @@ INLINE void i2c_put(I2c *i2c, uint8_t data)
 
        if (!i2c->errors)
        {
-               i2c->vt->put(i2c, data);
+               i2c->vt->putc(i2c, data);
                i2c->xfer_size--;
        }
 }
 
-INLINE void i2c_send(I2c *i2c, const void *_buf, size_t count)
+INLINE void i2c_write(I2c *i2c, const void *_buf, size_t count)
 {
        ASSERT(i2c);
        ASSERT(i2c->vt);
-       ASSERT(i2c->vt->send);
+       ASSERT(i2c->vt->write);
 
        ASSERT(_buf);
        ASSERT(count);
@@ -266,15 +298,15 @@ INLINE void i2c_send(I2c *i2c, const void *_buf, size_t count)
        ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
 
        if (!i2c->errors)
-               i2c->vt->send(i2c, _buf, count);
+               i2c->vt->write(i2c, _buf, count);
 }
 
 
-INLINE void i2c_recv(I2c *i2c, void *_buf, size_t count)
+INLINE void i2c_read(I2c *i2c, void *_buf, size_t count)
 {
        ASSERT(i2c);
        ASSERT(i2c->vt);
-       ASSERT(i2c->vt->recv);
+       ASSERT(i2c->vt->read);
 
        ASSERT(_buf);
        ASSERT(count);
@@ -283,7 +315,7 @@ INLINE void i2c_recv(I2c *i2c, void *_buf, size_t count)
        ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
 
        if (!i2c->errors)
-               i2c->vt->recv(i2c, _buf, count);
+               i2c->vt->read(i2c, _buf, count);
 }
 
 INLINE int i2c_error(I2c *i2c)
@@ -292,17 +324,10 @@ INLINE int i2c_error(I2c *i2c)
        int err = i2c->errors;
        i2c->errors = 0;
 
-       LOG_ERRB(
-               if (err)
-                       LOG_ERR("err[%02x]\n", err);
-       );
-
        return err;
 }
 
-INLINE void i2c_init_3(I2c *i2c, int dev, uint32_t clock)
-{
-       i2c_hw_init(i2c, dev, clock);
-}
+#define i2c_init_3(i2c, dev, clock)   (dev > I2C_BITBANG0) ?  i2c_hw_bitbangInit(i2c, dev) : i2c_hw_init(i2c, dev, clock)
+
 
 #endif