Skip to content

Commit

Permalink
GitHub Build Action: Automatically generate littlefs image
Browse files Browse the repository at this point in the history
If a data directory exists, the content of this directory will be placed in the littlefs image and embedded into the factory.bin file
  • Loading branch information
tbnobody committed Sep 26, 2024
1 parent 468cbad commit 67cae68
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
52 changes: 50 additions & 2 deletions pio-scripts/create_factory_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,64 @@

Import("env")

env = DefaultEnvironment()
platform = env.PioPlatform()

import sys
from os.path import join, getsize
import csv
import subprocess
import shutil
from os.path import join, getsize, exists, isdir
from os import listdir

sys.path.append(join(platform.get_package_dir("tool-esptoolpy")))
import esptool

def esp32_build_filesystem(fs_name, fs_size):
filesystem_dir = env.subst("$PROJECT_DATA_DIR")
print("Creating %dKiB filesystem with content:" % (int(fs_size, 0)/1024) )
if not isdir(filesystem_dir) or not listdir(filesystem_dir):
print("No files added -> will NOT create littlefs.bin and NOT overwrite fs partition!")
return False
# this does not work on GitHub, results in 'mklittlefs: No such file or directory'
tool = shutil.which(env.subst(env["MKFSTOOL"]))
if tool is None or not exists(tool):
print("Using fallback mklittlefs")
tool = "~/.platformio/packages/tool-mklittlefs/mklittlefs"

cmd = (tool, "-c", filesystem_dir, "-s", fs_size, fs_name)
returncode = subprocess.call(cmd, shell=False)
print("Return Code:", returncode)
return True

def esp32_create_combined_bin(source, target, env):
print("Generating combined binary for serial flashing")

# The offset from begin of the file where the app0 partition starts
# This is defined in the partition .csv file
app_offset = 0x10000
fs_offset = -1
fs_name = env.subst("$BUILD_DIR/littlefs.bin")

with open(env.BoardConfig().get("build.partitions")) as csv_file:
print("Read partitions from ", env.BoardConfig().get("build.partitions"))
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'{", ".join(row)}')
line_count += 1
else:
if (len(row) < 4):
continue
print(f'{row[0]} {row[1]} {row[2]} {row[3]} {row[4]}')
line_count += 1
if(row[0] == 'app0'):
app_offset = int(row[3], base=16)
elif(row[0] == 'spiffs'):
partition_size = row[4]
if esp32_build_filesystem(fs_name, partition_size):
fs_offset = int(row[3], base=16)

new_file_name = env.subst("$BUILD_DIR/${PROGNAME}.factory.bin")
sections = env.subst(env.get("FLASH_EXTRA_IMAGES"))
Expand Down Expand Up @@ -77,9 +121,13 @@ def esp32_create_combined_bin(source, target, env):
print(f" - {hex(app_offset)} | {firmware_name}")
cmd += [hex(app_offset), firmware_name]

if fs_offset != -1:
print(f" - {hex(fs_offset)} | {fs_name}")
cmd += [hex(fs_offset), fs_name]

print('Using esptool.py arguments: %s' % ' '.join(cmd))

esptool.main(cmd)


env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", esp32_create_combined_bin)
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", esp32_create_combined_bin)
2 changes: 2 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ custom_ci_action = generic,generic_esp32,generic_esp32s3,generic_esp32s3_usb

framework = arduino
platform = espressif32@6.8.1
platform_packages =
platformio/tool-mklittlefs

build_flags =
-DPIOENV=\"$PIOENV\"
Expand Down

0 comments on commit 67cae68

Please sign in to comment.