Sie sind auf Seite 1von 67

November 11, 2010

C for Embedded Systems Programming


AMF-ENT-T0001

Derrick Klotz
Regional Field Applications Engineer
TM
Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective
owners. Freescale Semiconductor, Inc. 2010.
Agenda

C Programming for Freescales 8-bit S08


with Guidelines Towards Migrating to 32-bit Architecture

Knowing the environment


Compiler and linker
.prm and map file
Programming models
Data types for embedded
Choosing the right data type
Variable types
Storage class modifiers
Project Software Architecture
Modular File Organization
Tips and considerations

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Embedded C versus Desktop C
C for Embedded Systems Programming

TM
Introduction

The C Programming Language was originally developed for and


implemented on the UNIX operating system, by Dennis Ritchie in 1971.
One of the best features of C is that it is not tied to any particular hardware
or system. This makes it easy for a user to write programs that will run
without any changes on practically all machines.
C is often called a middle-level computer language as it combines the
elements of high-level languages with the functionalism of assembly
language.
To produce the most efficient machine code, the programmer must not
only create an efficient high level design, but also pay attention to the
detailed implementation.

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Why Change to C?

C is much more flexible than other high-level programming languages:


C is a structured language.
C is a relatively small language.
C has very loose data typing.
C easily supports low-level bit-wise data manipulation.
C is sometimes referred to as a high-level assembly language.

When compared to assembly language programming:


Code written in C can be more reliable.
Code written in C can be more scalable.
Code written in C can be more portable between different platforms.
Code written in C can be easier to maintain.
Code written in C can be more productive.

C retains the basic philosophy that programmers know what they are doing.
C only requires that they state their intentions explicitly.

C program should be Clear, Concise, Correct, and Commented.

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Why Not C?

These are some of the common issues that we encounter when


considering moving to the C programming language:

Big and inefficient code generation


Fat code for the standard IO routines (printf, scanf, strcpy, etc)
The use of memory allocation: malloc(), alloc(),
The use of the stack is not so direct in C
Data declaration in RAM and ROM
Compiler optimizations
Difficulty writing Interrupt Service Routines

Many of these concerns are the result of failing to acknowledge the


available resource differences between embedded microcontrollers and
desktop computing environments

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Embedded versus Desktop Programming

Main characteristics of an Embedded programming environment:


Limited ROM.
Limited RAM.
Limited stack space.
Hardware oriented programming.
Critical timing (Interrupt Service Routines, tasks, ).
Many different pointer kinds (far / near / rom / uni / paged / ).
Special keywords and tokens (@, interrupt, tiny, ).

Successful Embedded C programs must keep the code small and


tight. In order to write efficient C code there has to be good
knowledge about:
Architecture characteristics
The tools for programming/debugging
Data types native support
Standard libraries
Understand the difference between simple code vs. efficient code

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Assembly Language versus C

A compiler is no more efficient than a good assembly language programmer.

It is much easier to write good code in C which can be converted to efficient


assembly language code than it is to write efficient assembly language code by hand.

C is a means to an end and not an end itself.

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Knowing the Environment Compiler & Linker
C for Embedded Systems Programming

TM
Compilers little Details

While choosing a compiler, you must remember that


the Devil is in the details

Nice features that can make a huge difference:

 Inline Assembly
 Interrupt Functions
 Assembly Language Generation
 Standard Libraries
 Startup code

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Compiler Requirements

.asm
.asm
.o
.o
.c.c Compiler
+ .h listing
listing
.cpp .h
.cpp

Generate ROM-able code


Generate Optimized code
Generate Re-entrant code
Support for Different Members in Microcontroller Family
Support for Different Memory Models

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Compiler Internal view
Front End reading th program:
Source Code Identifies the language (C, C++ )
Prevents syntax errors
Takes account of the preprocessing directives
(macros and typedef resolutions, conditional
compilation etc...)
Front End Code Generator:
Generate ROM-able code
Code Generate optimized code according to
the requested compiler options
Generator
Generate re-entrant code

Back End Back End:


Support for different members in
microcontroller family
Support for different memory models

Object files 90 % of the C programming issues are user related, so just as


with the compiler front end, when debugging an application,
the first step is to carefully read the program

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Overview

After compiling process is done, the linker works with the object files
generated in order to link the final application

There are a couple of features that could be achieved by the linker


because of the nature of the process

The linker parameter file could be used to do some useful tricks that
will aid during the development process

Information provided by the linker could be used within applications

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Linker Requirements

.o
.o
.s19
.s19
Linker
.o
.o
.map
.map
.o
.o

Target
description

Merging segments of code


Allocate target memory (RAM, ROM, stack, special areas)
Produce files for debugging (symbols, line numbers...)
Produce files for target (mirror of memory)

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Target Description S08

Direct Page
0x00FF

MC9S08QE32 MC9S08LL64

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Target Description S12X and ColdFire

MC9S12XEP100 ColdFire MCF51QE128

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Memory Models
Memory models have a meaning depending on the target used

HC(S)12
Model HC(S)08 Data Function
S12X
All data, including stack, must fit 8-bits 16-bits
Tiny into the zero page pointers have unless
8-bit addresses unless is explicitly specified
specified with __far
All pointers and functions have Data and functions are accessed by 16-bits 16-bits
Small 16-bit addresses. Code and data default with 16- bit addresses, code unless
shall be located in a 64Kb and data fit in 64 KB specified
address space
This model uses the Memory Data is also accessed with 16-bit 16-bits 24-bits
Banked Management Unit (MMU), addresses, functions are called
allowing the extension of program using banked calls
space beyond the 64 KB
Both code and data are accessed 24-bits 24-bits
Large using paged conventions

What about 32-bit architectures?

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Tying Everything Together

.asm
.asm
.o.o .s19
.s19
.c.c Compiler Linker
+ .h.h listing
listing .map
.cpp .map
.cpp

At this point the compiler doesnt Target


know about the memory description
characteristics of the device used

This means that the amount of memory used


doesnt matter and the compiler will use a
predefined convention to access data and functions

If the compiler doesnt know how the memory is


arranged, how can we specify the calling
convention that shall be used?

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Far and Near

The keywords far and near in C are intended to refer to data (either code or
variables) within the same memory block or outside of the memory block
Depending on the architecture, the memory block can mean different things

Applied to functions
__far and __near specify the calling convention. Far function calls can cross
pages. Near function calls must stay in the same page
void main(void) void __far MyFunction (void); void __near MyFunction (void);
{
MyFunction(); CALL MyFunction, PAGE(MyFunction) JSR MyFunction
}

Applied to variables
A variable declared __near is considered by the compiler to be allocated in the
memory section that can be accessed with direct addressing (first 256 bytes for
S08, first 64 KB for S12 and S12X) accessing variables in the zero page
generates less code and executes faster since the address is only 8-bits

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Using near and far

For both, code and data, near Linker


and far must be used in #pragma DATA_SEG MY_ZEROPAGE 0x0000

conjunction with a #pragma char var;


near var; DIRECT PAGE

directive to specify the memory 0x00FF


0x0100

#pragma DATA_SEG DEFAULT_RAM


section to place them char var;
far var; RAM

For variables
#pragma DATA_SEG <segment name>
Compiler
For code char var;
#pragma CODE_SEG <segment name> var++; ldhx #var
inc ,x

To understand what the char near var;


var++; inc var
segment name is we need to
understand how the linker char far var;
identifies the memory var++; ldhx
inc
#var
,x

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
MC9S08LL64 Linker Parameter File (.prm)
NAMES
NAMES END
END /*
/* CodeWarrior
CodeWarrior will
will pass
pass all
all the
the needed
needed files
files to
to the
the linker
linker by
by command
command line.
line. But
But

SEGMENTS
SEGMENTS /*
/* Here
Here all
all RAM/ROM
RAM/ROM areas
areas of
of the
the device
device are
are listed.
listed. Used
Used in
in PLACEMENT
PLACEMENT below.
below. */
*/
Z_RAM
Z_RAM == READ_WRITE
READ_WRITE 0x0060
0x0060 TO
TO 0x00FF;
0x00FF;
RAM
RAM == READ_WRITE
READ_WRITE 0x0100
0x0100 TO
TO 0x0FFF;
0x0FFF;
/*
/* unbanked
unbanked FLASH
FLASH ROM
ROM */
*/
ROM
ROM == READ_ONLY
READ_ONLY 0x18A1
0x18A1 TO
TO 0x7FFF;
0x7FFF;
SEGMENTS SECTION ROM1
ROM1 == READ_ONLY
READ_ONLY 0x103C
0x103C TO
TO 0x17FF;
0x17FF;
Defines the memory available in the MCU, ROM2
ROM2 == READ_ONLY
READ_ONLY 0xC000
0xC000 TO
TO 0xFFAB;
0xFFAB;
providing full control over memory allocation. ROM3
ROM3 == READ_ONLY
READ_ONLY 0xFFC0
0xFFC0 TO
TO 0xFFD1;
0xFFD1;
/*
/* banked
banked FLASH
FLASH ROM
ROM */
*/
This is essentially a translation of the data sheet. PPAGE_0 == READ_ONLY 0x008002 TO 0x00903B; /*
PPAGE_0 READ_ONLY 0x008002 TO 0x00903B; /* PAGE
PAGE partially
partially containe
containe
PPAGE_0_1
PPAGE_0_1 == READ_ONLY
READ_ONLY 0x009800
0x009800 TO
TO 0x0098A0;
0x0098A0;
PPAGE_2
PPAGE_2 == READ_ONLY
READ_ONLY 0x028000
0x028000 TO
TO 0x02BFFF;
0x02BFFF;
/*
/* PPAGE_1
PPAGE_1 == READ_ONLY
READ_ONLY 0x018000
0x018000 TO
TO 0x01BFFF;
0x01BFFF; PAGE
PAGE already
already contained
contained in
in
/*
/* PPAGE_3
PPAGE_3 == READ_ONLY
READ_ONLY 0x038000
0x038000 TO
TO 0x03BFFF;
0x03BFFF; PAGE
PAGE already
already contained
contained in
in
END
END

PLACEMENT
PLACEMENT /*
/* Here
Here all
all predefined
predefined and
and user
user segments
segments are
are placed
placed into
into the
the SEGMENTS
SEGMENTS defined
defined above
above
DEFAULT_RAM,
DEFAULT_RAM, /*
/* non-zero
non-zero page
page variables
variables */
*/
INTO
INTO RAM;
RAM;

