1# SPDX-License-Identifier: Apache-2.0
2
3include(ExternalProject)
4
5if(CONFIG_HAS_RPI_PICO)
6  zephyr_library()
7
8  set(rp2_common_dir ${ZEPHYR_HAL_RPI_PICO_MODULE_DIR}/src/rp2_common)
9  set(rp2040_dir ${ZEPHYR_HAL_RPI_PICO_MODULE_DIR}/src/rp2040)
10  set(common_dir ${ZEPHYR_HAL_RPI_PICO_MODULE_DIR}/src/common)
11  set(boot_stage_dir ${rp2_common_dir}/boot_stage2)
12
13  # The Second Stage Bootloader is only linked to the app that resides
14  # at 0x100. Therefore, only if the app's offset is 0x100, the second
15  # stage bootloader should be compiled.
16  if(CONFIG_RP2_REQUIRES_SECOND_STAGE_BOOT)
17    foreach(flash W25Q080 GENERIC_03H IS25LP080 W25X10CL AT25SF128A)
18      if(CONFIG_RP2_FLASH_${flash})
19        set(flash_type ${flash})
20        break()
21      endif()
22    endforeach()
23
24    set(rp2_bootloader_prefix ${CMAKE_BINARY_DIR}/bootloader)
25    set(rp2_bootloader_asm ${rp2_bootloader_prefix}/boot_stage2.S)
26
27    ExternalProject_Add(
28      second_stage_bootloader
29      SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/bootloader
30      BINARY_DIR ${rp2_bootloader_prefix}
31      CMAKE_ARGS
32        -DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}
33        -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
34        -DCMAKE_ASM_COMPILER=${CMAKE_ASM_COMPILER}
35        -DZEPHYR_HAL_RPI_PICO_MODULE_DIR=${ZEPHYR_HAL_RPI_PICO_MODULE_DIR}
36        -DZEPHYR_BASE=${ZEPHYR_BASE}
37        -DFLASH_TYPE=${flash_type}
38        -DPYTHON_EXECUTABLE=${Python3_EXECUTABLE}
39        -DRP2_BOOTLOADER_BYPRODUCT=${rp2_bootloader_asm}
40      INSTALL_COMMAND "" # No installation needed
41      BUILD_BYPRODUCTS ${rp2_bootloader_asm}
42      BUILD_ALWAYS TRUE
43      )
44
45      add_dependencies(${ZEPHYR_CURRENT_LIBRARY} second_stage_bootloader)
46      zephyr_library_sources(${rp2_bootloader_asm})
47  endif()
48
49  # Pico sources and headers necessary for every build.
50  # These contain definitions and implementation used mostly for
51  # initializing the SoC, and therefore are always required.
52
53  zephyr_library_sources(
54    ${rp2_common_dir}/hardware_clocks/clocks.c
55    ${rp2_common_dir}/hardware_pll/pll.c
56    ${rp2_common_dir}/hardware_xosc/xosc.c
57    ${rp2_common_dir}/hardware_watchdog/watchdog.c
58    ${rp2_common_dir}/pico_platform/platform.c
59    ${rp2_common_dir}/pico_bootrom/bootrom.c
60  )
61
62  zephyr_include_directories(
63    ${rp2_common_dir}/hardware_base/include
64    ${rp2_common_dir}/hardware_clocks/include
65    ${rp2_common_dir}/hardware_watchdog/include
66    ${rp2_common_dir}/hardware_xosc/include
67    ${rp2_common_dir}/hardware_pll/include
68    ${rp2_common_dir}/hardware_irq/include
69    ${rp2_common_dir}/hardware_sync/include
70    ${rp2_common_dir}/hardware_timer/include
71    ${rp2_common_dir}/hardware_resets/include
72    ${rp2_common_dir}/pico_bootrom/include
73    ${rp2040_dir}/hardware_regs/include
74    ${rp2040_dir}/hardware_structs/include
75    ${common_dir}/pico_base/include
76    ${rp2_common_dir}/pico_platform/include
77    ${CMAKE_CURRENT_LIST_DIR}
78  )
79
80  zephyr_library_sources_ifdef(CONFIG_PICOSDK_USE_GPIO
81          ${rp2_common_dir}/hardware_gpio/gpio.c)
82  zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_GPIO
83          ${rp2_common_dir}/hardware_gpio/include)
84
85  zephyr_library_sources_ifdef(CONFIG_PICOSDK_USE_UART
86          ${rp2_common_dir}/hardware_uart/uart.c)
87  zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_UART
88          ${rp2_common_dir}/hardware_uart/include)
89
90  zephyr_library_sources_ifdef(CONFIG_PICOSDK_USE_FLASH
91          ${rp2_common_dir}/hardware_flash/flash.c)
92  zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_FLASH
93          ${rp2_common_dir}/hardware_flash/include)
94
95  zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_PWM
96          ${rp2_common_dir}/hardware_pwm/include)
97
98  zephyr_library_sources_ifdef(CONFIG_PICOSDK_USE_ADC
99          ${rp2_common_dir}/hardware_adc/adc.c)
100  zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_ADC
101          ${rp2_common_dir}/hardware_adc/include)
102
103  zephyr_library_sources_ifdef(CONFIG_PICOSDK_USE_DMA
104          ${rp2_common_dir}/hardware_dma/dma.c)
105  zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_DMA
106          ${rp2_common_dir}/hardware_dma/include)
107
108  zephyr_library_sources_ifdef(CONFIG_PICOSDK_USE_CLAIM
109          ${rp2_common_dir}/hardware_claim/claim.c)
110  zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_CLAIM
111          ${rp2_common_dir}/hardware_claim/include)
112
113  # Some flash driver functions must be executed from the RAM.
114  # Originally pico-sdk places them in the RW data section, so this
115  # implementation does the same.
116  zephyr_linker_sources_ifdef(CONFIG_PICOSDK_USE_FLASH RWDATA timecritical.ld)
117
118  # A function in flash.c adds 1 to a function pointer, causing a warning
119  set_source_files_properties(
120    ${rp2_common_dir}/hardware_flash/flash.c
121    PROPERTIES
122    COMPILE_FLAGS $<TARGET_PROPERTY:compiler,warning_no_pointer_arithmetic>
123  )
124
125  zephyr_library_sources_ifdef(CONFIG_PICOSDK_USE_PIO
126          ${rp2_common_dir}/hardware_pio/pio.c)
127  zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_PIO
128          ${rp2_common_dir}/hardware_pio/include)
129
130  zephyr_library_sources_ifdef(CONFIG_PICOSDK_USE_CLAIM
131          ${rp2_common_dir}/hardware_claim/claim.c)
132  zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_CLAIM
133          ${rp2_common_dir}/hardware_claim/include)
134
135endif()
136