C program to check Endianness of processor


From previous post we know how important it is to know Endianness of processor. I was searching methods to find it out.

I found that a simple C program will do that. let me tell you How?

Endianness affects the order in which data is stored..So Data types in C also are influenced by this feature of processor..

Important data types to be considered in Porting problem(Little-endian to Big-Endian and vice-vesra) are:

Unions(in turn structures), Byte arrays,Bit fields and pointer casting..

So we can exploit this property to serve our purpose..!!

#include<stdio.h>

#include<string.h>

int main (int argc, char* argv[]) {
FILE* fp;

/* Our example data structure */
struct {
char one[4];
int  two;
char three[4];
} data;

/* Fill our structure with data */

strcpy (data.one, “foo”);
data.two = 0x12345678;
strcpy (data.three, “bar”);

/* Write it to a file */
fp = fopen (“output”, “wb”);
if (fp) {
fwrite (&data, sizeof (data), 1, fp);
fclose (fp);
}
}
// copy paste this code and run with following commands

$ gcc -o endian endian.c

$ ./endian

$ hexdump -C output

00000000 66 6f 6f 00 78 56 34 12 62 61 72 00      |foo.xV4.bar.|

0000000c

Mine is intel duo core processor i386 processor..It’s little-Endian.

If you are running on big-endian processor you should get in 12 34  56 78 and darkened  |foo.xV4.bar.|  be in different order but with same characters..

Just a little more to know to about data types Endianness problems:

Data types :

Unions
A union is a variable that may hold objects of different types and sizes, with the compiler keeping track of the size and alignment requirements. Objects of dissimilar types and sizes can only be held at different times. A union provides a way to manipulate different kinds of data in a single area of storage.
Problem – Unions work fine for using the same memory to access different data. The key is to know what type of data exists in the memory before it is accessed. Accessing the same data with different types is not a valid use of unions and can cause endian issues.
Problem – If data types longer than 8 bits are united with a byte array, the data becomes byte order dependent.

Solution A – Don’t access the same data in memory as different data types.
Byte Arrays:
Byte Arrays – A character array that is used to hold a specified number of bytes. The size of array is always
equal to the number of bytes to hold.
Problem – If data in the byte array is accessed outside of its native data type, the data becomes byte order
dependent.
Example: An array that is initialized with a list of characters will be read as different values between little
endian and big endian platforms. The following example shows a byte array initialized to a,b,c,d. Accessing
this array as a long data type on a little endian platform will result in the value 0x64636261. On a big endian
platform it results in the value 0x61626364.
Solution – Avoid accessing byte arrays outside of the byte data type.
Bit Fields and Bit Masks
Bit operations are endian-sensitive. Even a bit field defined within a single byte is endian-sensitive. Code that
defines bit fields is subject to Endianness conflicts when porting the code to an opposite endian platform.

Pointer Casts
Casting pointers changes the native meaning of the original data. Doing so will affect which data is addressed.
Problem – If the native data pointer is a 32-bit pointer and is cast to a byte pointer, depending on the Endian-
architecture of the host, either the first byte or the last byte will be pointed to.
Example: Casting a pointer that stores the 32-bit value 0x11223344 to a byte pointer, the big-Endian system
points to 0x11. The little-Endian system points to 0x44.
Solution – Never change the native type of a pointer. Instead, get the data in its native data type format and
use byte swapping macros to access the bytes individually.

Enjoy Coding and Computing..:)

References:

http://www.ibm.com/developerworks/aix/library/au-endianc/index.html?ca=drs-

www.intel.com/design/intarch/papers/endian.pdf

Endianness of a processor


If you are installing an Endian-specific software on your computer you need to know Endianness of your processor.Also if you are/want to become a software engineer or Computer hardware engineer you must know about Endianess..!!

Now we will see what are it’s importance, merits and demerits of this feature of a processor.

Precisely describing “Endianness is the format to how multi-byte data is stored in computer memory. It describes the location of the most significant byte (MSB) and least significant byte (LSB) of an address in memory”

NOTE1: Endianness is dictated by the CPU architecture implementation of the system. The operating system does not dictate the endian model implemented, but rather the endian model of the CPU architecture dictates how the operating system is implemented.

NOTE2:  Endian order means that any time a computer accesses a stream (a network tap, a local file, or an audio, video or multimedia stream), the computer has to know how the file is constructed. For example: if you write out a graphics file (such as a .BMP file, which is Little-Endian format) on a Big-Endian machine, you must first reverse the byte order of each integer you write or another “standard” program will not be able to read the file.

Endianness describes how multi-byte data is represented by a computer system and is dictated by the CPU architecture of the system. Unfortunately not all computer systems are designed with the same Endian- architecture. The difference in Endian-architecture is an issue when software or data is shared between computer systems. An analysis of the computer system and its interfaces will determine the requirements of the Endian implementation of the software. Software is sometimes designed with one specific Endian-architecture in mind, limiting the portability of the code to other processor architectures. This type of implementation is considered to be Endian-specific. However, Endian-neutral software can be developed, allowing the code to be ported easily between processors of different Endian-architectures, and without rewriting any code. Endian-neutral software is developed by identifying system memory and external data interfaces, and using Endian-neutral coding practices to implement the interfaces.Platform migration requires consideration of the Endian-architecture of the current and target platforms, as well as the Endian-architecture of the code.

