November 11, 2010

C for Embedded Systems Programming AMF-ENT-T0001

Derrick Klotz Regional Field Applications Engineer

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

C Programming for Freescale’s 8-bit S08 with Guidelines Towards Migrating to 32-bit Architecture

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

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

TM Introduction

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

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

► C is much more flexible than other high-level programming languages: • C is a structured language. • C is a relatively small language. • C has very loose data typing. • C easily supports low-level bit-wise data manipulation. • C is sometimes referred to as a “high-level assembly language”. ► When compared to assembly language programming: • Code written in C can be more reliable. • Code written in C can be more scalable. • Code written in C can be more portable between different platforms. • Code written in C can be easier to maintain. • Code written in C can be more productive. ► C retains the basic philosophy that programmers know what they are doing . ► C only requires that they state their intentions explicitly.

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

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

► These are some of the common issues that we encounter when considering moving to the C programming language:

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

► Many of these concerns are the result of failing to acknowledge the available resource differences between embedded and desktop computing environments

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

► Main characteristics of an Embedded programming environment: • Limited ROM. • Limited RAM. • Limited stack space. • Hardware oriented programming. • Critical timing (Interrupt Service Routines, tasks, …). • Many different pointer kinds (far / near / rom / uni / paged / …). • Special keywords and tokens (@, interrupt, tiny, …).

► Successful Embedded C programs must keep the code small and “tight”. In order to write efficient C code there has to be good knowledge about: • Architecture characteristics • The tools for programming/debugging • Data types native support • Standard libraries • Understand the difference between simple code vs. efficient code

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

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

• It is much easier to write good code in C which can be converted to efficient assembly language code than it is to write efficient assembly language code by hand.

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

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

TM Compiler’s little Details

While choosing a compiler, you must remember that the Devil is in the details

► Nice features that can make a huge difference:

 Inline Assembly  Interrupt Functions  Assembly Language Generation  Standard Libraries  Startup code

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

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

• Generate ROM-able code • Generate Optimized code • Generate Re-entrant code • Support for Different Members in Family • Support for Different Memory Models

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

Front End – reading th program: Source Code ► Identifies the language (C, C++ …) ► Prevents syntax errors ► Takes account of the preprocessing directives (macros and typedef resolutions, conditional compilation etc...) Front End Code Generator: ► Generate ROM-able code Code ► Generate optimized code according to Generator the requested compiler options ► Generate re-entrant code

Back End Back End: ► Support for different members in microcontroller family ► Support for different memory models

Object files 90 % of the C programming issues are user related, so just as with the compiler front end, when debugging an application, the first step is to carefully read the program

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

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

►There are a couple of features that could be achieved by the linker because of the nature of the process

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

►Information provided by the linker could be used within applications

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

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

Target description

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

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

Direct Page

0x00FF

MC9S08QE32 MC9S08LL64

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

MC9S12XEP100 ColdFire MCF51QE128

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

Memory models have a meaning depending on the target used

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

What about 32-bit architectures?

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

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

At this point the compiler doesn’t Target know about the memory description characteristics of the device used

This means that the amount of memory used doesn’t matter and the compiler will use a predefined convention to access data and functions

If the compiler doesn’t know how the memory is arranged, how can we specify the calling convention that shall be used?

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

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

► Applied to functions ► __far and __near specify the calling convention. Far function calls can “cross” pages. Near function calls must stay in the same page

void main(void) void __far MyFunction (void); void __near MyFunction (void); { MyFunction(); CALL MyFunction, PAGE(MyFunction) JSR MyFunction }

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

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

►For both, code and data, near Linker and far must be used in #pragma DATA_SEG MY_ZEROPAGE 0x0000 conjunction with a “#pragma ” char varnear; var ; DIRECT PAGE 0x00FF directive to specify the memory 0x0100 #pragma DATA_SEG DEFAULT_RAM section to place them char varfar; var ; RAM

►For variables •#pragma DATA_SEG Compiler ►For code char var ; var ++; •#pragma CODE_SEG ldhx #var inc ,x ►To understand what the char near var ; var ++; inc var segment name is we need to understand how the linker char far var ; identifies the memory var ++; ldhx #var inc ,x

Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM owners. © Freescale Semiconductor, Inc. 2010. MC9S08LL64 Linker Parameter File (.prm)

NAMESNAMES ENDEND/* /* CodeWarriorCodeWarrior willwill passpass allall thethe neededneeded filesfiles toto ththee linkerlinker byby commandcommand line.line. ButBut

SEGMENTSSEGMENTS/* /* HereHere allall RAM/ROMRAM/ROM areasareas ofof thethe devicedevice areare listed.listed. UsedUsed inin PLACEMENTPLACEMENT below.below. */*/ Z_RAMZ_RAM == READ_WRITEREAD_WRITE0x0060 0x0060 TOTO0x00FF; 0x00FF; RAMRAM == READ_WRITEREAD_WRITE0x0100 0x0100 TOTO0x0FFF; 0x0FFF; /*/* unbankedunbanked FLASHFLASH ROMROM */*/ ROMROM == READ_ONLYREAD_ONLY0x18A1 0x18A1 TOTO0x7FFF; 0x7FFF; SEGMENTS SECTION ROM1ROM1 == READ_ONLYREAD_ONLY0x103C 0x103C TOTO0x17FF; 0x17FF; • Defines the memory available in the MCU, ROM2ROM2 == READ_ONLYREAD_ONLY0xC000 0xC000 TOTO0xFFAB; 0xFFAB; providing full control over memory allocation. ROM3ROM3 == READ_ONLYREAD_ONLY0xFFC0 0xFFC0 TOTO0xFFD1; 0xFFD1; This is essentially a translation of the data sheet. /*/* bankedbanked FLASHFLASH ROMROM */*/ PPAGE_0PPAGE_0 == READ_ONLYREAD_ONLY0x008002 0x008002 TOTO0x00903B; 0x00903B; /*/* PAGEPAGE partiallypartially containecontaine PPAGE_0_1PPAGE_0_1 == READ_ONLYREAD_ONLY0x009800 0x009800 TOTO0x0098A0; 0x0098A0; PPAGE_2PPAGE_2 == READ_ONLYREAD_ONLY0x028000 0x028000 TOTO0x02BFFF; 0x02BFFF; /*/* PPAGE_1PPAGE_1 == READ_ONLYREAD_ONLY 0x018000x0180000 TOTO 0x01BFFF;0x01BFFF; PAGEPAGE alreadyalready containedcontained inin /*/* PPAGE_3PPAGE_3 == READ_ONLYREAD_ONLY 0x038000x0380000 TOTO 0x03BFFF;0x03BFFF; PAGEPAGE alreadyalready containedcontained inin ENDEND

PLACEMENTPLACEMENT/* /* HereHere allall predefinedpredefined andand useruser segmentssegments areare placedplaced intointo thethe SEGMENTSSEGMENTS defineddefined aboveabove DEFAULT_RAM,DEFAULT_RAM, /*/* non-zeronon-zero pagepage variablesvariables */*/ INTOINTORAM; RAM;

_PRESTART,_PRESTART, STARTUP,STARTUP, /*/* startupstartup codecode andand datadata structuresstructures */*/ ROM_VAR,ROM_VAR, /*/* constantconstant variablesvariables */*/ PLACEMENT SECTION STRINGS,STRINGS, /*/* stringstring literalsliterals */*/ • Provides the ability to assign each section from VIRTUAL_TABLE_SEGMENT,VIRTUAL_TABLE_SEGMENT, /*/* C++C++ virtualvirtual tabletable segmentsegment */*/ NON_BANKED, /* runtime routines which must not be banked */ the application to specific memory segments. NON_BANKED, /* runtime routines which must not be banked */ DEFAULT_ROM,DEFAULT_ROM, The names identified in this section are used in COPYCOPY /*/* copycopy downdown information:information: howhow toto initializeinitialize variablvariablee the source code, for example: INTOINTOROM; ROM; /*/* ,ROM1,ROM2,ROM3:,ROM1,ROM2,ROM3: ToTo useuse "ROM1,ROM2,ROM"ROM1,ROM2,ROM #pragma DATA_SEG MY_ZEROPAGE PAGED_ROMPAGED_ROM /*/* routinesroutines whichwhich cancan bebe bankedbanked */*/ INTOINTOPPAGE_2,ROM1,ROM2,ROM3,PPAGE_0,PPAGE_0_1; PPAGE_2,ROM1,ROM2,ROM3,PPAGE_0,PPAGE_0_1;

