Add support for i2c bitbang.
authorasterix <asterix@38d2e660-2303-0410-9eaa-f027e97ec537>
Fri, 30 Jul 2010 10:28:47 +0000 (10:28 +0000)
committerasterix <asterix@38d2e660-2303-0410-9eaa-f027e97ec537>
Fri, 30 Jul 2010 10:28:47 +0000 (10:28 +0000)
git-svn-id: https://src.develer.com/svnoss/bertos/branches/i2c@4091 38d2e660-2303-0410-9eaa-f027e97ec537

bertos/drv/i2c.h
bertos/drv/i2c_bitbang.c
bertos/hw/hw_i2c_bitbang.h

index c7fa9587f8aa960767a375c059c53b23af41f54d..661c87f29a0b3d19301a966ccb9a10638c274551 100644 (file)
@@ -134,6 +134,25 @@ bool i2c_recv(void *_buf, size_t count);
  * I2c new api
  */
 
+/**
+ * \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
   */
@@ -190,6 +209,7 @@ typedef struct I2c
  * 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);
@@ -303,7 +323,11 @@ INLINE int i2c_error(I2c *i2c)
 
 INLINE void i2c_init_3(I2c *i2c, int dev, uint32_t clock)
 {
-       i2c_hw_init(i2c, dev, clock);
+       if (dev > I2C_BITBANG0)
+               i2c_hw_bitbangInit(i2c, dev);
+       else
+               i2c_hw_init(i2c, dev, clock);
+
 }
 
 #endif
index 45847c93c4e598bc2f0d66106bd110294cdaa2ed..2cf0e573aaee4b918cb3c45f727e895e8e3090d3 100644 (file)
  * \brief I2C bitbang driver (implementation)
  *
  * \author Francesco Sacchi <batt@develer.com>
+ * \author Daniele Basile <asterix@develer.com>
  */
 
-#include "i2c.h"
+#include "hw/hw_i2c_bitbang.h"
+
 #include "cfg/cfg_i2c.h"
 
 #define LOG_LEVEL  I2C_LOG_LEVEL
 #include <cfg/module.h>
 
 #include <drv/timer.h>
+#include <drv/i2c.h>
+
 #include <cpu/irq.h>
 
-#include "hw/hw_i2c_bitbang.h"
+#include <cpu/attr.h>
+
 
 INLINE bool i2c_bitbang_start(void)
 {
@@ -181,106 +186,85 @@ void i2c_bitbang_init(void)
 /*
  * New I2C API
  */
+#define I2C_DEV(i2c)            ((int)((i2c)->hw))
 
-static void i2c_bitbang_stop(struct I2c *i2c)
+static void i2c_bitbang_stop_1(struct I2c *i2c)
 {
-       SDA_LO;
-       SCL_HI;
-       I2C_HALFBIT_DELAY();
-       SDA_HI;
+       i2c_hw_sdaLo(I2C_DEV(i2c));
+       i2c_hw_sclHi(I2C_DEV(i2c));
+       i2c_hw_halfbitDelay(I2C_DEV(i2c));
+       i2c_hw_sdaHi(I2C_DEV(i2c));
 }
 
-
-void i2c_bitbang_start(struct I2c *i2c, uint16_t slave_addr)
+INLINE bool i2c_bitbang_start_1(struct I2c *i2c)
 {
-       if (i2c->flags & I2C_START_R)
-               slave_addr |= I2C_READBIT;
-       else
-               slave_addr &= ~I2C_READBIT;
+       i2c_hw_sdaHi(I2C_DEV(i2c));
+       i2c_hw_sclHi(I2C_DEV(i2c));
+       i2c_hw_halfbitDelay(I2C_DEV(i2c));
+       i2c_hw_sdaLo(I2C_DEV(i2c));
+       i2c_hw_halfbitDelay(I2C_DEV(i2c));
 
-       /*
-        * 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())
-       {
-               if (i2c_bitbang_put(slave_addr))
-                       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(i2c);
-                       return;
-               }
-       }
-
-       LOG_ERR("START arbitration lost\n");
-       i2c->errors |= I2C_ARB_LOST;
-       i2c_bitbang_stop(i2c);
-       return;
+       return !i2c_hw_sdaIn(I2C_DEV(i2c));
 }
 
-uint8_t i2c_bitbang_get(struct I2c *i2c)
+
+static uint8_t i2c_bitbang_getc(struct I2c *i2c)
 {
        uint8_t data = 0;
        for (uint8_t i = 0x80; i != 0; i >>= 1)
        {
-               SCL_LO;
-               I2C_HALFBIT_DELAY();
-               SCL_HI;
-               if (SDA_IN)
+               i2c_hw_sclLo(I2C_DEV(i2c));
+               i2c_hw_halfbitDelay(I2C_DEV(i2c));
+               i2c_hw_sclHi(I2C_DEV(i2c));
+               if (i2c_hw_sdaIn(I2C_DEV(i2c)))
                        data |= i;
                else
                        data &= ~i;
 
-               I2C_HALFBIT_DELAY();
+               i2c_hw_halfbitDelay(I2C_DEV(i2c));
        }
-       SCL_LO;
+       i2c_hw_sclLo(I2C_DEV(i2c));
 
        /* Generate ACK/NACK */
        if (i2c->xfer_size > 1)
-               SDA_LO;
+               i2c_hw_sdaLo(I2C_DEV(i2c));
        else
-               SDA_HI;
+               i2c_hw_sdaHi(I2C_DEV(i2c));
 
-       I2C_HALFBIT_DELAY();
-       SCL_HI;
-       I2C_HALFBIT_DELAY();
-       SCL_LO;
-       SDA_HI;
+       i2c_hw_halfbitDelay(I2C_DEV(i2c));
+       i2c_hw_sclHi(I2C_DEV(i2c));
+       i2c_hw_halfbitDelay(I2C_DEV(i2c));
+       i2c_hw_sclLo(I2C_DEV(i2c));
+       i2c_hw_sdaHi(I2C_DEV(i2c));
 
        /* Generate stop condition (if requested) */
        if ((i2c->xfer_size == 1) && (i2c->flags & I2C_STOP))
-               i2c_bitbang_stop(i2c);
+               i2c_bitbang_stop_1(i2c);
 
        return data;
 }
 
-void i2c_bitbang_put(struct I2c *i2c, uint8_t _data)
+static void i2c_bitbang_putc(struct I2c *i2c, uint8_t _data)
 {
        /* Add ACK bit */
        uint16_t data = (_data << 1) | 1;
 
        for (uint16_t i = 0x100; i != 0; i >>= 1)
        {
-               SCL_LO;
+               i2c_hw_sclLo(I2C_DEV(i2c));
                if (data & i)
-                       SDA_HI;
+                       i2c_hw_sdaHi(I2C_DEV(i2c));
                else
-                       SDA_LO;
-               I2C_HALFBIT_DELAY();
+                       i2c_hw_sdaLo(I2C_DEV(i2c));
+               i2c_hw_halfbitDelay(I2C_DEV(i2c));
 
-               SCL_HI;
-               I2C_HALFBIT_DELAY();
+               i2c_hw_sclHi(I2C_DEV(i2c));
+               i2c_hw_halfbitDelay(I2C_DEV(i2c));
        }
 
-       bool ack = !SDA_IN;
-       SCL_LO;
-       I2C_HALFBIT_DELAY();
+       bool ack = !i2c_hw_sdaIn(I2C_DEV(i2c));
+       i2c_hw_sclLo(I2C_DEV(i2c));
+       i2c_hw_halfbitDelay(I2C_DEV(i2c));
 
        if (!ack)
        {
@@ -290,5 +274,67 @@ void i2c_bitbang_put(struct I2c *i2c, uint8_t _data)
 
        /* Generate stop condition (if requested) */
        if (((i2c->xfer_size == 1) && (i2c->flags & I2C_STOP)) || i2c->errors)
-               i2c_bitbang_stop(i2c);
+               i2c_bitbang_stop_1(i2c);
+}
+
+
+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_hw_bitbang_init(I2C_DEV(i2c));
+       i2c_hw_sdaHi(I2C_DEV(i2c));
+       i2c_hw_sclHi(I2C_DEV(i2c));
+}
+
index 8f3fbf5e88a5771876dd8a9130a20331e52b3329..626cf7d8542cc97434ac6e86d2fb2639af5312e8 100644 (file)
  */
 #define I2C_HALFBIT_DELAY() do { /* Implement me! */ } while (0)
 
+
+/*
+ * New api
+ */
+#include <cfg/compiler.h>
+
+INLINE void i2c_hw_sdaHi(int dev)
+{
+       (void)(dev);
+       /* Implement me:Set SDA High by setting SDA pin as input */
+}
+
+INLINE void i2c_hw_sdaLo(int dev)
+{
+       (void)(dev);
+       /* Implement me:Set SDA Low by setting SDA pin as open collector output */
+}
+
+INLINE void i2c_hw_sclHi(int dev)
+{
+       (void)(dev);
+       /* Implement me:Set SCL High by setting SCL pin as input */
+}
+
+INLINE void i2c_hw_sclLo(int dev)
+{
+       (void)(dev);
+       /* Implement me:Set SCL Low by setting SCL pin as open collector output */
+}
+
+INLINE bool i2c_hw_sdaIn(int dev)
+{
+       (void)(dev);
+       /* Implement me: read SDA pin state */
+       return true;
+}
+
+INLINE bool i2c_hw_sclIn(int dev)
+{
+       (void)(dev);
+       /* Implement me: read SCL pin state */
+       return true;
+}
+
+/**
+ * Half bit delay routine used to generate the correct timings.
+ */
+INLINE void i2c_hw_halfbitDelay(int dev)
+{
+       (void)(dev);
+       /* Implement me! */
+}
+
+/**
+ * This macro should set SDA and SCL lines as input.
+ */
+INLINE void i2c_hw_bitbang_init(int dev)
+{
+       (void)(dev);
+       /* Implement me! */
+}
+
 #endif /* HW_I2C_BITBANG_H */