From 9f229422eb51e3e620979580023804b25800c85c Mon Sep 17 00:00:00 2001 From: greyes Date: Wed, 18 Sep 2024 22:53:11 +0200 Subject: [PATCH 1/2] Added script to convert hex to bin file for OTA updates --- platformio.ini | 1 + scripts/create_binary.py | 79 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 scripts/create_binary.py diff --git a/platformio.ini b/platformio.ini index d153a00e..7a4ab9a5 100644 --- a/platformio.ini +++ b/platformio.ini @@ -67,6 +67,7 @@ lib_deps = lib_ignore = HALTest extra_scripts = + post:./scripts/create_binary.py monitor_speed = 115200 diff --git a/scripts/create_binary.py b/scripts/create_binary.py new file mode 100644 index 00000000..e67b2830 --- /dev/null +++ b/scripts/create_binary.py @@ -0,0 +1,79 @@ +"""Create a binary file from the .hex file. +Required for flashing the firmware through the DCS.""" + +# MIT License +# +# Copyright (c) 2022 - 2024 Andreas Merkle (web@blue-andi.de) +# +# 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. + +################################################################################ +# Imports +################################################################################ + +import os +Import("env") # pylint: disable=undefined-variable + +################################################################################ +# Variables +################################################################################ + +BUILD_DIR = env["BUILD_DIR"] # pylint: disable=undefined-variable +PROGRAM_PATH = os.path.join(BUILD_DIR, "firmware") +INPUT_FILE = PROGRAM_PATH + ".hex" +OUTPUT_FILE = PROGRAM_PATH + ".bin" + +# Command to convert .hex into .bin. +# Use the objcopy tool, which is part of the toolchain. +CMD = "${OBJCOPY} -I ihex \"" + INPUT_FILE +\ + "\" -O binary \"" + OUTPUT_FILE + "\"" + +################################################################################ +# Classes +################################################################################ + +################################################################################ +# Functions +################################################################################ + + +def create_binary(source, target, env): + """Create a binary file from the .hex file.""" + # pylint: disable=unused-argument + + # Convert .hex into .bin using objcopy. + print("Creating binary file...") + print(CMD) + + if 0 != env.Execute(CMD): + print("Command failed!") + Exit(1) # pylint: disable=undefined-variable + + +################################################################################ +# Main +################################################################################ + +# Always build .hex file. Assume it is always out-of-date +# pylint: disable=undefined-variable +env.AlwaysBuild(INPUT_FILE) # type: ignore + +# Register the callback to the creation of the .hex file +# pylint: disable=undefined-variable +env.AddPostAction(INPUT_FILE, create_binary) # type: ignore From 3d5f7df1981647bdab919eb132324af58039d763 Mon Sep 17 00:00:00 2001 From: Gabryel Reyes Date: Thu, 26 Sep 2024 08:51:15 +0200 Subject: [PATCH 2/2] Added missing documentation --- scripts/create_binary.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/scripts/create_binary.py b/scripts/create_binary.py index e67b2830..321efc8c 100644 --- a/scripts/create_binary.py +++ b/scripts/create_binary.py @@ -53,8 +53,16 @@ ################################################################################ -def create_binary(source, target, env): - """Create a binary file from the .hex file.""" +def create_binary(source, target, env) -> None: + """Create a binary file from the .hex file. + Arguments: + source: Output binary of build process. + target: File that triggered this callback. INPUT_FILE in this case. + env: SCons build environment + + Return: + None. Exits on fail. + """ # pylint: disable=unused-argument # Convert .hex into .bin using objcopy.