| 1 | --- |
| 2 | title: User scripts |
| 3 | --- |
| 4 | |
| 5 | ## Introduction |
| 6 | |
| 7 | pyOCD has support for customization through what are called user scripts. These are Python scripts |
| 8 | that are loaded by pyOCD before it connects to the target. A user script can define functions that |
| 9 | are called as hooks at certain points in the connection lifetime, and can extend, modify, or |
| 10 | completely override the default behaviour. |
| 11 | |
| 12 | If a file named `pyocd_user.py` or `.pyocd_user.py` is placed in the project directory, pyOCD will |
| 13 | automatically detect it and load it as a user script. If you prefer another name, you can set the |
| 14 | `user_script` session option, for example in a [config file]({% link _docs/configuration.md %}), or |
| 15 | by passing the `--script=<path>` command line argument. If a relative path is set either with the |
| 16 | option or command line, it will be searched for in the project directory. |
| 17 | |
| 18 | |
| 19 | ## Examples |
| 20 | |
| 21 | This example user script shows how to add a new memory region. |
| 22 | |
| 23 | ```py |
| 24 | # This example applies to the Nordic nRF52 devices. |
| 25 | |
| 26 | def will_connect(board): |
| 27 | # Create the new ROM region for the FICR. |
| 28 | ficr = RomRegion( |
| 29 | name="ficr", |
| 30 | start=0x10000000, |
| 31 | length=0x460 |
| 32 | ) |
| 33 | |
| 34 | # Add the FICR region to the memory map. |
| 35 | target.memory_map.add_region(ficr) |
| 36 | ``` |
| 37 | |
| 38 | This example shows how to override the flash algorithm for an external flash memory. |
| 39 | |
| 40 | ```py |
| 41 | # This example applies to the NXP i.MX RT10x0 devices. |
| 42 | |
| 43 | # Unlike the previous example, the board argument is excluded here. |
| 44 | def will_connect(): |
| 45 | # Look up the external flash memory region. |
| 46 | extFlash = target.memory_map.get_first_matching_region(name="flexspi") |
| 47 | |
| 48 | # Set the path to an .FLM flash algorithm. |
| 49 | extFlash.flm = "MIMXRT105x_QuadSPI_4KB_SEC.FLM" |
| 50 | ``` |
| 51 | |
| 52 | This example demonstrates setting the DBGMCU_CR register after connecting for STM32 devices. |
| 53 | |
| 54 | ```py |
| 55 | # This example applies to the ST STM32L0x1 devicess. |
| 56 | |
| 57 | DBG_CR = 0x40015804 |
| 58 | |
| 59 | def did_connect(): |
| 60 | # Set STANDBY, STOP, and SLEEP bits all to 1. |
| 61 | target.write32(DBG_CR, 0x7) |
| 62 | ``` |
| 63 | |
| 64 | Another common use for a script is to initialize external memory such as SDRAM. |
| 65 | |
| 66 | |
| 67 | ## User-defined commands |
| 68 | |
| 69 | New commands accessible from the commander subcommand or gdbserver monitor commands can be easily created in a user |
| 70 | script. |
| 71 | |
| 72 | User defined commands are created by using the `@command()` decorator on a function. The name of the new command can |
| 73 | either be the same as the name of the decorated function, or can be set explicitly with a `name` (or first positional) |
| 74 | argument to the decorator. For instance, either `@command('mycmd')` or `@command(name='anothercmd')`. Note that the |
| 75 | decorator requires parentheses (it must be called as a function) even if there are no parameters. |
| 76 | |
| 77 | Parameters for the new command are automatically determined using introspection and type annotations. Arguments for |
| 78 | parameters of these types are converted to the appropriate type before the function is called as a command. |
| 79 | |
| 80 | Supported parameter types: |
| 81 | - `int` |
| 82 | - `float` |
| 83 | - `str` |
| 84 | - Variable arguments, e.g. `*args`. |
| 85 | |
| 86 | Keyword parameters are not allowed. |
| 87 | |
| 88 | An `int` parameter is converted using the same method as for other pyOCD commands. Hexadecimal and binary numbers are |
| 89 | allowed, digits can be separated by underscores, and so on. For variable arguments, type annotations are ignored and the |
| 90 | tuple passed to the function will contain strings as entered in the command invocation. |
| 91 | |
| 92 | The decorated function remains accessible as a regular function in the user script namespace, and is therefore callable |
| 93 | from other functions within the user script. This is true even if the function definition is not compatible with the |
| 94 | command decorator, for instance if it has invalid parameter types. |
| 95 | |
| 96 | Help for the new command can be specified by passing a `help` argument to the `@command` decorator. |
| 97 | |
| 98 | Example: |
| 99 | |
| 100 | ```py |
| 101 | @command(help="Decode and print the first few vectors") |
| 102 | def vectable(base: int): |
| 103 | vecs = target.read_memory_block32(base, 4) |
| 104 | print(f"Initial SP: {vecs[0]:#010x}") |
| 105 | print(f"ResetHandler: {vecs[1]:#010x}") |
| 106 | print(f"NMI: {vecs[2]:#010x}") |
| 107 | print(f"HardFault: {vecs[3]:#010x}") |
| 108 | ``` |
| 109 | |
| 110 | |
| 111 | ## Script globals |
| 112 | |
| 113 | A number of useful symbols are made available in the global namespace of user scripts. These include |
| 114 | both target related objects, as well as parts of the pyOCD Python API. |
| 115 | |
| 116 | The usual Python builtins are available. |
| 117 | |
| 118 | ### Objects and functions |
| 119 | |
| 120 | | Symbol | Description | |
| 121 | |--------|-------------| |
| 122 | | `aps` | Dictionary of CoreSight Access Port (AP) objects. The keys are the APSEL value. | |
| 123 | | `board` | The `Board` object. | |
| 124 | | `command` | Decorator for defining new commands. See [user-defined commands](#user_defined_commands) for details. | |
| 125 | | `debug` | Log a debug message. | |
| 126 | | `dp` | The CoreSight Debug Port (DP) object. | |
| 127 | | `error` | Output an error log. | |
| 128 | | `info` | Output an info-level log message. | |
| 129 | | `LOG` | `Logger` object for the user script. | |
| 130 | | `options` | The session options dictionary. | |
| 131 | | `probe` | The connected debug probe object. | |
| 132 | | `session` | The session object, which is the root of the connection object graph. | |
| 133 | | `target` | The `CoreSightTarget` or subclass instance representing the MCU. | |
| 134 | | `warning` | Log a warning. | |
| 135 | |
| 136 | ### Modules and classes |
| 137 | |
| 138 | | Symbol | Description | |
| 139 | |--------|-------------| |
| 140 | | `BreakpointType` | Enumeration of breakpoint types. | |
| 141 | | `DeviceRegion` | Device-type memory region class. | |
| 142 | | `Error` | The base class for all pyOCD exceptions. | |
| 143 | | `Event` | Enumeration of notification event types. | |
| 144 | | `exceptions` | Module containing the exception classes. | |
| 145 | | `FileProgrammer` | Utility class to program files to target flash. | |
| 146 | | `FlashEraser` | Utility class to erase target flash. | |
| 147 | | `FlashLoader` | Utility class to program raw binary data to target memory. Deprecated, use `MemoryLoader` instead. | |
| 148 | | `FlashRegion` | Flash memory region. | |
| 149 | | `HaltReason` | Enumeration of halt reasons. | |
| 150 | | `MemoryLoader` | Utility class to program raw binary data to target memory. | |
| 151 | | `MemoryMap` | Class representing the device's memory map. | |
| 152 | | `MemoryType` | Memory region type enumeration. | |
| 153 | | `pyocd` | The root pyOCD module. | |
| 154 | | `RamRegion` | RAM memory region. | |
| 155 | | `ResetType` | Reset type enumeration. | |
| 156 | | `RomRegion` | ROM memory region. | |
| 157 | | `RunType` | Enumeration of types of run operations (step or run). | |
| 158 | | `SecurityState` | Enumeration of core security states. | |
| 159 | | `State` | Enumeration of target state. | |
| 160 | | `Target` | Base target class. | |
| 161 | | `TransferError` | Exception class for all transfer errors. | |
| 162 | | `TransferFaultError` | Exception subclass of `TransferError` for memory transfer faults. | |
| 163 | | `VectorCatch` | Namespace class containing bit mask constants for vector catch options. | |
| 164 | | `WatchpointType` | Enumeration of watchpoint types. | |
| 165 | |
| 166 | |
| 167 | ## Delegate functions |
| 168 | |
| 169 | This section documents all functions that user scripts can provide to modify pyOCD's behaviour. Some are simply |
| 170 | notifications, while others allow for overriding of default behaviour. Collectively, these are called delegate functions. |
| 171 | |
| 172 | All parameters of user script delegate functions are optional. Parameters can be declared in any order, and |
| 173 | those that are not needed can be excluded. In fact, most parameters are not necessary because the same objects |
| 174 | are available as [script globals](#script_globals), for instance `session` and `target`. |
| 175 | |
| 176 | Those parameters that are present must have names matching the specification below, and there must not be |
| 177 | additional unspecified, required parameters (those without a default value). Extra optional parameters are |
| 178 | allowed but will never be passed any value other than the default, unless you call the function yourself from |
| 179 | within the script. |
| 180 | |
| 181 | _Note:_ Delegate functions override CMSIS-Pack debug sequences. See the [debug sequence documentation]({% link _docs/open_cmsis_pack_support.md %}#debug-access-sequences) for more details. |
| 182 | |
| 183 | |
| 184 | ### will_connect |
| 185 | |
| 186 | Pre-init notification for the board. |
| 187 | ``` |
| 188 | will_connect(board: Board) -> None |
| 189 | ``` |
| 190 | |
| 191 | **Parameters** \ |
| 192 | *board* - A `Board` instance that is about to be initialized. \ |
| 193 | **Result** \ |
| 194 | Ignored. |
| 195 | |
| 196 | ### did_connect |
| 197 | |
| 198 | Post-initialization notification for the board. |
| 199 | ``` |
| 200 | did_connect(board: Board) -> None |
| 201 | ``` |
| 202 | |
| 203 | **Parameters** \ |
| 204 | *board* - A `Board` instance. \ |
| 205 | **Result** \ |
| 206 | Ignored. |
| 207 | |
| 208 | ### will_init_target |
| 209 | |
| 210 | Hook to review and modify init call sequence prior to execution. |
| 211 | ``` |
| 212 | will_init_target(target: SoCTarget, init_sequence: CallSequence) -> None |
| 213 | ``` |
| 214 | |
| 215 | **Parameters** \ |
| 216 | *target* - An `SoCTarget` object about to be initialized. \ |
| 217 | *init_sequence* - The `CallSequence` that will be invoked. Because call sequences are |
| 218 | mutable, this parameter can be modified before return to change the init calls. \ |
| 219 | **Result** \ |
| 220 | Ignored. |
| 221 | |
| 222 | ### did_init_target |
| 223 | |
| 224 | Post-initialization notification. |
| 225 | ``` |
| 226 | did_init_target(target: SoCTarget) -> None |
| 227 | ``` |
| 228 | |
| 229 | **Parameters** \ |
| 230 | *target* - An `SoCTarget` object. \ |
| 231 | **Result** \ |
| 232 | Ignored. |
| 233 | |
| 234 | ### unlock_device |
| 235 | |
| 236 | Hook to perform any required unlock sequence. |
| 237 | ``` |
| 238 | unlock_device(target: SoCTarget) -> None |
| 239 | ``` |
| 240 | |
| 241 | **Parameters** \ |
| 242 | *target* - An `SoCTarget` object. \ |
| 243 | **Result** \ |
| 244 | Ignored. |
| 245 | |
| 246 | Called after the DP is initialised but prior to discovery. This hook delegate can be used to unlock debug |
| 247 | access to the target. It can also be used to perform other pre-discovery actions. |
| 248 | |
| 249 | Note that because this hoook is called prior to discovery, APs and cores are not yet created. This means |
| 250 | that any register accesses must be performed through the DP's methods. (However, it's possible to create |
| 251 | a temporary instance of 'AccessPort' or one of its subclasses, such as `MEM_AP`.) |
| 252 | |
| 253 | ### will_start_debug_core |
| 254 | |
| 255 | Notification hook for before core debug is enabled. |
| 256 | |
| 257 | ``` |
| 258 | will_start_debug_core(core: CoreTarget) -> None |
| 259 | ``` |
| 260 | |
| 261 | **Parameters** \ |
| 262 | *core* - A `CoreTarget` object about to be initialized. \ |
| 263 | **Result** \ |
| 264 | Ignored. |
| 265 | |
| 266 | This hook is called during connection, prior to any register accesses being performed on the |
| 267 | indicated core (aside from the CoreSight peripheral ID registers that were read to identify |
| 268 | the core's presence during discovery). |
| 269 | |
| 270 | ### start_debug_core |
| 271 | |
| 272 | Hook to enable debug for the given core. |
| 273 | ``` |
| 274 | start_debug_core(core: CoreTarget) -> Optional[bool] |
| 275 | ``` |
| 276 | |
| 277 | **Parameters** \ |
| 278 | *core* - A `CoreTarget` object about to be initialized. \ |
| 279 | **Result** \ |
| 280 | *True* Do not perform the normal procedure to start core debug. \ |
| 281 | *False/None* Continue with normal behaviour. |
| 282 | |
| 283 | ### did_start_debug_core |
| 284 | |
| 285 | Notification hook that core debug has been enabled. |
| 286 | ``` |
| 287 | did_start_debug_core(core: CoreTarget) -> None |
| 288 | ``` |
| 289 | |
| 290 | **Parameters** \ |
| 291 | *core* - A `CoreTarget` object. \ |
| 292 | **Result** \ |
| 293 | Ignored. |
| 294 | |
| 295 | This hook method is called once a debug has been enabled for a core, and it has been fully |
| 296 | identified. |
| 297 | |
| 298 | ### will_stop_debug_core |
| 299 | |
| 300 | Pre core disconnect notification hook for the core. |
| 301 | ``` |
| 302 | will_stop_debug_core(core: CoreTarget) -> None |
| 303 | ``` |
| 304 | |
| 305 | **Parameters** \ |
| 306 | *core* - A `CoreTarget` object. \ |
| 307 | **Result** \ |
| 308 | Ignored. |
| 309 | |
| 310 | ### stop_debug_core |
| 311 | |
| 312 | Core debug disable hook. |
| 313 | ``` |
| 314 | stop_debug_core(core: CoreTarget) -> Optional[bool] |
| 315 | ``` |
| 316 | |
| 317 | **Parameters** \ |
| 318 | *core* - A `CoreTarget` object. \ |
| 319 | **Result** \ |
| 320 | *True* Do not perform the normal procedure to disable core debug. \ |
| 321 | *False/None* Continue with normal behaviour. |
| 322 | |
| 323 | This delegate is only called if resuming the core on disconnect, e.g. the `resume_on_disconnect` session |
| 324 | option is True. Therefore, the delegate should ensure that the core has properly resumed execution if it |
| 325 | returns True. |
| 326 | |
| 327 | ### did_stop_debug_core |
| 328 | |
| 329 | Post core disconnect notification hook for the core. |
| 330 | ``` |
| 331 | did_stop_debug_core(core: CoreTarget) -> None |
| 332 | ``` |
| 333 | |
| 334 | **Parameters** \ |
| 335 | *core* - A `CoreTarget` object. \ |
| 336 | **Result** \ |
| 337 | Ignored. |
| 338 | |
| 339 | ### will_disconnect |
| 340 | |
| 341 | Pre-disconnect notification. |
| 342 | ``` |
| 343 | will_disconnect(target: SoCTarget, resume: bool) -> None |
| 344 | ``` |
| 345 | |
| 346 | **Parameters** \ |
| 347 | *target* - An `SoCTarget` object. \ |
| 348 | *resume* - The value of the `disconnect_on_resume` option. \ |
| 349 | **Result** \ |
| 350 | Ignored. |
| 351 | |
| 352 | ### did_disconnect |
| 353 | |
| 354 | Post-disconnect notification. |
| 355 | ``` |
| 356 | did_disconnect(target: SoCTarget, resume: bool) -> None |
| 357 | ``` |
| 358 | |
| 359 | **Parameters** \ |
| 360 | *target* - An `SoCTarget` object. \ |
| 361 | *resume* - The value of the `disconnect_on_resume` option. \ |
| 362 | **Result** \ |
| 363 | Ignored. |
| 364 | |
| 365 | ### will_reset |
| 366 | |
| 367 | ``` |
| 368 | will_reset(core: CoreTarget, reset_type: Target.ResetType) -> Optional[bool] |
| 369 | ``` |
| 370 | Pre-reset hook. |
| 371 | |
| 372 | **Parameters** \ |
| 373 | *core* - A `CoreTarget` instance. \ |
| 374 | *reset_type* - One of the `Target.ResetType` enumerations. \ |
| 375 | **Result** \ |
| 376 | *True* The hook performed the reset. \ |
| 377 | *False/None* Caller should perform the normal reset procedure. |
| 378 | |
| 379 | ### did_reset |
| 380 | |
| 381 | Post-reset notification. |
| 382 | ``` |
| 383 | did_reset(core: CoreTarget, reset_type: Target.ResetType) -> None |
| 384 | ``` |
| 385 | |
| 386 | **Parameters** \ |
| 387 | *core* - A `CoreTarget` instance. \ |
| 388 | *reset_type* - One of the `Target.ResetType` enumerations. \ |
| 389 | **Result** \ |
| 390 | Ignored. |
| 391 | |
| 392 | ### set_reset_catch |
| 393 | |
| 394 | Hook to prepare target for halting on reset. |
| 395 | ``` |
| 396 | set_reset_catch(core: CoreTarget, reset_type: Target.ResetType) -> Optional[bool] |
| 397 | ``` |
| 398 | |
| 399 | **Parameters** \ |
| 400 | *core* - A `CoreTarget` instance. \ |
| 401 | *reset_type* - One of the `Target.ResetType` enumerations. \ |
| 402 | **Result** \ |
| 403 | *True* This hook handled setting up reset catch, caller should do nothing. \ |
| 404 | *False/None* Perform the default reset catch set using vector catch. |
| 405 | |
| 406 | ### clear_reset_catch |
| 407 | |
| 408 | Hook to clean up target after a reset and halt. |
| 409 | ``` |
| 410 | clear_reset_catch(core: CoreTarget, reset_type: Target.ResetType) -> None |
| 411 | ``` |
| 412 | |
| 413 | **Parameters** \ |
| 414 | *core* - A `CoreTarget` instance. \ |
| 415 | *reset_type* - One of the `Target.ResetType` enumerations. \ |
| 416 | **Result** \ |
| 417 | Ignored. |
| 418 | |
| 419 | ### mass_erase |
| 420 | |
| 421 | Hook to override mass erase. |
| 422 | ``` |
| 423 | mass_erase(target: SoCTarget) -> Optional[bool] |
| 424 | ``` |
| 425 | |
| 426 | **Parameters** \ |
| 427 | *target* - An `SoCTarget` object. \ |
| 428 | **Result** \ |
| 429 | *True* Indicate that mass erase was performed by the hook. \ |
| 430 | *False/None* Mass erase was not overridden and the caller should proceed with the standard mass erase procedure. |
| 431 | |
| 432 | ### trace_start |
| 433 | |
| 434 | Notification to prepare for tracing the target. |
| 435 | ``` |
| 436 | trace_start(target: SoCTarget, mode: int) -> None |
| 437 | ``` |
| 438 | |
| 439 | **Parameters** \ |
| 440 | *target* - A `CoreSightTarget` object. \ |
| 441 | *mode* - The trace mode. Currently always 0 to indicate SWO. \ |
| 442 | *Result* - Ignored. |
| 443 | |
| 444 | ### trace_stop |
| 445 | |
| 446 | Notification to clean up after tracing the target. |
| 447 | ``` |
| 448 | trace_stop(target: SoCTarget, mode: int) -> None |
| 449 | ``` |
| 450 | |
| 451 | **Parameters** \ |
| 452 | *target* - A `CoreSightTarget` object. \ |
| 453 | *mode* - The trace mode. Currently always 0 to indicate SWO. \ |
| 454 | **Result** \ |
| 455 | Ignored. |
| 456 | |
| 457 | ### trace_capture |
| 458 | |
| 459 | Notification to capture trace data. |
| 460 | ``` |
| 461 | trace_capture(target: SoCTarget, mode: int) -> None |
| 462 | ``` |
| 463 | |
| 464 | **Parameters** \ |
| 465 | *target* - A `CoreSightTarget` object. \ |
| 466 | *mode* - The trace mode. Currently always 0 to indicate SWO. \ |
| 467 | **Result** \ |
| 468 | Ignored. |
| 469 | |
| 470 | ### trace_flush |
| 471 | |
| 472 | Notification to flush trace data. |
| 473 | ``` |
| 474 | trace_flush(target: SoCTarget, mode: int) -> None |
| 475 | ``` |
| 476 | |
| 477 | **Parameters** \ |
| 478 | *target* - A `CoreSightTarget` object. \ |
| 479 | *mode* - The trace mode. Currently always 0 to indicate SWO. \ |
| 480 | **Result** \ |
| 481 | Ignored. |