Recent

Author Topic: ESP32-C3 riscv32  (Read 9984 times)

dbannon

  • Hero Member
  • *****
  • Posts: 3156
    • tomboy-ng, a rewrite of the classic Tomboy
Re: ESP32-C3 riscv32
« Reply #30 on: May 19, 2024, 02:15:31 pm »
Sorry, I posted over you.

.... Now to find a way to convince the linker to pick the newlib version...

That ROM code appears to be reproduced .espressif/tools/esp-rom-elfs/20220823/esp32c3_rev3_rom.elf

It has a date stamp and rev number so, is likely to be different in different hardware during a release cycle. Does not sound very reliable ....

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

ccrause

  • Hero Member
  • *****
  • Posts: 970
Re: ESP32-C3 riscv32
« Reply #31 on: May 19, 2024, 02:24:08 pm »
Unfortunately there are two versions, one defined in newlib and one that is included in the on chip ROM , defined in esp32c3.rom.newlib-time.ld. The non-working version links to the ROM version of _isatty_r, so this appears to not work correctly. Now to find a way to convince the linker to pick the newlib version...

As a work-around I have removed the sp32c3.rom.newlib-time.ld from fpc's list of scripts passed to the linker. Not sure this is the proper general solution, but at least now the startup process runs into the pascal $main function, then hangs in FPC_INIT_FUNC_TABLE.

But this is good news, the problem now lies in the fpc domain, so conceptually a bit easier to tackle.

ccrause

  • Hero Member
  • *****
  • Posts: 970
Re: ESP32-C3 riscv32
« Reply #32 on: May 19, 2024, 02:27:56 pm »
Sorry, I posted over you.

.... Now to find a way to convince the linker to pick the newlib version...

That ROM code appears to be reproduced .espressif/tools/esp-rom-elfs/20220823/esp32c3_rev3_rom.elf

It has a date stamp and rev number so, is likely to be different in different hardware during a release cycle. Does not sound very reliable ....

Davo
Thanks Davo, I was going to search for that image at some point.  Luckily I can work around that particular problem for now.

ccrause

  • Hero Member
  • *****
  • Posts: 970
Re: ESP32-C3 riscv32
« Reply #33 on: May 19, 2024, 02:45:53 pm »
Yep, makes sense, I prefer not to edit the fpc.cfg file, I have been building with a script that offers all that content on a fpc command line, easier IMHO to manage.
Yes, there are many ways to supply information to the compiler, so do whatever fits in best with your workflow.

Quote
But I still find myself missing symbols esp_app_get_elf_sha256, esp_partition_main_flash_region_safe, esp_app_get_description

Easy to look them up in the map file you mention and find the .a file they live in. And easy to manually add then to the link file and it all builds. But thats no solution. How can I tell FPC about needing them ? It finds all the other object archives it needs ?

The list of libraries required to link a minimal project depends on the ep-idf version.  This is handled by having secondary units with the version dependent list of esp-idf libraries, e.g. esp32c3idf_50000.pp.  FPC will select the applicable unit based on the esp-idf version specified via -WP.

Quote
What are your plans with "packaging" this ?  Its a labourous process installing IDF and its associated tool, it appears to me it would be legal to just cherry pick all the needed libraries and put them 'somewhere' suitable but how about the assembler and linker ?  Oh, and that esptool.py thing ....
Michael Ring is working on a script that does exactly this, packaging the esp-idf libraries, fragments, python tools, bintools and a python environment. This is used with fpcupdeluxe at the moment, so the end user just selects the target in fpcupdeluxe, then all the required files are just copied across. Of course fpcupdeluxe only support esp8266 (v3.4) and esp32 (v4.3) at the moment, but this will change as the situation stabilizes.

Quote
And should that process be based on the 5.2.1 stable release ?
I'm working with esp-idf v5.0.6 at the moment, since it appeared to correspond with what was in FPC when I started. Once I get the platform working, it is relatively easy to cover other esp-idf versions.

Edit: Added link to Michael's post of espsdk4fpc
« Last Edit: May 19, 2024, 02:50:03 pm by ccrause »

MiR

  • Sr. Member
  • ****
  • Posts: 274
Re: ESP32-C3 riscv32
« Reply #34 on: May 19, 2024, 04:36:25 pm »
Christo, I built fpc now based on your branch, I am now struggeling with this:

~/fpcupdeluxe/fpc/bin/aarch64-darwin/ppcrossrv32 -Tfreertos -XP/Users/tgdrimi9/devel/espsdk4fpc/5.0.6/esp32c6/riscv32-binutils-aarch64-darwin/bin/riscv32-esp-elf- -O1 -WpESP32C3 hello -vt -Fu~/fpcupdeluxe/fpc/units/riscv32-freertos/rtl

