OpenOCD
|
This page provides a primer for writing commands by introducing hello
module.
The full source code used in this example can be found in hello.c, and the Trying These Example Commands section shows how to use it.
A summary of this information can be found in OpenOCD Command API .
Defining new commands and their helpers is easy. The following code defines a simple command handler that delegates its argument parsing:
Here, the COMMAND_HANDLER
macro establishes the function signature, see in command.h by the __COMMAND_HANDLER
macro.
The COMMAND_HELPER macro function allows defining functions with an extended version of the base signature. These helper functions can be called (with the appropriate parameters), the CALL_COMMAND_HANDLER
macro to pass any e as parameters to the following helper function:
The subsequent blocks of code are a normal C function that can do anything, so only complex commands deserve should use command helper functions. In this respect, this example uses one to demonstrate how – not when – they should be used.
Of course, you may also call other macros or functions, but that extends beyond the scope of this tutorial on writing commands.
Before this new function can be used, it must be registered somehow. For a new module, registering should be done in a new function for the purpose, which must be called from openocd.c:
Note that the "usage" text should use the same EBNF that's found in the User's Guide: literals in 'single quotes', sequences of optional parameters in [square brackets], and alternatives in (parentheses|with|vertical bars), and so forth. No angle brackets.
That's it! The command should now be registered and available to scripts.
This example also shows how to chain command handler registration, so your modules can "inherit" commands provided by other (sub)modules. Here, the hello module includes the foo commands in the same context that the 'hello' command will be registered.
If the chain
field had been put in the 'hello' command, then the foo
module commands would be registered under it. Indeed, that technique is used to define the 'foo bar' and 'foo baz' commands, as well as for the example drivers that use these modules.
The code for the 'foo' command handlers can be found in hello.c
.
These commands have been inherited by the dummy interface, faux flash, and testee target drivers. The easiest way to test these is by using the dummy interface.
Once OpenOCD has been built with this example code, the following command demonstrates the abilities that the hello
module provides:
If saved in hello.cfg
, then running openocd -f hello.cfg
should produce the following output before displaying the help text and exiting: