[RP2040]搭建第一个自定义项目

搭建第一个自定义项目

前面我们已经搭建好了RP PICOubuntu开发环境,在RP2040C_SDK下编译了它的example项目,这一节我们参考它的example项目,创建我们的第一个工程。(默认大家都是了解CMake的使用前提下)

一、分析example项目的配置

项目构建是从当前目录的CMakeLists.txt开始, 我们找出其中与RP 2040构建相关的必要项分析

1. 项目根目录CMakeLists.txt

/home/hongxi/RP2040/raspberry_pi_pico/pico-examples/CMakeLists.txt

cmake_minimum_required(VERSION 3.12)
# 1
# Pull in SDK (must be before project)
include(pico_sdk_import.cmake)
# 2
include(pico_extras_import_optional.cmake)
# 3
project(pico_examples C CXX ASM)
# 4
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
# 5
if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")
    message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
endif()
# 6
set(PICO_EXAMPLES_PATH ${PROJECT_SOURCE_DIR})
# 7
# Initialize the SDK
pico_sdk_init()
# 8
include(example_auto_set_url.cmake)
# 9
# Add blink example
add_subdirectory(blink)

# Add hello world example
add_subdirectory(hello_world)
# 10
add_compile_options(-Wall
        -Wno-format          # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int
        -Wno-unused-function # we have some for the docs that aren't called
        )
if (CMAKE_C_COMPILER_ID STREQUAL "GNU") # 12
    add_compile_options(-Wno-maybe-uninitialized)
endif()
# 11
# Hardware-specific examples in subdirectories:
add_subdirectory(adc)
add_subdirectory(clocks)
add_subdirectory(cmake)
add_subdirectory(divider)
add_subdirectory(dma)
add_subdirectory(flash)
add_subdirectory(gpio)
add_subdirectory(i2c)
add_subdirectory(interp)
add_subdirectory(multicore)
add_subdirectory(picoboard)
add_subdirectory(pico_w)
add_subdirectory(pio)
add_subdirectory(pwm)
add_subdirectory(reset)
add_subdirectory(rtc)
add_subdirectory(spi)
add_subdirectory(system)
add_subdirectory(timer)
add_subdirectory(uart)
add_subdirectory(usb)
add_subdirectory(watchdog)

  • 1引用pico_sdk_import.cmake, 导入RP2040 C_SDK路径
  • 2引用pico_extras_import_optional.cmake, 引用这个文件中的方法,用于构建出bin,elf, uf2等格式的目标产物

注意:12都必须在3(project命令)前定义

  • 3设置项目名称

  • 45是定义使用的CMake用到的C和CPP的标准和版本,这两个是CMake的特殊变量

  • 6是定义项目的路径,PROJECT_SOURCE_DIR是CMake的特殊变量

  • 7是SDK初始化方法

  • 8引用example_auto_set_url.cmake, 这个用于生成GitHub链接,方便阅读对应源码,实际上我们项目不需要这个功能

  • 9添加子目录blink,子目录要求存在CMakeLists.txt

  • 10编译选项

  • 119

2. blink目录CMakeLists.txt

以blink功能为例子,看下子目录的CMakeLists.txt构成

/home/hongxi/RP2040/raspberry_pi_pico/pico-examples/blink/CMakeLists.txt

add_executable(blink
        blink.c
        )

# pull in common dependencies
target_link_libraries(blink pico_stdlib) # 1

# create map/bin/hex file etc.
pico_add_extra_outputs(blink) # 2

# add url via pico_set_program_url
example_auto_set_url(blink) # 3

  • 1 blink用到了SDK中的stdlib库,所以在这里来引入, 如果使用其他的库也需要导入
  • 2使用pico_add_extra_outputs构建出bin,elf, uf2等格式的目标产物
  • 3example_auto_set_url这个用于生成GitHub链接,方便阅读对应源码,实际上我们项目不需要这个功能

从上面两个CMakeLists.txt基本就知道构建我们的项目需要哪些文件和配置了,下面就开始构建我们项目

二、构建自定义项目

pico-examples同级目录(SDK下)创建我们的项目的目录:

mkdir my_project && cd my_project 
cp ../pico-examples/CMakeLists.txt .
cp ../pico-examples/example_auto_set_url.cmake .
cp ../pico-examples/pico_extras_import_optional.cmake .
cp ../pico-examples/pico_sdk_import.cmake .

mkdir led && cd led
cp ../pico-examples/blink/blink.c ./led.c
cp ../pico-examples/blink/CMakeLists.txt .

raspberry_pi_pico/my_project/CMakeLists.txt

cmake_minimum_required(VERSION 3.12)

# Pull in SDK (must be before project)
include(pico_sdk_import.cmake)

include(pico_extras_import_optional.cmake)

project(pico_my_project C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")
    message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
endif()

set(PICO_MY_PROJECT_PATH ${PROJECT_SOURCE_DIR})

# Initialize the SDK
pico_sdk_init()

include(example_auto_set_url.cmake)

add_compile_options(-Wall
        -Wno-format          # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int
        -Wno-unused-function # we have some for the docs that aren't called
        )
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
    add_compile_options(-Wno-maybe-uninitialized)
endif()

# sub-dir:
add_subdirectory(led)


raspberry_pi_pico/my_project/led/CMakeLists.txt

add_executable(led
        led.c
        )

# pull in common dependencies
target_link_libraries(led pico_stdlib)

# create map/bin/hex file etc.
pico_add_extra_outputs(led)

三、构建

cd my_project
mkdir build && cd build
cmake ..
cd led
make

同级目录下就会看到构建产物:

hongxi@hongxi-virtual-machine:~/RP2040/raspberry_pi_pico/my_project/build/led$ ls -al
总计 560
drwxrwxr-x 4 hongxi hongxi   4096  5月  4 23:38 .
drwxrwxr-x 9 hongxi hongxi   4096  5月  4 23:38 ..
drwxrwxr-x 4 hongxi hongxi   4096  5月  4 23:38 CMakeFiles
-rw-rw-r-- 1 hongxi hongxi   1221  5月  4 23:32 cmake_install.cmake
drwxrwxr-x 4 hongxi hongxi   4096  5月  4 23:15 elf2uf2
-rwxrwxr-x 1 hongxi hongxi   8540  5月  4 23:33 led.bin
-rw-rw-r-- 1 hongxi hongxi 156009  5月  4 23:33 led.dis
-rwxrwxr-x 1 hongxi hongxi  30216  5月  4 23:33 led.elf
-rw-rw-r-- 1 hongxi hongxi 200384  5月  4 23:33 led.elf.map
-rw-rw-r-- 1 hongxi hongxi  24086  5月  4 23:33 led.hex
-rw-rw-r-- 1 hongxi hongxi  17408  5月  4 23:33 led.uf2
-rw-rw-r-- 1 hongxi hongxi  98815  5月  4 23:32 Makefile
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坂田民工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值