Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makefile Improvements #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# compilation results #
#######################
*.o
*.a
*.bin
*.elf
*.hex
*.lst
*.map

# Makefile target files #
#########################
dirs
program

# Eclipse & editor files #
#########################
.project
.cproject
*~

# Log files #
#############
openocd.log
19 changes: 13 additions & 6 deletions Libraries/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CFLAGS = -g -O2 -Wall
CFLAGS += -mlittle-endian -mthumb -mcpu=cortex-m0 -march=armv6s-m
CFLAGS += -ffreestanding -nostdlib
CFLAGS += -includestm32f0xx_conf.h -ICMSIS/Include -ICMSIS/Device/ST/STM32F0xx/Include -ISTM32F0xx_StdPeriph_Driver/inc

CFLAGS += -I../inc

SRCS = stm32f0xx_adc.c stm32f0xx_cec.c stm32f0xx_comp.c stm32f0xx_crc.c \
stm32f0xx_dac.c stm32f0xx_dbgmcu.c stm32f0xx_dma.c stm32f0xx_exti.c \
Expand All @@ -18,17 +18,24 @@ SRCS = stm32f0xx_adc.c stm32f0xx_cec.c stm32f0xx_comp.c stm32f0xx_crc.c \
stm32f0xx_spi.c stm32f0xx_syscfg.c stm32f0xx_tim.c \
stm32f0xx_usart.c stm32f0xx_wwdg.c

OBJS = $(SRCS:.c=.o)
OBJS = $(addprefix objs/,$(SRCS:.c=.o))
DEPS = $(addprefix deps/,$(SRCS:.c=.d))

.PHONY: libstm32f0.a
.PHONY: all clean

all: libstm32f0.a

%.o : %.c
$(CC) $(CFLAGS) -c -o $@ $^
-include $(DEPS)

dirs:
mkdir -p deps objs
touch dirs

objs/%.o : %.c dirs
$(CC) $(CFLAGS) -c -o $@ $< -MMD -MF deps/$(*F).d

libstm32f0.a: $(OBJS)
$(AR) -r $@ $(OBJS)

clean:
rm -f $(OBJS) libstm32f0.a
rm -f dirs $(OBJS) $(DEPS) libstm32f0.a
36 changes: 26 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# put your *.o targets here, make should handle the rest!
# put your *.c source files here, make should handle the rest!
SRCS = main.c system_stm32f0xx.c

# all the files will be generated with this name (main.elf, main.bin, main.hex, etc)
Expand All @@ -21,6 +21,7 @@ OPENOCD_PROC_FILE=extra/stm32f0-openocd.cfg
###################################################

CC=arm-none-eabi-gcc
GDB=arm-none-eabi-gdb
OBJCOPY=arm-none-eabi-objcopy
OBJDUMP=arm-none-eabi-objdump
SIZE=arm-none-eabi-size
Expand All @@ -29,11 +30,11 @@ CFLAGS = -Wall -g -std=c99 -Os
#CFLAGS += -mlittle-endian -mthumb -mcpu=cortex-m0 -march=armv6s-m
CFLAGS += -mlittle-endian -mcpu=cortex-m0 -march=armv6-m -mthumb
CFLAGS += -ffunction-sections -fdata-sections
CFLAGS += -Wl,--gc-sections -Wl,-Map=$(PROJ_NAME).map

LDFLAGS += -Wl,--gc-sections -Wl,-Map=$(PROJ_NAME).map

###################################################

vpath %.c src
vpath %.a $(STD_PERIPH_LIB)

ROOT=$(shell pwd)
Expand All @@ -42,38 +43,53 @@ CFLAGS += -I inc -I $(STD_PERIPH_LIB) -I $(STD_PERIPH_LIB)/CMSIS/Device/ST/STM32
CFLAGS += -I $(STD_PERIPH_LIB)/CMSIS/Include -I $(STD_PERIPH_LIB)/STM32F0xx_StdPeriph_Driver/inc
CFLAGS += -include $(STD_PERIPH_LIB)/stm32f0xx_conf.h

SRCS += Device/startup_stm32f0xx.s # add startup file to build
STARTUP = Device/startup_stm32f0xx.s # add startup file to build

# need if you want to build with -DUSE_CMSIS
#SRCS += stm32f0_discovery.c
#SRCS += stm32f0_discovery.c stm32f0xx_it.c

OBJS = $(SRCS:.c=.o)
OBJS = $(addprefix objs/,$(SRCS:.c=.o))
DEPS = $(addprefix deps/,$(SRCS:.c=.d))

###################################################

.PHONY: lib proj
.PHONY: all lib proj program debug clean reallyclean

all: lib proj

-include $(DEPS)

lib:
$(MAKE) -C $(STD_PERIPH_LIB)

proj: $(PROJ_NAME).elf

$(PROJ_NAME).elf: $(SRCS)
$(CC) $(CFLAGS) $^ -o $@ -L$(STD_PERIPH_LIB) -lstm32f0 -L$(LDSCRIPT_INC) -Tstm32f0.ld
dirs:
mkdir -p deps objs
touch dirs

objs/%.o : src/%.c dirs
$(CC) $(CFLAGS) -c -o $@ $< -MMD -MF deps/$(*F).d

$(PROJ_NAME).elf: $(OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@ $(STARTUP) -L$(STD_PERIPH_LIB) -lstm32f0 -L$(LDSCRIPT_INC) -Tstm32f0.ld
$(OBJCOPY) -O ihex $(PROJ_NAME).elf $(PROJ_NAME).hex
$(OBJCOPY) -O binary $(PROJ_NAME).elf $(PROJ_NAME).bin
$(OBJDUMP) -St $(PROJ_NAME).elf >$(PROJ_NAME).lst
$(SIZE) $(PROJ_NAME).elf

program: $(PROJ_NAME).bin
program: all
openocd -f $(OPENOCD_BOARD_DIR)/stm32f0discovery.cfg -f $(OPENOCD_PROC_FILE) -c "stm_flash `pwd`/$(PROJ_NAME).bin" -c shutdown

debug: program
$(GDB) -x extra/gdb_cmds $(PROJ_NAME).elf

clean:
find ./ -name '*~' | xargs rm -f
rm -f *.o
rm -f objs/*.o
rm -f deps/*.d
rm -f dirs
rm -f $(PROJ_NAME).elf
rm -f $(PROJ_NAME).hex
rm -f $(PROJ_NAME).bin
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ If there is an error finding the .cfg file, please double-check the OPENOCD_BOAR

If you are not able to communicate with the STM32F0-Discovery board without root privileges you should follow the step from [the stlink repo readme file](https://github.com/texane/stlink#readme) for adding a udev rule for this hardware.

##On-Chip Debugging

Typing 'make debug' will launch a GDB session attached to OpenOCD. With this, you can set breakpoints, single-step through code, print variable values...anything you can do with GDB. See [this blog post](http://www.mjblythe.com/hacks/2013/02/debugging-stm32-with-gdb-and-openocd/) for info about how it works.

##Compiling your own toolchain
It might be best to use a precompiled toolchain liked CodeSourcery G++: Lite Edition. But if you would prefer to compile your own, give [this guide](http://www.kunen.org/uC/gnu_tool.html) a try. Just google for the source code to make sure you're using the most recent versions. GCC now comes with the core and g++ code all in one archive.

Expand Down
3 changes: 3 additions & 0 deletions extra/gdb_cmds
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target remote | openocd -f /usr/share/openocd/scripts/board/stm32f0discovery.cfg -c "gdb_port pipe; log_output openocd.log"
monitor reset halt
load