Skip to content

Wiki

Advanced Usage Tutorial (Universal Across All Platforms)

About 19 min read

Existing English translations were reused. 4 current source blocks remain in Simplified Chinese, without triggering paid translation.

1.Run and Debug?

1.1.Select packs

After the STM32 STLink appears, then press Enter

Search for the Packs corresponding to the chip and select them.

1.2.(RUN) Download the program to ST-Link

Click RUN, then in the newly popped-up window select the corresponding model, for example, I select STM32F103C8.

It can be seen that the command below has already flashed the program into the STM32, and then the STM32 also worked normally.

1.3.(DEBUG) Debugging Program

Set three breakpoints

extern "C"
void led_task(void const * argument)
{

    for(;;)
    {
        static int a = 5;
        bsp_led.LED_Toggle();  //实例化后调用对象翻转电平函数
        osDelay(500);
        a++;
    }
}

Click Debug and select the model

Then you can enter the Debug interface

Click the Start button

It can be seen that the breakpoint was successfully hit, and the value of a can be viewed through the window on the left.

Then click Continue.

The next breakpoint was also hit

Then click continue, and we find that the value of a has changed to 6, which is consistent with the operation of our program.

This way, you can debug normally.

2.VSCode header file configuration

(This is only to allow better code editing; these header files have not been added to the compilation environment.)

2.1.C/C++ plugin (not recommended)

If there is a situation where the header file cannot be found, configure the Include Path of VSCode's C/C++ plugin.

However, since this plugin requires configuring the compiler at the same time, various minor issues may arise.

Moreover, this plugin can be very laggy for large projects; you can choose to directly look at the clangd plugin tutorial below.

Add one more line here../\*\*

In addition to the above method, it can also be done by modifying the c_cpp_properties.json file.

Input "../**" (meaning to load all files from the previous directory (project root directory) into the Include Path)

At the same time, it is also recommended to add ARMCLANG's include files here as well "/home/tungchiahui/.vcpkg/artifacts/2139c4c6/compilers.arm.armclang/6.21.0/include/"

Each person's directory is different, but they are all under the hidden .vcpkg folder in the user folder; you can look for it yourself. (The image below is incomplete; please add according to the content above.)

After configuring it, we found that code suggestions also worked properly. Although header files might still be falsely reported by VSCode as not found, they could actually compile normally, and suggestions for these header files also worked properly.

2.2.Clangd plugin (highly recommended)

  1. Advantages: Since clangd is suitable for large CMake projects and performs far better in large projects than the C/C++ plugin, both the author and MDK6 recommend using clangd's language server.

The latest version of MDK6 now comes with the clangd plugin built in.

  1. Windows requires downloading and installing LLVM (Linux generally doesn't need to worry about it orsudo apt install llvm)

https://github.com/llvm/llvm-project/releases

I downloaded LLVM 18.1.8, select inAssetsselect inLLVM-18.1.8-win64.exe

Select this option hereAdd LLVM to the system PATH for all users; for the rest, just click Next without thinking.

You can open a terminal to test whether the installation was successful and the environment is configured properly.

clang -v

  1. Now let's install clangd:

按住Ctrl shift P打开搜索框

Enter clangd to find the language server download project, and click to install clangd (please maintain a good network connection).

  1. Next, configure clangd:

Disable the code suggestion feature of C/C++

如果没有上图的弹窗,可以进行手动关闭,依然是ctrl shift P,输入settings然后找到如下图的选项

Find the option shown in the figure below and change it to disabled.

"C_Cpp.intelliSenseEngine": "disabled"

Create a new settings.json file

Modify the contents within it; the contents are the path where the compile_commands.json file generated by CMake is located (the path will change with MDK6 version updates, so find the file path yourself)

Next, locate the include directory of the armclang compiler and add it as well; it is generally under the hidden .vcpkg folder in the user folder.

(There is no longer any need to search.)

The following is the Linux version settings.json example

{
    "clangd.arguments": [
        "--compile-commands-dir=${workspaceFolder}/tmp/Template_Linux/TemplateLinux"
    ]
}

以下是Windows版本的settings.json示例

Note that on Windows, the drive letter needs to be converted to lowercase, for exampleC:/should be changed toc:/then反斜杠\should be changed to斜杠/.

{
    "clangd.arguments": [
        "--compile-commands-dir=${workspaceFolder}/tmp/Template_Linux/TemplateLinux"
    ]
}

然后ctrl shift P搜索clangd找到如下图的选项

Code completion works normally now.

3.Add source files (corresponding to Project Items) and header files (corresponding to Include Path) to the compilation environment

3.1.Conventional method (modify the YAML file)

3.1.1.Related Materials

Adding source files requires modifying the cproject.yml file using the YAML markup language.

The official documentation provides more detailed information on this:https://github.com/Open-CMSIS-Pack/cmsis-toolbox/blob/main/docs/YML-Input-Format.md#source-file-management

3.1.2.Create files (.c and .h)

Here, we first create 4 files in the bsp and place them into Src and Inc respectively.

3.1.3.Add header file path

Write the directory where the header file is located

      add-path:
        - ../Core/Inc
        - ../Drivers/STM32F1xx_HAL_Driver/Inc
        - ../Drivers/STM32F1xx_HAL_Driver/Inc/Legacy
        - ../Drivers/CMSIS/Device/ST/STM32F1xx/Include
        - ../Drivers/CMSIS/Include
        - ../bsp/boards/Inc
        - ../applications/Inc
3.1.4.Add source file and grouping

Enter the group name and the path of the source files to be added here (here, because there are no source files in applications, we comment it out)

    - group: bsp/boards
      files:
        - file: ../bsp/boards/Src/gpio_demo.cpp
        - file: ../bsp/boards/Src/gpio_test.c

    # - group: applications

    #   files:

The source file and header file have both been successfully imported. We can now write the file contents and see whether they can pass compilation.

3.1.5.Write file and compile

You can see these lines in the log, showing that both gpio_demo and gpio_test were successfully compiled

[14/22] Building C object CMakeFiles/Template_Linux.dir/home/tungchiahui/user/Source/STM32_Projects/N1_F407ZGT6_GPIO_Test/bsp/boards/Src/gpio_test.o
[15/22] Building C object CMakeFiles/Template_Linux.dir/home/tungchiahui/user/Source/STM32_Projects/N1_F407ZGT6_GPIO_Test/Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.o
[16/22] Building CXX object CMakeFiles/Template_Linux.dir/home/tungchiahui/user/Source/STM32_Projects/N1_F407ZGT6_GPIO_Test/bsp/boards/Src/gpio_demo.o

3.2.Graphical

3.2.1.Introduction

Because the ARM team was quite productive, they got the graphical operation working in just two months, and it had already been updated by early March.

Whatever graphical features the ARM team updates, the tutorial below will be updated a few days later with the corresponding content.

3.2.2.Add source file

Waiting for ARM to update the feature... ...