Professional Documents
Culture Documents
Embedded C
Embedded C
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
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.
TM
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.
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
owners. Freescale Semiconductor, Inc. 2010.
TM
Why Change to C?
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.
C retains the basic philosophy that programmers know what they are doing.
C only requires that they state their intentions explicitly.
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.
TM
Why Not C?
These are some of the common issues that we encounter when
considering moving to the C programming language:
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.
TM
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, ).
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
owners. Freescale Semiconductor, Inc. 2010.
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.
TM
TM
Nice
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.
TM
Compiler Requirements
.asm
.asm
.o
.o
Compiler
.c.c
+
.cpp
.cpp
.h
.h
listing
listing
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.
TM
Front End
Code
Generator
Back End
Object files
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.
TM
Overview
After
compiling process is done, the linker works with the object files
generated in order to link the final application
There
The
Information
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.
TM
Linker Requirements
.o
.o
.o
.o
.s19
.s19
Linker
.map
.map
.o
.o
Target
description
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.
TM
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
owners. Freescale Semiconductor, Inc. 2010.
TM
MC9S12XEP100
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.
ColdFire MCF51QE128
TM
Memory Models
Memory models have a meaning depending on the target used
Model
HC(S)08
Tiny
Small
Banked
Large
HC(S)12
S12X
Data
Function
8-bits
unless
specified
with __far
16-bits
16-bits
unless
specified
16-bits
16-bits
24-bits
24-bits
24-bits
TM
.asm
.asm
.c.c
.cpp
.cpp
Compiler
+
.h.h
.o.o
Linker
listing
listing
.s19
.s19
.map
.map
Target
description
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.
TM
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
JSR MyFunction
Applied to
A variable
variables
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
owners. Freescale Semiconductor, Inc. 2010.
TM
Linker
For
For
0x0000
DIRECT PAGE
0x00FF
0x0100
RAM
variables
#pragma
Compiler
For
code
#pragma
To
char var;
var++;
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.
ldhx
inc
#var
,x
inc
var
ldhx
inc
#var
,x
TM
SEGMENTS SECTION
Defines the memory available in the MCU,
providing full control over memory allocation.
This is essentially a translation of the data sheet.
SEGMENTS
SEGMENTS /*
/* Here
Here all
all RAM/ROM
RAM/ROM
Z_RAM
Z_RAM
RAM
RAM
/*
/* unbanked
unbanked FLASH
FLASH ROM
ROM */
*/
ROM
ROM
ROM1
ROM1
ROM2
ROM2
ROM3
ROM3
/*
/* banked
banked FLASH
FLASH ROM
ROM */
*/
PPAGE_0
PPAGE_0
PPAGE_0_1
PPAGE_0_1
PPAGE_2
PPAGE_2
/*
/* PPAGE_1
PPAGE_1
/*
/* PPAGE_3
PPAGE_3
END
END
areas
areas of
of the
the device
device are
are listed.
listed. Used
Used in
in PLACEMENT
PLACEMENT below.
below. */
*/
== READ_WRITE
0x0060
READ_WRITE
0x0060 TO
TO 0x00FF;
0x00FF;
== READ_WRITE
0x0100
READ_WRITE
0x0100 TO
TO 0x0FFF;
0x0FFF;
==
==
==
==
READ_ONLY
READ_ONLY
READ_ONLY
READ_ONLY
READ_ONLY
READ_ONLY
READ_ONLY
READ_ONLY
0x18A1
0x18A1
0x103C
0x103C
0xC000
0xC000
0xFFC0
0xFFC0
TO
TO
TO
TO
TO
TO
TO
TO
==
==
==
==
==
READ_ONLY
READ_ONLY
READ_ONLY
READ_ONLY
READ_ONLY
READ_ONLY
READ_ONLY
READ_ONLY
READ_ONLY
READ_ONLY
0x008002
0x008002
0x009800
0x009800
0x028000
0x028000
0x018000
0x018000
0x038000
0x038000
0x7FFF;
0x7FFF;
0x17FF;
0x17FF;
0xFFAB;
0xFFAB;
0xFFD1;
0xFFD1;
TO
TO
TO
TO
TO
TO
TO
TO
TO
TO
0x00903B;
0x00903B;
0x0098A0;
0x0098A0;
0x02BFFF;
0x02BFFF;
0x01BFFF;
0x01BFFF;
0x03BFFF;
0x03BFFF;
/*
/* PAGE
PAGE partially
partially containe
containe
PAGE
PAGE already
already contained
contained in
in
PAGE
PAGE already
already contained
contained in
in
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;
PLACEMENT SECTION
Provides the ability to assign each section from
the application to specific memory segments.
The names identified in this section are used in
the source code, for example:
#pragma DATA_SEG MY_ZEROPAGE
_PRESTART,
_PRESTART, STARTUP,
STARTUP,
ROM_VAR,
ROM_VAR,
STRINGS,
STRINGS,
VIRTUAL_TABLE_SEGMENT,
VIRTUAL_TABLE_SEGMENT,
NON_BANKED,
NON_BANKED,
DEFAULT_ROM,
DEFAULT_ROM,
COPY
COPY
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
startup
startup code
code and
and data
data structures
structures */
*/
constant
constant variables
variables */
*/
string
string literals
literals */
*/
C++
C++ virtual
virtual table
table segment
segment */
*/
runtime
runtime routines
routines which
which must
must not
not be
be banked
banked */
*/
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,
MY_ZEROPAGE
MY_ZEROPAGE
/*
/* zero
zero page
page variables
variables */
*/
INTO
INTO Z_RAM;
Z_RAM;
/*
/* copy
copy down
down information:
information: how
how to
to initialize
initialize variable
variable
INTO
INTO ROM;
ROM; /*
/* ,ROM1,ROM2,ROM3:
,ROM1,ROM2,ROM3: To
To use
use "ROM1,ROM2,ROM
"ROM1,ROM2,ROM
END
END
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
owners. Freescale Semiconductor, Inc. 2010.
TM
= READ_WRITE
= READ_WRITE
0x0060 TO 0x00FF;
0x0100 TO 0x0FFF;
STACKSIZE 0x100
SEGMENTS
Z_RAM
RAM
STACK_RAM
END
= READ_WRITE
= READ_WRITE
= READ_WRITE
PLACEMENT
SSTACK
END
INTO
STACK_RAM;
STACKTOP 0x0FFF
or
0x0000
0x0000
Peripheral Registers
0x005F
0x0060
variables
0x0060 TO 0x00FF;
0x0100 TO 0x0EFF;
0x0F00 TO 0x0FFF;
0x00FF
0x0100
Peripheral Registers
Direct Page
variables
variables
stack
0x005F
0x0060
0x00FF
0x0100
Direct Page
variables
SP
RAM
RAM
0x0F00
0x0FFF
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.
0x0FFF
stack
SP
TM
MEMORY SEGMENT
Describes the available memory
MEMORY
MEMORY {{
code
code
userram
userram
}}
(RX)
(RX)
(RWX)
(RWX)
::
::
ORIGIN
ORIGIN
ORIGIN
ORIGIN
==
==
0x00000410,
0x00000410,
0x00800000,
0x00800000,
LENGTH
LENGTH
LENGTH
LENGTH
==
==
0x0001FBF0
0x0001FBF0
0x00002000
0x00002000
SECTIONS
SECTIONS {{
## Heap
Heap and
and Stack
Stack sizes
sizes definition
definition
___heap_size
== 0x0400;
___heap_size
0x0400;
___stack_size
== 0x0400;
___stack_size
0x0400;
SECTIONS SEGMENT
Defines the contents of memory sections and
global symbols
##
##
##
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.
## 88 Kbytes
Kbytes Internal
Internal
___RAM_ADDRESS
___RAM_ADDRESS ==
___RAM_SIZE
==
___RAM_SIZE
SRAM
SRAM
0x00800000;
0x00800000;
0x00002000;
0x00002000;
## 128
128 KByte
KByte Internal
Internal
___FLASH_ADDRESS
___FLASH_ADDRESS
___FLASH_SIZE
___FLASH_SIZE
Flash
Flash Memory
Memory
== 0x00000000;
0x00000000;
== 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
owners. Freescale Semiconductor, Inc. 2010.
TM
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
owners. Freescale Semiconductor, Inc. 2010.
TM
FILE SECTION
Lists the names of all files from which objects
were used
STARTUP SECTION
Lists the prestart code and the values used to
initialize the startup descriptor _startupData.
The startup descriptor is listed member by
member with the initialization data at the right
hand side of the member name
SECTION-ALLOCATION SECTION
Lists those segments for which at least one
object was allocated
*********************************************************************************************
*********************************************************************************************
TARGET
TARGET SECTION
SECTION
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Processor
:: Freescale
Processor
Freescale HC08
HC08
Memory
Memory Model:
Model: SMALL
SMALL
File
File Format
Format :: ELF\DWARF
ELF\DWARF 2.0
2.0
Linker
:: SmartLinker
Linker
SmartLinker V-5.0.39
V-5.0.39 Build
Build 10132,
10132, May
May 13
13 2010
2010
*********************************************************************************************
*********************************************************************************************
FILE
FILE SECTION
SECTION
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------main.obj
Model:
Lang:
main.obj
Model: SMALL,
SMALL,
Lang: ANSI-C
ANSI-C
start08.obj
Model:
Lang:
start08.obj
Model: SMALL,
SMALL,
Lang: ANSI-C
ANSI-C
mc9s08ll64.obj
Model:
Lang:
mc9s08ll64.obj
Model: SMALL,
SMALL,
Lang: ANSI-C
ANSI-C
*********************************************************************************************
*********************************************************************************************
STARTUP
STARTUP SECTION
SECTION
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------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
extern
extern struct
struct _tagStartup
_tagStartup {{
unsigned
11
unsigned nofZeroOut
nofZeroOut
_Range
pZeroOut
0x60
11
_Range
pZeroOut
0x60
_Copy
*toCopyDownBeg
_Copy
*toCopyDownBeg 0x1945
0x1945
}} _startupData;
_startupData;
*********************************************************************************************
*********************************************************************************************
SECTION-ALLOCATION
SECTION-ALLOCATION SECTION
SECTION
Section
Size
From
To
Segment
Section Name
Name
Size Type
Type
From
To
Segment
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------.init
132
RR
0x18A1
0x1924
ROM
.init
132
0x18A1
0x1924
ROM
.startData
14
RR
0x1925
0x1932
ROM
.startData
14
0x1925
0x1932
ROM
.text
18
RR
0x1933
0x1944
ROM
.text
18
0x1933
0x1944
ROM
.copy
22
RR
0x1945
0x1946
ROM
.copy
0x1945
0x1946
ROM
MY_ZEROPAGE
11
R/W
0x60
0x60
Z_RAM
MY_ZEROPAGE
R/W
0x60
0x60
Z_RAM
.abs_section_0
11
N/I
0x0
0x0
.absSeg0
.abs_section_0
N/I
0x0
0x0
.absSeg0
.abs_section_1
11
N/I
0x1
0x1
.absSeg1
.abs_section_1
N/I
0x1
0x1
.absSeg1
.abs_section_2
11
N/I
0x2
0x2
.absSeg2
.abs_section_2
N/I
0x2
0x2
.absSeg2
.abs_section_3
11
N/I
0x3
0x3
.absSeg3
.abs_section_3
N/I
0x3
0x3
.absSeg3
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.
TM
.init
.startData
.text
.copy
.stack
.data
.common
= _PRESTART
= STARTUP
= DEFAULT_ROM
= COPY
= SSTACK
= DEFAULT_RAM
= DEFAULT_RAM
SECTION-ALLOCATION SECTION
Lists those segments for which at least one
object was allocated
TARGET
TARGET SECTION
SECTION
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------PLACEMENT
/*
predefined
Processor
Freescale
HC08
PLACEMENT
/* Here
Here::all
all
predefined
and user
user segments
segments are
are placed
placed into
into the
the SEGMENTS
SEGMENTS defined
defined above.
above. */
*/
Processor
Freescale
HC08and
DEFAULT_RAM,
/*
Memory
DEFAULT_RAM,
/* non-zero
non-zero page
page variables
variables */
*/
Memory Model:
Model: SMALL
SMALL
INTO
File
INTO RAM;
RAM;
File Format
Format :: ELF\DWARF
ELF\DWARF 2.0
2.0
Linker
:: SmartLinker
Linker
SmartLinker V-5.0.39
V-5.0.39 Build
Build 10132,
10132, May
May 13
13 2010
2010
_PRESTART,
/*
_PRESTART,
/* startup
startup code
code */
*/
STARTUP,
/*
*********************************************************************************************
STARTUP,
/* startup
startup data
data structures
structures */
*/
*********************************************************************************************
ROM_VAR,
/*
FILE
ROM_VAR,
/* constant
constant variables
variables */
*/
FILE SECTION
SECTION
STRINGS,
/*
--------------------------------------------------------------------------------------------STRINGS,
/* string
string literals
literals */
*/
--------------------------------------------------------------------------------------------VIRTUAL_TABLE_SEGMENT,
/*
virtual
table
*/
main.obj
Model:
SMALL,
Lang:
VIRTUAL_TABLE_SEGMENT,
/* C++
C++
virtual
table segment
segment
*/ ANSI-C
main.obj
Model:
SMALL,
Lang:
ANSI-C
NON_BANKED,
/*
not
be
start08.obj
Model:
SMALL,
Lang:
ANSI-C
NON_BANKED,
/* runtime
runtime
routines
which must
must
not
be banked
banked */
*/
start08.obj
Model:routines
SMALL, which
Lang:
ANSI-C
DEFAULT_ROM,
mc9s08ll64.obj
Model:
Lang:
DEFAULT_ROM,
mc9s08ll64.obj
Model: SMALL,
SMALL,
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
*********************************************************************************************
STARTUP
STARTUP SECTION
SECTION
PAGED_ROM
/*
--------------------------------------------------------------------------------------------PAGED_ROM
/* routines
routines which
which can
can be
be banked
banked */
*/
--------------------------------------------------------------------------------------------INTO
Entry
INTO PPAGE_2,ROM1,ROM2,ROM3,PPAGE_0,PPAGE_0_1;
PPAGE_2,ROM1,ROM2,ROM3,PPAGE_0,PPAGE_0_1;
Entry point:
point: 0x191C
0x191C (_Startup)
(_Startup)
_startupData
_startupData is
is allocated
allocated at
at 0x1925
0x1925 and
and uses
uses 66 Bytes
Bytes
_DATA_ZEROPAGE,
/*
extern
_DATA_ZEROPAGE,
/* zero
zero page
page variables
variables */
*/
extern struct
struct _tagStartup
_tagStartup {{
MY_ZEROPAGE
INTO
unsigned
11
MY_ZEROPAGE
INTO Z_RAM;
Z_RAM;
unsigned nofZeroOut
nofZeroOut
END
_Range
pZeroOut
0x60
11
END
_Range
pZeroOut
0x60
_Copy
*toCopyDownBeg
_Copy
*toCopyDownBeg 0x1945
0x1945
}} _startupData;
_startupData;
*********************************************************************************************
*********************************************************************************************
SECTION-ALLOCATION
SECTION-ALLOCATION SECTION
SECTION
Section
Size
From
To
Segment
Section Name
Name
Size Type
Type
From
To
Segment
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------.init
132
RR
0x18A1
0x1924
ROM
.init
132
0x18A1
0x1924
ROM
.startData
14
RR
0x1925
0x1932
ROM
.startData
14
0x1925
0x1932
ROM
.text
18
RR
0x1933
0x1944
ROM
.text
18
0x1933
0x1944
ROM
.copy
22
RR
0x1945
0x1946
ROM
.copy
0x1945
0x1946
ROM
MY_ZEROPAGE
11
R/W
0x60
0x60
Z_RAM
MY_ZEROPAGE
R/W
0x60
0x60
Z_RAM
.abs_section_0
11
N/I
0x0
0x0
.absSeg0
.abs_section_0
N/I
0x0
0x0
.absSeg0
.abs_section_1
11
N/I
0x1
0x1
.absSeg1
.abs_section_1
N/I
0x1
0x1
.absSeg1
.abs_section_2
11
N/I
0x2
0x2
.absSeg2
.abs_section_2
N/I
0x2
0x2
.absSeg2
.abs_section_3
11
N/I
0x3
0x3
.absSeg3
.abs_section_3
N/I
0x3
0x3
.absSeg3
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.
TM
256
256
22
R/W
R/W
RR
Summary
Summary of
of section
section sizes
sizes per
per section
section
READ_ONLY
A8
READ_ONLY (R):
(R):
A8 (dec:
(dec:
READ_WRITE
101
READ_WRITE (R/W):
(R/W):
101 (dec:
(dec:
NO_INIT
CD
NO_INIT (N/I):
(N/I):
CD (dec:
(dec:
type:
type:
168)
168)
257)
257)
205)
205)
VECTOR-ALLOCATION SECTION
Lists each vectors address and to where it
points (ie., interrupt service routine)
*********************************************************************************************
*********************************************************************************************
VECTOR-ALLOCATION
VECTOR-ALLOCATION SECTION
SECTION
Address
InitValue
InitFunction
Address
InitValue
InitFunction
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------0xFFFE
0x191C
_Startup
0xFFFE
0x191C
_Startup
OBJECT-ALLOCATION SECTION
Contains the name, address and size of every
allocated object and groups them by module
0x100
0x100
0xFFFE
0xFFFE
0x1FF
0x1FF
0xFFFF
0xFFFF
RAM
RAM
.vectSeg188
.vectSeg188
*********************************************************************************************
*********************************************************************************************
OBJECT-ALLOCATION
OBJECT-ALLOCATION SECTION
SECTION
Name
Module
Addr
hSize
dSize
Ref
Section
RLIB
Name
Module
Addr
hSize
dSize
Ref
Section
RLIB
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------MODULE:
-MODULE:
-- main.obj
main.obj ---- PROCEDURES:
PROCEDURES:
main
1933
12
18
11
.text
main
1933
12
18
.text
-- VARIABLES:
VARIABLES:
nn
60
11
11
22
MY_ZEROPAGE
60
MY_ZEROPAGE
MODULE:
-MODULE:
-- start08.obj
start08.obj ---- PROCEDURES:
PROCEDURES:
loadByte
18A1
EE
14
55
.init
loadByte
18A1
14
.init
Init
18AF
6D
109
11
.init
Init
18AF
6D
109
.init
_Startup
191C
99
99
00
.init
_Startup
191C
.init
-- VARIABLES:
VARIABLES:
_startupData
1925
66
66
44
.startData
_startupData
1925
.startData
-- LABELS:
LABELS:
__SEG_END_SSTACK
200
00
00
11
__SEG_END_SSTACK
200
MODULE:
-MODULE:
-- mc9s08ll64.obj
mc9s08ll64.obj ---- PROCEDURES:
PROCEDURES:
-- VARIABLES:
VARIABLES:
_PTAD
00
11
11
00
.abs_section_0
_PTAD
.abs_section_0
_PTADD
11
11
11
00
.abs_section_1
_PTADD
.abs_section_1
_PTBD
22
11
11
00
.abs_section_2
_PTBD
.abs_section_2
_PTBDD
33
11
11
00
.abs_section_3
_PTBDD
.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
owners. Freescale Semiconductor, Inc. 2010.
TM
COPYDOWN SECTION
Lists each pre-initialized variable and its value
OBJECT-DEPENDENCIES SECTION
Lists for every function and variable that uses
other global objects the names of these global
objects
*********************************************************************************************
*********************************************************************************************
UNUSED-OBJECTS
UNUSED-OBJECTS SECTION
SECTION
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*********************************************************************************************
*********************************************************************************************
COPYDOWN
COPYDOWN SECTION
SECTION
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------22 --------- ROM-ADDRESS:
ROM-ADDRESS: 0x1945
0x1945 ------- SIZE
SIZE
--Filling
Filling bytes
bytes inserted
inserted
0000
0000
*********************************************************************************************
*********************************************************************************************
OBJECT-DEPENDENCIES
OBJECT-DEPENDENCIES SECTION
SECTION
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Init
USES
Init
USES _startupData
_startupData loadByte
loadByte
_Startup
USES
_Startup
USES __SEG_END_SSTACK
__SEG_END_SSTACK Init
Init main
main
main
USES
main
USES _PTCD
_PTCD _PTCDD
_PTCDD _SRS
_SRS nn
DEPENDENCY TREE
Using a tree format, shows all detected
dependencies between functions. Overlapping
local variables are also displayed at their
defining function
*********************************************************************************************
*********************************************************************************************
DEPENDENCY
DEPENDENCY TREE
TREE
*********************************************************************************************
*********************************************************************************************
main
main and
and _Startup
_Startup Group
Group
||
++- main
main
||
++- _Startup
_Startup
||
++- Init
Init
|| ||
|| ++- loadByte
loadByte
||
+(see
+- main
main
(see above)
above)
STATISTICS SECTION
Delivers information like the number of bytes of
code in the application
*********************************************************************************************
*********************************************************************************************
STATISTIC
STATISTIC SECTION
SECTION
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ExeFile:
ExeFile:
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
owners. Freescale Semiconductor, Inc. 2010.
TM
If
Specifier
Description
Specifier
Description
ALL
NONE
COPYDOWN
DEPENDENCY_TREE
FILE
SEC_ALLOC
OBJ_ALLOC
STARTUP_STRUCT
SORTED_OBJECT_LIST
MODULE_STATISTIC
OBJ_UNUSED
STATISTIC
OBJ_DEP
TARGET
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.
TM
Lab1
byte
byte
byte
byte
n;
n;
var;
var;
void
void main(void)
main(void)
{{
EnableInterrupts;
EnableInterrupts;
//
// initialize
initialize LEDs
LEDs
PTCD
PTCD == 0xFF;
0xFF;
PTCDD
PTCDD == 0b00111100;
0b00111100;
//
//
//
//
for
for (;;)
(;;)
{{
__RESET_WATCHDOG();
__RESET_WATCHDOG();
/*
/* feeds
feeds the
the dog
dog */
*/
for
for (n=0;;n++)
(n=0;;n++)
{{
PTCD++;
PTCD++;
var++;
var++;
}}
}} /*
/* loop
loop
/*
/* please
please
set
set
set
set
default
default value
value
LED
LED port
port pins
pins
for
for Port
Port CC
as
outputs
as outputs
//
// blink
blink LEDs
LEDs
forever
forever */
*/
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
owners. Freescale Semiconductor, Inc. 2010.
TM
Lab1
#pragma
#pragma DATA_SEG
DATA_SEG MY_ZEROPAGE
MY_ZEROPAGE
byte
near
byte near n;
n;
#pragma
#pragma DATA_SEG
DATA_SEG DEFAULT_RAM
DEFAULT_RAM
byte
far
byte far var
var == 7;
7;
void
void main(void)
main(void)
{{
EnableInterrupts;
EnableInterrupts;
//
// initialize
initialize LEDs
LEDs
PTCD
PTCD == 0xFF;
0xFF;
PTCDD
PTCDD == 0b00111100;
0b00111100;
//
//
//
//
for
for (;;)
(;;)
{{
__RESET_WATCHDOG();
__RESET_WATCHDOG();
/*
/* feeds
feeds the
the dog
dog */
*/
for
for (n=0;;n++)
(n=0;;n++)
{{
PTCD++;
PTCD++;
var++;
var++;
}}
}} /*
/* loop
loop
/*
/* please
please
set
set
set
set
default
default value
value
LED
LED port
port pins
pins
for
for Port
Port CC
as
outputs
as outputs
//
// blink
blink LEDs
LEDs
forever
forever */
*/
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
owners. Freescale Semiconductor, Inc. 2010.
TM
2.
3.
4.
5.
Call main()
clra
ldhx
stx
txs
ClearRAM:
psha
tst
bne
}
#MAP_RAM_last
MAP_RAM_first
MAP_RAM_first
ClearRAM
INIT_SP_FROM_STARTUP_DESC();
__asm jmp
main;
;
;
;
;
// initialize SP
// 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
owners. Freescale Semiconductor, Inc. 2010.
TM
DHJK 03-Feb-05
TM
Variables
The
Default
format
max
Formats
available
with option -T
char (unsigned)
8 bit
signed char
8 bit
-128
unsigned char
8 bit
signed short
16 bit
-32768
unsigned short
16 bit
enum (signed)
16 bit
-32768
signed int
16 bit
-32768
unsigned int
16 bit
signed long
32 bit
-2147483648
unsigned long
32 bit
32 bit
-2147483648
32 bit
Note:
All scalar types are signed by default,
except char
Size of int type is machine dependant
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.
TM
ANSI 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
owners. Freescale Semiconductor, Inc. 2010.
TM
For example, the natural data size for an 8-bit MCU is an 8-bit variable
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
owners. Freescale Semiconductor, Inc. 2010.
TM
the architecture
Consider:
8-bit S08
A++;
16-bit S12X
32-bit ColdFire
char A;
near A;
ldhx
inc
inc
@A
A
,x
inc
move.b
addq.l
move.b
A(a5),d0
#1,d0
d0,A(a5)
unsigned int A;
ldhx
inc
bne
inc
@A
1,x
Lxx
,x
incw
addq.l
#1,_A(a5)
ldhx
jsr
@A
_LINC
ldd
ldx
jsr
std
stx
A:2
A
_LINC
A:2
A
addq.l
#1,_A(a5)
Lxx:
unsigned long A;
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.
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.
TM
/* Types definition */
typedef unsigned char byte;
typedef unsigned int word;
typedef unsigned long dword;
typedef unsigned long dlong[2];
#ifndef __PE_Types_H
#define __PE_Types_H
typedef
typedef
typedef
typedef
typedef
typedef
typedef
/* Types definition */
typedef unsigned char bool;
typedef unsigned char byte;
typedef unsigned int word;
typedef unsigned long dword;
typedef unsigned long dlong[2];
unsigned char
unsigned char
signed char
unsigned short
signed short
unsigned long
signed long
__uint8__;
__byte__;
__int8__;
__uint16__;
__int16__;
__uint32__;
__int32__;
// 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
owners. Freescale Semiconductor, Inc. 2010.
TM
Memory Alignment
8
Var1
8 bits
Var2
16 bits
Var2
16 bits
Var3
32 bits
Var3
32 bits
Var3
32 bits
Var3
32 bits
Var4
16 bits
Var4
16 bits
typedef struct
{
byte Var1;
word Var2;
dword Var3;
word Var4;
} tData;
typedef struct
{
dword Var3;
word Var2;
word Var4;
byte Var1;
} tData;
32
Var1
8 bits
Var2
16 bits
Var3
32 bits
Var3
32 bits
Var3
32 bits
Var3
32 bits
32
Var3
32 bits
Var4
16 bits
Var4
16 bits
Var3
32 bits
Var3
32 bits
Var2
16 bits
Var4
16 bits
Var1
8 bits
Memory alignment can be simplified by declaring first the 32-bit variables, then 16-bit, then 8-bit.
Porting this to a 32-bit architecture ensures that there is no misaligned access to variables,
thereby saving processor time.
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
owners. Freescale Semiconductor, Inc. 2010.
Var2
16 bits
Var2
16 bits
Var4
16 bits
Var4
16 bits
Var1
8 bits
TM
Variable Types
Global
Static
Local
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.
TM
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
owners. Freescale Semiconductor, Inc. 2010.
TM
42
Static Variables
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
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
owners. Freescale Semiconductor, Inc. 2010.
TM
43
FILE1.c
FILE2.c
#include <FILE2.h>
//includes functions in file FILE2.c
//included in FILE2.c
MyFunction();
//included in FILE2.c
Before entering
MyFunction()
the 2nd time,
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
owners. Freescale Semiconductor, Inc. 2010.
TM
44
Static Functions
Functions
Features:
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
owners. Freescale Semiconductor, Inc. 2010.
TM
45
// Local
stuff.c
prototypes =================
//
static byte InternalFunction1 (byte);
static byte InternalFunction2 (byte);
static byte InternalFunction1 (byte)
// External functions ===============
{
//do_something;
byte ExternalFunction (byte)
}
{
InternalFunction1(param1);
InternalFunction2(param2);
static
byte InternalFunction2 (byte)
{ return_something;
} do_something_else;
}
// Local functions ==================
//
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
owners. Freescale Semiconductor, Inc. 2010.
TM
46
Volatile Variables
A volatile variable is one whose value may be change outside the normal
program flow
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.
TM
47
PTAD_PTAD0_MASK
PTAD_PTAD1_MASK
PTAD_PTAD2_MASK
PTAD_PTAD3_MASK
PTAD_PTAD4_MASK
PTAD_PTAD5_MASK
PTAD_PTAD6_MASK
PTAD_PTAD7_MASK
/*
/*
/*
/*
/*
/*
/*
/*
Port
Port
Port
Port
Port
Port
Port
Port
A
A
A
A
A
A
A
A
Data
Data
Data
Data
Data
Data
Data
Data
Register
Register
Register
Register
Register
Register
Register
Register
Bit
Bit
Bit
Bit
Bit
Bit
Bit
Bit
0
1
2
3
4
5
6
7
*/
*/
*/
*/
*/
*/
*/
*/
0x01
0x02
0x04
0x08
0x10
0x20
0x40
0x80
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.
TM
(void)
0x05;
0x05;
mov
lda
sta
// PORTA = 00000101
// PORTA = 00000101
#5,PORTA
#10
@value
10;
(void)
0x05;
0x05;
mov
mov
lda
lda
sta
// PORTA = 00000101
// PORTA = 00000101
10;
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.
#5,PORTA
#5,PORTA
SCI1S1
#10
@value
TM
49
Const Variables
It
is safe to assume that a parameter along with the keyword const means
a read-only parameter.
Some
Compilers
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.
TM
50
Keyword Const
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.
TM
51
Lab2
#pragma
#pragma DATA_SEG
DATA_SEG MY_ZEROPAGE
MY_ZEROPAGE
byte
near
byte near n;
n;
#pragma
#pragma DATA_SEG
DATA_SEG DEFAULT_RAM
DEFAULT_RAM
byte
far
byte far var
var == 7;
7;
void
void main(void)
main(void)
{{
EnableInterrupts;
EnableInterrupts;
//
// initialize
initialize LEDs
LEDs
PTCD
PTCD == 0xFF;
0xFF;
PTCDD
PTCDD == 0b00111100;
0b00111100;
//
//
//
//
for
for (;;)
(;;)
{{
__RESET_WATCHDOG();
__RESET_WATCHDOG();
/*
/* feeds
feeds the
the dog
dog */
*/
for
for (n=0;;n++)
(n=0;;n++)
{{
PTCD++;
PTCD++;
var++;
var++;
}}
}} /*
/* loop
loop
/*
/* please
please
set
set
set
set
default
default value
value
LED
LED port
port pins
pins
for
for Port
Port CC
as
outputs
as outputs
//
// blink
blink LEDs
LEDs
forever
forever */
*/
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
owners. Freescale Semiconductor, Inc. 2010.
TM
Lab2
#pragma
#pragma DATA_SEG
DATA_SEG DEFAULT_RAM
DEFAULT_RAM
byte
far
byte far var;
var;
void
void main(void)
main(void)
{{
#pragma
#pragma DATA_SEG
DATA_SEG
static
static byte
byte near
near
MY_ZEROPAGE
MY_ZEROPAGE
n;
n;
EnableInterrupts;
EnableInterrupts;
//
// initialize
initialize LEDs
LEDs
PTCD
PTCD == 0xFF;
0xFF;
PTCDD
PTCDD == 0b00111100;
0b00111100;
//
//
//
//
for
for (;;)
(;;)
{{
__RESET_WATCHDOG();
__RESET_WATCHDOG();
/*
/* feeds
feeds the
the dog
dog */
*/
for
for (n=0;;n++)
(n=0;;n++)
{{
PTCD++;
PTCD++;
var++;
var++;
}}
}} /*
/* loop
loop
/*
/* please
please
set
set
set
set
default
default value
value
LED
LED port
port pins
pins
for
for Port
Port CC
as
outputs
as outputs
//
// blink
blink LEDs
LEDs
forever
forever */
*/
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
owners. Freescale Semiconductor, Inc. 2010.
TM
_EntryPoint()
Hardware Initialization
_Startup()
Software Initialization
main()
Reset_Watchdog
Software Events
Hardware Events
Subroutine_1()
Interrupt_Service_Routine_1()
Subroutine_2()
Subroutine_3()
Interrupt_Service_Routine_2()
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
owners. Freescale Semiconductor, Inc. 2010.
TM
DHJK 03-Feb-05
TM
Application
Files developed completely
by the user
Application
Files
main.c
Translation
Project
Globals
Project
Interrupts
Project1_mcu.h
Project1_mcu.c
Foundation
Files that are consistent
with MCU documentation
and typically need
no modification by the user
Peripheral
Linker
Definitions Parameters
mcu.h
target.prm
ANSI C
Libraries
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
owners. Freescale Semiconductor, Inc. 2010.
TM
DHJK 03-Feb-05
Application
Files developed completely
by the user
Project 2
Application
Files
Application
Files
Copy
main.c
main.c
Translation
Project
Globals
Project
Interrupts
Project
Globals
Change
Project1_mcu.h
Project1_mcu.c
Project
Interrupts
Project2_mcu.h
Project2_mcu.c
Foundation
Files that are consistent
with MCU documentation
and typically need
no modification by the user
Peripheral
Linker
Definitions Parameters
mcu.h
target.prm
ANSI C
Libraries
Replace
ansixx.lib
Start08.c
Peripheral
Linker
Definitions Parameters
mcu.h
target.prm
ANSI C
Libraries
ansixx.lib
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
owners. Freescale Semiconductor, Inc. 2010.
TM
DHJK 03-Feb-05
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
owners. Freescale Semiconductor, Inc. 2010.
TM
Example system.h
system.h
/***********************************************************************************************\
/***********************************************************************************************\
** Project
Project Name
Name
\***********************************************************************************************/
\***********************************************************************************************/
#ifndef
#ifndef _SYSTEM_H_
_SYSTEM_H_
#define
#define _SYSTEM_H_
_SYSTEM_H_
#include
#include <hidef.h>
<hidef.h>
#include
#include "derivative.h"
"derivative.h"
/*
/* for
for EnableInterrupts
EnableInterrupts macro
macro */
*/
/*
/* 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
:1;
byte _0
_0
:1;
byte
:1;
byte _1
_1
:1;
byte
_2
:1;
byte _2
:1;
byte
_3
:1;
byte _3
:1;
byte
_4
:1;
byte _4
:1;
byte
_5
:1;
byte _5
:1;
byte
_6
:1;
byte _6
:1;
byte
_7
:1;
byte _7
: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
owners. Freescale Semiconductor, Inc. 2010.
TM
Example system.h
system.h
/***********************************************************************************************\
/***********************************************************************************************\
** Project
Project includes
includes
\***********************************************************************************************/
\***********************************************************************************************/
#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
System
** 0x1802 SOPT1
System Options
Options Register
Register 11
**
System
** 0x1803
0x1803 SOPT2
SOPT2
System Options
Options Register
Register 22
**
System
** 0x1808
0x1808 SPMSC1
SPMSC1
System Power
Power Management
Management Status
Status and
and Control
Control 11 Register
Register
**
System
** 0x1809
0x1809 SPMSC2
SPMSC2
System Power
Power Management
Management Status
Status and
and Control
Control 22 Register
Register
**
0x180B
SPMSC3
System
Power
Management
Status
and
Control
3
Register
** 0x180B SPMSC3
System Power Management Status and Control 3 Register
**
0x180E
SCGC1
System
Clock
Gating
Control
1
Register
** 0x180E SCGC1
System Clock Gating Control 1 Register
**
System
** 0x180F
0x180F SCGC2
SCGC2
System Clock
Clock Gating
Gating Control
Control 22 Register
Register
**
0x000F
IRQSC
Interrupt
** 0x000F IRQSC
Interrupt Pin
Pin Request
Request Status
Status and
and Control
Control Register
Register
*/
*/
#define
#define init_SOPT1
init_SOPT1
/*
/*
**
**
**
**
**
**
**
**
**
**
**
**
*/
*/
0b01000010
0b01000010
1100001U
1100001U ==
|||xxx||
|||xxx||
|||
|+-|||
|+-|||
+--|||
+--||+------||+------|+-------|+-------+--------+---------
#define
#define init_SOPT2
init_SOPT2
/*
/*
**
**
**
**
**
**
**
**
**
**
**
**
**
**
*/
*/
0b00000010
0b00000010
00000000
00000000 ==
|x||x|||
|x||x|||
|| ||
|| ||+-||+-|| ||
|| |+--|+--|| ||
|| +---+---|| |+-----|+-----|| +------+------+--------+---------
reset
reset
RSTPE
RSTPE
BKGDPE
BKGDPE
STOPE
STOPE
COPT
COPT
COPE
COPE
=0
=0
=1
=1
=0
=0
=1
=1
=0
=0
::
::
::
::
::
RESET
RESET pin
pin function
function disabled
disabled
Background
Background Debug
Debug pin
pin enabled
enabled
Stop
Stop Mode
Mode disabled
disabled
Long
Long COP
COP timeout
timeout period
period selected
selected
COP
COP Watchdog
Watchdog timer
timer disabled
disabled
=0
=0
=1
=1
=0
=0
=0
=0
=0
=0
=0
=0
::
::
::
::
::
::
ACMP1
ACMP1 output
output not
not
SDA
SDA on
on PTB6;
PTB6; SCL
SCL
ACMP2
ACMP2 output
output not
not
TPM1CH2
TPM1CH2 on
on PTA6
PTA6
TPM2CH2
TPM2CH2 on
on PTA7
PTA7
COP
COP clock
clock source
source
reset
reset
ACIC1
ACIC1
IICPS
IICPS
ACIC2
ACIC2
TPM1CH2PS
TPM1CH2PS
TPM2CH2PS
TPM2CH2PS
COPCLKS
COPCLKS
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.
connected
connected to
to TPM1CH0
TPM1CH0 input
input
on
on PTB7
PTB7
connected
connected to
to TPM2CH0
TPM2CH0 input
input
is
is internal
internal 1kHz
1kHz reference
reference
TM
Example system.h
system.h
/***********************************************************************************************
/***********************************************************************************************
**
**
**
** Port
Port I/O
I/O
**
**
**
Port
** 0x0000
0x0000 PTAD
PTAD
Port AA Data
Data Register
Register
**
Port
** 0x0001
0x0001 PTADD
PTADD
Port AA Data
Data Direction
Direction Register
Register
**
Port
** 0x0002
0x0002 PTBD
PTBD
Port BB Data
Data Register
Register
**
Port
** 0x0003
0x0003 PTBDD
PTBDD
Port BB Data
Data Direction
Direction Register
Register
**
Port
** 0x0004
0x0004 PTCD
PTCD
Port CC Data
Data Register
Register
**
Port
** 0x0005
0x0005 PTCDD
PTCDD
Port CC Data
Data Direction
Direction Register
Register
**
Port
** 0x0006
0x0006 PTDD
PTDD
Port DD Data
Data Register
Register
**
Port
** 0x0007
0x0007 PTDDD
PTDDD
Port DD Data
Data Direction
Direction Register
Register
**
Port
** 0x1840
0x1840 PTAPE
PTAPE
Port AA Pull
Pull Enable
Enable Register
Register
**
Port
** 0x1841
0x1841 PTASE
PTASE
Port AA Slew
Slew Rate
Rate Enable
Enable Register
Register
**
Port
** 0x1842
0x1842 PTADS
PTADS
Port AA Drive
Drive Strength
Strength Selection
Selection Register
Register
**
Port
** 0x1844
0x1844 PTBPE
PTBPE
Port BB Pull
Pull Enable
Enable Register
Register
**
0x1845
PTBSE
Port
B
Slew
Rate
Enable
Register
** 0x1845 PTBSE
Port B Slew Rate Enable Register
**
Port
** 0x1846
0x1846 PTBDS
PTBDS
Port BB Drive
Drive Strength
Strength Selection
Selection Register
Register
**
0x1848
PTCPE
Port
** 0x1848 PTCPE
Port CC Pull
Pull Enable
Enable Register
Register
**
0x1849
PTCSE
Port
C
Slew
Rate
Enable
Register
** 0x1849 PTCSE
Port C Slew Rate Enable Register
**
Port
** 0x184A
0x184A PTCDS
PTCDS
Port CC Drive
Drive Strength
Strength Selection
Selection Register
Register
*/
*/
#define
#define
//
//
#define
#define
//
//
#define
#define
//
//
#define
#define
//
//
#define
#define
//
//
/*
/*
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
*/
*/
#define
#define
#define
#define
#define
#define
init_PTAD
init_PTAD
init_PTADD
init_PTADD
init_PTAPE
init_PTAPE
init_PTASE
init_PTASE
init_PTADS
init_PTADS
0b10000000
0b10000000
00000000
00000000 ==
0b10000000
0b10000000
00000000
00000000 ==
0b00000110
0b00000110
00000000
00000000 ==
0b00000000
0b00000000
00000000
00000000 ==
0b00000000
0b00000000
00000000
00000000 ==
||||||||
||||||||
|||||||+-|||||||+-||||||+--||||||+--|||||+---|||||+---||||+----||||+----|||+-----|||+-----||+------||+------|+-------|+-------+--------+---------
INT1_IS_ACTIVE
INT1_IS_ACTIVE
LED_BlueOn
LED_BlueOn
LED_BlueOff
LED_BlueOff
reset
reset
reset
reset
reset
reset
reset
reset
reset
reset
X_OUT
X_OUT
Y_OUT
Y_OUT
Z_OUT
Z_OUT
DIS_MCU
DIS_MCU
BKGD
BKGD
RESET
RESET
EXTRA_AD
EXTRA_AD
LEDB_BB
LEDB_BB
-- MMA845x
MMA845x INT1
INT1
-- MMA845x
MMA845x INT2
INT2
-- Blue
Blue LED
LED cathode
cathode
(PTAD_PTAD1
(PTAD_PTAD1 ==
== 0)
0)
(PTAD_PTAD7
(PTAD_PTAD7 == 0)
0)
(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
owners. Freescale Semiconductor, Inc. 2010.
TM
Example system.h
system.h
#define
#define
//
//
#define
#define
//
//
#define
#define
//
//
#define
#define
//
//
#define
#define
//
//
/*
/*
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
*/
*/
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
init_PTCD
init_PTCD
init_PTCDD
init_PTCDD
init_PTCPE
init_PTCPE
init_PTCSE
init_PTCSE
init_PTCDS
init_PTCDS
0b10111011
0b10111011
00000000
00000000 ==
0b10110111
0b10110111
00000000
00000000 ==
0b01000000
0b01000000
00000000
00000000 ==
0b00000000
0b00000000
00000000
00000000 ==
0b00000000
0b00000000
00000000
00000000 ==
||||||||
||||||||
|||||||+-|||||||+-||||||+--||||||+--|||||+---|||||+---||||+----||||+----|||+-----|||+-----||+------||+------|+-------|+-------+--------+---------
LED_GreenOn
LED_GreenOn
LED_GreenOff
LED_GreenOff
SENSOR_SHUTDOWN
SENSOR_SHUTDOWN
SENSOR_ACTIVE
SENSOR_ACTIVE
LED_RedOn
LED_RedOn
LED_RedOff
LED_RedOff
LED_YellowOn
LED_YellowOn
LED_YellowOff
LED_YellowOff
SA0_PIN
SA0_PIN
reset
reset
reset
reset
reset
reset
reset
reset
reset
reset
LEDG_BB
LEDG_BB
SLEEP
SLEEP
G_SEL_2
G_SEL_2
G_SEL_1
G_SEL_1
LEDR_BB
LEDR_BB
BUZZER_BB
BUZZER_BB
PUSH_BUTTON
PUSH_BUTTON
LED_OB
LED_OB
----
Green
Green LED
LED cathode
cathode
MMA845x
MMA845x CS
CS
MMA845x
MMA845x SA0
SA0
-- Red
Red LED
LED cathode
cathode
-- Yellow
Yellow LED
LED cathode
cathode
(PTCD_PTCD0
(PTCD_PTCD0 ==
(PTCD_PTCD0
(PTCD_PTCD0 ==
(PTCD_PTCD1
(PTCD_PTCD1 ==
(PTCD_PTCD1
(PTCD_PTCD1 ==
(PTCD_PTCD4
(PTCD_PTCD4 ==
(PTCD_PTCD4
(PTCD_PTCD4 ==
(PTCD_PTCD7
(PTCD_PTCD7 ==
(PTCD_PTCD7
(PTCD_PTCD7 ==
(PTCD_PTCD2)
(PTCD_PTCD2)
0)
0)
1)
1)
0)
0)
1)
1)
0)
0)
1)
1)
0)
0)
1)
1)
/***********************************************************************************************\
/***********************************************************************************************\
** 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
owners. Freescale Semiconductor, Inc. 2010.
TM
Example sci.h
sci.h
/***********************************************************************************************\
/***********************************************************************************************\
** Project
Project name
name
**
** 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
#define
#define BUFFER_TX_SIZE
BUFFER_TX_SIZE
10
10
200
200
/***********************************************************************************************
/***********************************************************************************************
**
**
**
** Serial
Serial Communications
Communications Interface
Interface (SCI)
(SCI)
**
**
**
0x0020
SCIBDH
SCI
Baud
Rate
Register
** 0x0020 SCIBDH
SCI Baud Rate Register High
High
**
SCI
** 0x0021
0x0021 SCIBDL
SCIBDL
SCI Baud
Baud Rate
Rate Register
Register Low
Low
**
0x0022
SCIC1
SCI
Control
Register
1
** 0x0022 SCIC1
SCI Control Register 1
**
0x0023
SCIC2
SCI
Control
Register
2
** 0x0023 SCIC2
SCI Control Register 2
**
SCI
** 0x0024
0x0024 SCIS1
SCIS1
SCI Status
Status Register
Register 11
**
0x0025
SCIS2
SCI
** 0x0025 SCIS2
SCI Status
Status Register
Register 22
**
SCI
** 0x0026
0x0026 SCIC3
SCIC3
SCI Control
Control Register
Register 33
**
SCI
** 0x0027
0x0027 SCID
SCID
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
#define
#define init_SCIBDL
init_SCIBDL
0x00
0x00
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
owners. Freescale Semiconductor, Inc. 2010.
TM
Example sci.h
sci.h
#define
#define
#define
#define
#define
#define
#define
#define
ASCII_BS
ASCII_BS
ASCII_LF
ASCII_LF
ASCII_CR
ASCII_CR
ASCII_DEL
ASCII_DEL
0x08
0x08
0x0A
0x0A
0x0D
0x0D
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
void
void
void
void
void
void
void
void
void
void
byte
byte
byte
byte
void
void
void
void
void
void
void
void
SCIControlInit
SCIControlInit (void);
(void);
SCISendString
SCISendString (byte
(byte *pStr);
*pStr);
SCI_CharOut(byte
SCI_CharOut(byte data);
data);
SCI_NibbOut(byte
data);
SCI_NibbOut(byte data);
SCI_ByteOut(byte
SCI_ByteOut(byte data);
data);
SCI_putCRLF
SCI_putCRLF (void);
(void);
SCI_CharIn(void);
SCI_CharIn(void);
SCI_ByteIn(void);
SCI_ByteIn(void);
SCI_s12dec_Out
SCI_s12dec_Out (tword
(tword data);
data);
SCI_s8dec_Out(tword
SCI_s8dec_Out(tword data);
data);
SCI_s12int_Out
SCI_s12int_Out (tword
(tword data);
data);
SCI_s12frac_Out
SCI_s12frac_Out (tword
(tword data);
data);
byte
byte
byte
byte
byte
byte
void
void
isnum
isnum (byte
(byte data);
data);
ishex
ishex (byte
(byte data);
data);
tohex
tohex (byte
(byte data);
data);
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
owners. Freescale Semiconductor, Inc. 2010.
TM
Example terminal.h
terminal.h
/***********************************************************************************************\
/***********************************************************************************************\
** Project
Project Name
Name
**
** 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
void
void
void
void
TerminalInit
TerminalInit (void);
(void);
ProcessTerminal
ProcessTerminal (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
owners. Freescale Semiconductor, Inc. 2010.
TM
Example terminal.c
terminal.c
/***********************************************************************************************\
/***********************************************************************************************\
** Project
Project Name
Name
**
** 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
owners. Freescale Semiconductor, Inc. 2010.
TM
TM