Skip to content

Commit

Permalink
Rework build system to decouple linker script from platform
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanthom committed Dec 13, 2022
1 parent b69bb88 commit aabdb57
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 128 deletions.
92 changes: 23 additions & 69 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,69 +1,23 @@
SUPPORTED_PLATFORMS = baite bluepill olimexstm32h103 stlinkv2 stlinkv2white
PLATFORM ?= bluepill

OBJS := src/dirtyjtag.$(PLATFORM).o src/jtag.$(PLATFORM).o src/usb.$(PLATFORM).o src/delay.$(PLATFORM).o src/cmd.$(PLATFORM).o

PREFIX ?= arm-none-eabi
TARGETS := stm32/f1
DEFS += -DSTM32F1
ARCH_FLAGS := -mthumb -mcpu=cortex-m3 -msoft-float -mfix-cortex-m3-ldrd
LD_SCRIPT := ld/$(PLATFORM).ld

UCMX_DIR := $(realpath unicore-mx)
UCMX_INCLUDE_DIR := $(UCMX_DIR)/include
UCMX_LIB_DIR := $(UCMX_DIR)/lib

CFLAGS = -g -O2
CFLAGS += -Wall -Wextra -Werror
CFLAGS += -fno-common -ffunction-sections -fdata-sections
CFLAGS += -std=gnu11
CFLAGS += -DPLATFORM='HW_$(PLATFORM)'

CPPFLAGS = -MD -g
CPPFLAGS += -Wall -Wundef
CPPFLAGS += -I$(UCMX_INCLUDE_DIR) $(DEFS)

LDFLAGS += --static -nostartfiles
LDFLAGS += -L"$(UCMX_LIB_DIR)"
LDFLAGS += -T$(LD_SCRIPT)
LDFLAGS += -Wl,-Map=$(*).map
LDFLAGS += -Wl,--gc-sections # Remove deadcode

LDLIBS += -lucmx_stm32f1
LDLIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group

CC := $(PREFIX)-gcc
LD := $(PREFIX)-ld
AR := $(PREFIX)-ar
AS := $(PREFIX)-as
SIZE := $(PREFIX)-size
OBJCOPY := $(PREFIX)-objcopy

all: dirtyjtag

clean: dirtyjtag-clean ucmx-clean

dirtyjtag: src/dirtyjtag.$(PLATFORM).elf src/dirtyjtag.$(PLATFORM).bin

dirtyjtag-release: $(patsubst %, src/dirtyjtag.%.bin, $(SUPPORTED_PLATFORMS))

