第 3 節

Linux

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

Environment Introduction

This tutorial covers the environment setup:

  1. System: Fedora 43 KDE Edition Linux
  2. System kernel: Linux 6.19.12-200.fc43.x86_64
  3. Architecture: X86_64 (amd64)

Other Linux environments are also acceptable.

Install various software and environments

Install CubeMX

Download link:

https://www.st.com.cn/zh/development-tools/stm32cubemx.html

It is recommended to download version 6.14.1 (do not download version 6.15.0, as this version has a bug, and it is unclear when it will be fixed).

Decompress it.

Open this software with root privileges SetupSTM32CubeMX-6.15.0.

sudo ./SetupSTM32CubeMX-6.15.0

In the newly popped-up window, just keep clicking Next until the installation finishes. When it looks like the image below, the installation is successful.

/usr/local/STMicroelectronics/STM32Cube/STM32CubeMX Enter this folder, then open a terminal and type

./STM32CubeMX

Click Help

Select Manage embedded software packages, and check the first and latest firmware for STM32F1, F4, and H7.

Click install.

Logged into the account.

Then wait for the download and installation to finish.

Just download it.

Next, you can configure a desktop shortcut for CubeMX application for quick access. For detailed instructions, refer to the Appimage section of the Vinci Robotics Team Linux Beginner Tutorial. You can use Ctrl+F to quickly locate that section.

The desktop shortcut is as follows:

[Desktop Entry]
Name=STM32CubeMX
Exec=/usr/local/STMicroelectronics/STM32Cube/STM32CubeMX/STM32CubeMX
Icon=/usr/local/STMicroelectronics/STM32Cube/STM32CubeMX/help/STM32CubeMX.png
Type=Application
Categories=Development;Electronics;Embedded;
Comment=STM32CubeMX configuration and code generation tool
Terminal=false

Just follow the tutorial, and you'll be able to achieve this effect.

Install VSCode

https://code.visualstudio.com/Download

If it's a Debian-based system, download the .deb; if it's an RHEL-based system, download the .rpm.

After downloading, open your browser, locate the folder containing this installation package, and open a terminal in that path.

Debian-based: Enter sudo apt install ./code then press the tab key to autocomplete the filename, then press Enter.

RHEL series: Enter sudo dnf install ./code and then press the tab key to autocomplete the filename, then press Enter.

For example, after completion:

sudo dnf install ./code-1.102.1-1752598767.el8.x86_64.rpm

Then open VSCode and enter the following command in the terminal:

code

Then you can configure a separate environment for the CubeIDE plugin to use, avoiding conflicts with the default environment.

alt text

Make some settings; just follow my lead.

alt text

Select STM32

alt text

Then install some plugins.

Find the installation of the STM32CubeIDE for Visual Studio Code plugin below.

alt text

On the right, this prompt appears asking you to select and install (requires a reliable scientific network).

alt text

Next, some environment installations will be performed.

alt text

You can also install some other plugins, such as Codex and others. These are up to you.

alt text

Project Creation and Testing

Create a project using CubeMX

Click the button to select a microcontroller.

Search for the corresponding chip, and double-click the chip option.

Perform some configuration. The following are all very basic things that you definitely already know before watching this video.

Open any IO for testing, such as the LED's GPIO.

FreeRTOS also needs to be configured.