_PRESTART,
_PRESTART, STARTUP,
STARTUP, /*
/* startup
startup code
code and
and data
data structures
structures */
*/
ROM_VAR,
ROM_VAR, /*
/* constant
constant variables
variables */
*/
PLACEMENT SECTION STRINGS,
STRINGS, /*
/* string
string literals
literals */
*/
Provides the ability to assign each section from VIRTUAL_TABLE_SEGMENT,
VIRTUAL_TABLE_SEGMENT, /*
/* C++
C++ virtual
virtual table
table segment
segment */
*/
NON_BANKED,
NON_BANKED, /*
/* runtime
runtime routines
routines which
which must
must not
not be
be banked
banked */
*/
the application to specific memory segments. DEFAULT_ROM,
DEFAULT_ROM,
The names identified in this section are used in COPY
COPY /*
/* copy
copy down
down information:
information: how
how to
to initialize
initialize variable
variable
the source code, for example: INTO
INTO ROM;
ROM; /*
/* ,ROM1,ROM2,ROM3:
,ROM1,ROM2,ROM3: To
To use
use "ROM1,ROM2,ROM
"ROM1,ROM2,ROM
#pragma DATA_SEG MY_ZEROPAGE
PAGED_ROM
PAGED_ROM /*
/* routines
routines which
which can
can be
be banked
banked */
*/
INTO
INTO PPAGE_2,ROM1,ROM2,ROM3,PPAGE_0,PPAGE_0_1;
PPAGE_2,ROM1,ROM2,ROM3,PPAGE_0,PPAGE_0_1;

_DATA_ZEROPAGE,
_DATA_ZEROPAGE, /*
/* zero
zero page
page variables
variables */
*/
MY_ZEROPAGE
MY_ZEROPAGE INTO
INTO Z_RAM;
Z_RAM;
END
END
STACKSIZE is one way to reserve a portion of
memory for stack usage. STACKSIZE
STACKSIZE 0x100
0x100
VECTOR
VECTOR 00 _Startup
_Startup /*
/* Reset
Reset vector:
vector: this
this is
is the
the default
default entry
entry point
point for
for an
an application.
application. */
*/

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Controlling the Location of the Stack

SEGMENTS SEGMENTS
Z_RAM = READ_WRITE 0x0060 TO 0x00FF; Z_RAM = READ_WRITE 0x0060 TO 0x00FF;
RAM = READ_WRITE 0x0100 TO 0x0FFF; RAM = READ_WRITE 0x0100 TO 0x0EFF;
END STACK_RAM = READ_WRITE 0x0F00 TO 0x0FFF;
END

PLACEMENT
SSTACK INTO STACK_RAM;
END

STACKSIZE 0x100 STACKTOP 0x0FFF

0x0000
or 0x0000

Peripheral Registers Peripheral Registers


0x005F 0x005F
0x0060 Direct Page 0x0060 Direct Page
#pragma DATA_SEG MY_ZEROPAGE variables #pragma DATA_SEG MY_ZEROPAGE variables

0x00FF 0x00FF
0x0100 0x0100
#pragma DATA_SEG DEFAULT_RAM variables #pragma DATA_SEG DEFAULT_RAM variables
stack
SP
RAM RAM

0x0F00
0x0FFF 0x0FFF
stack
SP

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
ColdFire MCF51QE128 Linker Control File (.lcf)

## Sample
Sample Linker
Linker Command
Command File
File for
for CodeWarrior
CodeWarrior for
for ColdFire
ColdFire MCF51QE128
MCF51QE128

## Memory
Memory ranges
ranges

MEMORY
MEMORY {{
MEMORY SEGMENT code
code (RX)
(RX) :: ORIGIN
ORIGIN == 0x00000410,
0x00000410, LENGTH
LENGTH == 0x0001FBF0
0x0001FBF0
Describes the available memory userram
userram (RWX)
(RWX) :: ORIGIN
ORIGIN == 0x00800000,
0x00800000, LENGTH
LENGTH == 0x00002000
0x00002000
}}

SECTIONS
SECTIONS {{

## Heap
Heap and
and Stack
Stack sizes
sizes definition
definition
___heap_size
___heap_size == 0x0400;
0x0400;
___stack_size
___stack_size == 0x0400;
0x0400;

## MCF51QE128
MCF51QE128 Derivative
Derivative Memory
Memory map
map definitions
definitions from
from linker
linker command
command files:
files:
## ___RAM_ADDRESS,
___RAM_ADDRESS, ___RAM_SIZE,
___RAM_SIZE, ___FLASH_ADDRESS,
___FLASH_ADDRESS, ___FLASH_SIZE
___FLASH_SIZE linker
linker
## symbols
symbols must
must be
be defined
defined in
in the
the linker
linker command
command file.
file.
SECTIONS SEGMENT
Defines the contents of memory sections and ## 88 Kbytes
Kbytes Internal
Internal SRAM
SRAM
global symbols ___RAM_ADDRESS
___RAM_ADDRESS == 0x00800000;
0x00800000;
___RAM_SIZE
___RAM_SIZE == 0x00002000;
0x00002000;

## 128
128 KByte
KByte Internal
Internal Flash
Flash Memory
Memory
___FLASH_ADDRESS
___FLASH_ADDRESS == 0x00000000;
0x00000000;
___FLASH_SIZE
___FLASH_SIZE == 0x00020000;
0x00020000;

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
How can we verify where the Linker put our code and data?

The Map file

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
The Map File
*********************************************************************************************
*********************************************************************************************
TARGET
TARGET SECTION
SECTION
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
TARGET SECTION Processor
Processor :: Freescale
Freescale HC08
HC08
Memory
Memory Model:
Model: SMALL
SMALL
Names the target processor and memory model File
File Format
Format :: ELF\DWARF
ELF\DWARF 2.0
2.0
Linker
Linker :: SmartLinker
SmartLinker V-5.0.39
V-5.0.39 Build
Build 10132,
10132, May
May 13
13 2010
2010

*********************************************************************************************
*********************************************************************************************
FILE
FILE SECTION
SECTION
FILE SECTION ---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
main.obj
main.obj Model:
Model: SMALL,
SMALL, Lang:
Lang: ANSI-C
ANSI-C
Lists the names of all files from which objects
start08.obj
start08.obj Model:
Model: SMALL,
SMALL, Lang:
Lang: ANSI-C
ANSI-C
were used mc9s08ll64.obj
mc9s08ll64.obj Model:
Model: SMALL,
SMALL, Lang:
Lang: ANSI-C
ANSI-C

*********************************************************************************************
*********************************************************************************************
STARTUP
STARTUP SECTION
SECTION
STARTUP SECTION ---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
Lists the prestart code and the values used to Entry
Entry point:
point: 0x191C
0x191C (_Startup)
(_Startup)
_startupData
_startupData is
is allocated
allocated at
at 0x1925
0x1925 and
and uses
uses 66 Bytes
Bytes
initialize the startup descriptor _startupData. extern
extern struct
struct _tagStartup
_tagStartup {{
The startup descriptor is listed member by unsigned
unsigned nofZeroOut
nofZeroOut 11
member with the initialization data at the right _Range
_Range pZeroOut
pZeroOut 0x60
0x60 11
hand side of the member name _Copy
_Copy *toCopyDownBeg
*toCopyDownBeg 0x1945
0x1945
}} _startupData;
_startupData;

*********************************************************************************************
*********************************************************************************************
SECTION-ALLOCATION
SECTION-ALLOCATION SECTION
SECTION
Section
Section Name
Name Size
Size Type
Type From
From To
To Segment
Segment
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
.init
.init 132
132 RR 0x18A1
0x18A1 0x1924
0x1924 ROM
ROM
SECTION-ALLOCATION SECTION .startData
.startData 14
14 RR 0x1925
0x1925 0x1932
0x1932 ROM
ROM
.text
.text 18
18 RR 0x1933
0x1933 0x1944
0x1944 ROM
ROM
Lists those segments for which at least one .copy 22 RR 0x1945 0x1946 ROM
.copy 0x1945 0x1946 ROM
object was allocated MY_ZEROPAGE
MY_ZEROPAGE 11 R/W
R/W 0x60
0x60 0x60
0x60 Z_RAM
Z_RAM
.abs_section_0
.abs_section_0 11 N/I
N/I 0x0
0x0 0x0
0x0 .absSeg0
.absSeg0
.abs_section_1
.abs_section_1 11 N/I
N/I 0x1
0x1 0x1
0x1 .absSeg1
.absSeg1
.abs_section_2
.abs_section_2 11 N/I
N/I 0x2
0x2 0x2
0x2 .absSeg2
.absSeg2
.abs_section_3
.abs_section_3 11 N/I
N/I 0x3
0x3 0x3
0x3 .absSeg3
.absSeg3

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
The Map File
Linker
Linker Parameter
Parameter file
file
*********************************************************************************************
*********************************************************************************************
TARGET
TARGET SECTION
SECTION
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
PLACEMENT /*
/* Here
Processor
PLACEMENT
ProcessorHere::all predefined
Freescale
all HC08and
HC08
predefined
Freescale and user
user segments
segments are
are placed
placed into
into the
the SEGMENTS
SEGMENTS defined
defined above.
above. */
*/
.init = _PRESTART DEFAULT_RAM,
Memory
Memory Model:
Model: SMALL
DEFAULT_RAM, SMALL /*
/* non-zero
non-zero page
page variables
variables */
*/
.startData = STARTUP File
File Format
Format :: ELF\DWARF
ELF\DWARF 2.0
2.0 INTO
INTO RAM;
RAM;
Linker
Linker :: SmartLinker
SmartLinker V-5.0.39
V-5.0.39 Build
Build 10132,
10132, May
May 13
13 2010
2010
.text = DEFAULT_ROM _PRESTART, /*
_PRESTART, /* startup
startup code
code */
*/
.copy = COPY STARTUP,
STARTUP, /*
/* startup
startup data
data structures
structures */
*********************************************************************************************
*/
*********************************************************************************************
.stack = SSTACK ROM_VAR,
FILE
FILE SECTION
ROM_VAR,
SECTION /*
/* constant
constant variables
variables */*/
.data = DEFAULT_RAM STRINGS, /*
/* string
string literals
literals */
---------------------------------------------------------------------------------------------
STRINGS, */
---------------------------------------------------------------------------------------------
VIRTUAL_TABLE_SEGMENT,
main.obj
VIRTUAL_TABLE_SEGMENT,
main.obj /*
/* C++
C++ virtual
Model:
virtual
Model: table
table segment
SMALL,
SMALL, */
Lang:
segment */ ANSI-C
Lang: ANSI-C
.common = DEFAULT_RAM NON_BANKED, /*
start08.obj
NON_BANKED,
start08.obj /* runtime
Model:routines
Model:
runtime SMALL, which
SMALL,
routines which must
Lang:
must not
Lang: be
be banked
banked */
ANSI-C
not
ANSI-C */
DEFAULT_ROM,
mc9s08ll64.obj
DEFAULT_ROM,
mc9s08ll64.obj Model:
Model: SMALL,
SMALL, Lang:
Lang: ANSI-C
ANSI-C
COPY
COPY /*
/* copy
copy down
down information:
information: how
how to
to initialize
initialize variables
variables */
*/
INTO
INTO ROM;
ROM; /*
/* ,ROM1,ROM2,ROM3:
,ROM1,ROM2,ROM3: To To use
use "ROM1,ROM2,ROM3"
"ROM1,ROM2,ROM3" as
*********************************************************************************************
********************************************************************************************* as
Every variable allocated with an absolute STARTUP
STARTUP SECTION
SECTION
syntax of the kind: PAGED_ROM /*
/* routines
routines which
which can
can be
be banked
banked */
---------------------------------------------------------------------------------------------
PAGED_ROM */
---------------------------------------------------------------------------------------------
type variablename @0xABCD; Entry
Entry point:
point: 0x191C
0x191C (_Startup)
(_Startup) INTO
INTO PPAGE_2,ROM1,ROM2,ROM3,PPAGE_0,PPAGE_0_1;
PPAGE_2,ROM1,ROM2,ROM3,PPAGE_0,PPAGE_0_1;
_startupData
_startupData isis allocated
allocated at
at 0x1925
0x1925 and
and uses
uses 66 Bytes
Bytes
will have a section and a segment assigned _DATA_ZEROPAGE, /*
extern
extern struct
struct _tagStartup
_DATA_ZEROPAGE, _tagStartup {{ /* zero
zero page
page variables
variables */
*/
for its own. MY_ZEROPAGE
unsigned
unsigned nofZeroOut
MY_ZEROPAGE nofZeroOut 11 INTO
INTO Z_RAM;
Z_RAM;
END
END _Range
_Range pZeroOut
pZeroOut 0x60
0x60 11
All MCU registers are declared this way. This _Copy
_Copy *toCopyDownBeg
*toCopyDownBeg 0x1945
0x1945
}} _startupData;
_startupData;
is why we have one abs_section for every
MCU register. *********************************************************************************************
*********************************************************************************************
SECTION-ALLOCATION
SECTION-ALLOCATION SECTION
SECTION
Section
Section Name
Name Size
Size Type
Type From
From To
To Segment
Segment
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
.init
.init 132
132 RR 0x18A1
0x18A1 0x1924
0x1924 ROM
ROM
SECTION-ALLOCATION SECTION .startData
.startData 14
14 RR 0x1925
0x1925 0x1932
0x1932 ROM
ROM
.text
.text 18
18 RR 0x1933
0x1933 0x1944
0x1944 ROM
ROM
Lists those segments for which at least one .copy 22 RR 0x1945 0x1946 ROM
.copy 0x1945 0x1946 ROM
object was allocated MY_ZEROPAGE
MY_ZEROPAGE 11 R/W
R/W 0x60
0x60 0x60
0x60 Z_RAM
Z_RAM
.abs_section_0
.abs_section_0 11 N/I
N/I 0x0
0x0 0x0
0x0 .absSeg0
.absSeg0
.abs_section_1
.abs_section_1 11 N/I
N/I 0x1
0x1 0x1
0x1 .absSeg1
.absSeg1
.abs_section_2
.abs_section_2 11 N/I
N/I 0x2
0x2 0x2
0x2 .absSeg2
.absSeg2
.abs_section_3
.abs_section_3 11 N/I
N/I 0x3
0x3 0x3
0x3 .absSeg3
.absSeg3

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
The Map File
.stack
.stack 256
256 R/W
R/W 0x100
0x100 0x1FF
0x1FF RAM
RAM
.vectSeg188_vect
.vectSeg188_vect 22 RR 0xFFFE
0xFFFE 0xFFFF
0xFFFF .vectSeg188
.vectSeg188
SECTION-ALLOCATION SECTION Summary: Summary
Summary of
of section
section sizes
sizes per
per section
section type:
type:
READ_ONLY = Flash READ_ONLY
READ_ONLY (R):
(R): A8
A8 (dec:
(dec: 168)
168)
READ_WRITE = RAM READ_WRITE
READ_WRITE (R/W):
(R/W): 101
101 (dec:
(dec: 257)
257)
NO_INIT = Registers NO_INIT
NO_INIT (N/I):
(N/I): CD
CD (dec:
(dec: 205)
205)

*********************************************************************************************
*********************************************************************************************
VECTOR-ALLOCATION
VECTOR-ALLOCATION SECTION
SECTION
VECTOR-ALLOCATION SECTION
Address
Address InitValue
InitValue InitFunction
InitFunction
Lists each vectors address and to where it ---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
points (ie., interrupt service routine) 0xFFFE
0xFFFE 0x191C
0x191C _Startup
_Startup

