AMF-ENT-T0001 C For Embedded Systems Programming

7m ago
13 Views
0 Downloads
1.71 MB
67 Pages
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