你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

基于STM3的开发环境搭建经验分享

[复制链接]
攻城狮Melo 发布时间:2024-6-15 14:45
“ 从ubuntu20.04入门到完成stm32程序的调试。”8 c( W6 _1 G7 ?5 X3 u) a7 F

; @9 A+ `5 w! b  s! k+ S
微信图片_20240615144535.png
, Y0 P) w% w' C' n6 ^
5 _' A  C' o& t& b, C2 n3 R
1、更换软件源6 z7 G9 Y5 ]: z' @
源文件(sources.list)6 v$ {& X: ^' Z& L# b! |! Q: ~
更改源(Ubuntu20.04对应的源)& @" ~4 e$ E2 r8 x
更新源5 |1 S' m' a) a5 `" q; V6 s
% g+ w/ p, B1 R4 x5 g! J6 a: _' j8 p
2、安装拼音输入法; w! t1 N2 m  f' R( p# S
Region&Language->Language Support->Installed Languages* ]0 y* _8 m4 z% G- G1 u
Input Sources->"+"->chinese->Hanyu Pinyin
4 |/ t+ N  `1 `  T$ E0 t3 a5 f3 p6 A% _$ c1 F) c! W8 m- [
3、安装VSCode
. k" v3 I/ a' F) L. q下载.deb程序包5 s& g# U3 G' L0 \
使用dpkg安装
& H+ N+ L/ v8 O0 B# j9 m/ A( W7 _调整设置. V2 E; l+ ~6 L6 I+ m
启用Title Bar, ?0 M, f* Z0 d- z  t" O- U9 p
安装插件
9 Q8 P; v  J6 Y
" O) M' B- q" D, Z8 ?. @8 B
4、安装Build Essential及gdb& c* i+ B! Q! k7 `+ [
build essential介绍
& w6 h7 K0 K; Q' @, R& B; w命令行安装build essential和gdb
2 {7 J7 _- z1 N- @1 U1 nLinux C程序开发简介
# C8 A1 C6 Q$ W7 J  H无makefile
. p5 _0 e- o9 v/ `5 W4 e0 L有makefile& }! s& f: l/ X( @$ b
生成.vscode文件下的debug配置文件9 N: l+ p5 p" q
生成task.json文件: u6 e% f) e8 I! J' i; G* r
生成launch.json文件5 d' G5 i. w2 T' l1 E' p) _) d
生成c_cpp_properties.json文件
$ }/ @; W$ b4 }+ s

