Skip to content

Add WP control #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/SparkFun_External_EEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ bool ExternalEEPROM::begin(uint8_t deviceAddress, TwoWire &wirePort)

return true;
}
bool ExternalEEPROM::begin(uint8_t deviceAddress, TwoWire &wirePort, uint8_t WP)
{
if(WP != 255)
{
pinMode(WP, OUTPUT);
digitalWrite(WP, HIGH);
settings.wpPin = WP;
}
settings.i2cPort = &wirePort; // Grab which port the user wants us to use
settings.deviceAddress = deviceAddress;

if (isConnected() == false)
{
return false;
}

return true;
}

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

// Check if we are using Write Protection then disable WP for write access
if(settings.wpPin != 255 ) digitalWrite(settings.wpPin, LOW);

settings.i2cPort->beginTransmission(i2cAddress);
if (settings.addressSize_bytes > 1) // Device larger than 16,384 bits have two byte addresses
settings.i2cPort->write((uint8_t)((eepromLocation + recorded) >> 8)); // MSB
Expand All @@ -888,6 +909,9 @@ int ExternalEEPROM::write(uint32_t eepromLocation, const uint8_t *dataToWrite, u

if (settings.pollForWriteComplete == false)
delay(settings.writeTime_ms); // Delay the amount of time to record a page

// Enable Write Protection if we are using WP
if(settings.wpPin != 255) digitalWrite(settings.wpPin, HIGH);
}

return (result);
Expand Down
4 changes: 4 additions & 0 deletions src/SparkFun_External_EEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ struct struct_memorySettings
uint8_t writeTime_ms;
bool pollForWriteComplete;
uint8_t addressSize_bytes;
uint8_t wpPin;
};

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

bool begin(uint8_t deviceAddress = 0b1010000, TwoWire &wirePort = Wire); // By default use the Wire port
bool begin(uint8_t deviceAddress = 0b1010000, TwoWire &wirePort = Wire, uint8_t WP = 255); // By default use the Wire port

bool isConnected(uint8_t i2cAddress = 255);
bool isBusy(uint8_t i2cAddress = 255);
void erase(uint8_t toWrite = 0x00); // Erase the entire memory. Optional: write a given byte to each spot.
Expand Down Expand Up @@ -186,6 +189,7 @@ class ExternalEEPROM
.writeTime_ms = 5, //All EEPROMs seem to have a max write time of 5ms
.pollForWriteComplete = true,
.addressSize_bytes = 2, // Default to two address bytes, to support 24xx32 / 4096 byte EEPROMs and larger
.wpPin = 255, // By default, the write protection pin is not set
};
};

Expand Down