Monday, May 12, 2014

2.4 megasamples per second continuous AVR logic capture

In my last post I got an ATmega8A outputting a 6mbps serial UART data stream.  In this post I'll show how to use that as a way to sample 2 inputs (a USB data signal) at 2.4msps.  The reason the sample rate is not 6mbps/2 is due to the overhead of 1 start bit and 1 stop bit for every byte transmitted.  After accounting for that overhead, the data rate is 4.8mbps, and with 2 bits per sample, the sample rate is 2.4msps.

A look at the USBasp schematic shows USB D- and D+ connected to PB0 and PB1.  With the UART output rate of 6mbps and a 12Mhz CPU clock rate, a full 10-bit frame takes 20 cycles.  To take 4 2-bit samples every 20cycles requires taking a sample and packing those two bits into a byte every 5 cycles.  Single-cycle timing is very difficult to do in C, so I wrote the code in AVR assembler.

capture2:
in sample, PINB
out UDR, tmp
andi sample, 0x03
lsl sample
lsl sample
in tmp, PINB
andi tmp, 0x03
or sample, tmp
lsl sample
lsl sample
in tmp, PINB
andi tmp, 0x03
or sample, tmp
lsl sample
lsl sample
in tmp, PINB
andi tmp, 0x03
or tmp, sample
rjmp capture2
The output data is valid after the first loop itteration, so the first byte (4 samples) should be discarded.  I added code to capture a full 8 bits (at 600ksps), and a simple trigger that waits for the state of the input to change.  For the full code see sniffASM.S and sniff.c.  I've also posted sniff.hex if you don't want to build the code yourself.  With the Rx line (PD0) on the AVR high (idle state), 8-bit capture mode will be use.  If the Rx line is grounded, 2-bit capture mode will be used.

To be able to view the captured data, I like OLS.  It will read a simple text-based input file, which I wrote a small perl progam to output.  It takes a single argument, which is the serial port to read from.  Under windows I had problems getting perl to open high com ports (i.e. com10 or above), so I used the device manager advanced properties to change the port number to com2.  I also wrote a similar program for the 8-bit mode.  One second of samples will be recorded, although there is no technical limit to how long can be recorded.

Here's a portion of a screen shot of OLS showing a USB setup packet followed by 2 re-transmits after ~10us without receiving an ACK:


It's not as fast as the Logic Shrimp, but considering unlimited sampling length and the required hardware is a USB-TTL adapter that costs barely over $1 and a ~$3 USBasp that many people already have, now anyone can afford a basic logic analyzer.


No comments:

Post a Comment