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