1+
12cmake_minimum_required (VERSION 3.10)
2- project (bootloader C)
3+
4+ # Dynamically set project name from directory name
5+ get_filename_component (PROJECT_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME )
6+ project (${PROJECT_NAME} C)
7+
8+ # Set the start address for the bootloader (Boot Reset Vector) for ATmega328P
9+ # Fuse bits must be set accordingly to enable the bootloader section
10+ # BOOTSZ0 = 0, BOOTSZ1 = 1 for 1024 words (0x3C00 * 2 = 0x7800 for ATmega328P)
11+ set (BOOTLOADER_START_ADDRESS 0x7800)
12+
13+ # Fuse bits for ATmega328P
14+ # Default values are set for a 16MHz external crystal with 1024 words bootloader size
15+ # CAUTION: Setting incorrect fuse bits can brick your microcontroller.
16+ # https://www.engbedded.com/fusecalc/
17+ set (LFUSE 0xFF)
18+ set (HFUSE 0xDA)
19+ set (EFUSE 0xFD)
320
421set (CMAKE_EXE_LINKER_FLAGS
5- "${CMAKE_EXE_LINKER_FLAGS} -Wl,-section-start=.text=0x7C00 " )
22+ "${CMAKE_EXE_LINKER_FLAGS} -Wl,-section-start=.text=${BOOTLOADER_START_ADDRESS} " )
623
724add_executable (${PROJECT_NAME} .elf src/main.c)
825
@@ -17,14 +34,6 @@ add_custom_target(
1734 COMMENT "[[${PROJECT_NAME} ]] Building .hex and .bin files for \" ${MCU} \" "
1835)
1936
20- # Fuse bits for ATmega328P
21- # Default values are set for a 16MHz external crystal with 512 words bootloader size
22- # CAUTION: Setting incorrect fuse bits can brick your microcontroller.
23- # https://www.engbedded.com/fusecalc/
24- set (LFUSE 0xFF CACHE STRING "Low fuse byte" )
25- set (HFUSE 0xDC CACHE STRING "High fuse byte" )
26- set (EFUSE 0xFD CACHE STRING "Extended fuse byte" )
27-
2837# Write fuse bits
2938add_custom_target (
3039 ${PROJECT_NAME} _write_fusebits
@@ -39,4 +48,5 @@ add_custom_target(
3948 COMMAND avrdude -c ${AVRDUDE_PROGRAMMER} -p ${MCU} -U flash:w:${PROJECT_NAME} .hex
4049 DEPENDS ${PROJECT_NAME} _build
4150 COMMENT "[[${PROJECT_NAME} ]] Flashing the bootloader to \" ${MCU} \" using \" ${AVRDUDE_PROGRAMMER} \" "
51+ COMMENT "[[${PROJECT_NAME} ]] Bootloader start address: 0x${BOOTLOADER_START_ADDRESS} "
4252)
0 commit comments