/Users/tgdrimi9/devel/espsdk4fpc/5.0.6/esp32c6/riscv32-binutils-aarch64-darwin/bin/riscv32-esp-elf-ld: cannot open linker script file esp32c3_out.ld: No such file or directory

Is this file hand crafted by you or is this sections.ld from the sdk?

MiR

  • Sr. Member
  • ****
  • Posts: 274
Re: ESP32-C3 riscv32
« Reply #35 on: May 19, 2024, 04:40:20 pm »
.. guess I need to set -WP but not sure why this still fails:

~/fpcupdeluxe/fpc/bin/aarch64-darwin/ppcrossrv32 -Tfreertos -XP/Users/tgdrimi9/devel/espsdk4fpc/5.0.6/esp32c6/riscv32-binutils-aarch64-darwin/bin/riscv32-esp-elf- -O1 -WpESP32C3 hello -vt -Fu~/fpcupdeluxe/fpc/units/riscv32-freertos/rtl -WP5.0.6 -Fl~/devel/espsdk4fpc/5.0.6/esp32c3/riscv32-libs/riscv32/

/Users/tgdrimi9/devel/espsdk4fpc/5.0.6/esp32c6/riscv32-binutils-aarch64-darwin/bin/riscv32-esp-elf-ld: cannot open linker script file esp32c3.rom.ld: No such file or directory

Getting closer:

~/fpcupdeluxe/fpc/bin/aarch64-darwin/ppcrossrv32 -Tfreertos -XP/Users/tgdrimi9/devel/espsdk4fpc/5.0.6/esp32c6/riscv32-binutils-aarch64-darwin/bin/riscv32-esp-elf- -O1 -WpESP32C3 hello -vt -Fu~/fpcupdeluxe/fpc/units/riscv32-freertos/rtl -WP5.0.6 -Fl~/devel/espsdk4fpc/5.0.6/esp32c3/riscv32-libs/riscv32/ -Fl~/devel/espsdk4fpc/5.0.6/esp32c3/riscv32-binutils-aarch64-darwin/esp-idf-5.0.6/components/esp_rom/esp32c3/ld/ -Fl~/devel/espsdk4fpc/5.0.6/esp32c6/riscv32-binutils-aarch64-darwin/esp-idf-5.0.6/components/soc/esp32c3/ld/ -Fl~/devel/espsdk4fpc/tools-5.0.6/tools/riscv32-esp-elf/esp-2022r1-11.2.0/riscv32-esp-elf/riscv32-esp-elf/lib/rv32imc/ilp32/ -Fl~/devel/espsdk4fpc/tools-5.0.6/tools/riscv32-esp-elf/esp-2022r1-11.2.0/riscv32-esp-elf/lib/gcc/riscv32-esp-elf/11.2.0/rv32imc/ilp32/ -Fl~/devel/espsdk4fpc/5.0.6/esp32c3/riscv32-libs/riscv32/release/

but now I have multiple defines between newlib.a and libc.a (Tons of them)

libnewlib.a(locks.c.obj): in function `__retarget_lock_init':
newlib/locks.c:276: multiple definition of `__retarget_lock_init';
lib/rv32imc/ilp32/libc.a(lib_a-lock.o): first defined here
« Last Edit: May 19, 2024, 05:11:12 pm by MiR »

ccrause

  • Hero Member
  • *****
  • Posts: 970
Re: ESP32-C3 riscv32
« Reply #36 on: May 19, 2024, 05:29:35 pm »
cannot open linker script file esp32c3_out.ld: No such file or directory
Specify the esp-idf version with -WP.  The above name is for esp-idf before v4.4.

Edit:
Quote
Is this file hand crafted by you or is this sections.ld from the sdk?
It is sections.ld from the SDK since esp-idf v4.4.
« Last Edit: May 19, 2024, 05:38:11 pm by ccrause »

ccrause

  • Hero Member
  • *****
  • Posts: 970
Re: ESP32-C3 riscv32
« Reply #37 on: May 19, 2024, 05:34:16 pm »
but now I have multiple defines between newlib.a and libc.a (Tons of them)

libnewlib.a(locks.c.obj): in function `__retarget_lock_init':
newlib/locks.c:276: multiple definition of `__retarget_lock_init';
lib/rv32imc/ilp32/libc.a(lib_a-lock.o): first defined here
Please post the linker script from fpc (linkxxx.res). I managed to get past this by moving the standard C libraries to the end of the library list.

