stm32f103单片机开发

安装开发环境

单片机编译环境是用的 pio core官方网站

单片机开发板选择的是 qemu-system-gnuarmeclipse 软件模拟,qemu-system-gnuarmeclipse 能够模拟多款STM32的开发板,比如常见的 BluePill STM32F103C8T6 更多信息参考 https://xpack.github.io/dev-tools/qemu-arm

1
2
3
4
5
6
7
8
wget -O get-platformio.py https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py
python3 get-platformio.py
npm install --global xpm@latest
xpm install --global @xpack-dev-tools/qemu-arm@latest --verbose
# 将 qemu-system-gnuarmeclipse 路径加入系统
export QEMUARM=/home/hch/.local/xPacks/@xpack-dev-tools/qemu-arm/7.2.5-1.1/.content 
export PATH=$QEMUARM/bin:$PATH
export LD_LIBRARY_PATH=$QEMUARM/libexec:$LD_LIBRARY_PATH

开始嵌入式的HelloWorld: 闪灯

1
2
3
4
5
6
7
8
mkdir -p /data/stm32f103 && /data/stm32f103
pio init -b bluepill_f103c8 --ide vscode -O "framework=arduino" -O "upload_protocol=jlink" -O "debug_tool=jlink"
# 参考 https://github.com/platformio/platform-ststm32/tree/develop/examples
wget https://github.com/platformio/platform-ststm32/raw/develop/examples/arduino-blink/src/Blink.cpp -O src/Blink.cpp  
# 编译
pio run 
# 运行
qemu-system-gnuarmeclipse -board BluePill --image .pio/build/bluepill_f103c8/firmware.bin

终端会输出:

 [led:red off]
 [led:red on]
 [led:red off]

并且会打开一个图形窗口,LED 会闪烁 stm32f103_blink.gif

现在我们可以开始愉快地编写嵌入式应用了。 如果你觉得stm32f103性能太弱,那么可以试试其它支持的开发板。查看支持的开发板:

1
qemu-system-gnuarmeclipse -board help

想要模拟更强大的开发板,可以用 qemu-system-arm 查看支持的开发板:

1
qemu-system-arm -machine help

来硬的

也许你会想: 在模拟环境下能运行,那么在真实开发板上能运行吗?

连接硬件如图: jlink2stm32f103.jpg

在终端输入如下命令上传代码:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
hch@debian:~/data/stm32f103$ pio run -t upload
Processing bluepill_f103c8 (platform: ststm32; board: bluepill_f103c8; framework: arduino)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/bluepill_f103c8.html
PLATFORM: ST STM32 (15.2.0) > BluePill F103C8
HARDWARE: STM32F103C8T6 72MHz, 20KB RAM, 64KB Flash
DEBUG: Current (jlink) External (blackmagic, cmsis-dap, jlink, stlink)
PACKAGES: 
 - framework-arduinoststm32 @ 4.20100.211028 (2.1.0) 
 - framework-cmsis @ 2.50700.210515 (5.7.0) 
 - tool-dfuutil @ 1.9.200310 
 - tool-jlink @ 1.77001.0 (7.70.1) 
 - tool-openocd @ 2.1100.211028 (11.0) 
 - tool-stm32duino @ 1.0.1 
 - toolchain-gccarmnoneeabi @ 1.90201.191206 (9.2.1)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 11 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Checking size .pio/build/bluepill_f103c8/firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   4.0% (used 828 bytes from 20480 bytes)
Flash: [==        ]  17.1% (used 11192 bytes from 65536 bytes)
Configuring upload protocol...
AVAILABLE: blackmagic, cmsis-dap, dfu, jlink, mbed, stlink
CURRENT: upload_protocol = jlink
Uploading .pio/build/bluepill_f103c8/firmware.bin
SEGGER J-Link Commander V7.70a (Compiled Aug 10 2022 16:32:44)
DLL version V7.70a, compiled Aug 10 2022 16:32:29


J-Link Command File read successfully.
Processing script file...
J-Link>h
J-Link connection not established yet but required for command.
Connecting to J-Link via USB...O.K.
Firmware: J-Link OB-STM32F072-CortexM compiled Mar  8 2020 11:15:54
Hardware version: V1.00
J-Link uptime (since boot): N/A (Not supported by this model)
S/N: -1
License(s): GDB, RDI, FlashBP, FlashDL, JFlash, RDDI
VTref=3.300V
Target connection not established yet but required for command.
Device "STM32F103C8" selected.


