diff --git a/blue-pill/blinky/Makefile b/blue-pill/blinky/Makefile index d5a2772..d7a7cc6 100644 --- a/blue-pill/blinky/Makefile +++ b/blue-pill/blinky/Makefile @@ -16,7 +16,7 @@ ARMSIZE := $(ARMTC)-size OBJCOPY := $(ARMTC)-objcopy # compiler options -CPUFLAGS := -mcpu=cortex-m3 -mthumb +CPUFLAGS := -mcpu=cortex-m3 -mthumb -std=c99 CFLAGS := -Wall -Wextra -Os -MD $(CPUFLAGS) LDFLAGS := $(CPUFLAGS) -nostartfiles -Wl,-T,bluepill.ld LDLIBS := -lc -lnosys diff --git a/blue-pill/blinky/blinky.c b/blue-pill/blinky/blinky.c index 9f6dbc2..85f1ef2 100644 --- a/blue-pill/blinky/blinky.c +++ b/blue-pill/blinky/blinky.c @@ -1,5 +1,8 @@ /* based on example from https://github.com/satoshinm/pill_blink */ +#include "bluepill.h" +#define LED_PIN 13 + static inline void delay(unsigned long ms) { @@ -8,19 +11,17 @@ delay(unsigned long ms) static inline void led_off() { - (*(volatile unsigned int *)(0x40011010)) = (1 << 13); + clear_pin(GPIO_C, LED_PIN); } static inline void led_on() { - (*(volatile unsigned int *)(0x40011014)) = (1 << 13); + set_pin(GPIO_C, LED_PIN); } void __attribute__ ((weak, naked)) reset_handler(void) { - (*(volatile unsigned int *)(0x40021018)) |= (1 << 4); - - (*(volatile unsigned int *)(0x40011004)) |= (0x00 << (((13 - 8) * 4) + 2)); - (*(volatile unsigned int *)(0x40011004)) |= (0x02 << ((13 - 8) * 4)); + *RCC |= (1 << 4); /* enable port C clock */ + output_mode(GPIO_C, LED_PIN, OUTPUT_GPP, OUTPUT_MAX_2MHZ); while(1) { led_off(); diff --git a/blue-pill/draugr/Makefile b/blue-pill/draugr/Makefile new file mode 100644 index 0000000..0297f9d --- /dev/null +++ b/blue-pill/draugr/Makefile @@ -0,0 +1,56 @@ +# configurables +OBJS := +TARGET := blink +OBJS += $(TARGET).o + +# targets +ELF := $(TARGET).elf +BIN := $(TARGET).bin + +# toolchain setup +ARMTC := arm-none-eabi +ARMCC := $(ARMTC)-gcc +ARMCXX := $(ARMTC)-g++ +CC := $(ARMCC) +CXX := $(ARMCXX) +LD := $(ARMCXX) +ARMSIZE := $(ARMTC)-size +OBJCOPY := $(ARMTC)-objcopy + +# compiler options +CPUFLAGS := -mcpu=cortex-m3 -mthumb +CFLAGS := -Wall -Wextra -Os -MD $(CPUFLAGS) +CXXFLAGS := -Wall -Wextra -Os -MD $(CPUFLAGS) -std=c++14 +LDFLAGS := $(CPUFLAGS) -nostartfiles -Wl,-T,stm32f103.ld +LDLIBS := -lc -lnosys + +# programmer options +STARTMEM := 0x8000000 + +# targets + +.PHONY: all +all: $(BIN) + +$(ELF): $(OBJS) + $(ARMCC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS) + $(ARMSIZE) -A $@ + +$(BIN): $(ELF) + $(OBJCOPY) -O binary $< $@ + +.PHONY: flash +flash: $(BIN) + st-flash write $(BIN) $(STARTMEM) + +.PHONY: erase +erase: + st-flash erase + +.PHONY: install +install: erase flash + +.PHONY: clean +clean: + rm -f *.o *.bin *.elf *.d *.map + diff --git a/blue-pill/draugr/blink.cc b/blue-pill/draugr/blink.cc new file mode 100644 index 0000000..c15c8a1 --- /dev/null +++ b/blue-pill/draugr/blink.cc @@ -0,0 +1,50 @@ +#include "bluepill.h" + +#define LED_PIN 13 + +void reset_handler(void); + +__attribute__ ((section(".vectors"))) +struct { + unsigned int *initial_sp_value; + void (*reset)(void); + void (*nmi)(void); + void (*hard_fault)(void); + void (*memory_manage_fault)(void); + void (*bus_fault)(void); + void (*usage_fault)(void); + void (*reserved_x001c[4])(void); + void (*sv_call)(void); + void (*debug_monitor)(void); + void (*reserved_x0034)(void); + void (*pend_sv)(void); + void (*systick)(void); + void (*irq[68])(void); +} vector_table = { + initial_sp_value : (unsigned int *)0x100, + reset : reset_handler, +}; + +static inline void +delay(unsigned long ms) +{ + for (unsigned long i = 0; i < ms; ++i) __asm__("nop"); +} + +// void __attribute__ ((weak, naked)) reset_handler(void) { +void reset_handler() { + *RCC |= (1 << 4); /* enable port C clock */ + GPIO_C->output_mode(LED_PIN, OUTPUT_GPP, OUTPUT_MAX_2MHZ); + + while(1) { + GPIO_C->clear_pin(LED_PIN); + delay(1000000); + GPIO_C->set_pin(LED_PIN); + delay(100000); + GPIO_C->clear_pin(LED_PIN); + delay(100000); + GPIO_C->set_pin(LED_PIN); + delay(100000); + } +} + diff --git a/blue-pill/draugr/bluepill.h b/blue-pill/draugr/bluepill.h new file mode 100644 index 0000000..22fa314 --- /dev/null +++ b/blue-pill/draugr/bluepill.h @@ -0,0 +1,7 @@ +#ifndef __BLUEPILL_H__ +#define __BLUEPILL_H__ + +#include "bluepill/gpio.h" +#include "bluepill/rcc.h" + +#endif // __BLUEPILL_H__ diff --git a/blue-pill/draugr/bluepill/gpio.h b/blue-pill/draugr/bluepill/gpio.h new file mode 100644 index 0000000..5c75359 --- /dev/null +++ b/blue-pill/draugr/bluepill/gpio.h @@ -0,0 +1,54 @@ +#ifndef __BLUEPILL_GPIO_H__ +#define __BLUEPILL_GPIO_H__ + +#include + +constexpr volatile unsigned int *PORTA = reinterpret_cast(0x40010800); +constexpr volatile unsigned int *PORTB = reinterpret_cast(0x40010c00); +constexpr volatile unsigned int *PORTC = reinterpret_cast(0x40011000); +constexpr volatile unsigned int *PORTD = reinterpret_cast(0x40011400); +constexpr volatile unsigned int *PORTE = reinterpret_cast(0x40011800); + +class GPIO { +public: + void set_pin(uint32_t pin) { this->BSRR |= ((1 << pin) << 16); } + void clear_pin(uint32_t pin) { this->BSRR |= (1 << pin); } + void output_mode(uint32_t pin, uint32_t mode, uint32_t max) { + if (pin > 7) { + pin = ((pin - 8) * 4); + this->CRH |= (max << pin); + this->CRH |= (mode << (pin + 2)); + } + else { + pin *= 4; + this->CRL |= (max << pin); + this->CRL |= (mode << (pin + 2)); + } + } + +private: + 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_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; + + + +#endif // __BLUEPILL_GPIO_H__ diff --git a/blue-pill/draugr/bluepill/rcc.h b/blue-pill/draugr/bluepill/rcc.h new file mode 100644 index 0000000..50a2280 --- /dev/null +++ b/blue-pill/draugr/bluepill/rcc.h @@ -0,0 +1,6 @@ +#ifndef __BLUEPILL_RCC_H__ +#define __BLUEPILL_RCC_H__ + +constexpr volatile unsigned int *RCC = reinterpret_cast(0x40021000); + +#endif // __BLUEPILL_RCC_H__ diff --git a/blue-pill/draugr/stm32f103.ld b/blue-pill/draugr/stm32f103.ld new file mode 100644 index 0000000..bdbdac1 --- /dev/null +++ b/blue-pill/draugr/stm32f103.ld @@ -0,0 +1,19 @@ +/* from https://github.com/satoshinm/pill_blink/ */ + +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +EXTERN(vector_table); +ENTRY(reset_handler); + +SECTIONS +{ + .text : { + *(.vectors) + *(.text*) + . = ALIGN(4); + } >rom +} diff --git a/blue-pill/pins.txt b/blue-pill/pins.txt index eff2b84..13fd7d3 100644 --- a/blue-pill/pins.txt +++ b/blue-pill/pins.txt @@ -1,4 +1,4 @@ -BOARD: +BOARD (STM32F103C8T6): | | +-+-+-+-+-+