Setting up the Pi software Mar 2017

To turn the Raspberry Pi into a general-purpose uploader / debugger for ARM STM32 chips, we need to set up some software.

First of all - the OS. DietPi is a very practical little distribution these days. It’s minimal, well-supported, offers a simple way to manage lots of popular applications and to configure or update the system itself. All console driven, through simple old-fashioned text menus.

Underneath this all sits a standard Debian 8.0 system, with apt-get and all that jazz.

Most conveniently, DietPi is available for a very wide range of RasPi-like boards:

So the first step is to dowload the proper image and put it on an SD card. For boards which do not have on-board wired LAN, it’s easy to get WiFi started by editing /boot/dietpi.cfg before ejecting the SD card from the host machine (that boot partion is VFAT and can also be mounted in Windows and MacOS).

If all is well, you can login on the box over the network, using ssh (or Putty for Win-users):

At this point, you’ll be taken to the DietPi “Launcher” (it can also be started manually later):

No need to install extra packages in this first pass through DietPi’s setup. The only confusing part is that even when not installing anything else, you’ll need to go through that last “Install” step in the menu to complete the setup process, and only then should you exit the Launcher.

The entire setup process is well-documented, see DietPi’s Getting Started page. Once you’re up and running, and logged in as root on the RasPi, you will get a greeting similar to this:

 DietPi     | 13:30 | Mon 06/03/17
 V145       | RPi A (armv6l)
 IP Address |

 Created by : Daniel Knight
 Web        :
 Twitter    :
 Donate     :
 DietPi's web hosting is powered by:

 dietpi-launcher  = All the DietPi programs in one place.
 dietpi-config    = Feature rich configuration tool for your device.
 dietpi-software  = Select optimized software for installation.
 htop             = Resource monitor.
 cpu              = Shows CPU information and stats.

From here on, we’ll always use the root account to avoid problems with permissions on GPIO pins, etc. It’s a dedicated board, so it’s no big deal. Just pick a good password and SSH keys.

First, we need to start up dietpi-config and adjust a few settings:

Disabling the serial console does not seem to work, but this extra command will do the trick:

systemctl mask serial-getty@ttyAMA0.service

Time to bring everything up to date and install a few more packages:

apt-get update && apt-get upgrade
apt-get install aptitude i2c-tools stm32flash picocom vim

Now clean up a bit, reboot to start from a fresh power-up state - then log back in again:

apt-get clean

With these changes, the RasPi will start up with its serial console free for our own use (no getty running, no login prompt), and we can start using this for connecting µC boards - we now have serial, I2C, and SPI at our disposal on the RasPi header.

We can use PicoCom for example:

picocom -b 115200 -imod lfcrlf /dev/ttyAMA0

But we can also download Folie, unpack and move it to /usr/local/bin/, and use that:

folie -r -p /dev/ttyAMA0

If the DTR and RTS pins on the FTDI header have been wired up, we can use stm32flash (installed earlier) to verify their proper operation - here with a JeeNode Zero inserted:

# stm32flash -i 23,-18,18:-23,-18,18 /dev/ttyAMA0
stm32flash 0.4

Interface serial_posix: 57600 8E1
Version      : 0x31
Option 1     : 0x00
Option 2     : 0x00
Device ID    : 0x0417 (L05xxx/06xxx)
- RAM        : 8KiB  (4096b reserved by bootloader)
- Flash      : 64KiB (sector size: 32x128)
- Option RAM : 16b
- System RAM : 4KiB

Next, we’ll install OpenOCD. It’s particularly useful on RasPi, because it can toggle GPIO pins to create a JTAG/SWD programmer and debugger. It’s highly configurable and supports gdb.

The default OpenOCD package in Debian is version 0.8, but 0.10 is a better choice for SWD on RasPi - so we’ll build it ourselves from source. There’s an excellent description of all the steps involved. In summary, we need to enter the following commands - this will take a while:

apt-get install git autoconf libtool make pkg-config libusb-1.0-0-dev telnet
mkdir -p ~/src; cd ~/src
git clone git:// openocd
cd openocd && ./bootstrap
./configure --enable-maintainer-mode --enable-bcm2835gpio --enable-sysfsgpio
make && make install   # use "make -j4" on RasPi ≥ 2 to speed things up

Once OpenOCD is installed, we can set up some scripts and configuration files to upload a hex firmware image easily to an attached microcontroller. Note that this is not limited to the STM32L052 of the JeeNode Zero or the STM32F103 µC series - OpenOCD is considerably more advanced and generalised than that.

Below are a number of scripts which you can add to the home directory, i.e. /root/ to create a basic upload structure. The hex firmware images are expected to be in folders named images-l0/ and images-f1x/, and the 2nd argument to and is the image name.

Assuming there’s a file called $HOME/images-l0/jz4.hex, this will upload it via SWD:

./ l0 jz4

And this variant can be used to upload over serial with DTR+RTS pin toggling:

./ l0 jz4

Best of all, if you have set up SSH access to your RasPi box, then all of this can be done without even logging in, using commands such as:

ssh myraspi ./ l0 jz4
ssh myraspi ./ l0 jz4

Below are the scripts which make this possible, shown inline but provided as a Gist by GitHub:

There you go - hook it all up, install the above software, and you’ll be ready for any µC task!

Weblog © Jean-Claude Wippler. Generated by Hugo.