Skip to content

Commit 6c4e26d

Browse files
authored
Merge pull request #30 from giminotron5/main
Add WP control
2 parents 934c611 + fdfb864 commit 6c4e26d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/SparkFun_External_EEPROM.cpp

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

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

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

892+
// Check if we are using Write Protection then disable WP for write access
893+
if(settings.wpPin != 255 ) digitalWrite(settings.wpPin, LOW);
894+
874895
settings.i2cPort->beginTransmission(i2cAddress);
875896
if (settings.addressSize_bytes > 1) // Device larger than 16,384 bits have two byte addresses
876897
settings.i2cPort->write((uint8_t)((eepromLocation + recorded) >> 8)); // MSB
@@ -888,6 +909,9 @@ int ExternalEEPROM::write(uint32_t eepromLocation, const uint8_t *dataToWrite, u
888909

889910
if (settings.pollForWriteComplete == false)
890911
delay(settings.writeTime_ms); // Delay the amount of time to record a page
912+
913+
// Enable Write Protection if we are using WP
914+
if(settings.wpPin != 255) digitalWrite(settings.wpPin, HIGH);
891915
}
892916

893917
return (result);

src/SparkFun_External_EEPROM.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ struct struct_memorySettings
111111
uint8_t writeTime_ms;
112112
bool pollForWriteComplete;
113113
uint8_t addressSize_bytes;
114+
uint8_t wpPin;
114115
};
115116

116117
class ExternalEEPROM
@@ -122,6 +123,8 @@ class ExternalEEPROM
122123
int write(uint32_t eepromLocation, const uint8_t *dataToWrite, uint16_t blockSize);
123124

124125
bool begin(uint8_t deviceAddress = 0b1010000, TwoWire &wirePort = Wire); // By default use the Wire port
126+
bool begin(uint8_t deviceAddress = 0b1010000, TwoWire &wirePort = Wire, uint8_t WP = 255); // By default use the Wire port
127+
125128
bool isConnected(uint8_t i2cAddress = 255);
126129
bool isBusy(uint8_t i2cAddress = 255);
127130
void erase(uint8_t toWrite = 0x00); // Erase the entire memory. Optional: write a given byte to each spot.
@@ -186,6 +189,7 @@ class ExternalEEPROM
186189
.writeTime_ms = 5, //All EEPROMs seem to have a max write time of 5ms
187190
.pollForWriteComplete = true,
188191
.addressSize_bytes = 2, // Default to two address bytes, to support 24xx32 / 4096 byte EEPROMs and larger
192+
.wpPin = 255, // By default, the write protection pin is not set
189193
};
190194
};
191195

0 commit comments

Comments
 (0)