Search code examples
c#tclstm32openocd

OpenOCD Tcl interface


I am programming a C# application which will be used to program and test STM32 microcontrollers during production. I would like to program and verify the chip, then write some configuration to the flash memory and finally set the read-out protection. As a backend I decided to use OpenOCD and its Tcl interface running at port 6666.

The problem: I am able to execute commands and get their results, but I don't know how to check if the command was successfully executed or not. E.g. the reset command returns empty string no matters the target is connected or not... Some other commands like mdw return data or error string, but I am looking for some generic way how to check if the command succeeded or not.

Thank you for your ideas.


Solution

  • Assuming your Tcl code has a bit in its heart doing sendBack [eval $script], you'd change it to do this:

    set code [catch {eval $script} result]
    sendBack [list $code $result]
    

    or even this:

    set code [catch {eval $script} result options]
    sendBack [list $code $result $options]
    

    You'll need to unpack that list on the other side. The first element is the result code (0 for success, 1 for error, a few others theoretically but you probably won't see them), the second is the result value or the error message, and the third (if you use the second code snippet) is an options dictionary that can contain various things useful for debugging (including structured error codes, a stack trace, etc.)


    Passing back the full result tuple is how you transfer the entire result from one context to another. A number of remote debugging tools for Tcl use pretty much exactly the same trick.