0% found this document useful (0 votes)
170 views

RCSTM8

Uploaded by

iukhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
170 views

RCSTM8

Uploaded by

iukhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 214

ST7/STM8

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

3.4 Function return values................................................................................................................... 27


3.5 Data types...................................................................................................................................... 28
3.6 Bit-fields......................................................................................................................................... 28
3.7 Object storage rules....................................................................................................................... 30
3.8 IEEE-754 floating-point support..................................................................................................... 30
3.8.1 Endian notice.......................................................................................................................... 31
3.8.2 Representation....................................................................................................................... 31
3.8.3 IEEE-754 denormalized values............................................................................................... 32
3.8.4 Specific IEEE-754 values -0.0, ±INF and NaN........................................................................32
3.8.5 Fast floating-point libraries...................................................................................................... 34
3.9 Recursive and reentrant functions support....................................................................................35
3.10 Predefined macros and symbols.................................................................................................. 36
3.11 Quick Configuration Word (QCW)...............................................................................................37

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

4.10.2 Writing inram functions......................................................................................................... 55


4.10.3 Calling an inram function...................................................................................................... 56
4.10.4 Basic inram function example...............................................................................................56
4.10.5 Manual copy of an inram function from Flash to RAM..........................................................57
4.10.6 Overlaying several inram functions.......................................................................................58
4.11 Intrinsic functions......................................................................................................................... 59
4.12 Specific library functions.............................................................................................................. 60
4.13 Volatile variables declaration....................................................................................................... 60
4.14 Recursion and re-entrancy........................................................................................................... 60
4.14.1 Library functions reentrancy.................................................................................................. 60
4.15 Using the dynamic memory manager..........................................................................................63
4.15.1 Dynamic memory manager example....................................................................................64
4.16 Using I/O functions...................................................................................................................... 65
4.16.1 Optimized printf and sprintf variants......................................................................................65
4.16.2 Providing your own putchar function.....................................................................................65
4.17 Using switch statements.............................................................................................................. 67

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

6.2 AUTO (au) / NOAUTO (noau)........................................................................................................ 81


6.3 (NO)CLEANIRQDIVFLAG............................................................................................................. 82
6.4 CODE............................................................................................................................................ 83
6.5 COND (co) / NOCOND (noco)....................................................................................................... 84
6.6 DEBUG (db) / NODEBUG (nodb).................................................................................................. 85
6.7 DEFAULTGC (dgc)........................................................................................................................ 86
6.8 DEFAULTLC (dlc).......................................................................................................................... 87
6.9 DEFINE (df)................................................................................................................................... 88
6.10 DEFINEJOKER (defj).................................................................................................................. 89
6.11 DUPLICATESTRING (ds) / NODUPLICATESTRING (nods).......................................................90
6.12 ENUMTYPE (et).......................................................................................................................... 91
6.13 FARPOINTERS (fp) / NOFARPOINTERS (nofp).........................................................................92
6.14 GENERATEDEPFILE (gendep)................................................................................................... 93
6.15 GLOBAL (glb).............................................................................................................................. 94
6.16 INCLUDE (i)................................................................................................................................. 95
6.17 INITSTATICVAR (is) / NOINITSTATICVAR (nois).......................................................................96
6.18 INSECTION0 (in0) / NOINSECTION0 (noin0).............................................................................97
6.19 INRAM (ir) / NOINRAM (noir)....................................................................................................... 98
6.20 INTVECTOR (iv) / NOINTVECTOR(noiv)....................................................................................99
6.21 LARGEOBJECT (laob) / SMALLOBJECT (smob).....................................................................100
6.22 LARGESTACK (lstk) / NOLARGESTACK (nolstk).....................................................................101
6.23 LISTINCLUDE (lc) / NOLISTINCLUDE (nolc)............................................................................102
6.24 MAXERR / MAXWAR................................................................................................................ 103
6.25 MODANSI (ansi)........................................................................................................................ 104
6.26 NOPRINTOFFSET (noprof)....................................................................................................... 105
6.27 NOPROPAGATION (noprop)..................................................................................................... 106
6.28 OBJECT (oj) / NOOBJECT (nooj)..............................................................................................107
6.29 OPTIMIZATION (o).................................................................................................................... 108
6.30 OPTIMIZE (ot)........................................................................................................................... 109
6.31 PATHINCLUDE (pin)................................................................................................................. 110
6.32 PREPRINTON (ppon) / PREPRINTOFF (ppoff)........................................................................111
6.33 PREPRINT (pp)......................................................................................................................... 112
6.34 PRINT (pr) / NOPRINT (nopr).................................................................................................... 113
6.35 (NO)QUIET................................................................................................................................ 114
6.36 RESTORE................................................................................................................................. 115
6.37 SAVE......................................................................................................................................... 116
6.38 SPACEDEF (sd)........................................................................................................................ 117
6.39 SRC........................................................................................................................................... 118
6.40 STACKSIZEFOREXP (ssx)....................................................................................................... 119

-5-
ST7/STM8 C Compiler Manual

6.41 ST7SET (st7) / STM8SET (stm8).............................................................................................. 120


6.42 STM8STACKSET0 (stack0) / STM8STACKSET1 (stack1).......................................................121
6.43 STRUCT_OPT (sue_opt)........................................................................................................... 122
6.44 SYMBOLS (sb).......................................................................................................................... 123
6.45 UNSIGNEDCHAR (unsc) / SIGNEDCHAR (sc).........................................................................124
6.46 WARNINGLEVEL (wl)............................................................................................................... 125
6.47 WARNINGRETVAL (wrv).......................................................................................................... 126

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.39 ERROR C001: Specifier not allowed in the context...................................................................165


8.40 ERROR C002: Too many types................................................................................................. 165
8.41 ERROR C003: Undefined structure or union.............................................................................165
8.42 ERROR C004: Undefined enumeration.....................................................................................166
8.43 ERROR C005: Type error in re-declaration...............................................................................166
8.44 ERROR C006: Symbol defined more than once........................................................................166
8.45 ERROR C007: Invalid type in definition......................................................................................166
8.46 ERROR C008: Parameter list longer than the prototype............................................................167
8.47 ERROR C009: Parameter list smaller than the prototype..........................................................167
8.48 ERROR C010: not a parameter................................................................................................. 167
8.49 ERROR C011: Invalid syntax..................................................................................................... 167
8.50 ERROR C014: Undefined symbol.............................................................................................. 168
8.51 ERROR C015: unary operator not followed by an operand........................................................168
8.52 ERROR C017: Cast not followed by an operand.......................................................................168
8.53 ERROR C018: variable cannot be subscripted (not an array)....................................................168
8.54 WARNING C020: '%s' is assigned a value that is never used...................................................168
8.55 WARNING C021: Type is defined more than once....................................................................169
8.56 ERROR C022: '%s' requires a structure or union......................................................................169
8.57 ERROR C023: A field must be an identifier...............................................................................169
8.58 ERROR C024: Unknown field.................................................................................................... 169
8.59 ERROR C026: Wrong argument type........................................................................................170
8.60 ERROR C027: 'generic' is not legal in this context.....................................................................170
8.61 ERROR C029: requires integer operand....................................................................................170
8.62 ERROR C030: requires arithmetic operand...............................................................................170
8.63 ERROR C031: Type invalid for operator....................................................................................171
8.64 WARNING C032: Division by 0.................................................................................................. 171
8.65 ERROR C033: Type not allowed for bit field..............................................................................171
8.66 ERROR C034: Bit field too long................................................................................................. 171
8.67 FATAL ERROR C036: Too many errors....................................................................................172
8.68 ERROR C037: Too many warnings........................................................................................... 172
8.69 ERROR C038: Type qualifier not allowed with a function..........................................................172
8.70 ERROR C039: Type returned is invalid......................................................................................172
8.71 ERROR C040: Type conversion impossible..............................................................................172
8.72 FATAL ERROR C041: Internal error during code generation.....................................................173
8.73 WARNING C042: Condition always false...................................................................................173
8.74 WARNING C043: Condition always true....................................................................................173
8.75 ERROR C045: Invalid function call............................................................................................ 173
8.76 ERROR C047: Invalid initialization.............................................................................................173
8.77 ERROR C048: Too many initializations.....................................................................................174

-8-
ST7/STM8 C Compiler Manual

8.78 ERROR C049: Assignment of a constant..................................................................................174


8.79 ERROR C050: Invalid operation on pointers..............................................................................174
8.80 ERROR C052: Operand is not an lvalue....................................................................................174
8.81 ERROR C053: Absolute allocation missing...............................................................................175
8.82 ERROR C054: Too many storage classes.................................................................................175
8.83 ERROR C055: Constant expression required............................................................................175
8.84 ERROR C056: Syntax error....................................................................................................... 175
8.85 ERROR C057: Duplicated specifier........................................................................................... 175
8.86 ERROR C058: Parameter out of range......................................................................................176
8.87 ERROR C062: Illegal type qualifier............................................................................................176
8.88 WARNING C063: Undefined operation for generic pointers (char* assumed)...........................176
8.89 ERROR C066: 'case' duplicated................................................................................................ 176
8.90 ERROR C067: 'default' duplicated.............................................................................................177
8.91 ERROR C068: Unexpected '%s'................................................................................................ 177
8.92 ERROR C069: missing enclosing switch statement...................................................................177
8.93 ERROR C071: A void function cannot return a value.................................................................177
8.94 ERROR C072: void type not allowed......................................................................................... 178
8.95 ERROR C073: duplicated function parameter...........................................................................178
8.96 ERROR C074: Invalid declaration syntax...................................................................................178
8.97 ERROR C075: ( missing............................................................................................................ 178
8.98 ERROR C076: ) missing............................................................................................................ 178
8.99 ERROR C077: Invalid expression syntax...................................................................................178
8.100 ERROR C078: Invalid instruction '%s'......................................................................................179
8.101 ERROR C079: Invalid type for a register declaration...............................................................179
8.102 ERROR C080: Addressing of a register variable is not allowed...............................................179
8.103 ERROR C081: Identifier expected........................................................................................... 179
8.104 ERROR C083: Undefined or null array size.............................................................................180
8.105 ERROR C084: Variable cannot be initialized...........................................................................180
8.106 ERROR C085: Invalid conversion............................................................................................ 180
8.107 ERROR C086: Assignment of integer to enumeration.............................................................180
8.108 ERROR C087: Identified bit field is of null size........................................................................181
8.109 ERROR C088: Function defined more than once....................................................................181
8.110 WARNING C089: Function should return a value....................................................................181
8.111 WARNING C090: Call to function without prototype................................................................181
8.112 ERROR C091: Invalid parameter declaration..........................................................................181
8.113 WARNING C092: Symbol is declared but not used.................................................................182
8.114 ERROR C093: 'do' without 'while'............................................................................................182
8.115 ERROR C094: An expression is missing.................................................................................182
8.116 ERROR C095: '%s' without ';'.................................................................................................. 182

-9-
ST7/STM8 C Compiler Manual

8.117 WARNING C096: Function should return a value....................................................................183


8.118 ERROR C097: Undefined label................................................................................................183
8.119 WARNING C098: 'goto' bypasses initialization of a local variable............................................183
8.120 ERROR C099: Label duplicated.............................................................................................. 183
8.121 ERROR C100: Operand not allowed with '&'............................................................................184
8.122 ERROR C101: Invalid type control expression.........................................................................184
8.123 ERROR C103: Misplaced directive or control..........................................................................184
8.124 ERROR C104: Invalid parameter declaration..........................................................................185
8.125 WARNING C105: Possibly incorrect assignment.....................................................................185
8.126 ERROR C107: 'sizeof' cannot be applied to a function............................................................185
8.127 ERROR C108: Unexpected character.....................................................................................185
8.128 WARNING C110: assignment of generic pointer from code....................................................185
8.129 WARNING C111: Operation with void type..............................................................................186
8.130 FATAL ERROR C114: Unknown control..................................................................................186
8.131 ERROR C115: '(' after control expected..................................................................................186
8.132 ERROR C116: ')' After control expected..................................................................................187
8.133 FATAL C118: Bad decimal number......................................................................................... 187
8.134 FATAL C119: Number out of range......................................................................................... 187
8.135 FATAL C120: Identifier expected............................................................................................. 187
8.136 FATAL C123: Cannot have general control on invocation line.................................................188
8.137 WARNING C124: Unknown #pragma, line ignored..................................................................188
8.138 ERROR C125: 'ASM/ENDASM' requires SRC directive to be active.......................................188
8.139 ERROR C126: Misplaced primary control, line ignored............................................................188
8.140 WARNING C127: Misplaced control '%s', line ignored............................................................188
8.141 ERROR C129: Unclosed string................................................................................................ 189
8.142 ERROR C130: Unclosed comment.......................................................................................... 189
8.143 ERROR C131: Unbalanced #if-endif controls..........................................................................189
8.144 ERROR C132: Bad integer expression....................................................................................190
8.145 ERROR C133: '%s' unexpected in a formal list........................................................................190
8.146 ERROR C134: Pointer to field not allowed...............................................................................190
8.147 ERROR C135: Interrupt/trap may not receive or return value(s)..............................................190
8.148 ERROR C136: Invalid base address........................................................................................191
8.149 ERROR C137: Bit member not allowed in struct/union............................................................191
8.150 ERROR C138: Self relative struct/union..................................................................................191
8.151 ERROR C139: A TYPE declaration cannot follow VOID..........................................................191
8.152 ERROR C140: Void type is invalid...........................................................................................192
8.153 ERROR C141: Function cannot return another function..........................................................192
8.154 ERROR C142: Function cannot return an array.......................................................................192
8.155 ERROR C143: Illegal struct/union comparison........................................................................192

- 10 -
ST7/STM8 C Compiler Manual

8.156 ERROR C144: duplicate struct/union/enum tag.......................................................................193


8.157 ERROR C145: Not a union tag................................................................................................193
8.158 ERROR C146: Not a struct tag................................................................................................ 193
8.159 ERROR C147: Duplicate struct/union member........................................................................194
8.160 ERROR C148: Illegal type conversion from/to 'void'................................................................194
8.161 ERROR C149: Non-address/-constant initializer......................................................................194
8.162 ERROR C150: String out of bounds........................................................................................ 194
8.163 ERROR C152: Memory space specifier illegal on struct/union member..................................195
8.164 ERROR C154: 'ASM/ENDASM' not allowed in include file.......................................................195
8.165 ERROR C155: Cannot open file.............................................................................................. 195
8.166 ERROR C156: Not an lvalue.................................................................................................... 195
8.167 ERROR C157: Illegal indirection..............................................................................................195
8.168 ERROR C159: Missing character............................................................................................196
8.169 ERROR C161: Bit in illegal memory space..............................................................................196
8.170 ERROR C162: Syntax error near '%s'......................................................................................196
8.171 ERROR C163: Base^pos: invalid bit position...........................................................................196
8.172 WARNING C165: Absolute address does not match the memory space.................................196
8.173 ERROR C166: Pointer to bit is not allowed..............................................................................196
8.174 ERROR C167: Array of bits is not allowed...............................................................................197
8.175 WARNING C171: Bit(s) not allowed as local variable (char type assumed).............................197
8.176 ERROR C172: Invalid absolute bit address.............................................................................197
8.177 ERROR C173: Function member in struct/union.....................................................................197
8.178 ERROR C176: Only one operand has bit type.........................................................................197
8.179 ERROR C177: Illegal pointer conversion.................................................................................198
8.180 WARNING C178: Qualified local variable not allowed.............................................................198
8.181 WARNING C179: Memory space on parameter ignored.........................................................198
8.182 WARNING C183: Funcdef requires ANSI-parameter list.........................................................198
8.183 ERROR C185: Intrinsic '%s': declaration/activation error.........................................................199
8.184 ERROR C186: Function redefinition (different attribute)..........................................................199
8.185 WARNING C187: Variable redefinition (different qualifiers).....................................................199
8.186 FATAL C188: System error...................................................................................................... 199
8.187 WARNING C192: Assignment between pointers to char signed and unsigned........................199
8.188 ERROR C193: Assignment from generic to typed pointers......................................................199
8.189 ERROR C194: Not a valid identifier.........................................................................................201
8.190 ERROR C195: 'DATA': segment too large...............................................................................201
8.191 ERROR C199: 'BIT': segment too large...................................................................................201
8.192 ERROR C200: Evaluation version: maximum object size reached..........................................201
8.193 ERROR C203: Pointer: different mspace.................................................................................202
8.194 ERROR C204: Pointer: const to non-const assignment...........................................................202

- 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.1 Purpose of this manual


This manual has been written to serve two purposes:

1.1.1 For first time users


This manual gives an overview of a C compiler in general, using RCST7/STM8 as an example. At the
beginning of this document we describe what a compiler is, what it does and how it relates to the other
tools. Then going on to describe how to use the Compiler to achieve various results, including
descriptions of all the aspects of the tool. Armed with the knowledge in this manual, first time ST7 or
STM8 users (with previous knowledge of the C language) will be confident and able to properly use the
Compiler for any application they develop.

1.1.2 For experienced users


This manual has been written with the experienced user in mind, providing reference sections for all
Compiler directives, keywords and every single warning, error and fatal error message. Every aspect
where RCSTM8 differs or improves the ANSI C standard is clearly documented.
This manual is not a programming manual or a generic ‘C’ manual or tutorial. It is assumed that the
user is familiar with the C programming language and Windows, and has some familiarity with ST7 or
STM8 microcontrollers.

1.2 Organization of the manual


This manual is divided into the following chapters:

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.

1.3 Conventions Used in this Manual


File | New Refers to the menu item “New” on the File menu.

While(1); (bold, monospaced type) User input

filename Replace the italicized text with the item it represents

[] Items inside [ and ] are optional.

[…] Represents a list of optional items that are the same as the preceding optional
item.

while(1); (monospaced type) Code, Directives and software generated output

0000H A Hexadecimal value


0x0000 A Hexadecimal value

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.

1.4 Additional help or information


You may find additional documentation in the DOCS folder inside the Ride installation.
In addition, help is available via the Help menu in Ride.

You can also visit the Raisonance website: http://www.raisonance.com/

Address: Raisonance S.A.S.


17, Avenue Jean Kuntzmann,
38330 Montbonnot Saint Martin
France

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 Extensions to the ANSI standard


In order to take into account the ‘embedded’ nature of the ST7/STM8 microcontrollers and of the
applications that are developed around them, RCSTM8 implements a number of extensions to the
ANSI standard, and has some limitations that typical ANSI compilers (typically, desktop compilers) do
not have.
Apart from the few limitations listed below, RCSTM8 can be considered as a superset of the ANSI
standard: This means that programs developed for an ANSI C compiler, provided they don’t use
systems-specific resources that are not supported, should be easily compiled with RCSTM8.

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.

Please refer to Chapter 5 for details about these keywords.

2.1.3 C++-like comments


The RCSTM8 Compiler allows use of C++ style comments using double slash (“//”).text following a
double slash on a line, will be ignored by the RCSTM8 Compiler.

2.1.4 Binary constants


Binary constants can be entered as “0b<binary number>” on RCSTM8. This eases bit manipulations

Examples:

int i = 0b1101000010001001; // is equivalent to i = 0xD089


char a = 0b00100001; // is equivalent to a = 0x21

2.2 How to invoke the Compiler


The RCSTM8 Compiler can be invoked via the Integrated User Interface (Ride) or on the command
line (DOS-like). As RCSTM8 can be invoked with a number of different options (also called directives),
it is strongly recommended to use it together with Ride, as the graphical user interface minimizes the
risk of errors when setting the options.
Generally, compiler options and directives can be set either when the compiler is invoked (via Ride or
in the command line) or directly in the source file(s), with the #pragma directive.
See Chapter 6: “Directives” for a detailed description of all compilers options and directives.
Below is an overview of the main options and how they can be set either via Ride or from the command
line.

2.2.1 From Ride


RCSTM8 is invoked under Ride by choosing the Project | Translate menu when a file with extension .c
is selected in the Project Window.
There are other ways to invoke the compiler:
● Right click on a C file in the project window and choose “Translate”
● Using the “Make All” or “Build All” commands in the project menu will invoke the compiler for
every C source file of the project. See the Ride manual for the details.

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

2.2.2 From the Command Line


The Compiler is a command line executable program that may be executed by entering its name –
RCSTM8.exe – at the DOS prompt. Here is a sample output:

RCSTM8 COMPILER V2.28.10.092


Copyright (c) Raisonance S.A.S. 1987-2010. All rights reserved

*** FATAL-ERROR : No argument list


RCSTM8 TERMINATED

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:

RCSTM8 filename options

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.3 Directives list


Various controls (called “directives”) may be used to control the behavior of the Compiler. The
directives are described in detail in Chapter 6 Directives.

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.

The libraries are described in more detail in Chapter 8 Libraries.

2.5 Input files


The compiler accepts one single input file. This is a text file containing the C code to be compiled. If an
input file is specified without suffix, the .c suffix is assumed.

- 20 -
ST7/STM8 C Compiler Manual 2. Compiler overview

2.6 Output files

2.6.1 Relocatable object file


The result of the compiling process is a single relocatable object file. This file contains relocatable
segments, a symbol table and debugging information. Each segment may be absolute or relocatable.
The format of the object file is OMF- RCSTM8.
The usual file name extension for object files is “.obj”.

2.6.2 Listing file


The listing file is a text file generated by the Compiler that contains the assembler translation of each C
source line.
The usual file name extension for object files is “.lst”.

2.6.3 Preprocessor output file


The preprocessor output file is optionally generated and is a text file that contains the result of the pre-
process operation, that is, the source code, with all the macros expanded, the include files included
and the conditional compilation parts resolved.
Looking at the preprocessor output file can be useful if you have problems with a source with complex
macros and are not sure where the problem comes from. The usual file name extension for
preprocessor output files is “.i”.

2.7 How the Compiler relates to other tools


The following illustration shows how the Compiler relates to the other tools in the toolset:

- 21 -
ST7/STM8 C Compiler Manual

RIDE Editor

.c file .stm8 file

Compiler Assembler

.lib file

Linker Library Manager

.aof file .lib file

Object-HEX Converter

.hex file

Ride Simulator/Debugger

Illustration 2.1: Compiler and toolset connection

- 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.

3.1 Function naming conventions


Functions are sometimes called internally with a name that is slightly more complex than the bare
function name that you write in your code.
Suppose for example that you have written the following code:

void c_fct(void)
{
return;
}

void c_reentrant_fct(void) reentrant


{
return;
}

As you can see below from the linker report, the reentrant functions add an extra question mark (the “?”
symbol) before the function name.

LINK MAP OF MODULE: COMPARISONS.AOF (COMPARISONS)

TYPE BASE LENGTH RELOCATION SEGMENT 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).

3.2 The concept of “working registers”


RCSTM8 uses four 8-bit pseudo-registers called BH, BL, CH, CL stored in page0 and contiguous in
memory. These are reserved by the compiler and are used for internal handling of data objects.
These pseudo-registers are relocatable and are dynamically placed in memory during the link phase.

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

An example is provided in Chapter 4 “Interfacing C to Assembly”.

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”.

3.3 Parameters passing conventions


In order to optimize memory usage (both code and data) RCSTM8 passes parameters between
functions in working registers when possible. When passing parameters in registers is not possible (too
many parameters), parameters are passed in memory for non-reentrant functions, or on the stack for
reentrant functions.
Here again, if you just use C, you don’t care how parameters are passed because the compiler
manages the whole process, but, if you want to mix C and Assembler, you need to know how to get the
parameters in an assembler routine, when this routine is called from C.
The convention depends on the reentrant/non-reentrant attribute of the function.

3.3.1 ST7 Parameter passing convention


The register assignment for ST7 will be performed as follows:

Table 1: ST7 Parameter passing convention

1st parameter size 8 16 32 Other


A X:A BX:CX Memory
2nd 8 X CL X Memory
parameter 16 CX CX Memory Memory
size
32 BX:CX BX:CX Memory Memory
Other Memory Memory Memory Memory

3.3.2 STM8 Parameter passing convention


The register assignment for STM8 will be performed as follows:

Table 2: STM8 Parameter passing convention

1st parameter size 8 16 24 32 Other


A X Stack Stack Stack
2nd
8 Stack A Stack Stack Stack
parameter 16 X Stack Stack Stack Stack
size
24 Stack Stack Stack Stack Stack
32 Stack Stack Stack Stack Stack
Other Stack Stack Stack Stack Stack

- 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.

3.3.3 Functions returning struct or union


For functions that return a structure or union, the calling function needs to pass a pointer to the location
where the object (struct or union) will be returned.
This pointer is passed through BX for RCSTM8 (all parameters requiring BX or BL will be passed
through memory in this case), and through Y for STM8.

Note: Avoid returning structures or unions


Returning a structure or union is allowed by the C language, but is very inefficient as it requires
moving large data blocks. We do not recommend returning structures or unions, but rather to
reorganize your code structure so that only pointers to structures/unions are passed as function
parameters or returned by functions.

3.3.4 Indirect function calls


The RCSTM8 compiler uses different conventions for passing parameters to functions depending
whether the functions are reentrant or not. In the case of an indirect call (through a function pointer),
the compiler cannot detect whether it should use the reentrant convention or not.
We recommend using only reentrant functions upon indirect calls. This will avoid potential mistakes
that would otherwise be difficult to fix. If you use non-reentrant functions upon indirect calls, the
compiler will emit a warning C255.

Note: Reentrant mode


In STM8 mode, the Compiler is always in reentrant mode.

Tip: Mixing reentrant and non-reentrant indirect function calls


In case you need to mix reentrant and non-reentrant functions in indirect calls, you have to ensure
that all your functions share the same parameter passing convention between reentrant and non-
reentrant functions. This can be obtained if you limit your functions to a maximum of 2 arguments,
and if these are 8 or 16 bits only.

In order to have a better grasp of this issue, consider the following example:

// Prototype of the function. The function body is in assembler


void asm_fct_p2(long c, long d);

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.

Consider now a similar example for a reentrant function:

// Prototype of the function. The function body is in assembler


void asm_fct_p2(long c, long d) reentrant;

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.

A working example is provided in Chapter 4 “Interfacing C to Assembly”

3.4 Function return values


The RCSTM8 compiler always expects return values to be passed in working registers. Depending on
the number of bytes of the return value, a number of working registers are used, in the following way:

Table 3: Function return storage

Return type Storage used


Union and Structure returns Return is done on stack.
(whatever their size) and Tables
The stack storage space for return value is reserved on
larger than 4 bytes
stack by the caller.
The storage address is passed to the called function
through BX in ST7 mode and through Y in STM8 mode.
1 byte A register.
2 bytes X:A registers in ST7 mode and X in STM8 mode.
3 bytes BL:CH:CL registers.
4 bytes BH:BL:CH:CL registers.

Note : Register content


Except for the return value, the contents of registers and pseudo-registers are undefined upon
return from a function.

The example below demonstrates parameters passing and return conventions:

char asm_fct_p1(int a, int b);


int asm_fct_p2(long c, long d);

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:

0000 3F05 F CLR ?CH


0002 A602 LD A,#002H
0004 B706 F LD ?CL,A ; 2nd parameter (2) is in
CH:CL
0006 5F CLR X
0007 4A DEC A ; 1st parameter (1) is in
X:A
0008 CD0000 F CALL asm_fct_p1 ; Call function

- 27 -
ST7/STM8 C Compiler Manual

000B 3F00 F CLR result ; Clear MSB


000D B700 F LD result + 01H,A ; Update result
000F 81 RET

Note: CLR result


The result is an integer and asm_fct_p1 returns a char. This explains the “CLR result” line.

3.5 Data types

The RCSTM8 compiler implements the following data types:


● Bit: single bit. Bits are managed automatically by the compiler and linker; it is not possible to
define a bit as a part of an existing byte.
● Char: 8 bits. Characters are unsigned by default, but this can be changed with the directive
SIGNEDCHAR or into Ride under “Project | Properties | RCSTM8 compiler | Code Generation |
Default type for ‘char’”.
● Integer: 16 bits, signed by default.
● Long: 32 bits, signed by default.
● Float: 32 bits, IEEE 754 format.

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).

Note: “Double” keyword conversion


The compiler will accept the “double” keyword (for compatibility with existing sources), but it will
automatically convert it to “float” (32 bit single precision).

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.

For instance, with the following declaration:

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 @”:

Bit 7(msb) Bit 0(lsb)


Base @ c (8 bits char)
Base @+1 unused b2(2 bits) b3(3 bits)
Base @+2 unused b4 (4 bits)
Base @+3 c2 (8 bits char)

3.7 Object storage rules


Variables are stored in memory, usually in RAM (either data or register spaces), sometimes in ROM
(typically for constants).
Knowing how variables are stored is important, for example, for applications that implement
communication between different platforms.
RCSTM8 uses the so-called “big endian” convention, meaning that multi byte variables are stored in
memory with the Most Significant Byte (MSB) first, that is, at the lowest address.

Example:
The example of code below,

int page0 i1; // integer variable forced to be in the page0 space


int data l1; // long variable forced to be in the DATA space

void main (void)


{
i1=10;
l1=50;

while(1) ; // Infinite loop


}

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

3.8 IEEE-754 floating-point support


Floating point numbers are supported only in single precision and are stored on 4 bytes each according
to the IEEE-754 convention which is summarized below:
Sign: 1 bit
Exponent: 8 bits
Mantissa: 23 bits

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:

0000 100033 F LD f1,#033H


0003 100033 F LD f1 + 01H,#033H
0006 1000B3 F LD f1 + 02H,#0B3H
0009 100040 F LD f1 + 03H,#040H

This is because the hexadecimal representation for the floating point “5.6” is 0x40B33333 (see below
for details).

3.8.1 Endian notice


Some computer architectures are big-endian, other ones are little endian. We will not extend further on
endianness in this document, but the reader should be aware that examples are shown in big endian
mode.
This means that the representation of numbers is such that the byte found at lowest memory address
(leftmost) contains the most significant bits (MSB), although the highest byte in memory (rightmost)
contains the LSB.

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 |
+--------+---------------+---------------+---------------+---------------+

Additionally, the IEEE representation also requires the following:


● b: Bias. Is not represented but inherently used in the conversions. Its value is 7Fh (127d).

Converting from binary to normalized value


Note: Conventions
In the following paragraphs we will use the following conventions:
Hexadecimal representations of values are noted 'XXh', such as '1Ah' for instance.
Decimal representations of values are noted 'XXd', such as '26d' for instance.
Binary representations of values are noted 'XXb', such as '11010b' for instance.
For instance, 1Ah = 26d = 11010b

The following formula is used to convert a binary representation of a float to its actual value:

- 31 -
ST7/STM8 C Compiler Manual

Value = (-1)^s x 1.m x 2^(e-b)

For instance, π (pi) is represented as 40490FDAh:

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

3.8.3 IEEE-754 denormalized values


Denormalized values are a specific representation that can be used to improve accuracy on very small
values. However, their use adds a lot of complexity on the compiler and their use leads to less optimal
code.
Hence denormalized IEEE-754 values are not supported by the RCSTM8 compiler.

3.8.4 Specific IEEE-754 values -0.0, ±INF and NaN


The “-0.0” value, although internally supported by transcendental operations +, -, *, / or the RCSTM8
compiler, are never returned by these functions. Consequently, the floating-point comparison does not
handle the specific case “0.0 == -0.0”.

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):

-INF FF 80 00 00 (1111.1111 1000.0000 0000.0000 0000.0000)


-1 BF 80 00 00
-0 80 00 00 00 (never returned by +, -, *, / but can be used as input)
+0 00 00 00 00
+0.5 3F 00 00 00
+1 3F 80 00 00
Pi(3.1415926) 40 49 0F DA
+INF 7F 80 00 00 (0111.1111 1000.0000 0000.0000 0000.0000)
NaN 7F C0 00 00 (0111.1111 11xx.xxxx xxxx.xxxx xxxx.xxxx)

- 32 -
ST7/STM8 C Compiler Manual 3. Compiler internals

Note: Abbreviations and explanations

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.

NaN is the highest possible value, above Infinity.

'-0' (negative 0) is a possible value upon entry of RCSTM8 mathematical operations, but will never
be generated.

NaN or ±INF are easily identified by their 0xFF exponent

The following table shows how an IEEE float should be interpreted depending on its Exponent and
Mantissa values:

Table 4: IEEE Floating point

Mantissa = 0 Mantissa <> 0


Exponent = 0 0 Denormalized
(not supported by RCSTM8)
Exponent = 0xFF INF NaN

- 33 -
ST7/STM8 C Compiler Manual

3.8.5 Fast floating-point libraries


Raisonance has been developing an alternative version of the RCSTM8 C library which offers much
faster operation (typically 40 to 70% speed improvement) at the cost of a reduced feature set:

– 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

3.9 Recursive and reentrant functions support


A function is said to be recursive if it can call itself.
More generally, a function is said to be reentrant if it can be safely called from multiple tasks (for
instance from a program and from an interrupt service routine).

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:

// This is a functions that compares two numbers


char c_fct_p1(int a, int b)
{
if (a>b) return(1); else return(0);
}

// This is the same function as above, but is reentrant


char c_reentrant_fct_p1(int a, int b) reentrant
{
if (a>b) return(1); else return(0);
}

All libraries provided with RCSTM8 are reentrant.


The RCSTM8 compiler prefixes an interrogation mark ‘?’ to reentrant function names. This prevents
mistakes when interfacing assembly and C functions.

- 35 -
ST7/STM8 C Compiler Manual

3.10 Predefined macros and symbols


Predefined macros are a set of symbols (macros) that are predefined in the compiler (as opposed to
symbols and macros that you can define manually using the #define syntax) and that can be used for a
variety of purposes, including isolating compiler dependencies, debugging, and including
time/date/version information in your executable).

The table below shows the predefined macros and symbols available in RCSTM8.

Table 5: Predefined macros

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

Example: predefined macros

void main (void)


{
code char * date;
code char * time;
code char * file;
int version; // take care as the version is not a decimal number
int ln;

date=__DATE__;
time=__TIME__;
file=__FILE__;
version=__RCSTM8__;
ln = __LINE__;

printf("This is file %s, compiled on %s at %s "


"with RCSTM8 version %d\n", file, date, time, version);

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

Note: Preprocessed source


All predefined symbols are managed by the preprocessor, that is, the compiler itself has no
knowledge about them. If you have a doubt about how one of these symbols is represented (for
example, the format of the version number), you can check it in the preprocessor listing file (the
"filename.i" file that is only generated using the Ride option under “Project | Properties | RCSTM8
compiler | Listing | Create a Preprocessed source”)

3.11 Quick Configuration Word (QCW)


The RCSTM8 listing file shows you the compiler configuration used to compile a specific source file.
For instance, it describes whether AUTO mode was activated, or the default local memory space used.
The QCW is very useful during the development phases of your project, to ensure that the proper
options have been passed to the RCSTM8 Compiler. For instance, if you developed an application
using the Ride environment and you wish to build your project using command-line options, you can
easily check that the listings produced by both environments have the same settings just by comparing
the QCW they generated. If the QCW is different, then the compiler options are not the same
(potentially leading to a different object code).

- 37 -
ST7/STM8 C Compiler Manual

Note: Identical QCWs


Two identical QCW do not necessarily mean that the same options have been used for the two
compilations. For instance, macros defined on the compiler's command line are not reflected in
the QCW.

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.

Below is an example of QCW line:

QCW(0x00183F60)

The QCW is an unsigned long value, appearing in hexadecimal.

The following table explains the bits which are used for QCW:

Table 6: QCW bits

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.

4.1 Converting C to assembly


In normal usage, the Compiler will generate an obj file directly from the source, without producing an
intermediate assembler file. However, there are cases where it might be useful to have the compiler
generate an assembler file instead, leaving to the MASTM8 assembler the task of producing an object
file for execution or for inclusion in a library.
Sometimes, for example, you might want to use the compiler to produce a rough assembler file
corresponding to a given algorithm and them optimize the result by hand and keep the optimized
version rather than the original.
RCSTM8 can be instructed to generate an assembler file with the SRC directive which can be
accessed under Ride from option under “Project | Properties | RCSTM8 compiler | Compiler output |
Format output of the compiler”).

4.2 Interfacing C to assembly


RkitSTM8 allows the mixing of C and assembler in the same application.
Mixing C and assembler is more complex than writing in C only, but it can be useful to have complete
control over some parts or the code (the parts you write directly in assembler) or to obtain the
maximum performance in some subroutines.

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.

4.3 In-line assembly


In-line assembly means writing assembler instructions in a C file. This technique, whose usage is
discouraged (see the previous chapter on how to interface C and assembler), can be useful when very
small portions of assembler code are needed.

- 40 -
ST7/STM8 C Compiler Manual 4. Programming examples

Note: In-line assembly limitations


There is an important limitation on how in-line assembly can be used with RCSTM8: as RCSTM8
(the C compiler) and MASTM8 (the assembler) are two independent applications that cannot call
each other, assembler language cannot be understood by RCSTM8. You therefore need to either
write machine language directly (hexadecimal) or to tell the compiler to generate a source file (it
would usually generate an object file directly), and then put this file, containing the assembler
generated by RCSTM8 plus the assembler written by hand through the assembler and linker.

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.

If you write the following line in any C function:

asm { 0x8F } // No trailing ';' is necessary

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).

4.3.1 Important notes about the use of the SRC pragma


Here is a list of important things to know when using the SRC pragma (that is, when you use in-line
assembly

● 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)

Notes about SRC mode:


- The object file is directly generated by the compiler in SRC mode. This was not the case with
versions anterior to 2.20.09.161 (released June 2009).
- The SRC mode is available in evaluation versions of the Compiler, although the intermediate
assembly files are not generated by the compiler.

4.4 Startup files


The “startup code” is a block of code written by Raisonance and provided together with RCSTM8 that
takes care of initializing some system resources (memory and peripherals) for you, in such a way that
they can be conveniently and transparently used from C.
After a reset, the STM8 chips start executing code from vector at address FFFEh; STM8 chips start
executing code from address 8000h (with 24-bits vector at address 8001h). Some startup routines
(namely ?C?initdata and ?C?initdataWbit) may be needed for proper operation of code generated by
the RCSTM8 compiler, and must be called before executing the first line of your application code (JP
main).

- 41 -
ST7/STM8 C Compiler Manual

Note: SCI, UART or USART initialization routine


The SCI, UART or USART initialization routine is now embedded into the putchar function. The
putchar sources are given (assembly files) and can be found in %ridedir%\Inc\Sources\STM8 in a
variety of files adapted to the peripherals from the ST7/STM8 family.
The startup code provided together with RCSTM8 initializes the system resources that are needed
for a “typical” RCSTM8 application written in C, that is RAM objects (page0, bit, data and far data)
as required, when global variables are used within your application.

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...

// your application code here


while(1); // This safeguard prevents the PC to exit from main
}

4.4.1 Startup size


The RCSTM8 compiler, along with the RLSTM8 linker, uses a specific technology that automatically
reduces the startup code size. The data initialization code is linked in only when your program uses
global variables.

4.4.2 Modifying start-up files


There are cases where modifying the startup code is necessary.
If your hardware has a console I/O attached to a device different from SCI0, or if you need to change
the baud rate. In this case you must copy the files provided with Ride from “%ridedir
%\Inc\Sources\ST7\Startup” to your project’s directory, edit them according to your needs and then
include them in your project.

Note: Modifying start-up files


These files contain specific information that will help you in carrying out appropriate modifications,
and will not be further described in the present document.
The code files you create have priority over the “standard” ones (that come from the compiler
libraries), so the modifications you have done are automatically taken into account.

- 42 -
ST7/STM8 C Compiler Manual 4. Programming examples

4.5 Interrupts and trap


RCSTM8 allows easy and convenient management of interrupts and trap directly from C.

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:

void timerA_handler(void) interrupt 8


{
// Handle timer interrupt here
}

void top_level_interrupt_handler(void) interrupt 0


{
// Handle Top Level Interrupt here
}

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.

Note: Trap keyword


The “trap” keyword can be seen as “interrupt (-1)”.

Example:

void trap_handler(void) trap


{
// Handle S/W trap
}

- 44 -
ST7/STM8 C Compiler Manual 4. Programming examples

4.5.2.1 Handling multiple interrupts with a single function

It is sometimes desirable to handle several interrupts with the same function.


However, the RCSTM8 syntax for the definition of interrupts does not support defining several
interrupts for a single function.
The workaround is to write an interrupt function for one of the interrupts, then assign the other STM8
interrupt vectors directly with the address of the first interrupt function.

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:

Example for ST7:


In ST7 mode interrupt N vector is at address (0xFFFA – N*2), with a 2-bytes address.

// Interrupt 4 handler, defined as a RCSTM8 interrupt function


void isr(void) interrupt 4
{
// Actual ISR handling code comes here
}

// Define the ISR type, which is a pointer to an interrupt function


typedef void (code * ISR)(void);

// At absolute address (0xfffa - interruptNumber * 2) place a pointer to ISR


at 0xfff0 code ISR vect5 = isr; // 0xfff0 is interrupt 5 vector address

Example for STM8:


In STM8 mode interrupt N vector is at address (0x8008 + N*4) with a 3-bytes address, preceded by a
0x82 opcode.

// Interrupt 4 handler, defined as a RCSTM8 interrupt function


void isr(void) interrupt 4
{
// Actual ISR handling code comes here
}

// Define the ISR type, which is a pointer to an interrupt function


typedef void (fcode * ISR)(void); // fcode is necessary for 3-bytes address

// At absolute address (0x8008 + interruptNumber * 4) place a 0x82 byte


