“ 从ubuntu20.04入门到完成stm32程序的调试。”
, z) {+ I& o- w, L) _
w, e7 d) E! [; G) N4 r
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 w4、安装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 ^* e7、安装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 k8、使用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' P11、使用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
- chen@ubuntu:/etc/apt$ sudo gedit sources.list
复制代码- #阿里源(Ubuntu20.04)) O- _; }& _" }1 \1 H# I- i
- deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
& B! `! H/ B1 Y) k- H - deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse; J9 N; g: z& j
- deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
! g1 T$ {; y: f - deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse9 g5 W3 A! g: C# u! C) w2 \
- deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse0 x" b. H7 e; M8 D, G
- deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
+ g# F6 `. m/ g8 u- t - # deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
- m! }7 u6 u4 L8 h. |" z% r; e$ ] - # deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse/ f: e; Y) {& G2 m c; `
- deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
* X; m1 F3 K. Y, G1 J - deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
复制代码- #清华源(Ubuntu20.04)- C5 f6 T( r0 F
- # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
, `+ O7 R' L# q/ A. o$ A+ b+ F" v - deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
! u' ]' G$ B; f6 ~9 c - # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
& q5 D$ j3 A6 ?! J7 P - deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
8 K3 l5 i6 V7 X - # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse% h: X( H8 {" T/ L; q2 R. |
- deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse) [5 t0 ` J; G/ b$ b1 O
- # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse% y3 m" t) \! Z0 z0 h
- deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
* X0 k9 T5 O. t - # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
! j; D5 S4 @9 C5 i' X- t# [ - 7 }: d4 c2 C9 C: x9 D
- ! a8 G5 K, S4 d( M1 V" W
- # 预发布软件源,不建议启用
8 v+ B% j! [$ }, l* A/ V - # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
( i9 l! }; P q0 ?+ D - # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
复制代码- #中科大源(Ubuntu20.04)
& |) @9 P% i% n, k' S - deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
0 y+ {$ h9 H% i5 m1 o - deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
$ V- v! r) x+ G5 L3 U; f; L3 j' o - deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse. @: {: S7 D; W7 M. c
- deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse! B' q& }$ V( }) x# |5 J
- deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
% M0 Q7 d9 z5 H" i. C - deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
: a$ F$ z& u6 l1 y1 V5 Z9 Q - deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse& Y2 U, e+ @8 B& v1 q6 z
- deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
1 u0 k- O+ ^& ]1 n/ u' U - deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
& U C. }0 P& L5 _# n6 K6 d5 F - 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; 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
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( `- chen@ubuntu:~$ sudo apt-get install ibus-pinyin- J2 E& |% M- E2 ]5 Y$ I
- chen@ubuntu:~$ ibus restart
) q7 V! v/ {) e - chen@ubuntu:~$ ibus-setup
复制代码 6 N6 D" C* T' m6 |( V; L
\* k, O+ p( |& p" b
( {5 ?, o; \1 ~
# f6 a/ J& u0 P& V; {
5 t$ n* W& r& X, a$ Q
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
- chen@ubuntu:~/Downloads$ sudo dpkg -i code_1.89.1-1715060508_amd64.deb, p) N1 M, J5 C2 K# c- X. x9 d
- 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
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 ?
* S( x$ x9 B F O1 o( f9 G0 X4 o$ m* j! |0 ? P
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- chen@ubuntu:~$ sudo apt-get install git
; N" @$ w! ?3 B - 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
# p2 x4 i3 c5 ^, L7 i
0 Y" O9 U+ m# o0 o: P+ E1 z
- git clone https://gitee.com/kevinkerry/linux-stm32.git
" I. J+ c7 L" W5 H3 ^3 u - git config --global user.name 'kevin.kerry'9 g! z8 i1 K7 m# j% P' i8 e" o( d( g
- git config --global user.email 'ct15762287606@outlook.com'
% M/ F, ^- b( t. F
9 Z2 ]% z! Q. J
1 y `% S* M9 l7 E% @1 W- #示例
( c- |5 M5 ?0 [9 d - chen@ubuntu:~/prj_vscode$ git clone https://gitee.com/kevinkerry/linux-stm32.git( X) i, ]1 E9 p$ t- d
- Cloning into 'linux-stm32'...
1 V5 L. m4 e4 @; t1 K - Username for 'https://gitee.com': ct15762287606@outlook.com# [2 h8 M# x7 u5 M
- Password for 'https://ct15762287606@outlook.com@gitee.com':" h/ e! z- Z" L: M
- remote: Enumerating objects: 4, done.
) G0 H* j/ d; U$ r) P - remote: Counting objects: 100% (4/4), done.
( {9 b0 S, b- d4 w5 } - remote: Compressing objects: 100% (4/4), done.' g: t* D0 L E, a
- remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0* S$ `) s: w, }8 z; L
- Unpacking objects: 100% (4/4), 1.79 KiB | 1.79 MiB/s, done.7 r& u# S! g$ Y
- chen@ubuntu:~/prj_vscode$ ls1 X5 N7 G7 D# ^* R% o
- 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
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
* ^, 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
/ }) 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
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
" `# 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
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# ?: ?
- chen@ubuntu:~$ apt-cache depends build-essential: N! d8 r7 q8 M3 `, E( K$ o; j3 [# L
- build-essential/ _: R5 P V4 ?6 F- s
- |Depends: libc6-dev
. y3 K& @6 p$ X- n! P - Depends: <libc-dev>
, P7 f: L% ~# Z; K - libc6-dev# Z: b5 R! B. a8 e/ X0 o
- Depends: gcc
+ }3 k Q# ?4 r6 X% K0 J - Depends: g++
7 t9 ?- n* L7 X( ] - Depends: make) r' @( a) ?" ?
- make-guile5 x9 q2 `, S8 p7 L3 h8 h( C* }; |
- 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" ?- 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- chen@ubuntu:~$ mkdir linux_c& H$ m* p5 O @- B0 w
- chen@ubuntu:~$ cd linux_c
2 C; F2 x! Y; l0 y, H5 t - chen@ubuntu:~$ touch hello.c3 }6 Q8 B: X; U9 i6 S& u3 t: Q
- chen@ubuntu:~$ sudo vi hello.c
* J$ {. ^2 [* C5 J3 ] - chen@ubuntu:~$ cat hello.c
4 J+ H9 ]9 v1 K0 V+ J - #include <stdio.h>
% O u9 A9 d) P) G, T4 Z - int main(int argc,char *argv[])8 S" w% I0 Q$ `% ?
- { w8 U9 k* r1 U8 i5 z
- printf("hello world\n");
, y: l1 P3 C! D9 K: M9 c - }# f, X# i) J6 v H) y! j: l
- chen@ubuntu:~$ gcc hello.c0 @# W, U3 s1 q; \8 `: [ N
- chen@ubuntu:~$ gcc hello.c -o hello
, U+ w* Y( x) l8 _+ M - chen@ubuntu:~$ ls -al& O6 |8 ?( e+ ]) A. {, Y0 A& D+ C
- total 523 A) Q( ^$ d* H/ G6 n2 i
- drwxrwxr-x 2 chen chen 4096 May 16 18:32 .* J" ]( s& A* v( l! J7 {
- drwxrwxr-x 4 chen chen 4096 May 16 17:37 ..
/ j4 Z. ^3 I) @3 a3 | - -rwxrwxr-x 1 chen chen 16696 May 16 18:29 a.out5 G" f3 W/ _! d0 v# |- R
- -rwxrwxr-x 1 chen chen 16696 May 16 18:32 hello
8 C; K$ k* W3 J" { - -rw-rw-r-- 1 chen chen 82 May 16 18:29 hello.c
8 N' t3 [, p6 d. ^& b5 C% F - chen@ubuntu:~$ ./a.out- _; X" o, b" X" B5 D
- chen@ubuntu:~$ ./hello
复制代码
) B7 n2 u( U; O% [# a8 C3.2 有makefile
8 k9 p) }4 a" L- y! x$ n# i3.2.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
- #include <stdio.h>) G8 B% A$ \' ?2 h( Q7 o
- #include "input.h"# X9 g2 A4 \: }
- #include "calcu.h"
! D& L% i3 v t, @/ n C' v/ e0 i
# \4 S& `0 ^" M0 P' X) W. {
+ |+ r, H6 e E' E- int main(int argc, char *argv[])
* i1 N s# z. F# b9 G! x! m/ j6 _ - {
7 h0 {) J0 r/ ? x$ K - ! @) C: ?% U: u; N
- 8 D$ L: s* S3 I( A
- int a, b, num;
6 N9 P7 e5 Q% T! Q: _9 I
* G$ n. A2 F6 @1 d: h @, O4 f5 X
6 [8 K' Q/ r5 V, K6 R- input_int(&a, &b);
! A* {% |2 m0 c7 ^: E5 Q% b - num = calcu(a, b);& X* {) \3 x8 e
- printf("%d + %d = %d\r\n", a, b, num);5 u8 U2 {5 ` G( D/ I
-
" o% S/ u" L* h* R- l; ] - }
复制代码 & r+ s1 y8 [8 h( \
input.h- #ifndef _INPUT_H
0 s2 |7 G7 V' E9 ? - #define _INPUT_H/ }! {: E3 Y' f: j6 Y) X4 }
) h6 v G* }, V( r: |- void input_int(int *a, int *b);% q9 e, a; I4 ]
3 ~( E8 ]% o* i# W, D! G- #endif
复制代码
. i% _& O# ?5 l# Y4 Xinput.c3 f! S+ ]' n- Y
- #include <stdio.h>
4 T5 y. |2 o. i" Q4 D& J - #include "input.h"
% x4 H: c3 W: Z2 Q3 T& A
: U# ~( S% g) o N5 }2 u
6 N# s5 D- e5 E) i, q: f7 O/ g- void input_int(int *a, int *b)
3 q7 u* e0 U! z; A% s - {- j+ [8 V2 i, l- ~% n0 k. n
- printf("input two num:");3 @" ? q* D* I* a. U0 y$ s* Q: g
- scanf("%d %d", a, b);
2 h) I( |0 P( G: l - printf("\r\n");! t$ x9 x' `& M* G# I. X. n2 }+ {
- }
复制代码 + M: t B) q& D% r' ?) q, j
calcu.h- #ifndef _CALCU_H% Y) a2 R( \6 L4 q& W' l
- #define _CALCU_H1 [0 @9 X+ L8 }% c
- $ O0 p- D" F* L) t" F D
. k5 o7 A q; c- int calcu(int a, int b);
2 H/ x5 a: i+ F/ r+ Z0 R - #endif
复制代码 , x/ h1 E* {; c0 Y: u% a0 M
calcu.c6 u4 {' N$ `! T6 ]7 N
- #include "calcu.h"
3 D, ?" V7 p) E7 J - 9 ^* Y7 w5 n X
- 8 Y1 I. C% b" L! k$ q" p
- int calcu(int a,int b)+ X- M o7 \) l x* a8 O3 Q! P
- {
. [" S1 E& d' e" v - return (a+b);
3 D' d3 B1 q/ Z: Y4 Y v - }
复制代码 % b, V2 W: l+ v' [& \$ Q' O
3.2.2 编译测试一下
+ ?, s4 T2 Z* T5 |$ W; }- chen@ubuntu:~/prj_vscode/linux-c$ gcc main.c input.c calcu.c -o main$ ]+ A, h# y G3 i* j
- chen@ubuntu:~/prj_vscode/linux-c$ ./main2 Y3 `* P& r6 E3 U
- input two num:23 56$ [5 x4 a/ n) P, \3 T) a
- ( j, u) |2 Y4 w$ ^* l
- ) z4 w* i- C4 E' y% Z- |
- 23 + 56 = 79% x/ J0 I2 {: H; O( O! V
- chen@ubuntu:~/prj_vscode/linux-c$
复制代码- chen@ubuntu:~/prj_vscode/linux-c$ touch Makefile8 l+ M/ X5 w* R: e6 ^2 R
- chen@ubuntu:~/prj_vscode/linux-c$ cat Makefile# A4 y( D p4 g
- main: main.o input.o calcu.o
1 n9 e4 h+ l! { - gcc -o main main.o input.o calcu.o! u: c+ z. {- i2 ~! G7 u- g! v0 p% v, I
- main.o: main.c
, D/ | H" B. s) b - gcc -c main.c
+ J) T/ G6 L( h - input.o: input.c
3 a8 b, A% P) B! t# Y1 Q. P; ~ - gcc -c input.c
0 ^$ O i7 t. W7 d - calcu.o: calcu.c5 G C- K6 A/ k+ h
- gcc -c calcu.c! T! U/ T" W7 r3 v7 q
- . F1 g7 J+ p( s: o" `6 C
+ z& `& q& X: W6 s1 s" u; _- clean:+ b5 V; z0 s8 ], _
- rm *.o
8 {! Z5 |2 L, O/ P7 c7 l2 y+ S$ w - rm main7 Q u2 A& j0 `
- chen@ubuntu:~/prj_vscode/linux-c$ make clean Z( y$ j. p: u* P7 x
- 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
* 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
% d, K7 g1 V y( q4 R
# }- C8 X7 W8 n" M
修改生成的task.json文件如下3 o) I' A$ l! I/ D1 q0 f$ N
- {3 ?) i/ w* `2 P+ \+ c6 ]3 H
- "tasks": [
3 U, }3 [ V6 _* Y - {
. Z2 m8 O) t4 g* H* x - "type": "cppbuild",
# f2 Z8 j7 n: p) m/ k. r/ p O - "label": "C/C++: gcc build active file",
) ]8 i. O4 X8 u3 [- R. j - "command": "/usr/bin/gcc",
3 R+ ^% L. z' u$ A5 p - "args": [
7 w4 _8 ?# H6 K; k# g" {$ r - "-fdiagnostics-color=always",
- O6 @, o+ V% |! C - "-g",# b, s; H) M9 P, ]
- "${file}"," ?2 l, }& x' g# p
- "-o",! Q9 ]9 b; L/ r a0 h+ G
- "${fileDirname}/${fileBasenameNoExtension}"
+ `! }, c- A% A% C6 J - ],
9 B* A% S# Z/ h6 u - "options": {7 L! m1 I' ^$ k# r7 J4 s
- "cwd": "${fileDirname}"
8 I0 t4 ^ `8 Q - },
7 N& L+ e2 _1 Z) i. O- s - "problemMatcher": [
9 B1 u+ P! U" }# |. a7 r* p( m" X" ` - "$gcc"* p8 Y/ g- v/ X) p9 j1 x J
- ],! d# X3 v; O8 C. L
- "group": {
! c" ]6 L9 v O* z1 C- X - "kind": "build",
1 m* F3 e3 U: R( |& F - "isDefault": true$ R3 \ `0 F' y- W4 r: r, Q
- },
6 P$ U0 N3 s) G. X: U! `7 i! ^' q - "detail": "Task generated by Debugger."
* x9 g, s4 R2 }, T9 ]' y - }- i4 E/ p p9 ]
- ],! G8 g L! H) U& u9 [ k! _6 k7 Y
- "version": "2.0.0"
/ @. ~. V5 J6 O - }
复制代码
. v& q: q* U7 o% A* C修改如下- {
R' n+ @7 P; T3 ^( B - "version": "2.0.0",, j+ M; r! f- _0 _" o7 h; Q
- "tasks": [. \! s+ z; T# k# M( _( \
- {9 U/ U/ K! _# E* z
- "type": "shell",
# K/ G0 [6 k' u* k! M" R+ l - "label": "C/C++: g++ build active file",
0 X1 _3 i' ^; k! J% u - "command": "/usr/bin/g++",$ w( z. o7 O U, g8 y, Y6 i0 O
- "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
& l+ k: s/ O/ K3 \; i: t - "options": {
3 E9 ~, ~+ I& ?- F+ F- }7 F) B - "cwd": "/usr/bin"+ N$ Y) r8 x; x, m, c O3 w
- },
5 p/ Z7 D) a4 g! r) U [( ] - "problemMatcher": ["$gcc"],
: {+ F8 t9 a2 j& Y - "group": {
3 Z/ r( V. M# X9 o - "kind": "build",
# t1 k% f$ p( o( U$ d - "isDefault": true
2 z6 Z, t- c3 ~" P5 s3 b - },
2 Z) A) N- |- U' G! G - "detail": "Task generated by Debugger." Z: I$ X2 v% q( n) M! D0 T' Z
- }
, s, N9 ?/ m: t - ]: T9 j5 j- s' n O9 l
- }
复制代码
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- {
8 \' j7 T* z, ^1 H - "version": "2.0.0",, \9 p. O! l9 d
- "tasks": [
; V# f) g: g# b3 ~ - {
I U- C* C9 u, K: k1 b+ D! K - "type": "shell",7 h8 D' j: L7 Z4 q8 r. Q0 u
- "label": "Build",% g! R8 Y; W. H0 c
- "command": "make",: t+ P4 f; n+ a8 Y
- "args": ["NTHREADS=1", "TAG=dbg"],
' J& U( U2 M, g7 \6 C - 8 M* b o/ c) o3 t% z
9 R5 s" ^9 n7 c8 C' a U! q# ^- "group": {% _3 L# p g" a K
- "kind": "build",
: i+ C: K I7 Q: u; C - "isDefault": true" C+ i" @( E, Y+ s
- },
; M3 K+ M: c9 q l: |8 C8 |* j% e - // "detail": "compiler: /usr/bin/g++"
. v* V0 ?8 G* D - }; @( V) h* R1 T6 i
- 8 o% n) L4 g" X* S- `7 L+ X0 h
- }
复制代码 3 g1 x, c3 t0 x% o
一种可行的Makefile文件内容(gcc后添加-g命令)
2 [1 b8 a8 I C7 I- main: main.o input.o calcu.o
3 F# [- U, j7 G1 o& l3 l2 Y) h - gcc -o main1 main.o input.o calcu.o7 O! Q1 o: l. Y( T6 [3 u
- main.o: main.c
7 ~5 `# G9 u. c - gcc -c -g main.c+ p4 ~/ b- K; D) A L
- input.o: input.c
. E4 v m3 ]) ` t9 A7 u - gcc -c -g input.c! N9 e* F q% ]( z+ g+ ]
- calcu.o: calcu.c* V0 p$ i$ H9 W4 v" E# I8 b
- gcc -c -g calcu.c$ n2 \9 W# _8 q
- 0 P5 K2 s+ o# C4 f* a( K% A
3 d: r. J, d4 I# @5 O* g5 @" C- clean:* M g5 V8 a! n7 n5 \6 a, n
- rm *.o
% U, ?, d, E* p! z - 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
. q4 p! {7 O; t* w6 K6 B1 i
, f& z1 `0 h: m4 u5 T0 `7 I
! \1 ~- K9 A! w4 k0 p! @
图4.2.1 添加调试配置文件' e0 R+ q! k' ?9 ?3 X2 a
- {
# r T( @. h! z1 m9 u+ A* Q/ V - "configurations": [
/ c( f6 R; ?- p$ ] - {5 X" Q0 q+ B1 D% R! X$ v2 V
- "name": "C/C++: gcc build and debug active file",* G1 C- n9 M6 s% f- b) W o/ t$ P3 D
- "type": "cppdbg",/ E( N( H1 m" ]
- "request": "launch",
- K, @; W6 @& m$ ?8 U, n- X - "program": "${fileDirname}/${fileBasenameNoExtension}",
* z' o7 g- G8 j) o/ e- O# T - "args": [],
6 S- v) N+ a" f2 ?9 |; F. f" d - "stopAtEntry": false,- W2 V5 o5 ]* O& F3 w: [ L
- "cwd": "${fileDirname}",4 d& X' X' o5 |
- "environment": [],
+ U8 C: M$ q$ V) q* A1 z9 o' t - "externalConsole": false,3 e; P9 Q; P* q& {$ V0 u3 E
- "MIMode": "gdb", p) Z5 i3 k' w+ T* ~
- "setupCommands": [! d1 M4 G, d9 `: L
- {
) P" y( |) s! R) k) ]8 i- O - "description": "Enable pretty-printing for gdb",( z5 W6 G" E: ?4 O
- "text": "-enable-pretty-printing",6 a3 P9 y2 f5 M8 H3 j3 D0 O4 N$ s
- "ignoreFailures": true
3 ` H0 Y+ h& U2 i9 N! | - },% i; V3 \# A1 e- ?! v& ^; E
- {
" I7 d" H) g7 z% C" }' ~& B. G - "description": "Set Disassembly Flavor to Intel",
$ U3 F U3 Z O) N& a W" o' z - "text": "-gdb-set disassembly-flavor intel",
& J# G% [, S( D' B - "ignoreFailures": true$ \, M$ u2 I3 V% l; W: G2 v
- }6 h# g) U( R. L0 l, v* k: |
- ],
8 Z0 t7 z- K, ` - "preLaunchTask": "C/C++: gcc build active file",! h1 t7 l, [% E; y- }2 B
- "miDebuggerPath": "/usr/bin/gdb"
6 D; E! r( |8 R - }% O& Q/ f0 E# |3 A3 @
- ],
% Q$ D/ e5 R9 t; g, ?1 }( E - "version": "2.0.0"
# A9 B* ~# G. A; T$ i/ O - }
复制代码
, Q: z1 ]* K0 I$ v. M; N: j修改生成的launch.json文件如下- q( B1 C) A! f6 V' D
- {
' P0 E9 z' a) W7 n5 k3 O - "configurations": [
2 k' t8 J% }5 Y8 ^" P! f+ X - {
C( ?5 b) \, H0 H" ~( C7 e6 X; J - "name": "C/C++: g++ build and debug active file",! b) A- ?" K/ N Z/ s* @% ^
- "type": "cppdbg",
" k/ t8 n) A4 V3 s, z1 o0 }; j - "request": "launch",) x: D" I' ~& `/ a- x4 ]4 `" z
- "program": "${workspaceRoot}/a.out",
7 q& ?. }* r C' O7 b5 r1 y - "args": [ "1232"],
5 f$ L4 p4 y: y" z- l& ] - "stopAtEntry": false,! ]- O/ l$ L" H( i: ^
- "cwd": "${workspaceRoot}",
- A# S9 Q( k& L1 t2 G - "environment": [],
' G/ a+ D" n; p% G4 S; R - "externalConsole": false,
7 n3 [) |- _2 D T0 a$ A( d3 ^ - "MIMode": "gdb",
4 x; R# L3 x, h - "setupCommands": [
3 Y; c+ z, [0 V2 x7 f8 u; M - {
/ O5 s# a: K7 k, J z8 N - "description": "Enable pretty-printing for gdb",
0 f& w F9 s' f( j - "text": "-enable-pretty-printing",
. p+ h1 A x* n! Z5 M- w - "ignoreFailures": true e7 O$ ^/ I; a# U4 w1 q( { x
- },
3 z# e8 U9 @' c0 Y, d6 K- @ - {* G6 J5 K- ]6 `) b1 R4 @- N# W
- "description": "Set Disassembly Flavor to Intel",% B1 X4 |6 i. J% l; _
- "text": "-gdb-set disassembly-flavor intel",
$ A" F) j* R0 g3 u2 h* \4 ^9 Q" q - "ignoreFailures": true- F4 @: Y9 v) ?2 X5 a
- }- [5 X' X' N: K, E% |& d9 P3 t9 ~
- ],5 [. T% N8 y0 M( |
- "preLaunchTask": "Build",0 u% g, e( E! R9 h; Z i
- "miDebuggerPath": "/usr/bin/gdb"
1 r+ W/ W$ d$ e9 I; G8 o - }
' `* G+ j' _" ] - ],
% g; q' J3 J( _* t0 V$ x2 `2 K - "version": "2.0.0"
# d1 [! a. X1 M2 U+ j) S; ` - }
复制代码 : 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- {. r# I/ G9 r+ j' O
- "configurations": [
' j" P) N! s& g# c+ K* t - {( D0 N, I& e/ g7 x, n
- "name": "Linux",
( @ _! v- B4 B4 G5 _ - "includePath": ["${workspaceFolder}/**"],
. e4 t8 I2 _# P" d& ^4 i: C3 b* E7 Q - "defines": [],
( G0 @0 r5 U. e) u8 j - "compilerPath": "/usr/bin/gcc",: b: |# R9 b, e$ d
- "cStandard": "c11",1 d5 B5 N- {+ t2 i( W' a
- "cppStandard": "c++17",
: q% D1 a0 c1 l5 R- m( Z' h - "intelliSenseMode": "clang-x64"+ E0 |; J$ [$ K1 E
- }; ^4 r, u, ^6 j3 T( M
- ],7 t1 w6 X& ?& n/ Q/ Y$ I* ~* T
- "version": 4
. P) O$ N w# Q - }
复制代码
+ W# a, z5 e( {0 Y05安装Vim
# M0 D* |7 O% O' G" s9 I' p1、命令行安装, T8 _, |% [2 Y5 c3 x
- chen@ubuntu:~$ sudo apt-get install vim, t5 X6 J0 Y+ K! O) H* S$ P
- chen@ubuntu:~$ touch a.txt' ^5 `0 k& v4 B% j( C+ @5 }, F& k$ R
- 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
: 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- chen@ubuntu:~$ tar -vxjf gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2
# w7 h5 ^ v! |, o2 E1 F4 S - #对于bz2、gz文件解压
/ m/ g, K9 L1 v' K/ ?6 J8 l* J8 ? - tar -vxjf test1.tar.bz2
: j$ h W: L( e& P: ` - tar -vxzf test2.tar.gz( m* o1 C0 K; L/ w
- #对于bz2、gz文件压缩" Z0 }( x5 Y+ ?$ X! O/ M* b% b4 b
- tar -vxjf test1.tar.bz23 f2 v; R: k1 f- m
- 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# ^- chen@ubuntu:~$ sudo gedit .bashrc# `! M' n4 w, o, B! c' q& h. k+ Q, X
- #在文末添加下面一行
7 K$ H7 d' W; c L$ ]* l; F - export PATH=$PATH:~/gcc-arm-none-eabi-10.3-2021.10/bin
\3 [9 ]9 x/ [: B5 ]. e - #添加上述行
6 D" A: l8 h8 R - chen@ubuntu:~$ source ~/.bashrc6 G. A9 s6 n0 s* ]* W
- chen@ubuntu:~$ arm-none-eabi-gcc --help
7 @( x+ A6 p6 K - 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
! _# x' A# _, m7 D
, J5 S( j4 ~* |7 L0 H5 P2、安装- chen@ubuntu:~/Downloads$ unzip en.stm32cubemx-lin-v6-11-1.zip R S7 D9 @8 O: P
- chen@ubuntu:~/Downloads$ sudo chmod 777 SetupSTM32CubeMX-6.11.1
{7 t6 a. R! z- ^# b+ [ - chen@ubuntu:~/Downloads$ ls -l: y& o/ H2 }; n1 b
- -rwxrwxrwx 1 chen chen 539136342 Apr 11 09:41 SetupSTM32CubeMX-6.11.1/ j5 h( R2 I9 p% g0 y
- chen@ubuntu:~/Downloads$
4 g/ e2 Z1 y7 j1 O. s! T# W - chen@ubuntu:~/Downloads$ sudo ./SetupSTM32CubeMX-6.11.1
5 r, s/ t( U: r6 f6 a - chen@ubuntu:~/Downloads$ find /usr -name STM32CubeMX- V# I a: [2 Y3 z$ Q( Q) F- W0 c
- chen@ubuntu:/usr/local/STMicroelectronics/STM32Cube/STM32CubeMX$ ./STM32CubeMX
复制代码
0 U! r; V, ~$ Y; L! L3 D7 }3、增加图标到菜单: Z; ^, K: w7 o+ t% ]! C5 [- [
- chen@ubuntu:~$ /usr/share/applications/
/ n& ^4 K, m; B2 _3 i$ K8 g( ]3 v - chen@ubuntu:/usr/share/applications$ sudo touch stm32cubemx.desktop9 k9 ~4 y f2 u& j
- chen@ubuntu:/usr/share/applications$ sudo gedit stm32cubemx.desktop; N- H1 w: E$ _: K4 h9 {
- chen@ubuntu:/usr/share/applications$ cat stm32cubemx.desktop
! i; ]: B1 A& {- s- p - [Desktop Entry]5 x$ C% b7 F$ t1 N) I
- Type=Application( @5 f0 `# h- C7 i
- Name=STM32CubeMX
4 A& h* ^+ Z D0 g+ M9 `% A# m - Comment=STM32 Development Environment
- c5 H/ F5 p" P& D- o - Encoding=UTF-86 B. f/ G X! J! y5 r
- StartupNotify=true
! J" N7 J. [1 t/ y1 D# ` - Terminal=false# B5 T6 ]" L) f! K; |0 V; g
- Categories=Development;java;c;c++;
& B# Z. N% f5 L: J' Z7 [ - #icon= ##########图标的路径,自己找一个就行
9 e3 P6 B, l, \3 h* J: N - Exec= /usr/local/STMicroelectronics/STM32Cube/STM32CubeMX/STM32CubeMX# V4 d$ N" e- [
- 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
: 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
) 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
: K# G; u4 R. q( w7 I; c
- {
( L. P; U a6 _% N' o* n: B - "configurations": [4 G& M( U& n% D. q3 n
- {3 c) [+ }3 x; m2 M
- "name": "Linux",
# M' n! z2 r$ J4 B( H - "includePath": [
8 V/ B) @+ @& j8 B& | - "${workspaceFolder}/**"
% W7 p, K9 c) q; H" U/ v - ],1 P- o7 Z* I7 T# y0 A+ c" V5 Y, M
- "defines": [- m1 g5 y! p% |8 y
- "USE_HAL_DRIVER",/ E7 n6 D8 G: E: _' `
- "STM32F103xB"
$ C5 N1 c- u& b9 j7 U - ],( K& U; ?9 @3 b
- "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 - "cStandard": "c99",
4 Q5 z% d. U2 r, A( E3 S - "cppStandard": "gnu++14",
4 b0 J- B) n% W6 } - "intelliSenseMode": "gcc-arm"
* m3 q7 [* e S - }1 N) l$ o( M/ y0 h
- ]," l/ J& [3 n/ Y8 d! V- Y1 u/ {, K7 e
- "version": 4
6 s5 o' B# V4 e4 X* k - }
复制代码
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 {
$ i$ c1 }3 m1 p R: c$ J# D' `/ H
7 m. T6 e! e5 ^" n2 X
( }) r1 |9 o+ L7 M6 A
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$ `
& ]" ]' |% 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
- {
3 u$ `. \7 c% }" B' U/ [ - // See https://go.microsoft.com/fwlink/?LinkId=7335580 M9 d! J+ I: w& `9 V7 ^. I
- // for the documentation about the tasks.json format0 P1 C5 Z1 a/ R \$ i/ W
- "version": "2.0.0",; D2 }# v! g9 A! e- S! K/ h
- "tasks": [; i9 I/ d" T: a2 c2 e5 z
- {& Y% B/ h5 X! S! G8 C" `
- "label": "Build", // 标签名7 A- p- X- H7 J9 H- Y/ C8 K
- "type": "shell", // 终端(shell)型的任务
2 g* }% K& b4 O0 ~ - "command": "make", // *执行的命令
, I. ^) {9 f: k. Q - "args": [ // 命令的参数
; J1 E$ G7 x; f6 T. d, y" ] - "-j6" // 表示六线程同时编译
7 V$ U9 d7 Q" [; |, F7 [) D& k - ],
$ J( e2 b V+ `5 V - "problemMatcher": ["$gcc"], // 表示如果出现了编译报错,问题面板会显示 gcc类型的错误
. [1 q0 J5 w; v# a6 k) ^% A, U1 b - "group": {
2 f: g/ P* s$ {6 X - "kind": "build",
) j, B% c% D# V - "isDefault": true6 m* I0 {2 }0 g X+ \
- }& q$ ?" J$ ^ P1 h
- }
8 a; t* k" \ k* j" t - 1 d( K1 L% s0 w- }+ ]
- }
复制代码 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
/ 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- chen@ubuntu:~/prj_vscode/linux-stm32/test$ make clean
, l0 p! b/ _" g( g# d - chen@ubuntu:~/prj_vscode/linux-stm32/test$ make
复制代码
% B0 A) X0 `* s10调试( k: ]/ ?3 p. \4 N
1、安装openocd- chen@ubuntu:~$ sudo apt-get install openocd + B! Z( R+ G4 Y4 ]
- chen@ubuntu:~$ whereis openocd2 m+ b# [, ]3 ^. a* U9 b5 `; \
- openocd: 3 E# F# D2 p U. @% B4 ^6 N
- /usr/bin/openocd
6 j3 H5 d& F9 G$ c! q' e - /usr/share/openocd
1 P! I6 b4 p* W# a% z0 ? - /usr/share/man/man1/openocd.1.gz ! O5 l- S% d# b/ p! b8 E
- /usr/share/info/openocd.info-2.gz
7 b: I2 C% d6 a. O. s) x - /usr/share/info/openocd.info.gz
: y/ p9 x" s& }( F - /usr/share/info/openocd.info-1.gz+ f( [+ w1 J! Z# n! H, y" h5 J
- chen@ubuntu:~$
' F |/ T# r3 i0 M4 }/ w - //连接好stlinkv2-1到电脑和pcb板,输入以下命令。完成连接 a( ]6 Z* A. n! O g/ Z
- 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- {3 V" |4 j/ k% ^+ W' M! u( E" m& \
- // See https://go.microsoft.com/fwlink/?LinkId=7335585 J% a! s) o; w; \3 }9 A: D
- // for the documentation about the tasks.json format
: ?: d5 U4 q4 L! U6 p$ b# T/ D - "version": "2.0.0",. S6 @+ a( n/ U2 L) h" g
- "tasks": [
5 H0 W( _$ f* x# E: B - {3 e5 `9 x1 C8 v4 g
- "label": "Build", // 标签名
: o- b8 \5 Z( s. X2 v: T4 h2 U - "type": "shell", // 终端(shell)型的任务
" ^; [% w( s7 v9 y6 o4 { - "command": "make", // *执行的命令- W) A4 x& Q' i5 R/ p
- "args": [ // 命令的参数1 W5 D- w1 ~/ Y- N# j* ?
- // "-j6"表示六线程同时编译0 }! d* X! p5 i8 o5 c9 N4 i: v8 E
- ],6 b1 W1 Q( \" n; {3 c
- //"problemMatcher": ["$gcc"], // 表示如果出现了编译报错,问题面板会显示 gcc类型的错误# V; t. I+ N+ n: z n
- "group": {
8 {# O& j; t* S( }) T% o& X - "kind": "build"," E% K, W5 p: i& S' U% u+ W
- "isDefault": true
' b( i% v& F' S - }
: D/ L6 b. Q, `: y$ a9 C - },9 U( s, |; u8 J7 t6 w/ v: ]. G
- {) C$ k0 y2 N8 U4 K9 y3 q! D6 B) I
- "dependsOn":"Build", // 任务依赖6 u: ^( Q H( ^& p" O: q
- "label": "Build and Download",
# S! g/ t) t% f3 h/ K- K8 I4 @ - "type": "shell",+ [+ b$ s7 t. L e
- "command": "openocd",
K' ?3 O/ G5 Q, V M3 @2 O4 g - "args": [9 ]+ P0 ?8 @. S
- "-f",0 [' Q& u6 ?* ~" r" h. b2 |
- "/usr/share/openocd/scripts/interface/stlink-v2.cfg",; g1 U) c% f/ X6 j+ e, Y
- "-f",& u% O8 X" ^0 M- C' I7 M6 {/ T. [
- "/usr/share/openocd/scripts/target/stm32f1x_stlink.cfg",
/ A( m$ p9 u( v/ C& u9 [ u - "-c",& D! Z) s/ i' R$ i- W( P- X
- "program ./build/${workspaceFolderBasename}.elf verify reset exit"
( F" D) d! {/ |& q- z$ R( U - ],( m6 p9 T) F) d: B2 i
- "problemMatcher": []
% ~2 B" Z2 e# W: `% O - }
/ M8 N$ q) {' h* i" v -
- b5 a7 b) @1 h j) j/ n
8 C8 y0 _/ Q7 n1 |0 t- }
复制代码
! 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 |
! C* d) ]% j* X& @
5 D4 d4 f z2 {6 C# j: R% ? F
$ 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
5 J: Y+ J% H% B0 W! ]" G, a m. ?
" B! |& p) a* m
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
+ F4 M+ F6 r( O7 \, l$ I" e8 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
- Z9 O( `! z) i1 P& U2 @6 [
! T' ^( Z) Y' p. B. B5 P$ m- {
# G: b/ W+ F! P/ b, M - // 使用 IntelliSense 了解相关属性。
% S- M2 C3 X0 D, Q+ K# m - // 悬停以查看现有属性的描述。+ J6 \- O! c: R) k2 C0 [4 `
- // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
# A, I% R/ |8 p0 A! G- F& j - "version": "0.2.0",! n( L7 C) m# \' F' ^7 @+ g
- "configurations": [
/ B$ E1 |2 `5 b$ H5 S. Z - {
" e/ g2 W$ K, m0 h3 Y$ |4 z - "name": "Cortex Debug",
/ V |$ z; q! v; r7 T) I - "cwd": "${workspaceRoot}", // 输出路径
$ R& Z. T' H9 O2 t4 X, W" a - "executable": "./build/${workspaceRootFolderName}.elf", // 要调试的程序
3 q4 Y) i' r; {1 b. o+ b5 ? - "request": "launch",
" C( O5 m; Q o; v7 i - "type": "cortex-debug",& G K+ b7 M8 z# d% M: ]
- "servertype": "openocd", // 调试器选择 F @+ a, f% p
- "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 - //"svdFile": "./STM32L15xC.svd", // svd文件,有这个文件才能查看寄存器的值,每个单片机都不同。可以在以下地址找到 https://github.com/posborne/cmsis-svd下载
n \6 }( V9 ?# v" Y, r - "interface": "swd",. s, O" |3 T8 ^
- "configFiles": [
2 {$ ?# _4 X0 f - //"${workspaceRoot}/openocd.cfg", // *可以通过加载该文件来配置4 s1 p1 g4 g/ K8 t7 _3 y) a; W
- "/usr/share/openocd/scripts/interface/stlink-v2.cfg",# M$ b' V/ x) Z6 ]$ s
- "/usr/share/openocd/scripts/target/stm32f1x.cfg"
1 L. J0 D- a" O4 m* u+ D6 v1 a - ],
: E' D- w+ K; |% } - "runToEntryPoint": "false",+ X# O3 [" Z4 b S' N& F; l
- //"runToMain": false,
- j9 N2 p# }% q# K- U) ] - "preLaunchTask": "Build", // 在调试前预先执行的任务,此处是tasks.json中的
" d' B0 m0 y9 v/ C* d& h - //"armToolchainPath": "C:\\Program Files (x86)\\GNU Tools ARM Embedded\\5.4 2016q3\\bin" // 如果没有把 arm工具链路径添加到系统环境变量,则需要这条指令添加你的路径5 Y( ^9 q# ]1 k u- N$ M
- }
: o; w4 u5 s+ l0 |6 h8 h7 ~" x - ! s: C3 y' m( |6 O
- }
复制代码 # 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
( 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
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- chen@ubuntu:~$ lsusb
: @8 Q9 y. l( f - Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub! L1 ?6 \& n% q F' l8 L
- Bus 002 Device 007: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
" s* Z5 G! f9 Q3 I5 A& V! } - Bus 002 Device 006: ID 0483:3748 STMicroelectronics ST-LINK/V2
. H' v) A5 v' l6 w5 h+ `% v2 i8 r8 E - Bus 002 Device 004: ID 0e0f:0008 VMware, Inc. VMware Virtual USB Mouse5 Q8 A8 Y$ r6 Q1 `& d- D1 b
- Bus 002 Device 003: ID 0e0f:0002 VMware, Inc. Virtual USB Hub
% g) H3 I7 k. v& ~ - Bus 002 Device 002: ID 0e0f:0003 VMware, Inc. Virtual Mouse
" C8 V9 ~" j4 i& j( ` - Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
' b* k& g: a9 K& H( G - chen@ubuntu:~$
复制代码 & R- V# `8 q9 Z1 `1 `; ?* u8 N
2、加权限- 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- ]- //在执行 git add 和 git commit 之前,先执行以下命令查看当前仓库的状态:& C* q r) L$ v
- git status
7 g# Q' t. Y8 [- k% d3 ]/ r* Z1 r - //这将显示工作目录和暂存区的状态。确保没有未追踪的文件,也确保已将需要提交的文件添加到暂存区。
复制代码
0 k. H1 T$ Q" ?) Y& i2、确认 Git 配置:# O6 x. A* \0 m7 e. j
- git config user.name" h5 X; X) p5 o3 t
- git config user.email" W2 X6 |7 Z8 U; B" e5 ]
7 y- x1 z! H& y# d; {! J* s
$ S% T# V" I% D' T! h; ~0 r* A- 如果没有设置,请使用以下命令设置:
; P3 |. T2 S8 D! i ^$ I& Y - git config --global user.name "Your Name"* ^. ~1 t, O- Q
- 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 |