. ~  e8 ^3 j: ^. P5、安装Vim, v. b( Y/ e' v  g; x8 V
7 z+ U4 p/ F( W( A  m# ~
6、安装gcc-arm-none-eabi
, S  Z9 N3 \: U' [% c& e
下载嵌入式gcc编译器
2 G- c6 u0 a3 z# ^0 b/ a解压: V5 Q+ [$ `5 a" l1 _7 x
添加gcc-arm-none-eabi下的可执行文件命令到Terminal5 L; m: I+ M5 S# F

6 X1 L) i4 a2 E
7、安装stm32cubemx
0 \9 q) v2 Q7 g下载stm32cubemx
8 u+ ~1 {; u6 L. E安装1 Q. @: d# _4 m) V
增加图标到菜单
' L: k( s2 G  k6 q% ?' X  r: L
' U6 Y/ S: C- W7 w$ \- m
8、使用stm32cubemx生成初始代码
& ^2 J* a- j# |6 B: g. {$ P选择芯片->配置->Generate* `. x5 i  D8 F8 v  S2 P) e4 Q
勾选PinOut-Debug-SerialWire3 w5 C. Z! c4 q+ F
1 z5 L: |% y; r& I  v5 z
9、配置VSCode2 P- D7 `7 h9 c# a8 z
使用vscode打开上述生成的初始代码5 t2 _7 a9 L% X, s6 E% N
生成VSCode配置文件
* [) }% n) J, a5 k) [* ic_cpp_properties.json
) K. j1 L% s: e; q2 Y4 ptask.json
3 n9 ^1 k5 `6 jsettings.json
6 `6 y( j7 }6 i  }+ U& G) q3 x/ a% Y宏定义包含& C8 V( U7 F0 W1 J9 j7 j
make编译/使用ctrl+shift+B% r$ s* Z- W0 P/ p# o: G4 x

1 h2 u5 h7 c+ G' u& }( f$ M; p: Z
10、调试* d5 J( C( e2 |2 W7 N2 [/ ~
安装openocd# c$ A# D5 D* Q. T3 J, r( Y: `
在tasks.json中使用openocd进行编译后的下载
7 B8 C7 x0 Y  o/ ?$ v使用openocd进行调试; o. l5 F  n4 g% ^! b
安装Cortex Debug和Venus's Cortex-Debug插件4 d6 G' w; d3 n" p
添加调试配置9 r" c# [2 L( h1 i- z
选择cortex debug,生成launch.json
& P* f8 q4 w0 |6 e调试8 H' n. e( W& M+ J4 t$ ^

; e* E8 @' {" h+ ^6 B  ?) j
11、使用VSCode串口工具6 x$ C; E) e- L# o
安装serial monitor
* h; l$ V0 c3 h1 k) h" Y  C8 g7 ^. F4 I查看连接的usb5 r0 G' _. I8 f  O& V1 n
加权限
5 V! L' Q  s5 a0 [  N/ o- U2 B+ P( E1 q/ N# x7 a1 N
12、使用git
# ]: ~8 a* S5 z5 ?4 [01更换源6 y! Z: v2 h4 E
1、源文件(sources.list)
4 N8 B8 p  i4 x% S( G$ |3 c! G9 Xchen@ubuntu:/etc/apt$ ls -a( A7 Z% z* f7 @0 @9 |4 ?+ x: D& o
.   apt.conf.d   preferences.d  sources.list    trusted.gpg.d6 z" h  j4 z" e- `, A) i7 G; [' R
..  auth.conf.d  sources.bak    sources.list.d
7 D3 Z& n* F9 I) U1 `+ o& I& ]  T2 L8 v* t
2、更改源(Ubuntu20.04对应的源)3 p" V0 [* ~  O! v, x
  1. chen@ubuntu:/etc/apt$ sudo gedit sources.list
复制代码
  1. #阿里源(Ubuntu20.04)
    " q* v2 u" r4 X$ g% E  v7 c
  2. deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
    ( q  a4 I7 H2 j. _, h3 }( ?
  3. deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse! B5 ~9 y; N+ n7 i6 q/ @
  4. deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse0 G9 A; @" C5 B9 T' a. w
  5. deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse- ]. H) U" Z7 C, R9 M) [% `3 b
  6. deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
    5 M& {% [1 A- [6 X- c+ T1 ~
  7. deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
    - p- |# k- f% [' w) [5 N! w2 ]7 U
  8. # deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
    3 j0 B; o6 j  W4 Z0 U( `
  9. # deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
    3 @! x$ j; ~7 N9 w3 D
  10. deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
    # Y/ E4 S& K) X- U- B) P% V
  11. deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
复制代码
  1. #清华源(Ubuntu20.04)* |  w; j) A; _1 Q3 b
  2. # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
    * o% D8 g& n: B' F/ ~
  3. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse) Z, Y/ U8 o; O
  4. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
    & k; g8 ]  n. e6 D4 _
  5. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse* d! s3 d+ R1 m2 Z* j* f+ a9 G/ T
  6. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse9 K( P) Z+ n3 @
  7. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse( ~  l: o, F3 _" E4 p6 Q, L
  8. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse- f/ g* h( G" w+ O
  9. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
    6 x) J: z' c  ?2 Z. a- S  g
  10. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse" V, [1 t* I" G3 v* t

  11. ) A  _  c: X, Q: f7 Q
  12. 8 p) {& E- q4 r: ~) o! A4 Z4 K
  13. # 预发布软件源,不建议启用% s6 e  e3 j5 G( g# a- u
  14. # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
    " d1 x4 Z' h( T
  15. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
复制代码
  1. #中科大源(Ubuntu20.04)
    6 B) \" _: x, E' }3 ]0 `
  2. deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
    / V% r  W- u% s3 ?
  3. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse6 [1 H2 ]' p3 o) I' _
  4. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
    9 U; J- Z9 m# I; z6 g/ F5 V
  5. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse: S& r5 V- ~+ e% m6 l2 ~
  6. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
    & C) C; U7 ]( k
  7. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse4 G) W6 U5 U- n) K( w5 P! Z
  8. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
    ! T. D+ v9 p# g: B+ Q& z* G. w! p0 j
  9. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
    : ?/ [; |& Y- o! C0 Y( p) @2 _
  10. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
    5 j! G$ o) I$ b7 a" S
  11. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
复制代码
; t2 a( n7 j9 Z
3、更新源
" k( o: l* E2 d. I1 p5 w, \; s9 O
  1. sudo apt-get update
复制代码

: t, B/ y3 r# c* I: {02安装拼音输入法
; m5 b* \8 X* R5 N# n1、Region&Language->Language Support->Installed Languages
4 h( ]3 g3 s" E7 V6 Q; {& p7 q" z3 i2 \" F# N! S1 G1 D
微信图片_20240615144532.png
: V, S. e2 h1 S) O$ k
$ m0 h+ K0 C( j# `4 I2、Input Sources->"+"->chinese->Hanyu Pinyin% N$ P4 `0 M& N' s) B; }1 [
  1. chen@ubuntu:~$ sudo apt-get install ibus-pinyin
    5 z' n/ q7 X& T+ G# ]3 j
  2. chen@ubuntu:~$ ibus restart3 Z! W: T% ?  r
  3. chen@ubuntu:~$ ibus-setup
复制代码
* }; S" q3 @! j1 ~* H
微信图片_20240615144527.png
4 `  [, F( F1 I# ~% {
6 I4 x- P  p" x- G1 {5 H: W) `' T* h 微信图片_20240615144524.png
6 s, e' f' c5 [0 f) |% g8 n" V. O! d+ m
微信图片_20240615144520.png 3 f/ m6 Z! S' j' o
8 m. e' b. u# o1 A# |! x: R
03安装VSCODE
# k. l* x/ ], G. T3 ]5 G1、下载.deb程序包https://code.visualstudio.com/
% g  w/ B  s4 p/ {5 B4 p0 V1 c1 R/ y( O: a0 y! p
2、使用dpkg安装(在Terminal中Ctrl+Shift+C为复制,Ctrl+Shift+V为粘贴)+ p2 \/ j9 i% f% k( b2 L! M
  1. chen@ubuntu:~/Downloads$ sudo dpkg -i code_1.89.1-1715060508_amd64.deb) q) ?5 {  i$ @; r4 W5 w/ v/ H4 r
  2. chen@ubuntu:~/Downloads$ code .
复制代码

, B7 a8 n  I& I3 `3、调整设置; P( D: N( W8 g; J/ v# u2 f
启用Title Bar:View->Appearance->Editor Actions Position->Tab Bar
8 o! Z( |' r; t+ A
& Q7 X: Y7 v6 d& T  [
微信图片_20240615144516.png 8 ?2 c% w0 k$ Z8 U: A# O! O3 k

6 n3 K* r( t& H- t4、安装插件
  X: E7 A4 b9 o8 tgit安装9 s, V8 P6 i+ m
Download Git for Linux(https://git-scm.com/download/linux)" v. y4 L. O* T

4 A/ [& c# J1 L& M# z
微信图片_20240615144512.png / M% C1 F9 w5 Q/ _
/ d4 l9 [5 m( \- v
微信图片_20240615144509.png ! \5 L  \* M& B/ h

2 C( t4 Z2 y+ @命令行直接安装(Ctrl+Alt+T呼出Terminal)
9 U* z7 g  Z( p+ i3 }% V& F
  1. chen@ubuntu:~$ sudo apt-get install git
    4 z. J* F/ v4 m( r9 i. A1 D4 ]
  2. chen@ubuntu:~$ git --version
复制代码
# S5 G/ |0 D1 n) Y+ A% [! {
git设置
+ ]5 U5 Z* Z9 w4 j# b
/ u' h+ U9 M  o( C8 Z7 K4 S  _
微信图片_20240615144504.png
3 N' d% b0 J! F/ ~& e, e& S  C: g1 y$ A% E
  1. git clone https://gitee.com/kevinkerry/linux-stm32.git" u# Z. q6 s" F6 `
  2. git config --global user.name 'kevin.kerry'
    0 k. X' v- C  x! Z
  3. git config --global user.email 'ct15762287606@outlook.com'& b4 A  n; @: D: l2 D$ q1 e% X6 K" X% f
  4. * ?( H6 E( @- [6 N7 G

  5. 7 u* D) ~& E0 V2 n+ U
  6. #示例6 Y) J) u* @2 I0 ~
  7. chen@ubuntu:~/prj_vscode$ git clone https://gitee.com/kevinkerry/linux-stm32.git
    , T: g* f" ]. x( r! V
  8. Cloning into 'linux-stm32'...
    ) `2 g' H" b0 E! I! E4 x3 J0 e
  9. Username for 'https://gitee.com': ct15762287606@outlook.com
    0 O1 d5 N* j  Y1 [8 G
  10. Password for 'https://ct15762287606@outlook.com@gitee.com':6 f- w$ ^8 S( s$ O7 ^
  11. remote: Enumerating objects: 4, done.
    " `5 F, T9 K- S" w0 }4 e- ]
  12. remote: Counting objects: 100% (4/4), done.* x9 U/ w1 n7 |9 O9 V. p) J6 G: ]
  13. remote: Compressing objects: 100% (4/4), done.
    ! R9 B; P7 E$ `0 M
  14. remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0' S( Y. e8 C6 F
  15. Unpacking objects: 100% (4/4), 1.79 KiB | 1.79 MiB/s, done.9 L/ y  f$ t" F& R5 ~$ [* \; B
  16. chen@ubuntu:~/prj_vscode$ ls* \1 W0 P% a  K! S. G/ g/ s9 Z
  17. linux-stm32
复制代码

1 ?0 o7 S8 }( S: TGitLens — Git supercharged插件安装
; R, H7 o- x6 g$ S3 m0 I
" T' N  U' V8 W( }+ n
微信图片_20240615144459.png
: e8 S  q- ?' I5 @

6 j1 |. y( i$ a& |+ t( a3 E; n! dGit History2 d3 y/ h* X' p+ r- O7 `; E2 `  i+ s* V& G
0 f7 h# B# c$ q: i6 O+ x$ e- R4 }
微信图片_20240615144456.png
4 I  v# J8 P/ l& P  t/ I! V

7 g' l, E' S: v9 r( W* U/ |4 M* g. WBracket Pair Color DLW  H. b( `6 M5 e1 N# D

. k6 ~4 v6 P0 B, ^
微信图片_20240615144454.png 5 ~) [/ L& ], W  g
7 o/ N. R1 u; v
Arm Assembly9 W* c" e6 r# k% \
1 ?, y% R3 j3 n$ B6 Q" j
微信图片_20240615144451.png ( h0 a, t  q$ b; O2 d1 s4 M) p) S

# C5 x+ g$ p) p5 S" I" y; Y: A) SVenu's Cortex-Debug' c) Z4 q+ J, h- O$ U$ |4 J: T* Q
  U% W- y# I/ F! D
微信图片_20240615144447.png
$ g3 }7 p+ j& F+ j4 w0 R1 [

" G. x8 O- E7 Q3 ]! K) l! B- A4 p+ }C/C++3 _6 n5 K! K( k* ?

( I% G- C% b/ H9 c! u
微信图片_20240615144437.png - }4 d- W) Y4 i8 _3 R: Z( A

2 b/ Y- s6 q" Y' x# E8 F04安装Build Essential及gdb: L  Y0 G) T2 O( C( i( ~5 Q) O! r
1、build essential介绍& s+ T: U) s- V* g
build-essential 指的是编译程序必须的软件包。它本身不是一个软件,而是一个工具集,包含了在Linux开发程序的一些必要的软件包,包括但不限于gcc、g++、make等。$ u4 Z: l; U3 Y1 v, `! V

4 V- M! j* c6 \( V" g0 H( G2 w
查看该软件包的依赖关系,可以看到以下内容:4 B: i" x# ?! r4 U& n
  1. chen@ubuntu:~$ apt-cache depends build-essential
    4 ^, D5 T! ^& t- |5 _6 i' |5 p" t
  2. build-essential* U- e, u) ?: Y6 h" h/ K
  3. |Depends: libc6-dev
    $ V) i4 a+ L' x) Z7 W
  4.   Depends: <libc-dev>3 H( W8 I& R- F8 j+ F+ T
  5.     libc6-dev( t! e) O' K5 }, g
  6.   Depends: gcc
    & t* W+ b* d! @8 y, M
  7.   Depends: g++
    ) Y5 h1 o& R; Y
  8.   Depends: make: k% K5 L# K8 Z, E8 S5 \9 X/ l
  9.     make-guile: r/ Q! `; t- B  R
  10.   Depends: dpkg-dev
复制代码
, e$ h" X* C) Q
也就是说,安装了该软件包,编译c/c++所需要的软件包也都会被安装。因此如果想在Ubuntu中编译c/c++程序,只需要安装该软件包就可以了。6 o: V" y' D! n& ]) P

- e6 x, w0 m' S& Z- l) u5 l2、命令行安装build essential和gdb
6 r' `' Q9 B: N' S有时候可能会需要安装gdbserver
4 U: i4 _0 G1 E; O0 N) K
  1. chen@ubuntu:~$ sudo apt-get install build-essential gdb