*********************************************************************************************
*********************************************************************************************
OBJECT-ALLOCATION
OBJECT-ALLOCATION SECTION
SECTION
Name
Name Module
Module Addr
Addr hSize
hSize dSize
dSize Ref
Ref Section
Section RLIB
RLIB
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
MODULE:
MODULE: --
-- main.obj
main.obj --
--
-- PROCEDURES:
PROCEDURES:
main
main 1933
1933 12
12 18
18 11 .text
.text
-- VARIABLES:
VARIABLES:
nn 60
60 11 11 22 MY_ZEROPAGE
MY_ZEROPAGE
MODULE:
MODULE: --
-- start08.obj
start08.obj --
--
-- PROCEDURES:
PROCEDURES:
OBJECT-ALLOCATION SECTION loadByte
loadByte 18A1
18A1 EE 14
14 55 .init
.init
Contains the name, address and size of every Init
Init 18AF
18AF 6D
6D 109
109 11 .init
.init
_Startup
_Startup 191C
191C 99 99 00 .init
.init
allocated object and groups them by module -- VARIABLES:
VARIABLES:
_startupData
_startupData 1925
1925 66 66 44 .startData
.startData
-- LABELS:
LABELS:
__SEG_END_SSTACK
__SEG_END_SSTACK 200
200 00 00 11
MODULE:
MODULE: --
-- mc9s08ll64.obj
mc9s08ll64.obj --
--
-- PROCEDURES:
PROCEDURES:
-- VARIABLES:
VARIABLES:
_PTAD
_PTAD 00 11 11 00 .abs_section_0
.abs_section_0
_PTADD
_PTADD 11 11 11 00 .abs_section_1
.abs_section_1
_PTBD
_PTBD 22 11 11 00 .abs_section_2
.abs_section_2
_PTBDD
_PTBDD 33 11 11 00 .abs_section_3
.abs_section_3

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
The Map File
UNUSED-OBJECTS SECTION: *********************************************************************************************
*********************************************************************************************
Shows all of the variables declared but not used UNUSED-OBJECTS
UNUSED-OBJECTS SECTION
SECTION
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
after the optimizer did its job
*********************************************************************************************
*********************************************************************************************
COPYDOWN
COPYDOWN SECTION
SECTION
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
COPYDOWN SECTION -------
------- ROM-ADDRESS:
ROM-ADDRESS: 0x1945
0x1945 ----
---- SIZE
SIZE 22 ---
---
Lists each pre-initialized variable and its value Filling
Filling bytes
bytes inserted
inserted
0000
0000

*********************************************************************************************
*********************************************************************************************
OBJECT-DEPENDENCIES
OBJECT-DEPENDENCIES SECTION
SECTION
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
OBJECT-DEPENDENCIES SECTION Init
Init USES
USES _startupData
_startupData loadByte
loadByte
Lists for every function and variable that uses _Startup
_Startup USES
USES __SEG_END_SSTACK
__SEG_END_SSTACK Init
Init main
main
other global objects the names of these global main
main USES
USES _PTCD
_PTCD _PTCDD
_PTCDD _SRS
_SRS nn
objects *********************************************************************************************
*********************************************************************************************
DEPENDENCY
DEPENDENCY TREE
TREE
*********************************************************************************************
*********************************************************************************************
main
main and
and _Startup
_Startup Group
Group
||
DEPENDENCY TREE +-
+- main
main
Using a tree format, shows all detected ||
dependencies between functions. Overlapping +-
+- _Startup
_Startup
local variables are also displayed at their ||
+-
+- Init
Init
defining function || ||
|| +-
+- loadByte
loadByte
||
+-
+- main
main (see
(see above)
above)

*********************************************************************************************
*********************************************************************************************
STATISTIC
STATISTIC SECTION
SECTION
STATISTICS SECTION ---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
Delivers information like the number of bytes of ExeFile:
ExeFile:
code in the application Number
Number of
of blocks
blocks to
to be
be downloaded:
downloaded: 44
Total
Total size
size of
of all
all blocks
blocks to
to be
be downloaded:
downloaded: 168
168

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Configuring the Map File

The information displayed in the Map File can be configured by adding a MAPFILE
in the .prm file. Only the modules listed will be displayed. For example:

MAPFILE FILE SEC_ALLOC OBJ_UNUSED COPYDOWN

If no MAPFILE line is added, all information is included by default

Specifier Description Specifier Description

ALL Generates a map file containing all available information NONE Generates no map file

Writes information about the initialization value for objects Shows the allocation of overlapped variables
COPYDOWN DEPENDENCY_TREE
allocated in RAM (COPYDOWN section) (DEPENDENCY TREE section)

Includes information about the files building the application Includes information about the sections used in the
FILE SEC_ALLOC
(FILE section) application (SECTION ALLOCATION section)

Includes information about the allocated objects (OBJECT Includes information about the startup structure (STARTUP
OBJ_ALLOC STARTUP_STRUCT
ALLOCATION section) section)

Generates a list of all allocated objects, sorted by address Includes information about how much ROM/RAM specific
SORTED_OBJECT_LIST MODULE_STATISTIC
(OBJECT LIST SORTED BY ADDRESS section) modules (compilation units) use

Includes a list of all unused objects (UNUSED OBJECTS Includes statistic information about the link session
OBJ_UNUSED STATISTIC
section) (STATISTICS section)

Includes a list of dependencies between the objects in the Includes information about the target processor and
OBJ_DEP TARGET
application (OBJECT DEPENDENCY section) memory model (TARGET section)

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Lab1

byte
byte n;
n;
byte
byte var;
var;

void
void main(void)
main(void)
{{
EnableInterrupts;
EnableInterrupts;

//
// initialize
initialize LEDs
LEDs
PTCD
PTCD == 0xFF;
0xFF; //
// set
set default
default value
value for
for Port
Port CC
PTCDD
PTCDD == 0b00111100;
0b00111100; //
// set
set LED
LED port
port pins
pins as outputs
as outputs

for
for (;;)
(;;)
{{
__RESET_WATCHDOG();
__RESET_WATCHDOG(); /*
/* feeds
feeds the
the dog
dog */
*/

for
for (n=0;;n++)
(n=0;;n++)
{{
PTCD++;
PTCD++; //
// blink
blink LEDs
LEDs
var++;
var++;
}}

}} /*
/* loop
loop forever
forever */
*/
/*
/* please
please make
make sure
sure that
that you
you never
never leave
leave main
main */
*/
}}

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Lab1

#pragma
#pragma DATA_SEG
DATA_SEG MY_ZEROPAGE
MY_ZEROPAGE
byte
byte near n;
near n;

#pragma
#pragma DATA_SEG
DATA_SEG DEFAULT_RAM
DEFAULT_RAM
byte
byte far var
far var == 7;
7;

void
void main(void)
main(void)
{{
EnableInterrupts;
EnableInterrupts;

//
// initialize
initialize LEDs
LEDs
PTCD
PTCD == 0xFF;
0xFF; //
// set
set default
default value
value for
for Port
Port CC
PTCDD
PTCDD == 0b00111100;
0b00111100; //
// set
set LED
LED port
port pins
pins as outputs
as outputs

for
for (;;)
(;;)
{{
__RESET_WATCHDOG();
__RESET_WATCHDOG(); /*
/* feeds
feeds the
the dog
dog */
*/

for
for (n=0;;n++)
(n=0;;n++)
{{
PTCD++;
PTCD++; //
// blink
blink LEDs
LEDs
var++;
var++;
}}

}} /*
/* loop
loop forever
forever */
*/
/*
/* please
please make
make sure
sure that
that you
you never
never leave
leave main
main */
*/
}}

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Start08.c Startup Routine

Do we need the startup code? What does it do?

1. Stack Pointer/Frame setup 


2. Global Memory initialized to zero (Zero-Out) 
3. Global Variables initialized (Copy-Down)
4. Global Constructor calls (C++)
5. Call main() Start all Static and Global variables at zero
asm {
clra ; get clear data
ldhx #MAP_RAM_last ; point to last RAM location
stx MAP_RAM_first ; first RAM location is non-zero
txs ; initialize SP
ClearRAM:
psha ; clear RAM location
There is a simpler way tst MAP_RAM_first ; check if done
bne ClearRAM ; loop back if not
}

INIT_SP_FROM_STARTUP_DESC(); // initialize SP
__asm jmp main; // jump into main()

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
DHJK 03-Feb-05
Variable Data Types
C for Embedded Systems Programming

TM
Variables

The type of a variable determines what kinds of values it may take on.
In other words, selecting a type for a variable is closely connected to the
way(s) we'll be using that variable.
There are only a few basic data types in C:

Default value range Formats


Default
Type available
format min max with option -T
char (unsigned) 8 bit 0 255 8 bit, 16 bit, 32 bit

signed char 8 bit -128 127 8 bit, 16 bit, 32 bit


unsigned char 8 bit 0 255 8 bit, 16 bit, 32 bit Note:
signed short 16 bit -32768 32767 8 bit, 16 bit, 32 bit All scalar types are signed by default,
unsigned short 16 bit 0 65535 8 bit, 16 bit, 32 bit except char
enum (signed) 16 bit -32768 32767 8 bit, 16 bit, 32 bit
signed int 16 bit -32768 32767 8 bit, 16 bit, 32 bit
Size of int type is machine dependant
unsigned int 16 bit 0 65535 8 bit, 16 bit, 32 bit
signed long 32 bit -2147483648 2147483647 8 bit, 16 bit, 32 bit

unsigned long 32 bit 0 4294967295 8 bit, 16 bit, 32 bit


signed long long 32 bit -2147483648 2147483647 8 bit, 16 bit, 32 bit

