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

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

[复制链接]
攻城狮Melo 发布时间:2024-6-15 14:45
“ 从ubuntu20.04入门到完成stm32程序的调试。”
, z) {+ I& o- w, L) _
  w, e7 d) E! [; G) N4 r
微信图片_20240615144535.png
7 D( {% h' i& i, C- i( }( a

! @0 A6 q) p9 U# b5 u* q" j1、更换软件源$ {1 h: P. v7 p  o, ^
源文件(sources.list)* m: u- ~- U( u
更改源(Ubuntu20.04对应的源)4 t8 _* Z( ?8 k
更新源. ]1 t7 |9 x2 q) ~! p3 j
6 D. T# j# c% K0 o0 {
2、安装拼音输入法
9 t* V. Z5 t- X- G4 z3 t, oRegion&Language->Language Support->Installed Languages7 V5 s, v" s% s# }
Input Sources->"+"->chinese->Hanyu Pinyin4 H% E1 f0 J8 v5 x  \! I2 s. J

; {% k% e: s, @5 f3、安装VSCode4 O0 _3 Y" L$ k7 B2 b
下载.deb程序包1 g: b& z4 Y. c3 R5 E
使用dpkg安装9 B8 ^+ O) [; U/ N
调整设置# s2 Z4 y+ C7 f2 _  J% @
启用Title Bar( m/ s9 F4 U; R3 V
安装插件
, |4 T7 s& c. a( M
2 d' r' w: q/ U$ P6 w
4、安装Build Essential及gdb
* _4 X1 j1 B7 ~3 |8 u: kbuild essential介绍2 _- r: Y, M6 s7 n/ o
命令行安装build essential和gdb1 {; P3 R5 S  R5 V* n1 u0 h
Linux C程序开发简介
! O, C0 @; ^5 F" S- d无makefile; I2 {1 b  [4 b! G2 B
有makefile1 n' h" X6 m/ N' h% m
生成.vscode文件下的debug配置文件2 Q. @+ L( C4 z: c
生成task.json文件. A( \: o; I' |
生成launch.json文件
/ z# f7 y+ I: V5 W( x9 y4 W生成c_cpp_properties.json文件
; f& N5 K* J8 Z% M% p

# K+ E0 Z+ m0 d% E3 F  f2 _6 J5、安装Vim
2 Y: u& f7 ~) K: F7 f: [

0 A" j" O: Z1 B; j" H) b" V0 o6、安装gcc-arm-none-eabi

2 g* R7 L7 D) y* `下载嵌入式gcc编译器/ I# r9 b- h# A
解压: l3 b, K. j0 B
添加gcc-arm-none-eabi下的可执行文件命令到Terminal4 `* f4 u4 H- U  X" i

1 A; w: F/ e6 ^* e
7、安装stm32cubemx
0 d* \5 y+ \& W& q4 K! C+ z% s* w下载stm32cubemx
! @; {& ^, l: a( @安装
1 {- m) [% U0 v8 F& T" x9 G: y增加图标到菜单# u9 O& F. V$ c: l) ^

  c. }' @0 X# q" [2 m3 k
8、使用stm32cubemx生成初始代码* |2 N0 I% L4 n/ Y& l. ]3 n7 a
选择芯片->配置->Generate
2 f7 C' |# e6 Q! S/ O  [勾选PinOut-Debug-SerialWire
* q& j& G5 K8 B% ]  Q$ ^6 v# G. ~& s( p- V% }! w
9、配置VSCode
/ [7 C/ v0 q+ F8 _' r/ n9 u使用vscode打开上述生成的初始代码6 R% I' V+ Q! t9 ?2 r
生成VSCode配置文件
" C+ n$ s) R+ |& Ic_cpp_properties.json& k; O6 x2 V/ o  ~1 {# w1 e* }
task.json
9 |3 f0 f4 x8 v( Wsettings.json$ o3 v3 g: F- n* o5 g3 b& _
宏定义包含" J  [7 N/ T5 l. ~
make编译/使用ctrl+shift+B
1 A/ P" q* [% H3 I9 G" b- T5 \+ S
10、调试2 v4 ^* k& R/ I9 w/ e* r. p
安装openocd
& [$ x2 A' ^2 \: K! S在tasks.json中使用openocd进行编译后的下载
+ e, i" w" v6 e使用openocd进行调试
+ l& `. a- ]& q安装Cortex Debug和Venus's Cortex-Debug插件  y. R; c$ Z( t& F
添加调试配置
, R0 _" a4 G* [. ^8 F" ?7 x选择cortex debug,生成launch.json
5 ~' J6 X: d0 R2 @* [调试
4 G. ?% r7 E6 `( z! g
( L2 ]$ W- K9 y( i1 o' P
11、使用VSCode串口工具
6 r: w, D8 V; q2 u3 o, i1 c安装serial monitor' B2 E1 L6 s5 G9 z# z
查看连接的usb% l0 K. w/ h1 D5 v
加权限
7 W2 p! l) L  }; V; A$ \: h0 L; |
; f# n  m/ G/ l' w$ ?" a* u: `
12、使用git
  p1 s4 h) V. [01更换源8 R) N* d9 ^2 t2 g5 L
1、源文件(sources.list)
4 A' U9 N  `3 I( {chen@ubuntu:/etc/apt$ ls -a
2 A, K' I  N: V.   apt.conf.d   preferences.d  sources.list    trusted.gpg.d
) K6 C3 k& y* `% X' M) x..  auth.conf.d  sources.bak    sources.list.d
' y" Z' W: ^: Y2 H: M4 M8 y
& \+ c" |- D( e& N  W1 ^- H2、更改源(Ubuntu20.04对应的源)$ W0 I' _9 Z0 k3 i$ d7 y! b
  1. chen@ubuntu:/etc/apt$ sudo gedit sources.list
复制代码
  1. #阿里源(Ubuntu20.04)) O- _; }& _" }1 \1 H# I- i
  2. deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
    & B! `! H/ B1 Y) k- H
  3. deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse; J9 N; g: z& j
  4. deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
    ! g1 T$ {; y: f
  5. deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse9 g5 W3 A! g: C# u! C) w2 \
  6. deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse0 x" b. H7 e; M8 D, G
  7. deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
    + g# F6 `. m/ g8 u- t
  8. # deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
    - m! }7 u6 u4 L8 h. |" z% r; e$ ]
  9. # deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse/ f: e; Y) {& G2 m  c; `
  10. deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
    * X; m1 F3 K. Y, G1 J
  11. deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
复制代码
  1. #清华源(Ubuntu20.04)- C5 f6 T( r0 F
  2. # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
    , `+ O7 R' L# q/ A. o$ A+ b+ F" v
  3. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
    ! u' ]' G$ B; f6 ~9 c
  4. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
    & q5 D$ j3 A6 ?! J7 P
  5. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
    8 K3 l5 i6 V7 X
  6. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse% h: X( H8 {" T/ L; q2 R. |
  7. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse) [5 t0 `  J; G/ b$ b1 O
  8. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse% y3 m" t) \! Z0 z0 h
  9. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
    * X0 k9 T5 O. t
  10. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
    ! j; D5 S4 @9 C5 i' X- t# [
  11. 7 }: d4 c2 C9 C: x9 D
  12. ! a8 G5 K, S4 d( M1 V" W
  13. # 预发布软件源,不建议启用
    8 v+ B% j! [$ }, l* A/ V
  14. # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
    ( i9 l! }; P  q0 ?+ D
  15. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
复制代码
  1. #中科大源(Ubuntu20.04)
    & |) @9 P% i% n, k' S
  2. deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
    0 y+ {$ h9 H% i5 m1 o
  3. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
    $ V- v! r) x+ G5 L3 U; f; L3 j' o
  4. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse. @: {: S7 D; W7 M. c
  5. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse! B' q& }$ V( }) x# |5 J
  6. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
    % M0 Q7 d9 z5 H" i. C
  7. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
    : a$ F$ z& u6 l1 y1 V5 Z9 Q
  8. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse& Y2 U, e+ @8 B& v1 q6 z
  9. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
    1 u0 k- O+ ^& ]1 n/ u' U
  10. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
    & U  C. }0 P& L5 _# n6 K6 d5 F
  11. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
复制代码
) O# x: t/ w' [
3、更新源
6 A' t( j) L6 D! i; O
  1. sudo apt-get update
复制代码
; w, B/ i. ]3 V3 n! Z1 W5 {  B
02安装拼音输入法& U+ K; [/ l9 @6 @4 Q7 v, H4 E
1、Region&Language->Language Support->Installed Languages" F2 s- B- l" K* u* ]- E7 a  `

  E; j) n+ h, E2 _" g8 M! T6 C0 O
微信图片_20240615144532.png   h1 \; o! V: R
/ \* R4 G. ]) ?5 ^  _7 E
2、Input Sources->"+"->chinese->Hanyu Pinyin
. s2 m" W- h4 j0 z) O7 P% h9 ~. V( `
  1. chen@ubuntu:~$ sudo apt-get install ibus-pinyin- J2 E& |% M- E2 ]5 Y$ I
  2. chen@ubuntu:~$ ibus restart
    ) q7 V! v/ {) e
  3. chen@ubuntu:~$ ibus-setup
复制代码
6 N6 D" C* T' m6 |( V; L
微信图片_20240615144527.png
  \* k, O+ p( |& p" b
( {5 ?, o; \1 ~ 微信图片_20240615144524.png # f6 a/ J& u0 P& V; {

5 t$ n* W& r& X, a$ Q 微信图片_20240615144520.png 8 r* ^; U% s% U8 V7 R
0 A3 F3 L" D# U3 E5 W
03安装VSCODE4 k0 W7 D! Y" S6 t# i# u4 H
1、下载.deb程序包https://code.visualstudio.com/( V% R: h' m: i) m9 _

3 ^4 `/ g6 ^: b6 j0 I2、使用dpkg安装(在Terminal中Ctrl+Shift+C为复制,Ctrl+Shift+V为粘贴)" m7 r7 ?( ]8 h/ H) j
  1. chen@ubuntu:~/Downloads$ sudo dpkg -i code_1.89.1-1715060508_amd64.deb, p) N1 M, J5 C2 K# c- X. x9 d
  2. chen@ubuntu:~/Downloads$ code .
复制代码

. y! I0 }! G! K9 g3 C# X9 N3、调整设置) X9 A! V$ |7 `
启用Title Bar:View->Appearance->Editor Actions Position->Tab Bar3 B/ U% Y$ A3 N4 S* N. g
) ]: |. w8 f9 L* B: P! a  C
微信图片_20240615144516.png 9 [8 r2 K5 P: b* u9 i

+ N2 u% {/ j: K) O4 l2 V4、安装插件
* @, O3 G- f, n) ogit安装- e- c/ v! Y3 V  @$ b' Y
Download Git for Linux(https://git-scm.com/download/linux)
7 H2 n2 o% o/ J" U
0 A+ D/ ]/ r! d6 i8 H6 ?
微信图片_20240615144512.png
* S( x$ x9 B  F  O1 o( f9 G0 X4 o$ m* j! |0 ?  P
微信图片_20240615144509.png
6 s' u/ _2 S  i* {% X" Z0 C7 [

! z5 ]0 I3 T' x. @% g' b命令行直接安装(Ctrl+Alt+T呼出Terminal)
1 N! V) q+ g8 n4 O# o- P
  1. chen@ubuntu:~$ sudo apt-get install git
    ; N" @$ w! ?3 B
  2. chen@ubuntu:~$ git --version
复制代码

6 X4 q) p6 j# egit设置  L- f+ |, Y- C& ~4 S( c4 n& D

  N/ B* t" [6 Y9 M: M
微信图片_20240615144504.png # p2 x4 i3 c5 ^, L7 i
0 Y" O9 U+ m# o0 o: P+ E1 z
  1. git clone https://gitee.com/kevinkerry/linux-stm32.git
    " I. J+ c7 L" W5 H3 ^3 u
  2. git config --global user.name 'kevin.kerry'9 g! z8 i1 K7 m# j% P' i8 e" o( d( g
  3. git config --global user.email 'ct15762287606@outlook.com'
    % M/ F, ^- b( t. F

  4. 9 Z2 ]% z! Q. J

  5. 1 y  `% S* M9 l7 E% @1 W
  6. #示例
    ( c- |5 M5 ?0 [9 d
  7. chen@ubuntu:~/prj_vscode$ git clone https://gitee.com/kevinkerry/linux-stm32.git( X) i, ]1 E9 p$ t- d
  8. Cloning into 'linux-stm32'...
    1 V5 L. m4 e4 @; t1 K
  9. Username for 'https://gitee.com': ct15762287606@outlook.com# [2 h8 M# x7 u5 M
  10. Password for 'https://ct15762287606@outlook.com@gitee.com':" h/ e! z- Z" L: M
  11. remote: Enumerating objects: 4, done.
    ) G0 H* j/ d; U$ r) P
  12. remote: Counting objects: 100% (4/4), done.
    ( {9 b0 S, b- d4 w5 }
  13. remote: Compressing objects: 100% (4/4), done.' g: t* D0 L  E, a
  14. remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0* S$ `) s: w, }8 z; L
  15. Unpacking objects: 100% (4/4), 1.79 KiB | 1.79 MiB/s, done.7 r& u# S! g$ Y
  16. chen@ubuntu:~/prj_vscode$ ls1 X5 N7 G7 D# ^* R% o
  17. linux-stm32
