STM32/GD32
本文主要介绍STM32/GD32开发相关内容。
1. STM32
芯片
STM32F407ZGT6
开发工具
IDE: keil5
器件库:
Keil.STM32F4xx_DFP.2.17.1.pack
烧写工具:STlink、Jlink-OB
Helloworld
修改输入晶振频率,默认为25MHz,根据设计修改为8MHz
修改stm32f4xx.h
#define HSE_VALUE ((uint32_t)8000000)
bootloader
采用rhthrad的OTA
https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/application-note/system/rtboot/an0028-rtboot
生成后烧写到0x8000000地址,重启
__ ___ __ __ __ ___
|__) | __ |__) / \ / \ |
| \ | |__) \__/ \__/ |
2006 - 2019 Copyright by rt-thread team
0.9.2 build Nov 28 2023
[D/FAL] (fal_flash_init:63) Flash device | onchip_flash_16k_part | addr: 0x08000000 | len: 0x00020000 | blk_size: 0x00004000 |initialized finish.
[D/FAL] (fal_flash_init:63) Flash device | onchip_flash | addr: 0x08020000 | len: 0x000e0000 | blk_size: 0x00020000 |initialized finish.
[32;22m[I/FAL] ==================== FAL partition table ====================[0m
[32;22m[I/FAL] | name | flash_dev | offset | length |[0m
[32;22m[I/FAL] -------------------------------------------------------------[0m
[32;22m[I/FAL] | app | onchip_flash | 0x00000000 | 0x00020000 |[0m
[32;22m[I/FAL] | download | onchip_flash | 0x00020000 | 0x00020000 |[0m
[32;22m[I/FAL] =============================================================[0m
[32;22m[I/FAL] RT-Thread Flash Abstraction Layer (V0.4.0) initialize success.[0m
[32;22m[I/FAL] System initialization successful.[0m
[I]RT-Thread OTA package(V0.2.1) initialize success.
[E]Get firmware header occur CRC32(calc.crc: 7b93c5c8 != hdr.info_crc32: ffffffff) error on 'download' partition!
[E]Get OTA download partition firmware header failed!
[E]Get firmware header occur CRC32(calc.crc: 7b93c5c8 != hdr.info_crc32: ffffffff) error on 'app' partition!
[I]Begin to execute the program on app partition.
[E/FAL] Can't find user firmware on app partition. (Address: 0x08020000, Data: 0xffffffff)
[E/FAL] Bootloader stop.
APP修改
修改system_stm32f4xx.c中的向量地址
/* #define VECT_TAB_SRAM */
#define VECT_TAB_OFFSET 0x20000
编译后烧写即可
__ ___ __ __ __ ___
|__) | __ |__) / \ / \ |
| \ | |__) \__/ \__/ |
2006 - 2019 Copyright by rt-thread team
0.9.2 build Nov 28 2023
[D/FAL] (fal_flash_init:63) Flash device | onchip_flash_16k_part | addr: 0x08000000 | len: 0x00020000 | blk_size: 0x00004000 |initialized finish.
[D/FAL] (fal_flash_init:63) Flash device | onchip_flash | addr: 0x08020000 | len: 0x000e0000 | blk_size: 0x00020000 |initialized finish.
[32;22m[I/FAL] ==================== FAL partition table ====================[0m
[32;22m[I/FAL] | name | flash_dev | offset | length |[0m
[32;22m[I/FAL] -------------------------------------------------------------[0m
[32;22m[I/FAL] | app | onchip_flash | 0x00000000 | 0x00020000 |[0m
[32;22m[I/FAL] | download | onchip_flash | 0x00020000 | 0x00020000 |[0m
[32;22m[I/FAL] =============================================================[0m
[32;22m[I/FAL] RT-Thread Flash Abstraction Layer (V0.4.0) initialize success.[0m
[32;22m[I/FAL] System initialization successful.[0m
[I]RT-Thread OTA package(V0.2.1) initialize success.
[E]Get firmware header occur CRC32(calc.crc: 7b93c5c8 != hdr.info_crc32: ffffffff) error on 'download' partition!
[E]Get OTA download partition firmware header failed!
[E]Get firmware header occur CRC32(calc.crc: 7b93c5c8 != hdr.info_crc32: ffffffff) error on 'app' partition!
[I]Begin to execute the program on app partition.
[32;22m[I/FAL] Find user firmware at app partition 0x08020000 successfully.[0m
[32;22m[I/FAL] Bootloader jumps to user firmware now.[0m
HelloWorld!
2. STlink使用RTT调试
STM32/GD32的串口不够用时,可以使用SWD接口进行调试,jlink可以使用rtt,stlink没有现成的工具。通过pyocd可以在STlink上实现该功能。
2.1 Windows版本安装pyocd
先安装python,pip
参考文章python安装
第1种方法:
指定为国内源,安装很快:
pip install pyocd -i https://pypi.tuna.tsinghua.edu.cn/simple
网速慢安装失败可以采用第2种方法
第2种方法:
下载源码
https://github.com/mbedmicro/pyOCD.git
安装
python setup.py install
采用国内源:
pip install pyocd -i https://pypi.tuna.tsinghua.edu.cn/simple
安装完后在命令行查看已连接的下载器,可以看出连接了STLink
一般直接运行pyocd即可,如果出现这种情况
C:\Users\xx>pyocd
'pyocd' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
可以在前面加上python -m
python -m pyocd -help
C:\Users\xx>pyocd list -p
# Probe/Board Unique ID Target
--------------------------------------------------------
0 STM32 STLink 1711141A2115303030303032 n/a
C:\Users\xx>pyocd -V
0.36.0
C:\Users\xx>pyocd --help
usage: pyocd [-h] [-V] [--help-options] ...
PyOCD debug tools for Arm Cortex devices
options:
-h, --help show this help message and exit
-V, --version show program's version number and exit
--help-options Display available session options.
subcommands:
commander (cmd)
Interactive command console.
erase Erase entire device flash or specified sectors.
load (flash) Load one or more images into target device memory.
gdbserver (gdb)
Run the gdb remote server(s).
json Output information as JSON.
list List information about probes, targets, or boards.
pack Manage CMSIS-Packs for target support.
reset Reset a target device.
server Run debug probe server.
rtt SEGGER RTT Viewer/Logger.
安装芯片包
查看已安装的芯片包
未安装时:
C:\Users\xx>pyocd pack -s
Vendor Pack Version
--------------------------
更新芯片包索引
C:\Users\yw>pyocd pack -u
0000529 I Updating pack index... [pack_cmd]
Downloading descriptors (140/1067)
会在C:\Users\xx\AppData\Local\cmsis-pack-manager\cmsis-pack-manager目录中下载很多*.pdsc文件,有1000多个文件
下载完之后:
C:\Users\xx>pyocd pack find gd32f427
Part Vendor Pack Version Installed
---------------------------------------------------------------------------
GD32F427IE GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427IG GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427IK GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427RE GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427RG GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427RK GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427VE GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427VG GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427VK GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427ZE GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427ZG GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427ZK GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
或者
PS D:\work> python -m pyocd pack find gd32f427
Part Vendor Pack Version Installed
---------------------------------------------------------------------------
GD32F427IE GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427IG GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427IK GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427RE GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427RG GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427RK GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427VE GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427VG GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427VK GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427ZE GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427ZG GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
GD32F427ZK GigaDevice GigaDevice.GD32F4xx_DFP 3.0.3 False
PS D:\work> python -m pyocd pack find stm32f407
Part Vendor Pack Version Installed
---------------------------------------------------------------------------------
STM32F407IEHx STMicroelectronics Keil.STM32F4xx_DFP 2.17.1 False
STM32F407IETx STMicroelectronics Keil.STM32F4xx_DFP 2.17.1 False
STM32F407IGHx STMicroelectronics Keil.STM32F4xx_DFP 2.17.1 False
STM32F407IGTx STMicroelectronics Keil.STM32F4xx_DFP 2.17.1 False
STM32F407VETx STMicroelectronics Keil.STM32F4xx_DFP 2.17.1 False
STM32F407VGTx STMicroelectronics Keil.STM32F4xx_DFP 2.17.1 False
STM32F407ZETx STMicroelectronics Keil.STM32F4xx_DFP 2.17.1 False
STM32F407ZGTx STMicroelectronics Keil.STM32F4xx_DFP 2.17.1 False
在线安装芯片包
C:\Users\xx>pyocd pack install gd32f427re
Downloading packs (press Control-C to cancel):
GigaDevice.GD32F4xx_DFP.3.0.3
Downloading descriptors (001/001)
下载后生成文件:
C:\Users\xx\AppData\Local\cmsis-pack-manager\cmsis-pack-manager\GigaDevice\GD32F4xx_DFP\3.0.3.pack
C:\Users\yw>pyocd pack install gd32f427re
Downloading packs (press Control-C to cancel):
GigaDevice.GD32F4xx_DFP.3.0.3
Downloading descriptors (001/001)
C:\Users\xx>pyocd pack -s
Vendor Pack Version
----------------------------------------
GigaDevice GD32F4xx_DFP 3.0.3
在线下载比较慢,可以单独在网站上下载
已STM32为例,下载文件Keil.STM32F4xx_DFP.2.17.1.pack
按照文件名新建
C:\Users\xx\AppData\Local\cmsis-pack-manager\cmsis-pack-manager\Keil\STM32F4xx_DFP\2.17.1.pack
已经找到了STM32的芯片包
C:\Users\xx>pyocd pack -s
Vendor Pack Version
----------------------------------------
GigaDevice GD32F4xx_DFP 3.0.3
Keil STM32F4xx_DFP 2.17.1
之后就能连接芯片调试了:
C:\Users\xx>pyocd rtt -t GD32F427RE
0000927 I Target type is gd32f427re [board]
Exception in thread load-svd:
Traceback (most recent call last):
File "C:\Users\xx\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
self.run()
File "C:\Users\xx\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyocd\debug\svd\loader.py", line 72, in run
self._svd_location.load()
File "C:\Users\xx\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyocd\debug\svd\loader.py", line 48, in load
self.device = SVDParser.for_xml_file(self.filename).get_device()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\yxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyocd\debug\svd\parser.py", line 74, in for_xml_file
0000936 I DP IDR = 0x2ba01477 (v1 rev2) [dap]
return cls(ET.parse(path), remove_reserved)
^^^^^^^^^^^^^^
File "C:\Users\xx\AppData\Local\Programs\Python\Python311\Lib\xml\etree\ElementTree.py", line 1218, in parse
tree.parse(source, parser)
File "C:\Users\xx\AppData\Local\Programs\Python\Python311\Lib\xml\etree\ElementTree.py", line 580, in parse
self._root = parser._parse_whole(source)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
xml.etree.ElementTree.ParseError: XML or text declaration not at start of entity: line 1, column 2
0000941 I AHB-AP#0 IDR = 0x24770011 (AHB-AP var1 rev2) [discovery]
0000944 I AHB-AP#0 Class 0x1 ROM table #0 @ 0xe00ff000 (designer=751 part=e33) [rom_table]
0000947 I [0]<e000e000:SCS v7-M class=14 designer=43b:Arm part=00c> [rom_table]
0000952 I [1]<e0001000:DWT v7-M class=14 designer=43b:Arm part=002> [rom_table]
0000954 I [2]<e0002000:FPB v7-M class=14 designer=43b:Arm part=003> [rom_table]
0000955 I [3]<e0000000:ITM v7-M class=14 designer=43b:Arm part=001> [rom_table]
0000957 I [4]<e0040000:TPIU M4 class=9 designer=43b:Arm part=9a1 devtype=11 archid=0000 devid=ca1:0:0> [rom_table]
0000959 W Invalid coresight component, cidr=0x0 [rom_table]
0000959 I [5]e0041000: cidr=0, pidr=0, component invalid> [rom_table]
0000964 I CPU core #0: Cortex-M4 r0p1, v7.0-M architecture [cortex_m]
0000964 I Extensions: [DSP, FPU, FPU_V4, MPU] [cortex_m]
0000965 I FPU present: FPv4-SP-D16-M [cortex_m]
0000969 I Setting core #0 (Cortex-M4) default reset sequence to ResetSystem [pack_target]
0000970 I 4 hardware watchpoints [dwt]
0000974 I 6 hardware breakpoints, 4 literal comparators [fpb]
0000995 I 3 up channels and 3 down channels found [rtt_cmd]
0000995 I Reading from up channel 0 ("Terminal") [rtt_cmd]
0000999 I Writing to down channel 0 ("Terminal") [rtt_cmd]
msh >
msh >
msh >hheellpp
RT-Thread shell commands:
ef - easyflash test
reboot - Reboot System
setenv - Set an envrionment variable.
printenv - Print all envrionment variables.
saveenv - Save all envrionment variables to flash.
getvalue - Get an envrionment variable by name.
resetenv - Reset all envrionment variable to default.
fal - FAL (Flash Abstraction Layer) operate.
clear - clear the terminal screen
version - show RT-Thread version information
list_thread - list thread
list_sem - list semaphore in system
list_event - list event in system
list_mutex - list mutex in system
list_mailbox - list mail box in system
list_msgqueue - list message queue in system
list_memheap - list memory heap in system
list_mempool - list memory pool in system
list_timer - list timer in system
list_device - list device in system
exit
其他常用命令
复位
python -m pyocd cmd --command reset --target GD32F427RE
3. rt-thread
3.1 配置内核
下载env工具
注册工具到右键菜单
在工程目录右键打开ConEmu
menuconfig来配置内核
运行scons --target=mdk5来同步工程配置
3.2 pkgs --update不能更新的问题
问题:env下配置rt-thread内核,增加包之后,用pkgs --update下载包没有反应:
xx@DESK D:\stm32
> pkgs --list
package name : LittlevGL2RTT, ver : v0.0.1
xx@DESK D:\stm32
> pkgs --update
xx@DESK D:\stm32
>
解决办法:
下载python27包,安装
https://www.python.org/ftp/python/2.7.14/python-2.7.14.msi
默认安装在C:\Python27
备份env\tools\Python27,用C:\Python27整个目录替换env\tools\Python27目录,把原目录下的scons相关的问题都拷贝过来
把备份的env\tools\Python27\Lib\site-packages\scons目录拷贝到相同的位置
此时运行env可能会报错,提示import requests失败。
执行:
pip install requests
再运行env就可以了
> pkgs --update
[Use Gitee server]
Start to download package : LittlevGL2RTT-v0.0.1.zip
Downloded 1401 KB
Start to unpack. Please wait...
==============================> LITTLEVGL2RTT v0.0.1 is downloaded successfully.
Operation completed successfully.