Skip to content

Commit 4371944

Browse files
committed
Add support for new filesystems
Resolves platformio#546, resolves platformio#570, resolves platformio#643
1 parent 126c176 commit 4371944

File tree

3 files changed

+90
-39
lines changed

3 files changed

+90
-39
lines changed

builder/main.py

Lines changed: 71 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -119,34 +119,40 @@ def _update_max_upload_size(env):
119119

120120

121121
def _to_unix_slashes(path):
122-
return path.replace('\\', '/')
122+
return path.replace("\\", "/")
123123

124124

125125
#
126-
# SPIFFS helpers
126+
# Filesystem helpers
127127
#
128128

129129

130-
def fetch_spiffs_size(env):
131-
spiffs = None
130+
def fetch_fs_size(env):
131+
fs = None
132132
for p in _parse_partitions(env):
133-
if p['type'] == "data" and p['subtype'] == "spiffs":
134-
spiffs = p
135-
if not spiffs:
133+
if p["type"] == "data" and p["subtype"] in ("spiffs", "fat"):
134+
fs = p
135+
if not fs:
136136
sys.stderr.write(
137-
"Could not find the `spiffs` section in the partitions "
137+
"Could not find the any filesystem section in the partitions "
138138
"table %s\n" % env.subst("$PARTITIONS_TABLE_CSV")
139139
)
140140
env.Exit(1)
141141
return
142-
env["SPIFFS_START"] = _parse_size(spiffs['offset'])
143-
env["SPIFFS_SIZE"] = _parse_size(spiffs['size'])
144-
env["SPIFFS_PAGE"] = int("0x100", 16)
145-
env["SPIFFS_BLOCK"] = int("0x1000", 16)
142+
env["FS_START"] = _parse_size(fs["offset"])
143+
env["FS_SIZE"] = _parse_size(fs["size"])
144+
env["FS_PAGE"] = int("0x100", 16)
145+
env["FS_BLOCK"] = int("0x1000", 16)
146146

147+
# FFat specific offsets, see:
148+
# https://github.com/lorol/arduino-esp32fatfs-plugin#notes-for-fatfs
149+
if filesystem == "fatfs":
150+
env["FS_START"] += 4096
151+
env["FS_SIZE"] -= 4096
147152

148-
def __fetch_spiffs_size(target, source, env):
149-
fetch_spiffs_size(env)
153+
154+
def __fetch_fs_size(target, source, env):
155+
fetch_fs_size(env)
150156
return (target, source)
151157

152158

@@ -156,6 +162,7 @@ def __fetch_spiffs_size(target, source, env):
156162
board = env.BoardConfig()
157163
mcu = board.get("build.mcu", "esp32")
158164
toolchain_arch = "xtensa-%s" % mcu
165+
filesystem = board.get("build.filesystem", "spiffs")
159166
if mcu == "esp32c3":
160167
toolchain_arch = "riscv32-esp"
161168

@@ -186,9 +193,25 @@ def __fetch_spiffs_size(target, source, env):
186193
],
187194
ERASECMD='"$PYTHONEXE" "$OBJCOPY" $ERASEFLAGS erase_flash',
188195

189-
MKSPIFFSTOOL="mkspiffs_${PIOPLATFORM}_" + ("espidf" if "espidf" in env.subst(
190-
"$PIOFRAMEWORK") else "${PIOFRAMEWORK}"),
191-
ESP32_SPIFFS_IMAGE_NAME=env.get("ESP32_SPIFFS_IMAGE_NAME", "spiffs"),
196+
# mkspiffs package contains two different binaries for IDF and Arduino
197+
MKFSTOOL="mk%s" % filesystem
198+
+ (
199+
(
200+
"_${PIOPLATFORM}_"
201+
+ (
202+
"espidf"
203+
if "espidf" in env.subst("$PIOFRAMEWORK")
204+
else "${PIOFRAMEWORK}"
205+
)
206+
)
207+
if filesystem == "spiffs"
208+
else ""
209+
),
210+
# Legacy `ESP32_SPIFFS_IMAGE_NAME` is used as the second fallback value for
211+
# backward compatibility
212+
ESP32_FS_IMAGE_NAME=env.get(
213+
"ESP32_FS_IMAGE_NAME", env.get("ESP32_SPIFFS_IMAGE_NAME", filesystem)
214+
),
192215
ESP32_APP_OFFSET="0x10000",
193216

