Skip to content

Commit 831ff94

Browse files
committed
Implemented basic bootloader
1 parent b6c4da5 commit 831ff94

File tree

12 files changed

+893
-3
lines changed

12 files changed

+893
-3
lines changed

firmware/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22
# This program is free software; you can redistribute it and/or modify
33
# it under the terms of the GNU General Public License version 3.
44

5+
BOOT_DIR=bootloader
56
PROG_DIR=programmer
67

78
all:
9+
$(MAKE) -C $(BOOT_DIR)
810
$(MAKE) -C $(PROG_DIR)
911

1012
clean:
13+
$(MAKE) -C $(BOOT_DIR) clean
1114
$(MAKE) -C $(PROG_DIR) clean
1215

1316
distclean:
17+
$(MAKE) -C $(BOOT_DIR) distclean
1418
$(MAKE) -C $(PROG_DIR) distclean
1519

1620
program:
21+
$(MAKE) -C $(BOOT_DIR) program
1722
$(MAKE) -C $(PROG_DIR) program

firmware/bootloader/Makefile

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Copyright (C) 2017 Bogdan Bogush <[email protected]>
2+
# This program is free software; you can redistribute it and/or modify
3+
# it under the terms of the GNU General Public License version 3.
4+
5+
PROG_NAME=bootloader_fw
6+
7+
SRC_DIR=./
8+
OBJ_DIR=obj/
9+
LIB_DIR=../libs/
10+
TOOLCHAIN=../../../compiler/gcc-arm-none-eabi-4_9-2015q1/bin/arm-none-eabi-
11+
PROG=$(OBJ_DIR)$(PROG_NAME)
12+
13+
SPL_PATH=$(LIB_DIR)spl/
14+
SPL_DEVICE_SUPPORT_PATH=$(SPL_PATH)CMSIS/CM3/DeviceSupport/ST/STM32F10x/
15+
SPL_CORE_SUPPORT=$(SPL_PATH)CMSIS/CM3/CoreSupport/
16+
SPL_PERIPH_PATH=$(SPL_PATH)STM32F10x_StdPeriph_Driver/
17+
SPL_LIB=stm32f10x
18+
SPL_CONFIG_FILE=$(SPL_PATH)stm32f10x_conf.h
19+
SPL_FLAGS=-DSTM32F10X_HD -DCONFIG_BOOTLOADER
20+
21+
CC=$(TOOLCHAIN)gcc
22+
OBJCOPY=$(TOOLCHAIN)objcopy
23+
OBJDUMP=$(TOOLCHAIN)objdump
24+
SIZE=$(TOOLCHAIN)size
25+
26+
INCLUDES=-include$(SPL_CONFIG_FILE)
27+
INCLUDES+=-I$(SPL_CORE_SUPPORT)
28+
INCLUDES+=-I$(SPL_DEVICE_SUPPORT_PATH)
29+
INCLUDES+=-I$(SPL_PATH)
30+
INCLUDES+=-I$(SPL_PERIPH_PATH)inc
31+
INCLUDES+=-I$(SRC_DIR)
32+
33+
CFLAGS=-g -Wall -Werror -O3
34+
CFLAGS+=$(INCLUDES) -MMD -MP
35+
CFLAGS+=-ffunction-sections -fdata-sections
36+
CFLAGS+=-mcpu=cortex-m3 -mthumb
37+
CFLAGS+=$(SPL_FLAGS)
38+
39+
LDFLAGS=-mcpu=cortex-m3 -mthumb -Wl,--gc-sections -Wl,-Map=$(PROG).map
40+
41+
vpath %.c $(SRC_DIR) $(SPL_DEVICE_SUPPORT_PATH) $(SRC_BSP_DIR)
42+
vpath %.s $(SRC_DIR)
43+
44+
STARTUP=startup_stm32f10x_hd.s
45+
46+
SRCS=main.c system_stm32f10x.c syscalls.c uart.c
47+
48+
OBJS=$(addprefix $(OBJ_DIR),$(SRCS:.c=.o)) \
49+
$(addprefix $(OBJ_DIR),$(STARTUP:.s=.o))
50+
DEPS=$(OBJS:%.o=%.d)
51+
52+
LINKER_SCRIPT=$(SRC_DIR)stm32_flash.ld
53+
54+
all: lib dirs $(PROG).elf
55+
56+
lib:
57+
$(MAKE) -C $(SPL_PATH)
58+
59+
dirs:
60+
mkdir -p $(OBJ_DIR)
61+
62+
$(PROG).elf: $(OBJS)
63+
$(CC) $(LDFLAGS) -o $@ $^ -L$(SPL_PATH) -l$(SPL_LIB) -T$(LINKER_SCRIPT)
64+
$(OBJCOPY) -O ihex $(PROG).elf $(PROG).hex
65+
$(OBJCOPY) -O binary $(PROG).elf $(PROG).bin
66+
$(OBJDUMP) -St $(PROG).elf > $(PROG).lst
67+
$(SIZE) $(PROG).elf
68+
69+
$(OBJ_DIR)%.o: %.c
70+
$(CC) -c $(CFLAGS) $< -o $@
71+
72+
$(OBJ_DIR)%.o: %.s
73+
$(CC) -c $(CFLAGS) $< -o $@
74+
75+
-include $(DEPS)
76+
77+
clean:
78+
rm -rf $(OBJ_DIR)
79+
80+
distclean: clean
81+
$(MAKE) -C $(SPL_PATH) clean
82+
83+
program:
84+
st-flash write $(PROG).bin 0x8000000

firmware/bootloader/main.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* Copyright (C) 2017 Bogdan Bogush <[email protected]>
2+
* This program is free software; you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License version 3.
4+
*/
5+
6+
#include "uart.h"
7+
#include "version.h"
8+
#include <stdio.h>
9+
#include <string.h>
10+
11+
/* Flash layout
12+
* ------------ 0x08000000
13+
* |bootloader |
14+
* | 16K |
15+
* ------------ 0x08004000
16+
* |image1 |
17+
* |120K |
18+
* -------------0x08022000
19+
* |image2 |
20+
* |120K |
21+
* -------------0x08040000
22+
*/
23+
24+
#define STR(a) #a
25+
#define MAKE_STR(a) STR(a)
26+
#define VERSION "\r\nBootloader ver: " MAKE_STR(SW_VERSION_MAJOR) "." \
27+
MAKE_STR(SW_VERSION_MINOR) "." MAKE_STR(SW_VERSION_BUILD) "\r\n"
28+
29+
#define APP_ADDRESS_OFFSET 0x4000
30+
#define APP_ADDRESS (FLASH_BASE + APP_ADDRESS_OFFSET)
31+
32+
typedef void (*app_func_t)(void);
33+
34+
int main()
35+
{
36+
app_func_t app;
37+
uint32_t jump_addr;
38+
39+
uart_init();
40+
print(VERSION);
41+
42+
/* Relocate vector table */
43+
NVIC_SetVectorTable(NVIC_VectTab_FLASH, APP_ADDRESS_OFFSET);
44+
/* Set application address */
45+
jump_addr = *(__IO uint32_t *)(APP_ADDRESS + 4);
46+
app = (app_func_t)jump_addr;
47+
/* Initialize application's Stack Pointer */
48+
__set_MSP(*(__IO uint32_t *)APP_ADDRESS);
49+
/* Start application */
50+
app();
51+
52+
return 0;
53+
}

0 commit comments

Comments
 (0)