USB Protocol Analyzer
Now that the tools to program the SiliconBlue FPGA on the DSO Quad are freely available, it's only a matter of writing some code, right? My intent was to work on my Logic Analyzer for the Application Competition, but then I got a bit carried away..
I thought I would just quickly make a proof-of-concept using the USB PHY from OpenCores. It turned out that it worked pretty well, and I decided for write an application to make DSO Quad work as an USB protocol analyzer. I also decided to write it completely in Pawn, testing out the limits of what can be done in QuadPawn.
Most of the processing happens on the FPGA side. The FPGA receives and decodes USB protocol, and filters the packets to make the best use out of the limited RAM memory. Even though there is a lot happening on the FPGA, only 1300 out of 4000 total logic elements are in use.
Most interesting part of the FPGA code is probably the repeat filter. Its purpose is to eliminate repeating sequences of packets, such as "IN, NAK", which occur when host is polling for data. Processing variable-length packets on an FPGA is not very easy, but I managed to solve the problem by using a two-level design.
The bottom part of the repeat filter is a packet buffer, which takes simple commands such as "read packet", "compare packets", "write packet". The top part is written like a computer program, albeit implemented without a processor. It executes a program consisting of 60 instructions to detect the repeats and to eliminate them.
Most problematic part of programming the DSO Quad FPGA is the slow speed of it. I wanted everything to run at 72 MHz, synchronous to the CPU clock. Theoretically you could receive USB at 36 MHz also, or then I could have made separate clock domains for the USB PHY and other logic. But I went with 72 MHz, and after a few optimizations managed to achieve it. Most important is to pipeline all the things, which does in fact work quite well for FPGA architectures. There is a register available in each logic element, so why not use it? Only problem is that the code gets a bit convoluted as a result of the pipelining.
Download here. Source code here. To run, you need to have QuadPawn 0.06 or newer. It can be downloaded from the same page. To install, copy usb.amx and usb.fpg to the USB memory when QuadPawn is running. Press refresh and you should see it in the menu.
To use this program, you need some way to connect probes to the USB signals. Easiest way is to hack some USB extension cable for the purpose. Channel A must go to D- and channel B to D+. Plug it in and you should see some packets on the screen.
The program can save as BMP (screenshot), TXT or the raw data. Raw data is mostly useful for debugging, because it's not any standard format. I haven't studied how easy it would be to feed it to e.g. wireshark.
Currently it only supports USB full-speed. Support for low-speed wouldn't be too hard to add, but high speed is too fast for this hardware. The buffer space is only 8 kB, but that is probably enough for simple hobbyist devices.
– Petteri Aimonen on 13.1.2013