Skip to content

Commit 701f3f7

Browse files
committed
Add WP control
1 parent 7bdf095 commit 701f3f7

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/SparkFun_External_EEPROM.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ bool ExternalEEPROM::begin(uint8_t deviceAddress, TwoWire &wirePort)
6666

6767
return true;
6868
}
69+
bool ExternalEEPROM::begin(uint8_t WP = LED_BUILTIN, uint8_t deviceAddress, TwoWire &wirePort)
70+
{
71+
pinMode(WP, OUTPUT);
72+
digitalWrite(WP, HIGH);
73+
settings.wpPin = WP;
74+
settings.usingWP = true;
75+
settings.i2cPort = &wirePort; // Grab which port the user wants us to use
76+
settings.deviceAddress = deviceAddress;
77+
78+
if (isConnected() == false)
79+
{
80+
return false;
81+
}
82+
83+
return true;
84+
}
6985

7086
// Erase entire EEPROM
7187
void ExternalEEPROM::erase(uint8_t toWrite)
@@ -876,6 +892,9 @@ int ExternalEEPROM::write(uint32_t eepromLocation, const uint8_t *dataToWrite, u
876892
while (isBusy(settings.deviceAddress) == true) // Poll device's original address, not the modified one
877893
delayMicroseconds(100); // This shortens the amount of time waiting between writes but hammers the I2C bus
878894

895+
// Check if we are using Write Protection then disable WP for write access
896+
if(settings.usingWP) digitalWrite(settings.wpPin, LOW);
897+
879898
settings.i2cPort->beginTransmission(i2cAddress);
880899
if (settings.addressSize_bytes > 1) // Device larger than 16,384 bits have two byte addresses
881900
settings.i2cPort->write((uint8_t)((eepromLocation + recorded) >> 8)); // MSB
@@ -885,6 +904,8 @@ int ExternalEEPROM::write(uint32_t eepromLocation, const uint8_t *dataToWrite, u
885904
settings.i2cPort->write(dataToWrite[recorded + x]);
886905

887906
result = settings.i2cPort->endTransmission(); // Send stop condition
907+
// Enable Write Protection if we are using WP
908+
if(settings.usingWP) digitalWrite(settings.wpPin, HIGH);
888909

889910
recorded += amtToWrite;
890911

src/SparkFun_External_EEPROM.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ struct struct_memorySettings
111111
uint8_t writeTime_ms;
112112
bool pollForWriteComplete;
113113
uint8_t addressSize_bytes;
114+
bool usingWP;
115+
uint8_t wpPin;
114116
};
115117

116118
class ExternalEEPROM
@@ -122,6 +124,8 @@ class ExternalEEPROM
122124
int write(uint32_t eepromLocation, const uint8_t *dataToWrite, uint16_t blockSize);
123125

124126
bool begin(uint8_t deviceAddress = 0b1010000, TwoWire &wirePort = Wire); // By default use the Wire port
127+
bool begin(uint8_t WP, uint8_t deviceAddress = 0b1010000, TwoWire &wirePort = Wire); // By default use the Wire port
128+
125129
bool isConnected(uint8_t i2cAddress = 255);
126130
bool isBusy(uint8_t i2cAddress = 255);
127131
void erase(uint8_t toWrite = 0x00); // Erase the entire memory. Optional: write a given byte to each spot.
@@ -186,6 +190,8 @@ class ExternalEEPROM
186190
.writeTime_ms = 5, //All EEPROMs seem to have a max write time of 5ms
187191
.pollForWriteComplete = true,
188192
.addressSize_bytes = 2, // Default to two address bytes, to support 24xx32 / 4096 byte EEPROMs and larger
193+
.usingWP = false,
194+
.wpPin = LED_BUILTIN,
189195
};
190196
};
191197

0 commit comments

Comments
 (0)