Skip to content

Commit

Permalink
Draft changes to esp32 flash_script
Browse files Browse the repository at this point in the history
  • Loading branch information
cecille committed Jul 14, 2021
1 parent e922686 commit 93cd385
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 148 deletions.
36 changes: 2 additions & 34 deletions examples/all-clusters-app/esp32/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/../../common/cmake/idf_flashing.cmake)

set(EXTRA_COMPONENT_DIRS
"${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/config/esp32/components"
Expand All @@ -35,37 +36,4 @@ project(chip-all-clusters-app)
idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++14;-Os;-DLWIP_IPV6_SCOPES=0;-DCHIP_HAVE_CONFIG_H" APPEND)
idf_build_set_property(C_COMPILE_OPTIONS "-Os;-DLWIP_IPV6_SCOPES=0" APPEND)

idf_build_get_property(build_dir BUILD_DIR)
idf_build_get_property(project_path PROJECT_DIR)
idf_build_get_property(sdkconfig SDKCONFIG)
idf_build_get_property(idf_path IDF_PATH)

add_custom_command(OUTPUT "${build_dir}/firmware_utils.py"
COMMAND ${CMAKE_COMMAND} ARGS -E copy "${project_path}/third_party/connectedhomeip/scripts/flashing/firmware_utils.py" "${build_dir}/"
WORKING_DIRECTORY ${build_dir}
VERBATIM)

add_custom_command(OUTPUT "${build_dir}/esp32_firmware_utils.py"
COMMAND ${CMAKE_COMMAND} ARGS -E copy "${project_path}/third_party/connectedhomeip/scripts/flashing/esp32_firmware_utils.py" "${build_dir}/"
WORKING_DIRECTORY ${build_dir}
VERBATIM)

add_custom_command(OUTPUT "${build_dir}/${CMAKE_PROJECT_NAME}.flash.py"
COMMAND ${python}
"${project_path}/../../../scripts/flashing/gen_flashing_script.py" esp32
--output "${build_dir}/${CMAKE_PROJECT_NAME}.flash.py"
--port "$ENV{ESPPORT}"
--baud "$ENV{ESPBAUD}"
--before ${CONFIG_ESPTOOLPY_BEFORE}
--after ${CONFIG_ESPTOOLPY_AFTER}
--application "${CMAKE_PROJECT_NAME}.bin"
--bootloader "bootloader/bootloader.bin"
--partition "partition_table/partition-table.bin"
--use-partition-file "${build_dir}/partition_table/partition-table.bin"
--use-parttool ${idf_path}/components/partition_table/parttool.py
--use-sdkconfig ${project_path}/sdkconfig
WORKING_DIRECTORY ${build_dir}
COMMENT "To flash ${build_dir}/${CMAKE_PROJECT_NAME}.bin run ./build/${CMAKE_PROJECT_NAME}.flash.py"
VERBATIM)

add_custom_target(flashing_script DEPENDS "${build_dir}/${CMAKE_PROJECT_NAME}.bin" "${build_dir}/${CMAKE_PROJECT_NAME}.flash.py" "${build_dir}/esp32_firmware_utils.py" "${build_dir}/firmware_utils.py")
flashing_script()
71 changes: 71 additions & 0 deletions examples/common/cmake/idf_flashing.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright (c) 2021 Project CHIP Authors
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Common cmake code for creating flash scripts.
# Must include the IDF project.cmake file before this file.
# Usage:
#
# flashing_script([DEPENDS <dep1> <dep2>])
#
# where DEPENDS and its args are optional and list additional dependencies.
# (use full path).

function(get_additional_flashing_depends)
set(options)
set(oneValueArgs)
set(multiValueArgs DEPENDS)
cmake_parse_arguments(MYARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
set(additional_flashing_depends "${MYARGS_DEPENDS}" PARENT_SCOPE)
endfunction(get_additional_flashing_depends)

macro(flashing_script)
idf_build_get_property(build_dir BUILD_DIR)
idf_build_get_property(project_path PROJECT_DIR)
idf_build_get_property(sdkconfig SDKCONFIG)
idf_build_get_property(idf_path IDF_PATH)

set(flashing_utils_dir "${project_path}/third_party/connectedhomeip/scripts/flashing/")
set(board_firmware_utils "${IDF_TARGET}_firmware_utils.py")
configure_file("${flashing_utils_dir}/${board_firmware_utils}" "${build_dir}/${board_firmware_utils}")
configure_file("${flashing_utils_dir}/firmware_utils.py" "${build_dir}/firmware_utils.py")

get_additional_flashing_depends(${ARGN})
foreach(dep IN LISTS additional_flashing_depends)
get_filename_component(filename ${dep} NAME)
configure_file(${dep}, "${build_dir}/${filename}")
list(APPEND build_dir_depends "${build_dir}/${filename}")
endforeach(dep)

add_custom_target(flashing_script
COMMAND ${python}
"${project_path}/../../../scripts/flashing/gen_flashing_script.py" ${IDF_TARGET}
--output "${build_dir}/${CMAKE_PROJECT_NAME}.flash.py"
--port "$ENV{ESPPORT}"
--baud "$ENV{ESPBAUD}"
--before ${CONFIG_ESPTOOLPY_BEFORE}
--after ${CONFIG_ESPTOOLPY_AFTER}
--application "${CMAKE_PROJECT_NAME}.bin"
--bootloader "bootloader/bootloader.bin"
--partition "partition_table/partition-table.bin"
--use-partition-file "${build_dir}/partition_table/partition-table.bin"
--use-parttool ${idf_path}/components/partition_table/parttool.py
--use-sdkconfig ${project_path}/sdkconfig
WORKING_DIRECTORY ${build_dir}
DEPENDS "${build_dir}/${board_firmware_utils}"
"${build_dir}/firmware_utils.py"
"${build_dir_deps}"
COMMENT "To flash ${build_dir}/${CMAKE_PROJECT_NAME}.bin run ./build/${CMAKE_PROJECT_NAME}.flash.py"
VERBATIM)
endmacro(flashing_script)
36 changes: 2 additions & 34 deletions examples/persistent-storage/esp32/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/../../common/cmake/idf_flashing.cmake)

set(EXTRA_COMPONENT_DIRS
"${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/config/esp32/components"
Expand All @@ -26,37 +27,4 @@ project(chip-persistent-storage)
idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++14;-Os;-DLWIP_IPV6_SCOPES=0;-DCHIP_HAVE_CONFIG_H" APPEND)
idf_build_set_property(C_COMPILE_OPTIONS "-Os;-DLWIP_IPV6_SCOPES=0" APPEND)

idf_build_get_property(build_dir BUILD_DIR)
idf_build_get_property(project_path PROJECT_DIR)
idf_build_get_property(sdkconfig SDKCONFIG)
idf_build_get_property(idf_path IDF_PATH)

add_custom_command(OUTPUT "${build_dir}/firmware_utils.py"
COMMAND ${CMAKE_COMMAND} ARGS -E copy "${project_path}/third_party/connectedhomeip/scripts/flashing/firmware_utils.py" "${build_dir}/"
WORKING_DIRECTORY ${build_dir}
VERBATIM)

add_custom_command(OUTPUT "${build_dir}/esp32_firmware_utils.py"
COMMAND ${CMAKE_COMMAND} ARGS -E copy "${project_path}/third_party/connectedhomeip/scripts/flashing/esp32_firmware_utils.py" "${build_dir}/"
WORKING_DIRECTORY ${build_dir}
VERBATIM)

add_custom_command(OUTPUT "${build_dir}/${CMAKE_PROJECT_NAME}.flash.py"
COMMAND ${python}
"${project_path}/../../../scripts/flashing/gen_flashing_script.py" esp32
--output "${build_dir}/${CMAKE_PROJECT_NAME}.flash.py"
--port "$ENV{ESPPORT}"
--baud "$ENV{ESPBAUD}"
--before ${CONFIG_ESPTOOLPY_BEFORE}
--after ${CONFIG_ESPTOOLPY_AFTER}
--application "${CMAKE_PROJECT_NAME}.bin"
--bootloader "bootloader/bootloader.bin"
--partition "partition_table/partition-table.bin"
--use-partition-file "${build_dir}/partition_table/partition-table.bin"
--use-parttool ${idf_path}/components/partition_table/parttool.py
--use-sdkconfig ${project_path}/sdkconfig
WORKING_DIRECTORY ${build_dir}
COMMENT "To flash ${build_dir}/${CMAKE_PROJECT_NAME}.bin run ./build/${CMAKE_PROJECT_NAME}.flash.py"
VERBATIM)

add_custom_target(flashing_script DEPENDS "${build_dir}/${CMAKE_PROJECT_NAME}.bin" "${build_dir}/${CMAKE_PROJECT_NAME}.flash.py" "${build_dir}/esp32_firmware_utils.py" "${build_dir}/firmware_utils.py")
flashing_script()
45 changes: 1 addition & 44 deletions examples/pigweed-app/esp32/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,47 +27,4 @@ project(chip-pigweed-app)
idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++17;-Os;-DLWIP_IPV6_SCOPES=0;-DCHIP_HAVE_CONFIG_H" APPEND)
idf_build_set_property(C_COMPILE_OPTIONS "-Os;-DLWIP_IPV6_SCOPES=0" APPEND)

idf_build_get_property(build_dir BUILD_DIR)
idf_build_get_property(project_path PROJECT_DIR)
idf_build_get_property(sdkconfig SDKCONFIG)
idf_build_get_property(idf_path IDF_PATH)

add_custom_command(OUTPUT "${build_dir}/firmware_utils.py"
COMMAND ${CMAKE_COMMAND} ARGS -E copy "${project_path}/third_party/connectedhomeip/scripts/flashing/firmware_utils.py" "${build_dir}/"
WORKING_DIRECTORY ${build_dir}
VERBATIM)

add_custom_command(OUTPUT "${build_dir}/esp32_firmware_utils.py"
COMMAND ${CMAKE_COMMAND} ARGS -E copy "${project_path}/third_party/connectedhomeip/scripts/flashing/esp32_firmware_utils.py" "${build_dir}/"
WORKING_DIRECTORY ${build_dir}
VERBATIM)

add_custom_command(OUTPUT "${build_dir}/echo_test.py"
COMMAND ${CMAKE_COMMAND} ARGS -E copy "${project_path}/../mobly_tests/echo_test.py" "${build_dir}/"
WORKING_DIRECTORY ${build_dir}
VERBATIM)

add_custom_command(OUTPUT "${build_dir}/echo_test_config.yml"
COMMAND ${CMAKE_COMMAND} ARGS -E copy "${project_path}/echo_test_config.yml" "${build_dir}/"
WORKING_DIRECTORY ${build_dir}
VERBATIM)

add_custom_command(OUTPUT "${build_dir}/${CMAKE_PROJECT_NAME}.flash.py"
COMMAND ${python}
"${project_path}/../../../scripts/flashing/gen_flashing_script.py" esp32
--output "${build_dir}/${CMAKE_PROJECT_NAME}.flash.py"
--port "$ENV{ESPPORT}"
--baud "$ENV{ESPBAUD}"
--before ${CONFIG_ESPTOOLPY_BEFORE}
--after ${CONFIG_ESPTOOLPY_AFTER}
--application "${CMAKE_PROJECT_NAME}.bin"
--bootloader "bootloader/bootloader.bin"
--partition "partition_table/partition-table.bin"
--use-partition-file "${build_dir}/partition_table/partition-table.bin"
--use-parttool ${idf_path}/components/partition_table/parttool.py
--use-sdkconfig ${project_path}/sdkconfig
WORKING_DIRECTORY ${build_dir}
COMMENT "To flash ${build_dir}/${CMAKE_PROJECT_NAME}.bin run ./build/${CMAKE_PROJECT_NAME}.flash.py"
VERBATIM)

add_custom_target(flashing_script DEPENDS "${build_dir}/${CMAKE_PROJECT_NAME}.bin" "${build_dir}/${CMAKE_PROJECT_NAME}.flash.py" "${build_dir}/esp32_firmware_utils.py" "${build_dir}/firmware_utils.py" "${build_dir}/echo_test.py" "${build_dir}/echo_test_config.yml")
flashing_script(DEPENDS "${project_path}/echo_test_config.yml" "${project_path}/../mobly_tests/echo_test.py")
35 changes: 1 addition & 34 deletions examples/temperature-measurement-app/esp32/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,4 @@ project(chip-temperature-measurement-app)
idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++14;-Os;-DLWIP_IPV6_SCOPES=0;-DCHIP_HAVE_CONFIG_H" APPEND)
idf_build_set_property(C_COMPILE_OPTIONS "-Os;-DLWIP_IPV6_SCOPES=0" APPEND)

idf_build_get_property(build_dir BUILD_DIR)
idf_build_get_property(project_path PROJECT_DIR)
idf_build_get_property(sdkconfig SDKCONFIG)
idf_build_get_property(idf_path IDF_PATH)

add_custom_command(OUTPUT "${build_dir}/firmware_utils.py"
COMMAND ${CMAKE_COMMAND} ARGS -E copy "${project_path}/third_party/connectedhomeip/scripts/flashing/firmware_utils.py" "${build_dir}/"
WORKING_DIRECTORY ${build_dir}
VERBATIM)

add_custom_command(OUTPUT "${build_dir}/esp32_firmware_utils.py"
COMMAND ${CMAKE_COMMAND} ARGS -E copy "${project_path}/third_party/connectedhomeip/scripts/flashing/esp32_firmware_utils.py" "${build_dir}/"
WORKING_DIRECTORY ${build_dir}
VERBATIM)

add_custom_command(OUTPUT "${build_dir}/${CMAKE_PROJECT_NAME}.flash.py"
COMMAND ${python}
"${project_path}/../../../scripts/flashing/gen_flashing_script.py" esp32
--output "${build_dir}/${CMAKE_PROJECT_NAME}.flash.py"
--port "$ENV{ESPPORT}"
--baud "$ENV{ESPBAUD}"
--before ${CONFIG_ESPTOOLPY_BEFORE}
--after ${CONFIG_ESPTOOLPY_AFTER}
--application "${CMAKE_PROJECT_NAME}.bin"
--bootloader "bootloader/bootloader.bin"
--partition "partition_table/partition-table.bin"
--use-partition-file "${build_dir}/partition_table/partition-table.bin"
--use-parttool ${idf_path}/components/partition_table/parttool.py
--use-sdkconfig ${project_path}/sdkconfig
WORKING_DIRECTORY ${build_dir}
COMMENT "To flash ${build_dir}/${CMAKE_PROJECT_NAME}.bin run ./build/${CMAKE_PROJECT_NAME}.flash.py"
VERBATIM)

add_custom_target(flashing_script DEPENDS "${build_dir}/${CMAKE_PROJECT_NAME}.bin" "${build_dir}/${CMAKE_PROJECT_NAME}.flash.py" "${build_dir}/esp32_firmware_utils.py" "${build_dir}/firmware_utils.py")
flashing_script()
31 changes: 30 additions & 1 deletion scripts/examples/build-all-clusters-app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self):
self.run_cmd = os.path.join(self.chip_root, "scripts", "run_in_build_env.sh")
logging.info("Executing via: %s" % self.run_cmd)


def execute(self, command):
os.chdir(self.chip_root)
subprocess.call([self.run_cmd, 'source "%s/export.sh"; cd %s; idf.py %s' % (idf_path, ROOT, command)])
Expand All @@ -44,6 +44,13 @@ def main():
default=None,
choices=['m5stack', 'devkit', 'curr', 'default'],
)
parser.add_argument(
'--generate-flash-script',
action='store_true',
)
parser.add_argument('--port', type=str, help='port to use for flashing. Ex. --port /dev/ttyUSB0')
parser.add_argument('--baud', type=int, help='baud rage to use for flasing')

