Sunday, 24 January 2016

STM8 project with CMake

Now you have SDCC compiler with STM8 support installed on your system. And it is time to compile your project. The obvious approach is to create Makefile but I decided to use cmake. And it took me some time to succeed. Now I share the results.

For cross-compiling with cmake I created stm8_toolchain.cmake file with next content:

SET(CMAKE_SYSTEM_NAME Generic)

SET(CMAKE_C_COMPILER sdcc)

SET(CMAKE_C_FLAGS_INIT "-mstm8")

SET(CMAKE_EXE_LINKER_FLAGS_INIT "")

SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

We will use it for all our STM8 projects. I put it in the ~/projects/stm8 folder.

Now create cmake project. Our project folder will be ~/projects/stm8test . We will put all files there.

First create CMakeLists.cmake file with next few lines

cmake_minimum_required(VERSION 2.6)

project(stm8test C)
 

add_executable(${CMAKE_PROJECT_NAME} main.c)


Then create main.c file with the code:

void main()
{
}

Then create build folder and call cmake with toolchain specified:

cd ~/projects/stm8test
mkdir build
cd build
cmake -DCMAKE_TOOLCHAIN_FILE=../../stm8/stm8_toolchain.cmake ..

Now you could compile it with ordinary "make" command.
You will see nice output:

Scanning dependencies of target stm8test
[100%] Building C object CMakeFiles/stm8test.dir/main.c.rel
Linking C executable stm8test.ihx
[100%] Built target stm8test

Now populate the project with more code.
And to flash it to the micro-controller use stm8flash utility from here .

Building latest SDCC under Ubuntu 14.04 LTS

It not takes much time to find that there is only one practically useful free Linux C compiler for STM8 micro-controller at the moment. And this is SDCC.

More and more people start to use it for STM8 development. As result more bugs fixed and more changes applied to it. This leads to fast increasing difference between current developed version and default version of SDCC in Ubuntu repository. Actually the default version of SDCC in Ubuntu 14.04 LTS is not supporting STM8 at all.

To benefit from more features and also to contribute to SDCC testing I recommend to build it from latest sources. Here I describe how to do it.

First of all you need to install some extra packages:

sudo apt-get install build-essential gputils subversion bison flex libboost-all-dev texinfo

Also to build latest SDCC from source the automake 1.15 required to be on your system. You could find it at https://packages.debian.org/sid/all/automake/download . Install it in the way like this:

cd ~/Downloads
wget http://ftp.uk.debian.org/debian/pool/main/a/automake-1.15/automake_1.15-3_all.deb
sudo dpkg -i automake_1.15-3_all.deb


Now get the latest SDCC source code and compile it:

mkdir ~/src
cd ~/src
svn checkout svn://svn.code.sf.net/p/sdcc/code/trunk/sdcc sdcc
cd sdcc/
mkdir build
cd build/
../configure
make


The compilation process will take some time.
When it completes you will be able to install SDCC to your system with next standard command:

sudo make install

If after some time you decide to recompile it with recent updates then do next:

cd ~/src/sdcc
svn update
cd build
make

And then install it as before with "sudo make install".