+/** \defgroup i2c_driver I2C driver
+ */
+
+/*
+ * The following macros are needed to maintain compatibility with older i2c API.
+ * They can be safely removed once the old API is removed.
+ */
+
+ /**
+ * \addtogroup i2c_api
+ * \{
+ */
+#if COMPILER_C99
+ #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
+ /**
+ * Initialize I2C module.
+ *
+ * To initialize the module you can write this code:
+ * \code
+ * I2c ctx;
+ * i2c_init(&ctx, 0, CONFIG_I2C_FREQ);
+ * \endcode
+ * This macro expands in two versions, depending on the number of
+ * parameters, to maintain compatibility with old API:
+ * \li i2c_init_3(I2c *i2c, int dev, uint32_t clock)
+ * \li i2c_init_0(void)
+ *
+ * Do NOT use the above functions directly, use i2c_init().
+ * \note Use the version with 3 parameters, the other one is only for
+ * legacy code.
+ */
+ #define i2c_init(args...) PP_CAT(i2c_init ## _, COUNT_PARMS(args)) (args)
+
+ /**
+ * Start a write session.
+ *
+ * To start a write session, use the following code:
+ * \code
+ * i2c_start_w(i2c, dev, bytes, I2C_STOP);
+ * \endcode
+ *
+ * This macro expands in two versions, depending on the number of parameters:
+ * \li i2c_start_w_4(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
+ * \li i2c_builtin_start_w(uint8_t id): Deprecated API, don't use in new projects
+ * \li i2c_bitbang_start_w(uint8_t id): Deprecated API, don't use in new projects
+ *
+ * Do NOT use the above functions directly, use i2c_start_w().
+ * \note Use the version with 4 parameters, the others are only for legacy code
+ */
+ #define i2c_start_w(args...) PP_CAT(i2c_start_w ## _, COUNT_PARMS(args)) (args)
+
+ /**
+ * Start a read session.
+ *
+ * To start a read session, use the following code:
+ * \code
+ * i2c_start_r(i2c, dev, bytes, I2C_STOP);
+ * \endcode
+ *
+ * This macro expands in two versions, depending on the number of parameters:
+ * \li i2c_start_r_4(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
+ * \li i2c_builtin_start_r(uint8_t id): Deprecated API, don't use in new projects
+ * \li i2c_bitbang_start_r(uint8_t id): Deprecated API, don't use in new projects
+ *
+ * Do NOT use the above functions directly, use i2c_start_r().
+ * \note Use the version with 4 parameters, the others are only for legacy code
+ */
+ #define i2c_start_r(args...) PP_CAT(i2c_start_r ## _, COUNT_PARMS(args)) (args)
+#endif
+/**\}*/
+
+
+/**
+ * \name I2C bitbang devices enum
+ */
+enum
+{
+ I2C_BITBANG_OLD = -1,
+ I2C_BITBANG0 = 1000, ///< Use bitbang on port 0
+ I2C_BITBANG1, ///< Use bitbang on port 1
+ I2C_BITBANG2,
+ I2C_BITBANG3,
+ I2C_BITBANG4,
+ I2C_BITBANG5,
+ I2C_BITBANG6,
+ I2C_BITBANG7,
+ I2C_BITBANG8,
+ I2C_BITBANG9,
+
+ I2C_BITBANG_CNT /**< Number of i2c ports */
+};
+
+/** \defgroup i2c_api I2C driver API
+ * \ingroup i2c_driver
+ * \{
+ */
+
+/**
+ * \name I2C error flags
+ * \ingroup i2c_api
+ * @{
+ */
+#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
+/**@}*/
+
+/**
+ * \name I2C command flags
+ * \ingroup i2c_api
+ * @{
+ */
+#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;
+
+typedef void (*i2c_start_t)(struct I2c *i2c, uint16_t slave_addr);
+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_getc_t getc;
+ i2c_putc_t putc;
+ i2c_write_t write;
+ i2c_read_t read;
+} I2cVT;
+
+typedef struct I2c
+{
+ int errors;
+ int flags;
+ size_t xfer_size;
+ struct I2cHardware* hw;
+ const struct I2cVT *vt;
+} I2c;
+
+
+#include CPU_HEADER(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);
+
+/*
+ * Start a i2c transfer.
+ *
+ * \param i2c Context structure.
+ * \param slave_addr Address of slave device
+ * \param size Size of the transfer
+ */
+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);
+}
+
+/**
+ * \name I2C interface functions
+ * \ingroup i2c_api
+ * @{
+ */
+
+/**
+ * Start a read session.
+ * \param i2c I2C context
+ * \param slave_addr Address of the slave device
+ * \param size Number of bytes to be read from device
+ * \param flags Session flags (I2C command 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);
+}
+