Categories
Tutorials Assembly Language STM8

Getting Started with STM8 Assembly Language

Assembly language programming is always a great tool to understand underlying architecture of microcontroller. It also helps us to use any microcontroller at it’s full potential. In today’s Article we are going to develop a hello world program and we will be using ST Visual develop IDE throughout our tutorial. We are also going to present some very useful links and helping material to better understand stm8 assembly language. So without wasting any further time let’s just dive into actual stuff.

STM8S103F3P Target Board

We are going to target STM8S103F3P microcontroller and will be using stm8s blue pill board for this. You can get this development board via amazon or any other local store if available. I chooses this board because of its availability in our local store and because of it is very cost effective and cheap. If you find difficulty getting this board you can also purchase this microcontroller directly from ST website or you can choose some similar STM8S microcontroller board.



STM8S103F3P development board

Although you may see USB connector on this board but this is only for powering up this board. This connector has no other functionality rather then providing power to board. There is also one Reset switch to reset microcontroller and one Power LED and one Test LED. Power LED, as it’s names suggest is just to indicate power and the TEST led is available for user to program according to their need. According to its schematic this LED is connected to PORTD pin 3 or PD3

STM8S103F3P6 dev board schematic
Board Schematic

We will try to target this pin for blinking this on board LED available for us to play with.

Microcontroller Features

According to Datasheet of this microcontroller, STM8S103F3 has following features

  • 16 MHz advanced STM8 core with Harvard architecture and 3-stage pipeline
  • Program memory: 8 Kbyte
  • Data memory: 640 byte true data EEPROM; endurance 300 kcycle
  • RAM 1K
  • 2.95 to 5.5 V operating voltage
  • Nested interrupt controller with 32 interrupts
  • Up to 27 external interrupts on 6 vectors
  • 16-bit general purpose timer, with 3 CAPCOM channels (IC, OC or PWM)
  • 8-bit basic timer with 8-bit prescaler • Auto wake-up timer
  • Window watchdog and independent watchdog timers
  • UART, SmartCard, IrDA, LIN master mode, I2C, SPI
  • 10-bit, ±1 LSB ADC with up to 5 multiplexed channels, scan mode and analog watchdog
  • 96-bit unique key for each device

Flashing the board

To upload final object file into this microcontroller or to flash this development board we need a ST-LINK Utility and device. You should not assume that you are going to program this board like an Arduino board via USB cable.

ST-Link V2

If you are unable to figure out how to do this? Than in this blogpost about program stm8 board with st-link v2, writer had explained it very well.

Writing your first Assembly Program

Now its time to write a quick code and see something actually happening. So first of all just download and install STVD for stm8. After installing you will see its shortcut on desktop and also in start menu. So just go and start simply you do with any other application. Now Create a blank workspace and after creating a workspace, create a new empty project. While creating a project it will give option to select the compiler type and in this dialog box you need to select ST Assembler Linker option. After selecting this the editor will automatically add a path you do not need to change that path.

Selection of toolchain

In microcontroller selection screen, you need to select STM8S103F3P for our development board. You can choose other device if your board comes with any other STM8 Chip. Once you are done the project window will show three auto generated directories. There is main.asm file in source Files Directory. You can remove all the lengthy pre-written code and just come up with following simple and clean code.

STVD for stm8 basic program and project
Basic Program in STVD for STM8

You need to understand one very basic thing about STM8 assembly language

Indentation in Code

indentation here matters a lot. Indentation in STM8 assembly denotes as columns and according to reference manual the first column is denoted as Labels. So if you put any opcode in first column then that will be treated as label if it meet labels criteria or will be prompted as error during compile time.

stm8/ Label at first line

This label at first line tells assembler linker that the targeted platform is of STM8 microcontroller family. Next segment ‘rom’ Line tells to store code in rom. Segment opcode tells where to place it and we are telling assembler to put code at very beginning of the Flash ROM. The ‘rom’ directive is defined in mapping.asm file like this

WORDS   ; The following addresses are 16 bits long
segment byte at 8080-9FFF 'rom'Code language: JavaScript (javascript)

So according to this definition our code will be started at address 0x8080. It’s like ORG directive in other kind of Assembly programming.

end directive at last line

There must be an END directive at the last line of code and there must be a line break after that directive. This directive tells assembler that the code has been ended and whenever assembler find this directive it will stop compiling further code even if present.

JRA Instruction



This instruction is short form of JUMP RELATIVE ALWAYS. This is force jump instruction and tells controller to jump over specific relative address always. Whenever controller find this instruction it will jump to the provided address without any hesitation and without caring anything in code. So according to this definition it expect one operator which is relative address of a location to jump over. We can provide label here as we did in our code. So our final code looks something like this so far

stm8/

 #include "mapping.inc"
 segment 'rom'

MLOOP
  NOP
  JRA  MLOOP
 end
Code language: PHP (php)

Now its time to compile our code and see if we are able to compile without facing and error or warnings. To compile our code just press F7 key or go to build menu and selection Build option. If everything goes well this will show success message something like this

Compiled without any error

Closing statement

So that’s it for today’s tutorial. If you followed along this tutorial and end up with a successful compile message, Congratulations than, You had properly setup your development environment and written your first hello world program. This program will do one very important work for your microcontroller which is to keep it busy and stuck it to one loop called infinite loop in Embedded Systems. In our next tutorial we will blink LED and see things happening in our Development board.

Useful Resources

  1. Device Datasheet
  2. ST Assembler Linker User Manual
  3. STM8 Programming Manual
  4. ST-Link Utility
  5. ST-Link User manual
  6. ST Visual Development IDE
  7. Another LED Blink Example
  8. Program STM8S Board with ST-Link V2
  9. Programing STM8 with stlink



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.