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