at 0x801C code unsigned char vect2_head = 0x82; // 0x801C is irq5 vector address
at 0x801D code ISR vect5 = isr; // 3-bytes pointer to isr function

4.5.2.2 Preventing the RCSTM8 compiler from generating interrupt vectors

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

4.6 Memory space selection


The STM8 core allows access to the whole 64KB address range through different addressing modes.
Depending on the addressing mode and the address range, different sub-ranges can be considered:

● 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.

Note: Using the "near" qualifier


The "near" qualifier is defined in RCSTM8 for compatibility with other compilers. A near-qualified
pointer will be able to point to an object located in section 0 only [0000-FFFFH] memory address
range). As all pointers are near by default, it is not necessary to qualify near pointers.
In the future, the RCSTM8 compiler may support new modes in which generic (unqualified)
pointers will be far by default.

4.6.1 Memory space qualifiers


Depending on the purpose of a given variable, relocating it in one of the specific memory spaces listed
above can help reduce your code footprint. The RCSTM8 compiler allows specifying the "space
qualifier" of the variable during its declaration. Some extra keywords have been added for this purpose:

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

page0 char counter;


// the "page0" qualifier could be reserved for the most used variables.
// One byte of code will be saved for any accessing instruction.

data char counter;


// "data" is the generic RAM. It addresses the 0000-FFFFh range.

- 46 -
ST7/STM8 C Compiler Manual 4. Programming examples

code const int values [] = {1,10,100,1000};


// "code" locates the variable in ROM, hence is inherently non-modifiable.
// It addresses the 0000-FFFFh range.

fdata char fcounter;


// "fdata" is available on STM8 only
// It locates the variable in RAM.
// It addresses the 000000-FFFFFFh range (24-bits addressing).

fcode const int fvalues [] = {1,2,4,8,16,32,64,128,256};


// "fcode" is available on STM8 only
// It locates the variable in ROM, hence is inherently non-modifiable.
// It addresses the 000000-FFFFFFh range (24-bits addressing).

Tip: Correctly using “const” and “code” qualified variables


The const and code qualifiers have a very different meaning for RCSTM8:
“const” tells the compiler that the declared variable should not be modified. The compiler hence
uses this information to perform a semantic analysis of the variable usage, and forbid at compile-
time the use of any operation that could change the variable.

For instance, the declaration:

const int val=42;

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.

4.6.2 Locating a variable in ROM for smaller footprint


The “code” keyword locates the variable it applies to in ROM, so the variable contents will be read-only
at execution time.
This has the benefit of generating smaller code, as the optimizer does not have to manipulate the
variable contents, hence it can often reduce the constant value to a simpler expression during the code
evaluation.

Example:

// The following variable is non-const but in ROM


code char romstring[] = "This is a long string located in ROM";

// The following variable is in RAM but is const,


// so the compiler will reject any modification of its contents
const char conststring[] = "This is a RAM-located string";

- 47 -
ST7/STM8 C Compiler Manual

4.6.3 Local variables memory space qualifiers


Note: Memory space qualifiers
The memory space qualifiers cannot be used for local variables (the variables declared within the
body of a function), nor for parameters. For a function, all the local variables will be assigned to a
single segment associated to the "default local memory space" (see DLC below).
It is however possible to use a specifier for a local variable if it is defined as “static”.
The STM8 is always in AUTO mode, hence local variables are on stack, limited to 255 bytes per
function (refer to the LARGESTACK directive for cases where your local variables are larger than
255 bytes).

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);

// The following line is much better than a strlen, as it is computed at compile-time


printf("String size is %i", sizeof(welcome));
}

produces the optimal output below:

; FUNCTION func1 (BEGIN)


; SOURCE LINE # 7
AE00 F LD X,#HIGH(welcome)
A600 F LD A,#LOW(welcome)
CD0000 F CALL ?puts
; SOURCE LINE # 10
A61C LD A,#01CH
88 PUSH A
4F CLR A
88 PUSH A
AE00 F LD X,#HIGH(?STR?TEMPSTM8?BASE)
A600 F LD A,#LOW(?STR?TEMPSTM8?BASE)
CD0000 F CALL ?printf
84 POP A
84 POP A
; SOURCE LINE # 11
81 RET
; welcome array of signed char (size=28). Static variable in CODE

; FUNCTION func1 (END)

 Tip: Optimizing RAM usage by changing your stack size.


The default stack size is chosen as the largest possible stack size on your processor type (64, 128
or 256 bytes). However, it may be beneficial to your project to reduce this value, so that part of the
unused stack space can be used by your data objects. On processors with stack in zero page,
such as the Ultralite, this will let you have more “page0” RAM space. On other processors, you will
have more “data” RAM space.
In order to benefit from this capability, you need to pass the selected stack size to the RLSTM8
linker using the STACKSIZE(newsize) directive. Alternatively, this setting can be accessed directly
from Ride with “Options | Project | RLSTM8 | Linker | Stack size”.

- 48 -
ST7/STM8 C Compiler Manual 4. Programming examples

4.6.4 Default memory space


Specifying a space qualifier is optional. When the space qualifier is not specified, the compiler
assumes a "default" memory class that can be specified by the DLC (Default Local Memory Space,
that affects the local variables) and DGC (Default Global Memory Space, that affects the global
variables) directives.

Example:

#pragma DLC(page0) DGC(data)

int tab[10]; // array in data (default by DGC)

page0 int counter; // variable in page0 (explicit)

void main ( void )


{
int i, j; // two variables in page0 (default by DLC)
}

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)”.

4.7 Automatic variables relocation


As we have seen in the previous chapter, the STM8 architecture allows placement of data in “zero
page” and “bit” memory space, which leads to a much more efficient code.
However, the zero page is severely limited (typically 128 bytes of data only), so for a large project it is
not always obvious which variables should be located in zero page.
RCSTM8, in coordination with the RLSTM8 linker, allows automatic selection of which global variables
should be assigned to the zero page segment, depending on their size and their usage count within the
whole application. The most used variables will be relocated to page 0, producing the smallest code.

Tip: Reduce your code footprint using Auto Relocation Mode


The “Auto relocation Mode” is a project-wide optimization that can help reducing your code
footprint. Make sure you activate Auto relocation Mode for your large projects, in order to
automatically benefit from this feature.

To select the " Auto Relocation Mode":

● Ensure your project is functional (compiles and links without error)


● In Ride, select "Project | Properties”
● Select "Advanced ST7/STM8 options | Build development tools | Automatic variables relocation"
and set it to “active”. This will perform (upon next build) a specific (and silent) compilation and
link pass that produces the "best list" of variables to be relocated to the zero page.

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.7.1 Internal workings of the Auto relocation Mode


A project build command launches two complete builds when auto-relocation is activated:
During the first build, all the variables that do not have a specific memory space qualifier are relocated
in the "data" memory space.
As the linker starts its allocation from the bottom of the memory, it will calculate that a certain number
of "data" bytes are relocated to the zero page, but are still addressed in 16-bit mode. For each "data"
variable the linker will evaluate how many accesses are done to it, hence how many bytes could be
saved from the executable code if they were relocated to the zero page.
The result of this evaluation will be used for ordering the variables and to decide which ones could be
assigned to the zero page at compile time. The list of such variables is then output by the linker in a
text file named as the AOF file, with the extra extension ".areloc" (for example: "myproject.aof.areloc").
During the second build, Ride will forward the list of the variables to be considered as "page0" to the
compiler. This forced relocation is done using the SPACEDEF directive (see in the list of the
directives).

Note: No relocation of local variables


Automatic relocation handles only global variables. Local variables are always relocated in the
default memory class specified by the directive DLC (ST7), or located on the stack (STM8).

Tip: Reading your .map file


Read your .map file (produced by the linker) to check for proper variables memory placement.

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>

at 0x0090 char c; // Char at address 0x90


char *pc = &c; // Pointer on char
page0 int i; // Integer in page 0
int *pi = &i; // Pointer on int
code float f = 2.12; // Float defined in ROM
float *pf = &f; // Pointer on float

void main(void)
{
static char s; // Character allocated in RAM
char *ps = &s; // Pointer on char

printf("Address of c: %#4.4x\n", pc);


printf("Address of i: %#4.4x\n", pi);
printf("Address of f: %#4.4x\n", pf);
printf("Address of s: %#4.4x\n", ps);

while(1); // Loop forever


}

- 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

4.8.1 STM8 large memory model


The STM8 family of devices offers 24-bit addressing. It is then possible to have applications with more
than 64 KB addressing mode.
The RCSTM8 “STM8” directive activates the production of code optimized for the STM8 architecture.
Also this directive has an option to define the memory model (SMALL or LARGE) that should be used.
For instance, the “STM8(LARGE)” directive will indicate to the compiler that large addressing mode should
be used (please refer to chapter 6. Directives – for further detail). In large mode all function calls are
done through 24-bit addressing using the CALLF/RETF STM8 instructions.

Note: Library functions


Library functions are limited to handling pointers to objects located in the first 64 KB or memory.
For instance, it is not possible to use the puts() library function to write a string which is located
above the 0x00FFFF address.
The RCSTM8 library functions are always located in the first 64 KB of memory address space.
RAM objects (such as tables) are limited to a 64 KB size.

4.8.2 STM8 pointers


As the STM8 is able to address the 0x000000-0xFFFFFF (24-bit addressing, 16 MB address space),
generic pointers on STM8 require 3 bytes of storage.
However, as 24-bit addressing is not always necessary for pointers, it is possible to use 16-bit pointers,
in order to reduce the pointer manipulation burden by the compiler, as well as the storage necessary
for pointers.
RCSTM8 then has a notion of 16-bit (near) pointers and 24-bit (far) pointers in STM8 mode. The
keywords far and near are type modifiers that can be used to declare such pointers.

Example:
char far *fpc; // 24-bit pointer to a char

- 51 -
ST7/STM8 C Compiler Manual

Note: Far pointers


Due to the STM8 architecture, handling objects through far pointers is very inefficient in terms of
code size (direct extended addressing is only allowed for JPF and CALLF STM8 instructions, but
not for accessing data). You should always attempt to architecture your application in order to
avoid using far pointers, or at least limit their use.
If you need a pointer that only references addresses of objects residing in the first 64 KB or
memory, it is recommended that you use a near pointer.
Affecting a near pointer to a far pointer is accepted, but it is not possible to affect a far pointer to a
near pointer.
In case you make use of far pointer in your application, you should always use the
FARPOINTERS compiler directive.

4.8.3 Pointer arithmetic on STM8:


The C language allows pointer substraction: When 2 pointers access different elements of the same
array, their substraction returns the difference of the subscripts of the two array elements.
For the specific case of the RCSTM8 compiler, the size of the difference is of type ptrdiff_t, and is
limited to an integer (-32768 to 32767).
In other words, the difference between two 24-bit pointers always produces a 16-bit result.
You should use pointer subtraction only for objects that belong to the same section (see the INSECTION
linker directive).

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.

For example, if we have the following code:

char far * pc;

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.

Note: Huge pointers


To use pointer arithmetic with huge capabilities (pointer arithmetic on the full 24-bits range) you
can just use “long” variables instead of pointers, and cast them back to (far *) when needed:
char far *pc
long l = 0x01FFFF; // This will be used as a huge pointer
l++; // Arithmetic is performed on 32-bits, hence unlimited
pc = (char far*)l; // Place the correct result back in a far pointer

- 52 -
ST7/STM8 C Compiler Manual 4. Programming examples

4.9 Fast 16 x 16 → 32-bits multiplication


In some cases, it may be useful to perform a 16-bit x 16-bit multiplication, although keeping the result
on 32-bits.
The Standard C solution is to cast the 16-bit arguments as long, then to perform a long multiplication
(32-bit x 32-bit). This solution works fine, however the 32-bits multiplication takes about 180 cycles to
execute on STM8.

The RCSTM8 compiler offers a fast 16-bit x 16-bit → 32-bit multiplication, which takes only about 70
cycles to execute on STM8.

In order to activate the optimization:


• Your project should be targeting an STM8 device (this optimization is not available for ST7).
• You must be in SPEED optimization mode.
• There should be a long multiplication in which both operands are converted from 16-bit operands (or
smaller) to 32-bit.

For example:

short a, b;
long res = (long)a * b; // Long mul, both operands 16-bit: Optimized to muls161632

The RCSTM8 compiler is able to detect short constants held as long:

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

4.10 Inram functions


Some STM8 projects may require functions that are executed from RAM.
Typical uses for this feature are:
– In-application programming (IAP), where Flash must be reprogrammed. In this situation, the
reprogramming function must be in RAM.
– On some STM8 devices, it is possible to execute from RAM and to disable the Flash, thus creating a
very low-power mode.
The Raisonance STM8 compiler toolchain integrates some features that facilitate the development of
functions executed from RAM.
Typical projects have all their executable code in Flash. If a function must be executed from RAM, a
copy of the function code will be stored in Flash, in order to be copied to RAM and executed from there
when the application requires it. The copy of the function image to its RAM address is done
automatically by the C startup code, but can also be triggered manually for specific uses.
The following figure shows an STM8 memory map with a MyFunc() function whose Flash image gets
copied to RAM for execution.

Figure 4.1: Typical use of inram functions

- 54 -
ST7/STM8 C Compiler Manual 4. Programming examples

4.10.1 The inram keyword


To facilitate write functions that execute from RAM, the inram keyword has been defined. The inram
keyword is a function qualifier that must be added to a function declaration (after the argument list's
closing parenthesis) as follows:

void MyFunc(void) inram


{
// ... some code here
}

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.

4.10.2 Writing inram functions


An inram function must have the inram qualifier after the closing parenthesis of its declaration's
arguments list:

void MyFunc(void) inram;

This automatically makes the function copyable to RAM.


Limitations:
Caution should be taken when using use inram functions: As the code will be moved from Flash to
RAM, it needs to be fully absolute, hence limiting the use of relative addresses.
• Switch statements with jump tables are fully supported.
• Access to RAM variables (globals) and EEPROM is fully functional.
• Access to local variables is fully functional
Moreover, your inram functions may call other functions. If such functions are still in Flash (i.e. they
are not inram themselves) but your Flash access is disabled, the call will make your application fail.
As inram functions are usually executed while Flash is unavailable (being reprogrammed or
devalidated for ultra-low power mode), your code should not call functions in Flash nor reference any
item in Flash:
• C Strings (e.g., “Hello”) should not be used, as they are stored in Flash.
• Constant objects (such as lookup tables) should not be used, as they are stored in Flash.
• Non inram functions should not be called, as they are stored in Flash. This includes C library
function calls
• 32-bit objects (long or float variables) should not be used, as it requires some C library functions
which are stored in Flash.

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

4.10.3 Calling an inram function


An inram function is called normally. As the STM8 maps RAM and ROM addresses linearly, the CALL
assembly opcode will automatically locate the inram function call to its RAM address.

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.

4.10.4 Basic inram function example


The following code will execute the inRamFct() function from RAM.
// This is the function that will get copied from Flash to RAM
int inRamFct(void) inram
{
return 10;
}

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

4.10.5 Manual copy of an inram function from Flash to RAM


An inram function's code is copied from Flash to RAM by the C startup code. In some applications,
the RAM area where the code was initially stored may be erased or corrupted, so it may be desirable to
perform the copy again. The memcpy() function from the standard C library available through the
<string.h> header file is used to perform the copy.
The linker automatically defines 2 symbols for each inram function:
• __address__functionname is a symbol that holds the Flash address where the given function code is
stored.
• __size__functionname is a symbol that holds the function size in bytes.
• And we already have the function address (which is itself a pointer)
Symbols are specific objects, that have their own address but that do not have any associated storage.
You can think of them as assembly labels. As the C language does not have explicit support for
symbols, a specific cast must be operated for these symbols to be usable in C code.
Combined altogether, the memcpy() function can then be used to copy the inram function code from
Flash to RAM. This is demonstrated in the following example:
#include <string.h> // Needed by memcpy
// This is the function that will get copied from Flash to RAM
extern int __address__inRamFct; // Symbol holding the flash address
extern int __size__inRamFct; // Symbol holding the function size

int inRamFct(void) inram


{
return 10;
}

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

// We can then call the RAM function


res = inRamFct();
while(1); // Infinite loop
}

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

4.10.6 Overlaying several inram functions


If several inram functions are required by your application but they do not execute in a procedural
way, it is possible to spare some RAM space by locating several inram functions.
For instance, your application may have the reprogramFlashfromUART() and reprogramFlashfromCAN()
functions, which are both inram. Only one of these functions may be used at a given time.
The Raisonance linker provides some overlay technology, which will locate both functions at the same
RAM memory address. The SEGMENTOVERLAY linker directive1 will locate independent inram
functions at a common address, which saves some RAM space. In such a case, an inram function
must be copied to RAM before it can be called. Forgetting to do so may cause the wrong inram
function to be called.
Following is an example that demonstrates overlayed functions:

#include <stdio.h>
#include <string.h> // Needed by memcpy

void checkEqual(int a, int b)


{
if(a != b)
{
printf("Error: %i != %i\n", a, b);
}
}

extern int __address__inRamFct; // Symbol holding the flash address


extern int __size__inRamFct; // Symbol holding the function size
int inRamFct(void) inram
{
return 10;
}

extern int __address__inRamFct2; // Symbol holding the flash address


extern int __size__inRamFct2; // Symbol holding the function size
int inRamFct2(void) inram
{
return 3;
}

volatile int res = 0;


void main(void)
{
// Copy the first function code using memcpy()
memcpy(inRamFct, // RAM destination address
(void*)&__address__inRamFct, // Flash source address
(int)&__size__inRamFct); // Code size of the function

// We can then call the first RAM function


res = inRamFct();
checkEqual(res, 10); // OK

// 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

// Copy the second function code using memcpy()


memcpy(inRamFct2, // RAM destination address
(void*)&__address__inRamFct2,// Flash source address
(int)&__size__inRamFct2); // Code size of the function

// We can now call the first RAM function


res = inRamFct2();
checkEqual(res, 3); // OK

while(1); // Infinite loop


}

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.

4.11 Intrinsic functions


Intrinsic functions are provided to give the user a way to access some instructions offered by the
architecture that would otherwise never be generated by the compiler (typically interrupt management
or hardware related instructions).
Intrinsic functions are always inserted ‘in line’ in the generated code (as opposed to accessed as
subroutines, via call/ret).

Example:

#include <intrins.h>

at 0x88 char myport = 0xA5; // define a specific port at address 88h


int val1=0xBA42;

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

Note: non-functional code


The above example only serves to demonstrate RCSTM8 intrinsic functions; is not actually
functional.
The complete list of intrinsic functions can be found in Chapter 7, Libraries.

4.12 Specific library functions


● In order to handle specific situations where the library functions provided with RCSTM8 are too
limited, we offer some ANSI-like functions with extended functionality:
● The fmemcpy() library function is provided to copy a memory object from a 24-bit memory address
to another (objects of size 65535 maximum, only available in STM8 LARGE model).
● The lmemcpy() library function is provided to copy a memory object from a 16-bit memory address
to another (objects of size 65535 maximum).
● The lmemset() library function is provided to set a 24-bit memory addressed object of a given size
to a specific value.
● The lstrlen() library function is provided to compute the size of a null-terminated string with a 24-
bit memory address.

4.13 Volatile variables declaration


The volatile keyword specifies that a variable is volatile (that is, its value can change at any time,
independently from the program flow).
RCSTM8 treats volatile variables in a specific way, and never tries to optimize them (as it would do with
normal variables).
The volatile keyword should be used any time a global variable can be modified by an interrupt routine.

4.14 Recursion and re-entrancy


A function is said to be recursive if it can call itself.
More generally, a function is said to be reentrant if it can be called from different processes at the
same time and still work properly.
The typical case is the following: a function is called in the main program; during the execution of the
function an interrupt occurs, and the interrupt 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 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.

4.14.1 Library functions reentrancy


All library functions provided with RCSTM8 are reentrant. Despite this, using complex functions, such
as printf, in interrupt routines is not recommended, as their stack requirements may lead to stack data
corruption.

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

int PosArray[3][SIZE]; // = { { 4, 3, 2, 1 },{0, 0, 0, 0},{0, 0, 0, 0}};


// 3 mastes, four disks all set on left maste
//---------------------------------------------------------------------
// Functions forward definitions
//---------------------------------------------------------------------
void DrawPosition(void); //for demo purpose
void ComplexMove(int dest, int src, int size) reentrant;
void HardMove(int dest, int src); //move a disk from src to dest

//---------------------------------------------------------------------
// 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;

//first find the disk at the top of the src stack


for ( i = 0; i < SIZE; i++)
{
disk = PosArray[src][(SIZE-1)-i];
if (disk)
{
PosArray[src][(SIZE-1)-i] = 0; // remove the disk
break;
}
}
//then put it onto the dest stack
for ( i = SIZE-1; i >= 0; i--)
{
if (PosArray[dest][i-1] || (i==0)) // a floor exists
{
PosArray[dest][i] = disk; // put the new one over it
break;
}
}
DrawPosition();
}