复制代码
0 E8 `: u' n- S
GitLens — Git supercharged插件安装  }1 w. B; u- N4 c5 X. B3 X

) E* B, E  x+ p8 j8 _" u: X- f
微信图片_20240615144459.png 6 t% ?+ Z+ l1 _9 s. E
1 w9 Y" o! v; w$ a6 A0 M
Git History6 y# F" g3 S! v8 ?$ C
* K6 Z: o4 m$ S  n, G
微信图片_20240615144456.png * ^, Y5 _/ G- y
: n7 ^0 ]! l% t5 b1 K9 B" q
Bracket Pair Color DLW
& L- m1 e* P/ P6 I8 Z$ f3 @2 F
4 o4 d* _: W% M' d  h" m
微信图片_20240615144454.png
/ }) F# }) I' M* r( |
+ D& i: s5 B# G+ M9 t* mArm Assembly6 `5 U) _( x9 `. ^7 T. A
( U$ x9 e4 ~$ [& M" H/ F) M. b
微信图片_20240615144451.png
9 `0 @7 K; j! a; ?- J; K) \! W

! s. G1 ?* A* P2 N! V" qVenu's Cortex-Debug
, a+ u3 q; k$ Z" h
( K% J. ]$ P2 m
微信图片_20240615144447.png " `# L2 T' E) C/ L9 f
4 ^* t) W6 t- {) r9 J
C/C++
1 L) M0 }7 z, P( v6 E6 {+ q/ W( L, x2 l- w
微信图片_20240615144437.png 3 y/ q, f9 U8 C4 j& w

7 I' ^, C0 e( t( `6 \  x04安装Build Essential及gdb
7 O9 i- R0 ~4 Y4 q. |. I1、build essential介绍2 ?2 A3 c7 \  L& ?/ j* C$ Z
build-essential 指的是编译程序必须的软件包。它本身不是一个软件,而是一个工具集,包含了在Linux开发程序的一些必要的软件包,包括但不限于gcc、g++、make等。
* M9 U+ ?; h% N% s6 q1 v6 o& R+ k. O4 m8 H7 D: K
查看该软件包的依赖关系,可以看到以下内容:  g. W# D! ]( O$ r; ]/ s3 F# ?: ?
  1. chen@ubuntu:~$ apt-cache depends build-essential: N! d8 r7 q8 M3 `, E( K$ o; j3 [# L
  2. build-essential/ _: R5 P  V4 ?6 F- s
  3. |Depends: libc6-dev
    . y3 K& @6 p$ X- n! P
  4.   Depends: <libc-dev>
    , P7 f: L% ~# Z; K
  5.     libc6-dev# Z: b5 R! B. a8 e/ X0 o
  6.   Depends: gcc
    + }3 k  Q# ?4 r6 X% K0 J
  7.   Depends: g++
    7 t9 ?- n* L7 X( ]
  8.   Depends: make) r' @( a) ?" ?
  9.     make-guile5 x9 q2 `, S8 p7 L3 h8 h( C* }; |
  10.   Depends: dpkg-dev
复制代码
1 x- t- e" r$ j' z
也就是说,安装了该软件包,编译c/c++所需要的软件包也都会被安装。因此如果想在Ubuntu中编译c/c++程序,只需要安装该软件包就可以了。/ @8 ^+ h" l! O+ T
: x$ d. U7 H% z9 v( t8 r
2、命令行安装build essential和gdb- M6 a4 T( M3 u( S( `9 t
有时候可能会需要安装gdbserver
& m+ i# r' A" ?
  1. chen@ubuntu:~$ sudo apt-get install build-essential gdb
复制代码
& G. H3 H2 O# {# ?
3、Linux C程序开发简介
6 d" l+ N$ ?) d- `. Q3.1 无makefile
+ H9 M4 k5 t) y' g
  1. chen@ubuntu:~$ mkdir linux_c& H$ m* p5 O  @- B0 w
  2. chen@ubuntu:~$ cd linux_c
    2 C; F2 x! Y; l0 y, H5 t
  3. chen@ubuntu:~$ touch hello.c3 }6 Q8 B: X; U9 i6 S& u3 t: Q
  4. chen@ubuntu:~$ sudo vi hello.c
    * J$ {. ^2 [* C5 J3 ]
  5. chen@ubuntu:~$ cat hello.c
    4 J+ H9 ]9 v1 K0 V+ J
  6. #include <stdio.h>
    % O  u9 A9 d) P) G, T4 Z
  7. int main(int argc,char *argv[])8 S" w% I0 Q$ `% ?
  8. {  w8 U9 k* r1 U8 i5 z
  9.   printf("hello world\n");
    , y: l1 P3 C! D9 K: M9 c
  10. }# f, X# i) J6 v  H) y! j: l
  11. chen@ubuntu:~$ gcc hello.c0 @# W, U3 s1 q; \8 `: [  N
  12. chen@ubuntu:~$ gcc hello.c -o hello
    , U+ w* Y( x) l8 _+ M
  13. chen@ubuntu:~$ ls -al& O6 |8 ?( e+ ]) A. {, Y0 A& D+ C
  14. total 523 A) Q( ^$ d* H/ G6 n2 i
  15. drwxrwxr-x 2 chen chen  4096 May 16 18:32 .* J" ]( s& A* v( l! J7 {
  16. drwxrwxr-x 4 chen chen  4096 May 16 17:37 ..
    / j4 Z. ^3 I) @3 a3 |
  17. -rwxrwxr-x 1 chen chen 16696 May 16 18:29 a.out5 G" f3 W/ _! d0 v# |- R
  18. -rwxrwxr-x 1 chen chen 16696 May 16 18:32 hello
    8 C; K$ k* W3 J" {
  19. -rw-rw-r-- 1 chen chen    82 May 16 18:29 hello.c
    8 N' t3 [, p6 d. ^& b5 C% F
  20. chen@ubuntu:~$ ./a.out- _; X" o, b" X" B5 D
  21. chen@ubuntu:~$ ./hello
复制代码

) B7 n2 u( U; O% [# a8 C3.2 有makefile
8 k9 p) }4 a" L- y! x$ n# i3.2.1 创建程序文件
  1. chen@ubuntu:~/prj_vscode/linux-c$ touch main.c input.c calcu.c input.h calcu.h
复制代码
. E; S9 c" B% m1 c  c8 W: J
main.c# p9 t$ f! \9 }: w6 e6 i
  1. #include <stdio.h>) G8 B% A$ \' ?2 h( Q7 o
  2. #include "input.h"# X9 g2 A4 \: }
  3. #include "calcu.h"
    ! D& L% i3 v  t, @/ n  C' v/ e0 i

  4. # \4 S& `0 ^" M0 P' X) W. {

  5. + |+ r, H6 e  E' E
  6. int main(int argc, char *argv[])
    * i1 N  s# z. F# b9 G! x! m/ j6 _
  7. {
    7 h0 {) J0 r/ ?  x$ K
  8. ! @) C: ?% U: u; N
  9. 8 D$ L: s* S3 I( A
  10.   int a, b, num;
    6 N9 P7 e5 Q% T! Q: _9 I

  11. * G$ n. A2 F6 @1 d: h  @, O4 f5 X

  12. 6 [8 K' Q/ r5 V, K6 R
  13.    input_int(&a, &b);
    ! A* {% |2 m0 c7 ^: E5 Q% b
  14.    num = calcu(a, b);& X* {) \3 x8 e
  15.    printf("%d + %d = %d\r\n", a, b, num);5 u8 U2 {5 `  G( D/ I
  16.    
    " o% S/ u" L* h* R- l; ]
  17. }
复制代码
& r+ s1 y8 [8 h( \
input.h
  1. #ifndef _INPUT_H
    0 s2 |7 G7 V' E9 ?
  2. #define _INPUT_H/ }! {: E3 Y' f: j6 Y) X4 }

  3. ) h6 v  G* }, V( r: |
  4. void input_int(int *a, int *b);% q9 e, a; I4 ]

  5. 3 ~( E8 ]% o* i# W, D! G
  6. #endif
复制代码

. i% _& O# ?5 l# Y4 Xinput.c3 f! S+ ]' n- Y
  1. #include <stdio.h>
    4 T5 y. |2 o. i" Q4 D& J
  2. #include "input.h"
    % x4 H: c3 W: Z2 Q3 T& A

  3. : U# ~( S% g) o  N5 }2 u

  4. 6 N# s5 D- e5 E) i, q: f7 O/ g
  5. void input_int(int *a, int *b)
    3 q7 u* e0 U! z; A% s
  6. {- j+ [8 V2 i, l- ~% n0 k. n
  7. printf("input two num:");3 @" ?  q* D* I* a. U0 y$ s* Q: g
  8. scanf("%d %d", a, b);
    2 h) I( |0 P( G: l
  9. printf("\r\n");! t$ x9 x' `& M* G# I. X. n2 }+ {
  10. }
复制代码
+ M: t  B) q& D% r' ?) q, j
calcu.h
  1. #ifndef _CALCU_H% Y) a2 R( \6 L4 q& W' l
  2. #define _CALCU_H1 [0 @9 X+ L8 }% c
  3. $ O0 p- D" F* L) t" F  D

  4. . k5 o7 A  q; c
  5. int calcu(int a, int b);
    2 H/ x5 a: i+ F/ r+ Z0 R
  6. #endif
复制代码
, x/ h1 E* {; c0 Y: u% a0 M
calcu.c6 u4 {' N$ `! T6 ]7 N
  1. #include "calcu.h"
    3 D, ?" V7 p) E7 J
  2. 9 ^* Y7 w5 n  X
  3. 8 Y1 I. C% b" L! k$ q" p
  4. int calcu(int a,int b)+ X- M  o7 \) l  x* a8 O3 Q! P
  5. {
    . [" S1 E& d' e" v
  6.   return (a+b);
    3 D' d3 B1 q/ Z: Y4 Y  v
  7. }
复制代码
% b, V2 W: l+ v' [& \$ Q' O
3.2.2 编译测试一下
+ ?, s4 T2 Z* T5 |$ W; }
  1. chen@ubuntu:~/prj_vscode/linux-c$ gcc main.c input.c calcu.c -o main$ ]+ A, h# y  G3 i* j
  2. chen@ubuntu:~/prj_vscode/linux-c$ ./main2 Y3 `* P& r6 E3 U
  3. input two num:23 56$ [5 x4 a/ n) P, \3 T) a
  4. ( j, u) |2 Y4 w$ ^* l
  5. ) z4 w* i- C4 E' y% Z- |
  6. 23 + 56 = 79% x/ J0 I2 {: H; O( O! V
  7. chen@ubuntu:~/prj_vscode/linux-c$
复制代码
  1. chen@ubuntu:~/prj_vscode/linux-c$ touch Makefile8 l+ M/ X5 w* R: e6 ^2 R
  2. chen@ubuntu:~/prj_vscode/linux-c$ cat Makefile# A4 y( D  p4 g
  3. main: main.o input.o calcu.o
    1 n9 e4 h+ l! {
  4.   gcc -o main main.o input.o calcu.o! u: c+ z. {- i2 ~! G7 u- g! v0 p% v, I
  5. main.o: main.c
    , D/ |  H" B. s) b
  6.   gcc -c main.c
    + J) T/ G6 L( h
  7. input.o: input.c
    3 a8 b, A% P) B! t# Y1 Q. P; ~
  8.   gcc -c input.c
    0 ^$ O  i7 t. W7 d
  9. calcu.o: calcu.c5 G  C- K6 A/ k+ h
  10.   gcc -c calcu.c! T! U/ T" W7 r3 v7 q
  11. . F1 g7 J+ p( s: o" `6 C

  12. + z& `& q& X: W6 s1 s" u; _
  13. clean:+ b5 V; z0 s8 ], _
  14.   rm *.o
    8 {! Z5 |2 L, O/ P7 c7 l2 y+ S$ w
  15.   rm main7 Q  u2 A& j0 `
  16. chen@ubuntu:~/prj_vscode/linux-c$ make clean  Z( y$ j. p: u* P7 x
  17. chen@ubuntu:~/prj_vscode/linux-c$ make
复制代码

$ c  j0 `- g& _9 `4 d3 n4、生成.vscode文件下的debug配置文件# s2 ?2 V$ t: j
tasks.json:该文件主要用于配置编译的选项,如果使用makefile或者cmake进行编译,则用该文件调研make或者cmake命令进行配置。
, u  f& l; A/ R0 `3 M4 u$ ?launch.json:该文件主要对debugger进行设置
: w/ k- w4 q! o8 C5 t$ n7 @2 Hc_cpp_properties.json:编译器路径和intellisense设置  D" u6 g1 L8 \* m2 |1 I4 V
) q' M$ _( L* Q1 m( R/ e& q8 }
4.1、生成task.json文件
" I9 _, G+ Y8 }$ H
0 ~1 ?$ h3 Q0 X) U  D- x2 H
微信图片_20240615144428.png * I7 d& k2 w1 S3 y( n6 i
图4.1.1 Run C/C++File/ L  B4 c0 ^9 P- R/ r7 Q- t
6 n1 E3 _. N# u) j. y! S4 k
微信图片_20240615144424.png % d, K7 g1 V  y( q4 R
# }- C8 X7 W8 n" M
修改生成的task.json文件如下3 o) I' A$ l! I/ D1 q0 f$ N
  1. {3 ?) i/ w* `2 P+ \+ c6 ]3 H
  2.     "tasks": [
    3 U, }3 [  V6 _* Y
  3.         {
    . Z2 m8 O) t4 g* H* x
  4.             "type": "cppbuild",
    # f2 Z8 j7 n: p) m/ k. r/ p  O
  5.             "label": "C/C++: gcc build active file",
    ) ]8 i. O4 X8 u3 [- R. j
  6.             "command": "/usr/bin/gcc",
    3 R+ ^% L. z' u$ A5 p
  7.             "args": [
    7 w4 _8 ?# H6 K; k# g" {$ r
  8.                 "-fdiagnostics-color=always",
    - O6 @, o+ V% |! C
  9.                 "-g",# b, s; H) M9 P, ]
  10.                 "${file}"," ?2 l, }& x' g# p
  11.                 "-o",! Q9 ]9 b; L/ r  a0 h+ G
  12.                 "${fileDirname}/${fileBasenameNoExtension}"
    + `! }, c- A% A% C6 J
  13.             ],
    9 B* A% S# Z/ h6 u
  14.             "options": {7 L! m1 I' ^$ k# r7 J4 s
  15.                 "cwd": "${fileDirname}"
    8 I0 t4 ^  `8 Q
  16.             },
    7 N& L+ e2 _1 Z) i. O- s
  17.             "problemMatcher": [
    9 B1 u+ P! U" }# |. a7 r* p( m" X" `
  18.                 "$gcc"* p8 Y/ g- v/ X) p9 j1 x  J
  19.             ],! d# X3 v; O8 C. L
  20.             "group": {
    ! c" ]6 L9 v  O* z1 C- X
  21.                 "kind": "build",
    1 m* F3 e3 U: R( |& F
  22.                 "isDefault": true$ R3 \  `0 F' y- W4 r: r, Q
  23.             },
    6 P$ U0 N3 s) G. X: U! `7 i! ^' q
  24.             "detail": "Task generated by Debugger."
    * x9 g, s4 R2 }, T9 ]' y
  25.         }- i4 E/ p  p9 ]
  26.     ],! G8 g  L! H) U& u9 [  k! _6 k7 Y
  27.     "version": "2.0.0"
    / @. ~. V5 J6 O
  28. }
复制代码

. v& q: q* U7 o% A* C修改如下
  1. {
      R' n+ @7 P; T3 ^( B
  2.   "version": "2.0.0",, j+ M; r! f- _0 _" o7 h; Q
  3.   "tasks": [. \! s+ z; T# k# M( _( \
  4.     {9 U/ U/ K! _# E* z
  5.       "type": "shell",
    # K/ G0 [6 k' u* k! M" R+ l
  6.       "label": "C/C++: g++ build active file",
    0 X1 _3 i' ^; k! J% u
  7.       "command": "/usr/bin/g++",$ w( z. o7 O  U, g8 y, Y6 i0 O
  8.       "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
    & l+ k: s/ O/ K3 \; i: t
  9.       "options": {
    3 E9 ~, ~+ I& ?- F+ F- }7 F) B
  10.         "cwd": "/usr/bin"+ N$ Y) r8 x; x, m, c  O3 w
  11.       },
    5 p/ Z7 D) a4 g! r) U  [( ]
  12.       "problemMatcher": ["$gcc"],
    : {+ F8 t9 a2 j& Y
  13.       "group": {
    3 Z/ r( V. M# X9 o
  14.         "kind": "build",
    # t1 k% f$ p( o( U$ d
  15.         "isDefault": true
    2 z6 Z, t- c3 ~" P5 s3 b
  16.       },
    2 Z) A) N- |- U' G! G
  17.       "detail": "Task generated by Debugger."  Z: I$ X2 v% q( n) M! D0 T' Z
  18.     }
    , s, N9 ?/ m: t
  19.   ]: T9 j5 j- s' n  O9 l
  20. }
