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:



9 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:

2 Likes

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.

3 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

3 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)

1 Like

I am getting error “Unexpected command line argument: mass_eraseö” when programing as above with STlinkV2 when copy pate commmand openocd -f openocd.cfg -c “init” -c “halt” -c “nrf51 mass_erase” -c “program nrf51_vesc_ble_16k_16m_rx1_tx2_led3.bin verify reset exit”

I then manual typed the command and it programmed ok.

update:
fails to connect to VESC, ordered two more, they program ok but fail to connect. returned them for refund.

All nrf51 pcb modules are now using fake chips and fial to coonect.

Only buy modules sold as Vesc Bluetooth, like from flipsky or spintend or makerbase.

1 Like

@Morpheus this guide is excellent bit confused on h/w compatibility for the newer nRF52840 out there.

Vedder has a release for that

The programming appears to be the same but the chipsets mmm not so sure. This would allow older h/w like OF FOCboxes to have some limited cheap n’ nasty logging from FreeSK8 &/or UART remote capability.

Outside of running a Metr/iLogger/Robogotchi module(for logging) & limited availability of nRF52 receivers for remotes it would be sweet to DIY something.

I think now it’s time to move to a esp32 with vesc express firmware, the improvements are huge

2 Likes

Show us how Magi

1 Like

From the vesc discord, about $10 bom

I’d expect some basic single piece pcb designs to follow soon

3 Likes

Yass that is what I’m talking about. This would make an excellent thread.

Looks like the unofficial VESC discord invite no longer works

2 Likes

BOM:

US $0.85 5%OFF | SN65HVD230 CAN bus transceiver communication-module for arduino

US $1.26 18%OFF | 1PCS ESP32 Development Board WiFi+Bluetooth Ultra-Low Power Consumption Dual Core ESP-32 ESP-32S ESP 32 Similar ESP8266

US $0.40 12%OFF | 2x8 3x7 4x6 5x7 6x8 7x9 8x12 9x15 cm Double Side Prototype Diy Universal Printed Circuit PCB Board Protoboard For Arduino

US $2.32 31%OFF | Micro SD Storage Expansion Board Micro SD TF Card Memory Shield Module SPI For Arduino Promotion

2 Likes

Looks like a nice project. I may just start off with a nRF52 simple flash job though. It seems like it is easier to incorporate given the h/w is already part of the VESC proj

I know this is an old post but I couldn’t build the vesc express bin file from the source code. I setup the toolchain fine, there was a couple of errors, I’m still learning trying to figure this out. Any idea what’s going wrong?

esp-idf/bt/CMakeFiles/__idf_bt.dir/host/bluedroid/btc/profile/std/gap/btc_gap_ble.c.obj FAILED

C:/Espressif/frameworks/esp-idf-v5.1.1/components/bt/host/bluedroid/stack/include/stack/bt_types.h:35:34: error: ‘strncpy’ specified bound 33 equals destination size [-Werror=stringop-truncation]
35 | #define BCM_STRNCPY_S(x1,x2,x3) strncpy((x1),(x2),(x3))
| ^~~~~~~~~~~~~~~~~~~~~~~

C:/Espressif/frameworks/esp-idf-v5.1.1/components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_ble.c:89:9: note: in expansion of macro ‘BCM_STRNCPY_S’
89 | BCM_STRNCPY_S(param.get_dev_name_cmpl.name, name, BTC_MAX_LOC_BD_NAME_LEN + 1);
| ^~~~~~~~~~~~~
cc1.exe: some warnings being treated as errors