From 5734a63997141d407bdfb952ef0a34f71d034119 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Wed, 7 Mar 2018 19:50:01 -0800 Subject: [PATCH] blue-pill: fix bootloader. --- blue-pill/blinky/Makefile | 16 +++++++-- blue-pill/blinky/blinky.c | 3 +- blue-pill/blinky/bluepill.ld | 16 +++------ blue-pill/blinky/startup.s | 32 ++++++++++------- blue-pill/draugr/startup.s | 32 ++++++++++------- blue-pill/draugr/stm32f103.ld | 16 +++------ blue-pill/skeleton/Makefile | 37 +++++++++++++------ blue-pill/skeleton/bluepill.h | 65 ++++++++++++++++++++++++++++++++++ blue-pill/skeleton/bluepill.ld | 16 +++------ blue-pill/skeleton/startup.s | 28 +++++++++++++++ 10 files changed, 188 insertions(+), 73 deletions(-) create mode 100644 blue-pill/skeleton/bluepill.h create mode 100644 blue-pill/skeleton/startup.s diff --git a/blue-pill/blinky/Makefile b/blue-pill/blinky/Makefile index 2fe9da2..de6c013 100644 --- a/blue-pill/blinky/Makefile +++ b/blue-pill/blinky/Makefile @@ -14,10 +14,11 @@ 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) +CFLAGS := -Wall -Wextra -Os -MD $(CPUFLAGS) -g LDFLAGS := $(CPUFLAGS) -nostartfiles -Wl,-T,bluepill.ld LDLIBS := -lc -lnosys @@ -49,10 +50,21 @@ flash: $(BIN) erase: st-flash erase +.PHONY: reset +reset: + st-flash reset + .PHONY: install -install: erase flash +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/blinky/blinky.c b/blue-pill/blinky/blinky.c index 892620d..d0b465d 100644 --- a/blue-pill/blinky/blinky.c +++ b/blue-pill/blinky/blinky.c @@ -19,11 +19,12 @@ led_on() { set_pin(GPIO_C, LED_PIN); } -int +void main(void) { *RCC |= (1 << 4); /* enable port C clock */ output_mode(GPIO_C, LED_PIN, OUTPUT_GPP, OUTPUT_MAX_2MHZ); + led_on(); while(1) { led_off(); diff --git a/blue-pill/blinky/bluepill.ld b/blue-pill/blinky/bluepill.ld index bdbdac1..9d7fa01 100644 --- a/blue-pill/blinky/bluepill.ld +++ b/blue-pill/blinky/bluepill.ld @@ -1,19 +1,11 @@ -/* from https://github.com/satoshinm/pill_blink/ */ - MEMORY { - rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K - ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K + flash (rx) : ORIGIN = 0x00000000, LENGTH = 128K + sram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K } -EXTERN(vector_table); -ENTRY(reset_handler); - SECTIONS { - .text : { - *(.vectors) - *(.text*) - . = ALIGN(4); - } >rom + .text : { *(.text*) } > flash + .bss : { *(.bss*) } > sram } diff --git a/blue-pill/blinky/startup.s b/blue-pill/blinky/startup.s index fca5a0b..f55d87c 100644 --- a/blue-pill/blinky/startup.s +++ b/blue-pill/blinky/startup.s @@ -5,24 +5,32 @@ * TODO: is the IRQv buffer actually needed right now? */ +.cpu cortex-m3 +.thumb + .globl vectors vectors: .align 2 -.long 0x100 /* best guess at stack pointer */ +.long 0x20002000 /* best guess at stack pointer */ .long reset_handler /* reset handler */ -.long 0 /* NMI handler */ -.long 0 /* hard_fault_handler */ -.long 0 /* memory management handler */ -.long 0 /* bus fault handler */ -.long 0 /* usage fault handler */ +.long hang /* NMI handler */ +.long hang /* hard_fault_handler */ +.long hang /* memory management handler */ +.long hang /* bus fault handler */ +.long hang /* usage fault handler */ .skip 0x20 /* reserved */ -.long 0 /* svcall handler */ -.long 0 /* debug handler */ +.long hang /* svcall handler */ +.long hang /* debug handler */ .skip 4 /* reserved */ -.long 0 /* pendsv handler */ -.long 0 /* systick handler */ -.skip 0xf4 /* remaining / IRQ vectors */ +.long hang /* pendsv handler */ +.long hang /* systick handler */ +.skip 0x100 /* remaining / IRQ vectors */ -.globl reset_handler + +.thumb_func +hang: b . + + +.thumb_func reset_handler: bl main diff --git a/blue-pill/draugr/startup.s b/blue-pill/draugr/startup.s index fca5a0b..f55d87c 100644 --- a/blue-pill/draugr/startup.s +++ b/blue-pill/draugr/startup.s @@ -5,24 +5,32 @@ * TODO: is the IRQv buffer actually needed right now? */ +.cpu cortex-m3 +.thumb + .globl vectors vectors: .align 2 -.long 0x100 /* best guess at stack pointer */ +.long 0x20002000 /* best guess at stack pointer */ .long reset_handler /* reset handler */ -.long 0 /* NMI handler */ -.long 0 /* hard_fault_handler */ -.long 0 /* memory management handler */ -.long 0 /* bus fault handler */ -.long 0 /* usage fault handler */ +.long hang /* NMI handler */ +.long hang /* hard_fault_handler */ +.long hang /* memory management handler */ +.long hang /* bus fault handler */ +.long hang /* usage fault handler */ .skip 0x20 /* reserved */ -.long 0 /* svcall handler */ -.long 0 /* debug handler */ +.long hang /* svcall handler */ +.long hang /* debug handler */ .skip 4 /* reserved */ -.long 0 /* pendsv handler */ -.long 0 /* systick handler */ -.skip 0xf4 /* remaining / IRQ vectors */ +.long hang /* pendsv handler */ +.long hang /* systick handler */ +.skip 0x100 /* remaining / IRQ vectors */ -.globl reset_handler + +.thumb_func +hang: b . + + +.thumb_func reset_handler: bl main diff --git a/blue-pill/draugr/stm32f103.ld b/blue-pill/draugr/stm32f103.ld index bdbdac1..9d7fa01 100644 --- a/blue-pill/draugr/stm32f103.ld +++ b/blue-pill/draugr/stm32f103.ld @@ -1,19 +1,11 @@ -/* from https://github.com/satoshinm/pill_blink/ */ - MEMORY { - rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K - ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K + flash (rx) : ORIGIN = 0x00000000, LENGTH = 128K + sram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K } -EXTERN(vector_table); -ENTRY(reset_handler); - SECTIONS { - .text : { - *(.vectors) - *(.text*) - . = ALIGN(4); - } >rom + .text : { *(.text*) } > flash + .bss : { *(.bss*) } > sram } diff --git a/blue-pill/skeleton/Makefile b/blue-pill/skeleton/Makefile index 5f0531d..93aa363 100644 --- a/blue-pill/skeleton/Makefile +++ b/blue-pill/skeleton/Makefile @@ -1,7 +1,7 @@ # configurables -OBJS := +OBJS := startup.o TARGET := -OBJS += $(TARGET).o +OBJS += $(TARGET).o # targets ELF := $(TARGET).elf @@ -9,15 +9,16 @@ BIN := $(TARGET).bin # toolchain setup ARMTC := arm-none-eabi -ARMCC := $(ARMTC)-gcc -CC := $(ARMCC) -LD := $(ARMCC) +CC := $(ARMTC)-gcc +LD := $(ARMTC)-gcc +AS := $(ARMTC)-as ARMSIZE := $(ARMTC)-size OBJCOPY := $(ARMTC)-objcopy +PAGER ?= less # compiler options -CPUFLAGS := -mcpu=cortex-m3 -mthumb -CFLAGS := -Wall -Wextra -Os -MD $(CPUFLAGS) +CPUFLAGS := -mcpu=cortex-m3 -mthumb -std=c99 +CFLAGS := -Wall -Wextra -Os -MD $(CPUFLAGS) -g LDFLAGS := $(CPUFLAGS) -nostartfiles -Wl,-T,bluepill.ld LDLIBS := -lc -lnosys @@ -30,8 +31,13 @@ STARTMEM := 0x8000000 all: $(BIN) $(ELF): $(OBJS) - $(ARMCC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS) - $(ARMSIZE) -A $@ + $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS) + $(ARMSIZE) $@ + +.PHONY: strip +strip: $(ELF) + $(ARMTC)-strip $(ELF) + $(ARMSIZE) $(ELF) $(BIN): $(ELF) $(OBJCOPY) -O binary $< $@ @@ -44,10 +50,21 @@ flash: $(BIN) erase: st-flash erase +.PHONY: reset +reset: + st-flash reset + .PHONY: install -install: erase flash +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/skeleton/bluepill.h b/blue-pill/skeleton/bluepill.h new file mode 100644 index 0000000..6277194 --- /dev/null +++ b/blue-pill/skeleton/bluepill.h @@ -0,0 +1,65 @@ +#ifndef __BLUEPILL_H__ +#define __BLUEPILL_H__ + +#include + +#define PORTA (volatile unsigned int *)0x40010800 +#define PORTB (volatile unsigned int *)0x40010c00 +#define PORTC (volatile unsigned int *)0x40011000 +#define PORTD (volatile unsigned int *)0x40011400 +#define PORTE (volatile unsigned int *)0x40011800 + +#define RCC (volatile unsigned int *)0x40021000 + +/* + + 40011000 CRL + 40011004 CRH + 40011008 IDR + 4001100c ODR + 40011010 BRR + 40011012 LCKR + + */ +typedef struct _GPIO { + uint32_t CRL; /* configuration register low */ + uint32_t CRH; /* configuration register high */ + uint32_t IDR; /* input data register */ + uint32_t ODR; /* output data register */ + uint32_t BSRR; /* bit set / reset register */ + uint16_t BRR; /* reset register */ + uint32_t LCKR; /* locking register */ +} GPIO; + +GPIO *GPIO_A = (GPIO *)PORTA; +GPIO *GPIO_B = (GPIO *)PORTB; +GPIO *GPIO_C = (GPIO *)PORTC; +GPIO *GPIO_D = (GPIO *)PORTD; +GPIO *GPIO_E = (GPIO *)PORTE; + +const uint32_t OUTPUT_MAX_2MHZ = 2; +const uint32_t OUTPUT_MAX_10MHZ = 1; +const uint32_t OUTPUT_MAX_50MHZ = 3; +const uint32_t OUTPUT_GPP = 0; +const uint32_t OUTPUT_GOD = 1; +const uint32_t OUTPUT_APP = 2; +const uint32_t OUTPUT_AOD = 3; + +void set_pin(GPIO *gpio, uint32_t pin) { gpio->BSRR |= ((1 << pin) << 16); } +void clear_pin(GPIO *gpio, uint32_t pin) { gpio->BSRR |= (1 << pin); } +void output_mode(GPIO *gpio, uint32_t pin, uint32_t mode, uint32_t max) { + if (pin > 7) { + pin = ((pin - 8) * 4); + gpio->CRH |= (max << pin); + gpio->CRH |= (mode << (pin + 2)); + } + else { + pin *= 4; + gpio->CRL |= (max << pin); + gpio->CRL |= (mode << (pin + 2)); + } +} + + +#endif /* __BLUEPILL_H__ */ + diff --git a/blue-pill/skeleton/bluepill.ld b/blue-pill/skeleton/bluepill.ld index 57b696a..9d7fa01 100644 --- a/blue-pill/skeleton/bluepill.ld +++ b/blue-pill/skeleton/bluepill.ld @@ -1,19 +1,11 @@ -# from https://github.com/satoshinm/pill_blink/ - MEMORY { - rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K - ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K + flash (rx) : ORIGIN = 0x00000000, LENGTH = 128K + sram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K } -EXTERN(vector_table); -ENTRY(reset_handler); - SECTIONS { - .text : { - *(.vectors) - *(.text*) - . = ALIGN(4); - } >rom + .text : { *(.text*) } > flash + .bss : { *(.bss*) } > sram } diff --git a/blue-pill/skeleton/startup.s b/blue-pill/skeleton/startup.s new file mode 100644 index 0000000..fca5a0b --- /dev/null +++ b/blue-pill/skeleton/startup.s @@ -0,0 +1,28 @@ +/* + * Startup code for the STM32F103-based blue pill board. + * + * TODO: revisit stack pointer + * TODO: is the IRQv buffer actually needed right now? + */ + +.globl vectors +vectors: +.align 2 +.long 0x100 /* best guess at stack pointer */ +.long reset_handler /* reset handler */ +.long 0 /* NMI handler */ +.long 0 /* hard_fault_handler */ +.long 0 /* memory management handler */ +.long 0 /* bus fault handler */ +.long 0 /* usage fault handler */ +.skip 0x20 /* reserved */ +.long 0 /* svcall handler */ +.long 0 /* debug handler */ +.skip 4 /* reserved */ +.long 0 /* pendsv handler */ +.long 0 /* systick handler */ +.skip 0xf4 /* remaining / IRQ vectors */ + +.globl reset_handler +reset_handler: + bl main