Skip to content

Commit ec0bd8e

Browse files
committed
Use a separate flag to indicate the first write to EEPROM.
1 parent c59e377 commit ec0bd8e

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/ParallelEEPROM.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
https://github.com/Andy4495/ParallelEEPROM
44
55
03/05/20 - A.T. - Original
6+
03/06/20 - A.T. - Use a separate flag to indicate the first write to EEPROM
67
78
*/
89

@@ -42,7 +43,7 @@ ParallelEEPROM::ParallelEEPROM(byte A14, byte A13, byte A12, byte A11, byte A10,
4243
_EEPROM_WE = EEPROM_WE;
4344
_LVC245_OE = LVC_245_OE;
4445
_LVC245_DIR = LVC_245_DIR;
45-
_lastAddressWritten = 0xFFFF; // Use 0xFFFF as flag that we haven't written anything yet. This won't work if we start supporting 28C512 chips.
46+
_isThisTheFirstWrite = true;
4647
}
4748

4849
// 28C16 with 74LVC245 transceiver
@@ -79,7 +80,7 @@ ParallelEEPROM::ParallelEEPROM(byte A10, byte A9, byte A8,
7980
_EEPROM_WE = EEPROM_WE;
8081
_LVC245_OE = LVC_245_OE;
8182
_LVC245_DIR = LVC_245_DIR;
82-
_lastAddressWritten = 0xFFFF; // Use 0xFFFF as flag that we haven't written anything yet. This won't work if we start supporting 28C512 chips.
83+
_isThisTheFirstWrite = true;
8384
}
8485

8586
// 28C256/X28256 direct connect to microprocessor (no transceiver on data bus)
@@ -115,7 +116,7 @@ ParallelEEPROM::ParallelEEPROM(byte A14, byte A13, byte A12, byte A11, byte A10,
115116
_EEPROM_WE = EEPROM_WE;
116117
_LVC245_OE = NO_PIN;
117118
_LVC245_DIR = NO_PIN;
118-
_lastAddressWritten = 0xFFFF; // Use 0xFFFF as flag that we haven't written anything yet. This won't work if we start supporting 28C512 chips.
119+
_isThisTheFirstWrite = true;
119120
}
120121

121122
// 28C16 direct connect to microprocessor (no transceiver on data bus)
@@ -152,7 +153,7 @@ ParallelEEPROM::ParallelEEPROM(byte A10, byte A9, byte A8,
152153
_EEPROM_WE = EEPROM_WE;
153154
_LVC245_OE = NO_PIN;
154155
_LVC245_DIR = NO_PIN;
155-
_lastAddressWritten = 0xFFFF; // Use 0xFFFF as flag that we haven't written anything yet. This won't work if we start supporting 28C512 chips.
156+
_isThisTheFirstWrite = true;
156157
}
157158

158159
void ParallelEEPROM::begin() {
@@ -179,11 +180,16 @@ void ParallelEEPROM::begin() {
179180
}
180181
}
181182

