Forum > Embedded

ESP32-C3 riscv32

(1/12) > >>

dbannon:
This is a follow on from https://forum.lazarus.freepascal.org/index.php/topic,67252.msg517455

The 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 -
[x] Install the espressif tool kit, it has some system dependencies, a git tree and then a hidden directory containg some generated stuff. Pretty easy. Walks you through building an example binary. https://docs.espressif.com/projects/esp-idf/en/stable/esp32c3/index.html
[x] Building a riscv32 FPC compiler and rtl. Based on https://wiki.freepascal.org/Xtensa
[x] Building a Hello World FPC app - and here there be dragons !

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) -


--- Code: ---$> 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"
--- End code ---

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 -


--- Code: ---$> 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
--- End code ---

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 -


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---$> /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.resGNU ld (crosstool-NG esp-13.2.0_20230928) 2.41...opened script file link289829.resusing 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 elf32lriscvattempt to open hello.o succeededhello.oattempt 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 directoryattempt 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

--- Code: ---$> /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
--- End code ---

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
[x] Crazy Linker problem.
[x] FPC calls esptools (how does FPC know esptools even exits ?)
[x] Resulting binaries appear to be too small to be useful.

Any comments very welcome !


MiR:
ccrause is definitely the expert here...

but have you already checked if the linker code in ./compiler/systems/t_freertos.pas is correctly configured? There are currently two possible defines that may be active, XTENSA and RISCV32 and in the end it may be that you need code from both to get proper linking.
likely XTENSA is not set at all but it should contain all the required linker settings that will likely have to go into the RISCV32 code sections.

This may explain why your result is so small, currently close to nothing is defined in the linker script for RISCV32

ccrause:
I have started testing the build process for esp32c3.  There appears to be a few missing pieces in the compiler for handling the interaction with the esp-idf SDK for riscv32.  This is required for correct linking of the final binary image.  I will update this thread once I have a fix, or at least a better grasp of what is going on.

ccrause:

--- Quote from: dbannon on May 16, 2024, 07:42:53 am ---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.

--- End quote ---
Consider testing with Espressif's qemu fork for esp32c3.  This is especially useful for testing logic (as opposed to interacting with hardware).  It should output write(ln) statements, at least it does for esp32.

Fibonacci:
Would love to code these bad boys in FPC ;D

Navigation

[0] Message Index

[#] Next page

Go to full version