bluepill: start HAL, update skeleton.

This commit is contained in:
Kyle Isom 2018-03-07 08:06:25 -08:00
parent 4be849722a
commit 899a1813d1
9 changed files with 201 additions and 8 deletions

View File

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

View File

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

56
blue-pill/draugr/Makefile Normal file
View File

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

50
blue-pill/draugr/blink.cc Normal file
View File

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

View File

@ -0,0 +1,7 @@
#ifndef __BLUEPILL_H__
#define __BLUEPILL_H__
#include "bluepill/gpio.h"
#include "bluepill/rcc.h"
#endif // __BLUEPILL_H__

View File

@ -0,0 +1,54 @@
#ifndef __BLUEPILL_GPIO_H__
#define __BLUEPILL_GPIO_H__
#include <stdint.h>
constexpr volatile unsigned int *PORTA = reinterpret_cast<volatile unsigned int *>(0x40010800);
constexpr volatile unsigned int *PORTB = reinterpret_cast<volatile unsigned int *>(0x40010c00);
constexpr volatile unsigned int *PORTC = reinterpret_cast<volatile unsigned int *>(0x40011000);
constexpr volatile unsigned int *PORTD = reinterpret_cast<volatile unsigned int *>(0x40011400);
constexpr volatile unsigned int *PORTE = reinterpret_cast<volatile unsigned int *>(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__

View File

@ -0,0 +1,6 @@
#ifndef __BLUEPILL_RCC_H__
#define __BLUEPILL_RCC_H__
constexpr volatile unsigned int *RCC = reinterpret_cast<volatile unsigned int *>(0x40021000);
#endif // __BLUEPILL_RCC_H__

View File

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

View File

@ -1,4 +1,4 @@
BOARD:
BOARD (STM32F103C8T6):
| |
+-+-+-+-+-+