This tutorial aims to show how to use tools, supplied by stm8mu package, to build
program for STM8 MCU. For further examples and more advanced usage look at samples directory of package.
Lets look at simple assembler source file.
1 : .print "=========================================================="
2 : .print "File : led.asm"
3 : .print "Description : Sample program for blink LED on STM8S207C8"
4 : .print "=========================================================="
5 :
6 : .define PC_ODR.w16 $00500A
7 : .define PC_DDR.w16 $00500C
8 : .define PC_CR1.w16 $00500D
9 :
10: ; PC6
11: .define PIN_NUM 6
12:
13: .extern STACK_TOP.w16
14:
15: ;********************************
16: ;* *
17: ;********************************
18: .section "text"
19: jpf start
20: start.w24:
21: ldw X, #STACK_TOP
22: ldw SP, X
23:
24: bset PC_DDR, #PIN_NUM ; Output
25: bset PC_CR1, #PIN_NUM ; Push-pull
26: loop:
27: bset PC_ODR, #PIN_NUM
28: callr delay
29: bres PC_ODR, #PIN_NUM
30: callr delay
31: jra loop
32:
33: ;********************************
34: ;* *
35: ;********************************
36: .define DELAY_VAL_MS 500
37: .define FCPU_HZ {2 * 1000 * 1000}
38: .define LOOP_LENGTH 4
39: .define LOOPS {FCPU_HZ * DELAY_VAL_MS / 1000 / LOOP_LENGTH}
40:
41: delay:
42: pushw X
43: pushw Y
44:
45: ldw Y, #{LOOPS >> 16}.w16
46: ldw X, #{LOOPS & $ffff}.w16
47:
48: delay_dec:
49: decw X
50: jrne delay_dec
51: decw Y
52: jrne delay_dec
53:
54: popw Y
55: popw X
56: ret
At first line we can see ".print" directive.
Print directive can be used to output text, values of constant symbols and expressions during assembling.
There is ".section" directive at line 18. This directive submit following data/instructions to be placed at "text"
section.
To declare constant symbol you can use ".define" directive. Expression to define value of constant symbol can be another constant symbol,
number or arithmetic expression enclosed in "{" "}" brackets.
Instructions syntax are compliant to ST with little differences. Refer to STM8 CPU Programming Manual (PM044) for instructions syntax.
For exact syntax supported by stm8mu_asm look at "samples/asm_syntax/syntax.asm" file.
At line 20 label is defined. Label should have width attribute. Default width, if not specified, is ".w8",
which is shortmem, ".w16" refer to longmem and ".w24" refer to extmem.
To build object file from source invoke following command at command line:
stm8mu_asm --output=led.l0 led.asm
Assembler will create "led.l0" object file which contain position independent code. To make output file, suitable for programming in STM8
memory, this object should be processed by linker. Linker must know how to place data in memory of target. This info supplied by linker-script.
1 : ;
2 : ; Sample linker script for STM8S207C8
3 : ;
4 :
5 : FLASH_START = $8000
6 :
7 : RAM_START = $0000
8 : RAM_SIZE = {6 * 1024}
9 :
10: ; LMA VMA
11: .place "text" FLASH_START FLASH_START
12:
13: STACK_TOP = {RAM_START + RAM_SIZE - 1}
14: .export STACK_TOP
At line 5 declared FLASH_START symbol. This symbol is referenced at line 11.
At line 11 appeared ".place" directive. It is used to map section data to target memory.
There is two addresses used in ".place" directive. LMA address refer to offset of section in image (flash). VMA address
refer to offset of section in target memory.
In this example LMA match VMA for "text" section. In more complex situations, for example,
like "data" section LMA can be differ from VMA.
Look at "samples" directory for more info about linker files.
To build output file from object-files invoke following command at command line:
stm8mu_lkr --script=led.lkr --output=led.s19 led.l0
Linker will create "led.s19" file in motorola S-record format.
You can use "stm8mu_flash" utility to program file to STM8 memory via UART. Target must contain integrated bootloader.
stm8mu_flash --chip=STM8S207C8 --baud=115200 --cport=/dev/ttyUSB0 --input=led.s19 write
|