复制代码

5 \0 \  ~; ?4 c- J* T7 S- wcommand 代表使用什么程序去执行文件,这里使用的是g++. args表示的是传递给g++的参数,表明使用了什么文件(-g 后面的file文件所指内容),生成了什么文件(-o 后面文件所指的内容)。所以这command 和 args两行参数表达的意思是:使用g++编译器将${file}文件进行编译,生成${fileDirname}/${fileBasenameNoExtension}文件。
/ x; Z( {3 G$ a& R& b

" |1 r8 `% t7 d上面部分是对单个c++文件或者多个c++文件进行编译。但是如果一个项目使用makefile进行编译,则如何使用vscode 进行配置呢?我们这里进行讲解。由于我们使用make进行编译,所以我们将command命令后面加上make,args后面添加相应的参数。比如我们需要进行debug 所以要控制makefile 中所有的gcc编译命令后添加-g。不然的话,就会出现,编译成功了。但是debug的时候,断点无效,所有的执行都是一下子执行完的情况。由于在我们的makefile 中通过TAG = dbg来控制加不加编译选项-g,如图3所示。所以这里我们的参数添加了"TAG=dbg",这里根据不同的makefile进行不同的配置。
' q$ @5 c$ H  |/ D( y
  1. {
    8 \' j7 T* z, ^1 H
  2.   "version": "2.0.0",, \9 p. O! l9 d
  3.   "tasks": [
    ; V# f) g: g# b3 ~
  4.     {
      I  U- C* C9 u, K: k1 b+ D! K
  5.       "type": "shell",7 h8 D' j: L7 Z4 q8 r. Q0 u
  6.       "label": "Build",% g! R8 Y; W. H0 c
  7.       "command": "make",: t+ P4 f; n+ a8 Y
  8.       "args": ["NTHREADS=1", "TAG=dbg"],
    ' J& U( U2 M, g7 \6 C
  9. 8 M* b  o/ c) o3 t% z

  10. 9 R5 s" ^9 n7 c8 C' a  U! q# ^
  11.       "group": {% _3 L# p  g" a  K
  12.         "kind": "build",
    : i+ C: K  I7 Q: u; C
  13.         "isDefault": true" C+ i" @( E, Y+ s
  14.       },
    ; M3 K+ M: c9 q  l: |8 C8 |* j% e
  15.       // "detail": "compiler: /usr/bin/g++"
    . v* V0 ?8 G* D
  16.     }; @( V) h* R1 T6 i
  17. 8 o% n) L4 g" X* S- `7 L+ X0 h
  18. }
复制代码
3 g1 x, c3 t0 x% o
一种可行的Makefile文件内容(gcc后添加-g命令)
2 [1 b8 a8 I  C7 I
  1. main: main.o input.o calcu.o
    3 F# [- U, j7 G1 o& l3 l2 Y) h
  2.   gcc -o main1 main.o input.o calcu.o7 O! Q1 o: l. Y( T6 [3 u
  3. main.o: main.c
    7 ~5 `# G9 u. c
  4.   gcc -c -g main.c+ p4 ~/ b- K; D) A  L
  5. input.o: input.c
    . E4 v  m3 ]) `  t9 A7 u
  6.   gcc -c -g input.c! N9 e* F  q% ]( z+ g+ ]
  7. calcu.o: calcu.c* V0 p$ i$ H9 W4 v" E# I8 b
  8.   gcc -c -g calcu.c$ n2 \9 W# _8 q
  9. 0 P5 K2 s+ o# C4 f* a( K% A

  10. 3 d: r. J, d4 I# @5 O* g5 @" C
  11. clean:* M  g5 V8 a! n7 n5 \6 a, n
  12.   rm *.o
    % U, ?, d, E* p! z
  13.   rm main
复制代码
  `& |( ]$ _' C2 Y! i' M  l9 B: Q) L
4.2、生成launch.json文件
/ X) Z7 X' E' i3 u0 d8 m(注意只有在.c文件中才会有下图中右上角的图标显示)9 T  |* g' h  M2 m) D
# D" E6 V- u$ p7 {* O3 R( r: f; ?0 m- p
微信图片_20240615144421.png
. q4 p! {7 O; t* w6 K6 B1 i
, f& z1 `0 h: m4 u5 T0 `7 I 微信图片_20240615144417.png ! \1 ~- K9 A! w4 k0 p! @
图4.2.1 添加调试配置文件' e0 R+ q! k' ?9 ?3 X2 a
  1. {
    # r  T( @. h! z1 m9 u+ A* Q/ V
  2.     "configurations": [
    / c( f6 R; ?- p$ ]
  3.         {5 X" Q0 q+ B1 D% R! X$ v2 V
  4.             "name": "C/C++: gcc build and debug active file",* G1 C- n9 M6 s% f- b) W  o/ t$ P3 D
  5.             "type": "cppdbg",/ E( N( H1 m" ]
  6.             "request": "launch",
    - K, @; W6 @& m$ ?8 U, n- X
  7.             "program": "${fileDirname}/${fileBasenameNoExtension}",
    * z' o7 g- G8 j) o/ e- O# T
  8.             "args": [],
    6 S- v) N+ a" f2 ?9 |; F. f" d
  9.             "stopAtEntry": false,- W2 V5 o5 ]* O& F3 w: [  L
  10.             "cwd": "${fileDirname}",4 d& X' X' o5 |
  11.             "environment": [],
    + U8 C: M$ q$ V) q* A1 z9 o' t
  12.             "externalConsole": false,3 e; P9 Q; P* q& {$ V0 u3 E
  13.             "MIMode": "gdb",  p) Z5 i3 k' w+ T* ~
  14.             "setupCommands": [! d1 M4 G, d9 `: L
  15.                 {
    ) P" y( |) s! R) k) ]8 i- O
  16.                     "description": "Enable pretty-printing for gdb",( z5 W6 G" E: ?4 O
  17.                     "text": "-enable-pretty-printing",6 a3 P9 y2 f5 M8 H3 j3 D0 O4 N$ s
  18.                     "ignoreFailures": true
    3 `  H0 Y+ h& U2 i9 N! |
  19.                 },% i; V3 \# A1 e- ?! v& ^; E
  20.                 {
    " I7 d" H) g7 z% C" }' ~& B. G
  21.                     "description": "Set Disassembly Flavor to Intel",
    $ U3 F  U3 Z  O) N& a  W" o' z
  22.                     "text": "-gdb-set disassembly-flavor intel",
    & J# G% [, S( D' B
  23.                     "ignoreFailures": true$ \, M$ u2 I3 V% l; W: G2 v
  24.                 }6 h# g) U( R. L0 l, v* k: |
  25.             ],
    8 Z0 t7 z- K, `
  26.             "preLaunchTask": "C/C++: gcc build active file",! h1 t7 l, [% E; y- }2 B
  27.             "miDebuggerPath": "/usr/bin/gdb"
    6 D; E! r( |8 R
  28.         }% O& Q/ f0 E# |3 A3 @
  29.     ],
    % Q$ D/ e5 R9 t; g, ?1 }( E
  30.     "version": "2.0.0"
    # A9 B* ~# G. A; T$ i/ O
  31. }
复制代码

, Q: z1 ]* K0 I$ v. M; N: j修改生成的launch.json文件如下- q( B1 C) A! f6 V' D
  1. {
    ' P0 E9 z' a) W7 n5 k3 O
  2.     "configurations": [
    2 k' t8 J% }5 Y8 ^" P! f+ X
  3.         {
      C( ?5 b) \, H0 H" ~( C7 e6 X; J
  4.             "name": "C/C++: g++ build and debug active file",! b) A- ?" K/ N  Z/ s* @% ^
  5.             "type": "cppdbg",
    " k/ t8 n) A4 V3 s, z1 o0 }; j
  6.             "request": "launch",) x: D" I' ~& `/ a- x4 ]4 `" z
  7.             "program": "${workspaceRoot}/a.out",
    7 q& ?. }* r  C' O7 b5 r1 y
  8.             "args": [ "1232"],
    5 f$ L4 p4 y: y" z- l& ]
  9.             "stopAtEntry": false,! ]- O/ l$ L" H( i: ^
  10.             "cwd": "${workspaceRoot}",
    - A# S9 Q( k& L1 t2 G
  11.             "environment": [],
    ' G/ a+ D" n; p% G4 S; R
  12.             "externalConsole": false,
    7 n3 [) |- _2 D  T0 a$ A( d3 ^
  13.             "MIMode": "gdb",
    4 x; R# L3 x, h
  14.             "setupCommands": [
    3 Y; c+ z, [0 V2 x7 f8 u; M
  15.                 {
    / O5 s# a: K7 k, J  z8 N
  16.                     "description": "Enable pretty-printing for gdb",
    0 f& w  F9 s' f( j
  17.                     "text": "-enable-pretty-printing",
    . p+ h1 A  x* n! Z5 M- w
  18.                     "ignoreFailures": true  e7 O$ ^/ I; a# U4 w1 q( {  x
  19.                 },
    3 z# e8 U9 @' c0 Y, d6 K- @
  20.                 {* G6 J5 K- ]6 `) b1 R4 @- N# W
  21.                     "description": "Set Disassembly Flavor to Intel",% B1 X4 |6 i. J% l; _
  22.                     "text": "-gdb-set disassembly-flavor intel",
    $ A" F) j* R0 g3 u2 h* \4 ^9 Q" q
  23.                     "ignoreFailures": true- F4 @: Y9 v) ?2 X5 a
  24.                 }- [5 X' X' N: K, E% |& d9 P3 t9 ~
  25.             ],5 [. T% N8 y0 M( |
  26.             "preLaunchTask": "Build",0 u% g, e( E! R9 h; Z  i
  27.             "miDebuggerPath": "/usr/bin/gdb"
    1 r+ W/ W$ d$ e9 I; G8 o
  28.         }
    ' `* G+ j' _" ]
  29.     ],
    % g; q' J3 J( _* t0 V$ x2 `2 K
  30.     "version": "2.0.0"
    # d1 [! a. X1 M2 U+ j) S; `
  31. }
复制代码
: e8 ~: y. a- w) W0 a  f
"program"选项代表,需要进行调试的程序,所以这里需要修改为对应的文件。"PreLaunchTask" 代表debug流程的前序任务,这里的内容需要和tasks.json中的label相同。"args"可以添加执行程序的过程中,需要添加的参数。在下面的代码中,我们就添加了cacti程序,以及参数。
% v9 u" I% z4 }4 s# U0 U$ X
( L0 x' n/ i% h/ H
4.3、生成c_cpp_properties.json文件
1 o  v5 H7 h' Zctrl+shift+p 搜索C/C++:Edit configurations(UI) 即可生成该文件
( c2 w3 g( P) u/ H, U/ Q( P0 y
  1. {. r# I/ G9 r+ j' O
  2.   "configurations": [
    ' j" P) N! s& g# c+ K* t
  3.     {( D0 N, I& e/ g7 x, n
  4.       "name": "Linux",
    ( @  _! v- B4 B4 G5 _
  5.       "includePath": ["${workspaceFolder}/**"],
    . e4 t8 I2 _# P" d& ^4 i: C3 b* E7 Q
  6.       "defines": [],
    ( G0 @0 r5 U. e) u8 j
  7.       "compilerPath": "/usr/bin/gcc",: b: |# R9 b, e$ d
  8.       "cStandard": "c11",1 d5 B5 N- {+ t2 i( W' a
  9.       "cppStandard": "c++17",
    : q% D1 a0 c1 l5 R- m( Z' h
  10.       "intelliSenseMode": "clang-x64"+ E0 |; J$ [$ K1 E
  11.     }; ^4 r, u, ^6 j3 T( M
  12.   ],7 t1 w6 X& ?& n/ Q/ Y$ I* ~* T
  13.   "version": 4
    . P) O$ N  w# Q
  14. }
复制代码

+ W# a, z5 e( {0 Y05安装Vim
# M0 D* |7 O% O' G" s9 I' p1、命令行安装, T8 _, |% [2 Y5 c3 x
  1. chen@ubuntu:~$ sudo apt-get install vim, t5 X6 J0 Y+ K! O) H* S$ P
  2. chen@ubuntu:~$ touch a.txt' ^5 `0 k& v4 B% j( C+ @5 }, F& k$ R
  3. chen@ubuntu:~$ sudo vi a.txt
复制代码
$ N' M) \7 g* o( H* ?
06安装gcc-arm-none-eabi' C, P8 `8 V+ |: ]4 |$ S, M6 F+ o1 T
1、下载嵌入式gcc编译器
: Y: e& V7 Y" J4 x% U" R) l/ Mhttps://developer.arm.com/downloads/-/gnu-rm$ E% C1 @5 T9 a" U
. q) o! D2 f7 G& b# q% j
微信图片_20240615144414.png
: X; `9 U9 ^$ `# ^- Y" Q
' N- j  Q9 r+ q4 G" m8 B3 t( v) h2、解压gcc-arm-none-eabi-10.3-2021.10
! ~' L% o( K+ z% `3 @5 m3 B
  1. chen@ubuntu:~$ tar -vxjf gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2
    # w7 h5 ^  v! |, o2 E1 F4 S
  2. #对于bz2、gz文件解压
    / m/ g, K9 L1 v' K/ ?6 J8 l* J8 ?
  3. tar -vxjf test1.tar.bz2
    : j$ h  W: L( e& P: `
  4. tar -vxzf test2.tar.gz( m* o1 C0 K; L/ w
  5. #对于bz2、gz文件压缩" Z0 }( x5 Y+ ?$ X! O/ M* b% b4 b
  6. tar -vxjf test1.tar.bz23 f2 v; R: k1 f- m
  7. tar -vxzf test2.tar.gz
复制代码
4 Q% T6 _: R/ \5 u* `
3、添加gcc-arm-none-eabi下的可执行文件命令到Terminal(终端)
2 @" a, v& R- }5 n1 ~! N4 l# ^
  1. chen@ubuntu:~$ sudo gedit .bashrc# `! M' n4 w, o, B! c' q& h. k+ Q, X
  2. #在文末添加下面一行
    7 K$ H7 d' W; c  L$ ]* l; F
  3. export PATH=$PATH:~/gcc-arm-none-eabi-10.3-2021.10/bin
      \3 [9 ]9 x/ [: B5 ]. e
  4. #添加上述行
    6 D" A: l8 h8 R
  5. chen@ubuntu:~$ source ~/.bashrc6 G. A9 s6 n0 s* ]* W
  6. chen@ubuntu:~$ arm-none-eabi-gcc --help
    7 @( x+ A6 p6 K
  7. chen@ubuntu:~$ arm-none-eabi-gcc -v
复制代码

- w# a  G8 e8 a3 t4 N+ D07安装stm32cubemx
" D* Z( c& n1 }1、下载stm32cubemx
* k; c& y: ~8 k7 {. q. ~https://www.st.com/zh/development-tools/stm32cubemx.html
7 q3 W5 k5 V" D+ i: Y: ]4 K# p2 `" m
微信图片_20240615144411.png
! _# x' A# _, m7 D
, J5 S( j4 ~* |7 L0 H5 P2、安装
  1. chen@ubuntu:~/Downloads$ unzip en.stm32cubemx-lin-v6-11-1.zip  R  S7 D9 @8 O: P
  2. chen@ubuntu:~/Downloads$ sudo chmod 777 SetupSTM32CubeMX-6.11.1
      {7 t6 a. R! z- ^# b+ [
  3. chen@ubuntu:~/Downloads$ ls -l: y& o/ H2 }; n1 b
  4. -rwxrwxrwx 1 chen chen 539136342 Apr 11 09:41 SetupSTM32CubeMX-6.11.1/ j5 h( R2 I9 p% g0 y
  5. chen@ubuntu:~/Downloads$
    4 g/ e2 Z1 y7 j1 O. s! T# W
  6. chen@ubuntu:~/Downloads$ sudo ./SetupSTM32CubeMX-6.11.1
    5 r, s/ t( U: r6 f6 a
  7. chen@ubuntu:~/Downloads$ find /usr -name STM32CubeMX- V# I  a: [2 Y3 z$ Q( Q) F- W0 c
  8. chen@ubuntu:/usr/local/STMicroelectronics/STM32Cube/STM32CubeMX$ ./STM32CubeMX
复制代码

0 U! r; V, ~$ Y; L! L3 D7 }3、增加图标到菜单: Z; ^, K: w7 o+ t% ]! C5 [- [
  1. chen@ubuntu:~$ /usr/share/applications/
    / n& ^4 K, m; B2 _3 i$ K8 g( ]3 v
  2. chen@ubuntu:/usr/share/applications$ sudo touch stm32cubemx.desktop9 k9 ~4 y  f2 u& j
  3. chen@ubuntu:/usr/share/applications$ sudo gedit stm32cubemx.desktop; N- H1 w: E$ _: K4 h9 {
  4. chen@ubuntu:/usr/share/applications$ cat stm32cubemx.desktop
    ! i; ]: B1 A& {- s- p
  5. [Desktop Entry]5 x$ C% b7 F$ t1 N) I
  6. Type=Application( @5 f0 `# h- C7 i
  7. Name=STM32CubeMX
    4 A& h* ^+ Z  D0 g+ M9 `% A# m
  8. Comment=STM32 Development Environment
    - c5 H/ F5 p" P& D- o
  9. Encoding=UTF-86 B. f/ G  X! J! y5 r
  10. StartupNotify=true
    ! J" N7 J. [1 t/ y1 D# `
  11. Terminal=false# B5 T6 ]" L) f! K; |0 V; g
  12. Categories=Development;java;c;c++;
    & B# Z. N% f5 L: J' Z7 [
  13. #icon= ##########图标的路径,自己找一个就行
    9 e3 P6 B, l, \3 h* J: N
  14. Exec= /usr/local/STMicroelectronics/STM32Cube/STM32CubeMX/STM32CubeMX# V4 d$ N" e- [
  15. chen@ubuntu:/usr/share/applications$
复制代码

- _1 d- J4 ^; ~7 |  N. U5 V08使用stm32cubemx生成初始代码* c. ]; H: E4 d8 }1 Q: g4 X
1、选择芯片->配置->Generate
8 @" p: i# }! _. F: t
1 |+ B3 r0 x; y6 w* k7 ?0 i: R
微信图片_20240615144405.png
: a# j# o; ?4 U# [
7 K8 F* P9 k2 ?2、一定要记得勾选PinOut-Debug-SerialWire否则后面下载程序时会报错Error: init mode failed (unable to connect to the target) 。这个是巨坑,一定要小心!!!!!!!!!!* ^4 @1 x" ~2 p. K

& i4 j8 E* ]. g
微信图片_20240615144355.png
) C% n7 X# Q7 O" Q

% z/ X% R4 [2 O09配置VSCode5 h* x1 W# P2 l$ u
1、使用vscode打开上述生成的初始代码
% c( M# A0 J. M( \5 Y1 \3 U2、生成VSCode配置文件+ C2 f0 e% _+ P5 T
2.1 c_cpp_properties.jason
7 q6 C0 t% q/ f5 n& F+ a- ^  Xctrl+shift+p->Edit Configurations(JSON)生成.vscode/c_cpp_properties.json文件
1 t7 d) b( D9 y8 W% g) P
1 F: `! U7 L- P4 l2 ?. {9 C
微信图片_20240615144351.png : K# G; u4 R. q( w7 I; c
  1. {
    ( L. P; U  a6 _% N' o* n: B
  2.     "configurations": [4 G& M( U& n% D. q3 n
  3.         {3 c) [+ }3 x; m2 M
  4.             "name": "Linux",
    # M' n! z2 r$ J4 B( H
  5.             "includePath": [
    8 V/ B) @+ @& j8 B& |
  6.                 "${workspaceFolder}/**"
    % W7 p, K9 c) q; H" U/ v
  7.             ],1 P- o7 Z* I7 T# y0 A+ c" V5 Y, M
  8.             "defines": [- m1 g5 y! p% |8 y
  9.                 "USE_HAL_DRIVER",/ E7 n6 D8 G: E: _' `
  10.                 "STM32F103xB"
    $ C5 N1 c- u& b9 j7 U
  11.             ],( K& U; ?9 @3 b
  12.             "compilerPath": "/home/chen/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc",
    . N4 o' |( N, ^, i. e! C$ @7 K  q6 L
  13.             "cStandard": "c99",
    4 Q5 z% d. U2 r, A( E3 S
  14.             "cppStandard": "gnu++14",
    4 b0 J- B) n% W6 }
  15.             "intelliSenseMode": "gcc-arm"
    * m3 q7 [* e  S
  16.         }1 N) l$ o( M/ y0 h
  17.     ]," l/ J& [3 n/ Y8 d! V- Y1 u/ {, K7 e
  18.     "version": 4
    6 s5 o' B# V4 e4 X* k
  19. }
复制代码

5 x: m- A5 V/ D2 y) a2.2 task.json
! J% M; I3 M3 J* z1 o* f. o2 r修改好task.json即可使用ctrl+shift+B对stm32工程进行编译
. K& v! X& u  d' N0 V* B0 \
2 A! m0 V% p2 W+ C9 C  {
微信图片_20240615144348.png $ i$ c1 }3 m1 p  R: c$ J# D' `/ H
7 m. T6 e! e5 ^" n2 X
( }) r1 |9 o+ L7 M6 A
微信图片_20240615144344.png 0 [% c; o% k) e1 b% k# h* y$ X' U

! g9 k+ j. m: Q/ ?3 O: D  y" C) X
. C1 T/ e" A# B) z* k$ ` 微信图片_20240615144341.png & ]" ]' |% d' }- S

* V) V& {& F; n$ k) j7 `" f7 O% E5 @1 j% `/ a3 H3 g- q
修改如下:  J6 {7 l. L$ }! `1 A! T( a
  1. {
    3 u$ `. \7 c% }" B' U/ [
  2.     // See https://go.microsoft.com/fwlink/?LinkId=7335580 M9 d! J+ I: w& `9 V7 ^. I
  3.     // for the documentation about the tasks.json format0 P1 C5 Z1 a/ R  \$ i/ W
  4.     "version": "2.0.0",; D2 }# v! g9 A! e- S! K/ h
  5.     "tasks": [; i9 I/ d" T: a2 c2 e5 z
  6.         {& Y% B/ h5 X! S! G8 C" `
  7.           "label": "Build",      // 标签名7 A- p- X- H7 J9 H- Y/ C8 K
  8.           "type": "shell",      // 终端(shell)型的任务
    2 g* }% K& b4 O0 ~
  9.           "command": "make",    // *执行的命令
    , I. ^) {9 f: k. Q
  10.           "args": [          // 命令的参数
    ; J1 E$ G7 x; f6 T. d, y" ]
  11.             "-j6"          // 表示六线程同时编译
    7 V$ U9 d7 Q" [; |, F7 [) D& k
  12.           ],
    $ J( e2 b  V+ `5 V
  13.           "problemMatcher": ["$gcc"],  // 表示如果出现了编译报错,问题面板会显示 gcc类型的错误
    . [1 q0 J5 w; v# a6 k) ^% A, U1 b
  14.           "group": {
    2 f: g/ P* s$ {6 X
  15.             "kind": "build",
    ) j, B% c% D# V
  16.             "isDefault": true6 m* I0 {2 }0 g  X+ \
  17.           }& q$ ?" J$ ^  P1 h
  18.         }
    8 a; t* k" \  k* j" t
  19. 1 d( K1 L% s0 w- }+ ]
  20. }
复制代码
0 G9 k5 ^' p1 {) i0 Y5 P
2.3 settings.json; Q$ f3 y; Q0 p) z* a5 o- Q- M" F
2 S5 k# R% J, G4 e2 [
3、上述”defines“字段中的定义应包括stm32cubemx生成的Makefile文件中相关的宏定义
% u1 C  ^6 v4 c, h- p) b! s" O& k4 @" ?* U0 P
微信图片_20240615144337.png
/ z  w  G  ~% z9 n: X9 V* T$ P' D& Q, i8 j
4、make编译/使用ctrl+shift+B
% o* b: [7 T4 X7 k! A; T
  1. chen@ubuntu:~/prj_vscode/linux-stm32/test$ make clean
    , l0 p! b/ _" g( g# d
  2. chen@ubuntu:~/prj_vscode/linux-stm32/test$ make
复制代码

% B0 A) X0 `* s10调试( k: ]/ ?3 p. \4 N
1、安装openocd
  1. chen@ubuntu:~$ sudo apt-get install openocd + B! Z( R+ G4 Y4 ]
  2. chen@ubuntu:~$ whereis openocd2 m+ b# [, ]3 ^. a* U9 b5 `; \
  3. openocd: 3 E# F# D2 p  U. @% B4 ^6 N
  4. /usr/bin/openocd
    6 j3 H5 d& F9 G$ c! q' e
  5. /usr/share/openocd
    1 P! I6 b4 p* W# a% z0 ?
  6. /usr/share/man/man1/openocd.1.gz ! O5 l- S% d# b/ p! b8 E
  7. /usr/share/info/openocd.info-2.gz
    7 b: I2 C% d6 a. O. s) x
  8. /usr/share/info/openocd.info.gz
    : y/ p9 x" s& }( F
  9. /usr/share/info/openocd.info-1.gz+ f( [+ w1 J! Z# n! H, y" h5 J
  10. chen@ubuntu:~$
    ' F  |/ T# r3 i0 M4 }/ w
  11. //连接好stlinkv2-1到电脑和pcb板,输入以下命令。完成连接  a( ]6 Z* A. n! O  g/ Z
  12. chen@ubuntu:/usr/share/openocd/scripts$ openocd -f interface/stlink-v2.cfg -f target/stm32f1x_stlink.cfg
复制代码

7 R+ K, O% V% m) ]" L, P6 S( f2、在tasks.json中使用openocd进行编译后的下载- Z( i; J7 H( N/ y: a' ^) Z
, e5 H% V. m$ _* A6 e# N! b: i
在原本的tasks.json中追加popenocd的相关内容
7 `1 a, q/ L& s$ Z  s
  1. {3 V" |4 j/ k% ^+ W' M! u( E" m& \
  2.     // See https://go.microsoft.com/fwlink/?LinkId=7335585 J% a! s) o; w; \3 }9 A: D
  3.     // for the documentation about the tasks.json format
    : ?: d5 U4 q4 L! U6 p$ b# T/ D
  4.     "version": "2.0.0",. S6 @+ a( n/ U2 L) h" g
  5.     "tasks": [
    5 H0 W( _$ f* x# E: B
  6.         {3 e5 `9 x1 C8 v4 g
  7.           "label": "Build",      // 标签名
    : o- b8 \5 Z( s. X2 v: T4 h2 U
  8.           "type": "shell",      // 终端(shell)型的任务
    " ^; [% w( s7 v9 y6 o4 {
  9.           "command": "make",    // *执行的命令- W) A4 x& Q' i5 R/ p
  10.           "args": [          // 命令的参数1 W5 D- w1 ~/ Y- N# j* ?
  11.                           // "-j6"表示六线程同时编译0 }! d* X! p5 i8 o5 c9 N4 i: v8 E
  12.           ],6 b1 W1 Q( \" n; {3 c
  13.           //"problemMatcher": ["$gcc"],  // 表示如果出现了编译报错,问题面板会显示 gcc类型的错误# V; t. I+ N+ n: z  n
  14.           "group": {
    8 {# O& j; t* S( }) T% o& X
  15.             "kind": "build"," E% K, W5 p: i& S' U% u+ W
  16.             "isDefault": true
    ' b( i% v& F' S
  17.           }
    : D/ L6 b. Q, `: y$ a9 C
  18.         },9 U( s, |; u8 J7 t6 w/ v: ]. G
  19.         {) C$ k0 y2 N8 U4 K9 y3 q! D6 B) I
  20.             "dependsOn":"Build",          // 任务依赖6 u: ^( Q  H( ^& p" O: q
  21.             "label": "Build and Download",
    # S! g/ t) t% f3 h/ K- K8 I4 @
  22.             "type": "shell",+ [+ b$ s7 t. L  e
  23.             "command": "openocd",
      K' ?3 O/ G5 Q, V  M3 @2 O4 g
  24.             "args": [9 ]+ P0 ?8 @. S
  25.             "-f",0 [' Q& u6 ?* ~" r" h. b2 |
  26.             "/usr/share/openocd/scripts/interface/stlink-v2.cfg",; g1 U) c% f/ X6 j+ e, Y
  27.             "-f",& u% O8 X" ^0 M- C' I7 M6 {/ T. [
  28.             "/usr/share/openocd/scripts/target/stm32f1x_stlink.cfg",
    / A( m$ p9 u( v/ C& u9 [  u
  29.             "-c",& D! Z) s/ i' R$ i- W( P- X
  30.             "program ./build/${workspaceFolderBasename}.elf verify reset exit"
    ( F" D) d! {/ |& q- z$ R( U
  31.             ],( m6 p9 T) F) d: B2 i
  32.             "problemMatcher": []
    % ~2 B" Z2 e# W: `% O
  33.         }
    / M8 N$ q) {' h* i" v
  34.         
    - b5 a7 b) @1 h  j) j/ n

  35. 8 C8 y0 _/ Q7 n1 |0 t
  36. }
