From 943e2bfa931a0d9786a9bad07f7762b753d7a1a9 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Mon, 12 Mar 2018 22:16:18 -0700 Subject: [PATCH] blue-pill: fix linker scripts and asm led turnon --- blue-pill/asm-blink/Makefile | 70 +++++++++++++++++++++++++++++++++ blue-pill/asm-blink/blink.S | 43 ++++++++++++++++++++ blue-pill/asm-blink/bluepill.ld | 18 +++++++++ blue-pill/draugr/startup.s | 2 + blue-pill/draugr/stm32f103.ld | 13 ++++-- 5 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 blue-pill/asm-blink/Makefile create mode 100644 blue-pill/asm-blink/blink.S create mode 100644 blue-pill/asm-blink/bluepill.ld diff --git a/blue-pill/asm-blink/Makefile b/blue-pill/asm-blink/Makefile new file mode 100644 index 0000000..1c7ba4b --- /dev/null +++ b/blue-pill/asm-blink/Makefile @@ -0,0 +1,70 @@ +# configurables +OBJS := +TARGET := blink +OBJS += $(TARGET).o + +# targets +ELF := $(TARGET).elf +BIN := $(TARGET).bin + +# toolchain setup +ARMTC := arm-none-eabi +CC := $(ARMTC)-gcc +LD := $(ARMTC)-gcc +AS := $(ARMTC)-as +ARMSIZE := $(ARMTC)-size +OBJCOPY := $(ARMTC)-objcopy +PAGER ?= less + +# compiler options +CPUFLAGS := -mcpu=cortex-m3 -mthumb -std=c99 +CFLAGS := -Wall -Wextra -Os -MD $(CPUFLAGS) -g +LDFLAGS := $(CPUFLAGS) -nostartfiles -Wl,-T,bluepill.ld +LDLIBS := -lc -lnosys + +# programmer options +STARTMEM := 0x8000000 + +# targets + +.PHONY: all +all: $(BIN) + +$(ELF): $(OBJS) + $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS) + $(ARMSIZE) $@ + +.PHONY: strip +strip: $(ELF) + $(ARMTC)-strip $(ELF) + $(ARMSIZE) $(ELF) + +$(BIN): $(ELF) + $(OBJCOPY) -O binary $< $@ + +.PHONY: flash +flash: $(BIN) + st-flash write $(BIN) $(STARTMEM) + +.PHONY: erase +erase: + st-flash erase + +.PHONY: reset +reset: + st-flash reset + +.PHONY: install +install: erase flash reset + +.PHONY: clean +clean: + rm -f *.o *.bin *.elf *.d *.map + +.PHONY: disass +disass: $(ELF) + $(ARMTC)-objdump -D $(ELF) | $(PAGER) + +.PHONY: dump +dump: $(ELF) + $(ARMTC)-objcopy -D $(ELF) > $(TARGET).dump diff --git a/blue-pill/asm-blink/blink.S b/blue-pill/asm-blink/blink.S new file mode 100644 index 0000000..b4d0955 --- /dev/null +++ b/blue-pill/asm-blink/blink.S @@ -0,0 +1,43 @@ +.syntax unified +.cpu cortex-m3 +.thumb + +.section .text +.global vectors +vectors: +.align 2 +.word 0 +.word _start + +.thumb_func +.global _start +_start: + /* enable port C clock */ + ldr r0, =0x40021000 + ldr r1, [r0, #0x18] + mov r2, #5 + orr r1, r2 + str r1, [r0, #0x18] + + /* + * enable output mode at a max of 2 MHz. + * note that the default is a general purpose output push-pull, + * which is what it should be. + */ + ldr r0, =0x40011000 + ldr r1, [r0, #0x04] + mov r2, #0x02 + lsl r2, #0x14 + orr r1, r2 + str r1, [r0, #0x04] + + /* write LED pin (PC13) */ + ldr r0, =0x40011000 + ldr r1, [r0, #0x10] + mov r2, #0x01 + lsl r2, #0x0d + lsl r2, #0x10 + orr r1, r2 + str r1, [r0, #0x10] + +hang: bl . diff --git a/blue-pill/asm-blink/bluepill.ld b/blue-pill/asm-blink/bluepill.ld new file mode 100644 index 0000000..ee4d607 --- /dev/null +++ b/blue-pill/asm-blink/bluepill.ld @@ -0,0 +1,18 @@ +MEMORY +{ + flash (rx) : ORIGIN = 0x08000000, LENGTH = 64K + sram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +EXTERN(vectors); +ENTRY(_start); + +SECTIONS +{ + .text : { + *(.vectors*) + *(.text*) + . = ALIGN(4); + } > flash + .bss : { *(.bss*) } > sram +} diff --git a/blue-pill/draugr/startup.s b/blue-pill/draugr/startup.s index 5c55a35..f3f4e33 100644 --- a/blue-pill/draugr/startup.s +++ b/blue-pill/draugr/startup.s @@ -9,6 +9,8 @@ .cpu cortex-m3 .thumb +.section .text +.global vectors vectors: .align 2 .long 0x20005000 /* stack pointer points to top of SRAM */ diff --git a/blue-pill/draugr/stm32f103.ld b/blue-pill/draugr/stm32f103.ld index 9d7fa01..d00ebd2 100644 --- a/blue-pill/draugr/stm32f103.ld +++ b/blue-pill/draugr/stm32f103.ld @@ -1,11 +1,18 @@ MEMORY { - flash (rx) : ORIGIN = 0x00000000, LENGTH = 128K + flash (rx) : ORIGIN = 0x08000000, LENGTH = 64K sram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K } +EXTERN(vectors); +ENTRY(reset_handler); + SECTIONS { - .text : { *(.text*) } > flash - .bss : { *(.bss*) } > sram + .text : { + *(.vectors*) + *(.text*) + . = ALIGN(4); + } > flash + .bss : { *(.bss*) } > sram }