lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1 | # 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 | |
| 7 | GENHDRS = uart_regs.h gpio_regs.h spi_device_regs.h flash_ctrl_regs.h |
| 8 | |
| 9 | PROGRAM ?= boot_rom |
| 10 | PROGRAM_CFLAGS = -Wall -g -Os |
| 11 | ifeq ($(SIM),1) |
| 12 | PROGRAM_CFLAGS += -DSIMULATION |
| 13 | endif |
| 14 | ARCH = rv32imc |
| 15 | # ARCH = rv32im # to disable compressed instructions |
| 16 | SRCS = uart.c gpio.c spi_device.c flash_ctrl.c bootstrap.c $(PROGRAM).c |
| 17 | RV_TOOLS ?= /tools/riscv/bin |
| 18 | |
| 19 | |
| 20 | CC = ${RV_TOOLS}/riscv32-unknown-elf-gcc |
| 21 | |
| 22 | OBJCOPY ?= $(subst gcc,objcopy,$(wordlist 1,1,$(CC))) |
| 23 | OBJDUMP ?= $(subst gcc,objdump,$(wordlist 1,1,$(CC))) |
| 24 | REGTOOL = ../../util/regtool.py |
| 25 | |
| 26 | LINKER_SCRIPT ?= link.ld |
| 27 | CRT ?= crt0.S |
| 28 | CFLAGS ?= -march=$(ARCH) -mabi=ilp32 -static -mcmodel=medany \ |
| 29 | -fvisibility=hidden -nostdlib -nostartfiles $(PROGRAM_CFLAGS) |
| 30 | |
| 31 | OBJS := ${SRCS:.c=.o} ${CRT:.S=.o} |
| 32 | DEPS = $(OBJS:%.o=%.d) |
| 33 | |
| 34 | OUTFILES = $(PROGRAM).elf $(PROGRAM).vmem $(PROGRAM).bin $(PROGRAM).dis $(PROGRAM).map |
| 35 | |
| 36 | all: $(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 |
| 64 | GENHDR_BASE = $(subst _regs.h,,$(GENHDRS)) |
| 65 | define 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 |
| 74 | endef |
| 75 | $(foreach f,$(GENHDR_BASE),$(eval $(call header_gen,$f))) |
| 76 | |
| 77 | -include $(DEPS) |
| 78 | |
| 79 | clean: |
| 80 | $(RM) -f *.o *.d *.bin *.vmem *.dis *.elf *.map $(GENHDRS) |
| 81 | |
| 82 | distclean: clean |
| 83 | $(RM) -f $(OUTFILES) |