project_include.cmake 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # fatfs_create_partition_image
  2. #
  3. # Create a fatfs image of the specified directory on the host during build and optionally
  4. # have the created image flashed using `idf.py flash`
  5. function(fatfs_create_partition_image partition base_dir)
  6. set(options FLASH_IN_PROJECT WL_INIT PRESERVE_TIME)
  7. cmake_parse_arguments(arg "${options}" "" "${multi}" "${ARGN}")
  8. idf_build_get_property(idf_path IDF_PATH)
  9. idf_build_get_property(python PYTHON)
  10. if(arg_WL_INIT)
  11. set(fatfsgen_py ${python} ${idf_path}/components/fatfs/wl_fatfsgen.py)
  12. else()
  13. set(fatfsgen_py ${python} ${idf_path}/components/fatfs/fatfsgen.py)
  14. endif()
  15. if(arg_PRESERVE_TIME)
  16. set(default_datetime_option)
  17. else()
  18. set(default_datetime_option --use_default_datetime)
  19. endif()
  20. if("${CONFIG_FATFS_SECTORS_PER_CLUSTER_1}")
  21. set(sectors_per_cluster 1)
  22. elseif("${CONFIG_FATFS_SECTORS_PER_CLUSTER_2}")
  23. set(sectors_per_cluster 2)
  24. elseif("${CONFIG_FATFS_SECTORS_PER_CLUSTER_4}")
  25. set(sectors_per_cluster 4)
  26. elseif("${CONFIG_FATFS_SECTORS_PER_CLUSTER_8}")
  27. set(sectors_per_cluster 8)
  28. elseif("${CONFIG_FATFS_SECTORS_PER_CLUSTER_16}")
  29. set(sectors_per_cluster 16)
  30. elseif("${CONFIG_FATFS_SECTORS_PER_CLUSTER_32}")
  31. set(sectors_per_cluster 32)
  32. elseif("${CONFIG_FATFS_SECTORS_PER_CLUSTER_64}")
  33. set(sectors_per_cluster 64)
  34. elseif("${CONFIG_FATFS_SECTORS_PER_CLUSTER_128}")
  35. set(sectors_per_cluster 128)
  36. endif()
  37. if("${CONFIG_FATFS_SECTOR_512}")
  38. set(fatfs_sector_size 512)
  39. elseif("${CONFIG_FATFS_SECTOR_1024}")
  40. set(fatfs_sector_size 1024)
  41. elseif("${CONFIG_FATFS_SECTOR_2048}")
  42. set(fatfs_sector_size 2048)
  43. else()
  44. set(fatfs_sector_size 4096)
  45. endif()
  46. if("${CONFIG_FATFS_LFN_NONE}")
  47. set(fatfs_long_names_option)
  48. elseif("${CONFIG_FATFS_LFN_HEAP}")
  49. set(fatfs_long_names_option --long_name_support)
  50. elseif("${CONFIG_FATFS_LFN_STACK}")
  51. set(fatfs_long_names_option --long_name_support)
  52. endif()
  53. if("${CONFIG_FATFS_AUTO_TYPE}")
  54. set(fatfs_explicit_type 0)
  55. elseif("${CONFIG_FATFS_FAT12}")
  56. set(fatfs_explicit_type 12)
  57. elseif("${CONFIG_FATFS_FAT16}")
  58. set(fatfs_explicit_type 16)
  59. endif()
  60. get_filename_component(base_dir_full_path ${base_dir} ABSOLUTE)
  61. partition_table_get_partition_info(size "--partition-name ${partition}" "size")
  62. partition_table_get_partition_info(offset "--partition-name ${partition}" "offset")
  63. if("${size}" AND "${offset}")
  64. set(image_file ${CMAKE_BINARY_DIR}/${partition}.bin)
  65. # Execute FATFS image generation; this always executes as there is no way to specify for CMake to watch for
  66. # contents of the base dir changing.
  67. add_custom_target(fatfs_${partition}_bin ALL
  68. COMMAND ${fatfsgen_py} ${base_dir_full_path}
  69. ${fatfs_long_names_option}
  70. ${default_datetime_option}
  71. --partition_size ${size}
  72. --output_file ${image_file}
  73. --sector_size "${fatfs_sector_size}"
  74. --sectors_per_cluster "${sectors_per_cluster}"
  75. --fat_type "${fatfs_explicit_type}"
  76. )
  77. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY
  78. ADDITIONAL_MAKE_CLEAN_FILES
  79. ${image_file})
  80. idf_component_get_property(main_args esptool_py FLASH_ARGS)
  81. idf_component_get_property(sub_args esptool_py FLASH_SUB_ARGS)
  82. # Last (optional) parameter is the encryption for the target. In our
  83. # case, fatfs is not encrypt so pass FALSE to the function.
  84. esptool_py_flash_target(${partition}-flash "${main_args}" "${sub_args}" ALWAYS_PLAINTEXT)
  85. esptool_py_flash_to_partition(${partition}-flash "${partition}" "${image_file}")
  86. add_dependencies(${partition}-flash fatfs_${partition}_bin)
  87. if(arg_FLASH_IN_PROJECT)
  88. esptool_py_flash_to_partition(flash "${partition}" "${image_file}")
  89. add_dependencies(flash fatfs_${partition}_bin)
  90. endif()
  91. else()
  92. set(message "Failed to create FATFS image for partition '${partition}'. "
  93. "Check project configuration if using the correct partition table file.")
  94. fail_at_build_time(fatfs_${partition}_bin "${message}")
  95. endif()
  96. endfunction()
  97. function(fatfs_create_rawflash_image partition base_dir)
  98. set(options FLASH_IN_PROJECT PRESERVE_TIME)
  99. cmake_parse_arguments(arg "${options}" "" "${multi}" "${ARGN}")
  100. if(arg_FLASH_IN_PROJECT)
  101. if(arg_PRESERVE_TIME)
  102. fatfs_create_partition_image(${partition} ${base_dir} FLASH_IN_PROJECT PRESERVE_TIME)
  103. else()
  104. fatfs_create_partition_image(${partition} ${base_dir} FLASH_IN_PROJECT)
  105. endif()
  106. else()
  107. if(arg_PRESERVE_TIME)
  108. fatfs_create_partition_image(${partition} ${base_dir} PRESERVE_TIME)
  109. else()
  110. fatfs_create_partition_image(${partition} ${base_dir})
  111. endif()
  112. endif()
  113. endfunction()
  114. function(fatfs_create_spiflash_image partition base_dir)
  115. set(options FLASH_IN_PROJECT PRESERVE_TIME)
  116. cmake_parse_arguments(arg "${options}" "" "${multi}" "${ARGN}")
  117. if(arg_FLASH_IN_PROJECT)
  118. if(arg_PRESERVE_TIME)
  119. fatfs_create_partition_image(${partition} ${base_dir} FLASH_IN_PROJECT WL_INIT PRESERVE_TIME)
  120. else()
  121. fatfs_create_partition_image(${partition} ${base_dir} FLASH_IN_PROJECT WL_INIT)
  122. endif()
  123. else()
  124. if(arg_PRESERVE_TIME)
  125. fatfs_create_partition_image(${partition} ${base_dir} WL_INIT PRESERVE_TIME)
  126. else()
  127. fatfs_create_partition_image(${partition} ${base_dir} WL_INIT)
  128. endif()
  129. endif()
  130. endfunction()