找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 15567|回復(fù): 6
收起左側(cè)

開源pyOCD的使用介紹

  [復(fù)制鏈接]
ID:351097 發(fā)表于 2020-8-23 22:26 | 顯示全部樓層 |閱讀模式
本帖最后由 沒有你 于 2020-8-23 22:39 編輯

    pyOCD是一個(gè)開源Python軟件包,用于使用多種受支持的USB調(diào)試探針類型來編程和調(diào)試Arm Cortex-M微控制器。它是完全跨平臺的,并支持Linux,macOS和Windows。它內(nèi)置支持多達(dá)70種流行的MCU。另外,通過使用CMSIS-Pack,幾乎支持市場上的所有Cortex-M設(shè)備。pyOCD還可以作為GDB Service配合GDB調(diào)試芯片,支持多種探針,比如:CMSIS-DAP v1(HID)、CMSIS-DAP v2(WinUSB)、SEGGER J-Link、ST-LINK v2和ST-LINK v3。    pyOCD其實(shí)和openOCD功能類似,也是支持跨平臺的。pyOCD允許用戶通過命令來:燒錄、擦除、單步、停止、設(shè)置斷點(diǎn)、全速運(yùn)行、芯片上鎖、讀寫外設(shè)寄存器、讀寫存儲(chǔ)空間等操作。不過網(wǎng)上對pyOCD的教程介紹比較少,不像openOCD那么多,增加了學(xué)習(xí)的難度。我索性花了半天的時(shí)間研究了一下,現(xiàn)在把我所知道的總結(jié)一下,希望對后來學(xué)習(xí)pyOCD的人有所幫助。如果有轉(zhuǎn)載的需要,請務(wù)必標(biāo)注出處。學(xué)習(xí)的同時(shí)也要時(shí)時(shí)刻刻分享,分享的同時(shí),自己也會(huì)有所提升。
    我的操作系統(tǒng)是64位的Win10,python的版本為3.8.3,pyOCD的版本為0.27.2,探針是DAP-LINK(CMSIS-DAP v2),單片機(jī)是stm32f103rc。下面的所有操作都是基于這個(gè)環(huán)境完成。
一、pyOCD的安裝
    pyOCD是一個(gè)python庫,可用通過pip install pyocd就會(huì)自動(dòng)安裝pyOCD以及其依賴庫。其中依賴庫有:cmsis-pack-manger、intelhex、pyusb、pywinusb、pyelftools、pyyaml、six、pylink-square、prettytable、colorame和interbaltree。一般pip會(huì)去外網(wǎng)下庫,速度很慢。我們可用借助國內(nèi)的pip鏡像網(wǎng)站下載。比如
  1. pip install -i https://pypi.tuna.tsinghua.edu.cn/simpl pyocd
復(fù)制代碼
   速度就比較快了。安裝完畢之后,可用查看pyOCD版本:
  1. C:\Users\86188\Desktop> pyocd --version
  2. 0.27.2
復(fù)制代碼
二、查看pyOCD的幫助信息
    如果你是第一次接觸pyOCD并且什么都不知道,那么查看幫助文檔是個(gè)好建議?捎幂斎肴缦旅畈榭磶椭畔ⅲ
  1. C:\Users\86188\Desktop> pyocd --help
  2. usage: pyocd [-h] [-V] [--help-options]  ...

  3. PyOCD debug tools for Arm Cortex devices

  4. optional arguments:
  5.   -h, --help      show this help message and exit
  6.   -V, --version   show program's version number and exit
  7.   --help-options  Display available user options.

  8. subcommands:

  9.     commander     Interactive command console.
  10.     cmd           Alias for 'commander'.
  11.     erase         Erase entire device flash or specified sectors.
  12.     flash         Program an image to device flash.
  13.     reset         Reset a device.
  14.     gdbserver     Run the gdb remote server(s).
  15.     gdb           Alias for 'gdbserver'.
  16.     json          Output information as JSON.
  17.     list          List information about probes, targets, or boards.
  18.     pack          Manage CMSIS-Packs for target support.
復(fù)制代碼
   通過subcommand知道,pyOCD的子命令有cmd、erase、flash、reset、gdb、json、list和pack。下面對這些命令作簡要的介紹。
三、擦除命令erase
    輸入幫助命令可以查看到如下信息:
  1. C:\Users\86188\Desktop> pyocd erase --help
  2. usage: pyocd erase [-h] [-v] [-q] [-j PATH] [--config PATH] [--no-config] [--script PATH] [-O OPTION=VALUE]
  3.                    [-da DAPARG [DAPARG ...]] [--pack PATH] [-u UNIQUE_ID] [-b BOARD] [-t TARGET] [-f FREQUENCY] [-W]
  4.                    [-M MODE] [-c] [-s] [--mass]
  5.                    [<sector-address> [<sector-address> ...]]

  6. optional arguments:
  7.   -h, --help            show this help message and exit
  8.   -v, --verbose         More logging. Can be specified multiple times.
  9.   -q, --quiet           Less logging. Can be specified multiple times.

  10. configuration:
  11.   -j PATH, --dir PATH   Set the project directory. Defaults to the directory where pyocd was run.
  12.   --config PATH   Specify YAML configuration file. Default is pyocd.yaml or pyocd.yml.
  13.   --no-config       Do not use a configuration file.
  14.   --script PATH    Use the specified user script. Defaults to pyocd_user.py.
  15.   -O OPTION=VALUE     Set named option.
  16.   -da DAPARG [DAPARG ...], --daparg DAPARG [DAPARG ...]
  17.                         Send setting to DAPAccess layer.
  18.   --pack PATH     Path to a CMSIS Device Family Pack.

  19. connection:
  20.   -u UNIQUE_ID, --uid UNIQUE_ID, --probe UNIQUE_ID
  21.                         Choose a probe by its unique ID or a substring thereof.
  22.   -b BOARD, --board BOARD
  23.                         Set the board type (not yet implemented).
  24.   -t TARGET, --target TARGET
  25.                         Set the target type.
  26.   -f FREQUENCY, --frequency FREQUENCY
  27.                         SWD/JTAG clock frequency in Hz. Accepts a float or int with optional case-insensitive K/M
  28.                         suffix and optional Hz. Examples: "1000", "2.5khz", "10m".
  29.   -W, --no-wait         Do not wait for a probe to be connected if none are available.
  30.   -M MODE, --connect MODE
  31.                         Select connect mode from one of (halt, pre-reset, under-reset, attach).

  32. erase options:
  33.   -c, --chip            Perform a chip erase.
  34.   -s, --sector          Erase the sectors listed as positional arguments.
  35.   --mass                Perform a mass erase. On some devices this is different than a chip erase.
  36.   <sector-address>      List of sector addresses or ranges to erase.

  37. If no position arguments are listed, then no action will be taken unless the --chip or --mass-erase options are
  38. provided. Otherwise, the positional arguments should be the addresses of flash sectors or address ranges. The end
  39. address of a range is exclusive, meaning that it will not be erased. Thus, you should specify the address of the
  40. sector after the last one to be erased. If a '+' is used instead of '-' in a range, this indicates that the second
  41. value is a length rather than end address. Examples: 0x1000 (erase single sector starting at 0x1000) 0x800-0x2000
  42. (erase sectors starting at 0x800 up to but not including 0x2000) 0+8192 (erase 8 kB starting at address 0)
復(fù)制代碼
   這個(gè)幫助信息里面有幾個(gè)信息:usage、optional argument、configuration、connection、erase options。usage是基本使用方法介紹,里面詳細(xì)標(biāo)注pyocd erase可以帶哪些類型的參數(shù);optional argument是一些幫助信息等;configuration是一些配置信息,比如工程路徑、軟件包路徑等信息;connection是一些連接的信息,比如探針I(yè)D、板對象、目標(biāo)對象、SWD/JTAG通信頻率和連接模式等信息;erase options是擦除相關(guān)的參數(shù),里面有chip(擦除全部存儲(chǔ)空間)、sector(擦除程序使用的空間)、mass。最簡單的命令要包含connection和erase option,也就是操作對象的操作方法。下面列舉一些擦除的操作:
  1. 擦除全部存儲(chǔ)空間
  2.   pyocd erase --chip --target stm32f103rc
  3.   擦除程序使用的空間
  4.   pyocd erase --sector --target stm32f103rc
  5.   mass擦除
  6.   pyocd erase --mass --target stm32f103rc
復(fù)制代碼
  雖然可以用-c代替--chip,-t代替--target,也就是pyocd erase -c -t stm32f103rc?雌饋砗啙嵑芏,但是這種非常不具備可讀性,不建議這樣干。下面是操作的執(zhí)行結(jié)果

  1. C:\Users\86188\Desktop\test> pyocd erase --chip --target stm32f103rc
  2. 0001520:INFO:eraser:Erasing chip...
  3. 0001612:INFO:eraser:Done
復(fù)制代碼
四、燒錄命令flash
   
輸入幫助命令可以查看到如下信息:
  1. C:\Users\86188\Desktop\test> pyocd flash --help
  2. usage: pyocd flash [-h] [-v] [-q] [-j PATH] [--config PATH] [--no-config] [--script PATH] [-O OPTION=VALUE]
  3.                    [-da DAPARG [DAPARG ...]] [--pack PATH] [-u UNIQUE_ID] [-b BOARD] [-t TARGET] [-f FREQUENCY] [-W]
  4.                    [-M MODE] [-e {auto,chip,sector}] [-a ADDR] [--trust-crc] [--format {bin,hex,elf}] [--skip BYTES]
  5.                    PATH

  6. optional arguments:
  7.   -h, --help            show this help message and exit
  8.   -v, --verbose         More logging. Can be specified multiple times.
  9.   -q, --quiet           Less logging. Can be specified multiple times.

  10. configuration:
  11.   -j PATH, --dir PATH   Set the project directory. Defaults to the directory where pyocd was run.
  12.   --config PATH         Specify YAML configuration file. Default is pyocd.yaml or pyocd.yml.
  13.   --no-config           Do not use a configuration file.
  14.   --script PATH         Use the specified user script. Defaults to pyocd_user.py.
  15.   -O OPTION=VALUE       Set named option.
  16.   -da DAPARG [DAPARG ...], --daparg DAPARG [DAPARG ...]
  17.                         Send setting to DAPAccess layer.
  18.   --pack PATH           Path to a CMSIS Device Family Pack.

  19. connection:
  20.   -u UNIQUE_ID, --uid UNIQUE_ID, --probe UNIQUE_ID
  21.                         Choose a probe by its unique ID or a substring thereof.
  22.   -b BOARD, --board BOARD
  23.                         Set the board type (not yet implemented).
  24.   -t TARGET, --target TARGET
  25.                         Set the target type.
  26.   -f FREQUENCY, --frequency FREQUENCY
  27.                         SWD/JTAG clock frequency in Hz. Accepts a float or int with optional case-insensitive K/M
  28.                         suffix and optional Hz. Examples: "1000", "2.5khz", "10m".
  29.   -W, --no-wait         Do not wait for a probe to be connected if none are available.
  30.   -M MODE, --connect MODE
  31.                         Select connect mode from one of (halt, pre-reset, under-reset, attach).

  32. flash options:
  33.   -e {auto,chip,sector}, --erase {auto,chip,sector}
  34.                         Choose flash erase method. Default is sector.
  35.   -a ADDR, --base-address ADDR
  36.                         Base address used for the address where to flash a binary. Defaults to start of flash.
  37.   --trust-crc           Use only the CRC of each page to determine if it already has the same data.
  38.   --format {bin,hex,elf}
  39.                         File format. Default is to use the file's extension.
  40.   --skip BYTES          Skip programming the first N bytes. This can only be used with binary files.
  41.   PATH                  File to program into flash.
復(fù)制代碼
   通過flash options知道可以燒錄三種格式的文件:hex、bin和elf,其中bin和elf不帶地址信息,需要在參數(shù)指定燒錄起始地址。下面列舉一些燒錄的操作:
  1. 不擦除芯片,單純燒錄hex
  2. pyocd flash --target stm32f103rc template.hex
  3. 先擦除,再燒錄hex
  4. pyocd flash --erase chip --target stm32f103rc template.hex
  5. 先擦除,再燒錄bin,指定起始地址為0x8000000
  6. pyocd flash -erase chip --target stm32f103rc --base-address 0x8000000 template.bin
復(fù)制代碼
   一般燒錄前需要擦除要燒錄的區(qū)域,因?yàn)镽OM的特性,只能通過擦除才能寫1。下面是操作的執(zhí)行結(jié)果:
  1. C:\Users\86188\Desktop\test> pyocd flash --erase chip --target stm32f103rc template.hex
  2. [====================] 100%
  3. 0002818:INFO:loader:Erased chip, programmed 10240 bytes (5 pages), skipped 0 bytes (0 pages) at 13.31 kB/s
復(fù)制代碼
五、復(fù)位命令reset
    輸入幫助命令可以查看到如下信息:
  1. C:\Users\86188\Desktop\test> pyocd reset --help
  2. usage: pyocd reset [-h] [-v] [-q] [-j PATH] [--config PATH] [--no-config] [--script PATH] [-O OPTION=VALUE]
  3.                    [-da DAPARG [DAPARG ...]] [--pack PATH] [-u UNIQUE_ID] [-b BOARD] [-t TARGET] [-f FREQUENCY] [-W]
  4.                    [-M MODE] [-m METHOD]

  5. optional arguments:
  6.   -h, --help            show this help message and exit
  7.   -v, --verbose         More logging. Can be specified multiple times.
  8.   -q, --quiet           Less logging. Can be specified multiple times.

  9. configuration:
  10.   -j PATH, --dir PATH   Set the project directory. Defaults to the directory where pyocd was run.
  11.   --config PATH         Specify YAML configuration file. Default is pyocd.yaml or pyocd.yml.
  12.   --no-config           Do not use a configuration file.
  13.   --script PATH         Use the specified user script. Defaults to pyocd_user.py.
  14.   -O OPTION=VALUE       Set named option.
  15.   -da DAPARG [DAPARG ...], --daparg DAPARG [DAPARG ...]
  16.                         Send setting to DAPAccess layer.
  17.   --pack PATH           Path to a CMSIS Device Family Pack.

  18. connection:
  19.   -u UNIQUE_ID, --uid UNIQUE_ID, --probe UNIQUE_ID
  20.                         Choose a probe by its unique ID or a substring thereof.
  21.   -b BOARD, --board BOARD
  22.                         Set the board type (not yet implemented).
  23.   -t TARGET, --target TARGET
  24.                         Set the target type.
  25.   -f FREQUENCY, --frequency FREQUENCY
  26.                         SWD/JTAG clock frequency in Hz. Accepts a float or int with optional case-insensitive K/M
  27.                         suffix and optional Hz. Examples: "1000", "2.5khz", "10m".
  28.   -W, --no-wait         Do not wait for a probe to be connected if none are available.
  29.   -M MODE, --connect MODE
  30.                         Select connect mode from one of (halt, pre-reset, under-reset, attach).

  31. reset options:
  32.   -m METHOD, --method METHOD
  33.                         Reset method to use ('hw', 'sw', and others). Default is 'hw'.
復(fù)制代碼
   從reset options知道復(fù)位有2種方式:sw(軟復(fù)位)、hw(硬復(fù)位),區(qū)別就是hw是通過拉低目標(biāo)芯片的reset引腳來讓芯片產(chǎn)生硬件復(fù)位。下面列舉一些復(fù)位的操作:
  1. 軟復(fù)位
  2. pyocd reset --method sw --target stm32f103rc
  3. 硬復(fù)位
  4. pyocd reset -method hw --target stm32f103rc
復(fù)制代碼
   下面是操作的執(zhí)行結(jié)果:
  1. C:\Users\86188\Desktop\test> pyocd reset --method sw --target stm32f103rc
