Raspberry Pi Pico 在 macOS 上的开发环境搭建

Raspberry Pi Pico 是树莓派基金推出的第一款 MCU 控制级别的开发版,基于 ARM Cortex-M0+ 内核,想把计算平台覆盖再往下沉一级。

规格参数

  • 双核 Arm Cortex-M0 + @ 133MHz
  • 芯片内置 264KB SRAM 和 2MB 的板载闪存
  • 通过专用 QSPI 总线支持最高 16MB 的片外闪存
  • DMA 控制器
  • 30 个 GPIO 引脚,其中 4 个可用作模拟输入
  • 2 个 UART、2 个 SPI 控制器和 2 个 I2C 控制器
  • 16 个 PWM 通道
  • USB 1.1 主机和设备支持
  • 8 个树莓派可编程 I/O(PIO)状态机,用于自定义外围设备支持
  • 支持 UF2 的 USB 大容量存储启动模式,用于拖放式编程

Hello World

下载安装 sdk 和 example

(base) ➜  pico 
(base) ➜  pico git clone https://github.com/raspberrypi/pico-sdk
(base) ➜  pico cd pico-sdk
(base) ➜  pico git submodule update --init     ## 下载上级依赖
(base) ➜  pico cd ..
(base) ➜  pico git clone -b master https://github.com/raspberrypi/pico-examples.git   ## 实例代码
(base) ➜  pico ls
pico-examples pico-sdk       # 目录结构

然后进入 blink 例程目录,设置环境变量,以及尝试编译固件

(base) ➜  pico cd pico-examples/blink
(base) ➜  blink git:(master) cmake ..
Using PICO_SDK_PATH from environment ('../../pico-sdk')
PICO_SDK_PATH is /Users/mintisan/Workplace/pico/pico-sdk
Defaulting PICO_PLATFORM to rp2040 since not specified.
Defaulting PICO platform compiler to pico_arm_gcc since not specified.
-- Defaulting build type to 'Release' since not specified.
PICO compiler is pico_arm_gcc
CMake Error at /Users/mintisan/Workplace/pico/pico-sdk/cmake/preload/toolchains/find_compiler.cmake:28 (message):
  Compiler 'arm-none-eabi-gcc' not found, you can specify search path with
  "PICO_TOOLCHAIN_PATH".
Call Stack (most recent call first):
  /Users/mintisan/Workplace/pico/pico-sdk/cmake/preload/toolchains/pico_arm_gcc.cmake:20 (pico_find_compiler)
  /opt/homebrew/Cellar/cmake/3.25.1/share/cmake/Modules/CMakeDetermineSystem.cmake:124 (include)
  CMakeLists.txt:6 (project)


CMake Error: CMake was unable to find a build program corresponding to "Unix Makefiles".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_ASM_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!

如果提示以上错误,则需要安装工具链

brew install cmake
brew tap ArmMbed/homebrew-formulae
brew install arm-none-eabi-gcc

待安装结束,查看版本号,以及重新cmake

