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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. creds.c
  33. tcp_comm.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. # Needed so that lwip can find lwipopts.h
  46. target_include_directories(picowota PRIVATE ${CMAKE_CURRENT_LIST_DIR})
  47. pico_enable_stdio_usb(picowota 1)
  48. add_subdirectory(picowota_reboot)
  49. target_link_libraries(picowota
  50. cmsis_core
  51. hardware_dma
  52. hardware_flash
  53. hardware_resets
  54. hardware_structs
  55. pico_cyw43_arch_lwip_poll
  56. pico_stdlib
  57. pico_sync
  58. pico_util
  59. picowota_reboot
  60. )
  61. # Provide a helper to build a standalone target
  62. function(picowota_build_standalone NAME)
  63. get_target_property(PICOWOTA_SRC_DIR picowota SOURCE_DIR)
  64. pico_set_linker_script(${NAME} ${PICOWOTA_SRC_DIR}/standalone.ld)
  65. pico_add_bin_output(${NAME})
  66. endfunction()
  67. # Provide a helper to build a combined target
  68. # The build process is roughly:
  69. # 1. Build the bootloader, using a special linker script which leaves
  70. # two sections to be filled in with the header (.app_hdr) and
  71. # app binary (.app_bin)
  72. # 2. Build the app binary, using a special linker script to set the load
  73. # address properly and skip boot2.
  74. # 3. Calculate the checksum of the app binary
  75. # 4. Update the header and binary sections in the ELF from 1.
  76. function(picowota_build_combined NAME)
  77. set(APP_BIN ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.bin)
  78. set(APP_HDR_BIN ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_hdr.bin)
  79. set(COMBINED picowota_${NAME})
  80. get_target_property(PICOWOTA_SRC_DIR picowota SOURCE_DIR)
  81. get_target_property(PICOWOTA_BIN_DIR picowota BINARY_DIR)
  82. # The app must be built with the correct linker script (and a .bin)
  83. picowota_build_standalone(${NAME})
  84. # Build the bootloader with the sections to fill in
  85. pico_set_linker_script(picowota ${PICOWOTA_SRC_DIR}/bootloader_shell.ld)
  86. # TODO: The hard-coded address here is a bit nasty
  87. add_custom_target(${NAME}_hdr DEPENDS ${APP_BIN})
  88. add_custom_command(TARGET ${NAME}_hdr DEPENDS ${APP_BIN}
  89. COMMAND ${PICOWOTA_SRC_DIR}/gen_imghdr.py -a 0x1005B000 ${APP_BIN} ${APP_HDR_BIN}
  90. )
  91. add_custom_target(${COMBINED} ALL)
  92. add_dependencies(${COMBINED} picowota ${NAME}_hdr ${NAME})
  93. add_custom_command(TARGET ${COMBINED} DEPENDS ${APP_HDR_BIN} ${APP_BIN}
  94. COMMAND ${CMAKE_OBJCOPY}
  95. --update-section .app_hdr=${APP_HDR_BIN}
  96. --update-section .app_bin=${APP_BIN} ${PICOWOTA_BIN_DIR}/picowota.elf ${COMBINED}.elf
  97. )
  98. add_custom_command(TARGET ${COMBINED} POST_BUILD
  99. COMMAND ${CMAKE_OBJCOPY} -Obinary ${COMBINED}.elf ${COMBINED}.bin
  100. )
  101. if (NOT ELF2UF2_FOUND)
  102. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PICO_SDK_PATH}/tools)
  103. find_package(ELF2UF2)
  104. endif()
  105. if (ELF2UF2_FOUND)
  106. add_custom_command(TARGET ${COMBINED} POST_BUILD
  107. COMMAND ELF2UF2 ${COMBINED}.elf ${COMBINED}.uf2
  108. )
  109. endif()
  110. endfunction()