-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZEEPROM.cpp
73 lines (66 loc) · 2.09 KB
/
ZEEPROM.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
/*I2C driver for ZD24C128A 128kByte EEPROM
*/
#include <Arduino.h>
#include <Wire.h>
#include "ZEEPROM.h"
ZEEPROM::ZettaEEPROM()
{
}
void ZEEPROM::ZEEPROM(HardwareSlaveAddressBits address)
{
if (address >= HardwareSlaveAddressBits::A000 && address <= HardwareSlaveAddressBits::A111)
_address = static_cast<int>(address);
else
_address = static_cast<int>(HardwareSlaveAddressBits::A000);
_setDeviceTypeIdentifier(DeviceTypeIdentifier::MEMORY_ARRAY);
}
void ZEEPROM::_setDeviceTypeIdentifier(DeviceTypeIdentifier deviceTypeIdentifier)
{
if (deviceTypeIdentifier == DeviceTypeIdentifier::MEMORY_ARRAY || deviceTypeIdentifier == DeviceTypeIdentifier::IDENTIFICATION_PAGE)
_deviceTypeIdentifier = static_cast<int>(deviceTypeIdentifier);
else
_deviceTypeIdentifier = static_cast<int>(DeviceTypeIdentifier::MEMORY_ARRAY);
}
uint8_t ZEEPROM::_getHeader(READ_WRITE readWrite)
{
if (readWrite != READ_WRITE::READ && readWrite != READ_WRITE::WRITE)
readWrite = READ_WRITE::READ;
return (_deviceTypeIdentifier << 4) | (_address << 1) | static_cast<uint8_t>(readWrite);
}
uint16_t ZEEPROM::_getAddressBytes(uint16_t address)
{
address &= 0b0011111111111111; // 0x3FFF or 16383
return address;
}
bool ZEEPROM::writeByte(uint16_t address, uint8_t data)
{
uint8_t status = 1;
Wire.beginTransmission(_address);
status = Wire.write(_getHeader(READ_WRITE::WRITE));
if (status != 1)
return false;
status = Wire.write(_getAddressBytes(address));
if (status != 1)
return false;
Wire.write(data);
status = Wire.endTransmission();
if (status != 1)
return false;
return true;
}
uint8_t ZEEPROM::readByte(uint16_t address)
{
uint8_t status = 1;
Wire.beginTransmission(_address);
status = Wire.write(_getHeader(READ_WRITE::READ));
if (status != 1)
return false;
status = Wire.write(_getAddressBytes(address));
if (status != 1)
return false;
status = Wire.endTransmission();
if (status != 1)
return false;
Wire.requestFrom(_address, 1);
return true;
}