Setup Code::Blocks for STM8S Discovery

Unrestrained code generation for the STM8S Discovery board: Here's how to set up the free SDCC compiler and the Code::Blocks IDE (also free) under Windows.

I suggest reading the introduction STM8S Discovery – First contact with a cheapo first…

First of all we need the software. You will find Code::Blocks at codeblocks.org; The SDCC compiler ist located at sdcc.sourceforge.net. I'm using Win8, so I downloaded the 64 bit version sdcc-3.4.0-x64-setup.exe. Just do a full install with both applications.

Launch Code::Blocks. You will see a "Workspace" entry. It will very probably point to a directory called "CodeBlocks" in your personal "Documents" folder. This is where projects will be created, so remember that path.

Code::Blocks needs some adaptions in order to properly compile STM8 code. Open "Settings" > "Compiler" > "Global compiler settings". Choose "Small Device C Compiler" and add a new compiler flag for the "stm8" architecture setting:

CodeBlocks_Setup_01

CodeBlocks_Setup_02

Select the new CPU flag and the flag "Output Intel Hex" as default entry. You may also select some code optimization flags, but for the time being SDCC doesn't do much with them for STM8 code.

CodeBlocks_Setup_03

Switch to the tab "Toolchain executables" and choose the following settings:

CodeBlocks_Setup_04

Now go to "Other settings" > "Advanced options":

CodeBlocks_Setup_05

Adapt the options like this:

CodeBlocks_Setup_06

Finally save the compiler options dialogue.

Now let's create the project for our own STM8S105 static library.
Why a static library?
STM has created a "toolchain" called "Standard Peripheral Library". It consists of a number of header files (*.h) and C files (*.c). Most code examples make use of it by integrating the source code into each and every single project. This unnecessarily blows up the projects and makes them harder to understand. A static library can be compiled once and will be referenced only with the linker options of your projects. That's much smarter. Let's do it.

Create a new project with "File" > "New" > "Project…" > "Empty project". In the following popup, create a "Release" configuration for the static library and give it a recognizable name like "STM8S_lib".

CodeBlocks_Setup_07

The source code for this project is STM's "Standard Peripheral Library" (SPL). Go to st.com and use their page search function to find "STSW-STM8069". Download the zip file and walk into "STM8S_StdPeriph_Lib_V2.1.0\Libraries\STM8S_StdPeriph_Driver". Copy the directories "inc" and "src" into the project folder; It's located inside your workspace folder.

According to STM the SPL code is solely meant for evaluation. That should't be a problem for us. But they hardcoded the three available commercial compilers into the source code; In addition SDCC uses some slightly different syntax constructs. Fortunately only three need adaption; You can download them here:

STM8_SPL_adaptions.zip

Just replace the three files in "src" and "inc".
Now choose "Project" > "Add files recursively", choose the project folder and make sure all .h and .c files are selected. You should then end up with something like this just below the "Workspace" entry:

CodeBlocks_Setup_08

OK, the source is fixed and integrated. Now some hard stuff: Project and target setup. Choose "Project" > Settings and modify these settings:

CodeBlocks_Setup_09

Under "build targetfiles" the C files for the STM8S105 need to be checked (and only these files). The following features are valid and their C file must be checked:
adc1, awu, beep, clk, exti, flash, gpio, i2c, itc, iwdg, rst, spi, tim1, tim2, tim3, tim4, uart2, wwdg.

From within the settings window click "Build options…". Make sure the "stm8" flag is checked for the SDCC compiler.
Choose the target ("STM8S_lib") and open the sub-tab "#defines", then add "STM8S105".

CodeBlocks_Setup_11

Don't close this window yet. Instead of the target check the project itself (root node) and go to "Search directories". Add the "inc" folder (you can add it as relative path when asked):

CodeBlocks_Setup_12

That's it! You should now be able to compile the selected source files and build the static library. Choose "Build" > "Build" and watch the magic.
Sometimes the SDCC library archiver "sdar.exe" crashes. In this case just build again until it succeeds.
Now open your explorer, go to the project directory and look for the folder "lib". If the resulting .lib file is there we're done.

You may now continue with a first program.

Leave a Reply

Your email address will not be published.

Blue Captcha Image Refresh

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">