CMakeLists.txt 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. cmake_minimum_required(VERSION 3.16)
  2. include(${IDF_PATH}/tools/cmake/idf.cmake)
  3. project(${ULP_APP_NAME} ASM C)
  4. add_executable(${ULP_APP_NAME})
  5. set(CMAKE_EXECUTABLE_SUFFIX ".elf")
  6. option(ULP_COCPU_IS_RISCV "Use RISC-V based ULP" OFF)
  7. option(ULP_COCPU_IS_LP_CORE "Use RISC-V based LP Core" OFF)
  8. message(STATUS "Building ULP app ${ULP_APP_NAME}")
  9. # Check the supported assembler version
  10. if(NOT (ULP_COCPU_IS_RISCV OR ULP_COCPU_IS_LP_CORE))
  11. check_expected_tool_version("esp32ulp-elf" ${CMAKE_ASM_COMPILER})
  12. endif()
  13. set(ULP_MAP_GEN ${PYTHON} ${IDF_PATH}/components/ulp/esp32ulp_mapgen.py)
  14. get_filename_component(sdkconfig_dir ${SDKCONFIG_HEADER} DIRECTORY)
  15. foreach(include ${COMPONENT_INCLUDES})
  16. list(APPEND component_includes -I${include})
  17. endforeach()
  18. list(APPEND ULP_PREPROCESSOR_ARGS ${component_includes})
  19. list(APPEND ULP_PREPROCESSOR_ARGS -I${COMPONENT_DIR})
  20. list(APPEND ULP_PREPROCESSOR_ARGS -I${sdkconfig_dir})
  21. target_include_directories(${ULP_APP_NAME} PRIVATE ${COMPONENT_INCLUDES})
  22. list(APPEND ULP_PREPROCESSOR_ARGS -D__ASSEMBLER__)
  23. # Pre-process the linker script
  24. if(ULP_COCPU_IS_RISCV)
  25. set(ULP_LD_TEMPLATE ${IDF_PATH}/components/ulp/ld/ulp_riscv.ld)
  26. elseif(ULP_COCPU_IS_LP_CORE)
  27. set(ULP_LD_TEMPLATE ${IDF_PATH}/components/ulp/ld/lp_core_riscv.ld)
  28. else()
  29. set(ULP_LD_TEMPLATE ${IDF_PATH}/components/ulp/ld/ulp_fsm.ld)
  30. endif()
  31. get_filename_component(ULP_LD_SCRIPT ${ULP_LD_TEMPLATE} NAME)
  32. add_custom_command(OUTPUT ${ULP_LD_SCRIPT}
  33. COMMAND ${CMAKE_C_COMPILER} -E -P -xc -o ${ULP_LD_SCRIPT} ${ULP_PREPROCESSOR_ARGS} ${ULP_LD_TEMPLATE}
  34. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  35. MAIN_DEPENDENCY ${ULP_LD_TEMPLATE}
  36. DEPENDS ${SDKCONFIG_HEADER}
  37. COMMENT "Generating ${ULP_LD_SCRIPT} linker script..."
  38. VERBATIM)
  39. add_custom_target(ld_script DEPENDS ${ULP_LD_SCRIPT})
  40. add_dependencies(${ULP_APP_NAME} ld_script)
  41. target_link_options(${ULP_APP_NAME} PRIVATE SHELL:-T ${CMAKE_CURRENT_BINARY_DIR}/${ULP_LD_SCRIPT})
  42. # To avoid warning "Manually-specified variables were not used by the project"
  43. set(bypassWarning "${IDF_TARGET}")
  44. if(ULP_COCPU_IS_RISCV)
  45. #risc-v ulp uses extra files for building:
  46. list(APPEND ULP_S_SOURCES
  47. "${IDF_PATH}/components/ulp/ulp_riscv/ulp_core/start.S"
  48. "${IDF_PATH}/components/ulp/ulp_riscv/ulp_core/ulp_riscv_adc.c"
  49. "${IDF_PATH}/components/ulp/ulp_riscv/ulp_core/ulp_riscv_lock.c"
  50. "${IDF_PATH}/components/ulp/ulp_riscv/ulp_core/ulp_riscv_uart.c"
  51. "${IDF_PATH}/components/ulp/ulp_riscv/ulp_core/ulp_riscv_print.c"
  52. "${IDF_PATH}/components/ulp/ulp_riscv/ulp_core/ulp_riscv_i2c.c"
  53. "${IDF_PATH}/components/ulp/ulp_riscv/ulp_core/ulp_riscv_utils.c"
  54. "${IDF_PATH}/components/ulp/ulp_riscv/ulp_core/ulp_riscv_touch.c")
  55. target_link_options(${ULP_APP_NAME} PRIVATE "-nostartfiles")
  56. target_link_options(${ULP_APP_NAME} PRIVATE -Wl,--gc-sections)
  57. target_link_options(${ULP_APP_NAME} PRIVATE -Wl,-Map=${CMAKE_CURRENT_BINARY_DIR}/${ULP_APP_NAME}.map)
  58. target_sources(${ULP_APP_NAME} PRIVATE ${ULP_S_SOURCES})
  59. #Makes the csr utillies for riscv visible:
  60. target_include_directories(${ULP_APP_NAME} PRIVATE "${IDF_PATH}/components/ulp/ulp_riscv/ulp_core/include"
  61. "${IDF_PATH}/components/ulp/ulp_riscv/shared/include")
  62. target_link_options(${ULP_APP_NAME} PRIVATE SHELL:-T ${IDF_PATH}/components/ulp/ld/${IDF_TARGET}.peripherals.ld)
  63. target_link_options(${ULP_APP_NAME} PRIVATE "-Wl,--no-warn-rwx-segments")
  64. target_compile_definitions(${ULP_APP_NAME} PRIVATE IS_ULP_COCPU)
  65. target_compile_definitions(${ULP_APP_NAME} PRIVATE ULP_RISCV_REGISTER_OPS)
  66. elseif(ULP_COCPU_IS_LP_CORE)
  67. list(APPEND ULP_S_SOURCES
  68. "${IDF_PATH}/components/ulp/lp_core/lp_core/start.S"
  69. "${IDF_PATH}/components/ulp/lp_core/lp_core/vector.S"
  70. "${IDF_PATH}/components/ulp/lp_core/shared/ulp_lp_core_memory_shared.c"
  71. "${IDF_PATH}/components/ulp/lp_core/shared/ulp_lp_core_lp_timer_shared.c"
  72. "${IDF_PATH}/components/ulp/lp_core/lp_core/lp_core_startup.c"
  73. "${IDF_PATH}/components/ulp/lp_core/lp_core/lp_core_utils.c"
  74. "${IDF_PATH}/components/ulp/lp_core/lp_core/lp_core_i2c.c"
  75. "${IDF_PATH}/components/hal/uart_hal_iram.c"
  76. "${IDF_PATH}/components/hal/uart_hal.c"
  77. "${IDF_PATH}/components/ulp/lp_core/lp_core/lp_core_uart.c"
  78. "${IDF_PATH}/components/ulp/lp_core/lp_core/lp_core_print.c")
  79. target_link_options(${ULP_APP_NAME} PRIVATE "-nostartfiles")
  80. target_link_options(${ULP_APP_NAME} PRIVATE "-Wl,--no-warn-rwx-segments")
  81. target_link_options(${ULP_APP_NAME} PRIVATE -Wl,--gc-sections)
  82. target_link_options(${ULP_APP_NAME} PRIVATE -Wl,-Map=${CMAKE_CURRENT_BINARY_DIR}/${ULP_APP_NAME}.map)
  83. target_link_options(${ULP_APP_NAME}
  84. PRIVATE SHELL:-T ${IDF_PATH}/components/soc/${IDF_TARGET}/ld/${IDF_TARGET}.peripherals.ld)
  85. target_sources(${ULP_APP_NAME} PRIVATE ${ULP_S_SOURCES})
  86. target_include_directories(${ULP_APP_NAME} PRIVATE "${IDF_PATH}/components/ulp/lp_core/lp_core/include"
  87. "${IDF_PATH}/components/ulp/lp_core/shared/include")
  88. target_compile_definitions(${ULP_APP_NAME} PRIVATE IS_ULP_COCPU)
  89. else()
  90. foreach(ulp_s_source ${ULP_S_SOURCES})
  91. get_filename_component(ulp_ps_source ${ulp_s_source} NAME_WE)
  92. set(ulp_ps_output ${CMAKE_CURRENT_BINARY_DIR}/${ulp_ps_source}.ulp.S)
  93. # Generate preprocessed assembly files.
  94. add_custom_command(OUTPUT ${ulp_ps_output}
  95. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  96. COMMAND ${CMAKE_C_COMPILER} -E -P -xc ${ULP_PREPROCESSOR_ARGS}
  97. -o ${ulp_ps_output} ${ulp_s_source}
  98. DEPENDS ${ulp_s_source}
  99. VERBATIM)
  100. # During assembly file compilation, output listing files as well.
  101. set_source_files_properties(${ulp_ps_output}
  102. PROPERTIES COMPILE_FLAGS
  103. "-al=${CMAKE_CURRENT_BINARY_DIR}/${ulp_ps_source}.lst")
  104. list(APPEND ULP_PS_SOURCES ${ulp_ps_output})
  105. endforeach()
  106. target_link_options(${ULP_APP_NAME} PRIVATE -Map=${CMAKE_CURRENT_BINARY_DIR}/${ULP_APP_NAME}.map)
  107. target_sources(${ULP_APP_NAME} PRIVATE ${ULP_PS_SOURCES})
  108. endif()
  109. if(ULP_COCPU_IS_LP_CORE)
  110. set(ULP_BASE_ADDR "0x0")
  111. else()
  112. set(ULP_BASE_ADDR "0x50000000")
  113. endif()
  114. # Dump the list of global symbols in a convenient format
  115. add_custom_command(OUTPUT ${ULP_APP_NAME}.sym
  116. COMMAND ${CMAKE_NM} -f posix -g $<TARGET_FILE:${ULP_APP_NAME}> > ${ULP_APP_NAME}.sym
  117. DEPENDS ${ULP_APP_NAME}
  118. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  119. # Dump the binary for inclusion into the project
  120. add_custom_command(OUTPUT ${ULP_APP_NAME}.bin
  121. COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${ULP_APP_NAME}> ${ULP_APP_NAME}.bin
  122. DEPENDS ${ULP_APP_NAME}
  123. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  124. add_custom_command(OUTPUT ${ULP_APP_NAME}.ld ${ULP_APP_NAME}.h
  125. COMMAND ${ULP_MAP_GEN} -s ${ULP_APP_NAME}.sym -o ${ULP_APP_NAME} --base ${ULP_BASE_ADDR}
  126. DEPENDS ${ULP_APP_NAME}.sym
  127. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  128. # Building the component separately from the project should result in
  129. # ULP files being built.
  130. add_custom_target(build
  131. DEPENDS ${ULP_APP_NAME} ${ULP_APP_NAME}.bin ${ULP_APP_NAME}.sym
  132. ${CMAKE_CURRENT_BINARY_DIR}/${ULP_APP_NAME}.ld
  133. ${CMAKE_CURRENT_BINARY_DIR}/${ULP_APP_NAME}.h
  134. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})