pyOCD 是一个开源 Python 软件包,用于多种受支持的硬件调试器(DAP-Link、J-Link、ST-Link,CMSIS-DAP v1(HID)、CMSIS-DAP v2(WinUSB)、SEGGER J-Link、ST-LINK v2和ST-LINK v3)下编程和调试Arm Cortex-M微控制器。它是完全跨平台的,并支持Linux,macOS和Windows。它内置支持多达70种流行的MCU。通过使用CMSIS-Pack,几乎支持市场上的所有Cortex-M设备。pyOCD还可以作为GDB Service配合GDB调试芯片。
开发环境:
python 3.6.1,pyOCD 0.34.1,探针是DAP-LINK(CMSIS-DAP v2),MCU:stm32f103rc,64位的Win10操作系统。
项目主页: https://github.com/pyocd/pyOCD
pyOCD 允许用户通过命令来:烧录、擦除、单步、停止、设置断点、全速运行、芯片上锁、读写外设寄存器、读写存储空间等操作。
安装pyocd cmd中输入pip install –U pyocd
安装 libusb库 cmd中输入 pip install –U libusb
退出 python cmd中输入 exit()
Cmd中输入pyocd 出现图16提示说明pyocd安装成功。
https://pyocd.io/docs/configuring_logging.html
https://pypi.org/project/pyocd/0.8.1a1/#files
https://www.python.org/
一、pyOCD的安装
pyOCD是一个python库,可用通过pip install pyocd就会自动安装pyOCD以及其依赖库。其中依赖库有:cmsis-pack-manger、intelhex、pyusb、pywinusb、pyelftools、pyyaml、six、pylink-square、prettytable、colorame和interbaltree。一般pip会去外网下库,速度很慢。我们可用借助国内的pip镜像网站下载。比如
pip install -i https://pypi.tuna.tsinghua.edu.cn/simpl pyocd
安装完毕之后,可用查看pyOCD版本:
C:\Users\lbaihao>pyocd --version
0.34.1
如果你是第一次接触pyOCD并且什么都不知道,那么查看帮助文档是个好建议。可用输入如下命令查看帮助信息:
C:\Users\lbaihao>pyocd --help
usage: pyocd [-h] [-V] [--help-options] ...
PyOCD debug tools for Arm Cortex devices
optional arguments:
-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.
通过subcommand知道,pyOCD的子命令有cmd、erase、flash、reset、gdb、json、list和pack。下面对这些命令作简要的介绍。
输入帮助命令可以查看到如下信息:
C:\Users\lbaihao>pyocd --help
usage: pyocd [-h] [-V] [--help-options] ...
PyOCD debug tools for Arm Cortex devices
optional arguments:
-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.
C:\Users\lbaihao>pyocd erase --help
usage: pyocd erase [-h] [-v] [-q] [-L LOGGERS=LEVEL]
[--color [{always,auto,never}]] [-j PATH] [--config PATH]
[--no-config] [--script PATH] [-O OPTION=VALUE]
[-da DAPARG [DAPARG ...]] [--pack PATH] [-u UNIQUE_ID]
[-t TARGET] [-f FREQUENCY] [-W] [-M MODE] [-c] [-s]
[--mass]
[ [ ...]]
positional arguments:
List of sector addresses or ranges to erase.
optional arguments:
-h, --help show this help message and exit
logging:
-v, --verbose Increase logging level. Can be specified multiple
times.
-q, --quiet Decrease logging level. Can be specified multiple
times.
-L LOGGERS=LEVEL, --log-level LOGGERS=LEVEL
Set log level of loggers whose name matches any of the
comma-separated list of glob-style patterns. Log level
must be one of (critical, error, warning, info,
debug). Can be specified multiple times. Example:
-L*.trace,pyocd.core.*=debug
--color [{always,auto,never}]
Control color logging. Default is auto.
configuration:
-j PATH, --project PATH, --dir PATH
Set the project directory. Defaults to the directory
where pyocd was run.
--config PATH Specify YAML configuration file. Defaults to
pyocd.yaml or pyocd.yml in the project directory.
--no-config Do not use a configuration file.
--script PATH Use the specified user script. Defaults to
pyocd_user.py in the project directory.
-O OPTION=VALUE Set named option.
-da DAPARG [DAPARG ...], --daparg DAPARG [DAPARG ...]
(Deprecated) Send setting to DAPAccess layer.
--pack PATH Path to the .pack file for a CMSIS Device Family Pack.
connection:
-u UNIQUE_ID, --uid UNIQUE_ID, --probe UNIQUE_ID
Select the debug probe by its full or partial unique
ID. Optionally prefixed with ':' where
is the name of a probe plugin.
-t TARGET, --target TARGET
Set the target type. See available target types with
'pyocd list --targets'.
-f FREQUENCY, --frequency FREQUENCY
SWD/JTAG clock frequency in Hz. Accepts a float or int
with optional case-insensitive K/M suffix and optional
Hz. Examples: "1000", "2.5khz", "10m".
-W, --no-wait Do not wait for a probe to be connected if none are
available.
-M MODE, --connect MODE
Select connect mode from one of (halt, pre-reset,
under-reset, attach).
erase options:
-c, --chip Perform a chip erase.
-s, --sector Erase the sectors listed as positional arguments. This
is the default.
--mass Perform a mass erase. On some devices this is
different than a chip erase.
If no position arguments are listed, then no action will be taken unless the
--chip or --mass-erase options are provided. Otherwise, the positional
arguments should be the addresses of flash sectors or address ranges. The end
address of a range is exclusive, meaning that it will not be erased. Thus, you
should specify the address of the sector after the last one to be erased. If a
'+' is used instead of '-' in a range, this indicates that the second value is
a length rather than end address. Examples: 0x1000 (erase single sector
starting at 0x1000) 0x800-0x2000 (erase sectors starting at 0x800 up to but
not including 0x2000) 0+8192 (erase 8 kB starting at address 0)
这个帮助信息里面有几个信息:usage、optional argument、configuration、connection、erase options。usage是基本使用方法介绍,里面详细标注pyocd erase可以带哪些类型的参数;optional argument是一些帮助信息等;configuration是一些配置信息,比如工程路径、软件包路径等信息;connection是一些连接的信息,比如探针ID、板对象、目标对象、SWD/JTAG通信频率和连接模式等信息;erase options是擦除相关的参数,里面有chip(擦除全部存储空间)、sector(擦除程序使用的空间)、mass。最简单的命令要包含connection和erase option,也就是操作对象的操作方法。下面列举一些擦除的操作:
虽然可以用-c代替--chip,-t代替--target,也就是pyocd erase -c -t stm32f103rc。看起来简洁很多,但是这种非常不具备可读性,不建议这样干。下面是操作的执行结果:
四、烧录命令flash
输入帮助命令可以查看到如下信息:
通过flash options知道可以烧录三种格式的文件:hex、bin和elf,其中bin和elf不带地址信息,需要在参数指定烧录起始地址。下面列举一些烧录的操作:
一般烧录前需要擦除要烧录的区域,因为ROM的特性,只能通过擦除才能写1。下面是操作的执行结果:
五、复位命令reset
输入帮助命令可以查看到如下信息:
从reset options知道复位有2种方式:sw(软复位)、hw(硬复位),区别就是hw是通过拉低目标芯片的reset引脚来让芯片产生硬件复位。下面列举一些复位的操作:
下面是操作的执行结果:
六、列表命令list
输入帮助命令可以查看到如下信息:
下面列举一些list的操作:
下面是操作结果
C:\Users\lbaihao>pyocd list --probes
# Probe/Board Unique ID Target
--------------------------------------------------------
0 STM32 STLink 2900240008000054574E514E n/a
每个探针都有唯一的ID,如果有多个探针连接到PC,可以通过ID来确定要操作那个探针连接的开发板。下面是pyOCD内置支持的芯片。
C:\Users\lbaihao>pyocd list --targets
Name Vendor Part Number Families Source
-----------------------------------------------------------------------------------------------------
cc3220sf Texas Instruments CC3220SF builtin
cortex_m Generic CoreSightTarget builtin
cy8c64_sysap Cypress cy8c64_sysap builtin
cy8c64x5_cm0 Cypress cy8c64x5_cm0 builtin
cy8c64x5_cm0_full_flash Cypress cy8c64x5_cm0_full_flash builtin
cy8c64x5_cm4 Cypress cy8c64x5_cm4 builtin
cy8c64x5_cm4_full_flash Cypress cy8c64x5_cm4_full_flash builtin
cy8c64xa_cm0 Cypress cy8c64xA_cm0 builtin
cy8c64xa_cm0_full_flash Cypress cy8c64xA_cm0_full_flash builtin
cy8c64xa_cm4 Cypress cy8c64xA_cm4 builtin
cy8c64xa_cm4_full_flash Cypress cy8c64xA_cm4_full_flash builtin
cy8c64xx_cm0 Cypress cy8c64xx_cm0 builtin
cy8c64xx_cm0_full_flash Cypress cy8c64xx_cm0_full_flash builtin
cy8c64xx_cm0_nosmif Cypress cy8c64xx_cm0_nosmif builtin
cy8c64xx_cm0_s25hx512t Cypress cy8c64xx_cm0_s25hx512t builtin
cy8c64xx_cm4 Cypress cy8c64xx_cm4 builtin
cy8c64xx_cm4_full_flash Cypress cy8c64xx_cm4_full_flash builtin
cy8c64xx_cm4_nosmif Cypress cy8c64xx_cm4_nosmif builtin
cy8c64xx_cm4_s25hx512t Cypress cy8c64xx_cm4_s25hx512t builtin
cy8c6xx5 Cypress CY8C6xx5 builtin
cy8c6xx7 Cypress CY8C6xx7 builtin
cy8c6xx7_nosmif Cypress CY8C6xx7_nosmif builtin
cy8c6xx7_s25fs512s Cypress CY8C6xx7_S25FS512S builtin
cy8c6xxa Cypress CY8C6xxA builtin
hc32f003 HDSC HC32F003 builtin
hc32f005 HDSC HC32F005 builtin
hc32f030 HDSC HC32F030 builtin
hc32f072 HDSC HC32F072 builtin
hc32f120x6 HDSC HC32F120x6TA builtin
hc32f120x8 HDSC HC32F120x8TA builtin
hc32f160xa HDSC HC32F160xA builtin
hc32f160xc HDSC HC32F160xC builtin
hc32f190 HDSC HC32F190 builtin
hc32f196 HDSC HC32F196 builtin
hc32f451xc HDSC HC32F451xC builtin
hc32f451xe HDSC HC32F451xE builtin
hc32f452xc HDSC HC32F452xC builtin
hc32f452xe HDSC HC32F452xE builtin
hc32f460xc HDSC HC32F460xC builtin
hc32f460xe HDSC HC32F460xE builtin
hc32f4a0xg HDSC HC32F4A0xG builtin
hc32f4a0xi HDSC HC32F4A0xI builtin
hc32l072 HDSC HC32L072 builtin
hc32l073 HDSC HC32L073 builtin
hc32l110 HDSC HC32L110 builtin
hc32l130 HDSC HC32L130 builtin
hc32l136 HDSC HC32L136 builtin
hc32l190 HDSC HC32L190 builtin
hc32l196 HDSC HC32L196 builtin
hc32m120 HDSC HC32M120 builtin
hc32m120x6 HDSC HC32M120 builtin
hc32m423xa HDSC HC32M423xA builtin
k20d50m NXP K20D50M builtin
k22f NXP K22F builtin
k22fa12 NXP K22FA12 builtin
k28f15 NXP K28F15 builtin
k32l2b3 NXP K32L2B3 builtin
k32w042s NXP K32W042S builtin
k64f NXP K64F builtin
k66f18 NXP K66F18 builtin
k82f25615 NXP K82F25615 builtin
ke15z7 NXP KE15Z7 builtin
ke17z7 NXP KE17Z7 builtin
ke18f16 NXP KE18F16 builtin
kinetis NXP Kinetis builtin
kl02z NXP KL02Z builtin
kl05z NXP KL05Z builtin
kl25z NXP KL25Z builtin
kl26z NXP KL26Z builtin
kl27z4 NXP KL27Z4 builtin
kl28z NXP KL28x builtin
kl43z4 NXP KL43Z4 builtin
kl46z NXP KL46Z builtin
kl82z7 NXP KL82Z7 builtin
kv10z7 NXP KV10Z7 builtin
kv11z7 NXP KV11Z7 builtin
kw01z4 NXP KW01Z4 builtin
kw24d5 NXP KW24D5 builtin
kw36z4 NXP KW36Z4 builtin
kw40z4 NXP KW40Z4 builtin
kw41z4 NXP KW41Z4 builtin
lpc11u24 NXP LPC11U24 builtin
lpc11xx_32 NXP LPC11XX_32 builtin
lpc1768 NXP LPC1768 builtin
lpc4088 NXP LPC4088 builtin
lpc4088dm NXP LPC4088dm builtin
lpc4088qsb NXP LPC4088qsb builtin
lpc4330 NXP LPC4330 builtin
lpc54114 NXP LPC54114 builtin
lpc54608 NXP LPC54608 builtin
lpc5526 NXP LPC5526 builtin
lpc55s28 NXP LPC55S28 builtin
lpc55s36 NXP LPC55S36 builtin
lpc55s69 NXP LPC55S69 builtin
lpc800 NXP LPC800 builtin
lpc824 NXP LPC824 builtin
lpc845 NXP LPC845 builtin
m2354kjfae Nuvoton M2354KJFAE builtin
m252kg6ae Nuvoton M252KG6AE builtin
m263kiaae Nuvoton M263KIAAE builtin
m467hjhae Nuvoton M467HJHAE builtin
m487jidae Nuvoton M487JIDAE builtin
max32600 Maxim MAX32600 builtin
max32620 Maxim MAX32620 builtin
max32625 Maxim MAX32625 builtin
max32630 Maxim MAX32630 builtin
max32660 Maxim MAX32660 builtin
max32670 Maxim MAX32670 builtin
mimxrt1010 NXP MIMXRT1011xxxxx builtin
mimxrt1015 NXP MIMXRT1015xxxxx builtin
mimxrt1020 NXP MIMXRT1021xxxxx builtin
mimxrt1024 NXP MIMXRT1024xxxxx builtin
mimxrt1050 NXP MIMXRT1052xxxxB_hyperflash builtin
mimxrt1050_hyperflash NXP MIMXRT1052xxxxB_hyperflash builtin
mimxrt1050_quadspi NXP MIMXRT1052xxxxB_quadspi builtin
mimxrt1060 NXP MIMXRT1062xxxxA builtin
mimxrt1064 NXP MIMXRT1064xxxxA builtin
mimxrt1170_cm4 NXP MIMXRT1176xxxxx_CM4 builtin
mimxrt1170_cm7 NXP MIMXRT1176xxxxx_CM7 builtin
mps3_an522 Arm AN522 builtin
mps3_an540 Arm AN540 builtin
musca_a1 Arm MuscaA1 builtin
musca_b1 Arm MuscaB1 builtin
musca_s1 Arm MuscaS1 builtin
ncs36510 ONSemiconductor NCS36510 builtin
nrf51 Nordic Semiconductor NRF51 builtin
nrf51822 Nordic Semiconductor NRF51 builtin
nrf52 Nordic Semiconductor NRF52832 builtin
nrf52832 Nordic Semiconductor NRF52832 builtin
nrf52833 Nordic Semiconductor NRF52833 builtin
nrf52840 Nordic Semiconductor NRF52840 builtin
rp2040 Raspberry Pi RP2040Core0 builtin
rp2040_core0 Raspberry Pi RP2040Core0 builtin
rp2040_core1 Raspberry Pi RP2040Core1 builtin
rtl8195am Realtek Semiconductor RTL8195AM builtin
s5js100 Samsung S5JS100 builtin
stm32f051 STMicroelectronics STM32F051 builtin
stm32f103rc STMicroelectronics STM32F103RC builtin
stm32f412xe STMicroelectronics STM32F412xE builtin
stm32f412xg STMicroelectronics STM32F412xG builtin
stm32f429xg STMicroelectronics STM32F429xG builtin
stm32f429xi STMicroelectronics STM32F429xI builtin
stm32f439xg STMicroelectronics STM32F439xG builtin
stm32f439xi STMicroelectronics STM32F439xI builtin
stm32f767zi STMicroelectronics STM32F767xx builtin
stm32l031x6 STMicroelectronics STM32L031x6 builtin
stm32l432kc STMicroelectronics STM32L432xC builtin
stm32l475xc STMicroelectronics STM32L475xC builtin
stm32l475xe STMicroelectronics STM32L475xE builtin
stm32l475xg STMicroelectronics STM32L475xG builtin
w7500 WIZnet W7500 builtin
json命令和list命令的功能差不多,只不过json命令的数据输出格式是按照标准的json格式。下面列举一些json的操作:
下面是一些执行结果:
C:\Users\lbaihao>pyocd json --probes
{
"pyocd_version": "0.34.1",
"version": {
"major": 1,
"minor": 1
},
"status": 0,
"boards": [
{
"unique_id": "2900240008000054574E514E",
"info": "STMicroelectronics STM32 STLink",
"board_vendor": null,
"board_name": "Generic",
"target": "cortex_m",
"vendor_name": "STMicroelectronics",
"product_name": "STM32 STLink"
}
]
}
八、软件包命令pack
输入帮助命令可以查看到如下信息:
这个是软件包的管理操作命令,可以显示软件包的信息也可以下载软件包。但是实际上,下载软件包的时候总是出现服务器没有响应的情况,估计是服务器不在国内。所以这个功能还是比较鸡肋的,幸好pyOCD内置支持不少芯片。
如果芯片刚好不是内置支持的芯片,那就需要自己找对于的软件包。比如芯片是gd32f103c8,可以自己去找GigaDevice.GD32F10x_DFP.2.0.2.pack,然后使用的时候带上--pack参数指定pack包的路径即可。比如下面操作:
九、cmd命令
输入帮助命令可以查看到如下信息:
C:\Users\lbaihao> pyocd cmd --help
从commander options,可以知道--halt这个命令被标记为弃用,也就是不建议使用。--command命令里面有其他操作,可以输入帮助信息查看:
这里面的指令就比较多了,都是一些芯片调试相关的。下面列举一些操作:
这些指令主要是电脑端GDB和pyOCD的GDB Service交互用的。当然自己也可以试着调试,一般调试都要先让芯片halt(暂停),然后才能插入断点、单步、读写寄存器、读写内存等操作。下面是一些简单的调试过程:
先把程序load到芯片,然后让芯片halt下来,再单步、打断点、查看芯片状态、查看寄存器、查看断点、移除断点、全速运行、查看状态。一般用keil或者IAR,只需要点击几个图案就能完成芯片调试的操作,只能说明keil太方便了。这里是纯粹用命令行控制,虽然难度增加了不少,但是对调试器的原理和功能会多点认识,这就是进步吧。后面再配合GDB,就能用GDB的方式调试代码了。
开源pyOCD的使用介绍 - Cortex M3 M0 M4开发
二、Pyocd CMSIS-DAP调试仿真MCU-pudn.com
pyocd_loveshipting的博客-CSDN博客_pyocd和openocd
pip install six --upgrade --ignore-installed six