summaryrefslogtreecommitdiffhomepage
path: root/digital/avr/modules/twi/twi.avr.c
diff options
context:
space:
mode:
Diffstat (limited to 'digital/avr/modules/twi/twi.avr.c')
-rw-r--r--digital/avr/modules/twi/twi.avr.c76
1 files changed, 42 insertions, 34 deletions
diff --git a/digital/avr/modules/twi/twi.avr.c b/digital/avr/modules/twi/twi.avr.c
index 3b2a8e66..3452b36f 100644
--- a/digital/avr/modules/twi/twi.avr.c
+++ b/digital/avr/modules/twi/twi.avr.c
@@ -22,10 +22,18 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* }}} */
-
+#include "common.h"
#include "twi.h"
+#include "io.h"
+
+#if !AC_TWI_MASTER_ENABLE && !AC_TWI_SLAVE_ENABLE
+# error "twi: you should enable at least master or slave code"
+#endif
+
+#if AC_TWI_MASTER_ENABLE && AC_TWI_SLAVE_ENABLE
+# error "twi: multi-master not implemented"
+#endif
-#include <io.h>
#include <compat/twi.h>
// #include "modules/uart/uart.h"
@@ -41,30 +49,30 @@ enum
TWI_FAILURE
};
-#ifdef TWI_SLAVE_ENABLE
+#if AC_TWI_SLAVE_ENABLE
/* données reçues du master */
-static volatile uint8_t rcpt_buf_sl[TWI_SL_RCPT_SIZE];
+static volatile uint8_t rcpt_buf_sl[AC_TWI_SL_RECV_BUFFER_SIZE];
static volatile uint8_t data_ready_sl;
/* données à envoyer au master */
/* 2 buffers 1 pour envoyer vers l'user et l'autre vers l'i2c */
-static volatile uint8_t send_buf_sl1[TWI_SL_SEND_SIZE];
-static volatile uint8_t send_buf_sl2[TWI_SL_SEND_SIZE];
+static volatile uint8_t send_buf_sl1[AC_TWI_SL_SEND_BUFFER_SIZE];
+static volatile uint8_t send_buf_sl2[AC_TWI_SL_SEND_BUFFER_SIZE];
static volatile uint8_t *send_sys_sl, *send_user_sl;
static volatile uint8_t update_sl; /* lock pour savoir si on peut switcher les buffers sans risque */
static volatile uint8_t send_switch_sl;
-#endif /* TWI_SLAVE_ENABLE */
-#ifdef TWI_MASTER_ENABLE
+#endif /* AC_TWI_SLAVE_ENABLE */
+#if AC_TWI_MASTER_ENABLE
static volatile int8_t state_ms;
static volatile int8_t nb_retry_ms = 5; // XXX utile ?
static volatile uint8_t dest_addr_ms;
static volatile uint8_t len_buf_ms;
static volatile uint8_t *send_buf_ms;
-#endif /* TWI_MASTER_ENABLE */
+#endif /* AC_TWI_MASTER_ENABLE */
void
twi_init (uint8_t addr)
{
-#ifdef TWI_SLAVE_ENABLE
+#if AC_TWI_SLAVE_ENABLE
data_ready_sl = 0;
send_switch_sl = 0;
update_sl = 0;
@@ -73,19 +81,19 @@ twi_init (uint8_t addr)
TWAR = addr;
TWSR = 0x00;
TWCR = _BV(TWEA);
-#endif /* TWI_SLAVE_ENABLE */
-#ifdef TWI_MASTER_ENABLE
+#endif /* AC_TWI_SLAVE_ENABLE */
+#if AC_TWI_MASTER_ENABLE
#if defined(TWPS0)
TWSR = 0;
#endif
TWBR = (AC_FREQ / 100000UL - 16) / 2;
state_ms = TWI_FREE;
-#endif /* TWI_MASTER_ENABLE */
+#endif /* AC_TWI_MASTER_ENABLE */
/* Active la twi et les interruptions de la twi */
TWCR |= _BV (TWEN) | _BV (TWIE);
}
-#ifdef TWI_SLAVE_ENABLE
+#if AC_TWI_SLAVE_ENABLE
uint8_t
twi_sl_poll (uint8_t *buffer, uint8_t size)
{
@@ -102,9 +110,9 @@ twi_sl_poll (uint8_t *buffer, uint8_t size)
else
return 0;
}
-#endif /* TWI_SLAVE_ENABLE */
+#endif /* AC_TWI_SLAVE_ENABLE */
-#ifdef TWI_SLAVE_ENABLE
+#if AC_TWI_SLAVE_ENABLE
void
twi_sl_update (uint8_t *buffer, uint8_t size)
{
@@ -115,9 +123,9 @@ twi_sl_update (uint8_t *buffer, uint8_t size)
send_user_sl[size] = buffer[size];
update_sl = 0;
}
-#endif /* TWI_SLAVE_ENABLE */
+#endif /* AC_TWI_SLAVE_ENABLE */
-#ifdef TWI_MASTER_ENABLE
+#if AC_TWI_MASTER_ENABLE
int8_t
twi_ms_is_finished ()
{
@@ -129,9 +137,9 @@ twi_ms_is_finished ()
else
return 0;
}
-#endif /* TWI_MASTER_ENABLE */
+#endif /* AC_TWI_MASTER_ENABLE */
-#ifdef TWI_MASTER_ENABLE
+#if AC_TWI_MASTER_ENABLE
int8_t
twi_ms_send (uint8_t addr, uint8_t *data, uint8_t len)
{
@@ -154,9 +162,9 @@ twi_ms_send (uint8_t addr, uint8_t *data, uint8_t len)
return -1;
}
}
-#endif /* TWI_MASTER_ENABLE */
+#endif /* AC_TWI_MASTER_ENABLE */
-#ifdef TWI_MASTER_ENABLE
+#if AC_TWI_MASTER_ENABLE
int8_t
twi_ms_read (uint8_t addr, uint8_t *data, uint8_t len)
{
@@ -179,21 +187,21 @@ twi_ms_read (uint8_t addr, uint8_t *data, uint8_t len)
return -1;
}
}
-#endif /* TWI_MASTER_ENABLE */
+#endif /* AC_TWI_MASTER_ENABLE */
SIGNAL (SIG_2WIRE_SERIAL)
{
-#ifdef TWI_SLAVE_ENABLE
+#if AC_TWI_SLAVE_ENABLE
static uint8_t send_idx_sl = 0;
static uint8_t rcpt_idx_sl = 0;
-#endif /* TWI_SLAVE_ENABLE */
-#ifdef TWI_MASTER_ENABLE
+#endif /* AC_TWI_SLAVE_ENABLE */
+#if AC_TWI_MASTER_ENABLE
static uint8_t idx_ms;
#define NB_RETRY 5
-#endif /* TWI_MASTER_ENABLE */
+#endif /* AC_TWI_MASTER_ENABLE */
switch (TW_STATUS)
{
-#ifdef TWI_SLAVE_ENABLE
+#if AC_TWI_SLAVE_ENABLE
/***** slave transmitter mode *****/
/* START + SLA|W + ACK
* on a recu un start pour une ecriture et on a acquité
@@ -215,7 +223,7 @@ SIGNAL (SIG_2WIRE_SERIAL)
case TW_ST_DATA_ACK:
//uart0_putc ('p');
TWDR = send_sys_sl[send_idx_sl++];
- if (send_idx_sl == TWI_SL_SEND_SIZE)
+ if (send_idx_sl == AC_TWI_SL_SEND_BUFFER_SIZE)
TWCR &= ~_BV(TWEA);
TWCR |= _BV(TWINT);
break;
@@ -234,7 +242,7 @@ SIGNAL (SIG_2WIRE_SERIAL)
//uart0_putc ('u');
data_ready_sl = 0;
rcpt_idx_sl = 0;
- if (TWI_SL_RCPT_SIZE == 1)
+ if (AC_TWI_SL_RECV_BUFFER_SIZE == 1)
TWCR &= ~_BV(TWEA);
TWCR |= _BV(TWINT);
break;
@@ -243,7 +251,7 @@ SIGNAL (SIG_2WIRE_SERIAL)
case TW_SR_GCALL_DATA_ACK:
//uart0_putc ('s');
rcpt_buf_sl[rcpt_idx_sl++] = TWDR;
- if (TWI_SL_RCPT_SIZE - rcpt_idx_sl == 1)
+ if (AC_TWI_SL_RECV_BUFFER_SIZE - rcpt_idx_sl == 1)
TWCR &= ~_BV(TWEA);
TWCR |= _BV(TWINT);
break;
@@ -259,8 +267,8 @@ SIGNAL (SIG_2WIRE_SERIAL)
data_ready_sl = 1;
TWCR |= _BV(TWINT);
break;
-#endif /* TWI_SLAVE_ENABLE */
-#ifdef TWI_MASTER_ENABLE
+#endif /* AC_TWI_SLAVE_ENABLE */
+#if AC_TWI_MASTER_ENABLE
/* Master */
case TW_START:
case TW_REP_START:
@@ -358,6 +366,6 @@ SIGNAL (SIG_2WIRE_SERIAL)
TWCR |= _BV (TWSTO);
TWCR |= _BV (TWINT);
break;
-#endif/* TWI_MASTER_ENABLE */
+#endif/* AC_TWI_MASTER_ENABLE */
}
}