misc: Add extern "C" blocks to headers
[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 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 #include "sim_avr.h"
30
31 //#include "sim_twi.h"
32
33 enum {
34         TWI_IRQ_MISO = 0,
35         TWI_IRQ_MOSI,
36         TWI_IRQ_STATUS,
37         TWI_IRQ_COUNT
38 };
39
40 enum {
41         TWI_COND_START = (1 << 0),
42         TWI_COND_STOP = (1 << 1),
43         TWI_COND_ADDR = (1 << 2),
44         TWI_COND_ACK = (1 << 3),
45         TWI_COND_WRITE = (1 << 4),
46         TWI_COND_READ = (1 << 5),
47 };
48
49 typedef struct avr_twi_msg_t {
50         uint32_t unused : 8,
51                 msg : 8,
52                 addr : 8,
53                 data : 8;
54 } avr_twi_msg_t;
55
56 typedef struct avr_twi_msg_irq_t {
57         union {
58                 uint32_t v;
59                 avr_twi_msg_t twi;
60         } u;
61 } avr_twi_msg_irq_t;
62
63 // add port number to get the real IRQ
64 #define AVR_IOCTL_TWI_GETIRQ(_name) AVR_IOCTL_DEF('t','w','i',(_name))
65
66 typedef struct avr_twi_t {
67         avr_io_t        io;
68         char name;
69
70         avr_regbit_t    disabled;       // bit in the PRR
71
72         avr_io_addr_t   r_twbr;                 // bit rate register
73         avr_io_addr_t   r_twcr;                 // control register
74         avr_io_addr_t   r_twsr;                 // status register
75         avr_io_addr_t   r_twar;                 // address register (slave)
76         avr_io_addr_t   r_twamr;                // address mask register
77         avr_io_addr_t   r_twdr;                 // data register
78         
79         avr_regbit_t twen;              // twi enable bit
80         avr_regbit_t twea;              // enable acknowledge bit
81         avr_regbit_t twsta;             // start condition
82         avr_regbit_t twsto;             // stop condition
83         avr_regbit_t twwc;              // write collision
84         
85         avr_regbit_t twsr;              // status registers, (5 bits)
86         avr_regbit_t twps;              // prescaler bits (2 bits)
87         
88         avr_int_vector_t twi;   // twi interrupt
89
90         uint8_t state;
91         uint8_t peer_addr;
92         uint8_t next_twstate;
93 } avr_twi_t;
94
95 void
96 avr_twi_init(
97                 avr_t * avr,
98                 avr_twi_t * port);
99
100 /*
101  * Create a message value for twi including the 'msg' bitfield,
102  * 'addr' and data. This value is what is sent as the IRQ value
103  */
104 uint32_t
105 avr_twi_irq_msg(
106                 uint8_t msg,
107                 uint8_t addr,
108                 uint8_t data);
109
110 #ifdef __cplusplus
111 };
112 #endif
113
114 #endif /* AVR_TWI_H_ */