These folders also need to be configured properly. Finally, select CMake for Toolchain and choose GCC for the compiler (it's normal not to have the compiler selection option in version 6.14.1 and earlier).

(However, CubeMX 6.15.0 has a bug — selecting the GCC compiler here doesn't actually work, and you'll need to manually select the compiler later. This bug may be fixed in a future release.)

Configure and compile the project.

Open a terminal in the project folder.

alt text

code .

After opening VSCode, remember to switch to the STM32 configuration.

alt text

Select Yes here to configure the CMake preset.

alt text

Generally, choose Debug.

alt text

Find a C language code file to open, and a prompt will appear in the bottom right corner suggesting you install a C/C++ plugin. You can choose to install it or not — it does provide code suggestions, but its code suggestion capabilities are far weaker compared to the built-in clangd. If you're a beginner and don't know how to configure code suggestions, I recommend following my steps below and simply not installing this plugin.

alt text

You can test the code suggestions to see how powerful they are.

alt text

For compilation, either of these two "build" options in the image will work.

alt text

Porting author tungchiahui's standard C/C++ project template

Use the git clone command to clone the repository: https://github.com/tungchiahui/STM32HAL_CMake_CPP_Template

git clone https://github.com/tungchiahui/STM32HAL_CMake_CPP_Template.git

Copy all files and folders (except .git) from the repository into our STM32 project directory.

alt text

Then open the applications folder, and create led_task.cpp and led_task.h in the Src and Inc folders respectively, with the following contents:

led_task.cpp:

#include "led_task.h"
#include "cmsis_os.h"
#include "stm32f1xx_hal.h" 

GPIO_PinState pinstate = GPIO_PIN_RESET;

extern "C"
void StartDefaultTask(void *argument)
{
  for(;;)
  {
    HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,pinstate);
    pinstate = (pinstate == GPIO_PIN_RESET) ? GPIO_PIN_SET : GPIO_PIN_RESET;
    osDelay(500);
  }
}

led_task.h:

#ifndef __LED_TASK_H_
#define __LED_TASK_H_

#ifdef __cplusplus
extern "C"
{
#endif

#include "cpp_interface.h"

#ifdef __cplusplus
}
#endif

#endif

Then open CMakeLists.txt in the cmake/user folder and add the newly created led_task.cpp to it.

Detailed explanation (optional): The cmake/stm32cubemx under CMakeLists.txt here is managed by CubeMX. If you regenerate code with CubeMX, the contents of this file will be overwritten. However, the CMakeLists.txt at the workspace root will not be overwritten, and it provides some areas for adding source and header files. But this can make the file too cluttered. So we chose to create a new user folder, set up a CMakeLists.txt inside it, and then use the top-level CMakeLists.txt to load this sub-CMakeLists.txt. This sub-CMakeLists.txt makes it easier for us to modify, and the file structure is clearer. (You don't need to create these yourself—I've already added them to the template, and they were copied over when you copied the template above.)

Add the cpp file as shown in the image below.

Then, go to the top-level CMakeLists.txt and add this line to reference our own CMakeLists.txt.


# Add USER generated sources
add_subdirectory(cmake/user)

Great success, let's try compiling once. As shown in the image below, all the newly added files have been compiled.

alt text

Download the program to the board

Before downloading, you first need to configure

No need to configure ST-Link, just start debugging directly.

However, Jlink and similar tools require configuration.

Configure the debugger

No configuration required.

alt text

First, install the jlink-gdbserver bundle, as shown in the image below:

alt text

Then you also need to configure the launch:

Problem Solution: https://community.st.com/t5/stm32cubeide-for-visual-studio/stm32h7a3vg-debugging-with-j-link-under-vscode/m-p/826188#M960

Create a launch.json under the .vscode folder, then enter the following content:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "jlinkgdbtarget",
            "request": "launch",
            "name": "STM32Cube: STM32 Launch JLink GDB Server",
            "origin": "snippet",
            "cwd": "${workspaceFolder}",
            "preBuild": "${command:st-stm32-ide-debug-launch.build}",
            "runEntry": "main",
            "imagesAndSymbols": [
                {
                    "imageFileName": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}"
                }
            ]
        }
    ]
}

Done.

Debugging: (Here, Linux may encounter some USB permission issues; please resolve them on your own.)

If you are using an ST-Link, it should look like the image below:

alt text

If you are using a J-Link, it should look like the image below:

alt text

Then this bar will appear, and it will download the program to the board. alt text

Then the program was successfully downloaded and entered Debug mode.

alt text

音乐页