blob: 961d98e4da0f4d7057b8d4d81ee1314d0ab1acf6 [file] [log] [blame]
lowRISC Contributors802543a2019-08-31 12:12:56 +01001# Copyright lowRISC contributors.
2# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3# SPDX-License-Identifier: Apache-2.0
4#
5# Generate a baremetal application for the microcontroller
6
7GENHDRS = uart_regs.h gpio_regs.h spi_device_regs.h flash_ctrl_regs.h
8
9PROGRAM ?= boot_rom
10PROGRAM_CFLAGS = -Wall -g -Os
11ifeq ($(SIM),1)
12 PROGRAM_CFLAGS += -DSIMULATION
13endif
14ARCH = rv32imc
15# ARCH = rv32im # to disable compressed instructions
16SRCS = uart.c gpio.c spi_device.c flash_ctrl.c bootstrap.c $(PROGRAM).c
17RV_TOOLS ?= /tools/riscv/bin
18
19
20CC = ${RV_TOOLS}/riscv32-unknown-elf-gcc
21
22OBJCOPY ?= $(subst gcc,objcopy,$(wordlist 1,1,$(CC)))
23OBJDUMP ?= $(subst gcc,objdump,$(wordlist 1,1,$(CC)))
24REGTOOL = ../../util/regtool.py
25
26LINKER_SCRIPT ?= link.ld
27CRT ?= crt0.S
28CFLAGS ?= -march=$(ARCH) -mabi=ilp32 -static -mcmodel=medany \
29 -fvisibility=hidden -nostdlib -nostartfiles $(PROGRAM_CFLAGS)
30
31OBJS := ${SRCS:.c=.o} ${CRT:.S=.o}
32DEPS = $(OBJS:%.o=%.d)
33
34OUTFILES = $(PROGRAM).elf $(PROGRAM).vmem $(PROGRAM).bin $(PROGRAM).dis $(PROGRAM).map
35
36all: $(OUTFILES)
37
38$(PROGRAM).elf: $(OBJS) $(LINKER_SCRIPT)
39 $(CC) $(CFLAGS) -T $(LINKER_SCRIPT) $(OBJS) -o $@ $(LIBS) -Xlinker -Map=${PROGRAM}.map
40
41%.dis: %.elf
42 $(OBJDUMP) -SD $^ > $@
43
44# Note: this target requires the srecord package to be installed.
45# XXX: This could be replaced by objcopy once
46# https://sourceware.org/bugzilla/show_bug.cgi?id=19921
47# is merged.
48# XXX: Currently the start address 0x1000 is hardcoded. It could/should be
49# read from the elf file, but is lost in the bin file.
50# Switching to objcopy will resolve that as well.
51%.vmem: %.bin
52 srec_cat $^ -binary -offset 0x0 -byte-swap 4 -o $@ -vmem
53
54%.bin: %.elf
55 $(OBJCOPY) -O binary $^ $@
56
57%.o: %.c
58 $(CC) $(CFLAGS) -MMD -c $(INCS) -o $@ $<
59
60%.o: %.S
61 $(CC) $(CFLAGS) -MMD -c $(INCS) -o $@ $<
62
63# Can't find a way to use % twice in RHS, this based on stackoverflow
64GENHDR_BASE = $(subst _regs.h,,$(GENHDRS))
65define header_gen
66 $1.c: $1_regs.h
67
68 $1_regs.h: ../../hw/ip/$1/doc/*.hjson
69 if [ -f ../../hw/ip/$1/doc/$1.hjson ]; then \
70 $(REGTOOL) -D -o $1_regs.h ../../hw/ip/$1/doc/$1.hjson ; \
71 else \
72 $(REGTOOL) -D -o $1_regs.h ../../hw/ip/$1/doc/$1_reg.hjson ; \
73 fi
74endef
75$(foreach f,$(GENHDR_BASE),$(eval $(call header_gen,$f)))
76
77-include $(DEPS)
78
79clean:
80 $(RM) -f *.o *.d *.bin *.vmem *.dis *.elf *.map $(GENHDRS)
81
82distclean: clean
83 $(RM) -f $(OUTFILES)