//---------------------------------------------------------------------
// 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;

// first draw the top of the towers


putchar('\n');
for (i = 0; i < 3 ; i ++)
{
Repeat ( ' ', SIZE+1);
putchar('|');
Repeat ( ' ', SIZE);
}
putchar('\n');
for (i = SIZE-1; i >= 0 ; i--)
{
putchar ( ' ' );
for (j = 0; j < 3 ; j++)
{
size = PosArray[j][i];
n = SIZE - size ;
Repeat ( ' ', n ); //Space filling for small disks
Repeat ( '#', size ); //Left side of the disk
putchar('|'); //Central maste
Repeat ( '#', size ); //Right side of the disk
Repeat ( ' ', n ); //Space filling for small disks
putchar(' '); //Space interval between disks
}
putchar('\n');
}
for (i = 0; i < 3 ; i ++)
{
putchar ( ' ' );
Repeat ( '=', 2*SIZE+1 );
}
}

//---------------------------------------------------------------------
// 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
}

printf("\nNow running Hanoi towers algorithm");

//First we initialize the stack on the left (0) maste


DrawPosition();

//Move all 4 disks from left(0) to right(2)


ComplexMove(2, 0, SIZE);

printf("\nMove is done !");

// Infinite wait loop


while(1) ;
}

- 62 -
ST7/STM8 C Compiler Manual 4. Programming examples

4.15 Using the dynamic memory manager


RCSTM8 includes support for allocating and freeing dynamic memory.

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

4.15.1 Dynamic memory manager example

Here is an example that shows how the dynamic memory manager can be used:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Buffer for the dynamic memory manager (32 bytes minimum) */


unsigned char mempool_buffer[100];

char* buffer;

void main( void )


{
/* Initialization of the dynamic memory manager */
mempool_init( mempool_buffer, sizeof( mempool_buffer ) );

/* Allocates a 40-byte buffer */


buffer = (char *)malloc( 40 );

if( buffer != NULL )


{
/* Write something in the buffer and then print it */
strcpy( buffer, "hello" );
printf( "40 chars buffer at address 0x%04x, contains \"%s\"\n", buffer, buffer );
free( buffer );
}
else
{
printf( "Allocation error: Returned 0x%04x\n", buffer );
}

while(1);
}

- 64 -
ST7/STM8 C Compiler Manual 4. Programming examples

4.16 Using I/O functions


RCSTM8 is provided with a set of libraries that allows easy management of the IO stream via the
printf(), puts(), putchar(), getchar(), scanf() functions.
As a typical RCSTM8 application does not include a console, the output of the printf() function is
directed, via the putchar() function, on the SCI 0 or USART peripheral.

This makes debugging with the simulator easy, as the output of the SCI peripheral is shown in a
dedicated simulator window under Ride.

4.16.1 Optimized printf and sprintf variants


The RKit-STM8 toolset comes with a Raisonance-specific technology that automatically reduces the
ROM footprint required by the printf() function.
printf() (or the underlying sprintf) is a function with a complex set of options: It enables formatting of
characters, strings, numbers (decimal or hexadecimal), floating-point values. Along with that, it
provides some advanced formatting such as left/right justification, field width control, 0 or +/- prefixes,
precision control...

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:

Function name Description


?printf0 This will call puts, with a special case w/o the final '\n'.
?printf1 Integer output %d (or u,i,x,X,c,o), no formatting.
?printf2 Some formatting (+,-,#,0..9) but NO %s nor %p.
?printf3 Some %s or %p, no formatting.
?printf_fp Full printf.
?printf Same as ?printf_fp

4.16.2 Providing your own putchar function


In case you need to change the putchar behavior, or the device it applies to (for instance sending
characters to a display instead of to the SCI), you must provide your own putchar function. The
RCSTM8 library putchar functions are available in assembly source form from %ridedir
%\Inc\Sources\ST7-STM8\. Please refer to these files for in-depth information about how to modify
them.

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

Note: Redefining the putchar() function


When you redefine the putchar() function:
You must conform the prototype provided in stdio.h. You have to be particularly careful of the fact
that the parameter passed in is of “char” type, and the return is an int.
putchar() must return 1 on successful operation, otherwise printf() will stop after the first
character.

#include <stdio.h>

int putchar(char c)
{
// Copy c to port B
PBDR=c;
return(1);
}

void main (void)


{
// Init port B as output
PBDDR=0xFF;

printf("Welcome to the RCSTM8 Compiler demo program\n");


while(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.

Tip: Optimizing printf with your specific putchar


The RCSTM8 compiler library, along with the RLSTM8 linker, use an advanced technology that
makes it possible to automatically select the smallest possible printf version that fits your
application needs.

The available printf versions are:


?printf0: Only prints bare strings without conversion.
?printf1: Integer output %d (or u,i,x,X,c,o), no formatting allowed.
?printf2: Same as ?printf1 plus some formatting (+,-,#,0..9); no %s nor %p.
?printf3: Some %s or %p, but no formatting.
?printf: Full-featured printf, including floating-point support.

- 66 -
ST7/STM8 C Compiler Manual 4. Programming examples

Note: Using assembly-written putchar


For your convenience, the sources for putchar, getchar and ungetchar as provided in assembly
source form in the directory “%ridedir%\Inc\Sources\ST7-STM8” directory. These are provided for
all the SCI families available on the ST7 and STM8 devices.
Feel free to use them in your projects and rewrite them as needed.

4.17 Using switch statements


When using switch statements in your application, the code generated by RCSTM8 can vary quite a lot
depending on how the switch statement is structured and what compiler options are set. This chapter
provides an overview of how RCSTM8 manages “switch” statements, so that it will be easy for you to
write your code in an optimal way.The first and most important distinction is about how the “case”
statements inside the switch are sequenced:
● If the case statements tend to follow unitary increments of the case variable, the compiler will
generate jump tables (in several possible forms, depending on optimization switches)
● If the case statements are rather sparse, the compiler will generate a sequence of “if”
statements.
Consider the following code:

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;

void main ( void )


{
int aa=3;

switch (switch_var)
{
case 7: aa-=1; break;

- 67 -
ST7/STM8 C Compiler Manual

case 8: aa-=1; break;


case 9: aa-=1; break;
case 10: aa-=1; break;
default: break;
}
while (1);
}

Note :RCSTM8 and jump tables


When RCSTM8 uses jump tables, it generates a slightly different code when optimizing for size or
for speed. The first example of this chapter was optimized for speed. If we had chosen to optimize
for size, the jump table would have been only two bytes (instead) of 3 for each case statement.
The main difference is in the jump table: Lines are no more branches to the relevant case option,
but just contain the address to jump to, meaning the executing code is slightly more complex (and
therefore longer to execute), but the space taken by each case statement will be smaller. This
gains some code space when the number of case statements is high.

- 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.

The table 7 below shows a summary of RCSTM8-specific keywords.

Table 7: RCSTM8-specific keywords summary

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.

Examples: asm {0x8F}; // Wait For Interrupt (WFI)

Note: asm keyword


Using the asm keyword is not recommended. See Chapter 4 “Interfacing C to Assembler” for
more information.

5.2 at
Syntax: at <address> variable_declaration; or
at <address> function_declaration;

Description: Used in a variable or function declaration, it specifies the variable or function to be


at an absolute address.

Examples: at 0xBE char mychar;


at 0x2800 int my_absolute_function(void) {}
at 0x81 bit mybit; // bit 1 of byte at address 0x10
data0 char myvar; // variable in page 0
at myvar^2 bit bit2; // bit 2 at address of variable myvar

Note: Absolute addresses


Functions can be set at absolute addresses either by using the at keyword in the compiler or, later
on, in the Linker (see the Linker manual on how to do this).
For the “bit” or “hbit” variables, the absolute address must be specified as follows:
<byte address>*8 + <number of the bit>
Bits are numbered starting with 0 for the least significant bit, up to 7 for the most significant one.

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.

Examples: bit b1, b2, b3;

- 70 -
ST7/STM8 C Compiler Manual 5. Keywords

Note: Page 0 bit variables


The linker locates all bit variables in page 0 (0x00-0xFF memory range) in order to make
optimized use of bit-oriented RCSTM8 architecture.
“bit” variables must be global, hence cannot be declared within a function.
Pointers to bit (bit * pb) variables are not allowed in RCSTM8.

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.

Examples: char code tt=10;


char code *mycodestring = "a string in code";

See also: data, eeprom, fcode, fdata, page0

Note: Initialization value


As variables in code cannot be modified, an initialization value is required, or the variable
declaration would be meaningless (the compiler issues a warning if no initialization is provided).
The ANSI keyword “const” does not make the same effect as code: variables declared as const
are still put in RAM memory, and are constant only in the semantics of the C language.
It is recommended to place constant data such as strings in CODE memory.

5.5 data

Syntax: data

Description: Used in a variable declaration, it specifies the variable to be in the DATA memory
space.

Examples: int data var1;


float data var2;

See also: code, eeprom, fcode, fdata page0

Note: Data location


Depending on the memory model chosen, DATA might be the default memory space, so your
variables might end up in the data space even if you don’t use the data keyword.

- 71 -
ST7/STM8 C Compiler Manual

5.6 eeprom

Syntax: eeprom

Description: Used in a variable declaration, it specifies the variable to be in the EEPROM


memory space.

Examples: int eeprom var1 = 18;


float eeprom var2 = 2.0;

See also: code, data, fcode, fdata, page0

Note: EEPROM memory


EEPROM cannot be selected as the default memory space. Thus, variables will be present only if
they are explicitly located in 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).

Examples: nt far* myval; // 24-bit pointer

See also: near

Note: Near pointer


If you need a pointer that only references addresses of objects residing in the first 64 KB or
memory, it is recommended that you use a near pointer.
Affecting a near pointer to a far pointer is accepted, but it is not possible to affect a far pointer to a
near pointer.

- 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.

Examples: char fcode tt=10;


char fcode *mycodestring = "a string in code";

See also: code, data, eeprom, fdata, page0

Note: Initialization value


Reserved to STM8 LARGE memory model.
As variables in code cannot be modified, an initialization value is required, or the variable
declaration would be meaningless (the compiler issues a warning if no initialization is provided).
The ANSI keyword “const” does not make the same effect as code: variables declared as const
are still put in RAM memory, and are constant only in the semantics of the C language.

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.

Examples: int fdata var1;


loat fdata var2;

See also: code, data, eeprom, fdata, page0

Note: STM8 LARGE memory model


Reserved for STM8 LARGE memory model

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.

Examples: at 0x18F hbit OC1E; // Timer Output Compare 1, @31h bit7


at 0x18E hbit OC2E; // Timer Output Compare 2, @31h bit6

See also: at, hreg, hreg16

- 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.

Examples: /*PORT A register */


at 0x00 hreg PADR; /* Data register */
at 0x01 hreg PADDR; /* Data direction register */
at 0x02 hreg PAOR; /* Option register */

See also: at, hbit, hreg16

Note: “hreg” variables


“hreg” variables are slightly different to “volatile unsigned char”: The linker prevents an absolute
location of a C object at a reserved memory address (in the Hardware Registers address range),
and will only allow hreg objects to be located in such areas.

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.

Examples: at 0x0E hreg16 CNTR; /* STM8LITE Counter Register */

See also: at, hbit, hreg

Note: “hreg16” variables


“hreg16” variables are slightly different than “volatile unsigned short”: The linker prevents
absolute location of a C object at a reserved memory address (in the Hardware Registers address
range), and will only allow hreg/hreg16 objects to be located in such areas.

- 74 -
ST7/STM8 C Compiler Manual 5. Keywords

5.13 interrupt N

Syntax: void function_name(void) 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).

Examples: void my_int1_fct(void) interrupt 1 {}

See also: trap, directive INTVECTOR.

Note: Interrupt vectors


The interrupt vectors starting address (default 0xFFFA for ST7 and 0x8008 for STM8) can be
redefined using the pragma:
#pragma INTVECTOR(newaddr)

5.14 near

Syntax: near

Description: For the STM8, it indicates an address in the zero section (first 64KB)

Examples: int near* myval; // 16-bit pointer

See also: far

Note: Near pointer


Affecting a near pointer to a far pointer is accepted, but it is not possible to affect a far pointer to a
near pointer.

- 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.

Examples: float page0 myfloat;

See also: code, data, eeprom, fcode, fdata

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

Syntax: <function_definition> reentrant;

Description: Specifies that the function is reentrant (the local variables/parameters will be
stacked).

Examples: void c_reentrant_fct(void) reentrant {}

See also: See Chapter 4 “Recursion” for a complete explication and examples on how to
use reentrant functions.

Note: “reentrant” keyword


The “reentrant” keyword must be used for reentrant or recursive functions.

5.17 trap

Syntax: void function_name(void) 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_().

Examples: void my_int1_fct(void) trap {}

See also: interrupt N, 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)

Tip: Checking compiler directives


For directives specified via Ride or on the command line, you can check in the listing file to see if
they are properly taken into account (this is also useful to quickly find the syntax of a directive
specified in Ride).

The tables 8-12 below summarize available directives, by category:

Table 8: Listing directives

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

Table 9: General directives


Directive Description
GLOBAL Specify a directive that is ignored if unknown
DEFINE Define symbols for conditional compiling
GENERATEDFILE Generate a list of dependencies of the source file
INCLUDE Include a specific file before compilation
Define maximum number of errors/warnings before the compilation
MAXERR/MAXWAR
process is stopped
PATHINCLUDE Add the specific path to the list of included file paths
Define if all warnings should be shown (as opposed to only the most
WARNINGLEVEL
imprtant warnings)
WARNINGRETVAL Define the error level returned by RCSTM8
(NO)QUIET Do not show line numbers during compilation

Table 10: Code generation directives

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.

Table 11: Language directives

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

Table 12: ST7/STM8 specific 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

6.1 ASM / ENDASM

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.

Examples: #pragma ASM


JMP $ ; loop forever
#pragma ENDASM

Default: Not Applicable

Category: Code Generation Directives

See also: SRC

- 80 -
ST7/STM8 C Compiler Manual 6. Directives

6.2 AUTO (au) / NOAUTO (noau)

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

Default: Inactive (noauto)

Category: Language DirectivesCODE (cd)

- 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.

This error is generated in a very specific case, when a DIV/DIVW instruction is


interrupted and a second interrupt is generated precisely during the IRET
instruction execution of the first interrupt service routine.
Under these conditions, a DIV/DIVW instruction inside the second interrupt service
routine, or inside a function called by this second interrupt service routine, may
give an unexpected result.

When the CLEANIRQDIVFLAG directive is active, any interrupt service routine(i.e.


using the "interrupt" keyword), will perform the following code upon entry:

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.

If NOCLEANIRQDIVFLAG is active but the compiler detects that an interrupt


service routine makes use of the DIV or DIVW instruction or an external function is
called, the code sequence will be generated anyway.

This directive is available in STM8 mode only, and has no effect in ST7 mode.

Examples: NOCLEANIRQDIVFLAG
cleanirqdivflag

Default: Active

Category: STM8 specific directives

- 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

Category: Listing File Directives

- 83 -
ST7/STM8 C Compiler Manual

6.5 COND (co) / NOCOND (noco)


Syntax: COND
NOCOND

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

Category: Listing File Directives

- 84 -
ST7/STM8 C Compiler Manual 6. Directives

6.6 DEBUG (db) / NODEBUG (nodb)

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

Category: General Directives

- 85 -
ST7/STM8 C Compiler Manual

6.7 DEFAULTGC (dgc)

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

Category: ST7/STM8 options

- 86 -
ST7/STM8 C Compiler Manual 6. Directives

6.8 DEFAULTLC (dlc)

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

Category: ST7/STM8 options

- 87 -
ST7/STM8 C Compiler Manual

6.9 DEFINE (df)

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.

Note: the DEFINE directive


The DEFINE directive can be replaced by the #define preprocessor directive which is standard C,
but which cannot be expressed from the command line.

Several symbols can be defined in a single define directive. They should be


separated by commas (‘,’).

Note:“ = value” field


The parenthesis, the “= value” field is optional (that is, simply writing DEFINE(FOO) is perfectly legal)

Examples: DEFINE(BUF=10)
define(BUFSIZE=32,ENGLISH_VERSION,INTROSPECT=1,NDEBUG)
df(complete_libraries)

Default: Predefined macros such as __DATE__, __FILE__, __RCSTM8__ are automatically


defined by the RCSTM8 compiler. Please refer to chapter 3.10 “Predefined
macros and symbols“ for details.

Category: General Directives

- 88 -
ST7/STM8 C Compiler Manual 6. Directives

6.10 DEFINEJOKER (defj)

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

Category: General Directives

- 89 -
ST7/STM8 C Compiler Manual

6.11 DUPLICATESTRING (ds) / NODUPLICATESTRING (nods)


Syntax: DUPLICATESTRING
NODUPLICATESTRING

Description: This directive enables a compiler optimization that stores duplicated (but identical)
strings as a single object.

Examples: DUPLICATESTRING
ds
NODS

Default: NODUPLICATESTRING

Category: General Directives

- 90 -
ST7/STM8 C Compiler Manual 6. Directives

6.12 ENUMTYPE (et)

Syntax: ENUMTYPE(char|int)

Description: Tells the compiler if enumeration types should be regarded as characters or


integers. The former produces a better code, but is limited to 256 values.

This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored

Note: Automatic enum size detection


Starting with Rkit-STM8 version 2.28, RCSTM8 is now able to detect enumeration sizes and
automatically extend an enum from 8-bit to 16-bit when ENUMTYPE(char) is active.
For instance:
enum { SMALL = 40, LARGE = 4000 } myenum;
In such a case, “myenum” size will automatically be extended from char type to int type, in order to
be able to hold the LARGE value.

Examples: ENUMTYPE (char)


et(int)

Default: int

Category: Language Directives

- 91 -
ST7/STM8 C Compiler Manual

6.13 FARPOINTERS (fp) / NOFARPOINTERS (nofp)

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.

Note: STM8 only


This directive is available on STM8 only.

Examples: FARPOINTERS
nofp

Default: NOFARPOINTERS

Category: ST7/STM8 options

See also: No related directives

- 92 -
ST7/STM8 C Compiler Manual 6. Directives

6.14 GENERATEDEPFILE (gendep)

Syntax: GENERATEDEPFILE(TXT|filename)

Description: Generate the dependencies file (list of dependencies of the given source file,
including information about the source size)

If no parameter is given or if the “TXT” string is given as parameter, the


dependencies file is created as a text file, with the same name as the source file
but a “.dep” extension.
If a filename is given as parameter, the dependencies file is created as an “.rdb”
file (SQLite database format), with the provided name.

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)

Default: No dependency file generated.

Category: General Directives

- 93 -
ST7/STM8 C Compiler Manual

6.15 GLOBAL (glb)

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.

Category: General Directives

- 94 -
ST7/STM8 C Compiler Manual 6. Directives

6.16 INCLUDE (i)

Syntax: INCLUDE(filename)
I(filename)

Description: Include a file before compilation.

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.

Examples: INCLUDE(C:\My documents\projectA\myheader.h)


i(include\specifics.h)

Default: None.

Category: General Directives

- 95 -
ST7/STM8 C Compiler Manual

6.17 INITSTATICVAR (is) / NOINITSTATICVAR (nois)

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

Category: Language Directives

- 96 -
ST7/STM8 C Compiler Manual 6. Directives

6.18 INSECTION0 (in0) / NOINSECTION0 (noin0)

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.

This directive is available on STM8 only.

Examples: INSECTION0
noin0

Default: NOINSECTION0

Category: ST7/STM8 options

See also: No related directives

- 97 -
ST7/STM8 C Compiler Manual

6.19 INRAM (ir) / NOINRAM (noir)

Syntax: INRAM
NOINRAM

Description: Tells the compiler to compile all functions as “inram”.

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.

This directive is available on STM8 only.

Examples: INRAM
noir

Default: NOINRAM

Category: ST7/STM8 options

See also: No related directives

- 98 -
ST7/STM8 C Compiler Manual 6. Directives

6.20 INTVECTOR (iv) / NOINTVECTOR(noiv)

Syntax: INTVECTOR(0xFFFA)
NOINTVECTOR

Description: Defines a specific memory location for interrupt vectors.

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:

Vector for interrupt 0 is at address 0xFFFA;


Vector for interrupt 1 is at address 0xFFF8,
etc…
Vector for interrupt 13 is at address 0xFFE0

In other words, the vector for interrupt N is then at address 0xFFFA – 2 * N.

The default memory mapping for STM8 interrupt vectors is as follows:

Vector for interrupt 0 is at address 0x8009, preceded by the 0x82 opcode;


Vector for interrupt 1 is at address 0x800D, preceded by the 0x82 opcode;
etc…
Vector for interrupt 29 is at 0x807D, preceded by the 0x82 opcode;

In other words, the vector for interrupt N is then at address 0x8008 + 4 * N + 1,