183+
// This method does not include any delays or checks to see if a previous
184+
// write completed before starting a new write. The sketch calling this
185+
// method will need to add the appropriate delay (as defined in the device
186+
// datasheet) after every call to write().
182187
void ParallelEEPROM::write(uint16_t address, byte data) {
183188
int i;
184189

185190
_lastAddressWritten = address;
186191
_lastByteWritten = data;
192+
_isThisTheFirstWrite = false;
187193
setAddressLines(address);
188194
setDataOutputMode();
189195
if (_LVC245_DIR != NO_PIN) digitalWrite(_LVC245_DIR, LOW);
@@ -208,11 +214,11 @@ void ParallelEEPROM::write(uint16_t address, byte data) {
208214

209215
// This method uses Data Polling to check if prevous write
210216
// cycle completed before starting another write.
217+
// Check the device datasheet to see if Data Polling is supported.
211218
// When using this method, no explicit delays are required after writes.
212219
void ParallelEEPROM::writeWithPolling(uint16_t address, byte data) {
213220
// First check if this is the first write to the chip.
214-
// NOTE: This implementation will need to change if this library ever supports 28C512 chips
215-
if (_lastAddressWritten != 0xFFFF) {
221+
if (_isThisTheFirstWrite == false) {
216222
// Check if bit 7 is the complement of previous written bit.
217223
// Once bit 7 is correct, then write may proceed.
218224
while ((read(_lastAddressWritten) & 0x80) != (_lastByteWritten & 0x80) ) ;
@@ -222,6 +228,7 @@ void ParallelEEPROM::writeWithPolling(uint16_t address, byte data) {
222228

223229
// This method uses Toggle Bit Polling to check if prevous write
224230
// cycle completed before starting another write.
231+
// Check the device datasheet to see if Toggle Polling is supported.
225232
// When using this method, no explicit delays are required after writes.
226233
void ParallelEEPROM::writeWithTogglePolling(uint16_t address, byte data) {
227234
byte prevData, currentData;
@@ -236,6 +243,11 @@ void ParallelEEPROM::writeWithTogglePolling(uint16_t address, byte data) {
236243
write(address, data);
237244
}
238245

246+
// This method does not include any delays or checks to see if a previous
247+
// write completed before attempting a read. The sketch calling this
248+
// method will need to add the appropriate delay (as defined in the device
249+
// datasheet) after any previous call to write() to ensure that valid
250+
// data is read when using this method.
239251
byte ParallelEEPROM::read(uint16_t address) {
240252
byte data = 0;
241253

@@ -247,7 +259,6 @@ byte ParallelEEPROM::read(uint16_t address) {
247259
// The digitalWrites that follow easily make a > 250 ns delay
248260
if (_LVC245_DIR != NO_PIN) digitalWrite(_LVC245_DIR, HIGH);
249261
if (_LVC245_OE != NO_PIN) digitalWrite(_LVC245_OE, LOW);
250-
//delayMicroseconds(3);
251262

252263
if (digitalRead(_Data[7]) == HIGH) data |= 0x80;
253264
if (digitalRead(_Data[6]) == HIGH) data |= 0x40;
@@ -268,11 +279,11 @@ byte ParallelEEPROM::read(uint16_t address) {
268279

269280
// This method uses Data Polling to check if prevous write
270281
// cycle completed before reading.
282+
// Check the device datasheet to see if Data Polling is supported.
271283
// When using this method, no explicit delays are required after writes.
272284
byte ParallelEEPROM::readWithPolling(uint16_t address) {
273285
// First check if there has been a write to the chip.
274-
// NOTE: This implementation will need to change if this library ever supports 28C512 chips
275-
if (_lastAddressWritten != 0xFFFF) {
286+
if (_isThisTheFirstWrite == false) {
276287
// Check if bit 7 is the complement of previous written bit.
277288
// Once bit 7 is correct, then write may proceed.
278289
while ((read(_lastAddressWritten) & 0x80) != (_lastByteWritten & 0x80) ) ;
@@ -282,6 +293,7 @@ byte ParallelEEPROM::readWithPolling(uint16_t address) {
282293

283294
// This method uses Toggle Bit Polling to check if prevous write
284295
// cycle completed before trying to read the EEPROM.
296+
// Check the device datasheet to see if Toggle Polling is supported.
285297
// When using this method, no explicit delays are required after writes.
286298
byte ParallelEEPROM::readWithTogglePolling(uint16_t address) {
287299
byte prevData, currentData;

src/ParallelEEPROM.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
https://github.com/Andy4495/ParallelEEPROM
44
55
03/05/20 - A.T. - Original
6+
03/06/20 - A.T. - Use a separate flag to indicate the first write to EEPROM
67
78
*/
89

@@ -58,6 +59,7 @@ ParallelEEPROM(byte A10, byte A9, byte A8,
5859
byte _LVC245_OE, _LVC245_DIR;
5960
byte _lastByteWritten;
6061
uint16_t _lastAddressWritten;
62+
bool _isThisTheFirstWrite; // Set to true in constructor, set to false after a write
6163

6264
void setAddressLines(uint16_t address);
6365
void setDataOutputMode();

0 commit comments

Comments
 (0)