Skip to content

Commit b0edb40

Browse files
committed
Build for Pico is OK. Not tested. compile passed.
1 parent 3411054 commit b0edb40

File tree

4 files changed

+146
-24
lines changed

4 files changed

+146
-24
lines changed

CMakeLists.txt

+28-16
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,42 @@
11
# CMake 3.15 or newer is requried because of CMP0091.
22
cmake_minimum_required(VERSION "3.15")
33

4-
# The CMake compatibility flag for MSVC runtime library flag. New in 3.15. Must set "NEW"
5-
cmake_policy(SET CMP0091 "NEW")
6-
# Enforce the /MT or /MTd as link option for MS Visual Studio C++
7-
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
8-
9-
10-
#----------------------------------------------------------------------------
11-
# Parameters inside project
12-
project("app")
134

145
# Compiler option
156
# GoogleTest requirest C++11 or later
16-
set(CMAKE_CXX_STANDARD "11")
7+
set(CMAKE_CXX_STANDARD "17")
178
set(CMAKE_CXX_STANDARD_REQUIRED "ON")
18-
set(CMAKE_CXX_EXTENSIONS "OFF")
199

10+
# In the toplevel CMakeLists.txt, we can not use the
11+
# $WIN or $Unix. To avoid this problem, we check the
12+
# Compile path string. If it contains arm-none-eabi,
13+
# We guess this is bare metal target.
14+
15+
# Is compiler arm-none-eabi?
16+
string(FIND ${CMAKE_CXX_COMPILER} "arm-none-eabi" FOUNDPOS)
17+
if( ${FOUNDPOS} GREATER "-1") # If yes, it is pico
18+
message(STATUS "Target is bare metal")
19+
# obtain pico-sdk from GIT
20+
# (note this can come from environment, CMake cache etc)
21+
set(PICO_SDK_FETCH_FROM_GIT on)
22+
23+
# pico_sdk_import.cmake is a single file copied from this SDK
24+
# note: this must happen before project()
25+
include(pico_sdk_import.cmake)
26+
27+
project("app")
28+
29+
# initialize the Raspberry Pi Pico SDK
30+
pico_sdk_init()
31+
else() # WIN32 or Unix
32+
message(STATUS "Target is Windows or Linux")
33+
project("app")
2034

21-
#----------------------------------------------------------------------------
22-
# Do not edit the lines below
35+
# Unit test.
36+
add_subdirectory("test")
2337

24-
# Executable name
25-
set(EXECUTABLE_NAME ${PROJECT_NAME})
38+
endif() # string(FIND CMAKE_CXX_COMPILER "arm-none-eabi") GREATER_EQUAL 0
2639

2740
# Subdirectories
2841
add_subdirectory("src")
29-
add_subdirectory("test")
3042

pico_sdk_import.cmake

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#[[
2+
Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd.
3+
4+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5+
following conditions are met:
6+
7+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8+
disclaimer.
9+
10+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
11+
disclaimer in the documentation and/or other materials provided with the distribution.
12+
13+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
14+
derived from this software without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23+
]]
24+
25+
26+
# Newest version of this file can be found at :
27+
# https://github.com/raspberrypi/pico-sdk/blob/master/external/pico_sdk_import.cmake
28+
29+
# This is a copy of https://github.com/raspberrypi/pico-sdk/blob/1.5.1/external/pico_sdk_import.cmake
30+
31+
# This can be dropped into an external project to help locate this SDK
32+
# It should be include()ed prior to project()
33+
34+
if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
35+
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
36+
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
37+
endif ()
38+
39+
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
40+
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
41+
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
42+
endif ()
43+
44+
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
45+
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
46+
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
47+
endif ()
48+
49+
set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
50+
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
51+
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
52+
53+
if (NOT PICO_SDK_PATH)
54+
if (PICO_SDK_FETCH_FROM_GIT)
55+
include(FetchContent)
56+
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
57+
if (PICO_SDK_FETCH_FROM_GIT_PATH)
58+
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
59+
endif ()
60+
# GIT_SUBMODULES_RECURSE was added in 3.17
61+
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0")
62+
FetchContent_Declare(
63+
pico_sdk
64+
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
65+
GIT_TAG master
66+
GIT_SUBMODULES_RECURSE FALSE
67+
)
68+
else ()
69+
FetchContent_Declare(
70+
pico_sdk
71+
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
72+
GIT_TAG master
73+
)
74+
endif ()
75+
76+
if (NOT pico_sdk)
77+
message("Downloading Raspberry Pi Pico SDK")
78+
FetchContent_Populate(pico_sdk)
79+
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
80+
endif ()
81+
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
82+
else ()
83+
message(FATAL_ERROR
84+
"SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
85+
)
86+
endif ()
87+
endif ()
88+
89+
get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
90+
if (NOT EXISTS ${PICO_SDK_PATH})
91+
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
92+
endif ()
93+
94+
set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
95+
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
96+
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
97+
endif ()
98+
99+
set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)
100+
101+
include(${PICO_SDK_INIT_CMAKE_FILE})

src/CMakeLists.txt

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
add_executable(${PROJECT_NAME} main.cpp)
12

2-
#----------------------------------------------------------------------------
3-
# Do not edit the lines below
3+
target_link_libraries(${PROJECT_NAME}
4+
lib)
45

5-
add_executable(${EXECUTABLE_NAME} main.cpp)
6+
if(NOT(${WIN32}) AND NOT (${UNIX})) # Is Target Pico?
7+
# pull in common dependencies
8+
target_link_libraries(${PROJECT_NAME} pico_stdlib )
69

7-
target_link_libraries(${EXECUTABLE_NAME}
8-
lib)
10+
# create map/bin/hex/uf2 file etc.
11+
pico_add_extra_outputs(${PROJECT_NAME})
12+
endif() # (NOT(${WIN32}) AND NOT (${UNIX}))
913

1014
add_subdirectory(lib)

src/main.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
* @copyright Copyright (c) 2022
99
*
1010
*/
11-
#include <iostream>
11+
#if __has_include(<hardware/gpio.h>)
12+
#include <pico/stdlib.h>
13+
#else
14+
#include <stdlib.h>
15+
#endif
16+
17+
#include <stdio.h>
1218

1319
#include "calc.hpp"
1420

@@ -22,8 +28,7 @@ int main() {
2228

2329
// count from 1 to num
2430
for (int i = 1; i <= 10; i++)
25-
std::cout << "[CalcSqrt] The square root of " << i << " is " << calc.sqrt(i)
26-
<< std::endl;
31+
printf("[CalcSqrt] The square root of %d is %f \n", i, calc.sqrt(i));
2732

2833
return 0;
2934
}

0 commit comments

Comments
 (0)