SDK 设置

要开始使用 C/C++ SDK,请阅读我们的可以帮助你快速上手,开启板载 LED 的亮起功能,并再次将其关闭。文档,其中介绍了如何通过Raspberry Pi Pico 扩展在 VS Code 中进行设置并创建一个简单项目。支持的操作系统信息可在扩展的自述文件中找到。.

然而,如果您打算在32 位 Raspberry Pi OS 或 Raspberry Pi OS Lite 上为 Pico 开发,则我们的 VS Code 扩展不支持这些操作系统。此时,您可以通过在命令行中运行我们的安装脚本,快速搭建 C/C++ 工具链。

  1. 在运行安装脚本之前,请确保您的 Raspberry Pi 上的 Raspberry Pi OS 是最新最新版本。

  2. 请通过以下命令下载并运行安装脚本:

    wget https://raw.githubusercontent.com/raspberrypi/pico-setup/master/pico_setup.sh
    chmod +x pico_setup.sh
    ./pico_setup.sh
  3. 有关更多信息及设置测试说明,请参阅 pico-setup 的自述文件。.

树莓派 Pico C/C++ SDK

我们的官方 C SDK 既可在命令行中使用,也可在 Visual Studio Code 和 CLion 等主流集成开发环境中使用。要开始使用,请下载我们的 C/C++ SDK 和示例,并查看我们的可以帮助你快速上手,开启板载 LED 的亮起功能,并再次将其关闭。文档。如需快速设置,请参阅下一节。

您可以在以下位置找到有关 C/C++ SDK 的文档:

SDK 源代码是开源的,可在pico-sdk Github 仓库中获取.

要使用 C/C++ SDK 为Raspberry Pi Pico 以外的开发板构建应用程序,请传递-DPICO_BOARD=boardname to CMake, where boardname is the name of your board. For example:

  • 要为 Adafruit Feather RP2040 构建应用程序,请传递-DPICO_BOARD=adafruit_feather_rp2040.

  • 要为 Pico W 构建应用程序,请传递-DPICO_BOARD=pico_w (in addition to -DWIFI_SSID="Your Network" -DWIFI_PASSWORD="Your Password", if you want to connect to a wireless network).

更多信息,请参阅boards/ directory在 Raspberry Pi Pico SDK 和论坛.

您的第一个二进制文件

每个人在使用新的微控制器时编写的第一个程序都是让 LED 闪烁。Raspberry Pi Pico 开发板板载有一个 LED。该 LED 连接到GP25 on the board’s microcontroller for Pico and Pico 2, and WL_GPIO0 on the Infineon 43439 wireless chip for Pico W and Pico 2 W.

您可以通过以下方式使其闪烁:

  1. 下载使用 Blink UF2文件。该文件适用于所有 Pico 型号。

  2. 按住 BOOTSEL 按钮,并将您的 Pico 插入 Raspberry Pi 或其他计算机的 USB 端口。

  3. 它会作为名为 RPI-RP2(Pico)或 RP2350(Pico 2)的大容量存储设备被挂载。

  4. 将 Blink UF2 二进制文件拖放到 RPI-RP2 存储卷上。Pico 会自动重启。

如果一切正常,板载 LED 将开始闪烁。

上查看编写您自己的闪烁程序的代码。

下一个程序就是通过 USB 串口连接输出“Hello World”。

  1. 下载“Hello World” UF2.

  2. 按住 BOOTSEL 按钮,并将您的 Pico 插入 Raspberry Pi 或其他计算机的 USB 端口。

  3. 它会作为名为 RPI-RP2(Pico)或 RP2350(Pico 2)的大容量存储设备被挂载。

  4. 将“Hello World”UF2二进制文件拖放到已挂载的存储卷上,Pico 会自动重启。

  5. 打开终端窗口并输入:

    $ sudo apt install minicom
    $ minicom -b 115200 -o -D /dev/ttyACM0

终端中将显示“Hello, world!”。

您可以在以下位置查看编写自定义 hello_world 程序的代码:你可以从 Estefannie 的…

快速启动您自己的项目

以下说明较为简略,且仅适用于 Linux 系统。如需详细步骤、其他平台的说明以及更多内容,我们建议您参阅:开始使用 Raspberry Pi Pico和树莓派 Pico C/C++ SDK相关书籍。

安装 CMake(至少版本 3.13)和 GCC 交叉编译器。

$ sudo apt install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib

通过在本地克隆 SDK,将您的项目配置为使用 Raspberry Pi Pico SDK:

$ git clone https://github.com/raspberrypi/pico-sdk.git

复制external/pico_sdk_import.cmake from the SDK into your project directory

将PICO_SDK_PATH to the SDK location in your environment, or pass it (-DPICO_SDK_PATH=) to cmake later.

设置一个CMakeLists.txt like:

cmake_minimum_required(VERSION 3.13)

# initialise the SDK based on PICO_SDK_PATH
# note: this must happen before project()
include(pico_sdk_import.cmake)

project(my_project)

# initialise the Raspberry Pi Pico SDK
pico_sdk_init()

# rest of your project

接下来就可以编写代码了,详情请参见pico-examples的文章,还是树莓派 Pico C/C++ SDK相关书籍,其中介绍了具体的操作方法。

最简单的做法是使用单个源文件(例如,hello_world.c)

#include <stdio.h>
#include "pico/stdlib.h"

int main() {
    setup_default_uart();
    printf("Hello, world!\n");
    return 0;
}

并在 CMakeLists.txt 中添加如下内容:

add_executable(hello_world
    hello_world.c
)

# Add pico_stdlib library which aggregates commonly used features
target_link_libraries(hello_world pico_stdlib)

# create map/bin/hex/uf2 file in addition to ELF.
pico_add_extra_outputs(hello_world)
本示例使用默认 UART 作为标准输出;如果希望使用默认 USB,请参考 hello-usb 示例。

创建一个 CMake 构建目录。例如,如果不使用 IDE:

$ mkdir build
$ cd build
$ cmake ..

当为除 Raspberry Pi Pico 以外的开发板构建时,需传递-DPICO_BOARD=board_name to the cmake command above, for example, cmake -DPICO_BOARD=pico_w .. to configure the SDK and build options accordingly for that particular board.

这样可以设置各种编译器宏定义(例如,默认 UART 及其他硬件的引脚编号),并在某些情况下启用额外的库支持(例如,为构建PICO_BOARD=pico_w) which cannot be built without a board which provides the requisite functionality.

时启用无线功能)。有关 SDK 中定义的开发板列表,请查看此目录其中包含每个命名开发板的头文件。

请在您创建的构建目录中生成目标。

$ make hello_world

您现在拥有hello_world.elf to load by using a debugger, or hello_world.uf2 that can be installed and run on your Raspberry Pi Pico through drag and drop.