summaryrefslogtreecommitdiff
path: root/avr/i2c.c
diff options
context:
space:
mode:
Diffstat (limited to 'avr/i2c.c')
-rw-r--r--avr/i2c.c32
1 files changed, 13 insertions, 19 deletions
diff --git a/avr/i2c.c b/avr/i2c.c
index 3ab222a..5d414af 100644
--- a/avr/i2c.c
+++ b/avr/i2c.c
@@ -13,9 +13,11 @@
#include "debug.h"
#include "i2c.h"
-#ifdef DEBUG
-//# define DEBUG_I2C
-#endif
+#define DEBUG_I2C 0
+
+#define debug_i2c(fmt, args...) \
+ debug_cond(DEBUG_I2C, fmt, ##args)
+
/* General TWI Master status codes */
#define TWI_START 0x08 /* START has been transmitted */
@@ -205,9 +207,7 @@ ISR(TWI_vect)
xmit.stat = tmp_stat;
xmit.idx = tmp_idx;
-#ifdef DEBUG_I2C
- debug("|%02x", twsr);
-#endif
+ debug_i2c("|%02x", twsr);
TWCR = next_twcr;
}
@@ -226,7 +226,7 @@ static void _init(void)
/* (Reset TWI hardware state machine.) */
TWCR = TWI_C_DISABLE;
_delay_us(5);
-#ifdef DEBUG_I2C
+#if DEBUG_I2C
memset((void *) xmit.buf, 0xdf, sizeof(xmit.buf));
#endif
@@ -248,10 +248,6 @@ void i2c_init(uint32_t speed)
debug_cond((twps > 3), "*** TWCLK too low: %lu Hz\n", speed);
twbr = (uint8_t) tmp_twbr;
-
- debug("*** i2c_init: i2c_speed: %lu, twbr: %u, twps: %u\n",
- speed, twbr, twps);
-
_init();
}
@@ -270,14 +266,14 @@ int_fast8_t i2c_waitready(void)
xmit.stat |= timeout;
-#ifdef DEBUG_I2C
+#if DEBUG_I2C
dump_ram((uint8_t *) &xmit, 4, "=== i2c_wait ready: (done)");
_delay_ms(30);
#endif
return xmit.stat;
}
-//static
+static
int i2c_send(uint8_t chip, uint16_t addr, uint8_t alen, uint8_t *buffer, int8_t len)
{
uint8_t i, n;
@@ -297,7 +293,7 @@ int i2c_send(uint8_t chip, uint16_t addr, uint8_t alen, uint8_t *buffer, int8_t
xmit.buf[i] = *buffer++;
xmit.len = i;
-#ifdef DEBUG_I2C
+#if DEBUG_I2C
dump_ram((uint8_t *) &xmit, 0x20, "=== i2c_send");
_delay_ms(30);
#endif
@@ -309,7 +305,7 @@ int i2c_send(uint8_t chip, uint16_t addr, uint8_t alen, uint8_t *buffer, int8_t
return rc;
}
-//static
+static
int i2c_recv(uint8_t chip, uint8_t *buffer, int8_t len)
{
uint8_t rc;
@@ -322,7 +318,7 @@ int i2c_recv(uint8_t chip, uint8_t *buffer, int8_t len)
xmit.len = len + 1;
xmit.buf[0] = (chip<<1) | 1;
-#ifdef DEBUG_I2C
+#if DEBUG_I2C
dump_ram((uint8_t *) &xmit, 0x20, "=== i2c_recv: before start");
_delay_ms(30);
#endif
@@ -330,7 +326,7 @@ int i2c_recv(uint8_t chip, uint8_t *buffer, int8_t len)
TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWSTA);
rc = i2c_waitready();
-#ifdef DEBUG_I2C
+#if DEBUG_I2C
dump_ram((uint8_t *) &xmit, 0x20, "=== i2c_recv: after completion");
_delay_ms(30);
#endif
@@ -369,7 +365,6 @@ int i2c_write(uint8_t chip, unsigned int addr, uint_fast8_t alen,
i2c_send(chip, addr, alen, buffer, len);
rc = i2c_waitready();
- debug("** i2c_write: result=0x%02x\n",rc);
return (rc & XMIT_DONE) != 0;
}
@@ -389,7 +384,6 @@ int i2c_read(uint8_t chip, unsigned int addr, uint_fast8_t alen,
i2c_send(chip, addr, alen, NULL, 0);
}
rc = i2c_recv(chip, buffer, len);
- debug("** i2c_read: result=0x%02x\n",rc);
return !((rc & (XMIT_DONE|DATA_ACK)) == (XMIT_DONE|DATA_ACK));
}