Skip to content

Commit 42668c4

Browse files
committed
Add example firmware
1 parent 46c9ead commit 42668c4

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[submodule "firmware/libopencm3"]
2+
path = firmware/libopencm3
3+
url = https://github.com/libopencm3/libopencm3
4+
branch = 78c23ba5

firmware/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
firmware.*
2+
*.o

firmware/Makefile

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
##
2+
## Copyright (C) 2020 Marc Schink <[email protected]>
3+
##
4+
## This program is free software: you can redistribute it and/or modify
5+
## it under the terms of the GNU General Public License as published by
6+
## the Free Software Foundation, either version 3 of the License, or
7+
## (at your option) any later version.
8+
##
9+
## This program is distributed in the hope that it will be useful,
10+
## but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
## GNU General Public License for more details.
13+
##
14+
## You should have received a copy of the GNU General Public License
15+
## along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
##
17+
18+
PREFIX ?= arm-none-eabi-
19+
CC = $(PREFIX)gcc
20+
OBJCOPY = $(PREFIX)objcopy
21+
22+
OPENOCD ?= openocd
23+
24+
FP_FLAGS = -msoft-float
25+
ARCH_FLAGS = -mcpu=cortex-m3 -mthumb $(FP_FLAGS)
26+
27+
LD_SCRIPT = libopencm3/lib/stm32/f1/stm32f100xb.ld
28+
29+
LDFLAGS = -L./libopencm3/lib
30+
LDFLAGS += --static -nostartfiles
31+
LDFLAGS += -T$(LD_SCRIPT)
32+
33+
CFLAGS = $(ARCH_FLAGS)
34+
CFLAGS += -std=c11 -g3 -O0
35+
CFLAGS += -I./libopencm3/include
36+
CFLAGS += -fno-common -ffunction-sections -fdata-sections
37+
CFLAGS += -DSTM32F1
38+
39+
LDLIBS = -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group
40+
LDLIBS += -lopencm3_stm32f1
41+
42+
FIRMWARE_ELF = firmware.elf
43+
FIRMWARE_BIN = firmware.bin
44+
FIRMWARE_HEX = firmware.hex
45+
46+
all: $(FIRMWARE_ELF) $(FIRMWARE_BIN) $(FIRMWARE_HEX)
47+
48+
$(FIRMWARE_ELF): main.c libopencm3/lib/libopencm3_stm32f1.a
49+
$(CC) $(CFLAGS) $(LDFLAGS) main.c $(LDLIBS) -o firmware.elf
50+
51+
libopencm3/Makefile:
52+
git submodule update --init
53+
54+
libopencm3/lib/libopencm3_%.a: libopencm3/Makefile
55+
$(MAKE) -C libopencm3 TARGETS=stm32/f1
56+
57+
$(FIRMWARE_BIN): $(FIRMWARE_ELF)
58+
$(OBJCOPY) -O binary $< $@
59+
60+
$(FIRMWARE_HEX): $(FIRMWARE_ELF)
61+
$(OBJCOPY) -O ihex $< $@
62+
63+
flash: $(FIRMWARE_ELF)
64+
$(OPENOCD) -f interface/jlink.cfg -c "transport select swd" -f target/stm32f1x.cfg -c "program $(FIRMWARE_ELF) verify reset exit"
65+
66+
clean:
67+
$(RM) $(FIRMWARE_ELF) $(FIRMWARE_BIN) $(FIRMWARE_HEX)
68+
69+
distclean: clean
70+
$(MAKE) -C libopencm3 clean
71+
72+
.PHONY: all flash clean distclean

firmware/libopencm3

Submodule libopencm3 added at 78c23ba

firmware/main.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (C) 2020 Marc Schink <[email protected]>
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include <stdbool.h>
19+
20+
#include <libopencm3/stm32/rcc.h>
21+
#include <libopencm3/stm32/gpio.h>
22+
23+
#define LED2_GPIO GPIOA
24+
#define LED2_PIN GPIO5
25+
26+
char *text = "This is some secret data stored in the flash memory together with the firmware. Exception(al) failure...!";
27+
28+
int main(void)
29+
{
30+
rcc_periph_clock_enable(RCC_GPIOA);
31+
32+
gpio_set_mode(LED2_GPIO, GPIO_MODE_OUTPUT_2_MHZ,
33+
GPIO_CNF_OUTPUT_PUSHPULL, LED2_PIN);
34+
gpio_clear(LED2_GPIO, LED2_PIN);
35+
36+
while (true) {
37+
for (uint32_t i = 0; i < (1 << 18); i++) {
38+
__asm__("nop");
39+
}
40+
41+
gpio_toggle(LED2_GPIO, LED2_PIN);
42+
}
43+
}

0 commit comments

Comments
 (0)