Bogotobogo
contact@bogotobogo.com


C++ Tutorial - Embedded Systems Programming - 2013
Bookmark and Share
cplusplus logo
Full List of C++ Tutorials


GeyRyongSan


Embedded Systems

When we talk about embedded systems programming, in general, it's about writing programs for gadgets.

Gadget with a brain is the embedded system. Whether the brain is a microcontroller or a digital signal processor (DSP), gadgets have some interactions between hardware and software designed to perform one or a few dedicated functions, often with real-time computing constraints.

Usually, embedded systems are resource constrained compared to the desktop PC. Embedded systems, typically, have limited memory, small or no hard drives, and in some cases with no external network connectivity.




arm_performance_race.png

Picture: https://blogs.akamai.com - The Performance Arms Race





Presentation Materials: Effective C++ in an Embedded Environment (pdf) by Scott Meyers, 2012

embededCplusplus.png
http://www.artima.com/shop/effective_cpp_in_an_embedded_environment.



Why Linux?

According to LinuxDevices.com, Linux has emerged as the dominant OS for embedded systems, and it will reach 70% of the market by 2012.

Go to Embedded Linux.





Embedded Software & Tools Market in 2011 by VDC Research
  1. COMs gain traction as time-to-market accelerators for OEMs

    By combining COM express modules with off-the-shelf COMs, suppliers are able to offer several different configurations of CPU boards and leverage COMs' interchangeable characteristics. CPU vendors can thus offer a fairly wide range of boards without incurring high design and inventory carrying costs.

  2. PC/104 module family under pressure



    Although VDC data projects the PC/104 family will experience a single-digit rebound from the low points of the recent recession, vendors will have to commit resources to developing newer strategies in order for this technology to remain viable. Otherwise, the recovery of these architectures is likely to stall or decline in 2011.

  3. Asia continues to rise in the development of embedded technology

    2011 will see further strengthening of the Asian embedded supplier community as supply chain synergies, R&D capabilities and fabrication automation increases between upstream and downstream ecosystem partners.

  4. China's growth will power MCU market

    Continued economic growth in China will drive the country's automotive market and expand the need for MCU (microcontroller unit) technology. Despite reduction in government subsidies, VDC expects the Chinese automotive market to expand substantially through 2015, driving adoption of MCU solutions.

  5. Suppliers will invest in services value chain

    While embedded hardware margins show signs of stability in 2011, it's clear to VDC that leading embedded suppliers also recognize the value their clients place on a range of services capabilities. As a result, many leading suppliers will try to differentiate by investing in critical aspects of the services value chain, from consulting capabilities to enhanced warranty and end-of-life policies.

  6. FPGA and GPU will expand into a number of market segments

    The medical, industrial automation and military segments provide an attractive opportunity for FPGA (Field Programmable Gate Array) devices. From imaging equipment to diagnostic devices, there is a need for adaptable health care, factory control and military C4 solutions. The programmability, flexibility and reduced NRE (non-recurring engineering) costs associated with FPGAs will lend themselves to broader adoption in these markets.

  7. Tier 2/Tier 3 OEMs and ISVs will become more important

    Investment in solutions requiring embedded platforms continues to rebound; however, the market will still be driven by small- to mid-sized projects. This is related to the slow return of larger, blanket purchase orders let by Tier 1 accounts and to the user community preferences for projects with smaller footprints that fit within narrower application definitions and require short, sharply defined systems integration support. These projects are tailor-made for local, expert ISVs and ISIs, as well as Tier 2/Tier 3 OEMs.

  8. The market explores HaaS (Hardware as a Service) bundles

    Broad market expansion and deep application penetration of remote monitoring and control capabilities will advance across a number of market segments, foretelling a broader migration to managed services solution development and deployment models in supervisory monitoring and control applications. These embedded application clouds will require local points of presence (POPs) or on-site infrastructure and hardware rolled into service level agreements (SLAs) supporting the software and service delivery portions of contracts.

  9. Cross-platform processor suppliers learn to play nice

    From broader, bigger, more aggressive, public licensing agreements to M&A, the market will force suppliers of CPU, FPGA and GPU (graphics processing unit) technologies to collaborate more in 2011. VDC Research's surveys of hundreds of OEMS across a number of embedded markets reveal significant growth in OEM plans to develop solutions on hybrid platforms incorporating two or more of these technologies.

  10. Competition will intensify and growth will accelerate

    Even if the market does not return to pre-recession levels, growth will accelerate during 2011. VDC sees virtually every vertical market growing more than five percent, and most technology categories achieving the same five percent CAGR. However, profitability results may not be so positive. Demand for stable technologies, brutal price concessions and expanded services requirements will provide opportunities for differentiation and revenues, but not necessarily margin.

  11. Android to catalyze further growth in commercial Linux market

    As device manufacturers take Android into new application classes beyond mobile, the commercial Linux market will experience further growth.

  12. Multi-OS systems will grow in designs

    More application classes will have sophisticated UI functionality that is not supported by traditional OSs and end-users will seek out multi-OS systems.

  13. Virtualization in embedded and mobile systems will increase

    Driven by hardware bill of materials savings and reduced concerns regarding additional run-time execution latencies and costs, operating system virtualization will provide increased growth opportunities, and therefore will continue to be a significant focus for many suppliers.

  14. Symbian's loss to become MeeGo's gain

    Intel's increasing focus on embedded combined with Symbian's loss of strategic direction will drive additional gains for MeeGo as Nokia turns their attention toward the Linux-based platform.

  15. OEMs to increase focus on the use of web security test tools

    Increased interaction with the cloud and web-based content by more embedded device classes will increase OEM focus on use of web security test tools.

  16. Telecom vertical will reaccelerate spend on commercial products

    The increasing burden of mobile device data usage is driving the need for investment in wireless infrastructure and the telecom vertical market will reaccelerate spending on commercial products.

  17. Microsoft will regain relevance in the mobile phone sector

    Riding the wave of Windows Phone 7 buzz, Microsoft will re-emerge as a leading player in the mobile phone arena.

  18. Another acquisition to come?

    Following a string of high profile acquisitions in 2009/2010, VDC anticipates yet another major embedded real-time operating system supplier will get acquired in 2011.



