So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By rlivio
#74767 Hi

I'm testing with the ESP-01 module.
I'm following the instruction find at: https://tttapa.github.io/ (A-Beginner's-Guide-to-the-ESP8266)
All ok until "Uploading files to SPIFFS"

The problem is that I can't read the file uploaded with a browser.

Trying to isolate the problem I write this code where I do:
- Format the SPIFFS
- Show info about SPIFFS
- Open in write mode the file test.html
- Write 4 bytes
- Close
- Show again info about SPIFFS

Until here all seems to be ok, but when I try to open the file I receive the error -10002 (SPIFFS_ERR_NOT_FOUND)

In a more complex sketch I receive also the error -10012 (SPIFFS_ERR_IS_FREE) and when I try to write a second time the
file test.html it disappears from the directory.

Someone can help me?
Thank you
Livio

Code: Select all//-----------------------------------------------------------------------
// ESP8266 test FS
//-----------------------------------------------------------------------

#include <FS.h>   


#define Monitor Serial1

FSInfo fs_info;

char bufferTemp[300];

String filename;

File fsFile;

void ShowDIR(void);
void ShowInfo(void);
void ReadFile();

//-----------------------------------------------------------------------

void setup() {
    Monitor.begin(115200);                          // Debug console  (only Tx on ESP-01)
 
    SPIFFS.begin();                                 // Start the SPI Flash Files System
    Monitor.println("init SPIFFS");

    if(SPIFFS.format())
      Monitor.println("FORMAT OK");

    ShowInfo();
   
    filename = "test.html";

    Monitor.print("OPEN FILE in Write: "); Monitor.println(filename);
   
    fsFile = SPIFFS.open(filename, "w");          // Open the file for writing in SPIFFS (create if it doesn't exist)
    if(fsFile == false)
      Monitor.println("ERROR opening file"); 

    else{
      Monitor.println("FILE WRITE");
      fsFile.write('A');
      fsFile.write('B');
      fsFile.write('C');
      fsFile.write('D');     
      fsFile.close();                               // Close the file again
      Monitor.println("FILE CLOSE\n");
    }   

    delay(1000);

    ShowInfo();
    ShowDIR();

    ReadFile();
}


void loop() {
  delay(1000); 
}

   
void ShowInfo(void){
 
  SPIFFS.info(fs_info);
  sprintf(bufferTemp,  "Total Bytes:     %d", fs_info.totalBytes);
  Monitor.println(bufferTemp);
  sprintf(bufferTemp,  "Used Bytes:      %d", fs_info.usedBytes);
  Monitor.println(bufferTemp); 
  sprintf(bufferTemp,  "Block Size:      %d", fs_info.blockSize);
  Monitor.println(bufferTemp);   
  sprintf(bufferTemp,  "Page Size:       %d", fs_info.pageSize);
  Monitor.println(bufferTemp); 
  sprintf(bufferTemp,  "Max open files:  %d", fs_info.maxOpenFiles);
  Monitor.println(bufferTemp); 
  sprintf(bufferTemp,  "Max path lenght: %d", fs_info.maxPathLength);
  Monitor.println(bufferTemp);
  Monitor.println("");
}



void ShowDIR(void){

    Monitor.println("\n--------------------------------");
    Monitor.println("Directory:");
    Dir dir = SPIFFS.openDir("");
 
  while (dir.next()) {
    Monitor.print(dir.fileName());
    File f = dir.openFile("r");
    Monitor.print("\t\t");
    Monitor.println(f.size());
  }
  Monitor.println("--------------------------------");
  Monitor.println("");
}

void ReadFile(void){
    fsFile = SPIFFS.open(filename, "r");
    if(fsFile == false){
       Monitor.print("ERROR opening file: "); Monitor.println(filename);
    }       
    else{
       Monitor.print("\n\nRead file: "); Monitor.println(filename);
         
       int lunghezza = fsFile.readBytes(bufferTemp, 300);     
       Monitor.print("Lenght: "); Monitor.println(lunghezza);
       if(lunghezza > 0){
            Monitor.println("-----------------------------------------------");
            Monitor.println(bufferTemp);
            Monitor.println("-----------------------------------------------");
       }
    }
    fsFile.close();
}




The output on the monitor:

Code: Select allSDK:2.2.1(cfd48f3)/Core:2.4.1/lwIP:2.0.3(STABLE-2_0_3_RELEASE/glue:arduino-2.4.1)
SPIFFSImpl: allocating 512+240+1400=2152 bytes
SPIFFSImpl: mounting fs @7b000, size=80000, block=2000, page=100
SPIFFSImpl: mount rc=0
init SPIFFS
SPIFFSImpl: mounting fs @7b000, size=80000, block=2000, page=100
SPIFFSImpl: mount rc=0
SPIFFSImpl: mounting fs @7b000, size=80000, block=2000, page=100
SPIFFSImpl: mount rc=0
FORMAT OK
Total Bytes:     482673
Used Bytes:      0
Block Size:      8192
Page Size:       256
Max open files:  5
Max path lenght: 32

OPEN FILE in Write: test.html
FILE WRITE
SPIFFS_close: fd=1
SPIFFS_close: fd=1
FILE CLOSE

Total Bytes:     482673
Used Bytes:      502
Block Size:      8192
Page Size:       256
Max open files:  5
Max path lenght: 32


--------------------------------
Directory:
test.html      4
SPIFFS_close: fd=2
--------------------------------

SPIFFSImpl::open: fd=-10002 path=`test.html` openMode=0 accessMode=1 err=-10002
ERROR opening file: test.html
User avatar
By rlivio
#74824 Problem solved.
The problem is in the memory chip, Puya P25Q80H
The software need a patch as described in: https://github.com/esp8266/Arduino/issues/4061

I follow the instruction and I change the file ESP.cpp, replacing the function EspClass::flashWrite with:
Code: Select allbool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size) {
   static uint32_t flash_chip_id = 0;

   if (flash_chip_id == 0)
      flash_chip_id = getFlashChipId();
   ets_isr_mask(FLASH_INT_MASK);
   int rc;
   uint32_t* ptr = data;
   if ((flash_chip_id & 0x000000ff) == 0x85) { // 0x146085 PUYA
      static uint32_t read_buf[SPI_FLASH_SEC_SIZE / 4];
      rc = spi_flash_read(offset, read_buf, size);
      if (rc != 0) {
         ets_isr_unmask(FLASH_INT_MASK);
         return false;
      }
      for (size_t i = 0; i < size / 4; ++i) {
         read_buf[i] &= data[i];
      }
      ptr = read_buf;
   }
   rc = spi_flash_write(offset, ptr, size);
   ets_isr_unmask(FLASH_INT_MASK);
   return rc == 0;
}


Now it work fine.
Thank you.
Livio