To show two reparesentation formats:

Consider  0x12345678 (32-bit hexadecimal 12345678) will be stored in the order as below.

TAble1: Storage format of two Endians

Endianess Byte 00 Byte 01 Byte 02 Byte 03
Big-Endian 12(MSB) 34 56 78(LSB)
Little-Endian 78(LSB) 56 34 12(MSB)

So, big-Endian stores the MSB at the lowest memory address. Little-Endian stores the LSB at the lowest memory address. The lowest memory address of multi-byte data is considered the starting address of the data.

Merits of Endian-architecture:

In “Little-Endian” form, assembly language instructions for picking up a 1, 2, 4, or longer byte number proceed in exactly the same way for all formats: first pick up the lowest order byte at offset 0. Also, because of the 1:1 relationship between address offset and byte number (offset 0 is byte 0), multiple precision math routines are correspondingly easy to write.

In “Big-Endian” form, by having the high-order byte come first, you can always test whether the number is positive or negative by looking at the byte at offset zero. You don’t have to know how long the number is, nor do you have to skip over any bytes to find the byte containing the sign information. The numbers are also stored in the order in which they are printed out, so binary to decimal routines are particularly efficient. Most embedded communication processors and custom solutions associated with the data plane are Big-Endian (i.e. PowerPC, SPARC, etc.). Because of this, legacy code on these processors is often written specifically for
network byte order (Big-Endian).

Demerits:

There are two main areas where Endianness must be considered in any system. One area pertains to code portability. The
second area pertains to sharing data between platforms.
Code Portability:
It is not uncommon for software to be designed and implemented for the Endian-architecture of a specific
processor platform, without allowing for ease of portability to other platforms.
Endian-neutral code provides flexibility for software implementations to be compiled for and operate seamlessly
on processors of different Endian-architectures.
Shared Data:
Computer systems are made up of multiple components, including computers, interfaces, data storage and
shared memory. Any time file data or memory is shared between computers, there is a potential for an Endian-
architecture conflict. Data can be stored in ways that are not tied to endian-architecture and also in ways that
define the Endianness of the data.

List of several popular computer systems and their Endian-architectures. Note that some CPUs can be either big or little endian (Bi-Endian) by setting a processor register to the desired endian-architecture.

Table2. Computer system Endianness

Platform Endian-Architecture
ARM* Bi-Endian
DEC Alpha* Little-Endian
HP PA-RISC 8000* Bi-Endian
IBM PowerPC Bi-Endian
intel 80×86 Little-Endian
Intel IXP network processors Bi-Endian
Intel Itanium Processor family Bi-Endian
Java Virtual machine* Big-Endian
MIPS* Bi-Endian
Motorola 68k Big-Endian
Sun SPARC* Big-Endian

Abbreviations & Descriptions used:
ARM – Advanced RISC Machines Ltd.
CPU -Central Processing Unit
DEC – Digital Equipment Corporation
IA  – Intel Architecture
I/O  – Input / Output
IBM – International Business Machines Corporation
LSB – Least Significant Byte
MSB – Most Significant Byte
MIPS – MIPS Technologies, Inc.
RISC – Reduced Instruction Set Computer

Hope you enjoyed reading and information is useful.:)

Next we will see how to check Endianness of your system by executing a simple C program.

Reference:

www.intel.com/design/intarch/papers/endian.pdf

You can also download the document here


To begin Labview..


Are  you an Engineer??

Mechanical?Electronics? Electrical? Instrumentation? Aerospace?

or

You are an academician or UG/PG student or Research person??

If you belong  to any of these categories.. Labview is  platform which can help you to integrate your  system ( S/w and H/w) ,simulate , develop a prototype and deploy..!!!!

So now if you are thinking to start Labview… here is your beginner guide..

Labview evaluation software is available for free download in National Instruments website.You will just have to register in website.

you can also order DVD Kit which includes 3 Evaluation DVDs of Labview 2010(30-days trial version ) development system and device drivers.

then you will just have to install Labview and you can start these tutorials.

I attended two days of  Hands On, Experiential Workshop on Labview held in IIT kharagpur.

You can download these Presentation and exercises we were shared and you can start Labview.

Introduction to LabVIEW 8.6 in 6 Hours

Introduction to LabVIEW 8.6 in 6 Hours

To go to NI Labview Training in 6 hrs click  here

There is great technical support from National Instruments team online.

I have started Labview just out of my own interest. I am also a beginner in this.

Solution to these exercises will be uploaded soon..

Enjoy your development in Labview platform.

Power Supply Design for Micro controller projects(Your first Power Supply)


