Vous êtes sur la page 1sur 173

Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
RTAC Americas
C for Embedded Systems
Slide 2
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
RTAC Americas Team
This presentation has been made
by Freescale Applications Team
Guadalajara, Mxico
Slide 3
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
C for Embedded Systems - About the course
By the end of this course youll be able to:
Use C language to create embedded applications.
Manage resources to suit different target processors requirements.
Create portable, robust and concise C programs.
Identify time-critical pieces of code.
Optimize C code (smaller, faster code).
Apply theory into practice by interacting with actual hardware (labs).
Have the tools to be able to answer FAQ about Embedded C and
CodeWarrior.
Slide 4
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Agenda
Intro
What is C?
Why C?
From assembly to C
Benefits of C
C Basics
Coding structure
C Keywords
C Operands
Codewarrior
C Compiler
Memory placement: PRM file
Startup Routine
Compiler Optimizations
Conditional Compilation
Hands-on Applications
UART/EEPROM Example API
C for Embedded
Variables
Resource mapping
Bit Fields
Arrays
Pointers to functions
Stack Pointer
Interrupts
Iteration, jumps and loops
Standard C library
Slide 5
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
C Programming Language
What is C?
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.
Slide 6
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Why C?
C is much more flexible and free-wheeling. This freedom gives C much
more power that experienced users can employ.
C is a relatively small language, but one which wears well.
C is sometimes referred to as a ``high-level assembly language
Low level (BitWise) programming readily available.
Loose typing (unlike other high-level languages)
Structured language
C will allowyou to create any task you may have in mind.
C retains the basic philosophy that programmers know what they are
doing; it only requires that they state their intentions explicitly.
Slide 7
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
These are some of the common issues 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, not so direct in C
Data declaration in RAM and ROM
Compiler optimizations
Difficult to write Interrupt Service Routines
Why not C? Cultural issues
Slide 8
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
ANSI C for 8-Bits MCUs
Pure ANSI C is not always convenient for embedded systems because:
Embedded systems interact with hardware. ANSI C provides extremely
crude tools for addressing registers at fixed memory locations.
Almost all embedded systems use interrupts
ANSI C has various type promotion rules that are
absolute performance killers to an eight-bit machine.
Some microcontroller architectures dont have hardware
support for a C stack.
Many microcontrollers have multiple memory spaces
One should use
standard C as much
as possible...
However, when it
interferes with
solving the problem
at hand, do not
hesitate to bypass
it.
- Nigel Jones
Slide 9
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
When using C in a Low-End 8-bit Microcontroller we have to find
ways to keep code small. That means breaking a few
programming rules.
Breaking some C Paradigms
Enabling/Disabling Global Interrupts.
The use of GOTO GOTO statement.
Global Labels
Global Scratch Registers
Pointer support
Slide 10
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Main characteristics of an Embedded programming environment:
Limited RAM
Limited ROM
Limited stack space
Hardware oriented programming
Critical Timing (ISR, tasks, ...)
Many different pointer kinds (far/near/rom/uni/paged/...)
Special keywords/tokens (@, interrupt, tiny, ...)
Embedded vs. Desktop Programming
Slide 11
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Assembly vs. C
A compiler is no more efficient than a good assembly
programmer.
It is much easier to write good code in C which can be
converted into efficient assembly code than it is to write
efficient assembly code by hand.
C is a mean to an end and not an end itself.
Slide 12
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
1) C allows us to be more productive
2) Code written in C can be more reliable
3) C code can be far more scalable
4) More portable between different platforms
5) Code is easier to maintain
6) Documentation, books, third party libraries & programs are
available
There are many reasons to change assembly for C:
Why change to C?
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
C Basics
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Slide 14
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
C Coding Structure
A C program basically has the following form:
Preprocessor Commands
Type definitions
Function prototypes (declare function types and variables passed to function).
Variables
Functions
We must have
a main()
function
Every command line
must end with a
semicolon (;)
Slide 15
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
C Functions
A function has the form:
type function_name (parameters)
{
local variables
C Statements
}
Slide 16
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
C Keywords
Identifiers
struct
union
void
enum
Selection
if
else
switch
case
default
Storage Specifiers
register
typedef
auto
extern
Iteration
do
while
for
Jump
goto
continue
break
return
Function
Specifier
inline
Data type
char
short
signed
unsigned
int
float
long
double
Modifiers
const
static
volatile
restrict
Preprocessing
Directives
#include
#define
#undef
#line
#error
#pragma
Conditional
Compilation
#if
#ifdef
#ifndef
#elif
#else
#endif
Slide 17
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
C Operators
Assignment
= plain assignment
+= add
-= subtract
*= multiply
/= divide
%= remainder
<<= bit left shift
>>= bit right shift
&= bit AND
^= bit exclusive OR
|= bit inclusive OR
Bitwise
& bitwise AND
^ bitwise exclusive OR
| bitwise inclusive OR
< < bitwise left shift
>> bitwise right shift
Primary expressions and
postfix operators
( ) subexpression and function call
[ ] array subscript
-> structure pointer
. structure member
++ increment(postfix)
-- decrement(postfix)
Unary
! logical negation
~ one's complement
++ increment(prefix)
-- decrement(prefix)
- unary negation
+ unary plus
(type) type cast
* pointer indirection
& address of
sizeof size of
Math
* multiplication
/ division
% modulus(remainder)
+ addition
- subtraction
Relational
< less than left to right relational
<= less than or equal
> greater than
>= greater than or equal
== equal test
!= not equal test
Logical
&& logical AND
|| logical inclusive OR
Conditional
?: conditional test
Sequence
, comma
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Embedded Programming
C for Embedded Systems
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Variables
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Slide 20
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
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.
We'll learn about C's basic data types, how to write
constants and declare variables of these types.
Slide 21
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Selecting a type
There are several implications to remember:
The ``set of values'' is finite. C's int type can not represent
all of the integers; its float type can not represent all floating-
point numbers.
When declaring a new variable and picking a type for it, you
have to keep in mind the values and operations you'll be
needing.
Slide 22
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
There are only a few basic data types in C:
All scalar types
(except char) are
signed by default
Example:
int = signed int
0 255
Basic data types in C
NOTE: Size of INT
type is machine
dependant
Slide 23
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
CodeWarrior Data Types
The ANSI standard does not precisely define the size of its native types,
but CodeWarrior does...
Slide 24
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Data Type Facts
The greatest savings in code size and execution time can be made by
choosing the most appropriate data type for variables.
The natural internal data size for 8-bit MCU is 8-bits (one byte), whereas
the C preferred data type is int.
8-bit machines can process 8-bit data types more efficiently than 16-bit
types.
intand larger data types should only be used where required by the size
of data to be represented.
Double precision and floating point operations are particularly inefficient
and should be avoided wherever efficiency is important.
Slide 25
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Data Type Selection
There are 3 Rules for Data Type Selection on 8-bit MCUs:
Use the smallest possible type to get the job done.
Use unsigned type if posibble.
Use casts within expressions to reduce data types to the minimumrequired.
Use typedefs to get fixed size
Change according to compiler & system
Code is invariant across machines
Used when fixed #bits need for values
Slide 26
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Defines a
complete set of
data types
Three different type
variables are declared
inside main function
Open file:
Lab1-Variables.mcp
Slide 27
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Just the less significant bit is written;
A register is used for that
The remaining bits of each variable
are cleared using clr ,X
Variables have an address in stack
area
Slide 28
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Three different type
variables are declared
outside main function
Slide 29
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
The compiler just reserves
memory for variables being
used. In this example VarA is
the only used variable
Slide 30
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
All the global variables
declared are used.
Slide 31
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
In this case the
compiler reserves
memory for all variables.
Slide 32
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Memory area where variables are declared. Each
variables has different size (1, 2 and 4 bytes)
Each add operation is done
in different way, based on
the size of the variable
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Variables
Embedded Programming
Storage Class Modifiers
Slide 34
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
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.
These three key words, together, allowus to write not only better
code, but also tighter code.
static
volatile
const
Slide 35
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
The variable doesnt disappear between successive
invocations of a function.
When declared at the module level, it is accessible by all
functions in all the module, but by no one else.
Note:
These variables
wont be stored
in the Stack.
When applied to variables, static has two primary fuctions:
Static Variables
Slide 36
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Static variable Example
FILE1.c
#i ncl ude <FI LE2. h> i ncl udes f unct i ons
cont ai ned i n f i l e
FI LE2. c
voi d mai n ( voi d) {
MyFunct i on( ) ; i ncl uded i n FI LE2. c
MyFunct i on( ) ; i ncl uded i n FI LE2. c
}
FILE2.c
voi d MyFunct i on ( voi d) { Def i ni t i on of
MyFunct i on
i n FI LE2. C
st at i c char myVar = 0; l ocal var i abl e
decl ar ed static
myVar = myVar + 1;
}
Before entering to
MyFunction the first
time, myVar = 0
Before entering to
MyFunction for the
second time:
myVar = 1
The static Variable
keeps its value even
though myVar is local
Slide 37
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Static Functions
Features:
Only callable by other functions within its module.
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 may be called using a short version of the call or
jump instruction.
Slide 38
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Uses of the keyword static
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.
Functions declared static within a module may only be called by other
functions within that module.
For Embedded Systems:
Encapsulation of persistent data (wrapping)
Modular coding (data hiding)
Hiding of internal processing in each module
Slide 39
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Volatile Variables
In embedded systems, there are two ways this can happen:
Via an interrupt service routine
As a consequence of hardware action
A volatile variable is one whose value may be
changed outside the normal program flow.
It is very good practice
to declare volatile all
Peripheral Registers in
embedded devices.
Slide 40
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Access to variables defined as volatile are
never optimized by the compiler !!!
vol at i l e unsi gned char PORTA @0x00;
vol at i l e unsi gned char SCS1 @0x16;
unsi gned char val ue;
voi d mai n( voi d) {
PORTA = 0x05; / * PORTA = 00000101 */
PORTA = 0x05; / * PORTA = 00000101 */
SCS1;
val ue = 10;
}
MOV #5, PORTA
LDA #10
STA @val ue
Without Volatile keyword
MOV #5, PORTA
MOV #5, PORTA
LDA SCS1
LDX #10
STX @val ue
With Volatile keyword
volatile
volatile
Volatile variables are never Optimized
Slide 41
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
/ * MC68HC908GP20/ 32 Of f i ci al Per i pher al Regi st er Names */
vol at i l e unsi gned char PORTA @0x0000; / * Por t s and dat a di r ect i on */
vol at i l e unsi gned char PORTB @0x0001;
vol at i l e unsi gned char PORTC @0x0002;
vol at i l e unsi gned char PORTD @0x0003;
vol at i l e unsi gned char PORTE @0x0008;
vol at i l e unsi gned char DDRA @0x0004; / * Dat a Di r ect i on Regi st er s */
vol at i l e unsi gned char DDRB @0x0005;
vol at i l e unsi gned char DDRC @0x0006;
vol at i l e unsi gned char DDRD @0x0007;
vol at i l e unsi gned char DDRE @0x000C;
vol at i l e unsi gned char PTAPUE @0x000D; / * Por t pul l - up enabl es */
vol at i l e unsi gned char PTCPUE @0x000E;
vol at i l e unsi gned char PTDPUE @0x000F;
Volatile variable Example
Slide 42
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
The compiler keeps the program memory address of that
location. Since it is in ROM, its value cannot be changed.
An initialization value must be declared.
Example:
const double PI = 3.14159265;
The Declaration const is applied to any variable, and
it tells the compiler to store it in ROM code.
Const Variables
Slide 43
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Some compilers create a genuine variable in RAM to hold the
const variable. On RAM-limited systems, this can be a significant
penalty.
Some compilers, like CodeWarrior, store the variable in ROM.
However, the read onlyvariable is still treated as a variable and
accessed as such, typically using some form of indexed addressing
(16-bit). Compared to immediate addressing (8-bit), this method is
normally much slower.
Const constrains
Slide 44
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Keyword Const
It is safe to assume that a parameter along with the keyword
const means a readonlyparameter.
For Embedded Systems:
Parameters defined const are allocated in ROM space
The compiler protect 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;
Slide 45
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Const volatile variables
Can a variable be both, const and volatile?
Yes!
Yes!
This modifier formshould be use on any memory
location that can change unexpectedly (volatile) and
that is read-only (const).
The most obvious example of this is a hardware
status register:
/* SCI Status Register */
const volatile unsigned char SCS1 @0x0016
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Resource Mapping
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Slide 47
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Accessing fixed memory locations
Embedded systems are often characterized by requiring the programmer
to access a specific memory location.
Exercise: On a certain project it is required to set an integer variable at
the absolute address 0x2FFA to the value 0xAA55 (the compiler is a
pure ANSI compiler).
Write code to accomplish this task.
Int * ptr;
ptr = (int *)0x2FFA;
*ptr = 0xAA55;
Slide 48
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
How to access I/O Registers?
Many I/O and control registers are located in the direct page and
they should be declared as such, so the compiler can use the direct
addressing mode where possible.
One of the first questions that arises when programming embedded is:
How do I access I/O Registers?
The answer is as simple or complicated as you want...
Slide 49
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Defining I/O Registers
One common and very useful form is:
#define PortA ( * ( volatile unsigned char * ) 0x0000 )
This makes PortA a
variable of type char
at address 0x0000
This is a compiler-
specific syntax...
It is more readable,
but we pay the price
loosing portability.
An easier way to do this is:
volatile unsigned char PortA @0x0000;
Slide 50
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
The registers in the CPU are not memory mapped.
The instructions set contains a subset which allows modifing themall.
C doesnt provide a direct tool to accesing the CPU Registers.
The C compiler allows the use of assembly instructions within the C
code.
_asm AssemblyInstuction;
asm (AssemblyInstruction);
asm {
----
----
}
Accessing CPU Registers
Slide 51
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Modify the content of
the I bit of the CCR in
the CPU
Slide 52
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
The I bit is modified using
Assembly directives.
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Bit Fields
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Slide 54
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Bit fields
In embedded systems it is essential to be able to access
and modify one or several bits at a time, at a given address.
0 0 0 0 1 0 0 0
To accomplish this in C there are different ways to approach
and implement the solution
$0020
1
Slide 55
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Bit Fields
_bit PTA0;
int val = 16;
short foo = 0;
PTA0 = 1;
PTA0 = val; /* Set PTA0 to 1 */
PTA0 = foo; /* Set PTA0 to 0 */
TASKING C 8051
Cross-Compiler
Slide 56
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
An union is a variable that may
hold (at different times)
objects of different types and
sizes, with the compiler
keeping track of size and
alignment requirements.
Open file:
Lab2-BitFields.mcp
Slide 57
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Unions Unions provide a way to
manipulate different kinds of
data in a single area of storage,
without embedding any
machine-dependent information
in the program
Only PS bits are
modified
One-instruction
writing
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Arrays
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Slide 59
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Arrays
C allows developers to access contents in an Array in
several different ways.
Depending on the implementation, selecting the best to suit
the applications needs will result in faster and smaller code.
Slide 60
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Hard-coded:
Address resolved at compile time
Fast execution
Incremental Index:
Fast
More flexible than hard-coded
Pointer to array
Fast execution
Hard to read
May be used with loops
Array Access
Slide 61
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Hard-Coded
Incremental
Index
Pointer to Array
Open file:
Lab3-Arrays.mcp
Slide 62
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Each access type has
its own advantages,
and uses different
registers to
accomplish the
operations
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Pointers to Functions
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Slide 64
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Pointers to functions
Pointers to functions are useful for approximately
the same reasons as pointers to data, those are:
When you want an extra level of indirection.
When you'd like the same piece of code to call different
functions depending on circumstances.
Slide 65
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Defining a pointer to a function
The following code defines a pointer to a function that takes an
integer as an argument and returns an integer as well:
int (* function) (int);
The extra parentheses around (* function) are needed because
of precedence relationships in declarations.
Without them wed be declaring a function returning a pointer to
int.
Slide 66
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Example:
Pointer to function Initialization
Pointer to function
points now to a different
function.
Periodic interrupt which
calls the function the
pointer is pointing to.
Slide 67
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
HC08QL Example
SLIC Module has only one
Interrupt
User must read SLIC State
Vector register (SLCSV) to
verify interrupt source
Possible C Solutions:
Switch-case
Nested ifs
Pointers to functions
Slide 68
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Declare Array of
functions
Call a different function
every time
Open file:
Lab4-Pointers.mcp
Slide 69
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Debug (1):
1. BreakPoint on function call
2. Step into function (F11)
Slide 70
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Debug(2):
Will execute a different
function every time
Slide 71
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Debug(3)

Open
Open
Component
Component
-
-
>
>
VisualizationTool
VisualizationTool

Open
Open
Display.vtl
Display.vtl

Run
Run
VarA is modified in each
function
Slide 72
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
When to use pointers:
#funct 3 4 5 6 7
ROM: 264 ROM: 278 ROM: 292 ROM: 306 ROM: 320
RAM: 96 RAM: 96 RAM: 96 RAM: 96 RAM: 96
ROM: 307 ROM: 318 ROM: 329 ROM: 340 ROM: 351
RAM: 96 RAM: 96 RAM: 96 RAM: 96 RAM: 96
ROM: 270 ROM: 278 ROM: 286 ROM: 294 ROM: 302
RAM: 102 RAM: 104 RAM: 106 RAM: 108 RAM: 110
Pointers
Switch
IF
Nested ifs are easy to read and cheap in size when using a small amount of functions
Switch is the most readable, but expensive in size
Pointers generate less code when many functions are declared, but it must pay penalty
of RAM Size
Execution time is
always the same when
using pointers!!!
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Stack Pointer / Function Arguments
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Slide 74
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
The Stack Pointer Importance
A Stack Pointer Register supports very
key features of C:
Passing variables to subroutines
Allows the use of recursion
Is the base of Automatic variables
t ypedef st r uct {
unsi gned char I D;
unsi gned shor t Ti me;
} Obj ect Type;
voi d f oo ( unsi gned char val ue) {
vol at i l e Obj ect Type i nst ance;
i nst ance. I D = val ue;
}
f oo:
B00B A7FB AI S #- 3
B00D 9EE701 STA 1, SP
B010 A707 AI S #3
B012 81 RTS
Assembly:
Slide 75
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Stack Pointer addressing
Stack pointer instructions are similar to indexed instructions only
they use the contents of the stack pointer as an address of the
operand instead of the index register.
There are two modes of stack pointer addressing:
8-bit offset
16-bit offset
Stack pointer instructions require one extra byte and one extra
execution cycle compared to the equivalent indexed instruction.
Slide 76
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Stack Frames
Slide 77
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
HC08 Return Values
Slide 78
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Four different functions are
declared, each one of them returns
a different type variable
Each function has a different
type parameter (void, byte,
word)
Open file:
Lab5-Arguments.mcp
Slide 79
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
The Function is
called jumping to
the memory location
where its source
code is located
Assignment to global
varable
Function returns
with RTS.
Slide 80
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Parameter in A register
Operation made in stack and
return value stored in A
Result stored in variable.
A variable
Slide 81
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
A and X are used for
parameters as well as
retun registers
Slide 82
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
A used as register
for parameters and
it is used to address
directly the byte in
the array
H:X used to return the
pointer
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Interrupts
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Slide 84
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
The CodeWarrior compiler provides a non-ANSI compliant way to
specify directly the interrupt vector number in the source:
Vector
Number Vector Address
Vector
Address
Size
0 0xFFFE - 0xFFFF 2
1 0xFFFC 0xFFFD 2
2 0xFFFA 0xFFFB 2
... ... ...
n 0xFFFF ( n*2 ) 2
interrupt 17 voi d TBM_I SR ( voi d) {
/ * Ti mebase Modul e Handl er */
}
Interrupt Vector Table Allocation
How do I handle interrupts in C?
Slide 85
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
908 QT/QY Family Vector Table
Slide 86
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
An Interrupt Service
Routine is declared using
the interrupt modifier
Open file:
Lab6-Interrupts.mcp
Slide 87
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Uses RTI instead of RTS
The interrupt vector
stores the address
whe the ISR starts
Pointer to Function
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Iteration, jumps and loops
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Slide 89
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
While (1);
For (;;);
Loop:
goto Loop;
For Embedded Systems:
-Loops are always required for MCU based applications.
-For the application, loop (2) is better
-Why?
For (;;); is better because it does NOT throw the condition
always true warning
Implementing Infinite Loops
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Standard C Library
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Slide 91
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
C Standard Library
Performing Input/Output using
the C library functions
getchar
gets
printf
putchar
puts
scanf
sprintf
sscanf.
#i ncl ude <st di o. h>
voi d mai n( voi d) {
pr i nt f ( Hel l o Wor l d! \ n) ;
whi l e( 1) ;
}
All input/output performed by C library functions is supported
by underlying calls to getchar() and putchar()
The C source code for these and all other C library functions
are commonly included
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
C Compiler
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
CodeWarrior
Slide 93
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
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
Slide 94
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Compiler Requirements
.h
.h
.c
.c
.cpp
.cpp
.o
.o
listing
listing
Compiler
ROM-able code
Optimized code
Re-entrant code
Support for Different Member in CPU Family
Support for different memory models
Non Obvious optimization
Slide 95
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
The Compiler
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
CodeWarrior
Memory Placement PRM file
Slide 97
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Some more natural Questions...
So far we have covered C language basics and how these are
applied into embedded systems.
After seeing all this, some natural questions that may arise:
Where is my code?
Where are my variables?
Slide 98
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Where is my code?
In the PRM file (The Linker Parameter File), you can define
where you want to allocate each segments you have defined
in your source code.
In order to place a segment into a specific memory area; just
add the segment name in the PLACEMENT block of your
PRM file.
http://www.metrowerks.com/
..\CodeWarrior Manuals\common\Manual SmartLinker.pdf
Slide 99
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
# pragma causes the preprocessor
to perform an implementation-
dependent action
# pragma directive
Slide 100
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Slide 101
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Where are my variables?
The variables are placed into the Default_RAM PLACEMENT
unless otherwise stated with a #pragma...
It is important to define a segment into the Zero Page RAM
($40 -$FF) to place the most frequently used variables.
This way, the compiler will optimize the code using direct
addressing mode (8-bit address) instead of extended mode
(16-bit address).
Slide 102
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Define a Data Segment
(VarA) located in a specific
memory location. (i.e.
Communications protocols)
A new memory
segment is defined
The Variable is defined
in the segment, and its
location is resolved by
the linker
Open file:
Lab7-DataSeg.mcp
Slide 103
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
VarB in the DEFAULT Segment
VarA in location 0x80
Slide 104
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Define a code segment to
be located in a specific
memory location
The segment is defined and the
function is implemented in it
Open file:
Lab8-CodeSeg.mcp
Slide 105
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
The code segment function1
is located in memory segment
FunctionsROM which was
defined between EF00 and
EFFF
Slide 106
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Define a Constant Segment in
a specific memory
location
Open file:
Lab9-ConstSeg.mcp
Slide 107
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
The constant value stored in the
Array is not stored in memory and a
direct replacement of its content is
used. If the variable Array wants to
be updated in Flash, erroneous
results will show up.
Slide 108
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
The constants replacement optimization must
be turned off.
Slide 109
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
The array is located in the position we
specified
The variable address is used
instead of its value
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Startup Routine
CodeWarrior
Slide 111
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Startup Routine
The startup code is generally written in assembly language and
linked with any executable that you build. It prepares the way
for the execution of programs written in a high-level language.
1. Disable interrupts
2. Copy any initialized data from ROM to RAM
3. Zero the uninitialized data area
4. Allocate space for and initialize the stack
5. Create and initialize the heap
6. Enable interrupts
7. Call main()
Begin at the beginning . . .
and go on till you come to the
end: then stop.
- Lewis Carroll.
Slide 112
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Double-Click
Slide 113
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
The Startup routine is the
First one executed after
reset. The reset vector
stores the location where
_startup() is.
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Compiler Optimizations
CodeWarrior
Slide 115
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Compiler Optimizations
CodeWarrior Compiler provides several ways to
optimize the code generated from our C source code
to the actual assembly code programmed into the
microcontroller.
The Global Optimizations settings panel configures
how the compiler optimizes object code. All
optimization routines rearrange object code without
affecting its logical execution sequence.
Slide 116
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Compiler Optimizations Strength Reduction
Strength Reduction is an optimization that strives to replace
expensive operations by cheaper ones, where the cost factor is
either execution time or code size.
Inside loops, replaces multiplication instructions with
addition instructions.
The following example will demostrate how the compiler, based
on what the application does, will decide which operation will
achieve the same result with the less cost.
Slide 117
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Byte Multiplication by 3
Open file:
Lab10-Optimize1.mcp
Slide 118
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
It is implemented using the
MUL instruction
Slide 119
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Byte Multiplication by 4
Slide 120
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
It is implemented using 2 shift-lefts.
It uses H:X as a pointer to the value
we want to multiply
Slide 121
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
It is equivalent to
VarA = VarA * 4;
Slide 122
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Compiler Optimizations Dead Code Elimination
For dead code elimination the Compiler optimizes the
application and does not generate executable code for
statements that are not used.
Removes statements never logically executed or referred to
by other statements.
Slide 123
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Compiler Optimizations
Dead Assignments
Dead Assignments
For Dead Store Elimination, the Compiler removes
assignments to a variable that goes unused before being
reassigned again.
In the following compiler optimizations
In the following compiler optimizations
example
example
,
,
we
we

