Add configurable option to choose at compile time which i2c backend to use.
[bertos.git] / bertos / cpu / avr / drv / i2c_avr.c
index 61a5bdea16d64b61f93df4990c79f4643c8a5172..2681bb75cdab39144fa33f1e8bdd00e80eab12cd 100644 (file)
 #include "hw/hw_cpu.h"  /* CLOCK_FREQ */
 
 #include "cfg/cfg_i2c.h"
+
+#define LOG_LEVEL  I2C_LOG_LEVEL
+#define LOG_FORMAT I2C_LOG_FORMAT
+
+#include <cfg/log.h>
+
 #include <cfg/debug.h>
 #include <cfg/macros.h> // BV()
 #include <cfg/module.h>
@@ -61,7 +67,7 @@
  *
  * \return true on success, false otherwise.
  */
-static bool i2c_start(void)
+static bool i2c_builtin_start(void)
 {
        TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN);
        WAIT_TWI_READY;
@@ -69,7 +75,7 @@ static bool i2c_start(void)
        if (TW_STATUS == TW_START || TW_STATUS == TW_REP_START)
                return true;
 
-       kprintf("!TW_(REP)START: %x\n", TWSR);
+       LOG_ERR("!TW_(REP)START: %x\n", TWSR);
        return false;
 }
 
@@ -81,7 +87,7 @@ static bool i2c_start(void)
  *
  * \return true on success, false otherwise.
  */
-bool i2c_start_w(uint8_t id)
+bool i2c_builtin_start_w(uint8_t id)
 {
        /*
         * Loop on the select write sequence: when the eeprom is busy
@@ -90,7 +96,7 @@ bool i2c_start_w(uint8_t id)
         * keep trying until the eeprom responds with an ACK.
         */
        ticks_t start = timer_clock();
-       while (i2c_start())
+       while (i2c_builtin_start())
        {
                TWDR = id & ~I2C_READBIT;
                TWCR = BV(TWINT) | BV(TWEN);
@@ -100,12 +106,12 @@ bool i2c_start_w(uint8_t id)
                        return true;
                else if (TW_STATUS != TW_MT_SLA_NACK)
                {
-                       kprintf("!TW_MT_SLA_(N)ACK: %x\n", TWSR);
+                       LOG_ERR("!TW_MT_SLA_(N)ACK: %x\n", TWSR);
                        break;
                }
                else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
                {
-                       kprintf("Timeout on TWI_MT_START\n");
+                       LOG_ERR("Timeout on TWI_MT_START\n");
                        break;
                }
        }
@@ -121,9 +127,9 @@ bool i2c_start_w(uint8_t id)
  *
  * \return true on success, false otherwise.
  */
-bool i2c_start_r(uint8_t id)
+bool i2c_builtin_start_r(uint8_t id)
 {
-       if (i2c_start())
+       if (i2c_builtin_start())
        {
                TWDR = id | I2C_READBIT;
                TWCR = BV(TWINT) | BV(TWEN);
@@ -132,7 +138,7 @@ bool i2c_start_r(uint8_t id)
                if (TW_STATUS == TW_MR_SLA_ACK)
                        return true;
 
-               kprintf("!TW_MR_SLA_ACK: %x\n", TWSR);
+               LOG_ERR("!TW_MR_SLA_ACK: %x\n", TWSR);
        }
 
        return false;
@@ -142,7 +148,7 @@ bool i2c_start_r(uint8_t id)
 /**
  * Send STOP condition.
  */
-void i2c_stop(void)
+void i2c_builtin_stop(void)
 {
         TWCR = BV(TWINT) | BV(TWEN) | BV(TWSTO);
 }
@@ -154,14 +160,14 @@ void i2c_stop(void)
  *
  * \return true on success, false on error.
  */
-bool i2c_put(const uint8_t data)
+bool i2c_builtin_put(const uint8_t data)
 {
        TWDR = data;
        TWCR = BV(TWINT) | BV(TWEN);
        WAIT_TWI_READY;
        if (TW_STATUS != TW_MT_DATA_ACK)
        {
-               kprintf("!TW_MT_DATA_ACK: %x\n", TWSR);
+               LOG_ERR("!TW_MT_DATA_ACK: %x\n", TWSR);
                return false;
        }
        return true;
@@ -175,7 +181,7 @@ bool i2c_put(const uint8_t data)
  *
  * \return the byte read if ok, EOF on errors.
  */
-int i2c_get(bool ack)
+int i2c_builtin_get(bool ack)
 {
        TWCR = BV(TWINT) | BV(TWEN) | (ack ? BV(TWEA) : 0);
        WAIT_TWI_READY;
@@ -184,7 +190,7 @@ int i2c_get(bool ack)
        {
                if (TW_STATUS != TW_MR_DATA_ACK)
                {
-                       kprintf("!TW_MR_DATA_ACK: %x\n", TWSR);
+                       LOG_ERR("!TW_MR_DATA_ACK: %x\n", TWSR);
                        return EOF;
                }
        }
@@ -192,7 +198,7 @@ int i2c_get(bool ack)
        {
                if (TW_STATUS != TW_MR_DATA_NACK)
                {
-                       kprintf("!TW_MR_DATA_NACK: %x\n", TWSR);
+                       LOG_ERR("!TW_MR_DATA_NACK: %x\n", TWSR);
                        return EOF;
                }
        }
@@ -206,7 +212,7 @@ MOD_DEFINE(i2c);
 /**
  * Initialize TWI module.
  */
-void i2c_init(void)
+void i2c_builtin_init(void)
 {
        ATOMIC(
                /*