//************************************************************
//
// Notes on the basic script syntax can be found here
//
// You can use the links in the Help, other educational pages or google...
//
// https://doc.qt.io/qt-5/ecmascript.html
//
// https://www.w3schools.com/jsref/jsref_obj_array.asp
//
// Note for this script to work the following Waveforms window panels have to be open
// - Supplies - the internal AD2 PSU can be used
// - Main script -- location where this script is cut and pasted
// - Protocol ** - required to have acces to the AD2 internal SPI or UART or I2C bus engines
//
// edit using Notepad++ with the langauge Javascript selected.
//
//
//************************************************************
function bitExtracted( number, k, p)
{
// Function to extract k bits from p position
// and returns the extracted value as integer
// eg first 11 bits of a 16 bit number
// bitExtracted(int 0xfff,10,0)
return (((1 << k) - 1) & (number >> (p - 1)));
}
function spi_initialise()
{
if(!('Protocol' in this)) throw "Please open the Protocol tool window";
//************************************************************
// Script using Protocol SPI.
// SPI BUS Set up
//************************************************************
//
// SPI Bus physical wired set up
//
// Select AD_2_DIO3 // SPI: /CS
// Clock AD_2_DIO4 // SPI: Clock
// DQ0 AD_2_DIO5 // SPI: MOSI
// DQ1 AD_2_DIO6 // SPI: MOS0
//
// !! Remember to change the set up in function spi_pin_io_setup() if the SPI Pins are re assigned on the AD2 DIO GPIO pins !!
Protocol.Mode.text = "SPI"; // make sure SPI tab is selected
Protocol.SPI.Select.Active.value=0;
Protocol.SPI.Clock.Polarity.value=0 ; // set up spi clock initial Polarity
Protocol.SPI.Clock.Phase.value=0 ; // set up spi clock initial Phase
Protocol.SPI.Clock.Frequency.value=1e6 ; // set spi clock freqncy at 1MHz page 19 of the data sheet min spi clock 1MHz
if(Protocol.SPI.Clock.Frequency.value > 1e6) Protocol.SPI.Clock.Frequency.value = 1e6;
Protocol.SPI.Select.value = 3; //Assigns pin Analog Disovery 2 GPIO Pin DIO 3
Protocol.SPI.Clock.value = 4; //Assigns pin Analog Disovery 2 GPIO Pin DIO 4
Protocol.SPI.DQ0.value = 5; //Assigns pin Analog Disovery 2 GPIO Pin DIO 5 // // SPI: MOSI
Protocol.SPI.DQ1.value = 6; //Assigns pin Analog Disovery 2 GPIO Pin DIO 6 // SPI: MOS0
Protocol.config() // apply configuration changes now
print(" SPI initialised " + "\n\r" );
print(" SPI Select is connect to AD2 DIO Port : " + Protocol.SPI.Select.value + "\n\r" ) ;
print(" SPI Clock connect to AD2 DIO Port : " + Protocol.SPI.Clock.value + "\n\r" ) ;
print(" SPU MOSI port connect to AD2 DIO Port : " + Protocol.SPI.DQ0.value + "\n\r" ) ;
print(" SPU MOSO port connect to AD2 DIO Port : " + Protocol.SPI.DQ1.value + "\n\r" ) ;
print("\n\r"+" SPI initialisation complete " + "\n\r" ) ;
//************************************************************
// notes
// =====
//
// 1. On the AD and AD2 the digital IO pins use LVCMOS 3V3 standard. These are supplied at 3.3V and have 200 Ohm PTC and clamping diodes to GND and to 3.3V for protection.
// It will drive the digital wires at 3.3V and connecting 5V signals directly, without series resistor, it will sink about 10mA through the PTC and 3v3 diode.
// As for noise, make sure to have ground connection between the two devices.
//
// 2. The WF/Protocol/UART uses the Stop length option only for transmission, this is not relevant for reception.
//
// 3. The UART TX filed lets you type in characters, hexadecimal values are not supported here.
// Special chars you can copy/paste from other place or send from file.
//************************************************************
}
function i2c_initialise()
{
if(!('Protocol' in this)) throw "Please open the Protocol tool window";
//************************************************************
// Script using Protocol I2C.
// i2C BUS Set up
//************************************************************
//
// I2C Bus physical wired set up
//
// I2C SDA AD_2_DIO0
// I2C SCL AD_2_DIO1
// /Reset AD_2_DIO3
//
//
//
//
// Note Only DIO pins 0 through to 9 can be used for Protocol functions { SPI , I2C or UART}
//
//
var I2C_reset_AD2_DIO_port = 3 ; // Set to AD2 DIO Port which controls I2C /Reset io line
Protocol.Mode.text = "I2C"; // make sure SPI tab is selected
Protocol.I2C.Mode.value = 1; //Set the I2C Protocol tool into Master Mode
Protocol.I2C.SCL.value = 1; //Set the SCL pin to be DIO 0
Protocol.I2C.SDA.value = 0; //Set the SDA pin to be DIO 1
Protocol.I2C.SCL.Frequency.value = 100000; //Set the SCL clock frequency to 100 kHz
//Protocol.I2C.Clockstretching.value = 1; //Enable Clock Stretching´.. This line is not functioning
Protocol.I2C.ClockStretching.enable; // This is the replacement lines
Protocol.config() // apply configuration changes now
// flip the GPIO active low reset line set low then set high
set_static_io_pin(I2C_reset_AD2_DIO_port,0);
wait(0.1); // wait 100 milli second before next instruction.
set_static_io_pin(I2C_reset_AD2_DIO_port,1);
wait(0.1); // wait 100 milli second before next instruction.
print(" I2C initialised " + "\n\r" );
print(" I2C SCL connect to AD2 DIO Port " + Protocol.I2C.SCL.value + "\n\r" ) ;
print(" I2C SDA connect to AD2 DIO Port " + Protocol.I2C.SDA.value + "\n\r" ) ;
print(" I2C /RESET port connect to AD2 DIO Port " + I2C_reset_AD2_DIO_port + "\n\r" ) ;
print("\n\r"+" I2C initialisation complete " + "\n\r" ) ;
//************************************************************
// notes
// =====
//
// 1. On the AD and AD2 the digital IO pins use LVCMOS 3V3 standard. These are supplied at 3.3V and have 200 Ohm PTC and clamping diodes to GND and to 3.3V for protection.
// It will drive the digital wires at 3.3V and connecting 5V signals directly, without series resistor, it will sink about 10mA through the PTC and 3v3 diode.
// As for noise, make sure to have ground connection between the two devices.
//
// 2. The WF/Protocol/UART uses the Stop length option only for transmission, this is not relevant for reception.
//
// 3. The UART TX filed lets you type in characters, hexadecimal values are not supported here.
// Special chars you can copy/paste from other place or send from file.
//************************************************************
}
// notes
// =====
//
// 1. On the AD and AD2 the digital IO pins use LVCMOS 3V3 standard. These are supplied at 3.3V and have 200 Ohm PTC and clamping diodes to GND and to 3.3V for protection.
// It will drive the digital wires at 3.3V and connecting 5V signals directly, without series resistor, it will sink about 10mA through the PTC and 3v3 diode.
// As for noise, make sure to have ground connection between the two devices.
//
// 2. The WF/Protocol/UART uses the Stop length option only for transmission, this is not relevant for reception.
//
// 3. The UART TX filed lets you type in characters, hexadecimal values are not supported here.
// Special chars you can copy/paste from other place or send from file.
function UART_initialise()
{
clear(); // clear Output log
//
// Note Only DIO pins 0 through to 9 can be used for Protocol functions { SPI , I2C or UART}
//
//
if(!('Protocol' in this)) throw "Please open the Protocol instrument";
Protocol.Mode.text = "UART"; // make sure UART tab is selected
Protocol.UART.TX.value = 0; // Set up UART TX pin on Digilent AD2 :: DIO_0 Pin
Protocol.UART.RX.value = 1; // Set up UART RX pin on Digilent AD2 :: DIO_1 Pin
Protocol.UART.Bits.value=8; // Sets or gets the UART number of bits.
Protocol.UART.Parity.text="None" ;// Sets or gets the parity setting: “None”, “Even”, “Odd”, “Mark (High)”, “Space (Low)”.
Protocol.UART.Rate.value=9600; // Sets or gets the baud rate setting
Protocol.config(); // apply configuration changes now
Protocol.UART.Receiver(); // reset receiver - that is too start receiving data
print(" UART Serial Bus initialised . Beware this is 3V3 Logic RS232 & Not True +/- 15V Rs232" + "\n\r" );
print(" I2C SCL connect to AD2 DIO Port " + Protocol.I2C.SCL.value + "\n\r" ) ;
print(" I2C SDA connect to AD2 DIO Port " + Protocol.I2C.SCL.value + "\n\r" ) ;
print("\n\r"+" I2C initialisation complete " + "\n\r" ) ;
//
// ref https://forum.digilentinc.com/topic/21308-ad2-protocol-uart-script-reading-vaillant-ebus/
//
// https://forum.digilentinc.com/topic/21353-use-of-multiple-protocol-uart-ports-via-script-possible-at-different-times/
//
// notes
// =====
//
// 1. On the AD and AD2 the digital IO pins use LVCMOS 3V3 standard. These are supplied at 3.3V and have 200 Ohm PTC and clamping diodes to GND and to 3.3V for protection.
// It will drive the digital wires at 3.3V and connecting 5V signals directly, without series resistor, it will sink about 10mA through the PTC and 3v3 diode.
// As for noise, make sure to have ground connection between the two devices.
//
// 2. The WF/Protocol/UART uses the Stop length option only for transmission, this is not relevant for reception.
//
// 3. The UART TX filed lets you type in characters, hexadecimal values are not supported here.
// Special chars you can copy/paste from other place or send from file.
}
function UART_Test()
{
UART_initialise();
Protocol.UART.Receiver(); // start receiver
wait();
Protocol.UART.Send("hello"); // TX
wait(0.1); // 100ms wait for RX, depends on the external device and rate
print(Protocol.UART.Receive()); // RX
}
function UART_Test_example_2()
{
UART_initialise();
Protocol.UART.Receiver(); // reset receiver - that is start receiving data
var rx = [];
while(wait(1))
{
// wait 1 sec and repeat while not stopped
rx = rx.concat(Protocol.UART.ReceiveArray()); // append new words
while(rx.length>10)
{
if(rx.shift()==0xAA) continue;
if (rx[1] == 0xB5 && rx[2]== 0x11 && rx[7] == 0x09)
{
// find the temperatures for displaying
print ("Temp 1: " + (rx[8]/2).toFixed(1) + " °C Temp 2: " + (rx[9]/2).toFixed(1) + " °C"); // decimal data devided by 2 bring °Celsius
}
}
}
}
function UART_Test_example_3()
{
UART_initialise();
Protocol.UART.Receiver(); // reset receiver - that is start receiving data
//
Protocol.UART.Send("Hello") ; // send text with ending
var rx = Protocol.UART.Receive(); // receive text
print(rx) ;
//
Protocol.UART.Send("Hello ", false); // send text custom ending
var rx = Protocol.UART.Receive(); // receive text
print(escape(rx));
//
Protocol.UART.SendArray([1,2,3,4,5,0], false); // send array custom ending
var rx = Protocol.UART.ReceiveArray(); // receive array
print(rx);
}
function spi_write( address , data_to_write)
{
Protocol.SPI.Start();
Protocol.SPI.Write( address ,data_to_write ); // Read Register, Device ID
Protocol.SPI.Stop();
wait(0.1); // wait 100 milli second before next instruction.
}
function spi_write_read( address , data_to_write, number_of_bytes_to_read_back)
{
Protocol.SPI.Start();
Protocol.SPI.Write( address ,data_to_write ); // Read Register, Device ID
wait(0.1); // wait 100 milli second before reading data.
var rg_data = Protocol.SPI.Read(address, number_of_bytes_to_read_back);
Protocol.SPI.Stop();
wait(0.1); // wait 100 milli second before next instruction.
return rg_data ;
}
function power_up_psu( Power_supply_voltage )
{
//************************************************************
// Set up mini PSU inside Analogue Discovery 2 to supply 3.3V
//************************************************************
if(!('Supplies' in this)) throw "Please open the Supplies tool window";
Supplies.Output.PositiveSupply.Voltage.value=Power_supply_voltage;
Supplies.Output.PositiveSupply.Enable.setEnable();
Supplies.Output.NegativeSupply.Enable.setDisable();
Supplies.run(); // Turn on PSU unit inside AD2
print(" Power supply is UP VCC = " + Power_supply_voltage + "\n\r" );
wait(5); // wait 1 seocnds for system to stabilise.
//***********************************************************
}
function power_down_psu()
{
//************************************************************
// Power down mini PSU inside Analogue Discovery 2 to supply 3.3V
//************************************************************
print(" PSU is powering down" );
Supplies.stop();
wait(5); // wait 5 second before reading data.
Supplies.Output.PositiveSupply.Voltage.value=0.3;
Supplies.Output.PositiveSupply.Enable.setEnable();
Supplies.Output.NegativeSupply.Enable.setDisable();
Supplies.run(); // Turn on PSU unit inside AD2
wait(10); // wait 10 second before reading data.
Supplies.stop(); // Turn off PSU unit inside AD2 after measurment has finished.
print(" PSU is powered down" );
//***********************************************************
}
function PMOD_ADXL362_not_working()
{
//************************************************************
// Example block read ADXL362 Device ID : Start
//************************************************************
clear(); // clear display screen
print(" SPI Script for PMOD ACL2 3 axis accelometer with Analog devices ADXL362 running " + "\n\r ");
power_up_psu( 3.3 ) ; // Power up PSU to VCC = 3v3
spi_initialise(); // set up spi bus
var STATUS_reg =spi_write_read( 8, [0x0B, 0x00], 1) ; // Read ADXL362 STATUS_reg Page 23 & 25 of the ADXL362 Data sheet & read 1 words of 8 bit length
print(" ADXL362 Status regiser raw data after reset = " + STATUS_reg + "\n\r" );
wait(0.01); // wait 10 milli second before reading data.
var rgID = spi_write_read( 8, [0x0B, 0x00], 4) ;
var Device_ok=0;
if(rgID[0] != 0xAD || rgID[1] != 0x1D || rgID[2] != 0xF2){
print(" Device ID mismatch rgid(0) " + rgID[0] );
print(" Device ID mismatch rgid(1) " + rgID[1] );
print(" Device ID mismatch rgid(2) " + rgID[2] );
print(" ");
print(" Is the physical SPI Bus physical wired set up correctly ?? " );
print(" ");
spi_pin_io_setup();
}
else
{
print(" Device is ADXL362 : SPI register 0x00 read { 0dxx in decimal} : " + rgID + "\n\r" );
Device_ok=1;
}
//**********************************************************************************
//
// Result shoudl be : Read: hAD, h1D, hF2, h02,
// Output : 173,29,242,2 in dec --> hex --> 0xAD 0x1D 0xF2 Ox02
//
//*********************************************************************************
if ( Device_ok==1)
{
/*---------------------------------------------------------------------------------------------------------------
*
* ADXSL262 SPI register setup for POWER_CTL to be permantly on without sleep
*
* Bit 7 Reserved 0x0
* Bit 6 External CLK =1 int=0 0x0
* Bit 5:4 Power ns Noise Trade off 0x10 = Ultra Low Noise
* Bit 3 Wakeup mode 0x0 = OFF
* Bit 2 Autosleep 0x0 = OFF
* BIT 1:0 Measurement state 0x10 = Measurment Mode
*
* Resultant setting = b00 10 00 10 = 0x22
*---------------------------------------------------------------------------------------------------------------*/
spi_write( 8,[0x0A, 0x2D, 0x02] ); // Power Control Register, Measurement Mode
var rg = spi_write_read( 8, [0x0B, 0x0E], 8) ;
// convert data bits to signed value relative to gravitational constant
// MSbit sign, LSBit 0.001g for default 2g mode
var gx = (0.001* ((rg[1]<<28) | (rg[0]<<20)) /1048576).toFixed(3);
var gy = (0.001* ((rg[3]<<28) | (rg[2]<<20)) /1048576).toFixed(3);
var gz = (0.001* ((rg[5]<<28) | (rg[4]<<20)) /1048576).toFixed(3);
var tp = (0.065* ((rg[7]<<28) | (rg[6]<<20)) /1048576).toFixed(2);
print(" Raw data {16 bit data} {x,y,z,temp} = " + rg + "\n\r" );
print(" Accelerometer initial X value = " + gx );
print(" Accelerometer initial y value = " + gy);
print(" Accelerometer initial z value = " + gz);
print(" Accelerometer initial temp value = " + tp);
}
}
//************************************************************
// Main program block : Start
//************************************************************
clear(); // clear display screen
print(" Script Main body start " + "\n\r ");
power_up_psu( 3.3 ) ; // Power up PSU to VCC = 3v3
// **********************
// User code start
// **********************
// **********************
// User code stop
// **********************
//************************************************************
// Main program block : Stop End
//************************************************************
Question
SIB-V2
// This posted here so that Google and Bing search the script to help other users of Digilent Analog Discovery AD2
// and Digilent wave forms find programming examples which are very difficult to find.
// This driver is work in progress . SPI & I2C functionality not working yet .
// for use with Digilent Analog Discovery AD2 script engine.
//
// Attila : Please leave this post up on the forum as I spent a very long time curating the script to find all the
// small example pieces of code .
//
// -------------------------------------------------------------------------------------------------------------------
//************************************************************
//
// Notes on the basic script syntax can be found here
//
// You can use the links in the Help, other educational pages or google...
//
// https://doc.qt.io/qt-5/ecmascript.html
//
// https://www.w3schools.com/jsref/jsref_obj_array.asp
//
// Note for this script to work the following Waveforms window panels have to be open
// - Supplies - the internal AD2 PSU can be used
// - Main script -- location where this script is cut and pasted
// - Protocol ** - required to have acces to the AD2 internal SPI or UART or I2C bus engines
//
// edit using Notepad++ with the langauge Javascript selected.
//
//
//************************************************************
//************************************************************
//Function blocks
//************************************************************
function bitExtracted( number, k, p)
{
// Function to extract k bits from p position
// and returns the extracted value as integer
// eg first 11 bits of a 16 bit number
// bitExtracted(int 0xfff,10,0)
return (((1 << k) - 1) & (number >> (p - 1)));
}
function spi_initialise()
{
if(!('Protocol' in this)) throw "Please open the Protocol tool window";
//************************************************************
// Script using Protocol SPI.
// SPI BUS Set up
//************************************************************
//
// SPI Bus physical wired set up
//
// Select AD_2_DIO3 // SPI: /CS
// Clock AD_2_DIO4 // SPI: Clock
// DQ0 AD_2_DIO5 // SPI: MOSI
// DQ1 AD_2_DIO6 // SPI: MOS0
//
// !! Remember to change the set up in function spi_pin_io_setup() if the SPI Pins are re assigned on the AD2 DIO GPIO pins !!
Protocol.Mode.text = "SPI"; // make sure SPI tab is selected
Protocol.SPI.Select.Active.value=0;
Protocol.SPI.Clock.Polarity.value=0 ; // set up spi clock initial Polarity
Protocol.SPI.Clock.Phase.value=0 ; // set up spi clock initial Phase
Protocol.SPI.Clock.Frequency.value=1e6 ; // set spi clock freqncy at 1MHz page 19 of the data sheet min spi clock 1MHz
if(Protocol.SPI.Clock.Frequency.value > 1e6) Protocol.SPI.Clock.Frequency.value = 1e6;
Protocol.SPI.Select.value = 3; //Assigns pin Analog Disovery 2 GPIO Pin DIO 3
Protocol.SPI.Clock.value = 4; //Assigns pin Analog Disovery 2 GPIO Pin DIO 4
Protocol.SPI.DQ0.value = 5; //Assigns pin Analog Disovery 2 GPIO Pin DIO 5 // // SPI: MOSI
Protocol.SPI.DQ1.value = 6; //Assigns pin Analog Disovery 2 GPIO Pin DIO 6 // SPI: MOS0
Protocol.config() // apply configuration changes now
print(" SPI initialised " + "\n\r" );
print(" SPI Select is connect to AD2 DIO Port : " + Protocol.SPI.Select.value + "\n\r" ) ;
print(" SPI Clock connect to AD2 DIO Port : " + Protocol.SPI.Clock.value + "\n\r" ) ;
print(" SPU MOSI port connect to AD2 DIO Port : " + Protocol.SPI.DQ0.value + "\n\r" ) ;
print(" SPU MOSO port connect to AD2 DIO Port : " + Protocol.SPI.DQ1.value + "\n\r" ) ;
print("\n\r"+" SPI initialisation complete " + "\n\r" ) ;
//************************************************************
// notes
// =====
//
// 1. On the AD and AD2 the digital IO pins use LVCMOS 3V3 standard. These are supplied at 3.3V and have 200 Ohm PTC and clamping diodes to GND and to 3.3V for protection.
// It will drive the digital wires at 3.3V and connecting 5V signals directly, without series resistor, it will sink about 10mA through the PTC and 3v3 diode.
// As for noise, make sure to have ground connection between the two devices.
//
// 2. The WF/Protocol/UART uses the Stop length option only for transmission, this is not relevant for reception.
//
// 3. The UART TX filed lets you type in characters, hexadecimal values are not supported here.
// Special chars you can copy/paste from other place or send from file.
//************************************************************
}
function i2c_initialise()
{
if(!('Protocol' in this)) throw "Please open the Protocol tool window";
//************************************************************
// Script using Protocol I2C.
// i2C BUS Set up
//************************************************************
//
// I2C Bus physical wired set up
//
// I2C SDA AD_2_DIO0
// I2C SCL AD_2_DIO1
// /Reset AD_2_DIO3
//
//
//
//
// Note Only DIO pins 0 through to 9 can be used for Protocol functions { SPI , I2C or UART}
//
//
var I2C_reset_AD2_DIO_port = 3 ; // Set to AD2 DIO Port which controls I2C /Reset io line
Protocol.Mode.text = "I2C"; // make sure SPI tab is selected
Protocol.I2C.Mode.value = 1; //Set the I2C Protocol tool into Master Mode
Protocol.I2C.SCL.value = 1; //Set the SCL pin to be DIO 0
Protocol.I2C.SDA.value = 0; //Set the SDA pin to be DIO 1
Protocol.I2C.SCL.Frequency.value = 100000; //Set the SCL clock frequency to 100 kHz
//Protocol.I2C.Clockstretching.value = 1; //Enable Clock Stretching´.. This line is not functioning
Protocol.I2C.ClockStretching.enable; // This is the replacement lines
Protocol.config() // apply configuration changes now
// flip the GPIO active low reset line set low then set high
set_static_io_pin(I2C_reset_AD2_DIO_port,0);
wait(0.1); // wait 100 milli second before next instruction.
set_static_io_pin(I2C_reset_AD2_DIO_port,1);
wait(0.1); // wait 100 milli second before next instruction.
// ref https://forum.digilentinc.com/topic/4942-i2c-subaddress-not-working-properly-in-the-script-tool/
// Ref https://forum.digilentinc.com/topic/21906-how-to-measure-scope-and-protocol/
// ref https://forum.digilentinc.com/topic/18944-reading-i2c-via-script-displays-wrong-data/
print(" I2C initialised " + "\n\r" );
print(" I2C SCL connect to AD2 DIO Port " + Protocol.I2C.SCL.value + "\n\r" ) ;
print(" I2C SDA connect to AD2 DIO Port " + Protocol.I2C.SDA.value + "\n\r" ) ;
print(" I2C /RESET port connect to AD2 DIO Port " + I2C_reset_AD2_DIO_port + "\n\r" ) ;
print("\n\r"+" I2C initialisation complete " + "\n\r" ) ;
//************************************************************
// notes
// =====
//
// 1. On the AD and AD2 the digital IO pins use LVCMOS 3V3 standard. These are supplied at 3.3V and have 200 Ohm PTC and clamping diodes to GND and to 3.3V for protection.
// It will drive the digital wires at 3.3V and connecting 5V signals directly, without series resistor, it will sink about 10mA through the PTC and 3v3 diode.
// As for noise, make sure to have ground connection between the two devices.
//
// 2. The WF/Protocol/UART uses the Stop length option only for transmission, this is not relevant for reception.
//
// 3. The UART TX filed lets you type in characters, hexadecimal values are not supported here.
// Special chars you can copy/paste from other place or send from file.
//************************************************************
}
function i2c_notes_read_with_i2c_sub_address()
{
//
// Ref https://forum.digilentinc.com/topic/4942-i2c-subaddress-not-working-properly-in-the-script-tool/
//
//
// Special sub-addressing is only required for read operations. In this process the sub-address write is followed by a restart and read.
// ref https://www.i2c-bus.org/repeated-start-condition/
// For write operations you can just concatenate the "sub-address" with the array of data you have to write.
var a = [1,2] // sub-address
var b = [3,4] // data
var c = a.concat(b)
Protocol.I2C.Write(0x48, c)
}
function i2c_write( address , data_array_to_write)
{
//
// Examples of i2c_write
//
// address = 0x60
// data_array_to_write = [0,1]
//
Protocol.I2C.Write(address, data_array_to_write ) ;
// notes
// =====
//
// 1. On the AD and AD2 the digital IO pins use LVCMOS 3V3 standard. These are supplied at 3.3V and have 200 Ohm PTC and clamping diodes to GND and to 3.3V for protection.
// It will drive the digital wires at 3.3V and connecting 5V signals directly, without series resistor, it will sink about 10mA through the PTC and 3v3 diode.
// As for noise, make sure to have ground connection between the two devices.
//
// 2. The WF/Protocol/UART uses the Stop length option only for transmission, this is not relevant for reception.
//
// 3. The UART TX filed lets you type in characters, hexadecimal values are not supported here.
// Special chars you can copy/paste from other place or send from file.
}
function i2c_read( address , number_of_bytes_to_read_back )
{
//
// Examples of i2c_read
//
// address = 0x60
// number_of_bytes_to_read_back =32
//
var bytes = Protocol.I2C.Read( address, number_of_bytes_to_read_back) ;// returns array of x bytes read from device address 0x0D
return bytes ;
}
function i2c_read_example()
{
// From the Script tool you can perform I2C transfer like this:
var bytes = Protocol.I2C.Read(0x0D, 2) ;// returns array of 2 bytes read from device address 0x0D
var temp = (((bytes[0]<<26) | (bytes[1]<<18)) >>18)/32;
print("Temp: "+temp+"*C")
// ref https://forum.digilentinc.com/topic/17333-digital-discovery-logic-dios-are-different-to-dios-from-the-patterngenerator/
}
function spi_pin_io_setup()
{
print(" The SPI setup should be :- ");
print(" ");
print(" Select AD_2_DIO3 // SPI: /CS ");
print(" Clock AD_2_DIO4 // SPI: Clock ");
print(" DQ0 AD_2_DIO5 // SPI: MOSI ");
print(" DQ1 AD_2_DIO6 // SPI: MOS0 ");
}
function UART_initialise()
{
clear(); // clear Output log
//
// Note Only DIO pins 0 through to 9 can be used for Protocol functions { SPI , I2C or UART}
//
//
if(!('Protocol' in this)) throw "Please open the Protocol instrument";
Protocol.Mode.text = "UART"; // make sure UART tab is selected
Protocol.UART.TX.value = 0; // Set up UART TX pin on Digilent AD2 :: DIO_0 Pin
Protocol.UART.RX.value = 1; // Set up UART RX pin on Digilent AD2 :: DIO_1 Pin
Protocol.UART.Bits.value=8; // Sets or gets the UART number of bits.
Protocol.UART.Parity.text="None" ;// Sets or gets the parity setting: “None”, “Even”, “Odd”, “Mark (High)”, “Space (Low)”.
Protocol.UART.Rate.value=9600; // Sets or gets the baud rate setting
Protocol.config(); // apply configuration changes now
Protocol.UART.Receiver(); // reset receiver - that is too start receiving data
print(" UART Serial Bus initialised . Beware this is 3V3 Logic RS232 & Not True +/- 15V Rs232" + "\n\r" );
print(" I2C SCL connect to AD2 DIO Port " + Protocol.I2C.SCL.value + "\n\r" ) ;
print(" I2C SDA connect to AD2 DIO Port " + Protocol.I2C.SCL.value + "\n\r" ) ;
print("\n\r"+" I2C initialisation complete " + "\n\r" ) ;
//
// ref https://forum.digilentinc.com/topic/21308-ad2-protocol-uart-script-reading-vaillant-ebus/
//
// https://forum.digilentinc.com/topic/21353-use-of-multiple-protocol-uart-ports-via-script-possible-at-different-times/
//
// notes
// =====
//
// 1. On the AD and AD2 the digital IO pins use LVCMOS 3V3 standard. These are supplied at 3.3V and have 200 Ohm PTC and clamping diodes to GND and to 3.3V for protection.
// It will drive the digital wires at 3.3V and connecting 5V signals directly, without series resistor, it will sink about 10mA through the PTC and 3v3 diode.
// As for noise, make sure to have ground connection between the two devices.
//
// 2. The WF/Protocol/UART uses the Stop length option only for transmission, this is not relevant for reception.
//
// 3. The UART TX filed lets you type in characters, hexadecimal values are not supported here.
// Special chars you can copy/paste from other place or send from file.
}
function UART_Test()
{
UART_initialise();
Protocol.UART.Receiver(); // start receiver
wait();
Protocol.UART.Send("hello"); // TX
wait(0.1); // 100ms wait for RX, depends on the external device and rate
print(Protocol.UART.Receive()); // RX
}
function UART_Test_example_2()
{
UART_initialise();
Protocol.UART.Receiver(); // reset receiver - that is start receiving data
var rx = [];
while(wait(1))
{
// wait 1 sec and repeat while not stopped
rx = rx.concat(Protocol.UART.ReceiveArray()); // append new words
while(rx.length>10)
{
if(rx.shift()==0xAA) continue;
if (rx[1] == 0xB5 && rx[2]== 0x11 && rx[7] == 0x09)
{
// find the temperatures for displaying
print ("Temp 1: " + (rx[8]/2).toFixed(1) + " °C Temp 2: " + (rx[9]/2).toFixed(1) + " °C"); // decimal data devided by 2 bring °Celsius
}
}
}
}
function UART_Test_example_3()
{
UART_initialise();
Protocol.UART.Receiver(); // reset receiver - that is start receiving data
//
Protocol.UART.Send("Hello") ; // send text with ending
var rx = Protocol.UART.Receive(); // receive text
print(rx) ;
//
Protocol.UART.Send("Hello ", false); // send text custom ending
var rx = Protocol.UART.Receive(); // receive text
print(escape(rx));
//
Protocol.UART.SendArray([1,2,3,4,5,0], false); // send array custom ending
var rx = Protocol.UART.ReceiveArray(); // receive array
print(rx);
}
function spi_write( address , data_to_write)
{
Protocol.SPI.Start();
Protocol.SPI.Write( address ,data_to_write ); // Read Register, Device ID
Protocol.SPI.Stop();
wait(0.1); // wait 100 milli second before next instruction.
}
function spi_write_read( address , data_to_write, number_of_bytes_to_read_back)
{
Protocol.SPI.Start();
Protocol.SPI.Write( address ,data_to_write ); // Read Register, Device ID
wait(0.1); // wait 100 milli second before reading data.
var rg_data = Protocol.SPI.Read(address, number_of_bytes_to_read_back);
Protocol.SPI.Stop();
wait(0.1); // wait 100 milli second before next instruction.
return rg_data ;
}
function static_io_example()
{
// ref https://forum.digilentinc.com/topic/21512-how-to-energize-multiople-staticio-given-only-when-a-certain-dio-is-on/
// ref https://forum.digilentinc.com/topic/2150-staticio-scripting-documentations-for-waveforms3/
// ref https://forum.digilentinc.com/topic/18737-export-issues/
// ref https://forum.digilentinc.com/topic/18789-some-functional-problems/
clear()
if(!('StaticIO' in this)) throw "Please open the StaticIO instrument";
const IO = 7;
const delay = 0.3;
print("Running StaticIO script");
for(var c = 0; c <= IO; c++)
{
StaticIO.Channel0.DIO[c].Mode.text = "Switch";
}
StaticIO.run()
while(wait()){ // while script not stopped
if(StaticIO.Channel1.DIO8.Input.value == 1)
{
for(var c = 0; c <= IO; c++)
{
StaticIO.Channel0.DIO[c].text = "1";
StaticIO.config(); // apply changes now
if(!wait(delay)) return; // exit on script stop
StaticIO.Channel0.DIO[c].text = "0";
StaticIO.config();
if(!wait(delay)) return;
}
for(var c = IO; c > 0; c--)
{
StaticIO.Channel0.DIO[c-1].text = "1";
StaticIO.config();
if(!wait(delay)) return;
StaticIO.Channel0.DIO[c-1].text = "0";
StaticIO.config();
if(!wait(delay)) return;
}
}
}
}
function static_io_example_2()
{
// ref https://forum.digilentinc.com/topic/21512-how-to-energize-multiople-staticio-given-only-when-a-certain-dio-is-on/
// ref https://forum.digilentinc.com/topic/2150-staticio-scripting-documentations-for-waveforms3/
// ref https://forum.digilentinc.com/topic/18737-export-issues/
// ref https://forum.digilentinc.com/topic/18789-some-functional-problems/
clear();
if(!('StaticIO' in this)) throw "Please open the StaticIO instrument";
// 1.Probe
print("Running pin 1, channel 1")
StaticIO.Channel0.DIO[3].text = "0";
StaticIO.Channel0.DIO[4].text = "1";
StaticIO.Channel0.DIO[5].text = "1";
StaticIO.Channel0.DIO[0].text = "0";
StaticIO.Channel0.DIO[1].text = "0";
StaticIO.Channel0.DIO[2].text = "0";
StaticIO.config(); // force immediate configuration
wait(1); // wait on external device/circuit
}
function static_io_example_3()
{
//
// This is what's available on the wiki:
//
// .StaticIO
//
// .Channel#: groups of 8 signals
// .Mode.text: select between: I/Os, Slider, Progress
// .Input.value: input value
// .DIO#: I/Os
// .Input.checked: input value
// .Mode.text: select between: LED, Button, Switch
// .Button.text: select between: 0/1, 1/0, Z/1, 1/Z, Z/0, 0/Z
// .Switch.text: select between: TS, PP, OS, OD
// ref https://forum.digilentinc.com/topic/2150-staticio-scripting-documentations-for-waveforms3/
// I got the DIO13 configured as a PushPull Switch, but how do you set the actual value that you want the pin to output?
// Configure Static IO
StaticIO.Channel1.Mode.text = "I/Os"
// Configure Red LED
StaticIO.Channel1.DIO13.Mode.text = "Switch"
StaticIO.Channel1.DIO13.Switch.text= "PP"
}
function set_static_io_pin( AD2_Dio_pin , boolean_state )
{
if(!('StaticIO' in this)) throw "Please open the StaticIO instrument";
if( boolean_state == 1)
{
StaticIO.Channel0.DIO[AD2_Dio_pin].text = "1";
StaticIO.config() // force immediate configuration
}
if( boolean_state == 0)
{
StaticIO.Channel0.DIO[AD2_Dio_pin].text = "0";
StaticIO.config() // force immediate configuration
}
}
function power_up_psu( Power_supply_voltage )
{
//************************************************************
// Set up mini PSU inside Analogue Discovery 2 to supply 3.3V
//************************************************************
if(!('Supplies' in this)) throw "Please open the Supplies tool window";
Supplies.Output.PositiveSupply.Voltage.value=Power_supply_voltage;
Supplies.Output.PositiveSupply.Enable.setEnable();
Supplies.Output.NegativeSupply.Enable.setDisable();
Supplies.run(); // Turn on PSU unit inside AD2
print(" Power supply is UP VCC = " + Power_supply_voltage + "\n\r" );
wait(5); // wait 1 seocnds for system to stabilise.
//***********************************************************
}
function power_down_psu()
{
//************************************************************
// Power down mini PSU inside Analogue Discovery 2 to supply 3.3V
//************************************************************
print(" PSU is powering down" );
Supplies.stop();
wait(5); // wait 5 second before reading data.
Supplies.Output.PositiveSupply.Voltage.value=0.3;
Supplies.Output.PositiveSupply.Enable.setEnable();
Supplies.Output.NegativeSupply.Enable.setDisable();
Supplies.run(); // Turn on PSU unit inside AD2
wait(10); // wait 10 second before reading data.
Supplies.stop(); // Turn off PSU unit inside AD2 after measurment has finished.
print(" PSU is powered down" );
//***********************************************************
}
function PMOD_ADXL362_not_working()
{
//************************************************************
// Example block read ADXL362 Device ID : Start
//************************************************************
clear(); // clear display screen
print(" SPI Script for PMOD ACL2 3 axis accelometer with Analog devices ADXL362 running " + "\n\r ");
power_up_psu( 3.3 ) ; // Power up PSU to VCC = 3v3
spi_initialise(); // set up spi bus
var STATUS_reg =spi_write_read( 8, [0x0B, 0x00], 1) ; // Read ADXL362 STATUS_reg Page 23 & 25 of the ADXL362 Data sheet & read 1 words of 8 bit length
print(" ADXL362 Status regiser raw data after reset = " + STATUS_reg + "\n\r" );
wait(0.01); // wait 10 milli second before reading data.
var rgID = spi_write_read( 8, [0x0B, 0x00], 4) ;
var Device_ok=0;
if(rgID[0] != 0xAD || rgID[1] != 0x1D || rgID[2] != 0xF2){
print(" Device ID mismatch rgid(0) " + rgID[0] );
print(" Device ID mismatch rgid(1) " + rgID[1] );
print(" Device ID mismatch rgid(2) " + rgID[2] );
print(" ");
print(" Is the physical SPI Bus physical wired set up correctly ?? " );
print(" ");
spi_pin_io_setup();
}
else
{
print(" Device is ADXL362 : SPI register 0x00 read { 0dxx in decimal} : " + rgID + "\n\r" );
Device_ok=1;
}
//**********************************************************************************
//
// Result shoudl be : Read: hAD, h1D, hF2, h02,
// Output : 173,29,242,2 in dec --> hex --> 0xAD 0x1D 0xF2 Ox02
//
//*********************************************************************************
if ( Device_ok==1)
{
/*---------------------------------------------------------------------------------------------------------------
*
* ADXSL262 SPI register setup for POWER_CTL to be permantly on without sleep
*
* Bit 7 Reserved 0x0
* Bit 6 External CLK =1 int=0 0x0
* Bit 5:4 Power ns Noise Trade off 0x10 = Ultra Low Noise
* Bit 3 Wakeup mode 0x0 = OFF
* Bit 2 Autosleep 0x0 = OFF
* BIT 1:0 Measurement state 0x10 = Measurment Mode
*
* Resultant setting = b00 10 00 10 = 0x22
*---------------------------------------------------------------------------------------------------------------*/
spi_write( 8,[0x0A, 0x2D, 0x02] ); // Power Control Register, Measurement Mode
var rg = spi_write_read( 8, [0x0B, 0x0E], 8) ;
// convert data bits to signed value relative to gravitational constant
// MSbit sign, LSBit 0.001g for default 2g mode
var gx = (0.001* ((rg[1]<<28) | (rg[0]<<20)) /1048576).toFixed(3);
var gy = (0.001* ((rg[3]<<28) | (rg[2]<<20)) /1048576).toFixed(3);
var gz = (0.001* ((rg[5]<<28) | (rg[4]<<20)) /1048576).toFixed(3);
var tp = (0.065* ((rg[7]<<28) | (rg[6]<<20)) /1048576).toFixed(2);
print(" Raw data {16 bit data} {x,y,z,temp} = " + rg + "\n\r" );
print(" Accelerometer initial X value = " + gx );
print(" Accelerometer initial y value = " + gy);
print(" Accelerometer initial z value = " + gz);
print(" Accelerometer initial temp value = " + tp);
}
}
//************************************************************
// Main program block : Start
//************************************************************
clear(); // clear display screen
print(" Script Main body start " + "\n\r ");
power_up_psu( 3.3 ) ; // Power up PSU to VCC = 3v3
// **********************
// User code start
// **********************
// **********************
// User code stop
// **********************
power_down_psu();
//************************************************************
//************************************************************
// Main program block : Stop End
//************************************************************
Digilent_AD2_Script_master.js
Edited by SIB-V2Link to comment
Share on other sites
1 answer to this question
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now