第 3 節

Linux环境搭建(旧)

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

(本教程为2024年1月创建的,可能与以后的版本有些出入)

Software to prepare

  1. CubeMX latest version
  2. The latest version of VScode
  3. vcpkg package management tool
  4. pyOcd (see tutorial below for installation instructions)
  5. ST-Link driver (see tutorial below for installation instructions)

vcpkg Installation and Environment Setup

  1. Download the dependency packages
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential tar curl zip unzip
sudo apt-get install default-jre
  1. Clone the vcpkg repository

https://github.com/microsoft/vcpkg/tree/master

git clone https://github.com/microsoft/vcpkg.git
  1. Generate the vcpkg program.
cd vcpkg
sudo chmod a+x ./bootstrap-vcpkg.sh
sudo ./bootstrap-vcpkg.sh
  1. Set up the environment
vim ~/.bashrc

VCPKG_HOME is the vcpkg directory

#配置vcpkg环境 
export VCPKG_HOME=/home/tungchiahui/user/applications/vcpkg  #目录需要改为你的vcpkg的目录
export PATH=$VCPKG_HOME:$PATH

source ~/.bashrc
vcpkg --version

If the prompt shown in the image appears, the installation was successful!

MDK5 Project Generation and ARMCLANG (AC6) Compiler Configuration

Project Generation and Compiler Configuration

  1. Method 1: The compiler configuration tutorial needs to be done on Windows, as it is currently difficult to modify compiler options on Linux. You can refer to the Windows tutorial below for generating the project and configuring the default compiler. (Essentially, this means changing the compiler from the default AC5 to AC6.)
  2. 方式二 :克隆已经生成好的模板(模板目前只有几个常用型号的)

Repository link:

https://github.com/TungChiahuiMCURepos/CubeMX_CMake_Template

git clone https://github.com/TungChiahuiMCURepos/CubeMX_CMake_Template.git

Project configuration (e.g., initializing a GPIO pin and creating a task to toggle its level)

First, make a copy of the project template.

Rename the project

Open CubeMX (and click File -> Load Project at the top, or directly click the icon shown in the image below).

Find the project and load it, then configure the project.

Open a terminal in the MDK-ARM folder.

cd MDK-ARM
code .

Install and activate the MDK6 plugin.

Download the ARM Keil Studio Pack.

Activate the MDK6 plugin

First-time conversion of an MDK5 project and downloading dependency packages.

In the bottom right corner, install all the packages that need to be installed, and allow any prompts that appear.

When installing Packs, you need to ensure a good network environment (one with internet access to bypass restrictions).

This stage will last 5–20 minutes, so please wait patiently. (Depends on the speed of your proxy service.)

(These operations are only needed for the first run)

This survey can be skipped.

As shown in the figure, the installation was successful.

If the environment below is already configured, right-click on uvprojx and select Convert.

If the environment is not configured properly, right-click this file and select active environment (in the image, since my environment is already configured, it shows deactivate).

Then execute the Convert step from the previous step.

As shown in the figure, initialization has been successful.

Compile

Clicking the build button and finding the file size is the same means the compilation was successful.

If compilation fails, check whether the project file list has an extra dot configured. (Refer to the advanced tutorial below on adding source files to resolve this.)

Install pyOCD (Linux)

https://github.com/pyocd/pyOCD

First, open the terminal and enter (if you are using a Debian-based system, such as Ubuntu, please refer to the tutorial below).

sudo apt install python3-pip
python3 -mpip install -U pyocd

# 如果上面的不行,则输入下方的
pip3 install -U pyocd

If it still doesn't work, and the prompt says

error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.

then use (a Debian-based system)

sudo apt install python3-pyocd

Or simply clone the repository.

git clone https://github.com/pyocd/pyOCD.git
cd pyOCD
pip3 install .

You can also install pyOCD this way.

Next, we need to install the driver for the ST-Link debugger and similar tools.

pyOCD Debugger Driver Installation Official Tutorial

https://github.com/pyocd/pyOCD/tree/main/udev

You still need to use the files from the pyOCD repository.

If you haven't cloned the repository yet, please do so as soon as possible.

In the repository directory, enter the following command:

cd udev
sudo cp *.rules /etc/udev/rules.d
#重启udev
sudo udevadm control --reload
sudo udevadm trigger

Now the ST-Link can be detected normally.

If it is not detected, please unplug and re-plug the ST-Link, then click Add Device to add the device.

测试连接设备

随便插上一个debugger,比如我这里插上一个连着stm32f103c8t6的ST-link:

pyocd --version

pyocd list

像下图这样正常识别出来了,就说明成功了。

进一步,可以继续测试

pyocd list --targets | grep -i stm32f103

pyocd list --targets --name stm32f103

发现是stm32f103rc,然后尝试连接该设备:

pyocd commander -t stm32f103rc

如果成功,通常会进入 pyOCD commander:

然后你可以输入:

status 再退出:

exit

status 能返回 Running,说明不是“只枚举到了 USB”,而是真的已经连进目标 MCU 了。

成功了。

但是你会发现,咱们明明插的是stm32f103c8,为何识别出stm32f103rc呢?

pyOCD 当前内置目标里确实有 stm32f103rc,但没有内置 stm32f103c8;

pyOCD 官方也说明 target 类型决定 Flash 算法、内存映射等信息,所以正式下载程序时最好不要长期拿 RC 冒充 C8。

更规范的做法是给 pyOCD 安装 STM32F103C8 对应的 CMSIS-Pack:

先查找是否有:

pyocd pack find stm32f103c8

第一次跑这个会先下载索引,等一会儿吧(需要特殊网络环境)

找到后,显示没安装:

再安装:

pyocd pack install stm32f103c8

再查:

pyocd list --targets --name stm32f103

可以看到已经可以支持一堆设备了。

pyocd commander -t stm32f103c8

如果成功,通常会进入 pyOCD commander:

然后你可以输入:

status 再退出:

exit

https://www.st.com/en/development-tools/stsw-link007.html#get-software

Unable to display this content outside of Feishu documents for now.

After downloading, extract the files.

sudo apt install ./st-stlink-udev-rules-1.0.3-2-linux-all.deb

Simply restart VSCode.

(Scroll down for more related tutorial operations below.)

音乐页