AMF-ENT-T0001 C For Embedded Systems Programming

3y ago
72 Views
2 Downloads
1.71 MB
67 Pages
Last View : 15d ago
Last Download : 4m ago
Upload by : Milena Petrie
Transcription

November 11, 2010C for Embedded Systems ProgrammingAMF-ENT-T0001Derrick KlotzRegional Field Applications EngineerTMFreescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respectiveowners. Freescale Semiconductor, Inc. 2010.

AgendaC Programming for Freescale’s 8-bit S08with Guidelines Towards Migrating to 32-bit Architecture Knowing the environment Data types for embedded Compiler and linker.prm and map fileProgramming modelsChoosing the right data typeVariable typesStorage class modifiersProject Software Architecture Modular File OrganizationTips and considerationsFreescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respectiveowners. Freescale Semiconductor, Inc. 2010.TM

Embedded C versus Desktop CC for Embedded Systems ProgrammingTM

IntroductionThe ‘C’ Programming Language was originally developed for andimplemented 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 hardwareor system. This makes it easy for a user to write programs that will runwithout any changes on practically all machines. C is often called a middle-level computer language as it combines theelements of high-level languages with the functionalism of assemblylanguage. To produce the most efficient machine code, the programmer must notonly create an efficient high level design, but also pay attention to thedetailed implementation. Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respectiveowners. Freescale Semiconductor, Inc. 2010.TM

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 respectiveowners. Freescale Semiconductor, Inc. 2010.TM

Why Not C?These are some of the common issues that we encounter whenconsidering moving to the C programming language: Big and inefficient code generationFat 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 CData declaration in RAM and ROMCompiler optimizationsDifficulty writing Interrupt Service RoutinesMany of these concerns are the result of failing to acknowledge theavailable resource differences between embedded microcontrollers anddesktop computing environments Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respectiveowners. Freescale Semiconductor, Inc. 2010.TM

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 goodknowledge about: Architecture characteristicsThe tools for programming/debuggingData types native supportStandard librariesUnderstand the difference between simple code vs. efficient codeFreescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respectiveowners. Freescale Semiconductor, Inc. 2010.TM

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 efficientassembly 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 respectiveowners. Freescale Semiconductor, Inc. 2010.TM

Knowing the Environment – Compiler & LinkerC for Embedded Systems ProgrammingTM

Compiler’s little DetailsWhile choosing a compiler, you must remember thatthe Devil is in the details Nice features that can make a huge difference:Inline AssemblyInterrupt FunctionsAssembly Language GenerationStandard LibrariesStartup codeFreescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respectiveowners. Freescale Semiconductor, Inc. 2010.TM

Compiler Requirements.asm.asm.o.oCompiler.c.c .cpp.cpp .h.hlistinglistingGenerate ROM-able codeGenerate Optimized codeGenerate Re-entrant codeSupport for Different Members in Microcontroller FamilySupport for Different Memory ModelsFreescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respectiveowners. Freescale Semiconductor, Inc. 2010.TM

Compiler – Internal viewSource CodeFront EndCodeGeneratorBack EndObject filesFront End – reading th program: Identifies the language (C, C ) Prevents syntax errors Takes account of the preprocessing directives(macros and typedef resolutions, conditionalcompilation etc.)Code Generator: Generate ROM-able code Generate optimized code according tothe requested compiler options Generate re-entrant codeBack End: Support for different members inmicrocontroller family Support for different memory models90 % of the C programming issues are user related, so just aswith the compiler front end, when debugging an application,the first step is to carefully read the programFreescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respectiveowners. Freescale Semiconductor, Inc. 2010.TM

Overview Aftercompiling process is done, the linker works with the object filesgenerated in order to link the final application Thereare a couple of features that could be achieved by the linkerbecause of the nature of the process Thelinker parameter file could be used to do some useful tricks thatwill aid during the development process Informationprovided by the linker could be used within applicationsFreescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respectiveowners. Freescale Semiconductor, Inc. 2010.TM

Linker etdescription Merging segments of codeAllocate 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 respectiveowners. Freescale Semiconductor, Inc. 2010.TM

Target Description – S08Direct Page0x00FFMC9S08QE32MC9S08LL64Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respectiveowners. Freescale Semiconductor, Inc. 2010.TM

Target Description – S12X and ColdFireMC9S12XEP100Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respectiveowners. Freescale Semiconductor, Inc. 2010.ColdFire MCF51QE128TM

Memory ModelsMemory models have a meaning depending on the target usedModelHC(S)08TinyAll data, including stack, must fitinto the “zero page” pointers have8-bit addresses unless is explicitlyspecifiedSmallAll pointers and functions have16-bit addresses. Code and datashall be located in a 64Kbaddress spaceBankedThis model uses the MemoryManagement Unit (MMU),allowing the extension of programspace beyond the 64 dwith far16-bitsData and functions are accessed bydefault with 16- bit addresses, codeand data fit in 64 KB16-bitsunlessspecified16-bitsData is also accessed with 16-bitaddresses, functions are calledusing banked calls16-bits24-bitsBoth code and data are accessedusing paged conventions24-bits24-bitsWhat 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 respectiveowners. Freescale Semiconductor, Inc. 2010.TM

Tying Everything Together.asm.asm.c.c.cpp.cppCompiler .h.h.o.oLinkerlistinglistingAt this point the compiler doesn’tknow about the memorycharacteristics of the device used.s19.s19.map.mapTargetdescriptionThis means that the amount of memory useddoesn’t matter and the compiler will use apredefined convention to access data and functionsIf the compiler doesn’t know how the memory isarranged, how can we specify the callingconvention 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 respectiveowners. Freescale Semiconductor, Inc. 2010.TM

Far and Near Thekeywords far and near in C are intended to refer to data (either code orvariables) 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 specifythe calling convention. Far function calls can “cross”pages. Near function calls must stay in the same pagevoid main(void){MyFunction();}void far MyFunction (void);void near MyFunction (void);CALL MyFunction, PAGE(MyFunction)JSR MyFunction Applied to A variablevariablesdeclared near is considered by the compiler to be allocated in thememory section that can be accessed with direct addressing (first 256 bytes forS08, first 64 KB for S12 and S12X) accessing variables in the zero pagegenerates less code and executes faster since the address is only 8-bitsFreescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respectiveowners. Freescale Semiconductor, Inc. 2010.TM

Using “near” and “far”both, code and data, nearand far must be used inconjunction with a “#pragma”directive to specify the memorysection to place themLinker For For0x0000#pragma DATA SEG MY ZEROPAGEchar var;near var;DIRECT PAGE0x00FF0x0100#pragma DATA SEG DEFAULT RAMchar var;far var;RAMvariables #pragmaDATA SEG segment name Compiler Forcode #pragma ToCODE SEG segment name understand what thesegment name is we need tounderstand how the linkeridentifies the memorychar var;var ;char near var;var ;char far var;var ;Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respectiveowners. Freescale Semiconductor, Inc. 2010.ldhxinc#var,xincvarldhxinc#var,xTM

MC9S08LL64 Linker Parameter File (.prm)NAMESNAMES ENDEND /*/* CodeWarriorCodeWarrior willwill passpass allall thethe neededneeded filesfiles toto thethe linkerlinker byby commandcommand line.line. ButButSEGMENTS SECTION Defines the memory available in the MCU,providing full control over memory allocation.This is essentially a translation of the data sheet.SEGMENTSSEGMENTS /*/* HereHere allall RAM/ROMRAM/ROMZ RAMZ RAMRAMRAM/*/* unbankedunbanked FLASHFLASH ROMROM */*/ROMROMROM1ROM1ROM2ROM2ROM3ROM3/*/* bankedbanked FLASHFLASH ROMROM */*/PPAGE 0PPAGE 0PPAGE 0 1PPAGE 0 1PPAGE 2PPAGE 2/*/* PPAGE 1PPAGE 1/*/* PPAGE 3PPAGE 3ENDENDareasareas ofof thethe devicedevice areare listed.listed. UsedUsed inin PLACEMENTPLACEMENT below.below. */*/ READ WRITE0x0060READ WRITE0x0060 TOTO 0x00FF;0x00FF; READ WRITE0x0100READ WRITE0x0100 TOTO 0x0FFF;0x0FFF; READ ONLYREAD ONLYREAD ONLYREAD ONLYREAD ONLYREAD ONLYREAD ONLYREAD C0TOTOTOTOTOTOTOTO READ ONLYREAD ONLYREAD ONLYREAD ONLYREAD ONLYREAD ONLYREAD ONLYREAD ONLYREAD ONLYREAD /*/* PAGEPAGE partiallypartially containecontainePAGEPAGE alreadyalready containedcontained ininPAGEPAGE alreadyalready containedcontained ininPLACEMENTPLACEMENT /*/* HereHere allall predefinedpredefined andand useruser segmentssegments areare placedplaced intointo thethe SEGMENTSSEGMENTS defineddefined aboveaboveDEFAULT RAM,/*DEFAULT RAM,/* non-zeronon-zero pagepage variablesvariables */*/INTOINTO RAM;RAM;PLACEMENT SECTION Provides the ability to assign each section fromthe application to specific memory segments.The names identified in this section are used inthe source code, for example:#pragma DATA SEG MY ZEROPAGESTACKSIZE is one way to reserve a portion ofmemory for stack usage.PRESTART,PRESTART, STARTUP,STARTUP,ROM VAR,ROM VAR,STRINGS,STRINGS,VIRTUAL TABLE SEGMENT,VIRTUAL TABLE SEGMENT,NON BANKED,NON BANKED,DEFAULT ROM,DEFAULT ROM,COPYCOPY/*/*/*/*/*/*/*/*/*/*startupstartup codecode andand datadata structuresstructures */*/constantconstant variablesvariables */*/stringstring literalsliterals */*/C C virtualvirtual tabletable segmentsegment */*/runtimeruntime routinesroutines whichwhich mustmust notnot bebe bankedbanked */*/PAGED ROMPAGED ROM/*/* routinesroutines whichwhich cancan bebe bankedbanked */*/INTOINTO PPAGE 2,ROM1,ROM2,ROM3,PPAGE 0,PPAGE 0 1;PPAGE 2,ROM1,ROM2,ROM3,PPAGE 0,PPAGE 0 1;DATA ZEROPAGE,DATA ZEROPAGE,MY ZEROPAGEMY ZEROPAGE/*/* zerozero pagepage variablesvariables */*/INTOINTO Z RAM;Z RAM;/*/* copycopy downdown information:information: howhow toto initializeinitialize variablevariableINTOINTO ROM;ROM; /*/* ,ROM1,ROM2,ROM3:,ROM1,ROM2,ROM3: ToTo useuse ZE 0x1000x100VECTORVECTOR 00 StartupStartup /*/* ResetReset vector:vector: thisthis isis thethe defaultdefault entryentry pointpoint forfor 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 respectiveowners. Freescale Semiconductor, Inc. 2010.TM

Controlling the Location of the StackSEGMENTSZ RAMRAMEND READ WRITE READ WRITE0x0060 TO 0x00FF;0x0100 TO 0x0FFF;STACKSIZE 0x100SEGMENTSZ RAMRAMSTACK RAMEND READ WRITE READ WRITE READ WRITEPLACEMENTSSTACKENDINTOSTACK RAM;STACKTOP 0x0FFFor0x00000x0000Peripheral Registers0x005F0x0060variables#pragma DATA SEG MY ZEROPAGE#pragma DATA SEG DEFAULT RAM0x0060 TO 0x00FF;0x0100 TO 0x0EFF;0x0F00 TO 0x0FFF;0x00FF0x0100Peripheral RegistersDirect Pagevariables#pragma DATA SEG MY ZEROPAGE#pragma DATA SEG DEFAULT RAMvariablesstack0x005F0x00600x00FF0x0100Direct PagevariablesSPRAMRAM0x0F000x0FFFFreescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respectiveowners. Freescale Semiconductor, Inc. 2010.0x0FFFstackSPTM

ColdFire MCF51QE128 Linker Control File (.lcf)## SampleSample LinkerLinker CommandCommand FileFile forfor CodeWarriorCodeWarrior forfor ColdFireColdFire MCF51QE128MCF51QE128## MemoryMemory rangesrangesMEMORY SEGMENT Describes the available memoryMEMORYMEMORY IGINORIGINORIGINORIGIN LENGTHLENGTHLENGTH CTIONS {{## HeapHeap andand StackStack sizessizes definitiondefinitionheap size 0x0400;heap size0x0400;stack size 0x0400;stack size0x0400;SECTIONS SEGMENT Defines the contents of memory sections andglobal symbols######MCF51QE128MCF51QE128 DerivativeDerivative MemoryMemory mapmap definitionsdefinitions fromfrom linkerlinker commandcommand files:files:RAM ADDRESS,RAM ADDRESS, RAM SIZE,RAM SIZE, FLASH ADDRESS,FLASH ADDRESS, FLASH SIZEFLASH SIZE linkerlinkersymbolssymbols mustmust bebe defineddefined inin thethe linkerlinker commandcommand file.file.## 88 KbytesKbytes InternalInternalRAM ADDRESSRAM ADDRESS RAM SIZE RAM 02000;## 128128 KByteKByte InternalInternalFLASH ADDRESSFLASH ADDRESSFLASH SIZEFLASH SIZEFlashFlash MemoryMemory 0x00000000;0x00000000; 0x00020000;0x00020000; Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respectiveowners. Freescale Semiconductor, Inc. 2010.TM

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

