diff options
Diffstat (limited to 'buildroot/share/PlatformIO/scripts')
35 files changed, 1535 insertions, 0 deletions
diff --git a/buildroot/share/PlatformIO/scripts/STEVAL__F401XX.py b/buildroot/share/PlatformIO/scripts/STEVAL__F401XX.py new file mode 100644 index 0000000..580529c --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/STEVAL__F401XX.py @@ -0,0 +1,9 @@ +import os +Import("env") + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/variants/STEVAL_F401VE/ldscript.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script diff --git a/buildroot/share/PlatformIO/scripts/STM32F103RC_MEEB_3DP.py b/buildroot/share/PlatformIO/scripts/STM32F103RC_MEEB_3DP.py new file mode 100644 index 0000000..547d80a --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/STM32F103RC_MEEB_3DP.py @@ -0,0 +1,59 @@ +try: + import configparser +except ImportError: + import ConfigParser as configparser + +import os +Import("env", "projenv") +# access to global build environment +print(env) +# access to project build environment (is used source files in "src" folder) +print(projenv) + +config = configparser.ConfigParser() +config.read("platformio.ini") + +#com_port = config.get("env:STM32F103RC_meeb", "upload_port") +#print('Use the {0:s} to reboot the board to dfu mode.'.format(com_port)) + +# +# Upload actions +# + +def before_upload(source, target, env): + print("before_upload") + # do some actions + # use com_port + # + env.Execute("pwd") + +def after_upload(source, target, env): + print("after_upload") + # do some actions + # + # + env.Execute("pwd") + +print("Current build targets", map(str, BUILD_TARGETS)) + +env.AddPreAction("upload", before_upload) +env.AddPostAction("upload", after_upload) + +flash_size = 0 +vect_tab_addr = 0 + +for define in env['CPPDEFINES']: + if define[0] == "VECT_TAB_ADDR": + vect_tab_addr = define[1] + if define[0] == "STM32_FLASH_SIZE": + flash_size = define[1] + +print('Use the {0:s} address as the marlin app entry point.'.format(vect_tab_addr)) +print('Use the {0:d}KB flash version of stm32f103rct6 chip.'.format(flash_size)) + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/STM32F103RC_MEEB_3DP.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script diff --git a/buildroot/share/PlatformIO/scripts/STM32F103RC_SKR_MINI.py b/buildroot/share/PlatformIO/scripts/STM32F103RC_SKR_MINI.py new file mode 100644 index 0000000..497a035 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/STM32F103RC_SKR_MINI.py @@ -0,0 +1,20 @@ +import os +Import("env") + +STM32_FLASH_SIZE = 256 + +for define in env['CPPDEFINES']: + if define[0] == "VECT_TAB_ADDR": + env['CPPDEFINES'].remove(define) + if define[0] == "STM32_FLASH_SIZE": + STM32_FLASH_SIZE = define[1] + +# Relocate firmware from 0x08000000 to 0x08007000 +env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08007000")) + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/STM32F103RC_SKR_MINI_" + str(STM32_FLASH_SIZE) + "K.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script diff --git a/buildroot/share/PlatformIO/scripts/STM32F103RC_fysetc.py b/buildroot/share/PlatformIO/scripts/STM32F103RC_fysetc.py new file mode 100644 index 0000000..b69f625 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/STM32F103RC_fysetc.py @@ -0,0 +1,36 @@ +import os +from os.path import join +from os.path import expandvars +Import("env") + +# Relocate firmware from 0x08000000 to 0x08008000 +#for define in env['CPPDEFINES']: +# if define[0] == "VECT_TAB_ADDR": +# env['CPPDEFINES'].remove(define) +#env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08008000")) + +#custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/fysetc_stm32f103rc.ld") +#for i, flag in enumerate(env["LINKFLAGS"]): +# if "-Wl,-T" in flag: +# env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script +# elif flag == "-T": +# env["LINKFLAGS"][i + 1] = custom_ld_script + +# Custom HEX from ELF +env.AddPostAction( + join("$BUILD_DIR","${PROGNAME}.elf"), + env.VerboseAction(" ".join([ + "$OBJCOPY", "-O ihex", "$TARGET", # TARGET=.pio/build/fysetc_STM32F1/firmware.elf + "\"" + join("$BUILD_DIR","${PROGNAME}.hex") + "\"", # Note: $BUILD_DIR is a full path + ]), "Building $TARGET")) + +# In-line command with arguments +UPLOAD_TOOL="stm32flash" +platform = env.PioPlatform() +if platform.get_package_dir("tool-stm32duino") != None: + UPLOAD_TOOL=expandvars("\"" + join(platform.get_package_dir("tool-stm32duino"),"stm32flash","stm32flash") + "\"") + +env.Replace( + UPLOADER=UPLOAD_TOOL, + UPLOADCMD=expandvars(UPLOAD_TOOL + " -v -i rts,-dtr,dtr -R -b 115200 -g 0x8000000 -w \"" + join("$BUILD_DIR","${PROGNAME}.hex")+"\"" + " $UPLOAD_PORT") +) diff --git a/buildroot/share/PlatformIO/scripts/STM32F103RE_SKR_E3_DIP.py b/buildroot/share/PlatformIO/scripts/STM32F103RE_SKR_E3_DIP.py new file mode 100644 index 0000000..ecdd57f --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/STM32F103RE_SKR_E3_DIP.py @@ -0,0 +1,16 @@ +import os +Import("env") + +for define in env['CPPDEFINES']: + if define[0] == "VECT_TAB_ADDR": + env['CPPDEFINES'].remove(define) + +# Relocate firmware from 0x08000000 to 0x08007000 +env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08007000")) + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/STM32F103RE_SKR_E3_DIP.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script diff --git a/buildroot/share/PlatformIO/scripts/STM32F103VE_longer.py b/buildroot/share/PlatformIO/scripts/STM32F103VE_longer.py new file mode 100644 index 0000000..ece47ed --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/STM32F103VE_longer.py @@ -0,0 +1,33 @@ +import os +Import("env") + +# Relocate firmware from 0x08000000 to 0x08010000 +for define in env['CPPDEFINES']: + if define[0] == "VECT_TAB_ADDR": + env['CPPDEFINES'].remove(define) +env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08010000")) + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/STM32F103VE_longer.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script + + +# Rename ${PROGNAME}.bin and save it as 'project.bin' (No encryption on the Longer3D) +def encrypt(source, target, env): + firmware = open(target[0].path, "rb") + marlin_alfa = open(target[0].dir.path +'/project.bin', "wb") + length = os.path.getsize(target[0].path) + position = 0 + try: + while position < length: + byte = firmware.read(1) + marlin_alfa.write(byte) + position += 1 + finally: + firmware.close() + marlin_alfa.close() + +env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt); diff --git a/buildroot/share/PlatformIO/scripts/STM32F1_create_variant.py b/buildroot/share/PlatformIO/scripts/STM32F1_create_variant.py new file mode 100644 index 0000000..4849f59 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/STM32F1_create_variant.py @@ -0,0 +1,34 @@ +import os,shutil +from SCons.Script import DefaultEnvironment +from platformio import util + +def copytree(src, dst, symlinks=False, ignore=None): + for item in os.listdir(src): + s = os.path.join(src, item) + d = os.path.join(dst, item) + if os.path.isdir(s): + shutil.copytree(s, d, symlinks, ignore) + else: + shutil.copy2(s, d) + +env = DefaultEnvironment() +platform = env.PioPlatform() +board = env.BoardConfig() + +FRAMEWORK_DIR = platform.get_package_dir("framework-arduinoststm32-maple") +assert os.path.isdir(FRAMEWORK_DIR) +assert os.path.isdir("buildroot/share/PlatformIO/variants") + +variant = board.get("build.variant") +variant_dir = os.path.join(FRAMEWORK_DIR, "STM32F1", "variants", variant) + +source_dir = os.path.join("buildroot/share/PlatformIO/variants", variant) +assert os.path.isdir(source_dir) + +if os.path.isdir(variant_dir): + shutil.rmtree(variant_dir) + +if not os.path.isdir(variant_dir): + os.mkdir(variant_dir) + +copytree(source_dir, variant_dir) diff --git a/buildroot/share/PlatformIO/scripts/add_nanolib.py b/buildroot/share/PlatformIO/scripts/add_nanolib.py new file mode 100644 index 0000000..102b4b9 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/add_nanolib.py @@ -0,0 +1,2 @@ +Import("env") +env.Append(LINKFLAGS=["--specs=nano.specs"]) diff --git a/buildroot/share/PlatformIO/scripts/anet_et4_openblt.py b/buildroot/share/PlatformIO/scripts/anet_et4_openblt.py new file mode 100644 index 0000000..2911a28 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/anet_et4_openblt.py @@ -0,0 +1,14 @@ +# Generate the firmware as OpenBLT needs + +import os,sys +from os.path import join + +Import("env") + +env.AddPostAction( + "$BUILD_DIR/${PROGNAME}.elf", + env.VerboseAction(" ".join([ + "$OBJCOPY", "-O", "srec", + "\"$BUILD_DIR/${PROGNAME}.elf\"", "\"$BUILD_DIR/${PROGNAME}.srec\"" + ]), "Building " + join("$BUILD_DIR","${PROGNAME}.srec")) +) diff --git a/buildroot/share/PlatformIO/scripts/chitu_crypt.py b/buildroot/share/PlatformIO/scripts/chitu_crypt.py new file mode 100644 index 0000000..aa67587 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/chitu_crypt.py @@ -0,0 +1,127 @@ +Import("env") +import os +import random +import struct +import uuid + +# Relocate firmware from 0x08000000 to 0x08008800 +env['CPPDEFINES'].remove(("VECT_TAB_ADDR", "0x8000000")) +env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08008800")) + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/chitu_f103.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script + + +def calculate_crc(contents, seed): + accumulating_xor_value = seed; + + for i in range(0, len(contents), 4): + value = struct.unpack('<I', contents[ i : i + 4])[0] + accumulating_xor_value = accumulating_xor_value ^ value + return accumulating_xor_value + +def xor_block(r0, r1, block_number, block_size, file_key): + # This is the loop counter + loop_counter = 0x0 + + # This is the key length + key_length = 0x18 + + # This is an initial seed + xor_seed = 0x4BAD + + # This is the block counter + block_number = xor_seed * block_number + + #load the xor key from the file + r7 = file_key + + for loop_counter in range(0, block_size): + # meant to make sure different bits of the key are used. + xor_seed = int(loop_counter/key_length) + + # IP is a scratch register / R12 + ip = loop_counter - (key_length * xor_seed) + + # xor_seed = (loop_counter * loop_counter) + block_number + xor_seed = (loop_counter * loop_counter) + block_number + + # shift the xor_seed left by the bits in IP. + xor_seed = xor_seed >> ip + + # load a byte into IP + ip = r0[loop_counter] + + # XOR the seed with r7 + xor_seed = xor_seed ^ r7 + + # and then with IP + xor_seed = xor_seed ^ ip + + #Now store the byte back + r1[loop_counter] = xor_seed & 0xFF + + #increment the loop_counter + loop_counter = loop_counter + 1 + + +def encrypt_file(input, output_file, file_length): + input_file = bytearray(input.read()) + block_size = 0x800 + key_length = 0x18 + + uid_value = uuid.uuid4() + file_key = int(uid_value.hex[0:8], 16) + + xor_crc = 0xEF3D4323; + + # the input file is exepcted to be in chunks of 0x800 + # so round the size + while len(input_file) % block_size != 0: + input_file.extend(b'0x0') + + # write the file header + output_file.write(struct.pack(">I", 0x443D2D3F)) + # encrypt the contents using a known file header key + + # write the file_key + output_file.write(struct.pack("<I", file_key)) + + #TODO - how to enforce that the firmware aligns to block boundaries? + block_count = int(len(input_file) / block_size) + print ("Block Count is ", block_count) + for block_number in range(0, block_count): + block_offset = (block_number * block_size) + block_end = block_offset + block_size + block_array = bytearray(input_file[block_offset: block_end]) + xor_block(block_array, block_array, block_number, block_size, file_key) + for n in range (0, block_size): + input_file[block_offset + n] = block_array[n] + + # update the expected CRC value. + xor_crc = calculate_crc(block_array, xor_crc) + + # write CRC + output_file.write(struct.pack("<I", xor_crc)) + + # finally, append the encrypted results. + output_file.write(input_file) + return + + +# Encrypt ${PROGNAME}.bin and save it as 'update.cbd' +def encrypt(source, target, env): + firmware = open(target[0].path, "rb") + update = open(target[0].dir.path +'/update.cbd', "wb") + length = os.path.getsize(target[0].path) + + encrypt_file(firmware, update, length) + + firmware.close() + update.close() + +env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt); diff --git a/buildroot/share/PlatformIO/scripts/common-cxxflags.py b/buildroot/share/PlatformIO/scripts/common-cxxflags.py new file mode 100644 index 0000000..a0a3b45 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/common-cxxflags.py @@ -0,0 +1,22 @@ +# +# common-cxxflags.py +# Convenience script to apply customizations to CPP flags +# +Import("env") +env.Append(CXXFLAGS=[ + "-Wno-register" + #"-Wno-incompatible-pointer-types", + #"-Wno-unused-const-variable", + #"-Wno-maybe-uninitialized", + #"-Wno-sign-compare" +]) + +# Useful for JTAG debugging +# +# It will separe release and debug build folders. +# It useful when we need keep two live versions: one debug, for debugging, +# other release, for flashing. +# Without this, PIO will recompile everything twice for any small change. +# +if env.GetBuildType() == "debug": + env['BUILD_DIR'] = '$PROJECT_BUILD_DIR/$PIOENV/debug' diff --git a/buildroot/share/PlatformIO/scripts/common-dependencies-post.py b/buildroot/share/PlatformIO/scripts/common-dependencies-post.py new file mode 100644 index 0000000..2b1b948 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/common-dependencies-post.py @@ -0,0 +1,16 @@ +# +# common-dependencies-post.py +# Convenience script to add build flags for Marlin Enabled Features +# + +Import("env") +Import("projenv") + +def apply_board_build_flags(): + if not 'BOARD_CUSTOM_BUILD_FLAGS' in env['MARLIN_FEATURES']: + return + projenv.Append(CCFLAGS=env['MARLIN_FEATURES']['BOARD_CUSTOM_BUILD_FLAGS'].split()) + +# We need to add the board build flags in a post script +# so the platform build script doesn't overwrite the custom CCFLAGS +apply_board_build_flags() diff --git a/buildroot/share/PlatformIO/scripts/common-dependencies.h b/buildroot/share/PlatformIO/scripts/common-dependencies.h new file mode 100644 index 0000000..ed2f46a --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/common-dependencies.h @@ -0,0 +1,120 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * 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 3 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, see <https://www.gnu.org/licenses/>. + * + */ + +/** + * The purpose of this file is just include Marlin Configuration files, + * to discover which FEATURES are enabled, without any HAL include. + * Used by common-dependencies.py + */ + +#define NUM_SERIAL 1 // Normally provided by HAL/HAL.h + +#include "../../../../Marlin/src/inc/MarlinConfig.h" + +// +// Conditionals only used for [features] +// +#if ENABLED(SR_LCD_3W_NL) + // Feature checks for SR_LCD_3W_NL +#elif EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008) + #define USES_LIQUIDTWI2 +#elif ENABLED(LCD_I2C_TYPE_PCA8574) + #define USES_LIQUIDCRYSTAL_I2C +#elif ANY(HAS_MARLINUI_HD44780, LCD_I2C_TYPE_PCF8575, SR_LCD_2W_NL , LCM1602) + #define USES_LIQUIDCRYSTAL +#endif + +#if SAVED_POSITIONS + #define HAS_SAVED_POSITIONS +#endif + +#if ENABLED(HOST_PROMPT_SUPPORT) && DISABLED(EMERGENCY_PARSER) + #define HAS_GCODE_M876 +#endif + +#if EXTRUDERS + #define HAS_EXTRUDERS +#endif + +#if ENABLED(DUET_SMART_EFFECTOR) && PIN_EXISTS(SMART_EFFECTOR_MOD) + #define HAS_SMART_EFF_MOD +#endif + +#if HAS_LCD_MENU + #if ENABLED(BACKLASH_GCODE) + #define HAS_MENU_BACKLASH + #endif + #if ENABLED(LEVEL_BED_CORNERS) + #define HAS_MENU_BED_CORNERS + #endif + #if ENABLED(CANCEL_OBJECTS) + #define HAS_MENU_CANCELOBJECT + #endif + #if ENABLED(CUSTOM_USER_MENUS) + #define HAS_MENU_CUSTOM + #endif + #if EITHER(DELTA_CALIBRATION_MENU, DELTA_AUTO_CALIBRATION) + #define HAS_MENU_DELTA_CALIBRATE + #endif + #if EITHER(LED_CONTROL_MENU, CASE_LIGHT_MENU) + #define HAS_MENU_LED + #endif + #if ENABLED(ADVANCED_PAUSE_FEATURE) + #define HAS_MENU_FILAMENT + #endif + #if ENABLED(SDSUPPORT) + #define HAS_MENU_MEDIA + #endif + #if ENABLED(MIXING_EXTRUDER) + #define HAS_MENU_MIXER + #endif + #if ENABLED(POWER_LOSS_RECOVERY) + #define HAS_MENU_JOB_RECOVERY + #endif + #if HAS_POWER_MONITOR + #define HAS_MENU_POWER_MONITOR + #endif + #if HAS_CUTTER + #define HAS_MENU_CUTTER + #endif + #if HAS_TEMPERATURE + #define HAS_MENU_TEMPERATURE + #endif + #if ENABLED(MMU2_MENUS) + #define HAS_MENU_MMU2 + #endif + #if ENABLED(PASSWORD_FEATURE) + #define HAS_MENU_PASSWORD + #endif + #if HAS_TRINAMIC_CONFIG + #define HAS_MENU_TMC + #endif + #if ENABLED(TOUCH_SCREEN_CALIBRATION) + #define HAS_MENU_TOUCH_SCREEN + #endif + #if ENABLED(ASSISTED_TRAMMING_WIZARD) + #define HAS_MENU_TRAMMING + #endif + #if ENABLED(AUTO_BED_LEVELING_UBL) + #define HAS_MENU_UBL + #endif +#endif diff --git a/buildroot/share/PlatformIO/scripts/common-dependencies.py b/buildroot/share/PlatformIO/scripts/common-dependencies.py new file mode 100644 index 0000000..4500f52 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/common-dependencies.py @@ -0,0 +1,335 @@ +# +# common-dependencies.py +# Convenience script to check dependencies and add libs and sources for Marlin Enabled Features +# +import subprocess +import os +import re +try: + import configparser +except ImportError: + import ConfigParser as configparser +try: + # PIO < 4.4 + from platformio.managers.package import PackageManager +except ImportError: + # PIO >= 4.4 + from platformio.package.meta import PackageSpec as PackageManager + +PIO_VERSION_MIN = (5, 0, 3) +try: + from platformio import VERSION as PIO_VERSION + weights = (1000, 100, 1) + version_min = sum([x[0] * float(re.sub(r'[^0-9]', '.', str(x[1]))) for x in zip(weights, PIO_VERSION_MIN)]) + version_cur = sum([x[0] * float(re.sub(r'[^0-9]', '.', str(x[1]))) for x in zip(weights, PIO_VERSION)]) + if version_cur < version_min: + print() + print("**************************************************") + print("****** An update to PlatformIO is ******") + print("****** required to build Marlin Firmware. ******") + print("****** ******") + print("****** Minimum version: ", PIO_VERSION_MIN, " ******") + print("****** Current Version: ", PIO_VERSION, " ******") + print("****** ******") + print("****** Update PlatformIO and try again. ******") + print("**************************************************") + print() + exit(1) +except SystemExit: + exit(1) +except: + print("Can't detect PlatformIO Version") + +Import("env") + +#print(env.Dump()) + +try: + verbose = int(env.GetProjectOption('custom_verbose')) +except: + verbose = 0 + +def blab(str): + if verbose: + print(str) + +def parse_pkg_uri(spec): + if PackageManager.__name__ == 'PackageSpec': + return PackageManager(spec).name + else: + name, _, _ = PackageManager.parse_pkg_uri(spec) + return name + +FEATURE_CONFIG = {} + +def add_to_feat_cnf(feature, flines): + + try: + feat = FEATURE_CONFIG[feature] + except: + FEATURE_CONFIG[feature] = {} + + # Get a reference to the FEATURE_CONFIG under construction + feat = FEATURE_CONFIG[feature] + + # Split up passed lines on commas or newlines and iterate + # Add common options to the features config under construction + # For lib_deps replace a previous instance of the same library + atoms = re.sub(r',\\s*', '\n', flines).strip().split('\n') + for line in atoms: + parts = line.split('=') + name = parts.pop(0) + if name in ['build_flags', 'extra_scripts', 'src_filter', 'lib_ignore']: + feat[name] = '='.join(parts) + else: + for dep in line.split(','): + lib_name = re.sub(r'@([~^]|[<>]=?)?[\d.]+', '', dep.strip()).split('=').pop(0) + lib_re = re.compile('(?!^' + lib_name + '\\b)') + feat['lib_deps'] = list(filter(lib_re.match, feat['lib_deps'])) + [dep] + +def load_config(): + config = configparser.ConfigParser() + config.read("platformio.ini") + items = config.items('features') + for key in items: + feature = key[0].upper() + if not feature in FEATURE_CONFIG: + FEATURE_CONFIG[feature] = { 'lib_deps': [] } + add_to_feat_cnf(feature, key[1]) + + # Add options matching custom_marlin.MY_OPTION to the pile + all_opts = env.GetProjectOptions() + for n in all_opts: + mat = re.match(r'custom_marlin\.(.+)', n[0]) + if mat: + try: + val = env.GetProjectOption(n[0]) + except: + val = None + if val: + add_to_feat_cnf(mat.group(1).upper(), val) + +def get_all_known_libs(): + known_libs = [] + for feature in FEATURE_CONFIG: + feat = FEATURE_CONFIG[feature] + if not 'lib_deps' in feat: + continue + for dep in feat['lib_deps']: + name = parse_pkg_uri(dep) + known_libs.append(name) + return known_libs + +def get_all_env_libs(): + env_libs = [] + lib_deps = env.GetProjectOption('lib_deps') + for dep in lib_deps: + name = parse_pkg_uri(dep) + env_libs.append(name) + return env_libs + +def set_env_field(field, value): + proj = env.GetProjectConfig() + proj.set("env:" + env['PIOENV'], field, value) + +# All unused libs should be ignored so that if a library +# exists in .pio/lib_deps it will not break compilation. +def force_ignore_unused_libs(): + env_libs = get_all_env_libs() + known_libs = get_all_known_libs() + diff = (list(set(known_libs) - set(env_libs))) + lib_ignore = env.GetProjectOption('lib_ignore') + diff + blab("Ignore libraries: %s" % lib_ignore) + set_env_field('lib_ignore', lib_ignore) + +def apply_features_config(): + load_config() + for feature in FEATURE_CONFIG: + if not env.MarlinFeatureIsEnabled(feature): + continue + + feat = FEATURE_CONFIG[feature] + + if 'lib_deps' in feat and len(feat['lib_deps']): + blab("Adding lib_deps for %s... " % feature) + + # feat to add + deps_to_add = {} + for dep in feat['lib_deps']: + name = parse_pkg_uri(dep) + deps_to_add[name] = dep + + # Does the env already have the dependency? + deps = env.GetProjectOption('lib_deps') + for dep in deps: + name = parse_pkg_uri(dep) + if name in deps_to_add: + del deps_to_add[name] + + # Are there any libraries that should be ignored? + lib_ignore = env.GetProjectOption('lib_ignore') + for dep in deps: + name = parse_pkg_uri(dep) + if name in deps_to_add: + del deps_to_add[name] + + # Is there anything left? + if len(deps_to_add) > 0: + # Only add the missing dependencies + set_env_field('lib_deps', deps + list(deps_to_add.values())) + + if 'build_flags' in feat: + f = feat['build_flags'] + blab("Adding build_flags for %s: %s" % (feature, f)) + new_flags = env.GetProjectOption('build_flags') + [ f ] + env.Replace(BUILD_FLAGS=new_flags) + + if 'extra_scripts' in feat: + blab("Running extra_scripts for %s... " % feature) + env.SConscript(feat['extra_scripts'], exports="env") + + if 'src_filter' in feat: + blab("Adding src_filter for %s... " % feature) + src_filter = ' '.join(env.GetProjectOption('src_filter')) + # first we need to remove the references to the same folder + my_srcs = re.findall(r'[+-](<.*?>)', feat['src_filter']) + cur_srcs = re.findall(r'[+-](<.*?>)', src_filter) + for d in my_srcs: + if d in cur_srcs: + src_filter = re.sub(r'[+-]' + d, '', src_filter) + + src_filter = feat['src_filter'] + ' ' + src_filter + set_env_field('src_filter', [src_filter]) + env.Replace(SRC_FILTER=src_filter) + + if 'lib_ignore' in feat: + blab("Adding lib_ignore for %s... " % feature) + lib_ignore = env.GetProjectOption('lib_ignore') + [feat['lib_ignore']] + set_env_field('lib_ignore', lib_ignore) + +# +# Find a compiler, considering the OS +# +ENV_BUILD_PATH = os.path.join(env.Dictionary('PROJECT_BUILD_DIR'), env['PIOENV']) +GCC_PATH_CACHE = os.path.join(ENV_BUILD_PATH, ".gcc_path") +def search_compiler(): + try: + filepath = env.GetProjectOption('custom_gcc') + blab("Getting compiler from env") + return filepath + except: + pass + + if os.path.exists(GCC_PATH_CACHE): + blab("Getting g++ path from cache") + with open(GCC_PATH_CACHE, 'r') as f: + return f.read() + + # Find the current platform compiler by searching the $PATH + # which will be in a platformio toolchain bin folder + path_regex = re.escape(env['PROJECT_PACKAGES_DIR']) + gcc = "g++" + if env['PLATFORM'] == 'win32': + path_separator = ';' + path_regex += r'.*\\bin' + gcc += ".exe" + else: + path_separator = ':' + path_regex += r'/.+/bin' + + # Search for the compiler + for pathdir in env['ENV']['PATH'].split(path_separator): + if not re.search(path_regex, pathdir, re.IGNORECASE): + continue + for filepath in os.listdir(pathdir): + if not filepath.endswith(gcc): + continue + # Use entire path to not rely on env PATH + filepath = os.path.sep.join([pathdir, filepath]) + # Cache the g++ path to no search always + if os.path.exists(ENV_BUILD_PATH): + blab("Caching g++ for current env") + with open(GCC_PATH_CACHE, 'w+') as f: + f.write(filepath) + + return filepath + + filepath = env.get('CXX') + blab("Couldn't find a compiler! Fallback to %s" % filepath) + return filepath + +# +# Use the compiler to get a list of all enabled features +# +def load_marlin_features(): + if 'MARLIN_FEATURES' in env: + return + + # Process defines + build_flags = env.get('BUILD_FLAGS') + build_flags = env.ParseFlagsExtended(build_flags) + + cxx = search_compiler() + cmd = ['"' + cxx + '"'] + + # Build flags from board.json + #if 'BOARD' in env: + # cmd += [env.BoardConfig().get("build.extra_flags")] + for s in build_flags['CPPDEFINES']: + if isinstance(s, tuple): + cmd += ['-D' + s[0] + '=' + str(s[1])] + else: + cmd += ['-D' + s] + + cmd += ['-D__MARLIN_DEPS__ -w -dM -E -x c++ buildroot/share/PlatformIO/scripts/common-dependencies.h'] + cmd = ' '.join(cmd) + blab(cmd) + define_list = subprocess.check_output(cmd, shell=True).splitlines() + marlin_features = {} + for define in define_list: + feature = define[8:].strip().decode().split(' ') + feature, definition = feature[0], ' '.join(feature[1:]) + marlin_features[feature] = definition + env['MARLIN_FEATURES'] = marlin_features + +# +# Return True if a matching feature is enabled +# +def MarlinFeatureIsEnabled(env, feature): + load_marlin_features() + r = re.compile('^' + feature + '$') + found = list(filter(r.match, env['MARLIN_FEATURES'])) + + # Defines could still be 'false' or '0', so check + some_on = False + if len(found): + for f in found: + val = env['MARLIN_FEATURES'][f] + if val in [ '', '1', 'true' ]: + some_on = True + elif val in env['MARLIN_FEATURES']: + some_on = env.MarlinFeatureIsEnabled(val) + + return some_on + +# +# Check for Configfiles in two common incorrect places +# +def check_configfile_locations(): + for p in [ env['PROJECT_DIR'], os.path.join(env['PROJECT_DIR'], "config") ]: + for f in [ "Configuration.h", "Configuration_adv.h" ]: + if os.path.isfile(os.path.join(p, f)): + err = 'ERROR: Config files found in directory ' + str(p) + '. Please move them into the Marlin subdirectory.' + raise SystemExit(err) + +# +# Add a method for other PIO scripts to query enabled features +# +env.AddMethod(MarlinFeatureIsEnabled) + +# +# Add dependencies for enabled Marlin features +# +check_configfile_locations() +apply_features_config() +force_ignore_unused_libs() diff --git a/buildroot/share/PlatformIO/scripts/copy_marlin_variant_to_framework.py b/buildroot/share/PlatformIO/scripts/copy_marlin_variant_to_framework.py new file mode 100644 index 0000000..f7d3f0d --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/copy_marlin_variant_to_framework.py @@ -0,0 +1,58 @@ +import os,shutil +from SCons.Script import DefaultEnvironment +from platformio import util +try: + # PIO < 4.4 + from platformio.managers.package import PackageManager +except ImportError: + # PIO >= 4.4 + from platformio.package.meta import PackageSpec as PackageManager + +def parse_pkg_uri(spec): + if PackageManager.__name__ == 'PackageSpec': + return PackageManager(spec).name + else: + name, _, _ = PackageManager.parse_pkg_uri(spec) + return name + +def copytree(src, dst, symlinks=False, ignore=None): + for item in os.listdir(src): + s = os.path.join(src, item) + d = os.path.join(dst, item) + if os.path.isdir(s): + shutil.copytree(s, d, symlinks, ignore) + else: + shutil.copy2(s, d) + +env = DefaultEnvironment() +platform = env.PioPlatform() +board = env.BoardConfig() +variant = board.get("build.variant") + +platform_packages = env.GetProjectOption('platform_packages') +# if there's no framework defined, take it from the class name of platform +framewords = { + "Ststm32Platform": "framework-arduinoststm32", + "AtmelavrPlatform": "framework-arduino-avr" +} +if len(platform_packages) == 0: + platform_name = framewords[platform.__class__.__name__] +else: + platform_name = parse_pkg_uri(platform_packages[0]) + +FRAMEWORK_DIR = platform.get_package_dir(platform_name) +assert os.path.isdir(FRAMEWORK_DIR) +assert os.path.isdir("buildroot/share/PlatformIO/variants") + +variant_dir = os.path.join(FRAMEWORK_DIR, "variants", variant) + +source_dir = os.path.join("buildroot/share/PlatformIO/variants", variant) +assert os.path.isdir(source_dir) + +if os.path.isdir(variant_dir): + shutil.rmtree(variant_dir) + +if not os.path.isdir(variant_dir): + os.mkdir(variant_dir) + +copytree(source_dir, variant_dir) diff --git a/buildroot/share/PlatformIO/scripts/creality.py b/buildroot/share/PlatformIO/scripts/creality.py new file mode 100644 index 0000000..b9d7d70 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/creality.py @@ -0,0 +1,17 @@ +import os +Import("env") + +# Relocate firmware from 0x08000000 to 0x08007000 +for define in env['CPPDEFINES']: + if define[0] == "VECT_TAB_ADDR": + env['CPPDEFINES'].remove(define) + +env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08007000")) + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/creality.ld") + +for i, flag in enumerate(env['LINKFLAGS']): + if "-Wl,-T" in flag: + env['LINKFLAGS'][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env['LINKFLAGS'][i + 1] = custom_ld_script diff --git a/buildroot/share/PlatformIO/scripts/download_mks_assets.py b/buildroot/share/PlatformIO/scripts/download_mks_assets.py new file mode 100644 index 0000000..ae2ce46 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/download_mks_assets.py @@ -0,0 +1,46 @@ +Import("env") +import os +import requests +import zipfile +import tempfile +import shutil + +url = "https://github.com/makerbase-mks/Mks-Robin-Nano-Marlin2.0-Firmware/archive/master.zip" +zip_path = os.path.join(env.Dictionary("PROJECT_LIBDEPS_DIR"), "mks-assets.zip") +assets_path = os.path.join(env.Dictionary("PROJECT_BUILD_DIR"), env.Dictionary("PIOENV"), "assets") + +def download_mks_assets(): + print("Downloading MKS Assets") + r = requests.get(url, stream=True) + # the user may have a very clean workspace, + # so create the PROJECT_LIBDEPS_DIR directory if not exits + if os.path.exists(env.Dictionary("PROJECT_LIBDEPS_DIR")) == False: + os.mkdir(env.Dictionary("PROJECT_LIBDEPS_DIR")) + with open(zip_path, 'wb') as fd: + for chunk in r.iter_content(chunk_size=128): + fd.write(chunk) + +def copy_mks_assets(): + print("Copying MKS Assets") + output_path = tempfile.mkdtemp() + zip_obj = zipfile.ZipFile(zip_path, 'r') + zip_obj.extractall(output_path) + zip_obj.close() + if os.path.exists(assets_path) == True and os.path.isdir(assets_path) == False: + os.unlink(assets_path) + if os.path.exists(assets_path) == False: + os.mkdir(assets_path) + base_path = '' + for filename in os.listdir(output_path): + base_path = filename + for filename in os.listdir(os.path.join(output_path, base_path, 'Firmware', 'mks_font')): + shutil.copy(os.path.join(output_path, base_path, 'Firmware', 'mks_font', filename), assets_path) + for filename in os.listdir(os.path.join(output_path, base_path, 'Firmware', 'mks_pic')): + shutil.copy(os.path.join(output_path, base_path, 'Firmware', 'mks_pic', filename), assets_path) + shutil.rmtree(output_path, ignore_errors=True) + +if os.path.exists(zip_path) == False: + download_mks_assets() + +if os.path.exists(assets_path) == False: + copy_mks_assets() diff --git a/buildroot/share/PlatformIO/scripts/fly_mini.py b/buildroot/share/PlatformIO/scripts/fly_mini.py new file mode 100644 index 0000000..34d1329 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/fly_mini.py @@ -0,0 +1,16 @@ +import os +Import("env") + +# Relocate firmware from 0x08000000 to 0x08005000 +for define in env['CPPDEFINES']: + if define[0] == "VECT_TAB_ADDR": + env['CPPDEFINES'].remove(define) +env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08005000")) + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/fly_mini.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script + diff --git a/buildroot/share/PlatformIO/scripts/fysetc_cheetah_v20.py b/buildroot/share/PlatformIO/scripts/fysetc_cheetah_v20.py new file mode 100644 index 0000000..10471d3 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/fysetc_cheetah_v20.py @@ -0,0 +1,9 @@ +import os +Import("env") + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/variants/FYSETC_CHEETAH_V20/ldscript.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script diff --git a/buildroot/share/PlatformIO/scripts/generic_create_variant.py b/buildroot/share/PlatformIO/scripts/generic_create_variant.py new file mode 100644 index 0000000..0b82c69 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/generic_create_variant.py @@ -0,0 +1,27 @@ +import os,shutil +from SCons.Script import DefaultEnvironment +from platformio import util + +env = DefaultEnvironment() +platform = env.PioPlatform() +board = env.BoardConfig() + +FRAMEWORK_DIR = platform.get_package_dir("framework-arduinoststm32") +assert os.path.isdir(FRAMEWORK_DIR) +assert os.path.isdir("buildroot/share/PlatformIO/variants") + +mcu_type = board.get("build.mcu")[:-2] +variant = board.get("build.variant") +series = mcu_type[:7].upper() + "xx" +variant_dir = os.path.join(FRAMEWORK_DIR, "variants", variant) + +source_dir = os.path.join("buildroot/share/PlatformIO/variants", variant) +assert os.path.isdir(source_dir) + +if not os.path.isdir(variant_dir): + os.mkdir(variant_dir) + +for file_name in os.listdir(source_dir): + full_file_name = os.path.join(source_dir, file_name) + if os.path.isfile(full_file_name): + shutil.copy(full_file_name, variant_dir) diff --git a/buildroot/share/PlatformIO/scripts/jgaurora_a5s_a1_with_bootloader.py b/buildroot/share/PlatformIO/scripts/jgaurora_a5s_a1_with_bootloader.py new file mode 100644 index 0000000..6c8a4f6 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/jgaurora_a5s_a1_with_bootloader.py @@ -0,0 +1,48 @@ +import os +Import("env") + +# Relocate firmware from 0x08000000 to 0x0800A000 +env['CPPDEFINES'].remove(("VECT_TAB_ADDR", "0x8000000")) +#alternatively, for STSTM <=5.1.0 use line below +#env['CPPDEFINES'].remove(("VECT_TAB_ADDR", 134217728)) +env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x0800A000")) + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/jgaurora_a5s_a1.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script + +#append ${PROGNAME}.bin firmware after bootloader and save it as 'jgaurora_firmware.bin' +def addboot(source,target,env): + firmware = open(target[0].path, "rb") + lengthfirmware = os.path.getsize(target[0].path) + bootloader_dir = "buildroot/share/PlatformIO/scripts/jgaurora_bootloader.bin" + bootloader = open(bootloader_dir, "rb") + lengthbootloader = os.path.getsize(bootloader_dir) + firmware_with_boothloader_dir = target[0].dir.path +'/firmware_with_bootloader.bin' + if os.path.exists(firmware_with_boothloader_dir): + os.remove(firmware_with_boothloader_dir) + firmwareimage = open(firmware_with_boothloader_dir, "wb") + position = 0 + while position < lengthbootloader: + byte = bootloader.read(1) + firmwareimage.write(byte) + position += 1 + position = 0 + while position < lengthfirmware: + byte = firmware.read(1) + firmwareimage.write(byte) + position += 1 + bootloader.close() + firmware.close() + firmwareimage.close() + firmware_without_bootloader_dir = target[0].dir.path+'/firmware_for_sd_upload.bin' + if os.path.exists(firmware_without_bootloader_dir): + os.remove(firmware_without_bootloader_dir) + os.rename(target[0].path, firmware_without_bootloader_dir) + #os.rename(target[0].dir.path+'/firmware_with_bootloader.bin', target[0].dir.path+'/firmware.bin') + +env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", addboot); + diff --git a/buildroot/share/PlatformIO/scripts/jgaurora_bootloader.bin b/buildroot/share/PlatformIO/scripts/jgaurora_bootloader.bin Binary files differnew file mode 100644 index 0000000..c229e5b --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/jgaurora_bootloader.bin diff --git a/buildroot/share/PlatformIO/scripts/lerdge.py b/buildroot/share/PlatformIO/scripts/lerdge.py new file mode 100644 index 0000000..fd934a1 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/lerdge.py @@ -0,0 +1,46 @@ +import os,sys +Import("env") + +from SCons.Script import DefaultEnvironment +board = DefaultEnvironment().BoardConfig() + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/lerdge.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script + +def encryptByte(byte): + byte = 0xFF & ((byte << 6) | (byte >> 2)) + i = 0x58 + byte + j = 0x05 + byte + (i >> 8) + byte = (0xF8 & i) | (0x07 & j) + return byte + +def encrypt_file(input, output_file, file_length): + input_file = bytearray(input.read()) + for i in range(len(input_file)): + result = encryptByte(input_file[i]) + input_file[i] = result + + output_file.write(input_file) + return + +# Encrypt ${PROGNAME}.bin and save it as build.firmware +def encrypt(source, target, env): + print("Encrypting to:", board.get("build.firmware")) + firmware = open(target[0].path, "rb") + result = open(target[0].dir.path + "/" + board.get("build.firmware"), "wb") + length = os.path.getsize(target[0].path) + + encrypt_file(firmware, result, length) + + firmware.close() + result.close() + +if 'firmware' in board.get("build").keys(): + env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt); +else: + print("You need to define output file via board_build.firmware = 'filename' parameter") + exit(1); diff --git a/buildroot/share/PlatformIO/scripts/mks_encrypt.py b/buildroot/share/PlatformIO/scripts/mks_encrypt.py new file mode 100644 index 0000000..0d53cdf --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/mks_encrypt.py @@ -0,0 +1,32 @@ +import os,sys +Import("env") + +from SCons.Script import DefaultEnvironment +board = DefaultEnvironment().BoardConfig() + +# Encrypt ${PROGNAME}.bin and save it as build.firmware ('Robin.bin') +def encrypt(source, target, env): + key = [0xA3, 0xBD, 0xAD, 0x0D, 0x41, 0x11, 0xBB, 0x8D, 0xDC, 0x80, 0x2D, 0xD0, 0xD2, 0xC4, 0x9B, 0x1E, 0x26, 0xEB, 0xE3, 0x33, 0x4A, 0x15, 0xE4, 0x0A, 0xB3, 0xB1, 0x3C, 0x93, 0xBB, 0xAF, 0xF7, 0x3E] + + firmware = open(target[0].path, "rb") + robin = open(target[0].dir.path +'/'+ board.get("build.firmware"), "wb") + length = os.path.getsize(target[0].path) + position = 0 + try: + while position < length: + byte = firmware.read(1) + if position >= 320 and position < 31040: + byte = chr(ord(byte) ^ key[position & 31]) + if sys.version_info[0] > 2: + byte = bytes(byte, 'latin1') + robin.write(byte) + position += 1 + finally: + firmware.close() + robin.close() + +if 'firmware' in board.get("build").keys(): + env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt); +else: + print("You need to define output file via board_build.firmware = 'filename' parameter", file=sys.stderr) + exit(1); diff --git a/buildroot/share/PlatformIO/scripts/mks_robin.py b/buildroot/share/PlatformIO/scripts/mks_robin.py new file mode 100644 index 0000000..dd2342b --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/mks_robin.py @@ -0,0 +1,39 @@ +import os +Import("env") + +# Relocate firmware from 0x08000000 to 0x08007000 +for define in env['CPPDEFINES']: + if define[0] == "VECT_TAB_ADDR": + env['CPPDEFINES'].remove(define) +env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08007000")) + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/mks_robin.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script + +# Encrypt ${PROGNAME}.bin and save it as 'Robin.bin' +def encrypt(source, target, env): + import sys + + key = [0xA3, 0xBD, 0xAD, 0x0D, 0x41, 0x11, 0xBB, 0x8D, 0xDC, 0x80, 0x2D, 0xD0, 0xD2, 0xC4, 0x9B, 0x1E, 0x26, 0xEB, 0xE3, 0x33, 0x4A, 0x15, 0xE4, 0x0A, 0xB3, 0xB1, 0x3C, 0x93, 0xBB, 0xAF, 0xF7, 0x3E] + + firmware = open(target[0].path, "rb") + robin = open(target[0].dir.path +'/Robin.bin', "wb") + length = os.path.getsize(target[0].path) + position = 0 + try: + while position < length: + byte = firmware.read(1) + if position >= 320 and position < 31040: + byte = chr(ord(byte) ^ key[position & 31]) + if sys.version_info[0] > 2: + byte = bytes(byte, 'latin1') + robin.write(byte) + position += 1 + finally: + firmware.close() + robin.close() +env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt); diff --git a/buildroot/share/PlatformIO/scripts/mks_robin_e3.py b/buildroot/share/PlatformIO/scripts/mks_robin_e3.py new file mode 100644 index 0000000..3af623c --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/mks_robin_e3.py @@ -0,0 +1,40 @@ +import os +Import("env") + +# Relocate firmware from 0x08000000 to 0x08005000 +for define in env['CPPDEFINES']: + if define[0] == "VECT_TAB_ADDR": + env['CPPDEFINES'].remove(define) +env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08005000")) + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/mks_robin_e3.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script + + +# Encrypt ${PROGNAME}.bin and save it as 'mksLite.bin' +def encrypt(source, target, env): + import sys + + key = [0xA3, 0xBD, 0xAD, 0x0D, 0x41, 0x11, 0xBB, 0x8D, 0xDC, 0x80, 0x2D, 0xD0, 0xD2, 0xC4, 0x9B, 0x1E, 0x26, 0xEB, 0xE3, 0x33, 0x4A, 0x15, 0xE4, 0x0A, 0xB3, 0xB1, 0x3C, 0x93, 0xBB, 0xAF, 0xF7, 0x3E] + + firmware = open(target[0].path, "rb") + robin = open(target[0].dir.path +'/Robin_e3.bin', "wb") + length = os.path.getsize(target[0].path) + position = 0 + try: + while position < length: + byte = firmware.read(1) + if position >= 320 and position < 31040: + byte = chr(ord(byte) ^ key[position & 31]) + if sys.version_info[0] > 2: + byte = bytes(byte, 'latin1') + robin.write(byte) + position += 1 + finally: + firmware.close() + robin.close() +env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt); diff --git a/buildroot/share/PlatformIO/scripts/mks_robin_e3p.py b/buildroot/share/PlatformIO/scripts/mks_robin_e3p.py new file mode 100644 index 0000000..2aab14b --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/mks_robin_e3p.py @@ -0,0 +1,40 @@ +import os +Import("env") + +# Relocate firmware from 0x08000000 to 0x08007000 +for define in env['CPPDEFINES']: + if define[0] == "VECT_TAB_ADDR": + env['CPPDEFINES'].remove(define) +env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08007000")) + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/mks_robin_e3p.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script + + +# Encrypt ${PROGNAME}.bin and save it as 'mks_robin_e3p.bin' +def encrypt(source, target, env): + import sys + + key = [0xA3, 0xBD, 0xAD, 0x0D, 0x41, 0x11, 0xBB, 0x8D, 0xDC, 0x80, 0x2D, 0xD0, 0xD2, 0xC4, 0x9B, 0x1E, 0x26, 0xEB, 0xE3, 0x33, 0x4A, 0x15, 0xE4, 0x0A, 0xB3, 0xB1, 0x3C, 0x93, 0xBB, 0xAF, 0xF7, 0x3E] + + firmware = open(target[0].path, "rb") + robin = open(target[0].dir.path +'/Robin_e3p.bin', "wb") + length = os.path.getsize(target[0].path) + position = 0 + try: + while position < length: + byte = firmware.read(1) + if position >= 320 and position < 31040: + byte = chr(ord(byte) ^ key[position & 31]) + if sys.version_info[0] > 2: + byte = bytes(byte, 'latin1') + robin.write(byte) + position += 1 + finally: + firmware.close() + robin.close() +env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt); diff --git a/buildroot/share/PlatformIO/scripts/mks_robin_lite.py b/buildroot/share/PlatformIO/scripts/mks_robin_lite.py new file mode 100644 index 0000000..2f3ae1f --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/mks_robin_lite.py @@ -0,0 +1,40 @@ +import os +Import("env") + +# Relocate firmware from 0x08000000 to 0x08005000 +for define in env['CPPDEFINES']: + if define[0] == "VECT_TAB_ADDR": + env['CPPDEFINES'].remove(define) +env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08005000")) + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/mks_robin_lite.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script + + +# Encrypt ${PROGNAME}.bin and save it as 'mksLite.bin' +def encrypt(source, target, env): + import sys + + key = [0xA3, 0xBD, 0xAD, 0x0D, 0x41, 0x11, 0xBB, 0x8D, 0xDC, 0x80, 0x2D, 0xD0, 0xD2, 0xC4, 0x9B, 0x1E, 0x26, 0xEB, 0xE3, 0x33, 0x4A, 0x15, 0xE4, 0x0A, 0xB3, 0xB1, 0x3C, 0x93, 0xBB, 0xAF, 0xF7, 0x3E] + + firmware = open(target[0].path, "rb") + robin = open(target[0].dir.path +'/mksLite.bin', "wb") + length = os.path.getsize(target[0].path) + position = 0 + try: + while position < length: + byte = firmware.read(1) + if position >= 320 and position < 31040: + byte = chr(ord(byte) ^ key[position & 31]) + if sys.version_info[0] > 2: + byte = bytes(byte, 'latin1') + robin.write(byte) + position += 1 + finally: + firmware.close() + robin.close() +env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt); diff --git a/buildroot/share/PlatformIO/scripts/mks_robin_lite3.py b/buildroot/share/PlatformIO/scripts/mks_robin_lite3.py new file mode 100644 index 0000000..67ad442 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/mks_robin_lite3.py @@ -0,0 +1,40 @@ +import os +Import("env") + +# Relocate firmware from 0x08000000 to 0x08005000 +for define in env['CPPDEFINES']: + if define[0] == "VECT_TAB_ADDR": + env['CPPDEFINES'].remove(define) +env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08005000")) + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/mks_robin_lite.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script + + +# Encrypt ${PROGNAME}.bin and save it as 'mksLite.bin' +def encrypt(source, target, env): + import sys + + key = [0xA3, 0xBD, 0xAD, 0x0D, 0x41, 0x11, 0xBB, 0x8D, 0xDC, 0x80, 0x2D, 0xD0, 0xD2, 0xC4, 0x9B, 0x1E, 0x26, 0xEB, 0xE3, 0x33, 0x4A, 0x15, 0xE4, 0x0A, 0xB3, 0xB1, 0x3C, 0x93, 0xBB, 0xAF, 0xF7, 0x3E] + + firmware = open(target[0].path, "rb") + robin = open(target[0].dir.path +'/mksLite3.bin', "wb") + length = os.path.getsize(target[0].path) + position = 0 + try: + while position < length: + byte = firmware.read(1) + if position >= 320 and position < 31040: + byte = chr(ord(byte) ^ key[position & 31]) + if sys.version_info[0] > 2: + byte = bytes(byte, 'latin1') + robin.write(byte) + position += 1 + finally: + firmware.close() + robin.close() +env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt); diff --git a/buildroot/share/PlatformIO/scripts/mks_robin_mini.py b/buildroot/share/PlatformIO/scripts/mks_robin_mini.py new file mode 100644 index 0000000..d386698 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/mks_robin_mini.py @@ -0,0 +1,40 @@ +import os +Import("env") + +# Relocate firmware from 0x08000000 to 0x08007000 +for define in env['CPPDEFINES']: + if define[0] == "VECT_TAB_ADDR": + env['CPPDEFINES'].remove(define) +env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08007000")) + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/mks_robin_mini.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script + + +# Encrypt ${PROGNAME}.bin and save it as 'Robin_mini.bin' +def encrypt(source, target, env): + import sys + + key = [0xA3, 0xBD, 0xAD, 0x0D, 0x41, 0x11, 0xBB, 0x8D, 0xDC, 0x80, 0x2D, 0xD0, 0xD2, 0xC4, 0x9B, 0x1E, 0x26, 0xEB, 0xE3, 0x33, 0x4A, 0x15, 0xE4, 0x0A, 0xB3, 0xB1, 0x3C, 0x93, 0xBB, 0xAF, 0xF7, 0x3E] + + firmware = open(target[0].path, "rb") + robin = open(target[0].dir.path +'/Robin_mini.bin', "wb") + length = os.path.getsize(target[0].path) + position = 0 + try: + while position < length: + byte = firmware.read(1) + if position >= 320 and position < 31040: + byte = chr(ord(byte) ^ key[position & 31]) + if sys.version_info[0] > 2: + byte = bytes(byte, 'latin1') + robin.write(byte) + position += 1 + finally: + firmware.close() + robin.close() +env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt); diff --git a/buildroot/share/PlatformIO/scripts/mks_robin_nano.py b/buildroot/share/PlatformIO/scripts/mks_robin_nano.py new file mode 100644 index 0000000..a68fd30 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/mks_robin_nano.py @@ -0,0 +1,40 @@ +import os +Import("env") + +# Relocate firmware from 0x08000000 to 0x08007000 +for define in env['CPPDEFINES']: + if define[0] == "VECT_TAB_ADDR": + env['CPPDEFINES'].remove(define) +env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08007000")) + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/mks_robin_nano.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script + + +# Encrypt ${PROGNAME}.bin and save it as 'Robin_nano.bin' +def encrypt(source, target, env): + import sys + + key = [0xA3, 0xBD, 0xAD, 0x0D, 0x41, 0x11, 0xBB, 0x8D, 0xDC, 0x80, 0x2D, 0xD0, 0xD2, 0xC4, 0x9B, 0x1E, 0x26, 0xEB, 0xE3, 0x33, 0x4A, 0x15, 0xE4, 0x0A, 0xB3, 0xB1, 0x3C, 0x93, 0xBB, 0xAF, 0xF7, 0x3E] + + firmware = open(target[0].path, "rb") + robin = open(target[0].dir.path +'/Robin_nano.bin', "wb") + length = os.path.getsize(target[0].path) + position = 0 + try: + while position < length: + byte = firmware.read(1) + if position >= 320 and position < 31040: + byte = chr(ord(byte) ^ key[position & 31]) + if sys.version_info[0] > 2: + byte = bytes(byte, 'latin1') + robin.write(byte) + position += 1 + finally: + firmware.close() + robin.close() +env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt); diff --git a/buildroot/share/PlatformIO/scripts/mks_robin_nano35.py b/buildroot/share/PlatformIO/scripts/mks_robin_nano35.py new file mode 100644 index 0000000..0047289 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/mks_robin_nano35.py @@ -0,0 +1,40 @@ +import os +Import("env") + +# Relocate firmware from 0x08000000 to 0x08007000 +for define in env['CPPDEFINES']: + if define[0] == "VECT_TAB_ADDR": + env['CPPDEFINES'].remove(define) +env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08007000")) + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/mks_robin_nano.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script + + +# Encrypt ${PROGNAME}.bin and save it as 'Robin_nano35.bin' +def encrypt(source, target, env): + import sys + + key = [0xA3, 0xBD, 0xAD, 0x0D, 0x41, 0x11, 0xBB, 0x8D, 0xDC, 0x80, 0x2D, 0xD0, 0xD2, 0xC4, 0x9B, 0x1E, 0x26, 0xEB, 0xE3, 0x33, 0x4A, 0x15, 0xE4, 0x0A, 0xB3, 0xB1, 0x3C, 0x93, 0xBB, 0xAF, 0xF7, 0x3E] + + firmware = open(target[0].path, "rb") + robin = open(target[0].dir.path +'/Robin_nano35.bin', "wb") + length = os.path.getsize(target[0].path) + position = 0 + try: + while position < length: + byte = firmware.read(1) + if position >= 320 and position < 31040: + byte = chr(ord(byte) ^ key[position & 31]) + if sys.version_info[0] > 2: + byte = bytes(byte, 'latin1') + robin.write(byte) + position += 1 + finally: + firmware.close() + robin.close() +env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt); diff --git a/buildroot/share/PlatformIO/scripts/mks_robin_pro.py b/buildroot/share/PlatformIO/scripts/mks_robin_pro.py new file mode 100644 index 0000000..2ebf1ff --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/mks_robin_pro.py @@ -0,0 +1,39 @@ +import os +Import("env") + +# Relocate firmware from 0x08000000 to 0x08007000 +for define in env['CPPDEFINES']: + if define[0] == "VECT_TAB_ADDR": + env['CPPDEFINES'].remove(define) +env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08007000")) + +custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/mks_robin_pro.ld") +for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,-T" in flag: + env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script + elif flag == "-T": + env["LINKFLAGS"][i + 1] = custom_ld_script + +# Encrypt ${PROGNAME}.bin and save it as 'Robin.bin' +def encrypt(source, target, env): + import sys + + key = [0xA3, 0xBD, 0xAD, 0x0D, 0x41, 0x11, 0xBB, 0x8D, 0xDC, 0x80, 0x2D, 0xD0, 0xD2, 0xC4, 0x9B, 0x1E, 0x26, 0xEB, 0xE3, 0x33, 0x4A, 0x15, 0xE4, 0x0A, 0xB3, 0xB1, 0x3C, 0x93, 0xBB, 0xAF, 0xF7, 0x3E] + + firmware = open(target[0].path, "rb") + robin = open(target[0].dir.path +'/Robin_pro.bin', "wb") + length = os.path.getsize(target[0].path) + position = 0 + try: + while position < length: + byte = firmware.read(1) + if position >= 320 and position < 31040: + byte = chr(ord(byte) ^ key[position & 31]) + if sys.version_info[0] > 2: + byte = bytes(byte, 'latin1') + robin.write(byte) + position += 1 + finally: + firmware.close() + robin.close() +env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt); diff --git a/buildroot/share/PlatformIO/scripts/random-bin.py b/buildroot/share/PlatformIO/scripts/random-bin.py new file mode 100644 index 0000000..4d7ca5d --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/random-bin.py @@ -0,0 +1,5 @@ +Import("env") + +from datetime import datetime + +env['PROGNAME'] = datetime.now().strftime("firmware-%Y%m%d-%H%M%S") diff --git a/buildroot/share/PlatformIO/scripts/stm32_bootloader.py b/buildroot/share/PlatformIO/scripts/stm32_bootloader.py new file mode 100644 index 0000000..7f49ea0 --- /dev/null +++ b/buildroot/share/PlatformIO/scripts/stm32_bootloader.py @@ -0,0 +1,30 @@ +import os,sys,shutil +Import("env") + +from SCons.Script import DefaultEnvironment +board = DefaultEnvironment().BoardConfig() + +def noencrypt(source, target, env): + firmware = os.path.join(target[0].dir.path, board.get("build.firmware")) + shutil.copy(target[0].path, firmware) + +if 'offset' in board.get("build").keys(): + LD_FLASH_OFFSET = board.get("build.offset") + + for define in env['CPPDEFINES']: + if define[0] == "VECT_TAB_OFFSET": + env['CPPDEFINES'].remove(define) + env['CPPDEFINES'].append(("VECT_TAB_OFFSET", LD_FLASH_OFFSET)) + + maximum_ram_size = board.get("upload.maximum_ram_size") + + for i, flag in enumerate(env["LINKFLAGS"]): + if "-Wl,--defsym=LD_FLASH_OFFSET" in flag: + env["LINKFLAGS"][i] = "-Wl,--defsym=LD_FLASH_OFFSET=" + LD_FLASH_OFFSET + if "-Wl,--defsym=LD_MAX_DATA_SIZE" in flag: + env["LINKFLAGS"][i] = "-Wl,--defsym=LD_MAX_DATA_SIZE=" + str(maximum_ram_size - 40) + +board_keys = board.get("build").keys() +# Only copy file if there's no encryptation +if 'firmware' in board_keys and not 'encrypt' in board_keys: + env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", noencrypt) |