Tuesday, January 2, 2018

Programming STM8S using SDCC and GNU make

Programming STM8S using SDCC and GNU make


Recently I started looking at the STM8 microcontroller from  STMicroelectronics. I have used various microprocessors during the years, from the Z80 around 1990, through Hitachi H8, some TI 320C31~ 320C6711 DSPs, and more recently ARM F1~F4 series. The STM8S sounds like a downgrade, especially since during the time of writing, there is not a great price difference between, say 32 bit ARM F0 and S8, for small scale production or hobby work. 

However, STM8S is an interesting processor. It is simple enough that a newcomer can understand the complete inner workings, while it is a modern processor with great peripherals, especially since the architecture is suitable to be programmed in 'C'. Boards are dirt cheap, I have bought this, with a STM8S103F3P6 board for around $2:

I had also bought a ST-Link V2 programmer clone from China; mine is from DealExtreme.com:
Cheap ST_Link V2 ans SWIM programmer
Cheap and cheerful ST-Link V2 clone!

ST libraries available, but...

ST provides a very nice (and free) standard peripheral 'C' library for initializing and using the on chip peripherals of this processor (V2.2.0 at the time of writing). Using the library, you can very quicky setup and use all of the peripherals of this chip. They have even included some examples for various configurations, making this an even better processor for beginners.

However, there is a catch. The library is only intended to be used wirh proprietary 'C' compilers. That means, you will need to fork out the cash and buy the compiler, or live with code limitations the companies impose with the "free" versions of the compilers. Also, you will need to download quite large installers to start using these toolchains. Perhaps more importantly, you need to register to these companies to be able to download. Which I don't like.

Don't get me wrong, proprietary compiler suites are good for professional work where you need to have a someone to ask questions and expect a minimum performance guarantee with large code spaces and long projects, but probably an overkill for hobby work.

SDCC to the rescue

Small Device C Compiler (SDCC) is a 'C' compiler suite with the GPL licence, supporting many 8 bit microcontrollers. It has recently started support for the S8 processor from V8.4.0V3.4.0. I am using V8.5.0V3.5.0, on Ubuntu Linux 16.04. The basic compiler is provided as well as many components even including a simulator (check the licence of each component, not all are GPL). SDCC can be installed easily in Linux by the command:
$ sudo apt-get install sdcc

Of course GNU make can be used to automate building your code. I like to use emacs as a text editor. With a proper Makefile, it is as quick as using a GUI to develop and debug  your systems. The good side is, nothing is hidden from view in drop down lists in the GUI; they are all in plain view when you edit the Makefile. Also the development environment is very light as it is only a text editor.  BTW, I recommend using Meld to compare files, it is visual, light and very intuitive to use.

So all we need to do is to write a Makefile that will allow us to build S8 code using STM standard peripheral libraries.

All code is in Github

I prepared a sample code that will compile a multi-file 'C' project using SDCC for STM8S processors , which you can download from my Github account. One header file of the STM library must be slightly modified for the library to compile under SDCC (it involves modifying the syntax for inserting inline assembly instructions. This is clearly mentioned in the Github pages.

Please try it out, and let me know if I can improve it!