Makefile 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # Makefile for stm32-vserprog
  2. # * Simply make: will make the firmware for default board.
  3. # * Override toochain: make CROSS=/path/to/arm-none-eabi-
  4. # * Override UART for downloading: make SERIAL=/dev/ttyS1 flash
  5. # * Override hardware config: make BOARD=some_board_in_boards_folder
  6. # * Test and benchmark: make PSERIAL=/dev/ttyACM0 SPISPD=50000000 test (Upon failure please check both programmer and flash)
  7. # * Build for GD32 variants with 12MHz crystal: make EXTRA_CFLAGS=-DGD32F103
  8. ###############################################################################
  9. PROGRAM = stm32-vserprog
  10. CROSS ?= arm-none-eabi-
  11. SERIAL ?= /dev/ttyUSB0
  12. PSERIAL ?= /dev/ttyACM0
  13. SPISPD ?= 1000000000
  14. OBJS = vserprog.o \
  15. usbcdc.o \
  16. spi.o
  17. DOCS = README.html
  18. ###############################################################################
  19. CC = $(CROSS)gcc
  20. LD = $(CROSS)ld
  21. OBJCOPY = $(CROSS)objcopy
  22. OBJDUMP = $(CROSS)objdump
  23. SIZE = $(CROSS)size
  24. NM = $(CROSS)nm
  25. ELF = $(PROGRAM).elf
  26. BIN = $(PROGRAM).bin
  27. HEX = $(PROGRAM).hex
  28. MAP = $(PROGRAM).map
  29. DMP = $(PROGRAM).out
  30. # I suppose this variable is automatically setup by make/gmake
  31. #OS = $(shell uname -s)
  32. #ifeq ($(OS), Linux)
  33. # MAKE = make
  34. #else
  35. # MAKE = gmake
  36. #endif
  37. ifneq ($(wildcard last_board.mk),)
  38. include last_board.mk
  39. endif
  40. ifdef BOARD
  41. include boards/$(BOARD).mk
  42. endif
  43. all:
  44. ifndef BOARD
  45. @echo "Please specify a board by using '$(MAKE) BOARD=board'."
  46. @echo "Available boards:"
  47. @echo "===================="
  48. @ls boards/*.h | cut -d '/' -f 2 | cut -d '.' -f 1
  49. else
  50. ifndef LAST_BOARD
  51. @echo "First run, cleaning..."
  52. @$(MAKE) clean
  53. else
  54. ifneq ($(LAST_BOARD), $(BOARD))
  55. @echo "Board changed, cleaning..."
  56. @$(MAKE) clean
  57. endif
  58. endif
  59. @echo "LAST_BOARD = $(BOARD)" > last_board.mk
  60. @ln -sfT "boards/$(BOARD).h" board.h
  61. @$(MAKE) firmware
  62. endif
  63. CFLAGS += -O3 -Wall -g3 -gdwarf -std=gnu99
  64. #CFLAGS += -Os -Wall -g
  65. #CFLAGS += -Wextra -fprofile-generate -fprofile-use
  66. CFLAGS += -fno-common -ffunction-sections -fdata-sections -funit-at-a-time
  67. CFLAGS += -fgcse-sm -fgcse-las -fgcse-after-reload -funswitch-loops
  68. #CFLAGS += -funroll-loops -funsafe-loop-optimizations -fipa-pta -flto
  69. CFLAGS += $(ARCH_FLAGS) -Ilibopencm3/include/ $(EXTRA_CFLAGS)
  70. LIBC = $(shell $(CC) $(CFLAGS) --print-file-name=libc.a)
  71. LIBGCC = $(shell $(CC) $(CFLAGS) --print-libgcc-file-name)
  72. # LDPATH is required for libopencm3 ld scripts to work.
  73. LDPATH = libopencm3/lib/
  74. LDFLAGS += -L$(LDPATH) -T$(LDSCRIPT) -Map $(MAP) --gc-sections
  75. LDLIBS += $(LIBOPENCM3) $(LIBC) $(LIBGCC)
  76. .PHONY: firmware docs clean distclean flash flash-dfu reboot size symbols \
  77. test libopencm3-just-make
  78. firmware: $(BIN) $(HEX) $(DMP) size
  79. docs: $(DOCS)
  80. $(ELF): $(LDSCRIPT) $(OBJS) $(LIBOPENCM3)
  81. $(LD) -o $@ $(LDFLAGS) $(OBJS) $(LDLIBS)
  82. $(DMP): $(ELF)
  83. $(OBJDUMP) -d $< > $@
  84. %.hex: %.elf
  85. $(OBJCOPY) -S -O ihex $< $@
  86. %.bin: %.elf
  87. $(OBJCOPY) -S -O binary $< $@
  88. %.o: %.c board.h $(LIBOPENCM3) serprog.h flash.h
  89. $(CC) $(CFLAGS) -c $< -o $@
  90. %.html: %.md
  91. markdown $< > $@
  92. $(LIBOPENCM3):
  93. git submodule init
  94. git submodule update libopencm3
  95. CFLAGS="$(CFLAGS)" $(MAKE) -C libopencm3 $(OPENCM3_MK) PREFIX=$(patsubst %,%,$(CROSS)) V=1
  96. libopencm3-just-make:
  97. CFLAGS="$(CFLAGS)" $(MAKE) -C libopencm3 $(OPENCM3_MK) PREFIX=$(patsubst %,%,$(CROSS)) V=1
  98. flashrom/flashrom:
  99. git submodule init
  100. git submodule update flashrom
  101. $(MAKE) -C flashrom
  102. clean:
  103. rm -f $(OBJS) $(DOCS) $(ELF) $(HEX) $(BIN) $(MAP) $(DMP) board.h last_board.mk
  104. distclean: clean
  105. $(MAKE) -C libopencm3 clean
  106. $(MAKE) -C flashrom distclean
  107. rm -f *~ *.swp *.hex *.bin
  108. flash-uart: $(HEX)
  109. stm32flash -w $< -v $(SERIAL)
  110. flash-dfu: $(BIN)
  111. dfu-util -a 0 -d 0483:df11 -s 0x08000000:leave -D $<
  112. flash-stlink: $(HEX)
  113. st-flash --reset --format ihex write $<
  114. reboot:
  115. stm32flash -g 0x0 $(SERIAL)
  116. size: $(PROGRAM).elf
  117. @echo ""
  118. @$(SIZE) $(PROGRAM).elf
  119. @echo ""
  120. symbols: $(ELF)
  121. @$(NM) --demangle --size-sort -S $< | grep -v ' [bB] '
  122. # Erasing must come first, otherwise some sectors might be skipped.
  123. # After testing you may clear flash contents manually.
  124. test: flashrom/flashrom
  125. @echo ""; \
  126. FFLAGS="-p serprog:dev=$(PSERIAL),spispeed=$(SPISPD)"; \
  127. echo "Detecting flash..."; \
  128. FOUT=`command time -f 'XXTIMEXX %e' $< $${FFLAGS} 2>&1`; \
  129. FPART=`echo "$${FOUT}" | grep "Found" | grep -oP '\".*\"' | sed -e 's/"//g'`; \
  130. FSIZE=`echo "$${FOUT}" | grep -oP '[0-9]+ kB' | sed -e 's/ kB//g'`; \
  131. PTIME=`echo "$${FOUT}" | grep 'XXTIMEXX' | sed -e 's/XXTIMEXX //g'`; \
  132. echo "Generating test file..."; \
  133. dd if=/dev/urandom iflag=fullblock of=rand.bin bs=1024 count=$${FSIZE} 2>/dev/null; \
  134. echo "Erasing..."; \
  135. ETIME=`command time -f 'XXTIMEXX %e' $< $${FFLAGS} -E 2>&1 >/dev/null | grep 'XXTIMEXX' | sed -e 's/XXTIMEXX //g'`; \
  136. echo -n "Verifying... "; \
  137. $< $${FFLAGS} -r compare.bin 1>/dev/null 2>/dev/null; \
  138. STR_VERIFY=`sed 's/\xff//g' compare.bin | hd`; \
  139. if test -z "$${STR_VERIFY}"; \
  140. then echo "PASS"; \
  141. else echo "FAIL"; \
  142. exit 1; \
  143. fi; \
  144. echo "Writing..."; \
  145. WTIME=`command time -f 'XXTIMEXX %e' $< $${FFLAGS} -w rand.bin 2>&1 >/dev/null | grep 'XXTIMEXX' | sed -e 's/XXTIMEXX //g'`; \
  146. echo "Reading..."; \
  147. RTIME=`command time -f 'XXTIMEXX %e' $< $${FFLAGS} -r compare.bin 2>&1 >/dev/null | grep 'XXTIMEXX' | sed -e 's/XXTIMEXX //g'`; \
  148. echo -n "Comparing..."; \
  149. CKSUMS=`md5sum rand.bin compare.bin | cut -d ' ' -f 1 | uniq | wc -l`; \
  150. if test "$${CKSUMS}" = "1"; \
  151. then echo "PASS"; \
  152. else echo "FAIL"; \
  153. fi; \
  154. echo ""; \
  155. echo "Flash type: $${FPART}"; \
  156. echo "Flash size: $${FSIZE} KiB"; \
  157. echo "SPI speed : $(SPISPD) Hz (requested)"; \
  158. printf 'Probe : %6.2fs\n' "$${PTIME}"; \
  159. printf 'Erase : %6.2fs\n' "$${ETIME}"; \
  160. printf 'Write : %6.2fs\n' "$${WTIME}"; \
  161. printf 'Read : %6.2fs\n' "$${RTIME}"; \
  162. echo ""; \
  163. rm rand.bin compare.bin;