ll
ll
demostrate
demostrate
how we can modify the way CodeWarrior generates the code by
how we can modify the way CodeWarrior generates the code by
changing the optimizations settings for the compiler.
changing the optimizations settings for the compiler.
Slide 124
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
This loop defines the
value of i, based on
tiers, but it will store
a value of 1 after the
loop
Open file:
Lab12-Optimize3.mcp
Slide 125
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Slide 126
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Codewarrior HC08 Compiler Options Settings
Slide 127
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
All this code is skipped
Slide 128
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Variables used in the loops
are now global variables
Slide 129
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
The code generated for
the loops is not passed
over even though the
optimizer is on. It is
because the glogal
variable could be
accessed by hardware
(interrupts) action
Slide 130
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Loop Unrolling
Duplicates code inside a loop in
order to spread over more
operations branch and completion-
test overhead.
For the following example well
demostrate how the Loop Unrolling
option works:
Compiler Optimizations Loop Unrolling
By changing the settings
for the compiler we may
select different
optimization options that
will make a difference at
the time the code is
generated.
Slide 131
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Example without Loop unrolling:
Compiler generates
the code that
corresponds to the
4 cycle loop
Open file:
Lab13-Optimize4.mcp
Slide 132
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Loop Unrolling Example:
Using the same
code but changing
the settings to
loop unrolling,
the Compiler
generates the
following code
Compiler unrolls the loop.
for ( i = 0; i < 4; j++ )
Array [ i ] = i;
------------------------------
Array [ 0 ] = 0;
Array [ 1 ] = 1;
Array [ 2 ] = 2;
Array [ 3 ] = 3;
Slide 133
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
More Compiler Optimization Options:
Global Register
Allocation
Stores working values of heavily used variables in registers instead of
memory.
Branch
Optimizations
Merges and restructures portions of the intermediate code translation in order
to reduce branch instructions.
Arithmetic
Operations
Replaces intensive computational instructions with faster equivalent
instructions that produce the same result.
Expression
Simplification
Replaces complex arithmetic expressions with simplified equivalent
expressions.
Common
Subexpression
Elimination
Replaces redundant expressions with a single expression.
Copy
Propagation Replaces multiple occurrences of one variable with a single occurrence.
Slide 134
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Even More Compiler Optimization Options:
Peephole
Optimization
Applies local optimization routines to small sections of code.
Live Range
Splitting
Reduces variable lifetimes to achieve optimal allocation. Shorter variable
lifetimes reduce register spilling.
Loop-Invariant
Code Motion
Moves static computations outside of a loop
Loop
Transformations
Reorganizes loop object code in order to reduce setup and completion-test
overhead.
Lifetime Based
Register
Allocation
In a particular routine, uses the same processor register to store different
variables, as long as no statement uses those variables simultaneously.
Instruction
Scheduling
Rearranges the instruction sequence to reduce conflicts among registers
and processor resources.
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
CodeWarrior
Conditional Compilation
Slide 136
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Compiler directives
#if, #else, #elif, #endif:
Those directives are used for Conditional compilation
#if <constant-expression>
#else OR #elif <constant-expression>
#endif
Only compiles the lines following the #if directive when <constant-expression>
evaluates to nonzero. Otherwise, the lines that follow are skipped until the
matching #else or #endif is encountered.
#error
Defines a compiling error message to be displayed.
Slide 137
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
S12DP256 S12DP256 must must be be
defined defined, , otherwise otherwise, a , a
compiling compiling error error is is
generated generated. .
For Embedded Systems:
-Multiplatform support in same source code
-Source Code Flexibility (configuration at
compile time).
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
NITRON Training
UART & FLASH Examples
UART EMULATION
Slide 139
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Nitron Overview
Performance Reach of NITRON MCUs
68HC908QT/QY Family
Common Features
CORE: 0.5 HC08 RAM: 128 bytes
BUS SPEED: 8 MHz (125 ns min. instruction) SCI/SPI: (Software programmable/App Note available)
TIMER: 2 Ch 16 bit timer with IC, OC or PWM EXT Interrupts: IRQ, KBI, Timer IC
VOLTAGE: 2.7V 5.5V OSCILLATOR: Trimmable (+ 25%) Internal osc 3.2 MHz
TEMPERATURE: 40 to +85 Degrees C (+ 5% accuracy up to 105 Degrees C), ext. RC,
(Contact Marketing for extended temperature requirements) ext. clock, or ext. resonator/xtal
OTHER FEATURES: LVI COP with auto wakeup
from STOP, KBI
Device Features
Variant 68HC908QT1 68HC908QT2 68HC908QT4 68HC908QY1 68HC908QY2 68HC908QY4
FLASH 1.5K bytes 1.5K bytes 4K Bytes 1.5K bytes 1.5K bytes 4K bytes
ADC 4 Ch 8-bit 4 Ch 8-bit 4 Ch 8-bit 4 Ch 8-bit
PACKAGE 8 Lead SOIC/ 8 Lead SOIC/ 8 Lead SOIC/ 16 Lead SOIC/ 16 Lead SOIC/ 16 Lead SOIC
PDIP PDIP PDIP PDIP/TSSOP PDIP/TSSOP /PDIP/TSSOP
Slide 140
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
MCU Description
ClockGenerator
16-Bit
Timer Module
8-Bit ADC
PTA1/AD1/TCH1/KBI1 PTA0/AD0/TCH0/KBI0 PTA2/IRQ/KBI2 PTA3/RST/KBI3 PTA5/OSC1/AD3/KBI5 PTA4/OSC2/AD2/KBI4
PTB
Slide 141
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Implementing UART in NITRON
UART must be emulated using the TIMER
In order to use the existing Hardware in the board, a
bidirectional UART will be implemented in
PTA0/TCH0.
Note: care must be taken to avoid collisions.
Slide 142
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
UART Frame Description
The UART frame is composed by
A START Bit (Dominant State)
8 Bits
A STOP Bit (Reccesive State)
Slide 143
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
NITRON Timer Interface Module(TIM)
Timer
Overflow
Input Capture
Output
Compare
Slide 144
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
SendByte
Example Flowchart(1)
Initial Config.
Askfor User
input
SendString
Point to
next byte
Slide 145
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Sending a Byte through UART
The timer must be configured to Overflow every Bit
time.
Every Overflow Interruption a new bit is sent,
including Start and Stop bit.
Overflow every Bit time
Slide 146
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Finished
flag=1
BitCount++
SendStop
Bit
SendNext
Bit
SendByte
Example Flowchart(2)
SendString
Point to
next byte
Last byte
Sent?
SendByte
Config. Timer
toOV /bittime
Timer OV
SendStart
Bit
BitCount?
Finished
Flag?
Exit
0
1
0
<=8
>8
BitCount++
Exit
Exit
N
Y
Slide 147
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Code for UART_SendByte(Byte)
void UART_SendByte(unsigned char byte_to_send)
{
Tx_SHR_Reg=byte_to_send;
bitcount=0;
overmode=TRANSMITTING;
ChangeTimerConfig(OVERFLOW|_1BITTIME);
}
void interrupt 6 overflow_isr (void)
{...
DDRA|=0x01;
if (!bitcount)
{ SET_TX_LOW;
bitcount++; }
else if (bitcount<=8)
{
if (Tx_SHR_Reg&1)
SET_TX_HIGH;
else
SET_TX_LOW;
Tx_SHR_Reg>>=1;
bitcount++;
}
else
{ SET_TX_HIGH;
bitcount=0;
Flag=1; }...
Set Timer toOverflow
everyBit time
SendStart Bit, thebyteand
stop bit everyinterrupt
Open file:
Lab14-UART.mcp
Slide 148
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Example Flowchart(3)
Initial Config.
Askfor User
input
User input
received?
Receiveinput
N
SendByte
SendString
Point to
next byte
Last byte
Sent?
Exit
N
Y
ReceiveString
Point to
next byte
ReceiveByte
Slide 149
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Receiving a Byte from UART
In order to capture the start bit, the timer is
configured as Input Capture.
BaudRate escaler
FBUS
Bittime
* Pr
=
Input
Capture
Overflow every Bit time
Every OverflowInterruption the state of the pin is checked
and a newbit is placed.
Fromthen on, the timer is configured to overflowevery Bit
time
Slide 150
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Finished
flag=1
BitCount++
Receive
Stop Bit
Receive
Next Bit
ReceiveByte
Example Flowchart(4)
ReceiveString
Point to
next byte
Last byte
Received?
Timer OV
BitCount?
<8
=8
Exit
Exit
N
Y
IC Int
Config. Timer
toOverflow
Exit
ReceiveByte
Config. Timer
toInpCapt.
Finished
Flag?
Exit
0
1
Slide 151
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Code for UART_GetByte()
unsigned char UART_GetByte (void) {
ChangeTimerConfig(INPCAPTURE|MAX);
bitcount=0;
...
return Rx_SHR_Reg;
}
void interrupt 4 inpcapture (void)
{
ACK_INPCAPTUREINT;
overmode=RECEIVING_DATA;
bitcount=0;
ChangeTimerConfig(OVERFLOW|_1BITTIME);
}
void interrupt 6 overflow_isr (void)
{
ACK_OVERFLOWINT;
if (overmode==RECEIVING_DATA)
{
DDRA&=0xFE;
if (bitcount<8)
{
bitcount++;
Rx_SHR_Reg>>=1;
if (is_RX_HIGH)
Rx_SHR_Reg|=0x80;
} ...
Set Input
Capture
Whena Start Bit isDetected,
set timer toOverflow
Readthe8
bits andthen
exit
Slide 152
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Example Flowchart(FINAL)
Initial Config.
Askfor User
input
User input
received?
Receiveinput
SendBuffer ReceivetoBuffer
N
Y
<>
1 2
SendByte
SendString
Point to
next byte
Last byte
Sent?
Exit
N
Y
ReceiveString
Point to
next byte
ReceiveByte
Last byte
Received?
Exit
N
Y
Slide 153
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Main program
The example program sends a Message in FLASH
and waits for a user input:
1) Read the Buffer (Initialized as FREESCALE)
2) Write to the Buffer.
temp=UART_GetByte();
switch (temp)
{
case '1':
SEND_BREAK();
UART_SendString(pSend, SEND_LENGTH);
break;
case '2':
SEND_BREAK();
UART_GetString(&My_Receive[0],RECEIVE_LENGTH);
pSend=&My_Receive[0];
break;
}
Wait for a user Input
Receivebuffer[9] when2
SendBuffer if 1
Slide 154
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Power Up the Boards
Set up the jumpers for
programming
J P2 Set toenableReset
J P1 Set toenableLED
J P3 in MON toset
IRQ=Vtst
J P4 in OSC toset 20Mhz
oscillator
J P5 set toenableMON08
communication
Slide 155
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Power Up the Boards
Set up the jumpers for
testing
J P2 Set toenableReset
J P1 Set toenableLED
J P3 in USER
J P4 in OSC toset 20Mhz
oscillator
J P5 set toenableMON08
communication
Slide 156
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Test Program with HyperTerminal
Note: Remember that pin is used bidirectional so care must be taken to avoid collisions.
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
NITRON Training
UART & FLASH Examples
EEPROM EMULATION with On-ROM routines
Slide 158
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
NITRON FLASH
NITRON family uses 2nd Generation 0.5 Flash Technology
Flash-based systems offer ultra-fast programming,along with maximum flexibility and
creativity. WithFlash, your design can be reprogrammed many times during the development
cycle, or even late into manufacturing. Upgrades can even be made in the field. Its never too
late to fix bugs, and its easy to deliver new features or improve performance, safety and
security for your customers.
Flash is guaranteed for minimum 10,000 write/erase cycles (100K typical)
Flash Memory is organized in pages of 64 Bytes.
A page consists of two rows of 32 Bytes. and Flash is programmed a row at a time.
Flash is programmed a row (or part of a row) at a time but an erase operation necessarily
applies to a whole page.
Programming can be carried out at over 10 bytes per ms. (10x EEPROM)
Erase time is 4ms.
Slide 159
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
J umpfromflash program
toROM Flash Program
routine
Nitron Memory Map & Flash Utility
Codefor data retrievingand
Analysis2kb (Half Flash)
Variables
Variables & ReceivedData
ROM Routines
Programmust capture 2000 bytes
andthenmakeandanalysis
Store Data fromRAM
toflash (64 bytesat a
time until 2Kb filled)
J umptoROM Flash
Erase routine
Slide 160
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
NITRON On-ROM routines
In order to reduce user code, and programming and
testing purposes, common routines are implemented
on-ROM.
These are:
Name Address
GETBYTE 0x2800
RDVRRNG 0x2803
ERARNGE 0x2806
PGRRNGE 0x2809
DELNUS 0x280C
Slide 161
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
ON-ROM Routines Description
Slide 162
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
RAM variables
These ROM Routines use defined variables in RAM to
define the Address and Data to be programmed as
well as the CPU Speed and a Control Byte
These are:
Slide 163
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Nitron Memory Map
0x80...
0x88 CtrlByt
0x89 CPUSpd
0x8A-0x8B LstAddr
0x8CLength Bfrstrt
...0xFF
PGRRNGE 0x2809
ERARNGE 0x2806
0x2800...
...0x2DFF
64th (Last)
Page
0xFDC00xFDFF
2nd Page 0xEE400xEE7F
63th Page 0xFD800xFDBF
1
st
Page 0xEE000xEE3F
0xEE80...0xFD7F
Slide 164
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Example2 Flowchart(1)
Initial Config.
Askfor User
input
User input
received?
Receiveinput
SendBuffer
ReceivetoBuffer
andstore in FLASH
N
Y <>
1
3
Send
FLASH Buffer
2
Slide 165
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
CPUSPD(0x89)
contains4*fop
Example2 Flowchart(2)
Flash Erasing
Rangespecifiedin CTRBYT(0x88)
(Bit 6=masserase)
H:X containsaddress
in rangetobe erased
J umptoROM Routine
ERARNGE(0x2806)
Exit
Set Entry
conditions
Slide 166
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
FLASH ERASING Routine
//Definitions
#defineERARNGE() {__asmjsr 0x2806;}
#defineCTRLBYT (*(volatileunsignedchar*) (0x88))
#defineCPUSPD (*(volatileunsignedchar*) (0x89))
#defineLADDRH (*(volatileunsignedchar*) (0x8A))
#defineLADDRL (*(volatileunsignedchar*) (0x8B))
#defineFLASH_TEST_ADDRESS 0xFD40
#defineOSC_CONST FBUS/250000
//Function Call
address=FLASH_TEST_ADDRESS;
EraseRow(&address);
//FunctionDefinition
void EraseRow(Word*_row){
Word _address;
_address =*_row;
CPUSPD =OSC_CONST;
CTRLBYT &=0xBF;
__asmldhx_address;
ERARNGE();
return;
}
J umpto0x2806
RAM variables
Locationtobe erased(Page)
H:X must containthe
addresstobe erased
Clear bit 6 (not masserase)
Open file:
Lab15-EEPROM.mcp
Slide 167
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
J umptoROM Routine
PGRRNGE(0x2809)
CPUSPD(0x89)
contains4*fop
Example2 Flowchart(3)
Flash Programming
LADDR(0x8A&0x8B) containslast
addresstobe programmed
H:X containsaddress
in rangetobe programmed
Exit
Set Entry
conditions
Slide 168
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
FLASH PROGRAMMING Routine
//Definitions
#definePGRRNGE() {__asmjsr 0x2809;}
#defineCTRLBYT (*(volatileunsignedchar*) (0x88))
#defineCPUSPD (*(volatileunsignedchar*) (0x89))
#defineLADDRH (*(volatileunsignedchar*) (0x8A))
#defineLADDRL (*(volatileunsignedchar*) (0x8B))
unsignedchar My_Receive[RECEIVE_LENGTH]@0x8C;
#defineFLASH_TEST_ADDRESS 0xFD40
#defineOSC_CONST FBUS/250000
//Function Call
address=FLASH_TEST_ADDRESS;
ProgramRange(&address, RECEIVE_LENGTH);
//FunctionDefinition
void ProgramRange(Word*_ini, Byte _num) {
Word _first;
_first =*_ini;
CPUSPD =OSC_CONST;
LADDRH =((_first +_num -1) & 0xFF00) >>8;
LADDRL =((_first +_num -1) & 0x00FF);
__asmldhx_first;
PGRRNGE();
return;
}
J umpto0x2809
RAM variables
Locationtobe programmed
H:X must containthe
addresstobe
programmed
Set Last addresstoprogram
Check buffer in
BfrStr Location
Slide 169
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Main program
The example program uses the UART from previous
example and waits for a user input:
1) Read the Buffer (Initialized as FREESCALE)
2) Read EEPROM Location.
3) Write to the Buffer and EEPROM.
...case '2':
SEND_BREAK();
pSend=&My_ROM[0];
UART_SendString(pSend,SEND_LENGTH);
break;
case '3':
address =FLASH_TEST_ADDRESS;
EraseRow(&address);
SEND_BREAK();
UART_GetString(&My_Receive[0],RECEIVE_LENGTH);
pSend=&My_Receive[0];
ProgramRange(&address, RECEIVE_LENGTH);
break; ....
Send8 bytesfromEEPROM
const unsignedchar My_ROM[SEND_LENGTH] @FLASH_TEST_ADDRESS;
EraseEEPROM location
ProgramEEPROM
(Data isalreadyin BufrStrt
(My_Receive@0x8C) )
Slide 170
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Changes & Improvements
The example shown just erase and program the same
Flash location, which is not the best method available.
Freescale guarantees 10,000 write cycles (minimum) for flash
memory.
Other methods can be applied to extend the life of Flash
Memory.
Lookup for a $FF before writing a new buffer. (Must not use $FF
as valid byte, +Code, +Overhead)
Use a dummy byte before each data (1 more byte per buffer, +same limitations as
previous).
Flash memory life can be extended as much as
64*100,000 per block.
Slide 171
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Test Program with HyperTerminal
Slide 172
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
This is the end
C for Embedded Systems
Slide 173
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004
Freescale Semiconductor Confidential Proprietary. Freescaleand 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. 2004

Vous aimerez peut-être aussi