Skip to content

Commit afbf770

Browse files
committed
Add write protection pin control
1 parent 701f3f7 commit afbf770

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/SparkFun_External_EEPROM.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,14 @@ 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)
69+
bool ExternalEEPROM::begin(uint8_t deviceAddress, TwoWire &wirePort, uint8_t WP)
7070
{
71-
pinMode(WP, OUTPUT);
72-
digitalWrite(WP, HIGH);
73-
settings.wpPin = WP;
74-
settings.usingWP = true;
71+
if(WP != 255)
72+
{
73+
pinMode(WP, OUTPUT);
74+
digitalWrite(WP, HIGH);
75+
settings.wpPin = WP;
76+
}
7577
settings.i2cPort = &wirePort; // Grab which port the user wants us to use
7678
settings.deviceAddress = deviceAddress;
7779

@@ -893,7 +895,7 @@ int ExternalEEPROM::write(uint32_t eepromLocation, const uint8_t *dataToWrite, u
893895
delayMicroseconds(100); // This shortens the amount of time waiting between writes but hammers the I2C bus
894896

895897
// Check if we are using Write Protection then disable WP for write access
896-
if(settings.usingWP) digitalWrite(settings.wpPin, LOW);
898+
if(settings.wpPin != 255 ) digitalWrite(settings.wpPin, LOW);
897899

898900
settings.i2cPort->beginTransmission(i2cAddress);
899901
if (settings.addressSize_bytes > 1) // Device larger than 16,384 bits have two byte addresses
@@ -904,8 +906,6 @@ int ExternalEEPROM::write(uint32_t eepromLocation, const uint8_t *dataToWrite, u
904906
settings.i2cPort->write(dataToWrite[recorded + x]);
905907

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

910910
recorded += amtToWrite;
911911

@@ -914,6 +914,9 @@ int ExternalEEPROM::write(uint32_t eepromLocation, const uint8_t *dataToWrite, u
914914

915915
if (settings.pollForWriteComplete == false)
916916
delay(settings.writeTime_ms); // Delay the amount of time to record a page
917+
918+
// Enable Write Protection if we are using WP
919+
if(settings.wpPin != 255) digitalWrite(settings.wpPin, HIGH);
917920
}
918921

919922
return (result);

src/SparkFun_External_EEPROM.h

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

@@ -124,7 +123,7 @@ class ExternalEEPROM
124123
int write(uint32_t eepromLocation, const uint8_t *dataToWrite, uint16_t blockSize);
125124

126125
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
126+
bool begin(uint8_t deviceAddress = 0b1010000, TwoWire &wirePort = Wire, uint8_t WP = 255); // By default use the Wire port
128127

129128
bool isConnected(uint8_t i2cAddress = 255);
130129
bool isBusy(uint8_t i2cAddress = 255);
@@ -190,8 +189,7 @@ class ExternalEEPROM
190189
.writeTime_ms = 5, //All EEPROMs seem to have a max write time of 5ms
191190
.pollForWriteComplete = true,
192191
.addressSize_bytes = 2, // Default to two address bytes, to support 24xx32 / 4096 byte EEPROMs and larger
193-
.usingWP = false,
194-
.wpPin = LED_BUILTIN,
192+
.wpPin = 255, // By default, the write protection pin is not set
195193
};
196194
};
197195

0 commit comments

Comments
 (0)