当前位置:   article > 正文

Raspberry-Pi-PICO系列--第七篇 创建自己的工程_树莓派pico cmakelists

树莓派pico cmakelists

一、目的

        

        细心的朋友可能会发现官方提供了pico-examples仓库,示例代码都是作为一个文件夹的形式放在此仓库下,那么如果我们自己创建一个工程,是不是一定要放在这个仓库下呢?

答案是否定的,我们完全可以自己创建一个文件夹,然后添加源码文件和CMakeLists.txt文件就可以做到。

二、准备

我们先创建一个目录用于存放我们工程

$ mkdir -p ~/workspace/work/pico/blink

添加代码文件后如下

  1. $ tree
  2. .
  3. ├── CMakeLists.txt
  4. ├── main
  5. │   └── main.c
  6. └── pico_sdk_import.cmake
  7. 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文件内容

        

  1. cmake_minimum_required(VERSION 3.13)
  2. include(pico_sdk_import.cmake)
  3. project(blink C CXX ASM)
  4. pico_sdk_init()
  5. add_executable(blink
  6. main/main.c
  7. )
  8. # pull in common dependencies
  9. target_link_libraries(blink pico_stdlib)
  10. # create map/bin/hex file etc.
  11. 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文件 ;

三、实战

        下面我们开始配置工程

  1. $ cd ~/workspace/work/pico/blink
  2. $ mkdir build
  3. $ cd build/
  4. $ cmake ..
  1. Using PICO_SDK_PATH from environment ('/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk')
  2. PICO_SDK_PATH is /Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk
  3. Defaulting PICO_PLATFORM to rp2040 since not specified.
  4. Defaulting PICO platform compiler to pico_arm_gcc since not specified.
  5. -- Defaulting build type to 'Release' since not specified.
  6. PICO compiler is pico_arm_gcc
  7. -- The C compiler identification is GNU 10.3.1
  8. -- The CXX compiler identification is GNU 10.3.1
  9. -- The ASM compiler identification is GNU
  10. -- Found assembler: /Users/你的用户名/workspace/third/raspberry_pi_pico/toolchain/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc
  11. Defaulting PICO target board to pico since not specified.
  12. Using board configuration from /Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/boards/include/boards/pico.h
  13. -- Found Python3: /usr/local/Frameworks/Python.framework/Versions/3.9/bin/python3.9 (found version "3.9.2") found components: Interpreter
  14. TinyUSB available at /Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040; enabling build support for USB.
  15. -- Configuring done
  16. -- Generating done
  17. -- Build files have been written to: /Users/你的用户名/workspace/work/pico/blink/build

       如果需要编译出debug版本,可以执行以下命令

$ cmake  -DCMAKE_BUILD_TYPE=Debug ..

        开始编译

        

