forked from collin80/M2RET
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEEPROM.cpp
57 lines (48 loc) · 1.48 KB
/
EEPROM.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
#include <Arduino.h>
#include <due_wire.h>
#include "EEPROM.h"
EEPROMCLASS::EEPROMCLASS(TwoWire *i2cport)
{
port = i2cport;
//port->begin();
}
uint8_t EEPROMCLASS::readByte(uint32_t address)
{
uint8_t d,e;
uint8_t buffer[3];
uint8_t i2c_id;
buffer[0] = ((address & 0xFF00) >> 8);
buffer[1] = ((uint8_t)(address & 0x00FF));
i2c_id = 0b01010000 + ((address >> 16) & 0x03); //10100 is the chip ID then the two upper bits of the address
port->beginTransmission(i2c_id);
port->write(buffer, 2);
port->endTransmission(false); //do NOT generate stop
port->requestFrom(i2c_id, 1); //this will generate stop though.
if(port->available())
{
d = port->read(); // receive a byte as character
return d;
}
return 255;
}
void EEPROMCLASS::writeByte(uint32_t address, uint8_t valu)
{
uint16_t d;
uint8_t buffer[3];
uint8_t i2c_id;
while (writeTime > millis());
buffer[0] = ((address & 0xFF00) >> 8);
buffer[1] = ((uint8_t)(address & 0x00FF));
buffer[2] = valu;
i2c_id = 0b01010000 + ((address >> 16) & 0x03); //10100 is the chip ID then the two upper bits of the address
port->beginTransmission(i2c_id);
port->write(buffer, 3);
port->endTransmission(true);
writeTime = millis() + 8;
}
void EEPROMCLASS::setWPPin(uint8_t pin) {
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
//Instantiate the class with the proper name to pretend this is still the class from the non-Due arduinos
EEPROMCLASS EEPROM(&Wire);