with a preceding 0x82 opcode.
In case your device has a specific memory mapping for interrupt vectors, you can
change the base interrupt vector address to the value appropriate for your specific
requirements. Addresses for interrupt vectors will then be computed by the
RCSTM8 compiler using the new setting.

Note: Reset vector address


The reset vector address cannot be changed through the INTVECTOR directive, and requires a
specific startup file. Please refer to Chapter 4 for further information on this topic.

The NOINTVECTOR directive will prevent the compiler from emitting any interrupt
vector.

Examples: INTVECTOR(0xFFFA)
iv(0x00fa)
NOINTVECTOR

Default: 0xFFFA for ST7,


0x8008 for STM8.

Category: Code generation

See also: "interrupt" and "trap" keywords

- 99 -
ST7/STM8 C Compiler Manual

6.21 LARGEOBJECT (laob) / SMALLOBJECT (smob)

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

Default: Large objects allowed

Category: ST7/STM8 options

- 100 -
ST7/STM8 C Compiler Manual 6. Directives

6.22 LARGESTACK (lstk) / NOLARGESTACK (nolstk)

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

Category: ST7/STM8 options

See Also: No related directives

- 101 -
ST7/STM8 C Compiler Manual

6.23 LISTINCLUDE (lc) / NOLISTINCLUDE (nolc)

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

Category: Listing File Directives

- 102 -
ST7/STM8 C Compiler Manual 6. Directives

6.24 MAXERR / MAXWAR

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

Category: General Directives

- 103 -
ST7/STM8 C Compiler Manual

6.25 MODANSI (ansi)

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

Category: General Directives

See also: AUTO

- 104 -
ST7/STM8 C Compiler Manual 6. Directives

6.26 NOPRINTOFFSET (noprof)

Syntax: NOPRINTOFFSET

Description: Do not offset the lines in the listing file with spaces.

Examples: NOPRINTOFFSET

Default: Not applicable

Category: Listing File Directives

- 105 -
ST7/STM8 C Compiler Manual

6.27 NOPROPAGATION (noprop)

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");

c++; // This changes c, which is aliased to by *pc

if(*pc != 2) // Common subexpression is retrieved, but FALSE!


printf("Error\n");

while(1);
}

Default: Not applicable

Category: Code Generation Directives

- 106 -
ST7/STM8 C Compiler Manual 6. Directives

6.28 OBJECT (oj) / NOOBJECT (nooj)

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

Category: Object File Directives

- 107 -
ST7/STM8 C Compiler Manual

6.29 OPTIMIZATION (o)

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)

The optimization levels are as follows:

0: Expressions are reduced to constants whenever possible.


Internal data and bit access optimization.
Jump optimization.

1: Dead code elimination.


Conditional jump optimization.
Overlayable data is identified for linker optimization.
Complex move operations optimization.
Function arguments relocated to registers.
Common local sub-expression optimization.
Switch and case statements optimization.

2: Common global sub-expression optimization.


Loop optimization.
Loop performance optimization.

3: Peephole optimization.

The “OPTIMIZATION” directive supersedes the older “OPTIMIZE” directive.

Examples: OPTIMIZATION(2,SPEED)
o(3)

Default: O(3,SIZE)

Category: Code Generation Directives

See also: OPTIMIZE


Note: Optimize and Optimization directives
The Optimize directive has been introduced in RCSTM8 in order to match the optimization
scheme used by common compilers, and to reduce the number of optimization levels to a smaller
number.
The following is an equivalence table between the “Optimize” and “Optimization” levels:
O(0) is equivalent to OT(0)
O(1) is equivalent to OT(4)
O(2) is equivalent to OT(6)
O(3) is equivalent to OT(7)

- 108 -
ST7/STM8 C Compiler Manual 6. Directives

6.30 OPTIMIZE (ot)

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)

The optimization levels are as follows:


0: Expressions are reduced to constants whenever possible.
Internal data and bit access optimization.
Jump optimization.
1: Dead code elimination.
Conditional jump optimization.
2: Overlayable data is identified for linker optimization.
3: Complex move operations optimization.
4: Function arguments relocated to registers.
Common local sub-expression optimization.
Switch and case statements optimization.
5: Common global sub-expression optimization.
Loop optimization.
6: Loop performance optimization.
7: Peephole optimization.

The “OPTIMIZATION” directive now supersedes the “OPTIMIZE” directive. Please


do not use the OPTIMIZE directive for new developments.

Examples: OPTIMIZE(3,SPEED)
ot(7)

Default: OT(7,SIZE)

Category: Code Generation Directives

See also: OPTIMIZATION

Note: Speed or Size optimization


The Compiler produces different code depending on whether it optimizes code for size or for
speed. The differences can be summarized as:
– In SPEED optimization, switch statements are handled using jump tables.
– In SPEED optimization, the long divisions (which are very expensive in terms of execution
time) are optimized in favor of smaller division algorithms whenever possible, at the cost of
extra added code in the C libraries.
– In SPEED optimization, the objects are always moved inline by he Compiler. For instance,
when a “long” variable must be placed onto stack, it will be done directly (using registers); in
SIZE optimization some specific library functions are called to perform these data
movements, leading to smaller ROM code, but at the cost of extra cycles to perform the
CALL/RET sequences to the given library functions.

- 109 -
ST7/STM8 C Compiler Manual

6.31 PATHINCLUDE (pin)

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

Examples: PATHINCLUDE("C:\Program Files\Raisonance\Ride\Inc\")


PIN("..\MYPROJECT\")

Default: None.

Category: General Directives

- 110 -
ST7/STM8 C Compiler Manual 6. Directives

6.32 PREPRINTON (ppon) / PREPRINTOFF (ppoff)


Syntax: PREPRINTON
PREPRINTOFF

Description: These directives allows generation of a preprocessor listing file only for some
sections of the source file.

Examples: PREPRINTON
PPON
ppoff

Default: Not applicable

Category: Listing File Directives

See also: PREPRINT

- 111 -
ST7/STM8 C Compiler Manual

6.33 PREPRINT (pp)

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

Examples: PREPRINT("C:\Program Files\Raisonance\Ride\EXAMPLES\STM8.I")


PP

Default: Inactive

Category: Listing File Directives

See also: PREPRINTON, PREPRINTOFF

- 112 -
ST7/STM8 C Compiler Manual 6. Directives

6.34 PRINT (pr) / NOPRINT (nopr)


Syntax: PRINT("filename")
NOPRINT

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

Examples: PRINT("C:\Program Files\Raisonance\Ride\EXAMPLES\STM8.LST")


PR
nopr

Default: Active

Category: Listing File Directives

- 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.

Examples: RCSTM8 myfile.c quiet

Default: Inactive

Category: General Directives

- 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

// Back to some size-optimized code here

Default: None.

Category: General Directives

See also: SAVE

- 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)

/ Some speed-optimized code here

#pragma RESTORE

// Back to some size-optimized code here

Default: None.

Category: General Directives

See also: RESTORE

- 116 -
ST7/STM8 C Compiler Manual 6. Directives

6.38 SPACEDEF (sd)

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)

Default: Not applicable

Category: ST7/STM8 options

- 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

Category: Code Generation Directives

See also: ASM, ENDASM

Note: SRC mode unavailable in evaluation version


SRC mode is not available in evaluation versions of the Compiler.

- 118 -
ST7/STM8 C Compiler Manual 6. Directives

6.40 STACKSIZEFOREXP (ssx)

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 applies to STM8 mode only.

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)

Category: ST7/STM8 options

- 119 -
ST7/STM8 C Compiler Manual

6.41 ST7SET (st7) / STM8SET (stm8)

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

Category: ST7/STM8 options

- 120 -
ST7/STM8 C Compiler Manual 6. Directives

6.42 STM8STACKSET0 (stack0) / STM8STACKSET1 (stack1)

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.

This directive is not available in STM8 mode

Examples: STM8STACKSET0
stack1

Default: STM8SSTACKSET1

Category: ST7/STM8 options

- 121 -
ST7/STM8 C Compiler Manual

6.43 STRUCT_OPT (sue_opt)

Syntax: STRUCT_OPT

Description: This directive offers 2 different features:

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)

A second feature offered by this directive is to allow unnamed fields within


structures/unions.

For example, the STRUCT_OPT makes the following code valid:

struct s
{
char a;
union
{
struct
{
char b_hi8;
char b_lo8;
} bytes;

unsigned short b_16;


} dummy2;
} mystruct;

mystruct.b_hi8 = 42; // Shortcut for mystruct.dummy2.bytes.b_hi8


mystruct.b_16++; // Shortcut for mystruct.dummy2.b_16

Examples: SUE_OPT
struct_opt

Default: Inactive

Category: Language Directives

- 122 -
ST7/STM8 C Compiler Manual 6. Directives

6.44 SYMBOLS (sb)

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

Category: Listing File Directives

- 123 -
ST7/STM8 C Compiler Manual

6.45 UNSIGNEDCHAR (unsc) / SIGNEDCHAR (sc)

Syntax: UNSIGNEDCHAR
SIGNEDCHAR

Description: Define whether chars should be signed or unsigned by default.

This directive must appear at the beginning of a file (before any C statement),
otherwise it is ignored

Examples: UNSIGNEDCHAR
unsc
sc

Default: Unsigned char

Category: Language Directives

- 124 -
ST7/STM8 C Compiler Manual 6. Directives

6.46 WARNINGLEVEL (wl)

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)

Category: General Directives

- 125 -
ST7/STM8 C Compiler Manual

6.47 WARNINGRETVAL (wrv)

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)

Category: General Directives

- 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).

7.1 Standard C libraries


The standard C libraries implemented for RCSTM8 are available through the C header files reported in
the table below.

Table 13: Standard C library headers

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

7.2 Library files


The RCSTM8 library files are available in your “%ridedir%\Lib\ST7” directory.

The following files are present:

Table 14: Library files


File name Description
RCST7.LIB ST7 library.
RCST7FP.LIB ST7 floating-point library.
RCST7UL.LIB ST7 Ultralite with stack in zero page library.
RCST7ULF.LIB ST7 Ultralite with stack in zero page floating-point library.
RCSTM8.LIB STM8 library.
RCSTM8FP.LIB STM8 floating-point library.
RCSTM8L.LIB STM8 LARGE model (CALLF/RETF function calls) library.
RCSTM8LF.LIB STM8 LARGE model (CALLF/RETF function calls) floating-point library.

- 127 -
ST7/STM8 C Compiler Manual

RCST7.LIB ST7 library.


RCST7FP.LIB ST7 floating-point library.
RCST7UL.LIB ST7 Ultralite with stack in zero page library.

Additionally, the “%ridedir%\Lib\ST7\Startup” directory contains the STxxxx_s.OBJ files that contain the
various putchar versions adapted to specific devices:

Table 15: Available library files for putchar

File name Function


ST72264_S.OBJ putchar function (for ST72264 devices).
ST72321_S.OBJ putchar function (for ST72321, ST72324, ST72325 and ST72521 devices).
ST72345_S.OBJ putchar function (for ST72340, ST72344 and ST72345 devices).
ST72561_S.OBJ putchar function (for ST72361 and ST72561 devices).
ST7263B_S.OBJ putchar function (for ST7263B devices).
ST7LITE3_S.OBJ putchar function (for ST72LITE3 devices).
STM8_S.OBJ putchar function (for STM8 devices).
STM8L_S.OBJ putchar function (for STM8 devices LARGE model).

Note: Linker will link the file


The linker will automatically link the necessary file along with your project if you specify its
SCINAME directive (refer to the linker manual for instructions). Ride will perform this for you if
necessary.

7.3 STM8-specific Libraries


In addition, two STM8-specific libraries are provided as summarized in table below.

Table 16: STM8-specific library headers

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.

7.4 Intrinsic functions library


“intrins.h” is a library header file that is provided with all Raisonance compilers and that generally
contains a set of hardware-related functions. These allow, for example, generating all assembler
instructions that are not usually generated by the compiler. Usually, for a typical compiler, these

- 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).

The table below lists the functions included in intrins.h

Table 17: Intrins.h library functions

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_

Syntax: extern unsigned char _getCC_(void) reentrant;

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

See also: _setCC_()

- 130 -
ST7/STM8 C Compiler Manual 7. Libraries

7.4.2 _getSP_

Syntax: extern unsigned int _getSP_(void) reentrant;

Description: Return the current value of the Stack Pointer register (SP).

Examples: unsigned char saveSP;

saveSP = _getSP_(); // save SP


... // Additional code
setSP_(saveSP); // restore SP

See also: _setSP_()

- 131 -
ST7/STM8 C Compiler Manual

7.4.3 _halt_

Syntax: extern void _halt_(void) reentrant;

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.

Examples: _halt_(); // put the STM8 in halt mode

See also: _wfe_, _wfi_

- 132 -
ST7/STM8 C Compiler Manual 7. Libraries

7.4.4 _jmp_

Syntax: extern void _jmp_(_fctptr_t addr) reentrant;

Description: Generates a JP instruction. This jumps to a specific address.


The addr argument can be either a function name or an absolute memory
address.

The _fctptr_t type (pointer to function with no arguments, returning void) has been
defined to ease use of the _jmp_() function.

Examples: _jmp_((_fctptr_t )0x202F); // Jump to 0x202F. Notice the cast


_jmp_(main); // Jump to main() function

- 133 -
ST7/STM8 C Compiler Manual

7.4.5 _nop_

Syntax: extern void _nop_(void) reentrant;

Description: Generates the NOP (No Operation) instruction. This can be used to generate
delays or to use up some code memory space in ROM.

Examples: _nop_(); // will insert a NOP inline

- 134 -
ST7/STM8 C Compiler Manual 7. Libraries

7.4.6 _rim_

Syntax: extern void _rim_(void) reentrant;

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.

Examples void main(void)


{
initialize_hardware();
prepare_interrupts();
_rim_(); // Everything ready: enable interrupts

while(1); // Loop forever


}

- 135 -
ST7/STM8 C Compiler Manual

7.4.7 _rsp_

Syntax: extern void _rsp_(void) reentrant;

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.

Examples: void main(void)


{
_rsp_(); // Reset stack pointer for reset case

while(1); // Loop forever


}

- 136 -
ST7/STM8 C Compiler Manual 7. Libraries

7.4.8 _setCC_

Syntax: extern void _setCC_(unsigned char value) reentrant;

Description: Sets the Condition Code register (CC). This can be used to save/restore the CC
register.

Examples: unsigned char saveCC;


...
saveCC = _getCC_(); // save the CC flag
... // Additional code
_setCC_(saveCC); // restore the CC flag

See also: _getCC_()

- 137 -
ST7/STM8 C Compiler Manual

7.4.9 _setSP_

Syntax: extern void _setSP_(unsigned int newSP) reentrant;

Description: Sets the Stack Pointer (SP) to a new value. Please ensure that the new SP value
is properly supported by your STM8 derivative.

Examples: unsigned char saveSP;


...
saveSP = _getSP_(); // save SP
... // Additional code
_setSP_(saveSP); // restore SP

See also: _getSP_()

- 138 -
ST7/STM8 C Compiler Manual 7. Libraries

7.4.10 _sim_

Syntax: extern void _sim_(void) reentrant;

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_();

See also: _rim_

- 139 -
ST7/STM8 C Compiler Manual

7.4.11 _swapbyte_

Syntax: extern unsigned int _swapbyte_(unsigned int value) reentrant;

Description: Swap bytes of the given word (16 bits). This function performs an efficient
exchange of the two bytes from a given word.

Examples: page0 unsigned int w;


...
w = _swapbyte_(w); // Efficient exchange of w’s bytes.

See also: _swapnibble_

- 140 -
ST7/STM8 C Compiler Manual 7. Libraries

7.4.12 _swapnibble_

Syntax: extern unsigned char _swapnibble_(unsigned char value) reentrant;

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

Examples: page0 unsigned char c;


...
c = _swapnibble_(c); // Efficient nibble exchange

See also: _swapbyte_

- 141 -
ST7/STM8 C Compiler Manual

7.4.13 _trap_

Syntax: extern void _trap_(void) reentrant;

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.

Examples: _trap_(); // Generate a software trap

See also: trap” keyword (Chapter 5).

- 142 -
ST7/STM8 C Compiler Manual 7. Libraries

7.4.14 _wfe_

Syntax: extern void _wfe_(void) reentrant;

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.

Examples: _wfe_();// Set the CPU in low power mode

See also: _halt_, _wfi_

- 143 -
ST7/STM8 C Compiler Manual

7.4.15 _wfi_

Syntax: extern void _wfi_(void) reentrant;

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.

Examples: _wfi_(); // Set the CPU in low power mode

See also: _halt_, _wfe_

- 144 -
ST7/STM8 C Compiler Manual 7. Libraries

7.5 ST Firmware libraries


The STM8A and STM8S firmware libraries from STMicroelectronics are available from the %ridedir
%\Examples\STM8 directory.

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.

7.6 Hardware registers access


We define as a Hardware Register specific memory locations that map onto specific hardware
peripherals (such as I/O ports, I2C, SPI, Timers, SCI, ADC,…).
A collection of stM8xxx.h header files is provided along with RCSTM8. These files do not contain actual
code, but only Hardware Registers and symbol definitions relevant to the STM8 device in use.
The actual list of stM8xxx.h files can be retrieved from %ridedir%\inc\st7 and %ridedir%\inc\stm8.
Header files are present for each ST7 and STM8 derivative or family (sometimes similar derivatives
share the same header file). Each header file contains the definition for all Hardware Registers of the
given derivative.

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
//----------------------------------------------

/* IO Ports Hardware Registers */


at 0x00 hreg PADR ; /* PORT A data register */
at 0x01 hreg PADDR ;/* PORT A data direction register */
at 0x02 hreg PAOR ; /* PORT A option register */

at 0x03 hreg PBDR ; /* PORT B data register */


at 0x04 hreg PBDDR ;/* PORT B data direction register */
at 0x05 hreg PBOR ; /* PORT B option register */

/* LT registers for ST7FLITE0 */


at 0x0B hreg LTCSR ; /* Control and stats register */
at 0x0C hreg LTICR ; /* Input capture register */

/* LART registers for ST7LITE0 */


at 0x0D hreg ATCSR ; /* Control/Status Register */
at 0x0E hreg CNTRH ; /* Counter high Register */
at 0x0F hreg CNTRL ; /* Counter low Register */
at 0x10 hreg ATRH ; /* Autoreload high Register */
at 0x11 hreg ATRL ; /* Autoreload low Register */
at 0x12 hreg PWMCR ; /* PWM Output control Reg */
at 0x13 hreg PWM0CSR ; /* PWM Output control/Status */
at 0x17 hreg DCR0H ; /* PWM Duty Cycle HRegister */
at 0x18 hreg DCR0L ; /* PWM Duty Cycle LRegister */

/* 12 bit registers for LART */


at 0x0E hreg16 CNTR ; /* Counter Register */
at 0x10 hreg16 ATR ; /* Autoreload Register */
at 0x17 hreg16 DCR0 ; /* PWM Duty Cycle Register */

- 145 -
ST7/STM8 C Compiler Manual

/* FLASH Hardware Registers */


at 0x2F hreg FCSR ; /* Control/Status Register */

/* EEPROM Hardware Registers */


at 0x30 hreg EECSR ; /* Data EEPROM Control/Status Reg*/

/* SPI Hardware Registers */


at 0x31 hreg SPIDR ; /* SPI Data register */
at 0x32 hreg SPICR ; /* SPI Control register */
at 0x33 hreg SPICSR ; /* SPI Status register */

/* ADC registers for STLite0 */


at 0x34 hreg ADCCSR ; /* Control/Status Register */
at 0x35 hreg ADCDR ; /* Data Register */
at 0x36 hreg ADCAMP ; /* Amplifier Control Register */

/*Interrupt registers */
at 0x37 hreg EICR ; /*External Interrupt Register */

/* MCC Hardware Registers */


at 0x38 hreg MCCSR ; /* MCC Control/Status Register */
at 0x39 hreg RCCR ;/* RC Oscillator Control Register */
at 0x3A hreg SICSR ;/* System Integrity Control/Status*/

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:

// Small example that demonstrates STM8LITE0 register handling


#include <st7lite0.h>
#include <intrins.h>

void periph_init ( void )


{
/* Disable interrupts */
_sim_();

/* Initialize the ports configuration */


PAOR = 0;
PADDR = 0;
PBOR = 0;
PBDDR = 0;

/* Configure port A as output */


PADDR = 0x1F;
PAOR = 0x1F;

/* Configure port B as input pull-up */


PBOR = 0x1F;
}

void main(void)
{
_rsp_(); // Reset stack pointer for Reset case
periph_init(); // Initialize hardware

while(1) // Loop forever


{
PADR = PBDR; // Replicate the buttons values on the LEDs
}
}

- 146 -
ST7/STM8 C Compiler Manual 7. Libraries

7.7 Large objects access functions


