From 34b1d24bb738142fbbd9acc116f174cd895af4f0 Mon Sep 17 00:00:00 2001 From: Steve Karg Date: Sat, 27 Jan 2024 16:00:22 -0600 Subject: [PATCH] Build/add cmake to ports at91sam7 (#560) * added CMake to AT91SAM7S port example * Add ports AT91SAM7S and STM32F4xx CMake builds into pipeline --- .github/workflows/gcc.yml | 21 ++- Makefile | 14 ++ ports/at91sam7s/CMakeLists.txt | 228 +++++++++++++++++++++++++++++++++ 3 files changed, 261 insertions(+), 2 deletions(-) create mode 100644 ports/at91sam7s/CMakeLists.txt diff --git a/.github/workflows/gcc.yml b/.github/workflows/gcc.yml index 943dfeaa..89bacf86 100644 --- a/.github/workflows/gcc.yml +++ b/.github/workflows/gcc.yml @@ -161,7 +161,7 @@ jobs: make clean make ethernet - ports-arm: + ports-arm-makefile: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -171,7 +171,7 @@ jobs: sudo apt-get install -qq build-essential sudo apt-get install -qq gcc-arm-none-eabi sudo apt-get install -qq libnewlib-arm-none-eabi - - name: ports-arm + - name: ports-arm-makefile run: | make clean arm-none-eabi-gcc --version @@ -179,6 +179,23 @@ jobs: make stm32f4xx make at91sam7s + ports-arm-cmake: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Create Build Environment + run: | + sudo apt-get update -qq + sudo apt-get install -qq build-essential + sudo apt-get install -qq gcc-arm-none-eabi + sudo apt-get install -qq libnewlib-arm-none-eabi + sudo apt-get install -qq cmake + - name: ports-arm-cmake + run: | + arm-none-eabi-gcc --version + make stm32f4xx-cmake + make at91sam7s-cmake + ports-avr: runs-on: ubuntu-latest steps: diff --git a/Makefile b/Makefile index eaa61e92..debf25a1 100644 --- a/Makefile +++ b/Makefile @@ -226,6 +226,13 @@ at91sam7s: ports/at91sam7s/Makefile at91sam7s-clean: ports/at91sam7s/Makefile $(MAKE) -s -C ports/at91sam7s clean +AT91SAM7S_CMAKE_BUILD_DIR=ports/at91sam7s/build +.PHONY: at91sam7s-cmake +at91sam7s-cmake: + [ -d $(AT91SAM7S_CMAKE_BUILD_DIR) ] || mkdir -p $(AT91SAM7S_CMAKE_BUILD_DIR) + [ -d $(AT91SAM7S_CMAKE_BUILD_DIR) ] && cd $(AT91SAM7S_CMAKE_BUILD_DIR) && \ + cmake ../ && cmake --build . --clean-first + .PHONY: stm32f10x stm32f10x: ports/stm32f10x/Makefile $(MAKE) -s -C ports/stm32f10x clean all @@ -242,6 +249,13 @@ stm32f4xx: ports/stm32f4xx/Makefile stm32f4xx-clean: ports/stm32f4xx/Makefile $(MAKE) -s -C ports/stm32f4xx clean +STM32F4XX_CMAKE_BUILD_DIR=ports/stm32f4xx/build +.PHONY: stm32f4xx-cmake +stm32f4xx-cmake: + [ -d $(STM32F4XX_CMAKE_BUILD_DIR) ] || mkdir -p $(STM32F4XX_CMAKE_BUILD_DIR) + [ -d $(STM32F4XX_CMAKE_BUILD_DIR) ] && cd $(STM32F4XX_CMAKE_BUILD_DIR) && \ + cmake ../ && cmake --build . --clean-first + .PHONY: xplained xplained: ports/xplained/Makefile $(MAKE) -s -C ports/xplained clean all diff --git a/ports/at91sam7s/CMakeLists.txt b/ports/at91sam7s/CMakeLists.txt new file mode 100644 index 00000000..1fc2c6df --- /dev/null +++ b/ports/at91sam7s/CMakeLists.txt @@ -0,0 +1,228 @@ +# This is a CMake example for AT91SAM7S-EK development board +# using the ARM GCC compiler +# +# MCU AT91SAM7S64 +# CPU ARM7 +# Clock 180MHz +# RAM 16K +# Flash 64K +# +# To build this project you need to install: +# - ARM GCC compiler +# - CMake +# +# To build this project you need to run: +# - cmake -S . -B build +# - cmake --build build +# +# To flash this project you need to run: +# - st-flash write build/bacnet-mstp.hex 0x8000000 +# +# To debug this project you need to run: +# - arm-none-eabi-gdb -q build/bacnet-mstp.elf +# - (gdb) target extended-remote localhost:3333 +# - (gdb) monitor reset halt +# - (gdb) load +# - (gdb) monitor reset halt +# - (gdb) monitor reset init +# - (gdb) monitor reset run +# - (gdb) monitor reset exit +# - (gdb) quit +# +# You can also use VSCode with Cortex-Debug extension +# +# This example was tested with: +# - ARM GCC 10.3.1 +# - CMake 3.22.1 +# - BACnet Stack V1.3.2 +# +cmake_minimum_required(VERSION 3.20) + +# Cross compilers and tools +set(CMAKE_SYSTEM_NAME Generic) +set(CMAKE_SYSTEM_VERSION 1) +set(CMAKE_C_COMPILER arm-none-eabi-gcc) +set(CMAKE_CXX_COMPILER arm-none-eabi-g++) +set(CMAKE_ASM_COMPILER arm-none-eabi-gcc) +set(CMAKE_AR arm-none-eabi-ar) +set(CMAKE_OBJCOPY arm-none-eabi-objcopy) +set(CMAKE_OBJDUMP arm-none-eabi-objdump) +set(SIZE arm-none-eabi-size) +set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) + +project(bacnet-mstp) + +enable_language(C ASM) +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) +set(CMAKE_C_EXTENSIONS ON) + +# Specific ARM microcontroller compiler and linker settings +add_compile_options(-mcpu=arm7tdmi) +add_compile_options(-mno-thumb-interwork) +add_link_options(-mcpu=arm7tdmi) +add_link_options(-mno-thumb-interwork) +# Code size reduction using garbage collection sections +add_compile_options(-ffunction-sections -fdata-sections) +add_compile_options(-fno-common -fmessage-length=0) +add_link_options(-Wl,-gc-sections,--print-memory-usage) +# Build types +if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release") + message(STATUS "Maximum optimization for speed") + add_compile_options(-Ofast) +elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo") + message(STATUS "Maximum optimization for speed, debug info included") + add_compile_options(-Ofast -g) +elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel") + message(STATUS "Maximum optimization for size") + add_compile_options(-Os) +else () + message(STATUS "Minimal optimization, debug info included") + add_compile_definitions(DEBUG) + add_compile_options(-Og -g3) +endif () + +# eliminate the deprecated function warnings +option(BACNET_STACK_DEPRECATED_DISABLE "Disable deprecation compile warnings" ON) +if(BACNET_STACK_DEPRECATED_DISABLE) + add_definitions(-DBACNET_STACK_DEPRECATED_DISABLE) +endif() + +set(LIBRARY_BACNET_INC "../../src") +set(LIBRARY_BACNET_CORE "../../src/bacnet") +set(LIBRARY_BACNET_BASIC "../../src/bacnet/basic") + +set(BACNET_PROJECT_SOURCE + ${CMAKE_SOURCE_DIR}/crt.s + ${CMAKE_SOURCE_DIR}/at91sam7s256.h + ${CMAKE_SOURCE_DIR}/board.h + # CPU interfaces + ${CMAKE_SOURCE_DIR}/main.c + ${CMAKE_SOURCE_DIR}/init.c + ${CMAKE_SOURCE_DIR}/isr.c + ${CMAKE_SOURCE_DIR}/blinker.c + ${CMAKE_SOURCE_DIR}/rs485.c + ${CMAKE_SOURCE_DIR}/timer.c + # MSTP datalink + ${CMAKE_SOURCE_DIR}/dlmstp.c + # BACnet objects + ${CMAKE_SOURCE_DIR}/device.c + ${CMAKE_SOURCE_DIR}/netport.c + ${CMAKE_SOURCE_DIR}/ai.c + ${CMAKE_SOURCE_DIR}/av.c + ${CMAKE_SOURCE_DIR}/bi.c + ${CMAKE_SOURCE_DIR}/bv.c + # BACnet basic services + ${LIBRARY_BACNET_BASIC}/service/h_dcc.c + ${LIBRARY_BACNET_BASIC}/service/h_apdu.c + ${LIBRARY_BACNET_BASIC}/npdu/h_npdu.c + ${LIBRARY_BACNET_BASIC}/service/h_rd.c + ${LIBRARY_BACNET_BASIC}/service/h_rp.c + ${LIBRARY_BACNET_BASIC}/service/h_rpm.c + ${LIBRARY_BACNET_BASIC}/service/h_whohas.c + ${LIBRARY_BACNET_BASIC}/service/h_whois.c + ${LIBRARY_BACNET_BASIC}/service/h_wp.c + ${LIBRARY_BACNET_BASIC}/service/h_noserv.c + ${LIBRARY_BACNET_BASIC}/service/s_iam.c + ${LIBRARY_BACNET_BASIC}/service/s_ihave.c + ${LIBRARY_BACNET_BASIC}/tsm/tsm.c + # BACnet basic system modules + ${LIBRARY_BACNET_BASIC}/sys/debug.c + ${LIBRARY_BACNET_BASIC}/sys/ringbuf.c + ${LIBRARY_BACNET_BASIC}/sys/fifo.c + ${LIBRARY_BACNET_BASIC}/sys/mstimer.c + # BACnet core modules + ${LIBRARY_BACNET_CORE}/abort.c + ${LIBRARY_BACNET_CORE}/bacaddr.c + ${LIBRARY_BACNET_CORE}/bacapp.c + ${LIBRARY_BACNET_CORE}/bacdcode.c + ${LIBRARY_BACNET_CORE}/bacdest.c + ${LIBRARY_BACNET_CORE}/bacdevobjpropref.c + ${LIBRARY_BACNET_CORE}/bacerror.c + ${LIBRARY_BACNET_CORE}/bacint.c + ${LIBRARY_BACNET_CORE}/bacreal.c + ${LIBRARY_BACNET_CORE}/bacstr.c + ${LIBRARY_BACNET_CORE}/datalink/cobs.c + ${LIBRARY_BACNET_CORE}/datalink/crc.c +# ${LIBRARY_BACNET_CORE}/datalink/dlmstp.c +# ${LIBRARY_BACNET_CORE}/datalink/mstp.c + ${LIBRARY_BACNET_CORE}/datalink/mstptext.c + ${LIBRARY_BACNET_CORE}/datetime.c + ${LIBRARY_BACNET_CORE}/dcc.c + ${LIBRARY_BACNET_CORE}/indtext.c + ${LIBRARY_BACNET_CORE}/iam.c + ${LIBRARY_BACNET_CORE}/ihave.c + ${LIBRARY_BACNET_CORE}/hostnport.c + ${LIBRARY_BACNET_CORE}/lighting.c + ${LIBRARY_BACNET_CORE}/memcopy.c + ${LIBRARY_BACNET_CORE}/npdu.c + ${LIBRARY_BACNET_CORE}/proplist.c + ${LIBRARY_BACNET_CORE}/rd.c + ${LIBRARY_BACNET_CORE}/reject.c + ${LIBRARY_BACNET_CORE}/rp.c + ${LIBRARY_BACNET_CORE}/rpm.c + ${LIBRARY_BACNET_CORE}/timestamp.c + ${LIBRARY_BACNET_CORE}/weeklyschedule.c + ${LIBRARY_BACNET_CORE}/dailyschedule.c + ${LIBRARY_BACNET_CORE}/bactimevalue.c + ${LIBRARY_BACNET_CORE}/whohas.c + ${LIBRARY_BACNET_CORE}/whois.c + ${LIBRARY_BACNET_CORE}/wp.c +) + +set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/at91sam7s256.ld) + +set(EXECUTABLE ${PROJECT_NAME}.elf) + +add_executable(${EXECUTABLE} ${BACNET_PROJECT_SOURCE}) + +target_compile_definitions(${EXECUTABLE} PRIVATE + -DNDEBUG + -DUSE_STDPERIPH_DRIVER + -DSTM32F4XX + -DBACDL_MSTP + -DMAX_APDU=480 + -DBIG_ENDIAN=0 + -DPRINT_ENABLED=0 + -DMAX_TSM_TRANSACTIONS=0 + -DCRC_USE_TABLE +) + +# inhibit pedantic warnings +target_compile_options(${EXECUTABLE} PRIVATE + -Wall + -Wno-comment + -Wno-missing-braces + -Wno-unused-variable + -Wno-char-subscripts +) + +target_include_directories(${EXECUTABLE} PRIVATE + ${CMAKE_SOURCE_DIR} + ${LIBRARY_CMSIS_INC} + ${LIBRARY_CMSIS_GCC_INC} + ${LIBRARY_STM32_INC} + ${LIBRARY_BACNET_INC} +) + +target_link_options(${EXECUTABLE} PRIVATE + -T${LINKER_SCRIPT} + -specs=nano.specs + -lm + -lnosys + -Wl,-Map=${PROJECT_NAME}.map,--cref + -Wl,--gc-sections +) + +# Print executable size +add_custom_command(TARGET ${EXECUTABLE} + POST_BUILD + COMMAND arm-none-eabi-size ${EXECUTABLE} +) + +# Create hex file +add_custom_command(TARGET ${EXECUTABLE} + POST_BUILD + COMMAND arm-none-eabi-objcopy -O ihex ${EXECUTABLE} ${PROJECT_NAME}.hex + COMMAND arm-none-eabi-objcopy -O binary ${EXECUTABLE} ${PROJECT_NAME}.bin +)