# Copyright (c) Kuba SzczodrzyƄski 2022-07-20. from os.path import isfile, join from shutil import copyfile from platformio.platform.base import PlatformBase from platformio.platform.board import PlatformBoardConfig from SCons.Script import DefaultEnvironment, Environment env: Environment = DefaultEnvironment() platform: PlatformBase = env.PioPlatform() board: PlatformBoardConfig = env.BoardConfig() queue = env.AddLibraryQueue("realtek-ambz2") env.ConfigureFamily() COMPONENT_DIR = join("$SDK_DIR", "component") # Flags queue.AppendPublic( CCFLAGS=[ "-march=armv8-m.main+dsp", "-mthumb", "-mcmse", "-mfloat-abi=soft", "-fno-short-enums", ], CFLAGS=[ "-Wpointer-arith", "-Wno-write-strings", "-Wno-maybe-uninitialized", ], CXXFLAGS=[ "-fno-use-cxa-atexit", ], CPPDEFINES=[ # other options "__thumb2__", "CONFIG_PLATFORM_8710C", ("__ARM_ARCH_8M_MAIN__", "1"), ("CONFIG_BUILD_RAM", "1"), "V8M_STKOVF", ], CPPPATH=[ # allow including from GCC instead of RTL SDK join( platform.get_package_dir("toolchain-gccarmnoneeabi"), "arm-none-eabi", "include", ), ], LINKFLAGS=[ "-march=armv8-m.main+dsp", "-mthumb", "-mcmse", "-mfloat-abi=soft", "--specs=nano.specs", "-Wl,--use-blx", "-Wl,--undefined=gRamStartFun", "-Wl,--warn-section-align", "-Wl,-wrap,aesccmp_construct_mic_iv", "-Wl,-wrap,aesccmp_construct_mic_header1", "-Wl,-wrap,aesccmp_construct_ctr_preload", "-Wl,-wrap,rom_psk_CalcGTK", "-Wl,-wrap,rom_psk_CalcPTK", "-Wl,-wrap,aes_80211_encrypt", "-Wl,-wrap,aes_80211_decrypt", # stdlib wrappers "-Wl,-wrap,strcat", "-Wl,-wrap,strchr", "-Wl,-wrap,strcmp", "-Wl,-wrap,strncmp", "-Wl,-wrap,strcpy", "-Wl,-wrap,strncpy", "-Wl,-wrap,strlen", "-Wl,-wrap,strncat", "-Wl,-wrap,strpbrk", "-Wl,-wrap,strspn", "-Wl,-wrap,strstr", "-Wl,-wrap,strtok", "-Wl,-wrap,strxfrm", "-Wl,-wrap,strtod", "-Wl,-wrap,strtof", "-Wl,-wrap,strtold", "-Wl,-wrap,strtoll", "-Wl,-wrap,strtoul", "-Wl,-wrap,strtoull", "-Wl,-wrap,atoi", "-Wl,-wrap,atol", "-Wl,-wrap,atof", "-Wl,-wrap,memcmp", "-Wl,-wrap,memcpy", "-Wl,-wrap,memmove", "-Wl,-wrap,memset", # TODO remove this if possible "-Wl,-wrap,putc", # rt_printf wrappers are not here, as they're just changing code using #defines ], ) # Sources - from SDK project/realtek_amebaz2_v0_example/GCC-RELEASE/application.is.mk # - without "utilities - example", "bluetooth - example" and "network - app - mqtt" queue.AddLibrary( name="ambz2_sdk", base_dir=COMPONENT_DIR, srcs=[ # cmsis "+", "+", "+", # utilities "+", "+", "+", # os "+", "+", "+", "+", "+", "+", # peripheral - api "+", # peripheral - hal "+", "+", "+", "+", # peripheral - wlan # "+", # file_system - fatfs "+", "+", "+", "+", "+", "+", ], includes=[ "+<$SDK_DIR/project/realtek_amebaz2_v0_example/inc>", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", ], options=dict( CCFLAGS=[ "-Wno-int-conversion", "-Wno-unused-label", "-Wno-unused-but-set-variable", "-Wno-pointer-sign", "-Wno-parentheses", "-Wno-implicit-function-declaration", "-Wno-misleading-indentation", "-Wno-unused-value", # TODO remove this; only for example_wlan_fast_connect.c and at_cmd/* "-Wno-format-truncation", "-Wno-return-type", "-Wno-unused-variable", ], ), ) # Sources - FreeRTOS env.Replace(FREERTOS_PORT=env["FAMILY_NAME"], FREERTOS_PORT_DEFINE="REALTEK_AMBZ2") queue.AddExternalLibrary("freertos") queue.AddExternalLibrary("freertos-port") # Sources - lwIP queue.AddExternalLibrary("lwip", port="ambz2") # Sources - network utilities queue.AddLibrary( name="ambz2_net", base_dir=COMPONENT_DIR, srcs=[ # network - api "+", # network - api - wifi "+", "ARDUINO" in "ENV" and "-", # network - api - wifi - rtw_wpa_supplicant "+", "+", # network - app "+", "+", "+", "+", "+", "+", # network - coap "+", # network - http "+", "+", # network "+", # network - websocket "+", # network - mdns "+", # network - lwip - port "+", # network - ssl - ssl_ram_map "+", "+", ], includes=[ "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", ], options=dict( CCFLAGS=[ "-Wno-pointer-sign", "-Wno-unused-value", "-Wno-format", "-Wno-implicit-function-declaration", "-Wno-unused-function", "-Wno-parentheses", "-Wno-incompatible-pointer-types", "-Wno-array-bounds", "-Wno-stringop-overflow", ], ), ) # Sources - Bluetooth support queue.AddLibrary( name="ambz2_bluetooth", base_dir=join(COMPONENT_DIR, "common", "bluetooth", "realtek", "sdk"), srcs=[ "+", "+", "+", "+", "+", # "+", # "+", "+", "+", "-", ], includes=[ "+<.>", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", ], options=dict( CCFLAGS=[ "-Wno-unused-function", "-Wno-unused-variable", "-Wno-implicit-function-declaration", ], ), ) # Sources - mbedTLS queue.AddLibrary( name="ambz2_mbedtls", base_dir=join(COMPONENT_DIR, "common", "network", "ssl", "mbedtls-2.4.0"), srcs=[ "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", ], includes=[ "+", ], ) # Libs & linker config queue.AppendPublic( LIBPATH=[ # fmt: off join(COMPONENT_DIR, "soc", "realtek", "8710c", "misc", "bsp", "lib", "common", "GCC"), join(COMPONENT_DIR, "soc", "realtek", "8710c", "fwlib", "lib", "lib"), join(COMPONENT_DIR, "common", "bluetooth", "realtek", "sdk", "board", "amebaz2", "lib"), join(COMPONENT_DIR, "soc", "realtek", "8710c", "misc", "bsp", "ROM"), # fmt: on ], LIBS=[ "_soc_is", "_wlan", "_http", "_dct", "_eap", "_p2p", "_websocket", "_wps", "m", "c", "nosys", "gcc", # SCons trims the .a suffix automatically ":hal_pmc.a.a", ":btgap.a.a", ], ) # Misc options env.Replace( SIZEPROGREGEXP=r"^(?:\.ram\..*?|\.psram\.[cd].*?|\.data|\.xip[\._].*?)\s+([0-9]+).*", SIZEDATAREGEXP=r"^(?:\.ram\..*?|\.data)\s+([0-9]+).*", SIZECHECKCMD="$SIZETOOL -A -d $SOURCES", SIZEPRINTCMD="$SIZETOOL -B -d $SOURCES", ) # Bootloader - copy for linking # fmt: off bootloader_src = env.subst("${SDK_DIR}/component/soc/realtek/8710c/misc/bsp/image/bootloader.axf") bootloader_dst = env.subst("${BUILD_DIR}/bootloader.axf") # fmt: on if not isfile(bootloader_dst): copyfile(bootloader_src, bootloader_dst) # OTA2 clearing - 4096 bytes of 0xFF image_ota_clear = env.subst("${BUILD_DIR}/raw_ota_clear.bin") if not isfile(image_ota_clear): with open(image_ota_clear, "wb") as f: f.write(b"\xff" * 4096) # Build all libraries queue.BuildLibraries() # Main firmware outputs and actions image_part_table = "${BUILD_DIR}/image_part_table.${FLASH_PART_TABLE_OFFSET}.bin" image_bootloader = "${BUILD_DIR}/image_bootloader.${FLASH_BOOT_OFFSET}.bin" image_firmware_is = "${BUILD_DIR}/image_firmware_is.${FLASH_OTA1_OFFSET}.bin" env.Replace( # linker command (dual .bin outputs) LINK='${LTCHIPTOOL} link2bin ${BOARD_JSON} "" ""', # UF2OTA input list UF2OTA=[ # same OTA images for flasher and device f"{image_firmware_is},{image_firmware_is}=device:ota1,ota2;flasher:ota1,ota2", # having flashed an application image, update the bootloader and partition table (incl. keys) f"{image_bootloader},{image_bootloader}=flasher:boot,boot", f"{image_part_table},{image_part_table}=flasher:part_table,part_table", # clearing headers of the "other" OTA image (hence the indexes are swapped) f"{image_ota_clear},{image_ota_clear}=device:ota2,ota1;flasher:ota2,ota1", ], )