This is a follow on from
https://forum.lazarus.freepascal.org/index.php/topic,67252.msg517455The wiki page,
https://wiki.freepascal.org/Xtensa talks about the esp32 but the esp32-c3 is much more common now, substantially cheaper IMHO. (It too is marked as legacy, such is the market for such things).
The esp32-c3 is a different beast from the esp32, the -c3 gets you a riscv32 chip. The above mentioned wiki article suggests the use of espressif tool kit and model, and using their base C libraries to access the specialist functions these chips have. So, thought I'd give it a try. Sort of, works, with the disclaimer that my hardware has not arrived yet so not tested ! And my generated binaries are suspiciously small.
And some serious problems needing ugly workaround, better solutions would be very, very welcome !
Several stages -
Jump to second stage, I easily built what I think might be a esp32c3 compiler, based on fpc-main, dedicated to esp32-c3, I built a x86_64 in the conventional way first and then, the source directory (where the Makefile lives) -
$> make FPC=~/bin/FPC-esp32/fpc-3.3.1/bin/fpc CPU_TARGET=riscv32 OS_TARGET=freertos SUBARCH=rv32imc "CROSSOPT=-XPriscv32-esp-elf- -Cfsoft" all -j
$> cp compiler/ppcrossrv32 ../../fpc-3.3.1/bin/.
$> make crossinstall FPC="$FPC_DIR"/bin/ppcrossrv32 CPU_TARGET=riscv32 OS_TARGET=freertos SUBARCH=rv32imc INSTALL_PREFIX="$FPC_DIR"
That was easy, I ended up with a ppcrossrv32 binary and a whole lot of compiled units in ~/bin/FPC-esp32/fpc-3.3.1/units/riscv32-freertos/ that seemed to be what I expected -
$> file /home/dbannon/bin/FPC-esp32/fpc-3.3.1/units/riscv32-freertos/rtl/system.o
/home/dbannon/bin/FPC-esp32/fpc-3.3.1/units/riscv32-freertos/rtl/system.o: ELF 32-bit LSB relocatable, UCB RISC-V, RVC, soft-float ABI, version 1 (SYSV), not stripped
But after that, things get muddy. I could compile but not link. And the reason seems to be that the espressif supplied linker has some funny ideas how to use the FPC linker script. The compiler makes a nice linker script with absolute paths to all the necessary object files, but the the linker insists on prepending a path of its own devising. See tail end of a compile run (-va and -k" --verbose") here -
$> /home/dbannon/.espressif/tools/riscv32-esp-elf/esp-13.2.0_20230928/riscv32-esp-elf/bin/riscv32-esp-elf-ld -g --verbose --gc-sections -L. -o hello.elf -T link289829.res
GNU ld (crosstool-NG esp-13.2.0_20230928) 2.41
...
opened script file link289829.res
using external linker script:
==================================================
SEARCH_DIR("/home/dbannon/.espressif/tools/riscv32-esp-elf/esp-13.2.0_20230928/riscv32-esp-elf/riscv32-esp-elf/lib/")
SEARCH_DIR("/home/dbannon/bin/FPC-esp32/fpc-3.3.1/lib/fpc/3.3.1/units/riscv32-freertos/rtl/")
SEARCH_DIR("/home/dbannon/bin/FPC-esp32/fpc-3.3.1/bin/")
INPUT (
hello.o
/home/dbannon/bin/FPC-esp32/fpc-3.3.1/lib/fpc/3.3.1/units/riscv32-freertos/rtl/system.o
/home/dbannon/bin/FPC-esp32/fpc-3.3.1/lib/fpc/3.3.1/units/riscv32-freertos/rtl/esp32c3.o
/home/dbannon/bin/FPC-esp32/fpc-3.3.1/lib/fpc/3.3.1/units/riscv32-freertos/rtl/consoleio.o
/home/dbannon/bin/FPC-esp32/fpc-3.3.1/lib/fpc/3.3.1/units/riscv32-freertos/rtl/heapmgr.o
)
....
==================================================
/home/dbannon/.espressif/tools/riscv32-esp-elf/esp-13.2.0_20230928/riscv32-esp-elf/bin/riscv32-esp-elf-ld: mode elf32lriscv
attempt to open hello.o succeeded
hello.o
attempt to open /home/dbannon/.espressif/tools/riscv32-esp-elf/esp-13.2.0_20230928/riscv32-esp-elf/bin/../riscv32-esp-elf/home/dbannon/bin/FPC-esp32/fpc-3.3.1/lib/fpc/3.3.1/units/riscv32-freertos/rtl/system.o failed
/home/dbannon/.espressif/tools/riscv32-esp-elf/esp-13.2.0_20230928/riscv32-esp-elf/bin/riscv32-esp-elf-ld: cannot find /home/dbannon/bin/FPC-esp32/fpc-3.3.1/lib/fpc/3.3.1/units/riscv32-freertos/rtl/system.o: No such file or directory
attempt to open /home/dbannon/.espressif/tools/riscv32-esp-elf/esp-13.2.0_20230928/riscv32-esp-elf/bin/../riscv32-esp-elf/home/dbannon/bin/FPC-esp32/fpc-3.3.1/lib/fpc/3.3.1/units/riscv32-freertos/rtl/system.o failed
...
I could find no way to tell FPC to not supply the full path nor to tell tell the linker that the full path is there, use it ! So, in the end, I created a (crazy) directory structure below where the linker insists on looking with symlinks to the (few) needed files.
Thats was good, it build an elf file, but a very, very small one, 4688 bytes ??
Anyway, no .bin file because, after linking, FPC somehow calls
$> /home/dbannon/bin/esp/esp-idf//components/esptool_py/esptool/esptool.py --chip esp32c3 elf2image --flash_mode dio --flash_freq 80m --flash_size 4MB --elf-sha256-offset 0xb0 --min-rev 3 -o hello.bin hello.elf
But esptools is not executable, running it by hand (ie with python3) does make the hello.bin but its only 64bytes ?? That cannot be right but currently, no hardware! So, I wait ....
In summary, three issues
- Crazy Linker problem.
- FPC calls esptools (how does FPC know esptools even exits ?)
- Resulting binaries appear to be too small to be useful.
Any comments very welcome !