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:
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:
- OpenOCD Windows Programmer: https://gnutoolchains.com/arm-eabi/openocd/
- STLink V2 drivers: https://www.st.com/en/development-tools/stsw-link009.html
- Vedders NRF51822 github repo: https://github.com/vedderb/nrf51_vesc
Steps:
- Download needed files.
- Install STLink drivers.
- Unzip the OpenOCD files & Vedders NRF51 repo.
- Copy “openocd.cfg” from the NRF51 repo to the OpenOCD “bin” directory (same dir as openocd.exe)
- 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).
- Wire up the STLinkV2 to the NRF51 module; see this for pin assignment: https://github.com/vedderb/nrf51_vesc/blob/master/README.md
- 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).
- 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
- 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 - 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.
- 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.
- Possible issues:
- 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?
- Possible issues:
- OpenOCD Error:
Error: no device found
- Possible issues:
- You have not installed the STLinkV2 driver.
- STLinkV2 might be dud?
- Possible issues:
- 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.
- Possible issues:
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.
- Possible issues:
Part Links: