本帖最后由 沒有你 于 2020-8-23 22:39 編輯
pyOCD是一個開源Python軟件包,用于使用多種受支持的USB調試探針類型來編程和調試Arm Cortex-M微控制器。它是完全跨平臺的,并支持Linux,macOS和Windows。它內置支持多達70種流行的MCU。另外,通過使用CMSIS-Pack,幾乎支持市場上的所有Cortex-M設備。pyOCD還可以作為GDB Service配合GDB調試芯片,支持多種探針,比如:CMSIS-DAP v1(HID)、CMSIS-DAP v2(WinUSB)、SEGGER J-Link、ST-LINK v2和ST-LINK v3。 pyOCD其實和openOCD功能類似,也是支持跨平臺的。pyOCD允許用戶通過命令來:燒錄、擦除、單步、停止、設置斷點、全速運行、芯片上鎖、讀寫外設寄存器、讀寫存儲空間等操作。不過網上對pyOCD的教程介紹比較少,不像openOCD那么多,增加了學習的難度。我索性花了半天的時間研究了一下,現在把我所知道的總結一下,希望對后來學習pyOCD的人有所幫助。如果有轉載的需要,請務必標注出處。學習的同時也要時時刻刻分享,分享的同時,自己也會有所提升。
我的操作系統是64位的Win10,python的版本為3.8.3,pyOCD的版本為0.27.2,探針是DAP-LINK(CMSIS-DAP v2),單片機是stm32f103rc。下面的所有操作都是基于這個環境完成。
一、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\86188\Desktop> pyocd --version
- 0.27.2
復制代碼 二、查看pyOCD的幫助信息
如果你是第一次接觸pyOCD并且什么都不知道,那么查看幫助文檔是個好建議。可用輸入如下命令查看幫助信息:
- C:\Users\86188\Desktop> 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 user options.
- subcommands:
- commander Interactive command console.
- cmd Alias for 'commander'.
- erase Erase entire device flash or specified sectors.
- flash Program an image to device flash.
- reset Reset a device.
- gdbserver Run the gdb remote server(s).
- gdb Alias for 'gdbserver'.
- json Output information as JSON.
- list List information about probes, targets, or boards.
- pack Manage CMSIS-Packs for target support.
復制代碼 通過subcommand知道,pyOCD的子命令有cmd、erase、flash、reset、gdb、json、list和pack。下面對這些命令作簡要的介紹。
三、擦除命令erase
輸入幫助命令可以查看到如下信息:
- C:\Users\86188\Desktop> pyocd erase --help
- usage: pyocd erase [-h] [-v] [-q] [-j PATH] [--config PATH] [--no-config] [--script PATH] [-O OPTION=VALUE]
- [-da DAPARG [DAPARG ...]] [--pack PATH] [-u UNIQUE_ID] [-b BOARD] [-t TARGET] [-f FREQUENCY] [-W]
- [-M MODE] [-c] [-s] [--mass]
- [<sector-address> [<sector-address> ...]]
- optional arguments:
- -h, --help show this help message and exit
- -v, --verbose More logging. Can be specified multiple times.
- -q, --quiet Less logging. Can be specified multiple times.
- configuration:
- -j PATH, --dir PATH Set the project directory. Defaults to the directory where pyocd was run.
- --config PATH Specify YAML configuration file. Default is pyocd.yaml or pyocd.yml.
- --no-config Do not use a configuration file.
- --script PATH Use the specified user script. Defaults to pyocd_user.py.
- -O OPTION=VALUE Set named option.
- -da DAPARG [DAPARG ...], --daparg DAPARG [DAPARG ...]
- Send setting to DAPAccess layer.
- --pack PATH Path to a CMSIS Device Family Pack.
- connection:
- -u UNIQUE_ID, --uid UNIQUE_ID, --probe UNIQUE_ID
- Choose a probe by its unique ID or a substring thereof.
- -b BOARD, --board BOARD
- Set the board type (not yet implemented).
- -t TARGET, --target TARGET
- Set the target type.
- -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.
- --mass Perform a mass erase. On some devices this is different than a chip erase.
- <sector-address> List of sector addresses or ranges to 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,也就是操作對象的操作方法。下面列舉一些擦除的操作:
- 擦除全部存儲空間
- pyocd erase --chip --target stm32f103rc
- 擦除程序使用的空間
- pyocd erase --sector --target stm32f103rc
- mass擦除
- pyocd erase --mass --target stm32f103rc
復制代碼 雖然可以用-c代替--chip,-t代替--target,也就是pyocd erase -c -t stm32f103rc。看起來簡潔很多,但是這種非常不具備可讀性,不建議這樣干。下面是操作的執行結果:
- C:\Users\86188\Desktop\test> pyocd erase --chip --target stm32f103rc
- 0001520:INFO:eraser:Erasing chip...
- 0001612:INFO:eraser:Done
復制代碼 四、燒錄命令flash
輸入幫助命令可以查看到如下信息:
- C:\Users\86188\Desktop\test> pyocd flash --help
- usage: pyocd flash [-h] [-v] [-q] [-j PATH] [--config PATH] [--no-config] [--script PATH] [-O OPTION=VALUE]
- [-da DAPARG [DAPARG ...]] [--pack PATH] [-u UNIQUE_ID] [-b BOARD] [-t TARGET] [-f FREQUENCY] [-W]
- [-M MODE] [-e {auto,chip,sector}] [-a ADDR] [--trust-crc] [--format {bin,hex,elf}] [--skip BYTES]
- PATH
- optional arguments:
- -h, --help show this help message and exit
- -v, --verbose More logging. Can be specified multiple times.
- -q, --quiet Less logging. Can be specified multiple times.
- configuration:
- -j PATH, --dir PATH Set the project directory. Defaults to the directory where pyocd was run.
- --config PATH Specify YAML configuration file. Default is pyocd.yaml or pyocd.yml.
- --no-config Do not use a configuration file.
- --script PATH Use the specified user script. Defaults to pyocd_user.py.
- -O OPTION=VALUE Set named option.
- -da DAPARG [DAPARG ...], --daparg DAPARG [DAPARG ...]
- Send setting to DAPAccess layer.
- --pack PATH Path to a CMSIS Device Family Pack.
- connection:
- -u UNIQUE_ID, --uid UNIQUE_ID, --probe UNIQUE_ID
- Choose a probe by its unique ID or a substring thereof.
- -b BOARD, --board BOARD
- Set the board type (not yet implemented).
- -t TARGET, --target TARGET
- Set the target type.
- -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).
- flash options:
- -e {auto,chip,sector}, --erase {auto,chip,sector}
- Choose flash erase method. Default is sector.
- -a ADDR, --base-address ADDR
- Base address used for the address where to flash a binary. Defaults to start of flash.
- --trust-crc Use only the CRC of each page to determine if it already has the same data.
- --format {bin,hex,elf}
- File format. Default is to use the file's extension.
- --skip BYTES Skip programming the first N bytes. This can only be used with binary files.
- PATH File to program into flash.
復制代碼 通過flash options知道可以燒錄三種格式的文件:hex、bin和elf,其中bin和elf不帶地址信息,需要在參數指定燒錄起始地址。下面列舉一些燒錄的操作:
- 不擦除芯片,單純燒錄hex
- pyocd flash --target stm32f103rc template.hex
- 先擦除,再燒錄hex
- pyocd flash --erase chip --target stm32f103rc template.hex
- 先擦除,再燒錄bin,指定起始地址為0x8000000
- pyocd flash -erase chip --target stm32f103rc --base-address 0x8000000 template.bin
復制代碼 一般燒錄前需要擦除要燒錄的區域,因為ROM的特性,只能通過擦除才能寫1。下面是操作的執行結果:
- C:\Users\86188\Desktop\test> pyocd flash --erase chip --target stm32f103rc template.hex
- [====================] 100%
- 0002818:INFO:loader:Erased chip, programmed 10240 bytes (5 pages), skipped 0 bytes (0 pages) at 13.31 kB/s
復制代碼 五、復位命令reset
輸入幫助命令可以查看到如下信息:
- C:\Users\86188\Desktop\test> pyocd reset --help
- usage: pyocd reset [-h] [-v] [-q] [-j PATH] [--config PATH] [--no-config] [--script PATH] [-O OPTION=VALUE]
- [-da DAPARG [DAPARG ...]] [--pack PATH] [-u UNIQUE_ID] [-b BOARD] [-t TARGET] [-f FREQUENCY] [-W]
- [-M MODE] [-m METHOD]
- optional arguments:
- -h, --help show this help message and exit
- -v, --verbose More logging. Can be specified multiple times.
- -q, --quiet Less logging. Can be specified multiple times.
- configuration:
- -j PATH, --dir PATH Set the project directory. Defaults to the directory where pyocd was run.
- --config PATH Specify YAML configuration file. Default is pyocd.yaml or pyocd.yml.
- --no-config Do not use a configuration file.
- --script PATH Use the specified user script. Defaults to pyocd_user.py.
- -O OPTION=VALUE Set named option.
- -da DAPARG [DAPARG ...], --daparg DAPARG [DAPARG ...]
- Send setting to DAPAccess layer.
- --pack PATH Path to a CMSIS Device Family Pack.
- connection:
- -u UNIQUE_ID, --uid UNIQUE_ID, --probe UNIQUE_ID
- Choose a probe by its unique ID or a substring thereof.
- -b BOARD, --board BOARD
- Set the board type (not yet implemented).
- -t TARGET, --target TARGET
- Set the target type.
- -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).
- reset options:
- -m METHOD, --method METHOD
- Reset method to use ('hw', 'sw', and others). Default is 'hw'.
復制代碼 從reset options知道復位有2種方式:sw(軟復位)、hw(硬復位),區別就是hw是通過拉低目標芯片的reset引腳來讓芯片產生硬件復位。下面列舉一些復位的操作:
- 軟復位
- pyocd reset --method sw --target stm32f103rc
- 硬復位
- pyocd reset -method hw --target stm32f103rc
復制代碼 下面是操作的執行結果:
- C:\Users\86188\Desktop\test> pyocd reset --method sw --target stm32f103rc
復制代碼 六、列表命令list
輸入幫助命令可以查看到如下信息:
- C:\Users\86188\Desktop\test> pyocd list --help
- usage: pyocd list [-h] [-v] [-q] [-j PATH] [--config PATH] [--no-config] [--script PATH] [-O OPTION=VALUE]
- [-da DAPARG [DAPARG ...]] [--pack PATH] [-p] [-t] [-b] [-n NAME] [-r VENDOR] [-s {builtin,pack}]
- [-H]
- optional arguments:
- -h, --help show this help message and exit
- -v, --verbose More logging. Can be specified multiple times.
- -q, --quiet Less logging. Can be specified multiple times.
- configuration:
- -j PATH, --dir PATH Set the project directory. Defaults to the directory where pyocd was run.
- --config PATH Specify YAML configuration file. Default is pyocd.yaml or pyocd.yml.
- --no-config Do not use a configuration file.
- --script PATH Use the specified user script. Defaults to pyocd_user.py.
- -O OPTION=VALUE Set named option.
- -da DAPARG [DAPARG ...], --daparg DAPARG [DAPARG ...]
- Send setting to DAPAccess layer.
- --pack PATH Path to a CMSIS Device Family Pack.
- list output:
- -p, --probes List available probes.
- -t, --targets List all known targets.
- -b, --boards List all known boards.
- list options:
- -n NAME, --name NAME Restrict listing to items matching the given name. Applies to targets and boards.
- -r VENDOR, --vendor VENDOR
- Restrict listing to items whose vendor matches the given name. Applies to targets.
- -s {builtin,pack}, --source {builtin,pack}
- Restrict listing to targets from the specified source. Applies to targets.
- -H, --no-header Don't print a table header.
復制代碼 下面列舉一些list的操作:
- 列出可用的探針
- pyocd list --probes
- 列出所有支持的目標板
- pyocd list --targets
- 列出所有支持的開發板
- pyocd list --boards
復制代碼 下面是操作結果
- C:\Users\86188\Desktop\test> pyocd list --probes
- # Probe Unique ID
- --------------------------------------------------------------------------------------
- 0 NUCLEO-F103RB [stm32f103rb] 07000001066bff303631544157112037a5a5a5a597969908
復制代碼 每個探針都有唯一的ID,如果有多個探針連接到PC,可以通過ID來確定要操作那個探針連接的開發板。下面是pyOCD內置支持的芯片。
七、json命令
輸入幫助命令可以查看到如下信息:
- C:\Users\86188\Desktop\test> pyocd json --help
- usage: pyocd json [-h] [-j PATH] [--config PATH] [--no-config] [--script PATH] [-O OPTION=VALUE]
- [-da DAPARG [DAPARG ...]] [--pack PATH] [-p] [-t] [-b] [-f]
- optional arguments:
- -h, --help show this help message and exit
- configuration:
- -j PATH, --dir PATH Set the project directory. Defaults to the directory where pyocd was run.
- --config PATH Specify YAML configuration file. Default is pyocd.yaml or pyocd.yml.
- --no-config Do not use a configuration file.
- --script PATH Use the specified user script. Defaults to pyocd_user.py.
- -O OPTION=VALUE Set named option.
- -da DAPARG [DAPARG ...], --daparg DAPARG [DAPARG ...]
- Send setting to DAPAccess layer.
- --pack PATH Path to a CMSIS Device Family Pack.
- json output:
- -p, --probes List available probes.
- -t, --targets List all known targets.
- -b, --boards List all known boards.
- -f, --features List available features and options.
復制代碼 json命令和list命令的功能差不多,只不過json命令的數據輸出格式是按照標準的json格式。下面列舉一些json的操作:
- 以json格式列出可用的探針
- pyocd json --probes
- 以json格式列出所有支持的目標板
- pyocd json --targets
- 以json格式列出所有支持的開發板
- pyocd json --boards
- 以json格式列出所有支持的開發板
- pyocd json --features
復制代碼 下面是一些執行結果:
- C:\Users\86188\Desktop\test> pyocd json --targets
- {
- "pyocd_version": "0.27.2",
- "version": {
- "major": 1,
- "minor": 2
- },
- "status": 0,
- "targets": [
- {
- "name": "mps3_an522",
- "vendor": "Arm",
- "part_families": [],
- "part_number": "AN522",
- "source": "builtin"
- },
- {
- "name": "mps3_an540",
- "vendor": "Arm",
- "part_families": [],
- "part_number": "AN540",
- "source": "builtin"
- },
- {
- "name": "cortex_m",
- "vendor": "Generic",
- "part_families": [],
- "part_number": "CoreSightTarget",
- "source": "builtin"
- },
復制代碼 八、軟件包命令pack
輸入幫助命令可以查看到如下信息:
- C:\Users\86188\Desktop\test> pyocd pack --help
- usage: pyocd pack [-h] [-v] [-q] [-c] [-u] [-s] [-f GLOB] [-i GLOB] [-n] [-H]
- optional arguments:
- -h, --help show this help message and exit
- -v, --verbose More logging. Can be specified multiple times.
- -q, --quiet Less logging. Can be specified multiple times.
- pack operations:
- -c, --clean Erase all stored pack information.
- -u, --update Update the pack index.
- -s, --show Show the list of installed packs.
- -f GLOB, --find GLOB Report pack(s) in the index containing matching device part numbers.
- -i GLOB, --install GLOB
- Download and install pack(s) containing matching device part numbers.
- pack options:
- -n, --no-download Just list the pack(s) that would be downloaded, don't actually download anything.
- -H, --no-header Don't print a table header.
復制代碼 這個是軟件包的管理操作命令,可以顯示軟件包的信息也可以下載軟件包。但是實際上,下載軟件包的時候總是出現服務器沒有響應的情況,估計是服務器不在國內。所以這個功能還是比較雞肋的,幸好pyOCD內置支持不少芯片。
如果芯片剛好不是內置支持的芯片,那就需要自己找對于的軟件包。比如芯片是gd32f103c8,可以自己去找GigaDevice.GD32F10x_DFP.2.0.2.pack,然后使用的時候帶上--pack參數指定pack包的路徑即可。比如下面操作:
- pyocd flash --erase chip --target gd32f103c8 --pack=./GigaDevice.GD32F10x_DFP.2.0.2.pack template.hex
復制代碼 九、cmd命令
輸入幫助命令可以查看到如下信息:
- <blockquote>C:\Users\86188\Desktop> pyocd cmd --help
復制代碼 從commander options,可以知道--halt這個命令被標記為棄用,也就是不建議使用。--command命令里面有其他操作,可以輸入幫助信息查看:
- C:\Users\86188\Desktop\test> pyocd cmd --command help
- Commands:
- --------
- break ADDR Set a breakpoint address
- cmp, compare ADDR [LEN] FILENAME Compare a memory range against a binary file.
- core [NUM] Select CPU core by number or print selected core
- d, disasm [-c/--center] ADDR [LEN] Disassemble instructions at an address
- erase [ADDR] [COUNT] Erase internal flash sectors (performs mass erase if no arguments given)
- exit, quit Quit pyocd-tool
- fill [SIZE] ADDR LEN PATTERN Fill a range of memory with a pattern
- find ADDR LEN BYTE... Search for a value in memory within the given address range.
- gdbserver ACTION Start or stop the gdbserver.
- c, continue, g, go Resume execution of the target
- h, halt Halt the target
- ?, help [CMD] Show help for commands
- initdp Init DP and power up debug.
- list Show available targets
- load FILENAME [ADDR] Load a binary, hex, or elf file with optional base address
- loadmem ADDR FILENAME Load a binary file to an address in memory (RAM or flash)
- lsbreak List breakpoints
- lswatch List watchpoints
- makeap APSEL Creates a new AP object for the given APSEL.
- r16, read16, rh ADDR [LEN] Read 16-bit halfwords
- r32, read32, rw ADDR [LEN] Read 32-bit words
- r, rb, read, read8 ADDR [LEN] Read 8-bit bytes
- rap, readap [APSEL] ADDR Read AP register
- rdp, readdp ADDR Read DP register
- reg [-f] [REG] Print core or peripheral register(s).
- reinit Reinitialize the target object
- reset [-h/--halt] Reset the target
- rmbreak ADDR Remove a breakpoint
- rmwatch ADDR Remove a watchpoint
- savemem ADDR LEN FILENAME Save a range of memory to a binary file
- set NAME VALUE Set an option value
- show INFO Report info about the target
- st, stat, status Show the target's current state
- s, step Step one instruction
- symbol NAME Show a symbol's value.
- unlock Unlock security on the target
- watch ADDR [r|w|rw] [1|2|4] Set a watchpoint address, and optional access type (default rw) and size (4).
- where [ADDR] Show symbol, file, and line for address.
- wreg [-r] REG VALUE Set the value of a core or peripheral register.
- w16, wh, write16 ADDR DATA... Write 16-bit halfwords to memory (RAM or flash). The address may be unaligned. Flash writes are subject to minimum write size and alignment.
- w32, write32, ww ADDR DATA... Write 32-bit words to memory (RAM or flash). The address may be unaligned. Flash writes are subject to minimum write size and alignment.
- w, wb, write, write8 ADDR DATA... Write 8-bit bytes to memory (RAM or flash). Flash writes are subject to minimum write size and alignment.
- wap, writeap [APSEL] ADDR DATA Write AP register
- wdp, writedp ADDR DATA Write DP register
- All register names are also available as commands that print the register's value.
- Any ADDR or LEN argument will accept a register name.
- Prefix line with $ to execute a Python expression.
- Prefix line with ! to execute a shell command.
- Info:
- ----
- cores Information about CPU cores in the target.
- fault Fault status information.
- graph Print the target object graph.
- hnonsec Display the current HNONSEC value used by the selected MEM-AP.
- hprot Display the current HPROT value used by the selected MEM-AP.
- locked Report whether the target is locked.
- map Target memory map.
- mem-ap Display the currently selected MEM-AP used for memory read/write commands.
- nreset Current nRESET signal state.
- option Show the current value of one or more user options.
- peripherals List of target peripheral instances.
- si, step-into-interrupt Display whether interrupts are enabled when single stepping.
- target General target information.
- uid Target's unique ID
- vc, vector-catch Show current vector catch settings.
- Options:
- -------
- clock Set SWD or JTAG clock frequency in Hertz. A case-insensitive metric scale suffix of either 'k' or 'm' is allowed, as well as a trailing "Hz". There must be no space between the frequency and the suffix. For example, "2.5MHz" sets the clock to 2.5 MHz.
- hnonsec Set the current HNONSEC value used by the selected MEM-AP.
- hprot Set the current HPROT value used by the selected MEM-AP.
- log Set log level to one of debug, info, warning, error, critical
- mem-ap Select the MEM-AP used for memory read/write commands.
- nreset Set nRESET signal state. Accepts a value of 0 or 1.
- option Change the value of one or more user options.
- si, step-into-interrupt Set whether to enable or disable interrupts when single stepping. Set to 1 to enable.
- vc, vector-catch Control enabled vector catch sources.
復制代碼 這里面的指令就比較多了,都是一些芯片調試相關的。下面列舉一些操作:
- 加載hex文件
- pyocd cmd --command load template.hex --target stm32f103rc
- 復位芯片
- pyocd cmd --command reset --target stm32f103rc
- 擦除芯片
- pyocd cmd --command erase --target stm32f103rc
- 芯片暫停(然后就可用操作單步、打斷點等)
- pyocd cmd --connect halt --target stm32f103rc
- 設置斷點
- pyocd cmd --command break 0x0800029e --target stm32f103rc
- 單步運行
- pyocd cmd --command step --target stm32f103rc
- 恢復執行
- pyocd cmd --command go --target stm32f103rc
- 查詢芯片當前狀態
- pyocd cmd --command status --target stm32f103rc
- 讀取4個字節
- pyocd cmd --command read32 0x40010c00 12 --connect attach --target stm32f103rc
- 打印內核或者外設寄存器
- pyocd cmd --command reg --connect attach --target stm32f103rc
復制代碼 這些指令主要是電腦端GDB和pyOCD的GDB Service交互用的。當然自己也可以試著調試,一般調試都要先讓芯片halt(暫停),然后才能插入斷點、單步、讀寫寄存器、讀寫內存等操作。下面是一些簡單的調試過程:
- C:\Users\86188\Desktop\test> pyocd cmd --command load template.hex --target stm32f103rc
- [====================] 100%
- PS C:\Users\86188\Desktop\test> pyocd cmd --connect halt --target stm32f103rc
- Connected to STM32F103RC [Halted]: 07000001066bff303631544157112037a5a5a5a597969908
- pyocd> step
- PC = 0x080015de
- pyocd> step
- PC = 0x080004ce
- pyocd> break 0x080004ce
- Set breakpoint at 0x080004ce
- pyocd> status
- Core 0: Halted
- pyocd> reg
- r0: 0x080004cf r6: 0x00000000 r12: 0x00000001
- r1: 0x40022000 r7: 0x00000000 sp: 0x20001788
- r2: 0x00000001 r8: 0x00000000 lr: 0x080015e1
- r3: 0x7a6a5aa8 r9: 0x20000200 pc: 0x080004ce
- r4: 0x00000000 r10: 0x08002504 xpsr: 0x01000000
- r5: 0x20001124 r11: 0x00000000 primask: 0x00000000
- pyocd> go
- Device is halted; a debug event may have occurred
- pyocd> lsbreak
- 5 hardware breakpoints available
- 0: 0x080004ce
- pyocd> rmbreak 0x080004ce
- Removed breakpoint at 0x080004ce
- pyocd> go
- Successfully resumed device
- pyocd> status
- Core 0: Running
- pyocd>
復制代碼 先把程序load到芯片,然后讓芯片halt下來,再單步、打斷點、查看芯片狀態、查看寄存器、查看斷點、移除斷點、全速運行、查看狀態。一般用keil或者IAR,只需要點擊幾個圖案就能完成芯片調試的操作,只能說明keil太方便了。這里是純粹用命令行控制,雖然難度增加了不少,但是對調試器的原理和功能會多點認識,這就是進步吧。后面再配合GDB,就能用GDB的方式調試代碼了。
本來這篇介紹應該在下午完成,但是出了點意外。我習慣在51平臺網頁在線編輯,一不小心按了ctrl+A+Delete,全部刪了。想要撤銷操作,按了ctrl+Z,居然不支持,更坑的地方是網頁默認30秒保存,當時就剩下幾秒時間,來不及操作,就被覆蓋保存。點擊恢復,出現自動保存的空白頁面。急忙聯系網站負責人,還是沒有找回資料。白白浪費了3個小時。沒辦法晚上再花了3個半小時,重新碼字,總算完成。這個經歷給了一個教訓:一定要在本地保存!
如果感覺這篇介紹對你有幫助,請支持一下。有任何疑問,歡迎留言討論!
|