Search code examples
visual-studio-codestm32

Use VSCode to compile and run STM32 projects


I been using Visual Studio and VSCode for a long time; therefore, it will be great if I can use it instead of Eclipse or Keil. Should I move to those IDEs? I like having no dependencies where I have a make file that compiles and flashes my code to an stm32. Moreover, I can customize VSCode a lot. For example I have a plugin called highlight and I have a regex that will highlight everything that is between USER CODE BEGIN and USER CODE END. As a result my code looks like this: enter image description here

Now I know that my code can only exist on the highlighted regions. That way if I make any changes using CubeMx my code will not be lost.

Thanks to this videos I am able to debug, compile and flash stm32 projects:

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

Anyways my question is how can I remove the squiggles on VSCode? It will be great if VSCode gave me no errors/warnings

For example I get a lot of squiggles on the SystemCLock_Config function: enter image description here

But that is strange because if I press F12 on the __HAL_PWR_VOLTAGESCALING_CONFIG function it takes me to ...MyProject\Drivers\STM32L1xx_HAL_Driver\Inc\stm32l1xx_hal_pwr.h and the definition is #define __HAL_PWR_VOLTAGESCALING_CONFIG(__REGULATOR__) (MODIFY_REG(PWR->CR, PWR_CR_VOS, (__REGULATOR__)))

So for some reason F12 is able to find the definition but not the compiler. A solution would be to place this at the top of my main.c file:

#ifdefine _DEBUG
#define PWR 0
#endif

But it is tedious to do that for every squiggly error.

This question from stack-overflow has help me remove some errors but not all: uint32_t does not name a type - VSCode with STM32 in Windows.

Anyways from doing research from the internet this is how my c_cpp_properties.json file looks like:

{
    "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
}

I am using this board: https://www.st.com/en/evaluation-tools/nucleo-l152re.html . I have also tried it with the bluepill and I get the same results.


Solution

  • Finally found the solution:

    This file ...MyProject\Drivers\CMSIS\Device\ST\STM32L1xx\Include\stm32l1xx.h

    Has this code

    //.....
    #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
    

    So I had to add the definition STM32L152xE on my c_cpp_properties.json file. My c_cpp_properties.json file contains this:

    {
        "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
    }