Professional Documents
Culture Documents
COSMIC
Porting STM8/ST7 applications
from Cosmic to
Raisonance tools
Application Note AN55
Document version
28 May 2013
Contents
1. INTRODUCTION................................................................................................................4
1.1 Purpose of this manual.................................................................................................................... 4
1.2 Scope of this manual....................................................................................................................... 4
1.3 Additional help or information.......................................................................................................... 4
1.4 Raisonance brand microcontroller application development tools....................................................4
1.5 Conventions used in this manual..................................................................................................... 5
2. STM8/ST7 APPLICATIONS..............................................................................................6
5. MEMORY MODELS.........................................................................................................10
5.1 Near/far functions call sequence (STM8).......................................................................................10
5.2 Qualifiers....................................................................................................................................... 10
5.3 Default memory spaces................................................................................................................. 11
5.4 Automatic variables relocation....................................................................................................... 11
5.5 Static local variables...................................................................................................................... 11
5.6 Pointer size control........................................................................................................................ 12
5.7 C libraries selection........................................................................................................................ 12
5.7.1 Floating-point libraries............................................................................................................. 12
6. MEMORY MAPPING.......................................................................................................13
-2-
7. IMPLEMENTATION DETAILS.........................................................................................14
7.1 Global variables initialization.......................................................................................................... 14
7.1.1 Uninitialized global variables................................................................................................... 14
7.2 main() arguments and return value................................................................................................14
7.3 Bit variables................................................................................................................................... 15
8. INTERRUPT HANDLERS................................................................................................16
8.1 Cosmic interrupts........................................................................................................................... 16
8.2 Raisonance interrupts.................................................................................................................... 17
10. CONCLUSION...............................................................................................................22
12. INDEX............................................................................................................................24
13. HISTORY.......................................................................................................................25
-3-
1. Introduction
1. Introduction
This document guides you through the migration of STM8/ST7 projects originally developed using the
Cosmic compiler chain that you want to port to the Raisonance development suite.
The Raisonance STM8/ST7 compiler chain uses semantics, directives and keywords that are strongly
influenced by the 8051 toolchain, and are very similar to the Keil compiler.
Differences between the project architectures for Cosmic and Raisonance STM8/ST7 compiler suites
will be explained, as well as directives and command-line options for the tools and the C syntax specific
to both toolchains.
Although your applications could be developed without any Integrated Development Environment (IDE)
using only C files and compiler command-line, we strongly recommend using an IDE such as Ride7,
which will take care about lots of various settings and options, which could be tedious to handle
manually.
-4-
1. Introduction
-5-
2. STM8/ST7 applications
2. STM8/ST7 applications
The STM8/ST7 families of microcontrollers from STMicroelectronics, support applications developed
using the C language. Originally only the Cosmic C compiler toolchain was available for ST7.
Note: The Raisonance C compiler supports all ST7 and STM8 derivatives with a single product.
The Cosmic C compilers for STM8/ST7 are separate products, sold individually.
In the remainder of this document we explain porting differences from Cosmic compilers to the
Raisonance C compiler.
This document primarily focuses on STM8 projects, although ST7-specific details are available from
Annex A.
Users may find that older applications developed with the Cosmic compiler toolchain would benefit
from a port to the Raisonance Compiler toolchain. The C language is inherently portable hence the
Cosmic-Raisonance transition is easy. However, some parts of the 8-bit microcontrollers are handled
in a non-standard way and require some adaptation in user applications:
Code memory model. In applications that handle more than 64KB of memory, a specific model
must be handled that enables far functions call/ret sequences. Also the function local variables
memory placement.
Memory mapping. This describes the memory (RAM and ROM) placement of variables, stack,
peripherals, code, constants and reset vectors.
Peripherals and I/O access. Accessing peripherals from C is usually done using non-standard
extensions.
Global variables initialization. The C standard mandates that global variables should always be
initialized. However, in microcontroller code this constraint is usually lowered in order to favor a
smaller ROM footprint.
Specific startup files. Some applications requirements such as early reset conditions handling or
clock control may require some specific code to be executed before the main() function is entered.
Inline assembly and C/assembler interface. In cases where inline assembly must be used within
the application, the C compiler specifics must be adapted to match user requirements.
-6-
Raisonance
mods0
STM8(SMALL) DEFAULTGC(page0)
modsl0
STM8(SMALL) DEFAULTGC(data)
mods
STM8(LARGE) DEFAULTGC(page0)
modsl
STM8(LARGE) DEFAULTGC(data)
The linker automatically selects appropriate
libraries.
crtsxf.s
-7-
Raisonance
Global variables
@0x80 char a;
at 0x80 char a;
// Absolute variable
@tiny int i = 3;
page0 int i = 3;
// zero-page location
@near unsigned u;
data unsigned u;
// zero-section
Absolute functions
Must be handled through a specific segment and a
linker script directive on the Cosmic toolchain.
main() function
void main(void)
{
...
return;
// main() can return
}
void main(void)
{
...
while(1); // main() must not return
}
-8-
Description
__RCSTM8__
__ROM_MODEL__
Produces the constant "0" for small ROM model (limited to 64kB), and "1" for
LARGE models. Note that LARGE model is available only for STM8.
__ST7__
Produces the constant "1" when compiling for ST7, and "0" when compiling for
STM8.
__STM8__
Produces the constant "0" when compiling for ST7, and "1" when compiling for
STM8.
These macros can be used to separate Cosmic Code from Raisonance code as follows:
#if defined(__RCSTM8__)
page0 char var = 2;
// Raisonance-specific code
#else
@tiny char var = 2;
// Cosmic-specific code
#endif
-9-
5. Memory models
5. Memory models
The memory model that applies to an application defines the type of call/ret sequence depending on
whether far calls (24-bit addressing) should be used or near calls (16-bit addressing). It also handles
the memory placement of local variables and function parameters.
Cosmic
Raisonance
mods0
STM8(SMALL) DEFAULTGC(page0)
modsl0
STM8(SMALL) DEFAULTGC(data)
mods
STM8(LARGE) DEFAULTGC(page0)
modsl
STM8(LARGE) DEFAULTGC(data)
The linker automatically selects appropriate
libraries.
5.2 Qualifiers
Cosmic
Raisonance
@tiny int i = 3;
page0 int i = 3;
// zero-page location
@near unsigned u;
data unsigned u;
// zero-section
@far long l;
// 24-bit addressing
Memory (RAM or ROM) can be accessed by STM8/ST7 microcontrollers using different ranges:
1. 8-bit address ([0x00-0xFF] memory range) This is the most efficient way to store variables, as only 1
byte is necessary to address the variables, and pointers are only 1-byte. However, the memory range
is limited, as only 256 memory bytes are available.
Cosmic defines 8-bit memory as "short range", variables are located there by the "@tiny" qualifier.
Raisonance defines 8-bit memory as "page0 memory", variables are located there by the "page0"
qualifier.
2. 16-bit address ([0x0000-0xFFFF] memory range). This addresses 64 KB of memory.
Cosmic defines 16-bit memory as long range and variables are located there by the @near
qualifier.
Raisonance defines 16-bit memory as data memory and variables are located there by the data
qualifier.
3. 24-bit address (full memory range). This is used for addressing constants only, as the memory space
above address 0x10000 is for ROM only.
Cosmic locates variables or functions in 24-bit memory range using the @far qualifier.
Raisonance defines 24-bit memory as far memory and variables are located there using the far
qualifier.
- 10 -
5. Memory models
Note: The C language stores local variables and function parameters on the CPU stack. However,
8-bit microcontrollers have a constrained memory and the stack cannot be used extensively for
storage, so 8-bit C compilers can transparently store local variables and function parameters in RAM
(page0 or data) instead of stack.
The Cosmic C compiler documentation uses the stack denomination for the local variables storage
area (hence the stack short or stack large terms in the Cosmic documentation).
The Raisonance C compiler documentation uses the Default local class denomination for the local
variables storage area.
We refer to these storage areas as local variables storage. The function parameters are always
stored in the same area as the local variables, except when they can be held in CPU registers.
Upon next build, a specific (and silent) compilation and link pass will be performed, that
produces the "best list" of variables to be relocated to the zero page.
Subsequent builds of the project automatically relocate the variables according to the produced
Auto Relocation File.
Tip: Reduce your code footprint using auto-relocation mode
Auto-relocation mode is a project-wide optimization that reduces your code footprint. Make sure
you activate auto-relocation mode for your large projects to automatically benefit from this feature.
- 11 -
5. Memory models
far * p24bits;
ptab = &table[2];
Note: The pointer qualifiers "data", "far" and "page0" should be considered the same as standard
"volatile" or "const" qualifiers. Reading (or writing) complex variables declarations using such
qualifiers can therefore be done by applying standard procedures.
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, and should be reported as INF. This is properly handled by the standard (checked)
float library, but gives an invalid result with fast float.
IEEE rounding is NOT the same as the 8087 FPU, but is a truncate instead. This means that
transcendental operations +, -, * and / may report a slightly different result than standard float.
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.
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).
In order to use the fast floating point libraries in your projects, the FASTLIBRARY directive should be
activated for the linker.
Please refer to the RCSTM8 compiler manual for details between the standard (checked) and fast float
libraries.
- 12 -
6. Memory mapping
6. Memory mapping
6.1 Absolute variables
Variables must sometimes be absolutely located at a specific RAM address. Cosmic tools do this
through the @address qualifier. For bit variables @address:bitnumber can specify the bit number to be
used at the given address. For example (Cosmic syntax):
char MISCR1 @0x20;
_Bool PB3 @0x01:3;
The Raisonance syntax resembles Cosmic. The at address qualifier can locate variables. For bit
variables the at address^bitnumber can specify the bit number to be used at the given address.
The example above can easily be translated to Raisonance code:
at 0x20 char MISCR1;
Note: Absolute functions: The Raisonance toolchain enables easy location of functions at given
addresses: Use the at address qualifier as if the function was an absolute variable, for example:
at 0x8402 void absolutefct(unsigned u);
The location in memory is then performed automatically by the linker, without having to specify any
specific linker directive or linker script file.
{
{ 0x0164B8A9L, 0x14CD2A5EL, 0x74C4F7FFL, 0x082C4D97L, 0xF1EDF880L },
{ 0xD2516EE1L, 0xACFA5BAFL, 0x33DFC1C4L, 0x71E43844L, 0x9EF134C8L },
{ 0x3232AFFAL, 0x48628A26L, 0x653B5AAAL, 0x44541FD9L, 0x0D690603L }
};
When using the Cosmic toolchain, const global variables are always stored in ROM.
For the global variables to be stored in ROM using the Raisonance toolchain, simply place them in the
code memory space. So the example above must be written as follows on Raisonance:
// From SHA-1 algorithm
const code long shsTestResults[][ 5 ] =
{
...
Note: The fcode (far code) qualifier can also be used to locate variables in far code (above address
0xFFFF). This is especially useful if you have large data tables such as web pages or images.
Access to far code is less efficient (24-bit addresses must be handled instead of 16-bit), but some
STM8 derivatives will let you hold as much as 96KB of far code.
- 13 -
7. Implementation details
7. Implementation details
Some application requirements such as early reset conditions handling or clock control may require
some specific code to be executed before the main() function is entered.
Raisonance
crtsi.s
crtsx.s
crtsif.s
crtsxf.s
- 14 -
7. Implementation details
void main(void);
Consequently, you cannot have the common argc and argv arguments.
One difference between Cosmic and Raisonance Compilers is that the main function must never return
on the Raisonance toolchain.
- 15 -
8. Interrupt handlers
8. Interrupt handlers
Hardware interrupts serviced by C code require specific declarations because they are not called
from another function but run asynchronously, they cannot take any arguments, and cannot return a
function return.
Interrupt handlers have a prototype such as:
void irqhandler(void);
@interrupt
in order to be
An interrupt vector table must then be created that contains all the interrupt vectors (the example
assumes that our interrupt handler is for IRQ0 for STM8):
#pragma section const {vector}
void (* const @vector _vectab[32])() =
{
_stext,
/* RESET */
...
};
Then a specific option must be provided to the linker to map the interrupt vectors at the appropriate
address 0x8000:
+seg .const -b0x8000 vector.o
- 16 -
8. Interrupt handlers
Notes:
- The trap qualifier must be used instead of the interrupt n for a trap handler.
- Registers are saved by interrupt handlers (except for the registers which are untouched, which are
not saved/restored for better performance). There is no need to worry about them.
- There is no need for a specific C file declaring the interrupt functions on the Raisonance
toolchain, as the mapping on interrupt vectors is automatically done by the linker.
- Undefined interrupt vectors are left allocatable for code. So if you do not define any interrupts in
your project, some executable code can be mapped in the interrupt vector table.
- 17 -
Performing some very specific tricks with the stack (context switching, find return address from
current function, get access to the caller's stack frame,...).
} while(--time);
}
- 18 -
NOP
4A
DEC
26FB
JRNE
?DO_0001
81
RET
; time
unsigned char
As you see, there is no need to write any assembler in such cases, which makes the code more
portable.
Using intrinsic functions only requires that you include the <intrins.h> header file from the Raisonance
Compiler.
The available intrinsic functions are demonstrated in the following example (which does not perform
anything useful though):
#include <intrins.h>
at 0x88 char myport = 0xA5;
int val1=0xBA42;
void main(void)
{
_rsp_();
_jmp_(0x1234);
val1 = _swapbyte_(val1);
_sim_();
// disable interrupts
myport |= 4;
// Raise bit 2
myport = _swapnibble_(myport);
_nop_();
_rim_();
// re-enable interrupts
_halt_();
_wfi_();
_trap_();
// Software trap
- 19 -
; With:
;
struct TCB
unsigned SP;
// Stack pointer
unsigned char A;
// Accumulator
unsigned Y;
// Y register
;----------------------------------------------------------------------------; Input:
currenttask
SP+3
newtask
; Output:
None
;----------------------------------------------------------------------------$modestm8
PUBLIC
?OS_switch
CSEG
?OS_switch:
; Let's save current context
LD
(2,X), A
; Save A
LDW
(3,X), Y
; Save Y
LDW
Y, SP
LDW
(X), Y
; Save SP
X, (3,SP)
LD
A, (2,X)
; Restore A
LDW
Y, X
LDW
Y, (3,Y)
LDW
X, (X)
LDW
SP, X
; Restore Y
; Restore SP
_exit:
RET
END
- 20 -
void main(void)
{
#pragma ASM
NOP
LD
A, #4
...
#pragma ENDASM
printf("OK");
}
Details of this mechanism are beyond the scope of the present document. Please refer to the
Raisonance compiler manual for further detail.
- 21 -
10. Conclusion
10. Conclusion
As we have seen in this document, porting applications from the Cosmic to the Raisonance toolchain is
not difficult. Most of the changes occur in the compiler and linker command-line directives, plus some
changes for ROM constants and some syntax changes for memory space qualifiers.
The Raisonance toolchain has been designed with simplicity in mind:
The linker options are very often automatic and do not need any directive, command-line option or
linker script. They just understand what is required by the application and pick the appropriate
libraries or apply specific settings that match the project.
Most noticeably, handling interrupts has never been so easy as with the Raisonance toolchain: The
interrupt handler function prototype is the only specific setting that you have to perform yourself, all
the linker burden of mapping the interrupt vector and the specific function prologue/epilogue (IRET,
register saving) is done automatically.
The STM8/ST7 Raisonance toolchain is nicely integrated into the Ride7 IDE, as well as into the ST
Visual Develop IDE. These can be used for porting your projects in order to bootstrap your projects
quickly and to reach the best time-to-market.
- 22 -
Option
Local variables
storage
Global variables
storage
Default pointer
size
Memory
Compact
modc
Zero Page
Zero Page
8 Bits
Memory Short
modm
Zero Page
Zero Page
16 Bits
Memory Small
modms
Zero Page
Long Range
16 Bits
Memory Medium
modmm
Long Range
Zero Page
16 Bits
Memory Long
modml
Long Range
Long Range
16 Bits
Stack Short
mods
Physical
Zero Page
16 Bits
Stack Long
modsl
Physical
Long Range
16 Bits
Raisonance
DEFAULTLC(page0) DEFAULTGC(page0)
modm
DEFAULTLC(page0) DEFAULTGC(page0)
modms
DEFAULTLC(page0) DEFAULTGC(data)
modmm
DEFAULTLC(data) DEFAULTGC(page0)
modml
DEFAULTLC(data) DEFAULTGC(data)
mods
AUTO DEFAULTGC(page0)
modsl
AUTO DEFAULTGC(data)
The linker automatically selects appropriate
libraries.
- 23 -
12. Index
12. Index
16-bit address......................................................10
24-bit address......................................................10
8-bit address........................................................10
Absolute functions................................................13
Absolute variables................................................13
argc......................................................................15
argv......................................................................15
ASM/ENDASM pragmas......................................21
Assembly opcodes...............................................21
at address............................................................13
auto-relocation mode...........................................11
automatic interrupt generation.............................17
Automatic variables relocation.............................11
Bit type.................................................................15
Bit variables.........................................................15
C language extensions summary...........................8
CALLF/RETF.......................................................10
code memory space............................................13
const....................................................................13
Cosmic ST7 toolchain memory models...............23
crtsi.s...................................................................14
crtsif.s..................................................................14
crtsx.s..................................................................14
crtsxf.s.................................................................14
Declaring interrupt functions................................17
DEFAULTGC.......................................................11
far CALL/RET......................................................10
far code................................................................13
Floating-point libraries..........................................12
function _nop_() ..................................................18
Global variables...................................................14
Hide code...............................................................9
INITSTATICVAR..................................................14
Inline assembly....................................................18
interrupt n qualifier...............................................17
Interrupt qualifier..................................................17
intrinsic functions.................................................19
Large data tables.................................................13
Local variables storage........................................11
Macros...................................................................9
main()..................................................................14
mapping interrupt vectors....................................17
modc....................................................................23
modm...................................................................23
modml..................................................................23
modmm...............................................................23
modms.................................................................23
mods....................................................................23
mods0....................................................................7
modsl...................................................................23
modsl0...................................................................7
NOINITSTATICVAR............................................14
page0...................................................................10
Parameters passed in memory............................21
Parameters passed in stack.................................21
Passing parameters.............................................21
pointer size..........................................................12
Pointer size control..............................................12
ptab......................................................................12
Qualifiers..............................................................10
Reduce your code footprint..................................11
Ride7...................................................................11
Separate assembler files.....................................20
ST Visual Develop...............................................11
ST7 Memory model selection..............................23
Stack initialization................................................14
startup..................................................................14
startup.asm..........................................................14
static....................................................................11
STM8(LARGE......................................................10
STM8(SMALL).....................................................10
Toolchain directives...............................................7
trap......................................................................17
Trap handler........................................................17
Using inline assembler statements......................21
void irqhandler(void)............................................16
volatile..................................................................12
__RCSTM8__........................................................9
__ROM_MODEL__................................................9
__ST7__................................................................9
__STM8__.............................................................9
_Bool type............................................................15
.bss......................................................................14
.data.....................................................................14
@address:bitnumber...........................................13
@far.....................................................................10
@interrupt............................................................16
@near..................................................................10
@tiny....................................................................10
%ridedir%..............................................................5
+nobss.................................................................14
- 24 -
13. History
13. History
Date
Description
Mar 09
Initial version
Apr 09
Jun 09
Added some details about interrupt vectors mapping and boolean variables.
02 Oct 12
Modified cover page, final page and section 1.3 Additional help or information for KEOLABS
28 May 13
Modified template
- 25 -
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 purchasers 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 installation directory of
the most recent version of the software, 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 KEOLABS 2014 All rights reserved