Issues for Embedded Systems Programming

Here are some characteristics of embedded systems, and few systems suffer all of these constrains.

  • Reliability - failure is very expensive.
  • Limited Resources - memory, processor cycles, power.
  • Real-time Response.
  • A system may be Running Forever.

The characteristics of embedded systems affect the embedded systems programming:

  • Correctness - producing the results at the right time, in the right order, and using only an acceptable set of resources.
  • Fault tolerance
  • No downtime.
  • Real-time constraints.
  • Predictability.
  • Concurrency.


Real-time Applications - Predictability

Following features of C++ language are not predictable:

  • new and delete
    • Static memory poses no special problem in embedded systems programming since all is taken care of before the program starts to run and long before a system is deployed.

    • Stack memory can be a problem because it is possible to use too much of it. One way is to avoid recursive functions and stick to iterative implementation.

    • Dynamic memory allocation is usually banned or restricted. new is either banned or its use restricted to a startup period, and delete is banned because of the predictability and fragmentation.

    • However, there are two data structures that are particularly useful for predictable memory allocations: stacks and pools:

      • Pool is a data structure from which we can allocate object of a given type and later deallocate such object. A pool contains a maximum number of objects; that number is specified when the pool is created.
      • Stack is a data structure from which we can allocate chunks of memory and deallocate the last allocated chunk.
    • C++ standard containers such as vector, map, etc. and the standard string are not to be used because they indirectly use new.
  • Exceptions
    How can we catch all exceptions and how long it will take to find a matching catch.



Reliability

Avoid language features and programming techniques that have proved error-prone. Pointers!

  • Explicit conversions which are unchecked and unsafe - avoid them.
  • Passing pointers to array elements - An array is often passed to a function as a pointer to an element. Therefore, they lose their size, so that the receiving function cannot directly tell how many elements are pointed to. This is a cause of many bugs.


Bits

In code meant to be portable, use should use <limits> to make sure our assumption about sizes is correct. Here is the list of sizes of the primitive types:

  • bool - 1 bit, but takes up a byte
  • char - 8 bits
  • short - 16 bits
  • int - 32 bits, but many embedded systems have 16-bit ints
  • long int - 32 bits or 64 bits


bitset
#include <iostream>
#include <bitset>
#include <iomanip>

int main( ) 
{ 
	using namespace std;

	int i;
	while(cin >> i)
		cout << dec << i << "==" 
		<< hex << "0x" << i << "=="
		<< bitset<8*sizeof(int)>(i)<< endl;
	return 0; 
}

Outoput is:

4
4==0x4==00000000000000000000000000000100

8
8==0x8==00000000000000000000000000001000

16
16==0x10==00000000000000000000000000010000

32
32==0x20==00000000000000000000000000100000

127
127==0x7f==00000000000000000000000001111111

In the code, to print the individual bits of the integer, we used a standard library bitset:

bitset<8*sizeof(int)>(i)

A bitset is a fixed number of bits. In the above example, we used the number of bits in an int, which is 8*sizeof(int). Then, we initialized that bitset with i.

Another example:

#include <iostream>
#include <bitset>

int main( ) 
{ 
	using namespace std;

	const int max = 8;
	bitset<max> b;
	while(cin >> b) {
		cout << b << endl;
		for(int i = 0; i < max; ++i) cout << b[i];
		cout << endl;
	}                
}

Output is:

10100111
10100111
11100101


Signed and Unsigned

Let's look at the following example which looks innocent.

