Skip to content

Instantly share code, notes, and snippets.

@tstellanova
Last active March 25, 2020 18:18
Show Gist options
  • Save tstellanova/7d193fb8144d703e474ed37a371adfd0 to your computer and use it in GitHub Desktop.
Save tstellanova/7d193fb8144d703e474ed37a371adfd0 to your computer and use it in GitHub Desktop.
openocd configuration file for raspberry pi
# OpenOCD script for using Raspberry Pi as SWD Programmer for nRF52
# Based on https://devzone.nordicsemi.com/f/nordic-q-a/21650/flashing-nrf51-with-openocd-on-raspberry-pi
# https://stackoverflow.com/questions/52308978/problem-flashing-nrf52-chip-using-openocd/54372481#54372481
# https://www.raspberrypi.org/forums/viewtopic.php?t=252551
# https://learn.adafruit.com/programming-microcontrollers-using-openocd-on-raspberry-pi?view=all
# Build OpenOCD on Raspberry Pi with latest patches:
# sudo apt install git autoconf libtool make pkg-config libusb-1.0-0 libusb-1.0-0-dev libhidapi-dev libftdi-dev
# git clone https://github.com/ntfreak/openocd
# cd openocd
# ./bootstrap
# ./configure --enable-sysfsgpio --enable-bcm2835gpio --enable-cmsis-dap
# Check for << CMSIS-DAP Compliant Debugger yes >>
# make
# Run OpenOCD on Raspberry Pi:
# sudo /home/pi/openocd/src/openocd \
# -s /home/pi/openocd/tcl \
# -d4 \
# -f swd-pi.ocd
# Sample log is in swd-pi.log
# Select the Broadcom GPIO interface for Raspbery Pi
interface bcm2835gpio
###############################################################################
# Peripheral Base Address
# For Pi 4
# bcm2835gpio_peripheral_base 0xFE000000
# For Pi 2 and 3
# bcm2835gpio_peripheral_base 0x3F000000
# For Pi 1
# bcm2835gpio_peripheral_base 0x20000000
# For Pi Zero, Pi Zero W, and Compute Module
bcm2835gpio_peripheral_base 0x20000000
###############################################################################
# Transition delay calculation: SPEED_COEFF/khz - SPEED_OFFSET
# bcm2835gpio_speed SPEED_COEFF SPEED_OFFSET
# For Pi 4
# bcm2835gpio_speed_coeffs 236181 60
# For Pi 3 BCM2837 (1200Mhz)
# bcm2835gpio_speed_coeffs 194938 48
# For Pi 2 BCM2836 (900Mhz)
# bcm2835gpio_speed_coeffs 146203 36
# For Pi 1 BCM2835 (700Mhz) as well as Pi 0/W and similar:
bcm2835gpio_speed_coeffs 113714 28
###############################################################################
# SWD Configuration
# Assign each SWD line (SWCLK, SWDIO) to a Pi GPIO: (See https://pinout.xyz/ for Pi Header Pin Number to GPIO mapping)
# Connect Pi Header Pin Number 38 (GPIO 20) to nRF52 SWCLK (Yellow wire)
# Connect Pi Header Pin Number 40 (GPIO 21) to nRF52 SWDIO (Green wire)
bcm2835gpio_swd_nums 20 21
# Assign SRST (Reset) line to a Pi GPIO:
# Connect Pi Header Pin Number 12 (GPIO 18) to nRF52 RESET (White wire)
bcm2835gpio_srst_num 18
# Connect Pi 3.3V (Red) and GND (Purple) to nRF52 VDD and GND
###############################################################################
# Transport
transport select swd
###############################################################################
# Target
set WORKAREASIZE 0
source [find target/nrf52.cfg]
# Configure reset to use SRST line
reset_config srst_only srst_nogate
adapter_nsrst_delay 100
adapter_nsrst_assert_width 100
###############################################################################
# Execution
init
# Open another command prompt on Raspberry Pi and enter
# telnet localhost 4444
# To remove nRF52 flash protection, enter these commands...
# Check APPROTECTSTATUS status register for access port protection.
# Should show 0, which means protection enabled.
# nrf52.dap apreg 1 0x0c
# Set ERASEALL register to 1 to erase all FLASH and RAM and remove access port protection.
# nrf52.dap apreg 1 0x04 0x01
# Check ERASEALL register. Should show 1 when erasing FLASH and RAM and removing access port protection.
# Power off and on to perform the erase.
# nrf52.dap apreg 1 0x04
# After erasing, check APPROTECTSTATUS status register for access port protection.
# Should show 1, which means protection disabled.
# telnet localhost 4444
# targets
# halt
# nrf52.dap apreg 1 0x0c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment