From 5d17a976070be9d66349da0ddd28361884f3461d Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 26 Sep 2021 12:50:49 +0200 Subject: Link against libopencm3 --- Makefile | 23 +++++++++++++++++------ README.md | 5 +++++ ld/stm32.ld | 32 -------------------------------- ld/stm32f103c8t6.ld | 32 ++++++++++++++++++++++++++++++++ src/main.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/test_program.c | 8 -------- 6 files changed, 101 insertions(+), 46 deletions(-) delete mode 100644 ld/stm32.ld create mode 100644 ld/stm32f103c8t6.ld create mode 100644 src/main.c delete mode 100644 src/test_program.c diff --git a/Makefile b/Makefile index 4ef61dd..0b7e9c0 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,23 @@ -.PHONY: all clean +OBJS = main.o startup.o + +# tools +PREFIX ?= arm-none-eabi +CC = $(PREFIX)-gcc +LD = $(PREFIX)-gcc +OBJCOPY = $(PREFIX)-objcopy -all: +all: deps/libopencm3/lib/libopencm3_stm32f1.a mkdir build - arm-none-eabi-gcc -O0 -c -g -mcpu=cortex-m3 -mthumb -o build/test_program.o src/test_program.c - arm-none-eabi-gcc -O0 -c -g -mcpu=cortex-m3 -mthumb -o build/startup.o src/startup.c - arm-none-eabi-ld -Tld/stm32.ld -o build/test_program.elf build/startup.o build/test_program.o - arm-none-eabi-objcopy -O binary build/test_program.elf build/test_program.bin + $(CC) -O0 -c -g -DSTM32F1 -Ideps/libopencm3/include -mcpu=cortex-m3 -mthumb -o build/main.o src/main.c + $(CC) -O0 -c -g -DSTM32F1 -Ideps/libopencm3/include -mcpu=cortex-m3 -mthumb -o build/startup.o src/startup.c + $(LD) --static -nostartfiles -mcpu=cortex-m3 -mthumb -msoft-float -Tld/stm32f103c8t6.ld -Ldeps/libopencm3/lib build/startup.o build/main.o -lopencm3_stm32f1 -o build/ledcontrol.elf + $(OBJCOPY) -O binary build/ledcontrol.elf build/ledcontrol.bin + +deps/libopencm3/lib/libopencm3_stm32f1.a: + make -C deps/libopencm3 clean: rm -rf build + +.PHONY: all clean diff --git a/README.md b/README.md index dad276d..0306080 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,8 @@ A firmware project for the STM32F103C8T6 to control LED strips. +## External References + +- Nice [Tutorial](https://jacobmossberg.se/posts/2018/08/11/run-c-program-bare-metal-on-arm-cortex-m3.html) about C programs on bare-metal ARM chips +- Another useful [Tutorial](https://www.rhye.org/post/stm32-with-opencm3-0-compiling-and-uploading/) about STM32 programming with libopencm3 + diff --git a/ld/stm32.ld b/ld/stm32.ld deleted file mode 100644 index 9f74206..0000000 --- a/ld/stm32.ld +++ /dev/null @@ -1,32 +0,0 @@ -SECTIONS -{ - . = 0x0; /* From 0x00000000 */ - - .text : - { - *(vectors) /* Vector table */ - *(.text) /* Program code */ - } - .rodata : - { - *(.rodata) /* Read only data */ - } - _DATA_ROM_START = .; - - . = 0x20000000; /* From 0x20000000 */ - - _DATA_RAM_START = .; - .data : AT(_DATA_ROM_START) - { - *(.data) /* Data memory */ - } - _DATA_RAM_END = .; - - _BSS_START = .; /* Indicates where BSS section starts in RAM */ - .bss : - { - *(.bss) /* Zero-filled run time allocate data memory */ - } - _BSS_END = .; /* Indicates where BSS section ends in RAM */ -} - diff --git a/ld/stm32f103c8t6.ld b/ld/stm32f103c8t6.ld new file mode 100644 index 0000000..9f74206 --- /dev/null +++ b/ld/stm32f103c8t6.ld @@ -0,0 +1,32 @@ +SECTIONS +{ + . = 0x0; /* From 0x00000000 */ + + .text : + { + *(vectors) /* Vector table */ + *(.text) /* Program code */ + } + .rodata : + { + *(.rodata) /* Read only data */ + } + _DATA_ROM_START = .; + + . = 0x20000000; /* From 0x20000000 */ + + _DATA_RAM_START = .; + .data : AT(_DATA_ROM_START) + { + *(.data) /* Data memory */ + } + _DATA_RAM_END = .; + + _BSS_START = .; /* Indicates where BSS section starts in RAM */ + .bss : + { + *(.bss) /* Zero-filled run time allocate data memory */ + } + _BSS_END = .; /* Indicates where BSS section ends in RAM */ +} + diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..7a5290f --- /dev/null +++ b/src/main.c @@ -0,0 +1,47 @@ + + +#include +#include + + +#define DELAY 1000000 + + +void clock_init(void); +void gpio_init(void); +void delay(void); + + +int main(void) +{ + clock_init(); + gpio_init(); + + while(1){ + gpio_toggle(GPIOC, GPIO13); + delay(); + } + + return 0; +} + + +void clock_init(void) +{ + rcc_periph_clock_enable(RCC_GPIOC); // for PC13 blinking +} + + +void gpio_init(void) +{ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13); + gpio_set(GPIOC, GPIO13); +} + + +void delay(void) +{ + for (int i = 0; i < DELAY; i++) { + __asm__("nop"); + } +} diff --git a/src/test_program.c b/src/test_program.c deleted file mode 100644 index 80f569b..0000000 --- a/src/test_program.c +++ /dev/null @@ -1,8 +0,0 @@ -static const int a = 7; -static int b = 8; -static int sum; - -void main() -{ - sum = a + b; -} -- cgit v1.2.3-70-g09d2