Thursday, July 25, 2013

chapter4_Enumeration_part3

接着昨天的,更正一下,usb不是通过D+,D-的高低来encode数据的,是通过电平的变化来的,0代表变化(从高到低或者从低到高),1代表没有变化,而且每次连续的6个1后会加入一个0,就是传说中的bit stuffing + NRZI(none-return to zero inverted) encoding.
Addressed State
话说device重启过后进入default state了。
然后Host发送一个Get Descriptor 请求。这个descriptor感觉是让usb可以变得这么灵活,这么通用的关键所在。这个慢慢看,慢慢谈。
这次请求的目的是想知道默认情况下,endpoint 0可以接收的最大的packet数目。endpoint其实就是usb device上的用来传输接收数据的东西,一个usb device可以有多个,但最少最少得有一个,就是endpoint 0, 这个是在enumeration默认的。其实最开始就发这个询问支持的最大packet数目,我觉得有些不科学啊。见面了先得问名字吧。
好,完成了这个请求之后,windows 情况下host又会让device重启一次。linux 系统不清楚。
等重启过后,host给device 一个独特的地址,前面说过,7位的。“你就法号悟空吧”
device欣然接收,这个地址是一直到device 拔下来,端口被reset了,或者系统重启了。
这时候,device就进入Addressed state了。

Configured State
这次host再次发送Get Descriptor(Device)请求,请求地址是刚刚device新的地址哦。从device那得到比第一次丰富些的信息。这个device支持多少configuration, and so on.
然后host得寸进尺,向device请求各个configuration descriptor. 终于,host有了足够多的信息去挑选一个合适的driver for that device. 然后driver向device发送一个set configuration的请求。device处理了这个请求。然后进入configured state鸟。
圆满圆满。
enumeration完成。windows机器上这个时候大概会有个pop up 说有新的device连接了。




Wednesday, July 24, 2013

chapter4_Enumeration_part2

咱们今天接着谈enumeration.













看USB的标志和海王手中拿着的三叉戟是不是有异曲同工之妙,哈哈。
基本意思就是不管是圆的,方的,三角的,USB都可以连,所以叫universal serial bus 嘛。
这是题外话了。
During enumeration,总共有这么4个states: Powered, Default, Address, and Configured.
POWRED:
当把一个usb device插到usb接口上,比方说把你的手机插到你电脑USB口上,这个时候你手机开始充电,就是因为现在你的手机是device,然后电脑是host, host可以给你的手机供电。最开始的时候,最多最多可以提供100 mA电流。这就是powered state了。
DEFAULT:
device插上之后,这里要提一下,usb的信号线有两个D+,D-, 是differential signal.就是D+是高电瓶,D-是低电瓶的时候是logic 1. D+低电瓶  D-高电平时,是logic 0。 device插上去之前,device 端D+,D-有一个是高电平的, 但是host端两个都是低电平的。插上去之后,host端检测到有一个变高了,于是乎就知道有东西attached了。而且D+高,说明是full-speed或者high-speed device, D-高的话,就是low-speed device. 这三个speed的区别就是:
full-speed: 12 Mb/s
high-speed: 480 Mb/s
low-speed: 1.5 Mb/s
当然了还有USB3.0的super speed(5 Gb/s).这里不做讨论。
然后host等上个至少100 ms, 为的是等信号稳定。然后就发个信号给device让它重启一下,这个信号就是把D+,D-都拉低。这个reset的过程usb specification上建议是10-20 ms. 今天实际测了一下,那个usb device reset时,两个信号线被拉低了大概30ms.和spec上的有点点出入,但无伤大雅。重启完了,就进入了default state鸟。
又要睡觉了,明天再看。


Tuesday, July 23, 2013

chapter4_Enumeration_part1

我边读边写,要不等会看完了也就忘记完了。
这个enumeration中文翻译就是列举,听起来挺别留,不过光从字面意思就是能列举,就允许可以有多个。 具体多少个呢,我来google 一下。
答案是127个,原来USB device enumeration过后会有个7 bit的地址,所以2的7次方就是128,然后我估计地址0000000是reserved,所以就是127个了。
Enumeration的过程包括了assign address, reading descriptors from the device, assign and load a driver. 打个不恰当的比喻,就好比有这么一个风情万种的女子,才高N斗,精通100多种语言,于是乎许多人想来一睹其风采。女子怕麻烦,与其记住每个人的名字,她只是给每个人一个号码,就像探监的时候,XXX,你X来看你啦。给号码的时候,她问对方是用什么语言的,然后她自己也做下记录,跟这人交谈之时就好用那个语言。(写完感觉要是把这个女子换成一高僧,立刻这篇文章升华了好几个摄氏度)
说回enumeration, 每次当系统启动的时候,hub就会通知host,有没有usb devices attached. 系统运行过程中,如果有devices attach,那么hub也会通知host. 什么是hub,就是电脑上的usb口。什么是Host,就是住持。hub就是庙里的小和尚。每天早上起来,去开门,发现一诚心向佛之人横躺在门前,立刻大喊:“住持,有客人,还有空房么”. 或者是有人砰砰砰来敲门,小和尚开门大叫“住持,踢馆的”。 但是,不管你是找老和尚帮忙,还是找他PK,你都得满足最起码三点: 01: support control transfers 10: support standard requests 11:endpoint zero. 这个比较容易理解,我就不解释了。
During enumeration,总共有这么4个states: Powered, Default, Address, and Configured.
到点了,睡了,明天晚上再看。



Monday, July 22, 2013

USB

“USB又名你SB,是美国USA的弟弟。”记不得是什么时候在哪里看到的,应该是微博吧。
最近要test usb啊,这玩意儿真TM是big pain in the ass.

一看它的datasheet,足足600多页,谁发明的这东西,这不是给写driver的同志们添乱么。
要是按部就班的把这东西看完,也得花好几个月时间啊。得想想有什么比较效率的方法。
看到一本USB COMPLETE的书,500页,虽然也多,但里面更多的叫我们怎么用,这个正是我需要的。
准备啃这本书吧。
这书总共20章,

1. USB basics  NO
2. Inside USB transfers YES
3. A transfer type for every purpose YES
4.  Enumeration: how the host learns about devices. YES
5. Control transfers: YES
6. Chip choices:  NO
7. Device classes: (HID) NO
8. How the host communicates: YES
9. Matching a driver to a device. NO
10. Detecting devices YES
11.12. 13.  Human interface devices: YES
14. using winUSB for vendor-defined functions: NO
15. All abut hubs: NO
16. Managing Power:(power needs) YES
17 Testing and debugging NO
18. Packets on the Bus  NO
19. The Electrical and mechanical interface  NO
20. Hosts for embedded systems NO

重新排列一下:
4.  Enumeration: how the host learns about devices. YES
10. Detecting devices YES
2. Inside USB transfers YES
3. A transfer type for every purpose YES
5. Control transfers: YES
8. How the host communicates: YES
11.12. 13.  Human interface devices: YES
16. Managing Power:(power needs) YES
需要看的缩减到10章,祝我好运吧!

Thursday, October 20, 2011

Android app project_day3

Finally, ice cream sandwich reveals its face with a couple of awesome new features.
Google released the android 4.0 sdk as well.
It improves the NFC  API (if not a lot) quite a bit.
For instance:
1. when creating a mime type ndefRecord
previous version:
we have to write following code
NdefRecord tiredRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,"youMimeType".getBytes(Charset.forName("US-ASCII")),new byte[0], data.getBytes(Charset.forName("US-ASCII")));

new version:
NdefRecord happyRecord = createMimeRecord("yourMimeType",data.getBytes());

Much cleaner!

To be continued

Thursday, September 22, 2011

Android app project_Day2

Yesterday started my android app project.
The first step of the project is to implement a peer to peer communication using NFC.
It would be very helpful if we can view the android source code. 
From this site http://www.vogella.de/articles/Android/article.html, it mentioned how to view the source code in Eclipse. 

2.4. Android Source Code

The following step is optional.
During Android development it is very useful to have the Android source code available as Android uses a lot of defaults.
Haris Peco maintains plugins which provides access to the Android Source code code. Use the Eclipse update manager to install two of his plugins. Update site: "http://adt-addons.googlecode.com/svn/trunk/source/com.android.ide.eclipse.source.update" and "http://adt-addons.googlecode.com/svn/trunk/binedit/com.android.ide.eclipse.binedit.update".
More details can be found on the project website .

This is awesome!


The thing I am going to do today is to understand how NFC works. This is the very basic but vital step through the whole project.

Wish me luck!


Searched a bit, and found this site might be the most detailed.


"NFC works using magnetic induction: a reader emits a small electric current, which creates a magnetic field that in turn bridges the physical space between the devices. That field is received by a similar coil in the client device, where it is turned back into electrical impulses to communicate data such as identification number, status information, or any other information. So-called 'passive' NFC tags use the energy from the reader to encode their response, while 'active' or 'peer-to-peer' tags have their own power source and respond to the reader using their own electromagnetic fields.

Like RFID, NFC works in the 13.56MHz radiofrequency spectrum, using less than 15mA of power to communicate data over distances that are usually far less than 20cm. Tags typically store between 96 and 512 bytes of data and transfer data using at speeds of 106Kb/s, 212Kb/s, 424Kb/s or 848Kb/s – enough to move small pieces of information virtually instantaneously, as is essential in high-volume transport applications.
"




Sunday, April 10, 2011

11-April-2011

The Australian government recently made a proposal to make all cigarette plain packaged in order to reduce the number of dead from smoking every year in Australia.
The color of the new cigarette package will be olive green which , researchers showed, is the most unattractive color. Also the brand name of the cigarette printed on the package will be very small.

The opposition said that there is no evidence that this is going to work and the British American tabacco company claimed that if the government insists to proceed, they will ended up in the court.