SlideShare une entreprise Scribd logo
1  sur  4
Télécharger pour lire hors ligne
Overview




                                                          gram ming
                                                       Pro
                                                            mbe dded
                                                       for E
                                                        Syst ems

                            Even if C is losing its position as the mainstream programming language for
                            general application development, embedded programming is still its strong-
                            hold. Students and programmers new to embedded programming, though
                            proficient in general C programming, are often clueless about C programming
                            for embedded systems. This article offers an introduction.




                              E
                                          very embedded system has           quite different from general programming
                                          software and hardware elements     (for PCs). The main difference is due to
                                          that are very closely dependent,   the underlying hardware. The important
                                          and this affects how we program    characteristics of embedded systems when
                                          an embedded device. Based          compared to regular PCs are:
                            on this fact, we’ll look at the hardware         • Embedded devices have significant
                            characteristics of embedded devices, how the         resource constraints; typically, less
                            hardware affects the language chosen to write        memory, less processing power and
                            programs for it, how C is used differently           limited hardware devices compared to
                            for embedded programming and, finally,               regular desktop computers. [But this is
                            we’ll cover how GCC is used for embedded             slowly changing; the latest embedded
                            systems.                                             devices have considerably more resources
                                                                                 (such as a huge memory) and are more
                            Characteristics of embedded devices                  powerful than ever before. Nowadays,
                                Programming for embedded systems is              with smart mobile phones, we can


72   September 2008   |   LINUX For YoU   |   www.openItis.com
Overview



    browse the Internet, play games, make phone calls,           set breakpoints, visualise the processing of the embedded
    and even do programming—so, handheld devices are             hardware or monitor the signals in the pins. In this way,
    becoming as powerful as a PC, blurring the difference        it is possible to debug programs, though it is not as
    between embedded systems programming and general             convenient as regular debugging. There are also other aids
    programming.]                                                such as simulators. For those new to embedded systems,
•   The hardware components used are not the same as the         understanding such domain-specific details takes more time
    ones used for a PC: the components in an embedded            than actually writing the programs.
    device are lighter, cooler, smaller and less power-
    consuming. To be specific, the processor used in an          Languages for programming embedded devices
    embedded system is rarely the same used in a PC; and         C is the language of choice for most of the programming
    heavy I/O devices such as keyboards and monitors are         done for embedded systems. Why? Why not assembly
    not used in embedded systems.                                language, C++, Java or any other language?
•   Embedded systems are more tied to the hardware/OS                 It might appear that assembly language is intuitively
    vendor than the PCs. PCs are general-purpose machines        the most obvious choice, since embedded programming is
    and we can take the availability of applications and         all about programming hardware devices such as micro-
    programming tools for granted. However, embedded             controllers. It is true that micro-controllers were initially
    systems are typically more tied to the hardware or OS        programmed mostly in assembly language as with other
    vendor and, hence, the common applications or tool-          embedded devices. It is not that difficult to write an
    chains need not be widely or freely available.               assembly program since the assembly language produces
•   Embedded systems should be more reliable and                 the tightest code, making it possible to squeeze every
    efficient. If a program in a PC fails, we can restart the    possible byte of memory usage. However, the problem is
    program and nothing serious happens. However, for            that it becomes difficult to use for any reasonably-sized
    many embedded system programs, if they fail, they            program, and even a slightly complicated device. The
    can make the device useless. And sometimes they can          difficulties are in getting assembly programs to work
    cause serious harm -- imagine what could happen if an        correctly; and understanding, debugging, testing and,
    embedded program in a pacemaker device fails.                most importantly, maintaining them in the long run. Also,
                                                                 high quality C compilers can often generate code that is
Programming for embedded devices                                 comparable to the speed of programs written in assembly.
An embedded hardware device, depending on its size               So, the benefits of using assembly for efficiency are
and capabilities, can have an operating system—such as           negligible compared to the ease with which programmers
embedded Linux—with limited or minimal functionality             can write C code. However, if performance is the key to
compared to a desktop version. For very small embedded           make or break a device, then it is hard to beat assembly. For
devices, an OS might be entirely absent: it is not possible to   example, DSP (digital signal processing) devices are mostly
write programs, compile, run and debug the code in such          programmed in assembly even today, because performance
small devices. In such a situation, it is necessary to use       is the most important requirement in these devices.
cross compilers (or assemblers), which compile programs               Languages such as C++ have features that are often
written in a high-level language on a host system (typically     bulky, inefficient or inappropriate for use in resource-
a PC) and generate code for a target system (for example,        constrained environments such as embedded devices. In
an embedded device). If we write assembly programs and           particular, virtual functions and exception handling are
use an assembler running on a host to generate code for          two language features that are not efficient in terms of
a target device, it is a cross assembler. So, we can write       space and speed in embedded systems. Sometimes, C++
programs on our PC, generate code for the embedded               programming is used as ‘Safe C’, where only a small subset
device and run it there. This solves the problem of creating     of C++ features is included. However, for convenience, most
executable code for embedded systems, but testing,               embedded projects pragmatically use C itself.
debugging or tracing embedded programs is difficult.                  Languages with ‘managed runtimes’, such as Java, are
    One problem is that these programs usually don’t have        mostly heavyweight. Running Java programs requires a
input devices such as keyboards and a mouse, or output           Java Virtual Machine, which can take up a lot of resources.
devices like full-fledged monitors or display screens that we    Though Java is popular in high-end mobile phones because
usually take for granted in regular programming for PCs. So,     of the portability it provides and for browsing the Web, it is
debugging, fixing and testing them is often more difficult.      rarely suitable for use in small embedded devices.
Fortunately, there are tools available to help an embedded            There are numerous special purpose or proprietary
programmer. For example, an in-circuit emulator is a             languages meant to be used in embedded systems such
hardware device used to help debug the program that runs         as B# and Dynamic C. Others, like Forth, are also well
in an embedded system. It plugs on top of an embedded            suited for the purpose. However, C is widely used and
device; it can be even used to emulate a processor that is       familiar to programmers worldwide, and its tools are
yet to be developed! With help from this device, we can          easily available.


                                                                     www.openItis.com   |   LINUX For YoU   |   September 2008   73
Overview



How C is used differently for embedded                                  data structure which we conventionally program using
programming                                                             dynamic memory allocation?
If you are new to embedded C programming, you will notice               Fortunately, many of the data structures can be
that there are only subtle differences between a regular C           implemented by using static allocation itself. Here is a
program and an embedded C program.                                   simple illustration of a doubly linked list made up of three
    The following is a list of the most important differences        nodes that are allocated statically (instead of dynamic
between C programming for embedded systems and C                     memory allocation):
programming for PCs.
• Writing low-level code: In embedded programming, it                 struct	node	{	
    is necessary to directly access the underlying hardware.          struct	node	*	prev;
    For example, we might need to access a port, timer                int	data;
    or a memory location. Similarly, we might need to do              struct	node	*	next;
    low-level programming activities like accessing the job           };
    queue, raise some signals, interrupts, etc. So, we need
    to write low-level programs that directly access, modify          int	main(){
    or update hardware; this is an important characteristic           struct	node	one,	two,	three;
    of embedded C programs. How do we write low-level                 one.prev	=	&three;	one.next	=	&two;	one.data	=	100;	
    code? C features such as pointers and bit-manipulation            two.prev	=	&one;	two.next	=	&three;	two.data	=	200;		
    facilities enable us to program at the hardware level             three.prev	=	&two;	three.next	=	&one;	three.data	=	300;	
    directly; so these features are used extensively in               struct	node*	temp	=	&one;	
    embedded programming.                                             		          do	{
• Writing in-line assembly code: The C language                       	           	          printf(“%d	”,	temp->data);	
    provides a limited set of features, so it is not possible         	           	          temp	=	temp->next;
    to use high-level C code to perform a specific function.          	           }	while(temp	!=	&one);	
    For example, the device might have some instructions              }
    for which there is no direct equivalent in C code (for
    example, bit-wise rotation). In such cases, we can write              This program prints ‘100 200 300’. Using the basic
    assembly code embedded within C programs called                  idea from this program, if we pre-allocate the number
    ‘inline assembly’. The exact syntax depends on the               of expected nodes statically, then we can write our own
    compiler; here is an example for GCC:                            my_node_malloc() function that will return a node that
                                                                     was allocated statically. Using such techniques, it is possible
 	        int	a=10,	b;                                               to implement dynamic data structures such as linked lists
 								asm	(“movl	%1,	%%eax;	                                      and trees that still work under the limitations of embedded
 														movl	%%eax,	%0;”                                      devices.
 													:”=r”(b)								/*	output	*/                                Similarly, features such as recursion are not allowed
 													:”r”(a)									/*	input	*/                            in most of the embedded devices. One reason is that the
 													:”%eax”									/*	clobbered	register	*/               recursion is inefficient in terms of space and time compared
 													);			                                                  to iterative code. Fortunately, it is possible to write
                                                                     any recursive code as iterative code (though writing or
    This code just assigns a to b (!), but this is just an           understanding iterative versions of the code is not usually
example to show how in-line assembly code looks; usually             intuitive).
it will have the asm keyword prefixed and/or suffixed by                  To put it simply, costly language features (in terms of
underscores. The assembly code is written in C program               space and time) are either not available, nor recommended
itself and we can use C variables to refer to data; the              in embedded programming. As programmers, we should
register allocation and other details will be taken care of by       find alternative ways of achieving the same functionality.
the compiler. We can write high-level functionality such as          • Using limited C features or new language
looping in C syntax, and only when we require processor                   extensions: Some of the language features that we take
specific functionality, we can write in-line assembly code. In            for granted in regular programming might simply not
this way, writing in-line assembly is more convenient than                be available for embedded programming. For example,
writing full-fledged assembly code.                                       if the device does not support floating point numbers,
• No recursion, no heap: Many of the devices are small,                   then we cannot use floats or doubles in the programs.
    so the heap area (where dynamic memory segments will                  Problems frequently occur the other way also: often,
    be allocated) might be limited or might not even exist.          the underlying devices have hardware features that don’t
    So, we would need to program without using malloc                have direct support in the C programming language. For
    or its variations. This poses difficulties for those new         example, a device might support fixed-point numbers;
    to embedded systems: how to use a linked list or a tree          however, C language doesn’t have any support for this data


 74    September 2008    |   LINUX For YoU    |   www.openItis.com
Overview



type, so there is no direct way of programming it.                  symbols in that file
     Embedded C compilers differ from regular C compilers       •   size: displays the size of various components of a binary
in many ways; one important difference is the language              file
features they support. Most of the embedded C compilers         • strip: removes optional parts of a binary file (such as
will not have full conformance to the ANSI C standard               sections for debugging info)
(because it is heavy-weight); rather, they will support the     • objdump: reads the object file and displays the various
embedded C specification (check the ‘Reference’ section             sections and information from that file
for more details). This specification is meant for use in       • ar: stands for ‘archiver’; it can collect object files and
embedded systems, and the most common extensions and                store them as a single file (code archives)
features to enhance performance and convenience for                 The debugger is not necessarily part of this, but it is
accessing underlying hardware resources are provided in         often bundled with the rest of such binary utilities.
it. The advantage in using this specification is that a large       The glibc is a comprehensive, complex, portable
number of compilers implement the specification, and thus       implementation of the C standard library; but it is not very
it is easy to port embedded programs with it.                   suitable for embedded systems. uClibc is a configurable,
                                                                small C library meant for use in embedded systems.
Using GCC for embedded programming                              Since it is widely used and actively maintained, support
GCC is a very valuable tool for embedded engineering.           is available for it and, hence, it is often the No 1 choice in
Though there are quite a few commercial embedded                the embedded compiler tool chain. By porting the GCC
compilers available (such as the compilers from byte craft,     compiler tool chain, you need not leave the tools and
ACE, EDG and CoWare), GCC is the most widely used               techniques that we are already familiar with and can use
compiler for embedded programming.                              them for embedded programming.
    Why, you may ask?
    It is not just that GCC is open source and free, it is      Where to from here?
also available for almost all the processors released so far.   This article provided only an overview of C programming
The GCC tool chain and libraries also have variants that        for embedded systems. Low-level programming in C for
are meant for use in embedded systems. For example, the         hardware is one of the most interesting jobs one can get;
uClibc is a smaller, lightweight implementation of the C        so go ahead, explore and enjoy programming for your
library. With little pain, it is possible to build a compiler   embedded device!
tool chain for a new embedded platform, though that topic       1. www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-
is beyond the scope of this article (you can refer to www.          HOWTO.html#s4
linuxjournal.com/article/9904 to get started with it); we’ll    2. JTC1/SC22/WG14. Programming languages – C –
just cover what we can do with GCC.                                 Extensions to support embedded processors. Technical
    A compiler tool-chain is a collection of programs that          report, ISO/IEC, 2004 (www.open-std.org/jtc1/sc22/
is used for compiling and building programs. It consists            wg14)
of three parts: the compiler, binary utilities and standard     3. www.linuxjournal.com/article/9904
libraries. For example, the compiler, linker and assembler
are part of the compiler tool chain. Binary utilities               By: S.G. Ganesh is a research engineer at Siemens
(binutils) are low-level programs that are necessary for            (Corporate Technology). His latest book, “60 Tips on Object
working on a new platform. A few important binutils are             Oriented Programming”, was published by Tata McGraw-
given here:                                                         Hill in December last year. You can reach him at sgganesh@
                                                                    gmail.com
• nm: reads a binary file and displays the (name of)




                                                                      www.openItis.com   |   LINUX For YoU   |   September 2008   75

Contenu connexe

Tendances

Kernel Process Management
Kernel Process ManagementKernel Process Management
Kernel Process Managementpradeep_tewani
 
Beneath the Linux Interrupt handling
Beneath the Linux Interrupt handlingBeneath the Linux Interrupt handling
Beneath the Linux Interrupt handlingBhoomil Chavda
 
RISC-V Introduction
RISC-V IntroductionRISC-V Introduction
RISC-V IntroductionYi-Hsiu Hsu
 
8051 microcontroller lecture ppt by Tarun Khaneja ( 9034406598 )
8051 microcontroller lecture ppt by Tarun Khaneja ( 9034406598 )8051 microcontroller lecture ppt by Tarun Khaneja ( 9034406598 )
8051 microcontroller lecture ppt by Tarun Khaneja ( 9034406598 )Tarun Khaneja
 
System On Chip
System On ChipSystem On Chip
System On ChipA B Shinde
 
Virtualization Support in ARMv8+
Virtualization Support in ARMv8+Virtualization Support in ARMv8+
Virtualization Support in ARMv8+Aananth C N
 
Serial Communication Interfaces
Serial Communication InterfacesSerial Communication Interfaces
Serial Communication Interfacesanishgoel
 
Multi-core architectures
Multi-core architecturesMulti-core architectures
Multi-core architecturesnextlib
 
SOC System Design Approach
SOC System Design ApproachSOC System Design Approach
SOC System Design ApproachA B Shinde
 
Introduction to arm architecture
Introduction to arm architectureIntroduction to arm architecture
Introduction to arm architectureZakaria Gomaa
 
Unit 1 Introduction to Embedded computing and ARM processor
Unit 1 Introduction to Embedded computing and ARM processorUnit 1 Introduction to Embedded computing and ARM processor
Unit 1 Introduction to Embedded computing and ARM processorVenkat Ramanan C
 

Tendances (20)

ARM Architecture in Details
ARM Architecture in Details ARM Architecture in Details
ARM Architecture in Details
 
Embedded C - Optimization techniques
Embedded C - Optimization techniquesEmbedded C - Optimization techniques
Embedded C - Optimization techniques
 
Kernel Process Management
Kernel Process ManagementKernel Process Management
Kernel Process Management
 
Embedded C - Day 1
Embedded C - Day 1Embedded C - Day 1
Embedded C - Day 1
 
Beneath the Linux Interrupt handling
Beneath the Linux Interrupt handlingBeneath the Linux Interrupt handling
Beneath the Linux Interrupt handling
 
Embedded Linux on ARM
Embedded Linux on ARMEmbedded Linux on ARM
Embedded Linux on ARM
 
Linux Systems: Getting started with setting up an Embedded platform
Linux Systems: Getting started with setting up an Embedded platformLinux Systems: Getting started with setting up an Embedded platform
Linux Systems: Getting started with setting up an Embedded platform
 
E.s unit 6
E.s unit 6E.s unit 6
E.s unit 6
 
Linux-Internals-and-Networking
Linux-Internals-and-NetworkingLinux-Internals-and-Networking
Linux-Internals-and-Networking
 
SOC design
SOC design SOC design
SOC design
 
SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 
RISC-V Introduction
RISC-V IntroductionRISC-V Introduction
RISC-V Introduction
 
8051 microcontroller lecture ppt by Tarun Khaneja ( 9034406598 )
8051 microcontroller lecture ppt by Tarun Khaneja ( 9034406598 )8051 microcontroller lecture ppt by Tarun Khaneja ( 9034406598 )
8051 microcontroller lecture ppt by Tarun Khaneja ( 9034406598 )
 
System On Chip
System On ChipSystem On Chip
System On Chip
 
Virtualization Support in ARMv8+
Virtualization Support in ARMv8+Virtualization Support in ARMv8+
Virtualization Support in ARMv8+
 
Serial Communication Interfaces
Serial Communication InterfacesSerial Communication Interfaces
Serial Communication Interfaces
 
Multi-core architectures
Multi-core architecturesMulti-core architectures
Multi-core architectures
 
SOC System Design Approach
SOC System Design ApproachSOC System Design Approach
SOC System Design Approach
 
Introduction to arm architecture
Introduction to arm architectureIntroduction to arm architecture
Introduction to arm architecture
 
Unit 1 Introduction to Embedded computing and ARM processor
Unit 1 Introduction to Embedded computing and ARM processorUnit 1 Introduction to Embedded computing and ARM processor
Unit 1 Introduction to Embedded computing and ARM processor
 

Similaire à C Programming For Embedded Systems

Similaire à C Programming For Embedded Systems (20)

Introduction to embedded c
Introduction to embedded cIntroduction to embedded c
Introduction to embedded c
 
Consider the following interrupting system. The active-edge inputs o.pdf
Consider the following interrupting system. The active-edge inputs o.pdfConsider the following interrupting system. The active-edge inputs o.pdf
Consider the following interrupting system. The active-edge inputs o.pdf
 
Embedded systems
Embedded systemsEmbedded systems
Embedded systems
 
Embedded systems
Embedded systemsEmbedded systems
Embedded systems
 
Embedded Software Development
Embedded Software DevelopmentEmbedded Software Development
Embedded Software Development
 
Compilers programmingembedded
Compilers programmingembeddedCompilers programmingembedded
Compilers programmingembedded
 
Embedded software
Embedded softwareEmbedded software
Embedded software
 
Computer
ComputerComputer
Computer
 
Software And Hardware
Software And HardwareSoftware And Hardware
Software And Hardware
 
Embedded Systems
Embedded SystemsEmbedded Systems
Embedded Systems
 
embeddedsystems-100429081552-phpapp01.pdf
embeddedsystems-100429081552-phpapp01.pdfembeddedsystems-100429081552-phpapp01.pdf
embeddedsystems-100429081552-phpapp01.pdf
 
Clifford sugerman
Clifford sugermanClifford sugerman
Clifford sugerman
 
Different features of computer
Different features of computerDifferent features of computer
Different features of computer
 
main
mainmain
main
 
Exam 1 in ITEP 132
Exam 1 in ITEP 132Exam 1 in ITEP 132
Exam 1 in ITEP 132
 
Introduction to Operating Systems
Introduction to Operating SystemsIntroduction to Operating Systems
Introduction to Operating Systems
 
Software
SoftwareSoftware
Software
 
Java
JavaJava
Java
 
Embedded systems
Embedded systemsEmbedded systems
Embedded systems
 
Raspberry Pi
Raspberry PiRaspberry Pi
Raspberry Pi
 

Plus de Ganesh Samarthyam

Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeGanesh Samarthyam
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”Ganesh Samarthyam
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGanesh Samarthyam
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionGanesh Samarthyam
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeGanesh Samarthyam
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesGanesh Samarthyam
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationGanesh Samarthyam
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterGanesh Samarthyam
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Ganesh Samarthyam
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ Ganesh Samarthyam
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckGanesh Samarthyam
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageGanesh Samarthyam
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Ganesh Samarthyam
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz QuestionsGanesh Samarthyam
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz QuestionsGanesh Samarthyam
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizGanesh Samarthyam
 

Plus de Ganesh Samarthyam (20)

Wonders of the Sea
Wonders of the SeaWonders of the Sea
Wonders of the Sea
 
Animals - for kids
Animals - for kids Animals - for kids
Animals - for kids
 
Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in Practice
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't Enough
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - Description
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief Presentation
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - Poster
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship Deck
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming Language
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz Questions
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz Questions
 
Docker by Example - Quiz
Docker by Example - QuizDocker by Example - Quiz
Docker by Example - Quiz
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quiz
 

Dernier

Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 

Dernier (20)

Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 

C Programming For Embedded Systems

  • 1. Overview gram ming Pro mbe dded for E Syst ems Even if C is losing its position as the mainstream programming language for general application development, embedded programming is still its strong- hold. Students and programmers new to embedded programming, though proficient in general C programming, are often clueless about C programming for embedded systems. This article offers an introduction. E very embedded system has quite different from general programming software and hardware elements (for PCs). The main difference is due to that are very closely dependent, the underlying hardware. The important and this affects how we program characteristics of embedded systems when an embedded device. Based compared to regular PCs are: on this fact, we’ll look at the hardware • Embedded devices have significant characteristics of embedded devices, how the resource constraints; typically, less hardware affects the language chosen to write memory, less processing power and programs for it, how C is used differently limited hardware devices compared to for embedded programming and, finally, regular desktop computers. [But this is we’ll cover how GCC is used for embedded slowly changing; the latest embedded systems. devices have considerably more resources (such as a huge memory) and are more Characteristics of embedded devices powerful than ever before. Nowadays, Programming for embedded systems is with smart mobile phones, we can 72 September 2008 | LINUX For YoU | www.openItis.com
  • 2. Overview browse the Internet, play games, make phone calls, set breakpoints, visualise the processing of the embedded and even do programming—so, handheld devices are hardware or monitor the signals in the pins. In this way, becoming as powerful as a PC, blurring the difference it is possible to debug programs, though it is not as between embedded systems programming and general convenient as regular debugging. There are also other aids programming.] such as simulators. For those new to embedded systems, • The hardware components used are not the same as the understanding such domain-specific details takes more time ones used for a PC: the components in an embedded than actually writing the programs. device are lighter, cooler, smaller and less power- consuming. To be specific, the processor used in an Languages for programming embedded devices embedded system is rarely the same used in a PC; and C is the language of choice for most of the programming heavy I/O devices such as keyboards and monitors are done for embedded systems. Why? Why not assembly not used in embedded systems. language, C++, Java or any other language? • Embedded systems are more tied to the hardware/OS It might appear that assembly language is intuitively vendor than the PCs. PCs are general-purpose machines the most obvious choice, since embedded programming is and we can take the availability of applications and all about programming hardware devices such as micro- programming tools for granted. However, embedded controllers. It is true that micro-controllers were initially systems are typically more tied to the hardware or OS programmed mostly in assembly language as with other vendor and, hence, the common applications or tool- embedded devices. It is not that difficult to write an chains need not be widely or freely available. assembly program since the assembly language produces • Embedded systems should be more reliable and the tightest code, making it possible to squeeze every efficient. If a program in a PC fails, we can restart the possible byte of memory usage. However, the problem is program and nothing serious happens. However, for that it becomes difficult to use for any reasonably-sized many embedded system programs, if they fail, they program, and even a slightly complicated device. The can make the device useless. And sometimes they can difficulties are in getting assembly programs to work cause serious harm -- imagine what could happen if an correctly; and understanding, debugging, testing and, embedded program in a pacemaker device fails. most importantly, maintaining them in the long run. Also, high quality C compilers can often generate code that is Programming for embedded devices comparable to the speed of programs written in assembly. An embedded hardware device, depending on its size So, the benefits of using assembly for efficiency are and capabilities, can have an operating system—such as negligible compared to the ease with which programmers embedded Linux—with limited or minimal functionality can write C code. However, if performance is the key to compared to a desktop version. For very small embedded make or break a device, then it is hard to beat assembly. For devices, an OS might be entirely absent: it is not possible to example, DSP (digital signal processing) devices are mostly write programs, compile, run and debug the code in such programmed in assembly even today, because performance small devices. In such a situation, it is necessary to use is the most important requirement in these devices. cross compilers (or assemblers), which compile programs Languages such as C++ have features that are often written in a high-level language on a host system (typically bulky, inefficient or inappropriate for use in resource- a PC) and generate code for a target system (for example, constrained environments such as embedded devices. In an embedded device). If we write assembly programs and particular, virtual functions and exception handling are use an assembler running on a host to generate code for two language features that are not efficient in terms of a target device, it is a cross assembler. So, we can write space and speed in embedded systems. Sometimes, C++ programs on our PC, generate code for the embedded programming is used as ‘Safe C’, where only a small subset device and run it there. This solves the problem of creating of C++ features is included. However, for convenience, most executable code for embedded systems, but testing, embedded projects pragmatically use C itself. debugging or tracing embedded programs is difficult. Languages with ‘managed runtimes’, such as Java, are One problem is that these programs usually don’t have mostly heavyweight. Running Java programs requires a input devices such as keyboards and a mouse, or output Java Virtual Machine, which can take up a lot of resources. devices like full-fledged monitors or display screens that we Though Java is popular in high-end mobile phones because usually take for granted in regular programming for PCs. So, of the portability it provides and for browsing the Web, it is debugging, fixing and testing them is often more difficult. rarely suitable for use in small embedded devices. Fortunately, there are tools available to help an embedded There are numerous special purpose or proprietary programmer. For example, an in-circuit emulator is a languages meant to be used in embedded systems such hardware device used to help debug the program that runs as B# and Dynamic C. Others, like Forth, are also well in an embedded system. It plugs on top of an embedded suited for the purpose. However, C is widely used and device; it can be even used to emulate a processor that is familiar to programmers worldwide, and its tools are yet to be developed! With help from this device, we can easily available. www.openItis.com | LINUX For YoU | September 2008 73
  • 3. Overview How C is used differently for embedded data structure which we conventionally program using programming dynamic memory allocation? If you are new to embedded C programming, you will notice Fortunately, many of the data structures can be that there are only subtle differences between a regular C implemented by using static allocation itself. Here is a program and an embedded C program. simple illustration of a doubly linked list made up of three The following is a list of the most important differences nodes that are allocated statically (instead of dynamic between C programming for embedded systems and C memory allocation): programming for PCs. • Writing low-level code: In embedded programming, it struct node { is necessary to directly access the underlying hardware. struct node * prev; For example, we might need to access a port, timer int data; or a memory location. Similarly, we might need to do struct node * next; low-level programming activities like accessing the job }; queue, raise some signals, interrupts, etc. So, we need to write low-level programs that directly access, modify int main(){ or update hardware; this is an important characteristic struct node one, two, three; of embedded C programs. How do we write low-level one.prev = &three; one.next = &two; one.data = 100; code? C features such as pointers and bit-manipulation two.prev = &one; two.next = &three; two.data = 200; facilities enable us to program at the hardware level three.prev = &two; three.next = &one; three.data = 300; directly; so these features are used extensively in struct node* temp = &one; embedded programming. do { • Writing in-line assembly code: The C language printf(“%d ”, temp->data); provides a limited set of features, so it is not possible temp = temp->next; to use high-level C code to perform a specific function. } while(temp != &one); For example, the device might have some instructions } for which there is no direct equivalent in C code (for example, bit-wise rotation). In such cases, we can write This program prints ‘100 200 300’. Using the basic assembly code embedded within C programs called idea from this program, if we pre-allocate the number ‘inline assembly’. The exact syntax depends on the of expected nodes statically, then we can write our own compiler; here is an example for GCC: my_node_malloc() function that will return a node that was allocated statically. Using such techniques, it is possible int a=10, b; to implement dynamic data structures such as linked lists asm (“movl %1, %%eax; and trees that still work under the limitations of embedded movl %%eax, %0;” devices. :”=r”(b) /* output */ Similarly, features such as recursion are not allowed :”r”(a) /* input */ in most of the embedded devices. One reason is that the :”%eax” /* clobbered register */ recursion is inefficient in terms of space and time compared ); to iterative code. Fortunately, it is possible to write any recursive code as iterative code (though writing or This code just assigns a to b (!), but this is just an understanding iterative versions of the code is not usually example to show how in-line assembly code looks; usually intuitive). it will have the asm keyword prefixed and/or suffixed by To put it simply, costly language features (in terms of underscores. The assembly code is written in C program space and time) are either not available, nor recommended itself and we can use C variables to refer to data; the in embedded programming. As programmers, we should register allocation and other details will be taken care of by find alternative ways of achieving the same functionality. the compiler. We can write high-level functionality such as • Using limited C features or new language looping in C syntax, and only when we require processor extensions: Some of the language features that we take specific functionality, we can write in-line assembly code. In for granted in regular programming might simply not this way, writing in-line assembly is more convenient than be available for embedded programming. For example, writing full-fledged assembly code. if the device does not support floating point numbers, • No recursion, no heap: Many of the devices are small, then we cannot use floats or doubles in the programs. so the heap area (where dynamic memory segments will Problems frequently occur the other way also: often, be allocated) might be limited or might not even exist. the underlying devices have hardware features that don’t So, we would need to program without using malloc have direct support in the C programming language. For or its variations. This poses difficulties for those new example, a device might support fixed-point numbers; to embedded systems: how to use a linked list or a tree however, C language doesn’t have any support for this data 74 September 2008 | LINUX For YoU | www.openItis.com
  • 4. Overview type, so there is no direct way of programming it. symbols in that file Embedded C compilers differ from regular C compilers • size: displays the size of various components of a binary in many ways; one important difference is the language file features they support. Most of the embedded C compilers • strip: removes optional parts of a binary file (such as will not have full conformance to the ANSI C standard sections for debugging info) (because it is heavy-weight); rather, they will support the • objdump: reads the object file and displays the various embedded C specification (check the ‘Reference’ section sections and information from that file for more details). This specification is meant for use in • ar: stands for ‘archiver’; it can collect object files and embedded systems, and the most common extensions and store them as a single file (code archives) features to enhance performance and convenience for The debugger is not necessarily part of this, but it is accessing underlying hardware resources are provided in often bundled with the rest of such binary utilities. it. The advantage in using this specification is that a large The glibc is a comprehensive, complex, portable number of compilers implement the specification, and thus implementation of the C standard library; but it is not very it is easy to port embedded programs with it. suitable for embedded systems. uClibc is a configurable, small C library meant for use in embedded systems. Using GCC for embedded programming Since it is widely used and actively maintained, support GCC is a very valuable tool for embedded engineering. is available for it and, hence, it is often the No 1 choice in Though there are quite a few commercial embedded the embedded compiler tool chain. By porting the GCC compilers available (such as the compilers from byte craft, compiler tool chain, you need not leave the tools and ACE, EDG and CoWare), GCC is the most widely used techniques that we are already familiar with and can use compiler for embedded programming. them for embedded programming. Why, you may ask? It is not just that GCC is open source and free, it is Where to from here? also available for almost all the processors released so far. This article provided only an overview of C programming The GCC tool chain and libraries also have variants that for embedded systems. Low-level programming in C for are meant for use in embedded systems. For example, the hardware is one of the most interesting jobs one can get; uClibc is a smaller, lightweight implementation of the C so go ahead, explore and enjoy programming for your library. With little pain, it is possible to build a compiler embedded device! tool chain for a new embedded platform, though that topic 1. www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly- is beyond the scope of this article (you can refer to www. HOWTO.html#s4 linuxjournal.com/article/9904 to get started with it); we’ll 2. JTC1/SC22/WG14. Programming languages – C – just cover what we can do with GCC. Extensions to support embedded processors. Technical A compiler tool-chain is a collection of programs that report, ISO/IEC, 2004 (www.open-std.org/jtc1/sc22/ is used for compiling and building programs. It consists wg14) of three parts: the compiler, binary utilities and standard 3. www.linuxjournal.com/article/9904 libraries. For example, the compiler, linker and assembler are part of the compiler tool chain. Binary utilities By: S.G. Ganesh is a research engineer at Siemens (binutils) are low-level programs that are necessary for (Corporate Technology). His latest book, “60 Tips on Object working on a new platform. A few important binutils are Oriented Programming”, was published by Tata McGraw- given here: Hill in December last year. You can reach him at sgganesh@ gmail.com • nm: reads a binary file and displays the (name of) www.openItis.com | LINUX For YoU | September 2008 75