赞
踩
许多树莓派C/C++开发都依靠NMake以及PicoProjectGenerator,安装配置NMake较为麻烦。需要安装VisualStudio 生成工具,并且安装文件夹下有很多版本的nmake需要筛选,暂时没有解决。因此本章主要使用windows中常用编译C/C++的mingw工具链(包括了mingw下的gcc,make以及gdb等),包括MinGW Makefiles来进行树莓拍Pico C/C++开发。这也沿袭了第一章中的mingw使用,具有一致性。
工具/文件 | 版本 | 备注 |
---|---|---|
系统平台 | windows10 | x86_64bit |
VScode | 任意版本 | 安装C/C++、Cmake和CmakeTools扩展(见第一章) |
cmake -version | 3.24.2 | 添加到环境变量PATH和系统变量PATH中 |
mingw32-make -v | GNU Make 4.2.1 | 添加到环境变量PATH和系统变量PATH中 |
(交叉编译器)arm-none-eabi-gcc -v | gcc version 10.3.1 | 添加到环境变量PATH和系统变量PATH中 |
pico-sdk | github最新 | 一个文件夹,pico开发SDK,包含各种库和接口 |
以上工具和文件安装的具体路径可以参考章末视频,其中pico-sdk和pico-examples使用git可能因为会和github有连接问题,可以直接去github上下载(网址见下)。
https://www.raspberrypi.com/documentation/microcontrollers/c_sdk.html#raspberry-pi-pico-cc-sdk
github下载链接
pico-sdk参考文档:
注意!:安装时即使选择交叉编译器将其自动加入环境变量还是不会加入,因此需要手动加入一下。其中arm-none-eabi-gcc好像不能像gcc那样直接单用,会出现一系列路径报错(win和linux都是这样),需要结合cmake可以正常使用。
参考第一章5.4小节配置方法
设置PICO_SDK_PATH的值为下载下来的pico-sdk位置,这两个Environment都如此设置
设置Generator为MinGW Makefiles(这样会使用MinGW下的make工具)
配置MinGW的bin目录
至此配置完毕
新建本工程文件夹pico_project
在VScode中打开,并在其中新建code文件夹,CMakelists和pico_sdk_import.cmake文件,如下图
其中,pico_sdk_import.cmake是从pico_example文件中复制的,作用是cmake的时候将pico_sdk文件加进来,和前述PICO_SDK_PATH共同起作用。
pico_sdk_import.cmake
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake # This can be dropped into an external project to help locate this SDK # It should be include()ed prior to project() if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH)) set(PICO_SDK_PATH $ENV{PICO_SDK_PATH}) message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')") endif () if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT)) set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT}) message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')") endif () if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH)) set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH}) message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')") endif () set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK") 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") set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK") if (NOT PICO_SDK_PATH) if (PICO_SDK_FETCH_FROM_GIT) include(FetchContent) set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR}) if (PICO_SDK_FETCH_FROM_GIT_PATH) get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}") endif () # GIT_SUBMODULES_RECURSE was added in 3.17 if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0") FetchContent_Declare( pico_sdk GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk GIT_TAG master GIT_SUBMODULES_RECURSE FALSE ) else () FetchContent_Declare( pico_sdk GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk GIT_TAG master ) endif () if (NOT pico_sdk) message("Downloading Raspberry Pi Pico SDK") FetchContent_Populate(pico_sdk) set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR}) endif () set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE}) else () message(FATAL_ERROR "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git." ) endif () endif () get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") if (NOT EXISTS ${PICO_SDK_PATH}) message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found") endif () set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake) if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE}) message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK") endif () set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE) include(${PICO_SDK_INIT_CMAKE_FILE})
CMakeLists 这是最外层那个CmakeLists
cmake_minimum_required(VERSION 3.12)
# Pull in SDK (must be before project)
include(pico_sdk_import.cmake)
project(pico_examples C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
##add following sentences
pico_sdk_init()
add_subdirectory(code)
创建好以上两个文件并修改内容之后,保存,我们进入code文件夹。在code下面创建如下文件,分别是Blink.cpp和CMakeLists.txt,其内容见下。
Blink.cpp
#include "pico/stdlib.h"
int main(){
gpio_init(25);
gpio_set_dir(25,GPIO_OUT);
while(1){
gpio_put(25,1);
sleep_ms(1000);
gpio_put(25,0);
sleep_ms(1000);
}
return 1;
}
CMakeLists 这是内层那个CmakeLists,由外层所索引
add_executable(code Blink.cpp)
target_link_libraries(code pico_stdlib)
pico_add_extra_outputs(code)
至此文件创建完毕
可以像第一章5.5小节那样,使用cmake quick start和cmake configure指令
而本次使用另一种方法,点击左侧cmake插件,再点击配置所有文件按钮,这时会自动生成build文件夹,当然记得选择编译器为我们之前下载的交叉编译器arm-none-eabi-gcc
配置所有文件
确认没有问题之后,进行build,同样有两种方法
或者
选择uf2文件
树莓派pico接入电脑
直接把uf2拖到树莓派硬盘中
发现树莓派pico进行亮灯
当然,到这里还没完
我们上传的uf2是固件,树莓派烧录完固件后自动弹出,然后再也找不到树莓派的盘了,想要更换uf2文件怎么办?
开发手册上有说:重新烧录的方法是按住BOOTSEL的同时插上USB,这时就会回到U盘模式,可以为其重新烧录uf2文件
完美结束!
预告:下一章是在linux上尝试树莓派开发
https://www.youtube.com/watch?v=BAoTBg8MJJ4
特别鸣谢!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。