In order to ease access and handling large large objects and 24-bit memory objects (24-bit pointers
accessing the whole 16 MB memory address space), RCSTM8 provides some specialized functions:

7.7.1 The fmemcpy function


Synopsis:
#include <string.h>
extern void far* fmemcpy(void far* dest, const void far *src, unsigned n) reentrant;

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).

7.7.2 The lmemcpy function


Synopsis:
#include <string.h>
extern void * lmemcpy (void * dest, const void *src, unsigned n) reentrant;

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).

Note: Standard memcpy() function


The standard memcpy() function is limited to 255 characters for efficiency reasons.

7.7.3 The lmemset function


Synopsis:
#include <string.h>
extern void * lmemset (const void * s, char c, unsigned n) reentrant;

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.

Note: Standard memset() function


The standard memset() function is limited to 255 characters for efficiency reasons.

7.7.4 The lstrlen function


Synopsis:
#include <string.h>
extern unsigned lstrlen (const char *s) reentrant;

- 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).

Note: standard strlen function


The standard strlen function is limited to 255 characters for efficiency reasons.

Please refer to Chapter 4.8.1 “STM8 large memory model“ for advice about large memory
programming on STM8.

7.8 STM8 library


The STM8 software library is given along with RCSTM8. It consists of a C software library providing
tools, code, documentation and samples for ST7 core and peripherals. The STM8 software library has
been developed to make it easy for you to develop STM8-based applications Using the STM8 library,
you can use any ST7/STM8 device in your application without having to thoroughly study each
peripheral specifications. As a result, using this library can save you a lot of coding time.

Note: STM8 library


Specific information about the STM8 library is available from %ridedir%\wizard\st7\st7_lib.

7.9 Library files


The RCSTM8 compiler comes with some library files, which will be required by the RLSTM8 linker in
order to build your projects.
Different versions of the libraries are available, depending on your application’s requirements. The
following table describes the available libraries.

Table 18: Description of available libraries

Library name Description


RCST7.LIB ST7 library.
RCST7FP.LIB ST7 floating-point library.
RCST7UL.LIB ST7 library for models with stack in zero page (i.e., Ultralite)
RCST7ULF.LI ST7 floating-point library for models with stack in zero page (i.e., Ultralite)
B
RCSTM8.LIB STM8 library.
RCSTM8FP.LI STM8 floating-point library.
B
RCSTM8L.LIB STM8 large memory model (24-bits addressing space) library.
RCSTM8LF.LI STM8 large memory model (24-bits addressing space) floating point library.
B

- 148 -
ST7/STM8 C Compiler Manual 7. Libraries

These files are located by default in the %ridedir%\lib\st7 directory.

Note: Automatic location


The RLSTM8 linker will automatically locate the libraries needed by your application, and will
select the appropriate ones. So you do not have to specify which libraries your project requires.

- 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.

Error messages are also classified according to their type as follows:

● 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.

Table 19: System errors

Error Type Description


S000 FATAL Insufficient memory
S001 FATAL File not found
S002 FATAL Write error on file
S003 FATAL Read error on file
S004 FATAL Missing argument list
S005 FATAL Illegal extension
S006 FATAL Cannot open file
S008 FATAL User Break

Table 20: Preprocessor errors

Error Type Description


P000 ERROR Line too long
P001 ERROR Incorrect end of line

- 150 -
ST7/STM8 C Compiler Manual 8. Error messages

P002 ERROR Invalid character


P003 ERROR Invalid character
P004 ERROR Bad character constant definition
P005 WARNING Unknown directive or control
P006 ERROR Bad or missing file name
P007 ERROR Unexpected ENDIF
P008 ERROR Unexpected ELSE
P009 ERROR Unexpected ELIF
P010 ERROR Incorrect End Of File
P012 ERROR Wrong number of parameters
P014 ERROR Redefinition of macro is not allowed
P015 ERROR Macro cannot be modified
P016 ERROR Invalid numeric constant
P017 ERROR Incorrect number format
P018 ERROR Invalid octal character
P019 WARNING Integer constant out of range
P020 ERROR \x not followed by an hexadecimal digit
P022 ERROR Use of DEFINED without argument
P023 WARNING Incorrect use of control or directive
P024 ERROR missing macro name after 'define'
P025 ERROR Identifier expected
P026 WARNING Not Implemented
P027 ERROR String expected
P028 ERROR Generic error (a string will specify it)
P029 ERROR duplicated formal parameter
P030 ERROR Cannot remove predefined macro
P031 ERROR Macro body cannot start or end with '##'
P032 ERROR Newline expected, extra characters found
P033 WARNING Directive does not apply to ST7

- 151 -
ST7/STM8 C Compiler Manual

Table 21: Compiler errors


Error TYPE Description
C000 ERROR Missing Character
C001 ERROR specifier not allowed in the context
C002 ERROR Too many types
C003 ERROR Undefined struct or union
C004 ERROR Undefined enumeration
C005 ERROR Type error in re-declaration
C006 ERROR Symbol defined more than once
C007 ERROR Invalid type in definition
C008 ERROR Parameter list longer than the prototype
C009 ERROR Parameter list smaller than the prototype
C010 ERROR Not a parameter
C011 ERROR Invalid syntax
C014 ERROR Undefined symbol
C015 ERROR Unary operator not followed by an operand
C017 ERROR Cast not followed by an operand
C018 ERROR Variable cannot be subscripted (not an array)
C020 WARNING Value never used
C021 WARNING Type is defined more than once
C022 ERROR Structure or union required
C023 ERROR A field must be an identifier
C024 ERROR Unknown field
C026 ERROR Wrong argument type
C027 ERROR “generic” is not legal in this context
C029 ERROR Integer Operand required
C030 ERROR Arithmetic Operand Required
C031 ERROR Type invalid for operator
C032 WARNING Division by 0
C033 ERROR Type not allowed for bit field
C034 ERROR Bit field too long
C036 FATAL Too many errors
C037 ERROR Too many warnings
C038 ERROR Type qualifier not allowed with a function
C039 ERROR Type returned is invalid
C040 ERROR Type conversion impossible
C041 FATAL Internal error during code generation
C042 WARNING Condition always false
C043 WARNING Condition always true

- 152 -
ST7/STM8 C Compiler Manual 8. Error messages

C045 ERROR Invalid function call


C047 ERROR Invalid initialization
C048 ERROR Too many initializations
C049 ERROR Assignment of a constant
C050 ERROR Invalid operation for pointers
C052 ERROR Operand is not an lvalue
C053 ERROR Absolute allocation missing
C054 ERROR Too many storage classes
C055 ERROR Constant expression required
C056 ERROR Syntax error
C057 ERROR Duplicated specifier
C058 ERROR Parameter out of range
C062 ERROR Illegal type qualifier
C063 WARNING Undefined operation for generic pointers
C066 ERROR “case” duplicated
C067 ERROR “default” duplicated
C068 ERROR Unexpected “ ”
C069 ERROR Missing enclosing switch statement
C071 ERROR A void function cannot return a value
C072 ERROR void type not allowed
C073 ERROR Duplicated function parameter
C074 ERROR Invalid declaration syntax
C075 ERROR “(“ missing
C076 ERROR “)” missing
C077 ERROR Invalid expression syntax
C078 ERROR Invalid instruction
C079 ERROR Invalid type for a register declaration
C080 ERROR Addressing of a register variable is not allowed
C081 ERROR Identifier expected
C083 ERROR Undefined or null array size
C084 ERROR Variable cannot be initialized
C085 ERROR Invalid conversion
C086 ERROR Assignment of integer to enumeration
C087 ERROR Identified bit field is of null size
C088 ERROR Function defined more than once
C089 WARNING Function should return a value
C090 WARNING Call to function without prototype
C091 ERROR Invalid parameter declaration
C092 WARNING Symbol is declared but not used

- 153 -
ST7/STM8 C Compiler Manual

C093 ERROR “do” without “while”


C094 ERROR An expression is missing
C095 ERROR Missing “;”
C096 WARNING Function should return a value
C097 ERROR undefined label
C098 ERROR “goto” bypasses initialization of a local variable
C099 ERROR Duplicated label
C100 ERROR Operand not allowed with '&'
C101 ERROR Invalid type control expression
C103 ERROR Misplaced directive or control
C104 ERROR Invalid parameter declaration
C105 WARNING Possibly incorrect assignment
C106 ERROR Operand of undefined or null size
C107 ERROR “sizeof” cannot be applied to a function
C108 ERROR Unexpected character
C110 WARNING Assignment of generic pointer from code
C111 WARNING Operation with void type
C114 FATAL Unknown control
C115 FATAL '(' After control expected
C116 FATAL ')' after control expected
C118 FATAL Bad decimal number
C119 FATAL Number out of range
C120 FATAL Identifier expected
C123 FATAL Cannot have general control on invocation line
C124 WARNING Unknown pragma, line ignored
C125 ERROR “ASM/ENDASM” requires SRC directive to be active
C126 ERROR Misplaced primary control, line ignored
C127 WARNING Misplaced control, line ignored
C129 ERROR Unclosed string
C130 ERROR Unclosed comment
C131 ERROR Unbalanced #if-endif controls
C132 ERROR Bad integer expression
C133 ERROR Unexpected in a formal list
C134 ERROR Pointer to field not allowed
C135 ERROR “interrupt/trap” cannot receive or return value
C136 ERROR Invalid base address
C137 ERROR “bit” member not allowed in struct/union
C138 ERROR Self relative struct/union not allowed
C139 ERROR A type declaration cannot follow “void”

- 154 -
ST7/STM8 C Compiler Manual 8. Error messages

C140 ERROR “void” type is invalid


C141 ERROR Function cannot return another function
C142 ERROR Function cannot return an array
C143 ERROR Illegal struct/union comparison
C144 ERROR Duplicate struct/union/enum tag
C145 ERROR Not a union tag
C146 ERROR Not a struct tag
C147 ERROR Duplicate struct/union member
C148 ERROR Illegal type conversion from/to 'void'
C149 ERROR Non-address/-constant initializer
C150 ERROR String out of bounds
C152 ERROR Memory space specifier illegal on struct/union member
C153 ERROR Not implemented
C154 ERROR “ASM/ENDASM” not allowed in include file
C155 ERROR Cannot open file
C156 ERROR Not an lvalue
C157 ERROR '*' illegal indirection
C161 ERROR Bit in illegal memory space
C162 ERROR Syntax error near…
C163 ERROR base^pos: invalid bit position
C165 WARNING Absolute address does not match the memory space
C166 ERROR Pointer to bit is not allowed
C167 ERROR Array of bits is not allowed
C171 WARNING Bit in reentrant function (char type assumed)
C172 ERROR Invalid absolute bit address
C173 ERROR Function member in struct/union
C176 ERROR Only one operand has bit type
C177 ERROR Illegal pointer conversion
C178 ERROR Qualified local variable not allowed
C179 WARNING Memory space on parameter ignored
C183 ERROR Function definition requires ANSI style parameter list
C185 ERROR Intrinsic declaration/activation error
C186 ERROR Function redefinition (different attribute)
C187 WARNING Variable redefinition (different qualifiers)
C188 FATAL System error
C192 WARNING Assignment between signed and unsigned char pointers
C193 ERROR Assignment from generic to typed pointers
C194 ERROR Not a valid identifier
C195 ERROR 'DATA': segment too large

- 155 -
ST7/STM8 C Compiler Manual

C199 ERROR 'BIT': segment too large


C200 ERROR Evaluation version: maximum object size reached
C202 WARNING Pointer: different memory space
C203 ERROR Pointer: different memory space
C204 ERROR pointer: const to non-const assignment
C205 WARNING “bit” not allowed as automatic variables
C206 ERROR “volatile” not allowed for function
C207 WARNING Suspicious pointer conversion
C217 ERROR Assignment from a far to a near pointer
C221 WARNING “bit” not allowed as parameter
C222 ERROR Not Implemented
C228 ERROR Recursive file inclusion
C230 ERROR Missing initialization
C231 WARNING 'typedef' and memory space cannot go together
C238 ERROR Function pointers may have up to 4 bytes of parameters
C239 ERROR Array cannot hold functions
C241 ERROR Not Implemented
C249 WARNING Bit fields are handled as unsigned
C251 WARNING Type "double" not handled, "float" assumed
C252 ERROR Memory space mismatch in redeclaration
C253 ERROR Too much data on stack
C255 WARNING Indirect calls allowed only for reentrant functions
C256 ERROR Improper use of “typedef”
C257 WARNING Function recursively calls itself without having 'reentrant' qualifier
C258 WARNING No more space on stack to store Common Sub Expression
C259 WARNING Object larger than 256 bytes in smallobject mode.
C262 ERROR Function at an absolute address above 0xFFFF in SMALL model.

- 156 -
ST7/STM8 C Compiler Manual 8. Error messages

8.1 FATAL ERROR S000: Insufficient memory


Cause: The host system is running out of memory

Resolution: Close some applications.

8.2 FATAL ERROR S001: File not found


Cause: A file has not been found

Resolution: Check the existence of the file, its extension and its access path

8.3 FATAL ERROR S002: Write error on file


Cause: Write error on file

Resolution: Check the file permissions. Verify that the file is not in use in another application.

8.4 FATAL ERROR S003: Read error on file


Cause: Read error on file

Resolution: Check the file permissions. Verify that the file is not in use in another application.

8.5 FATAL ERROR S004: Missing argument list


Cause: A directive or an option you used needs an argument that has not been specified

Resolution: Check your directives and the options. Refer on chapter 6 for more details

8.6 FATAL ERROR S005: Illegal file extension


Cause: Illegal file extension

Resolution: Change the extension or make sure you are using the right file

8.7 FATAL ERROR S006: Cannot open file


Cause: A file has not been found

Resolution: Check the existence of the file, its extension and the path to access it.

8.8 FATAL ERROR S008: User Break


Cause: The user has interrupted the compilation progress by clicking on the “Cancel”
button

Resolution: Recompile

- 157 -
ST7/STM8 C Compiler Manual

8.9 ERROR P000: Line too long


Cause: The maximum length of a line is 512 characters

Resolution: Shorten the line to <512 characters

8.10 ERROR P001: Incorrect end of line


Cause: The undef directive is used without a macro to undefined.

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

8.11 ERROR P002: Invalid character


Cause: The character is invalid for an identifier, keyword, etc…

Example: int de#de;

Resolution: Correct the identifier, keyword, etc …


An identifier must be a sequence of non digits characters (this includes the
underscore character “_”, uppercase and lowercase letters) and digits. The first
character shall be a non digit character.

8.12 ERROR P003: Invalid character


Cause: The character (hexadecimal value) is invalid for an identifier, keyword etc.

Example: int de#de;

Resolution: Correct the identifier, keyword, etc …

An identifier must be a sequence of non digits characters (this includes underscore


_character, uppercase and lowercase letters) and digits. The first character shall
be a on digit character.

8.13 ERROR P005: Unknown directive or control


Cause: The compiler encounters a directive that it did not recognize. It might not be a
directive, or you might misspell it.

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.

8.14 ERROR P006: Bad or missing file name


Cause: You are using the include directive without specifying a file name or having
specified a wrong file name.

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.

8.15 ERROR P007: Unexpected ENDIF


Cause: An unexpected endif directive has been encounter. An endif directive is used
without having previously used an if or elif directive.

Example: extern int b;


void foo(void)
{
b=10;
}

#endif

Resolution: Make sure all your endif directives are matching an if or elif directive.

8.16 ERROR P008: Unexpected ELSE


Cause: An unexpected else directive has been encounter. An else directive is used without
having previously used an if or elif directive.

Example: extern int b;


void foo(void)
{
b=10;
}
#else

Resolution: Make sure all your else directives are matching a if or elif directive.

- 159 -
ST7/STM8 C Compiler Manual

8.17 ERROR P009: Unexpected ELIF


Cause: An unexpected elif directive has been encounter. An elif directive is used without
having previously used an if directive.

Example: extern int b;


void foo(void)
{
b=10;
}
#elif

Resolution: Make sure all your else directives are matching an if or elif directive.

8.18 ERROR P010: Incorrect End Of File


Cause: This error happens when the end of the file is found in the middle of a
preprocessor block.

Example: #ifdef // if this is the last line of file, you get


// error P010

Resolution: Close correctly the preprocessor block

8.19 ERROR P012: Wrong number of parameters


Cause: You are using a macro without using the right number of argument.

Example: #define EQUAL(a,b) iequals(a, b)

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

8.20 ERROR P014: Redefinition of macro is not allowed


Cause: You are trying to define a macro with the same name than a macro already
existing.

Example: void iequals(int, int);

#define EQUAL(a,b) iequals(a, b)


#define EQUAL(a,b,c) fequals(a, b, c)

void foo(void)
{
int a=5,b=10,c=2;
EQUAL(a,b);
a=b+c;
}

Resolution: Make sure your macro names are unique.

8.21 ERROR P015: Macro cannot be modified


Cause: You are trying to redefine a symbol with the command line which has been already
defined within your code.

Example: #include <stdio.h>

#define FOO 5

extern int a;
void foo(void)
{
int b=2;
a=b+1;
}

Using the following in the command line: DEFINE(FOO=3)

Resolution: Make sure there are no conflicts between the definition on the command line and
the one specified within your code.

8.22 ERROR P016: Invalid numeric constant

Cause: You are using a numeric constant which is not valid.

Example: extern int a;


void foo(void)
{
int b=2;
a=b+0x0ft;
}

Resolution: Make sure your numeric constant is correct.

- 161 -
ST7/STM8 C Compiler Manual

8.23 ERROR P017: Incorrect number format


Cause: You are using a number which is not written in a good format.

Example: extern int a;


void foo(void)
{
int b=2;
a=b+0x0ft.3;
}

Resolution: Make sure your number is written properly.

8.24 ERROR P018: Invalid octal character


Cause: You are using an octal number composed of a number which is not include
between 0 and 7

Example: int i=09;

Resolution: Make sure your octal number is composed of numbers include between 0 and 7

8.25 ERROR P019: Integer constant out of range


Cause: The integer number you specify is out of range and cannot be located in a variable
of the type your specified.

Example: int i=12358879512;

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

8.26 ERROR P020: \x not followed by an hexadecimal digit


Cause: You wrote a string with a number inside it. This number is defined a using “\x” and
the character following can not be part of a number (neither binary, octal, decimal
or hexadecimal)

Example: void foobar()


{
printf("\xz34");
}

Resolution: Change the character after the “\x” or change the “\x”

- 162 -
ST7/STM8 C Compiler Manual 8. Error messages

8.27 ERROR P022: Use of DEFINED without argument


Cause: You are using the defined operator without specifying an argument

Example: #if defined

int a;

#endif

Resolution: Add the identifier you want to test

8.28 ERROR P023: Incorrect use of control or directive


Cause: This error is generated when pragma ENUMTYPE or SPACEDEF are not used
correctly.

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 …

8.29 ERROR P024: Missing macro name after ‘define’


Cause: No macro name has been specified after #define

Example: #define

Resolution: Complete the statement

8.30 ERROR P025: Identifier expected


Cause: An identifier (variable) is expected somewhere in a preprocessor block

Example: #ifdef // an identifier is expected after #ifdef

Resolution: Add the missing identifier

8.31 ERROR P027: string expected


Cause: This error is generated when using the directive #error without specifying a string

Example: #error

Resolution: Make sure that the #error directive is followed by a string

- 163 -
ST7/STM8 C Compiler Manual

8.32 ERROR P028: Generic error


Cause: This error is generated when directive #error is encountered

Example: #error this is an error // Outputs "this is an error" upon compilation

Resolution: This error is generated by the developer

8.33 ERROR P029: Duplicated formal parameter


Cause: This error is generated when a function is defined using the directive #defined and
a parameter is defined more than once

Example: #define fct(a,a) my_fct(a,a)

Instead of
#define fct(a,b) my_fct(a,a)

Resolution: Make sure that parameters are declared correctly

8.34 ERROR P030: Cannot remove predefined macro


Cause: This error is generated when you are trying to undefined a predefined macro

Example: #undef __LINE__

Resolution: Do not try to undefined predefined macro.

8.35 ERROR P031: Macro body cannot start or end with '##'
Cause: The body of a macro cannot start or end with ‘##’

Example: #define macro ##mybody

Resolution: Remove the ##

8.36 ERROR P032: new line expected, extra characters found


Cause: This error is generated when new-line does not follow the #include <file> directive

Example: #include <stdio.h>; int a;

Resolution: Make sure that an #include statement with its file is alone on its line.

8.37 ERROR P033: Directive does not apply to ST7


Cause: This warning indicates that you applied a STM8-reserved compiler directive
although being in ST7 mode

Example: #pragma ST7


#pragma LARGESTACK // LARGESTACK applicable only in STM8 mode

Resolution: Remove the spurious directive.

- 164 -
ST7/STM8 C Compiler Manual 8. Error messages

8.38 ERROR C000: Missing Character


