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