blue-pill: fix bootloader.

This commit is contained in:
Kyle Isom 2018-03-07 19:50:01 -08:00
parent 524f860331
commit 5734a63997
10 changed files with 188 additions and 73 deletions

View File

@ -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

View File

@ -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();

View File

@ -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
}

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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

View File

@ -0,0 +1,65 @@
#ifndef __BLUEPILL_H__
#define __BLUEPILL_H__
#include <stdint.h>
#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__ */

View File

@ -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
}

View File

@ -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