STM8 Standard Peripheral Libarary

Today we are going to use the STM8 standard peripheral library (SPL) with STVD IDE and Cosmic Compiler. You may already know from our previous tutorials that we used the stm8 bare-metal programming. We always used the GPIOs and all the Peripherals like UART etc. But ST makes a good Peripheral library for the STM8 as well, just like they did for the STM32 devices. So it is a very unique way to access the peripherals of the STM8 microcontroller. So if you had previously worked with the STM32 devices, the API is going to be the same for the STM8 as well. So it is easy to migrate from the STM32 devices to STM8 devices using the STM8 standard Peripheral Library, which in short is sometimes referred to as SPL.

Download the STM8 Standard Peripheral Library

First of all, you need to go to the ST website and download the STM8S/A Standard Peripheral library package. You may also need to refer to the STVD documentation as a reference. Once you downloaded the libraries you need to extract the zip files into a Project folder which you are using for your workspace. For example in my case, I am using a COSMIC-SPL folder in the My Documents folder. You can choose the name of the folders whatever you like or choose the location of your choice on your computer. Once you are done creating a folder and extracting the Libraries into that folder, you need to create a new workspace in that folder in your STVD IDE.

Create a new Workspace in the STVD IDE

It is better that you create a separate workspace for these projects that you may be planning to do with the STM8 Standard Peripheral Library. So go to your STVD IDE and click on the file and click on create a new workspace. Choose the folder you created for your STM8 SPL libraries. Once everything is done just navigate to that folder and copy the Libraries folder from your SPL folder that you just downloaded and extracted. Here is the folder structure that you may see once you download this.

STM8S_StdPeriph_Lib folder structure
STM8S_StdPeriph_Lib folder structure

Here you see the Libraries folder, this is the folder where all the peripheral libraries are. There are further two folders inside the Libraries folder the inc and src. In the inc folder, there are all the header files and in the src folder, there are all the c files related to the Peripheral drivers. One more important folder is the “Project” folder. This folder contains a template project folder. This folder also has the STM8S_StdPeriph_Examples folder which contains all the examples for each and every peripheral which you may use as a starter project for that specific project.

Create a new Project in STVD IDE

Now go back to your STVD IDE and Add a new Project into your workspace. Select your folder and name your project. After that select your appropriate microcontroller part number. Once all is set up just finish the stm8 project initialization process. Now right click on the left project pannel into the “include file” folder icon and select the “add files to the folder” option. It will take you to the folder browser and here you need to navigate to the Standard Peripheral Libraries folder and from the “src” folder select all the “C” files for your desired Peripheral. Or you may all if you need. You also need to go to the “inc” folder from the Libraries folder and need to select the “stm8s.h” header file. You need one more header file which is “stm8s_conf.h” file. You may find this conf file from the Project template folder.

Write a C Code for STM8S SPL

Once you created your project and you try to compile it, you may see the error which is basically related to the assertions. One more thing you need to change is into the STM8S.h header file and you need to uncomment your prefered STM8S part number. This may look like in the following picture

stm8s.h header file controller definition

Now you need to go to the main.c file from your STVD IDE, and make sure it is changed like this

#include "stm8s.h"


void main(){
   while(1);

}


#ifdef USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endifCode language: C++ (cpp)

You should especially pay attention to the USE_FULL_ASSERT block here. This block will avoid the assertion error which is due to the USE_FULL_ASSERT macro somewhere into the STM8S/A Standard Peripheral Libraries. Now you compile the code and it should compile without any error. If you still face any error you may write them down.

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.