Wednesday, January 5, 2022

[SOLVED] Programing an STM32F4 with OpenOCD on Raspberry Pi 4

Issue

I'm trying to flash the bootloader on an STM32F407 via the SWD interface, the board in question is the Makerbase Robin Nano v3 this is a 3d printer controller board.

I have OpenOCD running fine on my Raspberry Pi 4 and I'm able to read sectors of the internal memory

The problem is that I can't write, and I'm not sure why.

I tried a simple test with

READ  -> stm32f4.cpu mdb 0x8000000 -> output: 0xFF
WRITE -> stm32f4.cpu mwb 0x8000000 0xAA -> no output
READ  -> stm32f4.cpu mdb 0x8000000 -> output: 0xFF // didn't change

Writing specific bytes doesn't seem to work.

If I try to program it with load_image this is the output

> load_image nano_v3_bootloader.bin 0x8000000     
SWD DPIDR 0x2ba01477
Failed to write memory and, additionally, failed to find out where

SWD DPIDR 0x2ba01477

If I try to program it with program this is the output

> program nano_v3_bootloader.bin 0x8000000
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc
** Programming Started **
SWD DPIDR 0xdeadbeef
Failed to write memory and, additionally, failed to find out where
error writing to flash at address 0x08000000 at offset 0x00000000
embedded:startup.tcl:308: Error: auto erase enabled

at file "embedded:startup.tcl", line 308

I'm brand new to ARM my guess is that maybe the bootloader is locked ( I tried to unlock it with stm32f4x unlock 0, didn't seem to make a difference), or I'm doing something in the wrong order or missing a step somewhere.

I would greatly appreciate it if someone could help me figure out what I'm doing wrong

This is my openocd.cfg

source [find interface/raspberrypi2-native.cfg]
transport select swd

set CHIPNAME stm32f4
source [find target/stm32f4x.cfg]

# did not yet manage to make a working setup using srst
#reset_config srst_only
reset_config  srst_nogate

adapter_nsrst_delay 100
adapter_nsrst_assert_width 100

init
targets
reset halt

And this is my raspberrypi2_native.cfg

#
# Config for using Raspberry Pi's expansion header
#
# This is best used with a fast enough buffer but also
# is suitable for direct connection if the target voltage
# matches RPi's 3.3V and the cable is short enough.
#
# Do not forget the GND connection, pin 6 of the expansion header.
#

adapter driver bcm2835gpio

bcm2835gpio_peripheral_base 0xFE000000

# Transition delay calculation: SPEED_COEFF/khz - SPEED_OFFSET
# These depend on system clock, calibrated for stock 700MHz
# bcm2835gpio_speed SPEED_COEFF SPEED_OFFSET
bcm2835gpio_speed_coeffs 236181 60

# Each of the JTAG lines need a gpio number set: tck tms tdi tdo
# Header pin numbers: 23 22 19 21
bcm2835gpio_jtag_nums 11 25 10 9

# Each of the SWD lines need a gpio number set: swclk swdio
# Header pin numbers: 23 22
bcm2835gpio_swd_nums 25 24

# If you define trst or srst, use appropriate reset_config
# Header pin numbers: TRST - 26, SRST - 18

# bcm2835gpio_trst_num 7
# reset_config trst_only

bcm2835gpio_srst_num 18
reset_config srst_only srst_push_pull

# or if you have both connected,
# reset_config trst_and_srst srst_push_pull

Solution

Running these commands fixed the problem

> flash erase_sector 0 0 11 // this will erase bank 0
> flash write_bank 0 nano_v3_bootloader.bin 0 // this will write the bin file to the bank 0


Answered By - Tiago Oliveira