diff options
-rw-r--r-- | CMakeLists.txt | 52 | ||||
-rw-r--r-- | src/blinky.c | 4 |
2 files changed, 37 insertions, 19 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index ee1147f..b8c26a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,47 +1,58 @@ cmake_minimum_required(VERSION 3.10) -project (ch537) +set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) -set (CMAKE_SYSTEM_NAME Generic) # Configure for Bare Metal. -set (CMAKE_SYSTEM_PROCESSOR riscv32) +project (ch537) -set (TC_PREFIX riscv32-unknown-elf-) -# set (CMAKE_VERBOSE_MAKEFILE ON) +# Configure for Bare Metal. +set(CMAKE_SYSTEM_NAME Generic) +set(CMAKE_SYSTEM_PROCESSOR riscv32) +set(TC_PREFIX riscv32-unknown-elf-) include_directories(include linker) -file (GLOB SOURCES "src/*.c") -file (GLOB LINKER_SCRIPT "linker/*.ld") +file(GLOB SOURCES "src/*.c") +file(GLOB LINKER_SCRIPT "linker/*.ld") -file (REAL_PATH "ch-flash/" CH_FLASH_DIR) +file(REAL_PATH "ch-flash/" CH_FLASH_DIR) +# Set compiler and tools +set(CMAKE_C_COMPILER "${TC_PREFIX}gcc" CACHE INTERNAL "C Compiler") +set(CMAKE_OBJCOPY "${TC_PREFIX}objcopy" CACHE INTERNAL "Object Copier") set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -set(CMAKE_C_FLAGS "-march=rv32imac -mabi=ilp32 -lgcc -static -nostartfiles -O -std=gnu99" CACHE INTERNAL "C Compiler options") -set(CMAKE_EXE_LINKER_FLAGS "--cref -static -T ${LINKER_SCRIPT}" CACHE INTERNAL "Linker options") -set (CMAKE_C_COMPILER ${TC_PREFIX}gcc CACHE INTERNAL "C Compiler") -set (CMAKE_OBJCOPY ${TC_PREFIX}objcopy CACHE INTERNAL "Object Copier") -set (CMAKE_EXE_LINKER_FLAGS_RELEASE "" CACHE INTERNAL "Linker options for release build type") -set (CMAKE_C_LINK_EXECUTABLE "${TC_PREFIX}ld <LINK_FLAGS> <OBJECTS> -o <TARGET>") +# Set compiler and linker flags +set(CMAKE_C_FLAGS "-ffreestanding -march=rv32imac -mabi=ilp32 -lgcc -static -nostartfiles -O -std=gnu99" CACHE INTERNAL "C Compiler options") +set(CMAKE_EXE_LINKER_FLAGS "--xref -static -T ${LINKER_SCRIPT}" CACHE INTERNAL "Linker options") + +# Add executable with custom linking commands add_executable(main.elf ${SOURCES}) + +# Replace default link command to use `ld` directly without `-Wl,` prefixes. set_target_properties(main.elf PROPERTIES - LINK_FLAGS "--nostdlib -e 0") + LINKER_LANGUAGE C + LINK_FLAGS "-nostdlib -e 0" + # Custom linking command + CMAKE_C_LINK_EXECUTABLE "${TC_PREFIX}ld <LINK_FLAGS> <OBJECTS> -o <TARGET> ${CMAKE_EXE_LINKER_FLAGS}" +) # Generates the binary with objcopy. add_custom_command( OUTPUT main.bin DEPENDS main.elf COMMENT "objcopy -O binary main.elf main.bin" - COMMAND ${CMAKE_OBJCOPY} ARGS -O binary main.elf main.bin + COMMAND ${CMAKE_OBJCOPY} -O binary main.elf main.bin ) + add_custom_command( OUTPUT objdump.txt DEPENDS main.elf COMMAND ${TC_PREFIX}objdump -D main.elf > objdump.txt ) + add_custom_target(main_bin ALL DEPENDS main.bin objdump.txt) -# generates the flash binary +# Generates the flash binary add_custom_command( OUTPUT ${CH_FLASH_DIR}/ch-flash COMMAND cd ${CH_FLASH_DIR} && make @@ -49,10 +60,13 @@ add_custom_command( add_custom_target( flash - DEPENDS main.bin ch-flash/ch-flash + DEPENDS main.bin ${CH_FLASH_DIR}/ch-flash COMMAND ${CH_FLASH_DIR}/ch-flash -f main.bin ) +# Uncomment to add debugging support # add_custom_target( # debug -# COMMAND "${TC_PREFIX}gdb" -tui -ex "tar ext :3333" -ex "file main.elf") +# COMMAND "${TC_PREFIX}gdb" -tui -ex "tar ext :3333" -ex "file main.elf" +# ) + diff --git a/src/blinky.c b/src/blinky.c index ca93df6..7e46389 100644 --- a/src/blinky.c +++ b/src/blinky.c @@ -61,8 +61,12 @@ static void start(void) for (;;) { gpio_a.out &= ~bit; // Pin low (turns on LED) delay(); + delay(); + delay(); gpio_a.out |= bit; // Pin high (turns off LED) delay(); + delay(); + delay(); } } |