(base) ➜  blink git:(master) ✗ arm-none-eabi-gcc -v
Using built-in specs.
COLLECT_GCC=arm-none-eabi-gcc
COLLECT_LTO_WRAPPER=/opt/homebrew/Cellar/arm-none-eabi-gcc/10.3-2021.07/gcc/bin/../lib/gcc/arm-none-eabi/10.3.1/lto-wrapper
Target: arm-none-eabi
Configured with: /tmp/jenkins-GCC-10-pipeline-259_20210727_1627345004/src/gcc/configure --target=arm-none-eabi --prefix=/tmp/jenkins-GCC-10-pipeline-259_20210727_1627345004/install-native --libexecdir=/tmp/jenkins-GCC-10-pipeline-259_20210727_1627345004/install-native/lib --infodir=/tmp/jenkins-GCC-10-pipeline-259_20210727_1627345004/install-native/share/doc/gcc-arm-none-eabi/info --mandir=/tmp/jenkins-GCC-10-pipeline-259_20210727_1627345004/install-native/share/doc/gcc-arm-none-eabi/man --htmldir=/tmp/jenkins-GCC-10-pipeline-259_20210727_1627345004/install-native/share/doc/gcc-arm-none-eabi/html --pdfdir=/tmp/jenkins-GCC-10-pipeline-259_20210727_1627345004/install-native/share/doc/gcc-arm-none-eabi/pdf --enable-languages=c,c++ --enable-plugins --disable-decimal-float --disable-libffi --disable-libgomp --disable-libmudflap --disable-libquadmath --disable-libssp --disable-libstdcxx-pch --disable-nls --disable-shared --disable-threads --disable-tls --with-gnu-as --with-gnu-ld --with-newlib --with-headers=yes --with-python-dir=share/gcc-arm-none-eabi --with-sysroot=/tmp/jenkins-GCC-10-pipeline-259_20210727_1627345004/install-native/arm-none-eabi --build=x86_64-apple-darwin10 --host=x86_64-apple-darwin10 --with-gmp=/tmp/jenkins-GCC-10-pipeline-259_20210727_1627345004/build-native/host-libs/usr --with-mpfr=/tmp/jenkins-GCC-10-pipeline-259_20210727_1627345004/build-native/host-libs/usr --with-mpc=/tmp/jenkins-GCC-10-pipeline-259_20210727_1627345004/build-native/host-libs/usr --with-isl=/tmp/jenkins-GCC-10-pipeline-259_20210727_1627345004/build-native/host-libs/usr --with-libelf=/tmp/jenkins-GCC-10-pipeline-259_20210727_1627345004/build-native/host-libs/usr --with-host-libstdcxx='-static-libgcc -Wl,-lstdc++ -lm' --with-pkgversion='GNU Arm Embedded Toolchain 10.3-2021.07' --with-multilib-list=rmprofile,aprofile
Thread model: single
Supported LTO compression algorithms: zlib
gcc version 10.3.1 20210621 (release) (GNU Arm Embedded Toolchain 10.3-2021.07)
(base) ➜  blink git:(master) ✗ cmake ..
PICO_SDK_PATH is /Users/mintisan/Workplace/pico/pico-sdk
PICO platform is rp2040.
-- The C compiler identification is GNU 10.3.1
-- The CXX compiler identification is GNU 10.3.1
-- The ASM compiler identification is GNU
-- Found assembler: /opt/homebrew/bin/arm-none-eabi-gcc
Build type is Release
Defaulting PICO target board to pico since not specified.
Using board configuration from /Users/mintisan/Workplace/pico/pico-sdk/src/boards/include/boards/pico.h
-- Found Python3: /opt/homebrew/Frameworks/Python.framework/Versions/3.11/bin/python3.11 (found version "3.11.0") found components: Interpreter
TinyUSB available at /Users/mintisan/Workplace/pico/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040; enabling build support for USB.
cyw43-driver available at /Users/mintisan/Workplace/pico/pico-sdk/lib/cyw43-driver
lwIP available at /Users/mintisan/Workplace/pico/pico-sdk/lib/lwip
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/mintisan/Workplace/pico/pico-examples/blink

进入cmake生成的blink目录,再进行make生成二进制固件

