Decoding the NZRI data (USB SNIFFER)

Good day to all.
I'm currently working on a lil project to see what my usb devices are up to.

Why: I'm troubleshooting a problem related to an underpowered PSU which make the keyboard freeze. Seeing what is being sent and being able to analyze it would be tremendous.

Goal: Using software, decode (and maybe encode ^^) NZRI data from the Usb Lines. Output Data to a serial terminal.

Parts done: So far I got a USB sniffing dongle I built. It gives me acces to the 5 cables (yes,USB has 5 lines,but 1 is irrelevant most of the time and they are: Vpp,D-,D+,Gnd,Shield. The shield is the connector ground but for some odd reason,it's most of the time not directly connected to the normal ground.Anyway knows why?). It also gives me acces to a power LED and a 3pin header is used for Voltage/Amp monitoring (usefull when we suspect underpowered stuff ;P) I'm gonna post a pic if you guys want one as I think this lil piece of hardware (even tho it's simple) is pretty usefull when dealing with USB to a certain level.

What I need help with: As I am no coding guru (sadly enough,I'm a mechanical engineering tech tho ^^) I need someone to help (and by help I mean,do most of the complex coding =D) on making the software on an Arduino decode the NZRI data from the 2 lines and spit it out into it's own serial port. A second objective would be to be able to inject packets like the "wakeup" call when there's no traffic (otherwise it would screw up the data lol) but that's not primordial.

This project is open source,as I am willing to share all if not beyond all the knowledge acquired while trying to make it work.
Thanks for reading and

Let's make usb accessible to everyone! =D

The BadWolf

Funny, there was a similar post to this just minutes ago.

It sounds like you are trying to recreate devices that cost $400 and up:

http://www.totalphase.com/solutions/apps/usb_analyzer_guide/

Should give you a sense for how doable this is :wink:

Why: I'm troubleshooting a problem related to an underpowered PSU which make the keyboard freeze. Seeing what is being sent and being able to analyze it would be tremendous.

I would kinda doubt it. If it's a power supply issue then the data that is being sent back and forth to the keyboard is unlikely to contain clues to failure. A power supply undervoltage transient could lock up the keyboard on an electrical level, without any data involvement.

Goal: Using software, decode (and maybe encode ^^) NZRI data from the Usb Lines. Output Data to a serial terminal.

This is really tough. Even at the lowest bit rate of 1.5 Mbps that's one bit every 667 nanoseconds. A 16 MHz AVR microcontroller has a clock period of 62.5 nanoseconds, so there are only ~10 clock periods in each USB bit. That's insanely fast data and a woefully small amount of time for the AVR to actually do anything with it. Forget about 12 Mbps USB.

People have done some amazing things with AVR's, including V-USB which could very well be the thing you're looking for, but I'm still skeptical :wink:

--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons

Thanks for the fast reply!

Also,I forgot to post this:

http://www.xs4all.nl/~dicks/avr/usbtiny/index.html

This guy got it working with an ATtiny =P and says that any Atmega would be able to do it fairly easily.
The number of cycles available for the controller is exactly 8 when running at 16mhz (from the guy's page)

And for the underpowered psu: When the keyboard lock itself,it cycle through a power mode (lights goes out and back on,without the usb disconecting) So by monitoring the voltage,amp and data,I could see if it's the keyboard's fault or if a drop in supply is causing garbage data.

Also,If an ATtiny can handle USB correctly, then I'm pretty sure an arduino can,and if it's too complicated,then we got 2 options to check:

1-Use an RS232 converter chip in between,or just skip the arduino and use that lol.

2-I got a STM32 discovery board with LOADS of processing power,but i lack the knowledge to program it -_-'

"When building new devices,we shall not hope for simplicity,or this device would already been done." =P

thanks!

lordzeppo:
Goal: Using software, decode (and maybe encode ^^) NZRI data from the Usb Lines. Output Data to a serial terminal.

Ah, no, Mr Wolf. Much as I would like to say it could be done, I have been spending quite some time recently analyzing USB, and have to say that it is one of the more complex protocols I have seen.

For a start, of the D+ and D- lines you have a number of different states they can assume. The normal one is that D+ and D- are the opposite of each other. However, depending on whether you have high or low-speed USB one is high and the other is low (the "idle" condition). And then they are both low for a while for a "reset" condition or a "keep alive" condition (the length tells you which).

Then you have to analyze what bits you get by measuring the length of time that the transition does not occur. That is, a short transition would be a 0, and a longer transition one or more 1 bits, but after 6 ones you also get a zero which doesn't count as a zero.

And that only gives you low-level bits. Then you move up to packets, and after that, transactions.

There is no way you can do that in 8 clock cycles. I would love to say there is, because I can see a nice project of doing an Arduino USB-analyzer. And of course that is low-speed USB. Modern keyboards, mice etc. are likely to do high-speed USB which is 10 times as fast.

What might be possible is basically a logic analyzer, where you simply sample the pulses and store them in memory. There simply won't be time to pump them out the async port. Unfortunately, there are a lot of samples in USB.

Sad Wolf--> =/

Thanks for the hint then. I think I'll go with this then:

http://www.zeroplus.com.tw/logic-analyzer_en/

Embeded in the system: USB analysys ^^