As I’ve written in my previous post, I’ve been playing around with RISC-V recently. As part of building for RISC-V a compiler is required. As I am an ArchLinux user, I query pacman for the compiler. In the community repository I’ve found riscv64-elf-gcc. Additionaly, I need a C library, which is in the riscv32-elf-newlib package. While the compiler package says riscv64, it supports both 32 and 64 bit targets. The compiler invocation would be something like riscv64-elf-gcc -march=rv32imac -mabi=ilp32 -mcmodel=medlow

The compiler compiles my code, and gets me up to main(), however, function calls are broken. The parameters get garbled up. Using a different compiler solved this problem, it took me a while to figure that one out. As I has to install the OpenOCD version for the WCH, which is in a binary distribution (mounriver-studio-toolchain-riscv-bin in AUR) that also includes a compiler. Using that compiler, function calls work as expected.

Looking at the assembly I don’t see any obvious differences, apart from the GPIO_Init function linked at a different location.


# Compiled with riscv64-elf-gcc

49 GPIO_Init(GPIOA, &init_pin);
000002a4: 93 07 04 fd addi a5,s0,-48
000002a8: be 85 mv a1,a5
000002aa: b7 17 01 40 lui a5,0x40011
000002ae: 13 85 07 80 addi a0,a5,-2048 # 0x40010800
000002b2: dd 28 jal 0x3a8

GPIO_Init:
000003a8: 39 71 addi sp,sp,-64
000003aa: 22 de sw s0,60(sp)
000003ac: 80 00 addi s0,sp,64
000003ae: 23 26 a4 fc sw a0,-52(s0) # This messes the parameters up
000003b2: 23 24 b4 fc sw a1,-56(s0)

//----------------------------------------

# Compiled with riscv-none-embed-

49 GPIO_Init(GPIOA, &init_pin);
000002a4: 93 07 04 fd addi a5,s0,-48
000002a8: be 85 mv a1,a5
000002aa: b7 17 01 40 lui a5,0x40011
000002ae: 13 85 07 80 addi a0,a5,-2048 # 0x40010800
000002b2: d5 28 jal 0x3a6

GPIO_Init:
000003a6: 39 71 addi sp,sp,-64
000003a8: 22 de sw s0,60(sp)
000003aa: 80 00 addi s0,sp,64
000003ac: 23 26 a4 fc sw a0,-52(s0)
000003b0: 23 24 b4 fc sw a1,-56(s0)

So the problem must exist somewhere else. As the riscv-none-embed-gcc is version 8.2.0, from the version it claims to be the xPack distribution. Did MounRiver repack it? Nevertheless, the not-working version is gcc 11.1.0. And I would like to use a recent version of gcc. I’m not sure what is going on.

Edit 2022-05-20: It seems this is a problem with the debugger rather then with the application, as when I have code that configured a pin change interrupt, it gets triggered, regardless the parameters to the GPIO init function looks like garbage in the debugger.