summaryrefslogtreecommitdiff
path: root/n/avr/twi-slave/twi_slave.c
blob: f95c4c452cf6d4ea7e0ea6601cacac12aea0d46f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* twi.c */
/* n.avr.twi - AVR TWI Module. {{{
 *
 * Copyright (C) 2004 Cl�ment Demonchy
 *
 * Robot APB Team/Efrei 2005.
 *        Web: http://assos.efrei.fr/robot/
 *      Email: robot AT efrei DOT fr
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "twi_slave.h"
#include "n/avr/rs232/rs232.h"

#include <avr/io.h>
#include <avr/twi.h>
#include <avr/signal.h>

//TODO acces exclusif au buffer

static volatile uint8_t twi_rcpt_tail;
static volatile uint8_t send_idx;
static volatile uint8_t data_ready;
static volatile uint8_t buf_rcpt[SIZE_BUF_RCPT];
static volatile uint8_t buf_send[SIZE_BUF_SEND];

void
twi_init (uint8_t addr)
{
    TWAR = addr;
    TWSR = 0x00;
    TWCR = _BV(TWEA) | _BV(TWEN) | _BV(TWIE);
}


SIGNAL (SIG_2WIRE_SERIAL)
{
    rs232_putc (';');
    switch (TW_STATUS)
      {
      /* slave transmitter mode */
      /* START + SLA|W + ACK */
      case TW_ST_SLA_ACK:
      case TW_ST_ARB_LOST_SLA_ACK:
	rs232_putc ('a');
	send_idx = 0;
	/* no break */
      case TW_ST_DATA_ACK:
	rs232_putc ('b');
	TWDR = buf_send[send_idx++];
	if (SIZE_BUF_SEND == send_idx)
	    TWCR &= ~_BV(TWEA);
	break;
      case TW_ST_DATA_NACK:
      case TW_ST_LAST_DATA:
	rs232_putc ('c');
	TWCR |= _BV (TWEA);
	break;
      /* slave receiver mode */
      /* START + SLA|W + ACK */
      case TW_SR_SLA_ACK:
      case TW_SR_ARB_LOST_SLA_ACK:
      case TW_SR_GCALL_ACK:
      case TW_SR_ARB_LOST_GCALL_ACK:
	rs232_putc ('z');
	data_ready = 0;
	twi_rcpt_tail = 0;
	if (SIZE_BUF_RCPT == 1)
	    TWCR &= ~_BV(TWEA);
	break;
      /* DATA + ACK */
      case TW_SR_DATA_ACK:
      case TW_SR_GCALL_DATA_ACK:
	rs232_putc ('y');
	buf_rcpt[twi_rcpt_tail++] = TWDR;
	if (SIZE_BUF_RCPT - twi_rcpt_tail == 1)
	    TWCR &= ~_BV(TWEA);
	break;
      /* DATA + NACK */
      case TW_SR_DATA_NACK:
      case TW_SR_GCALL_DATA_NACK:
	rs232_putc ('x');
	buf_rcpt[twi_rcpt_tail++] =  TWDR;
	/* no break */
      /* STOP */
      case TW_SR_STOP:
	rs232_putc ('w');
	data_ready = 1;
	break;
      }
    TWCR |= _BV(TWINT);
}

/** Recopie dans les donn�es recues dans buf.
 */
uint8_t
twi_poll (uint8_t buf[], uint8_t size)
{
    if (data_ready)
      {
	data_ready = 0;
	while (size --)
	    buf[size] = buf_rcpt[size];
	TWCR |= _BV (TWEA);
	return 1;
      }
    else
	return 0; 
}

void
twi_update (uint8_t buf[], uint8_t size)
{
    while (size --)
	buf_send[size] = buf[size];
}