RCSTM8
RCSTM8
Compiler manual
Document version
2011-01-25
ST7/STM8 C Compiler Manual
Contents
1. Introduction......................................................................................................................14
1.1 Purpose of this manual.................................................................................................................. 14
1.1.1 For first time users.................................................................................................................. 14
1.1.2 For experienced users............................................................................................................ 14
1.2 Organization of the manual............................................................................................................ 14
1.3 Conventions Used in this Manual................................................................................................... 16
1.4 Additional help or information......................................................................................................... 17
2. Compiler overview............................................................................................................18
2.1 Extensions to the ANSI standard................................................................................................... 18
2.1.1 Limitations.............................................................................................................................. 18
2.1.2 Extensions.............................................................................................................................. 18
2.1.3 C++-like comments................................................................................................................. 19
2.1.4 Binary constants..................................................................................................................... 19
2.2 How to invoke the Compiler........................................................................................................... 19
2.2.1 From Ride............................................................................................................................... 19
2.2.2 From the Command Line........................................................................................................ 20
2.3 Directives list.................................................................................................................................. 20
2.4 Libraries......................................................................................................................................... 20
2.5 Input files....................................................................................................................................... 20
2.6 Output files.................................................................................................................................... 21
2.6.1 Relocatable object file............................................................................................................. 21
2.6.2 Listing file................................................................................................................................ 21
2.6.3 Preprocessor output file.......................................................................................................... 21
2.7 How the Compiler relates to other tools......................................................................................... 21
3. Compiler internals............................................................................................................23
3.1 Function naming conventions........................................................................................................ 23
3.2 The concept of “working registers”................................................................................................. 23
3.3 Parameters passing conventions................................................................................................... 24
3.3.1 ST7 Parameter passing convention........................................................................................24
3.3.2 STM8 Parameter passing convention.....................................................................................24
3.3.3 Functions returning struct or union......................................................................................... 25
3.3.4 Indirect function calls.............................................................................................................. 25
-2-
ST7/STM8 C Compiler Manual
4. Programming examples...................................................................................................40
4.1 Converting C to assembly.............................................................................................................. 40
4.2 Interfacing C to assembly.............................................................................................................. 40
4.3 In-line assembly............................................................................................................................. 40
4.3.1 Important notes about the use of the SRC pragma.................................................................41
4.4 Startup files.................................................................................................................................... 41
4.4.1 Startup size............................................................................................................................. 42
4.4.2 Modifying start-up files............................................................................................................ 42
4.5 Interrupts and trap......................................................................................................................... 43
4.5.1 Interrupts................................................................................................................................ 43
4.5.2 Trap........................................................................................................................................ 44
4.6 Memory space selection................................................................................................................ 46
4.6.1 Memory space qualifiers......................................................................................................... 46
4.6.2 Locating a variable in ROM for smaller footprint.....................................................................47
4.6.3 Local variables memory space qualifiers................................................................................48
4.6.4 Default memory space............................................................................................................ 49
4.7 Automatic variables relocation....................................................................................................... 49
4.7.1 Internal workings of the Auto relocation Mode........................................................................50
4.8 Pointers......................................................................................................................................... 50
4.8.1 STM8 large memory model.................................................................................................... 51
4.8.2 STM8 pointers........................................................................................................................ 51
4.8.3 Pointer arithmetic on STM8:................................................................................................... 52
4.9 Fast 16 x 16 → 32-bits multiplication............................................................................................. 53
4.10 Inram functions............................................................................................................................ 54
4.10.1 The inram keyword............................................................................................................... 55
-3-
ST7/STM8 C Compiler Manual
5. Keywords.........................................................................................................................69
5.1 asm................................................................................................................................................ 70
5.2 at.................................................................................................................................................... 70
5.3 bit................................................................................................................................................... 70
5.4 code............................................................................................................................................... 71
5.5 data................................................................................................................................................ 71
5.6 eeprom.......................................................................................................................................... 72
5.7 far.................................................................................................................................................. 72
5.8 fcode.............................................................................................................................................. 73
5.9 fdata............................................................................................................................................... 73
5.10 hbit.............................................................................................................................................. 73
5.11 hreg............................................................................................................................................. 74
5.12 hreg16......................................................................................................................................... 74
5.13 interrupt N.................................................................................................................................... 75
5.14 near............................................................................................................................................. 75
5.15 page0........................................................................................................................................... 76
5.16 reentrant...................................................................................................................................... 76
5.17 trap.............................................................................................................................................. 76
6. Directives.........................................................................................................................77
6.1 ASM / ENDASM............................................................................................................................. 80
-4-
ST7/STM8 C Compiler Manual
-5-
ST7/STM8 C Compiler Manual
7. Libraries.........................................................................................................................127
7.1 Standard C libraries..................................................................................................................... 127
7.2 Library files.................................................................................................................................. 127
7.3 STM8-specific Libraries............................................................................................................... 128
7.4 Intrinsic functions library.............................................................................................................. 128
7.4.1 _getCC_............................................................................................................................... 130
7.4.2 _getSP_................................................................................................................................ 131
7.4.3 _halt_.................................................................................................................................... 132
7.4.4 _jmp_.................................................................................................................................... 133
7.4.5 _nop_.................................................................................................................................... 134
7.4.6 _rim_.................................................................................................................................... 135
7.4.7 _rsp_..................................................................................................................................... 136
7.4.8 _setCC_................................................................................................................................ 137
7.4.9 _setSP_................................................................................................................................ 138
7.4.10 _sim_.................................................................................................................................. 139
7.4.11 _swapbyte_......................................................................................................................... 140
7.4.12 _swapnibble_...................................................................................................................... 141
7.4.13 _trap_................................................................................................................................. 142
7.4.14 _wfe_.................................................................................................................................. 143
7.4.15 _wfi_................................................................................................................................... 144
7.5 ST Firmware libraries................................................................................................................... 145
7.6 Hardware registers access.......................................................................................................... 145
7.7 Large objects access functions.................................................................................................... 147
7.7.1 The fmemcpy function.......................................................................................................... 147
7.7.2 The lmemcpy function........................................................................................................... 147
7.7.3 The lmemset function........................................................................................................... 147
7.7.4 The lstrlen function............................................................................................................... 147
7.8 STM8 library................................................................................................................................ 148
7.9 Library files.................................................................................................................................. 148
-6-
ST7/STM8 C Compiler Manual
8. Error messages..............................................................................................................150
8.1 FATAL ERROR S000: Insufficient memory.................................................................................157
8.2 FATAL ERROR S001: File not found...........................................................................................157
8.3 FATAL ERROR S002: Write error on file.....................................................................................157
8.4 FATAL ERROR S003: Read error on file.....................................................................................157
8.5 FATAL ERROR S004: Missing argument list...............................................................................157
8.6 FATAL ERROR S005: Illegal file extension.................................................................................157
8.7 FATAL ERROR S006: Cannot open file.......................................................................................157
8.8 FATAL ERROR S008: User Break..............................................................................................157
8.9 ERROR P000: Line too long........................................................................................................ 158
8.10 ERROR P001: Incorrect end of line........................................................................................... 158
8.11 ERROR P002: Invalid character................................................................................................ 158
8.12 ERROR P003: Invalid character................................................................................................ 158
8.13 ERROR P005: Unknown directive or control..............................................................................158
8.14 ERROR P006: Bad or missing file name...................................................................................159
8.15 ERROR P007: Unexpected ENDIF............................................................................................159
8.16 ERROR P008: Unexpected ELSE............................................................................................. 159
8.17 ERROR P009: Unexpected ELIF...............................................................................................160
8.18 ERROR P010: Incorrect End Of File.......................................................................................... 160
8.19 ERROR P012: Wrong number of parameters............................................................................160
8.20 ERROR P014: Redefinition of macro is not allowed..................................................................161
8.21 ERROR P015: Macro cannot be modified.................................................................................161
8.22 ERROR P016: Invalid numeric constant....................................................................................161
8.23 ERROR P017: Incorrect number format....................................................................................162
8.24 ERROR P018: Invalid octal character........................................................................................162
8.25 ERROR P019: Integer constant out of range.............................................................................162
8.26 ERROR P020: \x not followed by an hexadecimal digit..............................................................162
8.27 ERROR P022: Use of DEFINED without argument...................................................................163
8.28 ERROR P023: Incorrect use of control or directive....................................................................163
8.29 ERROR P024: Missing macro name after ‘define’.....................................................................163
8.30 ERROR P025: Identifier expected............................................................................................. 163
8.31 ERROR P027: string expected.................................................................................................. 163
8.32 ERROR P028: Generic error..................................................................................................... 164
8.33 ERROR P029: Duplicated formal parameter.............................................................................164
8.34 ERROR P030: Cannot remove predefined macro.....................................................................164
8.35 ERROR P031: Macro body cannot start or end with '##'............................................................164
8.36 ERROR P032: new line expected, extra characters found.........................................................164
8.37 ERROR P033: Directive does not apply to ST7.........................................................................164
8.38 ERROR C000: Missing Character.............................................................................................. 165
-7-
ST7/STM8 C Compiler Manual
-8-
ST7/STM8 C Compiler Manual
-9-
ST7/STM8 C Compiler Manual
- 10 -
ST7/STM8 C Compiler Manual
- 11 -
ST7/STM8 C Compiler Manual
8.195 WARNING C205: BIT(s) not allowed as automatic variables (char type assumed).................203
8.196 ERROR C206: Volatile not allowed for function.......................................................................203
8.197 WARNING C207: Suspicious pointer conversion.....................................................................203
8.198 WARNING C221: BIT(s) not allowed as parameter(s) (char type assumed)...........................203
8.199 FATAL C228: Recursive file inclusion......................................................................................204
8.200 ERROR C230: Missing initialization......................................................................................... 204
8.201 WARNING C231: ‘Typedef’ and memory space cannot go together. Memory Space ignored. 204
8.202 ERROR C237: Memory space mismatch.................................................................................205
8.203 ERROR C240: Internal compiler error.....................................................................................205
8.204 WARNING C251: Type "double" not handled, "float" assumed................................................205
8.205 ERROR C252: Memory space mismatch in redeclaration.......................................................205
8.206 ERROR C253: Too much data on stack..................................................................................206
8.207 WARNING C255: Indirect calls allowed only for reentrant functions........................................206
8.208 ERROR C256: Improper use of “typedef”................................................................................206
8.209 WARNING C257: Function recursively calls itself without having the 'reentrant' qualifier........207
8.210 WARNING C258: Not enough space to store Common Sub-Expression................................207
8.211 WARNING C259: Object larger than 256 bytes in smallobject mode.......................................207
8.212 ERROR C262: Function at absolute address above 0xFFFF in SMALL model.......................208
9. Glossary.........................................................................................................................209
List of tables
Table 1: ST7 Parameter passing convention.................................................................................................. 24
Table 2: STM8 Parameter passing convention............................................................................................... 24
Table 3: Function return storage..................................................................................................................... 27
Table 4: IEEE Floating point............................................................................................................................ 33
Table 5: Predefined macros............................................................................................................................ 36
Table 6: QCW bits.......................................................................................................................................... 38
Table 7: RCSTM8-specific keywords summary.............................................................................................. 69
Table 8: Listing directives................................................................................................................................ 77
Table 9: General directives............................................................................................................................. 78
Table 10: Code generation directives.............................................................................................................. 78
Table 11: Language directives........................................................................................................................ 78
Table 12: ST7/STM8 specific directives.......................................................................................................... 79
Table 13: Standard C library headers............................................................................................................ 127
Table 14: Library files.................................................................................................................................... 127
Table 15: Available library files for putchar................................................................................................... 128
Table 16: STM8-specific library headers....................................................................................................... 128
Table 17: Intrins.h library functions............................................................................................................... 129
Table 18: Description of available libraries.................................................................................................... 148
Table 19: System errors................................................................................................................................ 150
Table 20: Preprocessor errors...................................................................................................................... 150
Table 21: Compiler errors............................................................................................................................. 152
- 12 -
ST7/STM8 C Compiler Manual
- 13 -
ST7/STM8 C Compiler Manual
1. Introduction
The Raisonance ST7/STM8 Development Kit is a complete solution to creating software for the ST7
and STM8 families of microcontrollers. The Development Kit comprises many different tools that allow
projects ranging from simple to highly complex to be developed with relative ease.
1. Introduction
This is the section you are currently reading. It covers what is in the manual, how it is arranged, and
conventions used by the manual.
2. Compiler overview
This section covers compiler basics. How the Compiler relates to other tools in the Raisonance
ST7/STM8 Development Kit, input files required by the Compiler and output files generated by it, how
the Compiler is controlled, and how the Compiler is invoked.
3. Compiler internals
- 14 -
ST7/STM8 C Compiler Manual 1. Introduction
This section deals with the internal aspects of the compiler that might be of interest for the advanced
user. It covers parameters passing conventions (useful when interfacing C and Assembler programs),
data types and sizes, stacks management, predefined macros and more.
- 15 -
ST7/STM8 C Compiler Manual
4. Programming examples
This section contains a number of examples on how to use the compiler in specific situations like
interfacing C and Assembler, using interrupts, space-qualified pointers, memory models, startup files,
recursion, intrinsic functions and more.
5. Keywords
This section contains the complete list of specific keywords (which are not part of the ANSI standard).
6. Directives
This section contains the complete list of RCSTM8 directives. Each directive is explained and
examples are provided.
7. Libraries
This section presents the list of libraries that are provided with RCSTM8.
8. Error messages
This section is a list of all the Error messages (including warnings and Fatal errors) that the compiler
can generate. Every error is explained and its most likely cause is suggested.
[…] Represents a list of optional items that are the same as the preceding optional
item.
The Ride installation directory will be referred to as “%ridedir%” in this document. For instance, if you
installed Ride in the default “C:\Program Files\Raisonance\Ride” directory, “%ridedir%\lib” will refer to
the actual “C:\Program Files\Raisonance\Ride\lib” directory.
- 16 -
ST7/STM8 C Compiler Manual 1. Introduction
Note:
Throughout this manual the terms “Compiler” or just “RCSTM8” will refer to the RCSTM8
Compiler.
In early versions of the Raisonance ST7/STM8 development kit, the Compiler file name was
RCST7.exe. This file name has been updated to RCSTM8.exe. All projects that you may have
created using the previous name will still be workable, although we recommend renaming the
Compiler to match the new file name.
Telephone: +33 4 76 61 02 30
Fax: +33 4 76 41 81 68
Email: info@raisonance.com
If you find any errors or omissions, or if you have suggestions for improving this manual, please let us
know by emailing: support@raisonance.com
- 17 -
ST7/STM8 C Compiler Manual
2. Compiler overview
RCSTM8 is a cross compiler for the C language that will produce machine code for the ST7 and STM8
families of microcontrollers.
RCSTM8 implements the ANSI standard for the C language as defined in the following standards:
● ISO/IEC 9899:1990 standard (sometimes referenced as ISO C90).
● Technical Corrigendum 1 (ISO/IEC 9899 TCOR1) approved in 1995.
● Technical Corrigendum 2 (ISO/IEC 9899 TCOR2), published in 1996.
The standard has been extended in order to allow close interaction with the hardware and make the
best out of the ST7 and STM8 architectures without the need to learn their assembler subtleties.
2.1.1 Limitations
The main limitations are in the compiler libraries: as RCSTM8 does not come with any operating
system, some libraries that require one (example: file management) are not implemented. This is
referenced as a “free standing” C implementation in the C Standard.
The ST7 and STM8 are 8-bit processors; hence high-order arithmetic is costly in terms of code size.
For this reason, data types larger than 32 bits are not implemented, as they cannot be handled
efficiently. In particular, all floating-point arithmetic is done in float (IEEE-754 32-bits single precision)
instead of double (64-bits) precision, including mathematical functions available through the "math.h"
header file.
The character constants (such as 'A' or '\n') have the type 'char' on RCSTM8, although the C Standard
specifies characters constants as being of type 'int'. For this reason, the RCSTM8 Compiler reports
sizeof('A') as having value 1, instead of 2 as the standard mandates.
2.1.2 Extensions
RCSTM8 ‘understands’ a number of keywords that are not in the ANSI standard. Keywords are
reserved words that have a special meaning for the Compiler and that cannot be used as variable
names.
RCSTM8 keywords are used to address the following specific features:
● Memory space modifiers. A specific memory space can be specified for each variable using
RCSTM8-specific keywords such as bit, code, data, page0, eeprom, fcode, and fdata. Hardware
Registers can be accessed through more specific keywords such as hbit, hreg and hreg16.
- 18 -
ST7/STM8 C Compiler Manual 2. Compiler overview
● Pointer size. In STM8 mode, pointers can be 16-bit (near) or 24-bit (far).The keywords far and
near are type modifiers that can be used to declare the proper type of pointer. Please refer to
chapter 4.8 , section “Pointers” for details.
● Absolute memory location. Variables can be places at specified memory locations using the
"at" keyword.
● In-line assembly code. Assembly instructions can be generated using the "asm" keyword.
● Interrupts and trap. Declaration of interrupt routines can be done through the "interrupt N" and
"trap" keywords.
● RAM- located functions, taking advantage of the “inram” keyword.
● Inline functions that do not use a CALL/RET sequence, using the “inline” keyword.
● Reentrancy. To specify a function as being reentrant, the "reentrant" keyword must be used.
Examples:
Compiler options can be set in Ride at global level (so that they are valid for all the C files of the
project) or at local level (so that they are valid only for a given file). When local options are specified
they overwrite the global options for that particular source file.
- 19 -
ST7/STM8 C Compiler Manual
The executable RCSTM8 accepts controls on the command line called “directives” that instruct the
Compiler on how to operate. The complete command line syntax is:
filename:
the name of the file to be compiled. If no extension is specified, the .c extension is assumed.
options:
a space separated list of directives. The directives may be listed in any order. If each directive is not
explicitly listed then defaults will be used for the missing directives.
Please refer to the chapter 6. Directives. Special directives may not be listed in this section of the
command line.
2.4 Libraries
A library is a pre-compiled collection of common or useful functions.
RCSTM8 is provided with ANSI standard libraries (some are not included, see “Limitations” above).
The libraries are written in assembly language for best efficiency.
Also some RCSTM8-specific libraries are provided for hardware-related manipulation. These libraries
can be accessed through the "intrins.h" header file (in-line generation of intrinsic functions) and through
the "stxxx.h" header files that provide easy access to Hardware Registers available on a specific ST7
or STM8 device.
- 20 -
ST7/STM8 C Compiler Manual 2. Compiler overview
- 21 -
ST7/STM8 C Compiler Manual
RIDE Editor
Compiler Assembler
.lib file
Object-HEX Converter
.hex file
Ride Simulator/Debugger
- 22 -
ST7/STM8 C Compiler Manual 3. Compiler internals
3. Compiler internals
This chapter deals with some of the internals of the Compiler that are useful for advanced users,
especially those wanting to mix C and Assembler in an application.
The chapter starts off by explaining the main assumptions that have been made to adapt the compiler to
the STM8 (this is mainly useful to understand what happens when you are debugging in the
disassembled code window) and continues with a set of practically oriented paragraphs where you find
information that you might need to develop special applications.
void c_fct(void)
{
return;
}
As you can see below from the linker report, the reentrant functions add an extra question mark (the “?”
symbol) before the function name.
* * * * * * * C O D E M E M O R Y * * * * * * *
CODE 2000H 0001H UNIT ?PR?c_fct?COMPARISONS
CODE 2001H 0001H UNIT ?PR??c_reentrant_fct?COMPARISONS
If you work in C only, you can simply ignore how functions are called internally, but if you want to mix C
and assembler you must know that a function declared as reentrant in C must be called from assembler
by prepending a question mark ‘?’ before the function name (“ c_reentrant_fct” becomes “?c_reentrant_fct ” in
the example above).
As they are contiguous, they can be referenced in pairs to handle 16-bit objects, or as a quadruplet to
handle 32-bit objects.
The BH:BL pair is then referenced as BX; The CH:CL pair is then referenced as CX; The BH:BL:CH:CL
quadruplet is referenced as BX:CX. The BL:CX 24-bits pseudo-register is used for addressing objects
above the first 64KB section (far pointers or far objects).
- 23 -
ST7/STM8 C Compiler Manual
Note:
The “All functions are reentrant” option in Ride (available through “Project | Properties | Compiler
options | Program Model | All functions are reentrant”) automatically makes all functions reentrant,
therefore adding a question mark before the name of each function.
This mode can also be called “auto mode”.
- 24 -
ST7/STM8 C Compiler Manual 3. Compiler internals
Notes:
As the STM8 makes good use of the stack, the pseudo-registers are not used for parameter
passing.
In STM8 mode, the RCSTM8 compiler is always in AUTO mode (all functions have the reentrant
attribute), the parameters are always passed through stack and never through RAM.
As it is not possible to access stack elements further than 256 bytes away, the size for
parameters plus the size for local variables is limited to 256 bytes.
In order to have a better grasp of this issue, consider the following example:
void main(void)
{
asm_fct_p2(1,2);
}
- 25 -
ST7/STM8 C Compiler Manual
We have a non-reentrant function with 8 bytes of parameters. The compiler will generate the following
code:
A600 F LD A,#HIGH(?asm_fct_p2?BYTE)
B703 F LD ?BH,A
A600 F LD A,#LOW(?asm_fct_p2?BYTE)
B704 F LD ?BL,A
CD0000 F CALL ?C?MV4_PG2IB
00 DB #000H
00 DB #000H
00 DB #000H
01 DB #001H
A600 F LD A,#HIGH(?asm_fct_p2?BYTE)
B703 F LD ?BH,A
A600 F LD A,#LOW(?asm_fct_p2?BYTE)
B704 F LD ?BL,A
CD0000 F CALL ?C?MV4_PG2IB
00 DB #000H
00 DB #000H
00 DB #000H
02 DB #002H
CD0000 F CALL asm_fct_p2
Lines 1 to 4 save the second parameter (the absolute value 2) in memory, at an address labelled ?
asm_fct_p2?BYTE. Lines 5 to 8 save the first parameter (the absolute value 1) in working registers
BH:BL:CH:CL. The function is then called.
void main(void)
{
asm_fct_p2(1,2);
}
The code generated for the function call in this case is as follows:
A602 LD A,#002H
88 PUSH A
A600 LD A,#000H
88 PUSH A
A600 LD A,#000H
88 PUSH A
A600 LD A,#000H
88 PUSH A
CD0000 F CALL ?C?MV4_PG2BC
00 DB #000H
00 DB #000H
00 DB #000H
01 DB #001H
CD0000 F CALL ?asm_fct_p2
9E DNF LD A,S
AB04 ADD A,#004H
95 LD S,A
As you can see the second parameter is pushed on the stack (lines 1 to 8), while the first parameter is
passed in working registers in the same way as the non-reentrant functions.
- 26 -
ST7/STM8 C Compiler Manual 3. Compiler internals
When parameters are passed via the memory the compiler creates a label to identify the memory
location where the parameters are stored: the name of this symbol is ?function_name?BYTE.
When you mix C and assembler you must take care of defining such symbols as public and to reserve
the necessary number of bytes for the parameters with the DS directive.
void main(void)
{
int result;
result=asm_fct_p1(1,2);
}
The code produced by the RCSTM8 compiler in ST7 mode will be as follows:
- 27 -
ST7/STM8 C Compiler Manual
Data types requiring more than 32 bits are not implemented, as they cannot be managed effectively by
the ST7 nor by the STM8 (which are 8-bit processors).
3.6 Bit-fields
The RCSTM8 compiler supports bit-fields as defined in the C99 standard. The way bit-fields are
allocated their storage by RCSTM8 is as follows:
● A bit-field can have a maximum size of 8 bits (1 byte, limited by the accumulator size).
● The RCSTM8 compiler allocates bytes as the minimum storage unit for bit-fields. If a bit-field
does not fill up the whole storage allocated, the unused bits are ignored. In other words, it is
not possible to use bit-fields to allocate storage units smaller than a byte.
● The bit-fields are stored in their order of apparition in the declaration of their holding structure
from lsb (bit 0) up to msb (bit 7).
● A bit-field cannot cross several bytes. In case there is not enough room in the current byte to
hold the next bit-field of a structure, a new byte is allocated for the structure at the memory
address immediately higher than the current one, and the bit-field is stored there.
struct
{
unsigned char c;
unsigned int b3:3;
unsigned int b2:2;
unsigned int b4:4;
unsigned char c2;
} bitstr;
- 28 -
ST7/STM8 C Compiler Manual 3. Compiler internals
- 29 -
ST7/STM8 C Compiler Manual
We will have the following allocation, relative to the base address in memory referenced as “Base @”:
Example:
The example of code below,
is translated as:
0000 A600 LD A,#000H
0002 B700 F LD i1,A
0004 A60A LD A,#00AH
0006 B700 F LD i1 + 01H,A
0008 A600 LD A,#000H
000A C70000 F LD l1,A
000D A632 LD A,#032H
000F C70000 F LD l1 + 01H,A
The sign bit is stored as the most significant bit of the highest address. The next 8 bits are the
Exponent and the remaining bits are the mantissa.
- 30 -
ST7/STM8 C Compiler Manual 3. Compiler internals
Example:
The C statement f1=5.6 is translated by the RCSTM8 compiler as:
This is because the hexadecimal representation for the floating point “5.6” is 0x40B33333 (see below
for details).
3.8.2 Representation
IEEE-754 32 bits floating point numbers are represented using 3 elements:
● s: Sign (1 bit). A '0' in this bit represents a positive number, '1' represents a negative.
● e: Exponent (8 bits)
● m: Mantissa (23 bits)
In big endian mode, we will hence have the following memory representation:
+--------+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |0|0|0|0|0|0|0|0|0|0|1|1|1|1|1|1|1|1|1|1|2|2|2|2|2|2|2|2|2|2|3|3|
|bit# |0|1|2|3|4|5|6|7|8|9|0|1|2|3|4|5|6|7|8|9|0|1|2|3|4|5|6|7|8|9|0|1|
+--------+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Element |S| Exponent |(msb) Mantissa (lsb)|
+--------+-+-------------+-+-------------+---------------+---------------+
|Byte | 0 | 1 | 2 | 3 |
+--------+---------------+---------------+---------------+---------------+
The following formula is used to convert a binary representation of a float to its actual value:
- 31 -
ST7/STM8 C Compiler Manual
s = 0,
e = 80h,
m = 499FDAh = 1001001 00001111 11011010b
b = 7Fh //mentioned here as a reminder: b is not part of the representation
hence
pi = (-1)^s x 1.m x 2^(e -b )
= (-1)^0 x 1.1001001 00001111 11011010b x 2^(80h-7Fh)
= 1 x 1.1001001 00001111 11011010b x 2^1
= 1 x 1.5707963d x 2
= 3.1415925
Some operations may return some values with a very large absolute value. In case the representation
of such numbers does not fit the IEEE-754 format, a specific value "INF" (Infinity) is returned instead.
Positive and negative infinities can be returned.
Some operations with invalid input operands (for example INF-INF) produce an unknown mathematical
output, which is represented as the specific value "NaN" (Not a Number).
The NaN value is compared as an ordered value in IEEE-754 terms. That means that it can be
compared as equal, different, greater, smaller than any other floating-point value including itself or
infinities. This is a specific behavior, as IEEE-754 NaN values can be unordered in some
implementations, in which NaN is never equal to any other value.
Consequently with the RCSTM8 compiler, “NaN == NaN” is true.
The following shows the representation of some specific IEEE-754 values (from smallest negative to
largest positive):
- 32 -
ST7/STM8 C Compiler Manual 3. Compiler internals
INF is an IEEE-specific representation for infinity. For instance, the result of 1.0 / 0.0 leads to
+INF. There is only one representation for Infinity (and another for minus Infinity) shown in the
table below.
NaN is an IEEE-specific representation for values that do not represent the value of a real
number. They are obtained after illegal operations (0/0, 0*Infinity, Infinity-Infinity, Infinity/Infinity)
and are silently propagated during computations. Several (lots of) representations of NaN are
possible in the IEEE-754 format, but the RCSTM8 compiler only emits the representation "7F C0
00 00" for NaN. This is referred as "Positive quiet NaN", and is the representation used by Intel®
80x87 floating-point processors.
No distinction is made between "quiet NaN" and "signaling NaN", which are defined by the IEEE-
754 standard. All operations involving NaN are considered "quiet" i.e., they do not generate any
form or fault, trap or exception on the processor. Signaling NaN are usually used as the result of
operations involving uninitialized variables and should not be propagated during computations.
'-0' (negative 0) is a possible value upon entry of RCSTM8 mathematical operations, but will never
be generated.
The following table shows how an IEEE float should be interpreted depending on its Exponent and
Mantissa values:
- 33 -
ST7/STM8 C Compiler Manual
– Infinities are not handled. For instance, 1.0 / 0.0 returns 1.0 with the fast float library.
– Overrun/underrun is not checked. For instance, 1e20 / 1e-20 leads to an result which is not
representable in IEEE-754 single precision, and should therefore be reported as INF. This is
properly handled by the standard float library, but leads to an invalid result with the fast float library.
– NaN are not handled in the fast float library.
– IEEE rounding is NOT the same as the 8087 FPU, but is a truncate instead. This means
that the transcendental operations +, -, * and / may report a result which is slightly different than the
standard float library.
– The "-0.0" value is not supported as input. Most noticeably, comparing 2 floats respectively
holding the 0.0 and -0.0 values will report the floats as being different.
– X / 0.0 returns X, for any X value.
– Arithmetic is performed on 24-bit mantissas (instead of 32-bits) for the standard float
library. This may in some cases lead to decreased accuracy (wrong mantissa's lsb).
Note:
The fast floating-point libraries are only available for STM8 and will not be developed for ST7.
In order to use the fast floating point libraries in your projects, the FASTLIBRARY directive should be
activated for the linker.
- 34 -
ST7/STM8 C Compiler Manual 3. Compiler internals
The typical case is a function called from the main program: during the execution of the function an
interrupt occurs, and the interrupt service routine calls the same function. If the function that is called
twice is not reentrant the whole application will fail. Reentrant functions have a number of constraints,
the main one being that all local variables, including parameters, must be located on the stack (as
opposed to a fixed memory area) so that they will not be corrupted in case of nested calls.
These constraints make management of reentrant functions heavy and, for this reason, functions are
not reentrant by default in RCSTM8.
In order to force the compiler to make a function reentrant, you have to use the keyword “reentrant” as
in the following example:
- 35 -
ST7/STM8 C Compiler Manual
The table below shows the predefined macros and symbols available in RCSTM8.
Symbol Description
Produces the constant "0" when compiling in standard mode, "1" when
__AUTO__
compiling in AUTO mode and “2” if LARGESTACK option is active.
Produces a string literal containing the date of compilation, in the form
__DATE__
''mm dd yyyy''.
Produces a string literal containing the date of compilation in the form
__DATE2__
''mm dd yy''.
Produces a string literal containing the date of compilation in the form
__DATE3__
''dd mm yy''.
__FILE__ Produces a string literal containing the name of the file being compiled.
__LINE__ Produces a decimal constant containing the current source line number.
Reflects the current global memory class (set through the DEFAULTGC
__MODEL__ directive. Produces the constant “0” when memory class is page0, and
“1” when class is “data”.
Produces the constant “1” when optimizing for speed, else “O” when
__OPTSPEED__
optimizing for size.
__RAISONANCE__ Produces the constant "7".
Deprecated macro, use __RCSTM8__ instead. Produces the RCSTM8
__RCST7__
version number.
__RCSTM8__ Produces the RCSTM8 version number.
Produces the constant "0" for small ROM model (limited to 64kB), and
__ROM_MODEL__ "1" for LARGE models. Note that LARGE model is available only for
STM8.
Produces the page number (0 or 1) in which the stack resides. This
__STACKPAGE__
information is not relevant for STM8.
__STDC__ Produces the constant “1”.
Produces the constant "1" when compiling for ST7, and "0" when
__ST7__
compiling for STM8.
Produces the constant "0" when compiling for ST7, and "1" when
__STM8__
compiling for STM8.
Produces a string literal containing the time of compilation, in the form
__TIME__
''hh:mm:ss''.
- 36 -
ST7/STM8 C Compiler Manual 3. Compiler internals
date=__DATE__;
time=__TIME__;
file=__FILE__;
version=__RCSTM8__;
ln = __LINE__;
while(1);
}
The __RCSTM8__ macro is typically used to isolate compiler dependencies, as in the following
example:
#if defined(__RCSTM8__)
// RCSTM8 specific stuff
#else
// For other compilers, remove some keywords that are RCSTM8 specific
#define DATA
// more keywords to be removed here…
#endif
- 37 -
ST7/STM8 C Compiler Manual
A thorough check of the command-line options used for compiling a specific file can be performed by
checking the listing file produced by the compiler: The actual command-line options passed to the
RCSTM8 Compiler will appear on the seventh line of the listing file (starting with "COMPILER
INVOKED BY:").
The third line of your RCSTM8 Compiler listing gives you the QCW used to compile the current file.
QCW(0x00183F60)
The following table explains the bits which are used for QCW:
Bits Description
Bits 2-0 Unused (always 0).
Bit 3 DUPLICATESTRING option active.
Bit 4 ENUMTYPE(CHAR) option active.
Bit 5 UNSIGNEDCHAR option active.
Bit 6 INITSTATICVAR option active.
Bit 7 AUTO mode active.
Bits 10-8 Optimization level (0 to 7).
Bit 11 GENERIC mode active.
Bit 12 Size optimization active.
Bit 13 PROMOTE mode active.
Bit 14 LARGE mode active.
Bit 15 Stack in page zero.
Bit 16 LARGESTACK option active.
Bit 17 LARGEOBJECT option active.
Bits 19-18 Default Global Memory Class (1=page0, 2=data, 3=fdata).
Bits 21-20 Default Local Memory Class (1=page0, 2=data, 3=fdata).
Bits 23-22 Compiler mode (0=ST7, 2=STM8)
Bit 24 FAR mode active.
Bit 25 ANSI mode active.
Bits 31-26 Reserved for future use.
- 38 -
ST7/STM8 C Compiler Manual 3. Compiler internals
In the example above, the QCW is 0x00183F60, where the following options set can be found:
● UNSIGNEDCHAR
● OT(7)
● GENERIC
● OT(size)
● PROMOTE
● DEFAULTGC(data)
● DEFAULTLC(page0)
- 39 -
ST7/STM8 C Compiler Manual
4. Programming examples
This chapter contains a number of examples on how to use the compiler in specific situations like
interfacing C and Assembler, using interrupts, space-qualified pointers, memory models, startup files,
recursion, intrinsic functions and more.
There are two slightly different approaches to mixing C and Assembler with RkitSTM8
● Writing C and Assembler in different files (recommended): you can then call Assembler
functions from C and C functions from assembler by knowing the parameters passing
conventions and the return value conventions (see chapter 3 and the example below)
● Writing C and assembler in the same file: this technique is called “in-line assembly” and is
supported by RkitSTM8 with some limitations:
Either you insert your assembler statements in hexadecimal directly (machine code, no symbols,
opcodes or mnemonics)
Or you use the SRC directive to tell the compiler to generate an intermediate assembler file (rather
than an object file directly). In this case, the project will have to be manipulated with Ride as explained
in Chapter 4.
In order to mix C and Assembler you must know some of the internals of the compiler: Reading and
understanding chapter 3 is mandatory in such a case. You should also be familiar with the listing file
from the compiler and the map file from the linker.
Mixing C and assembler is particularly difficult when you have reentrant functions and when you have
functions with several parameters.
- 40 -
ST7/STM8 C Compiler Manual 4. Programming examples
Let’s see first how to write some machine code directly into a source file. As this is practically impossible
to manage for more than one instruction, its use is reserved for exceptional cases.
This is the hexadecimal op-code translation of the WFI (Wait for Interrupt) assembly instruction, then
your CPU clock will be stopped until an interrupt occurs.
The WFI instruction can also be called through the _wfi_() intrinsic function (refer to chapter 4.11 for
details).
● The SRC pragma is mandatory for each file containing both C and Assembler.
● The SRC pragma must be the first line of your file (before any code)
- 41 -
ST7/STM8 C Compiler Manual
In a real application there are many things to initialize, but, as they can usually be initialized after the
main() function has been called, there is no need to modify the startup code; just call your initialization
functions from your main program:
void main(void)
{
init_hardware();
init_variables();
// other things that need initialization...
- 42 -
ST7/STM8 C Compiler Manual 4. Programming examples
4.5.1 Interrupts
An interrupt function must be declared with a specific syntax:
● void funcname(void) interrupt intnumber
● There must be no parameters and no return value.
● The keyword “interrupt” must follow the function header.
● After the “interrupt” keyword you can specify the interrupt number handled by the function.
● intnumber is the interrupt number taken by the function (from 0 to 13 on ST7, from 0 to 31 on
STM8).
Examples:
When the compiler translates the code for an interrupt function it behaves as follows:
● The function is normally compiled and placed somewhere in code memory (the linker decided
where)
● The interrupt vector address is located at its absolute address (please refer to the INTVECTOR
directive for details). At this absolute address the compiler puts the 2-bytes address of the actual
interrupt routine (In the STM8 case, the interrupt vector is a 3-bytes address).
● The function is terminated by RETI (as opposed to the standard RET).
● Any working registers used by the function (typically for local variables) are saved on stack and
restored before RETI.
When working with interrupts you should keep in mind the following considerations:
● Any function you call from an interrupt routine should be reentrant or your application could
crash.
● You must enable global interrupts and setup every specific peripherals to generate interrupts
before you can actually work with them.
● You must set interrupt priorities.
● Any interrupt you do not explicitly declare is not managed: If you don’t declare an interrupt
routine for PortA (for example) and if an interrupt happens on PortA nonetheless, then the
interrupt is serviced and a random address will be called –as the interrupt vector is randomly
filled– , leading to a system crash.
- 43 -
ST7/STM8 C Compiler Manual
4.5.2 Trap
RCSTM8 allows management of the trap function from C (please refer to STM8 device manual for
details). The “trap” keyword syntax is similar to the interrupt syntax, except that no interrupt number can
be given in the trap case:
void funcname(void) trap
● There must be no parameters and no return value.
● The keyword “trap” must follow the function header.
Example:
- 44 -
ST7/STM8 C Compiler Manual 4. Programming examples
The following code is an example showing how the isr() function, defined as the interrupt 4 handler,
can be assigned as the handler for interrupt 5 as well:
In case you need to generate interrupts but you do not want the compiler to generate the interrupt
vectors, you can use the NOINTVECTORS directive that will prevent any interrupt vector generation.
- 45 -
ST7/STM8 C Compiler Manual
● zero-page: the first 256 bytes [00-FFH] are addressable through 8-bit addresses instead of 16-
bit addresses for the rest of the memory space. This characteristic makes the "zero-page"
instructions more efficient than the standard equivalent instruction (typically one byte smaller).
● bit space: the zero-page can also be addressed with bit-instructions. Indeed, a single bit can be
considered as a standalone object, and a complete byte can contain up to 8 of such objects.
Addressing and testing bit objects is done using specific instructions leading to very efficient
code and low memory consumption.
● code space: most STM8 derivatives feature two different physical memories: a ROM (or
FLASH) and a RAM. The ROM generally contains the executable code, whereas RAM contains
the "variables". Some "constant variables", such as strings or large tables, can be located in
ROM. This avoids taking up some scarce RAM space, as well as avoids the need to initialize
their values upon startup.
● data space: This is the generic storage space located in the bottom part of the [0000-FFFFH]
memory address range. Please refer to your STM8 derivative data sheet for further detail.
● eeprom space: This is a storage space located in the [0000-FFFFH] memory address range. It
maps onto the EEPROM device embedded on your STM8 derivative. Please refer to your STM8
derivative data sheet for further detail.
● far space: This is the generic memory space (code or data) located anywhere in the [000000-
FFFFFFH] memory (24-bits) address range. Far space is only available for STM8 devices.
bit myflag;
// "bit" should be used for boolean variables.
// Such variables will be located in the 00-FFh memory range.
// RCSTM8 generates optimized code for accessing and testing value of
// bit-qualified variables
- 46 -
ST7/STM8 C Compiler Manual 4. Programming examples
ensures that no new value will be affected to the variable “val”, and that its reference will not be passed
to functions that may modify its value.
Example:
- 47 -
ST7/STM8 C Compiler Manual
Example:
The following code,
void func1(void)
{
// Let the "welcome" variable be declared in ROM using the "code" keyword
static code char welcome[] = "This is a great application";
puts(welcome);
- 48 -
ST7/STM8 C Compiler Manual 4. Programming examples
Example:
Note: DMC
The DMC (Default Memory Class) directive acts as if both DLC and DGC directives were applied.
Example:
“DMC(page0)” is equivalent to "DLC(page0) DGC(page0)”.
The list of relocated variables will then be passed to the compiler, and subsequent builds of the project
will automatically relocate the variables according to the produced “Auto Relocation File”.
- 49 -
ST7/STM8 C Compiler Manual
4.8 Pointers
Pointers in RCSTM8 are generic i.e., they can be used to access any object in any memory space
(Hardware Registers, page0, stack, RAM, EEPROM, ROM), except bit objects.
As the STM8 memory mapping is uniform in the 0x0000-0xFFFF space, pointers are represented
internally by the compiler as 16-bit (2 byte) objects. The first byte of a pointer (with the lowest address)
contains the most significant byte (MSB) of the address of the pointed object, although the second byte
contains its Least Significant Byte (LSB) (this can be referred to as big-endian mode).
Here is an example of pointer usage on RCSTM8:
#include <stdio.h>
void main(void)
{
static char s; // Character allocated in RAM
char *ps = &s; // Pointer on char
- 50 -
ST7/STM8 C Compiler Manual 4. Programming examples
After compilation, the execution of the program above produces the following output (results may vary
depending on your configuration and the STM8 device you selected):
The example below shows how the RCSTM8 handles the various memory areas using pointers.
Address of c: 0x0090
Address of i: 0x0081
Address of f: 0x1000
Address of s: 0x0087
Example:
char far *fpc; // 24-bit pointer to a char
- 51 -
ST7/STM8 C Compiler Manual
In some cases, your applications may require some pointers that can sweep through the whole
memory address space (from 000000h to FFFFFFh). Far pointers can be affected any possible
address, but the pointer arithmetic is limited to the current section.
pc = (char *far)0x01FFFF;
pc++; // Arithmetic performed in current section [010000-01FFFFh]
After the “pc++” statement, we should expect pc to contain the “0x02000” value. However, as pointer
arithmetic (including the “++” increment operator) is limited to the current section, the actual value will
be “0x02000” instead.
- 52 -
ST7/STM8 C Compiler Manual 4. Programming examples
The RCSTM8 compiler offers a fast 16-bit x 16-bit → 32-bit multiplication, which takes only about 70
cycles to execute on STM8.
For example:
short a, b;
long res = (long)a * b; // Long mul, both operands 16-bit: Optimized to muls161632
short a;
long res = 43L * a; // Long again: Optimized to muls161632
Note that there are two versions of the 16 x 16 → 32 multiplication, one for signed multiplication
(named ?C?MULS161632) and the other one for unsigned multiplication (named ?C?MULU161632) which is
even faster. You can check in your Compiler listing file whether the right multiplication has been
applied.
- 53 -
ST7/STM8 C Compiler Manual
- 54 -
ST7/STM8 C Compiler Manual 4. Programming examples
Functions that have the inram qualifier are executed from RAM. Their memory mapping is
automatically adapted by the linker without having to think about specific memory mapping.
Upon reset, the C startup code copies the inram function from Flash to its RAM location.
Note: inram functions can have any valid C prototype. More specifically, they can take any valid
list of parameters and return an object of any type.
Note: When using 32-bit objects such as long variables, the Compiler will use some (private)
helper functions. For instance, when shifting a long such as mylong >>= 5; the Compiler will use
its private ?C?sll328 function, which is stored in Flash.
- 55 -
ST7/STM8 C Compiler Manual
Note: The inram functions should be moved to RAM before being called. Forgetting this step will
lead to application failure.
The C startup code will automatically perform this step upon chip reset, however the RAM space
may get overwritten by the application, and manual copy of the function in RAM will ensure that
the proper code gets executed.
res = 0;
void main(void)
{
res = inRamFct(); // The RAM function is called and result stored in res
while(1); // Infinite loop
}
In this example, the code for the inRamFct is automatically copied from Flash to RAM during startup.
There is no specific compiler or linker setting to perform. Just drag/drop this code in a blank
application, build your project and execute it. You can step through the code to ensure that the PC is in
RAM space when the inRamFct is entered.
- 56 -
ST7/STM8 C Compiler Manual 4. Programming examples
int res = 0;
void main(void)
{
// Copy the function code using memcpy()
memcpy(inRamFct, // RAM destination address
(void*)&__address__inRamFct, // Flash source address
(int)&__size__inRamFct); // Code size of the function
Note: As you see in the example above, the strange “(void*)&” casts are here for accessing the
__address__inRamFct and __size__inRamFct. This is because these are not actual variables but
symbols defined by the linker. As such they do not have any storage but they have an address,
and the casts are required to convert this address into a pointer or a size.
- 57 -
ST7/STM8 C Compiler Manual
#include <stdio.h>
#include <string.h> // Needed by memcpy
// The second function is not ready (not at its proper RAM place)
res = inRamFct2(); // INVALID! inRamFct2 not yet in RAM
checkEqual(res, 3); // This check will fail if functions overlayed
1
Please refer to the RLSTM8 Linker Manual for full details about SEGMENTOVERLAY.
- 58 -
ST7/STM8 C Compiler Manual 4. Programming examples
In order to overlay the functions inRamFct() and inRamFct2() at the same RAM address, the following
directive should be passed on the linker's command-line:
SEGMENTOVERLAY(?PR??INRAMFCT?MAIN,?PR??INRAMFCT2?MAIN)
(The exact segment names that must be entered as directives arguments can be retrieved from your
project map file)
Note about debugging: Debugging inram functions is usually transparent, the debugger works
just as if the function was in Flash.
However, in the case of overlays, the debugger is not able to distinguish the source lines coming
from the overlayed inram functions, and the debug may jump erratically between the source
lines of all functions.
To work around this problem, compile your project with debug activated on only one of the
overlayed inram functions: the one you need to debug. The other overlayed inram function(s)
should not have the debug compiler directive activated.
This way you can freely debug your inram functions even in the case of overlays.
Example:
#include <intrins.h>
void main(void)
{
_rsp_(); // Reset stack pointer
_jmp_(0x1234); // Absolute jump to an address
val1 = _swapbyte_(val1); // Swap the bytes from val1
_sim_(); // disable interrupts
myport |= 4; // Raise bit 2
myport = _swapnibble_(myport); // swaps upper and low nibbles
_nop_(); // "do nothing" instruction
_rim_(); // re-enable interrupts
_halt_(); // Put the micro in halt mode.
_wfi_(); // Wait for interrupt
_wfe_(); // Wait for event
_trap_(); // Software trap
}
- 59 -
ST7/STM8 C Compiler Manual
Reentrant functions have a number of constraints which make them unsuitable for use with small
embedded micros (including STM8, see Chapter 3 for more details), However, in some cases, if the
performance penalty is accepted, reentrant functions can be handy for some kinds of calculations.
The following example uses reentrant functions to solve the “Towers of Hanoi” problem:
#include <stdio.h>
//---------------------------------------------------------------------
// Definitions and global values
//---------------------------------------------------------------------
#define SIZE 4 // Number of disks to move
- 60 -
ST7/STM8 C Compiler Manual 4. Programming examples
//---------------------------------------------------------------------
// Function: void ComplexMove ( int dest, int src, int size ) reentrant
//---------------------------------------------------------------------
// Description:
// Moves 'size' disks from 'src' maste to 'dest' maste.
//---------------------------------------------------------------------
void ComplexMove(int dest, int src, int size) reentrant
{
int ThirdLocation = ~(dest + src) & 3;
if (size>1)
ComplexMove(ThirdLocation, src, size-1);
HardMove(dest, src); if (size>1)
ComplexMove(dest, ThirdLocation, size-1);
}
//---------------------------------------------------------------------
// Function: void HardMove ( int dest, int src )
//---------------------------------------------------------------------
// Description:
// Takes a disk from 'src' maste an puts it on 'dest' maste.
//---------------------------------------------------------------------
void HardMove(int dest, int src)
{
int disk;
int i;
//---------------------------------------------------------------------
// Function: void Repeat ( char c, unsigned char n )
//---------------------------------------------------------------------
// Description:
// Outputs 'n' times the character 'c'.
//---------------------------------------------------------------------
void Repeat(char c, unsigned char n)
{
char k;
for ( k = 0 ; k < n ; k ++)
putchar(c);
}
//---------------------------------------------------------------------
// Function: void DrawPosition ( void )
//---------------------------------------------------------------------
// Description:
// Displays the state for the 3 mastes.
- 61 -
ST7/STM8 C Compiler Manual
//---------------------------------------------------------------------
void DrawPosition(void)
{
int j,size,n,i;
//---------------------------------------------------------------------
// Function: void main ( void )
//---------------------------------------------------------------------
// Description:
// This is the main procedure for this program.
//---------------------------------------------------------------------
void main (void)
{
int i;
// Stacks initialization
for (i=0;i<SIZE;i++)
{
PosArray[0][i] = (SIZE-i); //Left column contains all the disks
PosArray[1][i] = //Central column does not contain any disk
PosArray[2][i] = 0; //Right column does not contain any disk
}
- 62 -
ST7/STM8 C Compiler Manual 4. Programming examples
The dynamic memory handling functions are defined in the <stdlib.h> standard header file as follows:
void *malloc(size_t size); allocates an object of size 'size', and returns the address
of the object if successful; otherwise, it returns a null
pointer. The values stored in the object are
indeterminate.
void *calloc(size_t nelem, size_t size); allocates an array object containing 'nelem' elements
each of size 'size', stores zeros in all bytes of the array,
and returns the address of the first element of the array if
successful; otherwise, it returns a null pointer.
void *realloc(void *ptr, size_t size); allocates an object of size 'size', possibly obtaining initial
stored values from the object whose address is 'ptr'. It
returns the address of the new object if successful;
otherwise, it returns a null pointer.
void free(void *ptr); If 'ptr' is not a null pointer, the function deallocates the
object whose address is 'ptr'; otherwise, it does nothing.
You can deallocate only objects that have been allocated
by calling calloc, malloc, or realloc.
The dynamic memory comes from a "heap" of available space, which must first be granted to the
dynamic memory manager. This must be done using the mempool_init function before any call to dynamic
memory functions is performed.
void mempool_init(void *ptr, size_t size); initializes the dynamic memory manager and provides a
memory buffer of size 'size' at the address pointed to by
'ptr' for the heap.
Note: The dynamic memory manager requires at least 32 bytes to be functional. This is the
minimal size of the buffer to be passed to mempool_init().
- 63 -
ST7/STM8 C Compiler Manual
Here is an example that shows how the dynamic memory manager can be used:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* buffer;
while(1);
}
- 64 -
ST7/STM8 C Compiler Manual 4. Programming examples
This makes debugging with the simulator easy, as the output of the SCI peripheral is shown in a
dedicated simulator window under Ride.
The printf() available with the RKit-STM8 toolset fully implements the printf specification from the ISO
C90 standard. It even provides the “%p” extension for printing pointer values.
However, this complexity comes at the cost of a large ROM footprint 1. Even if printf is hand-coded in
assembly to obtain the best possible footprint, there are some cases where your projects need only
some of the printf functionality, and linking the whole printf code into your project would increase your
ROM size by several KB.
The Rkit-STM8 toolset transparently offers several versions of the printf function, and will automatically
link the smallest possible version of printf that fits your project source files requirements The following
printf versions are available:
1
Notice that printf, as any of the functions from the RCSTM8 library, does not require any RAM storage
and only uses stack for data storage.
- 65 -
ST7/STM8 C Compiler Manual
In actual applications, the main communication/display peripheral can vary: in order to still be able to
use the formatting capability of printf(), you can redirect its output by simply rewriting the putchar()
function: the following example assumes that you have a text display attached to port B (the logic to
access it is not taken into account) and shows how to redirect the printf() output to this display.
All console output functions such as printf and puts internally use the putchar function. You can provide
a replacement for putchar, and the printf/puts functions from the RCSTM8 library will still be available
for use in your programs
#include <stdio.h>
int putchar(char c)
{
// Copy c to port B
PBDR=c;
return(1);
}
When you run this program, you can see the string being printed on port B, as you can check in the
trace as follows:
● Compile / Link the file in a Ride project through “Project | Make Project”.
● Go to debug mode through “Debug | Start”.
● Open a trace view using “View | Debug | View Trace”.
● Add a trace on changing values of Port B (PBDR).
● Run the project through “Debug | Run”.
● You will see the characters from the string passed to printf displayed in the trace window.
If you create your own versions of console I/O ANSI functions such as printf, puts, putchar, getchar,
ungetchar and scanf, you must provide the appropriate initialization function.
- 66 -
ST7/STM8 C Compiler Manual 4. Programming examples
char switch_var;
void main ( void )
{
int aa=3;
switch (switch_var)
{
case 1: aa=1; break;
case 2: aa+=3; break;
case 3: aa*=5; break;
case 4: aa=10; break;
case 5: aa-=1; break;
case 6: aa-=1; break;
case 7: aa-=1; break;
case 8: aa-=1; break;
case 9: aa-=1; break;
case 10: aa-=1; break;
default: break;
}
while (1);
}
As the case variable increments regularly, RCSTM8 will translate this using a jump table, generating
optimized and a very small code.
If we now change one of the case values to break the sequence (for example, if we change “case 10”
with “case 100”), or if we reduce the number of cases below 10, RCSTM8 will switch back to normal
mode, where the switch variable is compared against every single case statement.
For example:
char switch_var;
switch (switch_var)
{
case 7: aa-=1; break;
- 67 -
ST7/STM8 C Compiler Manual
- 68 -
ST7/STM8 C Compiler Manual 5. Keywords
5. Keywords
This chapter provides a list of keywords that are specific to RCSTM8 (that is, that are not ANSI
standard). These keywords, which are case-sensitive (always use lower case only) are mainly used to
access RCSTM8 -specific features. As with all keywords, RCSTM8 -specific keywords cannot be used
as variable or function names.
Keyword Description
asm Allows in-line insertion of assembly instructions.
at Absolute memory address specification for data objects and functions.
bit Memory space qualifier that applies to a given object (i.e., a variable). Object is
located in the “page zero” RAM space. The bit keyword makes the RCSTM8
compiler aware that the object is to be handled as a single bit, hence it will optimize
access to the object using specific RCSTM8 instructions which make bit operations
very efficient.
code Memory space qualifier that applies to a given object (i.e., a variable). Object is
located in the “code” memory space, which is in ROM or FLASH memory. code
objects are inherently non-modifiable.
data Memory space qualifier that applies to a given object (i.e., a variable). Object is
located in the “data” memory space i.e., in the 0000h-FFFFh RAM address space.
eeprom Memory space qualifier that applies to a given object (i.e., a variable). Object is
located in the “eeprom” memory space.
far Pointer qualifier for STM8. Specifies relocation anywhere in the 16MB memory
space.
fcode Memory space qualifier (STM8 LARGE model only). Object is located in the FAR
CODE memory space (ROM).
fdata Memory space qualifier (STM8 LARGE model only). Object is located in the FAR
DATA memory space.
hbit Defines a bit in a Hardware Register.
hreg Defines an 8-bit Hardware Register.
hreg16 Defines a 16-bit Hardware Register.
interrupt N Qualifies a function as an interrupt handler
near Pointer qualifier for STM8. Specifies relocation in the “section zero” (first 64 KB)
memory space.
page0 Object is located in the “zero page” i.e., in the 00h-FFh RAM address space. This
enables the RCSTM8 compiler to make best use of the STM8 architecture, and
produces optimized code much more efficient for page0 than for data objects.
Object is located in “zero page” i.e. in the 00h-FFh RAM address space.
reentrant Qualifies a function as reentrant.
trap Same as interrupt (-1)
- 69 -
ST7/STM8 C Compiler Manual
5.1 asm
Syntax: asm {hex_value, hex_value, … }
Description: The asm keyword allows to insert some assembler (directly as hex numbers) in
line with C code.
5.2 at
Syntax: at <address> variable_declaration; or
at <address> function_declaration;
5.3 bit
Syntax: bit <variable_name>;
Description: The bit keyword allows defining a variable of type bit. Bit variables consume only
one bit of memory space (the linker will optimize bit variables so that they
consume the least possible memory).
Also the RCSTM8 compiler will generate optimized code for bit variables, using
specific bit-oriented assembly instruction.
- 70 -
ST7/STM8 C Compiler Manual 5. Keywords
5.4 code
Syntax: code
Description: Used in a variable declaration, it specifies the variable to be in the CODE memory
space i.e., in ROM. Note that variables in CODE memory cannot be modified, and
are therefore constants.
5.5 data
Syntax: data
Description: Used in a variable declaration, it specifies the variable to be in the DATA memory
space.
- 71 -
ST7/STM8 C Compiler Manual
5.6 eeprom
Syntax: eeprom
If no initializer is given to a variable, the compiler will emit a warning C230 “Initialization missing”;
however, the code will be fully functional. In this case, the variable will not be initialized in the HEX
file produced.
5.7 far
Syntax: far
Description: For the STM8, specifies relocation anywhere in the 16MB memory space (not
necessarily the zero section).
- 72 -
ST7/STM8 C Compiler Manual 5. Keywords
5.8 fcode
Syntax: Used in a variable declaration, it specifies the variable to be in the FAR CODE
memory space i.e., in ROM and anywhere in the 24-bit addressing range of the
STM8. Note that variables in FAR CODE memory cannot be modified, and are
therefore constants.
5.9 fdata
Syntax: Used in a variable declaration, it specifies the variable to be in the FAR DATA
memory space i.e., anywhere in the 24-bit addressing range of the STM8.
5.10 hbit
Syntax: hbit
Description: “hreg” is a C type that defines a bit in a Hardware Register. The variable declared
must be absolutely placed in memory using the “at” keyword, or the RCSTM8
compiler will generate an error.
- 73 -
ST7/STM8 C Compiler Manual
5.11 hreg
Syntax: hreg
Description: “hreg” is a C type that defines an 8-bit Hardware Register. The variable declared
must be absolutely placed in memory using the “at” keyword, or the RCSTM8
compiler will generate an error.
5.12 hreg16
Syntax: hreg16
Description: “hreg16” is a C type that defines a 16-bit Hardware Register. The variable declared
must be absolutely placed in memory using the “at” keyword, or the RCSTM8
compiler will generate an error.
- 74 -
ST7/STM8 C Compiler Manual 5. Keywords
5.13 interrupt N
Description: The function (must return “void” and accept only void parameters) is an interrupt
handler. It will be ended by an IRET instruction, and contains a safe frame
(push/pop of the working registers). It also places the interrupt vector at its
address:
(0xFFFA – N*2) for ST7 (2-bytes address).
(0x8008 + N*4) for STM8 (3-bytes address, preceded by a 0x82 opcode).
5.14 near
Syntax: near
Description: For the STM8, it indicates an address in the zero section (first 64KB)
- 75 -
ST7/STM8 C Compiler Manual
5.15 page0
Syntax: page0
Description: The object will be relocated in the “Zero page” i.e., in the 0x00-0xFF address
range. PAGE0 objects help the RCSTM8 produce optimized code, as the
ST7/STM8 architecture offers enhanced capabilities for PAGE0 handling.
Note: PAGE0
PAGE0 is a limited space, and ST7/STM8 devices typically offer only few dozen of byte storage in
PAGE0. For this reason, you should be careful about your choices of PAGE0 variables. Generally
the variables that are the most manipulated in your application will be the best candidates to be
located in PAGE0.
5.16 reentrant
Description: Specifies that the function is reentrant (the local variables/parameters will be
stacked).
See also: See Chapter 4 “Recursion” for a complete explication and examples on how to
use reentrant functions.
5.17 trap
Description: Non-maskable software trap definition. It will be ended by an IRET instruction, and
contains a safe frame (push/pop of the working registers).
A function defined as trap can be called very efficiently using the TRAP assembly
instruction, which can be generated using the RCSTM8 intrinsic function _trap_().
Note: “trap”
“trap” can be seen as “interrupt (-1)”, e.g. vector located at 0FFFCH
- 76 -
ST7/STM8 C Compiler Manual 6. Directives
6. Directives
Directives are special keywords that may be used to control the behavior of the Compiler. Most
directives can be specified in three different ways:
● Via the Ride interface
● On the command line
● In the source file after the #pragma keyword
Example: setting the default global memory class to “page0” can be achieved by either:
● Using #pragma defaultGC(page0) in the source file (note that this must be the first line of the
file for this specific pragma, or it is ignored)
● Setting the option in Ride through project properties dialog box, RCSTM8 general options.
● Specifying the “defaultGC(page0)” option in the command line:
Some directives have a local effect (that is, they affect only one function or one part of the code): these
can only be specified using the #pragma keyword.
Some directives, when specified in the source file with the #pragma keyword, can only be used at the
beginning of the file (otherwise a warning is issued and the directive is ignored)
Directive Description
CODE Include the assembler translation of C statements in the listing file.
Place lines that were not compiled because part of conditional compilation
(NO)COND
statements in the listing file or not.
(NO)LISTINCLUDE Include the lines coming from include files in the listing or not.
PREPRINT Generate a preprocessor listing file.
(NO)PRINT Generate the listing file or not.
PPON/PPOFF Turn on/off preprocessor listing generation.
Include in the listing file a list of the symbols generated from the source
SYMBOLS
file.
NOPRINTOFFSET Do not offset the lines in the listing file with spaces.
- 77 -
ST7/STM8 C Compiler Manual
Directive Description
ASM/ENDASM Insert inline assembler in a C source file.
NOPROPAGATION Forbid the const or code variable value propagation.
(NO)INTVECTOR Defines a specific memory location for interrupt vectors.
OPTIMIZE Defines the kind (size, speed) and level of optimization.
SRC Generate an assembler source file (as opposed to an object file directly).
SAVE Save the compiler's context.
RESTORE Restore the compiler's context.
Directive Description
ENUMTYPE Define if enum variables are handled as char or integer.
(NO)INITSTATICVAR Define if static variables should be initialized to zero or left non-initialized.
SUE_OPT Make the names for struct, union and enum optional (C++ syntax).
(UN)SIGNEDCHAR Define whether chars should be signed or unsigned by default.
(NO)AUTO Define whether all functions are reentrant or not.
MODANSI Restrict the compiler to ANSI mode.
- 78 -
ST7/STM8 C Compiler Manual 6. Directives
Directive Description
DEFAULTGC Define the default memory class for global variables.
DEFAULTLC Define the default memory class for local variables.
(NO)FARPOINTERS Tells the compiler that your application makes use of far pointers.
(NO)INSECTION0 Tells the compiler to locate all code in section 0 on STM8.
(NO)INRAM Tells the compiler to set all functions as “inram”.
(NO)LARGESTACK Tells the compiler to use a large stack (2-bytes large) on STM8.
SMALL/LARGEOBJEC Define if objects >256 bytes should be accepted or not.
T
ST7SET/STM8SET Generate ST7 or STM8 code.
Tells the compiler to use a stack in page 0 (0x00-0xFF memory range) or
STM8STACKSET0/1
in page 1 (0x100-0x1FF memory range)
SPACEDEF Assign specific memory spaces to some variables.
Change the size of the stack space allocated to Common Sub-
STACKSIZEFOREXP
Expressions.
- 79 -
ST7/STM8 C Compiler Manual
Syntax: ASM
ENDASM
Description: ASM, along with ENDASM , allows to insert inline assembler within a C source file.
Note that, for ASM to work, the SRC directive must be active.
- 80 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: AUTO
NOAUTO
Description: Define whether all functions are reentrant or not. This places local variables
(automatic variables and parameters) on the stack. This is the same as making all
functions reentrant.
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored.
In STM8 the compiler is always in AUTO mode, hence this directive has no effect.
Examples: AUTO
au
NOAUTO
- 81 -
ST7/STM8 C Compiler Manual
6.3 (NO)CLEANIRQDIVFLAG
Syntax: CLEANIRQDIVFLAG
NOCLEANIRQDIVFLAG
Description: In some corner cases, a DIV/DIVW instruction in an interrupt routine may generate
a false result on STM8.
PUSH CC
POP A
AND A,#0BFh
PUSH A
POP CC
This code sequence ensures that the DIV error never occurs.
The cost for this extra code sequence is 6 bytes and 5 execution cycles.
In case the extraneous cycles or code bytes necessary for this code sequence are
a limitation for your application, you may want to prevent the compiler to generate
it. In such a case, you can invoke the NOCLEANIRQDIVFLAG compiler directive.
This directive is available in STM8 mode only, and has no effect in ST7 mode.
Examples: NOCLEANIRQDIVFLAG
cleanirqdivflag
Default: Active
- 82 -
ST7/STM8 C Compiler Manual 6. Directives
6.4 CODE
Syntax: CODE
Description: This directive tells the compiler to include in the listing file the assembler
translation for each C source line.
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Examples: CODE
cd
Default: Inactive
- 83 -
ST7/STM8 C Compiler Manual
Description: These directives tell the compiler whether to include in the listing file the source
lines that where not compiled because of conditional compilation.
This directive must appear at the beginning of a file (before any C statement), otherwise it is ignored
Examples: COND
CO
noco
Default: Active
- 84 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: DEBUG
NODEBUG
Description: These directives tell the compiler to include debugging information in the object file
or not.
The directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored.
Examples: DEBUG
DB
nodb
Default: Active
- 85 -
ST7/STM8 C Compiler Manual
Syntax: DEFAULTGC(page0|data|fdata)
Description: Defines the Default Global variables Memory Class, that is, the class (or memory
space) where global variables will be put by default. Possible values are page0 for
zero page, data for the zero section (first 64 KB or memory) and fdata for any
memory location in the 24-bit addressing space (16 MB).
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
The fdata memory location is available only in STM8 LARGE memory model.
Examples: DEFAULTGC(PAGE0)
dgc(data)
Default: PAGE0
- 86 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: DEFAULTLC(page0|data|fdata)
Description: Defines the Default Local variables Memory Class, that is, the class (or memory
space) where local variables will be put by default. Possible values are page0 for
zero page, data for the zero section (first 64 KB or memory) and fdata for any
memory location in the 24-bit addressing space (16 MB).
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
The fdata memory location is available only in STM8 LARGE memory model.
Examples: DEFAULTLC(PAGE0)
dlc(data)
Default: PAGE0
- 87 -
ST7/STM8 C Compiler Manual
Syntax: DEFINE(name[=value][,name[=value]]*)
Description: This directive defines preprocessor macros from the compiler's command-line. It
can be used for instance to perform conditional compilation of your source files
using “#if”, “#ifdef” or “#if defined()” preprocessor directives.
Examples: DEFINE(BUF=10)
define(BUFSIZE=32,ENGLISH_VERSION,INTROSPECT=1,NDEBUG)
df(complete_libraries)
- 88 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: DEFINEJOKER(Symbol=Value)
Description: Defines a symbol that can be used as extern in other source file without any type
check.
Examples: DEFJ(Port=0xA0)
Default: None
- 89 -
ST7/STM8 C Compiler Manual
Description: This directive enables a compiler optimization that stores duplicated (but identical)
strings as a single object.
Examples: DUPLICATESTRING
ds
NODS
Default: NODUPLICATESTRING
- 90 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: ENUMTYPE(char|int)
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Default: int
- 91 -
ST7/STM8 C Compiler Manual
Syntax: FARPOINTERS
NOFARPOINTERS
Description: Tells the compiler whether that your application makes use of far pointers or not.
The RCSTM8 compiler handles pointers as 2-bytes by default; you will have to
explicitly set the pointer type to "page0*" in order to use 1-byte addressing, or "far*"
in order to use 3-bytes addressing.
In case you make use of far pointers in your application, you should instruct the
RCSTM8 compiler to take care of them by invoking the FARPOINTERS directive.
In the case where you make use of functions returning structures, the RCSTM8
compiler will then automatically return structures located in far data i.e., addressed
using 3-bytes pointers.
Examples: FARPOINTERS
nofp
Default: NOFARPOINTERS
- 92 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: GENERATEDEPFILE(TXT|filename)
Description: Generate the dependencies file (list of dependencies of the given source file,
including information about the source size)
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Examples: GENERATEDEPFILE
generatedepfile(outputfiles\main.mdb)
gendep(txt)
- 93 -
ST7/STM8 C Compiler Manual
Syntax: GLOBAL(directive)
Description: Allows specifying another directive that can be unknown to the compiler. If the
directive is known the compiler will process it; if it is unknown, it will ignore it with a
warning (whereas it would generate an error if global were not used). This directive
is used by Ride to manage some options that are common across different tool
chains and its usage by Customers is strongly discouraged.
Examples: GLOBAL(DEFAULTGC(data))
Default: None.
- 94 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: INCLUDE(filename)
I(filename)
This directive is useful to handle specific cases (tests, validation) which require
some special treatment. All the specific handling can be performed within a header
file, which can be included or not depending on the command line, from script files
or makefiles.
Default: None.
- 95 -
ST7/STM8 C Compiler Manual
Syntax: INITSTATICVAR
NOINITSTATICVAR
Description: Define if static variables should be initialized to zero or left non-initialized. When
you declare a global variable with no initialization value, the compiler can explicitly
generate code to set it to zero depending on this directive.
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Examples: INITSTATICVAR
is
nois
Default: NOINITSTATICVAR
- 96 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: INSECTION0
NOINSECTION0
Description: Tells the compiler to locate the generated code in section 0 (0x0000-0xffff memory
address range).
You can take advantage of this directive in cases where you need to handle
function pointers and your code does not fit in section 0 (more than 64 KB).
Ensuring that the code to be pointed to is in section 0 will enable use of 2-byte
pointer arithmetic and lead to more efficient code.
Examples: INSECTION0
noin0
Default: NOINSECTION0
- 97 -
ST7/STM8 C Compiler Manual
Syntax: INRAM
NOINRAM
You can use this directive to compile part of a project so that it executes from
RAM. For example, it can be used on a file that contains all the functions required
to reprogram the flash (in-application programming –IAP) that need to be executed
from RAM.
Examples: INRAM
noir
Default: NOINRAM
- 98 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: INTVECTOR(0xFFFA)
NOINTVECTOR
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored.
The default memory mapping for ST7 interrupt vectors is as follows:
The NOINTVECTOR directive will prevent the compiler from emitting any interrupt
vector.
Examples: INTVECTOR(0xFFFA)
iv(0x00fa)
NOINTVECTOR
- 99 -
ST7/STM8 C Compiler Manual
Syntax: LARGEOBJECT
SMALLOBJECT
Description: Allow large objects to be created by the compiler. Large objects are objects such
as tables, structures or unions that have a memory footprint larger than 255 bytes.
If your application does not use large objects, the SMALLOBJECT directive should
be invoked in order to generate an optimized code.
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Examples: LARGEOBJECT
SMALLOBJECT
laob
- 100 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: LARGESTACK
NOLARGESTACK
Description: Tells the compiler to accept more than 255 bytes of local variables on stack.
● The STM8 offers the capability to handle the stack pointer (SP) as a 16-bits register. However,
the STM8 instruction set only references stack objects relative to the stack pointer with a 1-byte
displacement.
● For performance and code compactness reasons, the RCSTM8 Compiler only offers 255 bytes
of variables local to a function in STM8 mode. All the objects are accessed using 1-byte
addresses relative to the Stack Pointer SP.
● In cases where some functions need more than 255 bytes for local data, the LARGESTACK
directive provides support for large (16-bits) access to stack objects.
● This option, however, requires a large amount of excess code.
● We recommend using this option in last resort only, and prefer using “static” local variables,
which will get allocated in RAM rather than on the stack.
● This directive is available on STM8 only.
Examples: LARGESTACK
nolstk
Default: NOLARGESTACK
- 101 -
ST7/STM8 C Compiler Manual
Syntax: LISTINCLUDE
Description: Include the lines coming from include files in the listing or not.
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Examples: LISTINCLUDE
LC
nolistinclude
Default: Inactive
- 102 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: MAXERR(1..200)
MAXWAR(1..512)
Description: This directive tells the compiler to stop compiling after a given number of errors /
warnings. Note that the code generation is stopped at the first error, but the
analysis of the code can go on to find other errors.
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Examples: MAXERR(20)
MAXWAR(50)
Default: 25
- 103 -
ST7/STM8 C Compiler Manual
Syntax: MODANSI
Description: Restrict the compiler to ANSI mode. This instructs the compiler to switch to “auto”
mode (all functions are reentrant), and activates ANSI-style integer promotion.
The code produced using MODANSI will have lesser optimization, hence will be
larger than without the directive activated.
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Examples: MODANSI
ansi
Default: 25
- 104 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: NOPRINTOFFSET
Description: Do not offset the lines in the listing file with spaces.
Examples: NOPRINTOFFSET
- 105 -
ST7/STM8 C Compiler Manual
Syntax: NOPROPAGATION(code|const|aggressive)
Description: This directive disables some optimizations. Depending on the argument, different
optimizations can be devalidated:
– Const: Disables the constant propagation optimizations. This is useful if you
are handling variables that are modified asynchronously by other portions of
code, but you do not want to declare the given variables as volatile.
– Code: Disables constant propagation mechanisms for “code”-qualified
variables. This is useful if you have a variable in ROM, but the value of the
variable is not constant, for instance it can be a calibration value which can be
dynamically reprogrammed in flash ROM.
– Aggressive: Disables aggressive common sub-expressions, where aliasing
phenomenons could lead to potentially invalid code. Note that this type of
optimizations are available only in STM8 mode, not in ST7.
Examples: NOPROPAGATION(CODE)
code unsigned char flag = 0; // Can be used even if modified in EEPROM
noprop(const)
NOPROP(AGRESSIVE)
For example, the following example on STM8 shows some code where there is an
alias between a pointer and a variable. As the optimizer builds a common sub-
expression to make the pointer calculation code shorter, the stored sub-
expression is not updated when the variable is updated.
NOPROP(AGGRESSIVE) will fix this issue.
#include <stdio.h>
char c = 0;
char *pc=&c;
void main(void)
{
if(*pc)
printf("Error\n");
*pc += 1;
if(*pc != 1) // *pc is stored as a common subexpression
printf("Error\n");
while(1);
}
- 106 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: OBJECT(filename)
NOOBJECT
Description: Tells the compiler whether to generate an object file or not. If an argument is
specified the object file is generated in the specified path and with the specified
name. If no argument is specified the object file is generated in the project
directory, with the same name as the source file and extension .obj
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Examples: OBJECT
oj(myfile.obj)
nooj
Default: Active
- 107 -
ST7/STM8 C Compiler Manual
Syntax: OPTIMIZATION(0..3[,(SIZE|SPEED)])
Description: Defines the optimization type (size, for compact code, or speed, for fast execution)
and level (0=no optimization, 3= full optimization)
3: Peephole optimization.
Examples: OPTIMIZATION(2,SPEED)
o(3)
Default: O(3,SIZE)
- 108 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: OPTIMIZE(0..7[,(SIZE|SPEED)])
Description: Defines the optimization type (size, for compact code, or speed, for fast execution)
and level (0=no optimization, 7= full optimization)
Examples: OPTIMIZE(3,SPEED)
ot(7)
Default: OT(7,SIZE)
- 109 -
ST7/STM8 C Compiler Manual
Syntax: PATHINCLUDE("path")
Description: Add the specified path to the list of paths where include files are searched for.
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Default: None.
- 110 -
ST7/STM8 C Compiler Manual 6. Directives
Description: These directives allows generation of a preprocessor listing file only for some
sections of the source file.
Examples: PREPRINTON
PPON
ppoff
- 111 -
ST7/STM8 C Compiler Manual
Syntax: PREPRINT("filename")
Description: This directive tells the compiler to generate a preprocessor listing file, that is, a text
file containing the source after the preprocessor pass. It can be useful to debug
conditional compilation or complex macro problems. If filename is specified, the
file is generated with the specified name.
The specified path. If filename is not specified the file is generated in the same
directory as the C source file, with the same name and with the .i extension.
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Default: Inactive
- 112 -
ST7/STM8 C Compiler Manual 6. Directives
Description: These directives tell the compiler whether to produce a listing file or not. If
filename is specified, the file is generated with the specified name and in the
specified path. If filename is not specified the file is generated in the same
directory as the C source file, with the same name and with the .lst extension.
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Default: Active
- 113 -
ST7/STM8 C Compiler Manual
6.35 (NO)QUIET
Syntax: QUIET
NOQUIET
Description: When using the compiler from the command line, these directives instruct the
compiler about whether to display the number of the line being compiled or not.
Default: Inactive
- 114 -
ST7/STM8 C Compiler Manual 6. Directives
6.36 RESTORE
Syntax: RESTORE
Description: Restores the compiler options from the temporary memory. Used in conjunction
with SAVE it allows to use a set of options throughout the whole source file apart in
a section that can be compiled with a different set of options. Save and restore are
typically used when compiling a code that must be optimized for size with some
functions that must be optimized for speed.
Example:
#pragma OPTIMIZE(7,SIZE)
// Some size-optimized code here
#pragma SAVE
#pragma OPTIMIZE(7,SPEED)
// Some speed-optimized code here
#pragma RESTORE
Default: None.
- 115 -
ST7/STM8 C Compiler Manual
6.37 SAVE
Syntax: SAVE
Description: Saves the current compiler options in a temporary memory. Used in conjunction
with RESTORE it allows using a set of options throughout the whole source file
apart in a section that can be compiled with a different set of options. Save and
restore are typically used when compiling a code that must be optimized for size
with some functions that must be optimized for speed.
Example:
#pragma OPTIMIZE(7,SIZE)
// Some size-optimized code here
#pragma SAVE
#pragma OPTIMIZE(7,SPEED)
#pragma RESTORE
Default: None.
- 116 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: SPACEDEF(bit|page0|data|fdata|code|fcode,variable[,variable]…)
Description: Assigns specific memory spaces to some variables. Tells the compiler which
memory space should be used for specific variables. This can be helpful for
instance to place large tables in “data” memory space while all other variables are
in “page0”.
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Examples: SPACEDEF(code,myLargeTable,myLargeString,anotherString)
sd(page0,smallvariable)
- 117 -
ST7/STM8 C Compiler Manual
6.39 SRC
Syntax: SRC("filename")
Description: This directive tells the compiler to generate an assembler source file rather than
an object file directly. If filename is specified, the file is generated with the
specified name and in the specified path. If filename is not specified the file is
generated in the same directory as the C source file, with the same name and with
the .STM8 extension. The main use of the SRC directive is for mixing C and
assembler.
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Examples: SRC
Default: Inactive
- 118 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: STACKSPACEFOREXP(size)
Description: Specifies the size of the stack space allocated to Common Sub-Expression
storage.
In case you project uses lots of common subexpressions, the optimizer will store
them on stack. In case it runs out of stack space for Common subexpressions with
a Warning C258, you can take advantage of the STACKSPACEFOREXP directive
to enlarge the stack space dedicated to Common Sub-Expression storage.
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Examples: STACKSPACEFOREXP(20)
ssx(32)
Default: STACKSPACEFOREXP(16)
- 119 -
ST7/STM8 C Compiler Manual
Syntax: ST7SET
STM8SET(small|large)
Description: Specifies the compilation target type. RCSTM8 can generate ST7 or STM8 code
depending on these directives.
Additionally, the STM8SET directive specifies whether the compiler should
generate LARGE or SMALL model code:
LARGE model should be used for projects with code above the zero section i.e.,
with functions located within the 16 MB memory address range. All functions will
be called using the CALLF instruction and a 3 bytes (24 bits) address, and will
return using the RETF instruction.
SMALL model should be used for projects where all the code fits into the zero
section (function calls limited to a 64 KB address range). In this case functions will
be called with the CALL instruction with a 2 bytes (16 bits) address, and will return
with the RET instruction.
LARGE and SMALL directives should not be mixed within a project. All the files
should be compiled with the same memory model.
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Examples: ST7SET
STM8(LARGE)
Default: STM8SET
If STM8SET is specified without any memory model, SMALL is taken as default
- 120 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: STM8SSTACKSET0
STM8SSTACKSET1
Description: Tells the compiler to use a stack in page 0 (0x00-0xFF memory range) or in page
1 (0x100-0x1FF memory range). This setting depends on the ST7/STM8 device
you use. Most of them use a stack in page 1, but some models such as the
ultralite use a stack in page 0. Please refer to your device data sheet for detail.
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored.
Examples: STM8STACKSET0
stack1
Default: STM8SSTACKSET1
- 121 -
ST7/STM8 C Compiler Manual
Syntax: STRUCT_OPT
Make the names for struct, union and enum optional. This directive tells the
compiler to accept the C++ like syntax where the keywords struct, union and enum
are sometimes optional (ANSI C always requires them)
struct s
{
char a;
union
{
struct
{
char b_hi8;
char b_lo8;
} bytes;
Examples: SUE_OPT
struct_opt
Default: Inactive
- 122 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: SYMBOLS
Description: Include in the listing file a list of the symbols generated from the source file.
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Examples: SYMBOLS
SB
Default: Inactive
- 123 -
ST7/STM8 C Compiler Manual
Syntax: UNSIGNEDCHAR
SIGNEDCHAR
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Examples: UNSIGNEDCHAR
unsc
sc
- 124 -
ST7/STM8 C Compiler Manual 6. Directives
Syntax: WARNINGLEVEL(1|2)
Description: Defines what kind of warnings will be generated. One means only important
warnings will be shown, while two mean all warnings will be shown.
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored
Examples: WARNINGLEVEL(1)
wl(2)
Default: WARNINGLEVEL(2)
- 125 -
ST7/STM8 C Compiler Manual
Syntax: WARNINGRETVAL(integer)
Description: Defines the ERRORLEVEL value returned by RCSTM8 when there are warnings
(must be an integer number). This is especially useful when working with
makefiles or scripted builds.
This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored.
Note: ERRORLEVEL
RCSTM8 will generate an ERRORLEVEL value of 2 if any errors are encountered during the
compilation; an ERRORLEVEL value of 1 if any warnings are encountered; and an ERRORLEVEL
value of 0 if no warning or error was encountered.
Examples: WARNINGRETVAL(12)
wrv(0)
Default: WARNINGRETVAL(1)
- 126 -
ST7/STM8 C Compiler Manual 7. Libraries
7. Libraries
RCSTM8 comes with a set of libraries, including most ANSI C library objects and two STM8-specific
libraries (intrinsic functions and Hardware Registers).
Library Description
assert.h Diagnostic macros.
ctype.h Classifications for the basic character set.
errno.h Error handling.
float.h Floating type representations properties.
limits.h Integer type representations properties.
math.h Mathematic libraries.
setjmp.h Control transfer functions.
stdarg.h Functions with variable number of arguments.
stddef.h Types and macros of general use.
stdio.h Input/output operations.
stdlib.h Assortment of useful functions
- 127 -
ST7/STM8 C Compiler Manual
Additionally, the “%ridedir%\Lib\ST7\Startup” directory contains the STxxxx_s.OBJ files that contain the
various putchar versions adapted to specific devices:
Library Description
intrins.h Intrinsic functions
STM8xxx.h Definition of Hardware Registers
The rest of this chapter describes the STM8-specific libraries in detail. For the standard libraries, a
short overview of the library is provided with, where applicable, a list of differences compared to the C
standard.
- 128 -
ST7/STM8 C Compiler Manual 7. Libraries
instructions are either useless (example: NOP) or too complex (bit management, byte swapping), and
a typical user does not need to know or use the intrins.h library for a typical application.
All the functions included in intrins.h are generated in-line (as opposed to called via the CALL/RET
mechanism).
Function Description
_getCC_ Return the Condition Code register (CC) as a char.
_getSP_ Return the Stack Pointer register (value).
_halt_ Put the micro in halt mode.
_jmp_(unsigned short address) Jump to the specified address.
_nop_ No OPeration.
_rim_ Instruction RIM (enable interrupts).
_rsp_ Instruction RSP (reset stack pointer).
_setCC_ (unsigned char value) Set the Condition Code register (CC) to the given value.
_setSP_ (unsigned int value) Set the Stack Pointer register (SP) to a new value.
_sim_ Instruction SIM (disable interrupts).
_trap_ Instruction TRAP (software trap).
_wfe_ Instruction WFE (wait for event).
_wfi_ Instruction WFI (wait for interrupt).
_swapbyte_(unsigned short w) Swap bytes of the “w” word (16 bits).
_swapnibble_(unsigned char c) Swaps upper and low nibbles of the “c” byte.
Note: Functions
Functions contained in this library are inserted “in-line”. No call/ret sequence is generated by the
RCSTM8 compiler upon calling them.
- 129 -
ST7/STM8 C Compiler Manual
7.4.1 _getCC_
Description: Return the Condition Code register (CC). This can be used to check the interrupt
mask or some specific arithmetic/logic flags.
Examples: unsigned char CC;
CC = _getCC_();
if(CC & 0x02) // check Zero flag
... // Specific code for handling Zero case
- 130 -
ST7/STM8 C Compiler Manual 7. Libraries
7.4.2 _getSP_
Description: Return the current value of the Stack Pointer register (SP).
- 131 -
ST7/STM8 C Compiler Manual
7.4.3 _halt_
Description: Generates the HALT instruction. This puts the STM8 in halt mode, in order to save
power. The core and peripherals will be frozen until an NMI or a RESET. See the
STM8 datasheet for more details about halt mode.
- 132 -
ST7/STM8 C Compiler Manual 7. Libraries
7.4.4 _jmp_
The _fctptr_t type (pointer to function with no arguments, returning void) has been
defined to ease use of the _jmp_() function.
- 133 -
ST7/STM8 C Compiler Manual
7.4.5 _nop_
Description: Generates the NOP (No Operation) instruction. This can be used to generate
delays or to use up some code memory space in ROM.
- 134 -
ST7/STM8 C Compiler Manual 7. Libraries
7.4.6 _rim_
Description: Generates the RIM instruction. This clears the Interrupt mask of the CC register,
which enables interrupts. This is generally put in the main program, after the reset
routine, once all the desired interrupts have been properly configured.
- 135 -
ST7/STM8 C Compiler Manual
7.4.7 _rsp_
Description: Generates the RSP instruction. This resets the stack pointer to its initial value.
This instruction is usually put as the first instruction in the reset routine.
- 136 -
ST7/STM8 C Compiler Manual 7. Libraries
7.4.8 _setCC_
Description: Sets the Condition Code register (CC). This can be used to save/restore the CC
register.
- 137 -
ST7/STM8 C Compiler Manual
7.4.9 _setSP_
Description: Sets the Stack Pointer (SP) to a new value. Please ensure that the new SP value
is properly supported by your STM8 derivative.
- 138 -
ST7/STM8 C Compiler Manual 7. Libraries
7.4.10 _sim_
Description: Generates the SIM instruction. This sets the Interrupt mask of the CC register,
which disables interrupts. This is typically done before changing Hardware
Register settings in order to avoid receiving spurious interrupts.
This instruction is useless at the beginning of an interrupt/trap routine.
Examples: _sim_();
Setup_hardware();
_rim_();
- 139 -
ST7/STM8 C Compiler Manual
7.4.11 _swapbyte_
Description: Swap bytes of the given word (16 bits). This function performs an efficient
exchange of the two bytes from a given word.
- 140 -
ST7/STM8 C Compiler Manual 7. Libraries
7.4.12 _swapnibble_
Description: Swap nibbles of a byte. This task is performed using the SWAP assembly
instruction. The upper and lower nibbles of the given byte are swapped over and
the result of the operation is returned
- 141 -
ST7/STM8 C Compiler Manual
7.4.13 _trap_
Description: Generates the TRAP instruction. This generates a software trap, that is, a non
maskable software interrupt. See the STM8 datasheet for more details.
Note that you must properly set a trap function handler (using the “ trap” keyword)
before generating a trap. Failure to do so will lead to unpredictable behavior and
system crash.
- 142 -
ST7/STM8 C Compiler Manual 7. Libraries
7.4.14 _wfe_
Description: Generates the WFE (Wait for Event) instruction. This clears the interrupt flag,
allowing interrupts to be fetched. The CPU clock is then stopped until an interrupt
occurs. See the STM8 datasheet for more details.
This instruction is available on STM8 only.
- 143 -
ST7/STM8 C Compiler Manual
7.4.15 _wfi_
Description: Generates the WFI (Wait for Interrupt) instruction. This clears the interrupt flag,
allowing interrupts to be fetched. The CPU clock is then stopped until an interrupt
occurs. See the ST7 or STM8 datasheet for more details.
- 144 -
ST7/STM8 C Compiler Manual 7. Libraries
The versions distributed through the RKit-STM8 for Ride7 are enhanced (in minor ways) by
Raisonance in order to offer a good integration to Ride7.
If we take as an example the st7lite0.h header file, we can use the hardware registers definitions for
this specific device just by adding the following lines in our C files:
#include st7\st7lite0.h
This header file contains information relative to each peripheral implemented in the ST7LITE0:
//----------------------------------------------
// Register Declarations for the STM8LITE0
// Copyright (c) Raisonance SA, 1987-2007
//----------------------------------------------
- 145 -
ST7/STM8 C Compiler Manual
/*Interrupt registers */
at 0x37 hreg EICR ; /*External Interrupt Register */
The names used for the Hardware registers are the same (wherever possible) as those used in the
corresponding STM8 derivative datasheet, for easier reference.
Once you have included the header file specific to the derivative you are using, all its Hardware
Registers are immediately accessible from C, as shown in the following example:
void main(void)
{
_rsp_(); // Reset stack pointer for Reset case
periph_init(); // Initialize hardware
- 146 -
ST7/STM8 C Compiler Manual 7. Libraries
Description:
The fmemcpy (far memory copy) function copies n characters from the object pointed to by the 24-bit
memory address src to the 24-bit address dest (objects of size 65535 maximum, only available in
STM8 LARGE model).
Description:
The lmemcpy (large memory copy) function copies n characters from the object pointed to by the 16-bit
memory address src to the 16-bit address dest (objects of size 65535 maximum).
Description:
The lmemset (large memset) function copies the value of c into each of the first n characters of the
object pointed to by s.
- 147 -
ST7/STM8 C Compiler Manual
Description:
The lstrlen (large strlen)function computes the length of the string pointed to by s (size 65535
maximum).
Please refer to Chapter 4.8.1 “STM8 large memory model“ for advice about large memory
programming on STM8.
- 148 -
ST7/STM8 C Compiler Manual 7. Libraries
- 149 -
ST7/STM8 C Compiler Manual
8. Error messages
The compiler produces an error message any time it finds something it cannot understand or translate.
Error messages are classified according to their severity as follows:
● Warnings: a warning is generated any time the compiler finds something that it could compile,
but that looks somehow “strange”. The warning is intended to warn the programmer of a
potential error or problem; a typical example is the “if (a=b)” syntax, which, most of the time is
meant to be “if (a==b)“
● Errors: something the compiler cannot translate. Errors do not abort the source code analysis
process, but code generation is suspended.
● Fatal errors: errors that aborts immediately the compilation process. Usually a system-related
problem, such as a file that cannot be found.
● System errors: something wrong with system resources, such as file not found, disk full or
similar. Usually a fatal error. System errors are identified by the letter “S” before the error
number, like in “Fatal S001: file not found”
● Preprocessor errors: something wrong before the actual compilation (in the preprocessor).
Preprocessor Errors are identified by the letter “P” before the error number, like in “Error P002:
invalid character”
● Compiling errors: something the compiler cannot understand or translate. Compiling errors are
identified by the letter “C” before the error number, like in “Error C006: symbol defined more
than once”
The tables below summarize errors that can be generated by RCSTM8: System, preprocessor or
compilation errors.
- 150 -
ST7/STM8 C Compiler Manual 8. Error messages
- 151 -
ST7/STM8 C Compiler Manual
- 152 -
ST7/STM8 C Compiler Manual 8. Error messages
- 153 -
ST7/STM8 C Compiler Manual
- 154 -
ST7/STM8 C Compiler Manual 8. Error messages
- 155 -
ST7/STM8 C Compiler Manual
- 156 -
ST7/STM8 C Compiler Manual 8. Error messages
Resolution: Check the existence of the file, its extension and its access path
Resolution: Check the file permissions. Verify that the file is not in use in another application.
Resolution: Check the file permissions. Verify that the file is not in use in another application.
Resolution: Check your directives and the options. Refer on chapter 6 for more details
Resolution: Change the extension or make sure you are using the right file
Resolution: Check the existence of the file, its extension and the path to access it.
Resolution: Recompile
- 157 -
ST7/STM8 C Compiler Manual
Example: #define a 10
void foo(void)
{
int b=a;
#undef
b+10;
}
Resolution: Make sure you specified a macro name when using the undef directive
Example: #definedd a 10
extern int b;
void foo(void)
{
- 158 -
ST7/STM8 C Compiler Manual 8. Error messages
int b=a;
}
Resolution: Make sure you are using an existing directive. Check the spelling of the directive
you are using.
Example: #include
extern int b;
void foo(void)
{
b=10;
}
Resolution: Make sure you specified the file name each time you use the include directive.
#endif
Resolution: Make sure all your endif directives are matching an if or elif directive.
Resolution: Make sure all your else directives are matching a if or elif directive.
- 159 -
ST7/STM8 C Compiler Manual
Resolution: Make sure all your else directives are matching an if or elif directive.
void foo(void)
{
int a=5,b=10,c=2;
EQUAL(a,b,c);
a=b+c;
}
Resolution: Make sure you are using the right macro and you have specified the right
parameters.
- 160 -
ST7/STM8 C Compiler Manual 8. Error messages
void foo(void)
{
int a=5,b=10,c=2;
EQUAL(a,b);
a=b+c;
}
#define FOO 5
extern int a;
void foo(void)
{
int b=2;
a=b+1;
}
Resolution: Make sure there are no conflicts between the definition on the command line and
the one specified within your code.
- 161 -
ST7/STM8 C Compiler Manual
Resolution: Make sure your octal number is composed of numbers include between 0 and 7
Resolution: Make sure the type of the variable you are using can contain the number you want
to keep in it. The minimum and maximum that can be contained by all types are
specified in the include file limits.h
Resolution: Change the character after the “\x” or change the “\x”
- 162 -
ST7/STM8 C Compiler Manual 8. Error messages
int a;
#endif
Example: ENUMTYPE(LONG)
Resolution: Make sure you use the pragma correctly. Check if the pragma needs parameters,
and if so, which type of parameters, which range …
Example: #define
Example: #error
- 163 -
ST7/STM8 C Compiler Manual
Instead of
#define fct(a,b) my_fct(a,a)
8.35 ERROR P031: Macro body cannot start or end with '##'
Cause: The body of a macro cannot start or end with ‘##’
Resolution: Make sure that an #include statement with its file is alone on its line.
- 164 -
ST7/STM8 C Compiler Manual 8. Error messages
Example: void fct(struct foo X); // Error if “struct foo” is not previously defined
- 165 -
ST7/STM8 C Compiler Manual
Example: void fct(enum foo X); // Error if “struct foo” is not previously defined
Resolution: Remove one of the declarations, or make them use the same type.
- 166 -
ST7/STM8 C Compiler Manual 8. Error messages
void main(void)
{
foo(2); // Error C008: function foo does not accept parameters
while(1);
}
Resolution: Change either the function prototype or the function call in such a way that the
number and type of parameters match
void main(void)
{
foo(); // Error C009: function foo requires 1 parameter
while(1);
}
Resolution: Change either the function prototype or the function call in such a way that the
number and type of parameters match
Example: int fct(a,b) int c; {;} // a or b would work, c is not in the list
- 167 -
ST7/STM8 C Compiler Manual
Resolution: In C, all symbols (variables) must be defined before they can be used
int aa[5];
aa[3][1]=2; // aa is an array with one index only
Resolution: Declare the variable as an array or use another variable. Remove the extra index.
do_nothing();
do_something();
}
- 168 -
ST7/STM8 C Compiler Manual 8. Error messages
Example: int i;
i.x=1;
Resolution: Change the left side of the operator in order to have a structure or a union object
or remove the . or the -> operator
foo->3 = 2;
Resolution: Change the name of the field you are trying to access
Example: struct {
int a;
char b;
} hh;
void main(void)
{
hh.c=3; // c is not a field of the structure
while(1);
}
- 169 -
ST7/STM8 C Compiler Manual
void bar()
{
int a;
int b;
foo(a, b);
}
Resolution: Check the type of each parameter with the function prototype and change the
parameter that do not have the correct type and which cannot be convert
Resolution: Either you do not perform the right operation, in which case change the operation,
or you do not apply your operation to the right operand, thus change the operand.
foo > i;
- 170 -
ST7/STM8 C Compiler Manual 8. Error messages
void main(void)
{
if(~a) // Error C031: Type "float" invalid for "~" operator
printf("Float is null");
}
Resolution: Change the operator and/or the operand, or perform a type cast on the operand.
Example: aa = 1 / 0;
Example: struct
{
float a:7;// float cannot support bit fields
char c;
} hh;
Example: struct
{
int a:9; // max is 8!
char c;
} hh;
- 171 -
ST7/STM8 C Compiler Manual
Example: N/A
Resolution: You can program the number of errors to be accepted before the fatal error C036
is generated with the directive MAXERR (see Chapter 6)
Example: N/A
Resolution: You can program the number of errors to be accepted before the fatal error C037
is generated with the directive MAXWAR (see Chapter 6)
Resolution: Check the operands of your operation and verify that the operation can be
performed and that implicit conversions are legal. Be careful to memory space
pointed by pointers
- 172 -
ST7/STM8 C Compiler Manual 8. Error messages
Resolution: Check with the latest version of the tools. If the problem persists, contact
support@raisonance.com. Send your project if possible, then wait for a fix or
workaround
Example: if (1==4)
{
... // Code here will never be executed
}
Resolution: You can remove the if-else statement and just let the false case since the
condition is always false
Resolution: You can remove the if-else statement and just let the true case since the condition
is always true
Resolution: Make sure you call the right function or that the function is correctly declared
Resolution: Change the type or the initialization value in such a way that they are consistent
- 173 -
ST7/STM8 C Compiler Manual
Example: 3 = 2;
Example: int* a;
char* b;
void foo()
{
b != a;
}
Resolution: Make sure your operand have the correct types. Make sure your comparison is
relevant. You can add explicit cast.
a++;
Resolution: Make sure you are trying to modify the right object.
- 174 -
ST7/STM8 C Compiler Manual 8. Error messages
Example: at 0 typedef a;
at 0 extern int b=0;
Note:
The keyword is a storage specifier.
Example: int i;
int a[i];
Example: N/A
- 175 -
ST7/STM8 C Compiler Manual
Cause: Using a function specifier that needs an integer parameter, you specify a
parameter which is out of the allowed range
8.88 WARNING C063: Undefined operation for generic pointers (char* assumed)
Cause: You are doing an operation on a void pointer, but this operation is not possible.
The pointer is considered as a char pointer in order to do the operation
- 176 -
ST7/STM8 C Compiler Manual 8. Error messages
Resolution: Remove the continue or the break statement or put a loop instruction to enclosed it
Resolution: Remove the return statement or change the type of the function from void to
something else
- 177 -
ST7/STM8 C Compiler Manual
Resolution: Correct the expression syntax. It can sometimes be difficult to understand where
the problems is exactly
- 178 -
ST7/STM8 C Compiler Manual 8. Error messages
if(a > 3)
goto 2;
}
Resolution: Remove the register qualifier from the variable declaration or do not retrieve the
address of the variable
Example: int;
int a = 0;
- 179 -
ST7/STM8 C Compiler Manual
Resolution: Change the type of one of the pointer (the const pointer to non-const or the
generic pointer to const) or remove the initialization
void foo(int i)
{
enumer = i;
}
- 180 -
ST7/STM8 C Compiler Manual 8. Error messages
Example: struct
{
int a:0; // not allowed
char c;
} bf;
Resolution: Add a return statement returning something of the right type or change the type of
the function to void
Resolution: A function should always be defined before being called (at least the prototype).
This error can be due to a missing include files (typically in the case of a library
function), a missing function prototype or a function prototype placed after its first
called in the file.
- 181 -
ST7/STM8 C Compiler Manual
- 182 -
ST7/STM8 C Compiler Manual 8. Error messages
Example: char fct(void) { }; // Body of the function should contain a return statement
Resolution: Add a return statement or change the type of the function to void
Example: goto bar; // Error if “bar:” is not found within the function
Lab:
a += b;
}
Resolution: Move your label before the initialization of local variable or move the jump after the
initialization of the local variable.
- 183 -
ST7/STM8 C Compiler Manual
int *a = &(myBitField.a);
Resolution: Do not try to access the address of a bitfield or of a variable declared with the
register type qualifier
Resolution: Some directives and controls must be in well defined positions (usually at the
beginning of the file, see chapter 6 for the details).
The error can be due to a misused keyword: in that make sure you use it correctly.
- 184 -
ST7/STM8 C Compiler Manual 8. Error messages
Resolution Check the declaration of each parameter and make sure they are valid.
if (a=b)
return 1;
Resolution: Make sure you really want to do the assignation as it is written. If not, correct it.
Example: int a; }
generic_ptr=code_ptr;
Resolution: Try to not mix generic and space-qualified pointers, mixing them is not a
suggested practice
- 185 -
ST7/STM8 C Compiler Manual
Resolution: The value of a void expression shall not be used in any way. The void type can be
used only as a pointer and it’s referenced object cannot be accessed if it has not
been previously cast in another type.
Thus, in the preceding example, having called function foo() this way:
int a = 3;
foo((void*)&a);
Resolution: Make sure you call the compiler with valid options. For each option, make sure that
the option is correctly used (the option might not have a value, the value might be
in a given range, …)
or
#pragma OT
Resolution: Make sure the options or the pragma you are using are correctly used and their
parameters are present
- 186 -
ST7/STM8 C Compiler Manual 8. Error messages
or
#pragma OT(4
Resolution: Make sure that the pragmas or the options are correctly written
or
#pragma OT(9)
Resolution: Make sure the pragmas and options are used with a parameter that is in the range
of the accepted values.
Resolution: See chapter 6 to find out what the legal values are for each directive.
or
#pragma OT()
- 187 -
ST7/STM8 C Compiler Manual
Resolution: Use a valid pragma (see chapter 6 for the list) or remove the line
Example: N/A
Resolution: See Chapter 6 for an example of how to use the ASM/ENDASM directives
Resolution: Move the pragma at the beginning of the file, before any statement
Example: nt foo(int a)
{
#pragma NOIS
if (a++)
return 1;
return 0;
}
Resolution: Place the directive at the correct place. Some directives need to be at a specific
location. Please refer to chapter 6 for more details.
- 188 -
ST7/STM8 C Compiler Manual 8. Error messages
- 189 -
ST7/STM8 C Compiler Manual
Resolution: Make sure the numeric value used is a correct integer expression. In the example
below 0x2f would be correct
Example: struct
{
int *a:7;
char c;
} *hh;
hh->a;
Resolution: Remove the pointer and access to the field another way
Resolution: Change the function so that it does not have any parameters and do not return any
value
- 190 -
ST7/STM8 C Compiler Manual 8. Error messages
Example: struct
{
bit r; // not allowed
int a;
char c;
} hh;
Resolution: In the definition of a structure type, this structure type must not be used if it is not
used as a pointer.
Resolution: If there function effectively do not have parameter, remove what comes after the
void. Or else, you might have forgot the * after the void.
- 191 -
ST7/STM8 C Compiler Manual
Resolution: Remove the void keyword or add a name to the parameter and change the type.
foo bar(void);
Resolution: You should probably return a function pointer instead. In the example above, the
"foo" type should preferably be defined as a pointer to function, such as:
Example: struct
{
char f;
int g;
} a, b;
a!=b;
Resolution: You can compare pointers to structures/unions but not the object itself, or else you
must compare field to field.
- 192 -
ST7/STM8 C Compiler Manual 8. Error messages
Resolution: Either you change the type of the tag or you change the way you are using it. Apply
to the example below this would means either:
or
Resolution: Either you change the type of the tag or you change the way you are using it. Apply
to the example below this would means either:
or
- 193 -
ST7/STM8 C Compiler Manual
Example: struct
{
int a;
char a; // a has already been defined
} hh;
Resolution: You can’t perform an implicit or an explicit conversion from a void expression.
Change your code so that no conversion is performed on a void expression.
In the example below, a possible resolution would be to change the type of the
function parameter from void* to char*
Resolution: Use a constant or move your initialization into a function using an assignment
Resolution: Change the size of the string so that there is enough place for the initializer
- 194 -
ST7/STM8 C Compiler Manual 8. Error messages
Example: struct
{
int data a; // “data” is not allowed here
char b;
} hh;
Example: N/A
Example: N/A
Resolution: Make sure that your source file is at the location you are indicate to the compiler.
Make sure that all your include file are either in an absolute path or pointed by the
path indicated by the PIN directive
void bar()
{
*foo;
}
- 195 -
ST7/STM8 C Compiler Manual
Example: at 0x80^9 data bit foo; // Error C163: Only bits 0-7 allowed
8.172 WARNING C165: Absolute address does not match the memory space
Cause: An absolute specifier makes an object going off bounds of its storage area.
Example: at 0xE000 data char tab[10000]; // tab goes higher than 0xFFFF
Resolution: Locate the object at an address that fits your device's memory mapping
Resolution: Use bit type without using it as a pointer or use a char pointer
- 196 -
ST7/STM8 C Compiler Manual 8. Error messages
8.175 WARNING C171: Bit(s) not allowed as local variable (char type assumed)
Cause: Bits are not allowed as local variables Local variables declared as “bit” will be
handled as “char”.
Resolution: Put your bits in data memory (the limit depends on the derivative you are using)
Example: struct
{
int a;
char b(); // functions not allowed!
} hh;
- 197 -
ST7/STM8 C Compiler Manual
Example: char a;
int *b;
a = b;
Resolution: Change the expression to eliminate the implicit pointer conversion or cast the
pointer
- 198 -
ST7/STM8 C Compiler Manual 8. Error messages
Resolution: Correct the prototypes is such a way that they are all the same
Resolution: Correct the declarations of the given variable so that they match.
Example:
8.187 WARNING C192: Assignment between pointers to char signed and unsigned
Cause: You are an assignation between a signed and an unsigned char
a = b;
Resolution: Explicitly cast your second operand into the type of the first operand.
- 199 -
ST7/STM8 C Compiler Manual
a1 = a2;
Resolution: Change your variables so that they have the same type or explicitly cast the
generic pointer into the qualified type
- 200 -
ST7/STM8 C Compiler Manual 8. Error messages
Resolution: As “data” is the largest memory space you need to use less variables to be able to
compile your source. Check the dimension of the arrays first.
Resolution: Remove some of the variables of type bit and convert them to unsigned chars.
- 201 -
ST7/STM8 C Compiler Manual
void foo(void)
{
b = a;
}
Resolution: Ensure that you are always storing a pointer in a pointer of large size. For instance,
a "data *" or a "code *" pointer is 2-bytes large; a "page0 *" pointer is 1-byte large;
and a "fdata *" (far data) is 3-bytes large (24-bits addressing).
Please refer to Chapter 4 “Programming Examples” for further detail about pointer sizes.
void main(void)
{
foo(constptr); // Error 204: const to non-const assignment
}
foo((char*)constptr); // OK
● If the function that processes you pointer does not functionally modify your pointer, you can
qualify the pointer argument as a const pointer. This improves your code quality, as your C
compiler will ensure that you do not perform any potential modification to your pointer in the
function's code. For example, in the code fragment above, replace the declaration of foo() by:
- 202 -
ST7/STM8 C Compiler Manual 8. Error messages
8.195 WARNING C205: BIT(s) not allowed as automatic variables (char type
assumed)
Cause: The bit is not allowed as an automatic variable. Type char is assumed
void my_funct(void)
{
bit a;
}
Resolution: Change the type for a char or do not use the automatic mode
a = b;
Resolution: Explicitly cast one of your operands in order to make this warning disappear
8.198 WARNING C221: BIT(s) not allowed as parameter(s) (char type assumed)
Cause: You defined a function that takes an argument of type "bit".
Resolution: This case is automatically handled by the RCSTM8 Compiler. The parameter will
have its type changed from "bit" to "char".
- 203 -
ST7/STM8 C Compiler Manual
#include “file1.h”
…
-content of file1.h:
#include “file1.h”
…
#include “file1.h”
…
-content of file1.h:
#include “file2.h”
…
-content of file2.h:
#include “file1.h”
…
8.201 WARNING C231: ‘Typedef’ and memory space cannot go together. Memory
Space ignored
Cause: Memory spaces cannot be applied using a typedef
- 204 -
ST7/STM8 C Compiler Manual 8. Error messages
Example: double a;
Notes: The double type is automatically handled as float, so even if warning C251 is
emitted, the compiled code will be fully functional.
This warning is only emitted once per compilation unit, even though there are
multiple uses of the “double”
- 205 -
ST7/STM8 C Compiler Manual
Example:
void foo(void) reentrant // Will hold locals on stack
{
long ltab[50]; // 50*4 = 200 bytes on stack
short stab[25]; // 25*2 = 50 bytes on stack
char ctab[8]; // C253 more than 256 bytes!
}
Alternatively, you can define the local variables as “static”, so that they are
allocated in RAM rather than on stack.
In STM8 mode the LARGESTACK directive lets the compiler use a 16-bits stack
model, although the code generated will be far greater than with the standard
stack access.
8.207 WARNING C255: Indirect calls allowed only for reentrant functions
Cause: You attempted to indirectly call a non-reentrant function. Reentrant and non-
reentrant functions do not share the same parameters-passing conventions, so
there is a risk that the indirect call is misinterpreted during execution.
The Compiler will emit a warning in cases where the parameter passing
convention makes it possible to misinterpret both function forms.
Please refer to Chapter 3 “Compiler internals, section Parameter passing conventions” for details.
If you just need to get the offset of a struct member, please use the offsetof()
macro available from the <stddef.h> header file.
- 206 -
ST7/STM8 C Compiler Manual 8. Error messages
8.209 WARNING C257: Function recursively calls itself without having the 'reentrant'
qualifier
Cause: A function recursively calls itself, although it does not have the 'reentrant qualifier.
This may cause erroneous behavior, as the parameters and local variables are
'static', hence all instances of the function will modify the local variables values for
all other instances.
Resolution: Use the “stacksizeforexp” directive to declare a larger space on stack for sub-
expressions.
8.211 WARNING C259: Object larger than 256 bytes in smallobject mode.
Cause: The compiler has detected that an object such as an array or a structure has been
declared with a size larger than 256 bytes, although the “smallobject” directive is
active.
In some cases, the Compiler may generate invalid code in such cases, for
example when using indirect access to an array member.
Resolution: Use the “largeobjects” directive so that the Compiler properly handles objects
larger than 256 bytes.
- 207 -
ST7/STM8 C Compiler Manual
8.212 ERROR C262: Function at absolute address above 0xFFFF in SMALL model
Cause: When in SMALL memory model, calling an absolute function located at an address
above 0xFFFF will generate the Error C262.
In SMALL memory model, a CALL instruction will be used to perform the function
call, but the section number will not be changed.
void main(void)
{
farfct();
}
Resolution: Switch to LARGE memory model, so that a FCALL instruction will be used to
perform function calls.
- 208 -
ST7/STM8 C Compiler Manual 9. Glossary
9. Glossary
ANSI RLSTM8
American National Standards Institute The command used to link objects files and
libraries using the Linker locator.
Argument
LIBOMF
The value that is passed to a macro or function
The command used to manipulate library files
using the Library Manager
ASCII (American Standard Code for
Information Interchange)
Library
This is a set of 256 codes used by computers to
represent digits, characters, punctuation and A file that stores a number of possibly related
other special symbols. object modules. The linker can extract modules
from the library to use in building a target object
file.
Block
A sequence of C statements, including LSB
definitions and declarations, enclosed within
braces. Least significant bit or byte.
RCST7 Macro
The command used to compile programs using An identifier that represents a series of
the ST7/STM8 C Compiler (deprecated) keystrokes that is defined using the #define
preprocessor directive.
RCSTM8
MSB
The command used to compile programs using
the ST7/STM8 C Compiler Most significant bit or byte.
Directive Object
An instruction to the C preprocessor or a control An area of memory that can be examined.
switch to the C Compiler. Usually used when referring to the memory area
associated with a variable or function
Include file
Object file
A text file that is incorporated into a source file
using the #include preprocessor directive. A file created by the compiler, that contains the
program segment information and relocatable
machine code.
Keyword
A reserved word with a predefined meaning on Operator
the computer
A symbol (eg. +, -, *, /) that specifies how to
manipulate the operands of an expression.
RLST7
The command used to link objects files and
libraries using the Linker locator (deprecated).
- 209 -
ST7/STM8 C Compiler Manual
Pragma Static
A statement that passes an instruction to the A storage class that, when used with a variable
compiler at compile time. declaration in a function, causes variables to
retain their value after exiting the block or
function in which they are declared.
Preprocessor
The compiler's first pass text processor that String
manipulates the contents of a C file. The
preprocessor defines and expands macros, An array of characters that is terminated with a
reads include files, and passes control null character ('\0').
directives to the compiler.
- 210 -
ST7/STM8 C Compiler Manual 9. Glossary
Alphabetical Index
Absolute............................................................19 NODUPLICATESTRING...................................90
ASM..................................................................80 NOFARPOINTERS...........................................92
Assembly...........................................................27 NOINITSTATICVAR..........................................96
AUTO................................................................81 NOINRAM.........................................................98
Automatic variables...........................................49 NOINSECTION0...............................................97
CODE................................................................83 NOINTVECTOR................................................99
COND................................................................84 NOLARGESTACK...........................................101
DEBUG.............................................................85 NOLISTINCLUDE............................................102
DEFAULTGC....................................................86 NOOBJECT.....................................................107
DEFAULTLC.....................................................87 NOPRINT........................................................113
DEFINE.............................................................88 NOPRINTOFFSET..........................................105
DEFINEJOKER.................................................89 NOPROPAGATION.........................................106
Directives..........................................................77 OBJECT..........................................................107
DUPLICATESTRING.........................................90 OPTIMIZATION ..............................................108
Dynamic memory (malloc/free).........................63 OPTIMIZE.......................................................109
ENDASM...........................................................80 Parameters passing conventions......................24
Endian...............................................................31 PATHINCLUDE...............................................110
ENUMTYPE......................................................91 Predefined macros and symbols.......................36
Error................................................................150 PREPRINT......................................................112
FARPOINTERS.................................................92 PREPRINTOFF...............................................111
Fast 16 x 16 → 32-bits multiplication.................53 PREPRINTON.................................................111
Fast floating-point libraries................................34 PRINT.............................................................113
Floating-point ....................................................30 Quick Configuration Word.................................37
GENERATEDEPFILE........................................93 QUIET.............................................................114
GLOBAL............................................................94 RCSTM8...........................................................18
Help menu.........................................................17 Recursion..........................................................60
IEEE-754...........................................................30 Reentrant..........................................................60
In-line assembly................................................40 Reset.................................................................41
INCLUDE..........................................................95 RESTORE.......................................................115
Infinity................................................................33 Ride...................................................................19
Initialization........................................................73 SAVE...............................................................116
INITSTATICVAR...............................................96 SIGNEDCHAR................................................124
inram.................................................................55 SMALLOBJECT..............................................100
INRAM...............................................................98 SPACEDEF.....................................................117
Inram functions..................................................54 SRC.................................................................118
INSECTION0.....................................................97 ST Firmware Libraries.....................................145
Interrupts...........................................................43 ST7SET...........................................................120
Intrinsic functions...............................................59 STACKSIZEFOREXP......................................119
INTVECTOR.....................................................99 Startup...............................................................41
Keywords...........................................................69 STM8SET........................................................120
LARGEOBJECT..............................................100 STM8STACKSET0..........................................121
LARGESTACK................................................101 STM8STACKSET1..........................................121
Libraries............................................................20 STRUCT_OPT................................................122
LISTINCLUDE.................................................102 SYMBOLS.......................................................123
MAXERR.........................................................103 Trap...................................................................43
MAXWAR........................................................103 UNSIGNEDCHAR...........................................124
MODANSI.......................................................104 WARNINGLEVEL............................................125
NaN...................................................................33 WARNINGRETVAL.........................................126
NOAUTO...........................................................81 Web site............................................................17
NOCOND..........................................................84 %ridedir%..........................................................16
NODEBUG........................................................85
- 211 -
ST7/STM8 C Compiler Manual
History
Author Date Modification
Bruno Richard October 2008 Explained differences between SIZE and SPEED optimizations of
the Compiler.
Updated the list of errors, providing some examples where useful.
Reformatting.
Described the new built-in macro __MODEL__.
Described the NOPROPAGATION(aggressive) directive, which
devalidates some potentially harmful optimizations.
ENUMTYPE is “int” by default, not “char”.
Removed deprecated PAGELENGTH and PAGEWIDTH.
Added ANSI mode to the quick configuration word.
Changed C165 message level from an error to a warning.
Bruno Richard November 2008 Documented the differences between ST7 and STM8 interrupt
vectors.
Added some information about how to use the assembler version
of putchar/getchar.
Bruno Richard February 2009 Documented internal handling of the -0.0 and NaN IEEE-754
floating-point values.
Documented warning C259.
Removed deprecated error C196.
Documented the fast floating-point libraries.
Added a reference about the ST firmware libraries.
Bruno Richard March 2009 Documented the FASTLIBRARY linker directive for fast floating-
point.
- 212 -
ST7/STM8 C Compiler Manual 9. Glossary
Bruno Richard October 2009 Corrected some references about intrinsic functions.
Quick Config Word documentation corrected (Compiler mode and
default local/global memory class were incorrect)
Bruno Richard December 2009 Described the new behavior of the STRUCT_OPT directive.
New warning C261.
Removed references to the deprecated directive EJECT.
Bruno Richard February 2010 Updated the auto-relocation information to accurately match
Ride7's behavior.
Documented automatic detection of enum sizes when in
ENUMTYPE(char) mode.
Removed error C260, as enumeration values can now be greater
than 256 when using ENUMTYPE(BYTE).
EEPROM variables without any initializer will not have any
initialization code in the HEX file.
Bruno Richard March 2010 Warning C261 is now Error C262 (Absolute function above
address 0xFFFF in SMALL memory model).
Added Warning C187.
Bruno Richard July 2010 Added information about the dynamic memory manager (malloc,
calloc, realloc, free).
Bruno Richard August 2010 Corrected the 16x16 → 32 fast long multiplication information:
Now both signed and unsigned versions can be activated, under
automatic compiler control.
Corrected bit 17 of Quick Configuration Word.
- 213 -
ST7/STM8 C Compiler Manual
Disclaimer
Disclaimer
Information in this document is subject to change without notice and does not represent a commitment on
the part of the manufacturer. The software described in this document is provided under license and may
only be used or copied in accordance with the terms of the agreement. It is illegal to copy the software onto
any medium, except as specifically allowed in the license or nondisclosure agreement.
No part of this manual may be reproduced or transmitted in any form or by any means, electronic or
mechanical, including photocopying, recording, or information storage and retrieval systems, for any
purpose other than the purchaser’s personal use, without prior written permission.
Every effort has been made to ensure the accuracy of this manual and to give appropriate credit to persons,
companies and trademarks referenced herein.
This manual exists both in paper and electronic form (pdf).
Please check the printed version against the .pdf installed on the computer in the Ride installation directory,
for the most up-to-date version.
The examples of code used in this document are for illustration purposes only and accuracy is not
guaranteed. Please check the code before use.
- 214 -