2022/10
qemu
qemu-architecture
qemu-system-architecture
hardware
boards
stm32
raspberrypi
(2022/10/1) QEMU simulation for hardware boards. After completing previous examples QEMU for Beginner and Advanced, moves on to next step to build image running on qemu
and on a real hardware board. We will start with STM32 board or Raspberry Pi
latest update on 2022/11/02
Table of Contents
So far, we tried those different images on qemu
, next thing to try is to use the images running on real hardware boards.
qemu-system-arm
on STM32Besides the official QEMU github supporting platforms of netduino2, netduinoplus2, stm32vldiscovery. There is one unofficial branch of QEMU supporting Olimex STM32_P103.
The QEMU for STM32 and Olimex STM32 P103 Development Kit Demos are maintained by Andre Beckus, a branch from official QEMU github v2.1.0. It was not merged with mainstream, with the reason below stated in beckus.github.io
The STM32 implementation is kept in the “stm32” branch (the “master” branch contains the unmodified QEMU code). The “stm32” branch may contain commits that make the software unstable or perhaps even uncompilable. Therefore, I recommend that you use the latest tagged release that looks like this: “stm32_v0.x.x”. These are considered to be relatively stable, and at least minimally tested.
It requires a re-compile/re-installation of STM32 version of qemu
for upcoming trials.
qemu-system-arm
and arm-linux-gdb
on STM-32Installation of arm-linux-gdb
stm32_p103_demos/makefile
also support gdb. So let's try it. (I was not familiar with gdb
. In the begining, I thought it works well with gdb
which was installed together with gcc
from sudo apt install build-essential
. But not.) It needs to install arm-linux-gdb
, so I follow this article arm-linux-gdb除錯工具的安裝與交叉編譯gdbserver for arm-linux-gdb
installation. That requires to download gdb
source code and compile with setting for ARM.
I am not sure if it is needed to instal gdbserver
. Still do it anyway.
Execution of qemu
with gdb
To work on gdb
, it requires to open 2 terminal
windows. Let's define the terminal for qemu
which qemu-system-arm
run on, and terminal for gdb
which arm-linux-gdb
runs on.
Set terminal for qemu
to the path of stm32_p103_demos
.
Try to understand makefile
the command input to run gdb
. (I learned the key word to grep
is QEMUDBG
because I peek the makefile
content already.) From cat makefile
, we can learn that we need to add _QEMUDBG
to run qemu
with gdb
function.
Let's try the same sample program adc_single
with gdb
feature by command make adc_single_QEMUDBG
under terminal for qemu
We can find in the script of qemu-system-arm -M stm32-p103 -gdb tcp::3333 -S -kernel demos/adc_single/main.bin
, it enables gdb with port 3333. And the program adc_single
do the intialization then halt. So we open the other terminal for gbd
to trace how the program executes.
Next step it to connect the gdb
to qemu
with command of target remote : 3333
, using the port we learned from qemu-system-arm
options.
Since we are just trying the gdb
function, not really doing the program debug. Let's run the adc_single
program by using c
or continue
command.
Then, we will find the terminal for qemu
starts running, and repeatly the cycle of LED Off
and LED On
. Unless we use CTRL-c
on theterminal for gdb
to break the execution.
openocd
and arm-linux-gdb
on hardware Olimex STM32_P103Previous examples in stm32_p103_demos
also support openocd
for programming the target hardware of stm32_p103 from Olimex. Luckily, I have two Olimex STM32-P103 boards and one ARM-USB-TINY-H JTAG debugging board on hand, so give it a try.
For this demo, we need to have one Olimex board, and one debugger adaptor, connected between Olimex board and PC. The debugger adpator could be JTAG
or serial wire debugging (SWD)
, with different interface to the host (PC). This article will not cover hardware related topics in this article. Please refer to Olimex STM-P103 development boardUser's manual for hardware related information. May have another article to describe the debugger board later.
About openocd
OpenOCD, the Open On-Chip Debugger has been created by Dominic Rath as part of a diploma thesis at the University of Applied Sciences, FH-Augsburg. OpenOCD is a free-software tool mainly used for on-chip debugging, in-system programming and boundary-scan testing. OpenOCD supports flashing and
debugging a wide variety of platforms such as:
ARMv5 through latest ARMv8
MIPS
AVR (incomplete)
Andes
RISC-V
The full list of supported CPU types can be accessed here: User's Guide.
Installation of openocd
Prerequisite 1: git config --global http.sslVerify false
to disable git certificate temporarily.
During ./configure --enable-maintainer-mode --enable-ftdi
, it will have below error configure: error: jimtcl not found, run git submodule init and git submodule update
and git submodule init && git submodule update
. After checking Stackoverflow article, we issue Linux command git config --global http.sslVerify false
to disable temporarily git certificate. After completion of installation, need to issue another Linux command git config --global http.sslVerify true
to enable the git certificate.
Prerequisite 2: Installation of libusb
and others
Installing openocd
with package installation (not recommended)
Below installation is try-and-error. Suggest to skip this step, and try next section of installation by source compiling.
First, install with the following Linux apt
command. But it produces error.
Tried to see if it can be corrected by adding user to plugdev
, but still failed.
Also, according to ARM-USB-TINY-H User Manual - 3.3.4 Driver installation in Linux, tried to create a file /etc/udev/rules.d/olimex-arm-usb-tiny-h.rules
, by putting this single line in the file:
The idVendor
and idProduct
shall use the setting from below table of the hardware debugger you use.
Table 2. Olimex OpenOCD debuggers, FTDI vendor and product IDs | ||||
---|---|---|---|---|
ARM-USB-TINY | ARM-USB-TINY-H | RM-USB-OCD | ARM-USB-OCD-H | |
VID (VENDOR ID) | 0×15BA | 0×15BA | 0×15BA | 0×15BA |
PID (PRODUCT ID) | 0×0004 | 0×002a | 0×0003 | 0×002b |
Eventually, still gave up. And decided to try with source compiling with different setting to enable ftdi
instead of default setting of ft2232
in sudo apt install openocd
.
Installing openocd
with compiling from source code (recommended)
First, remove the package installed from previous section, if you ever tried.
Then, follow the ARM-USB-TINY-H User Manual - 3.3.1 Getting OpenOC with the section describing the source code compilation.
So far, we will be able to launch openocd
. However, still more setting need to modify on stm32_p103_demos/makefile
. Within makefile
, we can find a line of default directory for olimex-arm-usb-tiny-h.cfg
Since we use different option for openocd
compilation of --enable-ftdi
. The file olimex-arm-usb-tiny-h.cfg
is under another directory, so to set it correctly by update that in stm32_p103_demos/makefile
.
Now, we complete openocd
installation.
Using openocd
to run stm32_p103_demos
The makefile
under stm32_p103_demos
support programming Olimex STM32-P103 board through ARM-USB-TINY-H debugger board. The usage is to add _PROG
after the demo program like example below.
We can see the LED on STM32-P103 board flashing now, as expected response of the demo program stm32_p103_demos/adc_single
does.
Using openocd
and arm-linux-gdb
to run stm32_p103_demos
After successfully programming (or usually called flash
) the Flash ROM of target hardware, try arm-linux-gdb
to debug the hardware via openocd
. We need to launch two separate terminal
screens, let me name it as terminal for openocd
and terminal for gdb
.
Then launch a new window of terminal for gdb
Screenshot - terminal for openocd
(left) and terminal for arm-linux-gdb
(right)
Screenshot - terminal for openocd
(left) and terminal for arm-linux-gdb with -tui option
(right)
openocd
and arm-linux-gdb
commandsSince we will send openocd
commands through arm-linux-gdb
, it is quite confusing to me with commands of the examples in the beginning. It was not clear to me which commands are gdb
commands, and which ones are openocd
commands. Try to summarize openocd
and gdb
documents, so to find the appropriate commands more easily.
gdb
related documents
gdb
official documents
gdb
popluar commands and usages
(gdb) target extended-remote localhost:3333
: connect to gdbserver. From previous example, when we launch openocd
, the system output Info : starting gdb server for stm32f1x.cpu on 3333
Info : Listening on port 3333 for gdb connections
, showing it starts a gdb server on port 3333. (since we use the same PC, so localhost
is used, instead of specific ip address. And localhost
can be omitted.)(gdb) disconnect
: to disconntect from gdb server.(gdb) monitor reset init
: Reset and intialize the target hardware.(gdb) run or r
: Run
the program continueously from the beginning. To stop, press CTRL-c
.(gdb) Continue or c
: Continue
the program continueously from where it stopped. To stop, press CTRL-c
.(gdb) Step or s
: Step
by step to execute the program per line instruction.gdb
is a magic command. You can use the monitor command to send special requests to gdbserver.
(gdb) monitor help
: This is a gdb
command to list the openocd
commands supported. More details in next section of openocd
.gdb
examples
openocd
Related Documents
openocd
official documents
gdb
for openocd
.openocd
popular commands and usage
gdb
is a magic command, which was introduced at previous gdb
section. Can use monitor + openocd
commands to control openocd
and target hardware in gdb
.
(gdb) monitor help
: This is a gdb
command to list the openocd
commands supported.(gdb) monitor reset {run, halt, init}
: Reset the hardware target, then take next action of run
(run the program), halt
(halt the CPU), init
(Initialize the target hardware system).(gdb) monitor soft_reset_halt
: Requesting target halt and executing a soft reset. This is often used when a target cannot be reset and halted. The target, after reset is released begins to execute code. OpenOCD attempts to stop the CPU and then sets the program counter back to the reset vector. Unfortunately the code that was executed may have left the hardware in an unknown state.(gdb) monitor flash help
to know more flash
command provided by target hardware about OpenOCD flash commands.
openocd
+ gdb
examples
arm-linux-gdb
commands to openocd
for programming Flash ROM of the target hardwareThe openocd
+ gdb
examples above, Stackoverflow - STM32 GDB/OpenOCD Commands and Initialization for Flash and Ram Debugging, provides a clear intruction on how to program the Flash ROM of the target STM32F107 board. However, need to modify some to use it on Olimex STM32-P103 board.
Below are the script from the article. First, launch openocd
Then, launch gdb
on another terminal
window.
However, there are couples of changes need modification due to different hardware used.
Modification 1: Identify the hardware JTAG tap and Flash ROM memory adress. It can be found by gdb
command monitor flash probe 0
. From the system output, we identify the JTAG tap is stm32f1x
and Flash ROM address at 0x08000000
. Write it down, and we will use it later.
Modfication 2: Identify the numbers of blocks of Flash ROM by issuing gdb
command monitor flash protect 0 0 127 off
. The sytem responds with ERROR: last block must be <= 31
. So we shall modify the command to (gdb) monitor flash protect 0 0 31 off
. It is strange to me though. Olimex STM32-P103 use STM32F103RB CPU with 128KB Flash ROM. According to the datasheet, it is 2KB per block. That shall be 64 blocks, instead of 32 indicated by JTAG.
Modification 3: Using the right JTAG tap from stm32x
to stm32f1x
as identified in previous command monitor flash probe 0
.
Modification 4: Adding the Flash ROM memory address in the command monitor flash write_image
, and use the absolute file path. The Flash ROM memory address was identified in previous command monitor flash probe 0
.
Good to go, after those modifications. Below are the result of the execution.
You can see Olimex STM32-P103 LED blinking now.
This example follows the Youtube - Embedded Linux from Scratch in 45 minutes, on RISC-V, and Presentation pdf.
Below is the script of the whole process. That’s possible to compile and assemble in less than 45 minutes!
Things to build
▶ Cross-compiling toolchain: Buildroot 2020.11.1
▶ Firmware / first stage bootloader: OpenSBI
▶ Bootloader: U-Boot 2021.01
▶ Kernel: Linux 5.11-rc3
▶ Root filesystem and application: BusyBox 1.33.0
Download and install Linux kernel, but kernel panic, as expected.
It runs, and fails. As expected, it mounts the root file system, but it fails in executing any process, because we haven't feeded the root file system yet.
gdb
for openocd
.gdb
for openocd
gdb
and openocd
examples
gdb
examples
gdb -tui
or cgdb
for Text UI or GUI operation../configure
should be removed.
Learn More →
Learn More →
Learn More →