From 125e4b54c1381f3ee9ad4ced8d58fd521675b783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Tempel?= Date: Wed, 7 Oct 2020 07:37:52 +0000 Subject: [PATCH 1/2] fe310: Support the LLVM toolchain (i.e. compilation with clang) This requires -nostartfiles to be only passed to the linker, not the compiler, as it is a linker flag and passing it to the compiler causes a clang warning to be emitted. Additionally, clang does not seem to support `-mcmodel=medlow` and `-msmall-data-limit=8` but these options do not seem strictly necessary to me anyhow thus they are deactivated conditionally when using clang. --- cpu/riscv_common/Makefile.include | 2 ++ makefiles/arch/riscv.inc.mk | 13 ++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/cpu/riscv_common/Makefile.include b/cpu/riscv_common/Makefile.include index 5b29fc9f8cdb..8182f8dfe56f 100644 --- a/cpu/riscv_common/Makefile.include +++ b/cpu/riscv_common/Makefile.include @@ -1,6 +1,8 @@ CFLAGS += -Wno-pedantic INCLUDES += -I$(RIOTCPU)/riscv_common/include +TOOLCHAINS_SUPPORTED = gnu llvm + # All variables must be defined in the CPU configuration when using the common # `ldscripts/riscv.ld` ifneq (,$(ROM_START_ADDR)$(RAM_START_ADDR)$(ROM_LEN)$(RAM_LEN)) diff --git a/makefiles/arch/riscv.inc.mk b/makefiles/arch/riscv.inc.mk index 12f42cd64bae..b79a4fdc04a7 100644 --- a/makefiles/arch/riscv.inc.mk +++ b/makefiles/arch/riscv.inc.mk @@ -30,8 +30,15 @@ TARGET_ARCH_RISCV ?= \ TARGET_ARCH ?= $(TARGET_ARCH_RISCV) # define build specific options -CFLAGS_CPU = -march=rv32imac -mabi=ilp32 -mcmodel=medlow -msmall-data-limit=8 -CFLAGS_LINK = -nostartfiles -ffunction-sections -fdata-sections +CFLAGS_CPU = -march=rv32imac -mabi=ilp32 +ifeq ($(TOOLCHAIN),llvm) + # Always use riscv32-none-elf as target triple for clang, as some + # autodetected gcc target triples are incompatible with clang + TARGET_ARCH_LLVM := riscv32-none-elf +else + CFLAGS_CPU += -mcmodel=medlow -msmall-data-limit=8 +endif +CFLAGS_LINK = -ffunction-sections -fdata-sections CFLAGS_DBG ?= -g3 CFLAGS_OPT ?= -Os @@ -42,4 +49,4 @@ LINKFLAGS += -T$(LINKER_SCRIPT) CFLAGS += $(CFLAGS_CPU) $(CFLAGS_DBG) $(CFLAGS_OPT) $(CFLAGS_LINK) ASFLAGS += $(CFLAGS_CPU) $(CFLAGS_DBG) # export linker flags -LINKFLAGS += $(CFLAGS_CPU) $(CFLAGS_LINK) $(CFLAGS_DBG) $(CFLAGS_OPT) -Wl,--gc-sections -static -lgcc +LINKFLAGS += $(CFLAGS_CPU) $(CFLAGS_LINK) $(CFLAGS_DBG) $(CFLAGS_OPT) -nostartfiles -Wl,--gc-sections -static -lgcc From 5b15dc89329b9a88209368c7499c5d701c692f98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Tempel?= Date: Wed, 7 Oct 2020 19:07:23 +0000 Subject: [PATCH 2/2] fe310: Fix debug format strings read_csr() returns an unsigned long, not a uint32_t. This causes a -Wformat warning to be emitted when compiling with clang. This commit fixes the warning by changing the format string. --- cpu/riscv_common/irq_arch.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpu/riscv_common/irq_arch.c b/cpu/riscv_common/irq_arch.c index 18326586f9a7..5b3507a84f05 100644 --- a/cpu/riscv_common/irq_arch.c +++ b/cpu/riscv_common/irq_arch.c @@ -116,8 +116,8 @@ void handle_trap(uint32_t mcause) #ifdef DEVELHELP printf("Unhandled trap:\n"); printf(" mcause: 0x%" PRIx32 "\n", mcause); - printf(" mepc: 0x%" PRIx32 "\n", read_csr(mepc)); - printf(" mtval: 0x%" PRIx32 "\n", read_csr(mtval)); + printf(" mepc: 0x%lx\n", read_csr(mepc)); + printf(" mtval: 0x%lx\n", read_csr(mtval)); #endif /* Unknown trap */ core_panic(PANIC_GENERAL_ERROR, "Unhandled trap");