Dangerous Prototypes

Dangerous Prototypes => Bus Pirate Support => Topic started by: hwmayer on September 18, 2010, 02:31:30 pm

Title: BusPirate oscilloscope
Post by: hwmayer on September 18, 2010, 02:31:30 pm
Hey,

I've created a simple python script which is able to draw plots from ADC measurements.
Unfortunately I've noticed that single ADC measure is quite slow so it's hard to say that this is working like a real oscope.
Is it possible to modify the firmware so the measurements were taken faster ?
UART 115200 baudrate  would probably let maximum measurable frequency to be about 3kHz in realtime.

See more here: http://hwmayer.blogspot.com/2010/09/bus ... cript.html (http://hwmayer.blogspot.com/2010/09/buspirate-oscilloscope-python-script.html)
Title: Re: BusPirate oscilloscope
Post by: ian on September 18, 2010, 02:57:48 pm
Nice job!

I can make a new ADC mode that just spits out the readings non-stop until you send a command. Would that be helpful?
Title: Re: BusPirate oscilloscope
Post by: rsdio on September 18, 2010, 02:58:11 pm
The ADC in this PIC has a maximum speed of 500 kHz, but, as you note, the UART bandwidth is the bottleneck.  You might be able to write some interesting firmware which sets some sort of trigger, records samples at full speed (possibly with lossless data compression), and then downloads the snapshot at a lower rate over the UART.  Of course, if you're writing your own firmware then you could potentially change the USB endpoints to handle higher rates.
Title: Re: BusPirate oscilloscope
Post by: hwmayer on September 18, 2010, 03:19:02 pm
Quote
I can make a new ADC mode that just spits out the readings non-stop until you send a command

This would be a good quick fix for now. But how fast will the readings be send ?

Quote
The ADC in this PIC has a maximum speed of 500 kHz, but, as you note, the UART bandwidth is the bottleneck.  You might be able to write some interesting firmware which sets some sort of trigger, records samples at full speed (possibly with lossless data compression), and then downloads the snapshot at a lower rate over the UART

I was thinking about something like a local buffer ( i suppose that there is one used for logic analyzer SUMP) which would be filled at defined speed after a local trigger (e.g rising edge or voltage level trigger) engage the measurement. Then plain data would be transfered by UART and whole thing repeats. This would give similar effect to a real oscilloscope which only displays part of the whole "timeline" so there is no need to send all data in real-time as probably only some part of it would be used.
Title: Re: BusPirate oscilloscope
Post by: ian on September 18, 2010, 04:04:44 pm
The readings would be sent at the max speed possible.
Title: Re: BusPirate oscilloscope
Post by: hwmayer on September 18, 2010, 04:14:53 pm
Ok
It would be great if you could add such mode.
Could you prepare a firmware compilation with those modifications for me ?
Title: Re: BusPirate oscilloscope
Post by: ian on September 19, 2010, 05:17:07 pm
I'll add this and post a nightly for you on Monday.
Title: Re: BusPirate oscilloscope
Post by: ian on September 20, 2010, 12:25:11 pm
Here's a firmware to test. I tesdted in a terminal, and this seems to work. New command 10101 (0x15) sends data as fast as UART will allow. It waits until each byte is done so there's no time distortion from the buffer.

Another possibility would be a 2024 byte sampling o-scope that goes faster and dumps all the samples at once.
Title: Re: BusPirate oscilloscope
Post by: alm on September 20, 2010, 06:06:16 pm
That would probably be a better option, since 2k samples is actually fairly decent, the sample rate is much more limiting in this case. Still, it will never be close to a real scope.
Title: Re: BusPirate oscilloscope
Post by: hwmayer on September 20, 2010, 08:52:11 pm
I've improved my script so it's now working much better.
It now uses new ADC mode. I've estimated that the data rate is about 5720 samples/second, what theoretically should give maximum measurable frequency at about 2.5kHz
In practice aliasing is noticeable at about 1kHz.
I've added a trigger which can sync on rising/falling slope

USAGE:
 f - trigger on falling slope
 r - trigger on rising slope
 s - trigger off
 key_up      - trigger level++
 key_down - trigger level--
 9 - time scale++ (zoom out)
 0 - time scale-- (zoom in)
 q - QUIT
Title: Re: BusPirate oscilloscope
Post by: ian on September 21, 2010, 09:55:22 am
Nice! Thanks for sharing, I'll add it to the wiki too. This feature will go into the next release.
Title: Re: BusPirate oscilloscope
Post by: rct on September 29, 2010, 05:34:38 pm
FYI, In case anyone else is wondering the discussion has been split to a separate topic Use FT232 clock for PIC and higher speeds? (http://http://dangerousprototypes.com/forum/index.php?topic=1004.0)

It's a bit confusing that the forum software sent "topic reply" notifications, that seemed to coincide with the split.  Of course when you click it, there are no new messages and no notification of the split.   Ian posted a message that he was going to split the topic and that the split was done but those both wound up in the new topic.
Title: Re: BusPirate oscilloscope
Post by: ian on September 29, 2010, 05:45:05 pm
Sorry about that. There's no current official location/documentation for the o-scope script, so I broke the other conversation off. I though it left a redirection post, maybe that's only when I move a whole topic.

@hw - may I put your usage instructions and screenshot on the wiki (that means a CC-BY-SA license)? May I add the script to the Bus Pirate SVN scripts folder (what license)?

I started a page here:
http://dangerousprototypes.com/docs/Bus ... cilloscope (http://dangerousprototypes.com/docs/Bus_Pirate:_Python_Oscilloscope)
Title: Re: BusPirate oscilloscope
Post by: hwmayer on September 29, 2010, 11:02:54 pm
sure, you can add it and use on GPL license, just keep some info about me in the script header.
Title: Re: BusPirate oscilloscope
Post by: ian on September 30, 2010, 03:23:27 pm
Thanks! I added it to the SVN here with your header, the GPL, a link to this thread, and the usage info.
http://code.google.com/p/the-bus-pirate ... cilloscope (http://code.google.com/p/the-bus-pirate/source/browse/#svn/trunk/scripts/oscilloscope)

I also added an image and usage info the docs wiki.

Please let me know if there's anything you'd like me to change.
Title: Re: BusPirate oscilloscope
Post by: hwmayer on September 30, 2010, 05:11:58 pm
Quote
Please let me know if there's anything you'd like me to change.

You may add a note that "pygame" lib is needed to run this script.

To install in Ubuntu use this command:
 [tt:]sudo apt-get install python-pygame[/tt:]
Title: Re: BusPirate oscilloscope
Post by: ian on September 30, 2010, 05:29:51 pm
Thanks, done.
Title: Re: BusPirate oscilloscope
Post by: PGMarv on October 14, 2010, 02:49:15 pm
Did anyone get this to work on Windows? I'm stuck here...

Code: [Select]
IDLE 2.6.4      ==== No Subprocess ====
>>>
Entering binmode:
Traceback (most recent call last):
  File "N:BPpyBusPirateLiteoscope_v1.2.py", line 72, in <module>
    if bp.BBmode():
  File "N:BPpyBusPirateLitepyBusPirateLiteBitBang.py", line 51, in BBmode
    self.resetBP()
  File "N:BPpyBusPirateLitepyBusPirateLiteBitBang.py", line 95, in resetBP
    self.reset()
  File "N:BPpyBusPirateLitepyBusPirateLiteBitBang.py", line 61, in reset
    self.timeout(0.1)
  File "N:BPpyBusPirateLitepyBusPirateLiteBitBang.py", line 113, in timeout
    select.select([], [], [], timeout)
error: (10022, 'Ein ungxfcltiges Argument wurde angegeben')
>>>

Win7x64, pyBusPirateLite and Oszi from SVN...
Title: Re: BusPirate oscilloscope
Post by: hwmayer on October 14, 2010, 10:16:08 pm
Did you try running any other scripts that uses pyBusPirateLite ?
Maybe try other Python version, I use 2.6.5
Title: Re: BusPirate oscilloscope
Post by: PGMarv on October 14, 2010, 10:35:02 pm
I will try one of the other Scripts later!

If that also fails, i'll try another Python... Still on 2.6.4 over here ;)
Title: Re: BusPirate oscilloscope
Post by: humberto121 on October 17, 2010, 11:37:29 pm
Help me

[glow=red,2,300]/Escritorio/the-bus-pirate-read-only/scripts/oscilloscope$ python oscope_v1.2.py
Entering binmode:  OK.
Traceback (most recent call last):
  File "oscope_v1.2.py", line 117, in
    voltage = voltage + ord(hex(measure[1]))
IndexError: string index out of range
[/glow]
Title: Re: BusPirate oscilloscope
Post by: ian on October 18, 2010, 08:32:53 am
Hi humberto121 - what version of the Bus Pirate firmware are you running? Only the latest version (5.8) includes the command for this script. It looks like it gave an unexpected reply, so that's my best guess.
Title: Re: BusPirate oscilloscope
Post by: drakelive on October 21, 2010, 07:40:37 pm
What is the problem?
I'm working on a 64bit ubuntu 10:10


Drake


drake@drake-laptop:~/Scrivania/Bus Pirate/Oscope$ ./oscope_v1.2.py
Traceback (most recent call last):
  File "./oscope_v1.2.py", line 54, in
    bp = UART(BUS_PIRATE_DEV,115200)
  File "/home/drake/Scrivania/Bus Pirate/Oscope/pyBusPirateLite/UART.py", line 47, in __init__
    BBIO.__init__(self, port, speed)
  File "/home/drake/Scrivania/Bus Pirate/Oscope/pyBusPirateLite/BitBang.py", line 48, in __init__
    self.port = serial.Serial(p, s, timeout=t)
  File "/usr/lib/python2.6/dist-packages/serial/serialutil.py", line 166, in __init__
    self.open()
  File "/usr/lib/python2.6/dist-packages/serial/serialposix.py", line 175, in open
    raise SerialException("could not open port %s: %s" % (self._port, msg))
serial.serialutil.SerialException: could not open port /dev/ttyUSB0: [Errno 16] Device or resource busy: '/dev/ttyUSB0'
drake@drake-laptop:~/Scrivania/Bus Pirate/Oscope$
Title: Re: BusPirate oscilloscope
Post by: Sjaak on October 21, 2010, 07:48:20 pm
You must define the right com port:

Quote
serial.serialutil.SerialException: could not open port /dev/ttyUSB0: [Errno 16] Device or resource busy: '/dev/ttyUSB0'

I could also be that an other program is using it and preventing access for other programs.
Title: Re: BusPirate oscilloscope
Post by: drakelive on October 21, 2010, 08:42:20 pm
Indeed, the device is busy! but by whom?
I tried to connect the Arduino and the terminal is perfect

Any ideas?


Thanks
Drake
Title: Re: BusPirate oscilloscope
Post by: Sjaak on October 21, 2010, 09:01:27 pm
My best guess the buspirate isn't on ttyUSB0.

Unplug the buspirate, start an xterm with the command 'tail -f /var/log/messages', plug the buspirate in. Somewhere in the outputted messages it should say 'Found usb serial bridge' and something with starting with '/dev/tty' Your buspirate is connected here.

I type this from  the top of my head so the exact messages could be different (and distro dependant).
Title: Re: BusPirate oscilloscope
Post by: PGMarv on October 22, 2010, 12:11:41 pm
Updating Python did not help here but now at least i know why there is a problem. It is Windows/Unix-Difference!

From the Python Docs (http://http://docs.python.org/library/select.html):
Quote
select.select(rlist, wlist, xlist[, timeout])¶
This is a straightforward interface to the Unix select() system call. The first three arguments are sequences of ‘waitable objects’: either integers representing file descriptors or objects with a parameterless method named fileno() returning such an integer:

rlist: wait until ready for reading
wlist: wait until ready for writing
xlist: wait for an “exceptional condition” (see the manual page for what your system considers such a condition)
Empty sequences are allowed, but acceptance of three empty sequences is platform-dependent. (It is known to work on Unix but not on Windows.) The optional timeout argument specifies a time-out as a floating point number in seconds. When the timeout argument is omitted the function blocks until at least one file descriptor is ready. A time-out value of zero specifies a poll and never blocks.

The return value is a triple of lists of objects that are ready: subsets of the first three arguments. When the time-out is reached without a file descriptor becoming ready, three empty lists are returned.

Among the acceptable object types in the sequences are Python file objects (e.g. sys.stdin, or objects returned by open() or os.popen()), socket objects returned by socket.socket(). You may also define a wrapper class yourself, as long as it has an appropriate fileno() method (that really returns a file descriptor, not just a random integer).

Note File objects on Windows are not acceptable, but sockets are. On Windows, the underlying select() function is provided by the WinSock library, and does not handle file descriptors that don’t originate from WinSock.

And that's what happens in pyBusPirateLiteBitBang.py, line 113:
Quote
select.select([], [], [], timeout

So that does not work under Windoze :/
Title: Re: BusPirate oscilloscope
Post by: benpheil on October 22, 2011, 05:56:42 pm
Hey all.  I am trying to modify this script to be able to write the sampled data to a .mat file in addition to displaying on the screen.  I'm having trouble getting the sampling frequency right for all time scale settings.  Any ideas?
Title: Re: BusPirate oscilloscope
Post by: Hubert on September 10, 2014, 12:54:58 pm
Sorry if I warm up an old thread.
You write the bottleneck is the 115kBaud of the FTDI Chip.
But when I look at the FTDI Website they write the FT232 works with baudrates from 300Baud up to 3MegaBaud.
You will find more in the FTDI knowledge base on the web. Sorry, as a new user I cannot post a link.
Why don´t u use the higher baudrates for the oscope?