“ 从ubuntu20.04入门到完成stm32程序的调试。” F0 S$ D. `# B# @
* H& s# }0 T. _0 M" y
) U' _7 p1 K$ \' N& M) t# [
4 x3 o! E$ c8 \+ e. v, A4 O1、更换软件源
% D$ C6 V0 j1 F& ?! J$ X8 B源文件(sources.list)" e1 o; C: V- T2 u; Q+ M* s; X% Y
更改源(Ubuntu20.04对应的源)/ J7 u$ s+ A8 _! s3 I4 c
更新源+ c0 r2 w8 u4 m* f+ G+ s. Q6 z' _
+ H4 P+ Y" s1 ]5 ~7 Q* y
2、安装拼音输入法; g& |: L0 g. V* j
Region&Language->Language Support->Installed Languages7 S: o: O" e& k
Input Sources->"+"->chinese->Hanyu Pinyin: k c5 N6 q7 M y5 D3 A
+ ]" K/ Z% Q2 y [3、安装VSCode
5 |+ X9 ^' Q; m下载.deb程序包. k9 z+ e3 u* S9 X& D2 `
使用dpkg安装
1 Y/ Y( H8 t& v调整设置- O+ h/ h4 o( \- |# M9 k* n
启用Title Bar8 q7 j$ o. l4 e) B
安装插件
, J- e: h5 s' b+ V% l' v) G
! H, C2 k3 a2 T4、安装Build Essential及gdb
3 P5 l! p9 ]5 G" G, U" U& f; nbuild essential介绍
/ Z0 q: s7 e, F- n! D+ d3 t0 I8 C8 a" M命令行安装build essential和gdb
7 q5 J% @, I7 h/ x% `) e; u# n1 pLinux C程序开发简介
9 }% f2 p/ s7 }( `9 `) c无makefile
h1 j$ |5 O! R! X, z5 Z7 ~有makefile
- `" H5 @% \9 j4 e生成.vscode文件下的debug配置文件' m' m# F H% r# f
生成task.json文件! k* o2 i5 C8 p
生成launch.json文件
k- k6 l: u. s. h, E0 d; z) a5 l生成c_cpp_properties.json文件
4 | l, E) `1 o
3 L4 L. n0 H' t5、安装Vim! t9 x# B( z7 x
% i8 C% [. y, m: T% j6、安装gcc-arm-none-eabi
7 M9 W+ M/ J: \下载嵌入式gcc编译器
" u/ U( H' U2 z- A0 V$ O解压
9 L; x6 }3 n v" O* T# Q! ?添加gcc-arm-none-eabi下的可执行文件命令到Terminal
. M# H4 D8 _& w ^- Y p" M0 M0 m6 S* x* U$ p
7、安装stm32cubemx
p) v! T1 r- n7 m9 K下载stm32cubemx( b- M5 h5 u, |5 S
安装
* o* D+ x5 ~2 B1 `7 D8 y H增加图标到菜单
5 c9 C j; a! R" i! H9 m
! f- m. }& Y) M& T% o8、使用stm32cubemx生成初始代码! Q' _# G( Q" c
选择芯片->配置->Generate
+ }0 P$ L$ t0 r. x勾选PinOut-Debug-SerialWire
4 y# K7 s- u8 Z, G! O/ K6 ?3 \( X3 _/ A" ]
9、配置VSCode, R7 A8 {9 s5 l" r. t
使用vscode打开上述生成的初始代码
. B' o7 C& d' _生成VSCode配置文件
) d$ f8 B @0 B! ]6 G" h+ r7 I9 ]( c0 S. |c_cpp_properties.json* w- b4 _$ n6 J6 @; A3 F/ g
task.json
* t% ~$ c' |5 }. fsettings.json
# m+ k0 {! L: z. h; `宏定义包含. P. A2 K( C0 Q7 A* M
make编译/使用ctrl+shift+B
& G7 g7 f$ g% e- @* r" w
e$ r* @: h: v$ F9 |10、调试
0 k# `9 w9 e8 |0 y: t安装openocd
7 b' ~# c0 ~& F! ~6 ~; M8 c在tasks.json中使用openocd进行编译后的下载
* A+ `8 M( \! L使用openocd进行调试3 m' w# c; Z5 B& _ B/ l
安装Cortex Debug和Venus's Cortex-Debug插件
3 k, w+ a* J3 C+ @# q L( X添加调试配置
^* Z t8 X4 P e# D3 f8 y选择cortex debug,生成launch.json- M, m9 C8 S0 l8 v
调试% d5 I) z$ _' [1 a1 H' W$ W8 @: s
, W+ a7 f8 K( B* f- j& d
11、使用VSCode串口工具
: D+ d$ {: `0 K/ Q安装serial monitor
8 S1 X. P' _) K) D5 g* Y9 O5 c/ ~查看连接的usb3 ^6 e8 }4 s% S
加权限5 R3 V" V. f3 a0 g% F* ]
! ^, T2 @( j$ }# F% Z. k7 V, K/ O
12、使用git
$ w# u1 x8 l+ t; S01更换源$ i0 u; }: z0 p; f
1、源文件(sources.list)7 \( n; g3 Y. E$ q# a
chen@ubuntu:/etc/apt$ ls -a
( b$ \) ~: W$ c% {9 g- j3 u. apt.conf.d preferences.d sources.list trusted.gpg.d
5 L/ `5 I. E5 e9 Y% `% t; `.. auth.conf.d sources.bak sources.list.d
# ?* V& f/ g6 i& M' y
! O/ f% C/ Y0 ^% X; `3 C2、更改源(Ubuntu20.04对应的源) f8 M0 S, ?. U& y
- chen@ubuntu:/etc/apt$ sudo gedit sources.list
复制代码- #阿里源(Ubuntu20.04)
; `1 F+ _, e) U7 y - deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse8 B4 C/ C/ [" g
- deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
: B |. l5 M5 S1 X - deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse) F, K, n% q8 V5 N' }
- deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse8 \0 G3 p" y7 i9 M5 c
- deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse4 Z9 v. G, H% _# K8 K, G& C) \. }
- deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse! b! Z; g0 C4 b0 S+ i* N: ~6 a
- # deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse9 \' p9 q* R( H$ a
- # deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse+ ~# D2 J3 e5 h' ~' \8 V- W. n; |
- deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
2 J. Q; l; J1 O6 n3 @6 W; C - deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
复制代码- #清华源(Ubuntu20.04)
9 Q5 ^* b) n# R- U3 R" h3 a - # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释. a! M B3 a3 D7 U' h0 ~( t
- deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse. y8 M6 ~& Y' K: Y' S" z
- # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse% B4 l0 R) H1 \- H8 a
- deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
: K! E9 T# o2 H1 P4 l( N3 e2 U - # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse) P9 v! H$ {" R8 a. l7 Q" k2 C
- deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
; J: o/ X0 [4 i' D" }0 Q) n, r6 Z - # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
( B. I! `/ y% Q/ H$ | - deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
7 v6 y" H1 ]. G- G5 [ - # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse+ Y! m; o! M5 o& f9 d. \
7 L- Y( u9 Y- q1 L- h- - T& x% r8 N& @0 G/ m& ]# |
- # 预发布软件源,不建议启用$ e$ Y2 i" k4 B: I
- # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
. U# q* ~% g# k6 D - # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
复制代码- #中科大源(Ubuntu20.04)
5 Q2 \/ c9 }0 o. p, G3 i2 m - deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
h g! p0 k; `6 ? - deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse/ G2 A6 Q- n$ @+ s7 u
- deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse3 U4 h' n; c- s4 z1 F
- deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse$ d: M- ]# f& Z% W: Q$ z/ l o% _
- deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse2 R7 y" g7 |* S; }0 D" H1 ~
- deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse- G0 _2 r/ a! d0 x& W Y/ s
- deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse/ x/ [( O2 E8 s8 i! @" y
- deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse8 {, E8 {1 b2 Q$ I4 S: [: c
- deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse' p) U/ ~& D7 w7 W
- deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
复制代码 ) [% n9 R% s1 R5 g' x/ [+ ~+ f+ Q
3、更新源4 [" Y' R. r; f& F. B' a
0 f7 T, T2 R* o+ r( K; N) n02安装拼音输入法) J+ W1 m2 G0 s7 z# d; ?1 f8 d( d
1、Region&Language->Language Support->Installed Languages! F3 g. P; u! Y- M
" U7 x: J: d6 o" X4 D
8 u+ F% ]6 Y6 j: g# T2 e/ S
9 p* Y- e1 s, u- g6 X" |. Y! W2、Input Sources->"+"->chinese->Hanyu Pinyin- ^2 k e0 _ k5 v) [
- chen@ubuntu:~$ sudo apt-get install ibus-pinyin) Z# M' z5 \# P1 B8 a
- chen@ubuntu:~$ ibus restart
% W% H: s) C" Z - chen@ubuntu:~$ ibus-setup
复制代码 + s, A! T3 v4 i8 f* u+ Q
# x/ R; N& [. V5 H; [3 t& y6 ~: v* y% R* M, e; H& F8 h
( |# L# s! i/ [/ }
/ f/ m6 D2 t# w
, C$ b) n) A6 n
- G( l1 l0 {+ {03安装VSCODE ^$ V& F( i* d! |0 T
1、下载.deb程序包https://code.visualstudio.com/- g! t, B1 w; n3 Q# Z2 @
. A' ?) e* H9 E7 V# K! [2、使用dpkg安装(在Terminal中Ctrl+Shift+C为复制,Ctrl+Shift+V为粘贴)3 @$ p* y/ h$ P) S3 c
- chen@ubuntu:~/Downloads$ sudo dpkg -i code_1.89.1-1715060508_amd64.deb" V3 W1 r$ i n& @
- chen@ubuntu:~/Downloads$ code .
复制代码
; j# g" e8 N3 N0 J3、调整设置
9 H1 I! X! D0 z+ e0 R启用Title Bar:View->Appearance->Editor Actions Position->Tab Bar
( A7 b" P0 y2 x9 w' y1 Z, f; r. ~. g+ _& s) k& M! i; M9 O! `+ z3 D
7 g: S% E5 r6 k9 {- C3 L) {# p J
4、安装插件7 a8 k! w1 ?+ w3 `" A
git安装3 D3 G& q5 R1 [% x
Download Git for Linux(https://git-scm.com/download/linux)3 S8 R9 `: V9 U- u; |) ~
2 V9 M7 L3 L7 W* W6 l- d
) N6 O; g7 b4 H, Q: l- Z+ l
/ r; ^; j3 r. I) X5 \1 \* e0 {8 l
/ {) i- f. C* ^' d( D1 S; D/ k& @& p2 W1 x: q7 ` s/ ~3 c! c% O7 x; R/ p
命令行直接安装(Ctrl+Alt+T呼出Terminal)' n- M, l3 p8 j( u2 F
- chen@ubuntu:~$ sudo apt-get install git
* w3 u3 S7 X0 v4 _" d x - chen@ubuntu:~$ git --version
复制代码 1 }- L1 a; S6 p( [0 q( p; f( H
git设置
4 G& u/ R. r' f8 |6 U
/ J( u/ o- _! T
' u: `4 y6 z0 f* b
3 u& q/ K. T! l( Y6 I7 a
- git clone https://gitee.com/kevinkerry/linux-stm32.git6 q( r% O0 c5 Q, g. x
- git config --global user.name 'kevin.kerry'% a. {3 j0 L O. ~; q
- git config --global user.email 'ct15762287606@outlook.com'2 C9 _* p: A a+ `5 c& r7 ~
- 6 }/ u+ `+ U3 D; M1 F
8 ~9 \: T5 Y; |6 `8 o0 R. Z0 H6 P2 ]- #示例5 }) W8 c4 u! ?! ~+ _
- chen@ubuntu:~/prj_vscode$ git clone https://gitee.com/kevinkerry/linux-stm32.git9 m- H4 H, j- l7 g( b2 p/ u$ _
- Cloning into 'linux-stm32'...+ Y# D( E7 C" W) z$ Z7 I' r
- Username for 'https://gitee.com': ct15762287606@outlook.com, K6 C' M/ e- g* g
- Password for 'https://ct15762287606@outlook.com@gitee.com':5 [; l0 W( J! E, ~5 M
- remote: Enumerating objects: 4, done.) w+ W) p- ?6 f- \1 [6 Q1 }7 ?6 Y
- remote: Counting objects: 100% (4/4), done.! `4 [$ f- x5 T/ [) R" N; |
- remote: Compressing objects: 100% (4/4), done.
/ A" \* b( K. ^ - remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 05 h/ b! |# p3 C( k' h( q' x/ k; Z
- Unpacking objects: 100% (4/4), 1.79 KiB | 1.79 MiB/s, done.
8 D/ v( w0 \ |% J, c/ ^ - chen@ubuntu:~/prj_vscode$ ls0 x% P, x) {5 o
- linux-stm32
复制代码
8 K2 p- Q2 a4 A8 P, B. EGitLens — Git supercharged插件安装 L, `" a+ h8 r' T J. l& a
# B! x9 l1 T$ c. O
+ _* H+ K# Y; c+ m4 B3 U n4 g8 `4 {% J! X
Git History( o& H8 C1 F7 c; |! d
) F0 B/ O3 r5 m/ l
, z& A3 P' ?1 \* N: N v# W( M4 u' f7 ?* X0 |2 m2 T J
Bracket Pair Color DLW
. g2 d8 ?. n; b# M7 e- R, S
" q& X) t8 z0 F* K+ q' S8 f
9 |" T3 \/ [7 G. f
; X3 g6 x- o7 @Arm Assembly
% E/ [! G& D8 n% v% x( d l5 W+ w9 L- s p7 W
9 `' q" V+ \& L2 O' r0 H3 p
) L: Z: [+ E, `) A- M' XVenu's Cortex-Debug0 ?' P) j# P) q& W. M
0 K/ O3 O1 E U" d. P: s
( h. {4 p( k, K4 X! E! G" W2 {) y3 g* u9 x. \6 P; E0 P' \1 ]
C/C++0 V* A, a6 f1 D3 ~/ ]9 D& A9 A8 o1 I
6 w) v; L' o" n j! y
9 Z" I" u) [. p' g' U
4 e& \4 H: n3 d9 K" M04安装Build Essential及gdb
9 B" t; F- F* h% j! i/ G+ S1、build essential介绍' F1 M7 x3 Z9 H8 U4 v& U
build-essential 指的是编译程序必须的软件包。它本身不是一个软件,而是一个工具集,包含了在Linux开发程序的一些必要的软件包,包括但不限于gcc、g++、make等。1 l/ U' X* Z2 t! r" ~: |7 c
J5 z' H _4 u, H& p6 J# e
查看该软件包的依赖关系,可以看到以下内容:. q9 k* o2 b3 m, K% l5 @- I& L4 }
- chen@ubuntu:~$ apt-cache depends build-essential6 j3 ]& k/ U0 Y* Y' y) l: P; O3 u
- build-essential: j" P- ?/ ? [$ E a
- |Depends: libc6-dev( P5 o* w5 S. K5 b0 r. G# `- h
- Depends: <libc-dev>5 @; x. T0 E3 q5 M; W: \
- libc6-dev
7 g8 Y& f0 e- o( W) } - Depends: gcc0 z2 ?4 v/ Y) I
- Depends: g++
+ k7 I7 f8 ]7 k0 R/ p0 ~ L - Depends: make( i# T. q7 K1 m9 ~6 B. X1 s) c
- make-guile4 O' |$ Y' w1 O7 A' ~0 t
- Depends: dpkg-dev
复制代码 ' }- P: `. l' r1 G2 m9 l- F; _; K+ M: ~1 n
也就是说,安装了该软件包,编译c/c++所需要的软件包也都会被安装。因此如果想在Ubuntu中编译c/c++程序,只需要安装该软件包就可以了。7 J7 @ s/ I4 U( T/ B
* o1 |8 h; ~* e) C& b q2 V
2、命令行安装build essential和gdb" {. t% {- ]; M# a
有时候可能会需要安装gdbserver
8 B; }, | [, c D- h/ d- chen@ubuntu:~$ sudo apt-get install build-essential gdb
复制代码 8 V* k5 J' B& W, F& i; H2 Z
3、Linux C程序开发简介
3 G% ^ v6 H7 y- n1 r' I3.1 无makefile) _* Q( l' e6 r* c4 K3 x$ s9 V3 m: U
- chen@ubuntu:~$ mkdir linux_c
0 d* e" `. \0 _0 x- }/ U7 g- T - chen@ubuntu:~$ cd linux_c1 ^9 E6 [4 C! D9 ]
- chen@ubuntu:~$ touch hello.c
9 g) V+ H. T; b( S( a8 n, T; a - chen@ubuntu:~$ sudo vi hello.c
/ V& R2 u0 I# l8 W - chen@ubuntu:~$ cat hello.c
) q# ?$ n7 N$ `& c - #include <stdio.h>: T: C+ Q7 C' Y d- h
- int main(int argc,char *argv[])
! n# [1 J: p7 ]5 v( J - {* v; V( }; I7 ]2 @% A
- printf("hello world\n");6 A8 [* `/ D9 F0 D+ X* e1 ~
- }$ E3 A5 N- ?- l$ l2 |
- chen@ubuntu:~$ gcc hello.c
5 M! D4 b- }8 D* C - chen@ubuntu:~$ gcc hello.c -o hello
- e; S7 ^) j6 n: n - chen@ubuntu:~$ ls -al, T/ P( p {6 m/ K9 L
- total 52
+ }, W: D" ^9 J. Q! m _1 \ - drwxrwxr-x 2 chen chen 4096 May 16 18:32 .
g+ y7 G. i9 E( U$ d4 s/ u5 } - drwxrwxr-x 4 chen chen 4096 May 16 17:37 ..
/ r8 |! }1 W0 L7 K - -rwxrwxr-x 1 chen chen 16696 May 16 18:29 a.out I N- h7 Y& o7 r
- -rwxrwxr-x 1 chen chen 16696 May 16 18:32 hello! `1 H* g" o u# @; M! I
- -rw-rw-r-- 1 chen chen 82 May 16 18:29 hello.c( U' M P+ l# ~& m8 w" M+ r
- chen@ubuntu:~$ ./a.out
! R2 E* @: r2 O, S - chen@ubuntu:~$ ./hello
复制代码
' Z2 b1 M% a9 ], @" o3.2 有makefile
$ \6 w+ A# ?+ K% W3.2.1 创建程序文件- chen@ubuntu:~/prj_vscode/linux-c$ touch main.c input.c calcu.c input.h calcu.h
复制代码
9 [# k, t8 B# \) y. v0 Mmain.c! E6 S3 ]) ?; x8 b/ r6 {' e4 Z: }; U7 p/ H
- #include <stdio.h>
0 [5 {& a1 H9 `1 l6 }( P5 C( l - #include "input.h"
" Y8 r; ?. I" Z4 ^ - #include "calcu.h"
5 q1 [! O& T# t( l1 w1 a2 K Q - $ h2 s$ ~7 _* R
0 J2 I z; A" _& O. j- int main(int argc, char *argv[])1 d a% y5 I: l. k }$ x) r1 v
- {
3 M' Z7 A2 o+ q1 g6 u6 Z0 g& V# e
6 K3 X, ?/ r5 T( @. h' J# {
' }6 n- u2 ? N" c. G+ t7 p j- int a, b, num;
; j. P0 F( ]( [- L, [2 p' F, z0 @ - ( M( `7 G5 b; e- d# a/ c
- ; }8 _. | T$ |& H+ C
- input_int(&a, &b);* T6 i; p, L( U) S9 ]% D9 y( x
- num = calcu(a, b);
5 F1 Z* ]# E2 N# c0 g7 T - printf("%d + %d = %d\r\n", a, b, num);
]& ?7 Q# c8 g* e - ; {5 Q) W: c8 a0 W4 ^1 ]" k: E
- }
复制代码
2 d# }3 j5 l, einput.h- #ifndef _INPUT_H- O9 K n* n. G" I* U
- #define _INPUT_H+ u6 ?! Q. U8 C9 B+ J* _( M
- 0 g1 g; {! ]6 U
- void input_int(int *a, int *b);
* D, {# V1 e7 _5 G; V
* ]% u4 H. E6 E- m% D- #endif
复制代码
& V: m' l2 c% e6 I+ }input.c) q* Z; ?# a* t; h" c( e: e
- #include <stdio.h>
( d( [) d: e _! [; b/ B1 T - #include "input.h"
+ P7 V3 Z8 l" k& H- r
9 N/ [6 p( _' I& y- " } U, K6 Y% j
- void input_int(int *a, int *b)
* s0 T+ R9 e I: @; t2 _+ @) _5 n - {
" R' p \% K, J% }1 n4 e& \ - printf("input two num:");0 [! x2 k; a2 _( j9 g) ^2 P- _
- scanf("%d %d", a, b);
& h4 V# J6 K5 g+ J; J - printf("\r\n");! q. l2 [: l' |
- }
复制代码
9 j5 h3 J% n7 |' k: `calcu.h- #ifndef _CALCU_H
; | `& J( w1 R$ f' B6 h5 v% h1 r# S - #define _CALCU_H5 z I! c- I: Y. ?9 h! u
) K# Q2 t; M/ o, w- - ~) t* I# B) U/ l
- int calcu(int a, int b);! q3 l8 Y1 y6 j6 R3 M
- #endif
复制代码 $ r) e( x$ y3 O% e( a. t
calcu.c
+ m( H7 B, E' |, k- #include "calcu.h"$ h# l$ y6 c1 o5 t2 |6 A3 S; A0 |! J
- , _' f f# V+ A
- 6 s! @, d( g6 f' b
- int calcu(int a,int b)7 _4 S+ R6 [1 e
- {) B0 M# w# z1 a2 B! }% W, G, k
- return (a+b);
: v# E# G( g7 G. c! ?( Y - }
复制代码
' E! A6 r( \$ h' @5 o7 ?3.2.2 编译测试一下( ~, ?9 c9 `3 v# s8 n
- chen@ubuntu:~/prj_vscode/linux-c$ gcc main.c input.c calcu.c -o main
0 Q( O# }. Z/ |; g! _' g& v - chen@ubuntu:~/prj_vscode/linux-c$ ./main) f# s- X9 ^5 w) X
- input two num:23 56
$ n. Y* l8 p" j+ G, [
& F2 i" t/ E" ~' B. u [: I- ) v! l/ U5 j; E( ]9 N
- 23 + 56 = 79) I. y1 `' |- u. {
- chen@ubuntu:~/prj_vscode/linux-c$
复制代码- chen@ubuntu:~/prj_vscode/linux-c$ touch Makefile; p' K W" d! {! h
- chen@ubuntu:~/prj_vscode/linux-c$ cat Makefile
% s4 }9 e/ B. P8 }5 } - main: main.o input.o calcu.o7 E; h. I0 y4 E5 q
- gcc -o main main.o input.o calcu.o6 X! S4 J5 T: t& v5 C4 ?
- main.o: main.c# u# I9 y# ?% o: d) z# H) b
- gcc -c main.c
2 R. e, \+ [9 ` - input.o: input.c
* O) B2 [$ n4 F. F - gcc -c input.c4 i) Y1 P: N( g) P0 C
- calcu.o: calcu.c
& ?$ x5 n3 {% w. M+ s6 L3 @& W - gcc -c calcu.c
' J1 x5 ]; K" r- `( x
, n0 W& ^# I+ q; U0 S; V- * y9 J7 I4 i: j" H- G
- clean:. c- }; @& s* g) R1 I
- rm *.o
6 l/ ^# E6 A4 T W- N9 y& G - rm main
$ h/ g) K+ t; f - chen@ubuntu:~/prj_vscode/linux-c$ make clean4 X8 V4 J2 V' E
- chen@ubuntu:~/prj_vscode/linux-c$ make
复制代码 % { C2 o. D7 w; H3 N
4、生成.vscode文件下的debug配置文件7 d& { |% |, X) s$ q
tasks.json:该文件主要用于配置编译的选项,如果使用makefile或者cmake进行编译,则用该文件调研make或者cmake命令进行配置。/ j1 x% o/ ?, u e8 C, R- Y" @5 }
launch.json:该文件主要对debugger进行设置
( R: h! f% t, X. t& i; Fc_cpp_properties.json:编译器路径和intellisense设置2 U/ ~8 w( _1 Z) M& R/ J
m1 ?- z0 q6 b* ?+ M
4.1、生成task.json文件" P; |3 T, B9 S
% [) F" D7 E: q: j; x
" S8 y5 U/ y" V' n# @
图4.1.1 Run C/C++File
$ j& @8 e6 S l! j, X- Q8 S H" [9 Y" w$ F
2 R4 v, i f: x# v
+ @" P& i7 }1 X; a( f8 s5 z5 U修改生成的task.json文件如下% `7 S. I$ d4 m. Z1 I
- { i% W/ P" t6 W5 i5 m
- "tasks": [
6 R5 O+ ]3 p' P- `2 {' m3 A - {
$ r7 {" { K c" |+ u - "type": "cppbuild",2 W/ c/ R8 R$ G6 N
- "label": "C/C++: gcc build active file",
, m" l L( W( \9 M - "command": "/usr/bin/gcc",1 f& _8 s5 b* b/ K U! A4 t
- "args": [- t5 N# P- f9 x+ W8 |0 ?4 T
- "-fdiagnostics-color=always",
7 l- t; m: P4 j1 m S$ ] - "-g", ~' n7 Z% J4 [
- "${file}",
7 V k! {( e( T - "-o",1 ^! v" x# A, p( V4 B
- "${fileDirname}/${fileBasenameNoExtension}"
/ y. t3 Y2 }) U$ y6 g7 | - ],
% e2 \! O- L V - "options": {4 m( o; h9 K* W' R D4 Y; n8 H
- "cwd": "${fileDirname}"3 k) Y& {* t* n: B8 O9 w2 f/ u
- },) J+ E0 O! h, h9 F- k( J
- "problemMatcher": [7 p) Q8 i; h+ U Q$ a$ j
- "$gcc"* n2 J4 }4 h& E% o+ J5 \. W
- ],
0 F# v, y: T! H - "group": {
$ Q+ p7 ?+ \& y2 a' D; ^* ?4 b - "kind": "build",
& k; o% z' m! N+ y - "isDefault": true
9 V4 y$ w, ~+ Q2 N- k3 j4 { - },+ L( a' x0 F' Q$ v- v* m& v
- "detail": "Task generated by Debugger."
9 e1 L) |7 F# U7 X1 G" t - }
N9 B/ k/ y, i5 Y - ],
7 ?; ~9 G s5 N j - "version": "2.0.0"# s e' ~3 E: P4 n s2 Z
- }
复制代码 ' a7 J: s: S( k/ o9 R
修改如下- {9 s/ R' P Y- j1 ^
- "version": "2.0.0",, e0 r; ^4 S0 I7 n; ^$ L' k
- "tasks": [
$ Y( o$ o# y1 e& g) [ - {. [+ I/ N+ \) t! S
- "type": "shell",
/ G' D" _' ^. L: n1 f" N3 D+ j - "label": "C/C++: g++ build active file",& E$ K9 i" Z x) R
- "command": "/usr/bin/g++",/ ~$ D- b v6 E z; B
- "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],0 F; S7 q$ s V! v5 `+ p) G5 r
- "options": {
; |. M( w1 {" X5 o: `& w - "cwd": "/usr/bin"; h, ^+ R" V/ ^# D0 @5 Z
- },- W( d J2 q1 E2 E! n
- "problemMatcher": ["$gcc"],
! ] O$ U4 A- s1 ]! ]+ x, u' | - "group": {
- d; k& [; b- ~/ N8 x - "kind": "build",$ E" i5 T9 J \/ G# I8 `
- "isDefault": true
! c; ^7 a; K, \2 S. W. ?& U" X - },7 o b" R. u; ^2 F7 u' s3 S
- "detail": "Task generated by Debugger."
% n% L" a+ ~- @* M) F7 g - }
8 u2 Z5 Z3 s1 {, p* R7 g - ]
9 m0 S) F0 M" ~5 C0 Y* [ - }
复制代码 7 T* P1 P+ Y1 `2 e W R4 \0 v
command 代表使用什么程序去执行文件,这里使用的是g++. args表示的是传递给g++的参数,表明使用了什么文件(-g 后面的file文件所指内容),生成了什么文件(-o 后面文件所指的内容)。所以这command 和 args两行参数表达的意思是:使用g++编译器将${file}文件进行编译,生成${fileDirname}/${fileBasenameNoExtension}文件。7 p6 a. m8 Y6 J2 C$ I7 ~" h$ R y
; V$ d4 o5 P' ~6 i7 ?8 r; E# Q; l8 u }上面部分是对单个c++文件或者多个c++文件进行编译。但是如果一个项目使用makefile进行编译,则如何使用vscode 进行配置呢?我们这里进行讲解。由于我们使用make进行编译,所以我们将command命令后面加上make,args后面添加相应的参数。比如我们需要进行debug 所以要控制makefile 中所有的gcc编译命令后添加-g。不然的话,就会出现,编译成功了。但是debug的时候,断点无效,所有的执行都是一下子执行完的情况。由于在我们的makefile 中通过TAG = dbg来控制加不加编译选项-g,如图3所示。所以这里我们的参数添加了"TAG=dbg",这里根据不同的makefile进行不同的配置。3 Y! @& w o* C. O5 O
- {" q+ W; P1 s& F/ ~! }$ b/ J) \
- "version": "2.0.0",
( l6 k, |* ?' O+ H' R - "tasks": [) w: U( P4 z+ I* U4 T! V
- {( p( L0 k* P! B8 e% _: W2 d
- "type": "shell",
0 F1 z& M8 t, q* Q- L$ o4 G4 e - "label": "Build",
& t5 Y- l2 r. B( ? - "command": "make",1 u3 T5 M9 y- ? E3 x- A
- "args": ["NTHREADS=1", "TAG=dbg"],) C. E. J7 r' P( ?) s2 ]% T! W
- 2 q! w' }& k) G: r3 i1 I5 V
- + @9 e! E# p3 `7 A8 s
- "group": {7 ?1 Z. S( M& E2 \. ?1 d
- "kind": "build",
2 K) X: w5 N" K - "isDefault": true
) b; k' l+ A7 K/ D* n - },
! ^0 _! M( Q6 Z! O! O& I" i C - // "detail": "compiler: /usr/bin/g++"
" I9 L. X7 W* @/ L7 j9 e - }
" f) N- v: S' ~; U+ j - 4 t% z# t. R2 X- N; z; {
- }
复制代码
. V. H+ s+ `* W& l一种可行的Makefile文件内容(gcc后添加-g命令)) Q, q2 s8 m0 b0 J+ g/ G8 w7 q. k! n
- main: main.o input.o calcu.o
" f4 h$ {9 J8 n" d5 }; Y& S5 m; ` - gcc -o main1 main.o input.o calcu.o
' J1 W7 T A3 V, p, T3 n0 | - main.o: main.c8 G: s4 h* V6 |+ {5 e
- gcc -c -g main.c
& c3 H: u' V5 w& \: |# E# p# H - input.o: input.c
& R/ G/ K' G5 p, ~, a: g! v/ j- C/ q* H - gcc -c -g input.c1 l/ e% P( @0 g$ L
- calcu.o: calcu.c
/ I/ V" _! g2 x1 l1 \; |0 J - gcc -c -g calcu.c4 U8 V% f1 y# O3 P0 G
- 7 q. b7 p9 R# F1 u
- 0 q4 l0 s q$ }5 P
- clean:
- D2 s& C$ r2 m - rm *.o
3 P D4 q8 Q0 }/ F4 ] - rm main
复制代码
{+ o; e* h& k! t: P A, A4 j8 y0 e; e' s4.2、生成launch.json文件
' ~, N3 j$ x( Y0 ~0 W* n(注意只有在.c文件中才会有下图中右上角的图标显示)
& }. n7 | e+ |0 ]( w2 o9 p
5 S3 x3 u. E6 r8 |( U, W
3 }5 y/ @0 |" {# ^$ z, b
2 e& h0 ~- k0 h# W/ q
0 V9 W0 J: r3 o2 C! D7 g" r; D图4.2.1 添加调试配置文件, o9 ~6 k! j6 |! ?7 z0 j1 r: \
- {: ]2 _: G5 l) @
- "configurations": [! h; O) Q, `8 z' v7 t+ b( K2 g/ r& B
- {. G i3 [( U9 r4 y, d" t' D. K4 M
- "name": "C/C++: gcc build and debug active file",, [% X. q5 y1 M5 U0 @- i U$ ]( a
- "type": "cppdbg",. o/ i7 K9 `& D5 p8 |/ n O
- "request": "launch",/ N' s1 ?; ?3 r- u; K
- "program": "${fileDirname}/${fileBasenameNoExtension}",+ S; }3 p9 T' h" ~
- "args": [],
2 ^4 x) A, c4 z7 o - "stopAtEntry": false,
* s. ~! r/ p& {3 O: ]0 B2 ^" \ - "cwd": "${fileDirname}",+ @5 `! T+ G6 X5 P3 z$ ?- ^) \
- "environment": [],
0 I$ w q; {( X& L6 p - "externalConsole": false,
6 [2 l6 D# T7 W2 X! x& R - "MIMode": "gdb",
5 Q5 w1 i7 Y, r: m& Y - "setupCommands": [2 V4 }7 K. f/ n
- {
! r2 s- _0 H8 M# R6 n1 D1 w - "description": "Enable pretty-printing for gdb",
% l, l M$ O3 j% O) Y G' d - "text": "-enable-pretty-printing",- A7 m1 S+ l: x d) z: h# U
- "ignoreFailures": true! Y, r# _7 _" i. D# f4 @# r' c& o
- },
5 l* G- }* q" E- o9 `! C - {/ D8 ~% W% D3 ~- O" y8 A: c3 ]
- "description": "Set Disassembly Flavor to Intel",
2 W) V/ g, }/ _; u8 ?( t% b - "text": "-gdb-set disassembly-flavor intel",
R7 j: d8 t) j2 h - "ignoreFailures": true4 D- {4 a2 L" {0 N
- }, v+ ^7 p2 U7 L3 M) z8 T1 r
- ],; X- B l/ S" d2 q: T( n% M
- "preLaunchTask": "C/C++: gcc build active file",% P1 k' ?) r2 H, B; x) k
- "miDebuggerPath": "/usr/bin/gdb"- `5 D& L- y' f1 ^6 W) l
- }2 b6 d- J- h- y' x
- ],
$ f3 L8 @0 q& d) P4 U4 h - "version": "2.0.0"
0 W5 u; j* M$ w$ C$ t1 r0 V - }
复制代码 4 D/ w4 R& T9 }6 z }3 I* g
修改生成的launch.json文件如下
, M: r5 A# w$ Y0 }) w0 J- {
+ v, _; g1 R5 u% A4 k% D: S" \+ f - "configurations": [
+ I5 H# B6 P* h5 U) | - {8 V* O( D% x* g7 L# Y
- "name": "C/C++: g++ build and debug active file",0 f+ t5 ^: ~3 L8 J( k) v
- "type": "cppdbg",! \8 ~3 j8 z# }- m3 ^9 g' g0 L
- "request": "launch",
4 }; `" k& e( v, }/ M5 m6 V - "program": "${workspaceRoot}/a.out",
' z/ i7 b+ ~& x4 E% ?3 E - "args": [ "1232"],
+ ~0 J& A! G9 i: p+ z* a2 r7 t, S - "stopAtEntry": false,
8 _" F8 X e8 S8 \4 V - "cwd": "${workspaceRoot}",
2 O: r9 D) a# i% W$ p" ?9 o. Q" o5 x0 {5 D - "environment": [],4 r5 b; [% s$ O+ l, X8 H- E
- "externalConsole": false,
! v% ~# y3 Q1 {4 ~; P+ e - "MIMode": "gdb",5 Y! i8 B+ \' K' z* A2 E
- "setupCommands": [# ]2 D7 z) _4 J7 Y0 f! T: ~! a
- {
) h7 F: v; k) v6 ^) p, f - "description": "Enable pretty-printing for gdb",3 K! |; d& q y. ^7 _8 \
- "text": "-enable-pretty-printing",. z% ^+ y$ x$ o, B; t% ?' R
- "ignoreFailures": true' W9 R4 {4 g- J0 L4 M% P) P* [2 M* Y
- },: z N$ d0 c+ [
- {' P% L% |# N+ F' J7 O# \, h$ Q
- "description": "Set Disassembly Flavor to Intel",3 W* f$ l; t, ^
- "text": "-gdb-set disassembly-flavor intel",
' l. d" P/ m; B: g) e; B( `- i - "ignoreFailures": true
0 b) h, ^, b3 p! m' N+ Y - }
5 Z. R% A2 R# L d9 g* v - ],
% f7 T6 S) F. g1 |- W - "preLaunchTask": "Build",
& f2 W' i$ |: u3 B( v4 M8 H' J) J& o5 V - "miDebuggerPath": "/usr/bin/gdb"
: M& _" J/ K& k. C, v# X* |& H - }5 O$ k9 @- P9 k% B1 J" t
- ],5 c. x" |' O* l. V6 w, v
- "version": "2.0.0"2 m" B! N1 b3 _" _9 |- x& B9 u
- }
复制代码 ( d' A! I. ~9 F% x/ B% Y( x
"program"选项代表,需要进行调试的程序,所以这里需要修改为对应的文件。"PreLaunchTask" 代表debug流程的前序任务,这里的内容需要和tasks.json中的label相同。"args"可以添加执行程序的过程中,需要添加的参数。在下面的代码中,我们就添加了cacti程序,以及参数。% g$ L- N, R$ {! A0 D7 K
$ q3 p& e$ L' b8 o4.3、生成c_cpp_properties.json文件$ |1 b; Z$ x) H3 N I( Q) G
ctrl+shift+p 搜索C/C++:Edit configurations(UI) 即可生成该文件
( D- Z* O5 r0 |7 A8 F) \. A- {+ [' G' _! `% s/ l2 O8 k/ ^. L& c
- "configurations": [
+ S. \- o- @& Y: m6 K - {
. [7 @$ z% V* m1 A- c - "name": "Linux",
( ]# `8 \. n; y, s, j - "includePath": ["${workspaceFolder}/**"],2 V l: h0 k" d1 G& R& V! d
- "defines": [],
' [# t4 k+ g E: A; M+ ^' F - "compilerPath": "/usr/bin/gcc",) A6 M) y" J8 \0 R
- "cStandard": "c11",
3 F4 L! E9 [* g, T - "cppStandard": "c++17",
9 N `+ c4 u, N i5 e3 |+ [ - "intelliSenseMode": "clang-x64"
" L u8 A# Y. \8 Q9 z - }
; w( z+ `: l3 D0 N( t) z - ],6 \6 I% E; M; U2 z, n! X* d- C
- "version": 4% ~7 a0 k( K9 B* x! |& ~$ c& L7 h
- }
复制代码 ' a6 D6 n6 K! X5 p. {6 U
05安装Vim
9 L; s; X/ P, }7 G# Y) C. e, C. W% e1、命令行安装& e, h* t @* N* U) O. F" G' [
- chen@ubuntu:~$ sudo apt-get install vim3 W+ R9 Q; t$ B
- chen@ubuntu:~$ touch a.txt
& \ L& `% C* ^ - chen@ubuntu:~$ sudo vi a.txt
复制代码 7 H$ w! i8 [3 U% e5 s1 @+ t% L
06安装gcc-arm-none-eabi
7 Z: _$ ~/ W2 M, q1 u1、下载嵌入式gcc编译器
3 o" ^% C5 q2 Ghttps://developer.arm.com/downloads/-/gnu-rm
. I" N V8 L. F# F" u+ Q3 i+ Q6 M8 y
2 B- W$ [5 \/ o0 j: @5 T$ m
( F( r! n q' D' R# l. l& a1 h- {$ j5 [3 D8 I2 R; J
2、解压gcc-arm-none-eabi-10.3-2021.107 ?! w9 }9 a" c5 d
- chen@ubuntu:~$ tar -vxjf gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2
3 _+ ~8 b: U; D1 a! I7 |3 k' l/ ] - #对于bz2、gz文件解压9 `. Q5 R0 f+ q( ^2 V
- tar -vxjf test1.tar.bz2
2 C2 N5 m8 ^& y- D8 G - tar -vxzf test2.tar.gz
" M3 L4 n1 |$ K# k" h0 r - #对于bz2、gz文件压缩
; n2 }; p' \0 m' ?5 \ - tar -vxjf test1.tar.bz26 k" G, j1 Y$ U, {% [5 k9 X6 ~3 X
- tar -vxzf test2.tar.gz
复制代码 . C F3 |# Z/ x9 ~) V" K7 q0 U
3、添加gcc-arm-none-eabi下的可执行文件命令到Terminal(终端)
b2 P3 U' ]- o$ \, {- chen@ubuntu:~$ sudo gedit .bashrc- l) Y: a# k* N' C- n" {7 `- Y
- #在文末添加下面一行' A, H' [: F, E6 o
- export PATH=$PATH:~/gcc-arm-none-eabi-10.3-2021.10/bin
$ B. M0 T7 E1 x( q% o1 J* X - #添加上述行
/ `$ b5 G( z: k( S - chen@ubuntu:~$ source ~/.bashrc2 _3 ~5 Q0 Q! j/ Q, S y5 W
- chen@ubuntu:~$ arm-none-eabi-gcc --help
/ [- n; j! \+ { - chen@ubuntu:~$ arm-none-eabi-gcc -v
复制代码
, G8 O! P! R0 A2 h( r) _! t07安装stm32cubemx' L R: k0 S8 U& e
1、下载stm32cubemx
) ]; x' }& p6 d8 ^" dhttps://www.st.com/zh/development-tools/stm32cubemx.html4 J" v4 _ h3 B9 d6 h
6 v: y l9 n5 M* r! B$ E
0 T% |+ j) g1 E- Q* n2 z2 N: Z
* b( U- P) q9 W0 j6 \# [ t8 A6 L
2、安装- chen@ubuntu:~/Downloads$ unzip en.stm32cubemx-lin-v6-11-1.zip
8 _" s9 p! ?5 L2 ~7 v# G% w - chen@ubuntu:~/Downloads$ sudo chmod 777 SetupSTM32CubeMX-6.11.1& K5 H2 @4 \! c7 R% K: O1 B) {* o
- chen@ubuntu:~/Downloads$ ls -l
2 {8 V1 f/ k" ]) k- B& L - -rwxrwxrwx 1 chen chen 539136342 Apr 11 09:41 SetupSTM32CubeMX-6.11.1
3 P. X. O. ~+ \, v - chen@ubuntu:~/Downloads$
# } c$ W9 B; w - chen@ubuntu:~/Downloads$ sudo ./SetupSTM32CubeMX-6.11.14 Q0 c2 @2 x. u2 g' K
- chen@ubuntu:~/Downloads$ find /usr -name STM32CubeMX
K6 l+ }& \1 o+ \8 k$ _ - chen@ubuntu:/usr/local/STMicroelectronics/STM32Cube/STM32CubeMX$ ./STM32CubeMX
复制代码
% U# a/ {+ q8 w* P% d/ @. X3、增加图标到菜单4 ~3 J. C3 j8 p: L a2 t" k
- chen@ubuntu:~$ /usr/share/applications/ W3 L! l8 a! E' Y7 U) M
- chen@ubuntu:/usr/share/applications$ sudo touch stm32cubemx.desktop
6 z. @. y. [6 Y$ ~5 j- {: G1 t: ^ - chen@ubuntu:/usr/share/applications$ sudo gedit stm32cubemx.desktop
! g, ]9 Q6 |- p - chen@ubuntu:/usr/share/applications$ cat stm32cubemx.desktop
' T( O% x) |5 Q; j+ Y - [Desktop Entry]- D( i. i4 X9 f3 Y) w
- Type=Application ^& N; F) H" d+ Y
- Name=STM32CubeMX
! J# t0 H+ V; x3 ], j - Comment=STM32 Development Environment! v& g! D6 x7 Y, o5 D
- Encoding=UTF-8
. A( Y- ^, `6 N' V - StartupNotify=true5 C! _ y: F# x0 Y; |1 \/ \
- Terminal=false6 }! I% T+ W: s7 b7 K
- Categories=Development;java;c;c++;
2 p m7 J5 W7 v% m& u, j9 E - #icon= ##########图标的路径,自己找一个就行8 |* _5 W; x$ z" Y/ ^
- Exec= /usr/local/STMicroelectronics/STM32Cube/STM32CubeMX/STM32CubeMX
' p% X! f6 r4 n- a - chen@ubuntu:/usr/share/applications$
复制代码 7 L1 P- _3 H) E
08使用stm32cubemx生成初始代码
8 n- o/ Y) a' C1 n1、选择芯片->配置->Generate ( R& S, h+ v: n' ]7 z
1 }- a2 a6 _7 R$ d
2 {5 K7 n3 V) L) A: a
/ k$ y. N! M3 N# A( ]2、一定要记得勾选PinOut-Debug-SerialWire否则后面下载程序时会报错Error: init mode failed (unable to connect to the target) 。这个是巨坑,一定要小心!!!!!!!!!!
) Y) I8 r& m) D+ e3 m3 S" x8 d+ W+ d& C* j
" P0 _% \ F, j `5 j8 B
4 c& m) r& Q8 r8 q$ r5 o3 G% x+ v
09配置VSCode4 E" E* f6 \+ x/ j* p
1、使用vscode打开上述生成的初始代码- h+ P0 w: z0 D+ r9 a1 z
2、生成VSCode配置文件# U3 w9 h/ f- H5 P4 }
2.1 c_cpp_properties.jason. H( c5 Q, X1 |1 F) N
ctrl+shift+p->Edit Configurations(JSON)生成.vscode/c_cpp_properties.json文件
# ?/ ], }% Q/ i
1 o% |7 C) X2 J" r j9 T+ b8 ~3 a# n
) a9 b1 D* e; Y3 {: ^9 o' r
- {
- I# a3 K( L/ c6 U - "configurations": [
$ N6 P$ P2 R3 \" L" x - {2 C# y: u V2 f8 I) ?$ ?6 a
- "name": "Linux",
( t Y+ F) {4 N+ i+ D - "includePath": [( l6 _! B9 `9 s. a/ G9 @0 Y
- "${workspaceFolder}/**"
4 {: A1 `, w/ G - ],! p; h$ V! }! o( c
- "defines": [/ D6 Q5 A: l& X) H+ ?/ r' f
- "USE_HAL_DRIVER",
' {7 _) C4 q- h- Q - "STM32F103xB"
6 ?% g% U0 n9 D3 J! A4 x& Y; k4 | - ],$ I* W! `% Q5 ~
- "compilerPath": "/home/chen/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc",' A. l2 D4 U8 n
- "cStandard": "c99",* M0 b) q* N* [2 G& ~
- "cppStandard": "gnu++14",8 y. r1 f. w0 i1 @# }
- "intelliSenseMode": "gcc-arm"% {% J* X: ~8 Q8 v4 l* N8 [
- }3 r' E' M- @* M0 q. M4 Y
- ],* K0 b! J F" f$ _
- "version": 4
4 ]+ B' u% s4 D - }
复制代码 + E0 t+ r2 D4 Y1 n
2.2 task.json
- r, e N0 F. A( z3 k8 ^修改好task.json即可使用ctrl+shift+B对stm32工程进行编译1 F6 _. F9 R; H* k2 J, v5 K0 H
, f3 P2 c' l$ ]
) f/ C K4 i" O) q8 _+ `) F) J! }/ `% k6 X& ?- U
; o) F Y. c# d- z- q
: S' g0 r0 V' d; x3 [
: h7 u. X$ O9 \* L+ k* l- {; H# z3 v) C+ {2 s) `
: v- I! V% i4 w& N9 g; P O
: V8 J6 A' K; c! _1 c
) d/ A6 G) k7 x& l+ u" O# W
修改如下:
: B& ^5 Z2 P% d' u# ^# j- {- Y N7 _& d# e D: L% ]
- // See https://go.microsoft.com/fwlink/?LinkId=733558
) D( h& x! {( U* J8 y/ E. W3 s. d5 [+ M - // for the documentation about the tasks.json format/ p, F0 y4 B! S! q1 U+ g& O B% k4 B
- "version": "2.0.0",( a$ \* Q2 s; I" H/ |& N
- "tasks": [* O: O4 u6 Y! X$ ]+ V* y+ @, E2 e0 K
- {
2 v! b: j% | p( l - "label": "Build", // 标签名
! T- P v( L {! H2 X - "type": "shell", // 终端(shell)型的任务
0 M% V3 [$ h; \- i - "command": "make", // *执行的命令
- t# J0 e0 h! M* p - "args": [ // 命令的参数( X* T9 y1 z9 L% O
- "-j6" // 表示六线程同时编译
9 I( f. Z5 a- Y3 r0 F - ],
- r/ }' @7 e7 w, K( `. z5 _& a - "problemMatcher": ["$gcc"], // 表示如果出现了编译报错,问题面板会显示 gcc类型的错误
: l& k! z3 n+ q" F - "group": {
$ N7 t ^5 v e; ` - "kind": "build",
* S& K1 [( ^8 P3 k* B - "isDefault": true
' y) c) z# \% j3 P8 @: O - }
$ Y: w3 [6 ~3 R6 L - }
, @1 u' ^2 \$ z# `: l5 A - ; j7 G' f! [$ Q
- }
复制代码 ! E0 Q- L; |; r; F1 {
2.3 settings.json L& W6 A @' m/ b0 A! g) G" Y
0 ^. A' K9 N' ^9 A' Z, R
3、上述”defines“字段中的定义应包括stm32cubemx生成的Makefile文件中相关的宏定义
! D2 g% A$ ~3 H+ D* t' g; j0 q3 M. X, C6 p+ ^( x" L
& ]* d8 r& C- } o% y( `! Z2 L* l
: n3 f# Q' T+ g, `5 x
4、make编译/使用ctrl+shift+B
1 r( p. S# `* ]- chen@ubuntu:~/prj_vscode/linux-stm32/test$ make clean
% c( U( S3 o$ L X1 h - chen@ubuntu:~/prj_vscode/linux-stm32/test$ make
复制代码
9 Y% O2 r7 z4 F1 D' w! o10调试
: X2 Q2 b8 W) L# C5 |: X4 y1、安装openocd- chen@ubuntu:~$ sudo apt-get install openocd
4 l, J6 a1 \9 Z5 T7 i- t% q - chen@ubuntu:~$ whereis openocd
$ e2 H1 h5 I; A% z8 | - openocd:
% G) w. w( ]7 [' s5 i: d - /usr/bin/openocd
, L, ?, a+ G& m5 u% R6 s' r - /usr/share/openocd 5 j% q: a. W: R
- /usr/share/man/man1/openocd.1.gz 2 H# `4 } C8 N
- /usr/share/info/openocd.info-2.gz
5 y4 d. c( w# j - /usr/share/info/openocd.info.gz
/ h: q; H6 ^1 E a# Y - /usr/share/info/openocd.info-1.gz5 b. ]: Q, y/ X7 N! ]) v
- chen@ubuntu:~$
* b8 L; c6 e$ D' @. {2 m& J8 E) p - //连接好stlinkv2-1到电脑和pcb板,输入以下命令。完成连接, M3 V5 \3 }! c$ W7 S3 Y( R
- chen@ubuntu:/usr/share/openocd/scripts$ openocd -f interface/stlink-v2.cfg -f target/stm32f1x_stlink.cfg
复制代码 3 w% B/ }# [8 U3 d! M7 S
2、在tasks.json中使用openocd进行编译后的下载
2 S" o( f# d$ W- X' B
7 [( d7 R! Y7 K0 p( v# v在原本的tasks.json中追加popenocd的相关内容5 U* E* R0 B. W5 k8 B
- {
& V/ _ ^) e2 _; S* W0 F5 l - // See https://go.microsoft.com/fwlink/?LinkId=733558
/ l( d3 a: x7 f: Y3 G! h9 S - // for the documentation about the tasks.json format
+ D9 f; ^8 }/ i: ` - "version": "2.0.0",+ G) I" z% O$ ^( u3 m7 r- s) V
- "tasks": [
5 h+ B5 o9 z& w: M1 J" @# X5 C - {8 w7 k# j* v7 c+ @! P! m8 S
- "label": "Build", // 标签名; m) ]7 b8 b/ s. e4 i
- "type": "shell", // 终端(shell)型的任务, R, G1 Z5 k5 j; B# y
- "command": "make", // *执行的命令& H# K/ _5 }" m9 X
- "args": [ // 命令的参数
) U/ e, H, d/ G6 s, a6 x; | - // "-j6"表示六线程同时编译
+ Z4 Q; D9 P) {' Q3 T - ],; X9 B E, F+ @% Z1 _/ p
- //"problemMatcher": ["$gcc"], // 表示如果出现了编译报错,问题面板会显示 gcc类型的错误* z: |7 o& }- J0 I& M+ ?
- "group": { B7 V E5 X# A b
- "kind": "build",
. G: i0 j5 d( w7 D0 b! f9 f9 D - "isDefault": true4 s* `. E1 r" P' T; h, H. y9 I
- }
' y/ C- b9 C3 b) H2 N - },* W1 M9 \; t& w3 D1 j* @+ b- c
- {, r& a" h, O3 t; w* n% c1 B
- "dependsOn":"Build", // 任务依赖" K1 o, j: \0 Q% c
- "label": "Build and Download",& Y' _* u3 P, P. c. i
- "type": "shell",
" y R' \- ?$ Q$ x - "command": "openocd",; X- F& [* c, `. A3 R: n# x& ?
- "args": [; ]. v: y( l; w6 S R: J. j- T
- "-f",; U9 b i2 ~/ p' e4 c6 L, S; ?. Y
- "/usr/share/openocd/scripts/interface/stlink-v2.cfg",
& Y, V' M7 }4 p* p3 x* f - "-f",8 c L9 R4 k$ L& i8 {+ N
- "/usr/share/openocd/scripts/target/stm32f1x_stlink.cfg"," v- f; g1 O$ `* E
- "-c",, L# w# p- I+ r2 y6 [- y0 \4 q$ y
- "program ./build/${workspaceFolderBasename}.elf verify reset exit"
" {' m$ d, z0 z4 u" f - ],
/ \: I3 y0 l6 }4 B. W/ A( E - "problemMatcher": []
' S4 I' c- {* `( Y! C+ ^- q& t - }, r" N7 c$ K" C' W, c
- ' F, o, f. w, |2 w1 s
- ' c# i6 N& L/ M* {& D! n
- }
复制代码
6 R- m2 ]/ Y( V" m+ y/ ?, M Q这个任务要求先进行 Build任务,然后通过命令打开 openocd,加载两个配置文件(-f interface/stlink.cfg 和 -f target/stm32l1.cfg),同时,这个 -c 的选项后面跟着的就是openocd成功加载后执行的内置命令,这里我们让它下载、验证、reset,然后直接退出,这样就实现了一条龙式的下载。$ E: ~* `0 L) k/ }& m( ^3 t
3 |6 g% _/ S' U, \8 d
+ E4 t. e! W! b. b9 C! w& y; Z
9 A5 H; X9 V8 i* u: R- x, C* G
; K: r6 u! _ }# V: n& [0 a* ]. k
3、使用openocd进行调试
s. o3 S7 k- q7 b2 p! T3 H3.1 安装Cortex Debug和Venus's Cortex-Debug插件(第二个插件一定要安装,否则会报错Failed to launch OpenOCD GDB Server: Timeout.)/ ], H8 v+ X9 x, m
; _( d$ g! _# \& e% _
: o% F. i+ Q+ Z# f3 K
) q; p" @5 l' W* W8 d$ M
! T3 m: v. b' F$ K' c
9 p! X" F0 N R. `( ~
3.2、 添加调试配置& ]* |1 R4 V8 _) U0 V* s2 a( E
8 v6 ^) u2 z7 r; g1 D
7 e5 j# v( A; P1 p6 I' y
7 C; y! Y) Z7 {+ Y
3.3 选择cortex debug,生成launch.json,并修改内容如下( ?8 z5 |# P& [ n
8 d9 H2 S A+ M. \+ z; l6 y
+ h7 r" z, Q! ?1 _) q7 i
' I/ a' G% J& P+ I- D# y- {, g: I7 B5 }7 U" b- E/ `$ i
- // 使用 IntelliSense 了解相关属性。( O7 x3 o6 T4 R- s/ K4 H, ]
- // 悬停以查看现有属性的描述。
2 _0 }8 x! ` m - // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
3 G$ U2 v" ^0 U C0 }" e) t - "version": "0.2.0",
. d! l. w# }0 \" J - "configurations": [
! ?$ S; E: J* L4 }4 S - {
: a8 j+ b6 R0 j- ? - "name": "Cortex Debug",. H& O/ q0 ~; q- E9 F0 s
- "cwd": "${workspaceRoot}", // 输出路径
' u* b* c0 Y, E- K) _* E - "executable": "./build/${workspaceRootFolderName}.elf", // 要调试的程序; `+ [( |6 \/ \' N9 C- Z5 Y
- "request": "launch",
! l5 y- I) z# _ - "type": "cortex-debug",
$ N4 n+ Q1 \ S+ j) u6 d - "servertype": "openocd", // 调试器选择
, R4 y! m& O$ q( s( ~ - "device": "STM32F103C8", // 使用J-link GDB Server时必须;其他GBD Server时可选(有可能帮助自动选择 SVD文件)。支持的设备见 https://www.segger.com/downloads/supported-devices.php0 }! u* o8 a- q4 ^- H& c
- //"svdFile": "./STM32L15xC.svd", // svd文件,有这个文件才能查看寄存器的值,每个单片机都不同。可以在以下地址找到 https://github.com/posborne/cmsis-svd下载: c0 O; ~4 C$ I) { Q
- "interface": "swd",$ h* R3 Y$ ^- L. X6 N% Q
- "configFiles": [4 O; [9 A4 f0 P0 l; m7 D
- //"${workspaceRoot}/openocd.cfg", // *可以通过加载该文件来配置1 [. _* x. y! n& m
- "/usr/share/openocd/scripts/interface/stlink-v2.cfg",
$ s5 F8 F' {; F$ }4 \ u4 j7 y) A - "/usr/share/openocd/scripts/target/stm32f1x.cfg"9 ]" S% n" S7 x5 J6 {* x$ P
- ]," p# u3 ^2 W: U5 ?6 t
- "runToEntryPoint": "false",! o% V: q6 {7 l, K
- //"runToMain": false,
* P( {/ z6 k; b0 S0 e - "preLaunchTask": "Build", // 在调试前预先执行的任务,此处是tasks.json中的( U* B- j7 p( ?# E4 M f
- //"armToolchainPath": "C:\\Program Files (x86)\\GNU Tools ARM Embedded\\5.4 2016q3\\bin" // 如果没有把 arm工具链路径添加到系统环境变量,则需要这条指令添加你的路径
% s8 k( L" V1 Y4 D% o b* R( n - }$ l- ]- Q6 I8 @: l
. I9 f$ y* p; A0 x: O' |' P! z- }
复制代码
9 e# E% V* I- x* B2 ]3.4 调试( P9 T* X+ R2 L/ L6 L
/ @7 a1 x! S, T4 R! ]" l9 p
& ]$ H! y2 Z5 g& ]% W/ ^# q, [" X+ r! W
11使用VSCode串口工具
. x& i7 h8 a) [" _3 M% z! q7 ]0 v1、安装serial monitor
( j/ n! L5 B9 P$ b* ?( T/ P/ T
( \' }/ ^0 o1 y! o
% k3 _. d1 h# G/ r7 v! [$ [2 U0 K9 t2 x
2、查看连接的usb
" c7 \; E* q% d' E: O- chen@ubuntu:~$ lsusb
0 W: I- ?* ^* F2 L: L( w1 E% ? - Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
6 `3 [8 f% z$ V; } - Bus 002 Device 007: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
) X5 J- T8 B3 j+ q - Bus 002 Device 006: ID 0483:3748 STMicroelectronics ST-LINK/V2
9 z* Y) K/ L" U+ f. U- u c6 q/ | - Bus 002 Device 004: ID 0e0f:0008 VMware, Inc. VMware Virtual USB Mouse
* F! }! D% M! ^1 B - Bus 002 Device 003: ID 0e0f:0002 VMware, Inc. Virtual USB Hub+ J1 b% L1 X5 D. c1 L- K2 U
- Bus 002 Device 002: ID 0e0f:0003 VMware, Inc. Virtual Mouse* `3 C* U1 O1 o+ s8 ]2 H$ V
- Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
" c- M; O% J5 s3 v - chen@ubuntu:~$
复制代码 ) }: L5 g: E& {, J7 B* J) Y) a
2、加权限- chen@ubuntu:~$ sudo chmod 777 /dev/ttyUSB0
复制代码
) ^7 u* q( ]! ?6 t- {% Z12使用git$ n8 R0 c' G9 _! B# M* k3 |0 d
1、检查状态
* |* c2 Y6 R2 B5 _- //在执行 git add 和 git commit 之前,先执行以下命令查看当前仓库的状态:$ i: |$ h7 ?9 F# V0 C" k
- git status
% O- w" M9 m9 V' s - //这将显示工作目录和暂存区的状态。确保没有未追踪的文件,也确保已将需要提交的文件添加到暂存区。
复制代码
6 u9 ~" t; E. c* E2、确认 Git 配置:
/ p. h% X2 [/ c4 ^5 H- git config user.name4 v$ x1 _5 [; S0 U
- git config user.email
- b& h7 v% H( @& \, ? - 4 E6 A% a9 ?0 `6 z
3 [" T+ V0 l B2 J/ h- 如果没有设置,请使用以下命令设置:
3 [0 |: v# n2 k( [0 D - git config --global user.name "Your Name"8 U; E) ?; r+ H* |1 l7 s
- git config --global user.email "your.email@example.com"
复制代码
- }) k" B% N0 x* ?1 z8 r3、检查文件权限3 m9 j' x% _+ b1 [: P
确保你有权限在新文件夹中进行写操作。使用 ls -l 命令检查文件和目录的权限:
* \# U+ e, Z3 W9 m, K+ M G1 X' als -l, y/ X1 \; v+ q
! m! p' x# Z9 Z& A4、查看错误信息:4 ]" L9 J7 S) J& `) x) i
如果 git commit 操作没有反应,尝试使用 -v 参数查看详细的错误信息:* W- S5 ?4 S& P5 O- q
git commit -v+ h. m' B8 }) T2 {3 W: a: a
& a9 y) ^9 o9 T3 Q# _
5、清理缓存:: x, Q# S) y5 }) v7 X
有时 Git 的内部缓存可能导致问题。尝试清理 Git 缓存并重新尝试:% |$ k2 F9 I% B4 ]
git rm -r --cached .( o( W* _7 F( ]$ r. x% B' S* B" S9 V5 ?
git add .! `" O" t0 ]& e- B
git commit -m "Your commit message"
1 s+ x/ S4 @% ~这将清理缓存并重新添加和提交所有更改。
) R# R$ ^0 X0 ^; }1 V
5 \5 N& d# x3 \9 b! k, K6 R4 V* d1 e" j, ?! M J
转载自:kevin
e# q& m" H$ h6 c( g& B1 X! n( O. E, C% v, y2 T- M( ~0 q- F
如有侵权请联系删除9 D4 \) w/ S$ Z0 }% \- w
, H& V4 n& w( |! ]) ^& V, I2 W; d' @" {, K( R% a
4 i$ g; u* k. G. N! p |