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.

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