MiR

  • Sr. Member
  • ****
  • Posts: 274
Re: ESP32-C3 riscv32
« Reply #38 on: May 19, 2024, 06:25:16 pm »
Here you go

Isn't newlib a replacement for libc?
« Last Edit: May 19, 2024, 06:27:42 pm by MiR »

ccrause

  • Hero Member
  • *****
  • Posts: 970
Re: ESP32-C3 riscv32
« Reply #39 on: May 19, 2024, 06:42:47 pm »
Isn't newlib a replacement for libc?
Espressif builds the standard toolchain libc based on newlib, then also use a different newlib in esp-idf. So linking order seems to be critical to correctly chain the dependencies. At least that is my impression.

Note the SDK build uses the no-rtti libs:
tools/riscv32-esp-elf/esp-2022r1-11.2.0/riscv32-esp-elf/riscv32-esp-elf/lib/rv32imc/ilp32/no-rtti/
tools/riscv32-esp-elf/esp-2022r1-11.2.0/riscv32-esp-elf/lib/gcc/riscv32-esp-elf/11.2.0/rv32imc/ilp32/no-rtti/

Compared to your linker file search paths:
tools/riscv32-esp-elf/esp-2022r1-11.2.0/riscv32-esp-elf/riscv32-esp-elf/lib/rv32imc/ilp32/
tools/riscv32-esp-elf/esp-2022r1-11.2.0/riscv32-esp-elf/lib/gcc/riscv32-esp-elf/11.2.0/rv32imc/ilp32/

ccrause

  • Hero Member
  • *****
  • Posts: 970
Re: ESP32-C3 riscv32
« Reply #40 on: May 19, 2024, 07:33:37 pm »
Progress!

Code: Pascal  [Select][+][-]
  1. program hello;
  2.  
  3. procedure vTaskDelay(xTicksToDelay: uint32); external;
  4. function gpio_reset_pin(gpio_num: uint32): uint32; external;
  5. function gpio_set_direction(gpio_num, mode: uint32): uint32; external;
  6. function gpio_set_level(gpio_num, level: uint32): uint32; external;
  7.  
  8. const
  9.   LED = 10; // DFRobot Beetle ESP32-C3 board
  10.   OUTPUT = 1 shl 1;
  11.  
  12. begin
  13.   gpio_reset_pin(LED);
  14.   gpio_set_direction(LED, OUTPUT);
  15.   repeat
  16.     WriteLn('Hello from FPC!');
  17.     gpio_set_level(LED, 1);
  18.     vTaskDelay(75);
  19.     gpio_set_level(LED, 0);
  20.     vTaskDelay(25);
  21.   until false;
  22. end.

Compile:
Code: [Select]
$ ~/fpc/installs/lib/fpc/3.3.1/ppcrossrv32 -Tfreertos -Wpesp32c3 -WP5.0.6 -vhwi hello.pp
Hint: Start of reading config file /home/christo/.fpc.cfg
Hint: End of reading config file /home/christo/.fpc.cfg
Target OS: FreeRTOS
Compiling hello.pp
Assembling hello
Linking hello
esptool.py v4.7.0
Creating esp32c3 image...
Merged 1 ELF section
Successfully created esp32c3 image.
24 lines compiled, 2.1 sec, 436 bytes code, 45 bytes data
2 hint(s) issued

Upload:
Code: [Select]
$ ~/fpc/xtensa/esp-idf-5.0.6/components/esptool_py/esptool/esptool.py -p /dev/ttyACM0 -b 460800 --before default_reset --after hard_reset --chip esp32c3  write_flash --flash_mode dio --flash_size detect --flash_freq 40m 0x10000 hello.bin
esptool.py v4.7.0
Serial port /dev/ttyACM0
Connecting...
Chip is ESP32-C3 (QFN32) (revision v0.3)
Features: WiFi, BLE, Embedded Flash 4MB (XMC)
Crystal is 40MHz
MAC: 84:f7:03:41:63:20
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Flash will be erased from 0x00010000 to 0x0003bfff...
Compressed 179488 bytes to 94964...
Wrote 179488 bytes (94964 compressed) at 0x00010000 in 1.7 seconds (effective 853.6 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

LED is blinking, I don't feel like posting a video.  Serial output:
Code: [Select]
I (225) heap_init: At 50000010 len 00001FD8 (7 KiB): RTCRAM
I (232) spi_flash: detected chip: generic
I (236) spi_flash: flash io: dio
W (240) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (254) app_start: Starting scheduler on CPU0
I (258) main_task: Started on CPU0
I (258) main_task: Calling app_main()
I (258) gpio: GPIO[10]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
Hello from FPC!
Hello from FPC!
Hello from FPC!
Hello from FPC!
Hello from FPC!
Hello from FPC!

MiR

  • Sr. Member
  • ****
  • Posts: 274
Re: ESP32-C3 riscv32
« Reply #41 on: May 19, 2024, 08:00:04 pm »
Gratulations!

Would you mind sending me output of

find . -type f -exec md5sum {} \;

and your linker file?

Then I can check what's different for me....
« Last Edit: May 19, 2024, 08:35:20 pm by MiR »

ccrause

  • Hero Member
  • *****
  • Posts: 970
Re: ESP32-C3 riscv32
« Reply #42 on: May 19, 2024, 08:48:31 pm »
Gratulations!

Would you mind sending me output of

find . -type f -exec md5sum {} \;

and your linker file?

Then I can check what's different for me....

Attached a linker file for the above program.  I ran the find command in the toolchain library folders:
Code: [Select]
/.espressif/tools/riscv32-esp-elf/esp-2022r1-11.2.0/riscv32-esp-elf/riscv32-esp-elf/lib/rv32imc/ilp32/no-rtti$ find . -type f -exec md5sum {} \;
51d7005d49d8c7b7040915e72180f56a  ./libm.a
bce0cd2e439edb19315d4a2bec8c5098  ./libnosys.a
0f2e66e53f9a139c8d9fcba1d5c32a55  ./semihost.specs
ce20bdc490b21f797baaf645d2147ecc  ./crt0.o
d08dd7b55a76d589a44b30ce93cd0af0  ./default.specs
a855456e73dc690d6b55a25151b70d2b  ./libsupc++.a
16d1f0a025fb66ed9439eb003deebc26  ./libsim.a
0c60d9947506a3ef279005db64ac15a0  ./sim.specs
8efe348cdde3362419670a2bfba81d33  ./libgloss.a
deef8236100e6e6e5ed87a4980e5c9bf  ./libstdc++.a-gdb.py
69f260c542f13ddf0355e30db1b39a87  ./nosys.specs
91ba9a643886d0dfd3587a92e3f8766b  ./libc_nano.a
f387bc33370f3be0d57dd884e355ce94  ./libg.a
4b9a9f1c669f766f761c4dbd17120a83  ./libm_nano.a
f387bc33370f3be0d57dd884e355ce94  ./libc.a
91ba9a643886d0dfd3587a92e3f8766b  ./libg_nano.a
f5b8c852015817240910cf899e67ce12  ./libsemihost.a
3bd403a3d6abe958b2bfd2a045dda8b1  ./nano.specs
782a7efe7c535fd192b38a2c8a155450  ./libstdc++.a
and
Code: [Select]
~/.espressif/tools/riscv32-esp-elf/esp-2022r1-11.2.0/riscv32-esp-elf/lib/gcc/riscv32-esp-elf/11.2.0/rv32imc/ilp32/no-rtti$ find . -type f -exec md5sum {} \;
685c71247615ed4907a4555e4d40febc  ./crtn.o
bd1a5b35b847d6aaffe4425d7ab3042c  ./libgcc.a
255b930e2d689a52f0c880e1b6061ac4  ./libgcov.a
a8ff41a27e98dc8041a7ac76b8bd35da  ./crtbegin.o
bc16f85b1c2de39cbda2f4255cfa30ba  ./crtend.o
685c71247615ed4907a4555e4d40febc  ./crti.o


MiR

  • Sr. Member
  • ****
  • Posts: 274
Re: ESP32-C3 riscv32
« Reply #43 on: May 20, 2024, 08:13:00 pm »
tgdrimi9@ubuntu:~/devel/fpc-esp32c3/test$ ./c
Free Pascal Compiler version 3.3.1 [2024/05/19] for riscv32
Copyright (c) 1993-2024 by Florian Klaempfl and others
Target OS: FreeRTOS
Compiling hello.pas
Assembling hello
Linking hello
(1023) IDF_PATH is /home/tgdrimi9/devel/espsdk4fpc/5.0.6/esp32c3/riscv32-binutils-amd64-linux/esp-idf-5.0.6
esptool.py v4.7.0
Creating esp32c3 image...
Merged 1 ELF section
Successfully created esp32c3 image.


.. found my issue, please pull espsdk4fpc again...

ccrause

  • Hero Member
  • *****
  • Posts: 970
Re: ESP32-C3 riscv32
« Reply #44 on: May 20, 2024, 10:10:52 pm »
Michael, hopefully I can test your script tomorrow. Could you test that your compiled program is functional?

 

TinyPortal © 2005-2018