• VS Code 搭建stm32开发环境


    MCU免费开发环境

    一般芯片厂家会提供各种开发IDE方案,通常其中就包括其自家的集成IDE,如:

    意法半导体 STM32CubeIDE

    NXP Codewarrior

    TI CCS

    另外也可以用eclipse、VS studio、VS code等搭建开发环境

    VS Code 搭建stm32开发环境

    1.搭建准备

    程序安装

    1.下载并安装 vs code

    2.下载并安装 STM32CubeMX

    支持最新的HAL库,工程代码配置与生成工具,支持生成IAR、Keil、STM32CubeIDE、Makefile等工程,这里使用其生成的Makefile工程。

    3.下载并安装 Git for Windows

    该工具集成有精简版的mingw,这里我们使用其bash终端和版本管理均非常有用。

    4.下载并安装 arm-none-eabi-gcc

    编译器,GUN的arm的通用交叉编译链工具,基本上常用的arm处理器均支持;

    安装时勾选设置全局环境变量以便于配置;

    使用离线免安装包时,解压到合适的位置,在系统环境变量添加in目录,运行CMD或者Windows PowerShell,测试一下是否可用。命令:arm-none-eabi-gcc -v

    5.下载并安装 mingw

    MinGW 的全称是:Minimalist GNU on Windows 。它实际上是将经典的开源 C语言 编译器 GCC 移植到了 Windows 平台下,并且包含了 Win32API 和 MSYS,因此可以将源代码编译生成 Windows 下的可执行程序,又能如同在 Linux 平台下时,使用一些 Windows 不具备的开发工具。

    一句话来概括:MinGW 就是 GCC 的 Windows 版本 。

    其安装一般为在线安装,按网上步骤即可。

    这里我们主要需要使用其 mingw32-make 功能.

    • 离线安装

    如果由于环境不能在线安装,可安装其离线安装包

    MinGW-W64 GCC-8.1.0 x86_64-win32-seh

    下载压缩文件并解压到合适的位置,在系统环境变量添加in目录,运行CMD或者Windows PowerShell,测试一下是否可用。命令:gcc -v

    同时为方便使用,复制 mingw32-make.exe 一份为 make.exe,这样后面编译程序使用 make 即可。

    6.安装mysy2

    shell 命令行开发环境,可用于替代 git-bash、cmd、power shell,功能相对更完善。

    安装之后,在vscode中配置 settings.json--"terminal.integrated.shell.windows": "C:msys64msys2_shell.cmd", 详见下节。

    7.下载并安装(可选) OpenOCD for Windows

    8.Jlink、ST-Link驱动

    9.STM32CubeProg 用于stm32下载程序

    VS Code插件搭建所需

    安装开发所需基础插件(插件在 vs code 拓展栏搜索名称即可)

    • C/C++(必要)

      增加了对C / C ++的语言支持,语法智能感知、加亮及调试功能

    • GBKtoUTF8

    • cortex-debug

    2.工程示例

    2.1 使用Cube-MX 生成Makefile工程

    1.芯片选型、HAL版本、引脚配置、时钟树配置等,可参照下面博客

    https://www.cnblogs.com/silencehuan/p/10904048.html

    2.工程管理中,选择生成makefile工程,然后点击 generate code即可

    在这里插入图片描述

    2.2 vs code配置

    默认情况下,工程下是不含.vscode的文件夹的,修改编辑用户或工程settings.json文件时会自动创建;

    user--settings.json文件参考修改如下(包含配置终端和一些格式等设置):

    {
        "C_Cpp.updateChannel": "Insiders",
        "http.proxySupport": "off",
        "workbench.iconTheme": "vscode-icons", //取消左侧自动聚焦
        "explorer.autoReveal": false,
        "[c]": {
            "editor.defaultFormatter": "ms-vscode.cpptools" //默认格式化工具
        },
        "[h]": {
            "editor.defaultFormatter": "ms-vscode.cpptools" //默认格式化工具
        },
        "editor.formatOnSave": true, //文件保存时自动格式化
        "editor.formatOnPaste": true, //代码粘贴时自动格式化
        "editor.formatOnType": true, //自动格式化键入行
        // "terminal.integrated.shell.windows": "D:\Program Files\Git\git-bash.exe",
        "terminal.integrated.shell.windows": "C:\msys64\msys2_shell.cmd",
        "terminal.integrated.shellArgs.windows": [
            "-defterm",
            "-mingw64",
            "-no-start",
            "-here",
            "-use-full-path"	//使用系统环境变量
        ],
        "terminal.external.windowsExec": "D:\Program Files\Git\git-bash.exe"
    }
    

    或者在菜单中设置 File--Preferences--Settings--Features--Terminal

    2.3 工程makefile

    1.实际开发需要熟悉 makefile

    工程后面添加的文件程序需要由makefile来组织编译;

    打开工程makefile,编译工具指定,默认 PREFIX = arm-none-eabi- ,如果已设置环境变量则无需修改,否则需添加(实际绝对路径路径) GCC_PATH = D:gcc-arm-none-eabi-5_4-2016q3-20160926-win32in

    PREFIX = arm-none-eabi-
    # The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx)
    # either it can be added to the PATH environment variable.
    ifdef GCC_PATH
    CC = $(GCC_PATH)/$(PREFIX)gcc
    AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
    CP = $(GCC_PATH)/$(PREFIX)objcopy
    SZ = $(GCC_PATH)/$(PREFIX)size
    else
    CC = $(PREFIX)gcc
    AS = $(PREFIX)gcc -x assembler-with-cpp
    CP = $(PREFIX)objcopy
    SZ = $(PREFIX)size
    endif
    HEX = $(CP) -O ihex
    BIN = $(CP) -O binary -S
    

    2.4 工程 .vscode json配置(4个)

    1.c_cpp_properties.json

    主要有添加include路径,编译器路径,宏定义等,设置好后索引、编译就跟keil一样方便;

    打开工程 .vscode 下面的 c_cpp_properties.json 配置脚本,这个json不允许有注释,如果你自己编写了头文件又不在workspaceFolder下,路径也要加到includePath和browse里。设置如下:

    {
        "configurations": [
            {
                "name": "Win32",
                "includePath": [
                    "${workspaceFolder}/**",
                    "${workspaceFolder}Drivers/STM32F4xx_HAL_Driver/Inc",
                    "${workspaceFolder}Drivers/STM32F4xx_HAL_Driver/Inc/Legacy",
                    "${workspaceFolder}Drivers/CMSIS/Device/ST/STM32F4xx/Include",
                    "${workspaceFolder}Drivers/CMSIS/Include",
                    "${workspaceFolder}Drivers/CMSIS/Include",
                    "D:/gcc-arm-none-eabi-5_4-2016q3-20160926-win32/arm-none-eabi/include"
                ],
                "defines": [
                    "_DEBUG",
                    "UNICODE",
                    "_UNICODE",
                    "USE_HAL_DRIVER",
                    "STM32F407xx"
                ],
                "compilerPath": "D:\gcc-arm-none-eabi-5_4-2016q3-20160926-win32\bin\arm-none-eabi-gcc.exe",
                "cStandard": "c11",
                "cppStandard": "c++17",
                "intelliSenseMode": "gcc-x86"
            }
        ],
        "version": 4
    }
    

    2.launch.json

    所需要调试的文件的路径、调试时的CWD(工作路径)、调试器的路径及一些调试参数(程序启动参数等);

    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "cwd": "${workspaceRoot}",
                "executable": "./bin/HAL_Test.elf",
                "name": "stm32 Debug",
                "request": "launch",
                "type": "cortex-debug",
                "servertype": "stutil",
                "device": "STM32F407ZG",
                "preLaunchTask": "编译并下载",
                "postDebugTask": "复位设备"
            }
        ]
    }
    

    3.tasks.json

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            {
                "label": "编译",
                "type": "shell",
                "command": "make -j6",
                "problemMatcher": [],
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            },
            {
                "label": "编译并下载",
                "type": "shell",
                "command": "make -j6 && make update",
                "problemMatcher": []
            },
            {
                "label": "重新编译",
                "type": "shell",
                "command": "make clean && make -j6",
                "problemMatcher": []
            },
            {
                "label": "复位设备",
                "type": "shell",
                "command": "STM32_Programmer_CLI -c port=SWD -hardRst",
                "problemMatcher": []
            }
        ]
    }
    

    4.工程下打开终端,输入 make

    工程编程会生成 .bin 文件,这个就是我们要烧录的目标文件。

    5.使用 Jlink 的 Jflash 工具烧录

    6.使用 JLink GDB server 调试,调试方法如同Linux下面的GDB,主要使用命令行

    3. vs code 配置

    1.取消文件自动定位到侧边栏

    当我在右侧点击某个文件时,左侧会自动定位到该文件所在位置,这点特别烦,尤其在项目目录很长的时候。

    在用户 settings.json 中修改

    "explorer.autoReveal": false
    

    2.设置默认终端

    File--Preferences--Settings-- 中打开用户 setting.json文件,修改如下:

    "terminal.integrated.shell.windows": "D:\Program Files\Git\bin\bash.exe",
    "terminal.external.windowsExec": "D:\Program Files\Git\bin\bash.exe",
    

    2.使用插件推荐(根据需求选择)

    插件 功能
    C/C++ C / C ++的语言支持,语法智能感知、加亮及调试功能,当然需要系统安装arm-none-eabi-gcc编译器
    Include Autocomplete 头文件自动匹配
    Code Runner 代码一键运行
    Cortex Debug 提供jlink、stlink等调试接口功能
    filesize 显示文件大小
    Python Python的语言支持,语法智能感知、加亮及调试功能,需要系统安装python
    Git History 查看版本历史及比较
    GitLens 代码中显示提交信息、日志查看方便,同时提供操作图标
    GBKtoUTF8
    ARM arm汇编语言支持
    Bracket Pair Colorizer 彩虹花括号,程序逻辑范围查看方便
    DeviceTree 设备树语法支持
    vscode-icons 文件图标,可快速查看文件类型
    PlatformIO IDE
  • 相关阅读:
    Swoole实战之手撸HttpServer框架 9 使用方法注解 注册路由
    Swoole实战之手撸HttpServer框架 10 利用Bean工厂雏形运行框架【里程碑】
    Markdown小册
    2.13
    12
    131
    1
    @vue/cli的配置知道多少publicPath,outputDir,assetsDir,indexPath,filenameHashing,configureWebpack,productionSourceMap
    笔记本的常见分辨率
    vue/cli中css.sourceMapopeninlinehostporthttpsopenPagecompress devServer.proxy的简单介绍
  • 原文地址:https://www.cnblogs.com/silencehuan/p/11815263.html
Copyright © 2020-2023  润新知