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

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

[复制链接]
攻城狮Melo 发布时间:2024-6-15 14:45
“ 从ubuntu20.04入门到完成stm32程序的调试。”
0 M' v8 v$ |8 i% T
0 K" r! T3 V) l7 Z: L4 A7 ^& C
微信图片_20240615144535.png
9 Q. S$ V$ D# r. Z, _) K
: D9 e) T, z- t
1、更换软件源
9 ~8 g  F1 z: `' y/ d) M: u源文件(sources.list)
+ n5 p9 w5 [& e3 r/ t8 ~1 U更改源(Ubuntu20.04对应的源)
3 o6 }: n% j0 T8 ]! d. P更新源
+ L/ m. W8 @. X* V6 M3 M
4 a- {6 h" L% Y: m2 f9 D2、安装拼音输入法
1 ^, k' L/ i( R7 TRegion&Language->Language Support->Installed Languages
2 C8 Y- {0 z% O/ U+ T. r: N/ z7 ^! v! `Input Sources->"+"->chinese->Hanyu Pinyin
0 s* U# E$ h2 ?+ Y9 E% u. s0 M
3 f. P9 l& @: |1 M+ h$ F3、安装VSCode
0 u9 _! f4 V7 Y* q9 ]2 O下载.deb程序包. g0 j) P% R  q! }
使用dpkg安装
2 Z; P0 {  J' z8 s. a- L0 w# y调整设置
& [, ]* \+ C( S7 `启用Title Bar
' K$ ]: ^( u- b6 O安装插件6 Z1 U* ]! S; [' o

$ x. E5 F4 V6 F( K& q! M
4、安装Build Essential及gdb
8 j7 X+ _7 t! f' O5 c3 Mbuild essential介绍
; E7 S" y, Y0 {: s2 H8 {$ m命令行安装build essential和gdb$ D' i0 Y" z5 m0 v
Linux C程序开发简介
5 s2 z' H# g+ H7 Y7 ?无makefile
, n6 O9 \- F2 A6 y9 t6 l有makefile" Y( b/ O5 A" F8 R5 q8 b
生成.vscode文件下的debug配置文件3 q! }- M- W! J1 G# a3 ^7 g  f3 Q0 p
生成task.json文件
8 Q0 y1 S6 g. I$ N- p. n" w生成launch.json文件+ m! c" j$ \# V$ W2 W/ f$ U
生成c_cpp_properties.json文件
4 a+ @9 D; r: v& N
: J/ [# o& S7 `) X& z9 |
5、安装Vim  z) E7 v' f/ p- ^( P# A

$ P2 j0 F/ F, s# [: h' ?4 W6、安装gcc-arm-none-eabi
3 p) u- l0 u- |: y
下载嵌入式gcc编译器6 _$ O  Q& z' O3 q
解压) Z) h' b3 l* X0 i+ T
添加gcc-arm-none-eabi下的可执行文件命令到Terminal1 F- P  I, x, r/ ^
& p1 J, `' ^3 i; K
7、安装stm32cubemx* N8 A6 u( U7 ~8 Z- |
下载stm32cubemx5 X6 {( |* a- Y. V
安装# Q, J0 U! R/ \# _3 j  s8 ~7 a
增加图标到菜单+ d  h! f* p  b8 T( U$ V- N0 l
9 b+ D# `. U9 X
8、使用stm32cubemx生成初始代码) C  g5 W0 A. Q; y: t& r/ r' Z) G
选择芯片->配置->Generate
: z0 y2 P- v7 i  f0 m7 Z0 w" J8 A勾选PinOut-Debug-SerialWire
3 w9 Y1 a4 R* |2 w" J
  E; @9 k$ P  W; W
9、配置VSCode1 ^5 a: R& u! T* \/ @
使用vscode打开上述生成的初始代码6 h5 n) q/ R7 z+ a5 A& k
生成VSCode配置文件2 T% P" I% `. \9 R$ z  D
c_cpp_properties.json
% Q- H% w  e* A" A6 Btask.json9 V0 m$ D2 g" K5 X0 i2 x! }
settings.json
9 e% g6 b4 |: l4 q宏定义包含
$ r2 b0 l0 M# J3 @# imake编译/使用ctrl+shift+B) r/ J+ S  b7 U/ x; D' X2 v

