| 1 | --- |
| 2 | title: Introduction to the API |
| 3 | --- |
| 4 | |
| 5 | Using pyOCD's Python API, you have extreme flexibility and precise control, and can do anything |
| 6 | SWD allows, at the expense of more complexity compared to `pyocd commander`. Using pyOCD like this is |
| 7 | particularly useful for situations where other debuggers become ineffective, such as device and |
| 8 | board bringup, or automated testing. |
| 9 | |
| 10 | This document assumes familiarity with the Arm CoreSight debug architecture. |
| 11 | |
| 12 | See the [architecture]({% link _docs/api/architecture.md %}) documentation for an overview of the classes and how |
| 13 | they are connected. |
| 14 | |
| 15 | ## Connecting |
| 16 | |
| 17 | pyOCD provides a handful of helper routines that make it very easy to enumerate and connect to |
| 18 | available debug probes. These routines are all available as static methods on the `ConnectHelper` |
| 19 | class in `pyocd.core.helpers`. |
| 20 | |
| 21 | `ConnectHelper.session_with_chosen_probe()` is the primary connection helper. This method returns |
| 22 | a single `Session` object, or None. If only a single probe is available, a new session for that |
| 23 | probe is returned immediately. But if there are multiple probes available, then by default, it will |
| 24 | present a simple console UI that lets the user select which probe they want to use. Other options |
| 25 | allow automatically picking the first available probe. |
| 26 | |
| 27 | One of the most useful parameters for `session_with_chosen_probe()` is `unique_id`. Pass whole or |
| 28 | part of a probe's unique ID (aka serial number) to programmatically select a specific probe. |
| 29 | |
| 30 | Session options may be passed to `session_with_chosen_probe()` in two ways. The `options` |
| 31 | parameter accepts a dictionary of session options. Or, you may pass options as keyword parameters. |
| 32 | The two methods may be combined. |
| 33 | |
| 34 | |
| 35 | ## DP access |
| 36 | |
| 37 | The DP is controlled through an instance of the `DebugPort` class (in `pyocd.coresight.dap`). You |
| 38 | get the `DebugPort` object via the 'dp' attribute of the target instance, i.e., `session.board.target.dp`. |
| 39 | |
| 40 | The `DebugPort` class has `read_reg(addr)` and `write_reg(addr, data)` methods. 'addr' must be an |
| 41 | integer in the set (0x0, 0x4, 0x8, 0xC). |
| 42 | |
| 43 | Example: |
| 44 | ```py |
| 45 | x = session.board.target.dp.read_reg(0x4) |
| 46 | session.board.target.dp.write_reg(0x8, 0x1) |
| 47 | ``` |
| 48 | |
| 49 | For completeness, the DebugPort class also has `readDP(addr)`, `writeDP(addr, data)`, `readAP(addr)`, and |
| 50 | `writeAP(addr, data)` methods. They work as described below, except that the `readAP()` and `writeAP()` methods |
| 51 | require the APSEL in the address (i.e., 0x010000fc to read ID of APSEL=1). The AP will automatically be |
| 52 | selected. |
| 53 | |
| 54 | |
| 55 | ## AP access |
| 56 | |
| 57 | CoreSight APs are represented with `AccessPort` classes defined in `pyocd.coresight.ap`. These include |
| 58 | the `MEM_AP` subclass and `AHB_AP` subclass of that. |
| 59 | |
| 60 | To get the AP objects you can use the 'aps' attribute of the `DebugPort`. This attribute is a dict |
| 61 | with the keys being the APSEL number and values being AccessPort instances. For instance, use |
| 62 | `session.board.target.aps[1]` to get the AP with APSEL=1, assuming it exists (if not, you'll get an |
| 63 | `IndexError` exception). |
| 64 | |
| 65 | `AccessPort` also has `read_reg(addr)` and `write_reg(addr, data)` methods. For these methods, `addr` is |
| 66 | an integer of the register offset. Note that you do not need to include the APSEL in the address, and |
| 67 | you do not need to modify the DP's SELECT register prior to accessing AP registers. The AP will |
| 68 | automatically be selected in the DP as required. |
| 69 | |
| 70 | The `MEM_AP`/`AHB_AP` class has the memory access methods that are available on |
| 71 | the target, but the access is, of course, performed through that specific AP. This is particularly |
| 72 | useful for multicore devices or Cortex-A class devices. |
| 73 | |
| 74 | Example showing access of the proprietary MDM-AP of NXP Kinetis MCUs: |
| 75 | ```py |
| 76 | mdm_ap = session.board.target.dp.aps[1] |
| 77 | idr = mdm_ap.read_reg(0xfc) # Read IDR. |
| 78 | mdm_ap.write_reg(0x4, 0x1) |
| 79 | ``` |
| 80 | |
| 81 | |
| 82 | ## Reset control |
| 83 | |
| 84 | To control reset, there are several options. |
| 85 | |
| 86 | `DebugPort` has methods for driving the hardware reset signal: |
| 87 | - `DebugPort.reset()`, asks the debug probe to perform a hardware reset of the target. |
| 88 | - `DebugPort.assert_reset(asserted)` to directly control the nRESET signal. Pass True to drive |
| 89 | nRESET low, False to drive high. |
| 90 | |
| 91 | A wider range of reset options is provided by these `Target` methods: |
| 92 | - `Target.reset(reset_type=None)`. Normally performs a software reset unless the optional parameter |
| 93 | is set to False. |
| 94 | - `Target.reset_and_halt(reset_type=None)` to perform a halting reset. Again, the reset defaults |
| 95 | to software but may be set to hardware. |
| 96 | |
| 97 | The `reset_type` parameter on the `Target` reset methods can be set to one of the `Target.ResetType` |
| 98 | enums: |
| 99 | - `ResetType.DEFAULT`: Uses the core's default reset method. For pack targets `defaultResetSequence` is used. |
| 100 | For internal targets, the default is mapped to `ResetType.SYSTEM`, unless `default_reset_type` |
| 101 | property is explicitly set. |
| 102 | - `ResetType.HARDWARE`: Execute `ResetHardware` debug sequence for pack targets, if the sequence doesn't exist, |
| 103 | hardware reset using the nSRST signal is used. The same mapping applies for internal targets. |
| 104 | - `ResetType.SYSTEM`: Execute `ResetSystem` debug sequence for pack targets, if the sequence doesn't exist, |
| 105 | software reset using SYSRESETREQ is used. The same mapping applies for internal targets. |
| 106 | - `ResetType.CORE`: Execute `ResetProcessor` debug sequence for pack targets, if the sequence doesn't exist, |
| 107 | software reset using VECTRESET is used. The same mapping applies for internal targets. |
| 108 | - `ResetType.NSRST`: Hardware reset using the nSRST signal. |
| 109 | - `ResetType.SYSRESETREQ`: Software reset using SYSRESETREQ, which usually resets the entire system |
| 110 | on most MCUs. |
| 111 | - `ResetType.VECTRESET`: Software reset using VECTRESET, only available on v7-M targets. This |
| 112 | resets only the core itself. If requested on non-v7-M targets, it will fall back to `ResetType.EMULATED`. |
| 113 | - `ResetType.EMULATED`: Restores the core to reset conditions by writing registers. However, this |
| 114 | will not trigger a reset vector catch. |
| 115 | |
| 116 | The `CortexM` objects have a `default_reset_type` property that lets you control the overall default reset type. |
| 117 | Property value can be any one of the `ResetType` enums. |
| 118 | |
| 119 | Another option for performing a halting reset is by setting vector catch with the target's `set_vector_catch()` |
| 120 | method, then using a normal reset. This has the benefit of always halting at reset, if you leave the |
| 121 | vector catch enabled. |
| 122 | |
| 123 | Example timed reset using the DP: |
| 124 | ``` |
| 125 | import time |
| 126 | |
| 127 | dp = session.board.target.dp |
| 128 | |
| 129 | # Timed reset. |
| 130 | dp.assert_reset(True) |
| 131 | time.sleep(1.0) |
| 132 | dp.assert_reset(False) |
| 133 | ``` |
| 134 | |
| 135 | |
| 136 | ## Notes |
| 137 | |
| 138 | You are encouraged to look through the code to see what additional functionality is available. The |
| 139 | most interesting places to look at are: |
| 140 | |
| 141 | - pyocd.core.target: defines Target class, which is the main API. |
| 142 | - pyocd.core.coresight_target: Represents the chip as a whole, |
| 143 | provides access to DP and APs, as well as each of the cores. |
| 144 | - pyocd.coresight.cortex_m: CortexM class to control a core, |
| 145 | implements Target API and adds some stuff. |
| 146 | - pyocd.flash.loader: high level flash programming of raw binary data. |
| 147 | - pyocd.flash.eraser: high level flash erasing. |
| 148 | - pyocd.flash.file_programmer: high level file programming. |
| 149 | - pyocd.flash.flash: low level flash programming API in the @ref pyocd.flash.flash.Flash "Flash" |
| 150 | class. Each flash memory region has a @ref pyocd.flash.flash.Flash "Flash" class instance |
| 151 | associated with it, accessible from the `flash` property on the region. To get the boot flash |
| 152 | memory region, call `target.memory_map.get_boot_memory()`. The `flash` attribute of this region |
| 153 | then returns the boot flash memory's @ref pyocd.flash.flash.Flash "Flash" instance. |