-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEEPROM24.cpp
105 lines (85 loc) · 2.17 KB
/
EEPROM24.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
EEPROM_24X1025.cpp - Library for interfacing 24XX1025 EEPROM chips.
Created by Valerio De Carolis, January, 2013.
Licensed under MIT license, see LICENSE.md
*/
#include "Arduino.h"
#include "EEPROM24.h"
#include <Wire.h>
// TWI clock frequency (400 kHz)
#define TWI_FREQ_FAST 400000L
// default config (address & speed)
#define EEPROM24_ADDR B10100000
#define EEPROM24_FAST false
/**
* Default constructor (using default address and low speed)
*/
EEPROM24::EEPROM24()
{
EEPROM24::EEPROM24(EEPROM24_ADDR, false);
}
/**
* Standard constructor using address and fast flag.
* It initializes the Wire library.
*/
EEPROM24::EEPROM24(byte address, bool fast)
{
_address = address;
_fast = fast;
// init Wire library
Wire.begin();
// change TWI base clock
if(fast) {
TWBR = ((F_CPU / TWI_FREQ_FAST) - 16) / 2;
}
}
/**
* [EEPROM24::read description]
* @param eeprom_addr [description]
* @return [description]
*/
int EEPROM24::read(unsigned int eeprom_addr)
{
byte i2c_status = 0;
int i2c_addr = (int) _address;
int ee_out = 0;
// swap chip (for daisy-chained eeproms)
if( eeprom_addr > 65535 )
i2c_addr = i2c_addr | B00001000;
// seven-bit address
i2c_addr = i2c_addr >> 1;
// i2c commands
Wire.beginTransmission(i2c_addr);
Wire.write( (byte) eeprom_addr >> 8 );
Wire.write( (byte) (eeprom_addr && 0x00FF) );
// check status
i2c_status = Wire.endTransmission();
if( i2c_status == 0 ) {
Wire.requestFrom(i2c_addr, 1);
while( Wire.available() )
ee_out = Wire.read();
return ee_out;
} else {
return -1;
}
}
/**
* [EEPROM24::write description]
* @param eeprom_addr [description]
* @param data [description]
* @return [description]
*/
int EEPROM24::write(unsigned int eeprom_addr, byte data)
{
int i2c_addr = (int) _address;
if( eeprom_addr > 65535 )
i2c_addr = i2c_addr | B00001000;
// seven-bit address
i2c_addr = i2c_addr >> 1;
// i2c commands
Wire.beginTransmission(i2c_addr);
Wire.write( (byte) eeprom_addr >> 8 );
Wire.write( (byte) (eeprom_addr && 0x00FF) );
Wire.write( data );
return Wire.endTransmission();
}