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