STM8L detailed first steps guide
We are talking about a low-power microcontroller STM8L001J3. I will try to tell in detail about the necessary steps for flashing “hello world” and various pitfalls.
The main characteristics of the microcontroller:
Maximum core frequency | 16 MHz |
Program memory capacity | 8 kB |
EEPROM volume | 256 bytes |
RAM | 1.5 kB |
Number of inputs/outputs | 6 |
Supported interfaces | SPI, I2C, UART |
Supply voltage | 1.8..3.6V |
Frame | SO8 |
From these data it is clear that STM8L001J3 may well replace the ATTiny13 so beloved by everyone. More details can be seen at STM official website.
To study we need:
Here is a device – STM8L moduleit is minimalistic, but everything you need is on board;
IDE, an application for writing and editing code; after long trials, I settled on IAR EW for STM8;
And, of course, the programmer ST-Link V2. Any clone, the main thing is to be working.
Part 1. Installing and configuring IAR EW for STM8
In addition to this option, there are other possible combinations of working with a microcontroller, for example, Code::Blocks/SDCC/ST Visual Programmer or STVD/Cosmic/STVP. By the way, “Cosmic” did not answer my letters for some unknown reason. We will use IAR EW for STM8.
To date, there are a lot of possible restrictions that can be circumvented with some effort. Therefore, I will cover only general points. Perhaps when you read this article something will change. Download IAR EW for STM8 from official site (optional) the latest version and install it on your computer. Next, we set up the environment for work. A small digression for those who love video tutorials: there are very good reviews from Bearded Engineer. True, there is a simple STM8, but the first lessons are quite suitable. And here I will tell you in pictures how to set it all up.
We open the application and go to File / New Workspace, thereby creating a new workspace. We save the project, we call, for example, “HW-STM8L”; press “OK”. We get this screen:
Adding library files to the project. To do this, we will create two new groups, RMB (right mouse button) on the root, Add / Add Group, give them the names “Inc” and “src”.
Further, carefully: if you watch the video in parallel, there will be differences. SPL library files for STM8 and STM8L are different. Therefore, we go to the official website of ST and download libraries for our 8L. The file is called “en.stsw-stm8012”. Save to disk and unzip. Now, to add the library files to our newly created folders, right click on those folders, Add/Add Files. We find the unzipped file en.stsw-stm8012\STM8L10x_StdPeriph_Lib\Libraries\STM8L10x_StdPeriph_Driver on the disk, select everything and add it to the folders according to their names.
Now we add the main file and configuration files. Find the unzipped library file. Select four files from the en.stsw-stm8012\STM8L10x_StdPeriph_Lib\Project\STM8L10x_StdPeriph_Templates folder and transfer them to the project folder.
We agree with the replacement, “OK”. For convenience, we will transfer these files to the root. To do this, again RMB on the root and add three files (mane.c has already changed):
We continue to configure the configuration: RMB on the root file, then Options. This window appears, select our microcontroller in it:
The next setting is C / C ++ Comiler / Preprocessor, we will add our “inc” and “src” folders there.
We also add a folder with four added files.
We register our microcontroller in this Defined window:
Next, in this window, go to Debugger and change the Simulator to ST-Link. Next, in the Output Converter: check the box and select the Output format as “Intel Extended”.
Click “OK” and wait until the settings are applied. Then F7 or a green polyhedron with an arrow, we name Work Space, “OK”, we give some time for assembly. After assembly, there should be no errors. If they are, then exclude the file that the application complains about from the “src” folder. Something like this: RMB on the file, “Options” and check the box here:
In my case, the complaint was about the “stm8l10x_itc.c” file, after excluding it, the error disappeared. Here it is, our wonderful endless loop. Setup completed.
Part 2. Disassembling the STM8L Module
On board this module is a microcontroller, an LED and a button. It is powered by an external voltage of 5-15 Volts. There are separate pins for connecting the programmer. If necessary, you can remove the jumpers J1 and J2, thereby freeing the pins of the microcontroller for your needs. In the kit, by the way, there is an additional microcontroller and a breadboard.
According to the diagram, we have an LED on pin #6 – PB6, and a button on pin #7 – PB7, we will need this in the third chapter when we write code for the microcontroller.
Also note that there are only three pins for in-circuit programming. The usual fourth output “Reset” is missing. This microcontroller does not. This feature must be taken into account when programming, and it imposes some restrictions when writing code. More details in the next chapter.
Part 3. We write the code and upload
First of all, you need to talk about the main features of the STM8L001J3 microcontroller. I already wrote that the developers abandoned the “Reset” legs. Therefore, it is recommended in the datasheet when writing code in the very first lines to give a 5-second delay before starting the execution of the main program. Thus, when power is applied, we have 5 seconds to start uploading the code. If there is no fill, the program will safely start working after a short pause. When the code is fully debugged, you can remove the delay and flash, realizing that you can no longer make code changes to the microcontroller.
So, relying on the datasheet and knowledge of C, we write our Hello World:
/**
******************************************************************************
* @file Project/STM8L10x_StdPeriph_Templates/main.c
* @author KHod
* @version V1
* @date 20-JNR-2023
* @brief This file contains the firmware main function.
******************************************************************************
*
* https://elmodule.sytes.net/?p=130
*
*
******************************************************************************
*/
/* MAIN.C file */
#include "stm8l10x.h"
#include "stm8l10x_gpio.h"
#include "stm8l10x_clk.h"
#define ASM asm
/* This delay should be added just after reset to have access to SWIM pin
and to be able to reprogram the device after power on (otherwise the
device will be locked) */
#define STARTUP_SWIM_DELAY_5S \
{ \
ASM(" PUSHW X \n" \
" PUSH A \n" \
" LDW X, #0xFFFF \n" \
"loop1: LD A, #50 \n" \
\
"loop2: DEC A \n" \
" JRNE loop2 \n" \
\
" DECW X \n" \
" JRNE loop1 \n" \
\
" POP A \n" \
" POPW X " );\
}
/* not connected pins as output low state (the best EMC immunity)
(PA1, PA3, PA5, PB0, PB1, PB2, PB4, PC5, PC6, PD1, PD2, PD3, PD4, PD5,
PD6, PD7)*/
#define CONFIG_UNUSED_PINS_STM8L001 \
{ \
GPIOA->DDR |= GPIO_Pin_1 | GPIO_Pin_3 | GPIO_Pin_5; \
GPIOB->DDR |= GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_4; \
GPIOC->DDR |= GPIO_Pin_5 | GPIO_Pin_6; \
GPIOD->DDR |= GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | \
GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; \
}
/* pin for testing */
#define Led_PORT GPIOB
#define Led_PIN GPIO_Pin_6
main()
{
uint16_t i;
/* -------------STM8L001 startup-------------- */
/* configure unbonded pins */
CONFIG_UNUSED_PINS_STM8L001;
/* delay for SWIM connection: ~5seconds */
STARTUP_SWIM_DELAY_5S;
/* ------------------------------------------- */
/* configure all STM8L001 pins as input with pull up */
GPIO_Init(GPIOA, GPIO_Pin_0, GPIO_Mode_In_PU_No_IT); // pin 1
GPIO_Init(GPIOA, GPIO_Pin_2, GPIO_Mode_In_PU_No_IT); // pin 2
GPIO_Init(GPIOD, GPIO_Pin_0, GPIO_Mode_In_PU_No_IT); // pin 5
GPIO_Init(GPIOB, GPIO_Pin_6, GPIO_Mode_In_PU_No_IT); // pin 6
GPIO_Init(GPIOB, GPIO_Pin_7, GPIO_Mode_In_PU_No_IT); // pin 7
GPIO_Init(GPIOC, GPIO_Pin_2, GPIO_Mode_In_PU_No_IT); // pin 8
/* initialize tested pin */
GPIO_Init(Led_PORT, Led_PIN, GPIO_Mode_Out_PP_Low_Fast);
while (1)
{
GPIO_ToggleBits(Led_PORT, Led_PIN);
/* delay */
for(i=0; i<64000; i++);
}
}
You can copy all the text in the “main.c” page. The main loop is at the very end, it is a few lines. Everything else is initialization and preparation. We transfer the text, press the green square with the arrow (ALT + F7) and the compiler should issue “Error 0”.
Let’s move on to the firmware. We use ST-LINK: download and install fresh drivers for it. Insert into USB. We connect three wires of the ST-LINK programmer: Gnd, Swim, +3.3 to the corresponding terminals of the STM8L Module. Here it should be noted that the test firmware is already loaded on the STM8L Module. Therefore, there are exactly 5 seconds for everything about everything. During this time, you need to connect three contacts and press the “Download and Debug” button (Ctrl + D). After the firmware, restart, wait 5 seconds and see our blinking LED. You can change the pause in the program, however, in order to increase it, you must set the variable “i” as uint32.
These are the first steps. The datasheet contains the code in C. You can add our program yourself so that you can use UART and timers.