Beginning-STM32 c2
Beginning-STM32 c2
Beginning-STM32 c2
Software Setup
Before you can get underway with project-related work, you need some software
installed. There are a number of “moving parts” involved. Despite this, the process
should proceed smoothly. Once accomplished, it need not be revisited.
O
perating Software
I’m also going to assume you have a POSIX (Linux/Unix) environment from which
to run commands. The Linux or Raspberry Pi environments using the bash shell are
perhaps the most natural. Other good environments include FreeBSD and MacOS. From
FreeBSD, I’ll assume that you are using the bash shell.
If you’re using Windows and you haven’t installed it yet, you’ll want to install Cygwin
(https://www.cygwin.com). Some might use MSYS instead. After installing the base
Cygwin system, make sure that you also install make and git. This will give you a Linux-
like command-line environment from which to build software.
17
© Warren Gay 2018
W. Gay, Beginning STM32, https://doi.org/10.1007/978-1-4842-3624-6_2
Chapter 2 Software Setup
Mac users will need to install git at a minimum. You’ll also need GNU make,
especially if you use FreeBSD (Free Berkeley Software Distribution). Sometimes GNU
make is installed as gmake instead on a BSD (Berkely Software Distribution) system. If
you’re using Mac Homebrew (https://brew.sh), you can install these as follows:
B
ook Software
The directory structure for building with libopencm3 and FreeRTOS is available from
github.com. Choose a suitable place from which to create a subdirectory. This book will
assume home directory:
$ cd ~
The preceding command will create directory ~/stm32f103c8t6. However, feel free
to rename it to something easier to type, like ~/stm32.
l ibopencm3
Next, we must download the libopencm3 software into the correct place. First, change to
the subdirectory, and then issue the git clone command for libopencm3:
$ cd ~/stm32f103f8t6
$ git clone https://github.com/libopencm3/libopencm3.git
18
Chapter 2 Software Setup
F reeRTOS
The next important piece of software is FreeRTOS. Unfortunately, it must be downloaded
and unpacked as a zip file.
1. Go to http://www.freertos.org.
3. Click the link “2. Click to download the latest official release from
SourceForge.”
Depending on your browser and operating system, a zip file should be downloaded
automatically. It will have a version number in the file name. At the time of writing, the
downloaded file name is FreeRTOSv10.0.1.zip. Change to the ~/stm32f103c8t6/rtos
subdirectory before unpacking the zip file. On my Mac, the download directory is
~/Downloads. Substitute in the unzip command as required for your system:
$ cd ~/stm32f103c8t6/rtos
$ unzip ~/Downloads/FreeRTOSv10.0.1.zip
Once that completes, there should be several files and subdirectories under
~/stm32f103c8t62/rtos/FreeRTOSv10.0.1.
~
/stm32f103c8t6/rtos/Project.mk
Because the version number of FreeRTOS is included in the subdirectory name, there is
a potential change left. Edit the file Project.mk with your favorite editor (or nano) and
locate the following line near the top of the file:
FREERTOS ?= FreeRTOSv10.0.1
If your version of FreeRTOS is newer than this, like FreeRTOSv11.0.0, then edit it to
match your version and resave the file:
FREERTOS ?= FreeRTOSv11.0.0
This will allow the Project.mk make file to work correctly later when you want to
create a new RTOS project.
19
Chapter 2 Software Setup
6. Create a system directory /opt (if you do not already have one):
$ sudo -i
# mkdir /opt
# cd /opt
Use tar option “j” if the ending of the file is .bz2. Otherwise,
use “z” when the ending is .gz. If you don’t have the GNU tar
command installed on the Mac, then you can install it using
macports (www.macports.org) or Homebrew (https://brew.sh/).
20
Chapter 2 Software Setup
9. Once the tar file has been extracted, it may produce a large
directory name like gcc-arm-none-eabi-6-2017-q2-update. Now
is a good time to shorten that:
# mv gcc-arm-none-eabi-6-2017-q2-update gcc-arm
10. Now, exit root and return to your developer session. In that
session, add the compiler’s bin directory to your PATH:
$ export PATH="/opt/gcc-arm/bin:$PATH"
11. At this point, you should be able to test your cross compiler:
$ arm-none-eabi-gcc --version
arm-none-eabi-gcc (GNU Tools for ARM Embedded Processors
6-2017-q2-update) 6.3.1 20170620 (release) [ARM/embedded-
6-branch revision 249437]
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying
conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
If the compiler doesn’t start and instead gives you a message like this:
$ arm-none-eabi-gcc --version
-bash: arm-none-eabi-gcc: command not found
then your PATH variable is either not set up properly or not exported, or the installed
tools are using a different prefix. Perform the following if necessary (the output has been
abbreviated slightly here):
$ ls -l /opt/gcc-arm/bin
total 75128
-rwxr-xr-x@ 1 root wheel 1016776 21 Jun 16:11 arm-none-eabi-addr2line
-rwxr-xr-x@ 2 root wheel 1055248 21 Jun 16:11 arm-none-eabi-ar
-rwxr-xr-x@ 2 root wheel 1749280 21 Jun 16:11 arm-none-eabi-as
21
Chapter 2 Software Setup
If you obtained your cross compiler from a different source than the one indicated,
you might not have the prefix names. If you see the file name gcc instead of arm-none-
eabi-gcc, you’ll need to invoke it as simply gcc. But be careful in this case, because your
cross compiler may get confused with your platform compiler. The prefix arm-none-
eabi- prevents this. When you go to use your cross platform gcc, check that the correct
compiler is being used with the type command:
$ type gcc
arm-none-eabi-gcc is hashed (/opt/gcc-arm/bin/gcc)
If your bash shell is locating gcc from a different directory than the one you installed,
then your PATH is not set correctly.
If you must change the toolchain prefix, then the top-level ~/stm32f103c8t6/
Makefile.incl should be edited:
$ cd ~/stm32f103c8t6
$ nano Makefile.incl
PREFIX ?= arm-none-eabi
In a normal situation where the cross-platform prefix is used, you should also be able
to make this confirmation:
$ type arm-none-eabi-gcc
arm-none-eabi-gcc is hashed (/opt/gcc-arm/bin/arm-none-eabi-gcc)
22
Chapter 2 Software Setup
This confirms that the compiler is being run from the installed /opt/gcc-arm directory.
Note The PATH variable will need modification for each new terminal session
to use the cross-compiler toolchain. For convenience, you may want to create a
script, modify your ~/.bashrc file, or create a shell alias command to do this.
$ cd ~/stm32f103c8t6
$ make
ST-Link Tool
There is one final piece of software that may need installation. If you’ve not already
installed it using your system’s package manager, you’ll need to install it now. Even if you
have it installed already, it may be outdated. Let’s test it to see:
$ st-flash
If you don’t see the option --flash=<fsize> mentioned, then you may want to
download the latest from github and build it from source code. This is only necessary if
you want to use more than 64K of flash memory. None of the demos in this book go over
that limit.
People have reported that many of the STM32F103C8T6 units support 128K of flash
memory, even though the device reports that it only has 64K. The following command
probes a unit that I own, from eBay, for example:
$ st-info --probe
Found 1 stlink programmers
serial: 493f6f06483f53564554133f
openocd: "\x49\x3f\x6f\x06\x48\x3f\x53\x56\x45\x54\x13\x3f"
flash: 65536 (pagesize: 1024)
sram: 20480
chipid: 0x0410
descr: F1 Medium-density device
The information reported indicates that the device only supports 65536 bytes (64K)
of flash. Yet, I know that I can flash up to 128K and use it (all of mine support 128K).
It has been suggested that both the F103C8 devices and the F103B8 devices use the
same silicon die. I’ll cover using the ST-Link V2 programmer on your device in the next
chapter.
If you don’t have these utilities installed, do so now using apt-get, brew, yum, or
whatever your package manager is. Failing a package install, you can download the latest
source code from github here:
24
Chapter 2 Software Setup
$ cd ~
$ git clone https://github.com/texane/stlink.git
$ cd ./stlink
$ make
$ cd build/Release
$ sudo make install
If you run into trouble with this, see the following online resources:
• https://github.com/texane/stlink/blob/master/doc/compiling.md
S
ummary
With the software installs out of the way, we can finally approach the hardware and do
something with it. In the next chapter, we’ll look at your power options and then apply
the ST-Link V2 programmer to probe your device.
25