Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary output file #157

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
87 changes: 87 additions & 0 deletions scripts/create_binary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""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) -> 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.
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