Saturday, November 7, 2009

USB-3rd Part

Descriptors and their types:-

All descriptors have three common fields like bLength bDescriptorType and bcdUSB. The first one decides the length of the descriptor, the second type indicates its type. The different types of descriptors are:-


1:- Device Descriptors: - The device descriptor represents the entire USB device and hence each device can have a single device descriptor. The idProduct and idVendor fields of device descriptor help the OS to find the appropriate driver for the device


2:- Configuration Descriptor:- The configuration descriptor specifies values such as the amount of power this particular configuration uses, if the device is self or bus powered and the number of interfaces it has. When a device is enumerated, the host reads the device descriptors and can make a decision of which configuration to enable. It can only enable one configuration at a time. Thus multiple configuration descriptors are possible.

3:- Interface descriptor: - This can be seen as a header or leader of a group of endpoints which is dedicated to perform a particular function. For example you could have a multi-function fax/scanner/printer device. Interface descriptor one could describe the endpoints of the fax function, Interface descriptor two the scanner function and Interface descriptor three the printer function. Unlike the configuration descriptor, there is no limitation as to having only one interface enabled at a time. A device could have 1 or many interface descriptors enabled at once. Though there is a system of identifying the primary and/or the secondary interface descriptor by judging the fields like bInterfaceNumber (specifying the Interface number) and a bAlternateSetting

4: - Endpoint Descriptor: - Endpoint descriptors are used to describe endpoints other than endpoint0. Endpoint zero is always assumed to be a control endpoint and is configured before any descriptors are even requested. The host will use the information returned from these descriptors to determine the bandwidth requirements of the bus

5: - String Descriptor: - These provides information to the end user and hence contains human readable data like the name “ Jetflash Mass Storage Device” which we are used to see in the property of a USB device.

Enumeration Steps:-

Enumeration is the initialization process by which an OS detects a device and specifies its power requirement, which class of device, type of data transfer it supports. The steps of enumeration in a windows machine are as follows:-

1:- First when the device is inserted in the USB connection of the machine, host gives it a time of 100ms for the power stabilization

2:- The device is assigned an address 0 by default by the host controller. No matter how many devices are attached to the host controller all those will be assigned as address 0 in the beginning and hence the multiple devices attached to the host are enumerated one by one and not simultaneously

3:- The host asks for the device descriptor

4: After receiving initial 8 bytes of the device descriptor the host assigns a reset command

5: The host now assigns an address to the device by issuing a set address command. The address issued by the host ranges from 1 to 127. Thus we are able to connect 127 devices to each host

6:- The remaining bytes of the device descriptor are asked for

7:- The host then asks for the configuration descriptor and then for the interface descriptor to determine the overall size and the types or no. of function to be performed by the device.

8:- The host then asks for string descriptor if there is any

After all these steps depending upon the idProduct and idVendor as provided by the device descriptor the OS will search for the device driver. If it founds one then it loads the driver and then pops up a message near the taskbar that “an USB device has been recognized “. If the OS can not load any driver then it will show the message “the device is not recognized”.


USB-2nd part

USB Data Transfer Protocol

The USB device consist of a logical entity called Endpoints in it through which the data transfer takes place. An USB device can have multiple Endpoints. The connection between the Endpoint and the host is called logical pipes. The device driver of the host sends data to the endpoint for any type of data transfer require point. It will send data to the Endpoint1-out buffer (for example) while will receive the incoming data packets in the Endpoint1-in buffer. Each device must support Endpoint0 which is used for the control transfer while the other are used for data transfer

Types Of Data Transfer

The types of data transfer are

1:- Control Transfer: - This type of transfer takes place for command and status operation like resetting or enumeration commands. Enumeration is the initialization of the of the USB device for the data transfer by communicating with the endpoint0. The steps taken for the process are as follows.

The host generates a setup token, which helps the incoming packet to recognize as the setup packet, to the endpoint0. The endpoint is selected by default as all the control transfers are done via the endpoint0. The host then sends the 8 bytes DATA0 packet requesting the device descriptor of the USB device, which will help the OS of the host to load the appropriate driver to recognize and control the USB device, to the endpoint0-out buffer. The USB device will acknowledge the packet if there is no error otherwise if the packet is corrupted then it just ignores the packet. The host will resend it in the next few seconds. The device descriptor request as send by the host to the endpoint0-out buffer is then checked and the device sends the device descriptors. But it sends these descriptors only when the host is ready to receive it which is indicated by the issuing of IN token by the host to the device. Then the device will send the device descriptor values to the endpoint0-in buffer.A small comparison between the OUT and IN concept is given below---
a: OUT :- Whenever the host wants to send any data it issues an out token followed by a data packet which is received by the device endpointx-out buffer and if there is no error an acknowledgement is send to the host.
b:- IN :- Whenever a host wants to receive something it issues an IN token which is received by the device and then it sends the data as requested by the host in the previous OUT data-packet, to the endpointx-in buffer.

Remember that the direction of data transfer is based with reference to the host not the device.

2:- Isochronous Transfer: - Isochronous transfers occur continuously and periodically. They typically contain time sensitive information, such as an audio or video stream

3:- Interrupt Transfer


Bulk Transfer: - Bulk transfers can be used for large bursty data. Such examples could include a print-job sent to a printer or an image generated from a scanner. These is supported only by the high speed or the full speed devices

Fields of Set-up Packet:-


The Setup Packet contains the following fields:-

bmRequestType , bRequest, wValue, wIndex, wLength


Depending upon various type of requests like

Standard device request, Standard Interface request, Standard endpoint request the above mentioned fields has different values


USB

Here I will tell about the USB protocol..which we all use but has a very little knowledge.As the very preliminary idea is also big I have make three parts of it.The first part deals with the introduction of USB..the second part describes the data transfer protocol and the third part is the zist of the transfer steps also called enumeration-------

Initialization:-

The USB protocol has been designed in 1990 by the leading companies like, INTEL, Compaq, IBM, Microsoft and Northan Telecom.

There are mainly 4 version of USB that are available now.

1:- USB1.0 protocol. Low Speed 1.5Mbits/s

2:- USB1.1 protocol. Low Speed 1.5Mbits/s and also full speed mode of 12Mbits/s

3:- USB2.0 protocol. High Speed 480Mbits/s

4:- USB3.0 protocol. Super Speed 4.8Gbits/s..

USB3.0 is not fully functional till now, though the first demonstration has takes place in the latest version of Linux.

Data Transfer Mechanical Explanation:-

The USB connector consists of 4 pins. Gnd, D+, D-, Vcc. Each data line corresponding to D+ and D- of the device in the host consist of a pull down resistor of typical value 15kOhm while in case of the device it is 1.5kOhm. While the device is not active the host pulls both the data line in logic 0 state thus producing the Single Ended Zero (SEZ0) state in USB Terminology.

In case of active state the host pulls one of the data lines of the device high through 1.5kOhm depending on the mode it is functioning. For 1.x mode of USB transfer the D+ line is pull high for full speed data transfer while the D- line is pulled low for low speed data transfer.

We get two state called Differential0 and Differential1 state by this.

Differential0:- D+ logic low D- logic high

Differential1:- D+ logic high D- logic low.

The corresponding two states Differential0 and Differential1 are known as J and K respectively in low speed mode while they are known as K and J in full speed mode respectively. The data gets transferred by alternating J and K state between the host and the device.

In case of USB2.0 the device first gets attached to the host as the full speed device i.e., D+ line is pulled high. After receiving the USB RESET command i.e., both the D+ and D- lines are pull low, it pulls the D- line high indicating that the device is capable of high speed data transfer.



Use of output commands

What are the difference between getch(),getche(),getchar()

We have all used getch() at the end of our program in turbo C++ compiler,but its not used/required in the gcc/g++/visual studio compiler..the question is why?

Actually the getch() is an input function which expects a value to be taken as input from the standard input device of a computer i.e., the keyboard.But the fact is it doesnt wait for the pressing of enter button to take the value as input rather as soon as the value is typed it take it as the input and it doesnt throw the input value to the VDU.Until and unless we provide an input the function halts itself at this line and as soon as we give the input the execution of the line completes and that inputted value leavs in the input buffer memory.As turbo C++/C by default dont let the output screen to halt we use this function just to hold the output(definitly if there is any).In the gcc/g++/visual studio compiler they are capable of holding the output data by themself hence we dont need the getch() function here.

getche() function does the same thing as that of getch()..in addition it 'e'choes or send the input value to the VDU.We can also use this function in the turbo C++/C compiler to halt the output screen just that u will see the input key u pressed for a fraction of second before the screen goes away.

getchar() on the other hand takes the input and wait for the enter key to be hit before it can take the pressed key value as the input unlike the previous two.Hence it's very easy to see what is being given as input as the pressed key is echoed to the VDU screen as long as the enter key is not hit..and definitly also after that.

Pointers

It is said that if a C programmer never have any problem in pointer then he is a hoax.Its confusing problematic and sometimes make ur head to go in tizzy.I will discuss here some of the not usual terms of pointers.

Dangling Pointer

Though most of us know this thing but few has a clear idea about this thing.But the most amusing thing is that we every body have used and is using this pointer unknowingly(except the most professionals one..who,I'm sure have done so previously in their life).
Dangling pointer is referred to that pointer which are pointing to a particular memory adress which has been already dereferenced already i.e., the object has been deleted without modifying the value of the pointer pointing its adress which still refers to the memory location of the dereferenced memory.
Now as the program continues it may refer that particular memory to new variables while the previous(now dangling) pointer is still referencing the memory..which might call for unpredictable behaviour.
An example of dangling pointer is when we make an array/block of memory using malloc/calloc.As soon as our function is over we call free() function to free it...but the pointer variable stays as the dangling pointer.

int main()
{
int *p;
p = (int *) malloc(sizeof(int));
/* ..... program steps .... */
free(p) ; // the pointer variable p becomes dangling
p=NULL; // the pointer variable is no longer dangling
}

Another dangling pointer example ----
int *f1(void)
{
int num = 10;
/* ...... program steps .....*/
return #
}

The above function returns the address value of the stack allocated localy declared variable num.But the stack memory is dereferenced as soon as the control shifts from the function f1 to the called function and the memory allocated for this type of variable is now dereferenced and contains garbage value.Still the value of num can be shown in the called function by accessing the returned address.Now if we call any other function say f2() and after the control comes back to the called function and we try to access the value num by its adress it will give unwanted result.This is because as soon as the second function f2() is called the variables in this function will overwrite the stack memory space which was previously taken by num variable..thus the pointer variable holding &num becomes dangling as its original pointee gets dereferenced.If we want to treat this pointer as not the dangling pointer we must declared it as static..so that the value of the variable has its scope beyond the function it is declared.


Wild pointer

Wild pointer is also another type of pointer we all use quite frequently infact every time.It is nothing but the pointer variable which has been declared but not defined.I'l give a code below to make it more simple-----

int wild(void)
{
int *p; // wild pointer
int num=1;
p=# // no more an wild pointer
}

I guess the above two is now clear to you....i'l come up with lot more

Thursday, November 5, 2009

Adventurism

This post deals with the books that make me dream to be a vagabond..and they are mainly written in bengali so i'l explainmy feeling also in bengali

Ami borabori choto thake khub Galper boi parte valo basi..Amar mate bangla deshe sarbokaler sera kishore sahityik halen Bibhutibhusan bandopadhay.Pratham jakhn Chader pahar pari takhn ami class 4er chatro.Galper nayak Shankarke jano chokher samne dekhte pachilam.Boita dhar niyechilam parar 1 didir kach thake.

Pare jedin nije boita kinechilam takhn gograshe parechilam.I just worship the book.Ki vabe 1ta pati bangali chele africar bistirno paharsreni r veld tapke hirer khani khojar kaje joriye gelo tai Chader Pahar boitar upojibyo.Bangla sahitye ato valo adventure uponash amar to mone hoy na r lekha hoyeche.Boita pore ami sapno dekhechilam amio 1din kadhe bag-pack jhuliye Chader pahar khujte jabo...jadio aj abdi chance hoyni kintu kono 1din jaboi a bapare amar khub icha....

Pare Dineshchandra Chattopadhayer-er lekha Duronto Igol poreo mugdho hoye gechilam.Vadrolok boita lekhar janyo either nije puro Tibet ghurechen or prachur..prachur..prachuuur parasona karechen oi part of world-er opor.Na hole ato valo vabe odesher description,culture,manasikata bojhano samvab hoto na.Boita lekha hoyeche Pamir valleyr opor.Ki vabe 1 dasyur pallay pare Pamirer Jura name 1 jubak russiar anti czar andoloner mukho naam hoye daralo tai niyei boita...Awesome..simply awesome.Ei Dineshchandra Chattopadhayer kishore der janyo besi boi lekhinnikano janina....kintu uni fatafati likhten jadi likhten tabe.Onar duronto igol charao Vaba series ami parechi..kintu tao vaba series duronto igoler nakher jugyi na..

Bibhutibhusan bandopadhayr r ek adventure uponash Hira Manik Jaleo jathesto valo kintu ager tar sathe kono tulonatei ase na..

Wednesday, November 4, 2009

LPC2148 Timer

I'm a newbie in LPC2148 timer programming.I faced a lot problems for finding out the real value of the datas for creating a 1sec delay.So I deceided to write it clearly in my blog so that no one else would be so hackled as me.
First of all make it clear that you have to include the Timer0 block of the Microcontroller.Its entirely necessary for accurate and effective delay.
The main registers that are required in Timer0 block are Prescaler Register(PR),Timer Counter(TC),Match Register0(MR0).
The prescaler Register increases by unity on each clock cycle until it reaches the value that we have set for it in Prescaler Register.After that value is reached at the advance of the next clock cycle the Prescaler Register counting is reset to start from 0 and the Timer Counter will increase by unity.We have to put a value in the Match Register which will monitor the value at Timer Counter and will be capable of taking appropriate action once the desired value is reached by the TC.

For example if the processor clock cycle(Pclk) is 12MHz i.e., the we need 12,000,000 clock cycle to complete 1sec.Let us put the value 9999(=0x270F) in the prescaler register.Then the Timer Counter will increase after every 10000 clock cycles.
Hence we need 1200 in the MR0 for covering 12,000,000 cycles and producing 1 sec delay.The total delay code will be such---

void delay()
{
// unsigned long check_value = 0x000004B0;
T0PR = 0x0000270F;
T0MR0 = 0x000004B0 ;
T0TCR = 0x01; // the timer0 is activated
while(T0TC != T0MR0);
T0MCR = 0x00000002;

T0TCR = 0x02;

}


If u r using keil compiler this code wont act as expected in the simulator.I warn u never believe the simulator.Alwqays try the code in the hardware.

Manhattan Project


In the year 1939 onwards initiated by then US president Roosevelt America started the creation of the then most deadly mass destructive weapon....Atom Bomb.The Project was headed by Robert J. Openheimer...and he chose Los Alamos for the project site at New Mexico.Quite ironically Los Alamon was a beautiful suburb area which contained a school of which Openheimer was once a student.Its a pity that he pushed the school and it's mentor,his one time teacher,out of their own place to create the Lab there.
There was another person from US army to look after the matter and his name was General Lesly Groves.He was a PhD scholar in physics.

The initial impetaus for the construction came from the Niels Bohr and that too when he understood his favourite student Heizenburg wrongly.Hitler had captured the total control of Germany at that time and was pushing all the non aryans and jews out of the country.Potsdamn in Germany was the famous university at that time which has housed eminent personalities like Albert Einstein.Like a mad power crazy man hitler pushed all the eminent physist out of the country leaving only the pure Germans like Heinzenburg,Oto Hunn and others.
At that time Heinzenburg somehow in a particular meeting spoke in such a topic which made him a pro-Hitler to Niels Bohr...though Heinzenburg was never in support of Hitler.In the mean time Oto Hunn has ruptured the heart of atom and Einstein has came up with the most famous equation of Physics E=mc^2.Along with this the experiment carried on my Madam Courie to get the Radium made the possiblity of getting enormous by rupturing the atoms.As German contains the most eminent league of physist everybody suspected Hitler for taking preparation to construct Atom Bomb.Niels Bohr thought his one time student Heinzenburg would assist Hitler in this act as he posed as the Pro-hitler to Bohr.But the fact was Heinzenburg was never abale to clear his position to his favourite sir..When time approached for Niels Bohr to leave Germany under the command of Hitler Heinzenburg managed a meeting with him but couldn't open up there to clear the confusion between them.Instead at that particular moment he asked Bohr " Do you think its possible for Germany to make an atom bomb " and Bohr being alarmed at this question replied " I'm not sure ".
Niels Bohr leaved for USA very soon after this meeting with the believe in his heart that Germany is preparing for atom bomb and he conveyed this message to the western world.

Leo zsilard who invented the chain reaction,realize the world to be in grave danger and was in search of a man who could aware the US Govt about the looming threat,he along with another scientist(i forgot his name rite now i'l let u knw letter) approached the Grand man of physics...Alber Einstein who was then retired in Long Island of USA.Einstein understood the whole scenarion in a minute and deceided to gave president Roosevelt a letter based on the present situation.It was belief that the content of this letter was wriiten by Leo zsilard,Einstein just put his signature in it.Nonetheless it was deceided to carry to Roosevelt by a wealthy businessman holding powerfull position in USA.Later Einstein also wrote 3 other letter to Roosevelt with the zist being the eminent catostrophy looming over the world by the name of Hitler who could be the proud owner of the atom bomb at any time.
You can see all the letter written by einstein to Roosevelt in the following URL
hypertextbook.com/eworld/einstein.shtml

Thus Manhattan Project was started with four centres all over america,Los Alamos being the prime one.And later i will told about the lives of the scientist here and about the greatest espionage carried out here.