_DATA_ZEROPAGE,_DATA_ZEROPAGE, /*/* zerozero pagepage variablesvariables */*/ MY_ZEROPAGEMY_ZEROPAGE INTOINTOZ_RAM; Z_RAM; END STACKSIZE is one way to reserve a portion of END memory for stack usage. STACKSIZESTACKSIZE0x100 0x100 VECTORVECTOR0 0 _Startup_Startup /*/* ResetReset vector:vector: thisthis isis thethe defaultdefault entryentry pointpoint foforr anan application.application. */*/

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

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

PLACEMENT SSTACK INTO STACK_RAM; END

STACKSIZE 0x100 STACKTOP 0x0FFF

0x0000 or 0x0000 Peripheral Registers Peripheral Registers 0x005F 0x005F 0x0060 0x0060 #pragma DATA_SEG MY_ZEROPAGE variables Direct Page #pragma DATA_SEG MY_ZEROPAGE variables Direct Page

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

0x0F00 stack 0x0FFF 0x0FFF SP

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

## SampleSample LinkerLinker CommandCommand FileFile forfor CodeWarriorCodeWarrior forfor CoColdFireldFire MCF51QE128MCF51QE128

## MemoryMemory rangesranges

MEMORYMEMORY{ { MEMORY SEGMENT codecode (RX)(RX) :: ORIGINORIGIN= = 0x00000410,0x00000410, LENGTHLENGTH= = 0x0001FBF00x0001FBF0 • Describes the available memory userramuserram (RWX) (RWX) :: ORIGINORIGIN= = 0x00800000,0x00800000, LENGTHLENGTH= = 0x000020000x00002000 }}

