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