$ make

        编译后输出

  1. 你的用户名@bogon:~/workspace/work/pico/blink/build$make
  2. Scanning dependencies of target ELF2UF2Build
  3. [ 1%] Creating directories for 'ELF2UF2Build'
  4. [ 3%] No download step for 'ELF2UF2Build'
  5. [ 5%] No update step for 'ELF2UF2Build'
  6. [ 6%] No patch step for 'ELF2UF2Build'
  7. [ 8%] Performing configure step for 'ELF2UF2Build'
  8. -- The C compiler identification is AppleClang 13.0.0.13000029
  9. -- The CXX compiler identification is AppleClang 13.0.0.13000029
  10. -- Detecting C compiler ABI info
  11. -- Detecting C compiler ABI info - done
  12. -- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
  13. -- Detecting C compile features
  14. -- Detecting C compile features - done
  15. -- Detecting CXX compiler ABI info
  16. -- Detecting CXX compiler ABI info - done
  17. -- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
  18. -- Detecting CXX compile features
  19. -- Detecting CXX compile features - done
  20. -- Configuring done
  21. -- Generating done
  22. -- Build files have been written to: /Users/你的用户名/workspace/work/pico/blink/build/elf2uf2
  23. [ 10%] Performing build step for 'ELF2UF2Build'
  24. Scanning dependencies of target elf2uf2
  25. [ 50%] Building CXX object CMakeFiles/elf2uf2.dir/main.cpp.o
  26. [100%] Linking CXX executable elf2uf2
  27. [100%] Built target elf2uf2
  28. [ 11%] No install step for 'ELF2UF2Build'
  29. [ 13%] Completed 'ELF2UF2Build'
  30. [ 13%] Built target ELF2UF2Build
  31. Scanning dependencies of target bs2_default
  32. [ 15%] Building ASM object pico-sdk/src/rp2_common/boot_stage2/CMakeFiles/bs2_default.dir/compile_time_choice.S.obj
  33. [ 16%] Linking ASM executable bs2_default.elf
  34. [ 16%] Built target bs2_default
  35. Scanning dependencies of target bs2_default_padded_checksummed_asm
  36. [ 18%] Generating bs2_default.bin
  37. [ 20%] Generating bs2_default_padded_checksummed.S
  38. [ 20%] Built target bs2_default_padded_checksummed_asm
  39. Scanning dependencies of target blink
  40. [ 22%] Building C object CMakeFiles/blink.dir/main/main.c.obj
  41. [ 23%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_stdlib/stdlib.c.obj
  42. [ 25%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_gpio/gpio.c.obj
  43. [ 27%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_claim/claim.c.obj
  44. [ 28%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_platform/platform.c.obj
  45. [ 30%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_sync/sync.c.obj
  46. [ 32%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_uart/uart.c.obj
  47. [ 33%] Building ASM object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_divider/divider.S.obj
  48. [ 35%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/common/pico_time/time.c.obj
  49. [ 37%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/common/pico_time/timeout_helper.c.obj
  50. [ 38%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_timer/timer.c.obj
  51. [ 40%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/common/pico_sync/sem.c.obj
  52. [ 42%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/common/pico_sync/lock_core.c.obj
  53. [ 44%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/common/pico_sync/mutex.c.obj
  54. [ 45%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/common/pico_sync/critical_section.c.obj
  55. [ 47%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/common/pico_util/datetime.c.obj
  56. [ 49%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/common/pico_util/pheap.c.obj
  57. [ 50%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/common/pico_util/queue.c.obj
  58. [ 52%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_runtime/runtime.c.obj
  59. [ 54%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_clocks/clocks.c.obj
  60. [ 55%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_irq/irq.c.obj
  61. [ 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
  62. [ 59%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_pll/pll.c.obj
  63. [ 61%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_vreg/vreg.c.obj
  64. [ 62%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_watchdog/watchdog.c.obj
  65. [ 64%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/hardware_xosc/xosc.c.obj
  66. [ 66%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_printf/printf.c.obj
  67. [ 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
  68. [ 69%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_bootrom/bootrom.c.obj
  69. [ 71%] Building ASM object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_divider/divider.S.obj
  70. [ 72%] Building ASM object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_double/double_aeabi.S.obj
  71. [ 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
  72. [ 76%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_double/double_math.c.obj
  73. [ 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
  74. [ 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
  75. [ 81%] Building ASM object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_float/float_aeabi.S.obj
  76. [ 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
  77. [ 84%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_float/float_math.c.obj
  78. [ 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
  79. [ 88%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_malloc/pico_malloc.c.obj
  80. [ 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
  81. [ 91%] Building ASM object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_standard_link/crt0.S.obj
  82. [ 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
  83. [ 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
  84. [ 96%] Building C object CMakeFiles/blink.dir/Users/你的用户名/workspace/third/raspberry_pi_pico/pico-sdk/src/rp2_common/pico_stdio/stdio.c.obj
  85. [ 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
  86. [100%] Linking CXX executable blink.elf
  87. [100%] Built target blink

        我们查看以下build目录下的内容

  1. $ tree -L 1
  2. .
  3. ├── CMakeCache.txt
  4. ├── CMakeFiles
  5. ├── Makefile
  6. ├── blink.bin
  7. ├── blink.dis
  8. ├── blink.elf
  9. ├── blink.elf.map
  10. ├── blink.hex
  11. ├── blink.uf2
  12. ├── cmake_install.cmake
  13. ├── elf2uf2
  14. ├── generated
  15. └── pico-sdk

        其中blink.elf/blink.uf2/blink.bin就是我们的固件文件(只需要烧写其中任何一个)。

关于烧写步骤参考其他博文中的说明,都是一样的过程,此处不再赘述

下篇我们将介绍如何通过SWD烧写和调试程序。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/468081
推荐阅读
相关标签
  

闽ICP备14008679号