From 6859fd8920e8dcdbaf46f5ea7054a2a70bb02774 Mon Sep 17 00:00:00 2001 From: Matthew Blythe Date: Fri, 22 Feb 2013 23:27:22 -0700 Subject: [PATCH 1/6] Improvements to the Makefile --- Makefile | 36 ++++++++++++++++++++++++++---------- extra/gdb_cmds | 3 +++ 2 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 extra/gdb_cmds diff --git a/Makefile b/Makefile index 15ea40b..1c5e2ef 100755 --- a/Makefile +++ b/Makefile @@ -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) @@ -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 @@ -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) @@ -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_stm32f30x.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 diff --git a/extra/gdb_cmds b/extra/gdb_cmds new file mode 100644 index 0000000..099b1b7 --- /dev/null +++ b/extra/gdb_cmds @@ -0,0 +1,3 @@ +target remote | openocd -f /home/matt/bin/openocd/share/openocd/scripts/board/stm32f3discovery.cfg -c "gdb_port pipe; log_output openocd.log" +monitor reset halt +load From 3b067d32cbf81414dfafcc524d703aea549141ee Mon Sep 17 00:00:00 2001 From: Matthew Blythe Date: Fri, 22 Feb 2013 23:36:10 -0700 Subject: [PATCH 2/6] Updating the Libraries Makefile, too --- Libraries/Makefile | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Libraries/Makefile b/Libraries/Makefile index 045985a..90e32c2 100755 --- a/Libraries/Makefile +++ b/Libraries/Makefile @@ -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 \ @@ -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 From 2a63c6a936c2ef4db81ab278402f0ff014087237 Mon Sep 17 00:00:00 2001 From: Matthew Blythe Date: Sat, 23 Feb 2013 01:27:33 -0700 Subject: [PATCH 3/6] fixing up gdb commands to match openocd path in Makefile. Also adding a section to the README about on-chip debugging --- README.md | 4 ++++ extra/gdb_cmds | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 89ace17..6101aef 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/extra/gdb_cmds b/extra/gdb_cmds index 099b1b7..97fe34f 100644 --- a/extra/gdb_cmds +++ b/extra/gdb_cmds @@ -1,3 +1,3 @@ -target remote | openocd -f /home/matt/bin/openocd/share/openocd/scripts/board/stm32f3discovery.cfg -c "gdb_port pipe; log_output openocd.log" +target remote | openocd -f /usr/share/openocd/scripts/board/stm32f0discovery.cfg -c "gdb_port pipe; log_output openocd.log" monitor reset halt load From 526212ae21e8da0e7f1f3e14c743f6f6e197c0dd Mon Sep 17 00:00:00 2001 From: Matthew Blythe Date: Sun, 24 Feb 2013 16:34:50 -0700 Subject: [PATCH 4/6] adding a .gitignore file --- .gitignore | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..22e312e --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +# compilation results # +####################### +*.o +*.a +*.bin +*.elf +*.hex +*.lst +*.map + +# Makefile target files # +######################### +dirs +program + +# Eclipse project files # +######################### +.project +.cproject + +# Log files # +############# +openocd.log From cef89f6be0bb9d19aec4e273323de5ba4a87250a Mon Sep 17 00:00:00 2001 From: Matthew Blythe Date: Sun, 24 Feb 2013 17:15:11 -0700 Subject: [PATCH 5/6] forgot to switch startup file back to stm32f0 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1c5e2ef..fef22a1 100755 --- a/Makefile +++ b/Makefile @@ -43,7 +43,7 @@ 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 -STARTUP = Device/startup_stm32f30x.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 From 19faadedc2006f1872cd9e7e146e45ff45bdf39c Mon Sep 17 00:00:00 2001 From: Matthew Blythe Date: Sun, 24 Feb 2013 17:16:02 -0700 Subject: [PATCH 6/6] tweak to .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 22e312e..6a5b5a6 100644 --- a/.gitignore +++ b/.gitignore @@ -13,10 +13,11 @@ dirs program -# Eclipse project files # +# Eclipse & editor files # ######################### .project .cproject +*~ # Log files # #############