复制代码
7 Q3 b: k) ^0 O3 T
3、Linux C程序开发简介
3 X8 K  F) ^9 o" K. Y4 r3.1 无makefile4 v) l' O# Y0 }  Z/ U7 E1 C- S# q9 ]
  1. chen@ubuntu:~$ mkdir linux_c) G- H5 |0 ?4 _
  2. chen@ubuntu:~$ cd linux_c
    % {! z7 S9 V; J
  3. chen@ubuntu:~$ touch hello.c
    , m3 Y* }* `* }$ V7 ?
  4. chen@ubuntu:~$ sudo vi hello.c  ~1 q: _/ T1 W) e8 ^
  5. chen@ubuntu:~$ cat hello.c
    $ `+ p# j" r5 L: n, d, g
  6. #include <stdio.h>! O2 x  a4 o1 N0 {: v# Z
  7. int main(int argc,char *argv[]): ]2 ^* g* p% w
  8. {
    ( y4 d% {# S2 M2 v6 ^
  9.   printf("hello world\n");: N8 p; ?* P# h4 ]$ p
  10. }: X( T* `* @  y
  11. chen@ubuntu:~$ gcc hello.c2 Y" [# P. _% m' N
  12. chen@ubuntu:~$ gcc hello.c -o hello
    ( o+ I- B/ ]# |$ {* B. z3 K) X! y
  13. chen@ubuntu:~$ ls -al
    ; \5 S5 U1 ~1 P; n/ c9 G) f" t
  14. total 52* U" \& U% O: k6 _2 l
  15. drwxrwxr-x 2 chen chen  4096 May 16 18:32 .
    6 }. \% r( A3 n
  16. drwxrwxr-x 4 chen chen  4096 May 16 17:37 ..- L+ t2 w: n% y: f
  17. -rwxrwxr-x 1 chen chen 16696 May 16 18:29 a.out$ F$ x2 Z( e5 g/ h9 F" [0 z* |  U
  18. -rwxrwxr-x 1 chen chen 16696 May 16 18:32 hello9 n- `6 G$ `5 `- F4 }3 \7 I
  19. -rw-rw-r-- 1 chen chen    82 May 16 18:29 hello.c9 h5 S4 F; V- h# N& x
  20. chen@ubuntu:~$ ./a.out
    , R; a+ W8 r( P7 E$ _8 G" t
  21. chen@ubuntu:~$ ./hello
复制代码
: T7 u2 u7 W: W4 I: _5 t! ^6 ]
3.2 有makefile) u# `7 [  O# V  _
3.2.1 创建程序文件
  1. chen@ubuntu:~/prj_vscode/linux-c$ touch main.c input.c calcu.c input.h calcu.h
复制代码
, e* Y. w! _9 \4 p. s" g% T: W
main.c) E2 ]  T! @- j( {8 e
  1. #include <stdio.h>$ I. Q) q4 b9 g7 t8 Z7 J2 y
  2. #include "input.h"
    ; N* A8 J2 @) R1 b% Y: x9 V$ g
  3. #include "calcu.h". r: H/ I2 F" [5 k! l+ i& o

  4. # i1 j! s+ z) z8 u. y, f1 O

  5. 5 h5 K- Y, S) r  a/ f
  6. int main(int argc, char *argv[])7 L+ A' O  e) p# P% d. x3 R; C* W
  7. {5 G1 I: u/ i+ R3 j' ]

  8. 7 a3 i4 w+ E$ `, m% i) N
  9. 6 J4 h* _, |1 X
  10.   int a, b, num;
    ) f( q- p6 \% d2 h' H8 K

  11. 9 E  u0 _) g. T5 s

  12. 9 R+ G, Y6 |( E+ e
  13.    input_int(&a, &b);( \4 e4 w2 S' R1 G! u
  14.    num = calcu(a, b);5 n' a: J; Y7 I5 @$ K4 N5 c% W% S6 M3 b
  15.    printf("%d + %d = %d\r\n", a, b, num);6 [& X) |  }8 Q
  16.    - C7 S1 f3 G  p8 O, I' w+ m' A
  17. }
复制代码

- P/ U3 s6 F- B4 X3 }6 d9 M; |input.h
  1. #ifndef _INPUT_H  [+ A  M! `( g% i
  2. #define _INPUT_H
    " @7 {+ F; P5 l2 r% Y2 z

  3. " z0 ?, N1 [. R6 z
  4. void input_int(int *a, int *b);
    & c- u" S) Q7 [3 u  \

  5. 0 F6 U) ~6 t/ q+ C3 B
  6. #endif
复制代码
2 v: n3 {" ?/ S! D) b' l
input.c
0 e0 I% C7 R" N* P8 Z/ g
  1. #include <stdio.h>
    4 |: g  w' h6 \/ O% J
  2. #include "input.h"
    0 O$ F; U; {. f5 j1 E  R( T

  3. $ S" {6 F  Z  u+ j# t7 p+ @
  4. 8 ~, G/ E% v) z+ e
  5. void input_int(int *a, int *b)  c/ [9 H% y( D; N0 |
  6. {
    # [; A% Z2 m  F) f! g7 Z# |0 B# T
  7. printf("input two num:");# D& D1 Q0 p0 m( n$ X
  8. scanf("%d %d", a, b);
    $ S1 Q7 D* [2 T7 k! r/ F# k
  9. printf("\r\n");; z2 Y- A; q+ v2 A, e5 k7 `7 p4 e
  10. }
复制代码
* a! W  Z6 ]7 E) N, a) x+ [0 f
calcu.h
  1. #ifndef _CALCU_H
    . o, o, g  ~  I$ I+ A: A% r  H& u5 e
  2. #define _CALCU_H
      ?: [8 f2 ]* [+ @

  3. " ?$ u' x3 c  O4 V

  4. 2 b2 o$ V4 r' h7 H/ }
  5. int calcu(int a, int b);2 l# X& g" @# t  I* a+ H
  6. #endif
复制代码
$ U- Z5 m; \9 Y3 x4 \6 p& C
calcu.c
: d! \! Q) Q* }0 V, Z# ?- [
  1. #include "calcu.h"& |0 M+ D& ^- B/ \" G6 T

  2.   ^2 W- p+ F" i% V+ t

  3. 4 \; B" b$ K7 q- Y# |+ t1 K
  4. int calcu(int a,int b)
    $ A" h9 C# N( I( d, S+ d$ r5 h! h
  5. {
    1 q0 j$ J# Z" ?1 ]' F7 V- |8 j8 f
  6.   return (a+b);* H9 u1 L- |9 i# j5 G. D3 X
  7. }
复制代码

( D& K2 |$ N8 L& _4 R2 `" \3.2.2 编译测试一下+ D) f8 s  h2 t/ A$ }
  1. chen@ubuntu:~/prj_vscode/linux-c$ gcc main.c input.c calcu.c -o main
    0 x) h* v' W  @4 {: ]
  2. chen@ubuntu:~/prj_vscode/linux-c$ ./main: F- Y2 V" l$ @/ p# V
  3. input two num:23 569 q& W& H$ n0 `4 q3 S% w' V
  4. $ g3 a/ F' ^* M; p3 t7 F- _

  5. ) n1 N, C) E! _/ C& C: ^
  6. 23 + 56 = 79) n; G$ L0 d5 Z* _5 ~5 J, H
  7. chen@ubuntu:~/prj_vscode/linux-c$
复制代码
  1. chen@ubuntu:~/prj_vscode/linux-c$ touch Makefile
    2 T' V9 m0 A5 o, R/ C( P; e
  2. chen@ubuntu:~/prj_vscode/linux-c$ cat Makefile
    ; |1 L' w9 w+ w4 c% u) A7 G
  3. main: main.o input.o calcu.o
    ( U; Y7 t$ M- A+ r. z5 `2 ?! t
  4.   gcc -o main main.o input.o calcu.o) P! {0 ]2 e( W! R& X
  5. main.o: main.c
    - ^4 k9 ]' \  @# [8 s% S8 k! A  p
  6.   gcc -c main.c
    9 a5 T8 ~0 j, ^- q$ v" l5 G6 _
  7. input.o: input.c% @/ B9 J. K. x* m# w4 M: H  c
  8.   gcc -c input.c
    4 n5 n: f  ^' F7 E/ k1 y
  9. calcu.o: calcu.c
      ~9 Y3 Z" Y6 z( ~$ o
  10.   gcc -c calcu.c
    ' W$ m; z1 f0 T+ s' l, N

  11. : {4 g- Z- s, @3 D5 ]
  12. 6 b& i/ x% `+ E7 H8 @$ \
  13. clean:
    ' E$ F2 G6 h% O+ X
  14.   rm *.o
    6 r. \# j$ q- F' f# U; x# G
  15.   rm main; B; G, \0 g$ l4 {" m
  16. chen@ubuntu:~/prj_vscode/linux-c$ make clean/ v5 s- I" Z, w& `) X) p" A# g
  17. chen@ubuntu:~/prj_vscode/linux-c$ make
复制代码

2 o2 Z0 u: u* `6 _: K: C; `/ @& W, g4、生成.vscode文件下的debug配置文件
# N- E1 \1 j) d" }& c5 utasks.json:该文件主要用于配置编译的选项,如果使用makefile或者cmake进行编译,则用该文件调研make或者cmake命令进行配置。
) T: a! l9 b# Blaunch.json:该文件主要对debugger进行设置
* i' _/ S8 d, Fc_cpp_properties.json:编译器路径和intellisense设置
$ J6 ~5 T, B' m/ D8 R5 y

3 N# p5 w1 q6 ]* I% h$ G0 l& f4.1、生成task.json文件9 n  R" X7 l5 L& b
$ i( J' h% u3 `. }4 V
微信图片_20240615144428.png
7 Q& E  [8 b2 |% k9 @- C, c* a! m图4.1.1 Run C/C++File! D8 N6 U+ T% H" g  ~

- d! x, y: y3 j2 ^9 S" _: U
微信图片_20240615144424.png
: ~6 _, \7 R3 l0 F- \8 ?6 I0 J
3 L3 @. Y+ [  E- l2 k* I修改生成的task.json文件如下
4 t+ x9 O$ {/ p% r
  1. {
    % u$ {3 c" m9 d( B0 z: n# `/ r3 z
  2.     "tasks": [( ?3 e; i+ B6 p4 J2 e' `
  3.         {# n9 `6 A7 D( h- Z% S
  4.             "type": "cppbuild",
    $ w3 m7 l) Y7 z) e
  5.             "label": "C/C++: gcc build active file",
      h9 j3 s# S! A7 t+ A# W* e7 L
  6.             "command": "/usr/bin/gcc",. t' a5 A+ v1 c
  7.             "args": [
      }4 p8 ]3 G3 y( A+ B/ V' T, n
  8.                 "-fdiagnostics-color=always",& V2 c& v* ?" G
  9.                 "-g",
    ' D1 @4 X3 S* H7 D$ E- h# J+ R/ g. P
  10.                 "${file}",! C& q! n1 s' E' a5 I! A  P
  11.                 "-o",
    2 G( @: g( s& v! S# |. `7 y* ?
  12.                 "${fileDirname}/${fileBasenameNoExtension}"+ J5 z  C3 ~, W+ |& k3 K
  13.             ],
    + ]& D: C/ t& W
  14.             "options": {
    $ l1 d- A  N/ H* h
  15.                 "cwd": "${fileDirname}"
    5 g1 ^3 L+ }5 {
  16.             },
    " ~" o8 A4 n1 _* w3 U% t! w
  17.             "problemMatcher": [- s: u, I8 F. p; g
  18.                 "$gcc"
    * y7 ^; z+ Q" E. r; @) T
  19.             ],
    * E7 y! h1 V/ }1 E! C$ ~
  20.             "group": {3 r3 P3 n! h! N
  21.                 "kind": "build",+ c- f+ e, Y6 i0 x# p/ s
  22.                 "isDefault": true
    ( Q3 U' r. i! m# H. y; Y
  23.             },: A7 L1 Y* G* U6 d2 u2 E" T6 O; A1 @
  24.             "detail": "Task generated by Debugger."9 ~2 E" B' Q2 ~+ j, [
  25.         }
    # B: J% G& {- J" O8 v: P$ f5 z
  26.     ],3 L1 e1 Z" v) I. |
  27.     "version": "2.0.0"
    , I) m% z" b+ p
  28. }
复制代码
' c8 M7 G. M- T5 V9 V3 N
修改如下
  1. {
    1 M& \0 }4 \$ s, x7 P& l# V" k0 L- }
  2.   "version": "2.0.0",
    5 G, [& N* m( O% f% V
  3.   "tasks": [
    , }6 |+ g' x, p
  4.     {
    5 q# O- G: r! a* n1 k
  5.       "type": "shell",
    ! \8 t8 j  y6 @2 s" ~& u! P
  6.       "label": "C/C++: g++ build active file",8 D: u; U0 a* w4 ~2 y8 E
  7.       "command": "/usr/bin/g++",
    " Y! ?: M# l) a1 _
  8.       "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
    8 ]" f# p- t3 ^& P& O
  9.       "options": {# a5 k1 d5 x/ X/ [9 O* V
  10.         "cwd": "/usr/bin"
    2 S7 @- u# j- G
  11.       },
    * _; F( F1 ?% y7 `; f
  12.       "problemMatcher": ["$gcc"],) h6 I5 d5 J4 C7 l8 Q
  13.       "group": {
    1 _+ u3 }8 ^+ |- [; X8 O. m
  14.         "kind": "build",' V; M1 S0 k+ G( W* N( ~
  15.         "isDefault": true3 f. X7 _. _/ b1 c- T" C
  16.       },
    4 |! o" r' b: [5 \
  17.       "detail": "Task generated by Debugger."0 m" ?# _7 t0 [+ g
  18.     }, F  F* M3 v, g8 l2 ^' h# P- Q( U
  19.   ]
    ' o) z6 o1 R& F: {" u" R
  20. }
复制代码

3 s' [  K3 [5 L% o' _+ E  D8 ~# [command 代表使用什么程序去执行文件,这里使用的是g++. args表示的是传递给g++的参数,表明使用了什么文件(-g 后面的file文件所指内容),生成了什么文件(-o 后面文件所指的内容)。所以这command 和 args两行参数表达的意思是:使用g++编译器将${file}文件进行编译,生成${fileDirname}/${fileBasenameNoExtension}文件。
! H3 b- q$ c' Z# \

8 B1 e0 C. u7 D上面部分是对单个c++文件或者多个c++文件进行编译。但是如果一个项目使用makefile进行编译,则如何使用vscode 进行配置呢?我们这里进行讲解。由于我们使用make进行编译,所以我们将command命令后面加上make,args后面添加相应的参数。比如我们需要进行debug 所以要控制makefile 中所有的gcc编译命令后添加-g。不然的话,就会出现,编译成功了。但是debug的时候,断点无效,所有的执行都是一下子执行完的情况。由于在我们的makefile 中通过TAG = dbg来控制加不加编译选项-g,如图3所示。所以这里我们的参数添加了"TAG=dbg",这里根据不同的makefile进行不同的配置。1 ~; ~; F. t6 {2 a; `; s6 \
  1. {. r. r/ b  n& u* a" r1 }
  2.   "version": "2.0.0",: D+ j2 l9 j* C4 q& u8 Q
  3.   "tasks": [
    . i! K3 t: ^- Y2 k1 g4 F  o. i' W
  4.     {
    , R5 U9 b4 E4 ~8 J5 \  R+ H; _& w; e' E
  5.       "type": "shell",
    2 _6 d' j8 R- T$ K
  6.       "label": "Build",1 b1 L& j2 T2 c3 W
  7.       "command": "make",
    5 {( P" L" i  M) k, n
  8.       "args": ["NTHREADS=1", "TAG=dbg"],+ ^+ D9 s: y5 n" l

  9. $ [9 i1 l1 ^! O" N

  10.   _; D1 r) P! G# h5 ?( B
  11.       "group": {
    $ Y" z# |; h' W6 T  F1 S
  12.         "kind": "build",
    5 Q; {5 z: D6 ~6 w7 L$ E
  13.         "isDefault": true5 p) Y! A/ U4 A1 Y
  14.       },
    ' R9 t8 k) ^/ m' T" W
  15.       // "detail": "compiler: /usr/bin/g++"
    , e7 j2 e8 b) m# Y8 m- G
  16.     }# ?+ v) X! K/ ]: U* h
  17. + m1 S' v1 {; k+ a1 v
  18. }
复制代码

% Q( s! k; j1 T一种可行的Makefile文件内容(gcc后添加-g命令)8 z% f; ^: K* q2 c6 V
  1. main: main.o input.o calcu.o7 q* }% `1 U0 G$ f6 y2 X) `+ k
  2.   gcc -o main1 main.o input.o calcu.o
    : T0 u4 W1 U( t7 f/ a
  3. main.o: main.c' c1 a( Y" U' T8 U6 {% P" c/ u
  4.   gcc -c -g main.c/ a  b$ K; l  N; Z: R
  5. input.o: input.c
    1 o: f" N; J0 j: ^7 ?3 @7 F
  6.   gcc -c -g input.c/ p" B& Y. W* U) C# L  S& S8 r
  7. calcu.o: calcu.c1 e  m/ a$ o8 a* z
  8.   gcc -c -g calcu.c. d  s2 H7 S$ Q; C7 Q
  9. : M1 G: w& k; c3 U% P$ Y
  10.   s% U0 V0 |  b. c
  11. clean:: F# A% s+ G, J
  12.   rm *.o. C5 |) f9 F3 ]8 q! L6 U
  13.   rm main
复制代码

8 r) D% r3 j# h: o$ M/ X9 m) I- {4.2、生成launch.json文件
, B( L! i: p% A( D- X% V3 {(注意只有在.c文件中才会有下图中右上角的图标显示)
# {% h' q! e5 A* k1 Y0 b1 e* b
/ a3 }! w- T: a" _8 P
微信图片_20240615144421.png # f8 a7 u1 g8 t/ H
3 ~# B, R0 x* L, t
微信图片_20240615144417.png " n0 W+ G2 U" j/ D5 x
图4.2.1 添加调试配置文件
" L# y: o& v- I  c* G( w! Z$ t: R
  1. {
    1 j% F# ]( G% ?7 C
  2.     "configurations": [
    * P9 d; ]$ |4 A2 g# s; K
  3.         {
    ' I5 C' q) M# d: L
  4.             "name": "C/C++: gcc build and debug active file",6 D. x' F8 R- z4 F* |" u, L
  5.             "type": "cppdbg",& J, @  l& d  K' A
  6.             "request": "launch",
    2 `/ e' P7 f, G4 t7 F, b6 m
  7.             "program": "${fileDirname}/${fileBasenameNoExtension}",; j/ c1 U; l2 x
  8.             "args": [],# l5 E& Q& K. B9 @3 }- L4 K0 ?
  9.             "stopAtEntry": false,
    3 J1 }. m" Z8 D
  10.             "cwd": "${fileDirname}",. J, C( r* T5 ^* o4 [- q- J. b
  11.             "environment": [],
    ) K0 ^: _% \7 s2 ~, t0 S8 S
  12.             "externalConsole": false,, i$ T5 g( r7 V+ L) ^2 \
  13.             "MIMode": "gdb",7 Q, m2 m- k. A1 M6 \" t
  14.             "setupCommands": [* G7 c! E- Y# f7 X! P$ Z/ s
  15.                 {
    / l  P2 K; Y/ @8 H; M  n" i3 W
  16.                     "description": "Enable pretty-printing for gdb",3 H) @7 E3 Q9 U& ~* e
  17.                     "text": "-enable-pretty-printing",
    5 v. s' J7 V' B* Y) v' e, G) o
  18.                     "ignoreFailures": true- f; i+ X9 _) j- Y% |% }6 z
  19.                 },1 a. U8 y. q7 y; z, W
  20.                 {
    ( i( ?( M- o- C' K1 h: y# v) n
  21.                     "description": "Set Disassembly Flavor to Intel",
    ( A: c: B/ r! G# t% ]# M$ k9 s
  22.                     "text": "-gdb-set disassembly-flavor intel",
      {% E% ]+ w! K8 a
  23.                     "ignoreFailures": true
    & `; B( Q& m8 M5 `: b: ^" c
  24.                 }
    * ?7 @; A) v: E+ M1 O& w
  25.             ],1 k1 b/ {3 N3 u2 V
  26.             "preLaunchTask": "C/C++: gcc build active file",7 S% y; m7 }3 S3 p3 ]9 ]% \, P1 c3 i
  27.             "miDebuggerPath": "/usr/bin/gdb") Z5 e* W4 r9 C4 E8 s) _6 T
  28.         }. e, w  k  b5 u$ ^3 S
  29.     ],
    0 n$ \0 {4 E+ i/ w7 d
  30.     "version": "2.0.0"
    7 e! j/ H) w! r
  31. }
复制代码

/ D1 ]5 l, Q$ H% _! |) O; O修改生成的launch.json文件如下
, E2 k* q8 K# s3 }
  1. {7 T( E! X( a: }* M
  2.     "configurations": [: Y: p& u) F0 J- l/ n
  3.         {
    1 L+ ?* P6 ^) `7 C) g
  4.             "name": "C/C++: g++ build and debug active file",
    , Q/ O3 y6 e" P# G. S& }% V, H& s# n
  5.             "type": "cppdbg",. W% Y' L. e7 t7 a
  6.             "request": "launch",: Y1 ], \+ B" D8 }; M% U
  7.             "program": "${workspaceRoot}/a.out",
    . h6 Q" ^' e6 W
  8.             "args": [ "1232"],
    ) m$ Z0 f% {# H- }# a8 m
  9.             "stopAtEntry": false,; N/ K" O$ u2 \# Y: Z' u
  10.             "cwd": "${workspaceRoot}",
    6 A- h- R* f0 H- |( b& y0 b$ f  E( z
  11.             "environment": [],
    ! C; z( _1 w# G2 m7 y
  12.             "externalConsole": false,1 |1 u' W$ K( q! z: j
  13.             "MIMode": "gdb",4 x* f# ^! f. O
  14.             "setupCommands": [
    ' \# H: D- J, _$ A! x
  15.                 {3 Z0 h2 h) a! [7 @5 z0 H- j' ~
  16.                     "description": "Enable pretty-printing for gdb",1 K6 x( c& g% q% ~  {' y# V+ o0 ?
  17.                     "text": "-enable-pretty-printing",1 k' L; i* z/ j
  18.                     "ignoreFailures": true
    " [4 u: C  b) ~2 T3 p
  19.                 },6 v( p0 U2 k2 I% [$ u
  20.                 {
    6 \* j9 [$ j# m% `
  21.                     "description": "Set Disassembly Flavor to Intel",: B! [, L7 E, m
  22.                     "text": "-gdb-set disassembly-flavor intel",
    & A3 U5 ^% A% ^4 O: Q7 ^6 W1 |- i
  23.                     "ignoreFailures": true5 M6 U4 o1 S" c$ S/ ?# x
  24.                 }
    0 E% v# T9 E3 c7 r
  25.             ],
    & W, c7 X4 U6 ~. s/ W  i& c
  26.             "preLaunchTask": "Build",4 q- d& W. ]8 H/ _9 K( s. M( \
  27.             "miDebuggerPath": "/usr/bin/gdb"
    & o0 b' l" @4 ?- v
  28.         }9 t: c6 K! Y6 d0 N1 `
  29.     ],4 O" v& T. a# h# D: o) I
  30.     "version": "2.0.0"1 x' W0 W, Z- P. T
  31. }
复制代码
8 G" K( g6 M; s
"program"选项代表,需要进行调试的程序,所以这里需要修改为对应的文件。"PreLaunchTask" 代表debug流程的前序任务,这里的内容需要和tasks.json中的label相同。"args"可以添加执行程序的过程中,需要添加的参数。在下面的代码中,我们就添加了cacti程序,以及参数。
0 g1 x& E9 h& C( N2 x( f2 l

  D. Q" R( `) W3 j" d" y9 _4.3、生成c_cpp_properties.json文件9 \5 m0 M3 z. i/ l/ c
ctrl+shift+p 搜索C/C++:Edit configurations(UI) 即可生成该文件
) S: w) G+ I/ M7 K* g/ d  H! j- T
  1. {
    2 x& }; _+ G; s6 x! B: I  A
  2.   "configurations": [
    ; z0 M/ a) e! {) M7 S$ Q& s3 v. K
  3.     {0 f* r( h' E0 l
  4.       "name": "Linux",
    & M; l( x' z  [* x7 T; {1 B6 d
  5.       "includePath": ["${workspaceFolder}/**"],5 l+ n6 Y  s' }+ X. u  E
  6.       "defines": [],
    $ Z8 q* V5 j0 X; B5 T
  7.       "compilerPath": "/usr/bin/gcc",0 C& f9 x- l' b
  8.       "cStandard": "c11",
    & Z# W) P0 ^. h- L" T
  9.       "cppStandard": "c++17",
    3 g( o( H0 K' g( k/ h# o5 \! @
  10.       "intelliSenseMode": "clang-x64"
    : P+ _; e& l2 d4 ^9 A
  11.     }
    : w1 O7 A6 m7 W1 U2 Y# H
  12.   ],+ |  O' C2 `" r" a
  13.   "version": 47 u6 M0 S) }$ s5 w6 L, P
  14. }
复制代码

$ i7 E: o  x  D* u05安装Vim
" x7 p: Z& z2 ?# h, F1、命令行安装
, r! `) T/ q2 L5 h1 W/ ^$ F: P0 x
  1. chen@ubuntu:~$ sudo apt-get install vim* j1 x, B. ]3 e5 \4 Q7 n, j0 D
  2. chen@ubuntu:~$ touch a.txt% h  n1 N/ K7 {( X# b* K
  3. chen@ubuntu:~$ sudo vi a.txt
复制代码

7 w6 _5 S4 p3 o: U3 s8 I06安装gcc-arm-none-eabi
$ a; M9 u, U$ d" f1、下载嵌入式gcc编译器: f: O, T1 r) a
https://developer.arm.com/downloads/-/gnu-rm
+ \: h" i$ M2 _8 Q9 ~6 X6 [+ V; H+ ~% n
微信图片_20240615144414.png
4 k" O9 }, W, i# J
& F' o* f0 o7 G7 ]4 f4 [+ w2、解压gcc-arm-none-eabi-10.3-2021.10& Z+ s' z3 A( k0 m/ R! H4 g
  1. chen@ubuntu:~$ tar -vxjf gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2
    9 _2 h* A  Q, P# B) P: [& \0 M1 |
  2. #对于bz2、gz文件解压7 }- X6 p! E2 V, J
  3. tar -vxjf test1.tar.bz22 J7 H! h" D3 ?' T8 T5 G
  4. tar -vxzf test2.tar.gz
    $ V: K7 ^1 b5 Z" i% T+ S, n1 f. _
  5. #对于bz2、gz文件压缩
      [# |: b. I) F
  6. tar -vxjf test1.tar.bz2& R& i/ m1 \" L2 s3 _, o; b
  7. tar -vxzf test2.tar.gz
复制代码
* X7 }) j0 Y+ _
3、添加gcc-arm-none-eabi下的可执行文件命令到Terminal(终端)) D  }9 _5 b7 V5 A) I
  1. chen@ubuntu:~$ sudo gedit .bashrc" \/ j1 ?! w1 b  m+ `& c; c* Z
  2. #在文末添加下面一行9 \3 g8 z2 L" ^% Y8 s) ^6 `
  3. export PATH=$PATH:~/gcc-arm-none-eabi-10.3-2021.10/bin
    3 e5 V& k3 l, I0 ]9 C0 o  _: A9 T
  4. #添加上述行
    # Z5 ?: [; R5 B
  5. chen@ubuntu:~$ source ~/.bashrc
    ' I; _+ h# I6 V8 F4 C
  6. chen@ubuntu:~$ arm-none-eabi-gcc --help
    * ]# k0 N( B/ T  I4 ?( C9 `/ I  I- K
  7. chen@ubuntu:~$ arm-none-eabi-gcc -v
复制代码
/ Y; \& k' u0 w, A% S
07安装stm32cubemx
" F$ I6 @/ ^" \8 }" E, b2 y1、下载stm32cubemx
, `: s; P- A, n* z- u6 Ehttps://www.st.com/zh/development-tools/stm32cubemx.html
. v' c* z& s. E: o
4 N+ M7 A$ {. N+ w" B
微信图片_20240615144411.png 0 |. w) _* ~  C* W

* }  C7 o* E, w$ A1 X) K2 _2、安装
  1. chen@ubuntu:~/Downloads$ unzip en.stm32cubemx-lin-v6-11-1.zip, e7 x7 [& M% g5 z* S. y* S8 J1 V
  2. chen@ubuntu:~/Downloads$ sudo chmod 777 SetupSTM32CubeMX-6.11.1
    / `( E- j( F: @# ^
  3. chen@ubuntu:~/Downloads$ ls -l
    * z0 ~/ o' S( z% B
  4. -rwxrwxrwx 1 chen chen 539136342 Apr 11 09:41 SetupSTM32CubeMX-6.11.1
    # H( d) `0 s- T  R" }8 h, K3 {
  5. chen@ubuntu:~/Downloads$8 _# x% V  O* D7 @
  6. chen@ubuntu:~/Downloads$ sudo ./SetupSTM32CubeMX-6.11.1
    ) p6 }6 H/ d  e& |
  7. chen@ubuntu:~/Downloads$ find /usr -name STM32CubeMX
    * _. T  S" R* E' w' a# W
  8. chen@ubuntu:/usr/local/STMicroelectronics/STM32Cube/STM32CubeMX$ ./STM32CubeMX
复制代码
. B9 p! ], `6 C$ l! @" `; ^/ M
3、增加图标到菜单! o- e8 x6 w0 _; d& O8 T
  1. chen@ubuntu:~$ /usr/share/applications/
    ' T( G4 W0 A5 L" x
  2. chen@ubuntu:/usr/share/applications$ sudo touch stm32cubemx.desktop
    2 c+ e. N+ d+ h2 v; C! ]
  3. chen@ubuntu:/usr/share/applications$ sudo gedit stm32cubemx.desktop( l( H2 C) Y% K' z* I- O% s& Q
  4. chen@ubuntu:/usr/share/applications$ cat stm32cubemx.desktop
    ( Y6 R/ G5 u' j# f3 G' ?
  5. [Desktop Entry]; N4 Z% D9 @6 N8 W  v4 {$ h2 a
  6. Type=Application
    ; |% U" D) [8 w0 }( E
  7. Name=STM32CubeMX
    5 a' i  N, H0 k4 P
  8. Comment=STM32 Development Environment
    3 u; K7 k/ r& ~' w" Z2 [* i
  9. Encoding=UTF-8
    3 ]: n( Y: S6 E+ W* z) T
  10. StartupNotify=true+ n) s& L+ N6 ]1 P1 O/ E2 f; y
  11. Terminal=false$ o/ [- c2 k: w% i8 N
  12. Categories=Development;java;c;c++;
    " {! E  g' ], E
  13. #icon= ##########图标的路径,自己找一个就行" U+ K0 d) Z8 O* U
  14. Exec= /usr/local/STMicroelectronics/STM32Cube/STM32CubeMX/STM32CubeMX" z$ E/ I# ^! \
  15. chen@ubuntu:/usr/share/applications$
复制代码

; D" W( F, p' f) s08使用stm32cubemx生成初始代码
" r; L" z3 u1 E8 R1 m$ {1、选择芯片->配置->Generate : Y6 f) u$ F" j4 s/ k; i

- J0 {9 m% H+ B$ h
微信图片_20240615144405.png % s6 O, \$ Q. a5 n. L1 [
/ Q/ o1 X! |7 Q+ m7 |  j; Z- U
2、一定要记得勾选PinOut-Debug-SerialWire否则后面下载程序时会报错Error: init mode failed (unable to connect to the target) 。这个是巨坑,一定要小心!!!!!!!!!!7 O3 g; t% D* i8 x

2 j- I- q% b, b5 i; q( a$ a
微信图片_20240615144355.png
: ^7 l% L  ?$ ?; l
/ y9 J) n- ~6 g( w8 v* S+ M
09配置VSCode
9 r* _* @$ D' n/ a! Y+ a6 W1、使用vscode打开上述生成的初始代码) G& x5 q! I0 y! L
2、生成VSCode配置文件" O& X& P' K& w
2.1 c_cpp_properties.jason
- R& g; F2 v; p4 c& [( u4 `ctrl+shift+p->Edit Configurations(JSON)生成.vscode/c_cpp_properties.json文件) [; I+ G' M3 N& w+ J8 Y

1 l) W" c8 c+ D7 R! {
微信图片_20240615144351.png
; |, T! ]) t; a! t% ?
  1. {2 I  q  O  A( U- Q# z
  2.     "configurations": [$ j7 y1 ~# a* d2 ^+ D  ~
  3.         {, S# A, I6 N' R, V+ p/ ^, D, I
  4.             "name": "Linux",
    + C/ D$ \3 B0 l: o& K/ |
  5.             "includePath": [
    3 z9 ?: V- U& @0 ~, B! d
  6.                 "${workspaceFolder}/**"
    ) U2 G0 _3 }" d3 `. v+ J
  7.             ],* |2 T) Z; k9 U+ }
  8.             "defines": [
    1 [5 Y2 `& ~& D
  9.                 "USE_HAL_DRIVER",2 P9 U$ H; B& G" V; i
  10.                 "STM32F103xB"
    ( U& Z8 H* b, v# A
  11.             ],8 j4 @3 x! V: M  j, ^- w% ?
  12.             "compilerPath": "/home/chen/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc",* b2 W$ E7 [" K1 [# i2 |2 g( a  k
  13.             "cStandard": "c99",
    % K, A+ q3 _: }* J7 |' H
  14.             "cppStandard": "gnu++14",
    1 I: j" v9 l# h7 r0 A
  15.             "intelliSenseMode": "gcc-arm"
      q) k1 y1 N& b/ b! z+ z
  16.         }) B! _* t; U$ z1 _6 q7 {
  17.     ],
    : a6 l- K5 Z4 W+ R+ w) k
  18.     "version": 4
    ! m7 A- {8 `0 o$ ~+ @, p
  19. }
复制代码

# i: _  V9 X. c( k% j4 }3 ~2.2 task.json/ X( s) v+ t' {; J% F
修改好task.json即可使用ctrl+shift+B对stm32工程进行编译% j! X& V- |0 @
" ~$ Y) r2 s( g$ ?! p7 e
微信图片_20240615144348.png
9 D# X( B9 z+ m5 M6 h. \& V0 e0 l+ U6 x+ H& X& _) u6 G
  A$ Z' O* I" Y2 U! q9 X- w" g
微信图片_20240615144344.png " u% Z( M4 S/ X' n* x( N8 H4 y8 g! t

) M3 F+ d& S6 o3 H  U5 v0 H4 t' ^4 m+ J8 E
微信图片_20240615144341.png
6 C1 Z5 L* I* b$ {) l7 |
4 _, Y0 j' v: f. p3 A, i, V
" x' c/ W! Q+ t修改如下:
6 p3 s! r+ l1 \, J
  1. {
    # q$ i6 n3 E  B& {! f% v
  2.     // See https://go.microsoft.com/fwlink/?LinkId=733558
    1 F0 R5 p/ @0 T5 b* R( l0 ?" K
  3.     // for the documentation about the tasks.json format
    * e# V% P4 x, B# h. k; Y- s
  4.     "version": "2.0.0",
    $ z- e% s7 O/ o2 R3 A! R$ U9 {) f
  5.     "tasks": [
    : Y% H) w1 j; e
  6.         {" u5 Z- Z8 @! Q1 Z
  7.           "label": "Build",      // 标签名+ _$ n, l7 z+ x
  8.           "type": "shell",      // 终端(shell)型的任务1 d2 [( m. ^) Q7 p  y! n. W
  9.           "command": "make",    // *执行的命令
    5 m8 \1 |6 r6 C7 g5 y$ e
  10.           "args": [          // 命令的参数
    ; h% i# u# N  C8 T# m
  11.             "-j6"          // 表示六线程同时编译3 b- i: I& n. H  d3 X5 X
  12.           ],
    3 K8 G$ g# ~0 i6 j+ q! S
  13.           "problemMatcher": ["$gcc"],  // 表示如果出现了编译报错,问题面板会显示 gcc类型的错误7 f6 e' E$ c7 m, E, R! j
  14.           "group": {
    ' o, Z- @: p4 u+ E
  15.             "kind": "build",, \5 Y) x; @5 b9 I" z" D' `0 l
  16.             "isDefault": true
    + i7 I, j4 `. F+ l* `
  17.           }; ^$ w$ j4 Y; R9 F4 e5 n" w
  18.         }0 ^* i! `% P1 ^; M  L" q) c$ L
  19. 4 n8 `- P  D* I
  20. }
复制代码
1 Y  t& E6 ~9 I1 _2 k4 I
2.3 settings.json% e5 V& m5 Y! k+ }& d! [0 \

  S: B" @( V$ y. o6 |3、上述”defines“字段中的定义应包括stm32cubemx生成的Makefile文件中相关的宏定义6 K# H, b7 o9 c, k* v
/ d8 q" A) i. |  {5 \
微信图片_20240615144337.png
! M# L* t2 h* m
! L( j& m- r8 a4、make编译/使用ctrl+shift+B, R5 d/ }2 A- U. V; t4 F4 @
  1. chen@ubuntu:~/prj_vscode/linux-stm32/test$ make clean
    : [# U5 w% b7 n, D2 c8 u2 v
  2. chen@ubuntu:~/prj_vscode/linux-stm32/test$ make
复制代码
3 p7 ]5 Q6 x1 q: f4 ^
10调试
8 l$ B" c, P0 A9 X( m" a1、安装openocd
  1. chen@ubuntu:~$ sudo apt-get install openocd ' O9 h* Z. q1 g& c( s3 W
  2. chen@ubuntu:~$ whereis openocd
    ! [( Z8 W% x5 l0 d. f
  3. openocd: , Q: R) [4 a$ T' R+ j
  4. /usr/bin/openocd
    8 G1 O% g. {4 `* t2 x2 w/ L. s5 k
  5. /usr/share/openocd ! w! V  T$ ?% {4 R9 u5 l1 {
  6. /usr/share/man/man1/openocd.1.gz 5 k! f$ W+ c( G! l2 A  Y4 g" l" G
  7. /usr/share/info/openocd.info-2.gz 8 v  C$ K1 j3 s- n9 G  ~
  8. /usr/share/info/openocd.info.gz
    7 n- s/ z/ C5 E
  9. /usr/share/info/openocd.info-1.gz
    - a+ Q+ [2 I6 T0 \5 d
  10. chen@ubuntu:~$
    # M8 C6 @6 U$ G0 h& P$ z. x
  11. //连接好stlinkv2-1到电脑和pcb板,输入以下命令。完成连接
    & Z2 |+ v* ~/ A
  12. chen@ubuntu:/usr/share/openocd/scripts$ openocd -f interface/stlink-v2.cfg -f target/stm32f1x_stlink.cfg
复制代码

8 L* r- n7 @# c  F( R2、在tasks.json中使用openocd进行编译后的下载3 L0 |- H4 I, o& ~% m! }: Y1 _
- m3 K0 D0 z# M0 F4 D
在原本的tasks.json中追加popenocd的相关内容
$ c7 l7 W  a' D, r0 S4 q
  1. {4 z: s9 J; D& X; [' r  U$ ^. C$ ]
  2.     // See https://go.microsoft.com/fwlink/?LinkId=733558
    , n5 p/ a! _$ I- K. t
  3.     // for the documentation about the tasks.json format! f$ U* V: Y' M4 L- @9 h
  4.     "version": "2.0.0",4 H4 H: O7 Y! u& X
  5.     "tasks": [' P! J: R6 u1 ]" ~/ f
  6.         {
    * a) L' |* P7 a+ s5 R
  7.           "label": "Build",      // 标签名3 n1 W% R' r5 y1 B1 x) X
  8.           "type": "shell",      // 终端(shell)型的任务
    : ]3 W. ^1 M9 n) v
  9.           "command": "make",    // *执行的命令
    7 V5 i8 _1 c( L! }& a
  10.           "args": [          // 命令的参数
    1 v  N- }7 x3 }8 Z5 t( z
  11.                           // "-j6"表示六线程同时编译6 {2 u( i8 H6 E% C1 z
  12.           ],
    6 l6 `; \5 [1 `1 c. l$ V7 P( L
  13.           //"problemMatcher": ["$gcc"],  // 表示如果出现了编译报错,问题面板会显示 gcc类型的错误
    % Z  M9 T' P* a  g; m
  14.           "group": {" A6 Y7 S  g, X3 u: G% w
  15.             "kind": "build",8 \6 D* t) K  c: ]- Q$ K
  16.             "isDefault": true3 K; y# l7 g& @/ l. s# B
  17.           }
    + `5 l  E8 A0 b- B% V% G1 o
  18.         },5 B5 P! Q" v0 P0 u7 q
  19.         {
    3 b6 R; n( T! t" \" k
  20.             "dependsOn":"Build",          // 任务依赖
    $ F& K: S* @- }
  21.             "label": "Build and Download",
    - A/ ]" [2 o: h3 Q1 a4 Q
  22.             "type": "shell",' f0 e/ k$ p( x$ Y6 F  H
  23.             "command": "openocd",
    ) [8 H; [8 o/ W" {! W, g7 ^
  24.             "args": [! K/ f) X5 F0 G3 J
  25.             "-f",: q) t  g$ N8 T* }0 X# y3 J
  26.             "/usr/share/openocd/scripts/interface/stlink-v2.cfg",
    ! F' T2 ~  g1 Q
  27.             "-f",7 i3 ]4 e# q% Q- F4 R. L; k& `9 T
  28.             "/usr/share/openocd/scripts/target/stm32f1x_stlink.cfg",
    9 g% A2 S# N: O/ T
  29.             "-c",* A$ p/ C4 I' f4 u1 v  j
  30.             "program ./build/${workspaceFolderBasename}.elf verify reset exit"
    4 i  Y7 d$ \" u6 V: g6 N2 j
  31.             ],% h; ]9 [" q4 Z+ L# `4 G
  32.             "problemMatcher": []
    . s5 H9 Y% y* @0 E& F7 j- ^0 l
  33.         }
    ; }$ I5 C0 T$ y9 Q- q! r
  34.         % e4 n5 |% ~) A2 k7 q! U( t: K

  35. # z- ?2 }' h5 C* n1 ]/ p" H  H- a
  36. }
复制代码
, P) c: b5 H7 @: F4 B0 o
这个任务要求先进行 Build任务,然后通过命令打开 openocd,加载两个配置文件(-f interface/stlink.cfg 和 -f target/stm32l1.cfg),同时,这个 -c 的选项后面跟着的就是openocd成功加载后执行的内置命令,这里我们让它下载、验证、reset,然后直接退出,这样就实现了一条龙式的下载。
  U3 C2 s* Z$ T) R
5 u9 U( Z: i6 V9 G3 M4 V: [
微信图片_20240615144333.png
3 t0 A6 c) N9 `4 a: n3 Y7 R
7 [/ t$ B" }& Y 微信图片_20240615144330.png # E5 x9 n' `1 s1 s7 F
! d) B( V2 f6 C" h+ I
3、使用openocd进行调试
! u5 ~3 E' B! x/ w8 E, @3.1 安装Cortex Debug和Venus's Cortex-Debug插件(第二个插件一定要安装,否则会报错Failed to launch OpenOCD GDB Server: Timeout.)/ \! f5 J7 {8 q& }5 k( Y
+ `2 t) R( h8 d" B" w0 L! i8 s
微信图片_20240615144327.png
9 K; l. D7 w9 L# s4 D6 r, I1 b: {% w. q. f: w
微信图片_20240615144324.png : ]# f, C1 W' n3 k& z6 [% W
9 Q. {# E5 f) E
3.2、 添加调试配置5 T& O4 ^' I( a6 f; n4 z  z& |

3 @2 T0 D5 _, \
微信图片_20240615144321.png
3 f" x+ E2 o, Q2 o8 U. V
# z( n. C* e) [! Q- G% J
3.3 选择cortex debug,生成launch.json,并修改内容如下9 i, `% @" j4 U% D1 g, r& o* O# F
3 E6 V- E  |2 R- v; p; }, B
微信图片_20240615144318.png * Y* i0 }2 D2 M9 s8 r% m
" _7 ?2 y2 O2 `
  1. {
    : {) q% T( @$ p1 ?* r% q! }( X
  2.     // 使用 IntelliSense 了解相关属性。( l- r# S* y1 j0 w6 J7 ?
  3.     // 悬停以查看现有属性的描述。9 t* t4 o. n# m/ o& @0 w
  4.     // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    * s* c0 B: e( y: P; J  Z1 V4 u" Y
  5.     "version": "0.2.0",$ u  q) J% V* o( C! o
  6.     "configurations": [; w2 g- _( a( B+ p
  7.         {( j8 Y' M2 [2 @1 `) Y- f: Z
  8.             "name": "Cortex Debug",
    ! J$ a* i( j# J
  9.             "cwd": "${workspaceRoot}",  // 输出路径4 E) C) j7 E4 [" K
  10.             "executable": "./build/${workspaceRootFolderName}.elf",    // 要调试的程序
    : L  g  u9 p6 q$ T/ a) f9 |
  11.             "request": "launch",( F: S, \6 L% _8 r/ `
  12.             "type": "cortex-debug",' \& Q# k7 K$ J# j
  13.             "servertype": "openocd",    // 调试器选择% z. o% r% T/ S1 @8 @5 E& ?
  14.             "device": "STM32F103C8",    // 使用J-link GDB Server时必须;其他GBD Server时可选(有可能帮助自动选择 SVD文件)。支持的设备见 https://www.segger.com/downloads/supported-devices.php
    $ P# r0 p$ F; H* `! M- q$ D6 d9 ?4 W
  15.             //"svdFile": "./STM32L15xC.svd",  // svd文件,有这个文件才能查看寄存器的值,每个单片机都不同。可以在以下地址找到 https://github.com/posborne/cmsis-svd下载
    ! i7 X0 j( U( d+ a- p$ A" `0 y) `* Y
  16.             "interface": "swd",, y  N5 U" G; I. \+ r
  17.             "configFiles": [
    $ A# `( ]% V8 e. l1 l/ P& S
  18.                 //"${workspaceRoot}/openocd.cfg",  // *可以通过加载该文件来配置! y# @- `! ]0 \! o+ P; f9 X
  19.                 "/usr/share/openocd/scripts/interface/stlink-v2.cfg",+ n- a2 \6 A" ]# F/ T; i
  20.                 "/usr/share/openocd/scripts/target/stm32f1x.cfg"
    # a; j5 Y$ [" @( c9 H
  21.             ],& ]8 T1 S# m0 P8 {* z
  22.             "runToEntryPoint": "false",
    $ m- R" I$ M/ e
  23.             //"runToMain": false,
    6 u2 p3 U* R! F7 {( l: ~
  24.             "preLaunchTask": "Build",   // 在调试前预先执行的任务,此处是tasks.json中的
    ! m2 R7 H. m, H3 z
  25.             //"armToolchainPath": "C:\\Program Files (x86)\\GNU Tools ARM Embedded\\5.4 2016q3\\bin"    // 如果没有把 arm工具链路径添加到系统环境变量,则需要这条指令添加你的路径, r$ ]: c0 B$ l' U0 V$ V
  26.         }* e1 c% e) {( z/ ?
  27. " t, C7 Z7 A* o5 X5 h- z9 Z
  28. }
复制代码

% N$ W# J6 N# Q3.4 调试  m6 R4 Y9 i! k! z1 d, ~
; W1 v: _) T0 v: ]
微信图片_20240615144314.png & Z& F! z' B- C' H0 L" \1 W+ S

8 Q- D- z8 K+ \11使用VSCode串口工具

( `( v, ]) Z& g$ E) F0 ?) w) Q1、安装serial monitor
' T" V0 J4 H6 Z/ b; F. S& k6 R# O" c3 N) ^7 _) m
微信图片_20240615144304.png
! J0 E; P' ~% d
' ~+ L, n: j1 ]2、查看连接的usb/ }" p; I: C  T; n
  1. chen@ubuntu:~$ lsusb
    . X: \3 }' Z: f1 @% {
  2. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    / G/ B: y# \2 b
  3. Bus 002 Device 007: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter, _+ A4 l1 Y9 N: b. u8 v0 N9 f9 t9 y6 k
  4. Bus 002 Device 006: ID 0483:3748 STMicroelectronics ST-LINK/V2
    ! K$ L/ S9 d4 _) A3 i2 Y6 o+ T
  5. Bus 002 Device 004: ID 0e0f:0008 VMware, Inc. VMware Virtual USB Mouse$ V( P2 E7 [$ J
  6. Bus 002 Device 003: ID 0e0f:0002 VMware, Inc. Virtual USB Hub
    . I. l5 s7 n  }- W# `. U
  7. Bus 002 Device 002: ID 0e0f:0003 VMware, Inc. Virtual Mouse
    ( s7 M2 Q1 }: r# j
  8. Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub4 }4 @6 \2 v, e) K5 C7 h; @
  9. chen@ubuntu:~$
复制代码
* ]+ e6 ?+ Y- [7 D* o4 P! x) y$ e
2、加权限
  1. chen@ubuntu:~$ sudo chmod 777 /dev/ttyUSB0
复制代码

! l: b- z! \. T5 T, X# G12使用git# Z8 F  p& \! e
1、检查状态
6 c  Z! s- y. F7 V, @/ T. m
  1. //在执行 git add 和 git commit 之前,先执行以下命令查看当前仓库的状态:9 z& E8 k3 k5 [: D/ w! U. f1 R5 A# _
  2. git status
    ; t/ b' n0 n- G$ Z6 K
  3. //这将显示工作目录和暂存区的状态。确保没有未追踪的文件,也确保已将需要提交的文件添加到暂存区。
复制代码

4 x7 N0 a' i. S) y8 L8 y2、确认 Git 配置:+ d$ T  r' c, D2 `* w
  1. git config user.name
    ) t( |; ], z0 _$ _7 b  M7 {
  2. git config user.email7 y' W# a# k1 p$ W: H* G3 ?

  3. 7 K4 Z* [0 c5 u, D9 K1 m; ^! s
  4. 6 `1 o( C$ o6 L5 e: t. m
  5. 如果没有设置,请使用以下命令设置:
    9 d9 w1 J3 u, U8 J
  6. git config --global user.name "Your Name"& H0 H( f: t3 V, c: I
  7. git config --global user.email "your.email@example.com"
复制代码
. T, d, @; `& \, q" K( `  C! \* w9 j2 y
3、检查文件权限
( F- J4 t6 X1 w5 S! Z4 J" h; O确保你有权限在新文件夹中进行写操作。使用 ls -l 命令检查文件和目录的权限:
! @! v- k- q6 \: x" F6 k- u4 k8 tls -l
8 {* Y+ p8 z/ w7 s' \! F% c9 _; O5 {) N- e0 U
4、查看错误信息:
4 s6 n( c. r9 ~: Q  _如果 git commit 操作没有反应,尝试使用 -v 参数查看详细的错误信息:
- b& d& T' r" y7 d' e$ C& Ugit commit -v7 \/ x. w2 G! K8 z2 ]
! p, W- T% }- N; C
5、清理缓存:
7 G% b. _  b! Y有时 Git 的内部缓存可能导致问题。尝试清理 Git 缓存并重新尝试:
9 X0 X' \: f$ X/ T; b0 N# Xgit rm -r --cached .& }0 a4 i: I& s9 I( O* T  E
git add .
9 H  _5 ^! s) r: D+ Ugit commit -m "Your commit message"7 J3 w- T5 T0 L, i' B
这将清理缓存并重新添加和提交所有更改。! t4 G3 {& v! [) `; u9 v

4 l+ E" n4 q8 X+ O3 d; _  [( Q5 F6 o
6 [5 w( B8 V3 `8 }$ f
转载自:kevin. d5 e6 ^1 H8 B: G) @9 M0 t
5 E# S1 z) K0 u/ Z/ O" h
如有侵权请联系删除; _9 j6 w& e8 Y( c& z6 O5 ^

' F1 O& s1 E6 y( G  j  S, Q+ J
2 j" O) d; x* m7 p* \
: P  @3 H1 m, T& c/ R- q
1.png
收藏 评论0 发布时间:2024-6-15 14:45

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版