194217
PROGSUFFIX=".elf"
@@ -205,9 +228,9 @@ def __fetch_spiffs_size(target, source, env):
205228
BUILDERS=dict(
206229
ElfToBin=Builder(
207230
action=env.VerboseAction(" ".join([
208-
'"$PYTHONEXE" "$OBJCOPY"',
231+
'"$PYTHONEXE" "$OBJCOPY"',
209232
"--chip", mcu,
210-
"elf2image",
233+
"elf2image",
211234
"--flash_mode", "$BOARD_FLASH_MODE",
212235
"--flash_freq", "${__get_board_f_flash(__env__)}",
213236
"--flash_size", board.get("upload.flash_size", "detect"),
@@ -216,41 +239,51 @@ def __fetch_spiffs_size(target, source, env):
216239
suffix=".bin"
217240
),
218241
DataToBin=Builder(
219-
action=env.VerboseAction(" ".join([
220-
'"$MKSPIFFSTOOL"',
221-
"-c", "$SOURCES",
222-
"-p", "$SPIFFS_PAGE",
223-
"-b", "$SPIFFS_BLOCK",
224-
"-s", "$SPIFFS_SIZE",
225-
"$TARGET"
226-
]), "Building SPIFFS image from '$SOURCES' directory to $TARGET"),
227-
emitter=__fetch_spiffs_size,
242+
action=env.VerboseAction(
243+
" ".join(
244+
['"$MKFSTOOL"', "-c", "$SOURCES", "-s", "$FS_SIZE"]
245+
+ (
246+
[
247+
"-p",
248+
"$FS_PAGE",
249+
"-b",
250+
"$FS_BLOCK",
251+
]
252+
if filesystem in ("spiffs", "littlefs")
253+
else []
254+
)
255+
+ ["$TARGET"]
256+
),
257+
"Building FS image from '$SOURCES' directory to $TARGET",
258+
),
259+
emitter=__fetch_fs_size,
228260
source_factory=env.Dir,
229-
suffix=".bin"
230-
)
261+
suffix=".bin",
262+
),
231263
)
232264
)
233265

234266
if not env.get("PIOFRAMEWORK"):
235267
env.SConscript("frameworks/_bare.py", exports="env")
236268

237269
#
238-
# Target: Build executable and linkable firmware or SPIFFS image
270+
# Target: Build executable and linkable firmware or FS image
239271
#
240272

241273
target_elf = None
242274
if "nobuild" in COMMAND_LINE_TARGETS:
243275
target_elf = join("$BUILD_DIR", "${PROGNAME}.elf")
244276
if set(["uploadfs", "uploadfsota"]) & set(COMMAND_LINE_TARGETS):
245-
fetch_spiffs_size(env)
246-
target_firm = join("$BUILD_DIR", "${ESP32_SPIFFS_IMAGE_NAME}.bin")
277+
fetch_fs_size(env)
278+
target_firm = join("$BUILD_DIR", "${ESP32_FS_IMAGE_NAME}.bin")
247279
else:
248280
target_firm = join("$BUILD_DIR", "${PROGNAME}.bin")
249281
else:
250282
target_elf = env.BuildProgram()
251283
if set(["buildfs", "uploadfs", "uploadfsota"]) & set(COMMAND_LINE_TARGETS):
252284
target_firm = env.DataToBin(
253-
join("$BUILD_DIR", "${ESP32_SPIFFS_IMAGE_NAME}"), "$PROJECT_DATA_DIR")
285+
join("$BUILD_DIR", "${ESP32_FS_IMAGE_NAME}"), "$PROJECT_DATA_DIR"
286+
)
254287
env.NoCache(target_firm)
255288
AlwaysBuild(target_firm)
256289
else:
@@ -286,7 +319,7 @@ def __fetch_spiffs_size(target, source, env):
286319
)
287320

288321
#
289-
# Target: Upload firmware or SPIFFS image
322+
# Target: Upload firmware or FS image
290323
#
291324

292325
upload_protocol = env.subst("$UPLOAD_PROTOCOL")
@@ -398,13 +431,13 @@ def __fetch_spiffs_size(target, source, env):
398431
debug_tools.get(upload_protocol).get("server").get("arguments", []))
399432
openocd_args.extend([
400433
"-c", "adapter_khz %s" % env.GetProjectOption("debug_speed", "5000"),
401-
"-c",
434+
"-c",
402435
"program_esp {{$SOURCE}} %s verify" %
403436
board.get("upload.offset_address", "$ESP32_APP_OFFSET"),
404437
])
405438
for image in env.get("FLASH_EXTRA_IMAGES", []):
406439
openocd_args.extend([
407-
"-c",
440+
"-c",
408441
'program_esp {{%s}} %s verify' %
409442
(_to_unix_slashes(image[1]), image[0])
410443
])
@@ -417,7 +450,7 @@ def __fetch_spiffs_size(target, source, env):
417450
for f in openocd_args
418451
]
419452
env.Replace(UPLOADER="openocd",
420-
UPLOADERFLAGS=openocd_args,
453+
UPLOADERFLAGS=openocd_args,
421454
UPLOADCMD="$UPLOADER $UPLOADERFLAGS")
422455
upload_actions = [env.VerboseAction("$UPLOADCMD", "Uploading $SOURCE")]
423456

platform.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@
138138
"owner": "platformio",
139139
"version": "~2.230.0"
140140
},
141+
"tool-mklittlefs": {
142+
"type": "uploader",
143+
"optional": true,
144+
"owner": "platformio",
145+
"version": "~1.203.0"
146+
},
147+
"tool-mkfatfs": {
148+
"type": "uploader",
149+
"optional": true,
150+
"owner": "platformio",
151+
"version": "~2.0.0"
152+
},
141153
"tool-cmake": {
142154
"optional": true,
143155
"owner": "platformio",

platform.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ def configure_default_packages(self, variables, targets):
3535
frameworks = variables.get("pioframework", [])
3636

3737
if "buildfs" in targets:
38-
self.packages["tool-mkspiffs"]["optional"] = False
38+
filesystem = variables.get("board_build.filesystem", "spiffs")
39+
if filesystem == "littlefs":
40+
self.packages["tool-mklittlefs"]["optional"] = False
41+
elif filesystem == "fatfs":
42+
self.packages["tool-mkfatfs"]["optional"] = False
43+
else:
44+
self.packages["tool-mkspiffs"]["optional"] = False
3945
if variables.get("upload_protocol"):
4046
self.packages["tool-openocd-esp32"]["optional"] = False
4147
if os.path.isdir("ulp"):

0 commit comments

Comments
 (0)