• 0
vinodcxlv

Controlling a DAC with SPI Protocol Analyzer

Question

Hello,

What is the difference between the "custom" and "sensor" tabs on the Protocol Analyzer in Waveforms? Are they used together? Where the custom tab gives the user the ability to write their own custom SPI Protocol and use it in the Sensor tab? And can I use them to develop a script that would control an SPI compatible 12-bit DAC. The goal is to read the 12 bit digital data from a file, with data ranging from 0x0000 to 0xFFFF (16 bits technically since the 2 MSB determine the DAC Power Modes and the 2 LSB are don't cares) and outputting that data from the Analog Discovery 2 to the 12-bit DAC

Share this post


Link to post
Share on other sites

16 answers to this question

Recommended Posts

  • 0

Hi @vinodcxlv

The 'sensor' tab is for sensors, ADCs. The timing of the loop function is precise, given by the device and it is identical in each cycle. The software reads the response of the devices.

You can use the 'custom' tab or the Script tool to read the file and send it over SPI.
See the examples and Help tab.

image.thumb.png.cb312ec18c6be30325736a12943ca3d5.png

image.png.5b718bcdc30fa1dad49acefbf93ebaa7.png

Share this post


Link to post
Share on other sites
  • 0
18 hours ago, attila said:

Hi @vinodcxlv

The 'sensor' tab is for sensors, ADCs. The timing of the loop function is precise, given by the device and it is identical in each cycle. The software reads the response of the devices.

You can use the 'custom' tab or the Script tool to read the file and send it over SPI.
See the examples and Help tab.

image.thumb.png.cb312ec18c6be30325736a12943ca3d5.png

image.png.5b718bcdc30fa1dad49acefbf93ebaa7.png

Thank you for the feedback. To read the file in the custom tab would I just need to comment rgData variable and provide the path? Does the file need to be JSON or can I just have my configurations (clock, select and data) in a text file?

Share this post


Link to post
Share on other sites
  • 0

Hello @attila

I've provided a screenshot of the logic I want to implement and a screenshot of the script I have came up with. However, I am receiving "undefined." 

Am I implementing to code correctly or is there something I am missing?

If there was an issue with my syntax then the window would output something along the lines of "Syntax Error" correct?

TestPlan Undefined Output.PNG

TestPlan Values.PNG

Script Logic.jpg

Share this post


Link to post
Share on other sites
  • 0

Hi @vinodcxlv

var rgFile = (""+FileRead("~/Desktop/default.txt")).split(", "); // 1, 2, 3
var rgSend = []
rgFile.forEach(function(v){rgSend.push(v<<2)})
Start()
Write(16,rgSend)
Stop()
return("done "+rgSend.length+" bytes")

image.thumb.png.f38310b8426609421ba96fdd7035e390.png

Share this post


Link to post
Share on other sites
  • 0
On 1/16/2020 at 12:45 AM, attila said:

Hi @vinodcxlv

var rgFile = (""+FileRead("~/Desktop/default.txt")).split(", "); // 1, 2, 3
var rgSend = []
rgFile.forEach(function(v){rgSend.push(v<<2)})
Start()
Write(16,rgSend)
Stop()
return("done "+rgSend.length+" bytes")

image.thumb.png.f38310b8426609421ba96fdd7035e390.png

Hello @attila

I forgot to turn on my notifications for this post, I was able to implement my logic using two for loops but your example using the read and for each functions will simplify my code tremendously. Thank you so much!

I had another question, is there anyway of incorporating the voltmeter function (or is it just for the electronic explorer?) so that the instrument reads the analog voltage of each converted 16 bit word into a register or a file for post analysis?

 

You've been tremendous help, thanks again!

voltmeter function.PNG

Share this post


Link to post
Share on other sites
  • 0

Hi @vinodcxlv

If you want to access multiple instruments use the Script tool.

The mentioned voltmeters are dedicated input on the EE board.
On AD2 you can use the oscilloscope inputs through Scope, Voltmeter, Logger, Spectrum... instruments.

Protocol.SPI.Start()
Protocol.SPI.Write(...)
Protocol.SPI.Start()

if(1){
  Scope.single()
  Scope.wait()
  var v1 = Scope.Channel1.measure("Average")
}else{
  wait(1.1)
  var v1 = Voltmeter.Input.Channel1.DC.value
}

 

Share this post


Link to post
Share on other sites
  • 0

Hello @attila

If we use the Script tool to write to the DAC over SPI how would you initialize the Clock (polarity, phase, polarity) and Active signals. I see that in the examples in the Protocol Analyzer custom the user would initialize the Active signal low via "Select.Active.value = 0" is this the same for the Script Tool? Also how would you go about setting your DIO pins for your 3 signals? 

 

Also I noticed there's a delay between the time the select signal is pulled low and data & clock signals are outputted. Is there a way to output the clock and data signals, implement a delay then draw the select signal low?

Edited by vinodcxlv

Share this post


Link to post
Share on other sites
  • 0
Posted (edited)

Hi @vinodcxlv

You can use the Ctrl+Space to see the available child nodes and properties.

image.png.66af47634acfe173b6d1c0064beb09ee.png

 

You can use the wait function for delay.

Protocol.SPI.Write(8, [1,2,3,4])
wait(0.125) // 125 ms
Protocol.SPI.Stop()

Edited by attila

Share this post


Link to post
Share on other sites
  • 0

Hello @attila

I think there's a slight misunderstanding. The issue I'm having is that the select signal is toggled low before the data/clock signal. I saw somewhere on the forums that the select signal can be synchronized with the data/clock signals by accessing the pattern generator through the script tool.

I am trying to do something similar to what you did in the project attached below: Only difference would be that the data signal would depend on this part of the code 

var rgFile = (""+FileRead("~/Desktop/default.txt")).split(", "); // 1, 2, 3

var rgSend = []

rgFile.forEach(function(v){rgSend.push(v<<2)})

 

Thank you in advanced!

 

 

Share this post


Link to post
Share on other sites
  • 0

Hi @vinodcxlv

Here you have a script to send array of data.

PatternsSPI.dwf3work

image.thumb.png.357f7468534114d944b5a280d123fb36.png

The 'insert code' is not working on the forum... better take the script from the workspace.

// bit 0 - data
// bit 1 - clk, sample on rising 
// bit 2 - select, active low
function setSPI(data, bits, hz){

   Patterns1.States.Repeat.value = 1
   Patterns1.States.RepeatTrigger.value = 0
   Patterns1.States.Run.value = (bits+1)/hz 
   // we need 2 samples for each SPI CLK
   Patterns1.Channels.Bus1.Frequency.value = 2*hz 

   var rg = new Array()
   rg.push(4) // select inactive high
   data.forEach(function(v){
       for(var i = 0; i < bits; i++, v>>=1){
          var f = v & 1
          rg.push(f) // set data 
          rg.push(f|2) // set clock
       }
   })
   rg.push(4) // select inactive high
   Patterns1.Channels.Bus1.custom = rg
}

var rgSend = []
if(1){
    var rgFile = (""+FileRead("~/Desktop/default.txt")).split(", "); // 1, 2, 3
    rgFile.forEach(function(v){rgSend.push(v<<2)})
}else{
    rgSend.push(1)
    rgSend.push(2)
    rgSend.push(3)
}

setSPI(rgSend, 8, 1000)
Patterns1.run()

Share this post


Link to post
Share on other sites
  • 0

Hi @vinodcxlv

SPI Mode 0 :
rg.push(4) // select
   ...
   rg.push(f) // data
   rg.push(f | 2) // data & clock
   ...
rg.push(0) // all low

rg.push(4) // select

SPI Mode 1 :
rg.push(4) // select
rg.push(0) // all low
   ...
   rg.push(f | 2) // data & clock
   rg.push(f ) // data
   ...
rg.push(4) // select

SPI Mode 2 :
rg.push(4 | 2) // select & clock
   ...
   rg.push(f | 2) // data & clock
   rg.push(f ) // data
   ...
rg.push(2) // clock

rg.push(4 | 2) // select & clock

SPI Mode 3 :
rg.push(4 | 2) // select & clock
rg.push(2) // clock
   ...
   rg.push(f ) // data & clock
   rg.push(f | 2) // data
  ...
rg.push(4 | 2) // select & clock

Share this post


Link to post
Share on other sites
  • 0
Posted (edited)

Hi, can u help me? I bought AudioQuest DragonFly Plug-in USB DAC, it has no controllers, can I use the same script as shown above?

Edited by Leiwu

Share this post


Link to post
Share on other sites
  • 0

Hi @Leiwu,

No, it will not be compatible as it is an entirely different product with a different application.

Thanks,
JColvin

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now