! V2 y5 {  y5 L8 |8 x$ K
10、调试: ]" o3 \. L/ z9 {
安装openocd
6 A1 ]; O9 [% h在tasks.json中使用openocd进行编译后的下载
6 z; y4 }- _5 X; ^使用openocd进行调试
& O8 J  @* k# b. Z安装Cortex Debug和Venus's Cortex-Debug插件, J( `, g! \9 }! d
添加调试配置9 Y2 p! W* c; R) b$ p% l
选择cortex debug,生成launch.json
# J6 M' f; O% ?  i( o! G调试
2 e2 C6 A6 Z' \& l, E3 F5 ]0 S) ^- {& u7 q" F
11、使用VSCode串口工具
4 |4 v2 r/ w$ ^7 O' Z安装serial monitor" w4 T9 P7 K5 [0 z, Q$ ^
查看连接的usb
" ?; k1 g8 u9 V/ W! N5 v( d" [8 H加权限* f% V) P/ B7 o: d: @7 o3 q
$ j- y6 D8 P5 R
12、使用git  J, ~+ O( s3 g
01更换源/ u/ j$ H+ b2 ~+ ~$ ^( S8 m
1、源文件(sources.list)
( |- z( u' ]0 D4 L( Nchen@ubuntu:/etc/apt$ ls -a
* G% K! [! k7 J# q7 D- Y1 F1 S.   apt.conf.d   preferences.d  sources.list    trusted.gpg.d5 `& Z% O7 C, }
..  auth.conf.d  sources.bak    sources.list.d7 U" h) _6 x; A4 R8 \- ^; q
6 G/ O% x* T( Y( Z
2、更改源(Ubuntu20.04对应的源)2 z' e; H# l8 ~3 m: @
  1. chen@ubuntu:/etc/apt$ sudo gedit sources.list
复制代码
  1. #阿里源(Ubuntu20.04)
    : r" X( Z) N* j% `& }2 J6 o. I3 @
  2. deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse* Q. L8 Q7 a7 _7 ?+ D
  3. deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
      Y" N5 `9 v! C1 J) T
  4. deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse# W2 b) I+ t9 P2 E/ o( e* d
  5. deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse- ?4 a! b0 n4 c  Z3 @. I- P& p' _
  6. deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
    & \1 X6 K; W. }% K: T$ b3 U2 j
  7. deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse- X( k. V- g4 P% t6 ~& E
  8. # deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
    - O3 H1 A5 V% U* j/ \. Q
  9. # deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse: e7 G3 w; q+ U: W  }! h
  10. deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse5 c$ h' B5 j% A" b2 R
  11. deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
复制代码
  1. #清华源(Ubuntu20.04)
    % K! q" W6 |' y# h# d: Q
  2. # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释& G; C1 p& t* y' M$ m$ g5 p/ C+ g8 U0 F
  3. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse8 Y9 P% t' N7 I$ D3 s# X
  4. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
    % y4 o3 V  _" z3 x% u# P
  5. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse9 D6 ^/ v  l* i6 {8 U) M. V3 E3 G
  6. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
    1 n. s, Y8 m9 P" L( D
  7. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse0 f: L) y6 }1 J2 C+ J$ f
  8. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
    / n- ^' |/ b" W. w2 C
  9. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse. \! M0 P/ I9 d$ w, `1 E) U% _
  10. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse' {' H3 c$ D2 r1 o% o% z0 U5 C' v

  11. 0 A8 R: S, b* q- b: A3 n' O" m

  12. 1 u! ~$ c; x1 z3 `; C# T
  13. # 预发布软件源,不建议启用# L2 d" U8 g3 k+ Z' E9 Q% {7 |
  14. # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse2 t2 q' v4 }8 }. Z9 ]$ D9 g/ C
  15. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
复制代码
  1. #中科大源(Ubuntu20.04)
    8 y$ t# T) f3 j+ P
  2. deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse& n) T8 H, ?7 r, c/ X& f
  3. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse9 Q- g! L7 M7 M2 G$ D- u
  4. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
    ! V- H! g1 L" Z2 B
  5. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
    6 ]: n" v' x& Z) B; d: A
  6. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
    ' x5 i1 v9 Q  l2 U+ g
  7. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
    6 H# n) D/ s! C) m+ D7 w
  8. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
    , B# H; Q+ N# y% h4 p9 d
  9. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse+ m& H( C6 A- A+ k! Y/ H  C9 Q
  10. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
    & A, P7 I- S! o7 {7 m! m
  11. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
复制代码

" u7 n: p1 x* n3 a6 H/ s2 E3、更新源
% G8 j( \7 g6 M& g
  1. sudo apt-get update
复制代码
& ~* w* m, `; n" Z6 w- R. {, O4 |
02安装拼音输入法
8 U- ?0 \8 t/ n$ s7 z9 C( q1、Region&Language->Language Support->Installed Languages- x3 X1 r) J, M* e3 ]8 W

0 y) b, j, O; b& q, H$ ?) `$ _
微信图片_20240615144532.png
  }; w' n0 a- r5 o6 I2 `( ~1 Y! F5 _2 S1 r
2、Input Sources->"+"->chinese->Hanyu Pinyin
" R" _$ ]( j) |' E2 F
  1. chen@ubuntu:~$ sudo apt-get install ibus-pinyin
    " ?+ U0 H- P+ ~) r2 v
  2. chen@ubuntu:~$ ibus restart
    3 P" h9 ]. C6 o6 Y5 o6 i
  3. chen@ubuntu:~$ ibus-setup
复制代码

( h* R$ O: @3 d 微信图片_20240615144527.png : n" f4 s' F: ~( R

6 N8 S, A3 \6 c! W3 \5 ` 微信图片_20240615144524.png
; \: T8 \3 _3 N: |0 H4 m; i
* q* D0 r0 p$ D) N+ |' |  ?" k6 o 微信图片_20240615144520.png # p0 K6 H- ~# w$ M9 C

' Y) g7 z7 ]8 p7 c03安装VSCODE
, W) v: F: {  u5 S) a$ m' P, ~6 I7 S1、下载.deb程序包https://code.visualstudio.com/' f& \9 |" b, M

1 ^) ^( [! i5 N4 I' H# \6 z2、使用dpkg安装(在Terminal中Ctrl+Shift+C为复制,Ctrl+Shift+V为粘贴)- n  o5 m, t! a6 s3 P9 q; W. R$ _
  1. chen@ubuntu:~/Downloads$ sudo dpkg -i code_1.89.1-1715060508_amd64.deb8 i  N# v8 X. v1 f
  2. chen@ubuntu:~/Downloads$ code .
复制代码
- P0 }8 V: q% ~4 r  _5 y
3、调整设置
) y" \4 [. j8 C1 N# p0 [启用Title Bar:View->Appearance->Editor Actions Position->Tab Bar
/ G, \# Q$ y$ v8 T: A* D/ z  f: k1 N
微信图片_20240615144516.png ' S1 s/ `1 V; S% {' ~6 z7 k
2 x" q  X/ o  z
4、安装插件/ W4 d. |7 E3 L, O3 c& _
git安装- N# P9 Z& P8 J; {
Download Git for Linux(https://git-scm.com/download/linux)3 R5 c/ {, j) `. x  X8 {1 a( @7 p

4 ?, e+ ^* k9 e) P7 X4 ~
微信图片_20240615144512.png
& _$ M; S# u' I/ R7 G# s$ h1 n' H' Z+ m# Z1 X7 q0 F# u* y
微信图片_20240615144509.png , M) a" s" C- P( T* P

5 u- o  k7 v3 G! J7 ?2 M命令行直接安装(Ctrl+Alt+T呼出Terminal)1 T4 u1 [% g1 `9 z% h6 m
  1. chen@ubuntu:~$ sudo apt-get install git
    # U/ `) r+ W+ B6 H3 _
  2. chen@ubuntu:~$ git --version
复制代码

. h9 y' |* Y) R+ Xgit设置1 m5 c3 j. j& P

' C, W6 ?) T* S% E- n4 F
微信图片_20240615144504.png
) m# k: |+ r0 U' k: `% a- z, e' L
0 l9 Q$ d6 T' t
  1. git clone https://gitee.com/kevinkerry/linux-stm32.git
    5 Y6 G$ _) n* s2 F4 p$ j# ?
  2. git config --global user.name 'kevin.kerry'
      [& [  r% Z) _; y2 M! b9 n- y
  3. git config --global user.email 'ct15762287606@outlook.com'6 l+ O, Q3 O: l: v

  4. / q) p3 ^1 t  B
  5. & x7 J6 B* l* x; N9 \4 J* s/ u
  6. #示例- T# `; k5 n0 o4 c2 f
  7. chen@ubuntu:~/prj_vscode$ git clone https://gitee.com/kevinkerry/linux-stm32.git
    4 e& n* D7 V' c* t7 l
  8. Cloning into 'linux-stm32'...; c" Q, D* {' B2 H& Q4 b
  9. Username for 'https://gitee.com': ct15762287606@outlook.com) a* p3 e4 W: Z
  10. Password for 'https://ct15762287606@outlook.com@gitee.com':9 P/ K7 r$ U3 x$ V8 d1 i5 A# {- Q
  11. remote: Enumerating objects: 4, done.
    , p4 J( y& Y0 Z- u( T7 i/ S
  12. remote: Counting objects: 100% (4/4), done.- R5 U8 x8 S6 _  e6 e  G$ z
  13. remote: Compressing objects: 100% (4/4), done.
    ! l+ h, {9 K1 k- \0 a8 z
  14. remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 07 N0 O4 s8 i, X1 ?% l/ F( H8 ~3 E% X1 q
  15. Unpacking objects: 100% (4/4), 1.79 KiB | 1.79 MiB/s, done." Y1 l) }7 P4 b/ G+ Q5 ^
  16. chen@ubuntu:~/prj_vscode$ ls5 L2 K* M& x% t+ M+ q: Y
  17. linux-stm32
复制代码
1 [6 {% _; w* e: c: d
GitLens — Git supercharged插件安装# |- |; U$ |: `% ^: S& [6 e
: Q* [7 X. z! R3 _8 M
微信图片_20240615144459.png
1 |& t: y$ |1 }8 s8 ?

$ J% ]0 k  \+ K" }% D2 {Git History& Y" p% P( }+ N" V# m
' g5 g% ^$ h; o9 e
微信图片_20240615144456.png 3 V, u  A- l" i0 K3 {1 x
/ w( z( T9 \1 f  O2 K, s6 M
Bracket Pair Color DLW
3 K; ]! X6 ]4 \. B' A* B( X* w$ U1 N) l% q5 n1 j- j$ L
微信图片_20240615144454.png
7 C8 S& n9 O6 X5 o/ O, h
2 ~& {* z( I5 y/ r; X! ?& YArm Assembly9 T+ t: g3 v6 \' i0 l
& Z2 H' I7 S$ A+ a. }/ \- l" O( h/ i
微信图片_20240615144451.png ' D9 H* ?+ H, u% s
2 O  h0 z" }% C3 m! o
Venu's Cortex-Debug
( d6 `; ]' }. \+ ?, o; E; {: _, k: \3 x
微信图片_20240615144447.png - U6 \2 ]1 x- L

/ {* m+ i) n% [C/C++) P' p, |5 Z+ Y( ]( W4 a* x

, M  a0 V1 ?9 d' H! g3 Z
微信图片_20240615144437.png
% I2 H. T( F1 C0 _4 m3 B

* b7 f% h! z9 i0 A) X04安装Build Essential及gdb! G7 u* g3 V) {7 Y) H
1、build essential介绍
1 U% w3 P: B& Fbuild-essential 指的是编译程序必须的软件包。它本身不是一个软件,而是一个工具集,包含了在Linux开发程序的一些必要的软件包,包括但不限于gcc、g++、make等。1 P: Z8 ~/ _; [4 X' ^& v7 g. [2 o
3 D3 f' s& [8 s0 t. C
查看该软件包的依赖关系,可以看到以下内容:1 ?* \* z; u7 A# m+ y/ S. M
  1. chen@ubuntu:~$ apt-cache depends build-essential
    2 K  b8 U9 I6 G; R5 f/ C5 Z
  2. build-essential! Q$ ~  R  F' M6 h: R1 x
  3. |Depends: libc6-dev. G5 X! G3 Y5 Z7 H: U- w
  4.   Depends: <libc-dev>. G8 u; [# {  o1 U8 R
  5.     libc6-dev& G$ I: n8 o4 x/ Z( U2 {) h1 f
  6.   Depends: gcc3 O1 o5 Y+ `0 J; {
  7.   Depends: g++* X" p% m! E2 t. [
  8.   Depends: make
    4 y$ T& i$ r: c6 [( N$ v" q2 l
  9.     make-guile
    - U" J5 v0 `# b: x0 }
  10.   Depends: dpkg-dev
复制代码

. G) e. P- V) l/ o* J也就是说,安装了该软件包,编译c/c++所需要的软件包也都会被安装。因此如果想在Ubuntu中编译c/c++程序,只需要安装该软件包就可以了。% k/ V. c0 q. y
# A- i! u; h9 F# D; `: H2 h
2、命令行安装build essential和gdb
. \3 [+ R$ {/ X- V* _有时候可能会需要安装gdbserver. h  V5 `" [3 M4 p
  1. chen@ubuntu:~$ sudo apt-get install build-essential gdb
复制代码

6 n. u" b1 L5 {( @: L" h1 u3、Linux C程序开发简介) V9 Q* y9 c6 e8 Y; f
3.1 无makefile
5 q1 l- U0 n  o: v1 w" X
  1. chen@ubuntu:~$ mkdir linux_c7 }0 p4 t- n9 h  C$ f" C- _1 |0 i
  2. chen@ubuntu:~$ cd linux_c
    8 E5 g/ S1 ?, ?
  3. chen@ubuntu:~$ touch hello.c$ k) q" u" H3 ]  W' ^( Z) ]
  4. chen@ubuntu:~$ sudo vi hello.c8 S6 }! H9 X- g1 ]. [1 x% l' s
  5. chen@ubuntu:~$ cat hello.c! s4 ^% g1 o0 K6 T
  6. #include <stdio.h>
    2 x% m) r  c4 g. H
  7. int main(int argc,char *argv[])
    - |9 n; i1 v+ w& j6 B/ ~% J
  8. {, u7 l' r! ]2 ^- b4 b) P
  9.   printf("hello world\n");
    " r3 ?3 l* ]1 k1 _% L2 q* B
  10. }
      F* H% `7 G* H% h1 f  _( X
  11. chen@ubuntu:~$ gcc hello.c+ f& [+ K) q/ e: |; y3 q; u
  12. chen@ubuntu:~$ gcc hello.c -o hello% {/ ~& g7 O' C; b# I
  13. chen@ubuntu:~$ ls -al
    4 e. f$ N' d6 ^: V% X; X3 e5 t
  14. total 52
    / m2 r5 |5 K) p% j. }, Q. L
  15. drwxrwxr-x 2 chen chen  4096 May 16 18:32 .
    4 R6 b; }# A; w" p) u
  16. drwxrwxr-x 4 chen chen  4096 May 16 17:37 ..
    ; z0 t! e0 I8 O* |5 Q$ u$ h# \! e
  17. -rwxrwxr-x 1 chen chen 16696 May 16 18:29 a.out3 M* b" E. |! U
  18. -rwxrwxr-x 1 chen chen 16696 May 16 18:32 hello, U+ k4 A7 c" e. @+ ?
  19. -rw-rw-r-- 1 chen chen    82 May 16 18:29 hello.c; G- t3 j% s- U1 M: V
  20. chen@ubuntu:~$ ./a.out* l1 U8 ~/ A6 ^/ X8 H4 }. ^) F
  21. chen@ubuntu:~$ ./hello
复制代码

% M* @) c: V7 F5 G6 u5 T3.2 有makefile
" u6 v  ^2 d7 X: u! f3.2.1 创建程序文件
  1. chen@ubuntu:~/prj_vscode/linux-c$ touch main.c input.c calcu.c input.h calcu.h
复制代码

  A, I! ^1 W; P- Dmain.c
8 S6 S6 J: K2 k8 h
  1. #include <stdio.h>+ W+ c9 `! Q: |: y
  2. #include "input.h"
    ) O$ z; O. d1 L7 F
  3. #include "calcu.h"6 H, e2 O$ Y* ]& W$ S

  4. # a, K; F! ^* a: ?  l4 i' J. y

  5. 5 ~9 ^) n* X: d9 u
  6. int main(int argc, char *argv[])
    5 h! s. G2 Y# R% A# e% B6 J" N8 E! ?
  7. {6 r* l3 h9 }$ A% @/ h6 K

  8. ' k+ U. t( `3 X+ V# u2 |9 H/ v
  9. " T- {4 B$ G3 R9 d7 a7 O
  10.   int a, b, num;
    * m$ O* o1 [8 M

  11. 0 S0 J- l. P) V; T5 _4 {

  12. ; B  C6 _0 Y+ B" n+ S
  13.    input_int(&a, &b);" G% M- K$ l0 S
  14.    num = calcu(a, b);+ Y% g% T& W/ r9 F# ]1 O# Z
  15.    printf("%d + %d = %d\r\n", a, b, num);- g, }. @5 _: n- r9 ~% `; J
  16.    # r5 S. j) R0 y$ C* C: u3 Y- t0 W
  17. }
复制代码
! Q; v* S: o6 F5 N
input.h
  1. #ifndef _INPUT_H+ F0 y# S2 k# f* W' Y- L! n( A" K
  2. #define _INPUT_H
    9 ~; q! ]1 _5 \+ D6 z6 Z  A
  3. + E, R  z/ }; q, q1 b& |
  4. void input_int(int *a, int *b);! V0 b, T- P/ t/ ~# @/ R: k
  5. 0 \9 X0 F8 G* o
  6. #endif
复制代码
1 ?3 A% p0 M" Q2 Y2 }3 G" X
input.c
  t$ u  ^0 I/ I2 N& y1 {
  1. #include <stdio.h>* ]2 i* Z" N9 e! [5 l
  2. #include "input.h"
    0 s! t. a& Z/ z+ u8 e& {! t) \
  3. ) a* o/ F4 t0 F' W4 g) c" L$ j

  4. , f# r1 R9 C6 j9 M1 v* a: [
  5. void input_int(int *a, int *b)% a9 ]; {6 z  _* Y
  6. {0 {' ~- |3 j" R) v+ N: r$ c
  7. printf("input two num:");
    * U, G! q! S5 X4 o& |
  8. scanf("%d %d", a, b);9 l9 ~6 M( N( f$ P) r; K
  9. printf("\r\n");: m. e! J- Z+ [" |# v4 x6 k
  10. }
复制代码
" v+ {/ h  Z# ^7 P, n  N
calcu.h
  1. #ifndef _CALCU_H
    1 R2 H& K4 A5 q2 Y+ w6 K+ A! I( b
  2. #define _CALCU_H( x+ h" \6 K& x) B- _8 ^
  3. / G5 ~: J% h7 ], B2 a( m; M" o
  4. : S) u: O0 w7 I  I9 y/ \
  5. int calcu(int a, int b);1 D& \+ i+ v/ Q& W. b% z8 \! t
  6. #endif
复制代码
9 Y# N; M! g4 z9 g2 x
calcu.c* V) K4 x" i" R+ {
  1. #include "calcu.h", B+ x, A5 {9 R: ~& p( F

  2. 1 K9 _2 `1 R9 q7 y

  3. ) A5 Z# Z4 i* O3 u1 b1 I( k
  4. int calcu(int a,int b)) h! U; t0 A5 b% O6 L
  5. {
    ) G; ]6 y) T% j7 D" ?0 q
  6.   return (a+b);
    ( J, c0 N1 C8 I) @
  7. }
复制代码

- n2 K* x$ p: G2 i3.2.2 编译测试一下- K+ S% A" R, E+ |0 j
  1. chen@ubuntu:~/prj_vscode/linux-c$ gcc main.c input.c calcu.c -o main
    . s! e7 @$ m% u" k3 ?. ^/ _$ b
  2. chen@ubuntu:~/prj_vscode/linux-c$ ./main8 M1 f) n. i( X5 k% M9 p* H/ L2 K; T
  3. input two num:23 56* j% C1 o5 K* E+ o. T2 w1 h
  4. # f# g3 t3 z8 h4 j5 n. |; P

  5. & C7 _2 c3 `( C5 u- k. b# x
  6. 23 + 56 = 79
    ! G" k3 y5 {( D% R
  7. chen@ubuntu:~/prj_vscode/linux-c$
复制代码
  1. chen@ubuntu:~/prj_vscode/linux-c$ touch Makefile* K% d( o! \" ?. N+ j
  2. chen@ubuntu:~/prj_vscode/linux-c$ cat Makefile
    : U- P/ r; k8 k0 p) i2 [# B! G
  3. main: main.o input.o calcu.o
    2 ~* ?; Q% x9 D& h* _3 z3 z: r
  4.   gcc -o main main.o input.o calcu.o
    ! e0 s/ n; o( Q5 V/ m* O) b. Q
  5. main.o: main.c0 j. O  i5 ]0 x" H& f, H
  6.   gcc -c main.c% Z# }- N' C$ X4 }8 h5 A0 J; J/ ]
  7. input.o: input.c. U2 `4 H0 z  h
  8.   gcc -c input.c
    : `. {8 z6 B3 @, C% ~" r" q' y
  9. calcu.o: calcu.c
    ! f8 @' W7 b( N" e
  10.   gcc -c calcu.c! _" j) `: ^+ G' [3 |
  11. # ]+ N8 d- q8 |- T+ G. }" a, |

  12. & v& r! e# N1 u3 @
  13. clean:! X- z; L( A+ Q+ f+ v
  14.   rm *.o# |+ T2 {. h; T% P  b% f, l
  15.   rm main
    : a, z, y; S( |$ j# ~+ A
  16. chen@ubuntu:~/prj_vscode/linux-c$ make clean6 L7 `# r: I% |; o+ p% `
  17. chen@ubuntu:~/prj_vscode/linux-c$ make
复制代码
3 F" X) v* q( M
4、生成.vscode文件下的debug配置文件9 E" L. a, F8 U" m
tasks.json:该文件主要用于配置编译的选项,如果使用makefile或者cmake进行编译,则用该文件调研make或者cmake命令进行配置。
7 S: U7 {4 z: @% u2 L, klaunch.json:该文件主要对debugger进行设置
$ z. R! E% z: A+ Lc_cpp_properties.json:编译器路径和intellisense设置
4 @6 ?9 @; G  Q

  B" J, u7 p& G5 v' R4 W4.1、生成task.json文件( J( D# M  [# y$ |" U8 S

2 d- W! i/ j' |8 ]
微信图片_20240615144428.png $ {# v+ O" O6 c9 U( g/ k' w$ D
图4.1.1 Run C/C++File
' Q7 g; e0 ~; W1 j3 R
+ x8 [6 D. I5 M( V9 D8 U+ j
微信图片_20240615144424.png
5 v  p( z6 Q4 O$ A* W9 a& k+ |1 t  l
修改生成的task.json文件如下1 O- y; S" W4 v' d
  1. {8 x( g+ {! a/ _. m0 M  p
  2.     "tasks": [5 g! V# \2 {2 G5 E( n! K
  3.         {$ q7 p: Y: P* t* i! S
  4.             "type": "cppbuild",
    1 W) w% q5 v& }+ m! q
  5.             "label": "C/C++: gcc build active file",8 x  T: ?% ?; H9 J2 m1 h2 g
  6.             "command": "/usr/bin/gcc",
    % ~. K7 k# O% |
  7.             "args": [
    ; j! d7 y6 x' i
  8.                 "-fdiagnostics-color=always",
    1 W" |/ g* d: Z, s2 R+ Z' s% S+ C
  9.                 "-g",5 B# n% o- g( S: |
  10.                 "${file}",; W/ G6 {3 W& v8 F# ?5 y# E2 h
  11.                 "-o",
    + C1 _$ r5 n6 J. @+ T
  12.                 "${fileDirname}/${fileBasenameNoExtension}"
    / a" P1 l6 D; H: p1 E# R9 F
  13.             ],2 ~/ ?* g) x# k; K8 X' j' A) c
  14.             "options": {
    ; L8 b8 `8 w) [8 X& \
  15.                 "cwd": "${fileDirname}"
    5 m7 B: e, r# e- F. T9 j
  16.             },
    / L& T# e. J! f* d# v
  17.             "problemMatcher": [
    6 j& @$ O& l: |/ m. v* u4 J1 B) X
  18.                 "$gcc"
    3 K# u) f. q$ e" a5 ]& u; c
  19.             ],
    % G' y( k8 K" a# t
  20.             "group": {, D4 r- ~8 _( p0 e6 C8 ]2 L
  21.                 "kind": "build",2 s# U/ @7 H+ ?
  22.                 "isDefault": true
    7 e) `, ?, p, u9 {
  23.             },
    9 ^0 W4 w; d1 |  F. V8 @* Q3 L; J
  24.             "detail": "Task generated by Debugger."
    4 b$ _0 ]5 u! q) ]) X0 b; Q
  25.         }
    % `) y. v/ o2 K. }7 C
  26.     ],# Z! s0 Z6 ]& H) a/ m. D. c& R. x
  27.     "version": "2.0.0"
    0 b9 ]5 a" a  L, W% F
  28. }
复制代码

& n  [8 N' }: L+ t3 ]7 P修改如下
  1. {5 l- a# J  G$ T, v: B: @$ k
  2.   "version": "2.0.0",6 T! E* [$ l+ A" Y/ F2 c
  3.   "tasks": [
    $ b+ X/ A* o6 t; u* g  B4 B! [
  4.     {0 h- Q. b# n# d5 w
  5.       "type": "shell",
    ' ~1 l) s; [3 T& c, ~5 R
  6.       "label": "C/C++: g++ build active file",
    " w; k, x1 i* a
  7.       "command": "/usr/bin/g++",
    , U8 S/ W. p4 p) o! h
  8.       "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],& I/ x( I% k3 h+ y( a
  9.       "options": {
    ; x. |" ~3 T5 Y1 d2 {( l  X
  10.         "cwd": "/usr/bin"
    # Y$ l1 C5 n  Z' \6 R" t* S/ [
  11.       },
    1 Z3 `" ]/ q$ W* \
  12.       "problemMatcher": ["$gcc"],
    : g  i, g8 A' P& b. j6 u1 _. E
  13.       "group": {" I# \: c3 a% f3 o/ E$ d0 s
  14.         "kind": "build",
    * N7 f' u* e# _8 G
  15.         "isDefault": true
    # R* `. V: i/ q# S5 a
  16.       },2 `3 M3 v1 d  |; Z1 k8 {
  17.       "detail": "Task generated by Debugger."
    , ?3 A( ^1 i+ f, W6 A% F$ F
  18.     }
      U* T; ]. w5 q* S7 R* a
  19.   ]
    ; p5 l, @4 m! P0 ?0 M6 D
  20. }
复制代码

8 S( m4 y8 ?6 I5 x2 R$ [command 代表使用什么程序去执行文件,这里使用的是g++. args表示的是传递给g++的参数,表明使用了什么文件(-g 后面的file文件所指内容),生成了什么文件(-o 后面文件所指的内容)。所以这command 和 args两行参数表达的意思是:使用g++编译器将${file}文件进行编译,生成${fileDirname}/${fileBasenameNoExtension}文件。
% \3 D' H" n) U5 @4 s
+ Z5 j4 `+ z& f% ]0 `# d+ L. P% Y# Q
上面部分是对单个c++文件或者多个c++文件进行编译。但是如果一个项目使用makefile进行编译,则如何使用vscode 进行配置呢?我们这里进行讲解。由于我们使用make进行编译,所以我们将command命令后面加上make,args后面添加相应的参数。比如我们需要进行debug 所以要控制makefile 中所有的gcc编译命令后添加-g。不然的话,就会出现,编译成功了。但是debug的时候,断点无效,所有的执行都是一下子执行完的情况。由于在我们的makefile 中通过TAG = dbg来控制加不加编译选项-g,如图3所示。所以这里我们的参数添加了"TAG=dbg",这里根据不同的makefile进行不同的配置。  n0 S" `2 W& b9 n
  1. {
      f, U$ t! A% X" Y" V0 }
  2.   "version": "2.0.0",, f& W: _8 L1 u# ~! A0 ^
  3.   "tasks": [8 r, f1 x2 [. T& C8 y" i
  4.     {9 l& t5 H! ^/ N+ G" U5 h
  5.       "type": "shell",
    - Z0 i6 l* L+ q& m. o0 ?
  6.       "label": "Build",
    ( E5 ]' ~5 q" h4 a: W
  7.       "command": "make",, P! ]; l9 }$ E  Z
  8.       "args": ["NTHREADS=1", "TAG=dbg"],
    % v  |2 c0 w0 U/ x/ i' w
  9.   _5 @; v, u: Y
  10. & {& p7 O/ @- _8 B
  11.       "group": {5 a) s% u: V/ T
  12.         "kind": "build",
    # X7 H4 @; y- A" v+ X
  13.         "isDefault": true
    % U& `, n2 s- e% r
  14.       },
    4 T/ }3 A  c* }! j( f
  15.       // "detail": "compiler: /usr/bin/g++"
    : H! l8 g( H! Y) K
  16.     }# u( {* L* e6 b' S& K

  17. , v* b& C) e. Y0 H' p" j, X# d
  18. }
复制代码

- `# ?( B1 e, z+ b一种可行的Makefile文件内容(gcc后添加-g命令)+ U2 _9 N, B; Z8 G
  1. main: main.o input.o calcu.o6 P/ f5 p4 _) a5 U
  2.   gcc -o main1 main.o input.o calcu.o- B( U% Z- C- a: e* {% Q
  3. main.o: main.c8 m6 D1 t+ X' Z: K. f6 n( N8 s
  4.   gcc -c -g main.c
    2 _/ K# }5 G, Y( P1 F) h3 ^
  5. input.o: input.c. H5 \3 D$ @( U3 C% }6 M( F9 J
  6.   gcc -c -g input.c& j" I# S6 z# B+ |( e
  7. calcu.o: calcu.c
    9 \1 T7 O7 E( I! l4 a; L* ?
  8.   gcc -c -g calcu.c$ F& n$ N/ R' o  x$ O: ]
  9. - r% q" ^8 Z( s' R

  10. , W1 o8 m7 A) A% v
  11. clean:
    - u9 C- r- u7 d8 Q- B' t# j$ c- }
  12.   rm *.o. r, z! d' K; L7 J
  13.   rm main
复制代码
4 [  r) h% o  i
4.2、生成launch.json文件
% v& i- i6 e! z# d3 m7 E(注意只有在.c文件中才会有下图中右上角的图标显示)
1 \- M) H. I' o6 F
( |; ]& W* b+ s0 d- A& H& K# ]% h
微信图片_20240615144421.png
8 u  Y9 R6 ]: n: M; u  V' e" n% [! F/ R/ u$ W# h5 h% a  c
微信图片_20240615144417.png
) ]. F, p5 Q( G  a1 N0 O! O& a8 X图4.2.1 添加调试配置文件
. z6 z% O: z+ o4 |( R
  1. {! W, ]8 P) C: J. T
  2.     "configurations": [
    $ x7 W# b1 B# d$ D% ]
  3.         {
    * q* q8 e4 n( a& }# ]8 Q7 r
  4.             "name": "C/C++: gcc build and debug active file",
    6 o$ u9 p7 r0 `2 u2 G9 A/ ?
  5.             "type": "cppdbg",
    3 Q8 C& X7 \6 C" k- e
  6.             "request": "launch",& V2 d% W4 p" s( e+ A4 U1 R4 [% e
  7.             "program": "${fileDirname}/${fileBasenameNoExtension}",
    ; {' x1 V5 G& l2 ?& n4 f8 X2 d
  8.             "args": [],
    ; O( R, g! ~* }4 v. E1 F
  9.             "stopAtEntry": false,
    & D" B: D7 |7 p# }& c. g/ t
  10.             "cwd": "${fileDirname}",' f# [0 P3 k9 S: i
  11.             "environment": [],9 K9 q# c1 S6 b$ Y
  12.             "externalConsole": false,
    , j9 `$ w7 K5 d. P
  13.             "MIMode": "gdb",
    % E) N& D/ E, @" V
  14.             "setupCommands": [
    " R7 d! A( k: N
  15.                 {
    ; {& t* W, Y7 N! g, I4 U
  16.                     "description": "Enable pretty-printing for gdb",
    * s5 d# n2 f& B+ Z
  17.                     "text": "-enable-pretty-printing",
    / Y8 R/ A7 U" t
  18.                     "ignoreFailures": true2 ~) a) A! P2 U) U2 v
  19.                 },' n% [, [% y$ s+ J; T! q5 h3 J, [
  20.                 {& M8 A9 o0 J9 s$ v9 D
  21.                     "description": "Set Disassembly Flavor to Intel",$ j! T# a5 p4 X8 t. W, |/ I
  22.                     "text": "-gdb-set disassembly-flavor intel",
    ; W3 N& o7 E3 h: t: b- k0 P
  23.                     "ignoreFailures": true
    ) {  M# c; P/ b3 D& _' a
  24.                 }
    , x) x# p3 `; ^( m- |$ p
  25.             ],/ b  e; |' g& N+ f9 L2 L
  26.             "preLaunchTask": "C/C++: gcc build active file",) U' V) ^/ m! g, o0 q) G
  27.             "miDebuggerPath": "/usr/bin/gdb"" q% G9 l" K# s! _. k7 O
  28.         }2 u- m/ h  c5 {( j" z& Q$ E
  29.     ],9 i1 Q. a* V* v5 m
  30.     "version": "2.0.0"# j  `4 Y$ y* h5 S( ^3 T
  31. }
复制代码

: c3 L9 D0 m+ g9 b修改生成的launch.json文件如下  G  p9 Z3 }$ x& A4 W
  1. {) v/ D( p9 D4 v. B  P5 l4 B  v2 O
  2.     "configurations": [
    9 M; ]: I* d) I; U4 s' K5 i* A
  3.         {8 L8 s9 E2 y! u3 T  W9 @/ V- V
  4.             "name": "C/C++: g++ build and debug active file",2 b1 k1 H) Z( o: [
  5.             "type": "cppdbg",
    ) c. U. }+ I  u9 M  D$ I; w
  6.             "request": "launch",4 W* T/ o4 t3 F, y4 w. }/ E. v
  7.             "program": "${workspaceRoot}/a.out",3 |5 Y2 z' o9 X% D6 J) ?% p* ~
  8.             "args": [ "1232"],
    + |- o8 k6 W, Y* W" l9 k
  9.             "stopAtEntry": false,
    . U/ Z# Z2 \* X: B6 E8 {
  10.             "cwd": "${workspaceRoot}",
    $ Z4 @8 f  E0 E4 d- _. l
  11.             "environment": [],, {0 F, N; }% W5 C
  12.             "externalConsole": false,3 F  K1 f# G8 c, ]: c* W
  13.             "MIMode": "gdb",; P/ ^- D" F: o( E2 Z
  14.             "setupCommands": [8 ?+ r9 U/ \& @- O% w
  15.                 {! C) Z4 W; b+ k: h- P. r+ ^+ c" E
  16.                     "description": "Enable pretty-printing for gdb",
      s  |# {1 p# n9 [8 c/ g
  17.                     "text": "-enable-pretty-printing",( ^9 J1 Y0 \, @8 f( v
  18.                     "ignoreFailures": true' K: w& w: B  ]$ f8 k2 x- d
  19.                 },( P. Z9 X  P0 d2 F8 n8 X3 }
  20.                 {! f, N. Q: ?  C* r
  21.                     "description": "Set Disassembly Flavor to Intel",( e' c# S6 m0 f/ K2 D3 w/ X. Y6 y& U
  22.                     "text": "-gdb-set disassembly-flavor intel",
    " h* a% Y% Y7 `8 t
  23.                     "ignoreFailures": true0 M* d/ T( e6 j  A) ^  p
  24.                 }
    ; T) _4 m  f$ v2 A  e! m
  25.             ],5 Z( w9 `2 ?% H/ x
  26.             "preLaunchTask": "Build",5 K1 p* J4 K4 A! [
  27.             "miDebuggerPath": "/usr/bin/gdb"
    ' c, }, V0 o1 h' u3 G' D& `. w0 z% `
  28.         }8 X. y9 a. ^4 O1 W( c* y
  29.     ],
    , D4 u( Q1 ~9 f8 |2 h, k$ L& K4 d6 e! k
  30.     "version": "2.0.0"
    : B& y, [& a- B+ G) q" ]
  31. }
复制代码

% j2 J. m$ y% P6 c2 v' |"program"选项代表,需要进行调试的程序,所以这里需要修改为对应的文件。"PreLaunchTask" 代表debug流程的前序任务,这里的内容需要和tasks.json中的label相同。"args"可以添加执行程序的过程中,需要添加的参数。在下面的代码中,我们就添加了cacti程序,以及参数。, y3 a  ?, k, |& o. S1 W' D
5 U8 k3 J6 i3 Z
4.3、生成c_cpp_properties.json文件
' c8 _' D' z( e( p7 ]ctrl+shift+p 搜索C/C++:Edit configurations(UI) 即可生成该文件
  g1 @# ^% V: K5 f) }) b& P! [1 F+ i) X% y
  1. {
    ! R* O' o) l7 \$ U
  2.   "configurations": [
    " D" q5 _, g( H  V3 o! [% t" {. d
  3.     {
    ( o- _  p, L: k" |" g8 N' l
  4.       "name": "Linux",/ G. J# [% Q4 h$ f4 |% W$ U; i
  5.       "includePath": ["${workspaceFolder}/**"],
    0 p; K9 |. a% n* U
  6.       "defines": [],0 e2 {4 V. Z( }2 o9 v
  7.       "compilerPath": "/usr/bin/gcc",
    . x0 b4 o: o/ H  }+ A% }" s
  8.       "cStandard": "c11",
    % r: t# V# O8 p" v% I! Q/ b
  9.       "cppStandard": "c++17",2 @  J1 V" _" e% o; s9 q
  10.       "intelliSenseMode": "clang-x64"
    6 r/ F+ P8 G! n$ ^+ U
  11.     }
    0 o9 u, S. X  _8 W
  12.   ],* k+ N# r( H! X' e$ Z
  13.   "version": 43 l  U$ P- x$ i8 c/ T& c
  14. }
复制代码

# V- Y+ w" u* F: S, \8 L  M0 J05安装Vim: @0 G( n7 f3 l* h! o7 [
1、命令行安装" X7 G+ Q/ X8 m$ O& v
  1. chen@ubuntu:~$ sudo apt-get install vim
    ( E$ }. }) a' ~
  2. chen@ubuntu:~$ touch a.txt
    0 C1 }9 h8 Q. D3 M' s0 X
  3. chen@ubuntu:~$ sudo vi a.txt
复制代码

6 e7 Z5 ?& C" d" a& }06安装gcc-arm-none-eabi0 ^. J+ R( X, h+ t& t8 q" n" A. r  s
1、下载嵌入式gcc编译器
& d( C9 \% N- Q! j  phttps://developer.arm.com/downloads/-/gnu-rm9 M6 k5 m- r5 d( ?

4 v- n' o0 w5 x8 H" f5 m
微信图片_20240615144414.png
* r  L) m/ M6 Y) x$ U
7 R1 Y6 c. n! d5 f" A" f2、解压gcc-arm-none-eabi-10.3-2021.106 U) }. x2 r  [' L" L( Y
  1. chen@ubuntu:~$ tar -vxjf gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2( ^1 ?$ r0 C" {' O" o* K
  2. #对于bz2、gz文件解压0 \2 Z1 F8 @6 u6 O3 b
  3. tar -vxjf test1.tar.bz2
    3 @0 y2 h) \% N- L
  4. tar -vxzf test2.tar.gz
    / j: Z! _, j0 Y) h( V& L
  5. #对于bz2、gz文件压缩
    $ y* g9 H3 i- r+ V* I: F% C' X
  6. tar -vxjf test1.tar.bz2
    * l% ]' k0 K" L3 R4 W  ?6 Z" Z
  7. tar -vxzf test2.tar.gz
复制代码

; b; Z) J4 p2 [# p, U+ L0 j9 i, K3、添加gcc-arm-none-eabi下的可执行文件命令到Terminal(终端)
7 L  D9 {' y$ X- Z! t; X& _1 p5 p6 Z
  1. chen@ubuntu:~$ sudo gedit .bashrc
    . s. U$ [" C  G
  2. #在文末添加下面一行
    : ]; L. Y2 s! z) t" j3 `
  3. export PATH=$PATH:~/gcc-arm-none-eabi-10.3-2021.10/bin6 {( B6 G; v1 P
  4. #添加上述行
    9 |5 n& ~& S. ^; S
  5. chen@ubuntu:~$ source ~/.bashrc
      Q/ v4 I6 m' @7 e( `. q; P
  6. chen@ubuntu:~$ arm-none-eabi-gcc --help4 U+ W( e0 E* z' q' v$ D& Z
  7. chen@ubuntu:~$ arm-none-eabi-gcc -v
复制代码

' s, n! ]9 R! I% X$ i, X( o6 `5 ]07安装stm32cubemx
4 {1 _( B6 \. C2 }; t' H0 F( o1、下载stm32cubemx
$ u6 p8 T: T; A3 thttps://www.st.com/zh/development-tools/stm32cubemx.html
0 K- F- M) m: v" k6 R( \, y5 G
( x: D8 i; U7 f& q4 S0 e
微信图片_20240615144411.png
& r' _0 m5 w& B4 H* t, a: G% c. }
$ q/ a) Y2 a: r" ]2、安装
  1. chen@ubuntu:~/Downloads$ unzip en.stm32cubemx-lin-v6-11-1.zip
    % U2 v' P3 {# j* T, K% j9 W# D
  2. chen@ubuntu:~/Downloads$ sudo chmod 777 SetupSTM32CubeMX-6.11.1
    # A+ y- I' K8 A0 S$ W! N
  3. chen@ubuntu:~/Downloads$ ls -l  e1 G. K5 \- G5 B% j- B/ e
  4. -rwxrwxrwx 1 chen chen 539136342 Apr 11 09:41 SetupSTM32CubeMX-6.11.17 |/ D2 F( u  S+ j: }* {' W( D
  5. chen@ubuntu:~/Downloads$8 F4 |$ N/ d3 a% s3 E& Y
  6. chen@ubuntu:~/Downloads$ sudo ./SetupSTM32CubeMX-6.11.1. I+ D, _: @, E1 p' @& l
  7. chen@ubuntu:~/Downloads$ find /usr -name STM32CubeMX6 B' @1 X5 B9 q* k2 R9 a
  8. chen@ubuntu:/usr/local/STMicroelectronics/STM32Cube/STM32CubeMX$ ./STM32CubeMX
复制代码
5 Q" ^. c' R& _( H( b( A
3、增加图标到菜单* l, f7 u- A0 Q( A0 O
  1. chen@ubuntu:~$ /usr/share/applications/: a1 t' E# p1 k' o
  2. chen@ubuntu:/usr/share/applications$ sudo touch stm32cubemx.desktop
    0 C( Q& J- D0 Y# `7 R
  3. chen@ubuntu:/usr/share/applications$ sudo gedit stm32cubemx.desktop5 ~# {5 W4 w- o
  4. chen@ubuntu:/usr/share/applications$ cat stm32cubemx.desktop& N* k  z' w/ ]4 {0 x5 C" l
  5. [Desktop Entry]
    5 T# @* H3 w4 ^/ h2 m& h$ G
  6. Type=Application
    & a, k+ y; Z' `/ G
  7. Name=STM32CubeMX' n0 F& W. y! _9 h$ k# z# n' I
  8. Comment=STM32 Development Environment. R' t( s4 J5 R+ w7 N: b, ?
  9. Encoding=UTF-8
    1 [' N  w3 {4 i8 D5 j! o
  10. StartupNotify=true
    5 A# q& ^: D% _  k$ \0 i5 R
  11. Terminal=false5 X2 L% j5 h; }+ w) J% v3 L
  12. Categories=Development;java;c;c++;
    & b# V  \% d. d- E& {/ l* W( ?
  13. #icon= ##########图标的路径,自己找一个就行( e* x" |. v* A2 X# @+ ?
  14. Exec= /usr/local/STMicroelectronics/STM32Cube/STM32CubeMX/STM32CubeMX
    & I. f. n9 M) n$ W
  15. chen@ubuntu:/usr/share/applications$
复制代码

! a$ F3 v# c' m& D  M, j" Q" C08使用stm32cubemx生成初始代码5 b/ f. d$ i- }/ v
1、选择芯片->配置->Generate . E4 s5 Q0 s  @2 Z! q; _0 e6 z# A+ a

! k  p# P! A2 |0 r' e- o
微信图片_20240615144405.png 3 f3 n3 C4 q% D" c5 r! h+ u+ o9 M
5 {. e0 R0 [  \
2、一定要记得勾选PinOut-Debug-SerialWire否则后面下载程序时会报错Error: init mode failed (unable to connect to the target) 。这个是巨坑,一定要小心!!!!!!!!!!
: Q2 [7 ~4 |5 x* P0 m( f
2 ^' y& E9 y1 D; _
微信图片_20240615144355.png 6 p9 B( e$ L4 U# A- L7 A7 J# B8 u* p
7 e6 {3 U+ J; q& Q8 C+ q( u
09配置VSCode
$ c/ @. Q* {( Y3 m  k; l1、使用vscode打开上述生成的初始代码6 [" ^) R) P, z
2、生成VSCode配置文件
) G$ u+ B2 ~3 s2 ~0 }3 k2.1 c_cpp_properties.jason
4 Z% Q: ?+ {" I: T: G- octrl+shift+p->Edit Configurations(JSON)生成.vscode/c_cpp_properties.json文件
% C% a# B- U# u5 O7 Y3 Q7 X, L" o
9 A$ v7 h9 @- r! o% \% w
微信图片_20240615144351.png   x1 k) I, J* y( y
  1. {( k* p) y% G8 c1 i& f; p, a% w3 W
  2.     "configurations": [% b1 B) l5 f& x! b6 L& @/ n
  3.         {
    8 D% w; x% P+ t8 }, G. h  ?# k: S% L. I
  4.             "name": "Linux",1 M3 w8 l+ o, g: s$ z6 V, w
  5.             "includePath": [7 {+ z" N, @) y' P4 o& _
  6.                 "${workspaceFolder}/**"/ O/ M5 l6 m1 h% G0 M
  7.             ],
    5 i* [2 {7 f% q% G: `; ~
  8.             "defines": [
    7 G$ M& b6 Z; k7 p. s5 e( H
  9.                 "USE_HAL_DRIVER",
    # T/ F  @$ i: _: i8 \9 q0 Z( E
  10.                 "STM32F103xB"
    & F" P- ~0 u$ d2 d# v8 M
  11.             ],
    ! w% b) z" Y+ l! o7 u! o
  12.             "compilerPath": "/home/chen/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc",
    ; v/ r6 M* {6 d0 C2 \
  13.             "cStandard": "c99",
    4 l0 K7 W! K* U+ Z3 N/ o$ W% W
  14.             "cppStandard": "gnu++14",; F& `9 r/ t& r/ s& p3 t
  15.             "intelliSenseMode": "gcc-arm"
    / F; a- J$ n4 G( T
  16.         }, E6 a) R. H/ j5 V
  17.     ],* _3 M2 w% x# {1 |
  18.     "version": 4
      \* k) |; W4 h5 k
  19. }
复制代码

* H* U. I; i/ W6 e3 f% R2.2 task.json
* J& Q( G7 c0 j! q修改好task.json即可使用ctrl+shift+B对stm32工程进行编译
* Z; _" O$ h! }% o3 H7 u& I* ]
( x$ q& E3 `1 K" S7 s
微信图片_20240615144348.png
  T3 _& |7 `7 J5 |" T# Q& g9 m! k! z' p- Q  V
+ B' G" F4 G0 w& `* x
微信图片_20240615144344.png # e: q, {; f* \' J; r
2 X, R2 c% |9 j& \& I$ R4 a; M

+ m* W( I0 C3 q1 ^- e" ~ 微信图片_20240615144341.png
3 d# ~7 H% F* }" ?
+ R; w- m! E; J& R. f! r
* V0 J, r+ s$ F, \# O- J: E修改如下:6 A( ^2 u# b0 T, @
  1. {) z  q+ d  Z4 l* E
  2.     // See https://go.microsoft.com/fwlink/?LinkId=733558! U. A# ~' o: }+ T
  3.     // for the documentation about the tasks.json format) c# u3 w. i( S* a
  4.     "version": "2.0.0",9 F: x2 z9 W7 Y% r; X+ [6 i+ M4 k6 h
  5.     "tasks": [
    $ _$ k& Y, [# [& s+ ~- h
  6.         {
    - T) }* M2 ?7 {2 @0 T
  7.           "label": "Build",      // 标签名( i# w2 V3 f( o/ [& E- w
  8.           "type": "shell",      // 终端(shell)型的任务  `7 B0 p7 Q# l
  9.           "command": "make",    // *执行的命令0 e9 p* w5 r5 N
  10.           "args": [          // 命令的参数, j' }/ T/ E% m* x8 ]1 s$ P; P
  11.             "-j6"          // 表示六线程同时编译8 h- B, a  W1 h- j+ d4 E
  12.           ],
    ( C/ |0 n$ K# N- x* }" F
  13.           "problemMatcher": ["$gcc"],  // 表示如果出现了编译报错,问题面板会显示 gcc类型的错误
    % v+ b, U' i$ \% O5 J
  14.           "group": {( T) A+ `% f' `+ e
  15.             "kind": "build",
    8 Q" {  k. o' f/ j# |) f5 m# ^, A
  16.             "isDefault": true9 V( x1 ~* Y0 y$ V$ j" d7 F$ ^" C, d
  17.           }/ v9 h: t7 }+ {+ `( T. C7 S
  18.         }8 B# A4 k+ q! ^* Q- |
  19.   e' n- L  m6 Z
  20. }
复制代码
4 ?& E. v/ Z5 I' X. C1 I* h
2.3 settings.json9 U" R- m/ V8 m- {  a, v" Y

. _* C1 p! q9 s8 t3、上述”defines“字段中的定义应包括stm32cubemx生成的Makefile文件中相关的宏定义
4 k3 i, }4 z4 J  H+ U& P1 {: }/ x- {6 f6 C( q# I3 B
微信图片_20240615144337.png
: r% C, C! D$ S. Z& L) N
9 u' s! ^% I; s! Z: {4、make编译/使用ctrl+shift+B5 t; X7 f7 j" n! c# ?
  1. chen@ubuntu:~/prj_vscode/linux-stm32/test$ make clean) v$ l4 r9 e" O8 j: h
  2. chen@ubuntu:~/prj_vscode/linux-stm32/test$ make
复制代码
6 d7 K1 Y- k+ y3 w0 A, I
10调试
& ]" \2 S' @. E  L4 h/ u. e1、安装openocd
  1. chen@ubuntu:~$ sudo apt-get install openocd / }" p! p# F3 U3 G3 g% I) x3 Y
  2. chen@ubuntu:~$ whereis openocd
    4 M& }+ }0 E" A7 y+ m8 i
  3. openocd: & q/ Z( \5 z2 x; w4 D" S- e  Q5 Y
  4. /usr/bin/openocd
    . e4 F6 P, [; P0 I; C
  5. /usr/share/openocd
    * {. H# p( t) W1 h
  6. /usr/share/man/man1/openocd.1.gz 5 m+ g* p; O1 |# }$ }
  7. /usr/share/info/openocd.info-2.gz 9 Z9 P& J8 S; V; k" I3 T5 B
  8. /usr/share/info/openocd.info.gz
    % Y$ m  k/ @) a$ n# Z
  9. /usr/share/info/openocd.info-1.gz) u# R, e% X7 u; B  r/ ]
  10. chen@ubuntu:~$
    ; e4 Z. E4 g5 E$ m- w
  11. //连接好stlinkv2-1到电脑和pcb板,输入以下命令。完成连接
    : q7 I$ W' M7 I4 S; W
  12. chen@ubuntu:/usr/share/openocd/scripts$ openocd -f interface/stlink-v2.cfg -f target/stm32f1x_stlink.cfg
复制代码

3 N5 T) f4 d( Z+ A2、在tasks.json中使用openocd进行编译后的下载
8 M3 Q* s. u& e1 S6 p8 B7 n* k0 j2 s( O! i& I/ x9 f( R
在原本的tasks.json中追加popenocd的相关内容8 w  o. u( o$ Z; g
  1. {4 ^; y3 S. H. O: P& Q: O! \7 ]+ j" e7 V
  2.     // See https://go.microsoft.com/fwlink/?LinkId=7335584 `  J, {9 _/ `2 G( b% i  Y  _
  3.     // for the documentation about the tasks.json format
    2 Q" h' A' w& Y: @
  4.     "version": "2.0.0",( \1 Y8 v0 l* J2 {5 Y" t* t9 A
  5.     "tasks": [
    ( C# h  d+ f+ |
  6.         {
    6 W0 ~5 K% ?) H" P. S* ~7 N
  7.           "label": "Build",      // 标签名1 Z0 B1 g) y& R7 _1 v2 Z
  8.           "type": "shell",      // 终端(shell)型的任务8 A2 b  U& L) W6 v
  9.           "command": "make",    // *执行的命令. j1 T9 a/ c. W( ^( a; v9 c
  10.           "args": [          // 命令的参数* p4 D- F6 r. ?3 t6 T. ~
  11.                           // "-j6"表示六线程同时编译% @6 s8 ?! d9 y5 ?& ~$ {9 [
  12.           ],
    ) v; S; U+ }. N* D  F
  13.           //"problemMatcher": ["$gcc"],  // 表示如果出现了编译报错,问题面板会显示 gcc类型的错误, a7 p4 S' _6 }  z. Q2 _
  14.           "group": {: s/ n( Q; t  @# |6 T
  15.             "kind": "build",% X8 L6 m6 a' v* e( P
  16.             "isDefault": true4 M+ f$ M: t# v9 {9 Q, Y& W- o! b
  17.           }5 y2 r7 j) y" ]5 m
  18.         },
    # i; n8 e1 n& M6 A
  19.         {
    ! y" d5 @" Y5 T4 p! Y+ t
  20.             "dependsOn":"Build",          // 任务依赖: v, {! T' A" s. V8 q
  21.             "label": "Build and Download",
    3 Q# K, c  a$ z
  22.             "type": "shell",; L$ O$ }' o! T5 l
  23.             "command": "openocd",5 n; b! h# \* }9 H3 L! P& X
  24.             "args": [
    ( ~8 P7 P9 {" n7 C
  25.             "-f",( G2 I0 N" h: Q; D  Z
  26.             "/usr/share/openocd/scripts/interface/stlink-v2.cfg",% _  o4 p, q; q/ C) {* d. B3 X
  27.             "-f",
    " l. b% i0 e; |+ }
  28.             "/usr/share/openocd/scripts/target/stm32f1x_stlink.cfg",
    / Y, X- c- N6 V0 |$ j
  29.             "-c",  N# G' ^5 {* [3 S: L
  30.             "program ./build/${workspaceFolderBasename}.elf verify reset exit"
    & g( R0 r0 J- V. D1 m' s& ]5 T
  31.             ],
    ' t2 W% u5 T0 k
  32.             "problemMatcher": []9 h& P! X( I+ r3 _
  33.         }
    . Q6 N( k+ _4 e+ L6 k
  34.         0 p3 l7 I5 L/ H& C
  35. ( c) x' I/ o' Z6 ?1 e/ e
  36. }
复制代码

" s& N1 d- x# j这个任务要求先进行 Build任务,然后通过命令打开 openocd,加载两个配置文件(-f interface/stlink.cfg 和 -f target/stm32l1.cfg),同时,这个 -c 的选项后面跟着的就是openocd成功加载后执行的内置命令,这里我们让它下载、验证、reset,然后直接退出,这样就实现了一条龙式的下载。
" P8 }2 ~) ^; J0 V1 ]- f  U
7 k1 ~4 L8 ?4 Q. c/ ?  o& `" B1 _- Q
微信图片_20240615144333.png
8 N  A- d& }2 M$ e. Q; B% y* F" x/ D( ~- N1 c
微信图片_20240615144330.png - M: N# {8 t. l: k* {# o
" Y# H% I* |+ G" _- u; ]( Z
3、使用openocd进行调试: e6 }- x6 x, c2 V
3.1 安装Cortex Debug和Venus's Cortex-Debug插件(第二个插件一定要安装,否则会报错Failed to launch OpenOCD GDB Server: Timeout.)
/ E' |( B/ {9 l8 }8 l
* j4 \; i; M" h* j$ G+ g+ i& c4 Z8 s* a) `
微信图片_20240615144327.png
+ f5 j( b4 h* N
: o+ T+ Y7 Z% Y$ @8 R, h( w 微信图片_20240615144324.png - ?- m! q4 M) i1 n0 m  d6 `
5 b+ z, m8 P+ N0 d1 I
3.2、 添加调试配置
3 L! J- h7 M! d: E- W1 U6 N! r2 o
微信图片_20240615144321.png
+ S9 W6 d8 t4 ^5 `

2 x# m# }/ }0 S5 A6 R) t8 S2 W3.3 选择cortex debug,生成launch.json,并修改内容如下# E8 T& U' I; `

7 r4 q% c% h& S0 n+ v0 d: Z: L1 X( k/ }8 r
微信图片_20240615144318.png $ [6 v  H! ~; ?8 |. \+ F

9 V, Z6 N. N& G0 O3 m5 a* b+ `
  1. {
    - \0 }; ?7 n$ H. f& n
  2.     // 使用 IntelliSense 了解相关属性。# ~5 d6 r( [4 z% S% I
  3.     // 悬停以查看现有属性的描述。
      Y7 d6 D2 S& z2 A
  4.     // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    7 B5 Q' n' I) T9 }( M
  5.     "version": "0.2.0",
      t2 ^, U' E2 [
  6.     "configurations": [6 ?0 X- h7 H0 a' H
  7.         {
    9 r$ R5 S  i4 w! o
  8.             "name": "Cortex Debug",
    7 [1 b. L/ }: J, O
  9.             "cwd": "${workspaceRoot}",  // 输出路径
    9 }- |0 l& R. H4 u, s
  10.             "executable": "./build/${workspaceRootFolderName}.elf",    // 要调试的程序9 W& I, t/ `" C8 g( @: a- L
  11.             "request": "launch",, D, e( c! T% K4 y, O9 C( m
  12.             "type": "cortex-debug",
    / H$ ~! J: D: H9 ~
  13.             "servertype": "openocd",    // 调试器选择
    7 k% ?. U% Y9 M& u7 D2 [
  14.             "device": "STM32F103C8",    // 使用J-link GDB Server时必须;其他GBD Server时可选(有可能帮助自动选择 SVD文件)。支持的设备见 https://www.segger.com/downloads/supported-devices.php
    3 w. O: J6 g7 C" }' `  Z3 P6 X
  15.             //"svdFile": "./STM32L15xC.svd",  // svd文件,有这个文件才能查看寄存器的值,每个单片机都不同。可以在以下地址找到 https://github.com/posborne/cmsis-svd下载5 W8 J, h, l  l# i
  16.             "interface": "swd",
    / T  {2 p* b+ a1 W: e
  17.             "configFiles": [3 n% [) S9 [" K! p6 q4 `
  18.                 //"${workspaceRoot}/openocd.cfg",  // *可以通过加载该文件来配置2 h2 A3 r* g  @( `: d% H
  19.                 "/usr/share/openocd/scripts/interface/stlink-v2.cfg",
    : j9 h; a& p; }# g+ O" |' j$ S
  20.                 "/usr/share/openocd/scripts/target/stm32f1x.cfg"
    # E6 p) V# p* ~, ]& k5 C# @
  21.             ],
    1 Y* t- q* |6 A8 h' d
  22.             "runToEntryPoint": "false",& l* |4 U$ d1 ?  r
  23.             //"runToMain": false,
    ! S  _) ~! A8 h  l8 Y: K8 y
  24.             "preLaunchTask": "Build",   // 在调试前预先执行的任务,此处是tasks.json中的8 D& }; e; m. A: m- A$ z
  25.             //"armToolchainPath": "C:\\Program Files (x86)\\GNU Tools ARM Embedded\\5.4 2016q3\\bin"    // 如果没有把 arm工具链路径添加到系统环境变量,则需要这条指令添加你的路径. }2 c/ S$ a& Q! u3 m  W
  26.         }
    0 w& W4 ~- I/ q1 l) {  |6 D5 n. S7 |
  27. ( Z3 o6 e! f) Z* V8 o& L! ?
  28. }
复制代码

' t4 [* `  A3 H3.4 调试- t  M! L0 n: v4 p% }+ W

. l: R% K. D3 b$ E5 G, i" f
微信图片_20240615144314.png , n0 D! ~) l) {& k& f/ j' o
. Y8 p3 z3 a' \3 Z* q, {
11使用VSCode串口工具

, F! O; m& I& S/ G1、安装serial monitor% h  t4 O6 r; Y9 V
% P, y# M) \1 t/ C0 C- s% T9 g; h
微信图片_20240615144304.png # q. k  i6 }% t0 m, n3 E

4 I9 y; n1 y: S8 A; ^2、查看连接的usb
! a. z3 y4 F7 {# Z$ T. T
  1. chen@ubuntu:~$ lsusb
    ; ?3 Q' O4 \" N7 t2 b. S/ L* f
  2. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    0 {4 N' U: D2 @( Y2 o, A6 K
  3. Bus 002 Device 007: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
    0 M- b0 y1 @: P6 t2 F3 S
  4. Bus 002 Device 006: ID 0483:3748 STMicroelectronics ST-LINK/V2
    . T0 Z7 Z; M; t' v4 A. D" M
  5. Bus 002 Device 004: ID 0e0f:0008 VMware, Inc. VMware Virtual USB Mouse5 i8 @7 A; C2 r+ o8 h$ z$ _: K
  6. Bus 002 Device 003: ID 0e0f:0002 VMware, Inc. Virtual USB Hub5 c( O. i; s  A; G# i
  7. Bus 002 Device 002: ID 0e0f:0003 VMware, Inc. Virtual Mouse1 c- f; E2 @$ `4 {( S4 R
  8. Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    , {+ B* S- m7 f9 f/ @! q, q
  9. chen@ubuntu:~$
复制代码

( l! j8 M& k0 Q: ~2 u( H  ^( U2、加权限
  1. chen@ubuntu:~$ sudo chmod 777 /dev/ttyUSB0
复制代码

6 U) u3 N8 K2 ~0 F9 n8 q12使用git* L6 Z( h. E. O8 I* x& M
1、检查状态9 R- A6 g+ w  M1 y2 M8 v
  1. //在执行 git add 和 git commit 之前,先执行以下命令查看当前仓库的状态:8 u, j1 z: b( M9 _5 A! f  K1 c
  2. git status
    3 x* V& C1 ^7 M4 B
  3. //这将显示工作目录和暂存区的状态。确保没有未追踪的文件,也确保已将需要提交的文件添加到暂存区。
复制代码

2 t( q& |) e7 {0 m; }# y9 ^8 s2、确认 Git 配置:% t5 n6 W) X1 Y! A4 ]
  1. git config user.name1 ^3 X8 Q( ~+ V
  2. git config user.email! u, S! @" w, y  I: e
  3.   ?2 V* _. b& w( Q1 x+ E+ y
  4. * F3 u) X7 i  P3 P* Y) @
  5. 如果没有设置,请使用以下命令设置:( R% H% a& W, S$ c' Q; u3 K8 s5 _* _
  6. git config --global user.name "Your Name"
    3 h; e8 M4 I' O3 W
  7. git config --global user.email "your.email@example.com"
复制代码

& ?) N9 Y" D) @% b! Q3、检查文件权限1 l' ?4 i+ F- R% [
确保你有权限在新文件夹中进行写操作。使用 ls -l 命令检查文件和目录的权限:$ ^9 O3 m, o1 K/ X, m
ls -l
. F: G1 G& C; x5 n* t5 J$ [( h/ B# k7 e2 t+ h+ m* \0 L. W5 z* ~$ g$ P  V0 A
4、查看错误信息:2 {; P! o) G5 H) Z1 ]: T7 k
如果 git commit 操作没有反应,尝试使用 -v 参数查看详细的错误信息:& X) a0 \1 a- w! [/ ]
git commit -v
: o6 L4 s; |) z" Z* K. z7 q
% P- A' h4 Y% F  x, i
5、清理缓存:" U. V3 B  p& e/ z; X" H
有时 Git 的内部缓存可能导致问题。尝试清理 Git 缓存并重新尝试:2 r7 X  q# f" f4 X1 N/ E
git rm -r --cached .
. m0 j) i1 J: D, jgit add .7 a4 {9 _+ ~% ~9 N& r' C! |
git commit -m "Your commit message"
5 T  P. I8 w8 O这将清理缓存并重新添加和提交所有更改。' ?# K4 _+ ?$ f! R

( Q: e. Q( n! s; `4 m" b
) s- e8 h; y" x' Q8 P3 W! ?9 D
转载自:kevin3 `6 U* N. N9 o+ |/ o
! f+ ]  k/ M, U9 W; D
如有侵权请联系删除
$ L) o1 _) y, C& z0 g6 c( L7 u- p$ \& i% `) e8 B- o
0 b4 e; ^& r/ \  A% ~

& K6 O) f. g. z, Y
1.png
收藏 评论0 发布时间:2024-6-15 14:45

举报

0个回答

所属标签

相似分享

官网相关资源

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