The Map FileTARGET SECTION Names the target processor and memory modelFILE SECTION Lists the names of all files from which objectswere usedSTARTUP SECTION Lists the prestart code and the values used toinitialize the startup descriptor “ startupData”.The startup descriptor is listed member bymember with the initialization data at the righthand side of the member nameSECTION-ALLOCATION SECTION Lists those segments for which at least oneobject was TTARGET ocessor:: FreescaleProcessorFreescale HC08HC08MemoryMemory Model:Model: SMALLSMALLFileFile FormatFormat :: ELF\DWARFELF\DWARF 2.02.0Linker:: SmartLinkerLinkerSmartLinker V-5.0.39V-5.0.39 BuildBuild 10132,10132, MayMay 1313 LE in.objModel:Lang:main.objModel: SMALL,SMALL,Lang: : SMALL,SMALL,Lang: jModel: SMALL,SMALL,Lang: ARTUPSTARTUP tryEn

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 .

Related Documents:

Bruksanvisning för bilstereo . Bruksanvisning for bilstereo . Instrukcja obsługi samochodowego odtwarzacza stereo . Operating Instructions for Car Stereo . 610-104 . SV . Bruksanvisning i original

10 tips och tricks för att lyckas med ert sap-projekt 20 SAPSANYTT 2/2015 De flesta projektledare känner säkert till Cobb’s paradox. Martin Cobb verkade som CIO för sekretariatet för Treasury Board of Canada 1995 då han ställde frågan

service i Norge och Finland drivs inom ramen för ett enskilt företag (NRK. 1 och Yleisradio), fin ns det i Sverige tre: Ett för tv (Sveriges Television , SVT ), ett för radio (Sveriges Radio , SR ) och ett för utbildnings program (Sveriges Utbildningsradio, UR, vilket till följd av sin begränsade storlek inte återfinns bland de 25 största

Hotell För hotell anges de tre klasserna A/B, C och D. Det betyder att den "normala" standarden C är acceptabel men att motiven för en högre standard är starka. Ljudklass C motsvarar de tidigare normkraven för hotell, ljudklass A/B motsvarar kraven för moderna hotell med hög standard och ljudklass D kan användas vid

LÄS NOGGRANT FÖLJANDE VILLKOR FÖR APPLE DEVELOPER PROGRAM LICENCE . Apple Developer Program License Agreement Syfte Du vill använda Apple-mjukvara (enligt definitionen nedan) för att utveckla en eller flera Applikationer (enligt definitionen nedan) för Apple-märkta produkter. . Applikationer som utvecklas för iOS-produkter, Apple .

Anand Dr. Lakesh K. Anand Anaesthesia 3080 Bhandari Dr. Suksham Bhandari Anaesthesia 3080 . Gupta Dr. Anish Gupta ENT J.C. Dr. Kanchan J.C. ENT 3131 Jain Dr. B.R. Jain ENT 3080 Jain Dr. K. C. Jain ENT 3040 Kolwadkar Dr. Nandu Pramod Kolwadkar ENT 3040 Leuva Dr. Dipak Leuva ENT

senting 2-, 3- and 4-stage balanced amplifier designs. FEEDBACK DESIGNS MITEQ has established a technique to counter the deficiencies of the balanced approach and devel-oped an amplifier series based upon a feedback design approach. Our "F" series AMF amplifiers use this approach when its advantages fit the specific applications.

American Revolution Activity Book This Activity Book contains activity pages that accompany the lessons from the Unit 6 Teacher Guide. The activity pages are organized and numbered according to the lesson number and the order in which they are used within the lesson. For example, if there are two activity pages for Lesson 4, the first will be numbered 4.1 and the second 4.2. The Activity Book .