TWI: Work in progress
[simavr] / simavr / sim / avr_twi.h
1 /*
2         avr_twi.h
3
4         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #ifndef AVR_TWI_H_
23 #define AVR_TWI_H_
24
25 #include "sim_avr.h"
26
27 #include "sim_twi.h"
28
29 enum {
30         TWI_IRQ_INPUT = 0,
31         TWI_IRQ_OUTPUT,
32         TWI_IRQ_COUNT
33 };
34
35
36 // add port number to get the real IRQ
37 #define AVR_IOCTL_TWI_GETIRQ(_name) AVR_IOCTL_DEF('t','w','i',(_name))
38 // return a pointer to the slave structure related to this TWI port
39 #define AVR_IOCTL_TWI_GETSLAVE(_name) AVR_IOCTL_DEF('t','w','s',(_name))
40 // retutn this twi interface "master" bus
41 #define AVR_IOCTL_TWI_GETBUS(_name) AVR_IOCTL_DEF('t','w','b',(_name))
42
43 typedef struct avr_twi_t {
44         avr_io_t        io;
45         char name;
46         
47         twi_slave_t     slave;          // when we are a slave, to be attached to some bus
48         twi_bus_t               bus;            // when we are a master, to attach slaves to
49         
50         avr_regbit_t    disabled;       // bit in the PRR
51
52         avr_io_addr_t   r_twbr;                 // bit rate register
53         avr_io_addr_t   r_twcr;                 // control register
54         avr_io_addr_t   r_twsr;                 // status register
55         avr_io_addr_t   r_twar;                 // address register (slave)
56         avr_io_addr_t   r_twamr;                // address mask register
57         avr_io_addr_t   r_twdr;                 // data register
58         
59         avr_regbit_t twen;              // twi enable bit
60         avr_regbit_t twea;              // enable acknowledge bit
61         avr_regbit_t twsta;             // start condition
62         avr_regbit_t twsto;             // stop condition
63         avr_regbit_t twwc;              // write collision
64         
65         avr_regbit_t twsr;              // status registers, (5 bits)
66         avr_regbit_t twps;              // prescaler bits (2 bits)
67         
68         avr_int_vector_t twi;   // spi interrupt
69 } avr_twi_t;
70
71 void avr_twi_init(avr_t * avr, avr_twi_t * port);
72
73 #endif /* AVR_TWI_H_ */