# How to Flash a Firmware with OpenOCD

This is a guide for flashing images for Redox Wireless rev2.0WHS. This should apply to other similar keyboards that uses nRF51.

Clone or download the precompiled images at <https://github.com/mattdibi/redox-w-firmware>

Connect a programmer to the board pin header with 3.3V, GND, CLK, IO.

Run&#x20;

```
openocd  -f interface/cmsis-dap.cfg  -f target/nrf51.cfg
```

Replace `interface/cmsis-dap.cfg` according to the programmer you use. For example, if you use an ST-LINK/V2, you should use `interface/stlink-v2.cfg`.

The terminal should show&#x20;

```
Open On-Chip Debugger 0.11.0
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "swd". To override use 'transport select <transport>'.
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : CMSIS-DAP: SWD  Supported
Info : CMSIS-DAP: JTAG Supported
Info : CMSIS-DAP: FW Version = 2.0.0
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 1 TDO = 0 nTRST = 0 nRESET = 1
Info : CMSIS-DAP: Interface ready
Info : clock speed 1000 kHz
Info : SWD DPIDR 0x0bb11477
Info : nrf51.cpu: hardware has 4 breakpoints, 2 watchpoints
Info : starting gdb server for nrf51.cpu on 3333
Info : Listening on port 3333 for gdb connections
```

Open another terminal and run&#x20;

```
telnet localhost 4444
```

It shows

```
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Open On-Chip Debugger
```

So now, we can send commands

```
init
reset init
flash write_image precompiled/precompiled-basic-left.hex
flash verify_image precompiled/precompiled-basic-left.hex
reset run
```

The terminal should show

```
> init
> reset init
target halted due to debug-request, current mode: Thread
xPSR: 0xc1000000 pc: 0x00000bb4 msp: 0x20004000
> flash write_image precompiled/precompiled-basic-left.hex
nRF51822-QFAA(build code: H0) 256kB Flash, 16kB RAM
wrote 15276 bytes from file precompiled/precompiled-basic-left.hex in 1.088257s (13.708 KiB/s)

> flash verify_image precompiled/precompiled-basic-left.hex
verified 15276 bytes from file precompiled/precompiled-basic-left.hex in 0.185782s (80.298 KiB/s)

> reset run
```

Another way to flash the image is to run

```
program precompiled/precompiled-basic-left.hex verify reset
```

The terminal should show

```
> program precompiled/precompiled-basic-left.hex verify reset
target halted due to debug-request, current mode: Thread
xPSR: 0xc1000000 pc: 0x00000bb4 msp: 0x20004000
** Programming Started **
Adding extra erase range, 0x00003bac .. 0x00003bff
** Programming Finished **
** Verify Started **
** Verified OK **
** Resetting Target **
```

You should also flash the right side, and the receiver with the respective image file.

If you prefer to flash it without telnet, you can use the -c flag with openocd, so you can flash an image with a one-liner command.

## Links

* <https://openocd.org/doc/html/Flash-Programming.html>
* <https://github.com/mattdibi/redox-w-firmware>
* <https://devzone.nordicsemi.com/f/nordic-q-a/6918/programming-nrf51822-with-openocd-and-stlink-v2-issues>
