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