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