Slave( STM32) and Master (ESP32) how to build and package binaries and data

I’ve designed a Board with an STM32 as I/O, Analog IO and real-time Processor as well as an ESP32 as Connectivity and networking platform. I set up a platformio project for each of them.

I want to be able to automatically build and package the STM32 binary into the littlefs filesystem and upload everything in one block, first over serial, later over the air. The ESP32 is able to flash the STM its firmware with the integrated bootloader.

How do I setup the project to build the 2 binaries in one go and package the STM binary into the data/littlefs partition?

You can use Advaned Scripting to pre-hook the action where the filesystem binary (spiffs.bin) would be build.

In that hook you can then build the other project using pio run with the appropriate directory and environment name, and finally copy the resulting firmware.bin file into your own data/ folder.

I tested this in a folder where the ESP32 and an STM32 project live side-by-side. The STM32 project folder is called bluepill_clock_test with the environment bluepill_f103c8 that I want to build.

Thus I set up my ESP32 project to do

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
extra_scripts = package_fw.py

whereas the package_fw.py in the root directory of the project would read

import shutil
Import("env")

def before_spiffs_build(source, target, env):
    print("Chance to do something here..")
    # harcoded build comment for the project folder relative to this project folder
    # use known environment name
    ret = env.Execute("pio run -d ../bluepill_clock_test -e bluepill_f103c8")
    print("Build ok: " + str(ret))
    if int(ret) == 0: 
        #build was OK, we can copy the firmware..
        try:
            shutil.copyfile("../bluepill_clock_test/.pio/build/bluepill_f103c8/firmware.bin", "data/stm32fw.bin")
            print("File copied OK.")
        except Exception as exc:
            print("Error while copying! " + str(exc))
    else: 
        print("Not copying anything, build failed..")

# custom action before building SPIFFS image. For example, compress HTML, etc.
env.AddPreAction("$BUILD_DIR/spiffs.bin", before_spiffs_build)

You can see that it works if I then use the project task “Build filesystem image”. Within there, it starts another build process, copies the firmware and then the resulting image is built with that firmware.

Processing esp32dev (platform: espressif32; board: esp32dev; framework: arduino)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
PLATFORM: Espressif 32 (3.0.0) > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
 - framework-arduinoespressif32 3.10004.210126 (1.0.4)
 - tool-esptoolpy 1.30000.201119 (3.0.0)
 - tool-mkspiffs 2.230.0 (2.30)
 - toolchain-xtensa32 2.50200.80 (5.2.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 29 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
before_spiffs_build([".pio\build\esp32dev\spiffs.bin"], ["data"])
Chance to do something here..
pio run -d ../bluepill_clock_test -e bluepill_f103c8
Processing bluepill_f103c8 (platform: ststm32; board: bluepill_f103c8; framework: stm32cube)
--------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/bluepill_f103c8.html
PLATFORM: ST STM32 (11.0.0) > BluePill F103C8
HARDWARE: STM32F103C8T6 72MHz, 20KB RAM, 64KB Flash
DEBUG: Current (stlink) External (blackmagic, cmsis-dap, jlink, stlink)
PACKAGES:
 - framework-stm32cubef1 1.8.3
 - tool-ldscripts-ststm32 0.1.0
 - toolchain-gccarmnoneeabi 1.70201.0 (7.2.1)
Warning! Cannot find a linker script for the required board! An auto-generated script will be used to link firmware!
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 28 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Checking size .pio\build\bluepill_f103c8\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   2.6% (used 524 bytes from 20480 bytes)
Flash: [==        ]  23.6% (used 15440 bytes from 65536 bytes)
========================= [SUCCESS] Took 1.63 seconds =========================
Build ok: 0
File copied OK.
Building SPIFFS image from 'data' directory to .pio\build\esp32dev\spiffs.bin
/stm32fw.bin
/test.txt

Notice that i’ve used SPIFFS here since that what is supported by default.

For LittleFS you would need extra scripts and libraries (LITTLEFS/examples/LITTLEFS_PlatformIO at master · lorol/LITTLEFS · GitHub).

Wow, such a fast and detailed reply even with useful links and code examples after just 15mins, thank you very much. I will now try this approach. How could I get you a coffe?

@flip @maxgerhardt Hi
I am new to stm32, I am using STM32G070RBT6 board. I am trying to keep stm32 as slave and ESP32 S3 C1 devkit as master. I am unable to do. Can you give me any input regarding this.

Thanks in advance.