dirtyjtag-clean:
$(Q)$(RM) src/*.d src/*.o src/*.map src/*.bin src/*.elf *.bin *.elf

ucmx:
$(Q)$(MAKE) -C $(UCMX_DIR) lib/stm32/f1

ucmx-clean:
$(Q)$(MAKE) -C $(UCMX_DIR) clean

%.bin: %.elf
$(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin

%.elf %.map: $(OBJS) $(LD_SCRIPT)
$(Q)$(CC) $(LDFLAGS) $(ARCH_FLAGS) $(OBJS) $(LDLIBS) -o $(*).elf
$(Q)$(SIZE) $(*).elf

%.$(PLATFORM).o: %.c | ucmx
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(ARCH_FLAGS) -o $@ -c $<

.PHONY: clean dirtyjtag dirtyjtag-release dirtyjtag-clean ucmx ucmx-clean
MAKE := make

all:
$(MAKE) -f Makefile.stm32f1 PLATFORM=bluepill LOADER=noloader
$(MAKE) -f Makefile.stm32f1 PLATFORM=baite LOADER=noloader
$(MAKE) -f Makefile.stm32f1 PLATFORM=olimexstm32h103 LOADER=noloader
$(MAKE) -f Makefile.stm32f1 PLATFORM=stlinkv2 LOADER=noloader
$(MAKE) -f Makefile.stm32f1 PLATFORM=stlinkv2white LOADER=noloader
$(MAKE) -f Makefile.stm32f1 PLATFORM=bluepill LOADER=loader2k
$(MAKE) -f Makefile.stm32f1 PLATFORM=baite LOADER=loader2k
$(MAKE) -f Makefile.stm32f1 PLATFORM=olimexstm32h103 LOADER=loader2k
$(MAKE) -f Makefile.stm32f1 PLATFORM=stlinkv2 LOADER=loader2k
$(MAKE) -f Makefile.stm32f1 PLATFORM=stlinkv2white LOADER=loader2k
$(MAKE) -f Makefile.stm32f1 PLATFORM=bluepill LOADER=loader4k
$(MAKE) -f Makefile.stm32f1 PLATFORM=baite LOADER=loader4k
$(MAKE) -f Makefile.stm32f1 PLATFORM=olimexstm32h103 LOADER=loader4k
$(MAKE) -f Makefile.stm32f1 PLATFORM=stlinkv2 LOADER=loader4k
$(MAKE) -f Makefile.stm32f1 PLATFORM=stlinkv2white LOADER=loader4k

clean:
$(MAKE) -f Makefile.stm32f1 clean

.PHONY: all clean
70 changes: 70 additions & 0 deletions Makefile.stm32f1
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
SUPPORTED_PLATFORMS = baite bluepill olimexstm32h103 stlinkv2 stlinkv2white
PLATFORM ?= bluepill
LOADER ?= noloader

OBJS := src/dirtyjtag.$(PLATFORM).o src/jtag.$(PLATFORM).o src/usb.$(PLATFORM).o src/delay.$(PLATFORM).o src/cmd.$(PLATFORM).o

PREFIX ?= arm-none-eabi
TARGETS := stm32/f1
DEFS += -DSTM32F1
ARCH_FLAGS := -mthumb -mcpu=cortex-m3 -msoft-float -mfix-cortex-m3-ldrd
LD_SCRIPT := ld/stm32f1-$(LOADER).ld

UCMX_DIR := $(realpath unicore-mx)
UCMX_INCLUDE_DIR := $(UCMX_DIR)/include
UCMX_LIB_DIR := $(UCMX_DIR)/lib

CFLAGS = -g -O2
CFLAGS += -Wall -Wextra -Werror
CFLAGS += -fno-common -ffunction-sections -fdata-sections
CFLAGS += -std=gnu11
CFLAGS += -DPLATFORM='HW_$(PLATFORM)' $(DEFS)

CPPFLAGS = -MD -g
CPPFLAGS += -Wall -Wundef
CPPFLAGS += -I$(UCMX_INCLUDE_DIR) $(DEFS)

LDFLAGS += --static -nostartfiles
LDFLAGS += -L"$(UCMX_LIB_DIR)"
LDFLAGS += -T$(LD_SCRIPT)
LDFLAGS += -Wl,-Map=$(*).map
LDFLAGS += -Wl,--gc-sections # Remove deadcode

LDLIBS += -lucmx_stm32f1
LDLIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group

CC := $(PREFIX)-gcc
LD := $(PREFIX)-ld
AR := $(PREFIX)-ar
AS := $(PREFIX)-as
SIZE := $(PREFIX)-size
OBJCOPY := $(PREFIX)-objcopy

all: dirtyjtag

clean: dirtyjtag-clean ucmx-clean

dirtyjtag: src/dirtyjtag.$(PLATFORM).$(LOADER).elf src/dirtyjtag.$(PLATFORM).$(LOADER).bin

#dirtyjtag-release: $(patsubst %, src/dirtyjtag.%.bin, $(SUPPORTED_PLATFORMS))

dirtyjtag-clean:
$(Q)$(RM) src/*.d src/*.o src/*.map src/*.bin src/*.elf *.bin *.elf

ucmx:
$(Q)$(MAKE) -C $(UCMX_DIR) lib/stm32/f1

ucmx-clean:
$(Q)$(MAKE) -C $(UCMX_DIR) clean

%.bin: %.elf
$(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin

%.$(LOADER).elf %.$(LOADER).map: $(OBJS) $(LD_SCRIPT)
$(Q)$(CC) $(LDFLAGS) $(ARCH_FLAGS) $(OBJS) $(LDLIBS) -o $(*).$(LOADER).elf
$(Q)$(SIZE) $(*).$(LOADER).elf

%.$(PLATFORM).o: %.c | ucmx
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(ARCH_FLAGS) -o $@ -c $<

.PHONY: clean dirtyjtag dirtyjtag-release dirtyjtag-clean ucmx ucmx-clean
32 changes: 22 additions & 10 deletions docs/building-dirtyjtag.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Manual build

In order to compile DirtyJTAG, you will need the following software :
In order to compile DirtyJTAG, you will need the following software:

* git
* ARM toolchain (I'm using Fedora's `arm-none-eabi-gcc-cs`/`arm-none-eabi-newline` packages)
Expand All @@ -15,21 +15,33 @@ git clone --recursive https://github.com/jeanthom/dirtyjtag
cd dirtyjtag
```

Then you can build the firmware :
Then you can build all the firmware versions :

```
make PLATFORM=bluepill
make
```

Currently there are two platforms :
Once the build is completed, your freshly compiled firmware will be available in `src/` as a binary file.

* bluepill : Default build setting
* stlinkv2 : Chinese ST-Link clone with specific pinout (the one that looks like a USB key)
* stlinkv2dfu : Same as above but can be loaded by the bootloader (unsupported, known to be buggy)
* olimexstm32h103 : STM32F103 board from Olimex
* baite : "Baite" ST-Link dongle
If you only want to build a specific configuration of DirtyJTAG, directly call the right Makefile with the correct parameters for the platform and the bootloader type:

Once the build is completed, your freshly compiled firmware will be available in `src/` as a binary file.
```
make -f Makefile.stm32f1 PLATFORM=bluepill LOADER=noloader
```

`PLATFORM` could be any of the following:

* `bluepill`
* `stlinkv2`
* `stlinkv2white`
* `baite`
* `olimexstm32h103`

`LOADER` could be any of the following:

* `noloader`: No bootloader
* `loader2k`: Bootloader is occupying 0x0-0x2000, DirtyJTAG starts at 0x2000 offset
* `loader2k`: Bootloader is occupying 0x0-0x4000, DirtyJTAG starts at 0x4000 offset

## Docker build

Expand Down
7 changes: 0 additions & 7 deletions ld/bluepill.ld

This file was deleted.

7 changes: 0 additions & 7 deletions ld/olimexstm32h103.ld

This file was deleted.

7 changes: 0 additions & 7 deletions ld/stlinkv2.ld

This file was deleted.

7 changes: 0 additions & 7 deletions ld/stlinkv2white.ld

This file was deleted.

7 changes: 7 additions & 0 deletions ld/stm32f1-loader2k.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* Generic STM32F1, application at 0x4000 */
MEMORY {
rom (rx) : ORIGIN = 0x08002000, LENGTH = 128K-0x2000
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
}

