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