| 1 | --- |
| 2 | title: Python API examples |
| 3 | --- |
| 4 | |
| 5 | ## Hello World example code |
| 6 | |
| 7 | This example shows basic connection, loading a firmware binary, and some simple target control. |
| 8 | |
| 9 | ```python |
| 10 | #!/usr/bin/env python3 |
| 11 | from pyocd.core.helpers import ConnectHelper |
| 12 | from pyocd.flash.file_programmer import FileProgrammer |
| 13 | import time |
| 14 | |
| 15 | import logging |
| 16 | logging.basicConfig(level=logging.INFO) |
| 17 | |
| 18 | with ConnectHelper.session_with_chosen_probe() as session: |
| 19 | |
| 20 | board = session.board |
| 21 | target = board.target |
| 22 | flash = target.memory_map.get_boot_memory() |
| 23 | |
| 24 | # Load firmware into device. |
| 25 | FileProgrammer(session).program("my_firmware.bin") |
| 26 | |
| 27 | # Reset |
| 28 | target.reset_and_halt() |
| 29 | |
| 30 | # Read some registers. |
| 31 | print("pc: 0x%X" % target.read_core_register("pc")) |
| 32 | |
| 33 | target.step() |
| 34 | print("pc: 0x%X" % target.read_core_register("pc")) |
| 35 | |
| 36 | target.resume() |
| 37 | time.sleep(0.2) |
| 38 | target.halt() |
| 39 | print("pc: 0x%X" % target.read_core_register("pc")) |
| 40 | |
| 41 | target.reset_and_halt() |
| 42 | |
| 43 | print("pc: 0x%X" % target.read_core_register("pc")) |
| 44 | ``` |
| 45 | |
| 46 | |
| 47 | ## ELF files and breakpoints |
| 48 | |
| 49 | Expanding on the above example, this code demonstrates reading a symbol address from an ELF file |
| 50 | and setting a breakpoint. Then the target is reset and run until the breakpoint is hit. |
| 51 | |
| 52 | ```python |
| 53 | #!/usr/bin/env python3 |
| 54 | from pyocd.core.helpers import ConnectHelper |
| 55 | from pyocd.core.target import Target |
| 56 | from pyocd.debug.elf.symbols import ELFSymbolProvider |
| 57 | |
| 58 | # Connect to the target. |
| 59 | with ConnectHelper.session_with_chosen_probe() as session: |
| 60 | target = session.target |
| 61 | |
| 62 | # Set ELF file on target. |
| 63 | target.elf = "my_firmware.elf" |
| 64 | |
| 65 | # Look up address of main(). |
| 66 | provider = ELFSymbolProvider(target.elf) |
| 67 | addr = provider.get_symbol_value("main") |
| 68 | print("main() address: 0x%X" % addr) |
| 69 | |
| 70 | # Set breakpoint. |
| 71 | target.set_breakpoint(addr) |
| 72 | |
| 73 | # Reset and run. |
| 74 | target.reset() |
| 75 | |
| 76 | # Wait until breakpoint is hit. |
| 77 | while target.get_state() != Target.State.HALTED: |
| 78 | pass |
| 79 | |
| 80 | # Print PC. |
| 81 | pc = target.read_core_register("pc") |
| 82 | print("pc: 0x%X" % pc) |
| 83 | |
| 84 | assert pc == addr & ~0x01 # mask off LSB |
| 85 | |
| 86 | # Remove breakpoint. |
| 87 | target.remove_breakpoint(addr) |
| 88 | ``` |
| 89 | |
| 90 | Note that you currently need to manually remove a breakpoint in order to step or run over it. |
| 91 | |
| 92 | |
| 93 | ## Alternative ways to create a session |
| 94 | |
| 95 | It's important to understand that the `ConnectHelper.session_with_chosen_probe()` method doesn't |
| 96 | itself open the session for you. Instead, it's using the session object as a context manager that |
| 97 | does this. |
| 98 | |
| 99 | Here is an example where the `with` statement is separated from the creation of the session. |
| 100 | |
| 101 | ```python |
| 102 | # Connect to the target. |
| 103 | session = ConnectHelper.session_with_chosen_probe() |
| 104 | |
| 105 | # Manually open the session. When the 'with' statement exits, the session is closed. |
| 106 | with session: |
| 107 | # ... control the target |
| 108 | ``` |
| 109 | |
| 110 | This example demonstrates complete manual control of session opening and closing. |
| 111 | |
| 112 | ```python |
| 113 | # Connect to the target. |
| 114 | session = ConnectHelper.session_with_chosen_probe() |
| 115 | |
| 116 | # Manually open the session. |
| 117 | session.open() |
| 118 | |
| 119 | # ... control the target |
| 120 | |
| 121 | # Close the session and connection. |
| 122 | session.close() |
| 123 | ``` |
| 124 | |
| 125 | In reality you might want to put the call to `close()` in a `finally` clause of an exception handler. |
| 126 | |
| 127 | The example here shows how the `auto_open` parameter can be used to achieve a combination of the |
| 128 | above approaches. When set to False, the session will _not_ be opened upon entry into a `with` |
| 129 | statement. This allows using a `with` statement to ensure the session is properly closed, but with |
| 130 | manual opening so you could, perhaps, further configure the session before it is opened. |
| 131 | |
| 132 | ```python |
| 133 | # Here we set auto_open to False, so the 'with' statement won't open the session. |
| 134 | session = ConnectHelper.session_with_chosen_probe(auto_open=False) |
| 135 | |
| 136 | # When the 'with' statement exits, the session is closed. |
| 137 | with session: |
| 138 | # ... modify the session |
| 139 | |
| 140 | # Manually open the session. |
| 141 | session.open() |
| 142 | |
| 143 | # ... control the target |
| 144 | ``` |
| 145 | |
| 146 | Selecting a probe and configuring the target connection is done as follows: |
| 147 | |
| 148 | ```python |
| 149 | # Connect to the target with some options. |
| 150 | session = ConnectHelper.session_with_chosen_probe(unique_id = "E6616407E3646B29", options = {"frequency": 4000000, "target_override": "nrf52840"}) |
| 151 | |
| 152 | with session: |
| 153 | # ... |
| 154 | ``` |
| 155 | |
| 156 | More options can be found [here](options.md). |
| 157 | |
| 158 | |
| 159 | ## Semihosting |
| 160 | |
| 161 | For in-target tests it is sometimes convenient to use semihosting, e.g. write coverage data into the hosts file system. |
| 162 | To execute semihosting requests a `wait_for_halt()` function must be implemented. If writing the profile data is |
| 163 | explicitly done at end of `main()`, a breakpoint on return is also helpful. Implementation could be like this: |
| 164 | |
| 165 | ```python |
| 166 | #!/usr/bin/env python3 |
| 167 | from pyocd.core.helpers import ConnectHelper |
| 168 | from pyocd.core.target import Target |
| 169 | from pyocd.debug.elf.symbols import ELFSymbolProvider |
| 170 | from pyocd.flash.file_programmer import FileProgrammer |
| 171 | from pyocd.debug import semihost |
| 172 | import time |
| 173 | |
| 174 | import traceback |
| 175 | import logging |
| 176 | logging.basicConfig(level=logging.INFO) |
| 177 | |
| 178 | |
| 179 | image_name = "profiling.elf" |
| 180 | |
| 181 | |
| 182 | def wait_for_halt(target, semihost): |
| 183 | go_on = True |
| 184 | while go_on: |
| 185 | state = target.get_state() |
| 186 | if state == Target.State.HALTED: |
| 187 | try: |
| 188 | # Handle semihosting |
| 189 | go_on = semihost.check_and_handle_semihost_request() |
| 190 | if go_on: |
| 191 | # target was halted due to semihosting request |
| 192 | target.resume() |
| 193 | except Exception as e: |
| 194 | print("semihost exception/resume------", e) |
| 195 | print(traceback.format_exc()) |
| 196 | target.resume() |
| 197 | go_on = True |
| 198 | else: |
| 199 | time.sleep(0.01) |
| 200 | |
| 201 | |
| 202 | session = ConnectHelper.session_with_chosen_probe(unique_id = "E6616407E3646B29", options = {"frequency": 4000000, |
| 203 | "target_override": "nrf52840", |
| 204 | "enable_semihosting": True, |
| 205 | "semihost_use_syscalls": False}) |
| 206 | |
| 207 | with session: |
| 208 | target = session.target |
| 209 | target_context = target.get_target_context() |
| 210 | semihost_io_handler = semihost.InternalSemihostIOHandler() |
| 211 | semihost = semihost.SemihostAgent(target_context, io_handler=semihost_io_handler, console=semihost_io_handler) |
| 212 | |
| 213 | # Load firmware into device. |
| 214 | FileProgrammer(session).program(image_name) |
| 215 | |
| 216 | # Set ELF file on target. |
| 217 | target.elf = image_name |
| 218 | provider = ELFSymbolProvider(target.elf) |
| 219 | |
| 220 | # Look up address of main(). |
| 221 | addr = provider.get_symbol_value("main") |
| 222 | print("main() address: 0x%X" % addr) |
| 223 | |
| 224 | # Set breakpoint. |
| 225 | print("set breakpoint to entry of main()") |
| 226 | target.set_breakpoint(addr) |
| 227 | |
| 228 | target.reset_and_halt() |
| 229 | print("execute") |
| 230 | target.resume() |
| 231 | |
| 232 | # Wait until breakpoint is hit. |
| 233 | wait_for_halt(target, semihost) |
| 234 | |
| 235 | # Print PC. |
| 236 | pc = target.read_core_register("pc") |
| 237 | print(" pc: 0x%X" % pc) |
| 238 | assert pc == addr & ~0x01 # mask off LSB |
| 239 | target.remove_breakpoint(addr) |
| 240 | |
| 241 | # set breakpoint to return address and execute til there |
| 242 | lr = target.read_core_register("lr") |
| 243 | print(" lr: 0x%X (return address)" % lr) |
| 244 | print("set breakpoint to return of main()") |
| 245 | target.set_breakpoint(lr) |
| 246 | print("execute") |
| 247 | target.resume() |
| 248 | wait_for_halt(target, semihost) |
| 249 | |
| 250 | pc = target.read_core_register("pc") |
| 251 | print(" pc: 0x%X" % pc) |
| 252 | assert pc == lr & ~0x01 # mask off LSB |
| 253 | target.remove_breakpoint(lr) |
| 254 | ``` |