# Copyright (c) Kuba SzczodrzyƄski 2022-04-20. from os.path import join from SCons.Script import Builder, DefaultEnvironment env = DefaultEnvironment() board = env.BoardConfig() env.AddDefaults("realtek-ambz", "framework-realtek-amb1") # Flags env.Append( CFLAGS=[ "-std=gnu99", "-mcpu=cortex-m4", "-mthumb", "-mfloat-abi=hard", "-mfpu=fpv4-sp-d16", "-g2", "-w", "-O2", "-Wno-pointer-sign", "-fdata-sections", "-ffunction-sections", "-fmessage-length=0", "-fno-common", "-fno-short-enums", "-fomit-frame-pointer", "-fsigned-char", ], CPPDEFINES=[ # LibreTuya configuration ("LT_HAS_LWIP", "1"), ("LT_HAS_LWIP2", "1"), ("LT_HAS_FREERTOS", "1"), ("LT_HAS_MBEDTLS", "1"), ("LT_PRINTF_BROKEN", "1"), # printf does not handle %.3f properly # other options "M3", "CONFIG_PLATFORM_8711B", # LwIP options ("LWIP_TIMEVAL_PRIVATE", "0"), ("LWIP_NETIF_HOSTNAME", "1"), # to support hostname changing ("LWIP_SO_RCVBUF", "1"), # for ioctl(FIONREAD) ("ip_addr", "ip4_addr"), # LwIP 2.0.x compatibility ("ip_addr_t", "ip4_addr_t"), # LwIP 2.0.x compatibility ("IN_ADDR_T_DEFINED", "1"), ("in_addr_t", "u32_t"), ("INT_MAX", "2147483647"), # for RECV_BUFSIZE_DEFAULT ("ERRNO", "1"), # for LwIP ("vprintf", "rtl_vprintf"), "MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED", # enable PSK in mbedTLS # "MBEDTLS_DEBUG_C", ], LINKFLAGS=[ "-mcpu=cortex-m4", "-mthumb", "-mfloat-abi=hard", "-mfpu=fpv4-sp-d16", "-g", "--specs=nano.specs", "-nostartfiles", "-Wl,-Map=" + join("$BUILD_DIR", "${PROGNAME}.map"), "-Os", "-Wl,--gc-sections", "-Wl,--cref", "-Wl,--entry=Reset_Handler", "-Wl,--no-enum-size-warning", "-Wl,--no-wchar-size-warning", "-Wl,-wrap,malloc", "-Wl,-wrap,free", "-Wl,-wrap,realloc", "-Wl,-wrap,rom_psk_CalcGTK", "-Wl,-wrap,rom_psk_CalcPTK", "-Wl,-wrap,CalcMIC", "-Wl,-wrap,CheckMIC", "-Wl,-wrap,aes_80211_encrypt", "-Wl,-wrap,aes_80211_decrypt", "-Wl,-wrap,DecGTK", ], ) # Sources - from SDK project/realtek_amebaz_va0_example/GCC-RELEASE/application.mk # - "console" is disabled as it introduces build error, and is generally useless # - "utilities example" are also not really needed env.AddLibrary( name="ambz_sdk", base_dir="$SDK_DIR", srcs=[ # NOTE: a fixup is used instead, to remove default main() # "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", # "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", ], includes=[ "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", # keep PolarSSL for headers for ROM crypto functions "+", ], ) # Sources - lwIP 2.0.0 env.AddLibraryLwIP(version="2.0.0", port="amb1") # Sources - mbedTLS env.AddLibrary( name="ambz_mbedtls", base_dir="$SDK_DIR", srcs=[ # mbedTLS from SDK "+", # replace these with fixups "-", "-", ], includes=[ "+", ], ) # Sources - family fixups env.AddLibrary( name="ambz_fixups", base_dir="$FIXUPS_DIR", srcs=[ "+", "+", "+", "+", # fix non-blocking sockets (Realtek disabled this for unknown reason) "+", # rtl sdk defines S1 and S2 which conflicts here "+", ], ) # Libs & linker config env.Append( LIBPATH=[ # fmt: off join("$SDK_DIR", "component", "soc", "realtek", "8711b", "misc", "bsp", "lib", "common", "GCC"), # fmt: on ], LIBS=[ "_platform", "_wlan", "_wps", "_p2p", "_dct", "_rtlstd", "m", "c", "nosys", "gcc", "_websocket", "_http", "_mdns", ], ) env.Replace( LDSCRIPT_PATH=[ join("$LD_DIR", "$LDSCRIPT_SDK"), ], ) # Misc options env.Replace( SIZEPROGREGEXP=r"^(?:\.ram_image2\.entry|\.ram_image2\.text|\.ram_image2\.data|\.xip_image2\.text)\s+([0-9]+).*", SIZEDATAREGEXP=r"^(?:\.ram_image2\.entry|\.ram_image2\.data|\.ram_image2\.bss|\.ram_image2\.skb\.bss)\s+([0-9]+).*", SIZECHECKCMD="$SIZETOOL -A -d $SOURCES", SIZEPRINTCMD="$SIZETOOL -B -d $SOURCES", ) env.Append( BUILDERS=dict( BinToObj=Builder( action=" ".join( [ "$OBJCOPY", "-I binary", "-O elf32-littlearm", "-B arm", "$SOURCE", "$TARGET", ], ), ) ), ) # Bootloader library boot_all = board.get("build.amb_boot_all") target_boot = env.StaticLibrary( join("$BUILD_DIR", "boot_all"), env.BinToObj( join("$BUILD_DIR", "boot_all.o"), join("$BIN_DIR", boot_all), ), ) env.Prepend(LIBS=[target_boot]) # Build all libraries env.BuildLibraries() # Main firmware outputs and actions env.Replace( # linker command (dual .bin outputs) LINK="${LINK2BIN} AMBZ xip1 xip2", # default output .bin name IMG_FW="image_${FLASH_OTA1_OFFSET}.ota1.bin", # UF2OTA input list UF2OTA=[ ( "ota1", "${BUILD_DIR}/image_${FLASH_OTA1_OFFSET}.ota1.bin", "ota2", "${BUILD_DIR}/image_${FLASH_OTA2_OFFSET}.ota2.bin", ), ], )