(base) ➜  blink git:(master) ✗ ls
CMakeCache.txt      blink               divider             gpio                pico-sdk            pwm                 timer
CMakeFiles          blink.c             dma                 hello_world         pico_w              reset               uart
CMakeLists.txt      clocks              elf2uf2             i2c                 picoboard           rtc                 usb
Makefile            cmake               flash               interp              pio                 spi                 watchdog
adc                 cmake_install.cmake generated           multicore           pioasm              system
(base) ➜  blink git:(master) ✗ cd blink
(base) ➜  blink git:(master) ✗ ls
CMakeFiles          Makefile            cmake_install.cmake elf2uf2
(base) ➜  blink git:(master) ✗ make -j8
...
[100%] Building C object blink/CMakeFiles/blink.dir/Users/mintisan/Workplace/pico/pico-sdk/src/rp2_common/pico_stdio/stdio.c.obj
[100%] Building C object blink/CMakeFiles/blink.dir/Users/mintisan/Workplace/pico/pico-sdk/src/rp2_common/pico_stdio_uart/stdio_uart.c.obj
[100%] Linking CXX executable blink.elf
[100%] Built target blink
(base) ➜  blink git:(master) ✗ ls -la
total 1032
drwxr-xr-x  12 mintisan  staff     384 Dec 27 12:13 .
drwxr-xr-x  36 mintisan  staff    1152 Dec 27 12:11 ..
drwxr-xr-x   7 mintisan  staff     224 Dec 27 12:13 CMakeFiles
-rw-r--r--   1 mintisan  staff   93875 Dec 27 12:11 Makefile
-rwxr-xr-x   1 mintisan  staff    8288 Dec 27 12:13 blink.bin
-rw-r--r--   1 mintisan  staff  149550 Dec 27 12:13 blink.dis
-rwxr-xr-x   1 mintisan  staff   30412 Dec 27 12:13 blink.elf
-rw-r--r--   1 mintisan  staff  187629 Dec 27 12:13 blink.elf.map
-rw-r--r--   1 mintisan  staff   23374 Dec 27 12:13 blink.hex
-rw-r--r--   1 mintisan  staff   16896 Dec 27 12:13 blink.uf2
-rw-r--r--   1 mintisan  staff    1154 Dec 27 12:11 cmake_install.cmake
drwxr-xr-x   4 mintisan  staff     128 Dec 27 12:11 elf2uf2

得到固件后,可以将 pico 用 usb 连接到电脑,可以看到 RPI-RP2 的一个 U 盘提示,将 blink.uf2 拖入即可【自动弹出-重启-LED闪烁】

(base) ➜  blink git:(master) ✗ lsusb
...
Bus 000 Device 012: ID 2e8a:0003 2e8a RP2 Boot  Serial: E0C912D24340          ####### pico
...

用 SWD 调试固件

编译生成 openocd bin 文件

安装依赖-添加环境变量-配置选项-编译-安装

(base) ➜  pico ls
pico-examples pico-sdk
(base) ➜  pico brew install libtool automake texinfo wget gcc pkg-config libusb
(base) ➜  pico export PATH="/usr/local/opt/texinfo/bin:$PATH"
(base) ➜  pico git clone https://github.com/raspberrypi/openocd.git \
  --branch picoprobe --depth=1
(base) ➜  pico ls
openocd       pico-examples pico-sdk
(base) ➜  pico
(base) ➜  pico cd openocd
(base) ➜  openocd ./bootstrap
(base) ➜  openocd ./configure  --enable-cmsis-dap --enable-cmsis-dap-v2 --enable-picoprobe --disable-werror --enable-stlink --enable-jlink
(base) ➜  openocd make -j4
(base) ➜  openocd make install
(base) ➜  openocd cd ~/git (optional)
(base) ➜  openocd rm -rf openocd (optional)

如果遇到缺少库,brew 安装即可

brew install libusb   ## 不知道为啥一串命令唯独这个没有成功,再来一次即可
brew install hidapi   ## --enable-cmsis-dap 需要

如果遇到 make install 不成功,则加 sudo 即可

(base) ➜  openocd git:(picoprobe) make install
/Library/Developer/CommandLineTools/usr/bin/make  install-recursive
Making install in jimtcl
make[2]: Nothing to be done for `install'.
Making install in src/jtag/drivers/libjaylink
Making install in libjaylink
make[4]: Nothing to be done for `install-exec-am'.
 ././install-sh -c -d '/usr/local/bin'
  /bin/sh ./libtool   --mode=install /usr/bin/install -c src/openocd '/usr/local/bin'
libtool: install: /usr/bin/install -c src/openocd /usr/local/bin/openocd
install: /usr/local/bin/openocd: Permission denied
make[3]: *** [install-binPROGRAMS] Error 71
make[2]: *** [install-am] Error 2
make[1]: *** [install-recursive] Error 1
make: *** [install] Error 2

安装完成可以在任何目录看下版本号

(base) ➜  pico openocd -v
Open On-Chip Debugger 0.11.0-g4f2ae61 (2022-12-27-15:33)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html

在 vs code 中单步调试

参考资料