Skip to content

Instantly share code, notes, and snippets.

@martinribelotta
Last active November 9, 2020 17:26
Show Gist options
  • Save martinribelotta/06f8a4d43286933902e27da4fd80d810 to your computer and use it in GitHub Desktop.
Save martinribelotta/06f8a4d43286933902e27da4fd80d810 to your computer and use it in GitHub Desktop.
Makefile for arm embedded
TARGET=firmware
SOURCES=$(wildcard src/*.c)
LIBPATH=lib
LIBS=nosys
LDSCRIPTS=link.ld
SPECS=rdimon
OUT=out
OBJECTS=$(addprefix $(OUT)/, $(patsubst %.c, %.o, $(SOURCES)))
TARGET_ELF=$(addsuffix .elf, $(addprefix $(OUT)/, $(TARGET)))
CROSS=arm-none-eabi-
CC=$(CROSS)gcc
LD=$(CROSS)gcc
ARCH_FLAGS=-mcpu=cortex-m3 -mthumb
CFLAGS=$(ARCH_FLAGS) -Og -g3
LDFLAGS=$(ARCH_FLAGS)
LDFLAGS+=-nostartfiles
LDFLAGS+=$(addprefix -L, $(LIBPATH))
LDFLAGS+=$(addprefix -l, $(LIBS))
LDFLAGS+=$(addprefix -T, $(LDSCRIPTS))
LDFLAGS+=$(addsuffix .specs, $(addprefix -specs=, $(SPECS)))
ifeq ($(VERBOSE),y)
Q=
else
Q=@
endif
all: $(TARGET_ELF)
$(OUT)/%.o: %.c
@echo CC $<
@mkdir -p $(dir $@)
$(Q)$(CC) $(CFLAGS) -o $@ -c $<
$(TARGET_ELF): $(OBJECTS)
@echo LD $@
@mkdir -p $(dir $@)
$(Q)$(LD) -o $@ $^ $(LDFLAGS)
clean:
@echo CLEAN
$(Q)rm -fr $(OUT)
.PHONY: all clean
OOCD=openocd
OOCD_FLAGS:=-f interface/stlink.cfg -f target/stm32f1x.cfg
OOCD_CMDS:=-c "init"
OOCD_CMDS+=-c "reset halt"
OOCD_CMDS+=-c "program $(TARGET_ELF) verify reset exit"
OOCD_RUN_CMDS:=-c "init"
OOCD_RUN_CMDS+=-c "reset halt"
OOCD_RUN_CMDS+=-c "program $(TARGET_ELF) verify"
OOCD_RUN_CMDS+=-c "reset halt"
OOCD_RUN_CMDS+=-c "arm semihosting enable"
OOCD_RUN_CMDS+=-c "echo ---------------------------------------------------"
OOCD_RUN_CMDS+=-c "resume"
program: $(TARGET_ELF)
$(Q)$(OOCD) $(OOCD_FLAGS) $(OOCD_CMDS)
run: $(TARGET_ELF)
$(Q)$(OOCD) -d0 $(OOCD_FLAGS) $(OOCD_RUN_CMDS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment