Skip to content

Commit d2a8153

Browse files
kilograhamgraham sanderson
and
graham sanderson
authored
RP2040: Allow FreeRTOS to be added to the parent CMake project post initialization of the Pico SDK (#497)
Co-authored-by: graham sanderson <[email protected]>
1 parent 7af41c2 commit d2a8153

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

portable/ThirdParty/GCC/RP2040/CMakeLists.txt

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,31 @@ if (NOT TARGET _FreeRTOS_kernel_inclusion_marker)
2121

2222
pico_is_top_level_project(FREERTOS_KERNEL_TOP_LEVEL_PROJECT)
2323

24-
# The real work gets done in library.cmake which is called at the end of pico_sdk_init
25-
list(APPEND PICO_SDK_POST_LIST_FILES ${CMAKE_CURRENT_LIST_DIR}/library.cmake)
26-
27-
# We need to inject the following header file into ALL SDK files (which we do via the config header)
28-
list(APPEND PICO_CONFIG_HEADER_FILES ${CMAKE_CURRENT_LIST_DIR}/include/freertos_sdk_config.h)
29-
30-
if (FREERTOS_KERNEL_TOP_LEVEL_PROJECT)
31-
message("FreeRTOS: initialize SDK since we're the top-level")
32-
# Initialize the SDK
33-
pico_sdk_init()
24+
# if the SDK has already been initialized, then just add our libraries now - this allows
25+
# this FreeRTOS port to just be added as a sub-directory or include within another project, rather than
26+
# having to include it at the top level before pico_sdk_init()
27+
if (TARGET _pico_sdk_inclusion_marker)
28+
if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.2")
29+
message(FATAL_ERROR "Require at least Raspberry Pi Pico SDK version 1.3.2 to include FreeRTOS after pico_sdk_init()")
30+
endif()
31+
include(${CMAKE_CURRENT_LIST_DIR}/library.cmake)
3432
else()
35-
set(PICO_SDK_POST_LIST_FILES ${PICO_SDK_POST_LIST_FILES} PARENT_SCOPE)
36-
set(PICO_CONFIG_HEADER_FILES ${PICO_CONFIG_HEADER_FILES} PARENT_SCOPE)
37-
set(FREERTOS_KERNEL_PATH ${FREERTOS_KERNEL_PATH} PARENT_SCOPE)
33+
# The real work gets done in library.cmake which is called at the end of pico_sdk_init
34+
list(APPEND PICO_SDK_POST_LIST_FILES ${CMAKE_CURRENT_LIST_DIR}/library.cmake)
35+
if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.2")
36+
# We need to inject the following header file into ALL SDK files (which we do via the config header)
37+
list(APPEND PICO_CONFIG_HEADER_FILES ${CMAKE_CURRENT_LIST_DIR}/include/freertos_sdk_config.h)
38+
endif()
39+
40+
if (FREERTOS_KERNEL_TOP_LEVEL_PROJECT)
41+
message("FreeRTOS: initialize SDK since we're the top-level")
42+
# Initialize the SDK
43+
pico_sdk_init()
44+
else()
45+
set(FREERTOS_KERNEL_PATH ${FREERTOS_KERNEL_PATH} PARENT_SCOPE)
46+
set(PICO_CONFIG_HEADER_FILES ${PICO_CONFIG_HEADER_FILES} PARENT_SCOPE)
47+
set(PICO_SDK_POST_LIST_FILES ${PICO_SDK_POST_LIST_FILES} PARENT_SCOPE)
48+
endif()
3849
endif()
3950
endif()
4051

portable/ThirdParty/GCC/RP2040/README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,28 @@ Note that a FreeRTOS SMP version of this port is also available in the FreeRTOS-
1010

1111
## Using this port
1212

13-
Copy [FreeRTOS-Kernel-import.cmake](FreeRTOS-Kernel-import.cmake) into your project, and
14-
add:
13+
You can copy [FreeRTOS-Kernel-import.cmake](FreeRTOS-Kernel-import.cmake) into your project, and
14+
add the following in your `CMakeLists.txt`:
1515

1616
```cmake
1717
import(FreeRTOS_Kernel_import.cmake)
1818
```
1919

20-
below the usual import of `pico_sdk_import.cmake`
20+
This will locate the FreeRTOS kernel if it is a direct sub-module of your project, or if you provide the
21+
`FREERTOS_KERNEL_PATH` variable in your environment or via `-DFREERTOS_KERNEL_PATH=/path/to/FreeRTOS-Kernel` on the CMake command line.
22+
23+
**NOTE:** If you are using version 1.3.1 or older of the Raspberry Pi Pico SDK then this line must appear before the
24+
`pico_sdk_init()` and will cause FreeRTOS to be included/required in all RP2040 targets in your project. After this SDK
25+
version, you can include the FreeRTOS-Kernel support later in your CMake build (possibly in a subdirectory) and the
26+
FreeRTOS-Kernel support will only apply to those targets which explicitly include FreeRTOS support.
27+
28+
As an alternative to the `import` statement above, you can just add this directory directly via thw following (with
29+
the same placement restrictions related to the Raspberry Pi Pico SDK version above):
30+
31+
```cmake
32+
add_subdirectory(path/to/this/directory FreeRTOS-Kernel)
33+
```
2134

22-
This will find the FreeRTOS kernel if it is a direct sub-module of your project, or if you provide the `FREERTOS_KERNEL_PATH` variable in your environment or via `-DFREERTOS_KERNEL_PATH=/path/to/FreeRTOS-Kernel` on the CMake command line.
2335

2436
## Advanced Configuration
2537

portable/ThirdParty/GCC/RP2040/library.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ target_sources(FreeRTOS-Kernel-Core INTERFACE
1616
)
1717
target_include_directories(FreeRTOS-Kernel-Core INTERFACE ${FREERTOS_KERNEL_PATH}/include)
1818

19+
if (PICO_SDK_VERSION_STRING VERSION_GREATER_EQUAL "1.3.2")
20+
target_compile_definitions(FreeRTOS-Kernel-Core INTERFACE
21+
PICO_CONFIG_RTOS_ADAPTER_HEADER=${CMAKE_CURRENT_LIST_DIR}/include/freertos_sdk_config.h)
22+
endif()
23+
1924
add_library(FreeRTOS-Kernel INTERFACE)
2025
target_sources(FreeRTOS-Kernel INTERFACE
2126
${CMAKE_CURRENT_LIST_DIR}/port.c

0 commit comments

Comments
 (0)