c for embedded

Upload: duydb56

Post on 13-Apr-2018

261 views

Category:

Documents


3 download

TRANSCRIPT

  • 7/27/2019 C for Embedded

    1/173

    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

  • 7/27/2019 C for Embedded

    2/173

    Slide 2

    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 Team

    This presentation has been made

    by Freescale Applications Team

    Guadalajara, Mxico

  • 7/27/2019 C for Embedded

    3/173

    Slide 3

    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 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 andCodeWarrior.

  • 7/27/2019 C for Embedded

    4/173

    Slide 4

    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

    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 EmbeddedVariablesResource mappingBit FieldsArraysPointers to functionsStack PointerInterruptsIteration, jumps and loopsStandard C library

  • 7/27/2019 C for Embedded

    5/173

    Slide 5

    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 Programming Language

    What is C?

    The 'C' programming language was originally developed for andimplemented on the UNIX operating system, by Dennis Ritchiein 1971.

    One of the best features of C is that it is not tied to any particularhardware or system. This makes it easy for a user to writeprograms that will run without any changes on practically allmachines.

    C is often called a middle-level computer language as itcombines the elements of high-level languages with thefunctionalism of assembly language.

  • 7/27/2019 C for Embedded

    6/173

    Slide 6

    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

    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 aredoing; it only requires that they state their intentions explicitly.

  • 7/27/2019 C for Embedded

    7/173

    Slide 7

    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

    These are some of the common issues we encounterwhen 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

  • 7/27/2019 C for Embedded

    8/173

    Slide 8

    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

    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 areabsolute performance killers to an eight-bit machine.

    Some microcontroller architectures dont have hardwaresupport for a C stack.

    Many microcontrollers have multiple memory spaces

    One should usestandard C as much

    as possible...However, when itinterferes withsolving the problemat hand, do not

    hesitate to bypassit.

    - Nigel Jones

  • 7/27/2019 C for Embedded

    9/173

    Slide 9

    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

    When using C in a Low-End 8-bit Microcontroller we have to find

    ways to keep code small. That means breaking a fewprogramming rules.

    Breaking some C Paradigms

    Enabling/Disabling Global Interrupts. The use of GOTOGOTO statement. Global Labels Global Scratch Registers Pointer support

  • 7/27/2019 C for Embedded

    10/173

    Slide 10

    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

    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

  • 7/27/2019 C for Embedded

    11/173

    Slide 11

    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

    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 writeefficient assembly code by hand.

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

  • 7/27/2019 C for Embedded

    12/173

    Slide 12

    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

    1) C allows us to be more productive2) 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 areavailable

    There are many reasons to change assembly for C:

    Why change to C?

  • 7/27/2019 C for Embedded

    13/173

    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 Basics

    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

  • 7/27/2019 C for Embedded

    14/173

    Slide 14

    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 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 havea main()

    function

    Every command line

    must end with asemicolon (;)

  • 7/27/2019 C for Embedded

    15/173

    Slide 15

    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 Functions

    A function has the form:

    type function_name (parameters){

    local variables

    C Statements}

  • 7/27/2019 C for Embedded

    16/173

    Slide 16

    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 Keywords

    Identifiersstructunion

    voidenum

    Selectionif

    elseswitchcase

    default

    Storage Specifiers

    registertypedef

    autoextern

    Iterationdo

    while

    for

    Jumpgoto

    continue

    breakreturn

    Function

    Specifierinline

    Data typechar

    short

    signedunsignedint

    floatlong

    double

    Modifiersconst

    staticvolatilerestrict

    PreprocessingDirectives#include#define#undef#line#error#pragma

    ConditionalCompilation# if

    # ifdef# ifndef# elif# else# endif

  • 7/27/2019 C for Embedded

    17/173

    Slide 17

    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 Operators

    Assignment= plain assignment+= add-= subtract*= multiply/= divide%= remainder= 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 andpostfix 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 ofsizeof size of

    Math* multiplication/ division% modulus(remainder)+ addition- subtraction

    Relational< less than left to right relational greater than

    >= greater than or equal== equal test!= not equal test

    Logical&& logical AND

    || logical inclusive OR

    Conditional?: conditional test

    Sequence, comma

  • 7/27/2019 C for Embedded

    18/173

    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

  • 7/27/2019 C for Embedded

    19/173

    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

  • 7/27/2019 C for Embedded

    20/173

    Slide 20

    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

    The type of a variable determines what kinds ofvalues it may take on.

    In other words, selecting a type for a variable isclosely connected to the way(s) we'll be using thatvariable.

    We'll learn about C's basic data types, how to writeconstants and declare variables of these types.

  • 7/27/2019 C for Embedded

    21/173

    Slide 21

    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

    Selecting a type

    There are several implications to remember:

    The ``set of values'' is finite. C's int type can not representall of the integers; its float type can not represent all floating-point numbers.

    When declaring a new variable and picking a type for it, youhave to keep in mind the values and operations you'll be

    needing.

  • 7/27/2019 C for Embedded

    22/173

    Slide 22

    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

    There are only a few basic data types in C:

    All scalar types(except char) aresigned by default

    Example:

    int = signed int

    0 255

    Basic data types in C

    NOTE: Size of INTtype is machine

    dependant

  • 7/27/2019 C for Embedded

    23/173

    Slide 23

    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 Data Types

    The ANSI standard does not precisely define the size of its native types,but CodeWarrior does...

  • 7/27/2019 C for Embedded

    24/173

    Slide 24

    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

    Data Type Facts

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

    The natural internal data size for 8-bit MCU is 8-bits (one byte), whereasthe C preferred data type is int.

    8-bit machines can process 8-bit data types more efficiently than 16-bit

    types.

    int and larger data types should only be used where required by the sizeof data to be represented.

    Double precision and floating point operations are particularly inefficientand should be avoided wherever efficiency is important.

  • 7/27/2019 C for Embedded

    25/173

    Slide 25

    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

    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

    O fil

  • 7/27/2019 C for Embedded

    26/173

    Slide 26

    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

    Defines a

    complete set ofdata types

    Three different typevariables are declared

    insidemain function

    Open file:

    Lab1-Variables.mcp

  • 7/27/2019 C for Embedded

    27/173

    Slide 27

    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

    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

  • 7/27/2019 C for Embedded

    28/173

    Slide 28

    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

    Three different typevariables are declaredoutside

    main function

  • 7/27/2019 C for Embedded

    29/173

    Slide 29

    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

    The compiler just reservesmemory for variables being

    used. In this example VarA isthe only used variable

  • 7/27/2019 C for Embedded

    30/173

    Slide 30

    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

    All the global variablesdeclared are used.

  • 7/27/2019 C for Embedded

    31/173

    Slide 31

    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

    In this case thecompiler reserves

    memory for all variables.

  • 7/27/2019 C for Embedded

    32/173

    Slide 32

    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

    Memory area where variables are declared. Eachvariables has different size (1, 2 and 4 bytes)

    Each add operation is donein different way, based on

    the size of the variable

  • 7/27/2019 C for Embedded

    33/173

    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

    Storage Class Modifiers

  • 7/27/2019 C for Embedded

    34/173

    Slide 34

    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

    Storage Class Modifiers

    The following keywords are used with variable declarations, tospecify specific needs or conditions associated with the storage

    of the variables in memory.

    These three key words, together, allow us to write not only bettercode, but also tightercode.

    static

    volatile

    const

    Static Variables

  • 7/27/2019 C for Embedded

    35/173

    Slide 35

    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

    The variable doesnt disappear between successiveinvocations 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 variableswont be storedin the Stack.

    When applied to variables, static has two primary fuctions:

    Static Variables

    Static variable Example

  • 7/27/2019 C for Embedded

    36/173

    Slide 36

    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

    Static variable Example

    FILE1.c

    #i ncl ude i ncl udes f unct i onscont ai ned i n f i l eFI 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 oni 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 toMyFunction the firsttime, myVar =0

    Before entering toMyFunction for thesecond time:

    myVar =1

    The static Variablekeeps its value eventhough myVar is local

    Static Functions

  • 7/27/2019 C for Embedded

    37/173

    Slide 37

    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

    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 cancall a given static function, it may strategically place the staticfunction such that may be called using a short version of the call or

    jump instruction.

    Uses of the keyword static

  • 7/27/2019 C for Embedded

    38/173

    Slide 38

    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

    Uses of the keyword static

    A variable declared static within the body of a function maintains itsvalue between function invocations

    A variable declared static within a module, (but outside the body of afunction) is accessible by all functions within that module.

    Functions declared static within a module may only be called by otherfunctions within that module.

    For Embedded Systems:

    Encapsulation of persistent data (wrapping)

    Modular coding (data hiding)

    Hiding of internal processing in each module

    Volatile Variables

  • 7/27/2019 C for Embedded

    39/173

    Slide 39

    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

    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 bechanged outside the normal program flow.

    It is very good practiceto declare volatile all

    Peripheral Registers inembedded devices.

    Volatile variables are never Optimized

  • 7/27/2019 C for Embedded

    40/173

    Slide 40

    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

    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, PORTALDA #10STA @val ue

    Without Volatile keyword

    MOV #5, PORTAMOV #5, PORTALDA SCS1LDX #10

    STX @val ue

    With Volatile keyword

    volatilevolatile

    p

    Volatile variable Example

  • 7/27/2019 C for Embedded

    41/173

    Slide 41

    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

    / * 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 rect i on Regi s ter 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;

    p

    Const Variables

  • 7/27/2019 C for Embedded

    42/173

    Slide 42

    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

    The compiler keeps the program memory address of thatlocation. 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 constrains

  • 7/27/2019 C for Embedded

    43/173

    Slide 43

    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

    Some compilers create a genuine variable in RAM to hold the

    const 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 andaccessed as such, typically using some form of indexed addressing

    (16-bit). Compared to immediate addressing (8-bit), this method isnormally much slower.

    Keyword Const

  • 7/27/2019 C for Embedded

    44/173

    Slide 44

    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

    y

    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;

    Const volatile variables

  • 7/27/2019 C for Embedded

    45/173

    Slide 45

    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

    Can a variable be both, const and volatile? Yes!Yes!

    This modifier form should be use on any memorylocation that can change unexpectedly (volatile) and

    that is read-only (const).

    The most obvious example of this is a hardwarestatus register:

    /* SCI Status Register */

    const volatile unsigned charSCS1 @0x0016

  • 7/27/2019 C for Embedded

    46/173

    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

    Accessing fixed memory locations

  • 7/27/2019 C for Embedded

    47/173

    Slide 47

    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

    Embedded systems are often characterized by requiring the programmerto access a specific memory location.

    Exercise: On a certain project it is required to set an integer variable atthe absolute address 0x2FFA to the value 0xAA55 (the compiler is apure ANSI compiler).

    Write code to accomplish this task.

    Int * ptr;

    ptr = (int *)0x2FFA;

    *ptr = 0xAA55;

    How to access I/O Registers?

  • 7/27/2019 C for Embedded

    48/173

    Slide 48

    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

    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...

    Defining I/O Registers

  • 7/27/2019 C for Embedded

    49/173

    Slide 49

    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

    One common and very useful form is:

    #define PortA ( * ( volatile unsigned char* ) 0x0000 )

    This makes PortA avariable 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 charPortA @0x0000;

    Accessing CPU Registers

  • 7/27/2019 C for Embedded

    50/173

    Slide 50

    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

    The registers in the CPU are not memory mapped.

    The instructions set contains a subset which allows modifing them all.

    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 {

    ----

    ----

    }

  • 7/27/2019 C for Embedded

    51/173

    Slide 51

    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

    Modify the content ofthe I bit of the CCR in

    the CPU

  • 7/27/2019 C for Embedded

    52/173

    Slide 52

    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

    The I bit is modified using

    Assembly directives.

  • 7/27/2019 C for Embedded

    53/173

    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

    Bit fields

  • 7/27/2019 C for Embedded

    54/173

    Slide 54

    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

    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

    Bit Fields

  • 7/27/2019 C for Embedded

    55/173

    Slide 55

    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 PTA0;

    int val = 16;

    short foo = 0;

    PTA0 = 1;

    PTA0 = val; /* Set PTA0 to 1 */

    PTA0 = foo; /* Set PTA0 to 0 */

    TASKING C 8051Cross-Compiler

    Open file:

    Lab2-BitFields.mcp

  • 7/27/2019 C for Embedded

    56/173

    Slide 56

    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

    An union is a variable that mayhold (at different times)

    objects of different types and

    sizes, with the compilerkeeping track of size andalignment requirements.

  • 7/27/2019 C for Embedded

    57/173

    Slide 57

    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

    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 aremodified

    One-instructionwriting

  • 7/27/2019 C for Embedded

    58/173

    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

    Arrays

  • 7/27/2019 C for Embedded

    59/173

    Slide 59

    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 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.

    Array Access

  • 7/27/2019 C for Embedded

    60/173

    Slide 60

    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

    Hard-coded:

    Address resolved at compile time

    Fast executionIncremental Index:

    Fast

    More flexible than hard-coded

    Pointer to array

    Fast execution Hard to read

    May be used with loops

    Open file:

    Lab3-Arrays.mcp

  • 7/27/2019 C for Embedded

    61/173

    Slide 61

    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

    Hard-Coded

    Incremental

    Index

    Pointer to Array

  • 7/27/2019 C for Embedded

    62/173

    Slide 62

    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

    Each access type hasits own advantages,and uses different

    registers toaccomplish the

    operations

  • 7/27/2019 C for Embedded

    63/173

    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 FunctionsFreescale 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

  • 7/27/2019 C for Embedded

    64/173

    Slide 64

    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 are useful for approximatelythe 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 differentfunctions depending on circumstances.

    Defining a pointer to a function

  • 7/27/2019 C for Embedded

    65/173

    Slide 65

    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

    The following code defines a pointer to a function that takes aninteger as an argument and returns an integer as well:

    int (* function) (int);

    The extra parentheses around (* function) are needed becauseof precedence relationships in declarations.

    Without them wed be declaring a function returning a pointer toint.

    Example:

  • 7/27/2019 C for Embedded

    66/173

    Slide 66

    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

    Pointer to function Initialization

    Pointer to functionpoints now to a different

    function.

    Periodic interrupt which

    calls the function thepointer is pointing to.

    HC08QL Example

  • 7/27/2019 C for Embedded

    67/173

    Slide 67

    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

    SLIC Module has only oneInterrupt

    User must read SLIC StateVector register (SLCSV) to

    verify interrupt source

    Possible C Solutions:

    Switch-case

    Nested ifs

    Pointers to functions

    Open file:

    Lab4-Pointers.mcp

  • 7/27/2019 C for Embedded

    68/173

    Slide 68

    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

    Declare Array offunctions

    Call a different functionevery time

    Debug (1):

  • 7/27/2019 C for Embedded

    69/173

    Slide 69

    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

    1. BreakPoint on function call

    2. Step into function (F11)

    Debug(2):

  • 7/27/2019 C for Embedded

    70/173

    Slide 70

    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

    Will execute a differentfunction every time

    Debug(3)

  • 7/27/2019 C for Embedded

    71/173

    Slide 71

    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

    OpenOpen ComponentComponent-->> VisualizationToolVisualizationTool

    OpenOpen Display.vtlDisplay.vtl

    RunRun

    VarA is modified in eachfunction

    When to use pointers:

    Execution time isalways the same when

    using pointers!!!

  • 7/27/2019 C for Embedded

    72/173

    Slide 72

    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

    #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 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

  • 7/27/2019 C for Embedded

    73/173

    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

    The Stack Pointer Importance

  • 7/27/2019 C for Embedded

    74/173

    Slide 74

    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

    A Stack Pointer Register supports verykey features of C:

    Passing variables to subroutines

    Allows the use of recursion

    Is the base of Automatic variables

    t ypedef str 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 #- 3B00D 9EE701 STA 1, SPB010 A707 AI S #3B012 81 RTS

    Assembly:

    Stack Pointer addressing

  • 7/27/2019 C for Embedded

    75/173

    Slide 75

    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 instructions are similar to indexed instructions onlythey use the contents of the stack pointer as an address of theoperand 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.

    Stack Frames

  • 7/27/2019 C for Embedded

    76/173

    Slide 76

    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

    HC08 Return Values

  • 7/27/2019 C for Embedded

    77/173

    Slide 77

    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

    Open file:

    Lab5-Arguments.mcp

  • 7/27/2019 C for Embedded

    78/173

    Slide 78

    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

    Four different functions aredeclared, each one of them returns

    a different type variable

    Each function has a differenttype parameter (void, byte,

    word)

  • 7/27/2019 C for Embedded

    79/173

    Slide 79

    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

    The Function iscalled jumping to

    the memory locationwhere its sourcecode is located

    Assignment to globalvarable

    Function returnswith RTS.

  • 7/27/2019 C for Embedded

    80/173

    Slide 80

    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

    Parameter in A register

    Operation made in stack and

    return value stored in A

    Result stored in variable.

    A variable

  • 7/27/2019 C for Embedded

    81/173

    Slide 81

    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

    A and X are used forparameters as well as

    retun registers

  • 7/27/2019 C for Embedded

    82/173

    Slide 82

    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

    A used as registerfor parameters andit is used to addressdirectly the byte in

    the array

    H:X used to return thepointer

  • 7/27/2019 C for Embedded

    83/173

    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

    InterruptsFreescale 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 do I handle interrupts in C?

  • 7/27/2019 C for Embedded

    84/173

    Slide 84

    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

    The CodeWarrior compiler provides a non-ANSI compliant way tospecify directly the interrupt vector number in the source:

    VectorNumber Vector Address

    VectorAddress

    Size

    0 0xFFFE - 0xFFFF 21 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

    908 QT/QY Family Vector Table

  • 7/27/2019 C for Embedded

    85/173

    Slide 85

    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

    Open file:

    Lab6-Interrupts.mcp

  • 7/27/2019 C for Embedded

    86/173

    Slide 86

    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

    An Interrupt ServiceRoutine is declared usingthe interrupt modifier

  • 7/27/2019 C for Embedded

    87/173

    Slide 87

    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

    Uses RTI instead of RTS

    The interrupt vectorstores the addresswhe the ISR starts

    Pointer to Function

  • 7/27/2019 C for Embedded

    88/173

    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

    Implementing Infinite Loops

  • 7/27/2019 C for Embedded

    89/173

    Slide 89

    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

    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

  • 7/27/2019 C for Embedded

    90/173

    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

    C Standard Library

  • 7/27/2019 C for Embedded

    91/173

    Slide 91

    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

    Performing Input/Output usingthe C library functions

    getchar gets printf

    putchar puts scanf

    sprintf sscanf.

    #i ncl ude

    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 supportedby underlying calls to getchar() and putchar()

    The C source code for these and all other C library functionsare commonly included

  • 7/27/2019 C for Embedded

    92/173

    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

    Compilers little Details

    While choosing a compiler you must remember that

  • 7/27/2019 C for Embedded

    93/173

    Slide 93

    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

    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

    Compiler Requirements

    h

  • 7/27/2019 C for Embedded

    94/173

    Slide 94

    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

    .h.h

    .c.c

    .cpp.cpp

    .o.o

    listinglisting

    Compiler

    ROM-able codeOptimized codeRe-entrant code

    Support for Different Member in CPU FamilySupport for different memory modelsNon Obvious optimization

    The Compiler

  • 7/27/2019 C for Embedded

    95/173

    Slide 95

    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

  • 7/27/2019 C for Embedded

    96/173

    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

    Some more natural Questions...

  • 7/27/2019 C for Embedded

    97/173

    Slide 97

    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

    So far we have covered C language basics and how these areapplied into embedded systems.

    After seeing all this, some natural questions that may arise:

    Where is my code?

    Where are my variables?

    Where is my code?

  • 7/27/2019 C for Embedded

    98/173

    Slide 98

    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

    In the PRM file (The Linker Parameter File), you can definewhere 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 yourPRM file.

    http://www.metrowerks.com/

    ..\CodeWarrior Manuals\common\Manual SmartLinker.pdf

    # pragma directive

    http://www.metrowerks.com/http://www.metrowerks.com/
  • 7/27/2019 C for Embedded

    99/173

    Slide 99

    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

    # pragma causes the preprocessorto perform an implementation-

    dependent action

  • 7/27/2019 C for Embedded

    100/173

    Slide 100

    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

    Where are my variables?

  • 7/27/2019 C for Embedded

    101/173

    Slide 101

    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

    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).

    Th V i bl is d fi d

    Open file:

    Lab7-DataSeg.mcp

  • 7/27/2019 C for Embedded

    102/173

    Slide 102

    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

    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

    VarA in location 0x80

  • 7/27/2019 C for Embedded

    103/173

    Slide 103

    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

    VarB in the DEFAULT Segment

    VarA in location 0x80

    Open file:

    Lab8-CodeSeg.mcp

  • 7/27/2019 C for Embedded

    104/173

    Slide 104

    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

    Define a code segment tobe located in a specific

    memory location

    The segment is defined and thefunction is implemented in it

    The code segment function1is located in memory segment

    F ti ROM hi h

  • 7/27/2019 C for Embedded

    105/173

    Slide 105

    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

    FunctionsROM which wasdefined between EF00 and

    EFFF

    Open file:

    Lab9-ConstSeg.mcp

  • 7/27/2019 C for Embedded

    106/173

    Slide 106

    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

    Define a Constant Segment ina specific memory

    location

    The constant value stored in theArray is not stored in memory and ad l f

  • 7/27/2019 C for Embedded

    107/173

    Slide 107

    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

    direct replacement of its content isused. If the variable Array wants to

    be updated in Flash, erroneous

    results will show up.

  • 7/27/2019 C for Embedded

    108/173

    Slide 108

    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

    The constants replacement optimization mustbe turned off.

    The variable address is usedinstead of its value

  • 7/27/2019 C for Embedded

    109/173

    Slide 109

    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

    The array is located in the position wespecified

    instead of its value

  • 7/27/2019 C for Embedded

    110/173

    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

    Startup Routine

    The startup code is generally written in assembly language andlinked with any executable that you build It prepares the way

  • 7/27/2019 C for Embedded

    111/173

    Slide 111

    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

    linked with any executable that you build. It prepares the wayfor 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 theend: then stop.

    - Lewis Carroll.

  • 7/27/2019 C for Embedded

    112/173

    Slide 112

    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

    Double-Click

  • 7/27/2019 C for Embedded

    113/173

    Slide 113

    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

    The Startup routine is theFirst one executed after reset. The reset vectorstores the location where

    _startup() is.

  • 7/27/2019 C for Embedded

    114/173

    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

    Compiler Optimizations

    CodeWarrior Compilerprovides several ways toti i th d t d f C d

  • 7/27/2019 C for Embedded

    115/173

    Slide 115

    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

    optimize the code generated from our C source codeto the actual assembly code programmed into themicrocontroller.

    The Global Optimizations settings panel configureshow the compiler optimizes object code. Alloptimization routines rearrange object code without

    affecting its logical execution sequence.

    Compiler Optimizations Strength Reduction

    Strength Reduction is an optimization that strives to replaceexpensive operations by cheaper ones where the cost factor is

  • 7/27/2019 C for Embedded

    116/173

    Slide 116

    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

    expensive operations by cheaper ones, where the cost factor iseither execution time or code size.

    Inside loops, replaces multiplication instructions withaddition instructions.

    The following example will demostrate how the compiler, basedon what the application does, will decide which operation will

    achieve the same result with the less cost.

    Open file:

    Lab10-Optimize1.mcp

  • 7/27/2019 C for Embedded

    117/173

    Slide 117

    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

    Byte Multiplication by 3

  • 7/27/2019 C for Embedded

    118/173

    Slide 118

    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

    It is implemented using the

    MUL instruction

  • 7/27/2019 C for Embedded

    119/173

    Slide 119

    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

    Byte Multiplication by 4

    It is implemented using 2 shift-lefts.It uses H:X as a pointer to the value

  • 7/27/2019 C for Embedded

    120/173

    Slide 120

    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

    It uses H:X as a pointer to the valuewe want to multiply

    It i i l t t

  • 7/27/2019 C for Embedded

    121/173

    Slide 121

    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

    It is equivalent to

    VarA = VarA * 4;

    Compiler Optimizations Dead Code Elimination

    For dead code elimination the Compiler optimizes theapplication and does not generate executable code for

  • 7/27/2019 C for Embedded

    122/173

    Slide 122

    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

    app ca o a d does o ge e a e e ecu ab e code ostatements that are not used.

    Removes statements never logically executed or referred toby other statements.

    Compiler Optimizations Dead AssignmentsDead Assignments

    For Dead Store Elimination, the Compiler removesassignments to a variable that goes unused before being

  • 7/27/2019 C for Embedded

    123/173

    Slide 123

    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

    g g greassigned again.

    In the following compiler optimizationsIn the following compiler optimizations exampleexample,, wewellll demostratedemostratehow we can modify the way CodeWarrior generates the code byhow we can modify the way CodeWarrior generates the code by

    changing the optimizations settings for the compiler.changing the optimizations settings for the compiler.

    Open file:

    Lab12-Optimize3.mcp

  • 7/27/2019 C for Embedded

    124/173

    Slide 124

    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

    This loop defines thevalue of i, based on

    tiers, but it will storea value of 1 after theloop

  • 7/27/2019 C for Embedded

    125/173

    Slide 125

    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 HC08 Compiler Options Settings

  • 7/27/2019 C for Embedded

    126/173

    Slide 126

    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

    All this code is skipped

  • 7/27/2019 C for Embedded

    127/173

    Slide 127

    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

    All this code is skipped

  • 7/27/2019 C for Embedded

    128/173

    Slide 128

    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 used in the loopsare now global variables

    The code generated forthe loops is not passedover even though the

  • 7/27/2019 C for Embedded

    129/173

    Slide 129

    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

    over even though theoptimizer is on. It is

    because the glogalvariable could beaccessed by hardware

    (interrupts) action

    Loop Unrolling

    Duplicates code inside a loop in

    Compiler Optimizations Loop Unrolling

  • 7/27/2019 C for Embedded

    130/173

    Slide 130

    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

    Duplicates code inside a loop inorder to spread over more

    operations branch and completion-test overhead.

    For the following example welldemostrate how the Loop Unrollingoption works:

    By changing the settings

    for the compiler we mayselect different

    optimization options thatwill make a difference at

    the time the code is

    generated.

    Example without Loop unrolling:

    Compiler generatesthe code that

    d t th

    Open file:

    Lab13-Optimize4.mcp

  • 7/27/2019 C for Embedded

    131/173

    Slide 131

    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

    corresponds to the4 cycle loop

    Loop Unrolling Example:

    Using the samecode but changing

    the settings to

  • 7/27/2019 C for Embedded

    132/173

    Slide 132

    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

    gloop unrolling,

    the Compilergenerates thefollowing 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;

    More Compiler Optimization Options:

    Global Register

    Allocation

    Stores working values of heavily used variables in registers instead of

    memory.

    Branch Merges and restructures portions of the intermediate code translation in order

  • 7/27/2019 C for Embedded

    133/173

    Slide 133

    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

    Optimizationsg p

    to reduce branch instructions.

    ArithmeticOperations

    Replaces intensive computational instructions with faster equivalentinstructions that produce the same result.

    Expression

    Simplification

    Replaces complex arithmetic expressions with simplified equivalent

    expressions.

    CommonSubexpression

    EliminationReplaces redundant expressions with a single expression.

    CopyPropagation Replaces multiple occurrences of one variable with a single occurrence.

    Even More Compiler Optimization Options:

    PeepholeOptimization Applies local optimization routines to small sections of code.

    Live Range Reduces variable lifetimes to achieve optimal allocation. Shorter variable

  • 7/27/2019 C for Embedded

    134/173

    Slide 134

    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

    Live RangeSplitting

    Reduces variable lifetimes to achieve optimal allocation. Shorter variablelifetimes reduce register spilling.

    Loop-InvariantCode Motion

    Moves static computations outside of a loop

    Loop

    Transformations

    Reorganizes loop object code in order to reduce setup and completion-test

    overhead.

    Lifetime BasedRegister

    Allocation

    In a particular routine, uses the same processor register to store differentvariables, as long as no statement uses those variables simultaneously.

    InstructionScheduling

    Rearranges the instruction sequence to reduce conflicts among registersand processor resources.

  • 7/27/2019 C for Embedded

    135/173

    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

    Compiler directives

    #if, #else, #elif, #endif:

    Those directives are used for Conditional compilation

  • 7/27/2019 C for Embedded

    136/173

    Slide 136

    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

    Those directives are used for Conditional compilation

    #if

    #else OR #elif

    #endif

    Only compiles the lines following the #if directive when evaluates to nonzero. Otherwise, the lines that follow are skipped until the

    matching #else or #endif is encountered.

    #errorDefines a compiling error message to be displayed.

    S12DP256S12DP256 mustmust bebe

    defineddefined, otherwiseotherwise a, a

  • 7/27/2019 C for Embedded

    137/173

    Slide 137

    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

    defineddefined,, otherwiseotherwise, a, a

    compilingcompiling errorerrorisis

    generatedgenerated..

    For Embedded Systems:

    -Multiplatform support in same source code

    -Source Code Flexibility (configuration atcompile time).

  • 7/27/2019 C for Embedded

    138/173

    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

    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)

  • 7/27/2019 C for Embedded

    139/173

    Slide 139

    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

    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 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 wakeupfrom 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

    MCU Description

    Clock Generator

    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

  • 7/27/2019 C for Embedded

    140/173

    Slide 140

    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

    Implementing UART in NITRON

    UART must be emulated using the TIMER

    In order to use the existing Hardware in the board, abidirectional UART will be implemented in

  • 7/27/2019 C for Embedded

    141/173

    Slide 141

    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

    bidirectional UART will be implemented in

    PTA0/TCH0.

    Note: care must be taken to avoid collisions.

    UART Frame Description

    The UART frame is composed by A START Bit (Dominant State) 8 Bits

  • 7/27/2019 C for Embedded

    142/173

    Slide 142

    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

    8 Bits

    A STOP Bit (Reccesive State)

    NITRON Timer Interface Module(TIM)

    TimerOverflow

  • 7/27/2019 C for Embedded

    143/173

    Slide 143

    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

    Input Capture

    OutputCompare

    Example Flowchart(1)

    Initial Config.

    Ask for Userinput

    Send String

    Point tot b t

  • 7/27/2019 C for Embedded

    144/173

    Slide 144

    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

    Send Byte

    inputnext byte

    Sending a Byte through UART

    The timer must be configured to Overflow every Bittime.

    Every Overflow Interruption a new bit is sent,

  • 7/27/2019 C for Embedded

    145/173

    Slide 145

    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

    y p ,

    including Start and Stop bit.

    Overflow every Bit time

    Example Flowchart(2)

    Send String

    Point tonext byte

    Send Byte

    Config. Timert OV /bitti

    Timer OV

    Bi C ?0 >8

  • 7/27/2019 C for Embedded

    146/173

    Slide 146

    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

    Finishedflag =1

    BitCount++

    Send StopBit

    Send NextBit

    Send Byte

    Last byteSent?

    to OV /bittime

    Send StartBit

    BitCount?

    FinishedFlag?

    Exit

    0

    1

  • 7/27/2019 C for Embedded

    147/173

    Slide 147

    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

    } DDRA|=0x01;if (!bitcount)

    { SET_TX_LOW;bitcount++; }

    else if (bitcount>=1;bitcount++;

    }

    else{ SET_TX_HIGH;bitcount=0;Flag=1; }...

    Send Start Bit, the byte and

    stop bit every interrupt

    Example Flowchart(3)

    Initial Config.

    Ask for Userinput

    Send String

    Point tonext byte

    Receive String

    Point tonext byte

  • 7/27/2019 C for Embedded

    148/173

    Slide 148

    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

    User inputreceived?

    Receive input

    N

    Send Byte

    Last byteSent?

    Exit

    N

    Y

    Receive Byte

    Receiving a Byte from UART

    In order to capture the start bit, the timer isconfigured as Input Capture.

    From then on, the timer is configured to overflow every Bittime

  • 7/27/2019 C for Embedded

    149/173

    Slide 149

    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

    BaudRateescaler

    FBUSBittime

    *Pr=

    Input

    Capture

    Overflow every Bit time

    Every Overflow Interruption the state of the pin is checkedand a new bit is placed.

    time

    Example Flowchart(4)

    Receive String

    Point tonext byte

    Timer OV

    BitCount?=8

    IC Int

    Config. Timer

    Receive Byte

    Config. Timert I C t

  • 7/27/2019 C for Embedded

    150/173

    Slide 150

    Freescale Semiconductor Confidential Proprietary. Freesc