復(fù)制代碼
六、列表命令list
    輸入幫助命令可以查看到如下信息:
  1. C:\Users\86188\Desktop\test> pyocd list --help
  2. usage: pyocd list [-h] [-v] [-q] [-j PATH] [--config PATH] [--no-config] [--script PATH] [-O OPTION=VALUE]
  3.                   [-da DAPARG [DAPARG ...]] [--pack PATH] [-p] [-t] [-b] [-n NAME] [-r VENDOR] [-s {builtin,pack}]
  4.                   [-H]

  5. optional arguments:
  6.   -h, --help            show this help message and exit
  7.   -v, --verbose         More logging. Can be specified multiple times.
  8.   -q, --quiet           Less logging. Can be specified multiple times.

  9. configuration:
  10.   -j PATH, --dir PATH   Set the project directory. Defaults to the directory where pyocd was run.
  11.   --config PATH         Specify YAML configuration file. Default is pyocd.yaml or pyocd.yml.
  12.   --no-config           Do not use a configuration file.
  13.   --script PATH         Use the specified user script. Defaults to pyocd_user.py.
  14.   -O OPTION=VALUE       Set named option.
  15.   -da DAPARG [DAPARG ...], --daparg DAPARG [DAPARG ...]
  16.                         Send setting to DAPAccess layer.
  17.   --pack PATH           Path to a CMSIS Device Family Pack.

  18. list output:
  19.   -p, --probes          List available probes.
  20.   -t, --targets         List all known targets.
  21.   -b, --boards          List all known boards.

  22. list options:
  23.   -n NAME, --name NAME  Restrict listing to items matching the given name. Applies to targets and boards.
  24.   -r VENDOR, --vendor VENDOR
  25.                         Restrict listing to items whose vendor matches the given name. Applies to targets.
  26.   -s {builtin,pack}, --source {builtin,pack}
  27.                         Restrict listing to targets from the specified source. Applies to targets.
  28.   -H, --no-header       Don't print a table header.
復(fù)制代碼
   下面列舉一些list的操作:
  1. 列出可用的探針
  2. pyocd list --probes
  3. 列出所有支持的目標(biāo)板
  4. pyocd list --targets
  5. 列出所有支持的開發(fā)板
  6. pyocd list --boards
復(fù)制代碼
   下面是操作結(jié)果
  1. C:\Users\86188\Desktop\test> pyocd list --probes
  2.   #   Probe                         Unique ID
  3. --------------------------------------------------------------------------------------
  4.   0   NUCLEO-F103RB [stm32f103rb]   07000001066bff303631544157112037a5a5a5a597969908
復(fù)制代碼
   每個(gè)探針都有唯一的ID,如果有多個(gè)探針連接到PC,可以通過ID來確定要操作那個(gè)探針連接的開發(fā)板。下面是pyOCD內(nèi)置支持的芯片。
  1. C:\Users\86188\Desktop\test> pyocd list --targets
  2.   Name                      Vendor                  Part Number                  Families   Source
  3. -----------------------------------------------------------------------------------------------------
  4.   cc3220sf                  Texas Instruments       CC3220SF                                builtin
  5.   cortex_m                  Generic                 CoreSightTarget                         builtin
  6.   cy8c64_sysap              Cypress                 cy8c64_sysap                            builtin
  7.   cy8c64x5_cm0              Cypress                 cy8c64x5_cm0                            builtin
  8.   cy8c64x5_cm0_full_flash   Cypress                 cy8c64x5_cm0_full_flash                 builtin
  9.   cy8c64x5_cm4              Cypress                 cy8c64x5_cm4                            builtin
  10.   cy8c64x5_cm4_full_flash   Cypress                 cy8c64x5_cm4_full_flash                 builtin
  11.   cy8c64xa_cm0              Cypress                 cy8c64xA_cm0                            builtin
  12.   cy8c64xa_cm0_full_flash   Cypress                 cy8c64xA_cm0_full_flash                 builtin
  13.   cy8c64xa_cm4              Cypress                 cy8c64xA_cm4                            builtin
  14.   cy8c64xa_cm4_full_flash   Cypress                 cy8c64xA_cm4_full_flash                 builtin
  15.   cy8c64xx_cm0              Cypress                 cy8c64xx_cm0                            builtin
  16.   cy8c64xx_cm0_full_flash   Cypress                 cy8c64xx_cm0_full_flash                 builtin
  17.   cy8c64xx_cm0_nosmif       Cypress                 cy8c64xx_cm0_nosmif                     builtin
  18.   cy8c64xx_cm0_s25hx512t    Cypress                 cy8c64xx_cm0_s25hx512t                  builtin
  19.   cy8c64xx_cm4              Cypress                 cy8c64xx_cm4                            builtin
  20.   cy8c64xx_cm4_full_flash   Cypress                 cy8c64xx_cm4_full_flash                 builtin
  21.   cy8c64xx_cm4_nosmif       Cypress                 cy8c64xx_cm4_nosmif                     builtin
  22.   cy8c64xx_cm4_s25hx512t    Cypress                 cy8c64xx_cm4_s25hx512t                  builtin
  23.   cy8c6xx5                  Cypress                 CY8C6xx5                                builtin
  24.   cy8c6xx7                  Cypress                 CY8C6xx7                                builtin
  25.   cy8c6xx7_nosmif           Cypress                 CY8C6xx7_nosmif                         builtin
  26.   cy8c6xx7_s25fs512s        Cypress                 CY8C6xx7_S25FS512S                      builtin
  27.   cy8c6xxa                  Cypress                 CY8C6xxA                                builtin
  28.   hc32f003                  HDSC                    HC32F003                                builtin
  29.   hc32f005                  HDSC                    HC32F005                                builtin
  30.   hc32f030                  HDSC                    HC32F030                                builtin
  31.   hc32f072                  HDSC                    HC32F072                                builtin
  32.   hc32f120x6                HDSC                    HC32F120x6TA                            builtin
  33.   hc32f120x8                HDSC                    HC32F120x8TA                            builtin
  34.   hc32f190                  HDSC                    HC32F190                                builtin
  35.   hc32f196                  HDSC                    HC32F196                                builtin
  36.   hc32f46x                  HDSC                    HC32F46x                                builtin
  37.   hc32l072                  HDSC                    HC32L072                                builtin
  38.   hc32l073                  HDSC                    HC32L073                                builtin
  39.   hc32l110                  HDSC                    HC32L110                                builtin
  40.   hc32l130                  HDSC                    HC32L130                                builtin
  41.   hc32l136                  HDSC                    HC32L136                                builtin
  42.   hc32l190                  HDSC                    HC32L190                                builtin
  43.   hc32l196                  HDSC                    HC32L196                                builtin
  44.   hc32m120                  HDSC                    HC32M120                                builtin
  45.   k20d50m                   NXP                     K20D50M                                 builtin
  46.   k22f                      NXP                     K22F                                    builtin
  47.   k22fa12                   NXP                     K22FA12                                 builtin
  48.   k28f15                    NXP                     K28F15                                  builtin
  49.   k32l2b3                   NXP                     K32L2B3                                 builtin
  50.   k32w042s                  NXP                     K32W042S                                builtin
  51.   k64f                      NXP                     K64F                                    builtin
  52.   k66f18                    NXP                     K66F18                                  builtin
  53.   k82f25615                 NXP                     K82F25615                               builtin
  54.   ke15z7                    NXP                     KE15Z7                                  builtin
  55.   ke18f16                   NXP                     KE18F16                                 builtin
  56.   kinetis                   NXP                     Kinetis                                 builtin
  57.   kl02z                     NXP                     KL02Z                                   builtin
  58.   kl05z                     NXP                     KL05Z                                   builtin
  59.   kl25z                     NXP                     KL25Z                                   builtin
  60.   kl26z                     NXP                     KL26Z                                   builtin
  61.   kl27z4                    NXP                     KL27Z4                                  builtin
  62.   kl28z                     NXP                     KL28x                                   builtin
  63.   kl43z4                    NXP                     KL43Z4                                  builtin
  64.   kl46z                     NXP                     KL46Z                                   builtin
  65.   kl82z7                    NXP                     KL82Z7                                  builtin
  66.   kv10z7                    NXP                     KV10Z7                                  builtin
  67.   kv11z7                    NXP                     KV11Z7                                  builtin
  68.   kw01z4                    NXP                     KW01Z4                                  builtin
  69.   kw24d5                    NXP                     KW24D5                                  builtin
  70.   kw36z4                    NXP                     KW36Z4                                  builtin
  71.   kw40z4                    NXP                     KW40Z4                                  builtin
  72.   kw41z4                    NXP                     KW41Z4                                  builtin
  73.   lpc11u24                  NXP                     LPC11U24                                builtin
  74.   lpc11xx_32                NXP                     LPC11XX_32                              builtin
  75.   lpc1768                   NXP                     LPC1768                                 builtin
  76.   lpc4088                   NXP                     LPC4088                                 builtin
  77.   lpc4088dm                 NXP                     LPC4088dm                               builtin
  78.   lpc4088qsb                NXP                     LPC4088qsb                              builtin
  79.   lpc4330                   NXP                     LPC4330                                 builtin
  80.   lpc54114                  NXP                     LPC54114                                builtin
  81.   lpc54608                  NXP                     LPC54608                                builtin
  82.   lpc55s28                  NXP                     LPC55S28                                builtin
  83.   lpc55s69                  NXP                     LPC55S69                                builtin
  84.   lpc800                    NXP                     LPC800                                  builtin
  85.   lpc824                    NXP                     LPC824                                  builtin
  86.   m252kg6ae                 Nuvoton                 M252KG6AE                               builtin
  87.   m263kiaae                 Nuvoton                 M263KIAAE                               builtin
  88.   max32600                  Maxim                   MAX32600                                builtin
  89.   max32620                  Maxim                   MAX32620                                builtin
  90.   max32625                  Maxim                   MAX32625                                builtin
  91.   max32630                  Maxim                   MAX32630                                builtin
  92.   mimxrt1010                NXP                     MIMXRT1011xxxxx                         builtin
  93.   mimxrt1015                NXP                     MIMXRT1015xxxxx                         builtin
  94.   mimxrt1020                NXP                     MIMXRT1021xxxxx                         builtin
  95.   mimxrt1050                NXP                     MIMXRT1052xxxxB_hyperflash              builtin
  96.   mimxrt1050_hyperflash     NXP                     MIMXRT1052xxxxB_hyperflash              builtin
  97.   mimxrt1050_quadspi        NXP                     MIMXRT1052xxxxB_quadspi                 builtin
  98.   mps3_an522                Arm                     AN522                                   builtin
  99.   mps3_an540                Arm                     AN540                                   builtin
  100.   musca_a1                  Arm                     MuscaA1                                 builtin
  101.   musca_b1                  Arm                     MuscaB1                                 builtin
  102.   ncs36510                  ONSemiconductor         NCS36510                                builtin
  103.   nrf51                     Nordic Semiconductor    NRF51                                   builtin
  104.   nrf52                     Nordic Semiconductor    NRF52832                                builtin
  105.   nrf52840                  Nordic Semiconductor    NRF52840                                builtin
  106.   rtl8195am                 Realtek Semiconductor   RTL8195AM                               builtin
  107.   s5js100                   Samsung                 S5JS100                                 builtin
  108.   stm32f051                 STMicroelectronics      STM32F051                               builtin
  109.   stm32f103rc               STMicroelectronics      STM32F103RC                             builtin
  110.   stm32f412xe               STMicroelectronics      STM32F412xE                             builtin
  111.   stm32f412xg               STMicroelectronics      STM32F412xG                             builtin
  112.   stm32f429xg               STMicroelectronics      STM32F429xG                             builtin
  113.   stm32f429xi               STMicroelectronics      STM32F429xI                             builtin
  114.   stm32f439xg               STMicroelectronics      STM32F439xG                             builtin
  115.   stm32f439xi               STMicroelectronics      STM32F439xI                             builtin
  116.   stm32f767zi               STMicroelectronics      STM32F767xx                             builtin
  117.   stm32l031x6               STMicroelectronics      STM32L031x6                             builtin
  118.   stm32l475xc               STMicroelectronics      STM32L475xC                             builtin
  119.   stm32l475xe               STMicroelectronics      STM32L475xE                             builtin
  120.   stm32l475xg               STMicroelectronics      STM32L475xG                             builtin
  121.   w7500                     WIZnet                  W7500                                   builtin
復(fù)制代碼
七、json命令
    輸入幫助命令可以查看到如下信息:
  1. C:\Users\86188\Desktop\test> pyocd json --help
  2. usage: pyocd json [-h] [-j PATH] [--config PATH] [--no-config] [--script PATH] [-O OPTION=VALUE]
  3.                   [-da DAPARG [DAPARG ...]] [--pack PATH] [-p] [-t] [-b] [-f]

  4. optional arguments:
  5.   -h, --help            show this help message and exit

  6. configuration:
  7.   -j PATH, --dir PATH   Set the project directory. Defaults to the directory where pyocd was run.
  8.   --config PATH         Specify YAML configuration file. Default is pyocd.yaml or pyocd.yml.
  9.   --no-config           Do not use a configuration file.
  10.   --script PATH         Use the specified user script. Defaults to pyocd_user.py.
  11.   -O OPTION=VALUE       Set named option.
  12.   -da DAPARG [DAPARG ...], --daparg DAPARG [DAPARG ...]
  13.                         Send setting to DAPAccess layer.
  14.   --pack PATH           Path to a CMSIS Device Family Pack.

  15. json output:
  16.   -p, --probes          List available probes.
  17.   -t, --targets         List all known targets.
  18.   -b, --boards          List all known boards.
  19.   -f, --features        List available features and options.
復(fù)制代碼
   json命令和list命令的功能差不多,只不過json命令的數(shù)據(jù)輸出格式是按照標(biāo)準(zhǔn)的json格式。下面列舉一些json的操作:
  1. 以json格式列出可用的探針
  2. pyocd json --probes
  3. 以json格式列出所有支持的目標(biāo)板
  4. pyocd json --targets
  5. 以json格式列出所有支持的開發(fā)板
  6. pyocd json --boards
  7. 以json格式列出所有支持的開發(fā)板
  8. pyocd json --features
