Hi,
I am using a library for the 25LC1024 EEPROM that I found here. I had to modify it very slightly to make it work with the Particle Photon.
Reading and writing works fine except across a specific page boundary at address 32,768 (max int16, coincidence?)
I can write and read across page boundary’s before and after the boundary at 37,768.
I can write and read within pages either side of the boundary at 32,768.
Here is the code I am using to test the writing and reading:
char buffer[]="{1458215700,c=1,t=0}";
uint16_t length = strlen(buffer) + 1;
uint32_t address = 32762; <-- Change this to set start of write and read
//Create check buffer
char checkBuffer[length];
checkBuffer[0] = '\0';
Serial.printf("Address: %d\r\n", address);
Serial.printf("Length: %d\r\n", length);
//Save item to EEPROM
buffer[length - 1] = '.'; //Replace '\0' with '.'
Serial.printf("Write success: %d\r\n", _spieep->writen(address, buffer, length));
buffer[length - 1] = '\0';
Serial.printf("Write buffer: %s\r\n", buffer);
//Read back item from EEPROM
Serial.printf("Read success: %d\r\n", _spieep->readn(address, checkBuffer, length));
checkBuffer[length - 1] = '\0';
Serial.printf("Read buffer: %s\r\n", checkBuffer);
Here are the write and read functions from the library with my extra logging added in:
boolean SPIEEP::writen(uint32_t p, byte *buf, uint16_t len) {
uint32_t idx, e, pg_i_e, wrlen, i; /* idx = start of current read block, e = last read address + 1,
* pg_i_e = address of last byte in idx's current page + 1,
* wrlen = # bytes to write in current block
*/
if (!verify_enabled()) return false;
if ((p+len-1) > _highestaddr) return false;
idx = p;
e = p+len;
do {
pg_i_e = (idx/_pagesize)*_pagesize + _pagesize;
Serial.printf("WRITE. Page size: %d, idx: %d, pg: %d\r\n", _pagesize, idx, pg_i_e);
if (e > pg_i_e)
{
wrlen = pg_i_e - idx;
Serial.printf("Writing over page bounday. wrlen: %d\r\n", wrlen);
}
else
{
wrlen = e - idx;
Serial.printf("Writing within page bounday. wrlen: %d\r\n", wrlen);
}
// Perform write for this block
wren();
if (!is_wren())
return false; // Couldn't enable WREN for some reason?
digitalWrite(_cspin, LOW);
if (_is_25LC040)
SPI.transfer(_compose_instruction_addrbit(idx, SPIEEP_WRITE));
else
SPI.transfer(SPIEEP_WRITE);
_write_address(idx);
for (i=0; i<wrlen; i++)
{
SPI.transfer(buf[idx-p+i]);
Serial.printf("Writing byte no: %d, which holds: %c\r\n", idx-p+i, (char)buf[idx-p+i]);
}
digitalWrite(_cspin, HIGH);
if (!_write_validation())
return false; // A failed write, abort before performing any more.
idx += wrlen;
} while (idx < e);
return true;
}
boolean SPIEEP::readn(uint32_t p, byte *buf, uint16_t len) {
uint32_t idx, e, pg_i_e, rdlen, i; /* idx = start of current read block, e = last read address + 1,
* pg_i_e = address of last byte in idx's current page + 1,
* rdlen = # bytes to read in current block
*/
if (!verify_enabled()) return false;
if ((p+len-1) > _highestaddr) return false;
idx = p;
e = p+len;
do {
pg_i_e = (idx/_pagesize)*_pagesize + _pagesize;
Serial.printf("READ. Page size: %d, idx: %d, pg: %d\r\n", _pagesize, idx, pg_i_e);
if (e > pg_i_e)
{
rdlen = pg_i_e - idx;
Serial.printf("Reading over page bounday. rdlen: %d\r\n", rdlen);
}
else
{
rdlen = e - idx;
Serial.printf("Reading within page bounday. rdlen: %d\r\n", rdlen);
}
// Perform read for this block
digitalWrite(_cspin, LOW);
if (_is_25LC040)
SPI.transfer(_compose_instruction_addrbit(idx, SPIEEP_READ));
else
SPI.transfer(SPIEEP_READ);
_write_address(idx);
for (i=0; i<rdlen; i++)
{
buf[idx-p+i] = SPI.transfer(0x0);
Serial.printf("Reading byte no: %d, which holds: %c\r\n", idx-p+i, (char)buf[idx-p+i]);
}
digitalWrite(_cspin, HIGH);
idx += rdlen;
} while (idx < e);
return true;
}
and I get the following results (last result is the one that writes over the page boundary at 37,768 and does not work correctly)
Writing within first page - NOT CROSSING BOUNDARY
INFO LOG - Thu Mar 17 11:55:00 2016 - Saving item: {1458215700,c=1,t=0}
Address: 128
Length: 21
WRITE. Page size: 256, idx: 128, pg: 256
Writing within page bounday. wrlen: 21
Writing byte no: 0, which holds: {
Writing byte no: 1, which holds: 1
Writing byte no: 2, which holds: 4
Writing byte no: 3, which holds: 5
Writing byte no: 4, which holds: 8
Writing byte no: 5, which holds: 2
Writing byte no: 6, which holds: 1
Writing byte no: 7, which holds: 5
Writing byte no: 8, which holds: 7
Writing byte no: 9, which holds: 0
Writing byte no: 10, which holds: 0
Writing byte no: 11, which holds: ,
Writing byte no: 12, which holds: c
Writing byte no: 13, which holds: =
Writing byte no: 14, which holds: 1
Writing byte no: 15, which holds: ,
Writing byte no: 16, which holds: t
Writing byte no: 17, which holds: =
Writing byte no: 18, which holds: 0
Writing byte no: 19, which holds: }
Writing byte no: 20, which holds: .
Write success: 1
Write buffer: {1458215700,c=1,t=0}
READ. Page size: 256, idx: 128, pg: 256
Reading within page bounday. rdlen: 21
Reading byte no: 0, which holds: {
Reading byte no: 1, which holds: 1
Reading byte no: 2, which holds: 4
Reading byte no: 3, which holds: 5
Reading byte no: 4, which holds: 8
Reading byte no: 5, which holds: 2
Reading byte no: 6, which holds: 1
Reading byte no: 7, which holds: 5
Reading byte no: 8, which holds: 7
Reading byte no: 9, which holds: 0
Reading byte no: 10, which holds: 0
Reading byte no: 11, which holds: ,
Reading byte no: 12, which holds: c
Reading byte no: 13, which holds: =
Reading byte no: 14, which holds: 1
Reading byte no: 15, which holds: ,
Reading byte no: 16, which holds: t
Reading byte no: 17, which holds: =
Reading byte no: 18, which holds: 0
Reading byte no: 19, which holds: }
Reading byte no: 20, which holds: .
Read success: 1
Read buffer: {1458215700,c=1,t=0}
Writing within first page - CROSSING BOUNADRY
INFO LOG - Thu Mar 17 11:57:00 2016 - Saving item: {1458215820,c=1,t=0}
Address: 250
Length: 21
WRITE. Page size: 256, idx: 250, pg: 256
Writing over page bounday. wrlen: 6
Writing byte no: 0, which holds: {
Writing byte no: 1, which holds: 1
Writing byte no: 2, which holds: 4
Writing byte no: 3, which holds: 5
Writing byte no: 4, which holds: 8
Writing byte no: 5, which holds: 2
WRITE. Page size: 256, idx: 256, pg: 512
Writing within page bounday. wrlen: 15
Writing byte no: 6, which holds: 1
Writing byte no: 7, which holds: 5
Writing byte no: 8, which holds: 8
Writing byte no: 9, which holds: 2
Writing byte no: 10, which holds: 0
Writing byte no: 11, which holds: ,
Writing byte no: 12, which holds: c
Writing byte no: 13, which holds: =
Writing byte no: 14, which holds: 1
Writing byte no: 15, which holds: ,
Writing byte no: 16, which holds: t
Writing byte no: 17, which holds: =
Writing byte no: 18, which holds: 0
Writing byte no: 19, which holds: }
Writing byte no: 20, which holds: .
Write success: 1
Write buffer: {1458215820,c=1,t=0}
READ. Page size: 256, idx: 250, pg: 256
Reading over page bounday. rdlen: 6
Reading byte no: 0, which holds: {
Reading byte no: 1, which holds: 1
Reading byte no: 2, which holds: 4
Reading byte no: 3, which holds: 5
Reading byte no: 4, which holds: 8
Reading byte no: 5, which holds: 2
READ. Page size: 256, idx: 256, pg: 512
Reading within page bounday. rdlen: 15
Reading byte no: 6, which holds: 1
Reading byte no: 7, which holds: 5
Reading byte no: 8, which holds: 8
Reading byte no: 9, which holds: 2
Reading byte no: 10, which holds: 0
Reading byte no: 11, which holds: ,
Reading byte no: 12, which holds: c
Reading byte no: 13, which holds: =
Reading byte no: 14, which holds: 1
Reading byte no: 15, which holds: ,
Reading byte no: 16, which holds: t
Reading byte no: 17, which holds: =
Reading byte no: 18, which holds: 0
Reading byte no: 19, which holds: }
Reading byte no: 20, which holds: .
Read success: 1
Read buffer: {1458215820,c=1,t=0}
Writing within page after address 32,768 - NOT CROSSING BOUNDARY
INFO LOG - Thu Mar 17 11:52:00 2016 - Saving item: {1458215520,c=1,t=0}
Address: 32800
Length: 21
WRITE. Page size: 256, idx: 32800, pg: 33024
Writing within page bounday. wrlen: 21
Writing byte no: 0, which holds: {
Writing byte no: 1, which holds: 1
Writing byte no: 2, which holds: 4
Writing byte no: 3, which holds: 5
Writing byte no: 4, which holds: 8
Writing byte no: 5, which holds: 2
Writing byte no: 6, which holds: 1
Writing byte no: 7, which holds: 5
Writing byte no: 8, which holds: 5
Writing byte no: 9, which holds: 2
Writing byte no: 10, which holds: 0
Writing byte no: 11, which holds: ,
Writing byte no: 12, which holds: c
Writing byte no: 13, which holds: =
Writing byte no: 14, which holds: 1
Writing byte no: 15, which holds: ,
Writing byte no: 16, which holds: t
Writing byte no: 17, which holds: =
Writing byte no: 18, which holds: 0
Writing byte no: 19, which holds: }
Writing byte no: 20, which holds: .
Write success: 1
Write buffer: {1458215520,c=1,t=0}
READ. Page size: 256, idx: 32800, pg: 33024
Reading within page bounday. rdlen: 21
Reading byte no: 0, which holds: {
Reading byte no: 1, which holds: 1
Reading byte no: 2, which holds: 4
Reading byte no: 3, which holds: 5
Reading byte no: 4, which holds: 8
Reading byte no: 5, which holds: 2
Reading byte no: 6, which holds: 1
Reading byte no: 7, which holds: 5
Reading byte no: 8, which holds: 5
Reading byte no: 9, which holds: 2
Reading byte no: 10, which holds: 0
Reading byte no: 11, which holds: ,
Reading byte no: 12, which holds: c
Reading byte no: 13, which holds: =
Reading byte no: 14, which holds: 1
Reading byte no: 15, which holds: ,
Reading byte no: 16, which holds: t
Reading byte no: 17, which holds: =
Reading byte no: 18, which holds: 0
Reading byte no: 19, which holds: }
Reading byte no: 20, which holds: .
Read success: 1
Read buffer: {1458215520,c=1,t=0}
Writing with page before address 32,768 - CROSSING BOUNDARY
INFO LOG - Thu Mar 17 12:00:00 2016 - Saving item: {1458216000,c=1,t=0}
Address: 32762
Length: 21
WRITE. Page size: 256, idx: 32762, pg: 32768
Writing over page bounday. wrlen: 6
Writing byte no: 0, which holds: {
Writing byte no: 1, which holds: 1
Writing byte no: 2, which holds: 4
Writing byte no: 3, which holds: 5
Writing byte no: 4, which holds: 8
Writing byte no: 5, which holds: 2
WRITE. Page size: 256, idx: 32768, pg: 33024
Writing within page bounday. wrlen: 15
Writing byte no: 6, which holds: 1
Writing byte no: 7, which holds: 6
Writing byte no: 8, which holds: 0
Writing byte no: 9, which holds: 0
Writing byte no: 10, which holds: 0
Writing byte no: 11, which holds: ,
Writing byte no: 12, which holds: c
Writing byte no: 13, which holds: =
Writing byte no: 14, which holds: 1
Writing byte no: 15, which holds: ,
Writing byte no: 16, which holds: t
Writing byte no: 17, which holds: =
Writing byte no: 18, which holds: 0
Writing byte no: 19, which holds: }
Writing byte no: 20, which holds: .
Write success: 1
Write buffer: {1458216000,c=1,t=0}
READ. Page size: 256, idx: 32762, pg: 32768
Reading over page bounday. rdlen: 6
Reading byte no: 0, which holds: {
Reading byte no: 1, which holds: 1
Reading byte no: 2, which holds: 4
Reading byte no: 3, which holds: 5
Reading byte no: 4, which holds: 8
Reading byte no: 5, which holds: 2
READ. Page size: 256, idx: 32768, pg: 33024
Reading within page bounday. rdlen: 15
Reading byte no: 6, which holds: Reading byte no: 7, which holds: 6 <-- looks like even byte numbers contain '\0'?
Reading byte no: 8, which holds: Reading byte no: 9, which holds: 0
Reading byte no: 10, which holds: Reading byte no: 11, which holds: ,
Reading byte no: 12, which holds: Reading byte no: 13, which holds: =
Reading byte no: 14, which holds: Reading byte no: 15, which holds: ,
Reading byte no: 16, which holds: Reading byte no: 17, which holds: =
Reading byte no: 18, which holds: Reading byte no: 19, which holds: }
Reading byte no: 20, which holds: Read success: 1
Read buffer: {14582
It looks like all the even bytes are being set to ‘\0’?
The weird thing is the data sheet says you can read back data in one go without worrying about page boundaries but the library does read in blocks up to each boundary. I tried reading all in one go but that prevented any cross boundary read from giving the correct data back.
I would really appreciate any advice on potential causes for this.
Cheers