You may not believe if I say we have so much of electronics to learn and apply in simple 5V power supply design, which is first step in any micro controller  project(5V/3V digital worlds)

Now a days micro controllers are operational at 1.5V . Look at these examples of popular  micro controllers.

AVR family micro controller ATmega16 has operable voltage range of 2.7V-5.5V ATmega16L and 4.5V-5.5V ATmega16 respectively.

So,now  you  must know the importance of  5V power supply in micro controller world.

All DTL,TTL and CMOS circuits mostly operational well at 5V.

Now , to design your 5V power supply just follow these steps and it could be a simple exercise for beginner.

  • Block diagram:These are the design objectives to be met

Available 230V/50Hz  AC line has to stepped down to level acceptable  by  Rectifier (AC-DC converter ) and 5V regultor (regulates voltage at 5V irrespective of power line fluctuations and rectifier output variations).Along with these we need a filter which filters out high frequency components.

So resultant block diagram of simple Power supply is:

Power Supply General Block diagram

Voltage specification of supply is 5V.. then what do you think current specification of the power supply we need??

1A/1.5A must be sufficient for most of designs including RF tranciever inclded in project(RF  devices need more current compared to other moicro controller I/O as per my experience so far..!!)

Blocks to circuit:

First part is Transformer which  can step down AC 230V to 12V(Why not 5V??? you will answered just read further )

Rectifier: there are two options for this part one is you can buy an Integrated Circuit already designed by intelligent EngineersJ or you can yourself build a simple bridge rectifier.

If you are going for IC this is Part no.DI104 Rectifier IC.

Bridge rectifier uses four silicon rectifier diodes In4001 ,1A  arranged as shown:TR1 is transformer.

Bridge Rectifier using silicon rectifier diodes

Now we need a simple capacitive filter which can smooth out Half-sine wave from rectifier. Capacitor is energy storage passive electronic component which can store charge and discharge it when voltage across it falls below it’s charged voltage.This electronic nature of capacitor accomplishes the job of smoothing out Half- sine rectified wave.

Now smoothed DC should be regulated to remain within acceptable range around 5V.

Regulator: Regulators are available as Integrated Circuits (ICs).78XX family regulators are normally used.So, you can go for 7805 5V,1A regulators . Just go through data sheet and application note of 7805 from manufacturer Texas Instruments ua78XX datasheet & Application note.

There you can observe operable range(test conditions) is 7V-25V. Hope you got the answer to question above.:)

And output voltage range it can regulate is in the range 4.8V-5.25V.(don’t scratch your head if you don’t get exact 5V output from your supply)

Now go to Application Information in the document:  It’s mentioned why we need  reverse bias protection regulator(Fig.7 of document). So connect a diode parallel to regulator7805 and also two more capacitors as mentioned in document for proper design.

And now power supply is ready but use a simple network consisting of LED and resistor.This is for User indication that the supply is ON/OFF.

Note: As I mentioned in my previous post here.

1mA of current must be sufficient to glow indicator LED to observable brightness. We need more current to drive load which will be connected across this network so use very high resistor in series with indicator LED.ON resistance of LED is negligible. So use

5V/1mA= 5k ohm resistance.

Now Power supply is ready for your micro controller  project. This is complete circuit diagram :

Power Supply circuit

NOTE:

Same way you can design 8V and 12V power supplies needed to drive DC or Servo motors if used in project just by replacing 7805 by 7808 and 7812 respectively.

Don’t forget to make common ground for micro controller and I/O device circuits.Otherwise the project won’t work..!!!!

All specifications and  information are taken from respective data sheets(Components) and User manuals(Micro controllers).

Now make it..!!! Good luck..:)

Tips for Electronic Hobby Projects Beginners


Hello guys, here is a small guide for beginners in electronic hobby projects.

Few important things you should know before you start your electronic project.These are the things I learned by experience and I gonna share for you. I am sure it helps you and you enjoy reading.

Things to be done:

  • First step is to come up with block diagram  of your simple electronic system(very important for clarity tin your system design )
  • Google the circuits for your system to meet requirements as per block diagram.
  • Never forget to refer data sheets and more importantly Application note provided by manufacturer
  • Keep the current and voltage ratings in design and use  wires and components accordingly.(to make sure that circuit won’t get burnt in operation).Sometimes power may be not sufficient to drive your circuit
  • Use minimum current to drive LEDs used for indication purpose in your circuit(reduce power consumed and try optimizing wherever possible ..we are engineers..!!)
  • Solder your circuits only after testing on breadboard
  • Before powering your circuit make sure all connections are proper(Otherwise it may spoil your components)
  • Make proper soldering (always use flux for firm solder).
  • Respect electricity- Don’t use wet hands and be safe.
  • Here are few good E-books for you guys. Just have a look before you begin your project

Check out  resources:   A book for beginners.

Electronics For You – Projects and Ideas 2000

Electronics Projects For Dummies

Here is  your first exercise Power Supply Design.

Enjoy Electronics..:)

Make and break circuits..!!!