Cause: The compiler has determined that a well defined character is missing in a line. The
missing character is shown together with the error message. The example below
produces error c000 if the function is the last of the source file.

Example: void fct(void)


{
// missing “}”

aa=3 // missing “;”

Resolution: Add the missing character.

8.39 ERROR C001: Specifier not allowed in the context


Cause: A specifier (keyword) has been found in a place where it is not allowed.

Example: void my_func(void)


{
at 0x80 char bb; // “at” specifier is not allowed within a function
}

Resolution: Remove the offending specifier

8.40 ERROR C002: Too many types


Cause: Too many types has been defined for the same variable.

Example: int char aa;

Resolution: Specify one type only.

8.41 ERROR C003: Undefined structure or union


Cause: Attempt to use an undefined structure.

Example: void fct(struct foo X); // Error if “struct foo” is not previously defined

Resolution: Define the structure before trying to use it.

- 165 -
ST7/STM8 C Compiler Manual

8.42 ERROR C004: Undefined enumeration


Cause: Attempt to use an undefined enumeration.

Example: void fct(enum foo X); // Error if “struct foo” is not previously defined

Resolution: Define the enumeration before trying to use it.

8.43 ERROR C005: Type error in re-declaration


Cause: A variable has been re-declared with a different type. Note that when you re-
declare a variable usually you get error C006, apart some cases where re-
declaring is allowed (typically, for extern variables)

Example: extern int c;


extern char c;

Resolution: Remove one of the declarations, or make them use the same type.

8.44 ERROR C006: Symbol defined more than once


Cause: A symbol (typically a variable) had been defined more than once

Example: int aa; char aa;

Resolution: Remove the redundant definition(s)

8.45 ERROR C007: Invalid type in definition


Cause: When declaring a function with parameters or its prototype, at least the type of the
parameters must appear in the parameter list. The parameter’s name can be omit
but the type must be present.

Example: void fct(a);

Resolution: Add the type of the parameter

- 166 -
ST7/STM8 C Compiler Manual 8. Error messages

8.46 ERROR C008: Parameter list longer than the prototype


Cause: A function has been called with more parameters than are in the function
definition.

Example: void foo(void)


{
while(1);
}

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

8.47 ERROR C009: Parameter list smaller than the prototype


Cause: A function has been called with less parameters than are in the function definition.

Example: void foo(int a)


{
while(1);
}

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

8.48 ERROR C010: not a parameter


Cause: When using K&R syntax, there’s a mismatch between the parameters declared in
the function header and those declared as local variables

Example: int fct(a,b) int c; {;} // a or b would work, c is not in the list

Resolution: Change the parameters names.

8.49 ERROR C011: Invalid syntax


Cause: This error is a general error that means a syntax error

Resolution: Correct the syntax

- 167 -
ST7/STM8 C Compiler Manual

8.50 ERROR C014: Undefined symbol


Cause: A symbol (variable) has been found that was not defined anywhere.

Example: void main(void)


{
aa=1; // aa is not defined
while(1);
}

Resolution: In C, all symbols (variables) must be defined before they can be used

8.51 ERROR C015: unary operator not followed by an operand


Cause: An operator for whose one operand is expected has been found with no operands

Example: +; // +aa; would work

Resolution: Add the missing operand

8.52 ERROR C017: Cast not followed by an operand


Cause: A cast has been found, but no operand for it to act on

Example: (char); // (char)aa; would work

Resolution: Add the missing operand

8.53 ERROR C018: variable cannot be subscripted (not an array)


Cause: The code is trying to use a variable as an array, but the variable is not an array.
The same error happens if you try to use an array with more indexes than it can
take

Example: int aa;


aa[3]=2; // aa is not an array

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.

8.54 WARNING C020: '%s' is assigned a value that is never used


Cause: A variable is assigned a value but this value is never used in the function

Example: void foo()


{
int j=2;

do_nothing();
do_something();
}

- 168 -
ST7/STM8 C Compiler Manual 8. Error messages

Resolution: Remove the unused initialization

8.55 WARNING C021: Type is defined more than once


Cause: You specified a type, for a unique declaration, more than once

Example: int int a;

Resolution: Remove the redundant type names

8.56 ERROR C022: '%s' requires a structure or union


Cause: The left side of a . or a -> requires a structure or a union

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

8.57 ERROR C023: A field must be an identifier


Cause: Left side of a . or -> operator must be an identifier declared in the definition of the
structure or the union

Example: struct myStruct


{
char a;
int b;
} *foo;

foo->3 = 2;

Resolution: Change the name of the field you are trying to access

8.58 ERROR C024: Unknown field


Cause: This error is related to structures and happens when you try to use a structure field
that is not defined

Example: struct {
int a;
char b;
} hh;

void main(void)
{
hh.c=3; // c is not a field of the structure
while(1);
}

Resolution: Define all structure fields

- 169 -
ST7/STM8 C Compiler Manual

8.59 ERROR C026: Wrong argument type


Cause: You are calling a function with an argument that does not have the correct type
and that can not be convert.

Example: void foo(int a, char *b);

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

8.60 ERROR C027: 'generic' is not legal in this context


Cause: You are using the keyword generic in a context that is not legal.

Example: int generic i;

Resolution: The generic keyword can only be used to declare pointers.

8.61 ERROR C029: requires integer operand


Cause: You are trying to perform an operation on a non integer operand but the operator
can only apply to an integer operand

Example: float t3;


t3 >>1;

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.

8.62 ERROR C030: requires arithmetic operand


Cause: You are trying to perform an operation which needs two arithmetic operands.

Example: char foo[] = "bar";


int i=4;

foo > i;

Resolution: Change the non-arithmetic operand

- 170 -
ST7/STM8 C Compiler Manual 8. Error messages

8.63 ERROR C031: Type invalid for operator


Cause: An operator cannot be applied to a given operand type.

Example: #include <stdio.h>


float a;

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.

8.64 WARNING C032: Division by 0


Cause: A division by zero has been detected

Example: aa = 1 / 0;

Resolution: Remove the division by zero

8.65 ERROR C033: Type not allowed for bit field


Cause: This error is related to structures and happens when you try to declare a bit field
on a type that does not support it

Example: struct
{
float a:7;// float cannot support bit fields
char c;
} hh;

Resolution: Change the type to int, unsigned int or signed int

8.66 ERROR C034: Bit field too long


Cause: This error is related to structures and happens when you declare a bit field with a
length greater than 8

Example: struct
{
int a:9; // max is 8!
char c;
} hh;

Resolution: Reduce the length of the bit field to a maximum of 8

- 171 -
ST7/STM8 C Compiler Manual

8.67 FATAL ERROR C036: Too many errors


Cause: Too many compilation errors have been found: the compiling process is aborted

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)

8.68 ERROR C037: Too many warnings


Cause: Too many compilation warnings have been found: the compiling process is
aborted

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)

8.69 ERROR C038: Type qualifier not allowed with a function


Cause: The return value of a function cannot be specified to be in a particular memory
space

Example: data char ftc(void) // “data” will cause error c038


{
return(1);
}

Resolution: Remove the type specifier

8.70 ERROR C039: Type returned is invalid


Cause: The function returns a value of a type that is not allowed as a return type.

Example: bit foo(int a)


{
...
}

Resolution: Change the type of the value returned by the function.

8.71 ERROR C040: Type conversion impossible


Cause: You are trying to perform a conversion which is not possible. That can be for
multiple reasons:
• conversion between variables and pointers having different qualifiers
• conversion between const and non-const.

Example: int generic *a = 3;

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

8.72 FATAL ERROR C041: Internal error during code generation


Cause: The compiler has found an unexpected situation that it cannot handle.

Example: This should never happen.

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

8.73 WARNING C042: Condition always false


Cause: You wrote a condition of an if-else statement that is always false

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

8.74 WARNING C043: Condition always true


Cause: You wrote a condition of an if-else statement that is always false

Example: if (1==1) // This test is always true


{
... // code will always be executed here
}

Resolution: You can remove the if-else statement and just let the true case since the condition
is always true

8.75 ERROR C045: Invalid function call


Cause: This error is generated when a function call is detected with an identifier that is not
a function

Example: int aa;


aa(); // aa is not a function name

Resolution: Make sure you call the right function or that the function is correctly declared

8.76 ERROR C047: Invalid initialization


Cause: An initialized variable has an initialization value that is inconsistent with its type

Example: int aa = 0.5; // an integer cannot be 0.5

Resolution: Change the type or the initialization value in such a way that they are consistent

- 173 -
ST7/STM8 C Compiler Manual

8.77 ERROR C048: Too many initializations


Cause: You are initializing a structure with more initializer than field available in the
structure

Example: struct myStr


{
int a;
int b;
} foo = {2, 3, 4};

Resolution: Remove the useless initializer

8.78 ERROR C049: Assignment of a constant


Cause: The code tried to assign a vale to a constant

Example: 3 = 2;

Resolution: Remove the wrong assignment.

8.79 ERROR C050: Invalid operation on pointers


Cause: This error is produce when trying to compare (!=, >, <, >=, <=) two pointers that
can not be compared. Two pointers can not be compared if they don’t pointed the
same type, unless they are explicitly cast

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.

8.80 ERROR C052: Operand is not an lvalue


Cause: You are doing an operation on an object that can not be on the left side of an
expression. That is, the object can not be modified

Example: const int a;

a++;

Resolution: Make sure you are trying to modify the right object.

- 174 -
ST7/STM8 C Compiler Manual 8. Error messages

8.81 ERROR C053: Absolute allocation missing


Cause: Symbols and variables in some memory spaces (hreg, hbit, hreg16) must be
specified at an absolute location.

Example: hreg babao // Correct syntax is: at <address> hreg babao

Resolution: Add the absolute address as shown in the example

8.82 ERROR C054: Too many storage classes


Cause: You defined an object with too much storage specifier

Example: at 0 typedef a;
at 0 extern int b=0;

Resolution: Remove the useless storage class.

Note:
The keyword is a storage specifier.

8.83 ERROR C055: Constant expression required


Cause: A constant expression is required at a location where you putted a variable

Example: int i;
int a[i];

Resolution: Remove the variable and put a constant expression

8.84 ERROR C056: Syntax error


Cause: Can mean just about anything. Any error that does not have a specific reason
ends up as being a syntax error

Example: N/A

Resolution: Check the syntax of your source

8.85 ERROR C057: Duplicated specifier


Cause: The specifier reentrant is duplicated in the declaration of a function

Example: void foo() reentrant reentrant;

Resolution: Remove one of the specifier

- 175 -
ST7/STM8 C Compiler Manual

8.86 ERROR C058: Parameter out of range

Cause: Using a function specifier that needs an integer parameter, you specify a
parameter which is out of the allowed range

Example: void foo(void) interrupt 223


{
int a;
a++;
}

Resolution: Replace the parameter by a correct value

8.87 ERROR C062: Illegal type qualifier


Cause: The type qualifier used can not be used that way

Example: bit * p; // Pointers on bit are not allowed

Resolution: Use another type.

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

Example: void foo(int a, void *b)


{
b+a;
}

Resolution: Change the type of the pointer to char *

8.89 ERROR C066: 'case' duplicated


Cause: This error is related to a switch statement and happens when two CASE
statements are specified for the same value of the switch variable

Example: switch (aa)


{
case 1: break;
case 1: break; // case 1 has already been specified
}

Resolution: Remove the duplicated case statement

- 176 -
ST7/STM8 C Compiler Manual 8. Error messages

8.90 ERROR C067: 'default' duplicated


Cause: This error is related to a switch statement and happens when two DEFAULT
statements are detected

Example: switch (aa)


{
default: break;
default: break; // default has already been specified
}

Resolution: Remove the redundant default statement

8.91 ERROR C068: Unexpected '%s'


Cause: A break or a continue is not enclosed by a loop instruction

Example: void foo(int a)


{
a++;
continue;
}

Resolution: Remove the continue or the break statement or put a loop instruction to enclosed it

8.92 ERROR C069: missing enclosing switch statement


Cause: A case statement is used without being enclosed by a switch statement

Example: void foo(int a)


{
a++;
case 1:
a--;
}

Resolution: Remove the case statement or add a switch statement

8.93 ERROR C071: A void function cannot return a value


Cause: A return statement has been found in a function that is not supposed to return a
value

Example: void check_errors(void)


{
return(3);
}

Resolution: Remove the return statement or change the type of the function from void to
something else

- 177 -
ST7/STM8 C Compiler Manual

8.94 ERROR C072: void type not allowed


Cause: The void type has been used in a place where it is not allowed.

Example: void aa; // You can’t declare a void variable

Resolution: Change to another type

8.95 ERROR C073: duplicated function parameter


Cause: The same parameter has been used more than once in the parameter list of a
function declaration

Example: void fct (int aa, int aa) {}

Resolution: Remove the duplicated parameter

8.96 ERROR C074: Invalid declaration syntax


Cause: There is a syntax error in a declaration

Example: extern int a

Resolution: Correct the syntax

8.97 ERROR C075: ( missing


Cause: An open parenthesis is missing.

Example: while 1); // should be while(1);

Resolution: Add the missing parenthesis

8.98 ERROR C076: ) missing


Cause: A closing parenthesis is missing

Example: while (1 ; // should be while (1);

Resolution: Add the missing parenthesis

8.99 ERROR C077: Invalid expression syntax


Cause: This is a generic error that applies to an expression and means that the
expression is incomplete or wrong.

Example: aa = ; // Missing the right side of equation

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

8.100 ERROR C078: Invalid instruction '%s'


Cause: You are performing an invalid instruction

Example: void foo(int a)


{
a++;

if(a > 3)
goto 2;
}

Resolution: Remove the invalid instruction

8.101 ERROR C079: Invalid type for a register declaration


Cause: Array and structures are not allowed in registers

Example: void foo(void)


{
register int array[4] = {1,2,3,4};

}

Resolution: Remove the register qualifier from the declaration.

8.102 ERROR C080: Addressing of a register variable is not allowed


Cause: The addressing of a variable that has the type qualifier register is not allowed

Example: register int i;


j = &i;

Resolution: Remove the register qualifier from the variable declaration or do not retrieve the
address of the variable

8.103 ERROR C081: Identifier expected


Cause: The identifier is missing in a declaration

Example: int;
int a = 0;

Resolution: Add the identifier to the declaration

- 179 -
ST7/STM8 C Compiler Manual

8.104 ERROR C083: Undefined or null array size


Cause: An array has been defined without specifying its size or the array has been specify
with a size of zero

Example: int aa[5]; // ok


int aa[]; // error c083
int aa[0]; // error c083

Resolution: Specify a non zero size for the array

8.105 ERROR C084: Variable cannot be initialized


Cause: You are trying to initialize an object which can not be initialized

Example: void foo(void) = 0;

Resolution: Remove the initializer

8.106 ERROR C085: Invalid conversion


Cause: You are trying to initialize a constant pointer to a generic pointer

Example: void foo(int *c)


{
const int *b = c;
generic int *a = b;
}

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

8.107 ERROR C086: Assignment of integer to enumeration


Cause: You are assigning an integer to an enumeration variable. It is possible that the
value of the integer could not be represented by the enumeration.

Example: enum {ONE=1, TWO, THREE} enumer;

void foo(int i)
{
enumer = i;
}

Resolution: Change the integer to an enumeration.

- 180 -
ST7/STM8 C Compiler Manual 8. Error messages

8.108 ERROR C087: Identified bit field is of null size


Cause: In a structure, a bit field of size 0 has been found

Example: struct
{
int a:0; // not allowed
char c;
} bf;

Resolution: Bit fields size must be in the range of 1 to 8

8.109 ERROR C088: Function defined more than once


Cause: A function has been defined more than once

Example: void fct (void) {};


void fct (void) {}; // fct has already been defined

Resolution: Remove the duplicate definition or rename one of the functions

8.110 WARNING C089: Function should return a value


Cause: There is no return statement in a function whose return type is different than void. Note that if you
don’t specify the return type for a function it is assumed to be integer

Example: char fct(void) { } ; // The body of the function should


// contain a return statement

Resolution: Add a return statement returning something of the right type or change the type of
the function to void

8.111 WARNING C090: Call to function without prototype


Cause: A function has been called but has not yet been defined

Example: void main(void )


{
foo(); // function is not defined
}

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.

8.112 ERROR C091: Invalid parameter declaration


Cause: There is an error in the declaration of one (or more) of a function parameter

Example: void fct(ont my_var) {}; // Note “ont” instead of “int”

Resolution: Make sure that all parameters are correctly declared

- 181 -
ST7/STM8 C Compiler Manual

8.113 WARNING C092: Symbol is declared but not used


Cause: A symbol is declared but it is never used

Example: void fct(int my_var) {}; // my_var is never used


// within the function

Resolution: Remove the useless symbol declaration

8.114 ERROR C093: 'do' without 'while'


Cause: The while statement is missing in a do .. while loop

Example: do {} while(1); // correct


do {}; // error c093

Resolution: Add the missing while clause

8.115 ERROR C094: An expression is missing


Cause: An expression is missing. This error can appear in three different cases:
• With a function, a function specifier needs a parameter and the parameter is
missing
• With a bitfield, the size of the bitfield is missing
• With enumeration,

Example: void foo(void) interrupt


struct bitfield
{
int a:2;
int b:;
} myBitflield;

typedef enum {One=, Two, Three} myEnum;

Resolution: Add the missing expression

8.116 ERROR C095: '%s' without ';'


Cause: You forgot a ‘;’ after a keyword of the language

Example: void foo(int a)


{
break
}

Resolution: Add the ‘;’

- 182 -
ST7/STM8 C Compiler Manual 8. Error messages

8.117 WARNING C096: Function should return a value


Cause: There is no return statement in a function whose type is not void. Note that if you
don’t specify the return type for a function it is assumed to be integer

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

8.118 ERROR C097: Undefined label


Cause: Attempt to use an undefined label

Example: goto bar; // Error if “bar:” is not found within the function

Resolution: Define the missing label

8.119 WARNING C098: 'goto' bypasses initialization of a local variable


Cause: A goto statement makes the execution of the code to jump over the initialization of
a local variable.

Example: goto Lab;


{
int b=2;

Lab:
a += b;
}

Resolution: Move your label before the initialization of local variable or move the jump after the
initialization of the local variable.

8.120 ERROR C099: Label duplicated


Cause: A label has been defined more than once.

Example: lab: //some code


lab: //duplication

Resolution: Remove the redundant label or rename one of them

- 183 -
ST7/STM8 C Compiler Manual

8.121 ERROR C100: Operand not allowed with '&'


Cause: The addressing of bitfield or variable declared with the register type qualifier are
not allowed

Example: struct bitfields


{
int a:4;
} myBitField;

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

8.122 ERROR C101: Invalid type control expression


Cause: The type of the control expression in the for loop is invalid

Example: int b,a=0;


struct {
char a;
int b;
} c;

for(b=0; c; a++) // c -> a structure is invalid here


{
a--;
}

Resolution: Change the control expression

8.123 ERROR C103: Misplaced directive or control


Cause: A directive or control (or something that is interpreted as such, like most keywords)
has been found in a place where it should not be

Example: void main(void)


{
int aa;
interrupt(); // interrupt is interpreted as a control
while(1);
}

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

8.124 ERROR C104: Invalid parameter declaration


Cause: A parameter declared in the function declaration has an invalid declaration

Example: int foo(int)


{
... // some code
}

Resolution Check the declaration of each parameter and make sure they are valid.

8.125 WARNING C105: Possibly incorrect assignment


Cause: You are doing an assignation that may be incorrect

Example: int a, int b;

if (a=b)
return 1;

Resolution: Make sure you really want to do the assignation as it is written. If not, correct it.

8.126 ERROR C107: 'sizeof' cannot be applied to a function


Cause: The function sizeof cannot be applied to a function

Example: void fct(void) {};


aa = sizeof(fct); // error c107

Resolution: Use sizeof only with variables

8.127 ERROR C108: Unexpected character


Cause: An unexpected character is present

Example: int a; }

Resolution: Remove the unexpected character.

8.128 WARNING C110: assignment of generic pointer from code


Cause: A generic pointer is assigned a pointer on code. This is allowed (assignment of
code pointer to generic is not) but is a possible programming error

Example: char code * code_ptr;


char generic * generic_ptr;

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

8.129 WARNING C111: Operation with void type


Cause: An operation is done on a void type

Example: int foo(void *a)


{
if (a++)
return 1;
return 0;
}

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);

A correct implementation of function foo() could be:

int foo(void *a)


{
int *b = (int *)a;
if( b++ )
return 1;
return 0;
}

8.130 FATAL ERROR C114: Unknown control


Cause: This error is generated when an option on the command line is unknown or do not
have the right parameter(s).

Example: rcstm8.exe c:\tmp\foo.c NOTANOPTION

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, …)

8.131 ERROR C115: '(' after control expected


Cause: An option or a pragma is used but the needed parameter is not specified.

Example: rcstm8.exe c:\tmp\foo.c DEFINE

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