復(fù)制代碼
   下面是一些執(zhí)行結(jié)果:
  1. C:\Users\86188\Desktop\test> pyocd json --targets
  2. {
  3.     "pyocd_version": "0.27.2",
  4.     "version": {
  5.         "major": 1,
  6.         "minor": 2
  7.     },
  8.     "status": 0,
  9.     "targets": [
  10.         {
  11.             "name": "mps3_an522",
  12.             "vendor": "Arm",
  13.             "part_families": [],
  14.             "part_number": "AN522",
  15.             "source": "builtin"
  16.         },
  17.         {
  18.             "name": "mps3_an540",
  19.             "vendor": "Arm",
  20.             "part_families": [],
  21.             "part_number": "AN540",
  22.             "source": "builtin"
  23.         },
  24.         {
  25.             "name": "cortex_m",
  26.             "vendor": "Generic",
  27.             "part_families": [],
  28.             "part_number": "CoreSightTarget",
  29.             "source": "builtin"
  30.         },
復(fù)制代碼
八、軟件包命令pack
    輸入幫助命令可以查看到如下信息:
  1. C:\Users\86188\Desktop\test> pyocd pack --help
  2. usage: pyocd pack [-h] [-v] [-q] [-c] [-u] [-s] [-f GLOB] [-i GLOB] [-n] [-H]

  3. optional arguments:
  4.   -h, --help            show this help message and exit
  5.   -v, --verbose         More logging. Can be specified multiple times.
  6.   -q, --quiet           Less logging. Can be specified multiple times.

  7. pack operations:
  8.   -c, --clean           Erase all stored pack information.
  9.   -u, --update          Update the pack index.
  10.   -s, --show            Show the list of installed packs.
  11.   -f GLOB, --find GLOB  Report pack(s) in the index containing matching device part numbers.
  12.   -i GLOB, --install GLOB
  13.                         Download and install pack(s) containing matching device part numbers.

  14. pack options:
  15.   -n, --no-download     Just list the pack(s) that would be downloaded, don't actually download anything.
  16.   -H, --no-header       Don't print a table header.
復(fù)制代碼
   這個(gè)是軟件包的管理操作命令,可以顯示軟件包的信息也可以下載軟件包。但是實(shí)際上,下載軟件包的時(shí)候總是出現(xiàn)服務(wù)器沒有響應(yīng)的情況,估計(jì)是服務(wù)器不在國內(nèi)。所以這個(gè)功能還是比較雞肋的,幸好pyOCD內(nèi)置支持不少芯片。
    如果芯片剛好不是內(nèi)置支持的芯片,那就需要自己找對于的軟件包。比如芯片是gd32f103c8,可以自己去找GigaDevice.GD32F10x_DFP.2.0.2.pack,然后使用的時(shí)候帶上--pack參數(shù)指定pack包的路徑即可。比如下面操作:
  1. pyocd flash --erase chip --target gd32f103c8 --pack=./GigaDevice.GD32F10x_DFP.2.0.2.pack template.hex
復(fù)制代碼
九、cmd命令
    輸入幫助命令可以查看到如下信息:
  1. <blockquote>C:\Users\86188\Desktop> pyocd cmd --help
復(fù)制代碼
   從commander options,可以知道--halt這個(gè)命令被標(biāo)記為棄用,也就是不建議使用。--command命令里面有其他操作,可以輸入幫助信息查看:
  1. C:\Users\86188\Desktop\test> pyocd cmd --command help
  2. Commands:
  3. --------
  4. break                     ADDR                 Set a breakpoint address
  5. cmp, compare              ADDR [LEN] FILENAME  Compare a memory range against a binary file.
  6. core                      [NUM]                Select CPU core by number or print selected core
  7. d, disasm                 [-c/--center] ADDR [LEN] Disassemble instructions at an address
  8. erase                     [ADDR] [COUNT]       Erase internal flash sectors (performs mass erase if no arguments given)
  9. exit, quit                                     Quit pyocd-tool
  10. fill                      [SIZE] ADDR LEN PATTERN Fill a range of memory with a pattern
  11. find                      ADDR LEN BYTE...     Search for a value in memory within the given address range.
  12. gdbserver                 ACTION               Start or stop the gdbserver.
  13. c, continue, g, go                             Resume execution of the target
  14. h, halt                                        Halt the target
  15. ?, help                   [CMD]                Show help for commands
  16. initdp                                         Init DP and power up debug.
  17. list                                           Show available targets
  18. load                      FILENAME [ADDR]      Load a binary, hex, or elf file with optional base address
  19. loadmem                   ADDR FILENAME        Load a binary file to an address in memory (RAM or flash)
  20. lsbreak                                        List breakpoints
  21. lswatch                                        List watchpoints
  22. makeap                    APSEL                Creates a new AP object for the given APSEL.
  23. r16, read16, rh           ADDR [LEN]           Read 16-bit halfwords
  24. r32, read32, rw           ADDR [LEN]           Read 32-bit words
  25. r, rb, read, read8        ADDR [LEN]           Read 8-bit bytes
  26. rap, readap               [APSEL] ADDR         Read AP register
  27. rdp, readdp               ADDR                 Read DP register
  28. reg                       [-f] [REG]           Print core or peripheral register(s).
  29. reinit                                         Reinitialize the target object
  30. reset                     [-h/--halt]          Reset the target
  31. rmbreak                   ADDR                 Remove a breakpoint
  32. rmwatch                   ADDR                 Remove a watchpoint
  33. savemem                   ADDR LEN FILENAME    Save a range of memory to a binary file
  34. set                       NAME VALUE           Set an option value
  35. show                      INFO                 Report info about the target
  36. st, stat, status                               Show the target's current state
  37. s, step                                        Step one instruction
  38. symbol                    NAME                 Show a symbol's value.
  39. unlock                                         Unlock security on the target
  40. watch                     ADDR [r|w|rw] [1|2|4] Set a watchpoint address, and optional access type (default rw) and size (4).
  41. where                     [ADDR]               Show symbol, file, and line for address.
  42. wreg                      [-r] REG VALUE       Set the value of a core or peripheral register.
  43. 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.
  44. 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.
  45. 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.
  46. wap, writeap              [APSEL] ADDR DATA    Write AP register
  47. wdp, writedp              ADDR DATA            Write DP register

  48. All register names are also available as commands that print the register's value.
  49. Any ADDR or LEN argument will accept a register name.
  50. Prefix line with $ to execute a Python expression.
  51. Prefix line with ! to execute a shell command.

  52. Info:
  53. ----
  54. cores                     Information about CPU cores in the target.
  55. fault                     Fault status information.
  56. graph                     Print the target object graph.
  57. hnonsec                   Display the current HNONSEC value used by the selected MEM-AP.
  58. hprot                     Display the current HPROT value used by the selected MEM-AP.
  59. locked                    Report whether the target is locked.
  60. map                       Target memory map.
  61. mem-ap                    Display the currently selected MEM-AP used for memory read/write commands.
  62. nreset                    Current nRESET signal state.
  63. option                    Show the current value of one or more user options.
  64. peripherals               List of target peripheral instances.
  65. si, step-into-interrupt   Display whether interrupts are enabled when single stepping.
  66. target                    General target information.
  67. uid                       Target's unique ID
  68. vc, vector-catch          Show current vector catch settings.

  69. Options:
  70. -------
  71. 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.
  72. hnonsec                   Set the current HNONSEC value used by the selected MEM-AP.
  73. hprot                     Set the current HPROT value used by the selected MEM-AP.
  74. log                       Set log level to one of debug, info, warning, error, critical
  75. mem-ap                    Select the MEM-AP used for memory read/write commands.
  76. nreset                    Set nRESET signal state. Accepts a value of 0 or 1.
  77. option                    Change the value of one or more user options.
  78. si, step-into-interrupt   Set whether to enable or disable interrupts when single stepping. Set to 1 to enable.
  79. vc, vector-catch          Control enabled vector catch sources.
復(fù)制代碼
   這里面的指令就比較多了,都是一些芯片調(diào)試相關(guān)的。下面列舉一些操作:
  1. 加載hex文件
  2. pyocd cmd --command load template.hex --target stm32f103rc
  3. 復(fù)位芯片
  4. pyocd cmd --command reset --target stm32f103rc
  5. 擦除芯片
  6. pyocd cmd --command erase --target stm32f103rc
  7. 芯片暫停(然后就可用操作單步、打斷點(diǎn)等)
  8. pyocd cmd --connect halt --target stm32f103rc
  9. 設(shè)置斷點(diǎn)
  10. pyocd cmd --command break 0x0800029e  --target stm32f103rc
  11. 單步運(yùn)行
  12. pyocd cmd --command step --target stm32f103rc
  13. 恢復(fù)執(zhí)行
  14. pyocd cmd --command go --target stm32f103rc
  15. 查詢芯片當(dāng)前狀態(tài)
  16. pyocd cmd --command status --target stm32f103rc
  17. 讀取4個(gè)字節(jié)
  18. pyocd cmd --command read32 0x40010c00 12 --connect attach --target stm32f103rc
  19. 打印內(nèi)核或者外設(shè)寄存器
  20. pyocd cmd --command reg  --connect attach --target stm32f103rc
復(fù)制代碼
   這些指令主要是電腦端GDB和pyOCD的GDB Service交互用的。當(dāng)然自己也可以試著調(diào)試,一般調(diào)試都要先讓芯片halt(暫停),然后才能插入斷點(diǎn)、單步、讀寫寄存器、讀寫內(nèi)存等操作。下面是一些簡單的調(diào)試過程:
  1. C:\Users\86188\Desktop\test> pyocd cmd --command load template.hex --target stm32f103rc
  2. [====================] 100%
  3. PS C:\Users\86188\Desktop\test> pyocd cmd --connect halt --target stm32f103rc
  4. Connected to STM32F103RC [Halted]: 07000001066bff303631544157112037a5a5a5a597969908
  5. pyocd> step
  6. PC = 0x080015de
  7. pyocd> step
  8. PC = 0x080004ce
  9. pyocd> break 0x080004ce
  10. Set breakpoint at 0x080004ce
  11. pyocd> status
  12. Core 0:  Halted
  13. pyocd> reg
  14.      r0: 0x080004cf       r6: 0x00000000      r12: 0x00000001
  15.      r1: 0x40022000       r7: 0x00000000       sp: 0x20001788
  16.      r2: 0x00000001       r8: 0x00000000       lr: 0x080015e1
  17.      r3: 0x7a6a5aa8       r9: 0x20000200       pc: 0x080004ce
  18.      r4: 0x00000000      r10: 0x08002504     xpsr: 0x01000000
  19.      r5: 0x20001124      r11: 0x00000000  primask: 0x00000000
  20. pyocd> go
  21. Device is halted; a debug event may have occurred
  22. pyocd> lsbreak
  23. 5 hardware breakpoints available
  24. 0: 0x080004ce
  25. pyocd> rmbreak 0x080004ce
  26. Removed breakpoint at 0x080004ce
  27. pyocd> go
  28. Successfully resumed device
  29. pyocd> status
  30. Core 0:  Running
  31. pyocd>
復(fù)制代碼
   先把程序load到芯片,然后讓芯片halt下來,再單步、打斷點(diǎn)、查看芯片狀態(tài)、查看寄存器、查看斷點(diǎn)、移除斷點(diǎn)、全速運(yùn)行、查看狀態(tài)。一般用keil或者IAR,只需要點(diǎn)擊幾個(gè)圖案就能完成芯片調(diào)試的操作,只能說明keil太方便了。這里是純粹用命令行控制,雖然難度增加了不少,但是對調(diào)試器的原理和功能會(huì)多點(diǎn)認(rèn)識,這就是進(jìn)步吧。后面再配合GDB,就能用GDB的方式調(diào)試代碼了。
    本來這篇介紹應(yīng)該在下午完成,但是出了點(diǎn)意外。我習(xí)慣在51平臺網(wǎng)頁在線編輯,一不小心按了ctrl+A+Delete,全部刪了。想要撤銷操作,按了ctrl+Z,居然不支持,更坑的地方是網(wǎng)頁默認(rèn)30秒保存,當(dāng)時(shí)就剩下幾秒時(shí)間,來不及操作,就被覆蓋保存。點(diǎn)擊恢復(fù),出現(xiàn)自動(dòng)保存的空白頁面。急忙聯(lián)系網(wǎng)站負(fù)責(zé)人,還是沒有找回資料。白白浪費(fèi)了3個(gè)小時(shí)。沒辦法晚上再花了3個(gè)半小時(shí),重新碼字,總算完成。這個(gè)經(jīng)歷給了一個(gè)教訓(xùn):一定要在本地保存!
    如果感覺這篇介紹對你有幫助,請支持一下。有任何疑問,歡迎留言討論!











評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評分

回復(fù)

使用道具 舉報(bào)

ID:354124 發(fā)表于 2020-10-26 02:35 | 顯示全部樓層
大佬你好,可以講解下如何讀取和設(shè)置寄存器嗎
回復(fù)

使用道具 舉報(bào)

ID:351097 發(fā)表于 2020-10-26 17:52 | 顯示全部樓層
timaker 發(fā)表于 2020-10-26 02:35
大佬你好,可以講解下如何讀取和設(shè)置寄存器嗎

你好,可以輸入pyocd cmd --command help,里面有關(guān)于讀寫寄存器的命令。
回復(fù)

使用道具 舉報(bào)

ID:1080590 發(fā)表于 2023-5-29 09:23 | 顯示全部樓層
請教下大佬,我把pyocd燒錄腳本打包成exe程序,在本地能實(shí)現(xiàn)燒錄等操作,換臺電腦就不行了,提升‘pyocd不是內(nèi)部或外部指令’,可我用pyinstaller打包時(shí)已經(jīng)把pyocd和liusb2個(gè)報(bào)單獨(dú)添加進(jìn)去了,求指導(dǎo)下應(yīng)該怎么做
回復(fù)

使用道具 舉報(bào)

ID:108361 發(fā)表于 2024-1-10 08:57 | 顯示全部樓層
這命令行調(diào)試有啥好處?
回復(fù)

使用道具 舉報(bào)

ID:20672 發(fā)表于 2024-2-5 15:28 | 顯示全部樓層
謝謝分享。正在摸索學(xué)習(xí)中
回復(fù)

使用道具 舉報(bào)

ID:584195 發(fā)表于 2025-2-2 13:33 | 顯示全部樓層
大佬這文章寫得太好了,有沒有分享一下,如何加載外部Qspi進(jìn)行燒寫的教程?謝謝了!
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表