复制代码

! L1 u% ^. {, Y9 J! S这个任务要求先进行 Build任务,然后通过命令打开 openocd,加载两个配置文件(-f interface/stlink.cfg 和 -f target/stm32l1.cfg),同时,这个 -c 的选项后面跟着的就是openocd成功加载后执行的内置命令,这里我们让它下载、验证、reset,然后直接退出,这样就实现了一条龙式的下载。
3 R0 R* }6 r3 {6 Y# u! F: U
/ S. d; I9 `. a5 v1 |
微信图片_20240615144333.png ! C* d) ]% j* X& @
5 D4 d4 f  z2 {6 C# j: R% ?  F
微信图片_20240615144330.png
$ z0 i8 o  E" z' z( g
) i- @. |% U9 _5 T$ e( b3 J3、使用openocd进行调试
/ f4 c( p1 b8 f- ^$ f2 E3.1 安装Cortex Debug和Venus's Cortex-Debug插件(第二个插件一定要安装,否则会报错Failed to launch OpenOCD GDB Server: Timeout.)
  E" `6 L) Z, d: Z
& ~" `+ o4 _1 \7 `* f
微信图片_20240615144327.png 5 J: Y+ J% H% B0 W! ]" G, a  m. ?
" B! |& p) a* m
微信图片_20240615144324.png
8 ~. {7 |# O& v, n* B8 \$ d

% F  y) J" F; `; i# N3.2、 添加调试配置
4 o0 g/ i4 t' C" N& x2 z7 }
2 }  A; C# `( Q; z2 o- E
微信图片_20240615144321.png
+ F4 M+ F6 r( O7 \, l$ I" e
8 Y9 s5 y- F0 V% T
3.3 选择cortex debug,生成launch.json,并修改内容如下
- p8 ?! U: r: W) J4 i  n
/ o( t& O! f, K9 p
微信图片_20240615144318.png
- Z9 O( `! z) i1 P& U2 @6 [
! T' ^( Z) Y' p. B. B5 P$ m
  1. {
    # G: b/ W+ F! P/ b, M
  2.     // 使用 IntelliSense 了解相关属性。
    % S- M2 C3 X0 D, Q+ K# m
  3.     // 悬停以查看现有属性的描述。+ J6 \- O! c: R) k2 C0 [4 `
  4.     // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    # A, I% R/ |8 p0 A! G- F& j
  5.     "version": "0.2.0",! n( L7 C) m# \' F' ^7 @+ g
  6.     "configurations": [
    / B$ E1 |2 `5 b$ H5 S. Z
  7.         {
    " e/ g2 W$ K, m0 h3 Y$ |4 z
  8.             "name": "Cortex Debug",
    / V  |$ z; q! v; r7 T) I
  9.             "cwd": "${workspaceRoot}",  // 输出路径
    $ R& Z. T' H9 O2 t4 X, W" a
  10.             "executable": "./build/${workspaceRootFolderName}.elf",    // 要调试的程序
    3 q4 Y) i' r; {1 b. o+ b5 ?
  11.             "request": "launch",
    " C( O5 m; Q  o; v7 i
  12.             "type": "cortex-debug",& G  K+ b7 M8 z# d% M: ]
  13.             "servertype": "openocd",    // 调试器选择  F  @+ a, f% p
  14.             "device": "STM32F103C8",    // 使用J-link GDB Server时必须;其他GBD Server时可选(有可能帮助自动选择 SVD文件)。支持的设备见 https://www.segger.com/downloads/supported-devices.php
    4 x+ ]* k6 x( u2 y1 N2 n; H) B
  15.             //"svdFile": "./STM32L15xC.svd",  // svd文件,有这个文件才能查看寄存器的值,每个单片机都不同。可以在以下地址找到 https://github.com/posborne/cmsis-svd下载
      n  \6 }( V9 ?# v" Y, r
  16.             "interface": "swd",. s, O" |3 T8 ^
  17.             "configFiles": [
    2 {$ ?# _4 X0 f
  18.                 //"${workspaceRoot}/openocd.cfg",  // *可以通过加载该文件来配置4 s1 p1 g4 g/ K8 t7 _3 y) a; W
  19.                 "/usr/share/openocd/scripts/interface/stlink-v2.cfg",# M$ b' V/ x) Z6 ]$ s
  20.                 "/usr/share/openocd/scripts/target/stm32f1x.cfg"
    1 L. J0 D- a" O4 m* u+ D6 v1 a
  21.             ],
    : E' D- w+ K; |% }
  22.             "runToEntryPoint": "false",+ X# O3 [" Z4 b  S' N& F; l
  23.             //"runToMain": false,
    - j9 N2 p# }% q# K- U) ]
  24.             "preLaunchTask": "Build",   // 在调试前预先执行的任务,此处是tasks.json中的
    " d' B0 m0 y9 v/ C* d& h
  25.             //"armToolchainPath": "C:\\Program Files (x86)\\GNU Tools ARM Embedded\\5.4 2016q3\\bin"    // 如果没有把 arm工具链路径添加到系统环境变量,则需要这条指令添加你的路径5 Y( ^9 q# ]1 k  u- N$ M
  26.         }
    : o; w4 u5 s+ l0 |6 h8 h7 ~" x
  27. ! s: C3 y' m( |6 O
  28. }
复制代码
# V8 x1 a+ |' W9 b+ h& E9 |/ J$ S6 t
3.4 调试8 X/ l( r' i2 Z  i4 b5 \7 o5 F

2 y: |  q" B6 f: I; K* [7 G. L
微信图片_20240615144314.png ( D, y9 j" c% |! g0 C
0 S+ \9 p$ s1 C: x
11使用VSCode串口工具

, [8 T9 R% F4 O2 z# M1、安装serial monitor. H7 F- ^% ~: v) A; O
  Z4 P9 `4 |: x( M: D
微信图片_20240615144304.png
1 b' V/ W2 u" ~  K7 d/ Y& N) S5 u% U) F1 E& c" |* H, Z  d7 g  b
2、查看连接的usb
, i# n7 |" |: B" C7 O  W: i( t
  1. chen@ubuntu:~$ lsusb
    : @8 Q9 y. l( f
  2. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub! L1 ?6 \& n% q  F' l8 L
  3. Bus 002 Device 007: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
    " s* Z5 G! f9 Q3 I5 A& V! }
  4. Bus 002 Device 006: ID 0483:3748 STMicroelectronics ST-LINK/V2
    . H' v) A5 v' l6 w5 h+ `% v2 i8 r8 E
  5. Bus 002 Device 004: ID 0e0f:0008 VMware, Inc. VMware Virtual USB Mouse5 Q8 A8 Y$ r6 Q1 `& d- D1 b
  6. Bus 002 Device 003: ID 0e0f:0002 VMware, Inc. Virtual USB Hub
    % g) H3 I7 k. v& ~
  7. Bus 002 Device 002: ID 0e0f:0003 VMware, Inc. Virtual Mouse
    " C8 V9 ~" j4 i& j( `
  8. Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    ' b* k& g: a9 K& H( G
  9. chen@ubuntu:~$
复制代码
& R- V# `8 q9 Z1 `1 `; ?* u8 N
2、加权限
  1. chen@ubuntu:~$ sudo chmod 777 /dev/ttyUSB0
复制代码

& x) V! V6 g0 F12使用git
9 S2 Y3 F+ q% Q  i. m1、检查状态
1 e, @- ~/ y4 U- ]
  1. //在执行 git add 和 git commit 之前,先执行以下命令查看当前仓库的状态:& C* q  r) L$ v
  2. git status
    7 g# Q' t. Y8 [- k% d3 ]/ r* Z1 r
  3. //这将显示工作目录和暂存区的状态。确保没有未追踪的文件,也确保已将需要提交的文件添加到暂存区。
复制代码

0 k. H1 T$ Q" ?) Y& i2、确认 Git 配置:# O6 x. A* \0 m7 e. j
  1. git config user.name" h5 X; X) p5 o3 t
  2. git config user.email" W2 X6 |7 Z8 U; B" e5 ]

  3. 7 y- x1 z! H& y# d; {! J* s

  4. $ S% T# V" I% D' T! h; ~0 r* A
  5. 如果没有设置,请使用以下命令设置:
    ; P3 |. T2 S8 D! i  ^$ I& Y
  6. git config --global user.name "Your Name"* ^. ~1 t, O- Q
  7. git config --global user.email "your.email@example.com"
复制代码

. S" @, P& y; y& g3、检查文件权限
; T  F2 n3 G9 c& h& w确保你有权限在新文件夹中进行写操作。使用 ls -l 命令检查文件和目录的权限:
: s8 d3 e& H& ]ls -l
/ U) |# |, S" }* H" u  M, C* V9 E) L8 P7 X: @
4、查看错误信息:
1 R" H+ L+ O8 p+ |  |如果 git commit 操作没有反应,尝试使用 -v 参数查看详细的错误信息:
4 k' S, B  F( \% bgit commit -v
  W+ _" f) Y$ _$ R$ g' a9 l% M1 B' H! x$ A, D( |% D
5、清理缓存:% C& U1 `! b9 `) w# T# b- A
有时 Git 的内部缓存可能导致问题。尝试清理 Git 缓存并重新尝试:$ F5 Z- ~/ a' s& D4 v+ g
git rm -r --cached .
$ L  I7 w' n1 v$ r& d3 @7 e/ S' cgit add .
' Y: @* u* [' \* K* B6 Fgit commit -m "Your commit message"
1 \0 R) i% z/ c: q5 m5 n这将清理缓存并重新添加和提交所有更改。
6 F4 f* m# n. ?/ C8 k- W( X& n0 a; h5 Z' m8 n2 c5 }+ {* ?' v4 q. }

; I1 E% y2 r5 a- L# q+ v( b6 l转载自:kevin5 E, e9 B9 B8 U

; v0 M7 |5 w0 @4 k. @如有侵权请联系删除
0 Q% q; y* ?5 C  o! R+ j9 l3 t; o/ u. ~

7 \5 w$ J! Y0 F. d
( W9 e& k+ y( u9 S
1.png
收藏 评论0 发布时间:2024-6-15 14:45

举报

0个回答

所属标签

相似分享

官网相关资源

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