8.132 ERROR C116: ')' After control expected


Cause: This error is generated when using options or pragmas with parameters and that
the closing parenthesis is not present.

Example: rcstm8.exe c:\tmp\foo.c DEFINE(FOO

or

#pragma OT(4

Resolution: Make sure that the pragmas or the options are correctly written

8.133 FATAL C118: Bad decimal number


Cause: This error is generated when specifying a decimal option parameter that is not
correct. For example, the optimization level that must be specified using pragma
OT must be between 0 and 7. Specifying 9 leads to this error.

Example: rcstm8.exe c:\tmp\foo.c OT(9)

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.

8.134 FATAL C119: Number out of range


Cause: A value that is not within the allowed range has been found in a directive or
control. The compilation is aborted.

Example: #pragma defaultLC(data) // OK


#pragma defaultMC() // Fatal error C119

Resolution: See chapter 6 to find out what the legal values are for each directive.

8.135 FATAL C120: Identifier expected


Cause: An identifier is expected as a parameter of an option or a directive and this
parameter is missing

Example: rcstm8.exe c:\tmp\tutu.c DEFINE(

or

#pragma OT()

Resolution: Specify the parameter of the option or the directive

- 187 -
ST7/STM8 C Compiler Manual

8.136 FATAL C123: Cannot have general control on invocation line


Cause: Use, on the command line, of a pragma that can not be used on the command line

Example: rcstm8.exe c:\tmp\foo.c RESTORE

Resolution: Use the pragma in the source file

8.137 WARNING C124: Unknown #pragma, line ignored


Cause: An unknown keyword has been found after #pragma

Example: #pragma foo

Resolution: Use a valid pragma (see chapter 6 for the list) or remove the line

8.138 ERROR C125: 'ASM/ENDASM' requires SRC directive to be active


Cause: The ASM or ENDASM directive have been found, but the SRC directive is not
active

Example: N/A

Resolution: See Chapter 6 for an example of how to use the ASM/ENDASM directives

8.139 ERROR C126: Misplaced primary control, line ignored


Cause: Some directives can only be specified as the first line of the source file (before any
code), otherwise they cannot be processed. When one such directive is specified
in the wrong position you get error C126

Example: #pragma PIN("c:\babao")


#include "../includefiles/testframe.h"
#pragma PIN("c:\abcd") // this line produce error C126

Resolution: Move the pragma at the beginning of the file, before any statement

8.140 WARNING C127: Misplaced control '%s', line ignored


Cause: A directive is misplaced within the source code.

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

8.141 ERROR C129: Unclosed string


Cause: A string is not closed with a double quote.

Example: char tab[] = "abcd; // Missing " at the end

Resolution: Close the string with a double quote

8.142 ERROR C130: Unclosed comment


Cause: A comment open with /* is not closed with */. Note that the fact of forgetting to
close a comment might have many different results depending on where the
comment is opened.

Example: /* Error C130 if this is the last line of the file

Resolution: Close the comment with */

8.143 ERROR C131: Unbalanced #if-endif controls


Cause: An #ifdef has been found without a corresponding #endif (or viceversa)

Example: #ifdef foo // should be closed by an #endif

Resolution: Make sure that if and endif are correctly balanced

- 189 -
ST7/STM8 C Compiler Manual

8.144 ERROR C132: Bad integer expression


Cause: This numeric control is done only on integer used with #if directive

Example: int foo(int a)


{
#if 0x2g
if (a++)
return 1;
#endif
return 0;
}

Resolution: Make sure the numeric value used is a correct integer expression. In the example
below 0x2f would be correct

8.145 ERROR C133: '%s' unexpected in a formal list


Cause: Error generated when parsing arguments of a macro definition.

Example: #define macro(a,

Resolution: Complete the definition of the macro

8.146 ERROR C134: Pointer to field not allowed


Cause: In a structure, a pointer has been found for a field that cannot accept it.

Example: struct
{
int *a:7;
char c;
} *hh;

hh->a;

Resolution: Remove the pointer and access to the field another way

8.147 ERROR C135: Interrupt/trap may not receive or return value(s)


Cause: An interrupt function shall not have parameters and shall not return any value.

Example: void foo(int a) interrupt 3


{
}

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

8.148 ERROR C136: Invalid base address


Cause: The base address used for a declaration is not valid in the context.

Example: #pragma ST7


at 0x300 hreg hh; // Base address must be below 0x100 for ST7

Resolution: Change the base address to an appropriate value.

8.149 ERROR C137: Bit member not allowed in struct/union


Cause: Bits are not allowed as members of structures or unions

Example: struct
{
bit r; // not allowed
int a;
char c;
} hh;

Resolution: Use another type to represent the value

8.150 ERROR C138: Self relative struct/union


Cause: A structure type definition uses its proper type, not being a pointer, in its
declaration.

Example: struct str


{
struct str a;
} STR;

Resolution: In the definition of a structure type, this structure type must not be used if it is not
used as a pointer.

8.151 ERROR C139: A TYPE declaration cannot follow VOID


Cause: In a function declaration, you specified a parameter after declaring that the
function did not accept parameters

Example: int foo(void, int a)


{

}

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.

Note: Void type


Void type cannot be used for a parameter if it is not a pointer, thus void*

- 191 -
ST7/STM8 C Compiler Manual

8.152 ERROR C140: Void type is invalid


Cause: A void type is used in a parameters list of a function declaration, but not as the first
parameter.

Example: int foo(int a, void)


{

}

Resolution: Remove the void keyword or add a name to the parameter and change the type.

8.153 ERROR C141: Function cannot return another function


Cause: It is not possible for a function to return a function.

Example: typedef void foo(void);

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:

typedef void (*foo)(void);

8.154 ERROR C142: Function cannot return an array


Cause: It is illegal to return an array in C.

Example: char foo(void)[10]; // Illegal: A function cannot return an array in C

Resolution: Return your value through a shared memory area.

8.155 ERROR C143: Illegal struct/union comparison


Cause: You are trying to make an illegal comparison between two structures or unions.

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

8.156 ERROR C144: duplicate struct/union/enum tag


Cause: You are using a structure, enumeration or union tag that is already used.

Example: struct S1 { int x; };


struct S1 { int x; };

Resolution: Change the name of one of the tag

8.157 ERROR C145: Not a union tag


Cause: You are defining a union using a tag which is not a union tag

Example: struct S1 { int x; char y; };


union S1 foo;

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:

union S1 { int x; char y; };


union S1 foo;

or

struct S1 { int x; char y; };


struct S1 foo;

8.158 ERROR C146: Not a struct tag


Cause: You are defining a structure using a tag which is not a structure tag

Example: union S1 { int x; char y; };


struct S1 foo;

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:

struct S1 { int x; char y; };


struct S1 foo;

or

union S1 { int x; char y; };


union S1 foo;

- 193 -
ST7/STM8 C Compiler Manual

8.159 ERROR C147: Duplicate struct/union member


Cause: In a structure or union, a field has been defined more than once

Example: struct
{
int a;
char a; // a has already been defined
} hh;

Resolution: Remove the duplicated definition

8.160 ERROR C148: Illegal type conversion from/to 'void'


Cause: You are trying to perform a conversion from a void expression

Example: void foo(void *a)


{
char b = *a;
}

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*

8.161 ERROR C149: Non-address/-constant initializer


Cause: An initialization value can only be a constant or an address

Example: int aa;


int bb=aa; // a variable is not allowed as initializer

Resolution: Use a constant or move your initialization into a function using an assignment

8.162 ERROR C150: String out of bounds


Cause: The string you are initializing is not large enough to contain the string initializer

Example: char b[9] = "1234567890";

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

8.163 ERROR C152: Memory space specifier illegal on struct/union member


Cause: A memory space specifier can be used on a structure/union as a whole, but not on
a single member

Example: struct
{
int data a; // “data” is not allowed here
char b;
} hh;

Resolution: Remove the memory space specifier

8.164 ERROR C154: 'ASM/ENDASM' not allowed in include file


Cause: The ASM and ENDASM directives cannot be used in include files.

Example: N/A

Resolution: Move your assembler code in the main source file

8.165 ERROR C155: Cannot open file


Cause: The specified file can not be found.

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

8.166 ERROR C156: Not an lvalue


Cause: An address operator was given an operator which is not a lvalue.

Example: int* bar(void)


{
return &3; // Error C156: "3" is not an lvalue
}

Resolution: The address operator must be given an addressable object.

8.167 ERROR C157: Illegal indirection


Cause: You are trying to perform an indirection on an object that is not a pointer.

Example: int foo;

void bar()
{
*foo;
}

Resolution: Remove the indirection or change the type of your object

- 195 -
ST7/STM8 C Compiler Manual

8.168 ERROR C159: Missing character


Cause: A string closing character is missing.

Example: // Next line has a missing '>' string closing character


#include <stdio.h

Resolution: Add the forgotten character.

8.169 ERROR C161: Bit in illegal memory space


Cause: Bits can only be defined in certain memory spaces

Example: bit code y; // bits are not allowed in code memory

Resolution: Change the memory space or use chars instead

8.170 ERROR C162: Syntax error near '%s'


Cause: This is a very generic error message that says that a syntax error (something that
cannot be understood by the compiler) has been found. The error message
specifies where in the line the error is (near <something>)

Example: int 12; // just an example

Resolution: Correct the syntax error

8.171 ERROR C163: Base^pos: invalid bit position


Cause: A specified bit position was not in the range 0..7.

Example: at 0x80^9 data bit foo; // Error C163: Only bits 0-7 allowed

Resolution: Remap your bits so that they are on byte positions.


For instance, replace the erroneous example above by
at 0x81^1 data bit foo; // OK: This fits our needs.

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

8.173 ERROR C166: Pointer to bit is not allowed


Cause: Pointer to bit are not allowed

Example: void foo()


{
bit *bar;
}

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.174 ERROR C167: Array of bits is not allowed


Cause: Arrays of bits are not allowed.

Example: bit b[4];

Resolution: Use another type for the array

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”.

Example: void my_funct(void)


{
bit a; // a will be handled as a char
}

Resolution: Change your variables to type “char” to remove the warning

8.176 ERROR C172: Invalid absolute bit address


Cause: Bits can only be in a well specified address range. If you specify an absolute bit
which is not in the range you get error c172

Example: at 99999 bit x; // address not allowed for bits

Resolution: Put your bits in data memory (the limit depends on the derivative you are using)

8.177 ERROR C173: Function member in struct/union


Cause: Structures and unions cannot contain functions

Example: struct
{
int a;
char b(); // functions not allowed!
} hh;

Resolution: Remove the function

8.178 ERROR C176: Only one operand has bit type


Cause: An operation has been found where the two operands should be of type bit, but
only one is.

Example: void main(void)


{
bit x;
int a;
x |= a; // only allowed with 2 bits
while(1);
}

Resolution: Change the operation or the type of the non-bit parameter

- 197 -
ST7/STM8 C Compiler Manual

8.179 ERROR C177: Illegal pointer conversion


Cause: The operation you are trying to perform implies a pointer conversion which is not
legal

Example: char a;
int *b;

a = b;

Resolution: Change the expression to eliminate the implicit pointer conversion or cast the
pointer

8.180 WARNING C178: Qualified local variable not allowed


Cause: You added a qualifier (such as volatile, data, page0) to a local variable. The
memory (or register) location of local variables is done by the compiler
automatically and cannot be manually changed.

Example: int foo()


{
data int a=2; // Warning C178
...
}

Resolution: Several solutions can help here.


- Remove the local variable qualifier.
- Set the variable as global.
- Set the variable as static.

8.181 WARNING C179: Memory space on parameter ignored


Cause: You cannot specify the memory space on a function parameter

Example: void my_funct(data int aa); “data” is ignored

Resolution: Remove the memory space specifier

8.182 WARNING C183: Funcdef requires ANSI-parameter list


Cause: You are calling a function with parameters without having specified the function
prototype

Example: void foo()


{
int a = 2, b = 3;
foo(a, b));
}

Resolution: Add the function’s prototype before using it

- 198 -
ST7/STM8 C Compiler Manual 8. Error messages

8.183 ERROR C185: Intrinsic '%s': declaration/activation error


Cause: You are using an intrinsic function needing parameters without specifying them

Example: _jmp_(); // Argument is missing

Resolution: Add the required argument.

8.184 ERROR C186: Function redefinition (different attribute)


Cause: A function prototype has been found more than once, with different attributes
(typically reentrant or interrupt)

Example: int foo(void) reentrant;


int foo(void);

Resolution: Correct the prototypes is such a way that they are all the same

8.185 WARNING C187: Variable redefinition (different qualifiers)


Cause: A variable has been defined with some qualifiers, and is declared a second time
with different qualifiers.

Example: extern int foo;

at 0x80 int foo; // Warning C187: Variable redefinition (different qualifiers)

Resolution: Correct the declarations of the given variable so that they match.

8.186 FATAL C188: System error


Cause: Generic error with the system resources. The kind of error is specified in the text.

Example:

Resolution: Refer to the error text.

8.187 WARNING C192: Assignment between pointers to char signed and unsigned
Cause: You are an assignation between a signed and an unsigned char

Example: unsigned char *a;


signed char *b;

a = b;

Resolution: Explicitly cast your second operand into the type of the first operand.

a = (unsigned char *)b;

8.188 ERROR C193: Assignment from generic to typed pointers


Cause: You are assigning a generic pointer to a qualified pointer

- 199 -
ST7/STM8 C Compiler Manual

Example: data int* a1;


generic int* a2;

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

8.189 ERROR C194: Not a valid identifier


Cause: You are defining a macro that needs argument and one of those arguments do not
have a valid identifier

Example: #define foo(a, ?)

Resolution: Change the wrong identifier

8.190 ERROR C195: 'DATA': segment too large


Cause: The size of variables to be stored in “data” memory (current source file) exceeds
the memory itself

Example: data long arr[20000];

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.

8.191 ERROR C199: 'BIT': segment too large


Cause: Too many variables of type bit have been declared.

Resolution: Remove some of the variables of type bit and convert them to unsigned chars.

8.192 ERROR C200: Evaluation version: maximum object size reached


Cause: You have reached the limit of the evaluation version. If you want to compile a file of
this dimension you need to get a license.

Resolution: Contact Raisonance by emailing support@raisonance.com

- 201 -
ST7/STM8 C Compiler Manual

8.193 ERROR C203: Pointer: different mspace


Cause: You are performing an operation with two pointers pointing in different memory
spaces

Example: data int *a;


page0 int *b;

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.

8.194 ERROR C204: Pointer: const to non-const assignment


Cause: An attempt was make to assign a const pointer to a non-const. This usually
happens when you use const-qualified pointers (in order to enforce your code's
safety) and pass the pointer to a function expecting a non-const pointer.
The C language forbids assignment of a const pointer to a non-const.

Example: const char * constptr = "Hello";

void foo(char *ptr)


{
... // Some code here
}

void main(void)
{
foo(constptr); // Error 204: const to non-const assignment
}

Resolution: Different approaches can be taken to solve this problem:


● Make your pointer non-const. this has the undesirable effect of weakening your code.
● Cast your pointer to non-const upon assignment. For example, in the code fragment above,
replace the call to foo() by:

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:

void foo(const char *ptr)

- 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

Example: #pragma auto

void my_funct(void)
{
bit a;
}

Resolution: Change the type for a char or do not use the automatic mode

8.196 ERROR C206: Volatile not allowed for function


Cause: The keyword “volatile” is not allowed with functions

Example: volatile void my_func(void) {}

Resolution: Remove the “volatile” qualifier.

8.197 WARNING C207: Suspicious pointer conversion


Cause: The operation you are doing performs an implicit conversion that seems
suspicious to the compiler. Some information might be lost during this implicit
conversion

Example: unsigned int *a;


signed char *b;

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".

Example: #include <stdio.h>

void printbit(bit t) // Warning C221: bit not allowed as parameter


{
if(t)
putchar('1');
else
putchar('0');
}

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

8.199 FATAL C228: Recursive file inclusion


Cause: A file includes itself, directly or indirectly

Example: The following is an example of direct file inclusion:


- content of file.c:

#include “file1.h”

-content of file1.h:

#include “file1.h”

The following is an example of indirect file inclusion:


- content of file.c:

#include “file1.h”

-content of file1.h:

#include “file2.h”

-content of file2.h:

#include “file1.h”

Resolution: Do not include file that could lead to a recursive inclusion

8.200 ERROR C230: Missing initialization


Cause: Variables in the code memory space must be initialized (as they cannot be written
later on)

Example: code int array; // missing initialization

Resolution: Add an initialization or move the variable in a write-able memory space

8.201 WARNING C231: ‘Typedef’ and memory space cannot go together. Memory
Space ignored
Cause: Memory spaces cannot be applied using a typedef

Example: typedef hbit foo // Warning C231: "hbit" is ignored

Resolution: Do not combine typedef with memory spaces.

- 204 -
ST7/STM8 C Compiler Manual 8. Error messages

8.202 ERROR C237: Memory space mismatch


Cause: You used an operator that does not accept the memory type of one of its
operands.

Example: typedef hbit foo;

Resolution: Change the operand's memory space.

8.203 ERROR C240: Internal compiler error


Cause: The compiler encountered an unexpected error.

Resolution: If the error persists, please contact your technical support at


support@raisonance.com

8.204 WARNING C251: Type "double" not handled, "float" assumed


Cause: You are using a variable declared with type “double” and this type is not handled in
RCSTM8

Example: double a;

Resolution: Change the type to float.

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”

8.205 ERROR C252: Memory space mismatch in redeclaration

Cause: You have redeclared a variable in a wrong space

Example: extern int val;


int code val;
void main( void )
{
while(1);
}

Resolution: Change space definition of the variable

- 205 -
ST7/STM8 C Compiler Manual

8.206 ERROR C253: Too much data on stack


Cause: The compiler detected that an object was too large to fit on stack.

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!
}

Resolution: Change memory space definition of local variables.

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.

Example: #pragma ST7 NOAUTO // non-reentrant mode


void foo(char,int,char);
void (*pfct)(char,int,char);
void bar(void)
{
pfct = foo;
(*pfct)(1,2,3); // Warning emitted
}

Resolution: Several solutions are possible:


- Limit the first 2 parameters of your called functions to 16-bits (no “long”, no
“float”)
- Compile your application in “AUTO” mode (code is less optimized however).
- Declare all your indirectly called functions as “reentrant”

Please refer to Chapter 3 “Compiler internals, section Parameter passing conventions” for details.

8.208 ERROR C256: Improper use of “typedef”


Cause: You attempted to use a type instead of a type name while using a structure.

Example typedef struct { int T1; } mystruct;


int i = mystruct.T1;

Resolution: Use a proper Standard C construct.

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.

Example int fact(int i)


{
if(i <= 1)
return i;
else
return i * fact(i – 1); // This will break the current parameter i
}

Resolution: Add the 'reentrant' qualifier to the recursive function.


In the example above, the following declaration should be used:

int fact(int i) reentrant

8.210 WARNING C258: Not enough space to store Common Sub-Expression


Cause: The optimizer has detected some common sub-expressions in your code, and
factorizes the code so that the common sub expression is stored on stack.
Only a limited number of bytes (16 by default) are available to store common
subexpressions on stack.

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.

Example at 0x19000 void farfct(void)


{
// Error C262 in SMALL model
}

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

Parameter Source file


The value that is passed to a macro or function. A text containing C program code.

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 March 2008 Updated references to STM8

Nigel Gray April 2008 Syntactical, formatting changes and corrections.


Added glossary, index.

Bruno Richard July 2008 Detailed the inner printf() workings.


Described the __OPTSPEED__ macro.
Described the bit-fields allocation scheme.
Modified auto relocation description to match the Ride7 user
experience.

Bruno Richard September 2008 Corrected the document title numbering.


Added some comments about ROM located constant variables.
Documented the STACKSPACEFOREXP directive.
Documented errors P033, C257 and C258.

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 January 2009 Documented the __STDC__ macro.

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.

Bruno Richard April 2009 Added the _wfe_ intrinsic function.


Corrected the hbit description which was showing invalid values.
Added STM8 example for multiple interrupt handler.

- 212 -
ST7/STM8 C Compiler Manual 9. Glossary

Bruno Richard May 2009 SUE_OPT was erroneously typed SUEOPT.


Clarified the DEFINE directive description.
Documented the fast 16 x 16 → 32-bits multiplication

Bruno Richard June 2009 Corrected some error messages.


Clarified the SRC pragma use in evaluation version.
Documented new error C260.
Warning C251 (double handled as float) appears only once per
compilation unit.

Bruno Richard July 2009 Documented the new “inram” features.


SRC mode now directly generates OBJ files, without requiring a
manual assembler pass.

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.

Bruno Richard January 2011 Documented the (NO)CLEANIRQDIVFLAG compiler directive.

- 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.

Copyright © Raisonance 1987-2011 All rights reserved.

- 214 -

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy