Skip to content

Latest commit

 

History

History
263 lines (191 loc) · 8.01 KB

2023-06-21-C1-环境搭建.md

File metadata and controls

263 lines (191 loc) · 8.01 KB

Class 1 环境搭建

【嵌入式小白的学习之路】1.Ubuntu嵌入式开发-环境搭建

【嵌入式小白的学习之路】番外篇:Windows下的环境搭建

视频演示的环境搭建基于 Ubuntu 20.04 LTS 。

众所周知,环境搭建永远是各个与电脑相关的开发中最困难的一步,本文档将会使用保姆级的步骤教你在Ubuntu 20.04 中配置嵌入式开发环境。

环境搭建推荐大家日常使用比较方便的设备。例如,我们在机器人开发中常用的 Ubuntu 系统的 ROS2GO 设备或者 mini PC设备。如果常用的开发设备是 Windows 系统的话,我推荐大家按照文档结尾补充的 Windows 系统下 GCC + OpenOCD + VSCode 的环境进行搭建。

不推荐大家在虚拟机里搭建开发环境(●'◡'●)

第一步、安装GCC

https://developer.arm.com/downloads/-/gnu-rm

  1. 解压GCC
tar -xvjf gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar
  1. 配置环境变量
sudo gedit /etc/profile
  1. 文档末尾输入
export PATH=$PATH:/home/tianbot/arm_stm32_study/arm/bin        
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/tianbot/arm_stm32_study/arm/lib  
  1. 应用环境变量
source /etc/profile
  1. 查看是否安装成功
arm-none-eabi-gcc -v

可能会出现问题,见下可能出现的问题部分的问题1

第二步、安装openocd

  1. 下载xpgk包

https://xpack.github.io/dev-tools/openocd/install/#linux_1

  1. 提取后随便放在哪个路径都行,但是要记得放在哪了
(openocd路径)/openocd -v
  1. 插入dap-link,看是否能查询到设备
(openocd路径)/openocd -f interface/cmsis-dap.cfg 

第三步、安装STM32CubeMX

  1. 在ST官网下载
  2. 生成工程
  3. Toolchain/IDE设置为makefile

第四步、配置VScode 并编译及运行工程

  1. 打开生成的工程
  2. 安装扩展Cortex Debug
  3. 点击小齿轮,配置Cortex Debug设置 setting.json: 安装gcc的目录位置
"cortex-debug.gdbPath": "/home/tianbot/arm_stm32_study/arm/bin/arm-none-eabi-gdb",

安装openocd的目录位置

"cortex-debug.openocdPath.linux": "/usr/bin/openocd"

设置launch.json文件

"configFiles":[
    "interface/cmsis-dap.cfg",        
    "target/stm32f4x.cfg"
]

openocd文件夹下自带的仿真器文件以及处理器的仿真文件 4. 连接调试器与mcu,测试命令

openocd -f interface/cmsis-dap.cfg -f target/stm32f4x.cfg

配置好的launch.json如下:

{
    "version": "0.2.0",
    "configurations": [
        {
            "showDevDebugOutput": "parsed",
            "name": "Cortex Debug STM32F4X",
            "cwd": "${workspaceFolder}",
            "executable": "./build/${workspaceFolderBasename}.elf",
            "request": "launch",
            "type": "cortex-debug",
            "device": "stm32f407",
            "servertype": "openocd",

            "configFiles": [
                "interface/cmsis-dap.cfg",        
                "target/stm32f4x.cfg"
            ]   
        }
    ]
}

第五步、(可选)应用错误波形曲线

偷懒的人可以直接关闭错误波形曲线,但是拥有错误波形曲线会使得我们的调试更加顺利。这一步Windows下和Ubuntu下的配置方式是相同的。

配置步骤如下:

  1. ctrl+shift+p搜错误波形曲线,打开

  2. 鼠标放置在选中的错误波形上,选择Quick Fix - Edit IncludePath 进入UI界面,点击Switch to the c_cpp_properties.json file 超链接进入.json文件

  3. 编辑c_cpp_properties.json 编辑defines为

"defines": [
                "USE_HAL_DRIVER",
                "STM32F407xx"
            ],

"compilerPath"改为arm-none-eabi-gcc的路径

c_cpp_properties.json示例如下

{
    "configurations": [
        {
            "name": "STM32",
            "includePath": [
                "${workspaceFolder}/**"
              ],
            "defines": [
                "USE_HAL_DRIVER",
                "STM32F407xx"
            ],
            "cStandard": "c99",
            "cppStandard": "c++14",
            "intelliSenseMode": "linux-clang-x64",
            "compilerPath": "/home/tianbot/arm_stm32_study/arm/bin/arm-none-eabi-gcc"
        }
    ],
    "version": 4
}

可能出现的问题

问题1

应用环境变量之后,报错缺少对应的libraries

tianbot@ros2go:~/Desktop/LED_TEST$ arm-none-eabi-gdb -v
arm-none-eabi-gdb: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory

解决方案:缺什么下什么,这里下载 libncurses5 依赖

sudo apt-get install libncurses5

问题2

Permission Deny

Experiments with a different CMSIS-DAP show that this is probably a permission issue - openocd does not distinguish between the absence of a CMISIS-DAP programmer vs. lack of permission to access one which is present. The usual solution is to create or install an appropriate udev rule and then restart udev or the system and replug the device.

基本上只出现在 linux 上, windows 是不会有这个的

这个是权限问题。也就是 openocd 没有权限看到、操作调试器。

解决方法:

打开目录 openocd/contrib ,将 dd-openocd.rules 拷贝到 /etc/udev/rules.d/ 下。

sudo cp ./60-openocd.rules  /etc/udev/rules.d

解释一下,该 *.rules 文件是一系列设备描述和权限配置的文件。官方已经给配好调试器的权限。只需要把这个文件放进 udev 的 rules.d 目录下,令其生效即可。有些时候,需要把调试器重新插上去,按照新规则进行挂载。

Ubuntu环境搭建参考资料: https://www.bilibili.com/video/BV12R4y1Q7ic

Windows下配置环境

简单四步,配置 Windows 下 GCC + OpenOCD + VSCode 开发环境

  1. VSCodeCubeMX 的下载安装,直接一路下一步

  2. Openocd xPack下载

    下载 xpack-openocd-0.12.0-1-win32-x64.zip ,随便解压到你认识的地方,接下来的步骤与Ubuntu下相同

  3. GNU Arm Embedded Toolchain Downloads

    下载安装 gcc-arm-none-eabi-10.3-2021.10-win32.exe ,一路下一步,最后勾选 Add path to environment variable

  4. VSCode 扩展中下载 cortex-debug ,在 setting.json 中配置 openocd 、arm-none-eabi-gdb 路径,配置 launch.json (过程和上述 Linux 的类似

    • cortex-debug配置

      点击小齿轮,进入 setting.json

      在最后添加下面 GCC 和 OpenOCD 路径, 记得把路径改为自己的文件路径

          "cortex-debug.gdbPath": "C:\\Program Files (x86)\\GNU Arm Embedded Toolchain\\10 2021.10\\bin\\arm-none-eabi-gdb.exe",
          "cortex-debug.openocdPath.windows": "D:\\Env\\xpack-openocd-0.12.0-1\\bin\\openocd"
    • launch.json 配置(完全与上述Ubuntu的一模一样)

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Cortex Debug",
                "cwd": "${workspaceFolder}",
                "executable": "./build/${workspaceFolderBasename}.elf",
                "request": "launch",
                "type": "cortex-debug",
                "device": "stm32f407",
                "servertype": "openocd",
    
                "configFiles": [
                    "interface/cmsis-dap.cfg",        
                    "target/stm32f4x.cfg"
                ]  
            }
        ]
    }
  5. Windows 下不用特地配置错误波形曲线 😊 但如果出现没有错误波形曲线的问题,可以参考第五步的配置:应用错误波形曲线

大家有任何问题欢迎提出!