Zwischen Chance und Risiko Keynote: Social Media Eckhard Sedlmayer.
C for Embedded Systems - Eckhard Gosch
Transcript of C for Embedded Systems - Eckhard Gosch
Freescale Semiconductor Confidential Proprietary. 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. 2004
RTAC Americas
C for Embedded Systems
Slide 2Freescale Semiconductor Confidential Proprietary. 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. 2004
RTAC Americas Team
This presentation has been madeby Freescale Applications Team
Guadalajara, México
Slide 3Freescale Semiconductor Confidential Proprietary. 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. 2004
C for Embedded Systems - About the course
By the end of this course you’ll 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 4Freescale Semiconductor Confidential Proprietary. 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. 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 5Freescale Semiconductor Confidential Proprietary. 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. 2004
C Programming Language
What is C?
The 'C' programming language was originally developed for and implemented on the UNIX operating system, by Dennis Ritchiein 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 6Freescale Semiconductor Confidential Proprietary. 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. 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 allow you 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 7Freescale Semiconductor Confidential Proprietary. 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. 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 8Freescale Semiconductor Confidential Proprietary. 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. 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 extremelycrude 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 don’t have hardware support for a C stack.
• Many microcontrollers have multiple memory spaces
One should use standard C as muchas possible...
However, when itinterferes withsolving the problemat hand, do nothesitate to bypassit.
- Nigel Jones
Slide 9Freescale Semiconductor Confidential Proprietary. 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. 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 GOTOGOTO statement.• Global Labels• Global Scratch Registers• Pointer support
Slide 10Freescale Semiconductor Confidential Proprietary. 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. 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 11Freescale Semiconductor Confidential Proprietary. 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. 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 12Freescale Semiconductor Confidential Proprietary. 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. 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. 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. 2004
C BasicsFreescale Semiconductor Confidential Proprietary. 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. 2004
Slide 14Freescale Semiconductor Confidential Proprietary. 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. 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 linemust end with a
semicolon (;)
Slide 15Freescale Semiconductor Confidential Proprietary. 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. 2004
C Functions
A function has the form:
type function_name (parameters) { local variablesC Statements}
Slide 16Freescale Semiconductor Confidential Proprietary. 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. 2004
C Keywords
Identifiersstructunionvoid
enum
Selectionif
elseswitchcase
default
Storage Specifiersregistertypedef
autoextern
Iterationdo
whilefor
Jumpgoto
continuebreakreturn
FunctionSpecifier
inline
Data typecharshort
signedunsigned
intfloatlong
double
Modifiersconststatic
volatilerestrict
PreprocessingDirectives#include#define #undef#line #error#pragma
Conditional Compilation# if# ifdef# ifndef# elif# else# endif
Slide 17Freescale Semiconductor Confidential Proprietary. 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. 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. 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. 2004Freescale Semiconductor Confidential Proprietary. 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. 2004
Embedded Programming
C for Embedded Systems
Freescale Semiconductor Confidential Proprietary. 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. 2004
Variables
Freescale Semiconductor Confidential Proprietary. 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. 2004
Slide 20Freescale Semiconductor Confidential Proprietary. 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. 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 21Freescale Semiconductor Confidential Proprietary. 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. 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 22Freescale Semiconductor Confidential Proprietary. 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. 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 INTtype is machine
dependant
Slide 23Freescale Semiconductor Confidential Proprietary. 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. 2004
CodeWarrior Data Types
The ANSI standard does not precisely define the size of its native types,but CodeWarrior does...
Slide 24Freescale Semiconductor Confidential Proprietary. 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. 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-bittypes.
• “int” and 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 inefficientand should be avoided wherever efficiency is important.
Slide 25Freescale Semiconductor Confidential Proprietary. 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. 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 minimum required.
Use typedefs to get fixed size• Change according to compiler & system• Code is invariant across machines• Used when fixed # bits need for values
Slide 26Freescale Semiconductor Confidential Proprietary. 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. 2004
Defines a complete set of
data types
Three different typevariables are declared
inside main function
Open file:Lab1-Variables.mcp
Slide 27Freescale Semiconductor Confidential Proprietary. 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. 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 stackarea
Slide 28Freescale Semiconductor Confidential Proprietary. 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. 2004
Three different typevariables are declaredoutside main function
Slide 29Freescale Semiconductor Confidential Proprietary. 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. 2004
The compiler just reserves memory for variables being
used. In this example VarA isthe only used variable
Slide 30Freescale Semiconductor Confidential Proprietary. 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. 2004
All the global variables declared are used.
Slide 31Freescale Semiconductor Confidential Proprietary. 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. 2004
In this case thecompiler reserves
memory for all variables.
Slide 32Freescale Semiconductor Confidential Proprietary. 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. 2004
Memory area where variables are declared. Eachvariables 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. 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. 2004
VariablesEmbedded Programming
Storage Class Modifiers
Slide 34Freescale Semiconductor Confidential Proprietary. 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. 2004
Storage Class Modifiers
The following keywords are used with variable declarations, to specify specific needs or conditions associated with the storageof the variables in memory.
These three key words, together, allow us to write not only bettercode, but also tighter code.
static
volatile
const
Slide 35Freescale Semiconductor Confidential Proprietary. 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. 2004
• The variable doesn’t disappear between successive invocations of a function.
• When declared at the module level, it is accessible by allfunctions in all the module, but by no one else.
Note:These variables won’t be stored in the Stack.
When applied to variables, static has two primary fuctions:
Static Variables
Slide 36Freescale Semiconductor Confidential Proprietary. 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. 2004
Static variable Example
FILE1.c
#include <FILE2.h> includes functionscontained in file FILE2.c
void main (void){
MyFunction(); included in FILE2.c
MyFunction(); included in FILE2.c
}
FILE2.c
void MyFunction (void){ Definition ofMyFunctionin FILE2.C
static char myVar = 0; local variable declared static
myVar = myVar + 1;
}
Before entering to MyFunction the firsttime, myVar = 0
Before entering to MyFunction for thesecond time:
myVar = 1
The static Variable keeps its value eventhough myVar is local
Slide 37Freescale Semiconductor Confidential Proprietary. 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. 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 staticfunction such that may be called using a short version of the call orjump instruction.
Slide 38Freescale Semiconductor Confidential Proprietary. 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. 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 39Freescale Semiconductor Confidential Proprietary. 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. 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 practiceto declare volatile allPeripheral Registers in embedded devices.
Slide 40Freescale Semiconductor Confidential Proprietary. 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. 2004
Access to variables defined as volatile are never optimized by the compiler !!!
volatile unsigned char PORTA @0x00;volatile unsigned char SCS1 @0x16;unsigned char value;
void main(void){
PORTA = 0x05; /* PORTA = 00000101 */PORTA = 0x05; /* PORTA = 00000101 */SCS1;value = 10;
}
MOV #5,PORTALDA #10STA @value
Without Volatile keyword
MOV #5,PORTAMOV #5,PORTALDA SCS1LDX #10STX @value
With Volatile keyword
volatilevolatile
Volatile variables are never Optimized
Slide 41Freescale Semiconductor Confidential Proprietary. 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. 2004
/* MC68HC908GP20/32 Official Peripheral Register Names */
volatile unsigned char PORTA @0x0000; /* Ports and data direction */volatile unsigned char PORTB @0x0001;volatile unsigned char PORTC @0x0002;volatile unsigned char PORTD @0x0003;volatile unsigned char PORTE @0x0008;
volatile unsigned char DDRA @0x0004; /* Data Direction Registers */volatile unsigned char DDRB @0x0005;volatile unsigned char DDRC @0x0006;volatile unsigned char DDRD @0x0007;volatile unsigned char DDRE @0x000C;
volatile unsigned char PTAPUE @0x000D; /* Port pull-up enables */volatile unsigned char PTCPUE @0x000E;volatile unsigned char PTDPUE @0x000F;
Volatile variable Example
Slide 42Freescale Semiconductor Confidential Proprietary. 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. 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, andit tells the compiler to store it in ROM code.
Const Variables
Slide 43Freescale Semiconductor Confidential Proprietary. 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. 2004
• Some compilers create a genuine variable in RAM to hold theconst variable. On RAM-limited systems, this can be a significantpenalty.
• Some compilers, like CodeWarrior, store the variable in ROM. However, the “read only” variable 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 44Freescale Semiconductor Confidential Proprietary. 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. 2004
Keyword “Const”
It is safe to assume that a parameter along with the keyword “const” means a “readonly” parameter.
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 45Freescale Semiconductor Confidential Proprietary. 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. 2004
Const volatile variables
Can a variable be both, const and volatile? Yes!Yes!
This modifier form should be use on any memorylocation that can change unexpectedly (volatile) andthat 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. 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. 2004
Resource Mapping
Freescale Semiconductor Confidential Proprietary. 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. 2004
Slide 47Freescale Semiconductor Confidential Proprietary. 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. 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 48Freescale Semiconductor Confidential Proprietary. 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. 2004
How to access I/O Registers?
Many I/O and control registers are located in the direct page andthey should be declared as such, so the compiler can use the directaddressing 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 49Freescale Semiconductor Confidential Proprietary. 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. 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 charat address 0x0000
This is a compiler-specific syntax...
It is more readable, but we pay the priceloosing portability.
An easier way to do this is:
volatile unsigned char PortA @0x0000;
Slide 50Freescale Semiconductor Confidential Proprietary. 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. 2004
• The registers in the CPU are not memory mapped.• The instructions set contains a subset which allows modifing them all.
• C doesn’t 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 51Freescale Semiconductor Confidential Proprietary. 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. 2004
Modify the content ofthe I bit of the CCR in
the CPU
Slide 52Freescale Semiconductor Confidential Proprietary. 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. 2004
The I bit is modified using
Assembly directives.
Freescale Semiconductor Confidential Proprietary. 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. 2004
Bit Fields
Freescale Semiconductor Confidential Proprietary. 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. 2004
Slide 54Freescale Semiconductor Confidential Proprietary. 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. 2004
Bit fields
In embedded systems it is essential to be able to accessand 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 approachand implement the solution…
$0020 1
Slide 55Freescale Semiconductor Confidential Proprietary. 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. 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 56Freescale Semiconductor Confidential Proprietary. 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. 2004
An union is a variable that may hold (at different times)
objects of different types andsizes, with the compiler
keeping track of size andalignment requirements.
Open file:Lab2-BitFields.mcp
Slide 57Freescale Semiconductor Confidential Proprietary. 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. 2004
UnionsUnions provide a way tomanipulate different kinds of
data in a single area of storage, without embedding any
machine-dependent informationin the program
Only PS bits are modified
One-instructionwriting
Freescale Semiconductor Confidential Proprietary. 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. 2004
Arrays
Freescale Semiconductor Confidential Proprietary. 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. 2004
Slide 59Freescale Semiconductor Confidential Proprietary. 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. 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 application’s needs will result in faster and smaller code.
Slide 60Freescale Semiconductor Confidential Proprietary. 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. 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 61Freescale Semiconductor Confidential Proprietary. 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. 2004
Hard-Coded
Incremental Index
Pointer to Array
Open file:Lab3-Arrays.mcp
Slide 62Freescale Semiconductor Confidential Proprietary. 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. 2004
Each access type has its own advantages, and uses different
registers toaccomplish the
operations
Freescale Semiconductor Confidential Proprietary. 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. 2004
Pointers to Functions
Freescale Semiconductor Confidential Proprietary. 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. 2004
Slide 64Freescale Semiconductor Confidential Proprietary. 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. 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 65Freescale Semiconductor Confidential Proprietary. 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. 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 we’d be declaring a function returning a pointer to int.
Slide 66Freescale Semiconductor Confidential Proprietary. 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. 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 67Freescale Semiconductor Confidential Proprietary. 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. 2004
HC08QL Example
•SLIC Module has only oneInterrupt
•User must read SLIC StateVector register (SLCSV) toverify interrupt source
Possible C Solutions:
•Switch-case
•Nested if’s
•Pointers to functions
Slide 68Freescale Semiconductor Confidential Proprietary. 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. 2004
Declare Array offunctions
Call a different functionevery time
Open file:Lab4-Pointers.mcp
Slide 69Freescale Semiconductor Confidential Proprietary. 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. 2004
Debug (1):
1. BreakPoint on function call
2. Step into function (F11)
Slide 70Freescale Semiconductor Confidential Proprietary. 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. 2004
Debug(2):
Will execute a differentfunction every time
Slide 71Freescale Semiconductor Confidential Proprietary. 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. 2004
Debug(3)
••OpenOpen ComponentComponent--> > VisualizationToolVisualizationTool
••OpenOpen Display.vtlDisplay.vtl
••RunRun
VarA is modified in eachfunction
Slide 72Freescale Semiconductor Confidential Proprietary. 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. 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: 110Pointers
Switch
IF
• Nested if’s 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 penaltyof RAM Size
Execution time isalways the same when
using pointers!!!
Freescale Semiconductor Confidential Proprietary. 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. 2004
Stack Pointer / Function Arguments
Freescale Semiconductor Confidential Proprietary. 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. 2004
Slide 74Freescale Semiconductor Confidential Proprietary. 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. 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
typedef struct {unsigned char ID;unsigned short Time;
} ObjectType;
void foo (unsigned char value) {
volatile ObjectType instance;instance.ID = value;
}
foo:B00B A7FB AIS #-3B00D 9EE701 STA 1,SPB010 A707 AIS #3B012 81 RTS
Assembly:
Slide 75Freescale Semiconductor Confidential Proprietary. 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. 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 76Freescale Semiconductor Confidential Proprietary. 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. 2004
Stack Frames
Slide 77Freescale Semiconductor Confidential Proprietary. 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. 2004
HC08 Return Values
Slide 78Freescale Semiconductor Confidential Proprietary. 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. 2004
Four different functions are declared, each one of them returns
a different type variable
Each function has a differenttype parameter (void, byte,
word)
Open file:Lab5-Arguments.mcp
Slide 79Freescale Semiconductor Confidential Proprietary. 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. 2004
The Function is“called” jumping to
the memory location where its source code is located
Assignment to global varable
Function returnswith RTS.
Slide 80Freescale Semiconductor Confidential Proprietary. 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. 2004
Parameter in A register
Operation made in stack andreturn value stored in A
Result stored in variable.
A variable
Slide 81Freescale Semiconductor Confidential Proprietary. 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. 2004
A and X are used forparameters as well as
retun registers
Slide 82Freescale Semiconductor Confidential Proprietary. 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. 2004
A used as registerfor parameters andit is used to addressdirectly the byte in
the array
H:X used to return thepointer
Freescale Semiconductor Confidential Proprietary. 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. 2004
Interrupts
Freescale Semiconductor Confidential Proprietary. 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. 2004
Slide 84Freescale Semiconductor Confidential Proprietary. 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. 2004
The CodeWarrior compiler provides a non-ANSI compliant way tospecify directly the interrupt vector number in the source:
Vector Number Vector Address
Vector Address
Size0 0xFFFE - 0xFFFF 21 0xFFFC – 0xFFFD 22 0xFFFA – 0xFFFB 2... ... ...n 0xFFFF – ( n*2 ) 2
interrupt 17 void TBM_ISR (void){
/* Timebase Module Handler*/
}
Interrupt Vector Table Allocation
How do I handle interrupts in C?
Slide 85Freescale Semiconductor Confidential Proprietary. 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. 2004
908 QT/QY Family Vector Table
Slide 86Freescale Semiconductor Confidential Proprietary. 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. 2004
An Interrupt ServiceRoutine is declared usingthe interrupt modifier
Open file:Lab6-Interrupts.mcp
Slide 87Freescale Semiconductor Confidential Proprietary. 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. 2004
Uses RTI instead of RTS
The interrupt vector stores the addresswhe the ISR starts
Pointer to Function
Freescale Semiconductor Confidential Proprietary. 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. 2004
Iteration, jumps and loops
Freescale Semiconductor Confidential Proprietary. 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. 2004
Slide 89Freescale Semiconductor Confidential Proprietary. 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. 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. 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. 2004
Standard C Library
Freescale Semiconductor Confidential Proprietary. 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. 2004
Slide 91Freescale Semiconductor Confidential Proprietary. 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. 2004
C Standard Library
Performing Input/Output using the C library functions• getchar• gets• printf• putchar• puts• scanf• sprintf• sscanf.
#include <stdio.h>
void main(void){printf(“Hello World!\n”);while(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. 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. 2004
C Compiler
Freescale Semiconductor Confidential Proprietary. 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. 2004
CodeWarrior
Slide 93Freescale Semiconductor Confidential Proprietary. 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. 2004
Compiler’s little Details
While choosing a compiler, you must remember thatthe Devil is in the details
Nice features that can make a huge difference:Inline AssemblyInterrupt FunctionsAssembly Language GenerationStandard LibrariesStartup code
Slide 94Freescale Semiconductor Confidential Proprietary. 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. 2004
Compiler Requirements
.h.h
.c.c
.cpp.cpp
.o.o
listinglistingCompiler
ROM-able codeOptimized codeRe-entrant codeSupport for Different Member in CPU FamilySupport for different memory modelsNon Obvious optimization
Slide 95Freescale Semiconductor Confidential Proprietary. 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. 2004
The Compiler
Freescale Semiconductor Confidential Proprietary. 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. 2004
CodeWarrior
Memory Placement – PRM file
Slide 97Freescale Semiconductor Confidential Proprietary. 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. 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 98Freescale Semiconductor Confidential Proprietary. 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. 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 definedin your source code.
In order to place a segment into a specific memory area; justadd the segment name in the PLACEMENT block of yourPRM file.
http://www.metrowerks.com/
“..\CodeWarrior Manuals\common\Manual SmartLinker.pdf”
Slide 99Freescale Semiconductor Confidential Proprietary. 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. 2004
# pragma causes the preprocessorto perform an implementation-
dependent action
# pragma directive
Slide 100Freescale Semiconductor Confidential Proprietary. 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. 2004
Slide 101Freescale Semiconductor Confidential Proprietary. 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. 2004
Where are my variables?
The variables are placed into the Default_RAM PLACEMENTunless 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 directaddressing mode (8-bit address) instead of extended mode(16-bit address).
Slide 102Freescale Semiconductor Confidential Proprietary. 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. 2004
Define a Data Segment(VarA) located in a specific
memory location. (i.e. Communications protocols)
A new memorysegment is defined
The Variable is definedin the segment, and itslocation is resolved by
the linker
Open file:Lab7-DataSeg.mcp
Slide 103Freescale Semiconductor Confidential Proprietary. 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. 2004
VarB in the DEFAULT Segment
VarA in location 0x80
Slide 104Freescale Semiconductor Confidential Proprietary. 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. 2004
Define a code segment tobe located in a specific
memory location
The segment is defined and thefunction is implemented in it
Open file:Lab8-CodeSeg.mcp
Slide 105Freescale Semiconductor Confidential Proprietary. 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. 2004
The code segment function1 is located in memory segment
FunctionsROM which wasdefined between EF00 and
EFFF
Slide 106Freescale Semiconductor Confidential Proprietary. 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. 2004
Define a Constant Segment in a specific memory
location
Open file:Lab9-ConstSeg.mcp
Slide 107Freescale Semiconductor Confidential Proprietary. 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. 2004
The constant value stored in theArray is not stored in memory and a direct replacement of its content isused. If the variable Array wants to
be updated in Flash, erroneousresults will show up.
Slide 108Freescale Semiconductor Confidential Proprietary. 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. 2004
The constants replacement optimization mustbe turned off.
Slide 109Freescale Semiconductor Confidential Proprietary. 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. 2004
The array is located in the position wespecified
The variable address is usedinstead of its value
Freescale Semiconductor Confidential Proprietary. 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. 2004
Startup Routine
CodeWarrior
Slide 111Freescale Semiconductor Confidential Proprietary. 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. 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 112Freescale Semiconductor Confidential Proprietary. 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. 2004
Double-Click
Slide 113Freescale Semiconductor Confidential Proprietary. 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. 2004
The Startup routine is theFirst one executed afterreset. The reset vector stores the location where_startup() is.
Freescale Semiconductor Confidential Proprietary. 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. 2004
Compiler Optimizations
CodeWarrior
Slide 115Freescale Semiconductor Confidential Proprietary. 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. 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 116Freescale Semiconductor Confidential Proprietary. 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. 2004
Compiler Optimizations – Strength Reduction
Strength Reduction is an optimization that strives to replaceexpensive operations by cheaper ones, where the cost factor iseither execution time or code size.
Inside loops, replaces multiplication instructions with addition instructions.
The following example will demostrate how the compiler, basedon what the application does, will decide which operation willachieve the same result with the less cost.
Slide 117Freescale Semiconductor Confidential Proprietary. 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. 2004
Byte Multiplication by 3
Open file:Lab10-Optimize1.mcp
Slide 118Freescale Semiconductor Confidential Proprietary. 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. 2004
It is implemented using the
MUL instruction
Slide 119Freescale Semiconductor Confidential Proprietary. 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. 2004
Byte Multiplication by 4
Slide 120Freescale Semiconductor Confidential Proprietary. 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. 2004
It is implemented using 2 shift-lefts. It uses H:X as a pointer to the value
we want to multiply
Slide 121Freescale Semiconductor Confidential Proprietary. 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. 2004
It is equivalent to
VarA = VarA * 4;
Slide 122Freescale Semiconductor Confidential Proprietary. 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. 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 123Freescale Semiconductor Confidential Proprietary. 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. 2004
Compiler Optimizations – Dead AssignmentsDead Assignments
For Dead Store Elimination, the Compiler removes assignments to a variable that goes unused before being reassigned again.
In the following compiler optimizationsIn the following compiler optimizations exampleexample, , wewe’’llll 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 124Freescale Semiconductor Confidential Proprietary. 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. 2004
This loop defines thevalue of i, based ontiers, but it will store a value of 1 after theloop
Open file:Lab12-Optimize3.mcp
Slide 125Freescale Semiconductor Confidential Proprietary. 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. 2004
Slide 126Freescale Semiconductor Confidential Proprietary. 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. 2004
Codewarrior HC08 Compiler Options Settings
Slide 127Freescale Semiconductor Confidential Proprietary. 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. 2004
All this code is skipped
Slide 128Freescale Semiconductor Confidential Proprietary. 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. 2004
Variables used in the loopsare now global variables
Slide 129Freescale Semiconductor Confidential Proprietary. 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. 2004
The code generated forthe loops is not passedover even though theoptimizer is on. It isbecause the glogalvariable could be
accessed by hardware (interrupts) action
Slide 130Freescale Semiconductor Confidential Proprietary. 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. 2004
• Loop UnrollingDuplicates code inside a loop in order to spread over more operations branch and completion-test overhead.
For the following example we’ll 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 131Freescale Semiconductor Confidential Proprietary. 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. 2004
Example without Loop unrolling:
Compiler generates the code that
corresponds to the 4 cycle loop
Open file:Lab13-Optimize4.mcp
Slide 132Freescale Semiconductor Confidential Proprietary. 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. 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 133Freescale Semiconductor Confidential Proprietary. 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. 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 134Freescale Semiconductor Confidential Proprietary. 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. 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. 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. 2004
CodeWarrior
Conditional Compilation
Slide 136Freescale Semiconductor Confidential Proprietary. 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. 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 137Freescale Semiconductor Confidential Proprietary. 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. 2004
S12DP256 S12DP256 mustmust be be defineddefined, , otherwiseotherwise, a , a compilingcompiling errorerror isisgeneratedgenerated..
For Embedded Systems:
-Multiplatform support in same source code
-Source Code Flexibility (configuration at compile time).
Freescale Semiconductor Confidential Proprietary. 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. 2004
NITRON Training
UART & FLASH ExamplesUART EMULATION
Slide 139Freescale Semiconductor Confidential Proprietary. 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. 2004
Nitron Overview
Performance Reach of NITRON MCUs
68HC908QT/QY Family
Common FeaturesCORE: 0.5µ HC08 RAM: 128 bytesBUS 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 ICVOLTAGE: 2.7V – 5.5V OSCILLATOR: Trimmable (+– 25%) Internal osc 3.2 MHzTEMPERATURE: – 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 FeaturesVariant 68HC908QT1 68HC908QT2 68HC908QT4 68HC908QY1 68HC908QY2 68HC908QY4FLASH 1.5K bytes 1.5K bytes 4K Bytes 1.5K bytes 1.5K bytes 4K bytesADC – 4 Ch 8-bit 4 Ch 8-bit – 4 Ch 8-bit 4 Ch 8-bitPACKAGE 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 140Freescale Semiconductor Confidential Proprietary. 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. 2004
MCU Description
Clock Generator16-BitTimer Module8-Bit ADCPTA1/AD1/TCH1/KBI1PTA0/AD0/TCH0/KBI0PTA2/IRQ/KBI2PTA3/RST/KBI3PTA5/OSC1/AD3/KBI5PTA4/OSC2/AD2/KBI4PTB
Slide 141Freescale Semiconductor Confidential Proprietary. 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. 2004
Implementing UART in NITRON
UART must be emulated using the TIMERIn 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 142Freescale Semiconductor Confidential Proprietary. 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. 2004
UART Frame Description
The UART frame is composed by • A START Bit (Dominant State)• 8 Bits • A STOP Bit (Reccesive State)
Slide 143Freescale Semiconductor Confidential Proprietary. 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. 2004
NITRON Timer Interface Module(TIM)
TimerOverflow
Input Capture
Output Compare
Slide 144Freescale Semiconductor Confidential Proprietary. 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. 2004
Send Byte
Example Flowchart(1)
Initial Config.
Ask for Userinput
Send String
Point tonext byte
Slide 145Freescale Semiconductor Confidential Proprietary. 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. 2004
Sending a Byte through UART
The timer must be configured to Overflow every Bittime.Every Overflow Interruption a new bit is sent, including Start and Stop bit.
Overflow every Bit time
Slide 146Freescale Semiconductor Confidential Proprietary. 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. 2004
Finishedflag =1BitCount++
Send StopBit
Send NextBit
Send Byte
Example Flowchart(2)
Send String
Point tonext byte
Last byteSent?
Send Byte
Config. Timerto OV /bittime
Timer OV
Send StartBit
BitCount?
FinishedFlag?
Exit
0
1
0
<=8
>8
BitCount++
ExitExit
N
Y
Slide 147Freescale Semiconductor Confidential Proprietary. 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. 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 to Overflowevery Bit time
Send Start Bit, the byte andstop bit every interrupt
Open file:Lab14-UART.mcp
Slide 148Freescale Semiconductor Confidential Proprietary. 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. 2004
Example Flowchart(3)
Initial Config.
Ask for Userinput
User inputreceived?
Receive input
N
Send Byte
Send String
Point tonext byte
Last byteSent?
Exit
N
Y
Receive String
Point tonext byte
Receive Byte
Slide 149Freescale Semiconductor Confidential Proprietary. 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. 2004
Receiving a Byte from UART
In order to capture the start bit, the timer isconfigured as Input Capture.
BaudRateescalerFBUSBittime
*Pr=
InputCapture
Overflow every Bit time
Every Overflow Interruption the state of the pin is checkedand a new bit is placed.
From then on, the timer is configured to overflow every Bittime
Slide 150Freescale Semiconductor Confidential Proprietary. 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. 2004
Finishedflag =1BitCount++
ReceiveStop Bit
ReceiveNext Bit
Receive Byte
Example Flowchart(4)
Receive String
Point tonext byte
Last byteReceived?
Timer OV
BitCount?
<8
=8
ExitExit
N
Y
IC Int
Config. Timerto Overflow
Exit
Receive Byte
Config. Timerto Inp Capt.
FinishedFlag?
Exit
0
1
Slide 151Freescale Semiconductor Confidential Proprietary. 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. 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 InputCapture
When a Start Bit is Detected, set timer to Overflow
Read the 8 bits and then
exit
Slide 152Freescale Semiconductor Confidential Proprietary. 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. 2004
Example Flowchart(FINAL)
Initial Config.
Ask for Userinput
User inputreceived?
Receive input
Send Buffer Receive to Buffer
N
Y< >
’1’ ’2’
Send Byte
Send String
Point tonext byte
Last byteSent?
Exit
N
Y
Receive String
Point tonext byte
Receive Byte
Last byteReceived?
Exit
N
Y
Slide 153Freescale Semiconductor Confidential Proprietary. 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. 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
Receive buffer[9] when ‘2’
Send Buffer if ‘1’
Slide 154Freescale Semiconductor Confidential Proprietary. 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. 2004
Power Up the Boards
Set up the jumpers forprogramming
JP2 Set to enable Reset
JP1 Set to enable LED
JP3 in MON to setIRQ=Vtst
JP4 in OSC to set 20Mhz oscillator
JP5 set to enable MON08 communication
Slide 155Freescale Semiconductor Confidential Proprietary. 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. 2004
Power Up the Boards
Set up the jumpers fortesting
JP2 Set to enable Reset
JP1 Set to enable LED
JP3 in USER
JP4 in OSC to set 20Mhz oscillator
JP5 set to enable MON08 communication
Slide 156Freescale Semiconductor Confidential Proprietary. 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. 2004
Test Program with HyperTerminal
Note: Remember that pin is used bidirectional so care must be taken to avoid collisions.
Freescale Semiconductor Confidential Proprietary. 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. 2004
NITRON Training
UART & FLASH ExamplesEEPROM EMULATION with On-ROM routines
Slide 158Freescale Semiconductor Confidential Proprietary. 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. 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. It’s never too
late to fix bugs, and it’s 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 necessarilyapplies to a whole page.Programming can be carried out at over 10 bytes per ms. (10x EEPROM)Erase time is 4ms.
Slide 159Freescale Semiconductor Confidential Proprietary. 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. 2004
Jump from flash programto ROM “Flash Program”
routine
Nitron Memory Map & Flash Utility
Code for data retrieving andAnalysis 2kb (Half Flash)
VariablesVariables & Received Data
ROM Routines
Program must capture 2000 bytesand then make and analysis
Store Data from RAM to flash (64 bytes at a time until 2Kb filled)
Jump to ROM “Flash Erase” routine
Slide 160Freescale Semiconductor Confidential Proprietary. 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. 2004
NITRON On-ROM routines
In order to reduce user code, and programming andtesting purposes, common routines are implementedon-ROM.These are: Name Address
GETBYTE 0x2800RDVRRNG 0x2803ERARNGE 0x2806PGRRNGE 0x2809DELNUS 0x280C
Slide 161Freescale Semiconductor Confidential Proprietary. 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. 2004
ON-ROM Routines Description
Slide 162Freescale Semiconductor Confidential Proprietary. 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. 2004
RAM variables
These ROM Routines use defined variables in RAM todefine the Address and Data to be programmed as well as the CPU Speed and a Control ByteThese are:
Slide 163Freescale Semiconductor Confidential Proprietary. 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. 2004
Nitron Memory Map
0x80...0x88 CtrlByt0x89 CPUSpd
0x8A-0x8B LstAddr0x8C→Length Bfrstrt
...0xFF
PGRRNGE0x2809ERARNGE0x2806
0x2800...
...0x2DFF
64th (Last) Page
0xFDC0→0xFDFF
2nd Page0xEE40→0xEE7F
63th Page0xFD80→0xFDBF
1st Page0xEE00→0xEE3F
0xEE80...0xFD7F
Slide 164Freescale Semiconductor Confidential Proprietary. 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. 2004
Example2 Flowchart(1)
Initial Config.
Ask for Userinput
User inputreceived?
Receive input
Send Buffer Receive to Bufferand store in FLASH
N
Y<>’1’ ’3’
SendFLASH Buffer
’2’
Slide 165Freescale Semiconductor Confidential Proprietary. 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. 2004
CPUSPD(0x89)contains 4*fop
Example2 Flowchart(2)
Flash Erasing
Range specified in CTRBYT(0x88)(Bit 6= mass erase)
H:X contains addressin range to be erased
Jump to ROM RoutineERARNGE(0x2806)
Exit
Set Entryconditions
Slide 166Freescale Semiconductor Confidential Proprietary. 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. 2004
FLASH ERASING Routine
//Definitions#define ERARNGE() {__asm jsr 0x2806;}#define CTRLBYT (*(volatile unsigned char*) (0x88))#define CPUSPD (*(volatile unsigned char*) (0x89))#define LADDRH (*(volatile unsigned char*) (0x8A))#define LADDRL (*(volatile unsigned char*) (0x8B))#define FLASH_TEST_ADDRESS 0xFD40#define OSC_CONST FBUS/250000//Function Calladdress = FLASH_TEST_ADDRESS;EraseRow(&address);//Function Definitionvoid EraseRow(Word *_row){
Word _address;_address = *_row;CPUSPD = OSC_CONST;CTRLBYT &= 0xBF;__asm ldhx _address;ERARNGE();return;
}
Jump to 0x2806
RAM variables
Location to be erased (Page)
H:X must contain theaddress to be erased
Clear bit 6 (not mass erase)
Open file:Lab15-EEPROM.mcp
Slide 167Freescale Semiconductor Confidential Proprietary. 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. 2004
Jump to ROM RoutinePGRRNGE(0x2809)
CPUSPD(0x89)contains 4*fop
Example2 Flowchart(3)
Flash Programming
LADDR(0x8A&0x8B) contains lastaddress to be programmed
H:X contains addressin range to be programmed
Exit
Set Entryconditions
Slide 168Freescale Semiconductor Confidential Proprietary. 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. 2004
FLASH PROGRAMMING Routine
//Definitions#define PGRRNGE() {__asm jsr 0x2809;}#define CTRLBYT (*(volatile unsigned char*) (0x88))#define CPUSPD (*(volatile unsigned char*) (0x89))#define LADDRH (*(volatile unsigned char*) (0x8A))#define LADDRL (*(volatile unsigned char*) (0x8B))unsigned char My_Receive[RECEIVE_LENGTH]@0x8C;#define FLASH_TEST_ADDRESS 0xFD40#define OSC_CONST FBUS/250000//Function Calladdress = FLASH_TEST_ADDRESS;ProgramRange (&address, RECEIVE_LENGTH);//Function Definitionvoid ProgramRange(Word *_ini, Byte _num) {
Word _first;_first = *_ini;CPUSPD = OSC_CONST;LADDRH = ((_first + _num -1) & 0xFF00) >> 8;LADDRL = ((_first + _num -1) & 0x00FF);__asm ldhx _first;PGRRNGE();return;
}
Jump to 0x2809
RAM variables
Location to be programmed
H:X must contain theaddress to be programmed
Set Last address to program
Check buffer in BfrStr Location
Slide 169Freescale Semiconductor Confidential Proprietary. 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. 2004
Main program
The example program uses the UART from previousexample 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; ....
Send 8 bytes from EEPROM
const unsigned char My_ROM[SEND_LENGTH] @FLASH_TEST_ADDRESS;
Erase EEPROM location
Program EEPROM
(Data is already in BufrStrt(My_Receive@0x8C) )
Slide 170Freescale Semiconductor Confidential Proprietary. 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. 2004
Changes & Improvements
The example shown just erase and program the sameFlash 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 171Freescale Semiconductor Confidential Proprietary. 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. 2004
Test Program with HyperTerminal
Slide 172Freescale Semiconductor Confidential Proprietary. 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. 2004
This is the end
“C for Embedded Systems”
Slide 173Freescale Semiconductor Confidential Proprietary. 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. 2004Freescale Semiconductor Confidential Proprietary. 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. 2004