project_include.cmake 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # nvs_create_partition_image
  2. #
  3. # Create a NVS image of the specified CSV on the host during build and
  4. # optionally have the created image flashed using `idf.py flash`
  5. function(nvs_create_partition_image partition csv)
  6. set(options FLASH_IN_PROJECT)
  7. set(one VERSION)
  8. set(multi DEPENDS)
  9. cmake_parse_arguments(arg "${options}" "${one}" "${multi}" "${ARGN}")
  10. # Default to version 2
  11. if(NOT DEFINED arg_VERSION)
  12. set(arg_VERSION 2)
  13. endif()
  14. idf_build_get_property(idf_path IDF_PATH)
  15. set(nvs_partition_gen_py
  16. ${PYTHON}
  17. ${idf_path}/components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py
  18. )
  19. get_filename_component(csv_full_path ${csv} ABSOLUTE)
  20. partition_table_get_partition_info(size "--partition-name ${partition}" "size")
  21. partition_table_get_partition_info(offset "--partition-name ${partition}" "offset")
  22. if("${size}" AND "${offset}")
  23. set(image_file ${CMAKE_BINARY_DIR}/${partition}.bin)
  24. add_custom_command(
  25. OUTPUT ${image_file}
  26. COMMAND ${nvs_partition_gen_py} generate --version ${arg_VERSION} ${csv_full_path} ${image_file} ${size}
  27. MAIN_DEPENDENCY ${csv_full_path}
  28. DEPENDS ${arg_DEPENDS}
  29. COMMENT "Generating NVS partition image for ${partition} from ${csv}"
  30. )
  31. add_custom_target(nvs_${partition}_bin ALL DEPENDS ${image_file})
  32. set_property(
  33. DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  34. APPEND
  35. PROPERTY ADDITIONAL_CLEAN_FILES ${image_file}
  36. )
  37. idf_component_get_property(main_args esptool_py FLASH_ARGS)
  38. idf_component_get_property(sub_args esptool_py FLASH_SUB_ARGS)
  39. esptool_py_flash_target(${partition}-flash "${main_args}" "${sub_args}" ALWAYS_PLAINTEXT)
  40. esptool_py_flash_to_partition(${partition}-flash "${partition}" "${image_file}")
  41. add_dependencies(${partition}-flash nvs_${partition}_bin)
  42. if(arg_FLASH_IN_PROJECT)
  43. esptool_py_flash_to_partition(flash "${partition}" "${image_file}")
  44. add_dependencies(flash nvs_${partition}_bin)
  45. endif()
  46. else()
  47. set(message
  48. "Failed to create NVS image for partition '${partition}'. "
  49. "Check project configuration if using the correct partition table file."
  50. )
  51. fail_at_build_time(nvs_${partition}_bin "${message}")
  52. endif()
  53. endfunction()