args = parser.parse_args()

# Ensures somewhat pretty logging of what is going on
Expand All @@ -54,6 +61,27 @@ def main():

e = IDFExecutor()

port = os.getenv('ESPPORT')
baud = os.getenv('ESPBAUD')
if args.generate_flash_script:
if args.port is not None:
port = args.port
if args.baud is not None:
baud = args.baud

envs_ok = True
if port is None:
logging.error('Port must be set to use flashing.')
logging.error('This can be set using the ESPPORT environment var or the --port argument in this script')
envs_ok = False
if baud is None:
logging.error('Baud rate must be set to use flashing.')
logging.error('This can be set using the ESPBAUD environment var or the --baud argument in this script')
envs_ok = False

if not envs_ok:
return

if args.clear_config:
old_default_sdkconfig = None
clear_curr = args.clear_config != 'curr'
Expand All @@ -79,6 +107,7 @@ def main():
shutil.move(old_default_sdkconfig, default_sdkconfig)

logging.info('Compiling')

e.execute('build')


Expand Down
2 changes: 1 addition & 1 deletion scripts/flashing/esp32_firmware_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
},
'bootloader_offset': {
'help': 'Bootloader offset',
'default': None,
'default': '0x1000',
'argparse': {
'metavar': 'OFFSET'
},
Expand Down

0 comments on commit 93cd385

Please sign in to comment.