SECTIONSSECTIONS{ {

## HeapHeap andand StackStack sizessizes definitiondefinition ___heap_size___heap_size = = 0x0400;0x0400; ___stack_size___stack_size = = 0x0400;0x0400;

## MCF51QE128MCF51QE128 DerivativeDerivative MemoryMemory mapmap definitionsdefinitions fromfrom linkerlinker commandcommand files:files: ## ___RAM_ADDRESS,___RAM_ADDRESS, ___RAM_SIZE,___RAM_SIZE, ___FLASH_ADDRESS,___FLASH_ADDRESS, _____FLASH_SIZE_FLASH_SIZE linkerlinker # symbols must be defined in the linker command file. SECTIONS SEGMENT # symbols must be defined in the linker command file. • Defines the contents of memory sections and ## 88 KbytesKbytes InternalInternal SRAMSRAM global symbols ___RAM_ADDRESS___RAM_ADDRESS == 0x00800000;0x00800000; ___RAM_SIZE___RAM_SIZE == 0x00002000;0x00002000;

## 128128 KByteKByte Internal Internal FlashFlash MemoryMemory ___FLASH_ADDRESS___FLASH_ADDRESS == 0x00000000;0x00000000; ___FLASH_SIZE___FLASH_SIZE == 0x00020000;0x00020000;

………………

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

The Map file

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

****************************************************************************************************************************************************************************************** TARGETTARGET SECTIONSECTION ------TARGET SECTION ProcessorProcessor :: FreescaleFreescale HC08HC08 Memory Model: SMALL • Names the target processor and memory model Memory Model: SMALL FileFile FormatFormat :: ELF\DWARFELF\DWARF 2.02.0 LinkerLinker :: SmartLinkerSmartLinker V-5.0.39 V-5.0.39 BuildBuild 10132,10132, MayMay 1313 20102010

****************************************************************************************************************************************************************************************** FILEFILE SECTIONSECTION FILE SECTION ------• Lists the names of all files from which objects main.objmain.obj Model:Model: SMALL,SMALL, Lang:Lang: ANSI-CANSI-C start08.obj Model: SMALL, Lang: ANSI-C were used start08.obj Model: SMALL, Lang: ANSI-C mc9s08ll64.objmc9s08ll64.obj Model:Model: SMALSMALL,L, Lang:Lang: ANSI-CANSI-C

****************************************************************************************************************************************************************************************** STARTUPSTARTUP SECTIONSECTION STARTUP SECTION ------• Lists the prestart code and the values used to EntryEntry point:point: 0x191C0x191C (_Startup)(_Startup) initialize the startup descriptor “_startupData”. _startupData_startupData is is allocatedallocated atat 0x19250x1925 andand usesuses 66 BytesBytes externextern structstruct _tagStartup _tagStartup { { The startup descriptor is listed member by unsignedunsigned nofZeroOutnofZeroOut 1 1 member with the initialization data at the right _Range_Range pZeroOutpZeroOut 0x60 0x60 1 1 hand side of the member name _Copy_Copy *toCopyDownBeg*toCopyDownBeg 0x1945 0x1945 }} _startupData;_startupData;

****************************************************************************************************************************************************************************************** SECTION-ALLOCATIONSECTION-ALLOCATION SECTIONSECTION SectionSection NameName SizeSize TypeType FromFrom ToTo Segment Segment ------.init.init 132132 RR 0x180x18A1A1 0x19240x1924 ROM ROM SECTION-ALLOCATION SECTION .startData.startData 14 14 RR 0x19250x1925 0x19320x1932 ROMROM • Lists those segments for which at least one .text.text 1818 RR 0x190x193333 0x19440x1944 ROM ROM .copy.copy 22 RR 0x190x194545 0x19460x1946 ROM ROM object was allocated MY_ZEROPAGEMY_ZEROPAGE 11 R/WR/W 0x0x6060 0x600x60 Z_RAM Z_RAM .abs_section_0.abs_section_0 11 N/IN/I 00x0x0 0x00x0 .absSeg0 .absSeg0 .abs_section_1.abs_section_1 11 N/IN/I 00x1x1 0x10x1 .absSeg1 .absSeg1 .abs_section_2.abs_section_2 11 N/IN/I 00x2x2 0x20x2 .absSeg2 .absSeg2 .abs_section_3.abs_section_3 11 N/IN/I 00x3x3 0x30x3 .absSeg3 .absSeg3

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

LinkerLinker****************************************************************************************************** ParameterParameter filefile ************************************************************************************ TARGETTARGET SECTIONSECTION ------PLACEMENTPLACEMENTProcessorProcessor/* /* HereHere : :all all FreescaleFreescale predefinedpredefined HC08HC08 andand useruser segmentssegments areare placedplaced intointo thethe SEGMENTSSEGMENTS defineddefined above.above. */*/ .init = _PRESTART DEFAULT_RAM,DEFAULT_RAM,MemoryMemory Model:Model: SMALL SMALL /*/* non-zeronon-zero pagepage variablesvariables */*/ .startData = STARTUP FileFile FormatFormat :: ELF\DWARFELF\DWARF 2.02.0 INTOINTORAM; RAM; .text = DEFAULT_ROM LinkerLinker :: SmartLinkerSmartLinker V-5.0.39 V-5.0.39 BuildBuild 10132,10132, MayMay 1313 20102010 _PRESTART, /* startup code */ .copy = COPY _PRESTART, /* startup code */ STARTUP,STARTUP,****************************************************************************************************** /*/* startupstartup datadata structures******************************************structures****************************************** */*/ .stack = SSTACK ROM_VAR,ROM_VAR,FILEFILE SECTIONSECTION /*/* constantconstant variablesvariables */*/ .data = DEFAULT_RAM STRINGS,STRINGS,------/*/* stringstring literalsliterals------*/*/ .common = DEFAULT_RAM VIRTUAL_TABLE_SEGMENT,VIRTUAL_TABLE_SEGMENT,main.objmain.obj /*/* C++C++Model:Model: virtualvirtual SMALL,SMALL, tabletable segment segment Lang:Lang: */*/ ANSI-CANSI-C NON_BANKED,NON_BANKED,start08.objstart08.obj /*/* runtimeruntime Model:Model: routinesrout SMALSMALinesL,L, which which must must Lang:Lang: notnot ANSI-CANSI-C bebe bankedbanked */*/ DEFAULT_ROM,DEFAULT_ROM,mc9s08ll64.objmc9s08ll64.obj Model:Model: SMALSMALL,L, Lang:Lang: ANSI-CANSI-C COPYCOPY /*/* copycopy downdown information:information: howhow toto initializeinitialize variablvariableses */*/ ******************************************************************************************************INTOINTOROM; ROM; /*/* ,ROM1,ROM2,ROM3:,ROM1,ROM2,ROM3:************************************************************************************ ToTo useuse "ROM1,ROM2,ROM3""ROM1,ROM2,ROM3" asas Every variable allocated with an absolute STARTUPSTARTUP SECTIONSECTION syntax of the kind: PAGED_ROMPAGED_ROM------/*/* routinesroutines whichwhich------cancan bebe bankedbanked */*/ type variablename @0xABCD ; EntryEntry point:point: 0x191C0x191C (_Startup)(_Startup) INTOINTOPPAGE_2,ROM1,ROM2,ROM3,PPAGE_0,PPAGE_0_1; PPAGE_2,ROM1,ROM2,ROM3,PPAGE_0,PPAGE_0_1; will have a “section” and a “segment” assigned _startupData_startupData is is allocatedallocated atat 0x19250x1925 andand usesuses 66 BytesBytes _DATA_ZEROPAGE,_DATA_ZEROPAGE,externextern structstruct _tagStartup _tagStartup { { /*/* zerozero pagepage variablesvariables */*/ for its own. MY_ZEROPAGEMY_ZEROPAGEunsignedunsigned nofZeroOut nofZeroOut 1 1 INTOINTOZ_RAM; Z_RAM; ENDEND _Range_Range pZeroOutpZeroOut 0x60 0x60 1 1 All MCU registers are declared this way. This _Copy_Copy *toCopyDownBeg*toCopyDownBeg 0x1945 0x1945 is why we have one abs_section for every }} _startupData;_startupData; MCU register. ****************************************************************************************************************************************************************************************** SECTION-ALLOCATIONSECTION-ALLOCATION SECTIONSECTION SectionSection NameName SizeSize TypeType FromFrom ToTo Segment Segment ------.init.init 132132 RR 0x180x18A1A1 0x19240x1924 ROM ROM SECTION-ALLOCATION SECTION .startData.startData 14 14 RR 0x19250x1925 0x19320x1932 ROMROM • Lists those segments for which at least one .text.text 1818 RR 0x190x193333 0x19440x1944 ROM ROM .copy.copy 22 RR 0x190x194545 0x19460x1946 ROM ROM object was allocated MY_ZEROPAGEMY_ZEROPAGE 11 R/WR/W 0x0x6060 0x600x60 Z_RAM Z_RAM .abs_section_0.abs_section_0 11 N/IN/I 00x0x0 0x00x0 .absSeg0 .absSeg0 .abs_section_1.abs_section_1 11 N/IN/I 00x1x1 0x10x1 .absSeg1 .absSeg1 .abs_section_2.abs_section_2 11 N/IN/I 00x2x2 0x20x2 .absSeg2 .absSeg2 .abs_section_3.abs_section_3 11 N/IN/I 00x3x3 0x30x3 .absSeg3 .absSeg3

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

.stack.stack 256256 R/WR/W 0x10x10000 0x1FF0x1FF RAM RAM .vectSeg188_vect.vectSeg188_vect 22 RR 0xFF0xFFFEFE 0xFFFF0xFFFF .vectSeg188 .vectSeg188 SECTION-ALLOCATION SECTION Summary: SummarySummary ofof sectionsection sizessizes perper sectionsection type:type: READ_ONLY = Flash READ_ONLYREAD_ONLY (R):(R): A8A8 (dec:(dec: 168)168) READ_WRITE = RAM READ_WRITEREAD_WRITE (R/W):(R/W): 101101 (dec:(dec: 257)257) NO_INIT = Registers NO_INITNO_INIT (N/I):(N/I): CDCD (dec:(dec: 205)205)

****************************************************************************************************************************************************************************************** VECTOR-ALLOCATION SECTION VECTOR-ALLOCATIONVECTOR-ALLOCATION SECTIONSECTION Address InitValue InitFunction • Lists each vector’s address and to where it Address InitValue InitFunction ------points (ie., interrupt service routine) 0xFFFE0xFFFE 0x191C0x191C _Startup_Startup

****************************************************************************************************************************************************************************************** OBJECT-ALLOCATIONOBJECT-ALLOCATION SECTIONSECTION NameName ModuleModule AddrAddr hSize hSize dSizedSize Ref Ref SectionSection RLIBRLIB ------MODULE:MODULE: ---- main.obj main.obj ------PROCEDURES: PROCEDURES: mainmain 19331933 1212 18 18 11 .text.text -- VARIABLES: VARIABLES: nn 6060 11 1 1 22 MY_ZEROPAGEMY_ZEROPAGE MODULE:MODULE: ---- start08.obj start08.obj ------PROCEDURES: PROCEDURES: OBJECT-ALLOCATION SECTION loadByteloadByte 18A118A1 EE 1414 55 .init .init • Contains the name, address and size of every InitInit 18AF18AF 6D6D 109109 11 .init.init _Startup 191C 9 9 0 .init allocated object and groups them by module _Startup 191C 9 9 0 .init -- VARIABLES: VARIABLES: _startupData_startupData 19251925 66 66 44 .startData.startData -- LABELS: LABELS: __SEG_END_SSTACK__SEG_END_SSTACK 200200 00 0 0 11 MODULE:MODULE: ---- mc9s08ll64.obj mc9s08ll64.obj ------PROCEDURES: PROCEDURES: -- VARIABLES: VARIABLES: _PTAD_PTAD 00 11 1 1 00 .abs_section_0.abs_section_0 _PTADD_PTADD 11 11 1 1 00 .abs_section_1.abs_section_1 _PTBD_PTBD 22 11 1 1 00 .abs_section_2.abs_section_2 _PTBDD_PTBDD 33 11 1 1 00 .abs_section_3.abs_section_3

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

UNUSED-OBJECTS SECTION: ****************************************************************************************************************************************************************************************** • Shows all of the variables declared but not used UNUSED-OBJECTSUNUSED-OBJECTS SECTIONSECTION ------after the optimizer did its job ------****************************************************************************************************************************************************************************************** COPYDOWNCOPYDOWN SECTIONSECTION ------COPYDOWN SECTION ------ROM-ADDRESS: ROM-ADDRESS: 0x19450x1945 ------SIZE SIZE 22 ------• Lists each pre-initialized variable and its value FillingFilling bytesbytes insertedinserted 00000000

****************************************************************************************************************************************************************************************** OBJECT-DEPENDENCIESOBJECT-DEPENDENCIES SECTIONSECTION ------OBJECT-DEPENDENCIES SECTION InitInit USESUSES _startupData_startupData loadByte loadByte • Lists for every function and variable that uses _Startup_Startup USESUSES __SEG_END_SSTACK__SEG_END_SSTACK IniInitt mainmain other global objects the names of these global mainmain USESUSES _PTCD_PTCD _PTCDD_PTCDD _SRS_SRS nn objects ****************************************************************************************************************************************************************************************** DEPENDENCYDEPENDENCY TREETREE ****************************************************************************************************************************************************************************************** mainmain andand _Startup_Startup GroupGroup DEPENDENCY TREE || +-+- main main • Using a tree format, shows all detected || dependencies between functions. Overlapping +-+- _Startup _Startup local variables are also displayed at their || defining function +-+- Init Init || || || +-+- loadByte loadByte || +-+- main main (see(see above)above)

****************************************************************************************************************************************************************************************** STATISTICS SECTION STATISTICSTATISTIC SECTIONSECTION ------• Delivers information like the number of bytes of ExeFile:ExeFile: code in the application NumberNumber ofof blocksblocks toto bebe downloaded:downloaded: 4 4 TotalTotal sizesize ofof allall blocksblocks toto bebe downloaded:downloaded: 168168

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

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

MAPFILE FILE SEC_ALLOC OBJ_UNUSED COPYDOWN

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

Specifier Description Specifier Description

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

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

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

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

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

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

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

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

bytebyte n;n; bytebyte var;var;

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

//// initializeinitialize LEDsLEDs PTCDPTCD == 0xFF;0xFF; //// setset defaultdefault valuevalue forfor PorPortt C C PTCDDPTCDD == 0b00111100;0b00111100; //// setset LEDLED portport pinspins asas outpoutputsuts

forfor(;;) (;;) {{ __RESET_WATCHDOG();__RESET_WATCHDOG(); /*/* feedsfeeds thethe dogdog */*/

forfor(n=0;;n++) (n=0;;n++) {{ PTCD++;PTCD++; //// blinkblink LEDsLEDs var++;var++; }}

}} /*/* looploop foreverforever */*/ /*/* pleaseplease makemake suresure thatthat youyou nevernever leaveleave mainmain */*/ }}

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

#pragma#pragmaDATA_SEG DATA_SEG MY_ZEROPAGEMY_ZEROPAGE bytebyte nearnear n;n;

#pragma#pragmaDATA_SEG DATA_SEG DEFAULT_RAMDEFAULT_RAM bytebyte farfar varvar = = 7;7;

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

//// initializeinitialize LEDsLEDs PTCDPTCD == 0xFF;0xFF; //// setset defaultdefault valuevalue forfor PorPortt C C PTCDDPTCDD == 0b00111100;0b00111100; //// setset LEDLED portport pinspins asas outpoutputsuts

forfor(;;) (;;) {{ __RESET_WATCHDOG();__RESET_WATCHDOG(); /*/* feedsfeeds thethe dogdog */*/

forfor(n=0;;n++) (n=0;;n++) {{ PTCD++;PTCD++; //// blinkblink LEDsLEDs var++;var++; }}

}} /*/* looploop foreverforever */*/ /*/* pleaseplease makemake suresure thatthat youyou nevernever leaveleave mainmain */*/ }}

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

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

1. Stack Pointer/Frame setup  2. Global Memory initialized to zero (Zero-Out)  3. Global Variables initialized (Copy-Down) 4. Global Constructor calls (C++) 5. Call main()  Start all Static and Global variables at zero

asm { clra ; get clear data ldhx #MAP_RAM_last ; point to last RAM location stx MAP_RAM_first ; first RAM location is non-zero txs ; initialize SP ClearRAM: psha ; clear RAM location There is a simpler way … tst MAP_RAM_first ; check if done bne ClearRAM ; loop back if not }

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

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

TM Variables

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

Formats Default Default value range Type available format min max with option -T char (unsigned) 8 bit 0 255 8 bit, 16 bit, 32 bit signed char 8 bit -128 127 8 bit, 16 bit, 32 bit unsigned char 8 bit 0 255 8 bit, 16 bit, 32 bit Note: signed short 16 bit -32768 32767 8 bit, 16 bit, 32 bit All scalar types are signed by default, unsigned short 16 bit 0 65535 8 bit, 16 bit, 32 bit except char enum (signed) 16 bit -32768 32767 8 bit, 16 bit, 32 bit signed int 16 bit -32768 32767 8 bit, 16 bit, 32 bit Size of int type is machine dependant unsigned int 16 bit 0 65535 8 bit, 16 bit, 32 bit signed long 32 bit -2147483648 2147483647 8 bit, 16 bit, 32 bit unsigned long 32 bit 0 4294967295 8 bit, 16 bit, 32 bit signed long long 32 bit -2147483648 2147483647 8 bit, 16 bit, 32 bit unsigned long long 32 bit 0 4294967295 8 bit, 16 bit, 32 bit

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

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

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

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

• The C preferred data type is ‘int’

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

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

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

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

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

unsigned int A; ldhx @A incw A addq.l #1,_A(a5) inc 1,x bne Lxx inc ,x Lxx:

unsigned long A; ldhx @A ldd A:2 addq.l #1,_A(a5) jsr _LINC ldx A jsr _LINC std A:2 stx A

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

► There are 3 Rules for data type selection: • Use the smallest possible type to get the job done • Use unsigned type if possible • Use casts within expressions to reduce data types to the minimum required

► Use typedefs to get fixed size • Change according to compiler and system • Code is invariant across machines • Used when a fixed number of bits is needed for values

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

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

► Avoid basic types (‘char’, ‘int’, ‘short’, ‘long’) in application code ► But how?

Basic CodeWarrior Stationary: Processor Expert CodeWarrior Stationary: Another Popular Technique:

/* Types definition */ #ifndef __PE_Types_H typedef unsigned char __uint8__; typedef unsigned char byte; #define __PE_Types_H typedef unsigned char __byte__; typedef unsigned int word; typedef signed char __int8__; typedef unsigned long dword; /* Types definition */ typedef unsigned short __uint16__; typedef unsigned long dlong[2]; typedef unsigned char bool ; typedef signed short __int16__; typedef unsigned char byte; typedef unsigned long __uint32__; typedef unsigned int word; typedef signed long __int32__; typedef unsigned long dword; typedef unsigned long dlong[2];

// other stuff

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

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

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

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

► Global • Global storage and global scope

► Static • Global storage and local scope

► Local • Local storage and local scope

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

►The following keywords are used with variable declarations, to specify specific needs or conditions associated with the storage of the variables in memory:

static volatile const

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

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

► When applied to variables, static has two primary functions:

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

► For Embedded Systems:

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

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

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

Before entering This is part of the ANSI C MyFunction() startup “copy down” procedure. the 1st time, myVar = 0

FILE1.c FILE2.c

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

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

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

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

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

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

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

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

stuff.c

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

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

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

► In embedded systems, there are two ways this can happen: • Via an interrupt service routine • As a consequence of hardware action

► It is considered to be very good practice to declare all peripheral registers in embedded devices as volatile

► The standard C solution: #define PORTA (*(( volatile unsigned char *) (0x0000))) This macro defines PORTA to be the content of a pointer to an unsigned char. This is portable over any architecture but not easily readable. And it doesn’t take advantage of the S08’s bit manipulation capabilities.

► The CodeWarrior solution: extern volatile PTADSTR _PTAD @0x00000000;

Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective TM owners. © Freescale Semiconductor, Inc. 2010. 47 CodeWarrior Declaration for PORT A Data Register

/*** PTAD - Port A Data Register; 0x00000000 ***/ typedef union { byte Byte; struct { byte PTAD0 :1; /* Port A Data Register Bit 0 */ byte PTAD1 :1; /* Port A Data Register Bit 1 */ byte PTAD2 :1; /* Port A Data Register Bit 2 */ byte PTAD3 :1; /* Port A Data Register Bit 3 */ byte PTAD4 :1; /* Port A Data Register Bit 4 */ byte PTAD5 :1; /* Port A Data Register Bit 5 */ byte PTAD6 :1; /* Port A Data Register Bit 6 */ byte PTAD7 :1; /* Port A Data Register Bit 7 */ } Bits; } PTADSTR; extern volatile PTADSTR _PTAD @0x00000000; #define PTAD _PTAD.Byte #define PTAD_PTAD0 _PTAD.Bits.PTAD0 #define PTAD_PTAD1 _PTAD.Bits.PTAD1 #define PTAD_PTAD2 _PTAD.Bits.PTAD2 #define PTAD_PTAD3 _PTAD.Bits.PTAD3 #define PTAD_PTAD4 _PTAD.Bits.PTAD4 #define PTAD_PTAD5 _PTAD.Bits.PTAD5 #define PTAD_PTAD6 _PTAD.Bits.PTAD6 #define PTAD_PTAD7 _PTAD.Bits.PTAD7

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

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

unsigned char PORTA @ 0x00; unsigned char SCI1S1 @ 0x1C; unsigned char value;

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

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

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

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

► Some compilers create a genuine variable in RAM to hold the const variable. Upon system software initialization, the “read-only” value is copied into RAM. On RAM-limited systems, this can be a significant penalty.

► Compilers for Embedded Systems, like CodeWarrior™, store const variables in ROM (or Flash). However, the “read-only ” variable is still treated as a variable and accessed as such, although the compiler protects const definitions from inadvertent writing. Each const variable must be declared with an initialization value.

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

For Embedded Systems: • Parameters defined const are allocated in ROM space • The compiler protects ‘const’ definitions of inadvertent writing • Express the intended usage of a parameter

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

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

#pragma#pragmaDATA_SEG DATA_SEG MY_ZEROPAGEMY_ZEROPAGE bytebyte nearnear n;n;

#pragma#pragmaDATA_SEG DATA_SEG DEFAULT_RAMDEFAULT_RAM bytebyte farfar varvar = = 7;7;

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

//// initializeinitialize LEDsLEDs PTCDPTCD == 0xFF;0xFF; //// setset defaultdefault valuevalue forfor PorPortt C C PTCDDPTCDD == 0b00111100;0b00111100; //// setset LEDLED portport pinspins asas outpoutputsuts

forfor(;;) (;;) {{ __RESET_WATCHDOG();__RESET_WATCHDOG(); /*/* feedsfeeds thethe dogdog */*/

forfor(n=0;;n++) (n=0;;n++) {{ PTCD++;PTCD++; //// blinkblink LEDsLEDs var++;var++; }}

}} /*/* looploop foreverforever */*/ /*/* pleaseplease makemake suresure thatthat youyou nevernever leaveleave mainmain */*/ }}

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

#pragma#pragmaDATA_SEG DATA_SEG DEFAULT_RAMDEFAULT_RAM bytebyte farfar var;var;

voidvoid main(main(void)void ) {{ #pragma#pragmaDATA_SEG DATA_SEG MY_ZEROPAGEMY_ZEROPAGE staticstatic bytebyte nearnear n;n;

EnableInterrupts;EnableInterrupts;

//// initializeinitialize LEDsLEDs PTCDPTCD == 0xFF;0xFF; //// setset defaultdefault valuevalue forfor PorPortt C C PTCDDPTCDD == 0b00111100;0b00111100; //// setset LEDLED portport pinspins asas outpoutputsuts

forfor(;;) (;;) {{ __RESET_WATCHDOG();__RESET_WATCHDOG(); /*/* feedsfeeds thethe dogdog */*/

forfor(n=0;;n++) (n=0;;n++) {{ PTCD++;PTCD++; //// blinkblink LEDsLEDs var++;var++; }}

}} /*/* looploop foreverforever */*/ /*/* pleaseplease makemake suresure thatthat youyou nevernever leaveleave mainmain */*/ }}

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

Reset

_EntryPoint() Hardware Initialization

_Startup() Software Initialization

main() Main Control Loop Reset_Watchdog

Software Events Hardware Events

Subroutine_1() Interrupt_Service_Routine_1()

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

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

TM Project Software Architecture

Application Project 1

Files developed completely Application by the user Files

main.c

Translation

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

Foundation

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

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

Application Project 1 Project 2

Files developed completely Application Application by the user Copy Files Files

main.c main.c

Translation

Files that connect the Project Project Change Project Project available target peripherals Globals Interrupts Globals Interrupts with how the application needs to use them Project1_mcu.h Project2_mcu.h Project1_mcu.c Project2_mcu.c

Foundation

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

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

main.c system.h

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

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

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

/**************************************************/***********************************************************************************************\*********************************************\ ** ProjectProject NameName \**************************************************\***********************************************************************************************/*********************************************/ system.h #ifndef#ifndef _SYSTEM_H_ _SYSTEM_H_ #define#define _SYSTEM_H__SYSTEM_H_

#include#include /*/* forfor EnableInteEnableInterruptsrrupts macro macro */*/ #include#include "derivative.h""derivative.h" /*/* includeinclude periphperipheraleral declarationsdeclarations */*/

/**************************************************/***********************************************************************************************\*********************************************\ ** PublicPublic typetype definitionsdefinitions \**************************************************\***********************************************************************************************/*********************************************/

/**************************************************/******************************************************************************************************************************************** **** **** VariableVariable typetype definition:definition: BIT_FIELDBIT_FIELD */*/ typedeftypedef union union {{ bytebyte Byte;Byte; structstruct { { bytebyte _0_0 :1;:1; bytebyte _1_1 :1;:1; bytebyte _2_2 :1;:1; bytebyte _3_3 :1;:1; bytebyte _4_4 :1;:1; bytebyte _5_5 :1;:1; bytebyte _6_6 :1;:1; bytebyte _7_7 :1;:1; }} Bit;Bit; }} BIT_FIELD;BIT_FIELD;

/**************************************************/******************************************************************************************************************************************** **** **** VariableVariable typetype definition:definition: twordtword */*/ typedeftypedef union union {{ unsignedunsigned shortshort Word;Word; structstruct {{ bytebyte hi;hi; bytebyte lo;lo; }} Byte;Byte; }} tword;tword;

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

/**************************************************/***********************************************************************************************\*********************************************\ ** ProjectProject includesincludes \**************************************************\***********************************************************************************************/*********************************************/ system.h #include#include "mma845x.h""mma845x.h" //// MMA845xQMMA845xQ macromacros s #include#include "iic.h""iic.h" //// IICIIC macrosmacros #include#include "sci.h""sci.h" //// SCISCI macrosmacros #include#include "spi.h""spi.h" //// SPISPI macrosmacros #include#include "terminal.h""terminal.h" //// TerminalTerminal interinterfaceface macrosmacros

/**************************************************/***********************************************************************************************\*********************************************\ ** PublicPublic macrosmacros \**************************************************\***********************************************************************************************/*********************************************/ /**************************************************/******************************************************************************************************************************************** **** **** GeneralGeneral SystemSystem ControlControl **** **** 0x18020x1802 SOPT1SOPT1 SystemSystem OptionsOptions RegisterRegister 1 1 **** 0x18030x1803 SOPT2SOPT2 SystemSystem OptionsOptions RegisterRegister 2 2 **** 0x18080x1808 SPMSC1SPMSC1 SystemSystem PowerPower ManagementManagement StatuStatuss andand ControlControl 1 1 RegisterRegister **** 0x18090x1809 SPMSC2SPMSC2 SystemSystem PowerPower ManagementManagement StatuStatuss andand ControlControl 2 2 RegisterRegister **** 0x180B0x180B SPMSC3SPMSC3 SystemSystem PowerPower ManagementManagement StatuStatuss andand ControlControl 3 3 RegisterRegister **** 0x180E0x180E SCGC1SCGC1 SystemSystem ClockClock GatingGating ControlControl 11 RegisterRegister **** 0x180F0x180F SCGC2SCGC2 SystemSystem ClockClock GatingGating ControlControl 22 RegisterRegister **** 0x000F0x000F IRQSCIRQSC InterruptInterrupt PinPin RequestRequest StatusStatus andand ControlControl RegisterRegister */*/

#define#define init_SOPT1init_SOPT1 0b010000100b01000010 /*/* 1100001U1100001U == resetreset **** |||xxx|||||xxx|| **** |||||| |+--|+-- RSTPE RSTPE =0=0 :: RESETRESET pinpin functionfunction disableddisabled **** |||||| +---+--- BKGDPE BKGDPE =1=1 :: BackgroundBackground DebugDebug pinpin enabledenabled **** ||+------||+------STOPE STOPE =0=0 :: StopStop ModeMode disableddisabled **** |+------|+------COPT COPT =1=1 :: LongLong COPCOP timeouttimeout periodperiod selectedselected **** +------+------COPE COPE =0=0 :: COPCOP WatchdogWatchdog timertimer disableddisabled */*/

#define#define init_SOPT2init_SOPT2 0b000000100b00000010 /*/* 0000000000000000 == resetreset **** |x||x||||x||x||| **** || |||| ||+--||+-- ACIC1 ACIC1 =0=0 :: ACMP1ACMP1 outputoutput notnot connectedconnected toto TPM1CH0TPM1CH0 inputinput **** || |||| |+---|+--- IICPS IICPS =1=1 :: SDASDA onon PTB6;PTB6; SCLSCL onon PTB7PTB7 **** || |||| +----+---- ACIC2 ACIC2 =0=0 :: ACMP2ACMP2 outputoutput notnot connectedconnected toto TPM2CH0TPM2CH0 inputinput **** || |+------|+------TPM1CH2PS TPM1CH2PS =0=0 :: TPM1CH2TPM1CH2 onon PTA6PTA6 **** || +------+------TPM2CH2PS TPM2CH2PS =0=0 :: TPM2CH2TPM2CH2 onon PTA7PTA7 **** +------+------COPCLKS COPCLKS =0=0 :: COPCOP clockclock sourcesource isis internalinternal 1kHz1kHz referencereference */*/

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

/**************************************************/******************************************************************************************************************************************** **** **** PortPort I/OI/O system.h **** **** 0x00000x0000 PTADPTAD PortPort AA DataData RegisterRegister **** 0x00010x0001 PTADDPTADD PortPort AA DataData DirectionDirection RegisteRegister r **** 0x00020x0002 PTBDPTBD PortPort BB DataData RegisterRegister **** 0x00030x0003 PTBDDPTBDD PortPort BB DataData DirectionDirection RegisteRegister r **** 0x00040x0004 PTCDPTCD PortPort CC DataData RegisterRegister **** 0x00050x0005 PTCDDPTCDD PortPort CC DataData DirectionDirection RegisteRegister r **** 0x00060x0006 PTDDPTDD PortPort DD DataData RegisterRegister **** 0x00070x0007 PTDDDPTDDD PortPort DD DataData DirectionDirection RegisteRegister r **** 0x18400x1840 PTAPEPTAPE PortPort AA PullPull EnableEnable RegisterRegister **** 0x18410x1841 PTASEPTASE PortPort AA SlewSlew RateRate EnableEnable RegisRegisterter **** 0x18420x1842 PTADSPTADS PortPort AA DriveDrive StrengthStrength SelectiSelectionon RegisterRegister **** 0x18440x1844 PTBPEPTBPE PortPort BB PullPull EnableEnable RegisterRegister **** 0x18450x1845 PTBSEPTBSE PortPort BB SlewSlew RateRate EnableEnable RegisRegisterter **** 0x18460x1846 PTBDSPTBDS PortPort BB DriveDrive StrengthStrength SelectiSelectionon RegisterRegister **** 0x18480x1848 PTCPEPTCPE PortPort CC PullPull EnableEnable RegisterRegister **** 0x18490x1849 PTCSEPTCSE PortPort CC SlewSlew RateRate EnableEnable RegisRegisterter **** 0x184A0x184A PTCDSPTCDS PortPort CC DriveDrive StrengthStrength SelectiSelectionon RegisterRegister */*/

#define#define init_PTADinit_PTAD 0b10000000 0b10000000 //// 0000000000000000 == resetreset #define#define init_PTADDinit_PTADD 0b10000000 0b10000000 //// 0000000000000000 == resetreset #define#define init_PTAPEinit_PTAPE 0b00000110 0b00000110 //// 0000000000000000 == resetreset #define#define init_PTASEinit_PTASE 0b00000000 0b00000000 //// 0000000000000000 == resetreset #define#define init_PTADSinit_PTADS 0b00000000 0b00000000 //// 0000000000000000 == resetreset /*/* |||||||||||||||| **** |||||||+--|||||||+-- X_OUT X_OUT **** ||||||+---||||||+--- Y_OUT Y_OUT -- MMA MMA845x845x INT1INT1 **** |||||+----|||||+---- Z_OUT Z_OUT -- MMA MMA845x845x INT2INT2 **** ||||+-----||||+----- DIS_MCU DIS_MCU **** |||+------|||+------BKGD BKGD **** ||+------||+------RESET RESET **** |+------|+------EXTRA_AD EXTRA_AD **** +------+------LEDB_BB LEDB_BB -- Blu Bluee LEDLED cathodecathode */*/ #define#define INT1_IS_ACTIVEINT1_IS_ACTIVE (PTAD_PTAD1(PTAD_PTAD1 ==== 0)0) #define#define LED_BlueOnLED_BlueOn (PTAD_PTAD7 (PTAD_PTAD7 == 0)0) #define#define LED_BlueOffLED_BlueOff (PTAD_PTAD7 (PTAD_PTAD7 == 1)1)

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

#define#define init_PTCDinit_PTCD 0b10111011 0b10111011 //// 0000000000000000 == resetreset #define#define init_PTCDDinit_PTCDD 0b10110111 0b10110111 system.h //// 0000000000000000 == resetreset #define#define init_PTCPEinit_PTCPE 0b01000000 0b01000000 //// 0000000000000000 == resetreset #define#define init_PTCSEinit_PTCSE 0b00000000 0b00000000 //// 0000000000000000 == resetreset #define#define init_PTCDSinit_PTCDS 0b00000000 0b00000000 //// 0000000000000000 == resetreset /*/* |||||||||||||||| **** |||||||+--|||||||+-- LEDG_BB LEDG_BB -- Gre Greenen LEDLED cathodecathode **** ||||||+---||||||+--- SLEEP SLEEP -- MMA MMA845x845x CSCS **** |||||+----|||||+---- G_SEL_2 G_SEL_2 -- MMA MMA845x845x SA0SA0 **** ||||+-----||||+----- G_SEL_1 G_SEL_1 **** |||+------|||+------LEDR_BB LEDR_BB -- Red Red LEDLED cathodecathode **** ||+------||+------BUZZER_BB BUZZER_BB **** |+------|+------PUSH_BUTTON PUSH_BUTTON **** +------+------LED_OB LED_OB -- Yel Yellowlow LEDLED cathodecathode */*/ #define#define LED_GreenOnLED_GreenOn (PTCD_PTCD0 (PTCD_PTCD0 == 0)0) #define#define LED_GreenOffLED_GreenOff (PTCD_PTCD0 (PTCD_PTCD0 == 1)1) #define#define SENSOR_SHUTDOWNSENSOR_SHUTDOWN (PTCD_PTCD1(PTCD_PTCD1 == 00) ) #define#define SENSOR_ACTIVESENSOR_ACTIVE (PTCD_PTCD1(PTCD_PTCD1 == 11) ) #define#define LED_RedOnLED_RedOn (PTCD_PTCD4 (PTCD_PTCD4 == 0)0) #define#define LED_RedOffLED_RedOff (PTCD_PTCD4 (PTCD_PTCD4 == 1)1) #define#define LED_YellowOnLED_YellowOn (PTCD_PTCD7 (PTCD_PTCD7 == 0)0) #define#define LED_YellowOffLED_YellowOff (PTCD_PTCD7 (PTCD_PTCD7 == 1)1) #define#define SA0_PINSA0_PIN (PTCD_PTCD2)(PTCD_PTCD2)

/**************************************************/***********************************************************************************************\*********************************************\ ** PublicPublic memorymemory declarationsdeclarations \**************************************************\***********************************************************************************************/*********************************************/

/**************************************************/***********************************************************************************************\*********************************************\ ** PublicPublic prototypesprototypes \**************************************************\***********************************************************************************************/*********************************************/

#endif#endif /* /* _SYSTEM_H__SYSTEM_H_ */*/

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

/**************************************************/***********************************************************************************************\*********************************************\ ** ProjectProject namename ** sci.h ** Filename:Filename: sci.hsci.h ** \**************************************************\***********************************************************************************************/*********************************************/ #ifndef#ifndef _SCI_H_ _SCI_H_ #define#define _SCI_H__SCI_H_

/**************************************************/***********************************************************************************************\*********************************************\ ** PublicPublic macrosmacros \**************************************************\***********************************************************************************************/*********************************************/

#define#define BUFFER_RX_SIZEBUFFER_RX_SIZE 1010 #define#define BUFFER_TX_SIZEBUFFER_TX_SIZE 200200

/**************************************************/******************************************************************************************************************************************** **** **** SerialSerial CommunicationsCommunications InterfaceInterface (SCI)(SCI) **** **** 0x00200x0020 SCIBDHSCIBDH SCISCI BaudBaud RateRate RegisterRegister HighHigh **** 0x00210x0021 SCIBDLSCIBDL SCISCI BaudBaud RateRate RegisterRegister LowLow **** 0x00220x0022 SCIC1SCIC1 SCISCI ControlControl RegisterRegister 1 1 **** 0x00230x0023 SCIC2SCIC2 SCISCI ControlControl RegisterRegister 2 2 **** 0x00240x0024 SCIS1SCIS1 SCISCI StatusStatus RegisterRegister 1 1 **** 0x00250x0025 SCIS2SCIS2 SCISCI StatusStatus RegisterRegister 2 2 **** 0x00260x0026 SCIC3SCIC3 SCISCI ControlControl RegisterRegister 3 3 **** 0x00270x0027 SCIDSCID SCISCI DataData RegisterRegister **** **** SCISCI targettarget baudratebaudrate = = 115.2k115.2k **** MCUMCU busbus frequencyfrequency == 9.216MHz9.216MHz **** **** SCISCI BaudBaud RateRate RegisterRegister == 5 5 **** **** SCISCI baudratebaudrate = = busbus // (16(16 ** BR)BR) **** == 9.216MHz9.216MHz // (16(16 ** 5)5) **** == 115.2k115.2k */*/

#define#define init_SCIBDHinit_SCIBDH 0x00 0x00 #define#define init_SCIBDLinit_SCIBDL 0x05 0x05

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

#define#define ASCII_BSASCII_BS 0x080x08 #define#define ASCII_LFASCII_LF 0x0A0x0A sci.h #define#define ASCII_CRASCII_CR 0x0D0x0D #define#define ASCII_DELASCII_DEL 0x7F0x7F

/**************************************************/***********************************************************************************************\*********************************************\ ** PublicPublic typetype definitionsdefinitions \**************************************************\***********************************************************************************************/*********************************************/

/**************************************************/***********************************************************************************************\*********************************************\ ** PublicPublic memorymemory declarationsdeclarations \**************************************************\***********************************************************************************************/*********************************************/

#pragma#pragma DATA_SEG DATA_SEG MY_ZEROPAGEMY_ZEROPAGE

externextern bytebyte BufferRx[BUFFER_RX_SIZE];BufferRx[BUFFER_RX_SIZE];

#pragma#pragma DATA_SEG DATA_SEG DEFAULTDEFAULT

/**************************************************/***********************************************************************************************\*********************************************\ ** PublicPublic prototypesprototypes \**************************************************\***********************************************************************************************/*********************************************/

voidvoid SCIControlInitSCIControlInit (void); (void); voidvoid SCISendStringSCISendString (byte (byte *pStr);*pStr); voidvoid SCI_CharOut(byteSCI_CharOut(byte data); data); voidvoid SCI_NibbOut(byteSCI_NibbOut(byte data); data); voidvoid SCI_ByteOut(byteSCI_ByteOut(byte data); data); voidvoid SCI_putCRLFSCI_putCRLF (void); (void); bytebyte SCI_CharIn(void);SCI_CharIn(void); bytebyte SCI_ByteIn(void);SCI_ByteIn(void); voidvoid SCI_s12dec_OutSCI_s12dec_Out (tword(tword data); data); voidvoid SCI_s8dec_Out(twordSCI_s8dec_Out(tword data);data); voidvoid SCI_s12int_OutSCI_s12int_Out (tword(tword data); data); voidvoid SCI_s12frac_OutSCI_s12frac_Out (tword(tword data); data);

bytebyte isnumisnum (byte (byte data);data); bytebyte ishexishex (byte (byte data);data); bytebyte tohextohex (byte (byte data);data); voidvoid hex2ASCIIhex2ASCII (byte(byte data,data, byte*byte* ptr);ptr);

#endif#endif /* /* _SCI_H__SCI_H_ */*/

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

/**************************************************/***********************************************************************************************\*********************************************\ ** ProjectProject NameName ** terminal.h ** Filename:Filename: terminal.hterminal.h ** \**************************************************\***********************************************************************************************/*********************************************/ #ifndef#ifndef _TERMINAL_H_ _TERMINAL_H_ #define#define _TERMINAL_H__TERMINAL_H_

/**************************************************/***********************************************************************************************\*********************************************\ ** PublicPublic macrosmacros \**************************************************\***********************************************************************************************/*********************************************/

/**************************************************/***********************************************************************************************\*********************************************\ ** PublicPublic typetype definitionsdefinitions \**************************************************\***********************************************************************************************/*********************************************/

/**************************************************/***********************************************************************************************\*********************************************\ ** PublicPublic memorymemory declarationsdeclarations \**************************************************\***********************************************************************************************/*********************************************/

/**************************************************/***********************************************************************************************\*********************************************\ ** PublicPublic prototypesprototypes \**************************************************\***********************************************************************************************/*********************************************/

voidvoid TerminalInitTerminalInit (void); (void); voidvoid ProcessTerminalProcessTerminal (void); (void); voidvoid OutputTerminalOutputTerminal (byte (byte BlockID,BlockID, bytebyte *ptr);*ptr);

#endif#endif /* /* _TERMINAL_H__TERMINAL_H_ */*/

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

/**************************************************/***********************************************************************************************\*********************************************\ ** ProjectProject NameName ** terminal.c ** Filename:Filename: terminal.cterminal.c ** \**************************************************\***********************************************************************************************/*********************************************/

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

/**************************************************/***********************************************************************************************\*********************************************\ ** PrivatePrivate macrosmacros \**************************************************\***********************************************************************************************/*********************************************/

/**************************************************/***********************************************************************************************\*********************************************\ ** PrivatePrivate typetype definitionsdefinitions \**************************************************\***********************************************************************************************/*********************************************/

/**************************************************/***********************************************************************************************\*********************************************\ ** PrivatePrivate prototypesprototypes \**************************************************\***********************************************************************************************/*********************************************/

/**************************************************/***********************************************************************************************\*********************************************\ ** PrivatePrivate memorymemory declarationsdeclarations \**************************************************\***********************************************************************************************/*********************************************/

#pragma#pragma DATA_SEG DATA_SEG MY_ZEROPAGEMY_ZEROPAGE

#pragma#pragma DATA_SEG DATA_SEG DEFAULT_RAMDEFAULT_RAM

/**************************************************/***********************************************************************************************\*********************************************\ ** PublicPublic functionsfunctions \**************************************************\***********************************************************************************************/*********************************************/

/**************************************************/***********************************************************************************************\*********************************************\ ** PrivatePrivate functionsfunctions \**************************************************\***********************************************************************************************/*********************************************/

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