16
Compilers, Compilers, assemblers, linkers, assemblers, linkers, loaders, and loaders, and programming issues programming issues For embedded Systems For embedded Systems Chris Gregg Chris Gregg January 29, 2009 January 29, 2009

Compilers programmingembedded

Embed Size (px)

DESCRIPTION

kjj

Citation preview

Page 1: Compilers programmingembedded

Compilers, assemblers, Compilers, assemblers, linkers, loaders, and linkers, loaders, and programming issuesprogramming issues

For embedded Systems For embedded Systems Chris GreggChris Gregg

January 29, 2009January 29, 2009

Page 2: Compilers programmingembedded

is programming for an is programming for an embedded system different?embedded system different?

•SpeedSpeed

•SizeSize

•CorrectnessCorrectness

•PortabilityPortability

•Real-Time or notReal-Time or not

•Other Hardware ConsiderationsOther Hardware Considerations

Page 3: Compilers programmingembedded

How does one actually program How does one actually program an embedded system?an embedded system?

• Has this changed over the years? Has there been Has this changed over the years? Has there been significant progress?significant progress?• How does Ford program their Fuel Injection System How does Ford program their Fuel Injection System

computer?computer?• How does GE program their Microwave Ovens (and was How does GE program their Microwave Ovens (and was

this different 30 years ago?)this different 30 years ago?)• How does Nikon program their digital SLR cameras?How does Nikon program their digital SLR cameras?• How does Apple/RIM/Microsoft program their How does Apple/RIM/Microsoft program their

iPhone/Blackberry/SmartPhone?iPhone/Blackberry/SmartPhone?• How does a hobbyist program a Pic microprocessor?How does a hobbyist program a Pic microprocessor?• How does NASA program (and How does NASA program (and reprogramreprogram) the Mars ) the Mars

Rover?Rover?• How did James Gosling at Sun want his set-top boxes How did James Gosling at Sun want his set-top boxes

programmed?programmed?• (How was the original 128K Macintosh programmed?) (How was the original 128K Macintosh programmed?)

Page 4: Compilers programmingembedded

Programming Programming LanguagesLanguages

Machine Code

Assembly Code

Higher Level Languages

Fixed Rom, Ram, Firmware

CompiledC, nesC, C+

+, Ada, Forth, etc.

Interpreted (?)

Perl, Python, Javascript

MarkupHTML, XML

Java All Eventually End up as Machine Code

Page 5: Compilers programmingembedded

So you’ve chosen C. So you’ve chosen C. Let’s Go!Let’s Go!

What does a programmer need to know about What does a programmer need to know about programming for an embedded system?programming for an embedded system?

• She’d better know about the hardware.She’d better know about the hardware.• PurposePurpose• How data flows (to include getting the program onto How data flows (to include getting the program onto

the system, I/O), how to interface with sensors, the system, I/O), how to interface with sensors, actuators, etc.actuators, etc.

• Whether there is an operating system, and how it Whether there is an operating system, and how it runs programsruns programs

• Limitations: memory, speed, upgradability Limitations: memory, speed, upgradability (firmware?)(firmware?)

• How are hardware errors handled? (think Mars Rover)How are hardware errors handled? (think Mars Rover)• Plan on debugging hardware issues…Plan on debugging hardware issues…

(Not so fast.)

Page 6: Compilers programmingembedded

So you’ve chosen C. So you’ve chosen C. Let’s Go!Let’s Go!

What does a programmer need to know about What does a programmer need to know about programming for an embedded system?programming for an embedded system?

• She’d better know about the software tools related to She’d better know about the software tools related to programming for the specific hardware.programming for the specific hardware.• Is there a compiler/linker/assembler? (hopefully!)Is there a compiler/linker/assembler? (hopefully!)• How is the memory addressed, what bit-level knowledge How is the memory addressed, what bit-level knowledge

of the hardware is necessary, how does one access pins, of the hardware is necessary, how does one access pins, etc.?etc.?

• How will debugging be managed?How will debugging be managed?• How will testing be done?How will testing be done?• What licensing needs to be organized (this can be tricky!)What licensing needs to be organized (this can be tricky!)• Does the software need to be portable? (using C is Does the software need to be portable? (using C is

probably probably going to help you—see the second point above).going to help you—see the second point above).

(Not so fast.)

Page 7: Compilers programmingembedded

The Embedded Software The Embedded Software Development ProcessDevelopment Process

Barr, M. & Massa, A. Oram, A. (ed.) Programming Embedded Systems in C and C++, 2nd Edition. O'Reilly & Associates, Inc., 2006 , p.55

Page 8: Compilers programmingembedded

The ToolsThe Tools

• Compiler: Translates human readable code into assembly Compiler: Translates human readable code into assembly language or opcodes for a particular processor (or language or opcodes for a particular processor (or possibly into machine-independent opcodes a la Java). possibly into machine-independent opcodes a la Java). Produces an object file.Produces an object file.

• Assembler: Translates assembly language into opcodes Assembler: Translates assembly language into opcodes (it is really a compiler, too). Also produces an object file.(it is really a compiler, too). Also produces an object file.

• Linker: Organizes the object files, necessary libraries, Linker: Organizes the object files, necessary libraries, and other data and produces a relocatable file. and other data and produces a relocatable file.

• Locator: Takes the relocatable file and information about Locator: Takes the relocatable file and information about the memory of the system and produces an executable.the memory of the system and produces an executable.

(By the way: gcc takes care of (By the way: gcc takes care of all all of these functions at of these functions at once)once)

Page 9: Compilers programmingembedded

The Tools: Embedded The Tools: Embedded System SpecificsSystem Specifics

All of the tools run on the host computer, not the embedded All of the tools run on the host computer, not the embedded computer.computer.

• Compiler: Has to know about the specific hardware (except in Compiler: Has to know about the specific hardware (except in very trivial cases). Should be able to optimize for size.very trivial cases). Should be able to optimize for size.

• Assembler: Produces “startup code”; not inserted Assembler: Produces “startup code”; not inserted automatically as in general purpose computers (i.e., the automatically as in general purpose computers (i.e., the programmer needs to compile it independently).programmer needs to compile it independently).

• Linker: Needs the correct libraries (open source c libraries, Linker: Needs the correct libraries (open source c libraries, such as such as newlibnewlib, are available)., are available).

• Locator: Needs programmer input for information about Locator: Needs programmer input for information about memory.memory.

Bottom Line: There can be a lot of extra work for the programmer, although certain systems (e.g. Pic programming) tools can automate most of it.

Page 10: Compilers programmingembedded

Moving the program onto the Moving the program onto the embedded systemembedded system

• Remember, the program is written (and possibly Remember, the program is written (and possibly run in an emulator) on a host computer, but it run in an emulator) on a host computer, but it still needs to get onto the embedded system.still needs to get onto the embedded system.

• Methods:Methods:• Build/burn the program into the hardware Build/burn the program into the hardware

(firmware or other flash memory)(firmware or other flash memory)• Bootloader: a bootloader resides on the embedded Bootloader: a bootloader resides on the embedded

system and facilitates loading programs onto the system and facilitates loading programs onto the system.system.

• Debug Monitor: The debug monitor is a more Debug Monitor: The debug monitor is a more robust program on an embedded system that helps robust program on an embedded system that helps with debugging and other chores, and can include with debugging and other chores, and can include a bootloader as well.a bootloader as well.

Page 11: Compilers programmingembedded

DebuggingDebugging

• Debugging embedded systems can be facilitated Debugging embedded systems can be facilitated with a Debug Monitor, or through a remote with a Debug Monitor, or through a remote debugger on the host computer. A serial link is debugger on the host computer. A serial link is normally set up, and the debugger acts more or less normally set up, and the debugger acts more or less like a general purpose debugger.like a general purpose debugger.

• Emulators can be used to test the system without Emulators can be used to test the system without utilizing the actual hardware (but this has many utilizing the actual hardware (but this has many caveats, and nothing beats testing on the real caveats, and nothing beats testing on the real system).system).

• Software Simulators allow the programmer to debug Software Simulators allow the programmer to debug completely on the host system, which can be completely on the host system, which can be quicker and can allow faster code turnaround.quicker and can allow faster code turnaround.

• When it comes down to it, an oscilloscope and a When it comes down to it, an oscilloscope and a multimeter can be your best friend for debugging.multimeter can be your best friend for debugging.

Page 12: Compilers programmingembedded

Final ThoughtsFinal Thoughts

• Programming for embedded systems has come Programming for embedded systems has come a long way since the days of toggle switches, a long way since the days of toggle switches, but it still involves greater programmer but it still involves greater programmer involvement in the hardware aspect of the involvement in the hardware aspect of the system.system.

• There are many software tools built for There are many software tools built for embedded systems, and many embedded embedded systems, and many embedded system hardware designers include debugging system hardware designers include debugging tools with the hardware.tools with the hardware.

• You had probably learn C (and a fair amount of You had probably learn C (and a fair amount of assembly) to program embedded systems.assembly) to program embedded systems.

Page 13: Compilers programmingembedded

Final ThoughtsFinal Thoughts

• Is this a necessary topic for an Embedded Is this a necessary topic for an Embedded Systems class?Systems class?

• YesYes• You’ve got to get your program onto the system You’ve got to get your program onto the system

some how, and flipping toggle switches doesn’t some how, and flipping toggle switches doesn’t put food on the table any more.put food on the table any more.

• Hardware only does what you tell it to, via Hardware only does what you tell it to, via software.software.

• Discussion of the tradeoffs necessary to Discussion of the tradeoffs necessary to program an embedded system versus a GP program an embedded system versus a GP system is a necessity.system is a necessity.

Page 14: Compilers programmingembedded

6 Questions6 Questions

1)1) Discuss the steps needed to get a program Discuss the steps needed to get a program from source code to executable in an from source code to executable in an embedded system, and the differences embedded system, and the differences between similar steps on a general purpose between similar steps on a general purpose system.system.

2)2) What trade-offs are necessary when thinking What trade-offs are necessary when thinking about how you are going to program for an about how you are going to program for an embedded system?embedded system?

3)3) Why is C generally considered a better Why is C generally considered a better language to program in for embedded systems language to program in for embedded systems than, say, Java or Perl? than, say, Java or Perl?

Page 15: Compilers programmingembedded

6 Questions6 Questions

4)4) Name three different ways you might debug Name three different ways you might debug the code for your embedded system.the code for your embedded system.

5)5) Why is it imperative that an embedded Why is it imperative that an embedded system programmer know the details of the system programmer know the details of the hardware that she is programming for? hardware that she is programming for? Compare this to someone writing a Java Compare this to someone writing a Java applet for a web site.applet for a web site.

6)6) Talk about the role firmware plays in Talk about the role firmware plays in developing software for an embedded developing software for an embedded system.system.

Page 16: Compilers programmingembedded

BibliographyBibliography

• Barr, M. & Massa, A. Oram, A. Barr, M. & Massa, A. Oram, A. (ed.)(ed.)Programming Embedded Systems in C and C++, Programming Embedded Systems in C and C++, 2nd Edition. 2nd Edition. O'Reilly & Associates, Inc., O'Reilly & Associates, Inc., 20062006

• Wolfe, M. How compilers and tools differ for Wolfe, M. How compilers and tools differ for embedded systems. embedded systems. 20052005. . http://www.pgroup.com/lit/pgi_article_cases.pdf

• Gay, D.; Levis, P.; von Behren, R.; Welsh, M.; Gay, D.; Levis, P.; von Behren, R.; Welsh, M.; Brewer, E. & Culler, D. The nesC language: A Brewer, E. & Culler, D. The nesC language: A holistic approach to networked embedded systemsholistic approach to networked embedded systemsSIGPLAN Not., ACM, SIGPLAN Not., ACM, 20032003, 38, 38, 1-11 , 1-11

• Williams, B.; Ingham, M.; Chung, S. & Elliott, P.Williams, B.; Ingham, M.; Chung, S. & Elliott, P.Model-based programming of intelligent Model-based programming of intelligent embedded systems and robotic space explorersembedded systems and robotic space explorersProceedings of the IEEE, Proceedings of the IEEE, 20032003, 91, 91, 212-237 , 212-237