twi: New master implementation
[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_MISO = 0,
31         TWI_IRQ_MOSI,
32         TWI_IRQ_STATUS,
33         TWI_IRQ_COUNT
34 };
35
36 enum {
37         TWI_COND_START = (1 << 0),
38         TWI_COND_STOP = (1 << 1),
39         TWI_COND_ADDR = (1 << 2),
40         TWI_COND_ACK = (1 << 3),
41         TWI_COND_WRITE = (1 << 4),
42         TWI_COND_READ = (1 << 5),
43 };
44
45 typedef struct avr_twi_msg_t {
46         uint32_t unused : 8,
47                 msg : 8,
48                 addr : 8,
49                 data : 8;
50 } avr_twi_msg_t;
51
52 typedef struct avr_twi_msg_irq_t {
53         union {
54                 uint32_t v;
55                 avr_twi_msg_t twi;
56         } u;
57 } avr_twi_msg_irq_t;
58
59 // add port number to get the real IRQ
60 #define AVR_IOCTL_TWI_GETIRQ(_name) AVR_IOCTL_DEF('t','w','i',(_name))
61
62 typedef struct avr_twi_t {
63         avr_io_t        io;
64         char name;
65
66         avr_regbit_t    disabled;       // bit in the PRR
67
68         avr_io_addr_t   r_twbr;                 // bit rate register
69         avr_io_addr_t   r_twcr;                 // control register
70         avr_io_addr_t   r_twsr;                 // status register
71         avr_io_addr_t   r_twar;                 // address register (slave)
72         avr_io_addr_t   r_twamr;                // address mask register
73         avr_io_addr_t   r_twdr;                 // data register
74         
75         avr_regbit_t twen;              // twi enable bit
76         avr_regbit_t twea;              // enable acknowledge bit
77         avr_regbit_t twsta;             // start condition
78         avr_regbit_t twsto;             // stop condition
79         avr_regbit_t twwc;              // write collision
80         
81         avr_regbit_t twsr;              // status registers, (5 bits)
82         avr_regbit_t twps;              // prescaler bits (2 bits)
83         
84         avr_int_vector_t twi;   // twi interrupt
85
86         uint8_t state;
87         uint8_t peer_addr;
88         uint8_t next_twstate;
89 } avr_twi_t;
90
91 void
92 avr_twi_init(
93                 avr_t * avr,
94                 avr_twi_t * port);
95
96 #endif /* AVR_TWI_H_ */