Fun and games with the STM8 on Linux

After my adventures with the STM32 I decided to give the STM8 a go. The STM8 microcontroller is the 8 bit version from the same manufacturer, similar performance to an Arduino but with much lower power and incredibly cheap.

I purchased an STM8S Discovery board for about $10 from element14. It’s an interesting little board, comes with a snap off programmer (also an STM8S), touch sensor, a prototype area (through-hole and SMD). It uses the older STLink programmer – not the STLink2 that the STM32 uses. I read that some people just buy this kit for the programmer, snap it off and throw the main board away!

I could see straight away that the open source toolchain is not as rich as the STM32 – and much of the code and tools are still proprietary. I think this is purely a historical thing – the maturity of the embedded Linux tools happened quite late in the life of this processor, and in the meantime many people have moved to 32bit architectures.  I couldn’t work out if there was support for this programmer in openocd, the documentation was sketchy, so instead I elected to go for the simpler stm8flash tool. To install just do the following:

git clone https://github.com/vdudouyt/stm8flash.git
cd stm8flash
sudo make install

One thing you will find with the STM8 is that Linux tries to auto-mount it as a file system – there is some documentation on this little file system, but this can apparently cause some issues with accessing the UART later on. You can configure Linux to ignore this device when auto-mounting via a variety of methods. I’ll post details when I can remember which way I did it! For basic programming though, it doens’t seem to cause a problem, you can leave it mounted.

Ok. Connectivity is done, the next step was to try and compile some code for the chip. There’s plenty of code on the ST website but it’s all for proprietary compilers and the lack of any sort of Makefile made me want to cry!

The open source compiler of choice for this level of microcontrollers is the Small Device C Compiler (SDCC) which looks great, plenty of contributions and active use. There is a version of SDCC in the Ubuntu repositories but I’m still on Ubuntu 13.10 and the version I had didn’t support the stm8 so I downloaded the latest snapshot and installed manually (there is sadly no ppa).

To install manually, download from http://sourceforge.net/projects/sdcc/files/sdcc-linux-x86/3.4.0 then just do something like this:

tar jxjf sdcc*.bz2
cd sdcc-3.4.0/
sudo cp -r * /usr/local

Then, check it’s installed correctly with

sdcc -v

If you are lucky, you might have a good version in your repository so in that case you can just do sudo apt-get install sdcc.

SDDC seems to have some good support in many IDEs (there is a good example using Code::Blocks if that’s your kind of thing) but I just wanted a simple Makefile at this point, so I went off searching and found some example code on github by the same guy that wrote the flash tool. Thanks Valentin!

Sadly this was written for the STM8L (low power version of the same microcontroller). I spent a fair while trying to convert this to STM8S using a mixture of this and the code from the ST site, but in the end just threw my hands in the air, bit the bullet and bought an STM8L discovery board for a whole $4 more! (serves me right for being such a cheapskate in the first place). The STM8L is likely to be the chip we eventually select so I’m not sure why I didn’t just do this in the first place… oh well lesson learned!

Cut to a few days later – and I finally have an STM8L in my hands. It’s a nice discovery board and has the added benefit of an LCD Screen. Out of the box when you plug it in, it runs a nice little power utilization app, displaying results on the screen.

You should now be able to compile the SDCC example code and flash to the board. There was a slight bug in the Makefile (it references a non-existent source file and the flash target didn’t work) so feel free to use my forked version.

Once you’ve built and installed stm8flash and cloned and built the example from this repo, using a simple “make” you can flash the blinky binary to the board and you should see a nice fast flashing LED (and the LCD will now be blank).

Have a play with the value in the for loop of blinky.c to change the blink – what happens when you increase it too much? I’ll leave that up to you to play with.

There are many other examples from the ST website and elsewhere that you can probably bring across to the project – if you make any good ones – please let me know and I’ll add them to the repository.

References:

  • http://blog.podstuff.de/stm8s-discovery-first-contact-with-a-cheapo
  • http://blog.podstuff.de/setup-codeblocks-for-stm8s-discovery
  • http://sdcc.sourceforge.net
  • https://github.com/vdudouyt/stm8flash