bashembeddedpipetelnetopenocd

Script to pipe instructions to OpenOCD telnet server


My goal is to create a script to program my embedded device using openocd.

Manually, I am able to successfully use telnet to program my device:

$ telnet localhost 4444
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Open On-Chip Debugger
> reset halt; program firmware.elf verify reset;
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x08003ddc msp: 0x20001980
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x08003ddc msp: 0x20001980
** Programming Started **
Padding image section 2 at 0x0800a3bc with 4 bytes (bank write end alignment)
Adding extra erase range, 0x0800a3c0 .. 0x0800afff
** Programming Finished **
** Verify Started **
** Verified OK **
** Resetting Target **
<Ctrl+]> <Ctrl+D>
telnet> Connection closed.

Now, I would like to script this behavior. So I try this...

"$ reset halt; program firmware.elf verify reset;" > /dev/tcp/127.0.0.1/4444
bash: reset halt; program firmware.elf verify reset;: No such file or directory

OpenOCD responds with this:

Info : 13000 3054792 server.c:100 add_connection(): accepting 'telnet' connection on tcp/4444
Error: 13001 3054792 telnet_server.c:382 telnet_input(): error during read: Connection reset by peer
Info : 13002 3054793 server.c:576 server_loop(): dropped 'telnet' connection

It says, error during read. Do I need to do something extra to collect the response, or is something else failing?


Solution

  • I've discovered a superior answer, that builds off my previous answer.

    I read the "REDIRECTION" section of the bash man pages (man bash), to learn all about moving inputs and outputs around. I collected some examples from the internet, and I cobbled them all together.

    I'll do my best to explain the changes and what they do.

    { echo "reset halt; program /home/zak/Development/GitHub/blues/note-zephyr/build/zephyr/zephyr.elf verify reset; sleep 0; exit;" >&3; cat <&3-; } 3<> /dev/tcp/127.0.0.1/4444
    

    Please feel free to clarify anything I get wrong in the comments, and I'll update this answer as I am able.

    Bash responds with:

    ��������Open On-Chip Debugger
    > reset halt; program firmware.elf verify reset; sleep 0; exit;
    target halted due to debug-request, current mode: Thread 
    xPSR: 0x01000000 pc: 0x08003ddc msp: 0x20001980
    target halted due to debug-request, current mode: Thread 
    xPSR: 0x01000000 pc: 0x08003ddc msp: 0x20001980
    ** Programming Started **
    Padding image section 2 at 0x0800a3bc with 4 bytes (bank write end alignment)
    Adding extra erase range, 0x0800a3c0 .. 0x0800afff
    ** Programming Finished **
    ** Verify Started **
    ** Verified OK **
    ** Resetting Target **
    

    OpenOCD responds with:

    Info : 47130 2809355 server.c:100 add_connection(): accepting 'telnet' connection on tcp/4444
    User : 47217 2809439 armv7m.c:696 armv7m_arch_state(): target halted due to debug-request, current mode: Thread
    xPSR: 0x01000000 pc: 0x08003ddc msp: 0x20001980
    User : 47309 2809524 armv7m.c:696 armv7m_arch_state(): target halted due to debug-request, current mode: Thread
    xPSR: 0x01000000 pc: 0x08003ddc msp: 0x20001980
    User : 47335 2809527 command.c:769 jim_echo(): ** Programming Started **
    Info : 47341 2809527 core.c:876 flash_write_unlock_verify(): Padding image section 2 at 0x0800a3bc with 4 bytes (bank write end alignment)
    Warn : 47351 2809527 core.c:552 flash_iterate_address_range_inner(): Adding extra erase range, 0x0800a3c0 .. 0x0800afff
    User : 51012 2810305 command.c:769 jim_echo(): ** Programming Finished **
    User : 51015 2810305 command.c:769 jim_echo(): ** Verify Started **
    User : 51264 2810547 command.c:769 jim_echo(): ** Verified OK **
    User : 51267 2810547 command.c:769 jim_echo(): ** Resetting Target **
    Debug: 12882 151681 command.c:146 script_debug(): command - sleep 0
    Debug: 12884 151682 command.c:146 script_debug(): command - exit
    Info : 51325 2810559 server.c:576 server_loop(): dropped 'telnet' connection
    

    Now, I have two-way communication with the OpenOCD telnet server!