Connecting to target via SWD
InitTarget() start
Can not attach to CPU. Trying connect under reset.
Connecting to CPU via connect under reset failed.
InitTarget() end
Connect failed. Resetting via Reset pin and trying again.
InitTarget() start
Can not attach to CPU. Trying connect under reset.
Connecting to CPU via connect under reset failed.
InitTarget() end
InitTarget() start
Can not attach to CPU. Trying connect under reset.
Connecting to CPU via connect under reset failed.
InitTarget() end
Connect failed. Resetting via Reset pin and trying again.
InitTarget() start
Can not attach to CPU. Trying connect under reset.
Connecting to CPU via connect under reset failed.
InitTarget() end
Cannot connect to target.
J-Link>loadbin .pio/build/bluepill_f103c8/firmware.bin, 0x08000000
Target connection not established yet but required for command.
Device "STM32F103C8" selected.
Connecting to target via SWD
InitTarget() start
Can not attach to CPU. Trying connect under reset.
Connecting to CPU via connect under reset failed.
InitTarget() end
Connect fallback: Reset via Reset pin & Connect.
InitTarget() start
Can not attach to CPU. Trying connect under reset.
Connecting to CPU via connect under reset failed.
InitTarget() end
InitTarget() start
Can not attach to CPU. Trying connect under reset.
Connecting to CPU via connect under reset failed.
InitTarget() end
Connect fallback: Reset via Reset pin & Connect.
InitTarget() start
Can not attach to CPU. Trying connect under reset.
Connecting to CPU via connect under reset failed.
InitTarget() end
Cannot connect to target.
J-Link>r
Target connection not established yet but required for command.
Device "STM32F103C8" selected.


Connecting to target via SWD
InitTarget() start
Can not attach to CPU. Trying connect under reset.
Connecting to CPU via connect under reset failed.
InitTarget() end
Connect fallback: Reset via Reset pin & Connect.
InitTarget() start
Can not attach to CPU. Trying connect under reset.
Connecting to CPU via connect under reset failed.
InitTarget() end
InitTarget() start
Can not attach to CPU. Trying connect under reset.
Connecting to CPU via connect under reset failed.
InitTarget() end
Connect fallback: Reset via Reset pin & Connect.
InitTarget() start
Can not attach to CPU. Trying connect under reset.
Connecting to CPU via connect under reset failed.
InitTarget() end
Cannot connect to target.
J-Link>q

Script processing completed.

从结果看, 烧录代码失败了, 分析原因是 pio 使用的 J-Link Commander V7.70a, 而我的JLINK是自己做的, 只能支持老版本的 JLinkExe。

手动从 https://www.segger.com/downloads/jlink/ 下载 JLink_Linux_V512j_x86_64.tgz 并压,添加路径到系统查找路径。

在当前目录建立文件 upload.jlink 然后手动烧录 :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
hch@debian:~/data/stm32f103$ cat upload.jlink 
 r
 h
 loadfile .pio/build/bluepill_f103c8/firmware.bin 0x8000000
 r
 g
 q

# 手动烧录程序 
hch@debian:~/data/stm32f103$ JLinkExe -If swd -Device STM32F103C8 -speed auto -commanderscript upload.jlink
SEGGER J-Link Commander V5.12j (Compiled Jul 18 2016 18:53:43)
DLL version V5.12j, compiled Jul 18 2016 18:53:38


Script file read successfully.
Processing script file...

J-Link connection not established yet but required for command.
Connecting to J-Link via USB...O.K.
Firmware: J-Link OB-STM32F072-CortexM compiled Mar  8 2020 11:15:54
Hardware version: V1.00
S/N: -1
License(s): GDB, RDI, FlashBP, FlashDL, JFlash, RDDI
VTref = 3.300V
Target connection not established yet but required for command.
Device "STM32F103C8" selected.
Found SWD-DP with ID 0x2BA01477
Found SWD-DP with ID 0x2BA01477
Found Cortex-M3 r2p1, Little endian.
FPUnit: 6 code (BP) slots and 2 literal slots
CoreSight components:
ROMTbl 0 @ E00FF000
ROMTbl 0 [0]: FFF0F000, CID: B105E00D, PID: 000BB000 SCS
ROMTbl 0 [1]: FFF02000, CID: B105E00D, PID: 003BB002 DWT
ROMTbl 0 [2]: FFF03000, CID: B105E00D, PID: 002BB003 FPB
ROMTbl 0 [3]: FFF01000, CID: B105E00D, PID: 003BB001 ITM
ROMTbl 0 [4]: FFF41000, CID: B105900D, PID: 003BB923 TPIU-Lite
ROMTbl 0 [5]: FFF42000, CID: B105900D, PID: 003BB924 ETM-M3


Found SWD-DP with ID 0x2BA01477
Found SWD-DP with ID 0x2BA01477
Found Cortex-M3 r2p1, Little endian.
FPUnit: 6 code (BP) slots and 2 literal slots
CoreSight components:
ROMTbl 0 @ E00FF000
ROMTbl 0 [0]: FFF0F000, CID: B105E00D, PID: 000BB000 SCS
ROMTbl 0 [1]: FFF02000, CID: B105E00D, PID: 003BB002 DWT
ROMTbl 0 [2]: FFF03000, CID: B105E00D, PID: 002BB003 FPB
ROMTbl 0 [3]: FFF01000, CID: B105E00D, PID: 003BB001 ITM
ROMTbl 0 [4]: FFF41000, CID: B105900D, PID: 003BB923 TPIU-Lite
ROMTbl 0 [5]: FFF42000, CID: B105900D, PID: 003BB924 ETM-M3
Cortex-M3 identified.
Reset delay: 0 ms
Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.

PC = 0800015C, CycleCnt = 00000000
R0 = 00000000, R1 = 00000000, R2 = 00000000, R3 = 00000000
R4 = 00000000, R5 = 00000000, R6 = 00000000, R7 = 00000000
R8 = 00000000, R9 = 00000000, R10= 00000000, R11= 00000000
R12= 00000000
SP(R13)= 20005000, MSP= 20005000, PSP= 00000000, R14(LR) = FFFFFFFF
XPSR = 01000000: APSR = nzcvq, EPSR = 01000000, IPSR = 000 (NoException)
CFBP = 00000000, CONTROL = 00, FAULTMASK = 00, BASEPRI = 00, PRIMASK = 00

Downloading file [.pio/build/bluepill_f103c8/firmware.bin]...

**************************
WARNING: CPU is running at low speed (5262 kHz).
**************************

Comparing flash   [100%] Done.
Erasing flash     [100%] Done.
Programming flash [100%] Done.
Verifying flash   [100%] Done.
J-Link: Flash download: Flash programming performed for 1 range (12288 bytes)
J-Link: Flash download: Total time needed: 0.293s (Prepare: 0.081s, Compare: 0.008s, Erase: 0.025s, Program: 0.168s, Verify: 0.002s, Restore: 0.007s)
O.K.

Reset delay: 0 ms
Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.


Script processing completed.

看来是烧录成功了, 开发板也按预期的工作了。

stm32f103_blink2.gif

也许你要想,有开发板为什么还要用模拟呢?原因是多方面的, 比如我再次烧录程序时:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
hch@debian:~/data/stm32f103$ JLinkExe -If swd -Device STM32F103C8 -speed auto -commanderscript upload.jlink
SEGGER J-Link Commander V5.12j (Compiled Jul 18 2016 18:53:43)
DLL version V5.12j, compiled Jul 18 2016 18:53:38


Script file read successfully.
Processing script file...

J-Link connection not established yet but required for command.
Connecting to J-Link via USB...O.K.
Firmware: J-Link OB-STM32F072-CortexM compiled Mar  8 2020 11:15:54
Hardware version: V1.00
S/N: -1
License(s): GDB, RDI, FlashBP, FlashDL, JFlash, RDDI
VTref = 3.300V
Target connection not established yet but required for command.
Device "STM32F103C8" selected.
STM32 (connect): Can not attach to CPU. Trying connect under reset.

****** Error: STM32: Connecting to CPU via connect under reset failed.
STM32 (connect): Can not attach to CPU. Trying connect under reset.

****** Error: Communication timed out: Requested 20 bytes, received 0 bytes !
Could not read hardware status!
STM32: Connecting to CPU via connect under reset failed.


Selected interface (1) is not supported by connected debug probe.
Reset delay: 0 ms
Reset type UNKNOWN: ???

PC: (R15) = 00000000, CPSR = 00000000 (Unknown mode, ARM)
Current:
     R0 =00000000, R1 =00000000, R2 =00000000, R3 =00000000
     R4 =00000000, R5 =00000000, R6 =00000000, R7 =00000000
USR: R8 =00000000, R9 =00000000, R10=00000000, R11=00000000, R12=00000000
     R13=00000000, R14=00000000
FIQ: R8 =00000000, R9 =00000000, R10=00000000, R11=00000000, R12=00000000
     R13=00000000, R14=00000000, SPSR=00000000
IRQ: R13=00000000, R14=00000000, SPSR=00000000
SVC: R13=00000000, R14=00000000, SPSR=00000000
ABT: R13=00000000, R14=00000000, SPSR=00000000
UND: R13=00000000, R14=00000000, SPSR=00000000

Downloading file [.pio/build/bluepill_f103c8/firmware.bin]...
Writing target memory failed.

Reset delay: 0 ms
Reset type UNKNOWN: ???



Script processing completed.

看,任何东西都没有改变, 却烧录失败了。 虽然都能想法处理,但总还是浪费时间, 最重要的是可以让我们把注意力放在最重要的事情上。

updatedupdated2024-02-292024-02-29