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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. )
  35. function(target_cl_options option)
  36. target_compile_options(picowota PRIVATE ${option})
  37. target_link_options(picowota PRIVATE ${option})
  38. endfunction()
  39. target_cl_options("-Wall")
  40. target_cl_options("-Os")
  41. target_cl_options("-ffunction-sections")
  42. target_cl_options("-fdata-sections")
  43. target_link_options(picowota PRIVATE "LINKER:--gc-sections")
  44. pico_add_extra_outputs(picowota)
  45. target_include_directories(picowota PRIVATE
  46. ${CMAKE_CURRENT_LIST_DIR} # Needed so that lwip can find lwipopts.h
  47. ${CMAKE_CURRENT_LIST_DIR}/dhcpserver)
  48. pico_enable_stdio_usb(picowota 1)
  49. add_subdirectory(picowota_reboot)
  50. target_link_libraries(picowota
  51. cmsis_core
  52. hardware_dma
  53. hardware_flash
  54. hardware_resets
  55. hardware_structs
  56. pico_cyw43_arch_lwip_poll
  57. pico_stdlib
  58. pico_sync
  59. pico_util
  60. picowota_reboot
  61. )
  62. # Retrieves build variables from the environment if present
  63. function(picowota_retrieve_variable name hidden)
  64. if (DEFINED ENV{${name}} AND (NOT ${name}))
  65. set(${name} $ENV{${name}} PARENT_SCOPE)
  66. if (hidden)
  67. set(log_value "hidden")
  68. else()
  69. set(log_value "'$ENV{${name}}'")
  70. endif()
  71. message("Using ${name} from environment (${log_value})")
  72. endif()
  73. endfunction()
  74. picowota_retrieve_variable(PICOWOTA_WIFI_SSID false)
  75. picowota_retrieve_variable(PICOWOTA_WIFI_PASS true)
  76. picowota_retrieve_variable(PICOWOTA_WIFI_AP false)
  77. if ((NOT PICOWOTA_WIFI_SSID) OR (NOT PICOWOTA_WIFI_PASS))
  78. message(FATAL_ERROR
  79. "WiFi SSID/Pass not set, please set PICOWOTA_WIFI_SSID/PICOWOTA_WIFI_PASS."
  80. )
  81. endif ()
  82. # TODO: This causes a full rebuild if they change, configure_file might
  83. # be better.
  84. target_compile_definitions(picowota PUBLIC PICOWOTA_WIFI_SSID=${PICOWOTA_WIFI_SSID})
  85. target_compile_definitions(picowota PUBLIC PICOWOTA_WIFI_PASS=${PICOWOTA_WIFI_PASS})
  86. # Use the WiFi AP mode upon request
  87. if (PICOWOTA_WIFI_AP)
  88. target_compile_definitions(picowota PUBLIC PICOWOTA_WIFI_AP=1)
  89. message("Building in WiFi AP mode.")
  90. endif()
  91. # Provide a helper to build a standalone target
  92. function(picowota_build_standalone NAME)
  93. get_target_property(PICOWOTA_SRC_DIR picowota SOURCE_DIR)
  94. pico_set_linker_script(${NAME} ${PICOWOTA_SRC_DIR}/standalone.ld)
  95. pico_add_bin_output(${NAME})
  96. endfunction()
  97. # Provide a helper to build a combined target
  98. # The build process is roughly:
  99. # 1. Build the bootloader, using a special linker script which leaves
  100. # two sections to be filled in with the header (.app_hdr) and
  101. # app binary (.app_bin)
  102. # 2. Build the app binary, using a special linker script to set the load
  103. # address properly and skip boot2.
  104. # 3. Calculate the checksum of the app binary
  105. # 4. Update the header and binary sections in the ELF from 1.
  106. function(picowota_build_combined NAME)
  107. set(APP_BIN ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.bin)
  108. set(APP_HDR_BIN ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_hdr.bin)
  109. set(COMBINED picowota_${NAME})
  110. get_target_property(PICOWOTA_SRC_DIR picowota SOURCE_DIR)
  111. get_target_property(PICOWOTA_BIN_DIR picowota BINARY_DIR)
  112. # The app must be built with the correct linker script (and a .bin)
  113. picowota_build_standalone(${NAME})
  114. # Build the bootloader with the sections to fill in
  115. pico_set_linker_script(picowota ${PICOWOTA_SRC_DIR}/bootloader_shell.ld)
  116. # TODO: The hard-coded address here is a bit nasty
  117. add_custom_target(${NAME}_hdr DEPENDS ${APP_BIN})
  118. add_custom_command(TARGET ${NAME}_hdr DEPENDS ${APP_BIN}
  119. COMMAND ${PICOWOTA_SRC_DIR}/gen_imghdr.py -a 0x1005B000 ${APP_BIN} ${APP_HDR_BIN}
  120. )
  121. add_custom_target(${COMBINED} ALL)
  122. add_dependencies(${COMBINED} picowota ${NAME}_hdr ${NAME})
  123. add_custom_command(TARGET ${COMBINED} DEPENDS ${APP_HDR_BIN} ${APP_BIN}
  124. COMMAND ${CMAKE_OBJCOPY}
  125. --update-section .app_hdr=${APP_HDR_BIN}
  126. --update-section .app_bin=${APP_BIN} ${PICOWOTA_BIN_DIR}/picowota.elf ${COMBINED}.elf
  127. )
  128. add_custom_command(TARGET ${COMBINED} POST_BUILD
  129. COMMAND ${CMAKE_OBJCOPY} -Obinary ${COMBINED}.elf ${COMBINED}.bin
  130. )
  131. if (NOT ELF2UF2_FOUND)
  132. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PICO_SDK_PATH}/tools)
  133. find_package(ELF2UF2)
  134. endif()
  135. if (ELF2UF2_FOUND)
  136. add_custom_command(TARGET ${COMBINED} POST_BUILD
  137. COMMAND ELF2UF2 ${COMBINED}.elf ${COMBINED}.uf2
  138. )
  139. endif()
  140. endfunction()