From 93cd385810b4c11bfba8deff5284e9efc290375b Mon Sep 17 00:00:00 2001 From: Cecille Freeman Date: Tue, 13 Jul 2021 21:35:31 -0400 Subject: [PATCH] Draft changes to esp32 flash_script --- .../all-clusters-app/esp32/CMakeLists.txt | 36 +--------- examples/common/cmake/idf_flashing.cmake | 71 +++++++++++++++++++ .../persistent-storage/esp32/CMakeLists.txt | 36 +--------- examples/pigweed-app/esp32/CMakeLists.txt | 45 +----------- .../esp32/CMakeLists.txt | 35 +-------- scripts/examples/build-all-clusters-app.py | 31 +++++++- scripts/flashing/esp32_firmware_utils.py | 2 +- 7 files changed, 108 insertions(+), 148 deletions(-) create mode 100644 examples/common/cmake/idf_flashing.cmake diff --git a/examples/all-clusters-app/esp32/CMakeLists.txt b/examples/all-clusters-app/esp32/CMakeLists.txt index 9d98e41df4be5c..ae2d79bbcf726b 100644 --- a/examples/all-clusters-app/esp32/CMakeLists.txt +++ b/examples/all-clusters-app/esp32/CMakeLists.txt @@ -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" @@ -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() diff --git a/examples/common/cmake/idf_flashing.cmake b/examples/common/cmake/idf_flashing.cmake new file mode 100644 index 00000000000000..a34cfbdab246b3 --- /dev/null +++ b/examples/common/cmake/idf_flashing.cmake @@ -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 ]) +# +# 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) diff --git a/examples/persistent-storage/esp32/CMakeLists.txt b/examples/persistent-storage/esp32/CMakeLists.txt index 4844a89caa49a1..fb5f8946f15a7e 100644 --- a/examples/persistent-storage/esp32/CMakeLists.txt +++ b/examples/persistent-storage/esp32/CMakeLists.txt @@ -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" @@ -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() diff --git a/examples/pigweed-app/esp32/CMakeLists.txt b/examples/pigweed-app/esp32/CMakeLists.txt index cd200eb302fc61..275cf37f04d097 100644 --- a/examples/pigweed-app/esp32/CMakeLists.txt +++ b/examples/pigweed-app/esp32/CMakeLists.txt @@ -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") \ No newline at end of file diff --git a/examples/temperature-measurement-app/esp32/CMakeLists.txt b/examples/temperature-measurement-app/esp32/CMakeLists.txt index b5bf9444c68a2a..a4f080a414a30d 100644 --- a/examples/temperature-measurement-app/esp32/CMakeLists.txt +++ b/examples/temperature-measurement-app/esp32/CMakeLists.txt @@ -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() diff --git a/scripts/examples/build-all-clusters-app.py b/scripts/examples/build-all-clusters-app.py index b2a669f4b6e7b7..5f7403efee0dad 100755 --- a/scripts/examples/build-all-clusters-app.py +++ b/scripts/examples/build-all-clusters-app.py @@ -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)]) @@ -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 @@ -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' @@ -79,6 +107,7 @@ def main(): shutil.move(old_default_sdkconfig, default_sdkconfig) logging.info('Compiling') + e.execute('build') diff --git a/scripts/flashing/esp32_firmware_utils.py b/scripts/flashing/esp32_firmware_utils.py index 50c904464cc067..ed5d2b8c93131b 100755 --- a/scripts/flashing/esp32_firmware_utils.py +++ b/scripts/flashing/esp32_firmware_utils.py @@ -227,7 +227,7 @@ }, 'bootloader_offset': { 'help': 'Bootloader offset', - 'default': None, + 'default': '0x1000', 'argparse': { 'metavar': 'OFFSET' },