#include <iostream>
#include <vector>

int main( ) 
{ 
	using namespace std;

	const int max = 10;
	vector<int> v(max,77);

	for(int i = 0; i < v.size(); ++i) 
		cout << v[i] << endl;               
}

When we compile it, we get a warning something like this:

signed/unsigned mismatch

That's because the index i is signed integer, but v.size() is unsigned integer. Mixing signed and unsigned could lead to disaster. For instance, the loop variable i might overflow. In other words, v.size() might be larger than the largest signed int. Then, i would reach the highest value that could represent a positive integer in a signed int. Then, the next ++ couldn't yield the next-highest integer and would instead result in a negative value. The loop would never terminate!

Here, we have two choices:

  • vector<int>::size_type
    	for(vector<int>::size_type i = 0; i < v.size(); ++i)
    		cout << v[i] << endl; 
    
  • iterator
    	for(vector<int>::iterator it = v.begin(); it != v.end(); ++it) 
    		cout << *it << endl;  

The size_type is guaranteed to be unsigned, so the first form has one more bit to play with than the int version. That can be significant, but it is still gives only a single bit of range. The loop using iterators has no such limitation.



Bit Manipulation

"People who play with bits will be bitten"

"People who play with bytes will be bytten"

When do we need to manipulate bits?

  • flags as hardware indicators
  • low-level communications - we need to extract from byte streams
  • graphics - we need to compose picture out of several images
  • encryption

Here is an example of extracting information from a short integer.

#include <iostream>
#include <bitset>
int main( ) 
{ 
	using namespace std;

	const int max = 8;
	
	short val = 4;
	unsigned char left = val & 0xff;          // leftmost (least significant) byte
	cout << bitset<max>(left) << endl;

	unsigned char right = (val >> 8) & 0xff;  // rightmost (most significant) byte
	cout << bitset<max>(right) << endl;

	val = 256;
	right = (val >> 8) & 0xff;
	cout << bitset<max>(right) << endl;

	val = 2*val;
	right = (val >> 8) & 0xff;
	cout << bitset<max>(right) << endl;

	val = -8;
	bool sign_bit = val & 0x8000;             // sign bit
	cout << sign_bit << endl;
}

Output is:

00000100
00000000
00000001
00000010
1

The operations are known as shift and mask. We shift to place the bits we want to consider to the rightmost (least significant) part of the word where they are easy to manipulate. We mask using and (&) together with a bit pattern such as 0xff to eliminate the bits we do not want in the result.



Processors of Embedded Systems

From wiki

Firstly, Embedded processors can be broken into two broad categories: ordinary microprocessors (µP) and microcontrollers ((µC), which have many more peripherals on chip, reducing cost and size. Contrasting to the personal computer and server markets, a fairly large number of basic CPU architectures are used; there are Von Neumann as well as various degrees of Harvard architectures, RISC as well as non-RISC and VLIW; word lengths vary from 4-bit to 64-bits and beyond (mainly in DSP processors) although the most typical remain 8/16-bit. Most architectures come in a large number of different variants and shapes, many of which are also manufactured by several different companies.

A long but still not exhaustive list of common architectures are: 65816, 65C02, 68HC08, 68HC11, 68k, 8051, ARM, AVR, AVR32, Blackfin, C167, Coldfire, COP8, Cortus APS3, eZ8, eZ80, FR-V, H8, HT48, M16C, M32C, MIPS, MSP430, PIC, PowerPC, R8C, SHARC, SPARC, ST6, SuperH, TLCS-47, TLCS-870, TLCS-900, Tricore, V850, x86, XE8000, Z80, AsAP etc.



Embedded Linux

From wiki

Embedded Linux is the use of Linux in embedded computer systems such as mobile phones, personal digital assistants, media players, set-top boxes, and other consumer electronics devices, networking equipment, machine control, industrial automation, navigation equipment and medical instruments. According to survey conducted by Venture Development Corporation, Linux was used by 18% of embedded engineers.

Linux has been ported to a variety of processors not always suited for use as the processor of desktop or server computers, such as various CPUs including ARM, AVR32, Blackfin, ETRAX CRIS, FR-V, H8300, IP7000 M32R, m68k, MIPS, mn10300, PowerPC, SuperH, or Xtensa processors, as an alternative to using a proprietary operating system and toolchain.

The advantages of embedded Linux over proprietary embedded operating systems include no royalties or licensing fees, a stable kernel, a support base that is not restricted to the employees of a single software company, and the ability to modify and redistribute the source code. The disadvantages include a comparatively larger memory footprint (kernel and root filesystem), complexities of user mode and kernel mode memory access and complex device drivers framework.

There are non-proprietary embedded operating systems that share the open-source advantages of Linux, without the memory requirements that make Linux unsuitable for many embedded applications.

See Linux Kernel - My first driver



JiriSan



Full List of C++ Tutorials