第 5 節

Advanced Usage Tutorial (Universal for All Platforms)

0瀏覽次數0訪問次數--跳出率--平均停留

Run (running a program) and Debug (debugging a program)?

Select packs

After "STM32 STLink" appears, then press Enter.

Search for the corresponding chip's Packs and select it.

Click RUN, then in the newly opened window select the corresponding model, for example I choose STM32F103C8.

You can see that the command below has successfully flashed the program into the STM32, and the STM32 is now working properly.

(DEBUG) Debugging the 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.

You can see that the breakpoint was successfully hit, and the value of a can be viewed in the left panel.

Then click Continue.

The next breakpoint was also hit.

Then click Continue, and you'll see that the value of a has changed to 6, which matches the execution of our program.

That's it, you can debug normally now.

VScode header file configuration

(This only allows for better code editing; these header files have not been added to the compilation environment)

If you encounter a situation where header files cannot be found, simply configure the Include Path of the VSCode C/C++ extension.

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

Moreover, this plugin can be very laggy for large projects. You can choose to directly read the clangd plugin tutorial below.

Add one more line here../**

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

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

It is also recommended to add the ARMCLANG include files here: "/home/tungchiahui/.vcpkg/artifacts/2139c4c6/compilers.arm.armclang/6.21.0/include/"

Everyone's directory is different, but they are all located in the hidden .vcpkg folder under the user folder. You can find it yourself. (The image below is incomplete; please add content based on the information above.)

After configuration, we found that code suggestions are working properly. Although the header files might still be incorrectly flagged by VSCode as missing, they can actually be compiled normally, and the suggestions for these header files are also working correctly.

  1. Advantage: Since clangd is well-suited for large CMake projects and performs far better than the C/C++ extension in such projects, both the author and MDK6 recommend using the clangd language server.

The latest version of MDK6 comes with a built-in clangd plugin.

  1. On Windows, you need to download and install LLVM (on Linux you usually don't need to worry about it, or sudo apt install llvm).

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

I downloaded LLVM 18.1.8, selecting Assets and selecting LLVM-18.1.8-win64.exe.

Select this option Add LLVM to the system PATH for all users here, then just click Next without changing anything else.

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

clang -v

  1. Now let's install clangd:

Hold down Ctrl+Shift+P to open the search box.

Enter clangd, find the "Download Language Server" option, and click to install clangd (please ensure a stable internet connection).

  1. Next, configure clangd:

Disable C/C++ code suggestion features.

If the pop-up shown above does not appear, you can manually close it. Again, press Ctrl+Shift+P, type "settings", and then find the option shown in the image below.

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

"C_Cpp.intelliSenseEngine": "disabled"

Create a new settings.json file.

Modify the content inside. This content is the path to the compile_commands.json file generated by CMake (the path may change with MDK6 version updates, so please locate the file path yourself).

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

(No need to look for it anymore)

Here is an example of settings.json for the Linux version.

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

Here is an example of the settings.json file for the Windows version.

Note that on Windows, drive letters need to be changed to lowercase, for example C:/ should be changed to c:/, and 反斜杠\ should be changed to 斜杠/.

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

Then press Ctrl+Shift+P, search for "clangd", and find the option shown in the image below.

The code hints are working normally now.

Add source files (corresponding to Project Items) and header files (corresponding to Include Path) to the build environment

Common method (modifying the yaml file)

references

To add source files, you need to modify the cproject.yml file using 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

Create files (.c and .h)

We'll first create 4 files in the BSP and place them into the Src and Inc directories respectively.

Add the 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
Add source files and groups

Enter the group name and the source file path to be added here (since 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 files and header files have been successfully imported. We can now write the file content to see if it compiles successfully.

Write the file and compile it.

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

Graphical

Introduction

Thanks to the ARM team's impressive efforts, they rolled out a graphical interface in just two months, and it has already been updated as of early March.

When the ARM team updates a graphical feature, the tutorial below will be delayed by a few days to update the corresponding content.

Add the source file.

Waiting for ARM to update the feature...

音乐页