unsigned long long 32 bit 0 4294967295 8 bit, 16 bit, 32 bit

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
CodeWarrior Data Types
TheANSI standard does not precisely define the size of its native types,
but CodeWarrior does

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Data Type Facts

The greatest savings in code size and execution time can be made by
choosing the most appropriate data type for variables

For example, the natural data size for an 8-bit MCU is an 8-bit variable

The C preferred data type is int

In 16-bit and 32-bit architectures it is possible to have ways to address either 8- or 16-bits
data efficiently but it is also possible that they are not addressed efficiently

Simple concepts like choosing the right data type or memory alignment can result into big
improvements

Double precision and floating point should be avoided wherever efficiency is important

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Data Type Selection

Mind the architecture


The same C source code could be efficient or inefficient
The programmer should keep in mind the architectures typical instruction size
and choose the appropriate data type accordingly
Consider:
8-bit S08 16-bit S12X 32-bit ColdFire
A++;
char A;
near A; ldhx
inc @A
A inc A move.b A(a5),d0
inc ,x addq.l #1,d0
move.b d0,A(a5)

unsigned int A; ldhx @A incw A addq.l #1,_A(a5)


inc 1,x
bne Lxx
inc ,x
Lxx:

unsigned long A; ldhx @A ldd A:2 addq.l #1,_A(a5)


jsr _LINC ldx A
jsr _LINC
std A:2
stx A

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Data Type Selection

There are 3 Rules for data type selection:


Use the smallest possible type to get the job done
Use unsigned type if possible
Use casts within expressions to reduce data types to the minimum required

Use typedefs to get fixed size


Change according to compiler and system
Code is invariant across machines
Used when a fixed number of bits is needed for values

Avoid basic types (char, int, short, long) in application code

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Data Type Naming Conventions

Avoid basic types (char, int, short, long) in application code


But how?

Basic CodeWarrior Stationary: Processor Expert CodeWarrior Stationary: Another Popular Technique:
/* Types definition */ #ifndef __PE_Types_H typedef unsigned char __uint8__;
typedef unsigned char byte; #define __PE_Types_H typedef unsigned char __byte__;
typedef unsigned int word; typedef signed char __int8__;
typedef unsigned long dword; /* Types definition */ typedef unsigned short __uint16__;
typedef unsigned long dlong[2]; typedef unsigned char bool; typedef signed short __int16__;
typedef unsigned char byte; typedef unsigned long __uint32__;
typedef unsigned int word; typedef signed long __int32__;
typedef unsigned long dword;
typedef unsigned long dlong[2];

// other stuff

#endif /* __PE_Types_H */

Recall:
C retains the basic philosophy that programmers know what they are doing
C only requires that they state their intentions explicitly
C provides a lot of flexibility; this can be good or bad

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Memory Alignment
8 8

typedef struct typedef struct


Var1 { { Var3
8 bits byte Var1; dword Var3; 32 bits
word Var2; word Var2;
Var2 Var3
dword Var3; word Var4;
16 bits 32 bits
word Var4; byte Var1;
Var2 } tData; } tData; Var3
16 bits 32 bits
32 32
Var3 Var3
32 bits 32 bits
Var1 Var2 Var3 Var3
Var3 8 bits 16 bits 32 bits 32 bits Var2
32 bits 16 bits
Var3 Var4 Var2 Var4
Var3 32 bits 16 bits 16 bits 16 bits Var2
32 bits 16 bits
Var4 Var1
Var3 16 bits 8 bits Var4
32 bits 16 bits
Var4 Var4
16 bits 16 bits
Memory alignment can be simplified by declaring first the 32-bit variables, then 16-bit, then 8-bit.
Var4 Porting this to a 32-bit architecture ensures that there is no misaligned access to variables, Var1
16 bits thereby saving processor time. 8 bits
Organizing structures like this means that were less dependent upon tools that may do this
automatically and may actually help these tools.

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Variable Types

Global
Global storage and global scope

Static
Global storage and local scope

Local
Local storage and local scope

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Storage Class Modifiers

The following keywords are used with variable declarations, to specify


specific needs or conditions associated with the storage of the variables in
memory:

static
volatile
const

These three key words, together, allow us to write not only better code,
but also tighter and more reliable code

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
42
Static Variables

When applied to variables, static has two primary functions:

A variable declared static within the body of a function maintains its value between
function invocations
A variable declared static within a module, but outside the body of a function, is
accessible by all functions within that module

For Embedded Systems:

Encapsulation of persistent data


Modular coding (data hiding)
Hiding of internal processing in each module

Note that static variables are stored globally, and not on the stack

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
43
Static Variable Example

Before entering This is part of the ANSI C


MyFunction() startup copy down procedure.
the 1st time,
myVar = 0

FILE1.c FILE2.c

#include <FILE2.h> void MyFunction (void)


//includes functions in file FILE2.c {
//Definition of MyFunction in FILE2.C
void main (void)
{ static byte myVar = 0;
MyFunction(); //included in FILE2.c //local variable declared static

MyFunction(); //included in FILE2.c myVar = myVar + 1;


} }

Before entering myVar is a local variable


MyFunction() but keeps its value
the 2nd time, because it is static.
myVar = 1

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
44
Static Functions

Functions declared static within a module may only be called by other


functions within that module

Features:
Good structured programming practice
Can result in smaller and/or faster code

Advantages:
Since the compiler knows at compile time exactly what functions can call a
given static function, it may strategically place the static function such that it
may be called using a short version of the call or jump instruction

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
45
Static Functions An Example

stuff.c

main.c stuff.c
// Local prototypes =================
//
static byte InternalFunction1 (byte);
static byte InternalFunction2 (byte);
static byte InternalFunction1 (byte)
// External functions ===============
{
//do_something;
byte ExternalFunction (byte)
}
extern byte ExternalFunction (byte); {
InternalFunction1(param1);
InternalFunction2(param2);
static byte InternalFunction2 (byte)
void main (void)
{ return_something;
{ } do_something_else;
byte n;
}
// Local functions ==================
n = ExternalFunction (byte); //
} static byte InternalFunction1
byte ExternalFunction (byte) (byte)
{
{
do_something;
InternalFunction1(param1);
} InternalFunction2(param2);
return_something;
static
} byte InternalFunction2 (byte)
{
do_something_else;
}

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
46
Volatile Variables

A volatile variable is one whose value may be change outside the normal
program flow

In embedded systems, there are two ways this can happen:


Via an interrupt service routine
As a consequence of hardware action

It is considered to be very good practice to declare all peripheral registers in


embedded devices as volatile

The standard C solution:


#define PORTA (*((volatile unsigned char*) (0x0000)))
This macro defines PORTA to be the content of a pointer to an unsigned char.
This is portable over any architecture but not easily readable.
And it doesnt take advantage of the S08s bit manipulation capabilities.

The CodeWarrior solution:


extern volatile PTADSTR _PTAD @0x00000000;

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
47
CodeWarrior Declaration for PORT A Data Register
/*** PTAD - Port A Data Register; 0x00000000 ***/
typedef union {
byte Byte;
struct {
byte PTAD0 :1; /* Port A Data Register Bit 0 */
byte PTAD1 :1; /* Port A Data Register Bit 1 */
byte PTAD2 :1; /* Port A Data Register Bit 2 */
byte PTAD3 :1; /* Port A Data Register Bit 3 */
byte PTAD4 :1; /* Port A Data Register Bit 4 */
byte PTAD5 :1; /* Port A Data Register Bit 5 */
byte PTAD6 :1; /* Port A Data Register Bit 6 */
byte PTAD7 :1; /* Port A Data Register Bit 7 */
} Bits;
} PTADSTR;
extern volatile PTADSTR _PTAD @0x00000000;
#define PTAD _PTAD.Byte
#define PTAD_PTAD0 _PTAD.Bits.PTAD0
#define PTAD_PTAD1 _PTAD.Bits.PTAD1
#define PTAD_PTAD2 _PTAD.Bits.PTAD2
#define PTAD_PTAD3 _PTAD.Bits.PTAD3
#define PTAD_PTAD4 _PTAD.Bits.PTAD4
#define PTAD_PTAD5 _PTAD.Bits.PTAD5
#define PTAD_PTAD6 _PTAD.Bits.PTAD6
#define PTAD_PTAD7 _PTAD.Bits.PTAD7

#define PTAD_PTAD0_MASK 0x01


#define PTAD_PTAD1_MASK 0x02
#define PTAD_PTAD2_MASK 0x04
#define PTAD_PTAD3_MASK 0x08
#define PTAD_PTAD4_MASK 0x10
#define PTAD_PTAD5_MASK 0x20
#define PTAD_PTAD6_MASK 0x40
#define PTAD_PTAD7_MASK 0x80

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Volatile Variables are Never Optimized

unsigned char PORTA @ 0x00;


unsigned char SCI1S1 @ 0x1C;
unsigned char value;

void main (void) without volatile keyword


