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

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

[复制链接]
攻城狮Melo 发布时间:2024-6-15 14:45
“ 从ubuntu20.04入门到完成stm32程序的调试。”
$ C& A3 C. L% |; I) f0 G
1 e, U7 l  E* q4 J9 u3 r5 _  X
微信图片_20240615144535.png ( Q$ u8 P" T3 L# F3 N. k7 K, q; b- w
" m/ d8 \; w( n3 Q
1、更换软件源
# O' N7 n! ]# b0 [9 `6 M' H  e源文件(sources.list). ?2 z3 r- x; F7 t" F+ l8 Y# }. s4 n  I# ~
更改源(Ubuntu20.04对应的源)
2 u! ^- ]5 w2 i  ~更新源+ f% X3 ~7 L" c

' G; T2 ~/ d" y  o& i2、安装拼音输入法" O* D, t# v' ^* w  C
Region&Language->Language Support->Installed Languages
( I! d* m$ p8 o4 s6 Q& wInput Sources->"+"->chinese->Hanyu Pinyin
7 H5 L. P/ U# S. ~7 i- c  a- I7 \  ]& F
3、安装VSCode
. L* B; ?/ _  h/ ^3 s) n  |下载.deb程序包
7 x. D5 p, K: x5 b使用dpkg安装! l+ L! u/ ]& ]9 E4 j7 E
调整设置
8 p) ?: p0 s; h: v) E启用Title Bar! H, P! j8 K5 r3 X
安装插件7 Y0 j8 f& {2 l- @8 _8 D

# x& p! U! ~& _4 L
4、安装Build Essential及gdb3 o! ?! \! l/ `' q5 }
build essential介绍
' P- _8 U' C# X- \* a命令行安装build essential和gdb
3 |" |# ~) _( ]7 xLinux C程序开发简介9 K5 X& E! q' t
无makefile& ~& [  [( Q; \# \
有makefile6 R9 U5 [9 t! }6 H9 r( S* b- E. D
生成.vscode文件下的debug配置文件% X" e. m7 ]: e* M  B& v
生成task.json文件* W( s' k* ~! L  w$ y
生成launch.json文件3 a0 E. i' K7 C" m  I" X3 g
生成c_cpp_properties.json文件  W* L2 c9 g1 `
. U6 `) r8 r  r2 @! n- u( D4 b/ R5 u
5、安装Vim; q. s2 B2 n9 ~% D2 D5 r

# Q+ l; R# B  m/ c* D6、安装gcc-arm-none-eabi
" b1 E" C5 i# Y, ]
下载嵌入式gcc编译器
- e7 P; Z- S0 J& t; E& L8 m2 \+ O解压
6 a. t" R" H2 ]" r添加gcc-arm-none-eabi下的可执行文件命令到Terminal- l# q% O6 D' I. t% [+ H4 A5 i- J
, o# a4 c3 X  v" ?0 r* m7 C2 T" n
7、安装stm32cubemx
! ]* L" p9 U+ H下载stm32cubemx
1 e+ F- g( i/ K6 b0 u安装
, Z' o' V  j4 ^增加图标到菜单
; w8 i. q, r$ I+ C
* Z5 f% c. |+ Z/ B
8、使用stm32cubemx生成初始代码
1 a# G$ C' u  x3 d+ E) K5 J选择芯片->配置->Generate$ }- s, ?% D" o9 K0 P. m
勾选PinOut-Debug-SerialWire
2 E# x. m4 \3 t+ O7 B2 z% z4 }+ I
4 x, Y& i0 P- @+ T# V1 r% S
9、配置VSCode3 X8 c8 _6 v0 S6 B& X
使用vscode打开上述生成的初始代码
% ~% x, h- I* l! B2 c9 X生成VSCode配置文件
! N" Y4 o  u* U6 p  t1 oc_cpp_properties.json
, M1 L/ a) H( N) v- O* b. Ltask.json, i( [) s* H8 O0 z. F. l
settings.json
' N# X$ @8 x: F6 U/ S$ D; S宏定义包含9 s6 A' D% D3 P/ Q1 t( n- E
make编译/使用ctrl+shift+B6 o; r, w# j; s4 ?

5 l6 ~- }( Q5 p( |" w1 R. h$ q
10、调试
& O( n: O) w8 S5 x6 J安装openocd. o, {- Z$ ~% F, ]" R/ e7 F, W6 l" Z
在tasks.json中使用openocd进行编译后的下载) K' U1 a: B% p1 I
使用openocd进行调试1 o0 w8 U- t% A. n* E, h
安装Cortex Debug和Venus's Cortex-Debug插件, s9 B' ?* L9 }1 ^+ H. w% v7 v
添加调试配置# K+ J4 G) ~. F/ s! B' x
选择cortex debug,生成launch.json- y4 H3 U% M: L; T
调试4 n$ `' f* B5 C, H8 m/ G/ ]

; ]% R( d+ @  U$ n% s
11、使用VSCode串口工具# O1 K4 O$ J. b, O! e
安装serial monitor1 I0 _. V4 s! `/ I5 k
查看连接的usb
* t( p2 T5 U6 J' a- A# V7 D# b加权限
1 _: f* T6 [1 p$ D$ e% Y7 ?) u2 |  h8 R+ q: b: x
12、使用git" l! P* K) ]# |
01更换源
4 \: m' Z9 [8 [2 a3 O% f1、源文件(sources.list)
; l! c' n3 R* h7 C5 f# p3 ^chen@ubuntu:/etc/apt$ ls -a
: j; ]( U7 P& \+ y, U.   apt.conf.d   preferences.d  sources.list    trusted.gpg.d
: A  ^: N+ x) T..  auth.conf.d  sources.bak    sources.list.d( f- K! ~; S0 Q8 p

  V  d  Z+ u5 O% x0 p' _2、更改源(Ubuntu20.04对应的源)* ?' o! E% v- H
  1. chen@ubuntu:/etc/apt$ sudo gedit sources.list
复制代码
  1. #阿里源(Ubuntu20.04)
    * b- [/ i  P" L7 i2 c& u1 p
  2. deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
    * I/ x2 `2 p& |% y2 `# o  p+ K0 d
  3. deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
    ' y( @& g% N/ w* E
  4. deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse1 B4 I9 G9 z  }3 g. E4 q
  5. deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
    5 a; r8 V2 o# z
  6. deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse( B1 }/ y: e/ N6 B2 I
  7. deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
    : E( U  n% Z& j' `) C8 Q  V
  8. # deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
    2 j& Z+ f- ~2 h5 q: u/ R) a
  9. # deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse) E* g3 z2 f+ S: J/ n
  10. deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse) Z- a% |/ F- }, I+ ~; j
  11. deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
复制代码
  1. #清华源(Ubuntu20.04)
    % }( D# Y5 d; O* d% q
  2. # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
    & n  t7 B* q) Q# e' H
  3. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse) D0 V, v8 n$ P7 t% l
  4. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
    & j6 o$ x6 X% C1 P4 l
  5. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse- }) F& H6 w. N2 U
  6. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
    # |: O2 Z7 }& _! _: ?: L
  7. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse4 J" i5 N0 P9 l3 |) Y5 {, ~0 ]
  8. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
    2 U% s/ h- U$ L
  9. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
    , F  c/ g( G6 B3 J! J$ N: X
  10. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse' ?9 @. i; w- x! Y! y  r. ~9 @; C8 S

  11.   E; b# v$ ]2 U$ y
  12. 2 h! z8 k, S4 M& P2 w
  13. # 预发布软件源,不建议启用: H# V- L. y1 e7 r& u" K. i/ B1 N! a5 c* p
  14. # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
    : R5 W' b' D1 C
  15. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
复制代码
  1. #中科大源(Ubuntu20.04)' @4 L2 M6 M7 |: k" t
  2. deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
    5 ]. L7 k4 S4 p4 F: z
  3. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse1 B$ u) s" [. K) s; Z+ ~; g4 s
  4. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
    2 h+ g2 u" I( O7 t
  5. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse5 k: E' r+ k* p9 v
  6. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse5 @! m4 N  V- H' [
  7. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
    ( M! V( U- J$ r* L9 i! m8 z- F0 R) Z
  8. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse: l0 N" w2 a" H& z& A8 `
  9. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse( Q! R4 |' ^0 l8 q' l
  10. deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
    % W0 Y/ B- ~2 \( y" }! [  E' M
  11. deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
复制代码

7 M6 B. y( z# ^" b3 N* p0 z3、更新源* O, Y& D/ Z6 Y4 ^! I2 J5 R
  1. sudo apt-get update
复制代码

* ?- f; d0 a! w2 {. K02安装拼音输入法7 }: B: O2 [- Y4 O7 n. z
1、Region&Language->Language Support->Installed Languages
, ]6 k, Q" C- n
2 a: v0 t- w' z7 s
微信图片_20240615144532.png
! _( r5 C' y* ^+ D3 v9 j3 x( B/ o4 J" w: a6 m4 X
2、Input Sources->"+"->chinese->Hanyu Pinyin
7 |( M! J! h, P, p- f5 i
  1. chen@ubuntu:~$ sudo apt-get install ibus-pinyin) M6 j2 p, |% I  ]1 [
  2. chen@ubuntu:~$ ibus restart0 Z7 n3 q) n7 A
  3. chen@ubuntu:~$ ibus-setup
复制代码
1 N( [% r1 @( o/ Z
微信图片_20240615144527.png   o4 Z0 m8 [# M# Q/ m+ ?

) B8 M3 P) S3 ^0 X 微信图片_20240615144524.png
$ X1 {2 U( j9 B7 V- V  P& P
# t, s0 E; ~) R- M6 \ 微信图片_20240615144520.png # n: v+ C0 @% d, F/ }

: d* q7 V; d) `3 E8 i9 }# l$ @03安装VSCODE
+ O$ p1 T. S! E$ W0 b6 q7 q1、下载.deb程序包https://code.visualstudio.com// m  M! s; v6 {& _

