Skip to content

Commit 00744f3

Browse files
committed
removed standard system startup
1 parent 569e0d2 commit 00744f3

File tree

2 files changed

+45
-40
lines changed

2 files changed

+45
-40
lines changed

bootloader_F1/Makefile

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ VECTOR_TABLE_OFFSET = 0x0000
66

77
C_SRCS = Src/main.c Src/usb.c Src/hid.c Src/led.c Src/flash.c
88

9-
DEVICE_ASM_SRCS += CMSIS/Device/ST/STM32F10x/Source/Templates/gcc/startup_stm32f10x_md.s
109
DEVICE_C_SRCS += CMSIS/Device/ST/STM32F10x/Source/Templates/system_stm32f10x.c
1110

1211
# Be silent per default, but 'make V=1' will show all compiler calls.
@@ -25,12 +24,6 @@ C_SRCS += $(DEVICE_C_SRCS)
2524
SRCS += $(C_SRCS)
2625
vpath %.c $(sort $(dir $(C_SRCS)))
2726
OBJS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SRCS:.c=.o)))
28-
ASM_SRCS += $(DEVICE_ASM_SRCS)
29-
SRCS += $(ASM_SRCS)
30-
vpath %.s $(sort $(dir $(DEVICE_ASM_SRCS)))
31-
OBJS += $(addprefix $(BUILD_DIR)/,$(notdir $(DEVICE_ASM_SRCS:.s=.o)))
32-
33-
ASFLAGS += -mcpu=cortex-m3 -mthumb
3427

