你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

基于STM3的开发环境搭建经验分享

[复制链接]
攻城狮Melo 发布时间:2024-6-15 14:45
“ 从ubuntu20.04入门到完成stm32程序的调试。”

微信图片_20240615144535.png

1、更换软件源
源文件(sources.list)
更改源(Ubuntu20.04对应的源)
更新源

2、安装拼音输入法
Region&Language->Language Support->Installed Languages
Input Sources->"+"->chinese->Hanyu Pinyin

3、安装VSCode
下载.deb程序包
使用dpkg安装
调整设置
启用Title Bar
安装插件

4、安装Build Essential及gdb
build essential介绍
命令行安装build essential和gdb
Linux C程序开发简介
无makefile
有makefile
生成.vscode文件下的debug配置文件
生成task.json文件
生成launch.json文件
生成c_cpp_properties.json文件

5、安装Vim

6、安装gcc-arm-none-eabi

下载嵌入式gcc编译器
解压
添加gcc-arm-none-eabi下的可执行文件命令到Terminal

7、安装stm32cubemx
下载stm32cubemx
安装
增加图标到菜单

8、使用stm32cubemx生成初始代码
选择芯片->配置->Generate
勾选PinOut-Debug-SerialWire

9、配置VSCode
使用vscode打开上述生成的初始代码
生成VSCode配置文件
c_cpp_properties.json
task.json
settings.json
宏定义包含
make编译/使用ctrl+shift+B

10、调试
安装openocd
在tasks.json中使用openocd进行编译后的下载
使用openocd进行调试
安装Cortex Debug和Venus's Cortex-Debug插件
添加调试配置
选择cortex debug,生成launch.json
调试

11、使用VSCode串口工具
安装serial monitor
查看连接的usb
加权限

12、使用git
01更换源
1、源文件(sources.list)
chen@ubuntu:/etc/apt$ ls -a
.   apt.conf.d   preferences.d  sources.list    trusted.gpg.d
..  auth.conf.d  sources.bak    sources.list.d

2、更改源(Ubuntu20.04对应的源)
  1. chen@ubuntu:/etc/apt$ sudo gedit sources.list
复制代码
  1. #阿里源(Ubuntu20.04)
  2. deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
  3. deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
  4. deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
  5. deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
  6. deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
  7. deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
  8. # deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
  9. # deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
  10. deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
  11. deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
复制代码
  1. #清华源(Ubuntu20.04)
  2. # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
  3. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
  4. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
  5. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
  6. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
  7. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
  8. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
  9. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
  10. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse


  11. # 预发布软件源,不建议启用
  12. # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
  13. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
复制代码
  1. #中科大源(Ubuntu20.04)
  2. deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
  3. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
  4. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
  5. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
  6. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
  7. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
  8. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
  9. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
  10. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
  11. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
复制代码

3、更新源
  1. sudo apt-get update
复制代码

02安装拼音输入法
1、Region&Language->Language Support->Installed Languages

微信图片_20240615144532.png

2、Input Sources->"+"->chinese->Hanyu Pinyin
  1. chen@ubuntu:~$ sudo apt-get install ibus-pinyin
  2. chen@ubuntu:~$ ibus restart
  3. chen@ubuntu:~$ ibus-setup
复制代码

微信图片_20240615144527.png

微信图片_20240615144524.png

微信图片_20240615144520.png

03安装VSCODE
1、下载.deb程序包https://code.visualstudio.com/

2、使用dpkg安装(在Terminal中Ctrl+Shift+C为复制,Ctrl+Shift+V为粘贴)
  1. chen@ubuntu:~/Downloads$ sudo dpkg -i code_1.89.1-1715060508_amd64.deb
  2. chen@ubuntu:~/Downloads$ code .
复制代码

3、调整设置
启用Title Bar:View->Appearance->Editor Actions Position->Tab Bar

微信图片_20240615144516.png

