diff --git a/.gitignore b/.gitignore index d3b11730..0be69d3d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +*.su +*.nm +*.pyc *.log *.a *.o diff --git a/ports/at91sam7s/CMakeLists.txt b/ports/at91sam7s/CMakeLists.txt index 2ad90c84..0b57d69c 100644 --- a/ports/at91sam7s/CMakeLists.txt +++ b/ports/at91sam7s/CMakeLists.txt @@ -48,7 +48,12 @@ 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_SIZE arm-none-eabi-size) +set(CMAKE_NM arm-none-eabi-nm) +set(CMAKE_CSTACK "${CMAKE_SOURCE_DIR}/../../tools/check-stack-usage/checkStackUsage.py") +set(CMAKE_MEMAP "${CMAKE_SOURCE_DIR}/../../tools/memap/memap.py") set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) +set(EXECUTABLE ${PROJECT_NAME}.elf) project(bacnet-mstp) @@ -65,6 +70,7 @@ 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_compile_options(-fstack-usage -fdump-rtl-dfinish) add_link_options(-Wl,-gc-sections,--print-memory-usage) # Build types if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release") @@ -240,3 +246,30 @@ add_custom_command(TARGET ${EXECUTABLE} COMMAND arm-none-eabi-objcopy -O ihex ${EXECUTABLE} ${PROJECT_NAME}.hex COMMAND arm-none-eabi-objcopy -O binary ${EXECUTABLE} ${PROJECT_NAME}.bin ) + +# sort the RAM usage by size and place into a file +add_custom_target(symbols + DEPENDS ${EXECUTABLE} + COMMENT "Print memory symbols by size" + COMMAND ${CMAKE_NM} -t d -S --size-sort ${EXECUTABLE} 1> ${PROJECT_NAME}.nm + COMMAND echo "RAM usage by size analysis in ${PROJECT_NAME}.nm" + COMMAND echo "=ADDRESS= ==RAM=== = ==VARIABLE-NAME==" + COMMAND tail ${PROJECT_NAME}.nm +) + +# calculate the worst case CSTACK memory usage by size and place into a file +add_custom_target(cstack + DEPENDS ${EXECUTABLE} + COMMENT "Print CSTACK memory depth by size" + COMMAND ${CMAKE_CSTACK} ${EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR} 1> ${PROJECT_NAME}.su + COMMAND echo "C-Stack maxium depth analysis in ${PROJECT_NAME}.su" + COMMAND echo "==DEPTH== : == Functions called ==" + COMMAND tail ${PROJECT_NAME}.su +) + +# Print file and library sizes +add_custom_target(memmap + DEPENDS ${PROJECT_NAME}.map + COMMENT "Print file and library memory usage by size" + COMMAND ${CMAKE_MEMAP} -t GCC_ARM ${PROJECT_NAME}.map +) diff --git a/ports/stm32f10x/CMakeLists.txt b/ports/stm32f10x/CMakeLists.txt new file mode 100644 index 00000000..262c63e4 --- /dev/null +++ b/ports/stm32f10x/CMakeLists.txt @@ -0,0 +1,311 @@ +# This is a CMake example for STM32 ARM Cortex-M3 STM32F103RGT6 on +# a STM32 Discovery Kit evaluation board using the ARM GCC compiler +# and STM32 CMSIS library. +# +# Board STM32F103 Discovery Kit +# MCU STM32F103RGT6 +# CPU Cortex-M3 +# RAM 96KB +# Flash 1024KB +# +# 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.out +# - (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 +# - stm32f10x_StdPeriph_Driver V1.0.0 +# - 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(CMAKE_SIZE arm-none-eabi-size) +set(CMAKE_NM arm-none-eabi-nm) +set(CMAKE_CSTACK "${CMAKE_SOURCE_DIR}/../../tools/check-stack-usage/checkStackUsage.py") +set(CMAKE_MEMAP "${CMAKE_SOURCE_DIR}/../../tools/memap/memap.py") +set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) +set(EXECUTABLE ${PROJECT_NAME}.elf) + +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=cortex-m3) +add_compile_options(-mthumb -mno-thumb-interwork -mabi=aapcs) +add_link_options(-mcpu=cortex-m3) +add_link_options(-mthumb -mno-thumb-interwork -mabi=aapcs) +# Compiler and linker settings for garbage collection and memory usage +add_compile_options(-ffunction-sections -fdata-sections) +add_compile_options(-fno-common -fmessage-length=0) +add_compile_options(-fstack-usage -fdump-rtl-dfinish) +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 "${CMAKE_SOURCE_DIR}/../../src") +set(LIBRARY_BACNET_CORE "${CMAKE_SOURCE_DIR}/../../src/bacnet") +set(LIBRARY_BACNET_BASIC "${CMAKE_SOURCE_DIR}/../../src/bacnet/basic") +set(LIBRARY_STM32_SRC "${CMAKE_SOURCE_DIR}/drivers/src") +set(LIBRARY_STM32_INC "${CMAKE_SOURCE_DIR}/drivers/inc") +set(LIBRARY_CMSIS_INC "${CMAKE_SOURCE_DIR}/CMSIS") +set(LIBRARY_CMSIS_GCC_INC "${CMAKE_SOURCE_DIR}/CMSIS/gcc_ride7") + +set(BACNET_PROJECT_SOURCE + ${LIBRARY_STM32_SRC}/stm32f10x_adc.c + ${LIBRARY_STM32_SRC}/stm32f10x_bkp.c + ${LIBRARY_STM32_SRC}/stm32f10x_can.c + ${LIBRARY_STM32_SRC}/stm32f10x_cec.c + ${LIBRARY_STM32_SRC}/stm32f10x_crc.c + ${LIBRARY_STM32_SRC}/stm32f10x_dac.c + ${LIBRARY_STM32_SRC}/stm32f10x_dbgmcu.c + ${LIBRARY_STM32_SRC}/stm32f10x_dma.c + ${LIBRARY_STM32_SRC}/stm32f10x_exti.c + ${LIBRARY_STM32_SRC}/stm32f10x_flash.c + ${LIBRARY_STM32_SRC}/stm32f10x_fsmc.c + ${LIBRARY_STM32_SRC}/stm32f10x_gpio.c + ${LIBRARY_STM32_SRC}/stm32f10x_i2c.c + ${LIBRARY_STM32_SRC}/stm32f10x_iwdg.c + ${LIBRARY_STM32_SRC}/stm32f10x_misc.c + ${LIBRARY_STM32_SRC}/stm32f10x_pwr.c + ${LIBRARY_STM32_SRC}/stm32f10x_rcc.c + ${LIBRARY_STM32_SRC}/stm32f10x_rtc.c + ${LIBRARY_STM32_SRC}/stm32f10x_sdio.c + ${LIBRARY_STM32_SRC}/stm32f10x_spi.c + ${LIBRARY_STM32_SRC}/stm32f10x_tim.c + ${LIBRARY_STM32_SRC}/stm32f10x_usart.c + ${LIBRARY_STM32_SRC}/stm32f10x_wwdg.c + ${LIBRARY_STM32_SRC}/syscalls.c + + ${CMAKE_SOURCE_DIR}/stm32f10x_conf.h + # main entry and STM32 hardware setup + ${CMAKE_SOURCE_DIR}/main.c + ${CMAKE_SOURCE_DIR}/stm32f10x_it.c + ${CMAKE_SOURCE_DIR}/stm32f10x_it.h + ${CMAKE_SOURCE_DIR}/system_stm32f10x.c + # BACnet specific hardware abstraction, configuration and tasks + ${CMAKE_SOURCE_DIR}/bacnet.c + ${CMAKE_SOURCE_DIR}/led.c + ${CMAKE_SOURCE_DIR}/mstimer-init.c + ${CMAKE_SOURCE_DIR}/rs485.c + # BACnet objects in this device + ${CMAKE_SOURCE_DIR}/device.c + ${CMAKE_SOURCE_DIR}/netport.c + ${CMAKE_SOURCE_DIR}/bo.c + # BACnet services library + ${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 + ${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 library + ${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}/calendar_entry.c + ${LIBRARY_BACNET_CORE}/special_event.c + ${LIBRARY_BACNET_CORE}/bactimevalue.c + ${LIBRARY_BACNET_CORE}/whohas.c + ${LIBRARY_BACNET_CORE}/whois.c + ${LIBRARY_BACNET_CORE}/wp.c + + ${LIBRARY_CMSIS_GCC_INC}/startup_stm32f10x_xl.s + CMSIS/stm32f10x.h +) + +set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/stm32f10x.ld) + +set(EXECUTABLE ${PROJECT_NAME}.elf) + +add_executable(${EXECUTABLE} ${BACNET_PROJECT_SOURCE}) + +target_compile_definitions(${EXECUTABLE} PRIVATE + -DNDEBUG + # STM32 CMSIS library + -DUSE_STDPERIPH_DRIVER + -DSTM32F10X_CL + -DHSE_VALUE=25000000 + # BACnet Stack library + -DBACDL_MSTP + -DMAX_APDU=1476 + -DBIG_ENDIAN=0 + -DMAX_TSM_TRANSACTIONS=0 + -DBACAPP_MINIMAL + -DMAX_CHARACTER_STRING_BYTES=64 + -DMAX_OCTET_STRING_BYTES=64 +) + +# inhibit pedantic warnings +target_compile_options(${EXECUTABLE} PRIVATE + -Wall -Wextra -pedantic + -Wfloat-equal -Wconversion -Wredundant-decls + -Wswitch-default + # don't warn about conversion, sign, compares, long long and attributes + # since they are common in embedded + -Wno-sign-conversion + -Wno-conversion + -Wno-sign-compare + -Wno-long-long + -Wno-attributes + # don't warn about implicit fallthrough since it is common in network protocols + -Wno-implicit-fallthrough + # the SDK does not meet coding guidelines + -Wno-comment + -Wno-missing-braces + -Wno-unused-variable + # reference the linker file for CSTACK size + -Wstack-usage=16384 + -Wno-unused-parameter + -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 +) + +# Create hex and bin files +add_custom_command(TARGET ${EXECUTABLE} + POST_BUILD + COMMAND ${CMAKE_OBJCOPY} -O ihex ${EXECUTABLE} ${PROJECT_NAME}.hex + COMMAND ${CMAKE_OBJCOPY} -O binary ${EXECUTABLE} ${PROJECT_NAME}.bin +) + +# Print executable size +add_custom_command(TARGET ${EXECUTABLE} + POST_BUILD + COMMAND ${CMAKE_SIZE} ${EXECUTABLE} +) + +# sort the RAM usage by size and place into a file +add_custom_target(symbols + DEPENDS ${EXECUTABLE} + COMMENT "Print memory symbols by size" + COMMAND ${CMAKE_NM} -t d -S --size-sort ${EXECUTABLE} 1> ${PROJECT_NAME}.nm + COMMAND echo "RAM usage by size analysis in ${PROJECT_NAME}.nm" + COMMAND echo "=ADDRESS= ==RAM=== = ==VARIABLE-NAME==" + COMMAND tail ${PROJECT_NAME}.nm +) + +# calculate the worst case CSTACK memory usage by size and place into a file +add_custom_target(cstack + DEPENDS ${EXECUTABLE} + COMMENT "Print CSTACK memory depth by size" + COMMAND ${CMAKE_CSTACK} ${EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR} 1> ${PROJECT_NAME}.su + COMMAND echo "C-Stack maxium depth analysis in ${PROJECT_NAME}.su" + COMMAND echo "==DEPTH== : == Functions called ==" + COMMAND tail ${PROJECT_NAME}.su +) + +# Print file and library sizes +add_custom_target(memmap + DEPENDS ${PROJECT_NAME}.map + COMMENT "Print file and library memory usage by size" + COMMAND ${CMAKE_MEMAP} -t GCC_ARM ${PROJECT_NAME}.map +) diff --git a/ports/stm32f10x/Makefile b/ports/stm32f10x/Makefile index 47de1dc7..3269dfee 100644 --- a/ports/stm32f10x/Makefile +++ b/ports/stm32f10x/Makefile @@ -13,6 +13,9 @@ LIBRARY_STM32 = ./drivers/src LIBRARY_STM32_INCLUDES = ./drivers/inc LIBRARY_CMSIS = ./CMSIS +CSTACK_TOOL := $(BACNET_DIR)/tools/avstack/avstack.pl +MEMAP_TOOL := $(BACNET_DIR)/tools/memap/memap.py + INCLUDES = -I$(PLATFORM_DIR) INCLUDES += -I$(LIBRARY_STM32_INCLUDES) INCLUDES += -I$(LIBRARY_CMSIS) @@ -23,7 +26,6 @@ PLATFORM_SRC = \ $(PLATFORM_DIR)/bacnet.c \ $(PLATFORM_DIR)/bo.c \ $(PLATFORM_DIR)/device.c \ - $(PLATFORM_DIR)/dlmstp.c \ $(PLATFORM_DIR)/netport.c \ $(PLATFORM_DIR)/led.c \ $(PLATFORM_DIR)/rs485.c \ @@ -61,13 +63,15 @@ BACNET_SRC = \ $(BACNET_CORE)/bacint.c \ $(BACNET_CORE)/bacreal.c \ $(BACNET_CORE)/bacstr.c \ - $(BACNET_CORE)/datalink/automac.c \ - $(BACNET_CORE)/datalink/crc.c \ + $(BACNET_CORE)/bactimevalue.c \ + $(BACNET_CORE)/calendar_entry.c \ $(BACNET_CORE)/datetime.c \ + $(BACNET_CORE)/dailyschedule.c \ $(BACNET_CORE)/dcc.c \ + $(BACNET_CORE)/hostnport.c \ $(BACNET_CORE)/iam.c \ $(BACNET_CORE)/ihave.c \ - $(BACNET_CORE)/hostnport.c \ + $(BACNET_CORE)/indtext.c \ $(BACNET_CORE)/lighting.c \ $(BACNET_CORE)/memcopy.c \ $(BACNET_CORE)/npdu.c \ @@ -76,16 +80,20 @@ BACNET_SRC = \ $(BACNET_CORE)/reject.c \ $(BACNET_CORE)/rp.c \ $(BACNET_CORE)/rpm.c \ + $(BACNET_CORE)/special_event.c \ $(BACNET_CORE)/timestamp.c \ $(BACNET_CORE)/weeklyschedule.c \ - $(BACNET_CORE)/dailyschedule.c \ - $(BACNET_CORE)/bactimevalue.c \ - $(BACNET_CORE)/calendar_entry.c \ - $(BACNET_CORE)/special_event.c \ $(BACNET_CORE)/whohas.c \ $(BACNET_CORE)/whois.c \ $(BACNET_CORE)/wp.c +DATALINK_SRC = \ + $(BACNET_CORE)/datalink/cobs.c \ + $(BACNET_CORE)/datalink/crc.c \ + $(BACNET_CORE)/datalink/dlmstp.c \ + $(BACNET_CORE)/datalink/mstp.c \ + $(BACNET_CORE)/datalink/mstptext.c + STM32_SRC = \ $(LIBRARY_STM32)/stm32f10x_adc.c \ $(LIBRARY_STM32)/stm32f10x_bkp.c \ @@ -115,17 +123,20 @@ STM32_SRC = \ CSRC = $(PLATFORM_SRC) CSRC += $(BASIC_SRC) CSRC += $(BACNET_SRC) +CSRC += $(DATALINK_SRC) CSRC += $(STM32_SRC) ASRC = $(LIBRARY_CMSIS)/gcc_ride7/startup_stm32f10x_xl.s #Set the toolchain command names (only the ones needed are defined) +# sudo apt install gcc-arm-none-eabi PREFIX ?= arm-none-eabi- CC = $(PREFIX)gcc OBJCOPY = $(PREFIX)objcopy OBJDUMP = $(PREFIX)objdump AR = $(PREFIX)ar +NM = $(PREFIX)nm SIZE = $(PREFIX)size LDSCRIPT = $(PLATFORM_DIR)/stm32f10x.ld @@ -142,7 +153,7 @@ OPTIMIZE_FLAGS += -DNDEBUG BACNET_FLAGS = -DBACDL_MSTP=1 BACNET_FLAGS += -DBACAPP_MINIMAL -BACNET_FLAGS += -DMAX_APDU=480 +BACNET_FLAGS += -DMAX_APDU=1476 BACNET_FLAGS += -DBIG_ENDIAN=0 BACNET_FLAGS += -DMAX_TSM_TRANSACTIONS=0 BACNET_FLAGS += -DMAX_CHARACTER_STRING_BYTES=64 @@ -161,6 +172,8 @@ CFLAGS += $(BACNET_FLAGS) CFLAGS += $(INCLUDES) # enable garbage collection of unused functions and data to shrink binary CFLAGS += -ffunction-sections -fdata-sections -fno-strict-aliasing +# enable stack usage tracking +CFLAGS += -fstack-usage # function calls will not use any special __builtin_xx to allow debug/linking CFLAGS += -fno-builtin # place uninitialized global variables in the data section of the object file. @@ -199,6 +212,7 @@ ODFLAGS = -x --syms AOBJ = $(ASRC:.s=.o) COBJ = $(CSRC:.c=.o) +CSTACK = $(CSRC:.c=.su) all: $(TARGET).bin $(TARGET).elf $(OBJDUMP) $(ODFLAGS) $(TARGET).elf > $(TARGET).dmp @@ -210,18 +224,22 @@ $(TARGET).bin: $(TARGET).elf $(TARGET).elf: $(COBJ) $(AOBJ) Makefile $(CC) $(CFLAGS) $(AOBJ) $(COBJ) $(LDFLAGS) -o $@ -# allow a single file to be unoptimized for debugging purposes -#dlmstp.o: -# $(CC) -c $(CFLAGS) $*.c -o $@ -# -#main.o: -# $(CC) -c $(CFLAGS) $*.c -o $@ -# -#$(BACNET_CORE)/npdu.o: -# $(CC) -c $(CFLAGS) $*.c -o $@ -# -#$(BACNET_CORE)/apdu.o: -# $(CC) -c $(CFLAGS) $*.c -o $@ +.PHONY: ram-usage +ram-usage: + @$(NM) -t d -S --size-sort $(TARGET).elf 1> $(TARGET).nm + @echo "=ADDRESS= ==SIZE== = ==VARIABLE NAME==" + @tail $(TARGET).nm + +.PHONY: cstack +cstack: + @$(CSTACK_TOOL) $(COBJ) 2> /dev/null 1> $(TARGET).su + @head -n 25 $(TARGET).su + +.PHONY: memap +memap: + # memmap needs Python and PrettyPrint and IntelHex + # sudo apt install python3-prettytable python3-intelhex + $(MEMAP_TOOL) -t GCC_ARM $(TARGET).map .c.o: $(CC) -c $(OPTIMIZATION) $(CFLAGS) $*.c -o $@ @@ -231,8 +249,9 @@ $(TARGET).elf: $(COBJ) $(AOBJ) Makefile .PHONY: clean clean: - -rm -rf $(COBJ) $(AOBJ) $(COREOBJ) + -rm -rf $(COBJ) $(AOBJ) $(COREOBJ) $(CSTACK) -rm -rf $(TARGET).elf $(TARGET).bin $(TARGET).dmp $(TARGET).map + -rm -rf $(TARGET).su $(TARGET).nm -rm -rf *.lst ## Other dependencies diff --git a/ports/stm32f10x/bacnet.ewp b/ports/stm32f10x/bacnet.ewp index 448b5f0b..84b9c01b 100644 --- a/ports/stm32f10x/bacnet.ewp +++ b/ports/stm32f10x/bacnet.ewp @@ -227,7 +227,7 @@ STM32F10X_XL USE_STDPERIPH_DRIVER BACDL_MSTP - MAX_APDU=480 + MAX_APDU=1476 MAX_TSM_TRANSACTIONS=0 BACAPP_MINIMAL @@ -1079,6 +1079,9 @@ $PROJ_DIR$\..\..\src\bacnet\dcc.c + + $PROJ_DIR$\..\..\src\bacnet\indtext.c + $PROJ_DIR$\..\..\src\bacnet\iam.c @@ -1131,13 +1134,19 @@ BACnet-Datalink - $PROJ_DIR$\..\..\src\bacnet\datalink\automac.c + $PROJ_DIR$\..\..\src\bacnet\datalink\cobs.c $PROJ_DIR$\..\..\src\bacnet\datalink\crc.c - $PROJ_DIR$\dlmstp.c + $PROJ_DIR$\..\..\src\bacnet\datalink\dlmstp.c + + + $PROJ_DIR$\..\..\src\bacnet\datalink\mstp.c + + + $PROJ_DIR$\..\..\src\bacnet\datalink\mstptext.c diff --git a/ports/stm32f10x/device.c b/ports/stm32f10x/device.c index d1e41c14..bd43f7cc 100644 --- a/ports/stm32f10x/device.c +++ b/ports/stm32f10x/device.c @@ -45,10 +45,6 @@ #include "bacnet/basic/object/netport.h" #endif -/* forward prototype */ -int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata); -bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data); - static struct my_object_functions { BACNET_OBJECT_TYPE Object_Type; object_init_function Object_Init; diff --git a/ports/stm32f10x/dlmstp.c b/ports/stm32f10x/dlmstp.c deleted file mode 100644 index cf546855..00000000 --- a/ports/stm32f10x/dlmstp.c +++ /dev/null @@ -1,1778 +0,0 @@ -/*####COPYRIGHTBEGIN#### - ------------------------------------------- - Copyright (C) 2009 Steve Karg - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to: - The Free Software Foundation, Inc. - 59 Temple Place - Suite 330 - Boston, MA 02111-1307 - USA. - - As a special exception, if other files instantiate templates or - use macros or inline functions from this file, or you compile - this file and link it with other works to produce a work based - on this file, this file does not by itself cause the resulting - work to be covered by the GNU General Public License. However - the source code for this file must still be made available in - accordance with section (3) of the GNU General Public License. - - This exception does not invalidate any other reasons why a work - based on this file might be covered by the GNU General Public - License. - ------------------------------------------- -####COPYRIGHTEND####*/ -#include -#include -#include -#include -/* BACnet Stack defines - first */ -#include "bacnet/bacdef.h" -/* BACnet Stack API */ -#include "bacnet/datalink/dlmstp.h" -#include "bacnet/npdu.h" -#include "bacnet/bacaddr.h" -#include "bacnet/basic/sys/mstimer.h" -#include "bacnet/basic/sys/ringbuf.h" -#include "bacnet/datalink/crc.h" -#include "bacnet/datalink/mstpdef.h" -#include "bacnet/datalink/automac.h" -#include "bacnet/basic/object/device.h" -/* port specific */ -#include "rs485.h" - -/* This file has been customized for use with small microprocessors */ -/* Assumptions: - Only one MS/TP datalink layer -*/ - -/* The state of the Receive State Machine */ -static MSTP_RECEIVE_STATE Receive_State; -/* When a master node is powered up or reset, */ -/* it shall unconditionally enter the INITIALIZE state. */ -static MSTP_MASTER_STATE Master_State; -/* bit-sized boolean flags */ -static struct mstp_flag_t { - /* A Boolean flag set to TRUE by the Receive State Machine */ - /* if an invalid frame is received. */ - /* Set to FALSE by the main state machine. */ - unsigned ReceivedInvalidFrame : 1; - /* A Boolean flag set to TRUE by the Receive State Machine */ - /* if a valid frame is received. */ - /* Set to FALSE by the main state machine. */ - unsigned ReceivedValidFrame : 1; - /* set to TRUE when we get a frame not for us */ - unsigned ReceivedValidFrameNotForUs : 1; - /* A Boolean flag set to TRUE by the master machine if this node is the */ - /* only known master node. */ - unsigned SoleMaster : 1; - /* A Boolean flag set TRUE by the datalink if a - packet has been received, but not processed. */ - unsigned ReceivePacketPending : 1; -} MSTP_Flag; - -/* Used to store the data length of a received frame. */ -static uint32_t DataLength; -/* Used to store the destination address of a received frame. */ -static uint8_t DestinationAddress; -/* Used to count the number of received octets or errors. */ -/* This is used in the detection of link activity. */ -/* Compared to Nmin_octets */ -static uint8_t EventCount; -/* Used to store the frame type of a received frame. */ -static uint8_t FrameType; -/* An array of octets, used to store octets as they are received. */ -/* InputBuffer is indexed from 0 to InputBufferSize-1. */ -/* FIXME: assign this to an actual array of bytes! */ -/* Note: the buffer is designed as a pointer since some compilers - and microcontroller architectures have limits as to places to - hold contiguous memory. */ -static uint8_t *InputBuffer; -static uint16_t InputBufferSize; -/* Used to store the Source Address of a received frame. */ -static uint8_t SourceAddress; -/* "This Station," the MAC address of this node. TS is generally read from a */ -/* hardware DIP switch, or from nonvolatile memory. Valid values for TS are */ -/* 0 to 254. The value 255 is used to denote AutoMAC addressing */ -static uint8_t This_Station = 255; -/* This parameter represents the value of the Max_Master property of the */ -/* node's Device object. The value of Max_Master specifies the highest */ -/* allowable address for master nodes. The value of Max_Master shall be */ -/* less than or equal to 127. If Max_Master is not writable in a node, */ -/* its value shall be 127. */ -static uint8_t Nmax_master = 127; - -/* The minimum time without a DataAvailable or ReceiveError event */ -/* that a node must wait for a station to begin replying to a */ -/* confirmed request: 255 milliseconds. (Implementations may use */ -/* larger values for this timeout, not to exceed 300 milliseconds.) */ -#ifndef Treply_timeout -#define Treply_timeout 260 -#endif - -/* The time without a DataAvailable or ReceiveError event that a node must */ -/* wait for a remote node to begin using a token or replying to a Poll For */ -/* Master frame: 20 milliseconds. (Implementations may use larger values for */ -/* this timeout, not to exceed 35 milliseconds.) */ -#ifndef Tusage_timeout -#define Tusage_timeout 30 -#endif - -/* The minimum number of DataAvailable or ReceiveError events that must be */ -/* seen by a receiving node in order to declare the line "active": 4. */ -#define Nmin_octets 4 - -/* The minimum time without a DataAvailable or ReceiveError event within */ -/* a frame before a receiving node may discard the frame: 60 bit times. */ -/* (Implementations may use larger values for this timeout, */ -/* not to exceed 100 milliseconds.) */ -/* At 9600 baud, 60 bit times would be about 6.25 milliseconds */ -/* const uint16_t Tframe_abort = 1 + ((1000 * 60) / 9600); */ -/* At 115200 baud, 60 bit times would be about 0.5 milliseconds */ -/* const uint16_t Tframe_abort = 1 + ((1000 * 60) / 115200); */ -#ifndef Tframe_abort -#define Tframe_abort 30 -#endif - -/* The maximum time a node may wait after reception of a frame that expects */ -/* a reply before sending the first octet of a reply or Reply Postponed */ -/* frame: 250 milliseconds. */ -#ifndef Treply_delay -#define Treply_delay (250 - 50) -#endif - -/* we need to be able to increment without rolling over */ -#define INCREMENT_AND_LIMIT_UINT8(x) \ - { \ - if (x < 0xFF) \ - x++; \ - } - -/* data structure for MS/TP transmit packet */ -struct mstp_tx_packet { - uint16_t length; - uint16_t index; - uint8_t buffer[DLMSTP_MPDU_MAX]; -}; -/* count must be a power of 2 for ringbuf library */ -#ifndef MSTP_TRANSMIT_PACKET_COUNT -#define MSTP_TRANSMIT_PACKET_COUNT 1 -#endif -static struct mstp_tx_packet Transmit_Buffer[MSTP_TRANSMIT_PACKET_COUNT]; -static RING_BUFFER Transmit_Queue; - -/* data structure for MS/TP PDU Queue */ -struct mstp_pdu_packet { - bool data_expecting_reply; - uint8_t destination_mac; - uint16_t length; - uint8_t buffer[DLMSTP_MPDU_MAX]; -}; -/* count must be a power of 2 for ringbuf library */ -#ifndef MSTP_PDU_PACKET_COUNT -#define MSTP_PDU_PACKET_COUNT 2 -#endif -static struct mstp_pdu_packet PDU_Buffer[MSTP_PDU_PACKET_COUNT]; -static RING_BUFFER PDU_Queue; -/* This parameter represents the value of the Max_Info_Frames property of */ -/* the node's Device object. The value of Max_Info_Frames specifies the */ -/* maximum number of information frames the node may send before it must */ -/* pass the token. Max_Info_Frames may have different values on different */ -/* nodes. This may be used to allocate more or less of the available link */ -/* bandwidth to particular nodes. If Max_Info_Frames is not writable in a */ -/* node, its value shall be 1. */ -static uint8_t Nmax_info_frames = MSTP_PDU_PACKET_COUNT; - -void dlmstp_automac_hander(void); - -bool dlmstp_init(char *ifname) -{ - (void)ifname; - Ringbuf_Init(&Transmit_Queue, (uint8_t *)Transmit_Buffer, - sizeof(struct mstp_tx_packet), MSTP_TRANSMIT_PACKET_COUNT); - Ringbuf_Init(&PDU_Queue, (uint8_t *)&PDU_Buffer, - sizeof(struct mstp_pdu_packet), MSTP_PDU_PACKET_COUNT); - rs485_init(); - automac_init(); - - return true; -} - -void dlmstp_cleanup(void) -{ - /* nothing to do for static buffers */ -} - -void dlmstp_fill_bacnet_address(BACNET_ADDRESS *src, uint8_t mstp_address) -{ - int i = 0; - - if (mstp_address == MSTP_BROADCAST_ADDRESS) { - /* mac_len = 0 if broadcast address */ - src->mac_len = 0; - src->mac[0] = 0; - } else { - src->mac_len = 1; - src->mac[0] = mstp_address; - } - /* fill with 0's starting with index 1; index 0 filled above */ - for (i = 1; i < MAX_MAC_LEN; i++) { - src->mac[i] = 0; - } - src->net = 0; - src->len = 0; - for (i = 0; i < MAX_MAC_LEN; i++) { - src->adr[i] = 0; - } -} - -static bool dlmstp_compare_data_expecting_reply(uint8_t *request_pdu, - uint16_t request_pdu_len, - uint8_t src_address, - uint8_t *reply_pdu, - uint16_t reply_pdu_len, - uint8_t dest_address) -{ - uint16_t offset; - /* One way to check the message is to compare NPDU - src, dest, along with the APDU type, invoke id. - Seems a bit overkill */ - struct DER_compare_t { - BACNET_NPDU_DATA npdu_data; - BACNET_ADDRESS address; - uint8_t pdu_type; - uint8_t invoke_id; - uint8_t service_choice; - }; - struct DER_compare_t request; - struct DER_compare_t reply; - bool request_segmented = false; - bool reply_segmented = false; - - /* decode the request data */ - request.address.mac[0] = src_address; - request.address.mac_len = 1; - offset = bacnet_npdu_decode(request_pdu, request_pdu_len, NULL, - &request.address, &request.npdu_data); - if (request.npdu_data.network_layer_message) { - return false; - } - if (offset >= request_pdu_len) { - return false; - } - request.pdu_type = request_pdu[offset] & 0xF0; - if (request.pdu_type != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) { - return false; - } - if (request_pdu[offset] & BIT(3)) { - request_segmented = true; - } - if ((offset + 2) >= request_pdu_len) { - return false; - } - request.invoke_id = request_pdu[offset + 2]; - /* segmented message? */ - if (request_segmented) { - if ((offset + 5) >= request_pdu_len) { - return false; - } - request.service_choice = request_pdu[offset + 5]; - } else { - if ((offset + 3) >= request_pdu_len) { - return false; - } - request.service_choice = request_pdu[offset + 3]; - } - /* decode the reply data */ - reply.address.mac[0] = dest_address; - reply.address.mac_len = 1; - offset = bacnet_npdu_decode( - reply_pdu, reply_pdu_len, &reply.address, NULL, &reply.npdu_data); - if (reply.npdu_data.network_layer_message) { - return false; - } - if (offset >= request_pdu_len) { - return false; - } - reply.pdu_type = reply_pdu[offset] & 0xF0; - if (reply_pdu[offset] & BIT(3)) { - reply_segmented = true; - } - /* reply could be a lot of things: - confirmed, simple ack, abort, reject, error */ - switch (reply.pdu_type) { - case PDU_TYPE_SIMPLE_ACK: - if ((offset + 2) >= request_pdu_len) { - return false; - } - reply.invoke_id = reply_pdu[offset + 1]; - reply.service_choice = reply_pdu[offset + 2]; - break; - case PDU_TYPE_COMPLEX_ACK: - /* segmented message? */ - if (reply_segmented) { - if ((offset + 4) >= request_pdu_len) { - return false; - } - reply.invoke_id = reply_pdu[offset + 1]; - reply.service_choice = reply_pdu[offset + 4]; - } else { - if ((offset + 2) >= request_pdu_len) { - return false; - } - reply.invoke_id = reply_pdu[offset + 1]; - reply.service_choice = reply_pdu[offset + 2]; - } - break; - case PDU_TYPE_ERROR: - if ((offset + 2) >= request_pdu_len) { - return false; - } - reply.invoke_id = reply_pdu[offset + 1]; - reply.service_choice = reply_pdu[offset + 2]; - break; - case PDU_TYPE_REJECT: - case PDU_TYPE_ABORT: - if ((offset + 1) >= request_pdu_len) { - return false; - } - reply.invoke_id = reply_pdu[offset + 1]; - break; - default: - return false; - } - if (request.invoke_id != reply.invoke_id) { - return false; - } - /* these services don't have service choice included */ - if ((reply.pdu_type != PDU_TYPE_REJECT) && - (reply.pdu_type != PDU_TYPE_ABORT)) { - if (request.service_choice != reply.service_choice) { - return false; - } - } - if (request.npdu_data.protocol_version != - reply.npdu_data.protocol_version) { - return false; - } -#if 0 - /* the NDPU priority doesn't get passed through the stack, and - all outgoing messages have NORMAL priority */ - if (request.npdu_data.priority != reply.npdu_data.priority) { - return false; - } -#endif - if (!bacnet_address_same(&request.address, &reply.address)) { - return false; - } - - return true; -} - -typedef enum { - MSTP_TX_STATE_IDLE, - MSTP_TX_STATE_SILENCE_WAIT, - MSTP_TX_STATE_SEND_WAIT, - MSTP_TX_STATE_STOP -} MSTP_TX_STATE; -static bool MSTP_Transmit_FSM(void) -{ - static MSTP_TX_STATE state = MSTP_TX_STATE_IDLE; - static struct mstp_tx_packet *pkt; - -MSTP_TX_START: - switch (state) { - case MSTP_TX_STATE_IDLE: - if (!Ringbuf_Empty(&Transmit_Queue)) { - /* get the packet - but don't remove it from queue */ - pkt = (struct mstp_tx_packet *)Ringbuf_Peek(&Transmit_Queue); - state = MSTP_TX_STATE_SILENCE_WAIT; - } - break; - case MSTP_TX_STATE_SILENCE_WAIT: - if (rs485_turnaround_elapsed()) { - rs485_rts_enable(true); - pkt->index = 0; - rs485_byte_send(pkt->buffer[pkt->index]); - state = MSTP_TX_STATE_SEND_WAIT; - /* optimize a little - for slower CPUs */ - goto MSTP_TX_START; - } - break; - case MSTP_TX_STATE_SEND_WAIT: - if (rs485_byte_sent()) { - pkt->index++; - if (pkt->index < pkt->length) { - /* send next byte */ - rs485_byte_send(pkt->buffer[pkt->index]); - /* optimize a little - for slower CPUs */ - goto MSTP_TX_START; - } else { - state = MSTP_TX_STATE_STOP; - } - } - break; - case MSTP_TX_STATE_STOP: - if (rs485_byte_sent() && rs485_frame_sent()) { - rs485_rts_enable(false); - /* remove the packet from the queue */ - (void)Ringbuf_Pop(&Transmit_Queue, NULL); - state = MSTP_TX_STATE_IDLE; - } - break; - default: - state = MSTP_TX_STATE_IDLE; - break; - } - - return (state != MSTP_TX_STATE_IDLE); -} - -/* MS/TP Frame Format */ -/* All frames are of the following format: */ -/* */ -/* Preamble: two octet preamble: X`55', X`FF' */ -/* Frame Type: one octet */ -/* Destination Address: one octet address */ -/* Source Address: one octet address */ -/* Length: two octets, most significant octet first, of the Data field */ -/* Header CRC: one octet */ -/* Data: (present only if Length is non-zero) */ -/* Data CRC: (present only if Length is non-zero) two octets, */ -/* least significant octet first */ -/* (pad): (optional) at most one octet of padding: X'FF' */ -static void MSTP_Send_Frame( - uint8_t frame_type, /* type of frame to send - see defines */ - uint8_t destination, /* destination address */ - uint8_t source, /* source address */ - uint8_t *data, /* any data to be sent - may be null */ - uint16_t data_len) -{ /* number of bytes of data (up to 501) */ - uint8_t crc8 = 0xFF; /* used to calculate the crc value */ - uint16_t crc16 = 0xFFFF; /* used to calculate the crc value */ - struct mstp_tx_packet *pkt; - uint16_t i = 0; /* used to calculate CRC for data */ - - pkt = (struct mstp_tx_packet *)Ringbuf_Data_Peek(&Transmit_Queue); - if (pkt) { - /* create the MS/TP header */ - pkt->buffer[0] = 0x55; - pkt->buffer[1] = 0xFF; - pkt->buffer[2] = frame_type; - crc8 = CRC_Calc_Header(pkt->buffer[2], crc8); - pkt->buffer[3] = destination; - crc8 = CRC_Calc_Header(pkt->buffer[3], crc8); - pkt->buffer[4] = source; - crc8 = CRC_Calc_Header(pkt->buffer[4], crc8); - pkt->buffer[5] = data_len / 256; - crc8 = CRC_Calc_Header(pkt->buffer[5], crc8); - pkt->buffer[6] = data_len % 256; - crc8 = CRC_Calc_Header(pkt->buffer[6], crc8); - pkt->buffer[7] = (uint8_t)(~crc8); - pkt->length = 8; - if (data_len) { - /* calculate CRC for any data */ - for (i = 0; i < data_len; i++) { - crc16 = CRC_Calc_Data(data[i], crc16); - pkt->buffer[8 + i] = data[i]; - } - crc16 = ~crc16; - pkt->buffer[8 + data_len] = (crc16 & 0x00FF); - pkt->buffer[8 + data_len + 1] = ((crc16 & 0xFF00) >> 8); - pkt->length += data_len; - pkt->length += 2; - } - Ringbuf_Data_Put(&Transmit_Queue, (uint8_t *)pkt); - } else { - pkt = NULL; - } -} - -static void MSTP_Receive_Frame_FSM(void) -{ - /* stores the latest received data octet */ - uint8_t DataRegister = 0; - /* Used to accumulate the CRC on the data field of a frame. */ - static uint16_t DataCRC = 0; - /* Used to accumulate the CRC on the header of a frame. */ - static uint8_t HeaderCRC = 0; - /* Used as an index by the Receive State Machine, - up to a maximum value of the MPDU */ - static uint16_t Index = 0; - - switch (Receive_State) { - case MSTP_RECEIVE_STATE_IDLE: - /* In the IDLE state, the node waits - for the beginning of a frame. */ - if (rs485_receive_error()) { - /* EatAnError */ - rs485_silence_reset(); - INCREMENT_AND_LIMIT_UINT8(EventCount); - } else if (rs485_byte_available(&DataRegister)) { - rs485_silence_reset(); - INCREMENT_AND_LIMIT_UINT8(EventCount); - if (DataRegister == 0x55) { - /* Preamble1 */ - /* receive the remainder of the frame. */ - Receive_State = MSTP_RECEIVE_STATE_PREAMBLE; - } - } - break; - case MSTP_RECEIVE_STATE_PREAMBLE: - /* In the PREAMBLE state, the node waits for the - second octet of the preamble. */ - if (rs485_silence_elapsed(Tframe_abort)) { - /* Timeout */ - /* a correct preamble has not been received */ - /* wait for the start of a frame. */ - Receive_State = MSTP_RECEIVE_STATE_IDLE; - } else if (rs485_receive_error()) { - /* Error */ - rs485_silence_reset(); - INCREMENT_AND_LIMIT_UINT8(EventCount); - /* wait for the start of a frame. */ - Receive_State = MSTP_RECEIVE_STATE_IDLE; - } else if (rs485_byte_available(&DataRegister)) { - rs485_silence_reset(); - INCREMENT_AND_LIMIT_UINT8(EventCount); - if (DataRegister == 0xFF) { - /* Preamble2 */ - Index = 0; - HeaderCRC = 0xFF; - /* receive the remainder of the frame. */ - Receive_State = MSTP_RECEIVE_STATE_HEADER; - } else if (DataRegister == 0x55) { - /* ignore RepeatedPreamble1 */ - /* wait for the second preamble octet. */ - Receive_State = MSTP_RECEIVE_STATE_PREAMBLE; - } else { - /* NotPreamble */ - /* wait for the start of a frame. */ - Receive_State = MSTP_RECEIVE_STATE_IDLE; - } - } - break; - case MSTP_RECEIVE_STATE_HEADER: - /* In the HEADER state, the node waits - for the fixed message header. */ - if (rs485_silence_elapsed(Tframe_abort)) { - /* Timeout */ - /* indicate that an error has occurred - during the reception of a frame */ - MSTP_Flag.ReceivedInvalidFrame = true; - /* wait for the start of a frame. */ - Receive_State = MSTP_RECEIVE_STATE_IDLE; - } else if (rs485_receive_error()) { - /* Error */ - rs485_silence_reset(); - INCREMENT_AND_LIMIT_UINT8(EventCount); - /* indicate that an error has occurred - during the reception of a frame */ - MSTP_Flag.ReceivedInvalidFrame = true; - /* wait for the start of a frame. */ - Receive_State = MSTP_RECEIVE_STATE_IDLE; - } else if (rs485_byte_available(&DataRegister)) { - rs485_silence_reset(); - INCREMENT_AND_LIMIT_UINT8(EventCount); - if (Index == 0) { - /* FrameType */ - HeaderCRC = CRC_Calc_Header(DataRegister, HeaderCRC); - FrameType = DataRegister; - Index = 1; - } else if (Index == 1) { - /* Destination */ - HeaderCRC = CRC_Calc_Header(DataRegister, HeaderCRC); - DestinationAddress = DataRegister; - Index = 2; - } else if (Index == 2) { - /* Source */ - HeaderCRC = CRC_Calc_Header(DataRegister, HeaderCRC); - SourceAddress = DataRegister; - Index = 3; - } else if (Index == 3) { - /* Length1 */ - HeaderCRC = CRC_Calc_Header(DataRegister, HeaderCRC); - DataLength = DataRegister * 256; - Index = 4; - } else if (Index == 4) { - /* Length2 */ - HeaderCRC = CRC_Calc_Header(DataRegister, HeaderCRC); - DataLength += DataRegister; - Index = 5; - } else if (Index == 5) { - /* HeaderCRC */ - HeaderCRC = CRC_Calc_Header(DataRegister, HeaderCRC); - /* In the HEADER_CRC state, the node validates the CRC - on the fixed message header. */ - if (HeaderCRC != 0x55) { - /* BadCRC */ - /* indicate that an error has occurred during - the reception of a frame */ - MSTP_Flag.ReceivedInvalidFrame = true; - /* wait for the start of the next frame. */ - Receive_State = MSTP_RECEIVE_STATE_IDLE; - } else { - if (DataLength == 0) { - /* NoData */ - if ((DestinationAddress == This_Station) || - (DestinationAddress == - MSTP_BROADCAST_ADDRESS)) { - /* ForUs */ - /* indicate that a frame with - no data has been received */ - MSTP_Flag.ReceivedValidFrame = true; - } else { - /* NotForUs */ - MSTP_Flag.ReceivedValidFrameNotForUs = true; - } - /* wait for the start of the next frame. */ - Receive_State = MSTP_RECEIVE_STATE_IDLE; - } else { - /* receive the data portion of the frame. */ - if ((DestinationAddress == This_Station) || - (DestinationAddress == - MSTP_BROADCAST_ADDRESS)) { - if (DataLength <= InputBufferSize) { - /* Data */ - Receive_State = MSTP_RECEIVE_STATE_DATA; - } else { - /* FrameTooLong */ - Receive_State = - MSTP_RECEIVE_STATE_SKIP_DATA; - } - } else { - /* NotForUs */ - Receive_State = MSTP_RECEIVE_STATE_SKIP_DATA; - } - Index = 0; - DataCRC = 0xFFFF; - } - } - } else { - /* indicate that an error has occurred during */ - /* the reception of a frame */ - MSTP_Flag.ReceivedInvalidFrame = true; - /* wait for the start of a frame. */ - Receive_State = MSTP_RECEIVE_STATE_IDLE; - } - } - break; - case MSTP_RECEIVE_STATE_DATA: - case MSTP_RECEIVE_STATE_SKIP_DATA: - /* In the DATA state, the node waits - for the data portion of a frame. */ - if (rs485_silence_elapsed(Tframe_abort)) { - /* Timeout */ - /* indicate that an error has occurred - during the reception of a frame */ - MSTP_Flag.ReceivedInvalidFrame = true; - /* wait for the start of the next frame. */ - Receive_State = MSTP_RECEIVE_STATE_IDLE; - } else if (rs485_receive_error()) { - /* Error */ - rs485_silence_reset(); - INCREMENT_AND_LIMIT_UINT8(EventCount); - /* indicate that an error has occurred during - the reception of a frame */ - MSTP_Flag.ReceivedInvalidFrame = true; - /* wait for the start of the next frame. */ - Receive_State = MSTP_RECEIVE_STATE_IDLE; - } else if (rs485_byte_available(&DataRegister)) { - rs485_silence_reset(); - INCREMENT_AND_LIMIT_UINT8(EventCount); - if (Index < DataLength) { - /* DataOctet */ - DataCRC = CRC_Calc_Data(DataRegister, DataCRC); - if (Index < InputBufferSize) { - InputBuffer[Index] = DataRegister; - } - Index++; - } else if (Index == DataLength) { - /* CRC1 */ - DataCRC = CRC_Calc_Data(DataRegister, DataCRC); - Index++; - } else if (Index == (DataLength + 1)) { - /* CRC2 */ - DataCRC = CRC_Calc_Data(DataRegister, DataCRC); - /* STATE DATA CRC - no need for new state */ - /* indicate the complete reception of a valid frame */ - if (DataCRC == 0xF0B8) { - if (Receive_State == MSTP_RECEIVE_STATE_DATA) { - /* ForUs */ - MSTP_Flag.ReceivedValidFrame = true; - } else { - /* NotForUs */ - MSTP_Flag.ReceivedValidFrameNotForUs = true; - } - } else { - MSTP_Flag.ReceivedInvalidFrame = true; - } - Receive_State = MSTP_RECEIVE_STATE_IDLE; - } else { - MSTP_Flag.ReceivedInvalidFrame = true; - Receive_State = MSTP_RECEIVE_STATE_IDLE; - } - } - break; - default: - /* shouldn't get here - but if we do... */ - Receive_State = MSTP_RECEIVE_STATE_IDLE; - break; - } - - return; -} - -static void MSTP_Slave_Node_FSM(void) -{ - Master_State = MSTP_MASTER_STATE_IDLE; - if (MSTP_Flag.ReceivedInvalidFrame == true) { - /* ReceivedInvalidFrame */ - /* invalid frame was received */ - MSTP_Flag.ReceivedInvalidFrame = false; - } else if (MSTP_Flag.ReceivedValidFrame) { - MSTP_Flag.ReceivedValidFrame = false; - switch (FrameType) { - case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: - if (DestinationAddress != MSTP_BROADCAST_ADDRESS) { - /* indicate successful reception to the higher layers */ - MSTP_Flag.ReceivePacketPending = true; - } - break; - case FRAME_TYPE_TEST_REQUEST: - MSTP_Send_Frame(FRAME_TYPE_TEST_RESPONSE, SourceAddress, - This_Station, &InputBuffer[0], DataLength); - break; - case FRAME_TYPE_TOKEN: - case FRAME_TYPE_POLL_FOR_MASTER: - case FRAME_TYPE_TEST_RESPONSE: - case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - default: - break; - } - } else if (MSTP_Flag.ReceivePacketPending) { - if (!Ringbuf_Empty(&PDU_Queue)) { - /* packet from the PDU Queue */ - struct mstp_pdu_packet *pkt; - /* did the frame in the queue match the last request? */ - bool matched; - - pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue); - matched = dlmstp_compare_data_expecting_reply(&InputBuffer[0], - DataLength, SourceAddress, &pkt->buffer[0], pkt->length, - pkt->destination_mac); - if (matched) { - /* Reply */ - /* If a reply is available from the higher layers */ - /* within Treply_delay after the reception of the */ - /* final octet of the requesting frame */ - /* (the mechanism used to determine this is a local matter), */ - /* then call MSTP_Send_Frame to transmit the reply frame */ - /* and enter the IDLE state to wait for the next frame. */ - uint8_t frame_type; - if (pkt->data_expecting_reply) { - frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; - } else { - frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; - } - MSTP_Send_Frame(frame_type, pkt->destination_mac, This_Station, - (uint8_t *)&pkt->buffer[0], pkt->length); - (void)Ringbuf_Pop(&PDU_Queue, NULL); - } - /* clear our flag we were holding for comparison */ - MSTP_Flag.ReceivePacketPending = false; - } else if ((rs485_silence_elapsed(Treply_delay))) { - /* If no reply will be available from the higher layers - within Treply_delay after the reception of the final octet - of the requesting frame (the mechanism used to determine - this is a local matter), then no reply is possible. */ - /* clear our flag we were holding for comparison */ - MSTP_Flag.ReceivePacketPending = false; - } - } -} - -/* returns true if we need to transition immediately */ -static bool MSTP_Master_Node_FSM(void) -{ - /* The number of frames sent by this node during a single token hold. */ - /* When this counter reaches the value Nmax_info_frames, the node must */ - /* pass the token. */ - static uint8_t FrameCount; - /* "Next Station," the MAC address of the node to which This Station - passes the token. If the Next_Station is unknown, Next_Station shall - be equal to This_Station. */ - static uint8_t Next_Station; - /* "Poll Station," the MAC address of the node to which This Station last */ - /* sent a Poll For Master. This is used during token maintenance. */ - static uint8_t Poll_Station; - /* A counter of transmission retries used for Token and Poll For Master */ - /* transmission. */ - static unsigned RetryCount; - /* The number of tokens received by this node. When this counter reaches */ - /* the value Npoll, the node polls the address range between TS and NS */ - /* for additional master nodes. TokenCount is set to zero at the end of */ - /* the polling process. */ - static unsigned TokenCount; - /* next-x-station calculations */ - uint8_t next_poll_station = 0; - uint8_t next_this_station = 0; - uint8_t next_next_station = 0; - /* timeout values */ - uint16_t my_timeout = 10, ns_timeout = 0; - bool matched = false; - /* transition immediately to the next state */ - bool transition_now = false; - /* packet from the PDU Queue */ - struct mstp_pdu_packet *pkt; - - /* auto mode is active */ - if (This_Station == 255) { - Master_State = MSTP_MASTER_STATE_INITIALIZE; - } - /* some calculations that several states need */ - next_poll_station = (Poll_Station + 1) % (Nmax_master + 1); - next_this_station = (This_Station + 1) % (Nmax_master + 1); - next_next_station = (Next_Station + 1) % (Nmax_master + 1); - switch (Master_State) { - case MSTP_MASTER_STATE_INITIALIZE: - if (This_Station == 255) { - dlmstp_automac_hander(); - if (This_Station != 255) { - Next_Station = automac_next_station(This_Station); - if (Next_Station == 255) { - /* indicate that the next station is unknown */ - Next_Station = This_Station; - Poll_Station = This_Station; - TokenCount = Npoll; - EventCount = 0; - MSTP_Flag.SoleMaster = true; - Master_State = MSTP_MASTER_STATE_POLL_FOR_MASTER; - } else { - Poll_Station = This_Station; - TokenCount = 1; - EventCount = 0; - RetryCount = 0; - MSTP_Flag.SoleMaster = false; - Master_State = MSTP_MASTER_STATE_PASS_TOKEN; - } - } - } else { - /* DoneInitializing */ - /* indicate that the next station is unknown */ - Next_Station = This_Station; - Poll_Station = This_Station; - /* cause a Poll For Master to be sent when this node first */ - /* receives the token */ - TokenCount = Npoll; - MSTP_Flag.SoleMaster = false; - Master_State = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - break; - case MSTP_MASTER_STATE_IDLE: - /* In the IDLE state, the node waits for a frame. */ - if (rs485_silence_elapsed(Tno_token)) { - /* LostToken */ - /* assume that the token has been lost */ - EventCount = 0; /* Addendum 135-2004d-8 */ - /* set the receive frame flags to false in case we received - some bytes and had a timeout for some reason */ - MSTP_Flag.ReceivedValidFrame = false; - MSTP_Flag.ReceivedInvalidFrame = false; - MSTP_Flag.ReceivedValidFrameNotForUs = false; - Master_State = MSTP_MASTER_STATE_NO_TOKEN; - transition_now = true; - } else if (MSTP_Flag.ReceivedInvalidFrame == true) { - /* ReceivedInvalidFrame */ - /* invalid frame was received */ - MSTP_Flag.ReceivedInvalidFrame = false; - /* wait for the next frame - remain in IDLE */ - } else if (MSTP_Flag.ReceivedValidFrame == true) { - switch (FrameType) { - case FRAME_TYPE_TOKEN: - /* ReceivedToken */ - /* tokens can't be broadcast */ - if (DestinationAddress == MSTP_BROADCAST_ADDRESS) - break; - MSTP_Flag.ReceivedValidFrame = false; - FrameCount = 0; - MSTP_Flag.SoleMaster = false; - Master_State = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - break; - case FRAME_TYPE_POLL_FOR_MASTER: - /* ReceivedPFM */ - /* DestinationAddress is equal to TS */ - if (DestinationAddress == This_Station) { - MSTP_Send_Frame(FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, - SourceAddress, This_Station, NULL, 0); - } - break; - case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - if ((DestinationAddress == MSTP_BROADCAST_ADDRESS) && - (npdu_confirmed_service(InputBuffer, DataLength))) { - /* BTL test: verifies that the IUT will quietly - discard any Confirmed-Request-PDU, whose - destination address is a multicast or - broadcast address, received from the - network layer. */ - } else { - /* indicate successful reception to higher layer */ - MSTP_Flag.ReceivePacketPending = true; - } - break; - case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: - if (DestinationAddress == MSTP_BROADCAST_ADDRESS) { - /* broadcast DER just remains IDLE */ - } else { - /* indicate successful reception to higher layers */ - MSTP_Flag.ReceivePacketPending = true; - Master_State = - MSTP_MASTER_STATE_ANSWER_DATA_REQUEST; - } - break; - case FRAME_TYPE_TEST_REQUEST: - MSTP_Send_Frame(FRAME_TYPE_TEST_RESPONSE, SourceAddress, - This_Station, &InputBuffer[0], DataLength); - break; - case FRAME_TYPE_TEST_RESPONSE: - default: - break; - } - /* For DATA_EXPECTING_REPLY, we will keep the Rx Frame for - reference, and the flag will be cleared in the next state */ - if (Master_State != MSTP_MASTER_STATE_ANSWER_DATA_REQUEST) { - MSTP_Flag.ReceivedValidFrame = false; - } - } - break; - /* In the USE_TOKEN state, the node is allowed to send one or */ - /* more data frames. These may be BACnet Data frames or */ - /* proprietary frames. */ - case MSTP_MASTER_STATE_USE_TOKEN: - /* Note: We could wait for up to Tusage_delay */ - if (Ringbuf_Empty(&PDU_Queue)) { - /* NothingToSend */ - FrameCount = Nmax_info_frames; - Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; - transition_now = true; - } else { - uint8_t frame_type; - pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue); - if (pkt->data_expecting_reply) { - frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; - } else { - frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; - } - MSTP_Send_Frame(frame_type, pkt->destination_mac, This_Station, - (uint8_t *)&pkt->buffer[0], pkt->length); - FrameCount++; - switch (frame_type) { - case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: - /* SendAndWait */ - if (pkt->destination_mac == MSTP_BROADCAST_ADDRESS) - Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; - else - Master_State = MSTP_MASTER_STATE_WAIT_FOR_REPLY; - break; - case FRAME_TYPE_TEST_REQUEST: - Master_State = MSTP_MASTER_STATE_WAIT_FOR_REPLY; - break; - case FRAME_TYPE_TEST_RESPONSE: - case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - default: - /* SendNoWait */ - Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; - break; - } - (void)Ringbuf_Pop(&PDU_Queue, NULL); - } - break; - case MSTP_MASTER_STATE_WAIT_FOR_REPLY: - /* In the WAIT_FOR_REPLY state, the node waits for */ - /* a reply from another node. */ - if (rs485_silence_elapsed(Treply_timeout)) { - /* ReplyTimeout */ - /* assume that the request has failed */ - FrameCount = Nmax_info_frames; - Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; - /* Any retry of the data frame shall await the next entry */ - /* to the USE_TOKEN state. */ - /* (Because of the length of the timeout, */ - /* this transition will cause the token to be */ - /* passed regardless */ - /* of the initial value of FrameCount.) */ - transition_now = true; - } else { - if (MSTP_Flag.ReceivedInvalidFrame == true) { - /* InvalidFrame */ - /* error in frame reception */ - MSTP_Flag.ReceivedInvalidFrame = false; - Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; - transition_now = true; - } else if (MSTP_Flag.ReceivedValidFrame == true) { - if (DestinationAddress == This_Station) { - /* What did we receive? */ - switch (FrameType) { - case FRAME_TYPE_REPLY_POSTPONED: - /* ReceivedReplyPostponed */ - Master_State = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; - break; - case FRAME_TYPE_TEST_RESPONSE: - Master_State = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; - break; - case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - /* ReceivedReply */ - /* or a proprietary type that indicates - a reply */ - /* indicate successful reception to - the higher layers */ - MSTP_Flag.ReceivePacketPending = true; - Master_State = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; - break; - default: - /* if proprietary frame was expected, you might - need to transition to DONE WITH TOKEN */ - Master_State = MSTP_MASTER_STATE_IDLE; - break; - } - } else { - /* ReceivedUnexpectedFrame */ - /* an unexpected frame was received */ - /* This may indicate the presence of multiple tokens */ - /* or a device that didn't see activity after passing */ - /* a token (how lame!). */ - /* Synchronize with the network. */ - /* This action drops the token. */ - Master_State = MSTP_MASTER_STATE_IDLE; - } - MSTP_Flag.ReceivedValidFrame = false; - transition_now = true; - } - } - break; - /* The DONE_WITH_TOKEN state either sends another data frame, */ - /* passes the token, or initiates a Poll For Master cycle. */ - case MSTP_MASTER_STATE_DONE_WITH_TOKEN: - /* SendAnotherFrame */ - if (FrameCount < Nmax_info_frames) { - /* then this node may send another information frame */ - /* before passing the token. */ - Master_State = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - } else if ((MSTP_Flag.SoleMaster == false) && - (Next_Station == This_Station)) { - /* NextStationUnknown - added in Addendum 135-2008v-1 */ - /* then the next station to which the token - should be sent is unknown - so PollForMaster */ - Poll_Station = next_this_station; - MSTP_Send_Frame(FRAME_TYPE_POLL_FOR_MASTER, Poll_Station, - This_Station, NULL, 0); - RetryCount = 0; - Master_State = MSTP_MASTER_STATE_POLL_FOR_MASTER; - } - /* Npoll changed in Errata SSPC-135-2004 */ - else if (TokenCount < (Npoll - 1)) { - if ((MSTP_Flag.SoleMaster == true) && - (Next_Station != next_this_station)) { - /* SoleMaster */ - /* there are no other known master nodes to */ - /* which the token may be sent - (true master-slave operation). */ - FrameCount = 0; - TokenCount++; - Master_State = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - } else { - /* SendToken */ - /* Npoll changed in Errata SSPC-135-2004 */ - /* The comparison of NS and TS+1 - eliminates the Poll For Master - if there are no addresses between - TS and NS, since there is no - address at which a new master node - may be found in that case. */ - TokenCount++; - /* transmit a Token frame to NS */ - MSTP_Send_Frame( - FRAME_TYPE_TOKEN, Next_Station, This_Station, NULL, 0); - RetryCount = 0; - EventCount = 0; - Master_State = MSTP_MASTER_STATE_PASS_TOKEN; - } - } else if (next_poll_station == Next_Station) { - if (MSTP_Flag.SoleMaster == true) { - /* SoleMasterRestartMaintenancePFM */ - Poll_Station = next_next_station; - MSTP_Send_Frame(FRAME_TYPE_POLL_FOR_MASTER, Poll_Station, - This_Station, NULL, 0); - /* no known successor node */ - Next_Station = This_Station; - RetryCount = 0; - TokenCount = 1; /* changed in Errata SSPC-135-2004 */ - /* EventCount = 0; removed in Addendum 135-2004d-8 */ - /* find a new successor to TS */ - Master_State = MSTP_MASTER_STATE_POLL_FOR_MASTER; - } else { - /* ResetMaintenancePFM */ - Poll_Station = This_Station; - /* transmit a Token frame to NS */ - MSTP_Send_Frame( - FRAME_TYPE_TOKEN, Next_Station, This_Station, NULL, 0); - RetryCount = 0; - TokenCount = 1; /* changed in Errata SSPC-135-2004 */ - EventCount = 0; - Master_State = MSTP_MASTER_STATE_PASS_TOKEN; - } - } else { - /* SendMaintenancePFM */ - Poll_Station = next_poll_station; - MSTP_Send_Frame(FRAME_TYPE_POLL_FOR_MASTER, Poll_Station, - This_Station, NULL, 0); - RetryCount = 0; - Master_State = MSTP_MASTER_STATE_POLL_FOR_MASTER; - } - break; - /* The PASS_TOKEN state listens for a successor to begin using */ - /* the token that this node has just attempted to pass. */ - case MSTP_MASTER_STATE_PASS_TOKEN: - if (rs485_silence_elapsed(Tusage_timeout)) { - if (RetryCount < Nretry_token) { - /* RetrySendToken */ - RetryCount++; - /* Transmit a Token frame to NS */ - MSTP_Send_Frame( - FRAME_TYPE_TOKEN, Next_Station, This_Station, NULL, 0); - EventCount = 0; - /* re-enter the current state to listen for NS */ - /* to begin using the token. */ - } else { - /* FindNewSuccessor */ - /* Assume that NS has failed. */ - /* note: if NS=TS-1, this node could send PFM to self! */ - Poll_Station = next_next_station; - /* Transmit a Poll For Master frame to PS. */ - MSTP_Send_Frame(FRAME_TYPE_POLL_FOR_MASTER, Poll_Station, - This_Station, NULL, 0); - /* no known successor node */ - Next_Station = This_Station; - RetryCount = 0; - TokenCount = 0; - /* EventCount = 0; removed in Addendum 135-2004d-8 */ - /* find a new successor to TS */ - Master_State = MSTP_MASTER_STATE_POLL_FOR_MASTER; - } - } else { - if (EventCount > Nmin_octets) { - /* SawTokenUser */ - /* Assume that a frame has been sent by - the new token user. */ - /* Enter the IDLE state to process the frame. */ - Master_State = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - } - break; - /* The NO_TOKEN state is entered if Silence Timer - becomes greater than Tno_token, indicating that - there has been no network activity for that period - of time. The timeout is continued to determine - whether or not this node may create a token. */ - case MSTP_MASTER_STATE_NO_TOKEN: - my_timeout = Tno_token + (Tslot * This_Station); - if (rs485_silence_elapsed(my_timeout)) { - ns_timeout = Tno_token + (Tslot * (This_Station + 1)); - if (rs485_silence_elapsed(ns_timeout)) { - /* should never get here unless timer resolution is bad */ - rs485_silence_reset(); - Master_State = MSTP_MASTER_STATE_IDLE; - } else { - /* GenerateToken */ - /* Assume that this node is the lowest numerical address */ - /* on the network and is empowered to create a token. */ - Poll_Station = next_this_station; - /* Transmit a Poll For Master frame to PS. */ - MSTP_Send_Frame(FRAME_TYPE_POLL_FOR_MASTER, Poll_Station, - This_Station, NULL, 0); - /* indicate that the next station is unknown */ - Next_Station = This_Station; - RetryCount = 0; - TokenCount = 0; - /* EventCount = 0; removed Addendum 135-2004d-8 */ - /* enter the POLL_FOR_MASTER state - to find a new successor to TS. */ - Master_State = MSTP_MASTER_STATE_POLL_FOR_MASTER; - } - } else { - if (EventCount > Nmin_octets) { - /* SawFrame */ - /* Some other node exists at a lower address. */ - /* Enter the IDLE state to receive and - process the incoming frame. */ - Master_State = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - } - break; - /* In the POLL_FOR_MASTER state, the node listens for a reply to */ - /* a previously sent Poll For Master frame in order to find */ - /* a successor node. */ - case MSTP_MASTER_STATE_POLL_FOR_MASTER: - if (MSTP_Flag.ReceivedValidFrame == true) { - if ((DestinationAddress == This_Station) && - (FrameType == FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER)) { - /* ReceivedReplyToPFM */ - MSTP_Flag.SoleMaster = false; - Next_Station = SourceAddress; - EventCount = 0; - /* Transmit a Token frame to NS */ - MSTP_Send_Frame( - FRAME_TYPE_TOKEN, Next_Station, This_Station, NULL, 0); - Poll_Station = This_Station; - TokenCount = 0; - RetryCount = 0; - Master_State = MSTP_MASTER_STATE_PASS_TOKEN; - } else { - /* ReceivedUnexpectedFrame */ - /* An unexpected frame was received. */ - /* This may indicate the presence of multiple tokens. */ - /* enter the IDLE state to synchronize with the network. */ - /* This action drops the token. */ - Master_State = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - MSTP_Flag.ReceivedValidFrame = false; - } else if ((rs485_silence_elapsed(Tusage_timeout)) || - (MSTP_Flag.ReceivedInvalidFrame == true)) { - if (MSTP_Flag.SoleMaster == true) { - /* SoleMaster */ - /* There was no valid reply to the periodic poll */ - /* by the sole known master for other masters. */ - FrameCount = 0; - /* TokenCount++; removed in 2004 */ - Master_State = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - } else { - if (Next_Station != This_Station) { - /* DoneWithPFM */ - /* There was no valid reply to the maintenance */ - /* poll for a master at address PS. */ - EventCount = 0; - /* transmit a Token frame to NS */ - MSTP_Send_Frame(FRAME_TYPE_TOKEN, Next_Station, - This_Station, NULL, 0); - RetryCount = 0; - Master_State = MSTP_MASTER_STATE_PASS_TOKEN; - } else { - if (next_poll_station != This_Station) { - /* SendNextPFM */ - Poll_Station = next_poll_station; - /* Transmit a Poll For Master frame to PS. */ - MSTP_Send_Frame(FRAME_TYPE_POLL_FOR_MASTER, - Poll_Station, This_Station, NULL, 0); - RetryCount = 0; - /* Re-enter the current state. */ - } else { - /* DeclareSoleMaster */ - /* to indicate that this station - is the only master */ - MSTP_Flag.SoleMaster = true; - FrameCount = 0; - Master_State = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - } - } - } - MSTP_Flag.ReceivedInvalidFrame = false; - } - break; - /* The ANSWER_DATA_REQUEST state is entered when a */ - /* BACnet Data Expecting Reply, a Test_Request, or */ - /* a proprietary frame that expects a reply is received. */ - case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST: - pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue); - if (pkt != NULL) { - matched = dlmstp_compare_data_expecting_reply(&InputBuffer[0], - DataLength, SourceAddress, &pkt->buffer[0], pkt->length, - pkt->destination_mac); - } else { - matched = false; - } - if (matched) { - /* Reply */ - /* If a reply is available from the higher layers */ - /* within Treply_delay after the reception of the */ - /* final octet of the requesting frame */ - /* (the mechanism used to determine this is a local matter), */ - /* then call MSTP_Send_Frame to transmit the reply frame */ - /* and enter the IDLE state to wait for the next frame. */ - uint8_t frame_type; - if (pkt->data_expecting_reply) { - frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; - } else { - frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; - } - MSTP_Send_Frame(frame_type, pkt->destination_mac, This_Station, - (uint8_t *)&pkt->buffer[0], pkt->length); - Master_State = MSTP_MASTER_STATE_IDLE; - /* clear our flag we were holding for comparison */ - MSTP_Flag.ReceivedValidFrame = false; - /* clear the queue */ - (void)Ringbuf_Pop(&PDU_Queue, NULL); - } else if (rs485_silence_elapsed(Treply_delay) || (pkt != NULL)) { - /* DeferredReply */ - /* If no reply will be available from the higher layers */ - /* within Treply_delay after the reception of the */ - /* final octet of the requesting frame (the mechanism */ - /* used to determine this is a local matter), */ - /* then an immediate reply is not possible. */ - /* Any reply shall wait until this node receives the token. */ - /* Call MSTP_Send_Frame to transmit a Reply Postponed frame, */ - /* and enter the IDLE state. */ - MSTP_Send_Frame(FRAME_TYPE_REPLY_POSTPONED, SourceAddress, - This_Station, NULL, 0); - Master_State = MSTP_MASTER_STATE_IDLE; - /* clear our flag we were holding for comparison */ - MSTP_Flag.ReceivedValidFrame = false; - } - break; - default: - Master_State = MSTP_MASTER_STATE_IDLE; - break; - } - - return transition_now; -} - -/* returns number of bytes sent on success, zero on failure */ -int dlmstp_send_pdu(BACNET_ADDRESS *dest, /* destination address */ - BACNET_NPDU_DATA *npdu_data, /* network information */ - uint8_t *pdu, /* any data to be sent - may be null */ - unsigned pdu_len) -{ /* number of bytes of data */ - int bytes_sent = 0; - struct mstp_pdu_packet *pkt; - uint16_t i = 0; - - pkt = (struct mstp_pdu_packet *)Ringbuf_Data_Peek(&PDU_Queue); - if (pkt) { - pkt->data_expecting_reply = npdu_data->data_expecting_reply; - for (i = 0; i < pdu_len; i++) { - pkt->buffer[i] = pdu[i]; - } - pkt->length = pdu_len; - if (dest && dest->mac_len) { - pkt->destination_mac = dest->mac[0]; - } else { - pkt->destination_mac = MSTP_BROADCAST_ADDRESS; - } - if (Ringbuf_Data_Put(&PDU_Queue, (uint8_t *)pkt)) { - bytes_sent = pdu_len; - } - } - - return bytes_sent; -} - -/* returns true if the Send PDU Queue is Empty */ -bool dlmstp_send_pdu_queue_empty(void) -{ - return Ringbuf_Empty(&PDU_Queue); -} - -/* returns true if the Send PDU Queue is Full */ -bool dlmstp_send_pdu_queue_full(void) -{ - return Ringbuf_Full(&PDU_Queue); -} - -/* master node FSM states */ -typedef enum { - AUTOMAC_STATE_IDLE = 0, - AUTOMAC_STATE_PFM = 1, - AUTOMAC_STATE_TOKEN = 2, - AUTOMAC_STATE_TESTING = 3, - AUTOMAC_STATE_CONFIRM = 4 -} AUTOMAC_STATE; -/* buffer used to send and validate a response - size is min APDU size */ -static uint8_t AutoMAC_Test_Buffer[50]; -void dlmstp_automac_hander(void) -{ - static AUTOMAC_STATE state = AUTOMAC_STATE_IDLE; - uint8_t mac = 0; - uint32_t serial_number = 0; - uint16_t vendor_id = 0; - bool take_address = false; - bool start_over = false; - - switch (state) { - case AUTOMAC_STATE_IDLE: - if ((MSTP_Flag.ReceivedValidFrame) || - (MSTP_Flag.ReceivedValidFrameNotForUs)) { - MSTP_Flag.ReceivedValidFrame = false; - MSTP_Flag.ReceivedValidFrameNotForUs = false; - /* store stats until we get a MAC */ - automac_emitter_set(SourceAddress); - switch (FrameType) { - case FRAME_TYPE_TOKEN: - automac_token_set(SourceAddress); - break; - case FRAME_TYPE_POLL_FOR_MASTER: - automac_pfm_set(DestinationAddress); - break; - default: - break; - } - } else if (MSTP_Flag.ReceivedInvalidFrame) { - MSTP_Flag.ReceivedInvalidFrame = false; - } else if (automac_pfm_cycle_complete()) { - mac = automac_free_address_random(); - if (automac_free_address_valid(mac)) { - automac_address_set(mac); - state = AUTOMAC_STATE_PFM; - } else { - /* start over again */ - automac_init(); - state = AUTOMAC_STATE_IDLE; - } - } else if (rs485_silence_elapsed(automac_time_slot())) { - /* long silence indicates we are alone or - with other silent devices */ - SourceAddress = automac_address(); - state = AUTOMAC_STATE_TESTING; - } - break; - case AUTOMAC_STATE_PFM: - if ((MSTP_Flag.ReceivedValidFrame) || - (MSTP_Flag.ReceivedValidFrameNotForUs)) { - MSTP_Flag.ReceivedValidFrame = false; - MSTP_Flag.ReceivedValidFrameNotForUs = false; - /* store stats until we get a MAC */ - switch (FrameType) { - case FRAME_TYPE_POLL_FOR_MASTER: - mac = automac_address(); - if (mac == SourceAddress) { - /* start over again */ - automac_init(); - state = AUTOMAC_STATE_IDLE; - } else if (mac == DestinationAddress) { - MSTP_Send_Frame(FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, - SourceAddress, mac, NULL, 0); - state = AUTOMAC_STATE_TOKEN; - } - break; - case FRAME_TYPE_TEST_REQUEST: - mac = automac_address(); - if ((mac == DestinationAddress) || - (mac == SourceAddress)) { - /* start over again */ - automac_init(); - state = AUTOMAC_STATE_IDLE; - } - break; - case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: - case FRAME_TYPE_TEST_RESPONSE: - case FRAME_TYPE_TOKEN: - mac = automac_address(); - if (mac == SourceAddress) { - /* start over again */ - automac_init(); - state = AUTOMAC_STATE_IDLE; - } - break; - default: - break; - } - } else if (MSTP_Flag.ReceivedInvalidFrame) { - MSTP_Flag.ReceivedInvalidFrame = false; - } else if (rs485_silence_elapsed(automac_time_slot())) { - /* start over again */ - automac_init(); - state = AUTOMAC_STATE_IDLE; - } - break; - case AUTOMAC_STATE_TOKEN: - if ((MSTP_Flag.ReceivedValidFrame) || - (MSTP_Flag.ReceivedValidFrameNotForUs)) { - MSTP_Flag.ReceivedValidFrame = false; - MSTP_Flag.ReceivedValidFrameNotForUs = false; - switch (FrameType) { - case FRAME_TYPE_TOKEN: - mac = automac_address(); - if (mac == SourceAddress) { - /* start over again */ - automac_init(); - state = AUTOMAC_STATE_IDLE; - } else if (mac == DestinationAddress) { - state = AUTOMAC_STATE_TESTING; - } - break; - default: - /* start over again */ - automac_init(); - state = AUTOMAC_STATE_IDLE; - break; - } - } else if (MSTP_Flag.ReceivedInvalidFrame) { - MSTP_Flag.ReceivedInvalidFrame = false; - } else if (rs485_silence_elapsed(automac_time_slot())) { - /* start over again */ - automac_init(); - state = AUTOMAC_STATE_IDLE; - } - break; - case AUTOMAC_STATE_TESTING: - /* I have the token - confirm my MAC with a quick test */ - mac = automac_address(); - vendor_id = Device_Vendor_Identifier(); - encode_unsigned16(&AutoMAC_Test_Buffer[0], vendor_id); - serial_number = Device_Object_Instance_Number(); - encode_unsigned32(&AutoMAC_Test_Buffer[2], serial_number); - MSTP_Send_Frame(FRAME_TYPE_TEST_REQUEST, SourceAddress, mac, - &AutoMAC_Test_Buffer[0], 6); - state = AUTOMAC_STATE_CONFIRM; - break; - case AUTOMAC_STATE_CONFIRM: - /* we may timeout if our chosen MAC is unique */ - if (MSTP_Flag.ReceivedInvalidFrame) { - MSTP_Flag.ReceivedInvalidFrame = false; - start_over = true; - } else if ((MSTP_Flag.ReceivedValidFrame) || - (MSTP_Flag.ReceivedValidFrameNotForUs)) { - MSTP_Flag.ReceivedValidFrame = false; - MSTP_Flag.ReceivedValidFrameNotForUs = false; - mac = automac_address(); - if ((mac == DestinationAddress) && (DataLength >= 6)) { - decode_unsigned16(&InputBuffer[0], &vendor_id); - decode_unsigned32(&InputBuffer[2], &serial_number); - if ((vendor_id == Device_Vendor_Identifier()) && - (serial_number == Device_Object_Instance_Number())) { - take_address = true; - } else { - start_over = true; - } - } else { - start_over = true; - } - } else if (rs485_silence_elapsed(300)) { - /* use maximum possible value for Treply_timeout */ - /* in case validating device doesn't support Test Request */ - /* no response and no collission */ - take_address = true; - } - if (take_address) { - /* take the address */ - This_Station = automac_address(); - DestinationAddress = automac_next_station(This_Station); - if (DestinationAddress < 128) { - MSTP_Send_Frame(FRAME_TYPE_TOKEN, DestinationAddress, - This_Station, NULL, 0); - } - state = AUTOMAC_STATE_IDLE; - } else if (start_over) { - /* start over again */ - automac_init(); - state = AUTOMAC_STATE_IDLE; - } - break; - default: - break; - } -} - -/* Return the length of the packet */ -uint16_t dlmstp_receive(BACNET_ADDRESS *src, /* source address */ - uint8_t *pdu, /* PDU data */ - uint16_t max_pdu, /* amount of space available in the PDU */ - unsigned timeout) -{ /* milliseconds to wait for a packet */ - uint16_t pdu_len = 0; /* return value */ - bool transmitting = false; - - /* set the input buffer to the same data storage for zero copy */ - if (!InputBuffer) { - InputBuffer = pdu; - InputBufferSize = max_pdu; - } - if (This_Station == 255) { - automac_enabled_set(true); - } - if (Receive_State == MSTP_RECEIVE_STATE_IDLE) { - transmitting = MSTP_Transmit_FSM(); - } - if (transmitting == false) { - while ((MSTP_Flag.ReceivedValidFrame == false) && - (MSTP_Flag.ReceivedValidFrameNotForUs == false) && - (MSTP_Flag.ReceivedInvalidFrame == false)) { - /* only do receive state machine while we don't have a frame */ - MSTP_Receive_Frame_FSM(); - /* process another byte, if available */ - if (!rs485_byte_available(NULL)) { - break; - } - } - } - /* only do master state machine while rx is idle */ - if ((Receive_State == MSTP_RECEIVE_STATE_IDLE) && (transmitting == false)) { - if ((This_Station != 255) && (MSTP_Flag.ReceivedValidFrameNotForUs)) { - MSTP_Flag.ReceivedValidFrameNotForUs = false; - if ((SourceAddress == This_Station) && automac_enabled()) { - /* duplicate MAC on the wire */ - automac_init(); - This_Station = 255; - } - } else { - if ((This_Station > 127) && (This_Station < 255)) { - MSTP_Slave_Node_FSM(); - } else if (This_Station <= 127) { - while (MSTP_Master_Node_FSM()) { - /* do nothing while some states fast transition */ - }; - } - } - } - if (This_Station != 255) { - /* if there is a packet that needs processed, do it now. */ - if (MSTP_Flag.ReceivePacketPending) { - if (This_Station <= 127) { - /* master nodes clear immediately */ - MSTP_Flag.ReceivePacketPending = false; - } - pdu_len = DataLength; - src->mac_len = 1; - src->mac[0] = SourceAddress; - /* data is already in the pdu pointer */ - } - } - - return pdu_len; -} - -void dlmstp_set_mac_address(uint8_t mac_address) -{ - /* Master Nodes can only have address 0-127 */ - if (mac_address <= 127) { - This_Station = mac_address; - if (mac_address > Nmax_master) { - dlmstp_set_max_master(127); - } - } else if (mac_address == 255) { - /* auto-MAC provision */ - This_Station = mac_address; - } - - return; -} - -uint8_t dlmstp_mac_address(void) -{ - return This_Station; -} - -/* This parameter represents the value of the Max_Info_Frames property of */ -/* the node's Device object. The value of Max_Info_Frames specifies the */ -/* maximum number of information frames the node may send before it must */ -/* pass the token. Max_Info_Frames may have different values on different */ -/* nodes. This may be used to allocate more or less of the available link */ -/* bandwidth to particular nodes. If Max_Info_Frames is not writable in a */ -/* node, its value shall be 1. */ -void dlmstp_set_max_info_frames(uint8_t max_info_frames) -{ - if (max_info_frames >= MSTP_PDU_PACKET_COUNT) { - Nmax_info_frames = max_info_frames; - } - - return; -} - -uint8_t dlmstp_max_info_frames(void) -{ - return Nmax_info_frames; -} - -/* This parameter represents the value of the Max_Master property of the */ -/* node's Device object. The value of Max_Master specifies the highest */ -/* allowable address for master nodes. The value of Max_Master shall be */ -/* less than or equal to 127. If Max_Master is not writable in a node, */ -/* its value shall be 127. */ -void dlmstp_set_max_master(uint8_t max_master) -{ - if (max_master <= 127) { - if ((This_Station == 255) || (This_Station <= max_master)) { - Nmax_master = max_master; - } - } - - return; -} - -uint8_t dlmstp_max_master(void) -{ - return Nmax_master; -} - -void dlmstp_get_my_address(BACNET_ADDRESS *my_address) -{ - int i = 0; /* counter */ - - my_address->mac_len = 1; - my_address->mac[0] = This_Station; - my_address->net = 0; /* local only, no routing */ - my_address->len = 0; - for (i = 0; i < MAX_MAC_LEN; i++) { - my_address->adr[i] = 0; - } - - return; -} - -void dlmstp_get_broadcast_address(BACNET_ADDRESS *dest) -{ /* destination address */ - int i = 0; /* counter */ - - if (dest) { - dest->mac_len = 1; - dest->mac[0] = MSTP_BROADCAST_ADDRESS; - dest->net = BACNET_BROADCAST_NETWORK; - dest->len = 0; /* always zero when DNET is broadcast */ - for (i = 0; i < MAX_MAC_LEN; i++) { - dest->adr[i] = 0; - } - } - - return; -} - -bool dlmstp_sole_master(void) -{ - if (MSTP_Flag.SoleMaster) { - return true; - } - - return false; -} - -uint8_t dlmstp_max_info_frames_limit(void) -{ - return MSTP_PDU_PACKET_COUNT; -} - -uint8_t dlmstp_max_master_limit(void) -{ - return 127; -} diff --git a/ports/stm32f10x/drivers/src/syscalls.c b/ports/stm32f10x/drivers/src/syscalls.c index 121622c7..c6067ab1 100644 --- a/ports/stm32f10x/drivers/src/syscalls.c +++ b/ports/stm32f10x/drivers/src/syscalls.c @@ -20,8 +20,6 @@ extern int errno; extern int __io_putchar(int ch); -register char * stack_ptr asm("sp"); - char *__env[1] = { 0 }; char **environ = __env; @@ -61,6 +59,7 @@ int _write(int file, char *ptr, int len) caddr_t _sbrk(int incr) { + register char * stack_ptr asm("sp"); extern char end asm("end"); static char *heap_end; char *prev_heap_end; diff --git a/ports/stm32f10x/main.c b/ports/stm32f10x/main.c index 9beb8f04..927c9d14 100644 --- a/ports/stm32f10x/main.c +++ b/ports/stm32f10x/main.c @@ -27,13 +27,40 @@ #include #include "hardware.h" #include "bacnet/basic/sys/mstimer.h" -#include "bacnet/basic/sys/mstimer.h" +#include "bacnet/datalink/datalink.h" +#include "bacnet/datalink/dlmstp.h" +#include "bacnet/datalink/mstp.h" #include "rs485.h" #include "led.h" #include "bacnet.h" /* local version override */ char *BACnet_Version = "1.0"; +/* MS/TP port */ +static struct mstp_port_struct_t MSTP_Port; +static struct dlmstp_rs485_driver RS485_Driver = { + .init = rs485_init, + .send = rs485_bytes_send, + .read = rs485_byte_available, + .transmitting = rs485_rts_enabled, + .baud_rate = rs485_baud_rate, + .baud_rate_set = rs485_baud_rate_set, + .silence_milliseconds = rs485_silence_milliseconds, + .silence_reset = rs485_silence_reset +}; +static struct dlmstp_user_data_t MSTP_User_Data; +static uint8_t Input_Buffer[DLMSTP_MPDU_MAX]; +static uint8_t Output_Buffer[DLMSTP_MPDU_MAX]; + +/** + * @brief Called from _write() function from printf and friends + * @param[in] ch Character to send + */ +int __io_putchar(int ch) +{ + (void)ch; + return 0; +} #ifdef USE_FULL_ASSERT @@ -102,6 +129,37 @@ void lse_init(void) } } +/** + * @brief Configure the MSTP datalink layer + */ +static void mstp_configure(void) +{ + /* initialize MSTP datalink layer */ + MSTP_Port.Nmax_info_frames = DLMSTP_MAX_INFO_FRAMES; + MSTP_Port.Nmax_master = DLMSTP_MAX_MASTER; + MSTP_Port.InputBuffer = Input_Buffer; + MSTP_Port.InputBufferSize = sizeof(Input_Buffer); + MSTP_Port.OutputBuffer = Output_Buffer; + MSTP_Port.OutputBufferSize = sizeof(Output_Buffer); + /* user data */ + MSTP_Port.ZeroConfigEnabled = true; + MSTP_Port.SlaveNodeEnabled = false; + MSTP_User_Data.RS485_Driver = &RS485_Driver; + MSTP_Port.UserData = &MSTP_User_Data; + dlmstp_init((char *)&MSTP_Port); + if (MSTP_Port.ZeroConfigEnabled) { + dlmstp_set_mac_address(255); + } else { + /* FIXME: get the address from hardware DIP or from EEPROM */ + dlmstp_set_mac_address(1); + } + /* FIXME: get the baud rate from hardware DIP or from EEPROM */ + dlmstp_set_baud_rate(DLMSTP_BAUD_RATE_DEFAULT); +} + +/** + * @brief Main entry point of the C application + */ int main(void) { struct mstimer Blink_Timer; @@ -116,9 +174,13 @@ int main(void) RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE, ENABLE); + /* initialize hardware layer */ mstimer_init(); lse_init(); led_init(); + /* initialize MSTP datalink layer */ + mstp_configure(); + /* initialize application layer*/ bacnet_init(); mstimer_set(&Blink_Timer, 125); for (;;) { diff --git a/ports/stm32f10x/rs485.c b/ports/stm32f10x/rs485.c index b5872151..717bb0c3 100644 --- a/ports/stm32f10x/rs485.c +++ b/ports/stm32f10x/rs485.c @@ -45,70 +45,35 @@ static FIFO_BUFFER Receive_Buffer; static struct mstimer Silence_Timer; /* baud rate */ static uint32_t Baud_Rate = 38400; +/* flag to track RTS status */ +static volatile bool Transmitting; -/* The minimum time after the end of the stop bit of the final octet of a */ -/* received frame before a node may enable its EIA-485 driver: 40 bit times. */ -/* At 9600 baud, 40 bit times would be about 4.166 milliseconds */ -/* At 19200 baud, 40 bit times would be about 2.083 milliseconds */ -/* At 38400 baud, 40 bit times would be about 1.041 milliseconds */ -/* At 57600 baud, 40 bit times would be about 0.694 milliseconds */ -/* At 76800 baud, 40 bit times would be about 0.520 milliseconds */ -/* At 115200 baud, 40 bit times would be about 0.347 milliseconds */ -/* 40 bits is 4 octets including a start and stop bit with each octet */ -#define Tturnaround (40UL) +/* statistics */ +static volatile uint32_t RS485_Transmit_Bytes; +static volatile uint32_t RS485_Receive_Bytes; -/************************************************************************* - * Description: Reset the silence on the wire timer. - * Returns: nothing - * Notes: none - **************************************************************************/ + +/** + * @brief Reset the silence on the wire timer. + */ void rs485_silence_reset(void) { mstimer_set(&Silence_Timer, 0); } -/************************************************************************* - * Description: Determine the amount of silence on the wire from the timer. - * Returns: true if the amount of time has elapsed - * Notes: none - **************************************************************************/ -bool rs485_silence_elapsed(uint32_t interval) +/** + * @brief Return the RS-485 silence time in milliseconds + * @return silence time in milliseconds + */ +uint32_t rs485_silence_milliseconds(void) { - return (mstimer_elapsed(&Silence_Timer) > interval); + return mstimer_elapsed(&Silence_Timer); } -/************************************************************************* - * Description: Baud rate determines turnaround time. - * Returns: amount of milliseconds - * Notes: none - **************************************************************************/ -static uint16_t rs485_turnaround_time(void) -{ - /* delay after reception before transmitting - per MS/TP spec */ - /* wait a minimum 40 bit times since reception */ - /* at least 2 ms for errors: rounding, clock tick */ - if (Baud_Rate) { - return (2 + ((Tturnaround * 1000UL) / Baud_Rate)); - } else { - return 2; - } -} - -/************************************************************************* - * Description: Use the silence timer to determine turnaround time. - * Returns: true if turnaround time has expired. - * Notes: none - **************************************************************************/ -bool rs485_turnaround_elapsed(void) -{ - return (mstimer_elapsed(&Silence_Timer) > rs485_turnaround_time()); -} - -/************************************************************************* - * Description: Determines if an error occured while receiving - * Returns: true an error occurred. - * Notes: none - **************************************************************************/ +/** + * @brief Determines if an error occured while receiving + * @return true an error occurred + */ bool rs485_receive_error(void) { return false; @@ -125,6 +90,7 @@ void USART2_IRQHandler(void) /* Read one byte from the receive data register */ data_byte = USART_ReceiveData(USART2); (void)FIFO_Put(&Receive_Buffer, data_byte); + RS485_Receive_Bytes++; } if (USART_GetFlagStatus(USART2, USART_FLAG_ORE) != RESET) { /* note: enabling RXNE interrupt also enables the ORE interrupt! */ @@ -134,11 +100,35 @@ void USART2_IRQHandler(void) } } -/************************************************************************* - * DESCRIPTION: Return true if a byte is available - * RETURN: true if a byte is available, with the byte in the parameter - * NOTES: none - **************************************************************************/ +/** + * @brief Control the DE and /RE pins on the RS-485 transceiver + * @param enable - true to set DE and /RE high, false to set /DE and RE low + */ +void rs485_rts_enable(bool enable) +{ + if (enable) { + led_tx_on_interval(10); + GPIO_WriteBit(GPIOA, GPIO_Pin_1, Bit_SET); + } else { + GPIO_WriteBit(GPIOA, GPIO_Pin_1, Bit_RESET); + } +} + +/** + * @brief Determine the status of the transmit-enable line on the RS-485 + * transceiver + * @return true if RTS is enabled, false if RTS is disabled + */ +bool rs485_rts_enabled(void) +{ + return Transmitting; +} + +/** + * @brief Return true if a byte is available + * @param data_register - byte in this parameter if there is one available + * @return true if a byte is available, with the byte in the parameter + */ bool rs485_byte_available(uint8_t *data_register) { bool data_available = false; /* return value */ @@ -155,50 +145,37 @@ bool rs485_byte_available(uint8_t *data_register) return data_available; } -/************************************************************************* - * DESCRIPTION: Sends a byte of data - * RETURN: nothing - * NOTES: none - **************************************************************************/ -void rs485_byte_send(uint8_t tx_byte) -{ - led_tx_on_interval(10); - USART_SendData(USART2, tx_byte); - rs485_silence_reset(); -} - -/************************************************************************* - * Description: Determines if a byte in the USART has been shifted from - * register - * Returns: true if the USART register is empty - * Notes: none - **************************************************************************/ +/** + * @brief Determines if a byte in the USART has been shifted from register + * @return true if the USART register is empty + */ bool rs485_byte_sent(void) { return USART_GetFlagStatus(USART2, USART_FLAG_TXE); } -/************************************************************************* - * Description: Determines if the entire frame is sent from USART FIFO - * Returns: true if the USART FIFO is empty - * Notes: none - **************************************************************************/ +/** + * @brief Determines if the entire frame is sent from USART FIFO + * @brief true if the USART FIFO is empty + */ bool rs485_frame_sent(void) { return USART_GetFlagStatus(USART2, USART_FLAG_TC); } -/************************************************************************* - * DESCRIPTION: Send some data and wait until it is sent - * RETURN: true if a collision or timeout occurred - * NOTES: none - **************************************************************************/ +/** + * @brief Transmit one or more bytes on RS-485. + * @param buffer - array of one or more bytes to transmit + * @param nbytes - number of bytes to transmit + * @return true if added to queue + */ void rs485_bytes_send(uint8_t *buffer, /* data to send */ uint16_t nbytes) { /* number of bytes of data */ uint8_t tx_byte; while (nbytes) { + rs485_rts_enable(true); /* Send the data byte */ tx_byte = *buffer; /* Send one byte */ @@ -208,22 +185,22 @@ void rs485_bytes_send(uint8_t *buffer, /* data to send */ } buffer++; nbytes--; + RS485_Transmit_Bytes++; } /* was the frame sent? */ while (!rs485_frame_sent()) { /* do nothing - wait until the entire frame in the Transmit Shift Register has been shifted out */ } + rs485_rts_enable(false); rs485_silence_reset(); return; } -/************************************************************************* - * Description: Configures the baud rate of the USART - * Returns: nothing - * Notes: none - **************************************************************************/ +/** + * @brief Configures the baud rate of the USART + */ static void rs485_baud_rate_configure(void) { USART_InitTypeDef USART_InitStructure; @@ -240,11 +217,10 @@ static void rs485_baud_rate_configure(void) USART_Init(USART2, &USART_InitStructure); } -/************************************************************************* - * Description: Sets the baud rate to non-volatile storeage and configures USART - * Returns: true if a value baud rate was saved - * Notes: none - **************************************************************************/ +/** + * @brief Sets the baud rate to non-volatile storeage and configures USART + * @return true if a value baud rate was saved + */ bool rs485_baud_rate_set(uint32_t baud) { bool valid = true; @@ -267,35 +243,36 @@ bool rs485_baud_rate_set(uint32_t baud) return valid; } -/************************************************************************* - * Description: Determines the baud rate in bps - * Returns: baud rate in bps - * Notes: none - **************************************************************************/ +/** + * @brief Return the RS-485 baud rate + * @return baud - RS-485 baud rate in bits per second (bps) + */ uint32_t rs485_baud_rate(void) { return Baud_Rate; } -/************************************************************************* - * Description: Enable the Request To Send (RTS) aka Transmit Enable pin - * Returns: nothing - * Notes: none - **************************************************************************/ -void rs485_rts_enable(bool enable) +/** + * @brief Return the RS-485 statistics for transmit bytes + * @return number of bytes transmitted + */ +uint32_t rs485_bytes_transmitted(void) { - if (enable) { - GPIO_WriteBit(GPIOA, GPIO_Pin_1, Bit_SET); - } else { - GPIO_WriteBit(GPIOA, GPIO_Pin_1, Bit_RESET); - } + return RS485_Transmit_Bytes; } -/************************************************************************* - * Description: Initialize the room network USART - * Returns: nothing - * Notes: none - **************************************************************************/ +/** + * @brief Return the RS-485 statistics for receive bytes + * @return number of bytes received + */ +uint32_t rs485_bytes_received(void) +{ + return RS485_Receive_Bytes; +} + +/** + * @brief Initialize the room network USART + */ void rs485_init(void) { GPIO_InitTypeDef GPIO_InitStructure; diff --git a/ports/stm32f10x/rs485.h b/ports/stm32f10x/rs485.h index b055de2a..ff8094c4 100644 --- a/ports/stm32f10x/rs485.h +++ b/ports/stm32f10x/rs485.h @@ -36,33 +36,34 @@ extern "C" { void rs485_init( void); + void rs485_rts_enable( bool enable); + bool rs485_rts_enabled( + void); + bool rs485_byte_available( uint8_t * data_register); bool rs485_receive_error( void); void rs485_bytes_send( - uint8_t * buffer, /* data to send */ - uint16_t nbytes); /* number of bytes of data */ + uint8_t * buffer, + uint16_t nbytes); + uint32_t rs485_baud_rate( void); bool rs485_baud_rate_set( uint32_t baud); - /* a granular approach */ - void rs485_byte_send( - uint8_t data_register); - bool rs485_byte_sent( - void); - bool rs485_frame_sent( - void); - bool rs485_turnaround_elapsed( - void); + uint32_t rs485_silence_milliseconds( + void); void rs485_silence_reset( void); - bool rs485_silence_elapsed( - uint32_t interval); + + uint32_t rs485_bytes_transmitted( + void); + uint32_t rs485_bytes_received( + void); #ifdef __cplusplus } diff --git a/ports/stm32f4xx/CMakeLists.txt b/ports/stm32f4xx/CMakeLists.txt index c428e6ef..ce7a089d 100644 --- a/ports/stm32f4xx/CMakeLists.txt +++ b/ports/stm32f4xx/CMakeLists.txt @@ -49,8 +49,12 @@ 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_SIZE arm-none-eabi-size) +set(CMAKE_NM arm-none-eabi-nm) +set(CMAKE_CSTACK "${CMAKE_SOURCE_DIR}/../../tools/check-stack-usage/checkStackUsage.py") +set(CMAKE_MEMAP "${CMAKE_SOURCE_DIR}/../../tools/memap/memap.py") set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) +set(EXECUTABLE ${PROJECT_NAME}.elf) project(bacnet-mstp) @@ -64,6 +68,7 @@ add_compile_options(-mcpu=cortex-m4) add_compile_options(-mthumb -mthumb-interwork) add_compile_options(-ffunction-sections -fdata-sections) add_compile_options(-fno-common -fmessage-length=0) +add_compile_options(-fstack-usage -fdump-rtl-dfinish) add_link_options(-mcpu=cortex-m4) add_link_options(-mthumb -mthumb-interwork) add_link_options(-Wl,-gc-sections,--print-memory-usage) @@ -95,13 +100,13 @@ 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(LIBRARY_STM32_SRC "./STM32F4xx_StdPeriph_Driver/src") -set(LIBRARY_STM32_INC "./STM32F4xx_StdPeriph_Driver/inc") -set(LIBRARY_CMSIS_INC "./CMSIS") -set(LIBRARY_CMSIS_GCC_INC "./CMSIS/gcc_ride7") +set(LIBRARY_BACNET_INC "${CMAKE_SOURCE_DIR}/../../src") +set(LIBRARY_BACNET_CORE "${CMAKE_SOURCE_DIR}/../../src/bacnet") +set(LIBRARY_BACNET_BASIC "${CMAKE_SOURCE_DIR}/../../src/bacnet/basic") +set(LIBRARY_STM32_SRC "${CMAKE_SOURCE_DIR}/STM32F4xx_StdPeriph_Driver/src") +set(LIBRARY_STM32_INC "${CMAKE_SOURCE_DIR}/STM32F4xx_StdPeriph_Driver/inc") +set(LIBRARY_CMSIS_INC "${CMAKE_SOURCE_DIR}/CMSIS") +set(LIBRARY_CMSIS_GCC_INC "${CMAKE_SOURCE_DIR}/CMSIS/gcc_ride7") set(BACNET_PROJECT_SOURCE ${LIBRARY_STM32_SRC}/stm32f4xx_adc.c @@ -242,6 +247,8 @@ target_compile_options(${EXECUTABLE} PRIVATE -Wno-comment -Wno-missing-braces -Wno-unused-variable + # reference the linker file for CSTACK size + -Wstack-usage=16384 -Wno-unused-parameter -Wno-char-subscripts ) @@ -263,15 +270,42 @@ target_link_options(${EXECUTABLE} PRIVATE -Wl,--gc-sections ) +# Create hex and bin files +add_custom_command(TARGET ${EXECUTABLE} + POST_BUILD + COMMAND ${CMAKE_OBJCOPY} -O ihex ${EXECUTABLE} ${PROJECT_NAME}.hex + COMMAND ${CMAKE_OBJCOPY} -O binary ${EXECUTABLE} ${PROJECT_NAME}.bin +) + # Print executable size add_custom_command(TARGET ${EXECUTABLE} POST_BUILD - COMMAND arm-none-eabi-size ${EXECUTABLE} + COMMAND ${CMAKE_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 +# sort the RAM usage by size and place into a file +add_custom_target(symbols + DEPENDS ${EXECUTABLE} + COMMENT "Print memory symbols by size" + COMMAND ${CMAKE_NM} -t d -S --size-sort ${EXECUTABLE} 1> ${PROJECT_NAME}.nm + COMMAND echo "RAM usage by size analysis in ${PROJECT_NAME}.nm" + COMMAND echo "=ADDRESS= ==RAM=== = ==VARIABLE-NAME==" + COMMAND tail ${PROJECT_NAME}.nm +) + +# calculate the worst case CSTACK memory usage by size and place into a file +add_custom_target(cstack + DEPENDS ${EXECUTABLE} + COMMENT "Print CSTACK memory depth by size" + COMMAND ${CMAKE_CSTACK} ${EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR} 1> ${PROJECT_NAME}.su + COMMAND echo "C-Stack maxium depth analysis in ${PROJECT_NAME}.su" + COMMAND echo "==DEPTH== : == Functions called ==" + COMMAND tail ${PROJECT_NAME}.su +) + +# Print file and library sizes +add_custom_target(memmap + DEPENDS ${PROJECT_NAME}.map + COMMENT "Print file and library memory usage by size" + COMMAND ${CMAKE_MEMAP} -t GCC_ARM ${PROJECT_NAME}.map ) diff --git a/ports/stm32f4xx/Makefile b/ports/stm32f4xx/Makefile index 37d16287..ce4b024c 100644 --- a/ports/stm32f4xx/Makefile +++ b/ports/stm32f4xx/Makefile @@ -13,6 +13,9 @@ LIBRARY_STM32 = ./STM32F4xx_StdPeriph_Driver/src LIBRARY_STM32_INCLUDES = ./STM32F4xx_StdPeriph_Driver/inc LIBRARY_CMSIS = ./CMSIS +CSTACK_TOOL := $(BACNET_DIR)/tools/avstack/avstack.pl +MEMAP_TOOL := $(BACNET_DIR)/tools/memap/memap.py + INCLUDES = -I$(PLATFORM_DIR) INCLUDES += -I$(LIBRARY_STM32_INCLUDES) INCLUDES += -I$(LIBRARY_CMSIS) @@ -59,17 +62,15 @@ BACNET_SRC = \ $(BACNET_CORE)/bacint.c \ $(BACNET_CORE)/bacreal.c \ $(BACNET_CORE)/bacstr.c \ - $(BACNET_CORE)/datalink/cobs.c \ - $(BACNET_CORE)/datalink/crc.c \ - $(BACNET_CORE)/datalink/dlmstp.c \ - $(BACNET_CORE)/datalink/mstp.c \ - $(BACNET_CORE)/datalink/mstptext.c \ + $(BACNET_CORE)/bactimevalue.c \ + $(BACNET_CORE)/calendar_entry.c \ + $(BACNET_CORE)/dailyschedule.c \ $(BACNET_CORE)/datetime.c \ $(BACNET_CORE)/dcc.c \ - $(BACNET_CORE)/indtext.c \ + $(BACNET_CORE)/hostnport.c \ $(BACNET_CORE)/iam.c \ $(BACNET_CORE)/ihave.c \ - $(BACNET_CORE)/hostnport.c \ + $(BACNET_CORE)/indtext.c \ $(BACNET_CORE)/lighting.c \ $(BACNET_CORE)/memcopy.c \ $(BACNET_CORE)/npdu.c \ @@ -78,16 +79,20 @@ BACNET_SRC = \ $(BACNET_CORE)/reject.c \ $(BACNET_CORE)/rp.c \ $(BACNET_CORE)/rpm.c \ + $(BACNET_CORE)/special_event.c \ $(BACNET_CORE)/timestamp.c \ $(BACNET_CORE)/weeklyschedule.c \ - $(BACNET_CORE)/dailyschedule.c \ - $(BACNET_CORE)/calendar_entry.c \ - $(BACNET_CORE)/special_event.c \ - $(BACNET_CORE)/bactimevalue.c \ $(BACNET_CORE)/whohas.c \ $(BACNET_CORE)/whois.c \ $(BACNET_CORE)/wp.c +DATALINK_SRC = \ + $(BACNET_CORE)/datalink/cobs.c \ + $(BACNET_CORE)/datalink/crc.c \ + $(BACNET_CORE)/datalink/dlmstp.c \ + $(BACNET_CORE)/datalink/mstp.c \ + $(BACNET_CORE)/datalink/mstptext.c + STM32_SRC = \ $(LIBRARY_STM32)/stm32f4xx_adc.c \ $(LIBRARY_STM32)/stm32f4xx_can.c \ @@ -118,6 +123,7 @@ STM32_SRC = \ CSRC = $(PLATFORM_SRC) CSRC += $(BASIC_SRC) CSRC += $(BACNET_SRC) +CSRC += $(DATALINK_SRC) CSRC += $(STM32_SRC) ASRC = $(LIBRARY_CMSIS)/gcc_ride7/startup_stm32f4xx.s @@ -173,6 +179,8 @@ CFLAGS += $(BACNET_FLAGS) CFLAGS += $(INCLUDES) # enable garbage collection of unused functions and data to shrink binary CFLAGS += -ffunction-sections -fdata-sections -fno-strict-aliasing +# enable stack usage tracking +CFLAGS += -fstack-usage # function calls will not use any special __builtin_xx to allow debug/linking CFLAGS += -fno-builtin # place uninitialized global variables in the data section of the object file. @@ -211,6 +219,7 @@ ODFLAGS = -x --syms AOBJ = $(ASRC:.s=.o) COBJ = $(CSRC:.c=.o) +CSTACK = $(CSRC:.c=.su) all: $(TARGET).bin $(TARGET).hex $(TARGET).elf $(OBJDUMP) $(ODFLAGS) $(TARGET).elf > $(TARGET).dmp @@ -225,9 +234,22 @@ $(TARGET).hex: $(TARGET).elf $(TARGET).elf: $(COBJ) $(AOBJ) Makefile $(CC) $(CFLAGS) $(AOBJ) $(COBJ) $(LDFLAGS) -o $@ -: ram-usage +.PHONY: ram-usage ram-usage: - $(NM) -t d -S --size-sort $(TARGET).elf 1> $(TARGET).nm + @$(NM) -t d -S --size-sort $(TARGET).elf 1> $(TARGET).nm + @echo "=ADDRESS= ==SIZE== = ==VARIABLE NAME==" + @tail $(TARGET).nm + +.PHONY: cstack +cstack: + @$(CSTACK_TOOL) $(COBJ) 2> /dev/null 1> $(TARGET).su + @head -n 25 $(TARGET).su + +.PHONY: memap +memap: + # memmap needs Python and PrettyPrint and IntelHex + # sudo apt install python3-prettytable python3-intelhex + $(MEMAP_TOOL) -t GCC_ARM $(TARGET).map # GDB using st-util (GDB server for ST Link) GDB_PORT = 3333 @@ -282,6 +304,7 @@ install: $(TARGET).bin .PHONY: clean clean: -rm -rf $(COBJ) $(AOBJ) $(COREOBJ) + -rm -rf $(CSTACK) $(CEXPAND) -rm -rf $(TARGET).elf $(TARGET).bin $(TARGET).dmp $(TARGET).map -rm -rf $(TARGET).su $(TARGET).nm -rm -rf *.lst diff --git a/ports/stm32f4xx/STM32F4xx_StdPeriph_Driver/src/syscalls.c b/ports/stm32f4xx/STM32F4xx_StdPeriph_Driver/src/syscalls.c index 121622c7..c6067ab1 100644 --- a/ports/stm32f4xx/STM32F4xx_StdPeriph_Driver/src/syscalls.c +++ b/ports/stm32f4xx/STM32F4xx_StdPeriph_Driver/src/syscalls.c @@ -20,8 +20,6 @@ extern int errno; extern int __io_putchar(int ch); -register char * stack_ptr asm("sp"); - char *__env[1] = { 0 }; char **environ = __env; @@ -61,6 +59,7 @@ int _write(int file, char *ptr, int len) caddr_t _sbrk(int incr) { + register char * stack_ptr asm("sp"); extern char end asm("end"); static char *heap_end; char *prev_heap_end; diff --git a/ports/stm32f4xx/device.c b/ports/stm32f4xx/device.c index 1c70b706..f2056fb8 100644 --- a/ports/stm32f4xx/device.c +++ b/ports/stm32f4xx/device.c @@ -45,10 +45,6 @@ #endif #include "bacnet/basic/object/device.h" -/* forward prototype */ -int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata); -bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data); - static struct my_object_functions { BACNET_OBJECT_TYPE Object_Type; object_init_function Object_Init; diff --git a/tools/avstack/avstack.pl b/tools/avstack/avstack.pl new file mode 100755 index 00000000..93d39fa1 --- /dev/null +++ b/tools/avstack/avstack.pl @@ -0,0 +1,251 @@ +#!/usr/bin/perl -w +# avstack.pl: GCC C stack checker +# Copyright (C) 2013 Daniel Beer +# +# Permission to use, copy, modify, and/or distribute this software for +# any purpose with or without fee is hereby granted, provided that the +# above copyright notice and this permission notice appear in all +# copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL +# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR +# PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER +# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +# PERFORMANCE OF THIS SOFTWARE. +# +# Usage +# ----- +# +# This script requires that you compile your code with -fstack-usage. +# This results in GCC generating a .su file for each .o file. Once you +# have these, do: +# +# ./avstack.pl +# +# This will disassemble .o files to construct a call graph, and read +# frame size information from .su. The call graph is traced to find, for +# each function: +# +# - Call height: the maximum call height of any callee, plus 1 +# (defined to be 1 for any function which has no callees). +# +# - Inherited frame: the maximum *inherited* frame of any callee, plus +# the GCC-calculated frame size of the function in question. +# +# Using these two pieces of information, we calculate a cost (estimated +# peak stack usage) for calling the function. Functions are then listed +# on stdout in decreasing order of cost. +# +# Functions which are recursive are marked with an 'R' to the left of +# them. Their cost is calculated for a single level of recursion. +# +# The peak stack usage of your entire program can usually be estimated +# as the stack cost of "main", plus the maximum stack cost of any +# interrupt handler which might execute. + +use strict; + +# Configuration: set these as appropriate for your architecture/project. + +my $objdump = "arm-none-eabi-objdump"; +my $call_cost = 4; + +# First, we need to read all object and corresponding .su files. We're +# gathering a mapping of functions to callees and functions to frame +# sizes. We're just parsing at this stage -- callee name resolution +# comes later. + +my %frame_size; # "func@file" -> size +my %call_graph; # "func@file" -> {callees} +my %addresses; # "addr@file" -> "func@file" + +my %global_name; # "func" -> "func@file" +my %ambiguous; # "func" -> 1 + +foreach (@ARGV) { + # Disassemble this object file to obtain a callees. Sources in the + # call graph are named "func@file". Targets in the call graph are + # named either "offset@file" or "funcname". We also keep a list of + # the addresses and names of each function we encounter. + my $objfile = $_; + my $source; + + open(DISASSEMBLY, "$objdump -dr $objfile|") || + die "Can't disassemble $objfile"; + while () { + chomp; + + if (/^([0-9a-fA-F]+) <(.*)>:/) { + my $a = $1; + my $name = $2; + + $source = "$name\@$objfile"; + $call_graph{$source} = {}; + $ambiguous{$name} = 1 if defined($global_name{$name}); + $global_name{$name} = "$name\@$objfile"; + + $a =~ s/^0*//; + $addresses{"$a\@$objfile"} = "$name\@$objfile"; + } + + if (/: R_[A-Za-z0-9_]+_CALL[ \t]+(.*)/) { + my $t = $1; + + if ($t eq ".text") { + $t = "\@$objfile"; + } elsif ($t =~ /^\.text\+0x(.*)$/) { + $t = "$1\@$objfile"; + } + + $call_graph{$source}->{$t} = 1; + } + } + close(DISASSEMBLY); + + # Extract frame sizes from the corresponding .su file. + if ($objfile =~ /^(.*).o$/) { + my $sufile = "$1.su"; + + open(SUFILE, "<$sufile") || die "Can't open $sufile"; + while () { + $frame_size{"$1\@$objfile"} = $2 + $call_cost + if /^.*:([^\t ]+)[ \t]+([0-9]+)/; + } + close(SUFILE); + } +} + +# In this step, we enumerate each list of callees in the call graph and +# try to resolve the symbols. We omit ones we can't resolve, but keep a +# set of them anyway. + +my %unresolved; + +foreach (keys %call_graph) { + my $from = $_; + my $callees = $call_graph{$from}; + my %resolved; + + foreach (keys %$callees) { + my $t = $_; + + if (defined($addresses{$t})) { + $resolved{$addresses{$t}} = 1; + } elsif (defined($global_name{$t})) { + $resolved{$global_name{$t}} = 1; + warn "Ambiguous resolution: $t" if defined ($ambiguous{$t}); + } elsif (defined($call_graph{$t})) { + $resolved{$t} = 1; + } else { + $unresolved{$t} = 1; + } + } + + $call_graph{$from} = \%resolved; +} + +# Create fake edges and nodes to account for dynamic behaviour. +$call_graph{"INTERRUPT"} = {}; + +foreach (keys %call_graph) { + $call_graph{"INTERRUPT"}->{$_} = 1 if /^__vector_/; +} + +# Trace the call graph and calculate, for each function: +# +# - inherited frames: maximum inherited frame of callees, plus own +# frame size. +# - height: maximum height of callees, plus one. +# - recursion: is the function called recursively (including indirect +# recursion)? + +my %has_caller; +my %visited; +my %total_cost; +my %call_depth; + +sub trace { + my $f = shift; + + if ($visited{$f}) { + $visited{$f} = "R" if $visited{$f} eq "?"; + return; + } + + $visited{$f} = "?"; + + my $max_depth = 0; + my $max_frame = 0; + + my $targets = $call_graph{$f} || die "Unknown function: $f"; + if (defined($targets)) { + foreach (keys %$targets) { + my $t = $_; + + $has_caller{$t} = 1; + trace($t); + + my $is = $total_cost{$t}; + my $d = $call_depth{$t}; + + $max_frame = $is if $is > $max_frame; + $max_depth = $d if $d > $max_depth; + } + } + + $call_depth{$f} = $max_depth + 1; + $total_cost{$f} = $max_frame + ($frame_size{$f} || 0); + $visited{$f} = " " if $visited{$f} eq "?"; +} + +foreach (keys %call_graph) { trace $_; } + +# Now, print results in a nice table. +printf " %-40s %8s %8s %8s\n", + "Function Name", "Cost", "Frame", "Height"; +print "------------------------------------"; +print "------------------------------------\n"; + +my $max_iv = 0; +my $main = 0; + +foreach (sort { $total_cost{$b} <=> $total_cost{$a} } keys %visited) { + my $name = $_; + + if (/^(.*)@(.*)$/) { + $name = $1 unless $ambiguous{$name}; + } + + my $tag = $visited{$_}; + my $cost = $total_cost{$_}; + + $name = $_ if $ambiguous{$name}; + $tag = ">" unless $has_caller{$_}; + + if (/^__vector_/) { + $max_iv = $cost if $cost > $max_iv; + } elsif (/^main@/) { + $main = $cost; + } + + if ($ambiguous{$name}) { $name = $_; } + + printf "%s %-40s %8d %8d %8d\n", $tag, $name, $cost, + $frame_size{$_} || 0, $call_depth{$_}; +} + +print "\n"; + +print "Peak execution estimate (main + worst-case IV):\n"; +printf " main = %d, worst IV = %d, total = %d\n", + $total_cost{$global_name{"main"}}, + $total_cost{"INTERRUPT"}, + $total_cost{$global_name{"main"}} + $total_cost{"INTERRUPT"}; + +print "\n"; + +print "The following functions were not resolved:\n"; +foreach (keys %unresolved) { print " $_\n"; } diff --git a/tools/avstack/readme.md b/tools/avstack/readme.md new file mode 100644 index 00000000..1d4aee93 --- /dev/null +++ b/tools/avstack/readme.md @@ -0,0 +1,120 @@ +# avstack.pl + +dlbeer@gmail.com +31 May 2013 (updated 24 Aug 2015) + +When developing for memory constrained systems, it's useful to know at compile-time that the available memory is sufficient for your application firmware. Dynamic allocation is usually avoided, and the size of statically allocated memory in the `.data` and `.bss` sections can be easily inspected with GNU `size` or a similary tool. This leaves one problem: stack use. The stack grows and shrinks dynamically at runtime, but under some circumstances, the peak stack use can be statically analyzed. + +* [avstack.pl](avstack.pl) + +The script linked above is a static stack checker, intended for use with a recent-ish version of [AVR-GCC](http://gcc.gnu.org/wiki/avr-gcc). In order to use it, you must ensure that your object files are compiled with `-fstack-usage`. This causes GCC to generate a `.su` file for every `.o` file. [These files](http://gcc.gnu.org/onlinedocs/gnat_ugn_unw/Static-Stack-Usage-Analysis.html) contain information on the size of the stack frame for each function compiled in the `.o` file. For example: + + sha1.c:43:13:mix_w 0 static + sha1.c:54:13:rot_left 0 static + sha1.c:69:13:rot_right 0 static + sha1.c:178:6:sha1_init 0 static + sha1.c:183:6:sha1_next 65 static + sha1.c:192:6:sha1_final 6 static + +Once you have these files, invoke `avstack.pl`, passing as arguments the names of all `.o` files that will be linked into your binary. The `.su` files are assumed to be located in the same directories as their corresponding `.o` files. + +The script reads all `.su` files, and disassembles all `.o` files, including relocation data. The disassemblies are parsed and used to construct a call graph. Multiple functions in different translation units with the same name don't cause problems, provided there are no global namespace collisions. Information will appear on any unresolvable or ambiguous references. + +Next, the call graph is traced to find the peak stack usage of all functions. This is calculated for each function as the maximum stack usage of any of its callees, plus its own stack frame, plus some call-cost constant (not included in GCC's analysis). + +Here's an example of the output produced: + + $ ./avstack.pl */*.o + Func Cost Frame Height + ------------------------------------------------------------------------ + > main 235 90 4 + prng_next 145 72 3 + sha1_final 83 10 3 + sha1_next 73 69 2 + __vector_16 28 20 3 + > INTERRUPT 28 0 4 + clock_signal_tick 8 4 2 + clock_iterate 8 4 2 + clock_init 4 4 1 + led_init 4 4 1 + led_set 4 4 1 + > led_get 4 4 1 + rot_right 4 4 1 + mix_w 4 4 1 + prng_init 4 4 1 + clock_signal_pps 4 4 1 + clock_get_raw 4 4 1 + clock_get 4 4 1 + prng_stir 4 4 1 + rot_left 4 4 1 + sha1_init 4 4 1 + led_slot_clock_tick 4 4 1 + + The following functions were not resolved: + memcpy_P + +The columns are: + +* **Cost**: peak stack usage during a call to the function. +* **Frame**: stack frame size, obtained from the `.su` file, plus the call-cost constant. +* **Height**: height in call graph -- calculated as maximum height of any callee, plus one. + +Indicators to the left of the function name indicate features of the call graph. A `>` indicates that the function has no callee. This could be because it's an entry point (like `main` or an interrupt vector), because the function is called dynamically through a function pointer, or simply that it's unused. An `R` indicates that the function is recursive, and the cost estimate is for a single level of recursion. Multiple functions with the same name are distinguished in the listing by appending a suffix of the form `@filename.o`. + +You can customize this script by altering two variables near the beginning: + + my $objdump = "avr-objdump"; + my $call_cost = 4; + +Note that making sense of the output of the stack analysis still requires you to know something about how your program runs. For example, in many programs, the actual peak stack use would be the cost of `main`, plus the maximum cost of any interrupt handler that might execute. You will also need to take into account dynamically invoked functions, if you have any. + +To make things easier, there is a processing section in which fake nodes and edges can be added to the call graph to account for dynamic behaviour. For example, the script currently contains in this section, to represent interrupt execution: + + $call_graph{"INTERRUPT"} = {}; + + foreach (keys %call_graph) { + $call_graph{"INTERRUPT"}->{$_} = 1 if /^__vector_/; + } + +## Use with C++ + +Updated: 24 Aug 2015 + +Unfortunately, GCC uses "printable" names in the output for `-fstack-usage`. For example, the function: + + namespace test { + + uint32_t crc32_ieee802_3(const uint8_t *data, size_t len, + uint32_t crc = 0) + { + ... + } + + } + +Gets listed as: + + foo.cpp:6:10:uint32_t test::crc32_ieee802_3(const uint8_t*, size_t, uint32_t) 32 static + +This is difficult to match up with the disassembler output. Using `c++filt` doesn't help, because it doesn't know anything about typedefs (`uint8_t` becomes `unsigned char`, for example). + +I've prepared the following patches for two versions of GCC. They're independent of language and target: + +* [gcc-4.7.2-mangled-stack-usage.patch](../downloads/gcc-4.7.2-mangled-stack-usage.patch) +* [gcc-4.9.2-mangled-stack-usage.patch](../downloads/gcc-4.9.2-mangled-stack-usage.patch) + +These patches cause GCC to use names in the output for `-fstack-usage` which match the disassembler output: + + foo.cpp:6:10:_ZN4test15crc32_ieee802_3EPKhyj 32 static + +Nothing changes for C code. With these patches, `avstack.pl` works fine for C++ (although you probably want to run the final output through `c++filt`). + +## Copyright + +Copyright © 2013 Daniel Beer dlbeer@gmail.com + +> + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/tools/check-stack-usage/.gitignore b/tools/check-stack-usage/.gitignore new file mode 100644 index 00000000..b43a707f --- /dev/null +++ b/tools/check-stack-usage/.gitignore @@ -0,0 +1,3 @@ +.analysed +.setup +.venv/ diff --git a/tools/check-stack-usage/LICENSE b/tools/check-stack-usage/LICENSE new file mode 100644 index 00000000..7446fa69 --- /dev/null +++ b/tools/check-stack-usage/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright © 2021 Thanassis Tsiodras + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/tools/check-stack-usage/Makefile b/tools/check-stack-usage/Makefile new file mode 100644 index 00000000..784e3dc9 --- /dev/null +++ b/tools/check-stack-usage/Makefile @@ -0,0 +1,73 @@ +TARGET:= checkStackUsage.py +PYTHON:=python3 +VENV:=.venv + +all: .setup .analysed test ## Check everything + +.analysed: ${TARGET} + $(MAKE) flake8 + $(MAKE) pylint + $(MAKE) mypy + @touch $@ + +flake8: dev-install ## PEP8 compliance checks with Flake8 + @echo "============================================" + @echo " Running flake8..." + @echo "============================================" + ${VENV}/bin/flake8 ${TARGET} + +pylint: dev-install ## Static analysis with Pylint + @echo "============================================" + @echo " Running pylint..." + @echo "============================================" + ${VENV}/bin/pylint --disable=I --rcfile=pylint.cfg ${TARGET} + +mypy: dev-install ## Type checking with mypy + @echo "============================================" + @echo " Running mypy..." + @echo "============================================" + ${VENV}/bin/mypy --ignore-missing-imports ${TARGET} + +dev-install: .setup | prereq + +prereq: + @${PYTHON} -c 'import sys; sys.exit(1 if (sys.version_info.major<3 or sys.version_info.minor<5) else 0)' || { \ + echo "=============================================" ; \ + echo "[x] You need at least Python 3.5 to run this." ; \ + echo "=============================================" ; \ + exit 1 ; \ + } + +.setup: requirements.txt + @if [ ! -d ${VENV} ] ; then \ + echo "[-] Installing VirtualEnv environment..." ; \ + ${PYTHON} -m venv ${VENV} || exit 1 ; \ + fi + echo "[-] Installing packages inside environment..." ; \ + . ${VENV}/bin/activate || exit 1 ; \ + ${PYTHON} -m pip install -r requirements.txt || exit 1 + touch $@ + +test: ## Test proper functioning + $(MAKE) -C tests/ + +clean: ## Cleanup artifacts + rm -rf .cache/ .mypy_cache/ .analysed .setup __pycache__ \ + tests/__pycache__ .pytest_cache/ .processed .coverage + $(MAKE) -C tests/ clean + +help: ## Display this help section + @awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z0-9_-]+:.*?## / {printf "\033[36m%-18s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) +.DEFAULT_GOAL := help + +define newline # a literal \n + + +endef + +# Makefile debugging trick: +# call print-VARIABLE to see the runtime value of any variable +# (hardened a bit against some special characters appearing in the output) +print-%: + @echo '$*=$(subst ','\'',$(subst $(newline),\n,$($*)))' +.PHONY: flake8 pylint mypy clean dev-install prereq diff --git a/tools/check-stack-usage/README.md b/tools/check-stack-usage/README.md new file mode 100644 index 00000000..61ccf81f --- /dev/null +++ b/tools/check-stack-usage/README.md @@ -0,0 +1,173 @@ +*(Detailed blog post [here](https://www.thanassis.space/stackusage.html) )* + +# Introduction + +This is a utility that computes the stack usage per function. It is +particularly useful in embedded systems programming, where memory is at a +premium - and also in safety-critical SW *(blowing up from a stack overflow +while you operate medical equipment or fly in space is not exactly optimal)*. + +# In detail + +*(Detailed blog post [here](https://www.thanassis.space/stackusage.html) )* + +You can read many details about how this script works in the blog post +linked above; but the executive summary is this: + +- We expect the source code compilation to use GCC's `-fstack-usage`. + This generates `.su` files with the stack usage of each function + **(in isolation)** stored per compilation unit. Simply put, `file.c` + compiled with `-fstack-usage` will create `file.o` *and* `file.su`. + +- The script can then be launched like so: + + checkStackUsage.py binaryELF folderTreeContainingSUfiles + +For example, if after the build we have a tree like this: + + bin/ + someBinary + src/ + file1.c + file1.o + file1.su + lib1_src/ + lib1.c + lib1.o + lib1.su + lib2_src/ + lib2.c + lib2.o + lib2.su + +...we run this: + + checkStackUsage.py bin/someBinary src/ + +The script will scan all .su files in the `src` folder *(recursively, +at any depth)* and collect the standalone use of stack for each function. + +It will then launch the appropriate `objdump` with option `-d` - to +disassemble the code, and create the call graph. Simplistically, it +detects patterns like this: + + : + .... + call + +...and proceeds from there to create the entire call graph. +It can then accumulate the use of all subordinate calls from each function, +and therefore compute it's total stack usage. + +Output looks like this: + + 176: foo (foo(16),func(160)) + 288: func (func(288)) + 304: bar (bar(16),func(288)) + 320: main (main(16),bar(16),func(288)) + +...which means that function `foo` uses 176 bytes of stack; 16 because of +itself, and 160 because it calls `func`. `main` uses 320 bytes, etc. + +Notice that `bar` also uses `func` - but reports a larger stack size for it +in that call chain. Read section "Repeated functions" below, to see why; +suffice to say, this is one of the few stack checkers that can cope with +symbols defined more than once. + +# Platforms + +The script needs to spawn the right `objdump`. It uses +`file` to detect the ELF signature, and uses appropriate regexes +to match disassembly `call` forms for: + + - SPARC/LEONs (used in the European Space Agency missions) + - x86/amd64 (both 32 and 64 bits) + - 32-bit ARM + +Adding additional platforms is very easy - just tell the script what +`objdump` flavor to use, and what regex to scan for to locate the +call sequences; [relevant code is here](checkStackUsage.py#L198). + +# Repeated functions + +Each function can only have a specific stack usage - right? + +Sadly, no :-( + +Feast yourself on moronic - yet perfectly valid - C code like this: + + // a.c + static int func() { ...} + void foo() { func(); } + + // b.c + static int func() { ...} + void bar() { func(); } + +"Houston, we have a problem". While scanning the `.su` files for `a.c` +and `b.c`, we find `func` twice - and due to the `static`, we want +to use the right value on each call (from `foo`/`bar`) based on +filescope. In effect, the .su files' content need to be read +*prioritizing local static calls* when computing stack usage. + +# Hidden calls + +The scanning of `objdump` output for call sequences is the best we can do; +but it's not perfect. For example, any calls made via function pointer +indirections are "invisible" to this process. + +And since fp-based calls can do all sorts of shenanigans - e.g. +reading the call endpoint from an array of functions via some +algorithm - statically deducing which functions are actually called +is tantamount to the halting problem. + +I am open to suggestions on this. + +# Static Analysis + +The script is written in Python - `make all` will check it with: + +- `flake8` (PEP8 compliance) +- `pylint` (Static Analysis) +- ...and `mypy` (static type checking). + +All dependencies to perform these checks will be automatically +installed via `pip` in a local virtual environment (i.e. +under folder `.venv`) the first time you invoke `make all`. + +# Test example + +The scenario of repeated functions is tested via `make test`; +in my 64-bit Arch Linux I see this output: + + $ make test + ... + make[1]: Entering directory '/home/ttsiod/Github/checkStackUsage/tests' + ============================================== + 176: foo (foo(16),func(160)) + 288: func (func(288)) + 304: bar (bar(16),func(288)) + 320: main (main(16),bar(16),func(288)) + ============================================== + 1. The output shown above must contain 4 lines + 2. 'foo' and 'bar' must both be calling 'func' + *but with different stack sizes in each*. + 3. 'main' must be using the largest 'func' + (i.e. be going through 'bar') + 4. The reported sizes must properly accumulate + ============================================== + make[1]: Leaving directory '/home/ttsiod/Github/checkStackUsage/tests' + +Given the content of [a.c](tests/a.c), [b.c](tests/b.c) and +[main.c](tests/main.c), the output looks good. Notice that for +`func` we report the maximum of the two (the one reported by +GCC inside `b.c`, when it is called by `bar`). + +# Feedback + +If you see something wrong in the script that isn't documented above, +questions (and much better, pull requests) are most welcome. + +Thanassis Tsiodras, Dr.-Ing. +ttsiodras_at-no-spam_thanks-gmail_dot-com + diff --git a/tools/check-stack-usage/checkStackUsage.py b/tools/check-stack-usage/checkStackUsage.py new file mode 100755 index 00000000..d8a6f1d1 --- /dev/null +++ b/tools/check-stack-usage/checkStackUsage.py @@ -0,0 +1,445 @@ +#!/usr/bin/env python3 +""" +Utility to detect recursive calls and calculate total stack usage per function +(via following the call graph). + +Published under the GPL, (C) 2011 Thanassis Tsiodras +Suggestions/comments: ttsiodras@gmail.com + +Updated to use GCC-based .su files (-fstack-usage) in 2021. +""" + +import os +import re +import sys +import operator + +from typing import Dict, Set, Optional, List, Tuple + +FunctionName = str +FunctionNameToInt = Dict[FunctionName, int] + +# For each function, what is the set of functions it calls? +CallGraph = Dict[FunctionName, Optional[Set[FunctionName]]] + +# .su files data +Filename = str +SuData = Dict[FunctionName, List[Tuple[Filename, int]]] + + +# findStackUsage will return a tuple: +# - The total stack size used +# - A list of pairs, of the functions/stacksizes, adding up to the final use. +UsageResult = Tuple[int, List[Tuple[FunctionName, int]]] + + +class Matcher: + """regexp helper""" + def __init__(self, pattern, flags=0): + self._pattern = re.compile(pattern, flags) + self._hit = None + + def match(self, line): + self._hit = re.match(self._pattern, line) + return self._hit + + def search(self, line): + self._hit = re.search(self._pattern, line) + return self._hit + + def group(self, idx): + return self._hit.group(idx) + + +def lookupStackSizeOfFunction( + fn: FunctionName, + fns: List[Tuple[FunctionName, int]], + suData: SuData, + stackUsagePerFunction: FunctionNameToInt) -> int: + """ + What do you do when you have moronic C code like this? + + === + a.c + === + static int func() { ...} + void foo() { func(); } + + === + b.c + === + static int func() { ...} + void bar() { func(); } + + You have a problem. There is only one entry in stackUsagePerFunction, + since we assign as we scan the su files - but you basically want to + use the right value, based on filescope (due to the static). + In effect, the .su files need to be read *prioritizing local calls* + when computing stack usage. + + So what we do is this: we have suData - a dictionary that stores + per each function name, WHERE we found it (at which .su filenames) + and what size it had in each of them. We scan that list, looking for + the last filename in our call chain so far (i.e. the last of the 'fns'). + + And we then report the associated stack use. + """ + # If the call chain so far is empty, or our function is not in the + # .su files (e.g. it comes from a library we linked with), then we + # use the 'su-agnostic' information we got during our symbol scan. + if not fns or fn not in suData: + return stackUsagePerFunction[fn] + + # Otherwise, we first get the last function in the call chain... + suFilename = None + lastCallData = fns[-1] + functionName = lastCallData[0] + + # ...and use the .su files information to get the file it lives in + # Remember, SuData is a Dict[FunctionName, List[Tuple[Filename, int]]] + suList = suData.get(functionName, []) + if suList: + suFilename = suList[0][0] # Get the filename part of the first tuple + if not suFilename: + return stackUsagePerFunction[fn] + + # Now that we have the filename of our last caller in the chain + # that calls us, scan our OWN existence in the .su data, to find + # the one that matches - thus prioritizing local calls! + for elem in suData.get(fn, []): + xFilename, xStackUsage = elem + if xFilename == suFilename: + return xStackUsage + + # ...otherwise (no match in the .su data) we use the 'su-agnostic' + # information we got during our symbol scan. + return stackUsagePerFunction[fn] + + +def findStackUsage( + fn: FunctionName, + fns: List[Tuple[FunctionName, int]], + suData: SuData, + stackUsagePerFunction: FunctionNameToInt, + callGraph: CallGraph, + badSymbols: Set[FunctionName]) -> UsageResult: + """ + Calculate the total stack usage of the input function, + taking into account who it calls. + """ + if fn in [x[0] for x in fns]: + # So, what to do with recursive functions? + # We just reached this point with fns looking like this: + # [a, b, c, d, a] + # A "baseline" answer is better than no answer; + # so we let the parent series of calls accumulate to + # as + bs + cs + ds + # ...ie. the stack sizes of all others. + # We therefore return 0 for this "last step" + # and stop the recursion here. + totalStackUsage = sum(x[1] for x in fns) + if fn not in badSymbols: + # Report recursive functions. + badSymbols.add(fn) + print("[x] Recursion detected:", fn, 'is called from', + fns[-1][0], 'in this chain:', fns) + return (totalStackUsage, fns[:]) + + if fn not in stackUsagePerFunction: + # Unknown function, what else to do? Count it as 0 stack usage... + totalStackUsage = sum(x[1] for x in fns) + return (totalStackUsage, fns[:] + [(fn, 0)]) + + thisFunctionStackSize = lookupStackSizeOfFunction( + fn, fns, suData, stackUsagePerFunction) + calledFunctions = callGraph.get(fn, set()) + + # If we call noone else, stack usage is just our own stack usage + if fn not in callGraph or not calledFunctions: + totalStackUsage = sum(x[1] for x in fns) + thisFunctionStackSize + res = (totalStackUsage, fns[:] + [(fn, thisFunctionStackSize)]) + return res + + # Otherwise, we check the stack usage for each function we call + totalStackUsage = 0 + maxStackPath = [] + for x in sorted(calledFunctions): + total, path = findStackUsage( + x, + fns[:] + [(fn, thisFunctionStackSize)], + suData, + stackUsagePerFunction, + callGraph, + badSymbols) + if total > totalStackUsage: + totalStackUsage = total + maxStackPath = path + return (totalStackUsage, maxStackPath[:]) + + +def ParseCmdLineArgs(cross_prefix: str) -> Tuple[ + str, str, Matcher, Matcher, Matcher]: + if cross_prefix: + objdump = cross_prefix + 'objdump' + nm = cross_prefix + 'nm' + functionNamePattern = Matcher(r'^(\S+) <([a-zA-Z0-9\._]+?)>:') + callPattern = Matcher(r'^.*call\s+\S+\s+<([a-zA-Z0-9\._]+)>') + stackUsagePattern = Matcher( + r'^.*save.*%sp, (-[0-9]{1,}), %sp') + else: + binarySignature = os.popen(f"file \"{sys.argv[-2]}\"").readlines()[0] + + x86 = Matcher(r'ELF 32-bit LSB.*80.86') + x64 = Matcher(r'ELF 64-bit LSB.*x86-64') + leon = Matcher(r'ELF 32-bit MSB.*SPARC') + arm = Matcher(r'ELF 32-bit LSB.*ARM') + + if x86.search(binarySignature): + objdump = 'objdump' + nm = 'nm' + functionNamePattern = Matcher(r'^(\S+) <([a-zA-Z0-9_]+?)>:') + callPattern = Matcher(r'^.*call\s+\S+\s+<([a-zA-Z0-9_]+)>') + stackUsagePattern = Matcher(r'^.*[add|sub]\s+\$(0x\S+),%esp') + elif x64.search(binarySignature): + objdump = 'objdump' + nm = 'nm' + functionNamePattern = Matcher(r'^(\S+) <([a-zA-Z0-9_]+?)>:') + callPattern = Matcher(r'^.*callq?\s+\S+\s+<([a-zA-Z0-9_]+)>') + stackUsagePattern = Matcher(r'^.*[add|sub]\s+\$(0x\S+),%rsp') + elif leon.search(binarySignature): + objdump = 'sparc-rtems5-objdump' + nm = 'sparc-rtems5-nm' + functionNamePattern = Matcher(r'^(\S+) <([a-zA-Z0-9_]+?)>:') + callPattern = Matcher(r'^.*call\s+\S+\s+<([a-zA-Z0-9_]+)>') + stackUsagePattern = Matcher( + r'^.*save.*%sp, (-([0-9]{2}|[3-9])[0-9]{2}), %sp') + elif arm.search(binarySignature): + objdump = 'arm-none-eabi-objdump' + nm = 'arm-none-eabi-nm' + functionNamePattern = Matcher(r'^(\S+) <([a-zA-Z0-9_]+?)>:') + callPattern = Matcher(r'^.*bl\s+\S+\s+<([a-zA-Z0-9_]+)>') + stackUsagePattern = Matcher( + r'^.*sub.*sp, (#[0-9][0-9]*)') + else: + print("Unknown signature:", binarySignature, "please use -cross") + sys.exit(1) + return objdump, nm, functionNamePattern, callPattern, stackUsagePattern + + +def GetSizeOfSymbols(nm: str, elf_binary: str) -> Tuple[ + FunctionNameToInt, FunctionNameToInt]: + # Store .text symbol offsets and sizes (use nm) + offsetOfSymbol = {} # type: FunctionNameToInt + for line in os.popen( + nm + " \"" + elf_binary + "\" | grep ' [Tt] '").readlines(): + offsetData, unused, symbolData = line.split() + offsetOfSymbol[symbolData] = int(offsetData, 16) + sizeOfSymbol = {} + lastOffset = 0 + lastSymbol = "" + sortedSymbols = sorted( + offsetOfSymbol.items(), key=operator.itemgetter(1)) + for symbolStr, offsetInt in sortedSymbols: + if lastSymbol != "": + sizeOfSymbol[lastSymbol] = offsetInt - lastOffset + lastSymbol = symbolStr + lastOffset = offsetInt + sizeOfSymbol[lastSymbol] = 2**31 # allow last .text symbol to roam free + return sizeOfSymbol, offsetOfSymbol + + +def GetCallGraph( + objdump: str, + offsetOfSymbol: FunctionNameToInt, sizeOfSymbol: FunctionNameToInt, + functionNamePattern: Matcher, stackUsagePattern: Matcher, + callPattern: Matcher) -> Tuple[CallGraph, FunctionNameToInt]: + + # Parse disassembly to create callgraph (use objdump -d) + functionName = "" + stackUsagePerFunction = {} # type: FunctionNameToInt + callGraph = {} # type: CallGraph + insideFunctionBody = False + + offsetPattern = Matcher(r'^([0-9A-Za-z]+):') + for line in os.popen(objdump + " -d \"" + sys.argv[-2] + "\"").readlines(): + # Have we matched a function name yet? + if functionName != "": + # Yes, update "insideFunctionBody" boolean by checking + # the current offset against the length of this symbol, + # stored in sizeOfSymbol[functionName] + offset = offsetPattern.match(line) + if offset: + offset = int(offset.group(1), 16) + if functionName in offsetOfSymbol: + startOffset = offsetOfSymbol[functionName] + insideFunctionBody = \ + insideFunctionBody and \ + (offset - startOffset) < sizeOfSymbol[functionName] + + # Check to see if we see a new function: + # 08048be8 <_functionName>: + fn = functionNamePattern.match(line) + if fn: + offset = int(fn.group(1), 16) + functionName = fn.group(2) + callGraph.setdefault(functionName, set()) + # make sure this is the function we found with nm + # UPDATE: no, can't do - if a symbol is of local file scope + # (i.e. if it was declared with 'static') + # then it may appear in multiple offsets!... + # + # if functionName in offsetOfSymbol: + # if offsetOfSymbol[functionName] != offset: + # print "Weird,", functionName, \ + # "is not at offset reported by", nm + # print hex(offsetOfSymbol[functionName]), hex(offset) + insideFunctionBody = True + foundFirstCall = False + stackUsagePerFunction[functionName] = 0 + + # If we're inside a function body + # (i.e. offset is not out of symbol size range) + if insideFunctionBody: + # Check to see if we have a call + # 8048c0a: e8 a1 03 00 00 call 8048fb0 + call = callPattern.match(line) + if functionName != "" and call: + foundFirstCall = True + calledFunction = call.group(1) + calledFunctions = callGraph[functionName] + if calledFunctions is not None: + calledFunctions.add(calledFunction) + + # Check to see if we have a stack reduction opcode + # 8048bec: 83 ec 04 sub $0x46,%esp + if functionName != "" and not foundFirstCall: + stackMatch = stackUsagePattern.match(line) + if stackMatch: + value = stackMatch.group(1) + if value.startswith("0x"): + # sub $0x46,%esp + value = int(stackMatch.group(1), 16) + if value > 2147483647: + # unfortunately, GCC may also write: + # add $0xFFFFFF86,%esp + value = 4294967296 - value + elif value.startswith("#"): + # sub sp, sp, #1024 + value = int(value[1:]) + else: + # save %sp, -104, %sp + value = -int(value) + assert( + stackUsagePerFunction[functionName] is not None) + stackUsagePerFunction[functionName] += value + + # for fn,v in stackUsagePerFunction.items(): + # print fn,v + # print "CALLS:", callGraph[fn] + return callGraph, stackUsagePerFunction + + +def ReadSU(fullPathToSuFile: str) -> Tuple[FunctionNameToInt, SuData]: + stackUsagePerFunction = {} # type: FunctionNameToInt + suData = {} # type: SuData + # pylint: disable=R1732 + for line in open(fullPathToSuFile, encoding='utf-8'): + data = line.strip().split() + if len(data) == 3 and data[2] == 'static': + try: + functionName = data[0].split(':')[-1] + functionStackUsage = int(data[1]) + except ValueError: + continue + stackUsagePerFunction[functionName] = functionStackUsage + suData.setdefault(functionName, []).append( + (fullPathToSuFile, functionStackUsage)) + return stackUsagePerFunction, suData + + +def GetSizesFromSUfiles(root_path) -> Tuple[FunctionNameToInt, SuData]: + stackUsagePerFunction = {} # type: FunctionNameToInt + suData = {} # type: SuData + for root, unused_dirs, files in os.walk(root_path): + for f in files: + if f.endswith('.su'): + supf, sud = ReadSU(root + os.sep + f) + for functionName, v1 in supf.items(): + stackUsagePerFunction[functionName] = max( + v1, stackUsagePerFunction.get(functionName, 0)) + + # We need to augment the list of .su if there's + # a symbol that appears in two or more .su files. + # SuData = Dict[FunctionName, List[Tuple[Filename, int]]] + for functionName, v2 in sud.items(): + suData.setdefault(functionName, []).extend(v2) + return stackUsagePerFunction, suData + + +def main() -> None: + cross_prefix = '' + try: + idx = sys.argv.index("-cross") + except ValueError: + idx = -1 + if idx != -1: + cross_prefix = sys.argv[idx + 1] + del sys.argv[idx] + del sys.argv[idx] + + chosenFunctionNames = [] + if len(sys.argv) >= 4: + chosenFunctionNames = sys.argv[3:] + sys.argv = sys.argv[:3] + + if len(sys.argv) < 3 or not os.path.exists(sys.argv[-2]) \ + or not os.path.isdir(sys.argv[-1]): + print(f"Usage: {sys.argv[0]} [-cross PREFIX]" + " ELFbinary root_path_for_su_files [functions...]") + print("\nwhere the default prefix is:\n") + print("\tarm-none-eabi- for ARM binaries") + print("\tsparc-rtems5- for SPARC binaries") + print("\t(no prefix) for x86/amd64 binaries") + print("\nNote that if you use '-cross', SPARC opcodes are assumed.\n") + sys.exit(1) + + objdump, nm, functionNamePattern, callPattern, stackUsagePattern = \ + ParseCmdLineArgs(cross_prefix) + sizeOfSymbol, offsetOfSymbol = GetSizeOfSymbols(nm, sys.argv[-2]) + callGraph, stackUsagePerFunction = GetCallGraph( + objdump, + offsetOfSymbol, sizeOfSymbol, + functionNamePattern, stackUsagePattern, callPattern) + + supf, suData = GetSizesFromSUfiles(sys.argv[-1]) + alreadySeen = set() # type: Set[Filename] + badSymbols = set() # type: Set[Filename] + for k, v in supf.items(): + if k in alreadySeen: + badSymbols.add(k) + alreadySeen.add(k) + stackUsagePerFunction[k] = max( + v, stackUsagePerFunction.get(k, 0)) + + # Then, navigate the graph to calculate stack needs per function + results = [] + for fn, value in stackUsagePerFunction.items(): + if chosenFunctionNames and fn not in chosenFunctionNames: + continue + if value is not None: + results.append( + (fn, + findStackUsage( + fn, [], suData, stackUsagePerFunction, callGraph, + badSymbols))) + for fn, data in sorted(results, key=lambda x: x[1][0]): + # pylint: disable=C0209 + print( + "%10s: %s (%s)" % ( + data[0], fn, ",".join( + x[0] + "(" + str(x[1]) + ")" + for x in data[1]))) + + +if __name__ == "__main__": + main() diff --git a/tools/check-stack-usage/pylint.cfg b/tools/check-stack-usage/pylint.cfg new file mode 100644 index 00000000..040e0e8d --- /dev/null +++ b/tools/check-stack-usage/pylint.cfg @@ -0,0 +1,381 @@ +[MASTER] + +# Specify a configuration file. +#rcfile= + +# Python code to execute, usually for sys.path manipulation such as +# pygtk.require(). +#init-hook= + +# Add files or directories to the blacklist. They should be base names, not +# paths. +ignore=CVS + +# Pickle collected data for later comparisons. +persistent=yes + +# List of plugins (as comma separated values of python modules names) to load, +# usually to register additional checkers. +load-plugins= + +# Use multiple processes to speed up Pylint. +jobs=1 + +# Allow loading of arbitrary C extensions. Extensions are imported into the +# active Python interpreter and may run arbitrary code. +unsafe-load-any-extension=no + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code +extension-pkg-whitelist=lxml,clang + +# Allow optimization of some AST trees. This will activate a peephole AST +# optimizer, which will apply various small optimizations. For instance, it can +# be used to obtain the result of joining multiple strings with the addition +# operator. Joining a lot of strings can lead to a maximum recursion error in +# Pylint and this flag can prevent that. It has one side effect, the resulting +# AST will be different than the one from reality. +optimize-ast=no + + +[MESSAGES CONTROL] + +# Only show warnings with the listed confidence levels. Leave empty to show +# all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED +confidence= + +# Enable the message, report, category or checker with the given id(s). You can +# either give multiple identifier separated by comma (,) or put this option +# multiple time (only on the command line, not in the configuration file where +# it should appear only once). See also the "--disable" option for examples. +#enable= + +# Disable the message, report, category or checker with the given id(s). You +# can either give multiple identifiers separated by comma (,) or put this +# option multiple times (only on the command line, not in the configuration +# file where it should appear only once).You can also use "--disable=all" to +# disable everything first and then reenable specific checks. For example, if +# you want to run only the similarities checker, you can use "--disable=all +# --enable=similarities". If you want to run only the classes checker, but have +# no Warning level messages displayed, use"--disable=all --enable=classes +# --disable=W" +disable=coerce-method,nonzero-method,buffer-builtin,unichr-builtin,reload-builtin,using-cmp-argument,reduce-builtin,filter-builtin-not-iterating,zip-builtin-not-iterating,raising-string,long-builtin,backtick,long-suffix,delslice-method,suppressed-message,cmp-method,old-octal-literal,basestring-builtin,metaclass-assignment,print-statement,execfile-builtin,round-builtin,oct-method,standarderror-builtin,hex-method,import-star-module-level,indexing-exception,map-builtin-not-iterating,old-ne-operator,setslice-method,input-builtin,apply-builtin,range-builtin-not-iterating,xrange-builtin,parameter-unpacking,no-absolute-import,old-raise-syntax,dict-iter-method,unicode-builtin,unpacking-in-except,old-division,file-builtin,next-method-called,useless-suppression,raw_input-builtin,intern-builtin,getslice-method,dict-view-method,cmp-builtin,coerce-builtin,line-too-long,missing-docstring,protected-access,global-statement,too-many-arguments,too-many-branches,too-many-locals,bare-except,invalid-name,too-many-statements,broad-except,too-many-instance-attributes,too-many-public-methods,too-few-public-methods,similarities,no-else-return,fixme,relative-beyond-top-level,import-outside-toplevel,too-many-nested-blocks + +never-returning-functions=dmt.commonPy.utility.panic,sys.exit + +[REPORTS] + +# Set the output format. Available formats are text, parseable, colorized, msvs +# (visual studio) and html. You can also give a reporter class, eg +# mypackage.mymodule.MyReporterClass. +output-format=text + +# Put messages in a separate file for each module / package specified on the +# command line instead of printing them on stdout. Reports (if any) will be +# written in a file name "pylint_global.[txt|html]". +files-output=no + +# Tells whether to display a full report or only the messages +reports=yes + +# Python expression which should return a note less than 10 (10 is the highest +# note). You have access to the variables errors warning, statement which +# respectively contain the number of errors / warnings messages and the total +# number of statements analyzed. This is used by the global evaluation report +# (RP0004). +evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10) + +# Template used to display messages. This is a python new-style format string +# used to format the message information. See doc for all details +#msg-template= + + +[BASIC] + +# List of builtins function names that should not be used, separated by a comma +bad-functions=map,filter + +# Good variable names which should always be accepted, separated by a comma +good-names=i,j,k,ex,Run,_ + +# Bad variable names which should always be refused, separated by a comma +bad-names=foo,bar,baz,toto,tutu,tata + +# Colon-delimited sets of names that determine each other's naming style when +# the name regexes allow several styles. +name-group= + +# Include a hint for the correct naming format with invalid-name +include-naming-hint=no + +# Regular expression matching correct module names +module-rgx=(([a-z_][A-Za-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$ + +# Naming hint for module names +module-name-hint=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$ + +# Regular expression matching correct constant names +const-rgx=(([a-zA-Z_][a-zA-Z0-9_]*)|(__.*__))$ + +# Naming hint for constant names +const-name-hint=(([a-zA-Z_][A-Z0-9_]*)|(__.*__))$ + +# Regular expression matching correct class names +class-rgx=[A-Z_][a-zA-Z0-9]+$ + +# Naming hint for class names +class-name-hint=[A-Z_][a-zA-Z0-9]+$ + +# Regular expression matching correct method names +method-rgx=[a-zA-Z_][a-zA-Z0-9_]{2,30}$ + +# Naming hint for method names +method-name-hint=[a-z_][a-z0-9_]{2,30}$ + +# Regular expression matching correct argument names +argument-rgx=[a-z_][A-Za-z0-9_]{2,30}$ + +# Naming hint for argument names +argument-name-hint=[a-z_][A-Za-z0-9_]{2,30}$ + +# Regular expression matching correct class attribute names +class-attribute-rgx=([A-Za-z_][A-Za-z0-9_]{2,30}|(__.*__))$ + +# Naming hint for class attribute names +class-attribute-name-hint=([A-Za-z_][A-Za-z0-9_]{2,30}|(__.*__))$ + +# Regular expression matching correct inline iteration names +inlinevar-rgx=[A-Za-z_][A-Za-z0-9_]*$ + +# Naming hint for inline iteration names +inlinevar-name-hint=[A-Za-z_][A-Za-z0-9_]*$ + +# Regular expression matching correct function names +function-rgx=[A-Za-z_][A-Za-z0-9_]{2,30}$ + +# Naming hint for function names +function-name-hint=[a-z_][a-z0-9_]{2,30}$ + +# Regular expression matching correct attribute names +attr-rgx=[a-z_][A-Za-z0-9_]{2,30}$ + +# Naming hint for attribute names +attr-name-hint=[a-z_][a-z0-9_]{2,30}$ + +# Regular expression matching correct variable names +variable-rgx=[a-z_][A-Za-z0-9_]{2,30}$ + +# Naming hint for variable names +variable-name-hint=[a-z_][a-z0-9_]{2,30}$ + +# Regular expression which should only match function or class names that do +# not require a docstring. +no-docstring-rgx=^_ + +# Minimum line length for functions/classes that require docstrings, shorter +# ones are exempt. +docstring-min-length=-1 + + +[ELIF] + +# Maximum number of nested blocks for function / method body +max-nested-blocks=5 + + +[SIMILARITIES] + +# Minimum lines number of a similarity. +min-similarity-lines=4 + +# Ignore comments when computing similarities. +ignore-comments=yes + +# Ignore docstrings when computing similarities. +ignore-docstrings=yes + +# Ignore imports when computing similarities. +ignore-imports=no + + +[FORMAT] + +# Maximum number of characters on a single line. +max-line-length=140 + +# Regexp for a line that is allowed to be longer than the limit. +ignore-long-lines=^\s*(# )??$ + +# Allow the body of an if to be on the same line as the test if there is no +# else. +single-line-if-stmt=no + +# List of optional constructs for which whitespace checking is disabled. `dict- +# separator` is used to allow tabulation in dicts, etc.: {1 : 1,\n222: 2}. +# `trailing-comma` allows a space between comma and closing bracket: (a, ). +# `empty-line` allows space-only lines. +no-space-check=trailing-comma,dict-separator + +# Maximum number of lines in a module +max-module-lines=1000 + +# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 +# tab). +indent-string=' ' + +# Number of spaces of indent required inside a hanging or continued line. +indent-after-paren=4 + +# Expected format of line ending, e.g. empty (any line ending), LF or CRLF. +expected-line-ending-format= + + +[VARIABLES] + +# Tells whether we should check for unused import in __init__ files. +init-import=no + +# A regular expression matching the name of dummy variables (i.e. expectedly +# not used). +dummy-variables-rgx=_$|dummy|unused.*$|__* + +# List of additional names supposed to be defined in builtins. Remember that +# you should avoid to define new builtins when possible. +additional-builtins= + +# List of strings which can identify a callback function by name. A callback +# name must start or end with one of those strings. +callbacks=cb_,_cb + + +[MISCELLANEOUS] + +# List of note tags to take in consideration, separated by a comma. +notes=FIXME,XXX,TODO + + +[SPELLING] + +# Spelling dictionary name. Available dictionaries: none. To make it working +# install python-enchant package. +spelling-dict= + +# List of comma separated words that should not be checked. +spelling-ignore-words= + +# A path to a file that contains private dictionary; one word per line. +spelling-private-dict-file= + +# Tells whether to store unknown words to indicated private dictionary in +# --spelling-private-dict-file option instead of raising a message. +spelling-store-unknown-words=no + + +[TYPECHECK] + +# Tells whether missing members accessed in mixin class should be ignored. A +# mixin class is detected if its name ends with "mixin" (case insensitive). +ignore-mixin-members=yes + +# List of module names for which member attributes should not be checked +# (useful for modules/projects where namespaces are manipulated during runtime +# and thus existing member attributes cannot be deduced by static analysis. It +# supports qualified module names, as well as Unix pattern matching. +ignored-modules= + +# List of classes names for which member attributes should not be checked +# (useful for classes with attributes dynamically set). This supports can work +# with qualified names. +ignored-classes= + TokenKind + +# List of members which are set dynamically and missed by pylint inference +# system, and so shouldn't trigger E1101 when accessed. Python regular +# expressions are accepted. +generated-members= + + +[LOGGING] + +# Logging modules to check that the string format arguments are in logging +# function parameter format +logging-modules=logging + + +[CLASSES] + +# List of method names used to declare (i.e. assign) instance attributes. +defining-attr-methods=__init__,__new__,setUp + +# List of valid names for the first argument in a class method. +valid-classmethod-first-arg=cls + +# List of valid names for the first argument in a metaclass class method. +valid-metaclass-classmethod-first-arg=mcs + +# List of member names, which should be excluded from the protected access +# warning. +exclude-protected=_asdict,_fields,_replace,_source,_make + + +[IMPORTS] + +# Deprecated modules which should not be used, separated by a comma +deprecated-modules=optparse + +# Create a graph of every (i.e. internal and external) dependencies in the +# given file (report RP0402 must not be disabled) +import-graph= + +# Create a graph of external dependencies in the given file (report RP0402 must +# not be disabled) +ext-import-graph= + +# Create a graph of internal dependencies in the given file (report RP0402 must +# not be disabled) +int-import-graph= + + +[DESIGN] + +# Maximum number of arguments for function / method +max-args=5 + +# Argument names that match this expression will be ignored. Default to name +# with leading underscore +ignored-argument-names=_.*|unused.*|dummy.* + +# Maximum number of locals for function / method body +max-locals=15 + +# Maximum number of return / yield for function / method body +max-returns=6 + +# Maximum number of branch for function / method body +max-branches=12 + +# Maximum number of statements in function / method body +max-statements=50 + +# Maximum number of parents for a class (see R0901). +max-parents=7 + +# Maximum number of attributes for a class (see R0902). +max-attributes=7 + +# Minimum number of public methods for a class (see R0903). +min-public-methods=2 + +# Maximum number of public methods for a class (see R0904). +max-public-methods=20 + +# Maximum number of boolean expressions in a if statement +max-bool-expr=5 + + +[EXCEPTIONS] + +# Exceptions that will emit a warning when being caught. Defaults to +# "Exception" +overgeneral-exceptions=Exception diff --git a/tools/check-stack-usage/requirements.txt b/tools/check-stack-usage/requirements.txt new file mode 100644 index 00000000..8ed2cc36 --- /dev/null +++ b/tools/check-stack-usage/requirements.txt @@ -0,0 +1,3 @@ +flake8>=3.9.0 +pylint>=2.6.2 +mypy>=0.812 diff --git a/tools/check-stack-usage/tests/.gitignore b/tools/check-stack-usage/tests/.gitignore new file mode 100644 index 00000000..76596c79 --- /dev/null +++ b/tools/check-stack-usage/tests/.gitignore @@ -0,0 +1,3 @@ +*.o +*.su +main diff --git a/tools/check-stack-usage/tests/Makefile b/tools/check-stack-usage/tests/Makefile new file mode 100644 index 00000000..f37ebe7a --- /dev/null +++ b/tools/check-stack-usage/tests/Makefile @@ -0,0 +1,23 @@ +OBJ:=$(patsubst %.c, %.o, $(wildcard *.c)) +CFLAGS:=-fstack-usage +TARGET:=main +CC?=gcc + +all: ${TARGET} + @echo "==============================================" + @../checkStackUsage.py $< . 2>&1 | \ + grep -E '(func|foo|bar|main) ' + @echo "==============================================" + @echo "1. The output shown above must contain 4 lines" + @echo "2. 'foo' and 'bar' must both be calling 'func'" + @echo " *but with different stack sizes in each*." + @echo "3. 'main' must be using the largest 'func'" + @echo " (i.e. be going through 'bar')" + @echo "4. The reported sizes must properly accumulate" + @echo "==============================================" + +${TARGET}: ${OBJ} + ${CC} -o $@ ${CFLAGS} $^ + +clean: + rm -f ${OBJ} ${TARGET} *.su diff --git a/tools/check-stack-usage/tests/a.c b/tools/check-stack-usage/tests/a.c new file mode 100644 index 00000000..285a5659 --- /dev/null +++ b/tools/check-stack-usage/tests/a.c @@ -0,0 +1,12 @@ +#include + +static void func() +{ + char a[128]; + memset(a, ' ', sizeof(a)); +} + +void foo() +{ + func(); +} diff --git a/tools/check-stack-usage/tests/b.c b/tools/check-stack-usage/tests/b.c new file mode 100644 index 00000000..4d89ea6f --- /dev/null +++ b/tools/check-stack-usage/tests/b.c @@ -0,0 +1,12 @@ +#include + +static void func() +{ + char b[256]; + memset(b, ' ', sizeof(b)); +} + +void bar() +{ + func(); +} diff --git a/tools/check-stack-usage/tests/main.c b/tools/check-stack-usage/tests/main.c new file mode 100644 index 00000000..c043d5c2 --- /dev/null +++ b/tools/check-stack-usage/tests/main.c @@ -0,0 +1,8 @@ +extern void foo(); +extern void bar(); + +int main() +{ + foo(); + bar(); +} diff --git a/tools/memap/memap.py b/tools/memap/memap.py new file mode 100755 index 00000000..301af1e0 --- /dev/null +++ b/tools/memap/memap.py @@ -0,0 +1,979 @@ +#!/usr/bin/env python + +""" +Copyright (c) 2016-2019 ARM Limited. All rights reserved. + +SPDX-License-Identifier: Apache-2.0 + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" +from __future__ import print_function, division, absolute_import + +from abc import abstractmethod, ABCMeta +from sys import stdout, exit, argv, path +from os import sep +from os.path import (basename, dirname, join, relpath, abspath, commonprefix, + splitext) +import re +import csv +import json +from argparse import ArgumentParser +from copy import deepcopy +from collections import defaultdict +from prettytable import PrettyTable, HEADER +from jinja2 import FileSystemLoader, StrictUndefined +from jinja2.environment import Environment +from future.utils import with_metaclass + + +# Be sure that the tools directory is in the search path +ROOT = abspath(join(dirname(__file__), "..")) +path.insert(0, ROOT) + +from utils import ( + argparse_filestring_type, + argparse_lowercase_hyphen_type, + argparse_uppercase_type +) # noqa: E402 + + +class _Parser(with_metaclass(ABCMeta, object)): + """Internal interface for parsing""" + SECTIONS = ('.text', '.data', '.bss', '.heap', '.stack') + MISC_FLASH_SECTIONS = ('.interrupts', '.flash_config') + OTHER_SECTIONS = ('.interrupts_ram', '.init', '.ARM.extab', + '.ARM.exidx', '.ARM.attributes', '.eh_frame', + '.init_array', '.fini_array', '.jcr', '.stab', + '.stabstr', '.ARM.exidx', '.ARM') + + def __init__(self): + self.modules = dict() + + def module_add(self, object_name, size, section): + """ Adds a module or section to the list + + Positional arguments: + object_name - name of the entry to add + size - the size of the module being added + section - the section the module contributes to + """ + if not object_name or not size or not section: + return + + if object_name in self.modules: + self.modules[object_name].setdefault(section, 0) + self.modules[object_name][section] += size + return + + obj_split = sep + basename(object_name) + for module_path, contents in self.modules.items(): + if module_path.endswith(obj_split) or module_path == object_name: + contents.setdefault(section, 0) + contents[section] += size + return + + new_module = defaultdict(int) + new_module[section] = size + self.modules[object_name] = new_module + + def module_replace(self, old_object, new_object): + """ Replaces an object name with a new one + """ + if old_object in self.modules: + self.modules[new_object] = self.modules[old_object] + del self.modules[old_object] + + @abstractmethod + def parse_mapfile(self, mapfile): + """Parse a given file object pointing to a map file + + Positional arguments: + mapfile - an open file object that reads a map file + + return value - a dict mapping from object names to section dicts, + where a section dict maps from sections to sizes + """ + raise NotImplemented + + +class _GccParser(_Parser): + RE_OBJECT_FILE = re.compile(r'^(.+\/.+\.o(bj)?)$') + RE_LIBRARY_OBJECT = re.compile( + r'^.+' + r''.format(sep) + r'lib((.+\.a)\((.+\.o(bj)?)\))$' + ) + RE_STD_SECTION = re.compile(r'^\s+.*0x(\w{8,16})\s+0x(\w+)\s(.+)$') + RE_FILL_SECTION = re.compile(r'^\s*\*fill\*\s+0x(\w{8,16})\s+0x(\w+).*$') + RE_TRANS_FILE = re.compile(r'^(.+\/|.+\.ltrans.o(bj)?)$') + OBJECT_EXTENSIONS = (".o", ".obj") + + ALL_SECTIONS = ( + _Parser.SECTIONS + + _Parser.OTHER_SECTIONS + + _Parser.MISC_FLASH_SECTIONS + + ('unknown', 'OUTPUT') + ) + + def check_new_section(self, line): + """ Check whether a new section in a map file has been detected + + Positional arguments: + line - the line to check for a new section + + return value - A section name, if a new section was found, None + otherwise + """ + line_s = line.strip() + for i in self.ALL_SECTIONS: + if line_s.startswith(i): + return i + if line.startswith('.'): + return 'unknown' + else: + return None + + def parse_object_name(self, line): + """ Parse a path to object file + + Positional arguments: + line - the path to parse the object and module name from + + return value - an object file name + """ + if re.match(self.RE_TRANS_FILE, line): + return '[misc]' + + test_re_mbed_os_name = re.match(self.RE_OBJECT_FILE, line) + + if test_re_mbed_os_name: + object_name = test_re_mbed_os_name.group(1) + + # corner case: certain objects are provided by the GCC toolchain + if 'arm-none-eabi' in line: + return join('[lib]', 'misc', basename(object_name)) + return object_name + + else: + test_re_obj_name = re.match(self.RE_LIBRARY_OBJECT, line) + + if test_re_obj_name: + return join('[lib]', test_re_obj_name.group(2), + test_re_obj_name.group(3)) + else: + if ( + not line.startswith("LONG") and + not line.startswith("linker stubs") + ): + print("Unknown object name found in GCC map file: %s" + % line) + return '[misc]' + + def parse_section(self, line): + """ Parse data from a section of gcc map file + + examples: + 0x00004308 0x7c ./BUILD/K64F/GCC_ARM/spi_api.o + .text 0x00000608 0x198 ./BUILD/K64F/HAL_CM4.o + + Positional arguments: + line - the line to parse a section from + """ + is_fill = re.match(self.RE_FILL_SECTION, line) + if is_fill: + o_name = '[fill]' + o_size = int(is_fill.group(2), 16) + return [o_name, o_size] + + is_section = re.match(self.RE_STD_SECTION, line) + if is_section: + o_size = int(is_section.group(2), 16) + if o_size: + o_name = self.parse_object_name(is_section.group(3)) + return [o_name, o_size] + + return ["", 0] + + def parse_mapfile(self, file_desc): + """ Main logic to decode gcc map files + + Positional arguments: + file_desc - a stream object to parse as a gcc map file + """ + current_section = 'unknown' + + with file_desc as infile: + for line in infile: + if line.startswith('Linker script and memory map'): + current_section = "unknown" + break + + for line in infile: + next_section = self.check_new_section(line) + + if next_section == "OUTPUT": + break + elif next_section: + current_section = next_section + + object_name, object_size = self.parse_section(line) + self.module_add(object_name, object_size, current_section) + + common_prefix = dirname(commonprefix([ + o for o in self.modules.keys() + if ( + o.endswith(self.OBJECT_EXTENSIONS) + and not o.startswith("[lib]") + )])) + new_modules = {} + for name, stats in self.modules.items(): + if name.startswith("[lib]"): + new_modules[name] = stats + elif name.endswith(self.OBJECT_EXTENSIONS): + new_modules[relpath(name, common_prefix)] = stats + else: + new_modules[name] = stats + return new_modules + + +class _ArmccParser(_Parser): + RE = re.compile( + r'^\s+0x(\w{8})\s+0x(\w{8})\s+(\w+)\s+(\w+)\s+(\d+)\s+[*]?.+\s+(.+)$') + RE_OBJECT = re.compile(r'(.+\.(l|a|ar))\((.+\.o(bj)?)\)') + OBJECT_EXTENSIONS = (".o", ".obj") + + def parse_object_name(self, line): + """ Parse object file + + Positional arguments: + line - the line containing the object or library + """ + if line.endswith(self.OBJECT_EXTENSIONS): + return line + + else: + is_obj = re.match(self.RE_OBJECT, line) + if is_obj: + return join( + '[lib]', basename(is_obj.group(1)), is_obj.group(3) + ) + else: + print( + "Malformed input found when parsing ARMCC map: %s" % line + ) + return '[misc]' + + def parse_section(self, line): + """ Parse data from an armcc map file + + Examples of armcc map file: + Base_Addr Size Type Attr Idx E Section Name Object + 0x00000000 0x00000400 Data RO 11222 self.RESET startup_MK64F12.o + 0x00000410 0x00000008 Code RO 49364 * !!!main c_w.l(__main.o) + + Positional arguments: + line - the line to parse the section data from + """ # noqa: E501 + test_re = re.match(self.RE, line) + + if ( + test_re + and "ARM_LIB_HEAP" not in line + ): + size = int(test_re.group(2), 16) + + if test_re.group(4) == 'RO': + section = '.text' + else: + if test_re.group(3) == 'Data': + section = '.data' + elif test_re.group(3) == 'Zero': + section = '.bss' + elif test_re.group(3) == 'Code': + section = '.text' + else: + print( + "Malformed input found when parsing armcc map: %s, %r" + % (line, test_re.groups()) + ) + + return ["", 0, ""] + + # check name of object or library + object_name = self.parse_object_name( + test_re.group(6)) + + return [object_name, size, section] + + else: + return ["", 0, ""] + + def parse_mapfile(self, file_desc): + """ Main logic to decode armc5 map files + + Positional arguments: + file_desc - a file like object to parse as an armc5 map file + """ + with file_desc as infile: + # Search area to parse + for line in infile: + if line.startswith(' Base Addr Size'): + break + + # Start decoding the map file + for line in infile: + self.module_add(*self.parse_section(line)) + + common_prefix = dirname(commonprefix([ + o for o in self.modules.keys() + if ( + o.endswith(self.OBJECT_EXTENSIONS) + and o != "anon$$obj.o" + and o != "anon$$obj.obj" + and not o.startswith("[lib]") + )])) + new_modules = {} + for name, stats in self.modules.items(): + if ( + name == "anon$$obj.o" + or name == "anon$$obj.obj" + or name.startswith("[lib]") + ): + new_modules[name] = stats + elif name.endswith(self.OBJECT_EXTENSIONS): + new_modules[relpath(name, common_prefix)] = stats + else: + new_modules[name] = stats + return new_modules + + +class _IarParser(_Parser): + RE = re.compile( + r'^\s+(.+)\s+(zero|const|ro code|inited|uninit)\s' + r'+0x([\'\w]+)\s+0x(\w+)\s+(.+)\s.+$') + + RE_CMDLINE_FILE = re.compile(r'^#\s+(.+\.o(bj)?)') + RE_LIBRARY = re.compile(r'^(.+\.a)\:.+$') + RE_OBJECT_LIBRARY = re.compile(r'^\s+(.+\.o(bj)?)\s.*') + OBJECT_EXTENSIONS = (".o", ".obj") + + def __init__(self): + _Parser.__init__(self) + # Modules passed to the linker on the command line + # this is a dict because modules are looked up by their basename + self.cmd_modules = {} + + def parse_object_name(self, object_name): + """ Parse object file + + Positional arguments: + line - the line containing the object or library + """ + if object_name.endswith(self.OBJECT_EXTENSIONS): + try: + return self.cmd_modules[object_name] + except KeyError: + return object_name + else: + return '[misc]' + + def parse_section(self, line): + """ Parse data from an IAR map file + + Examples of IAR map file: + Section Kind Address Size Object + .intvec ro code 0x00000000 0x198 startup_MK64F12.o [15] + .rodata const 0x00000198 0x0 zero_init3.o [133] + .iar.init_table const 0x00008384 0x2c - Linker created - + Initializer bytes const 0x00000198 0xb2 + .data inited 0x20000000 0xd4 driverAtmelRFInterface.o [70] + .bss zero 0x20000598 0x318 RTX_Conf_CM.o [4] + .iar.dynexit uninit 0x20001448 0x204 + HEAP uninit 0x20001650 0x10000 + + Positional_arguments: + line - the line to parse section data from + """ # noqa: E501 + test_re = re.match(self.RE, line) + if test_re: + if ( + test_re.group(2) == 'const' or + test_re.group(2) == 'ro code' + ): + section = '.text' + elif (test_re.group(2) == 'zero' or + test_re.group(2) == 'uninit'): + if test_re.group(1)[0:4] == 'HEAP': + section = '.heap' + elif test_re.group(1)[0:6] == 'CSTACK': + section = '.stack' + else: + section = '.bss' # default section + + elif test_re.group(2) == 'inited': + section = '.data' + else: + print("Malformed input found when parsing IAR map: %s" % line) + return ["", 0, ""] + + # lookup object in dictionary and return module name + object_name = self.parse_object_name(test_re.group(5)) + + size = int(test_re.group(4), 16) + return [object_name, size, section] + + else: + return ["", 0, ""] + + def check_new_library(self, line): + """ + Searches for libraries and returns name. Example: + m7M_tls.a: [43] + + """ + test_address_line = re.match(self.RE_LIBRARY, line) + if test_address_line: + return test_address_line.group(1) + else: + return "" + + def check_new_object_lib(self, line): + """ + Searches for objects within a library section and returns name. + Example: + rt7M_tl.a: [44] + ABImemclr4.o 6 + ABImemcpy_unaligned.o 118 + ABImemset48.o 50 + I64DivMod.o 238 + I64DivZer.o 2 + + """ + test_address_line = re.match(self.RE_OBJECT_LIBRARY, line) + if test_address_line: + return test_address_line.group(1) + else: + return "" + + def parse_command_line(self, lines): + """Parse the files passed on the command line to the iar linker + + Positional arguments: + lines -- an iterator over the lines within a file + """ + for line in lines: + if line.startswith("*"): + break + for arg in line.split(" "): + arg = arg.rstrip(" \n") + if ( + not arg.startswith("-") + and arg.endswith(self.OBJECT_EXTENSIONS) + ): + self.cmd_modules[basename(arg)] = arg + + common_prefix = dirname(commonprefix(list(self.cmd_modules.values()))) + self.cmd_modules = {s: relpath(f, common_prefix) + for s, f in self.cmd_modules.items()} + + def parse_mapfile(self, file_desc): + """ Main logic to decode IAR map files + + Positional arguments: + file_desc - a file like object to parse as an IAR map file + """ + with file_desc as infile: + self.parse_command_line(infile) + + for line in infile: + if line.startswith(' Section '): + break + + for line in infile: + self.module_add(*self.parse_section(line)) + + if line.startswith('*** MODULE SUMMARY'): # finish section + break + + current_library = "" + for line in infile: + library = self.check_new_library(line) + + if library: + current_library = library + + object_name = self.check_new_object_lib(line) + + if object_name and current_library: + temp = join('[lib]', current_library, object_name) + self.module_replace(object_name, temp) + return self.modules + + +class MemapParser(object): + """An object that represents parsed results, parses the memory map files, + and writes out different file types of memory results + """ + + print_sections = ('.text', '.data', '.bss') + delta_sections = ('.text-delta', '.data-delta', '.bss-delta') + + # sections to print info (generic for all toolchains) + sections = _Parser.SECTIONS + misc_flash_sections = _Parser.MISC_FLASH_SECTIONS + other_sections = _Parser.OTHER_SECTIONS + + def __init__(self): + # list of all modules and their sections + # full list - doesn't change with depth + self.modules = dict() + self.old_modules = None + # short version with specific depth + self.short_modules = dict() + + # Memory report (sections + summary) + self.mem_report = [] + + # Memory summary + self.mem_summary = dict() + + # Totals of ".text", ".data" and ".bss" + self.subtotal = dict() + + # Flash no associated with a module + self.misc_flash_mem = 0 + + # Name of the toolchain, for better headings + self.tc_name = None + + def reduce_depth(self, depth): + """ + populates the short_modules attribute with a truncated module list + + (1) depth = 1: + main.o + mbed-os + + (2) depth = 2: + main.o + mbed-os/test.o + mbed-os/drivers + + """ + if depth == 0 or depth is None: + self.short_modules = deepcopy(self.modules) + else: + self.short_modules = dict() + for module_name, v in self.modules.items(): + split_name = module_name.split(sep) + if split_name[0] == '': + split_name = split_name[1:] + new_name = join(*split_name[:depth]) + self.short_modules.setdefault(new_name, defaultdict(int)) + for section_idx, value in v.items(): + self.short_modules[new_name][section_idx] += value + delta_name = section_idx + '-delta' + self.short_modules[new_name][delta_name] += value + if self.old_modules: + for module_name, v in self.old_modules.items(): + split_name = module_name.split(sep) + if split_name[0] == '': + split_name = split_name[1:] + new_name = join(*split_name[:depth]) + self.short_modules.setdefault(new_name, defaultdict(int)) + for section_idx, value in v.items(): + delta_name = section_idx + '-delta' + self.short_modules[new_name][delta_name] -= value + + export_formats = ["json", "csv-ci", "html", "table"] + + def generate_output(self, export_format, depth, file_output=None): + """ Generates summary of memory map data + + Positional arguments: + export_format - the format to dump + + Keyword arguments: + file_desc - descriptor (either stdout or file) + depth - directory depth on report + + Returns: generated string for the 'table' format, otherwise None + """ + if depth is None or depth > 0: + self.reduce_depth(depth) + self.compute_report() + try: + if file_output: + file_desc = open(file_output, 'w') + else: + file_desc = stdout + except IOError as error: + print("I/O error({0}): {1}".format(error.errno, error.strerror)) + return False + + to_call = {'json': self.generate_json, + 'html': self.generate_html, + 'csv-ci': self.generate_csv, + 'table': self.generate_table}[export_format] + output = to_call(file_desc) + + if file_desc is not stdout: + file_desc.close() + + return output + + @staticmethod + def _move_up_tree(tree, next_module): + tree.setdefault("children", []) + for child in tree["children"]: + if child["name"] == next_module: + return child + else: + new_module = {"name": next_module, "value": 0, "delta": 0} + tree["children"].append(new_module) + return new_module + + def generate_html(self, file_desc): + """Generate a json file from a memory map for D3 + + Positional arguments: + file_desc - the file to write out the final report to + """ + tree_text = {"name": ".text", "value": 0, "delta": 0} + tree_bss = {"name": ".bss", "value": 0, "delta": 0} + tree_data = {"name": ".data", "value": 0, "delta": 0} + for name, dct in self.modules.items(): + cur_text = tree_text + cur_bss = tree_bss + cur_data = tree_data + modules = name.split(sep) + while True: + try: + cur_text["value"] += dct['.text'] + cur_text["delta"] += dct['.text'] + except KeyError: + pass + try: + cur_bss["value"] += dct['.bss'] + cur_bss["delta"] += dct['.bss'] + except KeyError: + pass + try: + cur_data["value"] += dct['.data'] + cur_data["delta"] += dct['.data'] + except KeyError: + pass + if not modules: + break + next_module = modules.pop(0) + cur_text = self._move_up_tree(cur_text, next_module) + cur_data = self._move_up_tree(cur_data, next_module) + cur_bss = self._move_up_tree(cur_bss, next_module) + if self.old_modules: + for name, dct in self.old_modules.items(): + cur_text = tree_text + cur_bss = tree_bss + cur_data = tree_data + modules = name.split(sep) + while True: + try: + cur_text["delta"] -= dct['.text'] + except KeyError: + pass + try: + cur_bss["delta"] -= dct['.bss'] + except KeyError: + pass + try: + cur_data["delta"] -= dct['.data'] + except KeyError: + pass + if not modules: + break + next_module = modules.pop(0) + if not any( + cld['name'] == next_module + for cld in cur_text['children'] + ): + break + cur_text = self._move_up_tree(cur_text, next_module) + cur_data = self._move_up_tree(cur_data, next_module) + cur_bss = self._move_up_tree(cur_bss, next_module) + + tree_rom = { + "name": "ROM", + "value": tree_text["value"] + tree_data["value"], + "delta": tree_text["delta"] + tree_data["delta"], + "children": [tree_text, tree_data] + } + tree_ram = { + "name": "RAM", + "value": tree_bss["value"] + tree_data["value"], + "delta": tree_bss["delta"] + tree_data["delta"], + "children": [tree_bss, tree_data] + } + + jinja_loader = FileSystemLoader(dirname(abspath(__file__))) + jinja_environment = Environment(loader=jinja_loader, + undefined=StrictUndefined) + + template = jinja_environment.get_template("memap_flamegraph.html") + name, _ = splitext(basename(file_desc.name)) + if name.endswith("_map"): + name = name[:-4] + if self.tc_name: + name = "%s %s" % (name, self.tc_name) + data = { + "name": name, + "rom": json.dumps(tree_rom), + "ram": json.dumps(tree_ram), + } + file_desc.write(template.render(data)) + return None + + def generate_json(self, file_desc): + """Generate a json file from a memory map + + Positional arguments: + file_desc - the file to write out the final report to + """ + file_desc.write(json.dumps(self.mem_report, indent=4)) + file_desc.write('\n') + return None + + RAM_FORMAT_STR = ( + "Total Static RAM memory (data + bss): {}({:+}) bytes\n" + ) + + ROM_FORMAT_STR = ( + "Total Flash memory (text + data): {}({:+}) bytes\n" + ) + + def generate_csv(self, file_desc): + """Generate a CSV file from a memoy map + + Positional arguments: + file_desc - the file to write out the final report to + """ + writer = csv.writer(file_desc, delimiter=',', + quoting=csv.QUOTE_MINIMAL) + + module_section = [] + sizes = [] + for i in sorted(self.short_modules): + for k in self.print_sections + self.delta_sections: + module_section.append((i + k)) + sizes += [self.short_modules[i][k]] + + module_section.append('static_ram') + sizes.append(self.mem_summary['static_ram']) + + module_section.append('total_flash') + sizes.append(self.mem_summary['total_flash']) + + writer.writerow(module_section) + writer.writerow(sizes) + return None + + def generate_table(self, file_desc): + """Generate a table from a memoy map + + Returns: string of the generated table + """ + # Create table + columns = ['Module'] + columns.extend(self.print_sections) + + table = PrettyTable(columns, junction_char="|", hrules=HEADER) + table.align["Module"] = "l" + for col in self.print_sections: + table.align[col] = 'r' + + for i in list(self.print_sections): + table.align[i] = 'r' + + for i in sorted(self.short_modules): + row = [i] + + for k in self.print_sections: + row.append("{}({:+})".format( + self.short_modules[i][k], + self.short_modules[i][k + "-delta"] + )) + + table.add_row(row) + + subtotal_row = ['Subtotals'] + for k in self.print_sections: + subtotal_row.append("{}({:+})".format( + self.subtotal[k], self.subtotal[k + '-delta'])) + + table.add_row(subtotal_row) + + output = table.get_string() + output += '\n' + + output += self.RAM_FORMAT_STR.format( + self.mem_summary['static_ram'], + self.mem_summary['static_ram_delta'] + ) + output += self.ROM_FORMAT_STR.format( + self.mem_summary['total_flash'], + self.mem_summary['total_flash_delta'] + ) + + return output + + toolchains = ["ARM", "ARM_STD", "ARM_MICRO", "GCC_ARM", "IAR"] + + def compute_report(self): + """ Generates summary of memory usage for main areas + """ + self.subtotal = defaultdict(int) + + for mod in self.modules.values(): + for k in self.sections: + self.subtotal[k] += mod[k] + self.subtotal[k + '-delta'] += mod[k] + if self.old_modules: + for mod in self.old_modules.values(): + for k in self.sections: + self.subtotal[k + '-delta'] -= mod[k] + + self.mem_summary = { + 'static_ram': self.subtotal['.data'] + self.subtotal['.bss'], + 'static_ram_delta': + self.subtotal['.data-delta'] + self.subtotal['.bss-delta'], + 'total_flash': (self.subtotal['.text'] + self.subtotal['.data']), + 'total_flash_delta': + self.subtotal['.text-delta'] + self.subtotal['.data-delta'], + } + + self.mem_report = [] + if self.short_modules: + for name, sizes in sorted(self.short_modules.items()): + self.mem_report.append({ + "module": name, + "size": { + k: sizes.get(k, 0) for k in (self.print_sections + + self.delta_sections) + } + }) + + self.mem_report.append({ + 'summary': self.mem_summary + }) + + def parse(self, mapfile, toolchain): + """ Parse and decode map file depending on the toolchain + + Positional arguments: + mapfile - the file name of the memory map file + toolchain - the toolchain used to create the file + """ + self.tc_name = toolchain.title() + if toolchain in ("ARM", "ARM_STD", "ARM_MICRO", "ARMC6"): + parser = _ArmccParser + elif toolchain == "GCC_ARM": + parser = _GccParser + elif toolchain == "IAR": + parser = _IarParser + else: + return False + try: + with open(mapfile, 'r') as file_input: + self.modules = parser().parse_mapfile(file_input) + try: + with open("%s.old" % mapfile, 'r') as old_input: + self.old_modules = parser().parse_mapfile(old_input) + except IOError: + self.old_modules = None + return True + + except IOError as error: + print("I/O error({0}): {1}".format(error.errno, error.strerror)) + return False + + +def main(): + """Entry Point""" + version = '0.4.0' + + # Parser handling + parser = ArgumentParser( + description="Memory Map File Analyser for ARM mbed\nversion %s" % + version) + + parser.add_argument( + 'file', type=argparse_filestring_type, help='memory map file') + + parser.add_argument( + '-t', '--toolchain', dest='toolchain', + help='select a toolchain used to build the memory map file (%s)' % + ", ".join(MemapParser.toolchains), + required=True, + type=argparse_uppercase_type(MemapParser.toolchains, "toolchain")) + + parser.add_argument( + '-d', '--depth', dest='depth', type=int, + help='specify directory depth level to display report', required=False) + + parser.add_argument( + '-o', '--output', help='output file name', required=False) + + parser.add_argument( + '-e', '--export', dest='export', required=False, default='table', + type=argparse_lowercase_hyphen_type(MemapParser.export_formats, + 'export format'), + help="export format (examples: %s: default)" % + ", ".join(MemapParser.export_formats)) + + parser.add_argument('-v', '--version', action='version', version=version) + + # Parse/run command + if len(argv) <= 1: + parser.print_help() + exit(1) + + args = parser.parse_args() + + # Create memap object + memap = MemapParser() + + # Parse and decode a map file + if args.file and args.toolchain: + if memap.parse(args.file, args.toolchain) is False: + exit(0) + + if args.depth is None: + depth = 2 # default depth level + else: + depth = args.depth + + returned_string = None + # Write output in file + if args.output is not None: + returned_string = memap.generate_output( + args.export, + depth, + args.output + ) + else: # Write output in screen + returned_string = memap.generate_output(args.export, depth) + + if args.export == 'table' and returned_string: + print(returned_string) + + exit(0) + + +if __name__ == "__main__": + main() diff --git a/tools/memap/readme.md b/tools/memap/readme.md new file mode 100644 index 00000000..403a58c1 --- /dev/null +++ b/tools/memap/readme.md @@ -0,0 +1,85 @@ +# memap - Static Memory Map Analysis + +## Introduction + +_memap_ is a simple utility that displays static memory information required by [mbed](https://github.com/mbedmicro/mbed) applications. This information is produced by analysing the memory map file previously generated by your toolchain. + +**Note**: this tool shows static RAM usage and the total size of allocated heap and stack space defined at compile time, not the actual heap and stack usage (which may be different depending on your application). + +## Table of contents + +1. [Using memap](#using-memap) +2. [Information on memory sections](#info-mem-sections) +3. [Current support](#current-support) +4. [Known problems](#known-problems) + +## Using memap + +_memap_ is automatically invoked after an mbed build finishes successfully. It’s also possible to manually run the program with different command line options, for example: + + $> python memap.py + usage: memap.py [-h] -t TOOLCHAIN [-o OUTPUT] [-e EXPORT] [-v] file + + Memory Map File Analyser for ARM mbed version 0.3.11 + + positional arguments: + file memory map file + + optional arguments: + -h, --help show this help message and exit + -t TOOLCHAIN, --toolchain TOOLCHAIN + select a toolchain used to build the memory map file + (ARM, GCC_ARM, IAR) + -o OUTPUT, --output OUTPUT + output file name + -e EXPORT, --export EXPORT + export format (examples: 'json', 'csv-ci', 'table': + default) + -v, --version show program's version number and exit + +Result example: + + $> python memap.py GCC_ARM\myprog3.map -t GCC_ARM + + +----------------------------+-------+-------+------+ + | Module | .text | .data | .bss | + +----------------------------+-------+-------+------+ + | Fill | 170 | 0 | 2294 | + | Misc | 36282 | 2220 | 2152 | + | core/hal | 15396 | 16 | 568 | + | core/rtos | 6751 | 24 | 2662 | + | features/FEATURE_IPV4 | 96 | 0 | 48 | + | frameworks/greentea-client | 912 | 28 | 44 | + | frameworks/utest | 3079 | 0 | 732 | + | Subtotals | 62686 | 2288 | 8500 | + +----------------------------+-------+-------+------+ + Allocated Heap: 65540 bytes + Allocated Stack: 32768 bytes + Total Static RAM memory (data + bss): 10788 bytes + Total RAM memory (data + bss + heap + stack): 109096 bytes + Total Flash memory (text + data + misc): 66014 bytes + +## Information on memory sections + +The table above showed multiple memory sections. + +* `.text`: is where the code application and constants are located in Flash. +* `.data`: non-zero initialized variables; allocated in both RAM and Flash memory (variables are copied from Flash to RAM at run time) +* `.bss`: uninitialized data allocated in RAM, or variables initialized to zero. +* `Heap`: dynamic allocations in the Heap area in RAM (for example, used by `malloc`). The maximum size value may be defined at build time. +* `Stack`: dynamic allocations in the Stack area in RAM (for example, used to store local data, temporary data when branching to a subroutine or context switch information). The maximum size value may be defined at build time. + +There are other entries that require a bit of clarification: + +* Fill: represents the bytes in multiple sections (RAM and Flash) that the toolchain has filled with zeros because it requires subsequent data or code to be aligned appropriately in memory. +* Misc: usually represents helper libraries introduced by the toolchain (like `libc`), but can also represent modules that are not part of mbed. + +## Current support + +_memap_ has been tested on Windows 7, Linux and Mac OS X and works with memory map files are generated by the GCC_ARM, ARM (ARM Compiler 5) and IAR toochains. + +## Known issues and new features + +This utility is considered ‘alpha’ quality at the moment. The information generated by this utility may not be fully accurate and may vary from one toolchain to another. + +If you are experiencing problems, or would like additional features, please raise a ticket on [GitHub](https://github.com/mbedmicro/mbed/issues) and use `[memap]` in the title. diff --git a/tools/memap/utils.py b/tools/memap/utils.py new file mode 100644 index 00000000..d2aae7ff --- /dev/null +++ b/tools/memap/utils.py @@ -0,0 +1,617 @@ +""" +mbed SDK +Copyright (c) 2011-2013 ARM Limited +SPDX-License-Identifier: Apache-2.0 + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" +from __future__ import print_function, division, absolute_import +import sys +import inspect +import os +import argparse +import math +from os import listdir, remove, makedirs +from shutil import copyfile +from os.path import isdir, join, exists, split, relpath, splitext, abspath +from os.path import commonprefix, normpath, dirname +from subprocess import Popen, PIPE, STDOUT, call +from math import ceil +import json +from collections import OrderedDict +import logging +from intelhex import IntelHex +import io + +try: + unicode +except NameError: + unicode = str + +def remove_if_in(lst, thing): + if thing in lst: + lst.remove(thing) + +def compile_worker(job): + """Standard task runner used for compiling + + Positional argumets: + job - a dict containing a list of commands and the remaining arguments + to run_cmd + """ + results = [] + for command in job['commands']: + try: + _, _stderr, _rc = run_cmd(command, work_dir=job['work_dir'], + chroot=job['chroot']) + except KeyboardInterrupt: + raise ToolException + + results.append({ + 'code': _rc, + 'output': _stderr, + 'command': command + }) + + return { + 'source': job['source'], + 'object': job['object'], + 'commands': job['commands'], + 'results': results + } + +def cmd(command, check=True, verbose=False, shell=False, cwd=None): + """A wrapper to run a command as a blocking job""" + text = command if shell else ' '.join(command) + if verbose: + print(text) + return_code = call(command, shell=shell, cwd=cwd) + if check and return_code != 0: + raise Exception('ERROR %d: "%s"' % (return_code, text)) + + +def run_cmd(command, work_dir=None, chroot=None, redirect=False): + """Run a command in the foreground + + Positional arguments: + command - the command to run + + Keyword arguments: + work_dir - the working directory to run the command in + chroot - the chroot to run the command in + redirect - redirect the stderr to a pipe to be used later + """ + if chroot: + # Conventions managed by the web team for the mbed.org build system + chroot_cmd = [ + '/usr/sbin/chroot', '--userspec=33:33', chroot + ] + for element in command: + chroot_cmd += [element.replace(chroot, '')] + + logging.debug("Running command %s", ' '.join(chroot_cmd)) + command = chroot_cmd + work_dir = None + + try: + process = Popen(command, stdout=PIPE, + stderr=STDOUT if redirect else PIPE, cwd=work_dir, + universal_newlines=True) + _stdout, _stderr = process.communicate() + except OSError: + print("[OS ERROR] Command: "+(' '.join(command))) + raise + + return _stdout, _stderr, process.returncode + + +def run_cmd_ext(command): + """ A version of run command that checks if the command exists befor running + + Positional arguments: + command - the command line you are trying to invoke + """ + assert is_cmd_valid(command[0]) + process = Popen(command, stdout=PIPE, stderr=PIPE) + _stdout, _stderr = process.communicate() + return _stdout, _stderr, process.returncode + + +def is_cmd_valid(command): + """ Verify that a command exists and is executable + + Positional arguments: + command - the command to check + """ + caller = get_caller_name() + cmd_path = find_cmd_abspath(command) + if not cmd_path: + error("%s: Command '%s' can't be found" % (caller, command)) + if not is_exec(cmd_path): + error("%s: Command '%s' resolves to file '%s' which is not executable" + % (caller, command, cmd_path)) + return True + + +def is_exec(path): + """A simple check to verify that a path to an executable exists + + Positional arguments: + path - the executable + """ + return os.access(path, os.X_OK) or os.access(path+'.exe', os.X_OK) + + +def find_cmd_abspath(command): + """ Returns the absolute path to a command. + None is returned if no absolute path was found. + + Positional arguhments: + command - the command to find the path of + """ + if exists(command) or exists(command + '.exe'): + return os.path.abspath(command) + if not 'PATH' in os.environ: + raise Exception("Can't find command path for current platform ('%s')" + % sys.platform) + path_env = os.environ['PATH'] + for path in path_env.split(os.pathsep): + cmd_path = '%s/%s' % (path, command) + if exists(cmd_path) or exists(cmd_path + '.exe'): + return cmd_path + + +def mkdir(path): + """ a wrapped makedirs that only tries to create a directory if it does not + exist already + + Positional arguments: + path - the path to maybe create + """ + if not exists(path): + makedirs(path) + + +def write_json_to_file(json_data, file_name): + """ + Write json content in file + :param json_data: + :param file_name: + :return: + """ + # Create the target dir for file if necessary + test_spec_dir = os.path.dirname(file_name) + + if test_spec_dir: + mkdir(test_spec_dir) + + try: + with open(file_name, 'w') as f: + f.write(json.dumps(json_data, indent=2)) + except IOError as e: + print("[ERROR] Error writing test spec to file") + print(e) + + +def copy_file(src, dst): + """ Implement the behaviour of "shutil.copy(src, dst)" without copying the + permissions (this was causing errors with directories mounted with samba) + + Positional arguments: + src - the source of the copy operation + dst - the destination of the copy operation + """ + if isdir(dst): + _, base = split(src) + dst = join(dst, base) + copyfile(src, dst) + + +def copy_when_different(src, dst): + """ Only copy the file when it's different from its destination. + + Positional arguments: + src - the source of the copy operation + dst - the destination of the copy operation + """ + if isdir(dst): + _, base = split(src) + dst = join(dst, base) + if exists(dst): + with open(src, 'rb') as srcfd, open(dst, 'rb') as dstfd: + if srcfd.read() == dstfd.read(): + return + copyfile(src, dst) + + +def delete_dir_files(directory): + """ A function that does rm -rf + + Positional arguments: + directory - the directory to remove + """ + if not exists(directory): + return + + for element in listdir(directory): + to_remove = join(directory, element) + if not isdir(to_remove): + remove(to_remove) + + +def get_caller_name(steps=2): + """ + When called inside a function, it returns the name + of the caller of that function. + + Keyword arguments: + steps - the number of steps up the stack the calling function is + """ + return inspect.stack()[steps][3] + + +def error(msg): + """Fatal error, abort hard + + Positional arguments: + msg - the message to print before crashing + """ + print("ERROR: %s" % msg) + sys.exit(1) + + +def rel_path(path, base, dot=False): + """Relative path calculation that optionaly always starts with a dot + + Positional arguments: + path - the path to make relative + base - what to make the path relative to + + Keyword arguments: + dot - if True, the path will always start with a './' + """ + final_path = relpath(path, base) + if dot and not final_path.startswith('.'): + final_path = './' + final_path + return final_path + + +class ToolException(Exception): + """A class representing an exception throw by the tools""" + pass + +class NotSupportedException(Exception): + """A class a toolchain not supporting a particular target""" + pass + +class InvalidReleaseTargetException(Exception): + pass + +class NoValidToolchainException(Exception): + """A class representing no valid toolchain configurations found on + the system""" + pass + +def split_path(path): + """spilt a file name into it's directory name, base name, and extension + + Positional arguments: + path - the file name to split + """ + base, has_ext = split(path) + name, ext = splitext(has_ext) + return base, name, ext + + +def get_path_depth(path): + """ Given a path, return the number of directory levels present. + This roughly translates to the number of path separators (os.sep) + 1. + Ex. Given "path/to/dir", this would return 3 + Special cases: "." and "/" return 0 + + Positional arguments: + path - the path to calculate the depth of + """ + normalized_path = normpath(path) + path_depth = 0 + head, tail = split(normalized_path) + + while tail and tail != '.': + path_depth += 1 + head, tail = split(head) + + return path_depth + + +def args_error(parser, message): + """Abort with an error that was generated by the arguments to a CLI program + + Positional arguments: + parser - the ArgumentParser object that parsed the command line + message - what went wrong + """ + parser.exit(status=2, message=message+'\n') + + +def construct_enum(**enums): + """ Create your own pseudo-enums + + Keyword arguments: + * - a member of the Enum you are creating and it's value + """ + return type('Enum', (), enums) + + +def check_required_modules(required_modules, verbose=True): + """ Function checks for Python modules which should be "importable" + before test suite can be used. + @return returns True if all modules are installed already + """ + import imp + not_installed_modules = [] + for module_name in required_modules: + try: + imp.find_module(module_name) + except ImportError: + # We also test against a rare case: module is an egg file + try: + __import__(module_name) + except ImportError as exc: + not_installed_modules.append(module_name) + if verbose: + print("Error: %s" % exc) + + if verbose: + if not_installed_modules: + print("Warning: Module(s) %s not installed. Please install " + "required module(s) before using this script." + % (', '.join(not_installed_modules))) + + if not_installed_modules: + return False + else: + return True + + +def _ordered_dict_collapse_dups(pair_list): + to_ret = OrderedDict() + for key, value in pair_list: + if key in to_ret: + if isinstance(to_ret[key], dict): + to_ret[key].update(value) + elif isinstance(to_ret[key], list): + to_ret[key].extend(value) + else: + raise ValueError( + "Key %s found twice and is not mergeable" % key + ) + else: + to_ret[key] = value + return to_ret + + +def json_file_to_dict(fname): + """ Read a JSON file and return its Python representation, transforming all + the strings from Unicode to ASCII. The order of keys in the JSON file is + preserved. + + Positional arguments: + fname - the name of the file to parse + """ + try: + with io.open(fname, encoding='ascii', + errors='ignore') as file_obj: + return json.load( + file_obj, object_pairs_hook=_ordered_dict_collapse_dups + ) + except (ValueError, IOError) as e: + sys.stderr.write("Error parsing '%s': %s\n" % (fname, e)) + raise + +# Wowza, double closure +def argparse_type(casedness, prefer_hyphen=False): + def middle(lst, type_name): + def parse_type(string): + """ validate that an argument passed in (as string) is a member of + the list of possible arguments. Offer a suggestion if the case of + the string, or the hyphens/underscores do not match the expected + style of the argument. + """ + if not isinstance(string, unicode): + string = string.decode() + if prefer_hyphen: + newstring = casedness(string).replace("_", "-") + else: + newstring = casedness(string).replace("-", "_") + if string in lst: + return string + elif string not in lst and newstring in lst: + raise argparse.ArgumentTypeError( + "{0} is not a supported {1}. Did you mean {2}?".format( + string, type_name, newstring)) + else: + raise argparse.ArgumentTypeError( + "{0} is not a supported {1}. Supported {1}s are:\n{2}". + format(string, type_name, columnate(lst))) + return parse_type + return middle + +# short cuts for the argparse_type versions +argparse_uppercase_type = argparse_type(unicode.upper, False) +argparse_lowercase_type = argparse_type(unicode.lower, False) +argparse_uppercase_hyphen_type = argparse_type(unicode.upper, True) +argparse_lowercase_hyphen_type = argparse_type(unicode.lower, True) + +def argparse_force_type(case): + """ validate that an argument passed in (as string) is a member of the list + of possible arguments after converting it's case. + """ + def middle(lst, type_name): + """ The parser type generator""" + if not isinstance(lst[0], unicode): + lst = [o.decode() for o in lst] + def parse_type(string): + """ The parser type""" + if not isinstance(string, unicode): + string = string.decode() + for option in lst: + if case(string) == case(option): + return option + raise argparse.ArgumentTypeError( + "{0} is not a supported {1}. Supported {1}s are:\n{2}". + format(string, type_name, columnate(lst))) + return parse_type + return middle + +# these two types convert the case of their arguments _before_ validation +argparse_force_uppercase_type = argparse_force_type(unicode.upper) +argparse_force_lowercase_type = argparse_force_type(unicode.lower) + +def argparse_many(func): + """ An argument parser combinator that takes in an argument parser and + creates a new parser that accepts a comma separated list of the same thing. + """ + def wrap(string): + """ The actual parser""" + return [func(s) for s in string.split(",")] + return wrap + +def argparse_filestring_type(string): + """ An argument parser that verifies that a string passed in corresponds + to a file""" + if exists(string): + return string + else: + raise argparse.ArgumentTypeError( + "{0}"" does not exist in the filesystem.".format(string)) + +def argparse_profile_filestring_type(string): + """ An argument parser that verifies that a string passed in is either + absolute path or a file name (expanded to + mbed-os/tools/profiles/.json) of a existing file""" + fpath = join(dirname(__file__), "profiles/{}.json".format(string)) + + # default profiles are searched first, local ones next. + if exists(fpath): + return fpath + elif exists(string): + return string + else: + raise argparse.ArgumentTypeError( + "{0} does not exist in the filesystem.".format(string)) + +def columnate(strings, separator=", ", chars=80): + """ render a list of strings as a in a bunch of columns + + Positional arguments: + strings - the strings to columnate + + Keyword arguments; + separator - the separation between the columns + chars - the maximum with of a row + """ + col_width = max(len(s) for s in strings) + total_width = col_width + len(separator) + columns = math.floor(chars / total_width) + output = "" + for i, string in zip(range(len(strings)), strings): + append = string + if i != len(strings) - 1: + append += separator + if i % columns == columns - 1: + append += "\n" + else: + append = append.ljust(total_width) + output += append + return output + +def argparse_dir_not_parent(other): + """fail if argument provided is a parent of the specified directory""" + def parse_type(not_parent): + """The parser type""" + abs_other = abspath(other) + abs_not_parent = abspath(not_parent) + if abs_not_parent == commonprefix([abs_not_parent, abs_other]): + raise argparse.ArgumentTypeError( + "{0} may not be a parent directory of {1}".format( + not_parent, other)) + else: + return not_parent + return parse_type + +def argparse_deprecate(replacement_message): + """fail if argument is provided with deprecation warning""" + def parse_type(_): + """The parser type""" + raise argparse.ArgumentTypeError("Deprecated." + replacement_message) + return parse_type + +def print_large_string(large_string): + """ Breaks a string up into smaller pieces before print them + + This is a limitation within Windows, as detailed here: + https://bugs.python.org/issue11395 + + Positional arguments: + large_string - the large string to print + """ + string_limit = 1000 + large_string_len = len(large_string) + num_parts = int(ceil(float(large_string_len) / float(string_limit))) + for string_part in range(num_parts): + start_index = string_part * string_limit + if string_part == num_parts - 1: + sys.stdout.write(large_string[start_index:]) + else: + sys.stdout.write(large_string[start_index: + start_index + string_limit]) + sys.stdout.write("\n") + +def intelhex_offset(filename, offset): + """Load a hex or bin file at a particular offset""" + _, inteltype = splitext(filename) + ih = IntelHex() + if inteltype == ".bin": + ih.loadbin(filename, offset=offset) + elif inteltype == ".hex": + ih.loadhex(filename) + else: + raise ToolException("File %s does not have a known binary file type" + % filename) + return ih + +def integer(maybe_string, base): + """Make an integer of a number or a string""" + if isinstance(maybe_string, int): + return maybe_string + else: + return int(maybe_string, base) + +def generate_update_filename(name, target): + return "%s_update.%s" % ( + name, + getattr(target, "OUTPUT_EXT_UPDATE", "bin") + ) + +def print_end_warnings(end_warnings): + """ Print a formatted list of warnings + + Positional arguments: + end_warnings - A list of warnings (strings) to print + """ + if end_warnings: + warning_separator = "-" * 60 + print(warning_separator) + for end_warning in end_warnings: + print(end_warning) + print(warning_separator)