refactor logger
[simavr] / examples / parts / i2c_eeprom.h
1 /*
2         i2c_eeprom.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
23 #ifndef __I2C_EEPROM_H___
24 #define __I2C_EEPROM_H___
25
26 #include "sim_irq.h"
27
28 /*
29  * This is a generic i2c eeprom; it can be up to 4096 bytes, and can work
30  * in two modes :
31  * 1) ONE slave address, and either one or two bytes sent on i2c to specify
32  *    the byte to read/write.
33  *    So a transaction looks like:
34  *    <i2c address> [<byte offset MSB>] <byte offset LSB> [<data>]
35  * 2) Multiple slave address to specify the high byte offset value, and one
36  *    byte offset sent
37  *    So a transaction looks like:
38  *    <i2c address; x low bits used as byte offset> <byte offset LSB> [<data>]
39  *
40  * these two modes seem to cover many eeproms
41  */
42 typedef struct i2c_eeprom_t {
43         avr_irq_t *     irq;            // irq list
44         uint8_t addr_base;
45         uint8_t addr_mask;
46         int verbose;
47
48         uint8_t selected;               // selected address
49         int index;      // byte index in current transaction
50
51         uint16_t reg_addr;              // read/write address register
52         int size;                               // also implies the address size, one or two byte
53         uint8_t ee[4096];
54 } i2c_eeprom_t;
55
56 /*
57  * Initializes an eeprom.
58  *
59  * The address is the TWI/i2c address base, for example 0xa0 -- the 7 MSB are
60  * relevant, the bit zero is always meant as the "read write" bit.
61  * The "mask" parameter specifies which bits should be matched as a slave;
62  * if you want to have a peripheral that handle read and write, use '1'; if you
63  * want to also match several addresses on the bus, specify these bits on the
64  * mask too.
65  * Example:
66  * Address 0xa1 mask 0x00 will match address 0xa0 in READ only
67  * Address 0xa0 mask 0x01 will match address 0xa0 in read AND write mode
68  * Address 0xa0 mask 0x03 will match 0xa0 0xa2 in read and write mode
69  *
70  * The "data" is optional, data is initialized as 0xff like a normal eeprom.
71  */
72 void
73 i2c_eeprom_init(
74                 struct avr_t * avr,
75                 i2c_eeprom_t * p,
76                 uint8_t addr,
77                 uint8_t mask,
78                 uint8_t * data,
79                 size_t size);
80
81 /*
82  * Attach the eeprom to the AVR's TWI master code,
83  * pass AVR_IOCTL_TWI_GETIRQ(0) for example as i2c_irq_base
84  */
85 void
86 i2c_eeprom_attach(
87                 struct avr_t * avr,
88                 i2c_eeprom_t * p,
89                 uint32_t i2c_irq_base );
90
91 #endif /* __I2C_EEPROM_H___ */