|
| 1 | +# Makefile skeleton adapted from Peter Harrison's - www.micromouse.com |
| 2 | + |
| 3 | +# MCU name and submodel |
| 4 | +MCU = cortex-m3 |
| 5 | +SUBMDL = stm32f103 |
| 6 | + |
| 7 | +# toolchain (using code sourcery now) |
| 8 | +TCHAIN = arm-none-eabi |
| 9 | +THUMB = -mthumb |
| 10 | +THUMB_IW = -mthumb-interwork |
| 11 | + |
| 12 | +# Target file name (without extension). |
| 13 | +BUILDDIR = build |
| 14 | +TARGET = $(BUILDDIR)/maple_boot |
| 15 | + |
| 16 | +ST_LIB = stm32_lib |
| 17 | +ST_USB = usb_lib |
| 18 | + |
| 19 | +# Optimization level [0,1,2,3,s] |
| 20 | +OPT ?= 0 |
| 21 | +DEBUG = |
| 22 | +#DEBUG = dwarf-2 |
| 23 | + |
| 24 | +INCDIRS = ./$(ST_LIB) ./$(ST_USB) |
| 25 | + |
| 26 | +CFLAGS = $(DEBUG) |
| 27 | +CFLAGS += -O$(OPT) |
| 28 | +CFLAGS += -ffunction-sections -fdata-sections |
| 29 | +CFLAGS += -Wall -Wimplicit |
| 30 | +CFLAGS += -Wcast-align |
| 31 | +CFLAGS += -Wpointer-arith -Wswitch |
| 32 | +CFLAGS += -Wredundant-decls -Wreturn-type -Wshadow -Wunused |
| 33 | +CFLAGS += -Wa,-adhlns=$(BUILDDIR)/$(subst $(suffix $<),.lst,$<) |
| 34 | +CFLAGS += $(patsubst %,-I%,$(INCDIRS)) |
| 35 | + |
| 36 | +# Aeembler Flags |
| 37 | +ASFLAGS = -Wa,-adhlns=$(BUILDDIR)/$(<:.s=.lst)#,--g$(DEBUG) |
| 38 | + |
| 39 | +LDFLAGS = -nostartfiles -Wl,-Map=$(TARGET).map,--cref,--gc-sections |
| 40 | +LDFLAGS += -lc -lgcc |
| 41 | + |
| 42 | +# Set the linker script |
| 43 | +LDFLAGS +=-T$(ST_LIB)/c_only_md.ld |
| 44 | + |
| 45 | +# Define programs and commands. |
| 46 | +SHELL = sh |
| 47 | +CC = $(TCHAIN)-gcc |
| 48 | +CPP = $(TCHAIN)-g++ |
| 49 | +AR = $(TCHAIN)-ar |
| 50 | +OBJCOPY = $(TCHAIN)-objcopy |
| 51 | +OBJDUMP = $(TCHAIN)-objdump |
| 52 | +SIZE = $(TCHAIN)-size |
| 53 | +NM = $(TCHAIN)-nm |
| 54 | +REMOVE = rm -f |
| 55 | +REMOVEDIR = rm -r |
| 56 | +COPY = cp |
| 57 | + |
| 58 | +# Define Messages |
| 59 | +# English |
| 60 | +MSG_ERRORS_NONE = Errors: none |
| 61 | +MSG_BEGIN = "-------- begin --------" |
| 62 | +MSG_ETAGS = Created TAGS File |
| 63 | +MSG_END = -------- end -------- |
| 64 | +MSG_SIZE_BEFORE = Size before: |
| 65 | +MSG_SIZE_AFTER = Size after: |
| 66 | +MSG_FLASH = Creating load file for Flash: |
| 67 | +MSG_EXTENDED_LISTING = Creating Extended Listing: |
| 68 | +MSG_SYMBOL_TABLE = Creating Symbol Table: |
| 69 | +MSG_LINKING = Linking: |
| 70 | +MSG_COMPILING = Compiling C: |
| 71 | +MSG_ASSEMBLING = Assembling: |
| 72 | +MSG_CLEANING = Cleaning project: |
| 73 | + |
| 74 | +# Combine all necessary flags and optional flags. |
| 75 | +# Add target processor to flags. |
| 76 | +GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d |
| 77 | +ALL_CFLAGS = -mcpu=$(MCU) $(THUMB_IW) -I. $(CFLAGS) $(GENDEPFLAGS) |
| 78 | +ALL_ASFLAGS = -mcpu=$(MCU) $(THUMB_IW) -I. -x assembler-with-cpp $(ASFLAGS) |
| 79 | + |
| 80 | +# --------------------------------------------- # |
| 81 | +# file management |
| 82 | +ASRC = $(ST_LIB)/c_only_startup.s $(ST_LIB)/cortexm3_macro.s |
| 83 | + |
| 84 | +STM32SRCS = |
| 85 | + |
| 86 | +_STM32USBSRCS = usb_regs.c \ |
| 87 | +usb_int.c \ |
| 88 | +usb_init.c \ |
| 89 | +usb_core.c \ |
| 90 | +usb_mem.c |
| 91 | + |
| 92 | +STM32USBSRCS = $(patsubst %, $(ST_USB)/%,$(_STM32USBSRCS)) |
| 93 | + |
| 94 | +SRCS = usb.c usb_callbacks.c usb_descriptor.c main.c hardware.c dfu.c |
| 95 | + |
| 96 | + |
| 97 | +SRC = $(SRCS) $(STM32SRCS) $(STM32USBSRCS) |
| 98 | + |
| 99 | +# Define all object files. |
| 100 | +_COBJ = $(SRC:.c=.o) |
| 101 | +_AOBJ = $(ASRC:.s=.o) |
| 102 | +COBJ = $(patsubst %, $(BUILDDIR)/%,$(_COBJ)) |
| 103 | +AOBJ = $(patsubst %, $(BUILDDIR)/%,$(_AOBJ)) |
| 104 | + |
| 105 | +# Define all listing files. |
| 106 | +_LST = $(ASRC:.s=.lst) |
| 107 | +_LST += $(SRC:.c=.lst) |
| 108 | +LST = $(patsubst %, $(BUILDDIR)/%,$(_LST)) |
| 109 | + |
| 110 | +# Display size of file. |
| 111 | +HEXSIZE = $(SIZE) --target=binary $(TARGET).hex |
| 112 | +ELFSIZE = $(SIZE) -A $(TARGET).elf |
| 113 | + |
| 114 | +# go! |
| 115 | +all: begin gccversion build sizeafter finished end |
| 116 | +build: elf bin lss sym |
| 117 | + |
| 118 | +bin: $(TARGET).bin |
| 119 | +elf: $(TARGET).elf |
| 120 | +lss: $(TARGET).lss |
| 121 | +sym: $(TARGET).sym |
| 122 | +dfu: $(TARGET).bin |
| 123 | + sudo dfu-util -d 0110:1001 -a 0 -D $(TARGET).bin |
| 124 | + |
| 125 | +begin: |
| 126 | + mkdir -p build/stm32_lib |
| 127 | + mkdir -p build/usb_lib |
| 128 | + @echo -- |
| 129 | + @echo $(MSG_BEGIN) |
| 130 | + @echo $(COBJ) |
| 131 | + |
| 132 | +finished: |
| 133 | + @echo $(MSG_ERRORS_NONE) |
| 134 | +tags: |
| 135 | + etags `find . -name "*.c" -o -name "*.cpp" -o -name "*.h"` |
| 136 | + @echo $(MSG_ETAGS) |
| 137 | +end: |
| 138 | + @echo $(MSG_END) |
| 139 | + @echo |
| 140 | +sizeafter: |
| 141 | + @if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi |
| 142 | +gccversion: |
| 143 | + @$(CC) --version |
| 144 | + |
| 145 | +program: |
| 146 | + @echo "Flash-programming with OpenOCD" |
| 147 | + cp $(TARGET).bin flash/tmpflash.bin |
| 148 | + cd flash && openocd -f flash.cfg |
| 149 | + |
| 150 | +program_serial: |
| 151 | + @echo "Flash-programming with stm32loader.py" |
| 152 | + ./flash/stm32loader.py -p /dev/ttyUSB0 -evw build/maple_boot.bin |
| 153 | + |
| 154 | +debug: $(TARGET).bin |
| 155 | + @echo "Flash-programming with OpenOCD - DEBUG" |
| 156 | + cp $(TARGET).bin flash/tmpflash.bin |
| 157 | + cd flash && openocd -f debug.cfg |
| 158 | + |
| 159 | +install: $(TARGET).bin |
| 160 | + cp $(TARGET).bin build/main.bin |
| 161 | + openocd -f flash/perry_flash.cfg |
| 162 | + |
| 163 | +run: $(TARGET).bin |
| 164 | + openocd -f flash/run.cfg |
| 165 | + |
| 166 | +# Create final output file (.hex) from ELF output file. |
| 167 | +%.hex: %.elf |
| 168 | + @echo |
| 169 | + @echo $(MSG_FLASH) $@ |
| 170 | + $(OBJCOPY) -O binary $< $@ |
| 171 | + |
| 172 | +# Create final output file (.bin) from ELF output file. |
| 173 | +%.bin: %.elf |
| 174 | + @echo |
| 175 | + @echo $(MSG_FLASH) $@ |
| 176 | + $(OBJCOPY) -O binary $< $@ |
| 177 | + |
| 178 | + |
| 179 | +# Create extended listing file from ELF output file. |
| 180 | +# testing: option -C |
| 181 | +%.lss: %.elf |
| 182 | + @echo |
| 183 | + @echo $(MSG_EXTENDED_LISTING) $@ |
| 184 | + $(OBJDUMP) -h -S -D $< > $@ |
| 185 | + |
| 186 | + |
| 187 | +# Create a symbol table from ELF output file. |
| 188 | +%.sym: %.elf |
| 189 | + @echo |
| 190 | + @echo $(MSG_SYMBOL_TABLE) $@ |
| 191 | + $(NM) -n $< > $@ |
| 192 | + |
| 193 | + |
| 194 | +# Link: create ELF output file from object files. |
| 195 | +.SECONDARY : $(TARGET).elf |
| 196 | +.PRECIOUS : $(COBJ) $(AOBJ) |
| 197 | + |
| 198 | +%.elf: $(COBJ) $(AOBJ) |
| 199 | + @echo |
| 200 | + @echo $(MSG_LINKING) $@ |
| 201 | + $(CC) $(THUMB) $(ALL_CFLAGS) $(AOBJ) $(COBJ) --output $@ $(LDFLAGS) |
| 202 | + |
| 203 | +# Compile: create object files from C source files. ARM/Thumb |
| 204 | +$(COBJ) : $(BUILDDIR)/%.o : %.c |
| 205 | + @echo |
| 206 | + @echo $(MSG_COMPILING) $< |
| 207 | + $(CC) -c $(THUMB) $(ALL_CFLAGS) $< -o $@ |
| 208 | + |
| 209 | +# Assemble: create object files from assembler source files. ARM/Thumb |
| 210 | +$(AOBJ) : $(BUILDDIR)/%.o : %.s |
| 211 | + @echo |
| 212 | + @echo $(MSG_ASSEMBLING) $< |
| 213 | + $(CC) -c $(THUMB) $(ALL_ASFLAGS) $< -o $@ |
| 214 | + |
| 215 | +clean: begin clean_list finished end |
| 216 | + |
| 217 | +clean_list : |
| 218 | + @echo |
| 219 | + @echo $(MSG_CLEANING) |
| 220 | + $(REMOVE) $(TARGET).hex |
| 221 | + $(REMOVE) $(TARGET).bin |
| 222 | + $(REMOVE) $(TARGET).obj |
| 223 | + $(REMOVE) $(TARGET).elf |
| 224 | + $(REMOVE) $(TARGET).map |
| 225 | + $(REMOVE) $(TARGET).obj |
| 226 | + $(REMOVE) $(TARGET).a90 |
| 227 | + $(REMOVE) $(TARGET).sym |
| 228 | + $(REMOVE) $(TARGET).lnk |
| 229 | + $(REMOVE) $(TARGET).lss |
| 230 | + $(REMOVE) $(COBJ) |
| 231 | + $(REMOVE) $(AOBJ) |
| 232 | + $(REMOVE) $(LST) |
| 233 | + $(REMOVE) flash/tmpflash.bin |
| 234 | +# $(REMOVE) $(SRC:.c=.s) |
| 235 | +# $(REMOVE) $(SRC:.c=.d) |
| 236 | + $(REMOVE) .dep/* |
| 237 | + |
| 238 | +# Include the dependency files. |
| 239 | +-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*) |
| 240 | + |
| 241 | + |
| 242 | +# Listing of phony targets. |
| 243 | +.PHONY : all begin finish tags end sizeafter gccversion \ |
| 244 | +build elf hex bin lss sym clean clean_list program cscope |
| 245 | + |
| 246 | +cscope: |
| 247 | + rm -rf *.cscope |
| 248 | + find . -iname "*.[hcs]" | grep -v examples | xargs cscope -R -b |
| 249 | + |
0 commit comments