Thursday, October 25, 2012

STM32 LCD touch screen demo

[Update: Torrent available for downloading the Virtualbox image ]

You see many hobby projects use the good old 16x2 LCD.

Pretty easy to use. But limiting. No graphics. Limited text. Hard to have a very complicated UI. You need some other device for input - push buttons usually. Unintuitive.

In a word: ugly.

Most consumer devices these days include a graphical LCD and touch screen interface. People understand how to use them. People expect them.

So I started to get away from 16x2 LCDs in my projects.

Fortunately 320x240 LCDs with touch screen and micro controller can be had cheaply on ebay. So I have been using them for my projects.

I have a some friends who have started using these boards or expressed interest in using them. Now they are ARM based and can be tougher to bring up that an Arduino. So I have decided to make a downloadable VirtualBox image that has the toolchain and environment setup, with a demo project, that includes a simple menu system. This should help people get started with these boards.

Which boards?

ebay auctions expire all the time, so it's hard to link to specific boards. Instead take a look at a search:

Look for the ones that are clearly 2 boards - the LCD board on top. CPU is usually a STM32F103.

The first sort of board has a 2.4" LCD:

The second has a 3.2" LCD:

Both LCDs are the same resolution, but I prefer the 3.2", it's substantially bigger (and so buttons will be bigger for fingers to hit).

However the board with the smaller LCDs have other goodies, like 16MB flash, speaker and RS232.

You can take a look at the schematics for each:

Either way, LCD + touch screen + STM32 CPU for less than some Arduinos cost.

STM32 CPUs are significantly more powerful than the trusty old Atmels (Arduinos).
 Your code may have a main task to accomplish, and then the LCD+touch UI to look after. Programming to bare metal, this can be tricky to pull off. Why not use a multi-tasking OS to help?

FreeRTOS to the rescue. Go read the 5 or so pages here for a good overview of how it works:

So then how to get started?

I have put together a VirtualBox image you can download that contains:

  • Ubuntu desktop
  • ARM toolchain (gcc + newlib)
  • Eclipse C++ IDE
  • - for loading code into the board
  • openocd - for loading code and debugging
  • Example FreeRTOS project with all source code, include LCD+touch drivers and a simple menuing system to get you started quickly


Getting started

Download the virtual box image:

(~800MB download, ~3GB disk space uncompressed)

Login/password: ubuntu/reverse

Install virtualbox if you don't already have it:

You will need 7-zip to uncompress the image:

Create a new virtual machine in the Virtual box manager. Give it a gig or ram or so. Select "Use existing hard disk", and choose the ubuntu image. Boot it up.

Building and running the demo

Demo menu code walk through

Loading code

The ARM Cortex M3 chips have a built in bootloader that allows you to load code over the serial interface.

You need to boot the device with the boot0 pin grounded. Most dev boards have a switch or jumper.

This can become pretty tedious so it's worthwhile to get a JTAG programmer - you can then load your code faster, without touching the board. You can also run openocd and gdb to help debug your code.

I use and recommend a bus blaster:

OpenOCD config files are included in the JTAG directory in the Virtalbox Image.

You will also probably need a 20 pin JTAG cable.

printf debugging

gdb & JTAG debugging

Boot, FreeRTOS and the LCD - how it comes together