Starting STM8 Microcontrollers

STM8S003K3 Discovery

Hardware Tools

The list of hardware tools needed is not very long.  We will obviously need a STM8 board and I prefer a Discovery board over other boards since it comes with a built-in ST-Link programmer/debugger hardware. If you have some other board like the ones I already showed, you will need a ST-Link programmer. I recommend an additional ST-Link programmer apart from the one available on board.

ST-LINK

ST-Link programmers/debuggers communicate with target STM8 micros via SWIM interface. This interface is the standard for all STM8 micros. Basically, it is a four-wire interface with two wire (VDD and GND) being used for powering the target. The rest two are reset I/O and SWIM I/O. In the official ST-Link V2 programmer unlike other ST-Link programmers, there is a dedicated port for SWIM interface with STM8 inscribed near it. Cheap USB flash drive-sized ST-Links are also available in the market and they are portable and as good as the official ones.

SWIM Interface

Apart from these we will also require some basic electronic lab stuffs like a USB-to-serial converter, connecting/jumper wires, LEDs, buttons, various types of sensors, etc. that are typically found in a common Arduino starter kit.

Arduino Starter Kit

It is yet better if you have either a logic analyser or oscilloscope. A good multimeter and a well-regulated DC power supply/source are must haves. You can also use a cell phone charging power bank as a power source since Disco boards have USB ports.

Saleae Logic 4

Software Tools

Just like any other software developer, my choice of language for software development is C language. I don’t want to spend time coding complex stuffs in assembly language. Apart from that I chose C language for the fact that STMicroelectronics has provided a Standard Peripheral Library (SPL) that is very easy to use. With SPL, it becomes totally unnecessary to program each register with meaningless numbers and maintain coding sequence. We will never need to access registers for any reasons as everything is done under the hood of SPL. All sequences are deal inside the SPL. All that we will ever need is the clear concept of each hardware block, their working principles, their capabilities and limitations.

We will need an Integrated Development Environment (IDE) and a C-language toolchain. The best stuffs you can get your hands on at zero costs are ST Visual Develop (STVD) IDE and Cosmic C compiler. Both are free but a rather difficult to use at first. STVD also comes with a programmer software tool called ST Visual Programmer (STVP). We’ll need STVP to upload codes to target STM8 micros.

Cosmic used to be a paid tool just like your PC’s antivirus software but at the time of writing this article, the Cosmic team has made it absolutely free for STM8 family. However, to use it you will need to register and acquire a license key via email. Usually this procedure of acquiring license and registration is maintained automatically by the software company’s server but with Cosmic it is different story. You will need to wait for some guy at Cosmic end to respond to your license request. It may take a few minutes or even a day but still the best part is getting a full version compiler for nothing.

STVD

You can get

STVD from here: http://www.st.com/en/development-tools/stvd-stm8.html and

Cosmic C compiler from here: http://www.cosmic-software.com/download.php.

You need to register in order to download both software. For Cosmic you will also need to acquire a free license for it work. So just fill in some basic info about you.

COSMIC 1

Firstly, we will need to install STVD. Installation procedure is simple and same as typical software installation. Just click next, next and next. After that we will need to install Cosmic C compiler. Again, just next, next and next until the screen as shown below.

COSMIC 2

After installation, you’ll prompted for licence. You must register your license unless you have already registered. If you have already registered, then you’ll be asked if to overwrite registration. You should skip reregistering.

COSMIC 2b

For the first run, you’ll get the following screen looking for a valid license.

COSMIC 3

You must fill all the starred (*) points to complete the process of registration. Select “Write to File” option and save the file as a text (.txt) file. The file name should be “CM8_license.txt”. Send this file to stm8_Free@cosmic.fr with subject “STM8FSE, STM32 32K License Request”. Now you’ll need to wait for the Cosmic team to respond to you. They’ll send you an email back with an electronic key license. The file will have a name like “license.lic” and the email will have some instructions.

This was my emailed license.

Email

Once you get the license, you’ll need to show the software its location and complete the licensing process as shown below. Save the license file in a secured location.

COSMIC 4

COSMIC 5

COSMIC 6

At the end of this process, we can enjoy the compiler without any limitations.