( g! k: q/ ^. X: ]8 ~, k$ ?2、使用dpkg安装(在Terminal中Ctrl+Shift+C为复制,Ctrl+Shift+V为粘贴)
" H# E& s' B1 s( l: S( z3 k' n
  1. chen@ubuntu:~/Downloads$ sudo dpkg -i code_1.89.1-1715060508_amd64.deb
    ; H0 ^1 G% Y# B3 A1 g9 h# X# l6 \
  2. chen@ubuntu:~/Downloads$ code .
复制代码
2 e7 g+ C, i" H: ~: k8 I
3、调整设置
3 F# S! s  d% `8 s  ~% r2 s9 u! d启用Title Bar:View->Appearance->Editor Actions Position->Tab Bar! R, K& G: h" s- i+ k

; a' N; u+ x! {+ r
微信图片_20240615144516.png
5 B. ?# W3 C  i- K9 {. v5 B. ~' z2 i' y0 h: }, u1 v7 \
4、安装插件. A2 A" a" g1 {, S5 e6 h, F
git安装4 ]) A% R, S4 @% U4 ?3 U- r! s  b1 i
Download Git for Linux(https://git-scm.com/download/linux)
; J. L! l. R$ O# [0 H; f' z( j
! f/ C  a- D) S/ r  G3 S$ ^
微信图片_20240615144512.png
& a( G5 X) C! Z0 x* P) U1 l0 T: L
8 k' m; b' e! P. B4 a4 d 微信图片_20240615144509.png
! X2 Z9 u0 a: Z; n- ^
6 I# A1 f$ B$ ]& \) t7 i' R
命令行直接安装(Ctrl+Alt+T呼出Terminal)
! ~. {" D7 r/ U& K
  1. chen@ubuntu:~$ sudo apt-get install git# h6 c/ [1 j* d5 l6 X- f: B
  2. chen@ubuntu:~$ git --version
复制代码

) j; M) X+ Z3 e2 V# V! Mgit设置1 L" Y( w/ m  ^1 X

  ?% L* h5 S# D; ]7 \% o9 l- {
微信图片_20240615144504.png # N4 W( Z" z, D" `* ^

: B9 j% L- Y: r/ E+ i
  1. git clone https://gitee.com/kevinkerry/linux-stm32.git# c6 q9 a. G% e+ @  F( i) i
  2. git config --global user.name 'kevin.kerry': D( m  f- B* T: ~" p
  3. git config --global user.email 'ct15762287606@outlook.com'5 [# G" k$ n2 @& @/ h

  4. + h  {) O, o, s+ R
  5. : N; c6 t+ \0 r% ?$ P+ Y
  6. #示例
    6 j; R  x/ l: g0 Z1 @
  7. chen@ubuntu:~/prj_vscode$ git clone https://gitee.com/kevinkerry/linux-stm32.git! ~& X$ w& k3 A  A* K+ y
  8. Cloning into 'linux-stm32'.../ C5 y/ h2 G# b& }
  9. Username for 'https://gitee.com': ct15762287606@outlook.com
    & M9 h% d; J9 M4 \% i
  10. Password for 'https://ct15762287606@outlook.com@gitee.com':, X4 g7 h0 D8 s$ Z2 x; J! J
  11. remote: Enumerating objects: 4, done.
    , j1 e3 T5 Q+ G4 j% m1 h8 X+ `
  12. remote: Counting objects: 100% (4/4), done.6 q, |# a7 h) \9 M' o
  13. remote: Compressing objects: 100% (4/4), done.' B, G! ?$ @: r& K  Y2 o6 |7 k+ ^
  14. remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
    7 r& G2 W! C) K) o7 L8 p8 A
  15. Unpacking objects: 100% (4/4), 1.79 KiB | 1.79 MiB/s, done.
    / n  _( V. i2 s& D6 l
  16. chen@ubuntu:~/prj_vscode$ ls! I1 p+ b  T% Y. n
  17. linux-stm32
复制代码

. U! }5 @* W2 W/ L. N* z6 dGitLens — Git supercharged插件安装2 P6 H$ [7 F  ]$ D
) e3 S8 {) Y) x9 U1 P9 u- ?: l
微信图片_20240615144459.png
5 V% l( X1 }5 B+ J% j- [0 |

. ^2 g1 o$ [3 t8 X7 tGit History
1 M# C) [( ~2 z) z. R
$ t% `: b0 p8 {& ~) L2 k. k9 P
微信图片_20240615144456.png ; ]* d  P4 J2 m* |+ _. P' l6 o. B

, }0 @% A& i  w* Q3 gBracket Pair Color DLW2 s2 H6 A5 u1 A

& j. }6 X5 b. d2 y2 `, f# u
微信图片_20240615144454.png
  g& \, P6 w  f" g) h1 e# z9 K
# x3 ^2 x7 c5 b5 tArm Assembly4 c# v! L! n3 D0 @; r
+ P# n1 Q9 H) w
微信图片_20240615144451.png ( w& _+ [5 t! m% [

8 \" X# u  Q1 }5 F/ H, DVenu's Cortex-Debug
2 B9 }$ ~0 J' f1 h" I; N' W6 E
# q' D' [; n/ ~9 {8 ~$ U$ J" r% _
微信图片_20240615144447.png 1 C! J3 G. D4 D' ?; o) a
4 H3 L: G1 {! n& _% m+ v
C/C++
. h- g4 w) K) u: ^- h2 Q6 W6 o  S, @' T/ V# g; p; d
微信图片_20240615144437.png 2 ^& G2 X% h  h
( Z$ J% ~6 R# Q, E, c$ s; U# g
04安装Build Essential及gdb
& [/ y5 d! o0 T1、build essential介绍
! ^, U) }) j; l2 z( H' zbuild-essential 指的是编译程序必须的软件包。它本身不是一个软件,而是一个工具集,包含了在Linux开发程序的一些必要的软件包,包括但不限于gcc、g++、make等。9 R3 l: e7 `/ E4 [) U