INCLUDE libucmx_stm32f1.ld
2 changes: 1 addition & 1 deletion ld/stlinkv2dfu.ld → ld/stm32f1-loader4k.ld
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Generic linker script for STM32F103RBT6/STM32F103R8T6 with 0x4000 bootloader */
/* Generic STM32F1, application at 0x4000 */
MEMORY {
rom (rx) : ORIGIN = 0x08004000, LENGTH = 128K-0x4000
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
Expand Down
2 changes: 1 addition & 1 deletion ld/baite.ld → ld/stm32f1-noloader.ld
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Generic linker script for STM32F103RBT6/STM32F103R8T6 */
/* Generic STM32F1, no bootloader */
MEMORY {
rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
Expand Down
36 changes: 18 additions & 18 deletions src/dirtyjtag.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@ int main(void) {
/* Clock init */
rcc_clock_setup_in_hse_8mhz_out_72mhz();

/* ST-Link v2 specific */
#if PLATFORM == HW_stlinkv2dfu
/* Resetting IRQs */
clean_nvic();

/* Resetting peripherals */
rcc_periph_reset_pulse(RST_TIM1);
rcc_periph_reset_pulse(RST_TIM2);
rcc_periph_reset_pulse(RST_TIM3);
rcc_periph_reset_pulse(RST_TIM4);
rcc_periph_reset_pulse(RST_AFIO);
rcc_periph_reset_pulse(RST_USB);
rcc_periph_reset_pulse(RST_SPI1);

/* Disable watchdog */
IWDG_KR = 0;
#endif
// /* ST-Link v2 specific */
// #if PLATFORM == HW_stlinkv2dfu
// /* Resetting IRQs */
// clean_nvic();

// /* Resetting peripherals */
// rcc_periph_reset_pulse(RST_TIM1);
// rcc_periph_reset_pulse(RST_TIM2);
// rcc_periph_reset_pulse(RST_TIM3);
// rcc_periph_reset_pulse(RST_TIM4);
// rcc_periph_reset_pulse(RST_AFIO);
// rcc_periph_reset_pulse(RST_USB);
// rcc_periph_reset_pulse(RST_SPI1);

// /* Disable watchdog */
// IWDG_KR = 0;
// #endif

delay_init();

Expand Down Expand Up @@ -94,7 +94,7 @@ int main(void) {
GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
gpio_set(GPIOC, GPIO13);
rcc_periph_clock_enable(RCC_SPI1);
#elif PLATFORM == HW_stlinkv2 || PLATFORM == HW_stlinkv2dfu
#elif PLATFORM == HW_stlinkv2
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL, GPIO9);
gpio_set(GPIOA, GPIO9);
Expand Down
1 change: 0 additions & 1 deletion src/jtag.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ bool jtag_strobe(uint8_t pulses, bool tms, bool tdi);

#define F_CPU 72000000UL
#define HW_stlinkv2 0
#define HW_stlinkv2dfu 0
#define HW_bluepill 1
#define HW_olimexstm32h103 1
#define HW_baite 2
Expand Down

0 comments on commit aabdb57

Please sign in to comment.