SPIMemory library - Formerly SPIFlash - now supports SPI FRAM as well! :)

New start :smiley:

some calluclation about the image size.....
image size: 49208 bytes
1 page = 256 bytes
1 sector = 4k / 4096 bytes
1 sector = 16 pages
4096 bytes * 256 = 1'048'576 = 1MB

PIC Size = 49208/4096 = 12.01... 13 sectors
13 sectors * 16 Pages = 208 total Pages / 16..207
208 pages and the last page (208 )just 56 bytes.

Clear the Flash from page 16..208

  uint16_t _pageOfset = 16; // Start from page 16. First 16 pages are reserved for configuration
  for (uint16_t p = _pageOfset; p < _pageOfset + 208; p++) {
  while (!flash.eraseSector(p, 0));
  }

Read the file:

File bmpFileTmp;
  // Open requested file on SD card
  if ((bmpFileTmp = SD.open(filename)) == NULL) {
    Serial.print("File not found");
    return;
  }

Write to flash:

 uint16_t _index = 0;
  uint8_t _page = _pageOfset;
  uint16_t maxPage = 0;
  uint16_t lastMaxIndex = 0;
  if (bmpFileTmp) {
    while (bmpFileTmp.available()) {
      if (_index == 256) {
        _page++;
        _index = 0;
      }
      flash.writeByte(_page, _index, (byte)bmpFileTmp.read());
      _index++;
    }
    bmpFileTmp.close();
  }  
maxPage = _page - _pageOfset;
lastMaxIndex = _index;

Check to first page (BMP signature) in the flash:

  printPage(16, 2);
  printPage(16, 1);
Reading page (0010)
066,077,056,192,000,000,000,000,000,000,054,000,000,000,040,000,
000,000,128,000,000,000,128,000,000,000,001,000,024,000,000,000,
000,000,002,192,000,000,229,076,000,000,229,076,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
Reading page (0010)
424d38c0000000000000360000002800
00008000000080000000010018000000
000002c00000e54c0000e54c00000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000

First two bytes are signature for BMP.
066,077 or 0x424d

So far all ok.

Read back from flash to file handler:

  Serial.print("Page to loop: ");
  Serial.println(_pageOfset + maxPage);

  uint32_t _indexTotal = 0;
  for (uint16_t p = _pageOfset; p <= _pageOfset + maxPage; p++) {
    if (p == _pageOfset + maxPage) {
      for (uint16_t i = 0; i <= lastMaxIndex; i++) {
        Serial.print(p);
        Serial.print("\t");
        Serial.print(i);
        Serial.print("\t");
        bmpFile.write((byte)flash.readByte(p, i));
        Serial.println(_indexTotal);
        _indexTotal++;
      }
    } else {
      for (uint16_t i = 0; i <= 255; i++) {
        Serial.print(p);
        Serial.print("\t");
        Serial.print(i);
        Serial.print("\t");
        Serial.println(_indexTotal);
        bmpFile.write((byte)flash.readByte(p, i));
        _indexTotal++;
      }
    }
  }

last output...
pages bytes index
208 56 49208

but when I check first two bytes from the new file handler I get wrong values.

  uint16_t checkBMP = read16(bmpFile);
  Serial.println(checkBMP);


uint16_t read16(File f) {
  uint16_t result;
  ((uint8_t *)&result)[0] = f.read(); // LSB
  ((uint8_t *)&result)[1] = f.read(); // MSB
  return result;
}

Here I expect 0x424D (d16973) but I get 65535. :roll_eyes:
The Bytes in the Flash looks good. In any case, the signature is correct.

Some Idea?