% _7 J& J0 D0 j' p0 P
查看该软件包的依赖关系,可以看到以下内容:
0 L1 C& f- ^3 \2 i2 _* J, @9 r6 K
  1. chen@ubuntu:~$ apt-cache depends build-essential( |- z* K2 x3 b+ g
  2. build-essential+ N: d9 q( i( g! u: k* b) y; V4 g; I
  3. |Depends: libc6-dev- O, a" ?0 A- Z0 F4 w
  4.   Depends: <libc-dev>- _( N1 o0 H! h# d
  5.     libc6-dev' b; C  \+ L/ n7 \% n6 [( A
  6.   Depends: gcc4 i: V4 D) o3 Q
  7.   Depends: g++
    ' |" [7 t( t2 U9 F! [7 N8 `
  8.   Depends: make
    4 P' U- O" g3 {7 S6 ~- J
  9.     make-guile
    ; J, x" n7 v* b8 F1 t
  10.   Depends: dpkg-dev
复制代码

8 ?" Y3 W" p. Z( u( P, b4 `也就是说,安装了该软件包,编译c/c++所需要的软件包也都会被安装。因此如果想在Ubuntu中编译c/c++程序,只需要安装该软件包就可以了。9 ~3 ~1 h& g1 }" \1 b2 {* |' Z( G
- d& S4 R  ~- P0 P
2、命令行安装build essential和gdb
& v* R$ s* v* O( I  I  x. Z有时候可能会需要安装gdbserver; u7 c1 x9 l+ E. |1 \/ w7 L9 F8 V! Z+ S
  1. chen@ubuntu:~$ sudo apt-get install build-essential gdb
复制代码

% `9 S7 y$ |) W4 ^% Q) U3、Linux C程序开发简介* I! v8 X' W5 a3 b' Q$ s* l
3.1 无makefile* f% f1 z) R! U- F: v
  1. chen@ubuntu:~$ mkdir linux_c% F, u, G( w1 C, t2 `& {! ~
  2. chen@ubuntu:~$ cd linux_c
    3 W/ x3 s: O2 k1 W3 d, Y+ X
  3. chen@ubuntu:~$ touch hello.c5 ^- b. D# \7 H  U1 S
  4. chen@ubuntu:~$ sudo vi hello.c
    7 c/ p  B3 _4 g1 Q2 m1 N0 h
  5. chen@ubuntu:~$ cat hello.c  Y- O) V# Q& E) k# o1 c
  6. #include <stdio.h>) k3 p% D. g8 u9 ]3 b
  7. int main(int argc,char *argv[])! A* h. F3 w: \. M+ M3 j# N
  8. {
    9 w! G' k9 Q. D$ x
  9.   printf("hello world\n");, L  A/ g7 X( A$ |' f- a
  10. }
    ( ~  q- g$ b1 v. Y8 q' j
  11. chen@ubuntu:~$ gcc hello.c
    5 j# [4 p$ H8 \  I' }
  12. chen@ubuntu:~$ gcc hello.c -o hello& k  A% y* B# s4 v. W' G1 i
  13. chen@ubuntu:~$ ls -al" B9 E& S+ ~0 t; ^" k2 a
  14. total 52; t8 u5 F% A8 z  ]+ W0 J6 x: H/ a
  15. drwxrwxr-x 2 chen chen  4096 May 16 18:32 .
    1 |. B: Y' |. @
  16. drwxrwxr-x 4 chen chen  4096 May 16 17:37 ..
    : P6 O# \- j$ a# a, W
  17. -rwxrwxr-x 1 chen chen 16696 May 16 18:29 a.out
    ; m; D7 E  W2 F6 }9 `6 ~, Q3 ]
  18. -rwxrwxr-x 1 chen chen 16696 May 16 18:32 hello* h9 A) e% Y' Z8 w0 O/ x% f
  19. -rw-rw-r-- 1 chen chen    82 May 16 18:29 hello.c
    : h2 ^7 L/ ?( _3 K$ k: f/ |9 C; b
  20. chen@ubuntu:~$ ./a.out# G* u% X0 p$ Z0 ~; t7 M/ M
  21. chen@ubuntu:~$ ./hello
复制代码

) v9 O# `0 E6 E* [2 G3.2 有makefile
* m( w- a1 _% ]3.2.1 创建程序文件
  1. chen@ubuntu:~/prj_vscode/linux-c$ touch main.c input.c calcu.c input.h calcu.h
复制代码

  ?! K, y" O1 Y6 [; w; {main.c: Y8 x: h1 W4 _: a, s
  1. #include <stdio.h>
    4 c/ U7 ^* m5 `  ~) u2 S
  2. #include "input.h". B, d& `5 ]2 \! O4 n" e6 `
  3. #include "calcu.h"
    " d  X* u4 p; U; F3 m4 \3 Z1 |

  4. " ]6 o7 T; D& I2 t: v8 P3 J; G
  5. 5 P( c) d% w/ U6 U$ P6 Q: D
  6. int main(int argc, char *argv[])4 j2 \7 F- _+ d% L2 K5 f
  7. {# J) m, p+ o/ ?

  8. - w) B& N7 G0 L/ K6 a
  9. : N3 I, _$ p! g) x) s3 Q
  10.   int a, b, num;! K" f1 u/ p, R+ r, Y

  11. 8 r/ _, o1 P+ i! T- l3 G; Q
  12. ' N; ^. A2 @' `" T8 r( N
  13.    input_int(&a, &b);( k) R5 Z4 J+ B/ z
  14.    num = calcu(a, b);
    + }/ L! i1 a7 p& M( }
  15.    printf("%d + %d = %d\r\n", a, b, num);
    , m" `% A+ F5 @0 ~
  16.    - @7 c! v3 J  P: Q, U$ p
  17. }
复制代码
; `" ?, N3 l* P5 `7 p
input.h
  1. #ifndef _INPUT_H  D$ l; M* ]. {4 f: `5 ~
  2. #define _INPUT_H4 \! h3 b) r4 t! f- ?6 w" i

  3. 0 b0 S  L% `7 j* f
  4. void input_int(int *a, int *b);/ Y5 D3 Y& {; K6 e3 Y# d( D7 ?, s

  5. ! ]1 e: n( {& k# |/ S  w  E3 `
  6. #endif
复制代码

6 E6 B1 v# r/ T& T( \input.c; T' |! b# i- ^% k3 b% N7 l
  1. #include <stdio.h>' ?, M, U2 j  s
  2. #include "input.h"' C% ^( C, X! x4 }0 D
  3. . Z, g4 e. x) b' A1 |

  4. 1 ?8 K+ A& c% E; v- v' @: u( M
  5. void input_int(int *a, int *b)
    ' f; q+ S- c* k* U
  6. {9 a1 f4 Y6 G% j: y! b. U3 `
  7. printf("input two num:");' k+ g$ M, }3 l
  8. scanf("%d %d", a, b);2 r/ I; c0 }6 Z8 L, _: P" f: q
  9. printf("\r\n");8 r6 z7 d4 f; Z) T0 d, x
  10. }
复制代码
' J; `: a. b* ~" F$ a
calcu.h
  1. #ifndef _CALCU_H
    1 M9 @3 a/ c+ I% Q) f' |9 I
  2. #define _CALCU_H
    . Q5 X7 Z4 e3 t: D# Q# |$ r7 ]

  3. + t7 P. X* P  u  ^* s4 I) L6 Y8 \/ c

  4. 3 ?. n! ^7 l" s* C! f0 C, s4 W
  5. int calcu(int a, int b);: M2 h) o  n9 A, j* P
  6. #endif
复制代码