I also recommend that you download Sublime Text (https://www.sublimetext.com/) or Notepad++ (https://notepad-plus-plus.org/) for viewing your code with ease. These are very cool software. This is not mandatory though.

Code

Continue Reading ...

Related Posts

32 comments

  • Hi there, i solve the previous ADC problem. Anyway, u make some good tutorial on stm8 chips. Nice work

    • What was causing that issue? How did you solve it?

      • because i put all these

        ADC_DeInit(ADC1);
        ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_4Cycles);
        ADC_SchmittTriggerConfig(ADC1, ADC_Channel_7, DISABLE);
        ADC_ChannelCmd(ADC1, ADC_Channel_7, ENABLE);
        ADC_Init(ADC1, ADC_ConversionMode_Continuous, ADC_Resolution_12Bit, ADC_Prescaler_1);
        ADC_Cmd(ADC1, ENABLE);
        ADC_ExternalTrigConfig(ADC1, ADC_ExtEventSelection_None, ADC_ExtTRGSensitivity_All);

        in ‘while(1)’. It keeps looping.

        Once i put it before ‘while (1)’, it becomes normal.

  • Hi there, thanks for sharing. I learn alot thru your example, but I have encounter a problem with adc. Im currently using stm8l151k4t6. I wanna use the pin D7 as to read the voltage from my battery and monitor it (display on LCD). Can u please check where it goes wrong on my code?

    ADC_DeInit(ADC1);
    ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_4Cycles);
    ADC_SchmittTriggerConfig(ADC1, ADC_Channel_7, DISABLE);
    ADC_ChannelCmd(ADC1, ADC_Channel_7, ENABLE);
    ADC_Init(ADC1, ADC_ConversionMode_Continuous, ADC_Resolution_12Bit, ADC_Prescaler_1);
    ADC_Cmd(ADC1, ENABLE);
    ADC_ExternalTrigConfig(ADC1, ADC_ExtEventSelection_None, ADC_ExtTRGSensitivity_All);
    ADC_SoftwareStartConv(ADC1);
    while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
    battery = ADC_GetConversionValue(ADC1);

  • hi shawon great tutorials it would be nice too if you covered the coide compiler for arm it’s a good free compiler

  • hi shawon thanks for your response and support it turns out that my problem was a license issue I stupidly thought i use one license on multiple pc’s I used the license for a particular computer for mine that’s why I’ve been having compilation issues you have to use the license file stm8 sends you that is generated by your own pc else it won’t compile it’ll jus kip throwing you errors

  • I haven’t compiled any of the examples yet I just tried to compile the main source file just for test purpose n it just keeps showing me the same errors

  • yes the comic compiler was installed properly i even received a license file from my mail with the instructions to copy the license file to the license folder in the install directory if you fail to do this it will keep popping up for you to put in the license file. iincluded all the source files and header files as instructed i am working with the cheap stm8s103f i uncommented the stm8s103f in the stm8.h header file as you instructed and just to compile the main file it just throws me errors

  • no I just followed your instructions I just compiled it after setup i did not write any code yet just compiled the default main file

    • Need more details….

      1. The version of STVD and Cosmic
      2. Which example is giving this issue?
      3. The chip you are using if it other than STM8S003
      4. Have you tried to compile something else other than my examples?
      5. Are the paths to libraries and other folders properly added?
      6. Is the Cosmic compiler registered properly?

  • it throws this line The command: “cxstm8 -iinc +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\FSE_Compilers\Hstm8″ -clDebug\ -coDebug\ main.c ” has failed, the returned value is: 1
    exit code=1.

  • hi I’m having compilation issues when I compile it just throws me error about comic compiler located in the program file ialso excluded unwanted header files but my problems still weren’t solved

  • hi thanks for the tutorials but i am having compiler error issues even when I remove unnecessary header files it say comic compiler error

  • I am new to STM8S003k3 Discovery board.I have done UART communication using STM standard peripheral library example. Now I want to define any other GPIO pin for UART communication. Is it possible to use d0 and d1 pin for the UART communication? If yes, then please let me know how?

  • thank you very much for this article!

  • Thank you for your time and effort.

  • Good job bro

  • Thank you for this exceptionally comprehensive article, much appreciated

    Rando!

  • It seems that you didn’t follow my instructions completely…. I found some issues:

    1. You have included header and source files of ADC2, CAN, UART2, etc which are unavailable in STM8S103…. Add only files for the peripherals available in your target MCU…. Exclude the rest….

    2. You have not included stm8s_delay header and source files…. Either disable it from stm8s_conf.h header file if you are not going to use it or add both the header and the source files for it in your project….

    Resolve these and you are good to go….

    Thanks….

Leave a Reply

Your email address will not be published. Required fields are marked *