Starting STM8 Microcontrollers
Preparing the Software Tools
Firstly, we need three major documents before starting to program STM8 micros. These are as follows:
- STM8 Reference Manual.
- Datasheet of the MCU (STM8S003) that we’ll be using.
- STM8SVLDiscovery Board User Manual.
These docs will be needed everywhere during learning session. The reference manual states the use and purpose of all the hardware blocks in details. It includes register descriptions, naming conventions, modes of operation of all hardware, etc. However, it does not specify the specifications of a given STM8 micro and that’s because it is a generalized literature for all STM8S and STM8AF micros. As we know even within a family of micros, one MCU differs from another in many aspects. Most commonly this variation comes in the form of memory capacities and I/O pin counts. Sometimes electrical specs also vary and so to know the limits and general specs of our target MCUs we need to checkout their respective datasheets. Lastly the Discovery board user manual is most useful for the hardware schematics, pin assignments and layouts. If you are using some other board then I suggest that you acquire at least its schematic.
Now with Cosmic, STVD and STVP installed our software tool setup is almost ready. There are two approaches to STM8 programming. The first uses the traditional concepts of register-access-based coding, meaning you’ll have to set up every register on your own. The second way utilizes a specialized method of coding by using standard libraries developed by STM that are both universal and platform independent, meaning your C code will be same for any compiler using these libraries. These libraries are called Standard Peripheral Libraries (SPL). With these libraries, no one will ever need to get down to register-level access. The libraries are so coded that a coder will only have to know his/her chips’ hardware specs and some basics of these hardware. On the coding part, he/she will only have to set properties and desired values. The SPL manages the rest. For instance, when setting up a UART, we will only need to set interrupts, IOs and UART properties like baud rate, parity, etc. All of these setups are done with meaningful numbers and texts.
The STMicroelectronics Standard Peripheral Libraries (SPL) for STM8 microcontrollers can be found here: http://www.st.com/en/embedded-software/stsw-stm8069.html.
I wrote this article using SPL since it will be ridiculous to code STM8s using the old-fashioned way of configuring registers one-by-one manually. Thus, it is a mandatory download item. You should preserve and retain the downloaded SPL zip file fully intact. You may need it when things get messy.
Now make two folders and name them “inc” and “src”. The “inc” folder will be filled with all the header files (“.h” extension files) from the extracted zip file. Similarly, the “src” folder will be holding the source files (“.c” extension files). For ease of work, it is better to keep these folders secured just like the SPL zip file because every time when we will be making new projects the files in these folders will be needed. You can copy these files to your project folder or you can keep it centrally somewhere. I prefer the former method as doing so will not have any conflicting issue with other projects needing modifications. However, it will cost hard-drive space. This method is however less confusing and trouble-free for beginners. Extract all the files as shown below:
Note that there are more header files than source files. This is because there are two extra header files – stm8s.h and stm8s_conf.h that define processor type and processor properties. To make things work, we will have to comment one line of the stm8s_conf.h. You will find a line at the bottom of this file written as:
#define USE_FULL_ASSERT (1)
You need to comment or disable this line, otherwise the compiler will throw tons of error messages. Always check this at the start of a project. Surely, we don’t want to assert our code.
Creating a New Code Project
Assuming that STVD, STVP and Cosmic are properly installed, we will see how to create a new project.
- Firstly, run STVD.
- Select File >> New Workspace.
- Select Create workspace and project.
- Select workspace folder and workspace name.
- Set project name and select toolchain STM8 Cosmic. You may need to set the path of your Cosmic compiler’s installation location. In my case, it is:
C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8
- Type and select target chip part number. Last two or three digits and letters are enough for finding the correct micro.
- Now add the source and header files from the previously mentioned SPL folder.
- After file inclusions, the workspace tab changes as shown below:
- Locate and open c file from the source tab, and then type #include “stm8s.h” at the top as shown below:
- You’ll have to edit the h header file and uncomment the chip number you are going to use as shown below:
- Compile the code once using the key combination CTRL+F7 or by pressing the compile button. If everything is okay, there should be no error or warning message. The reason for this blank compilation is to use the compiler’s powerful code assistant feature. With this feature, we can predict or complete a piece of code line by only writing the first few letters and then pressing CTRL + SPACE keys simultaneously.
During compilation, you may get tons of errors for hardware files that are not available in your target STM8S micro. For instance, CAN hardware is not available in STM8S003K3 and so if you have added CAN source and header files you will get an error for that. Once identified by the error messages, the corresponding header and source files for that particular hardware must be removed.
A wiser approach to avoid this issue is to check device datasheet for available hardware in it. The contents section of datasheet shows this as shown below:
STM8CubeMX can also be used for this purpose but some hardware peripherals which do not have any input-output dependency are not shown in it as previously discussed.
Similarly, one more caution must be observed. Unless your code is using any interrupt, interrupt source and header files (stm8s_it.h and stm8s_it.c) must be excluded. Sometimes it is better to add only those files that you will need to complete a project. For example, if your project is just using GPIOs, it is better to add GPIO files only along with stm8s.h and stm8s_conf.h. However, I recommend this technique only after you have mastered STM8 coding well because in most cases you will need multiple hardware which have dependencies on each other. As an example, when using SPI, you’ll need both GPIO and SPI modules. If you understand these dependencies, it is okay to select files as per need. You can, then, comment out unnecessary hardware module files specified in the stm8s.h header file and get a faster compilation and build process. After compilation, you should always build/rebuild your project by hitting the Build or Rebuild button. This will generate the final s19 output file in either Debug or Release folder according to the generation mode selected. If things are in order, there should be no error or warning message.
Lastly, I have not found any useful simulation software like Proteus VSM or Electronic Workbench that support STM8 family. Thus, we have to debug our code in real-life with real hardware. It may sound difficult but actually it is not so. We can, however, use such software to make models of STM8 micros and make our PCBs. I don’t like simulations as they are not always accurate and real-world type.
One more advice I would like to give to the readers. Please read the SPL help file. It is located in the SPL zip file under the name stm8s-a_stdperiph_lib_um.chm. It explains each function, definition, data structure, all internal hardware modules and how to use them properly. This is a very important document and your best friend in coding STM8 micros. Apart from this document the reference manual is equally important as it details the capabilities of all internal hardware. I won’t be detailing the internal hardware much as these docs will be doing so.
Check this video out for details: