LINUX For YoU
How C is used differently for embeddedprogramming
I you are new to embedded C programming, you will noticethat there are only subtle dierences between a regular Cprogram and an embedded C program.The ollowing is a list o the most important dierencesbetween C programming or embedded systems and Cprogramming or PCs.
Writing low-level code
: In embedded programming, itis necessary to directly access the underlying hardware.For example, we might need to access a port, timeror a memory location. Similarly, we might need to dolow-level programming activities like accessing the jobqueue, raise some signals, interrupts, etc. So, we needto write low-level programs that directly access, modiyor update hardware; this is an important characteristico embedded C programs. How do we write low-levelcode? C eatures such as pointers and bit-manipulationacilities enable us to program at the hardware leveldirectly; so these eatures are used extensively inembedded programming.
Writing in-line assembly code
: The C languageprovides a limited set o eatures, so it is not possibleto use high-level C code to perorm a specic unction.For example, the device might have some instructionsor which there is no direct equivalent in C code (orexample, bit-wise rotation). In such cases, we can writeassembly code embedded within C programs called‘inline assembly’. The exact syntax depends on thecompiler; here is an example or GCC:
This code just assigns a to b (!), but this is just anexample to show how in-line assembly code looks; usuallyit will have the
keyword prexed and/or suxed byunderscores. The assembly code is written in C programitsel and we can use C variables to reer to data; theregister allocation and other details will be taken care o bythe compiler. We can write high-level unctionality such aslooping in C syntax, and only when we require processorspecic unctionality, we can write in-line assembly code. Inthis way, writing in-line assembly is more convenient thanwriting ull-fedged assembly code.
No recursion, no heap:
Many o the devices are small,so the heap area (where dynamic memory segments willbe allocated) might be limited or might not even exist.So, we would need to program without using
or its variations. This poses diculties or those newto embedded systems: how to use a linked list or a tree
data structure which we conventionally program usingdynamic memory allocation?Fortunately, many o the data structures can beimplemented by using static allocation itsel. Here is asimple illustration o a doubly linked list made up o threenodes that are allocated statically (instead o dynamicmemory allocation):
This program prints ‘100 200 300’. Using the basicidea rom this program, i we pre-allocate the numbero expected nodes statically, then we can write our own
unction that will return a node thatwas allocated statically. Using such techniques, it is possibleto implement dynamic data structures such as linked listsand trees that still work under the limitations o embeddeddevices.Similarly, eatures such as recursion are not allowedin most o the embedded devices. One reason is that therecursion is inecient in terms o space and time comparedto iterative code. Fortunately, it is possible to writeany recursive code as iterative code (though writing orunderstanding iterative versions o the code is not usuallyintuitive).To put it simply, costly language eatures (in terms o space and time) are either not available, nor recommendedin embedded programming. As programmers, we shouldnd alternative ways o achieving the same unctionality.
Using limited C features or new languageextensions
: Some o the language eatures that we takeor granted in regular programming might simply notbe available or embedded programming. For example,i the device does not support foating point numbers,then we cannot use foats or doubles in the programs.Problems requently occur the other way also: oten,the underlying devices have hardware eatures that don’thave direct support in the C programming language. Forexample, a device might support xed-point numbers;however, C language doesn’t have any support or this data