200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 树莓派Pico|SHELL中microPython命令行|pico sdk开发环境搭建|点灯代码|必备开发工具|

树莓派Pico|SHELL中microPython命令行|pico sdk开发环境搭建|点灯代码|必备开发工具|

时间:2021-12-23 03:57:42

相关推荐

树莓派Pico|SHELL中microPython命令行|pico sdk开发环境搭建|点灯代码|必备开发工具|

文章目录

SHELL中microPython命令行SHELL中基于microPython的控制代码Hello Pico 代码SHELL中简单点灯代码SHELL中循环亮灯代码 基于pico sdk开发环境搭建及点灯代码必备开发工具Mingw-w64:著名C/C++编译器GCCarm-none-eabi:交叉编译工具Git:开源的分布式版本控制系统cmake:生成makefilepython:需要3.7.0以上。 安装及设置环境变量git必要的仓库编译准备正式编译以blink(点灯例程)为例,生成文件结构如下: 点灯测试 Tips:查看gcc涉及到的所有include文件目录Tips:SHELL中查看micropython版本及pico类型

SHELL中microPython命令行

SHELL中基于microPython的控制代码

Tips:一般采用Thonny进行控制,可参考博文:

如果是采用其他串口软件进行控制,采用的波特率是115200。

Hello Pico 代码

在 Shell 中输入以下命令,Pico 会显示

Hello, Pico!

print("Hello, Pico!")

SHELL中简单点灯代码

在 Shell 中输入以下命令

from machine import Pin #导入Pin控制库led = Pin(25, Pin.OUT) #初始化ledled.value(1) # 亮灯led.value(0) # 灭灯

SHELL中循环亮灯代码

Pico 将执行三次亮灯灭灯循环,最后输出 Loop End

from machine import Pinimport utimeLED=Pin(25,Pin.OUT)v = 1while v < 4:LED(1)utime.sleep(1)LED(0)utime.sleep(1)v += 1LED(1)utime.sleep(1)LED(0)utime.sleep(1)v += 1LED(1)utime.sleep(1)LED(0)utime.sleep(1)v += 1print('LED closed!') # 输出结束语句

基于pico sdk开发环境搭建及点灯代码

必备开发工具

Mingw-w64:著名C/C++编译器GCC

下载地址:/projects/mingw-w64/files/mingw-w64/mingw-w64-release/

mingw-w64-v11.0.0.zip需要在本地编译。

往下拉,编译好的版本选:x86_64-win32-seh,

下载:x86_64-8.1.0-release-win32-seh-rt_v6-rev0.7z

解压后将bin目录加入系统path即可。

测试命令:mingw32-make -v

正确安装返回值:

GNU Make 4.2.1Built for x86_64-w64-mingw32Copyright (C) 1988- Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later </licenses/gpl.html>This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.

arm-none-eabi:交叉编译工具

下载地址:/downloads/-/arm-gnu-toolchain-downloads

可选择:arm-gnu-toolchain-12.2.MPACBTI-Rel1-mingw-w64-i686-arm-none-eabi.exe

安装:安装时选择将bin路径加入系统path。

测试命令:arm-none-eabi-gcc -v

正确安装返回值:

Using built-in specs.COLLECT_GCC=arm-none-eabi-gccCOLLECT_LTO_WRAPPER=g:/arm-gnu-toolchain/12.2\ mpacbti-rel1/bin/../libexec/gcc/arm-none-eabi/12.2.1/lto-wrapper.exeTarget: arm-none-eabiConfigured with: /data/jenkins/workspace/GNU-toolchain/arm-12-mpacbti/src/gcc/configure --target=arm-none-eabi --prefix=/data/jenkins/workspace/GNU-toolchain/arm-12-mpacbti/build-mingw-arm-none-eabi/install --with-gmp=/data/jenkins/workspace/GNU-toolchain/arm-12-mpacbti/build-mingw-arm-none-eabi/host-tools --with-mpfr=/data/jenkins/workspace/GNU-toolchain/arm-12-mpacbti/build-mingw-arm-none-eabi/host-tools --with-mpc=/data/jenkins/workspace/GNU-toolchain/arm-12-mpacbti/build-mingw-arm-none-eabi/host-tools --with-isl=/data/jenkins/workspace/GNU-toolchain/arm-12-mpacbti/build-mingw-arm-none-eabi/host-tools --disable-shared --disable-nls --disable-threads --disable-tls --enable-checking=release --enable-languages=c,c++,fortran --with-newlib --with-gnu-as --with-gnu-ld --with-sysroot=/data/jenkins/workspace/GNU-toolchain/arm-12-mpacbti/build-mingw-arm-none-eabi/install/arm-none-eabi --with-multilib-list=aprofile,rmprofile --with-libiconv-prefix=/data/jenkins/workspace/GNU-toolchain/arm-12-mpacbti/build-mingw-arm-none-eabi/host-tools --host=i686-w64-mingw32 --with-pkgversion='Arm GNU Toolchain 12.2.MPACBTI-Rel1 (Build arm-12-mpacbti.34)' --with-bugurl=/Thread model: singleSupported LTO compression algorithms: zlibgcc version 12.2.1 0214 (Arm GNU Toolchain 12.2.MPACBTI-Rel1 (Build arm-12-mpacbti.34))

Git:开源的分布式版本控制系统

测试命令:git --version

正确安装返回值:

git version 2.23.0.windows.1

cmake:生成makefile

下载地址:/download/

安装:cmake-3.26.4-windows-x86_64,将bin路径加入系统path。

测试命令:cmake -h

正确安装返回值:

Usagecmake [options] <path-to-source>cmake [options] <path-to-existing-build>cmake [options] -S <path-to-source> -B <path-to-build>Specify a source directory to (re-)generate a build system for it in thecurrent working directory. Specify an existing build directory tore-generate its build system.Options-S <path-to-source>= Explicitly specify a source directory.-B <path-to-build> = Explicitly specify a build directory.-C <initial-cache> = Pre-load a script to populate the cache.-D <var>[:<type>]=<value> = Create or update a cmake cache entry.-U <globbing_expr> = Remove matching entries from CMake cache.-G <generator-name>= Specify a build system generator.-T <toolset-name> = Specify toolset name if supported bygenerator.-A <platform-name> = Specify platform name if supported bygenerator.

python:需要3.7.0以上。

安装及设置环境变量

git必要的仓库

必备库:pico-sdk和pico-examples

执行命令:

cd G:\bsp\RP2040git clone -b master /raspberrypi/pico-sdk.gitcd pico-sdk/git submodule update --initcd ..git clone -b master /raspberrypi/pico-examples.git

建议clone到本地的模板生成库(介绍可参考官方文档):

git clone /raspberrypi/pico-project-generator.git

编译准备

首先添加pico环境变量:

将pico-sdk的根目录路径添加到新建PICO_SDK_PATH中,如:

设置工具链路径,例如:

PICO_TOOLCHAIN_PATH=F:\dev\GNU Tools Arm Embedded\9 -q4-major\bin

执行CMD,并将当前目录变更为进入pico-examples根目录,并在其中新建build子文件夹。

cd pico-examples

mkdir build

cd build

进入build文件夹:

在当前目录下执行:

cmake -G "MinGW Makefiles" ..

执行结果为:

Using PICO_SDK_PATH from environment ('G:\bsp\RP2040\pico-sdk')PICO_SDK_PATH is G:/bsp/RP2040/pico-sdkDefaulting 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-- The C compiler identification is GNU 12.2.1-- The CXX compiler identification is GNU 12.2.1-- The ASM compiler identification is GNU-- Found assembler: G:/arm-gnu-toolchain/12.2 mpacbti-rel1/bin/arm-none-eabi-gcc.exe-- Detecting C compiler ABI info-- Detecting C compiler ABI info - done-- Check for working C compiler: G:/arm-gnu-toolchain/12.2 mpacbti-rel1/bin/arm-none-eabi-gcc.exe - skipped-- Detecting C compile features-- Detecting C compile features - done-- Detecting CXX compiler ABI info-- Detecting CXX compiler ABI info - done-- Check for working CXX compiler: G:/arm-gnu-toolchain/12.2 mpacbti-rel1/bin/arm-none-eabi-g++.exe - skipped-- Detecting CXX compile features-- Detecting CXX compile features - doneBuild type is ReleaseDefaulting PICO target board to pico since not specified.Using board configuration from G:/bsp/RP2040/pico-sdk/src/boards/include/boards/pico.h-- Found Python3: D:/ProgramData/Anaconda3/python.exe (found version "3.9.7") found components: InterpreterTinyUSB available at G:/bsp/RP2040/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040; enabling build support for USB.BTstack available at G:/bsp/RP2040/pico-sdk/lib/btstacklwIP available at G:/bsp/RP2040/pico-sdk/lib/lwipmbedtls available at G:/bsp/RP2040/pico-sdk/lib/mbedtlsSkipping TinyUSB dual examples, as TinyUSB hw/mcu/raspberry_pi/Pico-PIO-USB submodule unavailable-- Configuring done (9.2s)-- Generating done (4.2s)-- Build files have been written to: G:/bsp/RP2040/pico-examples/build

正式编译

默认情况下,SDK将构建针对Raspberry Pi Pico的二进制文件。如果您正在构建一个二进制的不同

board你需要通过 -DPICO_BOARD=board其中board是board的名称。例如,如果你正在建造一个Pico W

您应该传递-DPICO_BOARD=pico_w以及有关您的无线网络的信息

-DWIFI_SSID=“Your Network” -DWIFI_PASSWORD=“Your Password”.

CMake现在为pico-examples树准备了一个构建区。从这里开始,可以键入make来构建所有示例

应用程序。然而,由于我们正在构建blink,我们现在只会通过将directory更改为blink,在键入make之前,请先查看并进入目录。

提示

用-j4调用make将并行运行四个make作业以加快速度。Raspberry Pi 4有4个内核,所以-j4是Raspberry Pi 4的合理的数量。

cd blink

mingw32-make -j 8

make -j 8

-j 4 -j 8的表示用4个或8个线程编译,加快编译速度,可选参数。

如果在build根目录下,则执行全部release编译。

编译显示:

E:\pico\pico-examples\build>mingw32-make -j8Scanning dependencies of target bs2_default[ 0%] Performing build step for 'ELF2UF2Build'[ 0%] Performing build step for 'PioasmBuild'[ 0%] Built target bs2_default[ 0%] Built target bs2_default_padded_checksummed_asmConsolidate compiler generated dependencies of target elf2uf2Consolidate compiler generated dependencies of target pioasm[100%] Built target elf2uf2[100%] Built target pioasm[ 0%] No install step for 'ELF2UF2Build'[ 0%] No install step for 'PioasmBuild'[ 0%] Completed 'ELF2UF2Build'[ 0%] Completed 'PioasmBuild'[ 0%] Built target ELF2UF2Build[ 0%] Built target PioasmBuild

编译完成:

Scanning dependencies of target pio_ws2812Scanning dependencies of target adc_dma_captureScanning dependencies of target pio_ir_loopbackConsolidate compiler generated dependencies of target pio_st7789_lcdConsolidate compiler generated dependencies of target pio_uart_rx_intrConsolidate compiler generated dependencies of target pio_ws2812_parallelConsolidate compiler generated dependencies of target pio_uart_txConsolidate compiler generated dependencies of target pio_uart_rxConsolidate compiler generated dependencies of target pio_ws2812Consolidate compiler generated dependencies of target pio_ir_loopbackConsolidate compiler generated dependencies of target adc_dma_capture[ 93%] Built target pio_uart_rx_intr[ 94%] Built target pio_uart_tx[ 95%] Built target pio_st7789_lcd[ 96%] Built target pio_ws2812_parallel[ 97%] Built target pio_uart_rx[ 98%] Built target pio_ws2812[ 99%] Built target pio_ir_loopback[100%] Built target adc_dma_capture

以blink(点灯例程)为例,生成文件结构如下:

名称大小修改日期CMakeFiles//6/27 14:15:24elf2uf2//6/27 11:23:21blink.bin8.6 kB/6/27 11:26:22blink.dis158 kB/6/27 11:26:22blink.elf30.4 kB/6/27 11:26:21blink.elf.map155 kB/6/27 11:26:21blink.hex24.2 kB/6/27 11:26:22blink.uf217.5 kB/6/27 11:26:22cmake_install.cmake1.2 kB/6/27 11:23:46Makefile73.3 kB/6/27 11:23:46

•blink.Elf,由调试器使用

•blink.uf2,可以拖放到RP2040 USB大容量存储设备上运行

点灯测试

按住BOOT键后上电,电脑弹出虚拟U盘,将.uf2文件拖拽或发送至树莓pico的虚拟U盘中,自动重启并运行下载后的程序。

Tips:查看gcc涉及到的所有include文件目录

以F:\dev\mingw-w64\bin下的gcc.exe作为编译器为例(已加入系统path),

要查看gcc涉及到的所有include文件目录的命令为:

gcc.exe -v -E -x c++ -

E:\pico\pico-examples\build>gcc.exe -v -E -x c++ -Using built-in specs.COLLECT_GCC=gcc.exeTarget: x86_64-w64-mingw32Configured with: ../../../src/gcc-8.1.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw810/x86_64-810-win32-seh-rt_v6-rev0/mingw64 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=win32 --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-win32-seh-rev0, Built by MinGW-W64 project' --with-bugurl=/projects/mingw-w64 CFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-win32-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-win32-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS=' -I/c/mingw810/x86_64-810-win32-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' LDFLAGS='-pipe -fno-ident -L/c/mingw810/x86_64-810-win32-seh-rt_v6-rev0/mingw64/opt/lib -L/c/mingw810/prerequisites/x86_64-zlib-static/lib -L/c/mingw810/prerequisites/x86_64-w64-mingw32-static/lib 'Thread model: win32gcc version 8.1.0 (x86_64-win32-seh-rev0, Built by MinGW-W64 project)COLLECT_GCC_OPTIONS='-v' '-E' '-mtune=core2' '-march=nocona'F:/dev/mingw-w64/bin/../libexec/gcc/x86_64-w64-mingw32/8.1.0/cc1plus.exe -E -quiet -v -iprefix F:/dev/mingw-w64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/ -U_REENTRANT - -mtune=core2 -march=noconaignoring duplicate directory "F:/dev/mingw-w64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++"ignoring duplicate directory "F:/dev/mingw-w64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32"ignoring duplicate directory "F:/dev/mingw-w64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward"ignoring duplicate directory "F:/dev/mingw-w64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/8.1.0/include"ignoring nonexistent directory "C:/mingw810/x86_64-810-win32-seh-rt_v6-rev0/mingw64C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../include"ignoring duplicate directory "F:/dev/mingw-w64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed"ignoring duplicate directory "F:/dev/mingw-w64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"ignoring nonexistent directory "C:/mingw810/x86_64-810-win32-seh-rt_v6-rev0/mingw64/mingw/include"#include "..." search starts here:#include <...> search starts here:F:/dev/mingw-w64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++F:/dev/mingw-w64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32F:/dev/mingw-w64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backwardF:/dev/mingw-w64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/includeF:/dev/mingw-w64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixedF:/dev/mingw-w64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/includeEnd of search list.

Tips:SHELL中查看micropython版本及pico类型

Pi Pico 还是 PICO W?

我在哪个硬件上运行?

用MicroPython编写的软件没有直接的方法可以通过查看硬件来发现它是在Raspberry Pi Pico还是Pico W上运行。然而,你可以通过查看网络功能是否包含在你的特定MicroPython固件中来间接判断:

import networkif hasattr(network, "WLAN"):print("the board has WLAN capabilities")else:print("the board hasn't WLAN!")

通过打印输出是否有wlan功能来判断!

还可以采用sys模块,检查MicroPython固件版本,以检查它是为Raspberry Pi Pico还是Pico W编译的:

>>> import sys>>> sys.implementation(name='micropython', version=(1, 20, 0), _machine='Raspberry Pi Pico with RP2040', _mpy=4358)

树莓派Pico|SHELL中microPython命令行|pico sdk开发环境搭建|点灯代码|必备开发工具|gcc涉及的include文件目录

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。