赞
踩
细心的朋友可能会发现官方提供了pico-examples仓库,示例代码都是作为一个文件夹的形式放在此仓库下,那么如果我们自己创建一个工程,是不是一定要放在这个仓库下呢?
答案是否定的,我们完全可以自己创建一个文件夹,然后添加源码文件和CMakeLists.txt文件就可以做到。
我们先创建一个目录用于存放我们工程
$ mkdir -p ~/workspace/work/pico/blink
添加代码文件后如下
- $ tree
- .
- ├── CMakeLists.txt
- ├── main
- │ └── main.c
- └── pico_sdk_import.cmake
-
- 1 directory, 3 files
main.c文件其实就是pico-examples/blink/blink.c文件
pico_sdk_import.cmake文件是从pico-sdk中拷贝过来的
$ cp $PICO_SDK_PATH/external/pico_sdk_import.cmake .
我们主要看一下CMakeLists.txt文件内容
- cmake_minimum_required(VERSION 3.13)
- include(pico_sdk_import.cmake)
- project(blink C CXX ASM)
- pico_sdk_init()
-
- add_executable(blink
- main/main.c
- )
-
- # pull in common dependencies
- target_link_libraries(blink pico_stdlib)
-
- # create map/bin/hex file etc.
- pico_add_extra_outputs(blink)
其中cmake_minimum_requiresd指定cmake的最低版本;
include指定需要包含pico-sdk里面的cmake依赖文件;
project指定工程名以及编译此工程时涉及到的语言,包括C/C++/ASM;
pico_sdk_init初始化pico sdk;
add_executable指定需要编译的源码文件;
target_link_libraries告诉cmake最终的blink需要链接pico_stdlib库文件;
pico_add_extra_outpus告诉cmake需要生成一些额外的文件,包括map/bin/hex文件以及blink.uf2文件 ;
下面我们开始配置工程
- $ cd ~/workspace/work/pico/blink
- $ mkdir build
- $ cd build/
- $ cmake ..
- Using PICO_SDK_PATH from environment ('/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk')
- PICO_SDK_PATH is /Users/你的用户名/workspace/third/raspberry_pi_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
- -- 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: /Users/你的用户名/workspace/third/raspberry_pi_pico/toolchain/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc
- Defaulting PICO target board to pico since not specified.
- Using board configuration from /Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/boards/include/boards/pico.h
- -- Found Python3: /usr/local/Frameworks/Python.framework/Versions/3.9/bin/python3.9 (found version "3.9.2") found components: Interpreter
- TinyUSB available at /Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040; enabling build support for USB.
- -- Configuring done
- -- Generating done
- -- Build files have been written to: /Users/你的用户名/workspace/work/pico/blink/build
如果需要编译出debug版本,可以执行以下命令
$ cmake -DCMAKE_BUILD_TYPE=Debug ..
开始编译
$ make
编译后输出
- 你的用户名@bogon:~/workspace/work/pico/blink/build$make
- Scanning dependencies of target ELF2UF2Build
- [ 1%] Creating directories for 'ELF2UF2Build'
- [ 3%] No download step for 'ELF2UF2Build'
- [ 5%] No update step for 'ELF2UF2Build'
- [ 6%] No patch step for 'ELF2UF2Build'
- [ 8%] Performing configure step for 'ELF2UF2Build'
- -- The C compiler identification is AppleClang 13.0.0.13000029
- -- The CXX compiler identification is AppleClang 13.0.0.13000029
- -- Detecting C compiler ABI info
- -- Detecting C compiler ABI info - done
- -- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - 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: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
- -- Detecting CXX compile features
- -- Detecting CXX compile features - done
- -- Configuring done
- -- Generating done
- -- Build files have been written to: /Users/你的用户名/workspace/work/pico/blink/build/elf2uf2
- [ 10%] Performing build step for 'ELF2UF2Build'
- Scanning dependencies of target elf2uf2
- [ 50%] Building CXX object CMakeFiles/elf2uf2.dir/main.cpp.o
- [100%] Linking CXX executable elf2uf2
- [100%] Built target elf2uf2
- [ 11%] No install step for 'ELF2UF2Build'
- [ 13%] Completed 'ELF2UF2Build'
- [ 13%] Built target ELF2UF2Build
- Scanning dependencies of target bs2_default
- [ 15%] Building ASM object pico-sdk/src/rp2_common/boot_stage2/CMakeFiles/bs2_default.dir/compile_time_choice.S.obj
- [ 16%] Linking ASM executable bs2_default.elf
- [ 16%] Built target bs2_default
- Scanning dependencies of target bs2_default_padded_checksummed_asm
- [ 18%] Generating bs2_default.bin
- [ 20%] Generating bs2_default_padded_checksummed.S
- [ 20%] Built target bs2_default_padded_checksummed_asm
- Scanning dependencies of target blink
- [ 22%] Building C object CMakeFiles/blink.dir/main/main.c.obj
- [ 23%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_stdlib/stdlib.c.obj
- [ 25%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_gpio/gpio.c.obj
- [ 27%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_claim/claim.c.obj
- [ 28%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_platform/platform.c.obj
- [ 30%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_sync/sync.c.obj
- [ 32%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_uart/uart.c.obj
- [ 33%] Building ASM object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_divider/divider.S.obj
- [ 35%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/common/pico_time/time.c.obj
- [ 37%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/common/pico_time/timeout_helper.c.obj
- [ 38%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_timer/timer.c.obj
- [ 40%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/common/pico_sync/sem.c.obj
- [ 42%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/common/pico_sync/lock_core.c.obj
- [ 44%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/common/pico_sync/mutex.c.obj
- [ 45%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/common/pico_sync/critical_section.c.obj
- [ 47%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/common/pico_util/datetime.c.obj
- [ 49%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/common/pico_util/pheap.c.obj
- [ 50%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/common/pico_util/queue.c.obj
- [ 52%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_runtime/runtime.c.obj
- [ 54%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_clocks/clocks.c.obj
- [ 55%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_irq/irq.c.obj
- [ 57%] Building ASM object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_irq/irq_handler_chain.S.obj
- [ 59%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_pll/pll.c.obj
- [ 61%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_vreg/vreg.c.obj
- [ 62%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_watchdog/watchdog.c.obj
- [ 64%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_xosc/xosc.c.obj
- [ 66%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_printf/printf.c.obj
- [ 67%] Building ASM object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_bit_ops/bit_ops_aeabi.S.obj
- [ 69%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_bootrom/bootrom.c.obj
- [ 71%] Building ASM object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_divider/divider.S.obj
- [ 72%] Building ASM object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_double/double_aeabi.S.obj
- [ 74%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_double/double_init_rom.c.obj
- [ 76%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_double/double_math.c.obj
- [ 77%] Building ASM object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_double/double_v1_rom_shim.S.obj
- [ 79%] Building ASM object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_int64_ops/pico_int64_ops_aeabi.S.obj
- [ 81%] Building ASM object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_float/float_aeabi.S.obj
- [ 83%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_float/float_init_rom.c.obj
- [ 84%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_float/float_math.c.obj
- [ 86%] Building ASM object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_float/float_v1_rom_shim.S.obj
- [ 88%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_malloc/pico_malloc.c.obj
- [ 89%] Building ASM object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_mem_ops/mem_ops_aeabi.S.obj
- [ 91%] Building ASM object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_standard_link/crt0.S.obj
- [ 93%] Building CXX object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_standard_link/new_delete.cpp.obj
- [ 94%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_standard_link/binary_info.c.obj
- [ 96%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_stdio/stdio.c.obj
- [ 98%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_stdio_uart/stdio_uart.c.obj
- [100%] Linking CXX executable blink.elf
- [100%] Built target blink
我们查看以下build目录下的内容
- $ tree -L 1
- .
- ├── CMakeCache.txt
- ├── CMakeFiles
- ├── Makefile
- ├── blink.bin
- ├── blink.dis
- ├── blink.elf
- ├── blink.elf.map
- ├── blink.hex
- ├── blink.uf2
- ├── cmake_install.cmake
- ├── elf2uf2
- ├── generated
- └── pico-sdk
其中blink.elf/blink.uf2/blink.bin就是我们的固件文件(只需要烧写其中任何一个)。
关于烧写步骤参考其他博文中的说明,都是一样的过程,此处不再赘述
下篇我们将介绍如何通过SWD烧写和调试程序。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。