7 H+ m5 |. d" ^( Jcalcu.c
& l! M1 V0 D% y& t) s3 C
  1. #include "calcu.h"
    9 C( i: ~5 T0 d' Y: ~( W! O

  2. 4 y! a/ e( y; h5 |0 D: I

  3. 8 c" @) A: `; q3 S1 ]; K
  4. int calcu(int a,int b)
    ; ^1 k1 I- o0 h
  5. {
    0 ?2 \  M; d7 a1 X
  6.   return (a+b);( w4 z7 T, H4 [! f) N: Z: M9 S
  7. }
复制代码
. w7 x9 U- {3 u8 F5 d" H, ~% K4 `
3.2.2 编译测试一下, O, c  F2 y  V, a8 Z8 ?5 t
  1. chen@ubuntu:~/prj_vscode/linux-c$ gcc main.c input.c calcu.c -o main
    , i/ n$ O, g0 ]7 L/ p- U
  2. chen@ubuntu:~/prj_vscode/linux-c$ ./main! B) u% A5 Y; W' ]  U
  3. input two num:23 56! |' O: T3 m2 N! d. g% h

  4. ) t5 [; E8 F- M. D2 g  E% ~' S8 [
  5. - f& p2 H/ s$ s1 ^. t7 Y0 R7 L; @
  6. 23 + 56 = 79/ s' X9 D! E& N$ z+ {+ ^9 Z
  7. chen@ubuntu:~/prj_vscode/linux-c$
复制代码
  1. chen@ubuntu:~/prj_vscode/linux-c$ touch Makefile
    & |1 k% K3 b% x2 ^) {
  2. chen@ubuntu:~/prj_vscode/linux-c$ cat Makefile
    ' ?& q2 ^5 u+ c" l) i
  3. main: main.o input.o calcu.o
    % H& q* ?4 l. _. X& B- |5 p
  4.   gcc -o main main.o input.o calcu.o
    5 A/ t% S0 y% Y) p
  5. main.o: main.c
    ! q1 y. d6 Q# W' a/ ?- W. t
  6.   gcc -c main.c
      g( `" X, l3 f6 x3 b9 ]: ]5 H
  7. input.o: input.c
    7 G$ ]7 P2 ~0 I& l
  8.   gcc -c input.c* r( c! m5 x' e6 |- u
  9. calcu.o: calcu.c
    # @/ Z; y0 P- o2 a- R4 ]- A
  10.   gcc -c calcu.c
    5 @2 X- F* r- j
  11. 5 z9 S# U  m: |1 Z# }5 y0 H! w
  12. # D, M  \* u3 T
  13. clean:
    # u1 R+ v4 j+ |) J
  14.   rm *.o
    , X+ c- S3 Y! g- v3 G- m
  15.   rm main
    : S6 M9 ^: G; a
  16. chen@ubuntu:~/prj_vscode/linux-c$ make clean
    ( U* q; }0 S5 x- X
  17. chen@ubuntu:~/prj_vscode/linux-c$ make
复制代码

, k3 F8 `9 ]9 {& ]& x/ k9 t5 j$ K& l4、生成.vscode文件下的debug配置文件! r! v8 ?9 c) h5 k- o
tasks.json:该文件主要用于配置编译的选项,如果使用makefile或者cmake进行编译,则用该文件调研make或者cmake命令进行配置。2 t& p0 u; A3 k3 S( X* c' J8 |
launch.json:该文件主要对debugger进行设置
6 h4 k4 r  a# O, A, P! Nc_cpp_properties.json:编译器路径和intellisense设置
( x- w- b9 `+ E9 [8 c

6 W8 l* y. b* Z% s1 t3 f4.1、生成task.json文件: z3 b" ]6 _1 I; F3 D( K3 {
- P6 T$ a/ C  h5 {( R
微信图片_20240615144428.png
4 M, f+ V3 {- v* F% u图4.1.1 Run C/C++File
3 U  l- {/ c8 U6 B0 Z% T# A
5 k3 L: K) \" h& N: n+ T
微信图片_20240615144424.png
- p( L5 J0 ~% A/ T- `: @6 j$ A. n- f8 u
修改生成的task.json文件如下
* M/ r4 h( N+ Y, t- A, o
  1. {
    & f0 {: H& R* e5 ~8 c+ {
  2.     "tasks": [# c, \5 q# ~) k1 w+ O3 F
  3.         {
    8 R5 L* t. E$ @9 [3 |; o! T( _
  4.             "type": "cppbuild",% c1 k# x( W6 ?  W0 A
  5.             "label": "C/C++: gcc build active file",, z6 z2 D; V1 J5 Q# |
  6.             "command": "/usr/bin/gcc",5 ]& _& r2 u2 I" o. u' |+ L) l
  7.             "args": [
    1 a* |. z3 t1 @0 Y; L
  8.                 "-fdiagnostics-color=always",8 m- R: p( g, N; |+ Q
  9.                 "-g",
    8 G) ^5 h( T+ e, {1 l
  10.                 "${file}",# F, g5 _3 z. B) P# Q
  11.                 "-o",
    8 a; _+ S) B% \1 v. [
  12.                 "${fileDirname}/${fileBasenameNoExtension}". z9 C$ g' e2 c& Z! V- J1 q$ k
  13.             ],0 D3 J0 {# `  l
  14.             "options": {& Z$ \, X/ Y( u
  15.                 "cwd": "${fileDirname}"
    : W" ~: T, o; X2 t: A" S
  16.             },
    0 ^, D" S0 e2 u9 ]& `3 ]$ X
  17.             "problemMatcher": [
    % l' S  z2 U; A" j8 \
  18.                 "$gcc"
    $ ^% m; R% ^1 Z4 _7 s
  19.             ]," z1 @! p3 R9 d
  20.             "group": {
    % D' {# B7 g; b+ p0 [
  21.                 "kind": "build",
    9 Z5 N( y# G' f
  22.                 "isDefault": true
    6 ]6 C& n% }! q; a  @
  23.             },
    0 t/ u4 B# i1 u
  24.             "detail": "Task generated by Debugger."$ ~3 m6 w8 f# @+ p; o% [
  25.         }
    ; N& V# n; {+ o' A' E1 \
  26.     ],: j+ l6 a$ h' o$ w# |7 `3 C  m9 d- m2 @
  27.     "version": "2.0.0"2 I; G  z  d! [) j* Z9 a
  28. }
复制代码

! F, k* Y! F2 M! H修改如下
  1. {, V6 C7 |0 u% y# S" P+ h! R
  2.   "version": "2.0.0",+ t  o$ v! J% d* x+ H* Y0 v$ `$ p
  3.   "tasks": [
    0 b9 w5 V& C* h7 Q
  4.     {
    . @/ f- |; V* n, S) w2 q0 O
  5.       "type": "shell",
    3 e9 a, n, r! `' O/ g/ N
  6.       "label": "C/C++: g++ build active file",
    2 x9 H8 i9 b- O( l7 T
  7.       "command": "/usr/bin/g++",; W# f9 `3 v& A/ x, t; R3 x& U1 H
  8.       "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
    % A- _1 k+ q5 ?
  9.       "options": {# ?& e; ^9 H3 f" u' X! D
  10.         "cwd": "/usr/bin"5 r' i5 |1 H0 u& X: `
  11.       },- v, N1 e' i- X6 M& K+ m# [
  12.       "problemMatcher": ["$gcc"],' `' H2 c& {1 ^3 i
  13.       "group": {7 }0 e/ V, R" n4 ?; a1 E. _4 {
  14.         "kind": "build",3 H$ L( |! ]9 G% f( W
  15.         "isDefault": true
    . E7 S! x4 @6 [0 D/ j# ]+ w
  16.       },
    1 w. M9 m8 F& \* ?
  17.       "detail": "Task generated by Debugger."
    2 b# t7 M  e4 ^5 r! |: Y
  18.     }
    " `; p" N& M+ W) G: {% f# G5 U
  19.   ]: U3 E0 t, [8 O# T& a
  20. }
复制代码
$ Q5 I3 `3 ~* b- n% C+ T
command 代表使用什么程序去执行文件,这里使用的是g++. args表示的是传递给g++的参数,表明使用了什么文件(-g 后面的file文件所指内容),生成了什么文件(-o 后面文件所指的内容)。所以这command 和 args两行参数表达的意思是:使用g++编译器将${file}文件进行编译,生成${fileDirname}/${fileBasenameNoExtension}文件。; F, r) I+ K0 d  @: q
- I$ g8 y. y- _$ ~- W
上面部分是对单个c++文件或者多个c++文件进行编译。但是如果一个项目使用makefile进行编译,则如何使用vscode 进行配置呢?我们这里进行讲解。由于我们使用make进行编译,所以我们将command命令后面加上make,args后面添加相应的参数。比如我们需要进行debug 所以要控制makefile 中所有的gcc编译命令后添加-g。不然的话,就会出现,编译成功了。但是debug的时候,断点无效,所有的执行都是一下子执行完的情况。由于在我们的makefile 中通过TAG = dbg来控制加不加编译选项-g,如图3所示。所以这里我们的参数添加了"TAG=dbg",这里根据不同的makefile进行不同的配置。, t% B: O/ d' b# V3 T( P- a  O
  1. {; L/ z' {4 D: @6 v5 ]" C* u
  2.   "version": "2.0.0",
    3 Y/ K7 z2 {' H  s2 u
  3.   "tasks": [7 l6 t6 E# O$ Q" c6 \
  4.     {5 }( s3 \% X- H* ]$ o! h: _6 Z. r- b
  5.       "type": "shell",/ O6 `) |' e& ]) c* s2 g9 A1 C" s
  6.       "label": "Build",6 j0 e* c. E5 x
  7.       "command": "make",+ G& x" n# s# v: F5 c, A$ ?
  8.       "args": ["NTHREADS=1", "TAG=dbg"],. k1 l/ U- S. R3 `* O  m& l# J" W" ?

  9. + S$ Y5 o* Y7 p3 y1 l  I- `5 P

  10. ; F: X- i! J# F2 [$ Z
  11.       "group": {
    ! J/ m' y, S+ o* h  _3 n% ?6 W" b
  12.         "kind": "build",
    0 \" X" T( I  n$ L9 A; o) K* \
  13.         "isDefault": true. {$ r( m. P: J; ~" y: [) Y
  14.       },
    3 j0 D+ \) W- ?+ R( a) R
  15.       // "detail": "compiler: /usr/bin/g++"& p  H2 x) B4 z8 b1 L) A4 B
  16.     }: o# n$ _2 A9 a' J& Y& J
  17. + x  W5 X& N' K: `
  18. }
复制代码

9 F& q$ B: `4 I一种可行的Makefile文件内容(gcc后添加-g命令)
0 Z- e  b! ?; v" F$ t
  1. main: main.o input.o calcu.o
    4 U/ H- m9 t, V# W
  2.   gcc -o main1 main.o input.o calcu.o, [8 @8 g# k8 l! N- C$ \
  3. main.o: main.c3 i* l7 N6 B" x$ q0 C5 {/ h
  4.   gcc -c -g main.c& F( G- x3 m! K9 x2 l
  5. input.o: input.c, r+ h" I# q0 [% b, E
  6.   gcc -c -g input.c
    4 {7 C6 E) z2 s( ]- Q+ W/ H
  7. calcu.o: calcu.c( o6 t1 ?2 P5 f, T5 P) s8 l
  8.   gcc -c -g calcu.c( K1 \  g% F* u' B

  9. 0 T5 \5 G! R5 @
  10.   q0 |. Y! W  {' B. t8 j3 p5 S
  11. clean:5 C( Z( c0 C) t9 ~- Z7 J3 i* f
  12.   rm *.o9 {5 S. g/ ?5 X2 _
  13.   rm main
复制代码
: R1 X; c, V& o' q
4.2、生成launch.json文件9 `( p* p; X: t7 v) S& p3 S: g/ r
(注意只有在.c文件中才会有下图中右上角的图标显示)
9 A8 M- R3 i7 c, e+ c' T, ^% J8 N9 n  C  l" c: r: u  P0 b
微信图片_20240615144421.png / x1 ]$ p. `+ m

/ }6 Z* a/ J# u* \- P 微信图片_20240615144417.png
1 v/ q- R. P/ X. X8 Y' N  [图4.2.1 添加调试配置文件. V4 n& b$ z8 e' D% l
  1. {% q" A2 v, G  k5 T6 Q
  2.     "configurations": [3 ?& b0 d& z1 H6 @- T
  3.         {
    2 Z1 W/ X1 q* ^( q* B6 X
  4.             "name": "C/C++: gcc build and debug active file",
    5 L6 M; h- U/ O+ y4 \
  5.             "type": "cppdbg",
    7 ^+ `: }& J  N, W
  6.             "request": "launch",* x6 s# Z% A! ]* i* M
  7.             "program": "${fileDirname}/${fileBasenameNoExtension}",. f& Y& f3 q- D( Z  u* S- P
  8.             "args": [],2 f4 L$ l: J  \4 _, x
  9.             "stopAtEntry": false,
    9 G' L* O) [' ?" @7 e! L" P' P
  10.             "cwd": "${fileDirname}",/ b4 j! W0 E/ s( a
  11.             "environment": [],
    % \9 Q  w/ |- v) `
  12.             "externalConsole": false,
      h7 h9 Y" ^+ Y
  13.             "MIMode": "gdb",
    , a# A  m2 ^7 @8 x$ c
  14.             "setupCommands": [2 }3 w! g8 s! f* Y$ R3 P
  15.                 {
    + P3 I: x5 B9 i1 H
  16.                     "description": "Enable pretty-printing for gdb",% |+ d! [' B& @
  17.                     "text": "-enable-pretty-printing",
    ) z+ X5 u7 @4 z: F8 c6 F) b4 m0 B
  18.                     "ignoreFailures": true
    0 T1 J) e' P/ Z7 w1 f! ]
  19.                 },
    7 C& z, O% m  V* z
  20.                 {+ k1 m. I! W( v( X
  21.                     "description": "Set Disassembly Flavor to Intel",
    1 {: _- i% f5 K/ ~6 I! E% J
  22.                     "text": "-gdb-set disassembly-flavor intel",) h+ n0 F4 b( v8 ^4 w* v  {/ E
  23.                     "ignoreFailures": true4 R( C6 L7 ]0 [8 d
  24.                 }% m7 \2 l9 m) X; u6 D
  25.             ],
    4 x6 c. \% Z# a- Y- j! Q3 k
  26.             "preLaunchTask": "C/C++: gcc build active file",
    7 J) F. l( s3 D3 y1 f# e
  27.             "miDebuggerPath": "/usr/bin/gdb"# V2 r( H$ A6 K1 t* o5 q" u3 a+ O
  28.         }
    9 h- r" F5 N1 u3 A
  29.     ],
    9 W9 {* w5 V" ~6 _/ f4 G- L
  30.     "version": "2.0.0"+ }& Y9 g+ p* L9 A: v/ o0 n
  31. }
复制代码
/ m/ r# |" b4 O% H& A2 Z
修改生成的launch.json文件如下) [5 [3 q6 Q2 c( t6 Q
  1. {
    8 |: f( p' z) h. g) r
  2.     "configurations": [* X1 _( ~( Q+ [. X, K+ \
  3.         {3 _0 l/ J% P1 W5 j  w! o6 _
  4.             "name": "C/C++: g++ build and debug active file",1 o8 I2 P. _+ ]6 |5 ^5 Z4 T' t
  5.             "type": "cppdbg",
    5 W/ C5 u+ @$ g% o! b
  6.             "request": "launch",' o2 J" r( ^% S, L0 |( A( I% v* V! ~
  7.             "program": "${workspaceRoot}/a.out",
    1 d& }( K" E8 g4 y& l0 j' f+ c
  8.             "args": [ "1232"],
    7 N7 e" z7 @# i, c. k7 d. x
  9.             "stopAtEntry": false,3 ?" \0 n: p6 o
  10.             "cwd": "${workspaceRoot}",
    1 S! D( W4 ~% m/ t. A
  11.             "environment": [],: X7 e4 x4 c& S8 p% _6 K  e0 W
  12.             "externalConsole": false,
    9 }% f8 h# l5 K, v5 t5 g
  13.             "MIMode": "gdb",% i* P; z3 d' }) ?' M
  14.             "setupCommands": [
    ; a$ t, Y6 X/ A  j; {
  15.                 {2 i9 r4 B" ~2 q! C2 [
  16.                     "description": "Enable pretty-printing for gdb",
    8 p( Q* P8 l+ P: H( z6 K8 }
  17.                     "text": "-enable-pretty-printing",  R. ?3 ^6 @; ^: n% n: g
  18.                     "ignoreFailures": true' M& ~- [. {/ e1 K5 H8 ]
  19.                 },- h6 m+ R, a  A# t/ ?
  20.                 {7 }8 V* C( M) _0 U
  21.                     "description": "Set Disassembly Flavor to Intel",% s( k( Q/ z& ^; t  H0 k
  22.                     "text": "-gdb-set disassembly-flavor intel",7 \) O1 G( v8 a+ X$ f& a; ]
  23.                     "ignoreFailures": true
    ' C4 F& J( G3 D+ v8 U! v
  24.                 }8 D8 ]( U: ]) X) N! z# c% Q  V
  25.             ],3 b8 W* H  X. U. t! `' e/ B
  26.             "preLaunchTask": "Build",* l, P( g0 |9 ^, G6 F8 [
  27.             "miDebuggerPath": "/usr/bin/gdb", L! y# M+ ^0 O- f) b6 V; T( x
  28.         }
    $ x7 p! k4 h8 z5 U( `0 Z* ~' V
  29.     ],9 r5 z7 u/ ]9 i8 W: g7 q% M1 n# _
  30.     "version": "2.0.0"
    + W- v+ |$ v& D5 X5 \0 t
  31. }
复制代码

$ x; J0 p6 v: @; G; e"program"选项代表,需要进行调试的程序,所以这里需要修改为对应的文件。"PreLaunchTask" 代表debug流程的前序任务,这里的内容需要和tasks.json中的label相同。"args"可以添加执行程序的过程中,需要添加的参数。在下面的代码中,我们就添加了cacti程序,以及参数。
4 e7 z, [$ d5 W; o- Y: C
1 M$ P: ~; _/ ~% T8 {# }
4.3、生成c_cpp_properties.json文件
3 v: w+ [" M! U) b! O1 cctrl+shift+p 搜索C/C++:Edit configurations(UI) 即可生成该文件
4 o0 ^5 z5 E2 s* G$ ~
  1. {. z# v% Y4 a/ M7 H
  2.   "configurations": [9 l& o- t8 q2 W3 y  {0 ?: V
  3.     {
    0 l5 Z* ^$ b9 Z2 O+ k; s
  4.       "name": "Linux",
    ; F6 p$ G% {' Z8 Q5 G7 w
  5.       "includePath": ["${workspaceFolder}/**"],
    7 b5 m- T3 Z2 x' {  A7 g
  6.       "defines": [],
    1 S4 e# y; N1 s: j% `! J. J4 L
  7.       "compilerPath": "/usr/bin/gcc",. A( o/ g0 e2 ]6 s% e
  8.       "cStandard": "c11",
    & x  j, G7 `  |& C  I' {7 ~- U
  9.       "cppStandard": "c++17",& p! o$ m0 T) j: _
  10.       "intelliSenseMode": "clang-x64"$ e! P1 }' p. X5 N4 ^, J
  11.     }* ]" {( s7 t2 e. K& A$ f4 c
  12.   ],
    ! r# r( h" v, D/ k. J) c
  13.   "version": 44 B2 E- D0 p: w, Q5 }6 d5 O5 N
  14. }
复制代码
7 u5 G  d- N2 f0 G0 ]) x
05安装Vim
# l& }, f, ?5 t" f' f1、命令行安装
' [, G5 Y4 m, N
  1. chen@ubuntu:~$ sudo apt-get install vim9 t+ G% }! L% `! r
  2. chen@ubuntu:~$ touch a.txt
    * S5 z' X# s- N0 _# b5 ]
  3. chen@ubuntu:~$ sudo vi a.txt
复制代码

0 A. X* w( d6 e' p5 _  d06安装gcc-arm-none-eabi
' }% z$ ]9 h$ }7 I7 C1、下载嵌入式gcc编译器
  ?0 j$ V# i! A0 K) F& thttps://developer.arm.com/downloads/-/gnu-rm
- D' c# x- O: ?$ e6 n. n0 d$ X% S8 t
微信图片_20240615144414.png
& k# c: K/ I( I7 |" w: o. ?7 ?; {: N1 L0 @; x; ~% Y- R8 N- I5 B
2、解压gcc-arm-none-eabi-10.3-2021.10! M" R! K1 a6 H; a4 s
  1. chen@ubuntu:~$ tar -vxjf gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2
    9 H# ^$ g7 ^$ |7 H% Y. h+ H
  2. #对于bz2、gz文件解压
    $ c! `4 q5 E/ E0 v+ J
  3. tar -vxjf test1.tar.bz2  z6 Q( ]. }% F8 j
  4. tar -vxzf test2.tar.gz6 {7 c/ f. K6 e& w0 a! x; |
  5. #对于bz2、gz文件压缩
    # n2 p, L: ~. U) R% l
  6. tar -vxjf test1.tar.bz2( H7 @1 l: K; y: G9 q7 a( W
  7. tar -vxzf test2.tar.gz
复制代码
" x1 w0 y) N0 Z2 Y
3、添加gcc-arm-none-eabi下的可执行文件命令到Terminal(终端)
4 _5 A  u/ ?0 I' ]
  1. chen@ubuntu:~$ sudo gedit .bashrc
    % ]( p6 @8 U6 c( s9 Y+ F, K
  2. #在文末添加下面一行
    ; u* g5 J; Z/ I( ]1 S
  3. export PATH=$PATH:~/gcc-arm-none-eabi-10.3-2021.10/bin4 t# X) N: e" `
  4. #添加上述行% Z9 v, n8 b) p# Q
  5. chen@ubuntu:~$ source ~/.bashrc: V- ?8 T. [7 v" I  v7 Z+ q. Y  K, v
  6. chen@ubuntu:~$ arm-none-eabi-gcc --help
    0 K$ U3 _/ l  O; `3 s/ T) n
  7. chen@ubuntu:~$ arm-none-eabi-gcc -v
复制代码
4 R2 D* J, N5 U6 ]
07安装stm32cubemx  l, c% B1 \& p9 E
1、下载stm32cubemx
; Q# p4 K: [1 h, N3 y! N. r8 ehttps://www.st.com/zh/development-tools/stm32cubemx.html
: ~. O" z* A& S1 p  G1 n1 Q9 p' \0 E1 O! g4 L. X
微信图片_20240615144411.png 6 h+ d2 n, B' `) d9 U! p( n3 |

  @4 A& [: R3 M6 Y% S) f- e. M) [2、安装
  1. chen@ubuntu:~/Downloads$ unzip en.stm32cubemx-lin-v6-11-1.zip7 q0 i2 {  [8 L' R& k
  2. chen@ubuntu:~/Downloads$ sudo chmod 777 SetupSTM32CubeMX-6.11.1, |0 Q# j" Z4 t+ A
  3. chen@ubuntu:~/Downloads$ ls -l
    $ p  v8 c6 v  E
  4. -rwxrwxrwx 1 chen chen 539136342 Apr 11 09:41 SetupSTM32CubeMX-6.11.15 T' u; M' n4 c2 V! j
  5. chen@ubuntu:~/Downloads$  H" `# `) ?& \* A6 S+ k7 d+ r0 m+ B; h
  6. chen@ubuntu:~/Downloads$ sudo ./SetupSTM32CubeMX-6.11.1
    # k9 B0 P; y3 S6 |# v- k
  7. chen@ubuntu:~/Downloads$ find /usr -name STM32CubeMX$ D8 }$ G. n# ^1 ?) c
  8. chen@ubuntu:/usr/local/STMicroelectronics/STM32Cube/STM32CubeMX$ ./STM32CubeMX
复制代码

# J( I( K% g' @  b! r1 b3、增加图标到菜单- M9 |% h3 a" _* H
  1. chen@ubuntu:~$ /usr/share/applications/
    . M- m* H2 G# w2 y' {# G* Z0 I
  2. chen@ubuntu:/usr/share/applications$ sudo touch stm32cubemx.desktop2 q, A% ], ^5 S7 x) z% B' Z
  3. chen@ubuntu:/usr/share/applications$ sudo gedit stm32cubemx.desktop  G' u& }% _* S; C# B
  4. chen@ubuntu:/usr/share/applications$ cat stm32cubemx.desktop3 Y/ |- B- G" S: X9 K$ _
  5. [Desktop Entry]; Y! {. L2 l) f9 p. s! s- p4 C
  6. Type=Application$ W" `3 y& L1 }* `
  7. Name=STM32CubeMX
    : X0 C4 Z) ]2 J8 h- O8 a- H
  8. Comment=STM32 Development Environment
    6 M0 L" e- F8 ?
  9. Encoding=UTF-8  K; u) s% W# \, b
  10. StartupNotify=true
    0 g  O: C, w: i; n! N
  11. Terminal=false
    - K; T# h) v; O5 |0 E
  12. Categories=Development;java;c;c++;
    ; q6 t- D# Q4 E. ~
  13. #icon= ##########图标的路径,自己找一个就行
    7 B' F5 D% w3 f0 Q1 M; ^6 f; f0 [
  14. Exec= /usr/local/STMicroelectronics/STM32Cube/STM32CubeMX/STM32CubeMX5 F7 B/ j/ D& x! n5 ^/ g% r
  15. chen@ubuntu:/usr/share/applications$
复制代码

" T5 E+ x% r5 s08使用stm32cubemx生成初始代码2 ?( }+ \$ @4 c, b# L( w9 t
1、选择芯片->配置->Generate 4 O" {' {; n, ~* ]
- b4 Q  _$ Z. v) d; j: @
微信图片_20240615144405.png ) h6 L# _6 U/ M& v7 {9 S: R
* e1 E  ?: o* ^; r* U& E- p) f
2、一定要记得勾选PinOut-Debug-SerialWire否则后面下载程序时会报错Error: init mode failed (unable to connect to the target) 。这个是巨坑,一定要小心!!!!!!!!!!: P. {- R! S9 g5 G
. M* o  L3 C! k+ }& F4 Y) e( c3 t4 ^
微信图片_20240615144355.png
5 }- E7 d5 b) |( K7 I
4 H) o. A4 c/ T- h1 r
09配置VSCode
* l" Y. a6 }7 z  j3 J2 t& N8 F4 G1、使用vscode打开上述生成的初始代码0 i( L# l. w! V8 U; q7 \
2、生成VSCode配置文件
! ~- {' x: Y# U9 ~: u1 f" U2.1 c_cpp_properties.jason
1 [5 F- Y8 Y2 t1 @% N3 {9 o2 Nctrl+shift+p->Edit Configurations(JSON)生成.vscode/c_cpp_properties.json文件* g! c( M: Z3 i7 d$ v: r; x) C, u

1 ?6 |$ N" J9 E# v5 h
微信图片_20240615144351.png % d: x& i8 e: [; k: H( y
  1. {
    ' ^' `7 Q$ @! p  O' K
  2.     "configurations": [/ x2 X. {4 q- g( p# l8 M4 N
  3.         {2 }, [( n) b2 ]
  4.             "name": "Linux",: r& O/ n" C6 p9 \: O( _* v# y
  5.             "includePath": [
    1 e, i1 i& @  \( B& z, y
  6.                 "${workspaceFolder}/**"
    , y' F# g2 X5 z- K1 A
  7.             ],
    ( q- S  i/ x0 i; d& ]$ x0 o
  8.             "defines": [
    7 }% x) P# `) N7 p; y
  9.                 "USE_HAL_DRIVER",
    % h0 _. N; u: G
  10.                 "STM32F103xB"
    1 h. @' y: ?% f
  11.             ],
    . q. Z  J0 p( L) c( F' ]6 w3 m
  12.             "compilerPath": "/home/chen/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc",
      \; P" [! M: ?
  13.             "cStandard": "c99",
    ! A# W! A; a* ~1 g
  14.             "cppStandard": "gnu++14",5 S. z" _% ]0 A* B
  15.             "intelliSenseMode": "gcc-arm"
    ) T9 W+ O( t& t3 m
  16.         }
    ( ~! o8 b- }( ^( x$ i
  17.     ],' W, z- \& Y6 T  u6 ?/ A
  18.     "version": 4* K: d$ t0 o: F/ o  s. w. b
  19. }
复制代码
+ Y. Z; H. b. V) P! Y
2.2 task.json
4 s" ]5 P1 B7 P' w, g) {修改好task.json即可使用ctrl+shift+B对stm32工程进行编译
# V# I% b  `5 ^; S7 U8 P8 ]2 e2 J; O- Y/ k
微信图片_20240615144348.png   S5 p# e8 T8 [& f, e7 I

; I2 M9 Z6 o5 `6 |
) R1 n: c: R, Z+ p
微信图片_20240615144344.png 6 F4 t; w+ w3 T$ d
, r+ Z' Q) Z6 p# i& v$ U' d  Z( _, W
: D+ v( P1 _7 D
微信图片_20240615144341.png
- U: O. u  k7 Y& @* f" K
/ S7 I8 f1 I$ U1 |) t; _, `0 E8 h3 @0 u$ W
修改如下:
; F4 }2 Z6 r+ t; z+ W9 H8 n
  1. {" m3 k2 B7 l4 h' i0 s
  2.     // See https://go.microsoft.com/fwlink/?LinkId=733558
    1 ^/ A2 l$ Y: m$ i
  3.     // for the documentation about the tasks.json format
    " m$ J& i- C/ b6 q& a" f. Q
  4.     "version": "2.0.0",7 O3 T$ D3 F& b2 u% {  w9 z( z
  5.     "tasks": [( C/ Q! S0 O& q" g) S0 ]4 l* G3 t
  6.         {9 R" ?  L. T# A; {2 R5 l& G/ [6 f
  7.           "label": "Build",      // 标签名' D$ [2 J. g% o) L' Z1 ]
  8.           "type": "shell",      // 终端(shell)型的任务) N& Y3 q- n9 c" o4 i3 H
  9.           "command": "make",    // *执行的命令
    * B6 y; u5 ^1 I: s0 ?1 q
  10.           "args": [          // 命令的参数+ @8 Y7 \1 H7 X, ~2 }* d
  11.             "-j6"          // 表示六线程同时编译7 w( W, J9 Y; j& Q# h6 q8 d) r
  12.           ],
    / k/ a9 K& I, x8 }+ P- ]4 M: u
  13.           "problemMatcher": ["$gcc"],  // 表示如果出现了编译报错,问题面板会显示 gcc类型的错误
    6 t. d0 X. v! B" F
  14.           "group": {+ Z2 R2 p0 q1 ~  p, ^& G/ z$ J
  15.             "kind": "build",* `- @) G) r3 U
  16.             "isDefault": true
    ( Y% d( u# A) C/ d' b# |1 i2 u: M
  17.           }6 J' U; P7 ~6 n3 m4 \& ~5 _% @
  18.         }6 t* m8 Y, `$ x9 B. ?2 {3 [

  19. ; I. E2 d! w4 W6 y  o. T
  20. }
复制代码

1 p0 r4 m, W% K1 V$ B  W2.3 settings.json
' p0 C- _# S) [5 _) J
9 r; j: D; |- c
3、上述”defines“字段中的定义应包括stm32cubemx生成的Makefile文件中相关的宏定义
' r/ `8 w0 Q& d6 i$ d2 D  m7 X! r9 F  y& |( l% u! {% K# \
微信图片_20240615144337.png
7 W( X4 \2 Y+ F9 z- R. l! i+ E3 q% }6 q; [+ c
4、make编译/使用ctrl+shift+B
, y7 Y; h$ @: [$ ?7 N
  1. chen@ubuntu:~/prj_vscode/linux-stm32/test$ make clean, V0 l; H5 \: c7 L* y
  2. chen@ubuntu:~/prj_vscode/linux-stm32/test$ make
复制代码
3 b8 s: M' N) W* F
10调试
2 M& g. S7 @! a2 G. x( a) r1、安装openocd
  1. chen@ubuntu:~$ sudo apt-get install openocd
    0 A- F. l) K4 C- V; \
  2. chen@ubuntu:~$ whereis openocd
    1 f! m2 K  e- l; V
  3. openocd:
    / X4 w$ a3 a6 P4 v' t, k: a
  4. /usr/bin/openocd
    ( J8 `) m4 Z, [. z
  5. /usr/share/openocd & t4 N5 w4 z" g& V4 u3 k* R
  6. /usr/share/man/man1/openocd.1.gz
    " Y! ]2 O  U9 Y8 J, H+ w
  7. /usr/share/info/openocd.info-2.gz
    4 k  L. p/ [* H4 T* i( j! o- M8 L
  8. /usr/share/info/openocd.info.gz 3 D: g. x7 `0 U
  9. /usr/share/info/openocd.info-1.gz
    4 v& T( m) U% B: {
  10. chen@ubuntu:~$ & n, s9 [; L  A) [
  11. //连接好stlinkv2-1到电脑和pcb板,输入以下命令。完成连接6 X3 i+ h, L) n8 |
  12. chen@ubuntu:/usr/share/openocd/scripts$ openocd -f interface/stlink-v2.cfg -f target/stm32f1x_stlink.cfg
复制代码

0 i. b* n/ w* Z) [6 A+ r: h2、在tasks.json中使用openocd进行编译后的下载
+ Y" }' q* M4 D4 u" d# f, j  @9 q! P9 I+ f7 t, h, F* C
在原本的tasks.json中追加popenocd的相关内容
" |- ]% H& |* k/ E2 F
  1. {2 q! Q( f* f6 g8 v
  2.     // See https://go.microsoft.com/fwlink/?LinkId=7335586 j+ h: w0 U6 Y3 f; T- R$ r
  3.     // for the documentation about the tasks.json format  ^8 M3 }" G  t& z; D
  4.     "version": "2.0.0",
    4 R* R0 w; e& ~8 c
  5.     "tasks": [
    7 p6 Z6 C% V6 o9 T& e
  6.         {' a! i8 I  b- w1 j
  7.           "label": "Build",      // 标签名/ m2 C4 u* v; h* U
  8.           "type": "shell",      // 终端(shell)型的任务
    ! M) C; _- r8 z, ^* N
  9.           "command": "make",    // *执行的命令
    1 b" Y, p) z3 ^0 N
  10.           "args": [          // 命令的参数
    . [" A7 U9 Q0 B) Y  j
  11.                           // "-j6"表示六线程同时编译
    * o3 e" D3 S1 M$ M! ^4 m
  12.           ],4 |' r5 G2 ?- z6 y) S$ _* Z
  13.           //"problemMatcher": ["$gcc"],  // 表示如果出现了编译报错,问题面板会显示 gcc类型的错误
    0 M. Y- @1 o& ~5 P; R# L9 G
  14.           "group": {
      T$ J1 X& R4 H
  15.             "kind": "build",) R1 U- m) ?# H) _- e9 e
  16.             "isDefault": true. v( W  l6 N! a% m, s! f! M" t
  17.           }! z! U2 b( s7 q- w1 d1 c
  18.         },
    # X9 a2 p; b& f) h  R
  19.         {
    , g2 e/ s* h6 x" ^7 I, c2 G
  20.             "dependsOn":"Build",          // 任务依赖
    9 ?3 M# Q6 N1 p& ~. ?7 G6 W
  21.             "label": "Build and Download",2 t7 p1 O/ Q, A* _3 {. i
  22.             "type": "shell",
    9 j+ e% }* Q2 ~/ G6 m
  23.             "command": "openocd",! E8 w# d: X* }' y0 ~+ n7 e
  24.             "args": [2 I5 V9 i: B$ t9 s* ^9 `9 I. J" a
  25.             "-f",0 A6 l4 A/ ~6 k; k/ C' Z# G& A
  26.             "/usr/share/openocd/scripts/interface/stlink-v2.cfg",6 T) n; S5 s# t+ o
  27.             "-f",
    $ |0 d; W& b" W
  28.             "/usr/share/openocd/scripts/target/stm32f1x_stlink.cfg",
    5 L, h+ E8 F$ F7 S
  29.             "-c"," }, ^$ T: V; [  i" e
  30.             "program ./build/${workspaceFolderBasename}.elf verify reset exit"
    ; W& s9 a. H. C( H
  31.             ],2 ]+ W; \2 n* h- k: V6 |# j
  32.             "problemMatcher": []
    1 P! a* c5 I- J% p8 d7 C2 c
  33.         }
    . d& W1 s) Z# L1 Y, j
  34.         
    . w* ?; s  ^: ^" f9 D
  35. 2 e3 J7 X) J8 D
  36. }
复制代码
/ m3 s# H: r- t  w% T0 X$ o
这个任务要求先进行 Build任务,然后通过命令打开 openocd,加载两个配置文件(-f interface/stlink.cfg 和 -f target/stm32l1.cfg),同时,这个 -c 的选项后面跟着的就是openocd成功加载后执行的内置命令,这里我们让它下载、验证、reset,然后直接退出,这样就实现了一条龙式的下载。
/ T7 O1 Y7 [% _: n4 b: [& V5 g  @  k! b9 ^/ H$ ]' v
微信图片_20240615144333.png
; q' A! L3 B* G, m( _& s
4 T- c9 ^1 z# w0 I' j) ? 微信图片_20240615144330.png 1 h6 [. ^( ~% j8 P$ b2 o; B9 x
) f5 ]  |5 S: I$ ]7 A+ k
3、使用openocd进行调试( B: ]9 D* Q4 S
3.1 安装Cortex Debug和Venus's Cortex-Debug插件(第二个插件一定要安装,否则会报错Failed to launch OpenOCD GDB Server: Timeout.)" |/ q7 v) j( A6 F: Y" ]; l

  i7 ?' ^0 I$ G0 Q, \
微信图片_20240615144327.png
$ M' r* [+ O$ g( E
$ W0 j: ?) e/ ^# |. m- g& s, J) U 微信图片_20240615144324.png   H! y3 P2 F" A7 @, _1 ?3 z3 g

6 Q" g+ C: n# B4 j3.2、 添加调试配置
2 d" u* @8 F4 h$ y! X* ?" R
5 R& o3 w7 y" K5 v% b' t4 n5 u
微信图片_20240615144321.png 8 O! D4 I7 a( C! X  }' e
. s" b; g% |/ A( N. A) D
3.3 选择cortex debug,生成launch.json,并修改内容如下
2 m2 Y8 j9 z4 K4 K# p6 k
; B. j  w1 K1 Y6 v
微信图片_20240615144318.png 4 S. M$ k# ]/ s$ F7 O* e6 J
. \% |& I: Y/ e
  1. {
    . L& |( Z+ v) O; j) h
  2.     // 使用 IntelliSense 了解相关属性。( Y' {' n3 {3 T1 \, ~& H
  3.     // 悬停以查看现有属性的描述。( e6 D3 a) k  k# S8 D3 t$ p7 X# P
  4.     // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    3 M% U6 N: M3 @: h- I) h
  5.     "version": "0.2.0",
    7 Z% R+ c# f# @# K( m  y8 g
  6.     "configurations": [$ x2 b  _- J  V7 F$ J" b2 t; _
  7.         {7 `+ T0 f5 m8 Y
  8.             "name": "Cortex Debug",
    ; i/ j0 h& x3 @
  9.             "cwd": "${workspaceRoot}",  // 输出路径- l+ K& T# W% {2 c1 Z% b! O
  10.             "executable": "./build/${workspaceRootFolderName}.elf",    // 要调试的程序* F0 E5 c* U& W+ s8 w/ Y6 M
  11.             "request": "launch",
    + M& a6 L& ]1 i2 m) c/ }2 E
  12.             "type": "cortex-debug",
    , n/ ]. D* S1 ^! Q9 y: O/ Y
  13.             "servertype": "openocd",    // 调试器选择
    1 X' H- t2 x  U) o3 B/ d# k
  14.             "device": "STM32F103C8",    // 使用J-link GDB Server时必须;其他GBD Server时可选(有可能帮助自动选择 SVD文件)。支持的设备见 https://www.segger.com/downloads/supported-devices.php* B) y1 d$ B, k/ T  n4 \! {' L
  15.             //"svdFile": "./STM32L15xC.svd",  // svd文件,有这个文件才能查看寄存器的值,每个单片机都不同。可以在以下地址找到 https://github.com/posborne/cmsis-svd下载
    , ]/ x" k7 Y% |( c( \
  16.             "interface": "swd",! S: @8 |2 |6 h1 D( O. c  z
  17.             "configFiles": [0 G' a  d# v' S, D7 D& F( O
  18.                 //"${workspaceRoot}/openocd.cfg",  // *可以通过加载该文件来配置
    8 b; K! y. M. O, {7 [) V0 ^
  19.                 "/usr/share/openocd/scripts/interface/stlink-v2.cfg"," {2 S1 V. w- O# J$ A# [
  20.                 "/usr/share/openocd/scripts/target/stm32f1x.cfg"
    - L  F0 k. x; d2 z, v) W
  21.             ],
    ( a. p+ q0 m  k2 g
  22.             "runToEntryPoint": "false",
    8 X0 [: S4 d& w% \. q* w) Y
  23.             //"runToMain": false,
    4 b: Q' C: W, ?3 L; o" s8 }( n
  24.             "preLaunchTask": "Build",   // 在调试前预先执行的任务,此处是tasks.json中的
    6 O& h* C/ t4 q0 V* D  x* o
  25.             //"armToolchainPath": "C:\\Program Files (x86)\\GNU Tools ARM Embedded\\5.4 2016q3\\bin"    // 如果没有把 arm工具链路径添加到系统环境变量,则需要这条指令添加你的路径
    " m/ Z1 g5 p+ F/ ~
  26.         }
    4 n6 t# T1 m0 G* R, ]

  27. - Y5 Y5 Z# M( p( B% [6 {+ p8 _
  28. }
复制代码

! W% O. [+ a/ @' \0 G3.4 调试, l0 S! s7 \# e
2 W  K' E& P9 Y; a0 c0 Q( d
微信图片_20240615144314.png 1 u# c, |* ?* }+ I; I3 k3 N3 ~7 n

( W4 ?  y* r- t0 i2 h1 h& e( b11使用VSCode串口工具
% }- B( ]& q* J7 o  n6 c7 o
1、安装serial monitor
- E7 ^! q" X1 N; C+ z, ^
) A! w; R% n8 }2 [
微信图片_20240615144304.png 4 o( q  ?/ ]9 X6 N2 M8 [3 W. l2 Y6 Q

: `6 T- q1 K+ m* V+ r. ]2、查看连接的usb
7 Z6 M/ O: g# B& Y+ e
  1. chen@ubuntu:~$ lsusb3 z0 X) \$ P0 s, R
  2. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub! W7 o& z& p0 U( L4 u7 y
  3. Bus 002 Device 007: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
    % Z5 |" ?! [% P, Z( c
  4. Bus 002 Device 006: ID 0483:3748 STMicroelectronics ST-LINK/V2
    " j' h, F: J+ W6 M" C
  5. Bus 002 Device 004: ID 0e0f:0008 VMware, Inc. VMware Virtual USB Mouse9 D7 z, ]: m2 v. ]: U% x
  6. Bus 002 Device 003: ID 0e0f:0002 VMware, Inc. Virtual USB Hub0 U! \0 ~  o9 z/ [
  7. Bus 002 Device 002: ID 0e0f:0003 VMware, Inc. Virtual Mouse9 N- r2 Y- D$ K9 _0 G5 c# H
  8. Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    : t( L% f" Q+ @8 x$ Q( t
  9. chen@ubuntu:~$
复制代码

1 X1 q* K. f" g1 `2、加权限
  1. chen@ubuntu:~$ sudo chmod 777 /dev/ttyUSB0
复制代码

6 l0 c: j- s- U  T3 ~12使用git
' Q9 {2 `/ t: {- V9 x1、检查状态# k9 D0 `6 d; a3 M; A
  1. //在执行 git add 和 git commit 之前,先执行以下命令查看当前仓库的状态:  g4 g; ]" C: x! g9 b( Y
  2. git status
    / c' M& b1 ?6 A$ ~% D3 z0 m8 D
  3. //这将显示工作目录和暂存区的状态。确保没有未追踪的文件,也确保已将需要提交的文件添加到暂存区。
复制代码

& r7 W) a8 V- b2、确认 Git 配置:9 P! B0 B' ^, w4 P" _
  1. git config user.name
    8 |1 ]( @& g+ l
  2. git config user.email- i( h$ ^/ c( Y& |
  3. 8 s5 p9 O- V3 [0 F

  4. 1 M; i+ y) Y7 `" ~+ v
  5. 如果没有设置,请使用以下命令设置:
      r3 V( c# H+ g* I! J' [, B; ?' L
  6. git config --global user.name "Your Name"* d5 [/ s; B! I- }* _& T
  7. git config --global user.email "your.email@example.com"
复制代码
5 Y+ V6 m4 t' p- f: {& l3 O
3、检查文件权限3 M# L$ `0 j  }! o0 l/ P
确保你有权限在新文件夹中进行写操作。使用 ls -l 命令检查文件和目录的权限:
, Q9 i4 _- C' |4 v2 n9 Wls -l
4 _$ B) ~- y$ `2 }
% }0 Z8 S" y+ u; A. L2 u7 w7 p
4、查看错误信息:
7 t( M. Q: c; m" O+ Z* \3 k2 W' V如果 git commit 操作没有反应,尝试使用 -v 参数查看详细的错误信息:/ ?8 ^! V% K) U5 T
git commit -v& f" M% q- n; x: c- I0 D- {

6 `: l) s% Y% i% p
5、清理缓存:3 ?! u- A9 A; P( ?" E, [
有时 Git 的内部缓存可能导致问题。尝试清理 Git 缓存并重新尝试:9 p4 l! U% ]0 ]+ b0 K( s1 a2 x3 o
git rm -r --cached .
5 ?# f  _9 c) e! L( Cgit add .6 l6 Z4 n, A  l7 L* z; b: s
git commit -m "Your commit message"+ k& I2 p7 M% C4 t, f& ^
这将清理缓存并重新添加和提交所有更改。7 t: A$ y( d; g/ o2 O

3 I) F% k2 c8 v! s- _- w; ~; F% s

) P9 Q$ p# _# X* Y. a转载自:kevin. C. s5 n6 j4 Q) P1 W

9 c) K, i/ e' J- i3 t) N如有侵权请联系删除0 n9 H5 x: A% U5 K2 e* {

9 x5 N! h7 B0 w9 {, n$ \, K

2 a8 _) g8 Z7 ]! b) V8 q- u7 c* L$ Q% q8 E# g& W$ p( i
1.png
赞 收藏 评论0 发布时间:2024-6-15 14:45

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版