Phillip Stanley-Marbell
Foundations of
Embedded Systems
Physical Constraints, Sensor Uncertainty, Error Propagation,
Low-Level C on RISC-V, and Open-Source FPGA Tools
Draft Version of Michaelmas 2020
Appendices
C
Tools for programming the ARM Cortex-M0+
In this appendix, names of files and commands to be typed into the com-
mand shell will be displayed in a shell command or file block . Hyperlinks to
resources outside the document will be shown in a colored font (example
link
), and references to specific function names will be shown in a fixed-
width font (e.g., malloc()). Most of the files needed for the steps below are
linked to directly from the text below.
C.1 Optional installation of compilation tools
You can compile the firmware for the FRDM board using the server for 4B25
coursework (cpu1.f-of-e.org), but you will need a PC with a USB port to
install the SEGGER JLINK tools which we will use to load the compiled
binaries to the FRDM board.
If you have access to a Linux or macOS system, you can choose to install all
the necessary tools for both the compilation process and loading process on
your PC. If you have a Windows PC, you can install VirtualBox or VMWare
and setup a Linux virtual machine.
C.1.1 VirtualBox
For VirtualBox, you are encouraged to install the Oracle VM VirtualBox Exten-
sion Pack to get USB 2.0 support, to be able to use USB 2.0 devices with the
VM. You will also need to go to the VirtualBox Settings (under menu item
Machine -> Settings -> Ports, and: enable the serial port, configuring it to
connect to the serial port on your host machine (more details for how to do
this below); (optional) enable USB and USB 2.0 EHCI.
If you are installing VirtualBox on macOS, you will need to set the serial
port Port Mode to “Host Device” and the Path/Address to the device file on ma-
cOS. You can find the latter by doing
ls /dev/cu
*
with the FRDM KL03 plugged
4 phillip stanley-marbell
in. The path you are looking for will be something like
/dev/cu.usbmodem1441 .
Note: This is different from the file /dev/tty.usbmodem144 which is the path you
would need in connecting to the serial port directly from macOS.
If you are installing VirtualBox on Windows, you will instead need to set
the appropriate COM port.
C.2 Installing the cross compiler
We will use a for the ARM architecture to compile C programs on a host
workstation (e.g., MacOS or GNU/Linux) and generate binaries to run on
the ARM Cortex M0 processor on the FRDM KL03. This is similar to the
cross compilation you saw in Coursework Item 1, where we cross compiled
for the Sunflower emulator.
The ARM cross compiler tools are already installed on cpu1.f-of-e.org.
If you wish, you can install the cross-compilation tools on your own PC. On
Ubuntu, you can install the ARM cross compiler using
sudo apt-get install gcc-arm-none-eabi .
On macOS, you can get a version of the ARM cross-compiler tools that are
known to work, from
here. You will also need to install cmake if you are using
a system where cmake is not already installed.
C.3 Building the Warp firmware (on coursework server or your PC)
First, fork the Warp firmware repository on GitHub. If you choose to do
your compilation on the coursework server, then you will need to clone your
fork to your account on cpu1.f-of-e.org. Otherwise, clone your fork to the
Unix-based system (e.g., Linux or macOS) where you will do the compilation.
Before compiling the Warp firmware, you will need to set the ARMGCC
_
DIR
environment variable to indicate where the cross compiler is. This step
is also described in the
README.md of the Warp firmware repository. On
cpu1.f-of-e.org, you will need to execute export ARMGCC
_
DIR=/usr from
the shell, to set the compiler location environment variable correctly, since
the compiler is in
/usr/bin/arm-none-eabi-gcc. To save yourself time doing
this each time you login, you can add that export statement to the .bashrc in
your home directory on cpu1.f-of-e.org. Once you have set the ARMGCC
_
DIR
environment variable, continue following the instructions in the
README.md
in the Warp firmware repository to build the firmware.
Once you have built the firmware, your final executables will be in the
directory
work/demos/Warp/armgcc/Warp/release/, relative to the location
where you executed the command
./build.sh. You will need to copy the
file work/demos/Warp/armgcc/Warp/release/Warp.srec to the computer to
which you will plug your FRDM KL03 board. If you are running the compi-
lation on the same PC as you will be using to load the firmware, then the last
foundations of embedded systems 5
line printed after the build completes gives you a command string that you
can execute to load the firmware.
C.4 Some tips for debugging
1. On some PCs, the FRDM board will show up as “FRDM-KL03J” after
correctly loading the SEGGER firmware. If you can start JLinkEXE without
it complaining about no JLINK present, you should be OK.
2. From the table in the KL03 Technical Datasheet, note that pins PTA0,
PTA1, and PTA2 are special. You can in principle still use them, but you
will have to fully understand how they are treated from the datasheet.
3. To reduce the choppiness in the RTTClient output, you can increase the
’-speed’ option’s value in the JLinkEXE command to the highest your PC
/ the JLinkEXE program will support.
It could also be that you’ve made changes that are causing a heap and
stack collision at runtime. To fix any such issues you might have introduced:
1. Decrease the size of the heap to 0 (if it is not already 0).
2. Increase the size of the stack to the largest you can get to compile (with
the heap at 0).
3. Remove unneeded printf/SEGGER_RTT_WriteString/SEGGER_RTT_printf
which you have added.
4. Use git diff to double check what local changes you have made relative to
the original supplied repository.
5. Double check that you get the unmodified repository working correctly
before you make any changes.
6. Double check that you are using the updated files mentioned in office
hours which will make the task simpler in certain cases.
7. Re-check your wiring.
C.5 Loading binaries to the FRDM board (on your PC)
The SEGGER firmware allows you to use SEGGER’s JLink software to load
your own firmware to the board, even without using their specialized JLink
programming cables. The SEGGER firmware runs on the second of the two
microcontrollers on the FRDM board, as described in Lecture 1.
6 phillip stanley-marbell
You can find the SEGGER firmware at the
SEGGER Page for OpenSDA
firmware. To load the SEGGER firmware, follow this sequence (or see the
instructions at the SEGGER page above):
1. Hold the reset button on the FRDM KL03 board (Figure
C.1), keeping it
held down for the next step below.
2. While still holding down the reset button, plug the FRDM KL03 board into
the host computer (Figure C.2) then release the reset button. The FRDM
KL03 board should show up as the volume named
BOOTLOADER .
3. Copy the file
JLink
_
OpenSDA
_
2015-10-13.sda (or the equivalent SDA firmware
you downloaded from
SEGGER) to the BOOTLOADER disk volume. The boot-
loader is now setup. You can now reset the FRDM board by disconnecting
and reconnecting the board. You only ever have to update the SEGGER
firmware the first time you use the board. Once it is installed, you will
use the SEGGER JLink tools to load your modified Warp firmware to the
board.
Figure C.1: To setup the
bootloader, first hold
the reset button, before
plugging in the power/usb
cable.
Figure C.2: After plugging
in the power/usb cable,
release the reset button.
When the SEGGER firmware is loaded, you will be able to use the JLink
software (also known as the JLink Commander) from SEGGER. This software
is available online from SEGGER. Once you have installed the JLink software
on your host machine, you can use it to program new binaries to the FRDM
board. For example, on macOS
1
, the JLink software will typically install
1
The process on
GNU/Linux is similar.
in
/Applications/SEGGER/JLink/ . To connect to the board on macOS, you should
then run /Applications/SEGGER/JLink/JLinkExe -device MKL03Z32XXX4 -if SWD -speed 100000
(the device name is indeed "MKL03Z32XXX4"; that is not a placeholder). This
will launch a JLink command console. To load a binary to the board, you
can then issue a command, e.g.,
loadfile <full path to s-record file> where
you should replace <full path to s-record file> with the full path of your
compiled s-record format binary. Because you might have to disconnect the
FRDM board occasionally, the JLink commander might lose communication
with the board. Since it is convenient to have the command history (accessed
with the up and down arrow keys), you can tell JLink to re-connect to the
board without having to restart it, using the command
usb within JLink.
You can group several JLinkExe commands into a file to be issued when
you launch it. The Warp firmware provides such a default JLink configura-
tion command file (
link) which performs a number of JLink commands:
exec EnableRemarks
unlock kinetis
loadfile <full-path-to-warp-firmware>/build/ksdk1.1/work/demos/Warp/armgcc/Warp/
release/Warp.srec
r
go
foundations of embedded systems 7
If placed in the file
jlink.commands, you can launch JLinkExe instructing
it to use those commands:
JLinkExe -device MKL03Z32XXX4 -if SWD -speed 4000 -CommanderScript
Unless you have reason to, use this set of programming commands to
load your S-record file. The Warp firmware repositor y’s README.md gives
additional details on how to connect to a system running the Warp firmware
using the SEGGER JLink tools.
Index
cross-compiler,
4