{
mov #5,PORTA
PORTA = 0x05; // PORTA = 00000101
lda #10
PORTA = 0x05; // PORTA = 00000101
sta @value
SCI1S1;
value = 10;
}

volatile unsigned char PORTA @ 0x00;


volatile unsigned char SCI1S1 @ 0x1C;
unsigned char value;
with volatile keyword
void main (void)
{ mov #5,PORTA
PORTA = 0x05; // PORTA = 00000101 mov #5,PORTA
PORTA = 0x05; // PORTA = 00000101 lda SCI1S1
SCI1S1; lda #10
value = 10; sta @value
}

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
49
Const Variables

It is safe to assume that a parameter along with the keyword const means
a read-only parameter.

Some compilers create a genuine variable in RAM to hold the const


variable. Upon system software initialization, the read-only value is
copied into RAM. On RAM-limited systems, this can be a significant
penalty.

Compilers for Embedded Systems, like CodeWarrior, store const


variables in ROM (or Flash). However, the read-only variable is still
treated as a variable and accessed as such, although the compiler protects
const definitions from inadvertent writing. Each const variable must be
declared with an initialization value.

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
50
Keyword Const

For Embedded Systems:


Parameters defined const are allocated in ROM space
The compiler protects const definitions of inadvertent writing
Express the intended usage of a parameter

const unsigned short a;


unsigned short const a;
const unsigned short *a;
unsigned short * const a;

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
51
Lab2

#pragma
#pragma DATA_SEG
DATA_SEG MY_ZEROPAGE
MY_ZEROPAGE
byte
byte near n;
near n;

#pragma
#pragma DATA_SEG
DATA_SEG DEFAULT_RAM
DEFAULT_RAM
byte
byte far var
far var == 7;
7;

void
void main(void)
main(void)
{{
EnableInterrupts;
EnableInterrupts;

//
// initialize
initialize LEDs
LEDs
PTCD
PTCD == 0xFF;
0xFF; //
// set
set default
default value
value for
for Port
Port CC
PTCDD
PTCDD == 0b00111100;
0b00111100; //
// set
set LED
LED port
port pins
pins as outputs
as outputs

for
for (;;)
(;;)
{{
__RESET_WATCHDOG();
__RESET_WATCHDOG(); /*
/* feeds
feeds the
the dog
dog */
*/

for
for (n=0;;n++)
(n=0;;n++)
{{
PTCD++;
PTCD++; //
// blink
blink LEDs
LEDs
var++;
var++;
}}

}} /*
/* loop
loop forever
forever */
*/
/*
/* please
please make
make sure
sure that
that you
you never
never leave
leave main
main */
*/
}}

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Lab2

#pragma
#pragma DATA_SEG
DATA_SEG DEFAULT_RAM
DEFAULT_RAM
byte
byte far var;
far var;

void
void main(void)
main(void)
{{
#pragma
#pragma DATA_SEG
DATA_SEG MY_ZEROPAGE
MY_ZEROPAGE
static
static byte
byte near
near n;
n;

EnableInterrupts;
EnableInterrupts;

//
// initialize
initialize LEDs
LEDs
PTCD
PTCD == 0xFF;
0xFF; //
// set
set default
default value
value for
for Port
Port CC
PTCDD
PTCDD == 0b00111100;
0b00111100; //
// set
set LED
LED port
port pins
pins as outputs
as outputs

for
for (;;)
(;;)
{{
__RESET_WATCHDOG();
__RESET_WATCHDOG(); /*
/* feeds
feeds the
the dog
dog */
*/

for
for (n=0;;n++)
(n=0;;n++)
{{
PTCD++;
PTCD++; //
// blink
blink LEDs
LEDs
var++;
var++;
}}

}} /*
/* loop
loop forever
forever */
*/
/*
/* please
please make
make sure
sure that
that you
you never
never leave
leave main
main */
*/
}}

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Very Basic Software Flow

Reset

_EntryPoint() Hardware Initialization

_Startup() Software Initialization

main() Main Control Loop


Reset_Watchdog

Software Events Hardware Events

Subroutine_1() Interrupt_Service_Routine_1()

Subroutine_2()
Interrupt_Service_Routine_2()
Subroutine_3()
Interrupt_Service_Routine_3()

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
DHJK 03-Feb-05
Project Software Architecture
C for Embedded Systems Programming

TM
Project Software Architecture

Application Project 1

Files developed completely


Application
by the user
Files

main.c

Translation

Files that connect the Project Project


available target peripherals Globals Interrupts
with how the application
needs to use them Project1_mcu.h
Project1_mcu.c

Foundation

Files that are consistent Peripheral Linker ANSI C


with MCU documentation Definitions Parameters Libraries
and typically need
no modification by the user
mcu.h target.prm ansixx.lib
Start08.c

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
DHJK 03-Feb-05
Project Software Architecture

Application Project 1 Project 2

Files developed completely


Application Copy Application
by the user
Files Files

main.c main.c

Translation

Files that connect the Project Project Change Project Project


available target peripherals Globals Interrupts Globals Interrupts
with how the application
needs to use them Project1_mcu.h Project2_mcu.h
Project1_mcu.c Project2_mcu.c

Foundation

Files that are consistent Peripheral Linker ANSI C Replace Peripheral Linker ANSI C
with MCU documentation Definitions Parameters Libraries Definitions Parameters Libraries
and typically need
no modification by the user
mcu.h target.prm ansixx.lib mcu.h target.prm ansixx.lib
Start08.c Start12.c

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
DHJK 03-Feb-05
Modular File Organization

main.c system.h

terminal.c terminal.h ext_e2.c ext_e2.h sensor.c sensor.h

sci.c sci.h spi.c spi.h i2c.c i2c.h

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Example system.h
/***********************************************************************************************\
/***********************************************************************************************\
** Project
Project Name
Name
\***********************************************************************************************/
\***********************************************************************************************/
system.h #ifndef
#ifndef _SYSTEM_H_
_SYSTEM_H_
#define
#define _SYSTEM_H_
_SYSTEM_H_
#include
#include <hidef.h>
<hidef.h> /*
/* for
for EnableInterrupts
EnableInterrupts macro
macro */
*/
#include
#include "derivative.h"
"derivative.h" /*
/* include
include peripheral
peripheral declarations
declarations */
*/
/***********************************************************************************************\
/***********************************************************************************************\
** Public
Public type
type definitions
definitions
\***********************************************************************************************/
\***********************************************************************************************/
/***********************************************************************************************
/***********************************************************************************************
**
**
**
** Variable
Variable type
type definition:
definition: BIT_FIELD
BIT_FIELD
*/
*/
typedef union
typedef union
{{
byte
byte Byte;
Byte;
struct
struct {{
byte
byte _0
_0 :1;
:1;
byte
byte _1
_1 :1;
:1;
byte _2
byte _2 :1;
:1;
byte _3
byte _3 :1;
:1;
byte _4
byte _4 :1;
:1;
byte _5
byte _5 :1;
:1;
byte _6
byte _6 :1;
:1;
byte _7
byte _7 :1;
:1;
}} Bit;
Bit;
}} BIT_FIELD;
BIT_FIELD;

