CH32V arduino support issue

Hello,

I’m playing with arduino integration on the CH32V003 and I’m getting an error when I declare a mathematical function like sin().

Linking .pio/build/ch32v003f4p6_evt_r0/firmware.elf
/Users/xxxxxx/.platformio/packages/toolchain-riscv/bin/../lib/gcc/riscv-none-embed/8.2.0/../../../../riscv-none-embed/bin/ld: .pio/build/ch32v003f4p6_evt_r0/src/main.cpp.o: in function `loop':
main.cpp:(.text.loop+0x3e): undefined reference to `sinf'
platformio.ini

[env:ch32v003f4p6_evt_r0]
platform = ch32v
framework = arduino
monitor_speed = 115200
board = ch32v003f4p6_evt_r0

Any idea of the problem ?

Thank you.

Floating point functions? On a 16KB flash, 2K RAM device? I don’t think that’s what you’d want at all. But indeed the compiler should at least attemp to include them, and then tell you it overflowed the available flash space. Let’s see what the compiler is doing wrong…

Have you already tried build_flags = -lmath to link in the compiler’s math library?

Floating point functions? On a 16KB flash, 2K RAM device? I don’t think that’s what you’d want at all.

Absolutely, I was just trying to output a sin value on a serial plotter.

When I add build_flags = -lmath I get the error :

/.platformio/packages/toolchain-riscv/bin/…/lib/gcc/riscv-none-embed/8.2.0/…/…/…/…/riscv-none-embed/bin/ld: cannot find -lmath

Thank you for your answer.

When I use the OpenWCH core with

[env]
platform = ch32v
framework = arduino
monitor_speed = 115200
; use OpenWCH core impelmetnation
board_build.core = openwch
; uncomment this to use USB bootloader upload via WCHISP
;upload_protocol = isp

[env:ch32v003f4p6_evt_r0]
board = ch32v003f4p6_evt_r0

and the source file

#include <Arduino.h>

static float curAngle = 0.0f;

void setup() {
  Serial.begin(115200);
}
void loop() {
  // print current angle (3 decimal places) and sine of that
  String line = String(curAngle, 3) + "," + String(sin(curAngle), 3);
  // forward angle
  curAngle += 0.1f;
  delay(100);
}

it finds the function but overflows the flash by a whole 8KB.

Building in release mode
riscv-none-embed-g++ -o .pio\build\ch32v003f4p6_evt_r0\firmware.elf -T C:\Users\Max\.platformio\packages\framework-arduino-openwch-ch32\system\CH32V00x\SRC\Ld\Link.ld -march=rv32ecxw -mabi=ilp32e 
-msmall-data-limit=8 -msave-restore -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common -Wl,--gc-sections --specs=nosys.specs --specs=nano.specs -nostartfiles -Wl,-Map="C:\Users\Max\.platformio\platforms\ch32v\examples\blinky-arduino\.pio\build\ch32v003f4p6_evt_r0\firmware.map" .pio\build\ch32v003f4p6_evt_r0\FrameworkArduinoVariant\PeripheralPins.c.o .pio\build\ch32v003f4p6_evt_r0\FrameworkArduinoVariant\variant_CH32V003F4.cpp.o .pio\build\ch32v003f4p6_evt_r0\src\main.cpp.o -L.pio\build\ch32v003f4p6_evt_r0 -Wl,--start-group -lprintf -Wl,--whole-archive .pio\build\ch32v003f4p6_evt_r0\libFrameworkArduino.a -Wl,--no-whole-archive -lc -Wl,--end-group
c:/users/max/.platformio/packages/toolchain-riscv@src-300bd9bd04afe68c198406fcbb913040/bin/../lib/gcc/riscv-none-embed/8.2.0/../../../../riscv-none-embed/bin/ld.exe: .pio\build\ch32v003f4p6_evt_r0\firmware.elf section `.text' will not fit in region `FLASH'
c:/users/max/.platformio/packages/toolchain-riscv@src-300bd9bd04afe68c198406fcbb913040/bin/../lib/gcc/riscv-none-embed/8.2.0/../../../../riscv-none-embed/bin/ld.exe: region `FLASH' overflowed by 8816 bytes
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\ch32v003f4p6_evt_r0\firmware.elf] Error 1

The same code on the other (current standard but soon to be changed) core, without board_build.core = openwch, throws more linker errors.

Yes, I already tried to use the OpenWCH core and I get the same error.

The way I see it,

→ not possible without programming your own floating point lib, or using a better one, or at least sine function, using e.g. fixed point math.

You may get some ideas for it in https://www.youtube.com/watch?v=hffgNRfL1XY or https://www.youtube.com/watch?v=xFKFoGiGlXQ or https://github.com/pulp-platform/RVfplib.

Edit: the RVfplib is actually uncompilable for -march=rv32ecxw -mabi=ilp32e (what ch32v003 uses), it relies on rv32imc instructions. The core doesn’t have that.

Thank you very much for this clear answer and the provided link :+1: