Transcription of 07 Introduction to Programming Embedded Systems
1 11 Introduction to ProgrammingEmbedded SystemsSebastian of Computer and Information ScienceUniversity of PennsylvaniaCSE480/CIS700S. Fischmeister2 Goals Rough understanding of the underlying hardware. Understand how to develop software for the lab Fischmeister3 What is An Embedded system ?A general-purpose definition of Embedded Systems is that they aredevices used to control, monitor or assist the operation ofequipment, machinery or plant. Embedded reflects the fact thatthey are an integral part of the system . In many cases, their embeddedness may be such that their presence is far fromobvious to the casual of Electrical Engineers (IEE)CSE480/CIS700S. Fischmeister4 For Us PIC18F2680o3,328 B RAMo64kB B EEPROMo5 MIPS @ 20 MHzoA/D converterso1x UARTo1x 8bit Timero3x 16bit Timer3 CSE480/CIS700S. Fischmeister5 Will use in the PICDEM2 board to Blink LEDs Control an LCD display Communicate via the serial linewith a PC Communicate via the CANprotocol with other microchips Drive a stepper motorCSE480/CIS700S.
2 Fischmeister6 Use it further to Control a modular robot:47 The HardwareCSE480/CIS700S. Fischmeister8A Microprocessor Introduced as a programmable replacement for logic-based circuitsin the 1970s. Advantages compared to logic-based circuits:oProvide functional upgrades ( , add new feature to machine toolafter deployment)oProvide easy maintenance upgrades ( , fix a bug in the cell phonevia an SMS firmware upgrade)oLess fragile ( , instead of hundreds discrete logic chips and wiringonly one microprocessor)oProtection of intellectual property (it is more difficult to copy softwareburnt in the on-chip memory than to check the part numbers and thewiring)5 CSE480/CIS700S. Fischmeister9 What makes a Microprocessor? ProcessoroAn arithmetic logic unit (ALU) for processing. MemoryoPermanent memory for keeping the program (= ROM)oVolatile memory for computation (= RAM)oRewritable permanent memory for logging, tuning, storing intermediatedata (= EEPROM) Connectivity to peripheralsoBinary outputs via single chip pinsoIntegrated asynchronous and synchronous serial interfaces such asUART, I2C, RS232, CANCSE480/CIS700S.
3 Fischmeister10 What makes a Microprocessor? TimersoEvent counting, input capture, real-time interrupt, watchdog timeroPulse-width modulation (PWM) Support for the analogue worldoAnalog-to-digital converter (ADC)oDigital-to-analog converter (DAC) Software debug support hardwareoJTAG6 CSE480/CIS700S. Fischmeister11 Meet the PIC18F2680 CSE480/CIS700S. Fischmeister12 Inside7 CSE480/CIS700S. Fischmeister13 Harvard Architecture Assign data and program instructions to different memory spaces. Each memory space has a separate bus. This allows:oDifferent timing, size, and structure for program instructions and access to data and partitioning of data and instructions (=> security) This makes it harder to program, because static data can be in theprogram space or in the data space. If the program space and the data space are incompatible, copyingdata is no longer a (<start>,len) Fischmeister14 Data Memory Memory layoutoInstructions in the PIC18are limited to 16 address the wholearea you would need 12bit => too is split into 256 Bbanks.
4 Only one is active. Register typesoGeneral-purposeregisters (GPR)oSpecial function registers(SFR) SFR control the MCU andthe Fischmeister15 Program Memory Return address stack (31-entries) forsubroutine calls and interruptprocessing. Reset vector (0000h) is the program-starting address after power-on ormanual reset. High priority int. vec (0008h) is thestarting address of this ISR with atmost 16B. Low priority int. vec (0018h) ditto butwithout a restriction. The user program follows the lowpriority int. vector Fischmeister16 Further Processor Information It has a long list of CPU registers (see specification).oNot important when Programming C, not irrelevant example STKPTR, INTCON*, STATUS PIC18 supports instruction pipelining with a depth of two stepsoInstruction fetchoInstruction execute917 The Programming ProcessCSE480/CIS700S. Fischmeister18 Overview of the Programming Process10 CSE480/CIS700S. Fischmeister19 Source file#include < >#define SHIFT_ME 3#define LOOP_FOREVER() while(1);void delay(unsigned int x) { while(x--);}void main (void) { unsigned int xx = 100%2 << SHIFT_ME; delay(xx); LOOP_FOREVER();}CSE480/CIS700S.
5 Fischmeister20 Pre-processor The pre-processor processes the source code before it continueswith the compilation stage. The pre-processoroResolves #define statements (constants, variable types, macros)oConcatenates #include files and source file into one large fileoProcesses #ifdef - #endif statementsoProcesses #if - #endif statements Specifically for Embedded Systems the pre-processor alsoprocesses vendor-specific directives (non-ANSI)o#pragma11 CSE480/CIS700S. Fischmeister21 Source File After the file ..void delay(unsigned int x) { while(x--);}void main (void) { unsigned int xx = 100%2 << 3; delay(xx); while(1);}CSE480/CIS700S. Fischmeister22 Compiler The compiler turns source code into machine code packaged in object files. Common file format are object file format (COFF) or the extended linkerformat (ELF). A cross-compiler produces object files that will then be linked for the targetinstead of the computer running the compiler (compare Linux, embeddedLinux, PIC18) Details about the compilation process and how the compiler works look atAho, Sethi, Ullman, Compilers: Principles, Techniques, and Tools, Addison-Wesley, 2006.
6 Practical approach in Embedded Systems :oTURN OFF ALL OPTIMIZATION !!oIn MPLAB: -Ou- -Ot- -Ob- -Op- -Or- -Od- -Opa- -On-12 CSE480/CIS700S. Fischmeister23 Linker The linker performs the followingoIt combines object files by merging object file sections..text section for code .data section for initialized global variables .bss section for uninitialized global variablesoIt resolves all unresolved symbols. External variables Function callsoReports errors about unresolved the start-up code (see next slide)oProvide symbolic debug information The linker produces a relocatable file. For standard operatingsystems with a dynamic loader, the processes is now finished - notso for Embedded Systems which need absolutely located Fischmeister24 Startup Code Startup is a small fragment of assembly code that prepares themachine for executing a program written in a high-level C in Unix it is called or (assembly)oFor PIC it is typically also an object file specified in the linker script. Tasks of the startup codeoDisable all interruptsoInitialize stack pointers for software stackoInitialize idata sectionsoZero all uninitialized data areas in data memory (ANSI standard)oCall loop: main(); goto loop;13 CSE480/CIS700S.
7 Fischmeister25 Relocator The relocator converts a relocatable binary into an absolutelylocated binary. The relocator is guided by a linker script that specifies:oProgram and data memory for the target partoStack size and locationoLogical sections used in the source code to place code and ata The relocator then produces an executable , that is ready fordeployment on the target or for the Fischmeister26 Linker File for MPLINK The linker directives fall into four basic categories. Since MPLINK combines the linker and relocator in one, there is no cleanseparation. Command line directivesoLIBPATH: Search path for library and object : Search path for linker command : Additional files to be : Additional linker command files to be included. Stack definition SIZE=allocSize [RAM=memName] Specifies the size and location of the software Fischmeister27 Linker File for MPLINK Memory region definition NAME=memName START=addr END=addr [PROTECTED][FILL=fillvalue] Specifies a ROM directive that is used for program code, initialized data values,constant data values, and external memory PROTECTED specifies that it can only by explicit request Useful for reflashing on the NAME=memName START=addr END=addr [PROTECTED] Specifies a RAM directive that is used for volatile memory.
8 Useful for correlated data tables. Logical sections definition NAME=secName [ROM=memName | RAM=memName] The code or data specified using the #pragma directive will then be located in thespecified memory Fischmeister28 Sample Linker FileLIBPATH . FILES FILES FILES CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED CODEPAGE NAME=page START=0x2A END=0x1 FFFF CODEPAGE NAME=eeprom START=0x20000 END=0x1 FFFFF PROTECTED CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED CODEPAGE NAME=devid START=0x3 FFFFE END=0x3 FFFFF PROTECTED CODEPAGE NAME=eedata START=0xF00000 END=0xF003FF PROTECTED ACCESSBANK NAME=accessram START=0x0 END=0x5F DATABANK NAME=gpr0 START=0x60 END=0xFF DATABANK NAME=gpr1 START=0x100 END=0x1FF ..DATABANK NAME=gpr13 START=0xD00 END=0xDFF DATABANK NAME=gpr14 START=0xE00 END=0xEFF ACCESSBANK NAME=accesssfr START=0xF60 END=0xFFF PROTECTED SECTION NAME=CONFIG ROM=config SECTION NAME=STARTUP ROM=vectors // Reset and interrupt vectors SECTION NAME=PROG ROM=page // main application code space SECTION NAME=INTHAND ROM=eeprom // Interrupt handlers SECTION NAME=DATTBL ROM=eeprom // Data tables STACK SIZE=0x100 RAM=gpr14 15 CSE480/CIS700S.
9 Fischmeister29 Map File after Linking and Relocating Open external Fischmeister30 Bin2 Hex The executable still has to be transferred to the target via a serialline (or even Ethernet with applicable boot loaders). For standard compliance, the binary is converted into an ASCII representation useful to PROM programmers and emulators. A number of standards exists, Intel HEX is line in an Intel HEX file contains one HEX Fischmeister31 Example Program after Bin2 Hex:020000040000FA:0600000094EF00F012007 5:02002A000000D4:04002C002A0EF66E34:1000 3000000EF76E000EF86E00010900F550656FB6:1 00040000900F550666F03E1656701D03DD00900F 6:10005000F550606F0900F550616F0900F55062 6F4F:1000600009000900F550E96E0900F550EA6 E090033:1000700009000900F550636F0900F550 646F09002D:100080000900F6CF67F0F7CF68F0F 8CF69F060C0ED:10009000F6FF61C0F7FF62C0F8 FF0001635302E1A1:1000A000645307E00900F55 0EE6E6307F8E2640759:1000B000F9D767C0F6FF 68C0F7FF69C0F8FF000115:0A00C0006507000E6 65 BBFD7120053:0600CA00D9 CFE6 FFE1 CFF3:1000D000D9 FFFD0 EDBCF02F0DB06FE0 EDBCF03F017:1000E00001E2DB060250031001E0 F3D7E552E7CF4F:1000F000D9FF1200D9 CFE6 FFE1 CFD9FF020EE126EA:10010000DE6 ADD6 ADECFE6 FFDDCFE6 FFDEDFE55249:10011000E552 FFD7020EE15C02E2E16AE552E16ED0:08012000E 552E7 CFD9FF120000:080128001 BEE00F02 BEE00F0CD:10013000F86A019C16EC00F07 AEC00F0 FDD7120092.
10 00000001FF32 MCC18 Compiler Extensions17 CSE480/CIS700S. Fischmeister33 Embedded Systems C Compilers Embedded Systems developers need more control over thegenerated file than traditional C to assembly instructions for high-performance functionsoSpecify memory area for code and dataoExtra functionality for saving memoryoDefine ISRsoDefine chip configuration Every compiler provides different extensions. GCC is available for a small set of targets, but not for too Fischmeister34 Data Types and Limits18 CSE480/CIS700S. Fischmeister35 Storage Classes AutooAn auto variable is stored in the software basic reentrancy for default for variables and , can be changed in the MPLAB settings to a different setting. StaticoA static variable is allocated but surely eat up your memory. RegisteroCan be ignored, because PIC18 only has Fischmeister36 Storage Classes ExternoDeclares a variable that is defined somewhere when splitting software in multiple out for the type and storage qualifiers!