-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathI2CEEPROM.cpp
119 lines (99 loc) · 3.02 KB
/
I2CEEPROM.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* \brief I2C EEPROMs data writer/reader (implementation)
*
* \author Quentin Comte-Gaz <[email protected]>
* \date 15 January 2023
* \license MIT License (contact me if too restrictive)
* \copyright Copyright (c) 2023 Quentin Comte-Gaz
* \version 1.3
*/
#include "I2CEEPROM.h"
#include <Wire.h>
I2CEEPROM::I2CEEPROM(int i2c_device_address, enum address_mode addressing_mode, bool initialize_wire)
{
_i2c_device_address = i2c_device_address;
_addressing_mode = addressing_mode;
// Set the address mask (as defined in enum) to properly set I2C address when writing/reading
if (addressing_mode == ADDRESS_MODE_16BIT)
{
_i2c_address_mask = 0xFF;
}
else
{
_i2c_address_mask = (0xFF << addressing_mode);
}
// Compatibility -- defaults to true to automatically initialize the Wire library, but it may be unwanted by some users.
if (initialize_wire)
{
Wire.begin();
}
}
void I2CEEPROM::write(unsigned int address, byte data) const
{
// Generate the proper I2C address based on device mask and memory address
uint8_t dev_address = generate_I2C_address(address);
Wire.beginTransmission(dev_address);
// In classic device mode, send the 8 MSB of the memory address.
// This is already part of the device addr in CAT24CXX
if (_addressing_mode == ADDRESS_MODE_16BIT)
{
Wire.write((int)(address >> 8)); // First part of the address (MSB)
Wire.write((int)(address & 0xFF)); // Second part of the address (LSB)
}
else
{
Wire.write((byte)(address & 0xFF));
}
Wire.write(data); // Write byte
Wire.endTransmission();
// Writing in I2C EEPROM takes ~5ms (even if I2C writing already done)
delay(5);
}
byte I2CEEPROM::read(unsigned int address) const
{
// Generate the proper I2C address based on device mask and memory address
uint8_t dev_address = generate_I2C_address(address);
byte read_data = 0xFF;
Wire.beginTransmission(dev_address);
// In classic device mode, send the 8 MSB of the memory address.
// This is already part of the device addr in CAT24CXX
if (_addressing_mode == ADDRESS_MODE_16BIT)
{
Wire.write((int)(address >> 8)); // MSB
Wire.write((int)(address & 0xFF)); // LSB
}
else
{
Wire.write((byte)(address & 0xFF));
}
Wire.endTransmission();
// Request 1 byte from device
Wire.requestFrom((uint8_t)dev_address, (uint8_t)1);
if (Wire.available())
{
read_data = Wire.read();
}
return read_data;
}
void I2CEEPROM::update(unsigned int address, byte data) const
{
if (read(address) != data)
{
write(address, data);
}
else
{
// Data to write is the same as data already written
// => No need to write !
}
}
uint8_t I2CEEPROM::generate_I2C_address(uint16_t address) const
{
uint8_t dev_address = _i2c_device_address;
if (_addressing_mode != ADDRESS_MODE_16BIT)
{
// Set the N MSB of the address to the N LSB of the high-byte of the address
dev_address = (_i2c_device_address & _i2c_address_mask) | ((address >> 8) & (~_i2c_address_mask));
}
return dev_address;
}