You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CMakeLists.txt 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # Derived from the Pico SDK, which carries the following
  2. # LICENSE.txt:
  3. # Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd.
  4. #
  5. # Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
  6. # following conditions are met:
  7. #
  8. # 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
  9. # disclaimer.
  10. #
  11. # 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
  12. # disclaimer in the documentation and/or other materials provided with the distribution.
  13. #
  14. # 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
  15. # derived from this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  18. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. cmake_minimum_required(VERSION 3.13)
  25. include(pico_sdk_import.cmake)
  26. project(picowota C CXX ASM)
  27. set(CMAKE_C_STANDARD 11)
  28. set(CMAKE_CXX_STANDARD 17)
  29. pico_sdk_init()
  30. add_executable(picowota
  31. main.c
  32. tcp_comm.c
  33. dhcpserver/dhcpserver.c
  34. ${PICOWOTA_ADDITIONAL_SOURCES}
  35. )
  36. function(target_cl_options option)
  37. target_compile_options(picowota PRIVATE ${option})
  38. target_link_options(picowota PRIVATE ${option})
  39. endfunction()
  40. target_cl_options("-Wall")
  41. target_cl_options("-Os")
  42. target_cl_options("-ffunction-sections")
  43. target_cl_options("-fdata-sections")
  44. target_link_options(picowota PRIVATE "LINKER:--gc-sections")
  45. pico_add_extra_outputs(picowota)
  46. target_include_directories(picowota PRIVATE
  47. ${CMAKE_CURRENT_LIST_DIR} # Needed so that lwip can find lwipopts.h
  48. ${CMAKE_CURRENT_LIST_DIR}/dhcpserver
  49. ${PICOWOTA_ADDITIONAL_INCLUDES}
  50. )
  51. pico_enable_stdio_usb(picowota 1)
  52. add_subdirectory(picowota_reboot)
  53. target_link_libraries(picowota
  54. cmsis_core
  55. hardware_dma
  56. hardware_flash
  57. hardware_resets
  58. hardware_structs
  59. pico_cyw43_arch_lwip_poll
  60. pico_stdlib
  61. pico_sync
  62. pico_util
  63. picowota_reboot
  64. ${PICOWOTA_ADDITIONAL_LIBS}
  65. )
  66. # Retrieves build variables from the environment if present
  67. function(picowota_retrieve_variable name hidden)
  68. if (DEFINED ENV{${name}} AND (NOT ${name}))
  69. set(${name} $ENV{${name}} PARENT_SCOPE)
  70. if (hidden)
  71. set(log_value "hidden")
  72. else()
  73. set(log_value "'$ENV{${name}}'")
  74. endif()
  75. message("Using ${name} from environment (${log_value})")
  76. endif()
  77. endfunction()
  78. picowota_retrieve_variable(PICOWOTA_WIFI_SSID false)
  79. picowota_retrieve_variable(PICOWOTA_WIFI_PASS true)
  80. picowota_retrieve_variable(PICOWOTA_WIFI_AP false)
  81. if ((NOT PICOWOTA_WIFI_SSID) OR (NOT PICOWOTA_WIFI_PASS))
  82. message(FATAL_ERROR
  83. "WiFi SSID/Pass not set, please set PICOWOTA_WIFI_SSID/PICOWOTA_WIFI_PASS."
  84. )
  85. endif ()
  86. # TODO: This causes a full rebuild if they change, configure_file might
  87. # be better.
  88. target_compile_definitions(picowota PUBLIC PICOWOTA_WIFI_SSID=${PICOWOTA_WIFI_SSID})
  89. target_compile_definitions(picowota PUBLIC PICOWOTA_WIFI_PASS=${PICOWOTA_WIFI_PASS})
  90. target_compile_definitions(picowota PUBLIC PICOWOTA)
  91. # Use the WiFi AP mode upon request
  92. if (PICOWOTA_WIFI_AP)
  93. target_compile_definitions(picowota PUBLIC PICOWOTA_WIFI_AP=1)
  94. message("Building in WiFi AP mode.")
  95. endif()
  96. # Provide a helper to build a standalone target
  97. function(picowota_build_standalone NAME)
  98. get_target_property(PICOWOTA_SRC_DIR picowota SOURCE_DIR)
  99. pico_set_linker_script(${NAME} ${PICOWOTA_SRC_DIR}/standalone.ld)
  100. pico_add_bin_output(${NAME})
  101. endfunction()
  102. # Provide a helper to build a combined target
  103. # The build process is roughly:
  104. # 1. Build the bootloader, using a special linker script which leaves
  105. # two sections to be filled in with the header (.app_hdr) and
  106. # app binary (.app_bin)
  107. # 2. Build the app binary, using a special linker script to set the load
  108. # address properly and skip boot2.
  109. # 3. Calculate the checksum of the app binary
  110. # 4. Update the header and binary sections in the ELF from 1.
  111. function(picowota_build_combined NAME)
  112. set(APP_BIN ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.bin)
  113. set(APP_HDR_BIN ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_hdr.bin)
  114. set(COMBINED picowota_${NAME})
  115. get_target_property(PICOWOTA_SRC_DIR picowota SOURCE_DIR)
  116. get_target_property(PICOWOTA_BIN_DIR picowota BINARY_DIR)
  117. get_directory_property(initial_clean ADDITIONAL_MAKE_CLEAN_FILES)
  118. list(APPEND clean_files ${initial_clean})
  119. list(APPEND clean_files ${APP_BIN})
  120. list(APPEND clean_files ${APP_HDR_BIN})
  121. # The app must be built with the correct linker script (and a .bin)
  122. picowota_build_standalone(${NAME})
  123. # Build the bootloader with the sections to fill in
  124. pico_set_linker_script(picowota ${PICOWOTA_SRC_DIR}/bootloader_shell.ld)
  125. add_custom_target(${NAME}_hdr DEPENDS ${NAME} picowota)
  126. add_custom_command(TARGET ${NAME}_hdr
  127. COMMAND ${PICOWOTA_SRC_DIR}/gen_imghdr.py --map ${PICOWOTA_BIN_DIR}/picowota.elf.map --section .app_bin ${APP_BIN} ${APP_HDR_BIN}
  128. )
  129. add_custom_target(${COMBINED} ALL)
  130. add_dependencies(${COMBINED} picowota ${NAME}_hdr)
  131. add_custom_command(TARGET ${COMBINED}
  132. COMMAND ${CMAKE_OBJCOPY}
  133. --update-section .app_hdr=${APP_HDR_BIN}
  134. --update-section .app_bin=${APP_BIN} ${PICOWOTA_BIN_DIR}/picowota.elf ${COMBINED}.elf
  135. )
  136. list(APPEND clean_files ${COMBINED}.bin)
  137. list(APPEND clean_files ${COMBINED}.elf)
  138. add_custom_command(TARGET ${COMBINED} POST_BUILD
  139. COMMAND ${CMAKE_OBJCOPY} -Obinary ${COMBINED}.elf ${COMBINED}.bin
  140. )
  141. if (NOT ELF2UF2_FOUND)
  142. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PICO_SDK_PATH}/tools)
  143. find_package(ELF2UF2)
  144. endif()
  145. if (ELF2UF2_FOUND)
  146. add_custom_command(TARGET ${COMBINED} POST_BUILD
  147. COMMAND ELF2UF2 ${COMBINED}.elf ${COMBINED}.uf2
  148. )
  149. list(APPEND clean_files ${COMBINED}.uf2)
  150. endif()
  151. set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${clean_files}")
  152. endfunction()