Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 41 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ CLANG_INPUTS := $(filter-out $(IGNORED_CLANG_INPUTS), $(CLANG_INPUTS))

MAKEFILE_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
BEAR_ENABLE ?= 1
FIRMWARE_ROLE ?= app
BOOTLOADER_SIZE_KB ?= 64

######################################
# target
Expand All @@ -47,27 +49,9 @@ SERIES_GENERIC_CAP = STM32$(SERIES_CAP)xx
SERIES_LINE = stm32$(SERIES)$(LINE)
SERIES_LINE_CAP = STM32$(SERIES_CAP)$(LINE)

# Generate a list of STM32 variant patterns:
# 1. Remove the '.h' extension from filenames.
# 2. Replace 'x' with '.' for regex matching.
MCU_MATCHES = $(shell ls stm/$(SERIES_GENERIC)/CMSIS/Device/ST/$(SERIES_GENERIC_CAP)/Include \
| sed 's/\.h//g; s/x/./g')

# Find the generic STM32 series variant:
# 1. Iterate over MCU_MATCHES.
# 2. Check if the entry is exactly 11 characters long.
# 3. Match it against the pattern 'stm32$(SERIES_LINE)$(EXTRA_CUT)'.
# 4. Replace '.' with 'x' in the matched entry.
# 5. Break on the first valid match.
SERIES_LINE_GENERIC = $(shell \
for match in $(MCU_MATCHES); do \
if [ $${#match} -eq 11 ] && echo "stm32$(SERIES_LINE)$(EXTRA_CUT)" | grep -qE "$$match"; then \
echo "$${match//./x}"; \
break; \
fi; \
done)

SERIES_LINE_GENERIC_CAP = $(shell echo $(SERIES_LINE_GENERIC) | sed 's/[^x]/\U&/g')
SERIES_LINE_GENERIC = $(SERIES_LINE)xx

SERIES_LINE_GENERIC_CAP = $(shell python3 -c 's="$(SERIES_LINE_GENERIC)"; print(s[:-2].upper() + s[-2:])')

ifeq ($(strip $(SERIES_LINE_GENERIC)),)
$(error SERIES_LINE_GENERIC is not found in stm/$(SERIES_GENERIC)/CMSIS/Device/ST/$(SERIES_GENERIC_CAP)/Include. Please check the target configuration.)
Expand Down Expand Up @@ -104,19 +88,32 @@ FATFS_PATH := middleware/FatFs
# source
######################################
# C sources
C_SOURCES = \
$(PROJECT_C_SOURCES) \
COMMON_STM_SOURCES = \
$(filter-out %template.c, $(wildcard stm/$(SERIES_GENERIC)/$(SERIES_GENERIC_CAP)_HAL_Driver/Src/*.c)) \
stm/$(SERIES_GENERIC)/system_$(SERIES_GENERIC).c \
stm/$(SERIES_GENERIC)/$(SERIES_GENERIC)_hal_init.c \
stm/$(SERIES_GENERIC)/$(SERIES_GENERIC)_hal_timebase_tim.c \
stm/$(SERIES_GENERIC)/$(SERIES_GENERIC)_hal_timebase_tim.c

APP_ROLE_SOURCES = \
$(PROJECT_C_SOURCES) \
$(wildcard $(FREERTOS_PATH)/*.c) \
$(FREERTOS_PATH)/portable/GCC/ARM_CM4F/port.c \
$(wildcard common/Src/*.c) \
$(wildcard driver/Src/*.c) \
$(wildcard $(FATFS_PATH)/Src/*.c) \
$(filter-out $(addprefix bsp/Src/,$(addsuffix .c,$(BSP_DISABLE))),$(wildcard bsp/Src/*.c))

BOOTLOADER_ROLE_SOURCES = \
$(wildcard bootloader/Src/*.c) \
common/Src/uart_bootloader.c \
common/Src/stubs.c

ifeq ($(FIRMWARE_ROLE),bootloader)
C_SOURCES = $(BOOTLOADER_ROLE_SOURCES) $(COMMON_STM_SOURCES)
else
C_SOURCES = $(APP_ROLE_SOURCES) $(COMMON_STM_SOURCES)
endif


# ASM sources
ASM_SOURCES = \
Expand Down Expand Up @@ -171,6 +168,10 @@ USE_HAL_DRIVER \
$(SERIES_LINE_GENERIC_CAP) \
$(SERIES_GENERIC_CAP)

ifeq ($(FIRMWARE_ROLE),bootloader)
C_DEFS += FIRMWARE_ROLE_BOOTLOADER
endif

C_DEFS := $(addprefix -D,$(C_DEFS))

# AS includes
Expand All @@ -190,6 +191,10 @@ driver/Inc \
bsp/Inc \
middleware

ifeq ($(FIRMWARE_ROLE),bootloader)
C_INCLUDES += bootloader/Inc
endif

C_INCLUDES := $(addprefix -I,$(C_INCLUDES))

# compile gcc flags
Expand All @@ -208,7 +213,13 @@ CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
# LDFLAGS
#######################################
# link script
LDSCRIPT = stm/$(SERIES_GENERIC)/$(SERIES_LINE)/$(SERIES_LINE_CAP)$(EXTRA_CAP)x_FLASH.ld
ORIG_LDSCRIPT = stm/$(SERIES_GENERIC)/$(SERIES_LINE)/$(SERIES_LINE_CAP)$(EXTRA_CAP)x_FLASH.ld
GENERATED_LDSCRIPT = $(BUILD_DIR)/$(TARGET)_$(FIRMWARE_ROLE).ld
LDSCRIPT = $(ORIG_LDSCRIPT)

ifeq ($(FIRMWARE_ROLE),app)
LDSCRIPT = $(GENERATED_LDSCRIPT)
endif

# libraries
LIBS =
Expand Down Expand Up @@ -274,7 +285,7 @@ else
@echo "AS $< -> $@"
endif

$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile $(LDSCRIPT)
@if ls $(BUILD_DIR)/*.elf 1> /dev/null 2>&1; then \
rm -rf $(BUILD_DIR)/stm*.elf; \
fi
Expand Down Expand Up @@ -320,6 +331,10 @@ endif
$(BUILD_DIR):
mkdir -p $@

$(GENERATED_LDSCRIPT): $(ORIG_LDSCRIPT) Makefile | $(BUILD_DIR)
@python3 -c 'import pathlib; ld = pathlib.Path("$(ORIG_LDSCRIPT)").read_text(); size_kb = int("$(BOOTLOADER_SIZE_KB)"); flash_origin = 0x08000000 + size_kb * 1024; flash_length_kb = 512 - size_kb; ld = ld.replace("FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 512K", f"FLASH (rx) : ORIGIN = 0x{flash_origin:08x}, LENGTH = {flash_length_kb}K"); pathlib.Path("$(GENERATED_LDSCRIPT)").write_text(ld)'
Comment thread
ParthivS20 marked this conversation as resolved.
Outdated
@echo "LD_SCRIPT $(ORIG_LDSCRIPT) -> $(GENERATED_LDSCRIPT) ($(FIRMWARE_ROLE), FLASH=0x$$(printf '%x' $$((0x08000000 + $(BOOTLOADER_SIZE_KB)*1024))), LENGTH=$$((512-$(BOOTLOADER_SIZE_KB)))K)"

#######################################
# clean up
#######################################
Expand Down
46 changes: 46 additions & 0 deletions bootloader/Inc/bootloader_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef BOOTLOADER_CONFIG_H_
#define BOOTLOADER_CONFIG_H_

#include "bootloader_hal.h"

#ifndef BOOTLOADER_APP_BASE
#define BOOTLOADER_APP_BASE (0x08010000UL)
#endif

#ifndef BOOTLOADER_APP_MAX_SIZE
#define BOOTLOADER_APP_MAX_SIZE (448UL * 1024UL)
#endif

#ifndef BOOTLOADER_UART_BAUD
#define BOOTLOADER_UART_BAUD (115200U)
#endif

#ifndef BOOTLOADER_UART_INSTANCE
#if defined(USART3)
#define BOOTLOADER_UART_INSTANCE USART3
#elif defined(USART2)
#define BOOTLOADER_UART_INSTANCE USART2
#elif defined(USART1)
#define BOOTLOADER_UART_INSTANCE USART1
#else
#error "No supported UART instance available for bootloader."
#endif
#endif

#ifndef BOOTLOADER_WRITE_CHUNK_MAX
#define BOOTLOADER_WRITE_CHUNK_MAX (128U)
#endif

#ifndef BOOTLOADER_HANDSHAKE_TIMEOUT_MS
#define BOOTLOADER_HANDSHAKE_TIMEOUT_MS (0U)
#endif

#ifndef BOOTLOADER_APP_STARTUP_WAIT_MS
#define BOOTLOADER_APP_STARTUP_WAIT_MS (3000U)
#endif

#ifndef BOOTLOADER_POST_FLASH_BOOT_DELAY_MS
#define BOOTLOADER_POST_FLASH_BOOT_DELAY_MS (2000U)
#endif

#endif
14 changes: 14 additions & 0 deletions bootloader/Inc/bootloader_hal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef BOOTLOADER_HAL_H_
#define BOOTLOADER_HAL_H_

#if defined(STM32F4xx)
#include "stm32f4xx_hal.h"
#elif defined(STM32L4xx)
#include "stm32l4xx_hal.h"
#elif defined(STM32G4xx)
#include "stm32g4xx_hal.h"
#else
#error "Unsupported STM32 series for bootloader."
#endif

#endif
18 changes: 18 additions & 0 deletions bootloader/Inc/bootloader_indicator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef BOOTLOADER_INDICATOR_H_
#define BOOTLOADER_INDICATOR_H_

#include <stdint.h>

typedef enum {
BOOTLOADER_INDICATOR_NO_APP = 0,
BOOTLOADER_INDICATOR_APP_PRESENT,
BOOTLOADER_INDICATOR_CONNECTED,
BOOTLOADER_INDICATOR_FLASHING,
BOOTLOADER_INDICATOR_ERROR,
} bootloader_indicator_mode_t;

void bootloader_indicator_init(void);
void bootloader_indicator_set_mode(bootloader_indicator_mode_t mode);
void bootloader_indicator_update(uint32_t tick_ms);

#endif
18 changes: 18 additions & 0 deletions bootloader/Inc/bootloader_runtime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef BOOTLOADER_RUNTIME_H_
#define BOOTLOADER_RUNTIME_H_

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

void bootloader_runtime_init(void);
bool bootloader_runtime_wait_for_handshake(uint32_t timeout_ms);
bool bootloader_runtime_poll_sync(uint32_t timeout_ms);
bool bootloader_runtime_send_bytes(const uint8_t *data, uint16_t len);
bool bootloader_runtime_read_bytes(uint8_t *data, uint16_t len, uint32_t timeout_ms);
bool bootloader_runtime_erase_app(void);
bool bootloader_runtime_write_app(uint32_t app_offset, const uint8_t *data, size_t len);
bool bootloader_runtime_is_app_valid(void);
void bootloader_runtime_jump_to_app(void);

#endif
80 changes: 80 additions & 0 deletions bootloader/Src/bootloader_indicator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "bootloader_indicator.h"

#include "bootloader_hal.h"

#include <stdbool.h>

#if defined(STM32L432xx)
#define LED_PIN GPIO_PIN_3
#define LED_PORT GPIOB
#elif defined(STM32L431xx)
#define LED_PIN GPIO_PIN_11
#define LED_PORT GPIOB
#elif defined(STM32G473xx)
#define LED_PIN GPIO_PIN_3
#define LED_PORT GPIOC
#else
#define LED_PIN GPIO_PIN_5
#define LED_PORT GPIOA
#endif

static bootloader_indicator_mode_t s_mode = BOOTLOADER_INDICATOR_NO_APP;

static void bootloader_indicator_enable_port_clock(void) {
if (LED_PORT == GPIOA) {
__HAL_RCC_GPIOA_CLK_ENABLE();
} else if (LED_PORT == GPIOB) {
__HAL_RCC_GPIOB_CLK_ENABLE();
} else if (LED_PORT == GPIOC) {
__HAL_RCC_GPIOC_CLK_ENABLE();
} else if (LED_PORT == GPIOD) {
__HAL_RCC_GPIOD_CLK_ENABLE();
}
}

void bootloader_indicator_init(void) {
GPIO_InitTypeDef init = {0};
bootloader_indicator_enable_port_clock();

init.Pin = LED_PIN;
init.Mode = GPIO_MODE_OUTPUT_PP;
init.Pull = GPIO_NOPULL;
init.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_PORT, &init);
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_RESET);
}

void bootloader_indicator_set_mode(bootloader_indicator_mode_t mode) {
s_mode = mode;
if (s_mode == BOOTLOADER_INDICATOR_CONNECTED) {
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_SET);
} else {
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_RESET);
}
}

void bootloader_indicator_update(uint32_t tick_ms) {
bool led_on = false;

if (s_mode == BOOTLOADER_INDICATOR_CONNECTED) {
led_on = true;
} else if (s_mode == BOOTLOADER_INDICATOR_FLASHING) {
led_on = (tick_ms % 600U) < 300U;
} else if (s_mode == BOOTLOADER_INDICATOR_APP_PRESENT) {
uint32_t phase = tick_ms % 1000U;
led_on = (phase < 100U) || ((phase >= 200U) && (phase < 300U));
} else if (s_mode == BOOTLOADER_INDICATOR_NO_APP) {
/*
* Software PWM fade. The update loop runs often while polling UART, so
* a 20 ms PWM frame is enough for a visible breathe without a timer.
*/
uint32_t breath = tick_ms % 2000U;
uint32_t level = (breath < 1000U) ? breath : (1999U - breath);
uint32_t pwm = tick_ms % 20U;
led_on = pwm < (level / 50U);
} else if (s_mode == BOOTLOADER_INDICATOR_ERROR) {
led_on = (tick_ms % 500U) < 250U;
}

HAL_GPIO_WritePin(LED_PORT, LED_PIN, led_on ? GPIO_PIN_SET : GPIO_PIN_RESET);
}
Loading
Loading