Skip to content

Instantly share code, notes, and snippets.

@tcbennun
Created September 24, 2021 12:53
Show Gist options
  • Save tcbennun/e922796d8ba9da18e96b5bd609118da6 to your computer and use it in GitHub Desktop.
Save tcbennun/e922796d8ba9da18e96b5bd609118da6 to your computer and use it in GitHub Desktop.
Generate and flash NVS partition automatically in ESP-IDF project
# This example is for a file named nvs_initial.csv, living in the root of your project.
# Place this code after your project() command.
# Your NVS partition binary will be generated as part of the default build target,
# and will be flashed automatically when you perform `idf.py flash` (beware! see
# comment near bottom)
idf_build_get_property(idf_path IDF_PATH)
idf_build_get_property(python PYTHON)
idf_build_get_property(project_dir PROJECT_DIR)
idf_build_get_property(build_dir BUILD_DIR)
partition_table_get_partition_info(nvs_offset "--partition-type data --partition-subtype nvs" "offset")
partition_table_get_partition_info(nvs_size "--partition-type data --partition-subtype nvs" "size")
idf_component_get_property(main_args esptool_py FLASH_ARGS)
idf_component_get_property(sub_args esptool_py FLASH_SUB_ARGS)
set(nvs_initial_file ${build_dir}/nvs_initial.bin)
add_custom_command(OUTPUT ${nvs_initial_file}
COMMAND
${python} ${idf_path}/components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py
generate ${project_dir}/nvs_initial.csv ${nvs_initial_file}
${nvs_size}
DEPENDS
${project_dir}/nvs_initial.csv
)
add_custom_target(gen_nvs_initial ALL DEPENDS ${nvs_initial_file})
add_dependencies(flash gen_nvs_initial)
esptool_py_flash_target(nvs_initial_flash "${main_args}" "${sub_args}")
esptool_py_flash_target_image(nvs_initial_flash nvs_initial "${nvs_offset}" "${nvs_initial_file}")
# Comment the below line if you DO NOT want to automatically flash the NVS partition
# with the other binaries using idf.py flash
esptool_py_flash_target_image(flash nvs_initial "${nvs_offset}" "${nvs_initial_file}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment