Created
April 9, 2017 13:28
-
-
Save uxp/35c9d5d51b2229f5c4c2e5b11c3341e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct rcc { | |
volatile unsigned long rc; | |
volatile unsigned long cfg; | |
volatile unsigned long cir; | |
volatile unsigned long apb2; | |
volatile unsigned long apb1; | |
volatile unsigned long ape3; | |
volatile unsigned long ape2; | |
volatile unsigned long ape1; | |
volatile unsigned long bdcr; | |
volatile unsigned long csr; | |
}; | |
#define GPIOA_ENABLE 0x04 | |
#define GPIOB_ENABLE 0x08 | |
#define GPIOC_ENABLE 0x10 | |
#define RCC_BASE (struct rcc *) 0x40021000 | |
struct gpio { | |
volatile unsigned long cr[2]; | |
volatile unsigned long idr; | |
volatile unsigned long odr; | |
volatile unsigned long bsrr; | |
volatile unsigned long brr; | |
volatile unsigned long lock; | |
}; | |
#define GPIOA_BASE (struct gpio *) 0x40010800 | |
#define GPIOB_BASE (struct gpio *) 0x40010C00 | |
#define GPIOC_BASE (struct gpio *) 0x40011000 | |
#define MODE_OUT_2 0x02 | |
#define CONF_GP_UD 0x0 | |
#define CONF_GP_OD 0x4 | |
#define FAST 200 | |
#define SLOW 400 | |
#define SPEED SLOW | |
void | |
delay(void) | |
{ | |
int i; | |
for (i = 0; i < (1000 * SPEED); i++) | |
__asm__("nop"); | |
} | |
struct gpio *gp; | |
unsigned long on_mask; | |
unsigned long off_mask; | |
void | |
led_init( int bit ) | |
{ | |
int conf; | |
int shift; | |
struct rcc *rp = RCC_BASE; | |
/* Turn on GPIO C */ | |
rp->ape2 |= GPIOC_ENABLE; | |
gp = GPIOC_BASE; | |
shift = (bit - 8) * 4; | |
conf = gp->cr[1] & ~(0xf << shift); | |
conf |= (MODE_OUT_2|CONF_GP_OD) << shift; | |
gp->cr[1] = conf; | |
on_mask = 1 << bit; | |
off_mask = 1 << (bit+16); | |
} | |
void | |
led_on( void ) | |
{ | |
gp->bsrr = on_mask; | |
} | |
void led_off( void ) | |
{ | |
gp->bsrr = off_mask; | |
} | |
#define PC13 13 | |
void | |
startup( void ) | |
{ | |
led_init( PC13 ); | |
while(1) { | |
led_on(); | |
delay(); | |
led_off(); | |
delay(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* blink.lds | |
* linker script for blink demo. | |
* | |
* Memory layout on my STM32F103 is pretty much like this: | |
* | |
* 0x00000000 - 0x07ffffff - aliased to flash or sys memory depending on BOOT jumpers | |
* 0x08000000 - 0x0800ffff - Flash (64K ... or 128K) | |
* 0x1ffff000 - 0x1ffff7ff - Boot firmware in system memory | |
* 0x1ffff800 - 0x1fffffff - option bytes | |
* 0x20000000 - 0x20004fff - SRAM (20k) | |
* 0x40000000 - 0x40023400 - peripherals | |
*/ | |
MEMORY | |
{ | |
flash(RX) : ORIGIN = 0x00000000, LENGTH = 128K | |
sram(WAIL) : ORIGIN = 0x20000000, LENGTH = 20K | |
} | |
SECTIONS | |
{ | |
.text : { *(.text*) } > flash | |
.bss : { *(.bss*) } > sram | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* locore.s | |
* Assembler startup file for the STM32F103 | |
* Tom Trebisky 9-24-2016 | |
*/ | |
# The Cortex M3 is a thumb only processor | |
.cpu cortex-m3 | |
.thumb | |
.word 0x20005000 /* stack top address */ | |
.word _reset /* 1 Reset */ | |
.word spin /* 2 NMI */ | |
.word spin /* 3 Hard Fault */ | |
.word spin /* 4 MM Fault */ | |
.word spin /* 5 Bus Fault */ | |
.word spin /* 6 Usage Fault */ | |
.word spin /* 7 RESERVED */ | |
.word spin /* 8 RESERVED */ | |
.word spin /* 9 RESERVED*/ | |
.word spin /* 10 RESERVED */ | |
.word spin /* 11 SV call */ | |
.word spin /* 12 Debug reserved */ | |
.word spin /* 13 RESERVED */ | |
.word spin /* 14 PendSV */ | |
.word spin /* 15 SysTick */ | |
.word spin /* 16 IRQ0 */ | |
.word spin /* 17 IRQ1 */ | |
.word spin /* 18 IRQ2 */ | |
.word spin /* 19 ... */ | |
/* On to IRQ67 */ | |
spin: b spin | |
.thumb_func | |
_reset: | |
bl startup | |
b . | |
/* THE END */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Makefile for blink demo | |
#TOOLS = arm-linux-gnu | |
TOOLS = arm-none-eabi | |
# Assembling with gcc makes it want crt0 at link time. | |
#AS = $(TOOLS)-gcc | |
AS = $(TOOLS)-as | |
# Use the -g flag if you intend to use gdb | |
#CC = $(TOOLS)-gcc -mcpu=cortex-m3 -mthumb | |
CC = $(TOOLS)-gcc -mcpu=cortex-m3 -mthumb -g | |
#LD = $(TOOLS)-gcc | |
LD = $(TOOLS)-ld.bfd | |
OBJCOPY = $(TOOLS)-objcopy | |
DUMP = $(TOOLS)-objdump -d | |
GDB = $(TOOLS)-gdb | |
OBJS = locore.o blink.o | |
all: blink.bin blink.dump | |
blink.dump: blink.elf | |
$(DUMP) blink.elf >blink.dump | |
blink.bin: blink.elf | |
$(OBJCOPY) blink.elf blink.bin -O binary | |
blink.elf: $(OBJS) | |
$(LD) -T blink.lds -o blink.elf $(OBJS) | |
locore.o: locore.s | |
$(AS) locore.s -o locore.o | |
blink.o: blink.c | |
$(CC) -c blink.c | |
# To burn the image: | |
# Connect the STLINK gadget to the target. | |
# Be sure and start openOCD in this directory. | |
# (or else it won't be able to find the bin file). | |
# Then use "make flash" to start an openocd console session. | |
# Type: reset halt | |
# Type: flash write_image erase blink.bin 0x08000000 | |
flash: | |
@echo "type: flash write_image erase blink.bin 0x08000000" | |
telnet localhost 4444 | |
gdb: | |
$(GDB) --eval-command="target remote localhost:3333" blink.elf | |
gdbtui: | |
$(GDB) -tui --eval-command="target remote localhost:3333" blink.elf | |
clean: | |
rm -f *.o blink.elf blink.bin blink.dump |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice minimal example, wish they'd release examples like this.