esk8.news esk8 calc donate now

NRF51822 Flashing / Setup in Windows using a STLink V2

NOTE: You can use your VESC to do this whole think a lot easier. See video below:
https://www.youtube.com/watch?v=PFFiVxFHDM4

How to do it the hard way:

After some issues getting mine setup and working I thought I’d post a guide of how I did it.

Please do double check pins with what your board/programmer lists as they could vary

Also there is a guide here if you want to follow Vedders guide:

Vedders Guide:

https://github.com/vedderb/nrf51_vesc/blob/master/README.md

Parts you need:

  • NRF51822 Module (See bottom for link, the one I used was a 16k ram version)
  • STLink V2 (See bottom for links)
  • JST PH 2.0mm 7 pin connector, Crimp Tool & Wire
    or
  • Premade JST PH 2.0mm 7 pin wiring loom

Download these:

Steps:

  1. Download needed files.
  2. Install STLink drivers.
  3. Unzip the OpenOCD files & Vedders NRF51 repo.
  4. Copy “openocd.cfg” from the NRF51 repo to the OpenOCD “bin” directory (same dir as openocd.exe)
  5. Copy the relevant nrf51 hex from the NRF51 repo (under “nrf51_vesc-master\build_all” dir) to the OpenOCD “bin” directory. For me I used “nrf51_vesc_ble_16k_16m_rx11_tx9_led3.bin” as that matched the NRF module I bought (link below).
  6. Wire up the STLinkV2 to the NRF51 module; see this for pin assignment: https://github.com/vedderb/nrf51_vesc/blob/master/README.md
  7. Open up a Command Prompt window and browse to the OpenOCD bin folder (or type cmd.exe into the address bar of Windows Explorer if you are already in that folder to open a Cmd prompt already at that folder).
  8. Run this command (Change the name of the .bin file to whichever firmware you are using:
    openocd -f openocd.cfg -c “init” -c “halt” -c “nrf51 mass_erase” -c “program nrf51_vesc_ble_16k_16m_rx11_tx9_led3.bin verify reset exit”
    a. You should see an output something like this:
OpenOCD Output

C:\Users\USER1\Downloads\openocd-20200524\OpenOCD-20200524-0.10.0\bin>openocd -f openocd.cfg -c “init” -c “halt” -c “nrf51 mass_erase” -c “program nrf51_vesc_ble_16k_16m_rx11_tx9_led3.bin verify reset exit”
Open On-Chip Debugger 0.10.0 (2020-05-24) [https://github.com/sysprogs/openocd]
Licensed under GNU GPL v2
libusb1 09e75e98b4d9ea7909e8837b7a3f00dda4589dc3
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
WARNING: interface/stlink-v2.cfg is deprecated, please switch to interface/stlink.cfg
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : clock speed 1000 kHz
Info : STLINK v2 JTAG v27 API v2 SWIM v6 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 3.554792
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Stlink adapter speed set to 950 kHz
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
target halted due to debug-request, current mode: Handler SVCall
xPSR: 0x2100000b pc: 0x0000110c msp: 0x20003eb0
Info : nRF51822-QFAA(build code: G3) 256kB Flash, 16kB RAM
target halted due to debug-request, current mode: Thread
xPSR: 0xc1000000 pc: 0xfffffffe msp: 0xfffffffc
** Programming Started **
Warn : Adding extra erase range, 0x0001f288 … 0x0001f3ff
target halted due to breakpoint, current mode: Thread
xPSR: 0x61000000 pc: 0x20000020 msp: 0xfffffffc
** Programming Finished **
** Verify Started **
target halted due to breakpoint, current mode: Thread
xPSR: 0x61000000 pc: 0x2000002e msp: 0xfffffffc
** Verified OK **
** Resetting Target **
shutdown command invoked

  1. Now disconnect your STLink and your NRF51 module and wire your NRF51 module to your VESC as shown here (Take note, the pin assignment for TX/RX might be different if you used a different NRF Firmware than me):
    https://github.com/vedderb/nrf51_vesc/blob/master/README.md
  2. Make sure the firmware of your VESC is up to date and that UART (Baudrate 115200 bps) is enabled in the VESC tool -> App Settings.
  3. Profit! It should all work now.
Troubleshooting
  • The bluetooth device does not appear on my phone.
    • Possible issues:
      • Wrong firmware was flashed to NRF Module.
      • Firmware was not successfully flashed.
      • The VESC is not powered or you have wired it to the VESC incorrectly.
  • OpenOCD Error: Error: init mode failed (unable to connect to the target)
    • Possible issues:
      • You have not wired the NRF Module to the STLink correctly or the cable is bad.
      • NRF Module might be dud?
  • OpenOCD Error: Error: no device found
    • Possible issues:
      • You have not installed the STLinkV2 driver.
      • STLinkV2 might be dud?
  • OpenOCD Error: embedded:startup.tcl:26: Error: Can’t find openocd.cfg
    • Possible issues:
      • You have not copied the openocd.cfg to the same folder as OpenOCD from the NRF51 repo.
openocd.cfg

# nRF51822 Target
source [find interface/stlink-v2.cfg]

transport select hla_swd

set WORKAREASIZE 0x4000
source [find target/nrf51.cfg]

# use hardware reset, connect under reset
#reset_config srst_only srst_nogate

  • VESC-TOOL on my mobile sees the bluetooth device but cannot connect to the VESC.
    • Possible issues:
      • You have got the Tx/Rx wired incorrectly between the NRF and VESC. Remember these are crossed so NRF Tx goes to VESC Rx.
      • Your VESC has an issue. There was a report somewhere where the wiring trace between the MCU and the connector on their VESC was bad.

Part Links:



7 Likes

An easier alternative is to use vesc tool to flash the NRF51822. There is a guide by Vedder here.

It took me 10min including some soldering of wires :slight_smile:

4 Likes

I would go for flashing using a vesc, it’s simpler and detects the module you plugged in

1 Like

@Scepterr @Fatglottis at the time I only had a VESC 4.12 so couldn’t use this method unfortunately. That’s why I went down the STLink route.

hmm I believe it works for 4.12 too just need current firmware and vesctool

1 Like

Oh damn, I didn’t think the 4.12 had the hardware capability. Well I guess this guide is only useful to those without a vesc lol! :rofl::rofl::rofl:

2 Likes

still good info to have :+1:

1 Like

Yeah; I’ve edited the title and added the easy way at the top for others.

1 Like

I flashed my nrf52 using my vesc way easier.

2 Likes

Just a side note _ some of these modules come locked. like ebyte 73 modules I wasted a whole day trying to work it out why I couldnt flash some of my modules. Some have Flash protection it can be removed but it is additional steps. You need to use a jlink or in a pinch a rapberrypi.
Check out the section on removing flash protection. https://medium.com/@ly.lee/coding-nrf52-with-rust-and-apache-mynewt-on-visual-studio-code-9521bcba6004

2 Likes

So I am about to flash my module using this method since my vesc doesn’t have an swd port. In step 5, how do you tell which hex to use?

I’ve determined I have the 16kb ram version.
Can I flash either nrf51_vesc_ble_16k_16m_rx11_tx9_led3.bin or nrf51_vesc_ble_16k_16m_rx1_tx2_led3.bin depending on which pins I want to use for rx/tx or does it depend on the specific hardware revision I have(mine is QFAAH2)