使用VSCode编译和运行STM32项目

Use VSCode to compile and run STM32 projects

我已经使用Visual Studio和VSCode很长时间了;因此,如果我可以使用它来代替 Eclipse 或 Keil,那就太好了。我应该转向那些 IDE 吗? 我喜欢没有依赖项,我有一个 make 文件可以编译我的代码并将其闪存到 stm32。 此外,我可以自定义很多 VSCode。例如,我有一个名为 highlight 的插件,我有一个正则表达式可以突出显示 USER CODE BEGINUSER CODE END 之间的所有内容。结果我的代码如下所示:

现在我知道我的代码只能存在于高亮区域。这样,如果我使用 CubeMx 进行任何更改,我的代码将不会丢失。

感谢这个视频,我能够调试、编译和刷新 stm32 项目:

For some reason I had to create a build directory at the root of my project for my make file to run.

无论如何,我的问题是如何去除 VSCode 上的波浪线?如果VSCode给我没有errors/warnings

就好了

例如,我在 SystemCLock_Config 函数上得到了很多曲线:

但这很奇怪,因为如果我在 __HAL_PWR_VOLTAGESCALING_CONFIG 函数上按 F12,它会将我带到 ...MyProject\Drivers\STM32L1xx_HAL_Driver\Inc\stm32l1xx_hal_pwr.h 并且定义是 #define __HAL_PWR_VOLTAGESCALING_CONFIG(__REGULATOR__) (MODIFY_REG(PWR->CR, PWR_CR_VOS, (__REGULATOR__)))

所以出于某种原因,F12 能够找到定义,但不能找到编译器。一个解决方案是将它放在我的 main.c 文件的顶部:

#ifdefine _DEBUG
#define PWR 0
#endif

但是对于每一个波浪形的错误都这样做是乏味的。

这个来自 stack-overflow 的问题帮助我消除了一些错误,但不是全部: uint32_t does not name a type - VSCode with STM32 in Windows.

无论如何,通过互联网进行研究,这就是我的 c_cpp_properties.json 文件的样子:

{
    "configurations": [
        {
            "name": "STM32 L1",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE",
                "DUSE_HAL_DRIVER",
                "DSTM32L152xE",
                "STM32L1xx",
                "__CC_ARM"
            ],
            "intelliSenseMode": "gcc-arm",            
            "compilerPath": "${env:ARMGCC_DIR}/arm-none-eabi-gcc.exe",
            "compilerArgs": [
                "-mcpu=cortex-m3",
                "-mthumb"
            ],
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ],
    "version": 4
}

我正在使用这个开发板:https://www.st.com/en/evaluation-tools/nucleo-l152re.html。我也用 bluepill 试过,我得到了相同的结果。

终于找到解决方案:

这个文件...MyProject\Drivers\CMSIS\Device\ST\STM32L1xx\Include\stm32l1xx.h

有这个代码

//.....
#if defined(STM32L100xB)
  #include "stm32l100xb.h"
#elif defined(STM32L100xBA)
  #include "stm32l100xba.h"
#elif defined(STM32L100xC)
  #include "stm32l100xc.h"
#elif defined(STM32L151xB)
  #include "stm32l151xb.h"
#elif defined(STM32L151xBA)
  #include "stm32l151xba.h"
#elif defined(STM32L151xC)
  #include "stm32l151xc.h"
#elif defined(STM32L151xCA)
  #include "stm32l151xca.h"
#elif defined(STM32L151xD)
  #include "stm32l151xd.h"
#elif defined(STM32L151xDX)
  #include "stm32l151xdx.h"
#elif defined(STM32L151xE)
  #include "stm32l151xe.h"
#elif defined(STM32L152xB)
  #include "stm32l152xb.h"
#elif defined(STM32L152xBA)
  #include "stm32l152xba.h"
#elif defined(STM32L152xC)
  #include "stm32l152xc.h"
#elif defined(STM32L152xCA)
  #include "stm32l152xca.h"
#elif defined(STM32L152xD)
  #include "stm32l152xd.h"
#elif defined(STM32L152xDX)
  #include "stm32l152xdx.h"
#elif defined(STM32L152xE)
  #include "stm32l152xe.h"
#elif defined(STM32L162xC)
  #include "stm32l162xc.h"
#elif defined(STM32L162xCA)
  #include "stm32l162xca.h"
#elif defined(STM32L162xD)
  #include "stm32l162xd.h"
#elif defined(STM32L162xDX)
  #include "stm32l162xdx.h"
#elif defined(STM32L162xE)
  #include "stm32l162xe.h"
#else
 #error "Please select first the target STM32L1xx device used in your application (in stm32l1xx.h file)"
#endif

所以我不得不在我的 c_cpp_properties.json 文件中添加定义 STM32L152xE。我的 c_cpp_properties.json 文件包含以下内容:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE",
                "DUSE_HAL_DRIVER",
                "DSTM32L152xE",
                "STM32L152xE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "${env:ARMGCC_DIR}/arm-none-eabi-gcc.exe",
            "cStandard": "c17",
            "cppStandard": "c++20",
            "intelliSenseMode": "gcc-arm",
            "compilerArgs": [
                "-mcpu=cortex-m3",
                "-mthumb"
            ]
        }
    ],
    "version": 4
}