/***********************************************************************************************
/***********************************************************************************************
**
**
**
** Variable
Variable type
type definition:
definition: tword
tword
*/
*/
typedef
typedef union
union
{{
unsigned
unsigned short
short Word;
Word;
struct
struct
{{
byte
byte hi;
hi;
byte
byte lo;
lo;
}} Byte;
Byte;
}} tword;
tword;

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Example system.h
/***********************************************************************************************\
/***********************************************************************************************\
** Project
Project includes
includes
\***********************************************************************************************/
\***********************************************************************************************/
system.h #include
#include "mma845x.h"
"mma845x.h" //
// MMA845xQ
MMA845xQ macros
macros
#include
#include "iic.h"
"iic.h" //
// IIC
IIC macros
macros
#include
#include "sci.h"
"sci.h" //
// SCI
SCI macros
macros
#include
#include "spi.h"
"spi.h" //
// SPI
SPI macros
macros
#include
#include "terminal.h"
"terminal.h" //
// Terminal
Terminal interface
interface macros
macros
/***********************************************************************************************\
/***********************************************************************************************\
** Public
Public macros
macros
\***********************************************************************************************/
\***********************************************************************************************/
/***********************************************************************************************
/***********************************************************************************************
**
**
**
** General
General System
System Control
Control
**
**
** 0x1802 SOPT1
** 0x1802 SOPT1 System
System Options
Options Register
Register 11
**
** 0x1803
0x1803 SOPT2
SOPT2 System
System Options
Options Register
Register 22
**
** 0x1808
0x1808 SPMSC1
SPMSC1 System
System Power
Power Management
Management Status
Status and
and Control
Control 11 Register
Register
**
** 0x1809
0x1809 SPMSC2
SPMSC2 System
System Power
Power Management
Management Status
Status and
and Control
Control 22 Register
Register
** 0x180B SPMSC3
** 0x180B SPMSC3 System Power Management Status and Control 3 Register
System Power Management Status and Control 3 Register
** 0x180E SCGC1
** 0x180E SCGC1 System Clock Gating Control 1 Register
System Clock Gating Control 1 Register
**
** 0x180F
0x180F SCGC2
SCGC2 System
System Clock
Clock Gating
Gating Control
Control 22 Register
Register
** 0x000F IRQSC
** 0x000F IRQSC Interrupt
Interrupt Pin
Pin Request
Request Status
Status and
and Control
Control Register
Register
*/
*/
#define
#define init_SOPT1
init_SOPT1 0b01000010
0b01000010
/*
/* 1100001U
1100001U == reset
reset
**
** |||xxx||
|||xxx||
**
** |||
||| |+--
|+-- RSTPE
RSTPE =0
=0 :: RESET
RESET pin
pin function
function disabled
disabled
**
** |||
||| +---
+--- BKGDPE
BKGDPE =1
=1 :: Background
Background Debug
Debug pin
pin enabled
enabled
**
** ||+-------
||+------- STOPE
STOPE =0
=0 :: Stop
Stop Mode
Mode disabled
disabled
**
** |+--------
|+-------- COPT
COPT =1
=1 :: Long
Long COP
COP timeout
timeout period
period selected
selected
**
** +---------
+--------- COPE
COPE =0
=0 :: COP
COP Watchdog
Watchdog timer
timer disabled
disabled
*/
*/
#define
#define init_SOPT2
init_SOPT2 0b00000010
0b00000010
/*
/* 00000000
00000000 == reset
reset
**
** |x||x|||
|x||x|||
**
** || ||
|| ||+--
||+-- ACIC1
ACIC1 =0
=0 :: ACMP1
ACMP1 output
output not
not connected
connected to
to TPM1CH0
TPM1CH0 input
input
**
** || ||
|| |+---
|+--- IICPS
IICPS =1
=1 :: SDA
SDA on
on PTB6;
PTB6; SCL
SCL on
on PTB7
PTB7
**
** || ||
|| +----
+---- ACIC2
ACIC2 =0
=0 :: ACMP2
ACMP2 output
output not
not connected
connected to
to TPM2CH0
TPM2CH0 input
input
**
** || |+------
|+------ TPM1CH2PS
TPM1CH2PS =0
=0 :: TPM1CH2
TPM1CH2 on
on PTA6
PTA6
**
** || +-------
+------- TPM2CH2PS
TPM2CH2PS =0
=0 :: TPM2CH2
TPM2CH2 on
on PTA7
PTA7
**
** +---------
+--------- COPCLKS
COPCLKS =0
=0 :: COP
COP clock
clock source
source is
is internal
internal 1kHz
1kHz reference
reference
*/
*/

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Example system.h
/***********************************************************************************************
/***********************************************************************************************
**
**
**
** Port
Port I/O
I/O
system.h **
**
**
** 0x0000
0x0000 PTAD
PTAD Port
Port AA Data
Data Register
Register
**
** 0x0001
0x0001 PTADD
PTADD Port
Port AA Data
Data Direction
Direction Register
Register
**
** 0x0002
0x0002 PTBD
PTBD Port
Port BB Data
Data Register
Register
**
** 0x0003
0x0003 PTBDD
PTBDD Port
Port BB Data
Data Direction
Direction Register
Register
**
** 0x0004
0x0004 PTCD
PTCD Port
Port CC Data
Data Register
Register
**
** 0x0005
0x0005 PTCDD
PTCDD Port
Port CC Data
Data Direction
Direction Register
Register
**
** 0x0006
0x0006 PTDD
PTDD Port
Port DD Data
Data Register
Register
**
** 0x0007
0x0007 PTDDD
PTDDD Port
Port DD Data
Data Direction
Direction Register
Register
**
** 0x1840
0x1840 PTAPE
PTAPE Port
Port AA Pull
Pull Enable
Enable Register
Register
**
** 0x1841
0x1841 PTASE
PTASE Port
Port AA Slew
Slew Rate
Rate Enable
Enable Register
Register
**
** 0x1842
0x1842 PTADS
PTADS Port
Port AA Drive
Drive Strength
Strength Selection
Selection Register
Register
**
** 0x1844
0x1844 PTBPE
PTBPE Port
Port BB Pull
Pull Enable
Enable Register
Register
** 0x1845 PTBSE
** 0x1845 PTBSE Port B Slew Rate Enable Register
Port B Slew Rate Enable Register
**
** 0x1846
0x1846 PTBDS
PTBDS Port
Port BB Drive
Drive Strength
Strength Selection
Selection Register
Register
** 0x1848 PTCPE
** 0x1848 PTCPE Port
Port CC Pull
Pull Enable
Enable Register
Register
** 0x1849 PTCSE
** 0x1849 PTCSE Port C Slew Rate Enable Register
Port C Slew Rate Enable Register
**
** 0x184A
0x184A PTCDS
PTCDS Port
Port CC Drive
Drive Strength
Strength Selection
Selection Register
Register
*/
*/
#define
#define init_PTAD
init_PTAD 0b10000000
0b10000000
//
// 00000000
00000000 == reset
reset
#define
#define init_PTADD
init_PTADD 0b10000000
0b10000000
//
// 00000000
00000000 == reset
reset
#define
#define init_PTAPE
init_PTAPE 0b00000110
0b00000110
//
// 00000000
00000000 == reset
reset
#define
#define init_PTASE
init_PTASE 0b00000000
0b00000000
//
// 00000000
00000000 == reset
reset
#define
#define init_PTADS
init_PTADS 0b00000000
0b00000000
//
// 00000000
00000000 == reset
reset
/*
/* ||||||||
||||||||
**
** |||||||+--
|||||||+-- X_OUT
X_OUT
**
** ||||||+---
||||||+--- Y_OUT
Y_OUT -- MMA845x
MMA845x INT1
INT1
**
** |||||+----
|||||+---- Z_OUT
Z_OUT -- MMA845x
MMA845x INT2
INT2
**
** ||||+-----
||||+----- DIS_MCU
DIS_MCU
**
** |||+------
|||+------ BKGD
BKGD
**
** ||+-------
||+------- RESET
RESET
**
** |+--------
|+-------- EXTRA_AD
EXTRA_AD
**
** +---------
+--------- LEDB_BB
LEDB_BB -- Blue
Blue LED
LED cathode
cathode
*/
*/
#define
#define INT1_IS_ACTIVE
INT1_IS_ACTIVE (PTAD_PTAD1
(PTAD_PTAD1 ==
== 0)
0)
#define
#define LED_BlueOn
LED_BlueOn (PTAD_PTAD7
(PTAD_PTAD7 == 0)
0)
#define
#define LED_BlueOff
LED_BlueOff (PTAD_PTAD7
(PTAD_PTAD7 == 1)
1)

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Example system.h
#define
#define init_PTCD
init_PTCD 0b10111011
0b10111011
//
// 00000000
00000000 == reset
reset
#define
#define init_PTCDD
init_PTCDD 0b10110111
0b10110111
system.h //
// 00000000
00000000 == reset
reset
#define
#define init_PTCPE
init_PTCPE 0b01000000
0b01000000
//
// 00000000
00000000 == reset
reset
#define
#define init_PTCSE
init_PTCSE 0b00000000
0b00000000
//
// 00000000
00000000 == reset
reset
#define
#define init_PTCDS
init_PTCDS 0b00000000
0b00000000
//
// 00000000
00000000 == reset
reset
/*
/* ||||||||
||||||||
**
** |||||||+--
|||||||+-- LEDG_BB
LEDG_BB -- Green
Green LED
LED cathode
cathode
**
** ||||||+---
||||||+--- SLEEP
SLEEP -- MMA845x
MMA845x CS
CS
**
** |||||+----
|||||+---- G_SEL_2
G_SEL_2 -- MMA845x
MMA845x SA0
SA0
**
** ||||+-----
||||+----- G_SEL_1
G_SEL_1
**
** |||+------
|||+------ LEDR_BB
LEDR_BB -- Red
Red LED
LED cathode
cathode
**
** ||+-------
||+------- BUZZER_BB
BUZZER_BB
**
** |+--------
|+-------- PUSH_BUTTON
PUSH_BUTTON
**
** +---------
+--------- LED_OB
LED_OB -- Yellow
Yellow LED
LED cathode
cathode
*/
*/
#define
#define LED_GreenOn
LED_GreenOn (PTCD_PTCD0
(PTCD_PTCD0 == 0)
0)
#define
#define LED_GreenOff
LED_GreenOff (PTCD_PTCD0
(PTCD_PTCD0 == 1)
1)
#define
#define SENSOR_SHUTDOWN
SENSOR_SHUTDOWN (PTCD_PTCD1
(PTCD_PTCD1 == 0)
0)
#define
#define SENSOR_ACTIVE
SENSOR_ACTIVE (PTCD_PTCD1
(PTCD_PTCD1 == 1)
1)
#define
#define LED_RedOn
LED_RedOn (PTCD_PTCD4
(PTCD_PTCD4 == 0)
0)
#define
#define LED_RedOff
LED_RedOff (PTCD_PTCD4
(PTCD_PTCD4 == 1)
1)
#define
#define LED_YellowOn
LED_YellowOn (PTCD_PTCD7
(PTCD_PTCD7 == 0)
0)
#define
#define LED_YellowOff
LED_YellowOff (PTCD_PTCD7
(PTCD_PTCD7 == 1)
1)
#define
#define SA0_PIN
SA0_PIN (PTCD_PTCD2)
(PTCD_PTCD2)

