$10 O-Scope revisited

What are you developing?
Post Reply
User avatar
ahull
Posts: 1650
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

$10 O-Scope revisited

Post by ahull » Sat May 09, 2015 12:56 pm

Over the last few days I've been playing with Ray's STM32 Oscilloscope idea.


Image


Some preliminary results are available here..

https://github.com/pingumacpenguin/STM3 ... ontruction

My take on the Scope Sketch is here..

https://github.com/pingumacpenguin/STM32-O-Scope

Its a fun little project and well worth building just for the entertainment value. Currently bandwidth is a little limited, but with a little extra effort it would be possible to make a fully functional 2MHz Scope for next to nothing. Ideal for probing i2c, audio and other low speed signals.
Last edited by ahull on Sat May 09, 2015 8:21 pm, edited 1 time in total.
- Andy Hull -

madias
Posts: 813
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

Re: $10 O-Scope revisited

Post by madias » Sat May 09, 2015 1:18 pm

Interesting project!
I own a DSO nano (also STM32 powered) with benq firmware and the tools is really useful (I use it for audio data).
It would be interesting if there is a possibility to get 2 (or more) channels out of it (with half speed or so).

User avatar
mrburnette
Posts: 1877
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: $10 O-Scope revisited

Post by mrburnette » Sat May 09, 2015 3:02 pm

Wow!

Your implementation and enhancements surely takes the idea into the usable category!

Thanks.


Ray

User avatar
ahull
Posts: 1650
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: $10 O-Scope revisited

Post by ahull » Sat May 09, 2015 8:55 pm

madias wrote:Interesting project!
I own a DSO nano (also STM32 powered) with benq firmware and the tools is really useful (I use it for audio data).
It would be interesting if there is a possibility to get 2 (or more) channels out of it (with half speed or so).
I have seen the DSO nano and indeed almost bought one, but opted instead to buy a second hand analog scope, with 40MHz bandwidth...

I couldn't really justify a big spend on something that would only get occasional use, since I have access to a couple of scopes in my office, so that meant second hand, and since my background is electronics I opted to pick up a broken one and repair it. Details of my repair are here http://www.badcaps.net/forum/showthread.php?t=42768 and here http://www.electro-tech-online.com/thre ... pe.142398/

The DSO nano uses a better approach to both sampling and triggering, which I may adopt. As it currently stands, the Arduino sketch uses possibly the least effective method, a polling loop of analogRead() and thus is limited in both bandwidth and triggering ability.

See the manual in the firmware download for more details of how the DSO manages the 1MHz bandwidth and triggering tricks. http://www.seeedstudio.com/forum/viewto ... 1793#p5965

My idea was to demonstrate the abilities of the Arduino for STM32 port, rather than to produce a DSO nano clone. I am however now hooked on the idea of improving the current sketch to see what is possible. If I can get 2 x 1 MHz sampling I'll be happy.

The DSO uses the STM32F103VBT6 - and the BenF firmware is open source so in theory you should be able to run the firmware on a STM32F103VBT6 dev board. Equally it should be possible to adopt or adapt the ADC methods used and port them to the STM32F103C8XX

My next target is therefore likely to be ADC Reading + DMA - with an eye on getting 2 x 1MHz or 1 x 2MHz sampling and better triggering - assuming I can find the time. If anybody else has any suggestions feel free to post them.

Currently by unwrapping the analogRead() function and using the "how it works" of this to spin a tight adc_read() loop I can manage 6000 samples in 41152 us or around 145,793 samples per second which gives a theoretical bandwidth of 72,900 Hz and although this is pretty good, (plenty for audio work) we can do better.

Edit: Actually I'm grabing 1024*6 = 6144 samples so that actually equates to 149,300 samples/sec or 6.6979 us per sample for a theoretical bandwidth of 74,650 Hz ... or to put it another way about 15% of the theoretical maximum... we can certainly do better.
- Andy Hull -

User avatar
ahull
Posts: 1650
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: $10 O-Scope revisited

Post by ahull » Sun May 10, 2015 1:32 pm

A quick change to the code (no DMA or other trickery involved) allows me to push the sampling loop down to 3us per conversion...

Code now reads...

Code: Select all

  const adc_dev *dev = PIN_MAP[analogInPin].adc_device;
  int pinMapPB0 = PIN_MAP[analogInPin].adc_channel;
  adc_set_sample_rate(dev, ADC_SMPR_13_5);
  
  for (uint16_t j = 0; j <= maxSamples - 1 ; j++ )
  {
    dataPoints[j] = adc_read(dev, pinMapPB0);
  }
This effectively replaces most of the overheads of analogRead() without going for the slightly more complex route of DMA + ISR ...
I wonder if I can do better :D

My loop now does 6144 samples in 18976us giving an average sample time of 3.088541667 microseconds. Pushing the sample rate of the ADC any further - to ADC_SMPR_1_5, results in too much noise and attenuation to be useful. ADC sample rate of ADC_SMPR_7_5 is OK but the noise floor is obvious, so ADC_SMPR_13_5 seems the best compromise. This means I am sampling at 323,777Hz for an analog bandwidth of 161,888 Hz

What this means in practical terms is that you should be able to observe things like 100kHz i2c (but not resolve individual bits of 400kHz i2c) and the carrier of an IR remote control (30 to 56kHz) without any difficulty.
- Andy Hull -

User avatar
ahull
Posts: 1650
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: $10 O-Scope revisited

Post by ahull » Sun May 10, 2015 2:29 pm

Furthermore.... :D ... if we move the register setup outside of the loop. (See https://github.com/leaflabs/leaflabs-do ... erview.rst for the secret sauce that give the clues as to how to do this).

Code: Select all

const adc_dev *dev = PIN_MAP[analogInPin].adc_device;
  int pinMapPB0 = PIN_MAP[analogInPin].adc_channel;
  adc_set_sample_rate(dev, ADC_SMPR_13_5);
  adc_reg_map *regs = dev->regs;
  adc_set_reg_seqlen(dev, 1);
  regs->SQR3 = pinMapPB0;
  
  for (uint16_t j = 0; j <= maxSamples  ; j++ )
  {
    regs->CR2 |= ADC_CR2_SWSTART;
    while (!(regs->SR & ADC_SR_EOC))
        ;
    dataPoints[j]=(regs->DR & ADC_DR_DATA);
  }
... we reduce the sample time to around 2.76 microseconds. This gives us 362306 samples per second or 181,153 Hz bandwidth.
- Andy Hull -

User avatar
ahull
Posts: 1650
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: $10 O-Scope revisited

Post by ahull » Sun May 10, 2015 7:43 pm

Dropping the sample time to 7.5 clock cycles gives us 427616 samples/sec and a bandwidth of 213,808 Hz but we start to see some ringing in the signal path, which can be trimmed out with a little additional capacitance in our probe.
- Andy Hull -

User avatar
ahull
Posts: 1650
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: $10 O-Scope revisited

Post by ahull » Sun May 10, 2015 7:54 pm

Dropping the sample time still further to 1.5 clock cycles still yields a usable result, with 6144 samples taking a mere 11792 microseconds.

Image

We see more artifacts on the signal, but it is still usable and with a sample now taking 1.919270833 microseconds, we are getting pretty close to the theoretical minimum ADC conversion time. This gives us 521,031.207598372 samples per second, for a bandwidth of 260,515.603799186 Hz (i.e. just over 260.5 kHz) which 'aint bad.

The signal could be cleaned up slightly by using a proper scope probe and adjusting its trimmer. I'll give that a shot tomorrow. The lack of a separate ADC ground plane and clean ADC Vref on these little boards is noticeable at this kind of frequency. Also the volts per division is out, not sure if that is due to attenuation due to the higher frequency or problems with my code.

If you want to learn a little bit about scope probes and high frequency signals take a look here..
https://www.dfad.com.au/links/THE%20SEC ... 0OCt09.pdf

I'm happy with that. If I get a bit of spare time, I may look at using the ADC with DMA - but if anybody else wants to take a crack at beating 1.9 uS per conversion *with* DMA and an interrupt service routine, the gauntlet has been thrown. :D

The updated sketch is on github. https://github.com/pingumacpenguin/STM32-O-Scope
- Andy Hull -

User avatar
ahull
Posts: 1650
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: $10 O-Scope revisited

Post by ahull » Sun May 10, 2015 10:54 pm

One thing I may have failed to mention. The sketch can also spit out the data on the USB serial port, so you can analyze it further. Here is a sample plot using gnuplot http://www.gnuplot.info/ of the 6144 samples in one buffer taken with the latest sketch of a slightly noisy pwm signal.

Simply type s <enter> on the Serial Console and it fires back the data points.
Paste these into a csv file and feed it to gnuplot.

gnuplut> plot 'serial.csv'

Image

We can zoom in...

gnuplot> plot [500:2000] 'serial.csv' with lines

Image

.. you get the idea...
- Andy Hull -

User avatar
mrburnette
Posts: 1877
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: $10 O-Scope revisited

Post by mrburnette » Mon May 11, 2015 2:25 am

I'm impressed.

My initial idea was to just provide a low-cost teaching aid, but the current implementation is well beyond that! Great work.

Ray

Post Reply