当前位置:网站首页>Vs code + GCC environment compilation for STM32 development

Vs code + GCC environment compilation for STM32 development

2022-06-21 12:04:00 Summer foam and light rain

Write it at the front :
The purpose of this article is to summarize backup 、 For future reference , Because it's a personal summary , If there is any wrong , Welcome to correct ; in addition , Most of the content comes from the Internet 、 Books 、 And all kinds of manuals , In case of infringement, please inform , Immediately delete the post and apologize .



One 、 Program installation

1、VSCode:https://code.visualstudio.com/Download

Good use of the popular text editor , With powerful plug-ins , Make your development efficiency greatly improved .

2、Java:https://www.java.com/zh-CN/download/

STM32CubeMX The installation of requires JAVA Running environment (jre).

3、STM32CubeMX:https://my.st.com/content/my_st_com/en/products/development-tools/software-development-tools/stm32-software-development-tools/stm32-configurators-and-code-generators/stm32cubemx.html

Support the latest HAL Library and LL library , Engineering code configuration and generation tool , Support generation IAR、Keil、STM32CubeIDE、Makefile Wait for the project , Here we use the Makefile engineering .

4、MinGW-w64:https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/

A powerful and practical C/C++ compiler . After installation, you need to add system environment variables , You can use cmd command :gcc -v test .

5、arm-none-eabi-gcc:https://launchpad.net/gcc-arm-embedded/+download

GUN Of arm General cross compilation chain tool , Basically common arm All processors support . alike , After installation, you need to add system environment variables , You can use cmd command :arm-none-eabi-gcc -v test .

Two 、CubeMX Project generation

at present STM32 Now the main library is HAL library , And of course there is LL The library is used to manipulate the underlying registers ; about HAL library , The official has specially launched graphical software CubeMX To simplify the HAL Under the library STM32 Initialization code ; We only need to configure the corresponding peripheral functions and clock tree , Click generate code ,CubeMX The corresponding project file will be generated , It also supports a variety of IDE Tools , It's really necessary to travel at home . cough , Partial topic !

First , Select your target chip , Start project configuration , Here is a simple one LED Control heel DEBUG To configure .

The serial port configuration is as shown in the figure below , As usual , Asynchronous communication ,Baud rate:115200、8Bit、1Stop Bit

 Insert picture description here

Then configure LED IO Pin , Directly click the corresponding pin to configure , As shown in the figure below :

 Insert picture description here

How to use this software is not discussed here , There is also a little attention :

If you want to use STLink Wait for the online debugger , Then you need to select the debugging method you need in the following figure :

 Insert picture description here

For the clock tree , Like us, we usually use HSE High speed external crystal , Then you need to be in RCC There open the corresponding configuration :

 Insert picture description here

Then configure the corresponding peripheral clock in the clock configuration :

 Insert picture description here

Last , Because we want to be in VSCode On the development , Just choose to generate Makefile That's all right. . One thing to note is that , This step is not necessary , It's quite possible not to use CubeMX, Write it yourself Makefile It's OK, too . Then output the generated project file .

 Insert picture description here

3、 ... and 、VSCode The deployment environment

1、 Plug in installation

plug-in unit function
Chinese (Simplified) Language Pack for Visual Studio Code chinese ( Simplified Chinese character ) Language pack
Comment TranslateVSCode Annotation translation
C/C++C / C ++ Language support for , Grammar IntelliSense 、 Highlight and debug functions , Of course, system installation is required arm-none-eabi-gcc compiler
Cortex Debug Provide jlink、stlink And other debugging interface functions
ARMarm Assembly language support
vscode-icons File Icon , You can quickly view file types

2、 To configure VS Code

First , Take from STM32CubeMX The created project is imported into VS Code Inside , obtain :

 Insert picture description here

By default , The project does not include .vscode The folder of , It will be created automatically by operating the following three methods :

Open the command window to edit the configuration (Ctrl+Shitf+P > Edit Configurations(JSON))

 Insert picture description here

Or click... In the window bar terminal -> Configure tasks -> Create... Using templates task.json -> Other

 Insert picture description here

Or modify... In the workspace settings.json file ( window File–Preferences–Settings–Features–Terminal Of Terminal > Integrated > Automation Shell: Windows Column )

 Insert picture description here

The three created effects are as follows :

 Insert picture description here

Let's start with the first two files :

  • c_cpp_properties.json

Mainly add include route , Compiler path , Macro definition, etc ; After setting up , Indexes 、 Compiling is like keil As convenient as . then , Its configuration is modified as follows :

{
    
    "configurations": [
        {
    
            "name": "STM32",		//  Create a file called "STM32" Configuration of 
            "includePath": [		//  Declare the of this workspace  C/C++ What header file directories does the project contain 
                "${workspaceFolder}/**",
                "${workspaceFolder}/Drivers/CMSIS/Device/ST/STM32L1xx/Include",
                "${workspaceFolder}/Drivers/CMSIS/Include",
                "${workspaceFolder}/Drivers/STM32L1xx_HAL_Driver/Inc",
                "${workspaceFolder}/Drivers/STM32L1xx_HAL_Driver/Inc/Legacy",
                "${workspaceFolder}/Core/Inc"
            ],
            "defines": [			//  Global preprocessing macro definitions 
                "STM32L151xB",
                "USE_HAL_DRIVER"
            ],
            "compilerPath": "gcc.exe",		//  If you add an environment variable , It can be expressed like this ; Otherwise you have to add an absolute path , Such as :C:\\Program Files\\mingw64\\bin\\gcc.exe
            "cStandard": "c11",				//  The use of  C standard 
            "cppStandard": "c++17",			//  The use of  C++ standard 
            "intelliSenseMode": "gcc-x64"	//  Language style 
        }
    ],
    "version": 4
}

Be careful : This json Comments are not allowed , therefore , If it is like copying to your file , Remember to delete the notes .

  • tasks.json

Under this document tasks Each object in the array represents a task .

{
    
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
    
          "label": "Build",			//  Tag name 
          "type": "shell",			//  terminal (shell) Type of task 
          "command": "make",		// * Executed command 
          "args": [					//  Arguments to the command 
            "-j6"					//  It means that six threads compile at the same time 
          ],
          "problemMatcher": ["$gcc"],	//  Indicates that if a compilation error occurs , The question panel will show  gcc Type of mistake 
          "group": {
    
            "kind": "build",
            "isDefault": true
          }
        }
      ]
}

Be careful : above "command" The options are filled with “make”; actually , In our download of MinGW-w64 In the compiler , Execution is mingw32-make.exe; however , Here you can see mingw32-make.exe The filename is too long , And it's hard to remember , So we can copy it and rename it make.exe In this way, with Linux Under the make The orders are consistent ; If you don't copy it as mentioned above make.exe, that , stay "command" Fill in the options as “mingw32-make”, Similarly, when compiling , The command you type is mingw32-make.

  • settings.json

I'll leave this for you in combination with an example .

Four 、 Engineering compilation

After you configure the above operations , The best thing is to save all the files first , Then close it VS Code, Reopen the project .

There are two ways to compile :

1、 Click... In the window bar terminal -> Run build task ( Shortcut key Ctrl+Shitf+B)

 Insert picture description here

2、 Click... In the window bar terminal -> New terminal , choice cmd

 Insert picture description here

If not , Click on it , Then click “ Choose default Shell”, Then click the trash can next to it , Open it again .

type make, Finally press enter . When the compilation is completed, the compiled file size will be displayed as shown in the following figure :

 Insert picture description here

5、 ... and 、 Compile cleanup

Compile file cleanup , Just type... In the terminal console make clean That's all right. , however , however , If you are using cmd Of shell, You'll find that :

 Insert picture description here

The compiled file is not cleared . Why is that ? You can get the answer here :https://github.com/STAT545-UBC/Discussion/issues/55

According to the description, it can be summarized as :

resolvent 1: Use Windows Version of rm yes del, So you can simply use rm Replace with del. But be careful , Doing so means that you will not be able to use Git Bash shell, Because it doesn't support del Command procedure ;

Solution 2: Suppose you have installed Git Bash( If not, install Git Chant , all 0202 了 , No one else doesn't have to Git Well , I really don't want to use Git, Then find a support rm Terminal command desk , for example :msys2), Then you can switch the terminal command console to Bash above

The first one is simple , stay makefile In the rm Replace with del, As shown in the figure below :

 Insert picture description here

the second , Click again as above “ Choose default Shell”, And then choose Git bash
 Insert picture description here

 Insert picture description here

then , Use this operation to modify the terminal Sell, Different projects are important for selecting different terminals , Sometimes you need to switch frequently , inconvenient ; Then you have a workspace settings.json The role of file configuration .

stay settings.json In file , We only need to configure the following operations :

{
    
	/*  Terminal in Windows The use of shell The path of  */
    "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",	//  If you add an environment variable, you can remove the path 
    "terminal.external.windowsExec": "C:\\Program Files\\Git\\bin\\bash.exe"		//  Same as above 
}

In this way , Then it will only have an effect in the project workspace .

You see , The colors are different :

 Insert picture description here

6、 ... and 、 other

Next , download & Setup of debugging environment :STM32 Development of VS Code + GDB Download debugging

原网站

版权声明
本文为[Summer foam and light rain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211158514929.html