Die Klasse SerialEEPROM
Bevor wir mit der Implementierung von read
und write
beginnen, soll zunächst ein Konstruktor für die Klasse SerialEEPROM
erstellt werden. Er soll als Parameter die Angabe des angeschlossenen EEPROM-Typs ermöglichen und daraufhin einige Steuerparameter berechnen (Adresse, Größe etc.). Die folgenden Atmel EEPROM-Typen sollen unterstützt werden:
- AT24C02 - 2KBit (256 x 8) 2-wire Serial EEPROM
- AT24C04 - 4KBit (512 x 8) 2-wire Serial EEPROM
- AT24C08 - 8KBit (1024 x 8) 2-wire Serial EEPROM
- AT24C16 - 16KBit (2048 x 8) 2-wire Serial EEPROM
Nachfolgender Quelltext zeigt eine mögliche Implementierung des Konstruktors:
16 | public class SerialEEPROM { |
17 | |
18 | /** Atmel EEPROM types */ |
19 | public static final int TYPE_C02=2; |
20 | public static final int TYPE_C04=4; |
21 | public static final int TYPE_C08=8; |
22 | public static final int TYPE_C16=16; |
23 | |
24 | /** EEPROM chip address */ |
25 | private int address; |
26 | /** EEPROM size */ |
27 | private int size; |
28 | /** EEPROM maximum burst transfer */ |
29 | private int maxburst; |
30 | |
31 | /** Constructs a new SerEPP access object |
32 | * @param type the chip type of the serial EEprom, |
33 | * possible values are defined as constants |
34 | * @param address the address of the chip as defined by |
35 | * its address input pins, on C04, C08 and C16 |
36 | * a number of LSBs is ignored. |
37 | */ |
38 | public SerialEEPROM(int type, int address){ |
39 | this.address = ((16-type) & (address<<1)) + 0xa0; |
40 | this.size = type << 7; |
41 | this.maxburst = (type >= TYPE_C04) ? 16 : 8; |
42 | } |
43 | } |
Listing 1: Auszug aus SerialEEPROM.java
Nachdem nun die I2C-Adresse des EEPROMs (address
), der Speicherplatz (size
) und die Anzahl maximal in Folge übertragbarer Bytes (maxburst
) berechnet wurden, kann die Implementierung der read
- und write
-Methoden beginnen.