Tuesday, May 18, 2010

Embedded Programming - Overview

Before discussing the root article, Embedded Programming, Let us have a look on Embedded Systems.

Embedded Systems:
Embedded Systems are commonly known as special-purpose computer systems which are designed for some specific function(s) to be performed, while interacting with some real-time computing constraints. These systems are usually embedded into another system where it performs more with the help of integration of other systems, peripheral devices, mechanical parts and micro controllers.
As these systems are designed for specific tasks, their design, capability and performance can be enhanced or optimized by design engineers.

These systems can be commonly found in digital watches and MP3 Players. On the other hand, with enhanced capabilities, these systems can also be found in controlling Nuclear Power Plants. Which means, these systems can be a single microcontroller chip or may be a large chassis with multiple units, peripheral devices and mechanical parts.
Before dilating upon the topic in hand, it appears quite pertinent and even desirable to point-out at the outset that the term, ”Embedded System” is not an exactly defined term. For example, in palm devices or handheld computers, operating systems are powered by microprocessors, but these microprocessors are not truly embedded systems.

Embedded Systems are found in all aspects of modern life for example, in Telecommunication Systems, there are a lot of embedded systems from the telephone switches to mobile phones at end-users. Consumer electronics include Digital Cameras, PDAs, DVD Players and MP3 Players etc. Household appliances include washing machines, microwave ovens and other electronic devices. In the same way, there are many examples of Embedded Systems applications in all the fields like for example transportation means to medical equipments etc.
In early 1960s, embedded systems have come down in price. So, we had first microprocessor Intel 4004, which was designed for calculators and other small systems but still required many external memory and support chips. In 1978 National Engineering Manufacturers Association (NEMA), released a "standard" for programmable microcontrollers. In the mid of 1980s, many external system components had been integrated into the same chip as the processor.

There will be hardly any exaggeration in it if we describe the characteristics of embedded systems as follows.
1.Embedded systems are not always standalone devices.
2.The program written for these systems are referred to as firmware and are stored in read-only mode in memory or flash disk, whatever is used to store data.
3.These systems are designed to perform some specific task and function in contrast to general purpose computers which can perform multiple tasks at a time.

Embedded Programming:
Embedded Programming is a bit different from ordinary programming a PC. You have to think at a lower level as you don’t have an operating system (unless you buy some and that will be expensive). The I/O lines are to be programmed by single instructions. The serial/Parallel/USB or any other port will need functions to read and write data. There is no BIOS present so no BIOS Calls are available. Many manufacturers have their sample work online for programming on-chip peripherals.

If you are not using a real time operating system (RTOS), and you’re not writing your own pre-emptive multitasking kernal, then you will probably have a top-level sequencer loop.

After compilation or assembling the program, object files are created. Object files are then linked to form a complete program which can be downloaded to the microcontroller or programmable chip. These program is form of Intel’s HEX format, which is just a binary format for binary files. These files are same as .exe files on PC. The bootloader in the program should be able to accept this file through one of the microcontroller’s serial ports, and load it into the program memory, which may be RAM, or preferable FLASH. There are many different binary file formats, but the two most common in embedded work are Intel Hex and Motorola S-Records. Larger 32-bit processors may use ELF, or COFF formats.

There may be a simple loop which calls each module in turn, up to the most complex operating systems such as Windows NT or Linux! Let's describe these in turn starting from the simplest. All examples will be in C:

C Program: A Very Simple Main Loop:

VOID MAIN(VOID)  {      INT FLAG_KEY;      WHILE (FLAG_KEY == FALSE)      {          FLAG_KEY = GETCONTROL();          CONTROLWALK();          CONTROLTURN();          CONTROLSTOP();      }      POWEROFF();  }  

The three modules are called as fast as the processor can spin round the while loop. The three modules just get the current Flag_key (command). Notice the Flag_key variable which is set by the GetControl() function. If there is a problem with the data, this allows the variable to be set to FALSE which will cause an emergency shutdown in the PowerOff() function.

Real Time Operating Systems (RTOS)
The next most complicated step is to use a RTOS. These are available commercially and there are also a few public domain ones.
The main function of the RTOS is to control what modules get called when. In RTOS terminology, the modules are called processes. This is the same as what the sequencers above were doing, but RTOSs allow a lot more flexibility. Common features of them include:
Pre-emptive multitasking:
This means that a process may be running along happily, but when a more important process wants to run, it stops the first process and takes over until it has finished, whereupon the first process starts where it left off again.

Process priority setting:
Each process can be assigned a priority, often from 0 to 255. If a lower priority process is running when a higher one wants to start, the low priority process is suspended until the higher one has finished (or until the higher one suspends waiting for something else).

Inter process messaging:
Each process can send and receive user-defined messages between each other. These may be in the form of queues, pipelines, or FIFO stacks. If a process is expecting a message but the message has not arrived yet, it can suspend on that message, This means it will stop running (thereby allowing other lower priority processes to run) until the message arrives. Processes can also suspend waiting to send a message to a process which has a full mailbox.

Full timing control:
Each process can be set to run at regular intervals using timers. They can also use timers just to read, or to suspend on until the timer reaches some value. Any number of timers can be used because these are under software control of the RTOS, which uses the hardware timers to control them.

Interrupt control:
The RTOS will generally take care of the hardware interrupt actions, making use of these rather easier.

Peripheral drivers:
Some RTOSs include drivers for disc drives, FLASH memory, IIC devices, TCP/IP stacks, etc.

Now after a small discussion of RTOSs, Let’s now discuss Embedded Programming in the context of Software Engineering.

Embedded System’s Design Patterns:
One is the Data Stream and name of other one is state machine. Data Stream is good to use in digital signal processing where State machines are much suitable to re-active embedded systems such as user interfaces.

Data stream style:
As the name describes, it’s a sequence of incoming data which is known as stream. Data Stream Style interacts with a data structure known as “QUEUE”. It reads the incoming data, processes it, exits and then new data is inserted into queue. It supervises the data that comes in regularly and must be processed on the fly.

Lets’ have a workstation example. In workstation example, samples are to be processed over a given time interval. This data is received in form of a file and output is generated in a batch file. While talking about embedded systems, we must not only produce outputs in real time, but we also have to finish this job using minimum memory space.
Another data structure named, Circular Buffer, is also used in this technique. The circular buffer is a data structure that enables us to handle streaming data in a good manner. Circular buffer stores a subset of the data stream. At each point in time, the algorithm needs a subset of the data stream that forms a window into the stream.
The window slides as time is incremented and we throw out old values which are not longer needed and add new values. As the size of the window does not change, we can use a fixed-size buffer to hold the current data. Every time we add a new sample, we automatically overwrite the oldest sample, which is the one that needs to be thrown out. When the pointer gets to the end of the buffer, it wraps around to the top and this process continues.

State machine style:
Let inputs are appearing occasionally rather than as periodic samples, it is often suitable to think that systems are reacting to those inputs. The reaction produced by these inputs can be characterized in terms of the input received and the current state of the system. This phenomenon leads naturally to a finite-state machine style. If the behavior is same every time, A program can be written to implement that behavior in a state machine style.

No comments:

Post a Comment