Skip to content

Commit 6b752dc

Browse files
Added CM4 Flashing Tutorial
1 parent 367625b commit 6b752dc

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: "Tutorials"
3+
linkTitle: "Tutorials"
4+
weight: 99
5+
description: Tutorials to help you getting started
6+
---
7+
8+
This page is a growing collection of tutorials for common tasks with the xCore hardware.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
title: "Flashing STM32 From CM4"
3+
linkTitle: "Flashing STM32 From CM4"
4+
description: Tutorial for flashing the STM32 microcontroller from the Raspberry Pi CM4.
5+
---
6+
7+
In order to use the Raspberry Pi CM4 to upload firmware to the STM32, we need a custom version of OpenOCD which can use
8+
the CM4's GPIOs to connect to the STM32's SWD interface.
9+
10+
### Step 1: Building OpenOCD
11+
Execute the following commands in order to install the dependencies, build and install OpenOCD:
12+
```bash
13+
sudo apt install automake autoconf build-essential libtool libftdi-dev libusb-1.0-0-dev texinfo pkg-config rpi.gpio-common curl
14+
git clone https://github.com/raspberrypi/openocd.git --recursive
15+
cd openocd
16+
./bootstrap
17+
./configure --enable-ftdi --enable-sysfsgpio --enable-bcm2835gpio
18+
make -j$(nproc)
19+
sudo make install
20+
```
21+
22+
### Step 2: Get the OpenOCD Configuration
23+
Fetch the configuration and install it globally for OpenOCD to find it:
24+
```bash
25+
sudo curl -o /usr/local/share/openocd/scripts/interface/xcore.cfg https://core.x-tech.online/downloads/openocd-xcore.cfg
26+
```
27+
28+
### Step 3: Upload a binary
29+
Get a binary (.elf format) and upload it to the STM32 like this:
30+
```bash
31+
openocd -f interface/xcore.cfg -f target/stm32h7x.cfg -c "program your-binary.elf verify reset exit"
32+
```
33+
The firmware should be uploaded and the program should be running.

static/downloads/openocd-xcore.cfg

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
# Config for Raspberry Pi used as a bitbang adapter.
4+
# https://www.raspberrypi.com/documentation/computers/raspberry-pi.html
5+
6+
# Supports all models with 40-pin or 26-pin GPIO connector up to Raspberry Pi 4 B
7+
# also supports Raspberry Pi Zero, Zero W and Zero 2 W.
8+
9+
# Adapter speed calibration is computed from cpufreq/scaling_max_freq.
10+
# Adjusts automatically if CPU is overclocked.
11+
12+
adapter driver bcm2835gpio
13+
14+
proc read_file { name } {
15+
if {[catch {open $name r} fd]} {
16+
return ""
17+
}
18+
set result [read $fd]
19+
close $fd
20+
return $result
21+
}
22+
23+
proc measure_clock {} {
24+
set result [exec vcgencmd measure_clock arm]
25+
set clock_hz [lindex [split $result "="] 1]
26+
expr { $clock_hz / 1000 }
27+
}
28+
29+
proc get_max_cpu_clock { default } {
30+
set clock [read_file /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq]
31+
if { $clock > 100000 } {
32+
return $clock
33+
}
34+
35+
# cpufreq not available. As the last resort try Broadcom's proprietary utility
36+
if {![catch measure_clock clock] && $clock > 100000} {
37+
return $clock
38+
}
39+
40+
echo "WARNING: Host CPU clock unknown."
41+
echo "WARNING: Using the highest possible value $default kHz as a safe default."
42+
echo "WARNING: Expect JTAG/SWD clock significantly slower than requested."
43+
44+
return $default
45+
}
46+
47+
set compat [read_file /proc/device-tree/compatible]
48+
set clocks_per_timing_loop 4
49+
50+
if {[string match *bcm2711* $compat]} {
51+
set speed_offset 52
52+
} elseif {[string match *bcm2837* $compat] || [string match *bcm2710* $compat]} {
53+
set speed_offset 34
54+
} elseif {[string match *bcm2836* $compat] || [string match *bcm2709* $compat]} {
55+
set speed_offset 36
56+
} elseif {[string match *bcm2835* $compat] || [string match *bcm2708* $compat]} {
57+
set clocks_per_timing_loop 6
58+
set speed_offset 32
59+
} else {
60+
set speed_offset 32
61+
echo "WARNING: Unknown type of the host SoC. Expect JTAG/SWD clock slower than requested."
62+
}
63+
64+
set clock [get_max_cpu_clock 2000000]
65+
set speed_coeff [expr { $clock / $clocks_per_timing_loop }]
66+
67+
# Transition delay calculation: SPEED_COEFF/khz - SPEED_OFFSET
68+
# The coefficients depend on system clock and CPU frequency scaling.
69+
bcm2835gpio speed_coeffs $speed_coeff $speed_offset
70+
71+
transport select swd
72+
adapter gpio swclk -chip 0 27
73+
adapter gpio swdio -chip 0 22
74+

0 commit comments

Comments
 (0)