Skip to content

Commit

Permalink
Added script to convert hex to bin file for OTA updates
Browse files Browse the repository at this point in the history
  • Loading branch information
gabryelreyes committed Sep 18, 2024
1 parent 76b72a2 commit 9f22942
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ lib_deps =
lib_ignore =
HALTest
extra_scripts =
post:./scripts/create_binary.py

monitor_speed = 115200

Expand Down
79 changes: 79 additions & 0 deletions scripts/create_binary.py
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
#
# 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

0 comments on commit 9f22942

Please sign in to comment.