4、安装插件
git安装
Download Git for Linux(https://git-scm.com/download/linux)

微信图片_20240615144512.png

微信图片_20240615144509.png

命令行直接安装(Ctrl+Alt+T呼出Terminal)
  1. chen@ubuntu:~$ sudo apt-get install git
  2. chen@ubuntu:~$ git --version
复制代码

git设置

微信图片_20240615144504.png

  1. git clone https://gitee.com/kevinkerry/linux-stm32.git
  2. git config --global user.name 'kevin.kerry'
  3. git config --global user.email 'ct15762287606@outlook.com'


  4. #示例
  5. chen@ubuntu:~/prj_vscode$ git clone https://gitee.com/kevinkerry/linux-stm32.git
  6. Cloning into 'linux-stm32'...
  7. Username for 'https://gitee.com': ct15762287606@outlook.com
  8. Password for 'https://ct15762287606@outlook.com@gitee.com':
  9. remote: Enumerating objects: 4, done.
  10. remote: Counting objects: 100% (4/4), done.
  11. remote: Compressing objects: 100% (4/4), done.
  12. remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
  13. Unpacking objects: 100% (4/4), 1.79 KiB | 1.79 MiB/s, done.
  14. chen@ubuntu:~/prj_vscode$ ls
  15. linux-stm32
复制代码

GitLens — Git supercharged插件安装

微信图片_20240615144459.png

Git History

微信图片_20240615144456.png

Bracket Pair Color DLW

微信图片_20240615144454.png

Arm Assembly

微信图片_20240615144451.png

Venu's Cortex-Debug

微信图片_20240615144447.png

C/C++

微信图片_20240615144437.png

04安装Build Essential及gdb
1、build essential介绍
build-essential 指的是编译程序必须的软件包。它本身不是一个软件,而是一个工具集,包含了在Linux开发程序的一些必要的软件包,包括但不限于gcc、g++、make等。

查看该软件包的依赖关系,可以看到以下内容:
  1. chen@ubuntu:~$ apt-cache depends build-essential
  2. build-essential
  3. |Depends: libc6-dev
  4.   Depends: <libc-dev>
  5.     libc6-dev
  6.   Depends: gcc
  7.   Depends: g++
  8.   Depends: make
  9.     make-guile
  10.   Depends: dpkg-dev
复制代码

也就是说,安装了该软件包,编译c/c++所需要的软件包也都会被安装。因此如果想在Ubuntu中编译c/c++程序,只需要安装该软件包就可以了。

2、命令行安装build essential和gdb
有时候可能会需要安装gdbserver
  1. chen@ubuntu:~$ sudo apt-get install build-essential gdb
复制代码

3、Linux C程序开发简介
3.1 无makefile
  1. chen@ubuntu:~$ mkdir linux_c
  2. chen@ubuntu:~$ cd linux_c
  3. chen@ubuntu:~$ touch hello.c
  4. chen@ubuntu:~$ sudo vi hello.c
  5. chen@ubuntu:~$ cat hello.c
  6. #include <stdio.h>
  7. int main(int argc,char *argv[])
  8. {
  9.   printf("hello world\n");
  10. }
  11. chen@ubuntu:~$ gcc hello.c
  12. chen@ubuntu:~$ gcc hello.c -o hello
  13. chen@ubuntu:~$ ls -al
  14. total 52
  15. drwxrwxr-x 2 chen chen  4096 May 16 18:32 .
  16. drwxrwxr-x 4 chen chen  4096 May 16 17:37 ..
  17. -rwxrwxr-x 1 chen chen 16696 May 16 18:29 a.out
  18. -rwxrwxr-x 1 chen chen 16696 May 16 18:32 hello
  19. -rw-rw-r-- 1 chen chen    82 May 16 18:29 hello.c
  20. chen@ubuntu:~$ ./a.out
  21. chen@ubuntu:~$ ./hello
复制代码

3.2 有makefile
3.2.1 创建程序文件
  1. chen@ubuntu:~/prj_vscode/linux-c$ touch main.c input.c calcu.c input.h calcu.h
复制代码

main.c
  1. #include <stdio.h>
  2. #include "input.h"
  3. #include "calcu.h"


  4. int main(int argc, char *argv[])
  5. {


  6.   int a, b, num;


  7.    input_int(&a, &b);
  8.    num = calcu(a, b);
  9.    printf("%d + %d = %d\r\n", a, b, num);
  10.    
  11. }
复制代码

input.h
  1. #ifndef _INPUT_H
  2. #define _INPUT_H

  3. void input_int(int *a, int *b);

  4. #endif
复制代码

input.c
  1. #include <stdio.h>
  2. #include "input.h"


  3. void input_int(int *a, int *b)
  4. {
  5. printf("input two num:");
  6. scanf("%d %d", a, b);
  7. printf("\r\n");
  8. }
复制代码

calcu.h
  1. #ifndef _CALCU_H
  2. #define _CALCU_H


  3. int calcu(int a, int b);
  4. #endif
复制代码

calcu.c
  1. #include "calcu.h"


  2. int calcu(int a,int b)
  3. {
  4.   return (a+b);
  5. }
复制代码

3.2.2 编译测试一下
  1. chen@ubuntu:~/prj_vscode/linux-c$ gcc main.c input.c calcu.c -o main
  2. chen@ubuntu:~/prj_vscode/linux-c$ ./main
  3. input two num:23 56


  4. 23 + 56 = 79
  5. chen@ubuntu:~/prj_vscode/linux-c$
复制代码
  1. chen@ubuntu:~/prj_vscode/linux-c$ touch Makefile
  2. chen@ubuntu:~/prj_vscode/linux-c$ cat Makefile
  3. main: main.o input.o calcu.o
  4.   gcc -o main main.o input.o calcu.o
  5. main.o: main.c
  6.   gcc -c main.c
  7. input.o: input.c
  8.   gcc -c input.c
  9. calcu.o: calcu.c
  10.   gcc -c calcu.c


  11. clean:
  12.   rm *.o
  13.   rm main
  14. chen@ubuntu:~/prj_vscode/linux-c$ make clean
  15. chen@ubuntu:~/prj_vscode/linux-c$ make
复制代码

4、生成.vscode文件下的debug配置文件
tasks.json:该文件主要用于配置编译的选项,如果使用makefile或者cmake进行编译,则用该文件调研make或者cmake命令进行配置。
launch.json:该文件主要对debugger进行设置
c_cpp_properties.json:编译器路径和intellisense设置

4.1、生成task.json文件

微信图片_20240615144428.png
图4.1.1 Run C/C++File

微信图片_20240615144424.png

修改生成的task.json文件如下
  1. {
  2.     "tasks": [
  3.         {
  4.             "type": "cppbuild",
  5.             "label": "C/C++: gcc build active file",
  6.             "command": "/usr/bin/gcc",
  7.             "args": [
  8.                 "-fdiagnostics-color=always",
  9.                 "-g",
  10.                 "${file}",
  11.                 "-o",
  12.                 "${fileDirname}/${fileBasenameNoExtension}"
  13.             ],
  14.             "options": {
  15.                 "cwd": "${fileDirname}"
  16.             },
  17.             "problemMatcher": [
  18.                 "$gcc"
  19.             ],
  20.             "group": {
  21.                 "kind": "build",
  22.                 "isDefault": true
  23.             },
  24.             "detail": "Task generated by Debugger."
  25.         }
  26.     ],
  27.     "version": "2.0.0"
  28. }
复制代码

修改如下
  1. {
  2.   "version": "2.0.0",
  3.   "tasks": [
  4.     {
  5.       "type": "shell",
  6.       "label": "C/C++: g++ build active file",
  7.       "command": "/usr/bin/g++",
  8.       "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
  9.       "options": {
  10.         "cwd": "/usr/bin"
  11.       },
  12.       "problemMatcher": ["$gcc"],
  13.       "group": {
  14.         "kind": "build",
  15.         "isDefault": true
  16.       },
  17.       "detail": "Task generated by Debugger."
  18.     }
  19.   ]
  20. }
复制代码

command 代表使用什么程序去执行文件,这里使用的是g++. args表示的是传递给g++的参数,表明使用了什么文件(-g 后面的file文件所指内容),生成了什么文件(-o 后面文件所指的内容)。所以这command 和 args两行参数表达的意思是:使用g++编译器将${file}文件进行编译,生成${fileDirname}/${fileBasenameNoExtension}文件。

上面部分是对单个c++文件或者多个c++文件进行编译。但是如果一个项目使用makefile进行编译,则如何使用vscode 进行配置呢?我们这里进行讲解。由于我们使用make进行编译,所以我们将command命令后面加上make,args后面添加相应的参数。比如我们需要进行debug 所以要控制makefile 中所有的gcc编译命令后添加-g。不然的话,就会出现,编译成功了。但是debug的时候,断点无效,所有的执行都是一下子执行完的情况。由于在我们的makefile 中通过TAG = dbg来控制加不加编译选项-g,如图3所示。所以这里我们的参数添加了"TAG=dbg",这里根据不同的makefile进行不同的配置。
  1. {
  2.   "version": "2.0.0",
  3.   "tasks": [
  4.     {
  5.       "type": "shell",
  6.       "label": "Build",
  7.       "command": "make",
  8.       "args": ["NTHREADS=1", "TAG=dbg"],


  9.       "group": {
  10.         "kind": "build",
  11.         "isDefault": true
  12.       },
  13.       // "detail": "compiler: /usr/bin/g++"
  14.     }

  15. }
复制代码

一种可行的Makefile文件内容(gcc后添加-g命令)
  1. main: main.o input.o calcu.o
  2.   gcc -o main1 main.o input.o calcu.o
  3. main.o: main.c
  4.   gcc -c -g main.c
  5. input.o: input.c
  6.   gcc -c -g input.c
  7. calcu.o: calcu.c
  8.   gcc -c -g calcu.c


  9. clean:
  10.   rm *.o
  11.   rm main
复制代码

4.2、生成launch.json文件
(注意只有在.c文件中才会有下图中右上角的图标显示)

微信图片_20240615144421.png

微信图片_20240615144417.png
图4.2.1 添加调试配置文件
  1. {
  2.     "configurations": [
  3.         {
  4.             "name": "C/C++: gcc build and debug active file",
  5.             "type": "cppdbg",
  6.             "request": "launch",
  7.             "program": "${fileDirname}/${fileBasenameNoExtension}",
  8.             "args": [],
  9.             "stopAtEntry": false,
  10.             "cwd": "${fileDirname}",
  11.             "environment": [],
  12.             "externalConsole": false,
  13.             "MIMode": "gdb",
  14.             "setupCommands": [
  15.                 {
  16.                     "description": "Enable pretty-printing for gdb",
  17.                     "text": "-enable-pretty-printing",
  18.                     "ignoreFailures": true
  19.                 },
  20.                 {
  21.                     "description": "Set Disassembly Flavor to Intel",
  22.                     "text": "-gdb-set disassembly-flavor intel",
  23.                     "ignoreFailures": true
  24.                 }
  25.             ],
  26.             "preLaunchTask": "C/C++: gcc build active file",
  27.             "miDebuggerPath": "/usr/bin/gdb"
  28.         }
  29.     ],
  30.     "version": "2.0.0"
  31. }
复制代码

修改生成的launch.json文件如下
  1. {
  2.     "configurations": [
  3.         {
  4.             "name": "C/C++: g++ build and debug active file",
  5.             "type": "cppdbg",
  6.             "request": "launch",
  7.             "program": "${workspaceRoot}/a.out",
  8.             "args": [ "1232"],
  9.             "stopAtEntry": false,
  10.             "cwd": "${workspaceRoot}",
  11.             "environment": [],
  12.             "externalConsole": false,
  13.             "MIMode": "gdb",
  14.             "setupCommands": [
  15.                 {
  16.                     "description": "Enable pretty-printing for gdb",
  17.                     "text": "-enable-pretty-printing",
  18.                     "ignoreFailures": true
  19.                 },
  20.                 {
  21.                     "description": "Set Disassembly Flavor to Intel",
  22.                     "text": "-gdb-set disassembly-flavor intel",
  23.                     "ignoreFailures": true
  24.                 }
  25.             ],
  26.             "preLaunchTask": "Build",
  27.             "miDebuggerPath": "/usr/bin/gdb"
  28.         }
  29.     ],
  30.     "version": "2.0.0"
  31. }
复制代码

"program"选项代表,需要进行调试的程序,所以这里需要修改为对应的文件。"PreLaunchTask" 代表debug流程的前序任务,这里的内容需要和tasks.json中的label相同。"args"可以添加执行程序的过程中,需要添加的参数。在下面的代码中,我们就添加了cacti程序,以及参数。

4.3、生成c_cpp_properties.json文件
ctrl+shift+p 搜索C/C++:Edit configurations(UI) 即可生成该文件
  1. {
  2.   "configurations": [
  3.     {
  4.       "name": "Linux",
  5.       "includePath": ["${workspaceFolder}/**"],
  6.       "defines": [],
  7.       "compilerPath": "/usr/bin/gcc",
  8.       "cStandard": "c11",
  9.       "cppStandard": "c++17",
  10.       "intelliSenseMode": "clang-x64"
  11.     }
  12.   ],
  13.   "version": 4
  14. }
复制代码

05安装Vim
1、命令行安装
  1. chen@ubuntu:~$ sudo apt-get install vim
  2. chen@ubuntu:~$ touch a.txt
  3. chen@ubuntu:~$ sudo vi a.txt
复制代码

06安装gcc-arm-none-eabi
1、下载嵌入式gcc编译器
https://developer.arm.com/downloads/-/gnu-rm

微信图片_20240615144414.png

2、解压gcc-arm-none-eabi-10.3-2021.10
  1. chen@ubuntu:~$ tar -vxjf gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2
  2. #对于bz2、gz文件解压
  3. tar -vxjf test1.tar.bz2
  4. tar -vxzf test2.tar.gz
  5. #对于bz2、gz文件压缩
  6. tar -vxjf test1.tar.bz2
  7. tar -vxzf test2.tar.gz
复制代码

3、添加gcc-arm-none-eabi下的可执行文件命令到Terminal(终端)
  1. chen@ubuntu:~$ sudo gedit .bashrc
  2. #在文末添加下面一行
  3. export PATH=$PATH:~/gcc-arm-none-eabi-10.3-2021.10/bin
  4. #添加上述行
  5. chen@ubuntu:~$ source ~/.bashrc
  6. chen@ubuntu:~$ arm-none-eabi-gcc --help
  7. chen@ubuntu:~$ arm-none-eabi-gcc -v
复制代码

07安装stm32cubemx
1、下载stm32cubemx
https://www.st.com/zh/development-tools/stm32cubemx.html

微信图片_20240615144411.png

2、安装
  1. chen@ubuntu:~/Downloads$ unzip en.stm32cubemx-lin-v6-11-1.zip
  2. chen@ubuntu:~/Downloads$ sudo chmod 777 SetupSTM32CubeMX-6.11.1
  3. chen@ubuntu:~/Downloads$ ls -l
  4. -rwxrwxrwx 1 chen chen 539136342 Apr 11 09:41 SetupSTM32CubeMX-6.11.1
  5. chen@ubuntu:~/Downloads$
  6. chen@ubuntu:~/Downloads$ sudo ./SetupSTM32CubeMX-6.11.1
  7. chen@ubuntu:~/Downloads$ find /usr -name STM32CubeMX
  8. chen@ubuntu:/usr/local/STMicroelectronics/STM32Cube/STM32CubeMX$ ./STM32CubeMX
复制代码

3、增加图标到菜单
  1. chen@ubuntu:~$ /usr/share/applications/
  2. chen@ubuntu:/usr/share/applications$ sudo touch stm32cubemx.desktop
  3. chen@ubuntu:/usr/share/applications$ sudo gedit stm32cubemx.desktop
  4. chen@ubuntu:/usr/share/applications$ cat stm32cubemx.desktop
  5. [Desktop Entry]
  6. Type=Application
  7. Name=STM32CubeMX
  8. Comment=STM32 Development Environment
  9. Encoding=UTF-8
  10. StartupNotify=true
  11. Terminal=false
  12. Categories=Development;java;c;c++;
  13. #icon= ##########图标的路径,自己找一个就行
  14. Exec= /usr/local/STMicroelectronics/STM32Cube/STM32CubeMX/STM32CubeMX
  15. chen@ubuntu:/usr/share/applications$
复制代码

08使用stm32cubemx生成初始代码
1、选择芯片->配置->Generate

微信图片_20240615144405.png

2、一定要记得勾选PinOut-Debug-SerialWire否则后面下载程序时会报错Error: init mode failed (unable to connect to the target) 。这个是巨坑,一定要小心!!!!!!!!!!

微信图片_20240615144355.png

09配置VSCode
1、使用vscode打开上述生成的初始代码
2、生成VSCode配置文件
2.1 c_cpp_properties.jason
ctrl+shift+p->Edit Configurations(JSON)生成.vscode/c_cpp_properties.json文件

微信图片_20240615144351.png
  1. {
  2.     "configurations": [
  3.         {
  4.             "name": "Linux",
  5.             "includePath": [
  6.                 "${workspaceFolder}/**"
  7.             ],
  8.             "defines": [
  9.                 "USE_HAL_DRIVER",
  10.                 "STM32F103xB"
  11.             ],
  12.             "compilerPath": "/home/chen/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc",
  13.             "cStandard": "c99",
  14.             "cppStandard": "gnu++14",
  15.             "intelliSenseMode": "gcc-arm"
  16.         }
  17.     ],
  18.     "version": 4
  19. }
复制代码

2.2 task.json
修改好task.json即可使用ctrl+shift+B对stm32工程进行编译

微信图片_20240615144348.png


微信图片_20240615144344.png


微信图片_20240615144341.png


修改如下:
  1. {
  2.     // See https://go.microsoft.com/fwlink/?LinkId=733558
  3.     // for the documentation about the tasks.json format
  4.     "version": "2.0.0",
  5.     "tasks": [
  6.         {
  7.           "label": "Build",      // 标签名
  8.           "type": "shell",      // 终端(shell)型的任务
  9.           "command": "make",    // *执行的命令
  10.           "args": [          // 命令的参数
  11.             "-j6"          // 表示六线程同时编译
  12.           ],
  13.           "problemMatcher": ["$gcc"],  // 表示如果出现了编译报错,问题面板会显示 gcc类型的错误
  14.           "group": {
  15.             "kind": "build",
  16.             "isDefault": true
  17.           }
  18.         }

  19. }
复制代码

2.3 settings.json

3、上述”defines“字段中的定义应包括stm32cubemx生成的Makefile文件中相关的宏定义

微信图片_20240615144337.png

4、make编译/使用ctrl+shift+B
  1. chen@ubuntu:~/prj_vscode/linux-stm32/test$ make clean
  2. chen@ubuntu:~/prj_vscode/linux-stm32/test$ make
复制代码

10调试
1、安装openocd
  1. chen@ubuntu:~$ sudo apt-get install openocd
  2. chen@ubuntu:~$ whereis openocd
  3. openocd:
  4. /usr/bin/openocd
  5. /usr/share/openocd
  6. /usr/share/man/man1/openocd.1.gz
  7. /usr/share/info/openocd.info-2.gz
  8. /usr/share/info/openocd.info.gz
  9. /usr/share/info/openocd.info-1.gz
  10. chen@ubuntu:~$
  11. //连接好stlinkv2-1到电脑和pcb板,输入以下命令。完成连接
  12. chen@ubuntu:/usr/share/openocd/scripts$ openocd -f interface/stlink-v2.cfg -f target/stm32f1x_stlink.cfg
复制代码

2、在tasks.json中使用openocd进行编译后的下载

在原本的tasks.json中追加popenocd的相关内容
  1. {
  2.     // See https://go.microsoft.com/fwlink/?LinkId=733558
  3.     // for the documentation about the tasks.json format
  4.     "version": "2.0.0",
  5.     "tasks": [
  6.         {
  7.           "label": "Build",      // 标签名
  8.           "type": "shell",      // 终端(shell)型的任务
  9.           "command": "make",    // *执行的命令
  10.           "args": [          // 命令的参数
  11.                           // "-j6"表示六线程同时编译
  12.           ],
  13.           //"problemMatcher": ["$gcc"],  // 表示如果出现了编译报错,问题面板会显示 gcc类型的错误
  14.           "group": {
  15.             "kind": "build",
  16.             "isDefault": true
  17.           }
  18.         },
  19.         {
  20.             "dependsOn":"Build",          // 任务依赖
  21.             "label": "Build and Download",
  22.             "type": "shell",
  23.             "command": "openocd",
  24.             "args": [
  25.             "-f",
  26.             "/usr/share/openocd/scripts/interface/stlink-v2.cfg",
  27.             "-f",
  28.             "/usr/share/openocd/scripts/target/stm32f1x_stlink.cfg",
  29.             "-c",
  30.             "program ./build/${workspaceFolderBasename}.elf verify reset exit"
  31.             ],
  32.             "problemMatcher": []
  33.         }
  34.         

  35. }
复制代码

这个任务要求先进行 Build任务,然后通过命令打开 openocd,加载两个配置文件(-f interface/stlink.cfg 和 -f target/stm32l1.cfg),同时,这个 -c 的选项后面跟着的就是openocd成功加载后执行的内置命令,这里我们让它下载、验证、reset,然后直接退出,这样就实现了一条龙式的下载。

微信图片_20240615144333.png

微信图片_20240615144330.png

3、使用openocd进行调试
3.1 安装Cortex Debug和Venus's Cortex-Debug插件(第二个插件一定要安装,否则会报错Failed to launch OpenOCD GDB Server: Timeout.)

微信图片_20240615144327.png

微信图片_20240615144324.png

3.2、 添加调试配置

微信图片_20240615144321.png

3.3 选择cortex debug,生成launch.json,并修改内容如下

微信图片_20240615144318.png

  1. {
  2.     // 使用 IntelliSense 了解相关属性。
  3.     // 悬停以查看现有属性的描述。
  4.     // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  5.     "version": "0.2.0",
  6.     "configurations": [
  7.         {
  8.             "name": "Cortex Debug",
  9.             "cwd": "${workspaceRoot}",  // 输出路径
  10.             "executable": "./build/${workspaceRootFolderName}.elf",    // 要调试的程序
  11.             "request": "launch",
  12.             "type": "cortex-debug",
  13.             "servertype": "openocd",    // 调试器选择
  14.             "device": "STM32F103C8",    // 使用J-link GDB Server时必须;其他GBD Server时可选(有可能帮助自动选择 SVD文件)。支持的设备见 https://www.segger.com/downloads/supported-devices.php
  15.             //"svdFile": "./STM32L15xC.svd",  // svd文件,有这个文件才能查看寄存器的值,每个单片机都不同。可以在以下地址找到 https://github.com/posborne/cmsis-svd下载
  16.             "interface": "swd",
  17.             "configFiles": [
  18.                 //"${workspaceRoot}/openocd.cfg",  // *可以通过加载该文件来配置
  19.                 "/usr/share/openocd/scripts/interface/stlink-v2.cfg",
  20.                 "/usr/share/openocd/scripts/target/stm32f1x.cfg"
  21.             ],
  22.             "runToEntryPoint": "false",
  23.             //"runToMain": false,
  24.             "preLaunchTask": "Build",   // 在调试前预先执行的任务,此处是tasks.json中的
  25.             //"armToolchainPath": "C:\\Program Files (x86)\\GNU Tools ARM Embedded\\5.4 2016q3\\bin"    // 如果没有把 arm工具链路径添加到系统环境变量,则需要这条指令添加你的路径
  26.         }

  27. }
复制代码

3.4 调试

微信图片_20240615144314.png

11使用VSCode串口工具

1、安装serial monitor

微信图片_20240615144304.png

2、查看连接的usb
  1. chen@ubuntu:~$ lsusb
  2. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
  3. Bus 002 Device 007: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
  4. Bus 002 Device 006: ID 0483:3748 STMicroelectronics ST-LINK/V2
  5. Bus 002 Device 004: ID 0e0f:0008 VMware, Inc. VMware Virtual USB Mouse
  6. Bus 002 Device 003: ID 0e0f:0002 VMware, Inc. Virtual USB Hub
  7. Bus 002 Device 002: ID 0e0f:0003 VMware, Inc. Virtual Mouse
  8. Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
  9. chen@ubuntu:~$
复制代码

2、加权限
  1. chen@ubuntu:~$ sudo chmod 777 /dev/ttyUSB0
复制代码

12使用git
1、检查状态
  1. //在执行 git add 和 git commit 之前,先执行以下命令查看当前仓库的状态:
  2. git status
  3. //这将显示工作目录和暂存区的状态。确保没有未追踪的文件,也确保已将需要提交的文件添加到暂存区。
复制代码

2、确认 Git 配置:
  1. git config user.name
  2. git config user.email


  3. 如果没有设置,请使用以下命令设置:
  4. git config --global user.name "Your Name"
  5. git config --global user.email "your.email@example.com"
复制代码

3、检查文件权限
确保你有权限在新文件夹中进行写操作。使用 ls -l 命令检查文件和目录的权限:
ls -l

4、查看错误信息:
如果 git commit 操作没有反应,尝试使用 -v 参数查看详细的错误信息:
git commit -v

5、清理缓存:
有时 Git 的内部缓存可能导致问题。尝试清理 Git 缓存并重新尝试:
git rm -r --cached .
git add .
git commit -m "Your commit message"
这将清理缓存并重新添加和提交所有更改。


转载自:kevin

如有侵权请联系删除



1.png
收藏 评论0 发布时间:2024-6-15 14:45

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版