How to run this code from RAM (OpenOCD)?

    $ openocd -f openocd.cfg
    Open On-Chip Debugger 0.10.0+dev-01157-gd6541a81-dirty (2020-04-01-20:52)
    Licensed under GNU GPL v2
    For bug reports, read
            http://openocd.org/doc/doxygen/bugs.html
    riscv.cpu
    Info : ftdi: if you experience problems at higher adapter clocks, try the command "ftdi_tdo_sample_edge falling"
    Info : clock speed 10000 kHz
    Info : JTAG tap: riscv.cpu tap/device found: 0x20000913 (mfg: 0x489 (SiFive, Inc.), part: 0x0000, ver: 0x2)
    Info : datacount=1 progbufsize=16
    Info : Disabling abstract command reads from CSRs.
    Info : Examined RISC-V core; found 1 harts
    Info :  hart 0: XLEN=32, misa=0x40101105
    Info : Listening on port 3333 for gdb connections
    Ready for Remote Connections
    Info : Listening on port 6666 for tcl connections
    Info : Listening on port 4444 for telnet connections
    Info : accepting 'telnet' connection on tcp/4444
    Info : JTAG tap: riscv.cpu tap/device found: 0x20000913 (mfg: 0x489 (SiFive, Inc.), part: 0x0000, ver: 0x2)
    Info : Disabling abstract command writes to CSRs.
    shutdown command invoked
    Info : dropped 'telnet' connection

    $ telnet localhost 4444
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    Open On-Chip Debugger
    > reset halt
    JTAG tap: riscv.cpu tap/device found: 0x20000913 (mfg: 0x489 (SiFive, Inc.), part: 0x0000, ver: 0x2)
    > mdw 0x80000000 8
    0x80000000: 00000197 0a218193 00001517 ff850513 00001597 ff058593 00b57863 00052023

    > load_image taos.elf
    204 bytes written at address 0x80000000
    3892 bytes written at address 0x800000cc
    downloaded 4096 bytes in 0.066149s (60.470 KiB/s)

    > mdw 0x80000000 8
    0x80000000: 00000197 0a218193 00001517 ff850513 00001597 ff058593 00b57863 00052023

    > resume 0x80000000
    Disabling abstract command writes to CSRs.
    > halt
    > reg pc
    pc (/32): 0x800000A0

    > shutdown
    shutdown command invoked
    Connection closed by foreign host.

How to run this code from FLASH (OpenOCD)?

    Make sure to use himudev_flash.lds linker script.

    $ telnet localhost 4444
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    Open On-Chip Debugger
    > reset halt
    JTAG tap: riscv.cpu tap/device found: 0x20000913 (mfg: 0x489 (SiFive, Inc.), part: 0x0000, ver: 0x2)
    > flash write_image erase unlock taos.elf
    Found flash device 'issi is25lp032' (ID 0x0016609d)
    Disabling abstract command writes to CSRs.
    auto erase enabled
    auto unlock enabled
    wrote 65536 bytes from file taos.elf in 2.526374s (25.333 KiB/s)

    > reset run
    JTAG tap: riscv.cpu tap/device found: 0x20000913 (mfg: 0x489 (SiFive, Inc.), part: 0x0000, ver: 0x2)
    > halt
    > reg pc
    pc (/32): 0x200000AC

    > shutdown
    shutdown command invoked
    Connection closed by foreign host.

How to load to flash and debug via gdb?

    Use himudev_flash.lds as before.

    You must compile the binary with debug flags. Changes in Makefile:
        - `cargo build` instead of `cargo build --release`
        - TYPE=debug

    $ riscv64-unknown-linux-gnu-gdb taos.elf
    GNU gdb (GDB) 8.3.0.20190516-git
    Copyright (C) 2019 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    Type "show copying" and "show warranty" for details.
    This GDB was configured as "--host=x86_64-pc-linux-gnu --target=riscv64-unknown-linux-gnu".
    Type "show configuration" for configuration details.
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>.
    Find the GDB manual and other documentation resources online at:
        <http://www.gnu.org/software/gdb/documentation/>.

    For help, type "help".
    Type "apropos word" to search for commands related to "word"...
    Reading symbols from taos.elf...
    (gdb) target ext :3333
    Remote debugging using :3333
    0x20000990 in core::num::flt2dec::estimator::estimate_scaling_factor () at src/libcore/num/mod.rs:2408
    2408    src/libcore/num/mod.rs: Nie ma takiego pliku ani katalogu.
    (gdb) monitor reset halt
    JTAG tap: riscv.cpu tap/device found: 0x20000913 (mfg: 0x489 (SiFive, Inc.), part: 0x0000, ver: 0x2)
    (gdb) load
    Loading section .text, size 0x28546 lma 0x20000000
    Loading section .rodata, size 0x15794 lma 0x20028550
    Loading section .eh_frame, size 0x28 lma 0x2003dce4
    Start address 0x20000000, load size 253186
    Transfer rate: 21 KB/sec, 14065 bytes/write.
    (gdb) b kmain
    Breakpoint 1 at 0x20000306: file src/lib.rs, line 80.
    (gdb) r
    The program being debugged has been started already.
    Start it from the beginning? (y or n) y
    Starting program: /home/pillot/projects/electronics/riscv/taos/taos-himudev/taos.elf
    Note: automatically using hardware breakpoints for read-only addresses.

    Breakpoint 1, kmain () at src/lib.rs:80
    80              mmio_write(0x10012008, 0, 0x780000);
    (gdb) s
    taos::mmio_write (address=268509192, offset=0, value=7864320) at src/lib.rs:61
    61              let reg = address as *mut u32;
    (gdb) 

How to properly set gdb paths?

    Please read this: https://rust-embedded.github.io/debugonomicon/

    It is necessary, along with normal commands (shown above), to issue
    `substitute-path`. In my case it is following GDB command:

    set substitute-path /rustc/6ef275e6c3cb1384ec78128eceeb4963ff788dca /home/my_username/.rustup/toolchains/nightly-2019-09-25-x86_64-unknown-linux-gnu/lib/rustlib/src/rust

    General case:

    set substitute-path /rustc/$(rustc -Vv | grep commit-hash | cut -d' ' -f2) $(rustc --print sysroot)/lib/rustlib/src/rust

    Or, in one go:

    riscv64-unknown-linux-gnu-gdb -ex "set substitute-path /rustc/$(rustc -Vv | grep commit-hash | cut -d' ' -f2) $(rustc --print sysroot)/lib/rustlib/src/rust" -x init_load.gdb taos.elf

How to debug using J-Link?

    This command starts J-Link for the HiFive board, for hIMUdev board we should change at least the memory layout.

    $ JLinkGDBServer -device FE310

    Or, in my case:

    ~/root/opt/JLink_Linux_V662d_x86_64/JLinkGDBServer -device FE310