X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/blobdiff_plain/61b0cfe9df810db4fbca78e5f880d61c5063f324..54678798da1da8c4b53a9e201a098ef284b8498e:/avr/i2c.c?ds=sidebyside diff --git a/avr/i2c.c b/avr/i2c.c index df97fea..dbb5da1 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 */ @@ -99,12 +101,17 @@ uint8_t buf[CONFIG_SYS_I2C_BUFSIZE]; } i2c_msg_t; -static i2c_msg_t xmit; +static volatile i2c_msg_t xmit; ISR(TWI_vect) { - uint8_t next_twcr; + uint8_t tmp_stat; uint8_t tmp_idx; + uint8_t next_twcr; + uint8_t n; + + tmp_idx = xmit.idx; + tmp_stat = xmit.stat; uint8_t twsr = TWSR; @@ -112,98 +119,78 @@ ISR(TWI_vect) case TWI_START: case TWI_REP_START: - xmit.idx = 0; /* reset xmit_buf index */ - xmit.stat = BUSY | START; + tmp_stat = BUSY | START; + tmp_idx = 0; /* reset xmit_buf index */ - tmp_idx = xmit.idx; if (tmp_idx < xmit.len) { /* all bytes transmited? */ TWDR = xmit.buf[tmp_idx]; - xmit.idx = ++tmp_idx; + ++tmp_idx; next_twcr = (1< 255) { - tmptwbr >>= 4; + while (tmp_twbr > 255) { + tmp_twbr >>= 4; twps += 1; } - debug_cond((twps > 3), "TWCLK too low: %lu Hz\n", speed); + debug_cond((twps > 3), "*** TWCLK too low: %lu Hz\n", speed); - twbr = (uint8_t) tmptwbr; + twbr = (uint8_t) tmp_twbr; _init(); } @@ -261,14 +249,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; @@ -288,7 +276,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 @@ -300,7 +288,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; @@ -313,7 +301,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 @@ -321,7 +309,7 @@ int i2c_recv(uint8_t chip, uint8_t *buffer, int8_t len) TWCR = (1<