/***********************************************************************************************\
/***********************************************************************************************\
** Public
Public memory
memory declarations
declarations
\***********************************************************************************************/
\***********************************************************************************************/

/***********************************************************************************************\
/***********************************************************************************************\
** Public
Public prototypes
prototypes
\***********************************************************************************************/
\***********************************************************************************************/

#endif
#endif /*
/* _SYSTEM_H_
_SYSTEM_H_ */
*/

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Example sci.h
/***********************************************************************************************\
/***********************************************************************************************\
** Project
Project name
name
**
sci.h ** Filename:
Filename: sci.h
sci.h
**
\***********************************************************************************************/
\***********************************************************************************************/
#ifndef
#ifndef _SCI_H_
_SCI_H_
#define
#define _SCI_H_
_SCI_H_
/***********************************************************************************************\
/***********************************************************************************************\
** Public
Public macros
macros
\***********************************************************************************************/
\***********************************************************************************************/
#define
#define BUFFER_RX_SIZE
BUFFER_RX_SIZE 10
10
#define
#define BUFFER_TX_SIZE
BUFFER_TX_SIZE 200
200
/***********************************************************************************************
/***********************************************************************************************
**
**
**
** Serial
Serial Communications
Communications Interface
Interface (SCI)
(SCI)
**
**
** 0x0020 SCIBDH
** 0x0020 SCIBDH SCI
SCI Baud Rate Register High
Baud Rate Register High
**
** 0x0021
0x0021 SCIBDL
SCIBDL SCI
SCI Baud
Baud Rate
Rate Register
Register Low
Low
** 0x0022 SCIC1
** 0x0022 SCIC1 SCI Control Register
SCI Control Register 1 1
** 0x0023 SCIC2
** 0x0023 SCIC2 SCI Control Register
SCI Control Register 2 2
**
** 0x0024
0x0024 SCIS1
SCIS1 SCI
SCI Status
Status Register
Register 11
** 0x0025 SCIS2
** 0x0025 SCIS2 SCI
SCI Status
Status Register
Register 22
**
** 0x0026
0x0026 SCIC3
SCIC3 SCI
SCI Control
Control Register
Register 33
**
** 0x0027
0x0027 SCID
SCID SCI
SCI Data
Data Register
Register
**
**
**
** SCI
SCI target
target baudrate
baudrate == 115.2k
115.2k
**
** MCU
MCU bus
bus frequency
frequency == 9.216MHz
9.216MHz
**
**
**
** SCI
SCI Baud
Baud Rate
Rate Register
Register == 55
**
**
**
** SCI
SCI baudrate
baudrate == bus
bus // (16
(16 ** BR)
BR)
**
** == 9.216MHz
9.216MHz // (16
(16 ** 5)
5)
**
** == 115.2k
115.2k
*/
*/
#define
#define init_SCIBDH
init_SCIBDH 0x00
0x00
#define
#define init_SCIBDL
init_SCIBDL 0x05
0x05

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Example sci.h
#define
#define ASCII_BS
ASCII_BS 0x08
0x08
#define
#define ASCII_LF
ASCII_LF 0x0A
0x0A
sci.h #define
#define ASCII_CR
ASCII_CR 0x0D
0x0D
#define
#define ASCII_DEL
ASCII_DEL 0x7F
0x7F
/***********************************************************************************************\
/***********************************************************************************************\
** Public
Public type
type definitions
definitions
\***********************************************************************************************/
\***********************************************************************************************/
/***********************************************************************************************\
/***********************************************************************************************\
** Public
Public memory
memory declarations
declarations
\***********************************************************************************************/
\***********************************************************************************************/
#pragma
#pragma DATA_SEG
DATA_SEG MY_ZEROPAGE
MY_ZEROPAGE
extern
extern byte
byte BufferRx[BUFFER_RX_SIZE];
BufferRx[BUFFER_RX_SIZE];
#pragma
#pragma DATA_SEG
DATA_SEG DEFAULT
DEFAULT
/***********************************************************************************************\
/***********************************************************************************************\
** Public
Public prototypes
prototypes
\***********************************************************************************************/
\***********************************************************************************************/
void
void SCIControlInit
SCIControlInit (void);
(void);
void
void SCISendString
SCISendString (byte
(byte *pStr);
*pStr);
void
void SCI_CharOut(byte
SCI_CharOut(byte data);
data);
void
void SCI_NibbOut(byte data);
SCI_NibbOut(byte data);
void
void SCI_ByteOut(byte
SCI_ByteOut(byte data);
data);
void
void SCI_putCRLF
SCI_putCRLF (void);
(void);
byte
byte SCI_CharIn(void);
SCI_CharIn(void);
byte
byte SCI_ByteIn(void);
SCI_ByteIn(void);
void
void SCI_s12dec_Out
SCI_s12dec_Out (tword
(tword data);
data);
void
void SCI_s8dec_Out(tword
SCI_s8dec_Out(tword data);
data);
void
void SCI_s12int_Out
SCI_s12int_Out (tword
(tword data);
data);
void
void SCI_s12frac_Out
SCI_s12frac_Out (tword
(tword data);
data);
byte
byte isnum
isnum (byte
(byte data);
data);
byte
byte ishex
ishex (byte
(byte data);
data);
byte
byte tohex
tohex (byte
(byte data);
data);
void
void hex2ASCII
hex2ASCII (byte
(byte data,
data, byte*
byte* ptr);
ptr);

#endif
#endif /*
/* _SCI_H_
_SCI_H_ */
*/

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Example terminal.h
/***********************************************************************************************\
/***********************************************************************************************\
** Project
Project Name
Name
**
terminal.h ** Filename:
Filename: terminal.h
terminal.h
**
\***********************************************************************************************/
\***********************************************************************************************/
#ifndef
#ifndef _TERMINAL_H_
_TERMINAL_H_
#define
#define _TERMINAL_H_
_TERMINAL_H_
/***********************************************************************************************\
/***********************************************************************************************\
** Public
Public macros
macros
\***********************************************************************************************/
\***********************************************************************************************/
/***********************************************************************************************\
/***********************************************************************************************\
** Public
Public type
type definitions
definitions
\***********************************************************************************************/
\***********************************************************************************************/
/***********************************************************************************************\
/***********************************************************************************************\
** Public
Public memory
memory declarations
declarations
\***********************************************************************************************/
\***********************************************************************************************/
/***********************************************************************************************\
/***********************************************************************************************\
** Public
Public prototypes
prototypes
\***********************************************************************************************/
\***********************************************************************************************/
void
void TerminalInit
TerminalInit (void);
(void);
void
void ProcessTerminal
ProcessTerminal (void);
(void);
void
void OutputTerminal
OutputTerminal (byte
(byte BlockID,
BlockID, byte
byte *ptr);
*ptr);
#endif
#endif /*
/* _TERMINAL_H_
_TERMINAL_H_ */
*/

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
Example terminal.c
/***********************************************************************************************\
/***********************************************************************************************\
** Project
Project Name
Name
**
terminal.c ** Filename:
Filename: terminal.c
terminal.c
**
\***********************************************************************************************/
\***********************************************************************************************/

#include
#include "system.h"
"system.h"

/***********************************************************************************************\
/***********************************************************************************************\
** Private
Private macros
macros
\***********************************************************************************************/
\***********************************************************************************************/

/***********************************************************************************************\
/***********************************************************************************************\
** Private
Private type
type definitions
definitions
\***********************************************************************************************/
\***********************************************************************************************/

/***********************************************************************************************\
/***********************************************************************************************\
** Private
Private prototypes
prototypes
\***********************************************************************************************/
\***********************************************************************************************/

/***********************************************************************************************\
/***********************************************************************************************\
** Private
Private memory
memory declarations
declarations
\***********************************************************************************************/
\***********************************************************************************************/

#pragma
#pragma DATA_SEG
DATA_SEG MY_ZEROPAGE
MY_ZEROPAGE

#pragma
#pragma DATA_SEG
DATA_SEG DEFAULT_RAM
DEFAULT_RAM

/***********************************************************************************************\
/***********************************************************************************************\
** Public
Public functions
functions
\***********************************************************************************************/
\***********************************************************************************************/

/***********************************************************************************************\
/***********************************************************************************************\
** Private
Private functions
functions
\***********************************************************************************************/
\***********************************************************************************************/

Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM
owners. Freescale Semiconductor, Inc. 2010.
TM