libretiny-eu / libretiny

PlatformIO development platform for IoT modules
http://docs.libretiny.eu/
MIT License
401 stars 59 forks source link

Add LittleFS libs from ESP8266 project #189

Open kolos opened 11 months ago

kolos commented 11 months ago

"User data" partition definitions needs to be moved from LittleFS.cpp to the boards definitions.

Currently hardcoded for my test board variant: CB2S

nsavga commented 9 months ago

hello, i tried your pull request by manually merging it to master locally, as I need to implement LittleFS to work with WBR3 but it created a lot of compile time errors. most of them look like they are related to some libretiny macros. are you planning to finalize your pull request work soon? thanks & regards

kolos commented 9 months ago

Feel free to continue, I'm only using the methods needed for AsyncWebServer's serveStatic method; file upload and serve.

It's tested on CB2S and WB2S.

nsavga commented 9 months ago

thanks a lot!

i would appreciate if you could provide example linker script definitions you used for CB2S and WB2S,
FLASH_USERDATA_OFFSET and FLASH_USERDATA_LENGTH. I am not sure how to determine/find them for WBR3 yet, but it would be helpful as a starting point.

kind regards

kolos commented 9 months ago

Create a libretiny platformio project, it will do the definitions for those constants.

For example, here is my platformio.ini file:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:wb2s]
platform = libretiny
board = wb2s
framework = arduino
lib_deps = esphome/ESPAsyncWebServer-esphome@^3.1.0
upload_speed = 115200
monitor_speed = 115200
build_src_flags = -include Arduino.h
build_flags = 
    -DCONFIG_ASYNC_TCP_STACK_SIZE=4096
    -DLT_LOGLEVEL=LT_LEVEL_NONE
    -DLT_UART_SILENT_ALL=1
    -DLT_USE_TIME=1
;   -DconfigUSE_RECURSIVE_MUTEXES=1
    -DUSE_ARDUINO
    -DUSE_LIBRETUYA
    -Wno-sign-compare
    -Wno-unused-but-set-variable
    -Wno-unused-variable
    -Wno-unused-function
    -fno-exceptions
Mit4el commented 4 months ago

@kolos @nsavga You can help me. I don't understand how to build and upload FS to the chip. I have a bk7231n.

I took the libraries that you wrote, everything was assembled, but how to upload files to the FS. Do you have an open project where you can view work with LittleFS?

kolos commented 4 months ago

I'm using ESPAsyncWebserver like this:

#include <ESPAsyncWebServer.h>
#include "LittleFS.h"

AsyncWebServer server(80);

void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
  if (!index) {
    request->_tempFile = LittleFS.open("/" + filename, "w");
  }

  if (len) {
    request->_tempFile.write(data, len);
  }

  if (final) {
    request->_tempFile.close();
    request->redirect("/");
  }
}

void setup_webserver() {
  LittleFS.begin();

  server.on("/upload", HTTP_POST, [](AsyncWebServerRequest *request) {
    request->send(200);
  }, handleUpload);

  server.serveStatic("/", LittleFS, "/");

  server.begin();
}

and uploading files via curl:

curl -F "file=@index.html.gz;filename=index.htm.gz" http://192.168.1.123/upload
Mit4el commented 4 months ago
curl -F "file=@index.html.gz;filename=index.htm.gz " http://192.168.1.123/upload

Thank you, but is it impossible to download with firmware? I mean, build a bin and flash it at ltchiptool. I have a configuration in the file and I need it when starting the firmware. And another question, have you tried to launch the FS Browser from ESP?

kuba2k2 commented 4 months ago

You can flash any binary image using ltchiptool, provided that you enter the correct starting offset. You will need to have a binary image of the LittleFS of the correct size.

Mit4el commented 4 months ago

correct size

Is it possible to automate this in the script ~/.platformio/platforms/libretiny/builder/utils/ltchiptool-util.py or create a separate script using the FLASH_USERDATA_OFFSET and FLASH_USERDATA_LENGTH data from the board settings. To create a bin file of the required size and submit it to ltchiptool. Unfortunately, I don't have enough knowledge of python((

Mit4el commented 4 months ago

I can probably use the mklittlefs tool from esp8266/esp32, and then ltchiptool and write everything in extra_scripts = pre:tools/fsbuildscript.py Or even add an env.AddPlatformTarget to make the button appear in platformio

Mit4el commented 3 months ago

I wrote a script for the building file system by bk7231n. It works if you register it in platformio.ini extra_scripts = pre:tools/this_script.py In folder $Project/tools add this_script.py and mklittlefs.exe I am not an expert in python, the code can be made better))

import subprocess
from os.path import basename, join, normpath
Import("env")
from platformio.platform.base import PlatformBase
from platformio.platform.board import PlatformBoardConfig
platform: PlatformBase = env.PioPlatform()

import subprocess
from os.path import basename, join, normpath
Import("env")
from platformio.platform.base import PlatformBase
from platformio.platform.board import PlatformBoardConfig
platform: PlatformBase = env.PioPlatform()

def build_ltfs():
    print("Current Data dir", env.get("PROJECT_DATA_DIR"))
    pathmk = env.get("PROJECT_DIR")+ "/tools/mklittlefs.exe"
    pathmk = normpath(pathmk).replace("\\", "/")
    print("Current tools dir", pathmk)
    FS_SIZE ="0x25000"
    FS_PAGE = "0x100"
    FS_BLOCK = "0x1000" 
    subprocess.call([pathmk, "-c", env.get("PROJECT_DATA_DIR"), "-s", FS_SIZE, "-p", FS_PAGE, "-b", FS_BLOCK, "lt_littlefs.bin", ])
    print("------------!!!!!!!!!-------------")
    print("FS_SIZE", FS_SIZE)
    print("FS_PAGE", FS_PAGE)
    print("FS_BLOCK", FS_BLOCK)
    print("------------!!!!!!!!!-------------") 

build_ltfs()

Upload through the gui ltchiptool it works))