3528
CFLAGS += -mcpu=cortex-m3 -mthumb -Wall -Os
3629
CFLAGS += -std=gnu99
@@ -55,7 +48,6 @@ LDFLAGS += -T$(LINKER_SCRIPT)
5548
# Tool paths.
5649
PREFIX ?= arm-none-eabi-
5750
CC = $(PREFIX)gcc
58-
AS = $(PREFIX)as
5951
LD = $(PREFIX)ld
6052
OBJDUMP = $(PREFIX)objdump
6153
OBJCOPY = $(PREFIX)objcopy
@@ -185,14 +177,6 @@ $(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
185177
$(ECHO) "CC $<"
186178
$(Q)$(CC) $(CFLAGS) -c "$<" -o "$@"
187179

188-
$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
189-
$(ECHO) "AS $<"
190-
$(Q)$(AS) $(ASFLAGS) -c "$<" -o "$@"
191-
192-
$(BUILD_DIR)/%.s : %.c
193-
$(ECHO) "CC $<"
194-
$(Q)$(CC) -S $(CFLAGS) $< -o $@
195-
196180
output: $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
197181

198182
$(BUILD_DIR)/$(TARGET).hex: $(BUILD_DIR)/$(TARGET).elf

bootloader_F1/Src/main.c

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,32 @@
3838
/* SRAM size */
3939
#define SRAM_SIZE (20 * 1024)
4040

41+
/* SRAM end (bottom of stack) */
42+
#define SRAM_END (SRAM_BASE + SRAM_SIZE)
43+
4144
/* HID Bootloader takes 4 kb flash. */
4245
#define USER_PROGRAM (FLASH_BASE + BOOTLOADER_SIZE)
4346

47+
/* Simple function pointer type to call user program */
4448
typedef void (*funct_ptr)(void);
4549

46-
extern void Reset_Handler(void);
50+
/* The bootloader entry point gunction prototype */
51+
void Reset_Handler(void);
52+
53+
/* The SRAM vector table The initializer is to put this array in the
54+
* .data section at the begining of SRAM
55+
*/
56+
uint32_t RamVectors[37] __attribute__((section(".data")));
4757

48-
void _init(void);
49-
void __libc_init_array(void);
58+
/* Minimal initial Flash-based vector table */
59+
uint32_t *VectorTable[] __attribute__((section(".isr_vector"))) = {
5060

51-
uint32_t RamVectors[37] = {1};
61+
/* Initial stack pointer (MSP) */
62+
(uint32_t *) SRAM_END,
63+
64+
/* Reset handler */
65+
(uint32_t *) Reset_Handler
66+
};
5267

5368
static void delay(uint32_t tmr) {
5469
for (uint32_t i = 0; i < tmr; i++) {
@@ -73,8 +88,8 @@ static bool check_flash_complete(void) {
7388
return false;
7489
}
7590

76-
static bool check_user_code(u32 usrAddr) {
77-
u32 sp = *(vu32 *) usrAddr;
91+
static bool check_user_code(uint32_t usrAddr) {
92+
uint32_t sp = *(volatile uint32_t *) usrAddr;
7893

7994
/* Check if the stack pointer in the vector table points in
8095
RAM */
@@ -88,7 +103,7 @@ static bool check_user_code(u32 usrAddr) {
88103
static uint16_t get_and_clear_magic_word(void) {
89104

90105
/* Enable the power and backup interface clocks by setting the
91-
* PWREN and BKPEN bitsin the RCC_APB1ENR register
106+
* PWREN and BKPEN bits in the RCC_APB1ENR register
92107
*/
93108
SET_BIT(RCC->APB1ENR, RCC_APB1ENR_BKPEN | RCC_APB1ENR_PWREN);
94109
uint16_t value = BKP->DR10;
@@ -103,23 +118,23 @@ static uint16_t get_and_clear_magic_word(void) {
103118
return value;
104119
}
105120

106-
void _init(void) {
107-
}
108-
109-
void __libc_init_array(void) {
110-
}
121+
void Reset_Handler(void) {
111122

112-
int main(int argc, char *argv[]) {
113-
(void) argc;
114-
(void) argv;
123+
/* Setup the system clock (System clock source, PLL Multiplier
124+
* factors, AHB/APBx prescalers and Flash settings)
125+
*/
126+
SystemInit();
115127

116-
RamVectors[0] = SRAM_BASE + SRAM_SIZE;
128+
/* Setup to vector table in SRAM, so we can handle USB IRQs */
129+
RamVectors[0] = SRAM_END;
117130
RamVectors[1] = (uint32_t) Reset_Handler;
118131
RamVectors[36] = (uint32_t) USB_LP_CAN1_RX0_IRQHandler;
119132
SCB->VTOR = (volatile uint32_t) RamVectors;
120133

134+
/* Check for a magic word in BACKUP memory */
121135
uint16_t magic_word = get_and_clear_magic_word();
122136

137+
/* Initialize GPIOs */
123138
pins_init();
124139

125140
/* Wait 1uS so the pull-up settles... */
@@ -131,19 +146,25 @@ int main(int argc, char *argv[]) {
131146

132147
UploadStarted = false;
133148
UploadFinished = false;
134-
uint32_t userProgramAddress = *(volatile uint32_t *) (USER_PROGRAM + 0x04);
135-
funct_ptr userProgram = (funct_ptr) userProgramAddress;
136-
137-
/* If PB2 (BOOT 1 pin) is HIGH enter HID bootloader or no User
138-
* Code is uploaded to the MCU or <Battery Backed RAM
139-
* Register> was set from Arduino IDE exit from USB Serial
140-
* mode and go to HID mode ...
149+
funct_ptr UserProgram = (funct_ptr) *(volatile uint32_t *) (USER_PROGRAM + 0x04);
150+
151+
/* If:
152+
* - PB2 (BOOT 1 pin) is HIGH or
153+
* - no User Code is uploaded to the MCU or
154+
* - a magic word was stored in the battery-backed RAM
155+
* registers from the Arduino IDE
156+
* then enter HID bootloader...
141157
*/
142158
if ((magic_word == 0x424C) ||
143159
(GPIOB->IDR & GPIO_IDR_IDR2) ||
144160
(check_user_code(USER_PROGRAM) == false)) {
145161
if (magic_word == 0x424C) {
146162

163+
/* If a magic word was stored in the
164+
* battery-backed RAM registers from the
165+
* Arduino IDE, exit from USB Serial mode and
166+
* go to HID mode...
167+
*/
147168
#if defined HAS_LED2_PIN
148169
led2_on();
149170
#endif
@@ -178,7 +199,7 @@ int main(int argc, char *argv[]) {
178199
//CLEAR_BIT(RCC->APB2ENR, RCC_APB2ENR_IOPBEN);
179200
SCB->VTOR = USER_PROGRAM;
180201
__set_MSP((*(volatile uint32_t *) USER_PROGRAM));
181-
userProgram();
202+
UserProgram();
182203
for (;;) {
183204
;
184205
}

0 commit comments

Comments
 (0)