Examples of scripts for SP1000G
In this section, you will see how to generate UART, I2C and SPI frames using the Pattern genertion feature of SP1000G series devices. To achieve this goal in an code-efficient way, we’ll use the Builder Objet of each the protocol scripts stated above. Indeed, for each one of these protocols there is a ScanaStudio scripts that offer decoding and signal building features.
Prior to reading this chapter, we recommend reading the previous section - about the structure of a script for SP1000G series device, in addition to the construction of an Builder Object in Signal Builder chapter.
UART
Methodology
- First, you have to set your UART channel. In this example, we’ll call it ch_uart and initialize it as channel 0. We could also create a channel selector in the GUI interface, but let’s keep the example minimalistic and straight forward :
function on_pattern_generate()
{
var ch_uart = 0 ;
}
- Then, you must configure the output channel. Let’s assume for example that your required configuration is as follows :
- Output voltage : 3300 mV,
- Idle state : 1,
- Output type : push_pull.
ScanaStudio.builder_set_out_voltage(ch_uart, 3300);
ScanaStudio.builder_set_idle_state(ch_uart, 1);
ScanaStudio.builder_set_io(ch_uart, ScanaStudio.io_type.push_pull);
- Then, we’ll use a Builder Object, which is a group of “helper functions” used to build signals for a specific protocol or application. Here, the point is not to create a Builder Object from scratch, but rather to load and use an existing one.
The ability to instantiate several builder objects gives a lot of flexibility and allows easy code recycling. One big advantage of this method is that you may load different builder objects from different files, hence building very sophisticated signals using few, simple, easy to read lines of code.
Making use of an existing builder object from another script implies calling the function ScanaStudio.load_builder_object("script.js")
. Therefore, we will create a get_builder_object() function that will call the UART’s Builder Object, which already exists in the script “uart.js”, and that returns an UART’s Builder Object called builder.
function on_pattern_generate()
{
var uart_builder = get_builder_object() ;
}
function get_builder_object()
{
var builder = ScanaStudio.load_builder_object("uart.js") ;
return builder ;
}
-
Then, you have to configure this object by filling in the required parameters. To know the full list of the parameters (and their order), you can look in the script of the Builder Object (here in uart.js) and search for the builder object definition.
Here, for the UART’s builder, you have to set the following parameters :
- Channel,
- Baud,
- Number of bits per transfer
- Parity bit (0 for no parity bit, 1 for odd parity, 2 for even parity),
- Stop bit (1, 1.5 or 2 stop bits),
- Bit order (0 for LSB first, 1 for MSB first),
- Inverted logic (0 for not inverted logic, 1 for all signals inverted, 2 for only data inverted)
- Sample rate.
In this example, we will set the parameters as shown below :
uart_builder.config(
/* SET CHANNEL*/
ch_uart,
/* SET BAUD*/
9600,
/* SET NUMBER OF BITS PER TRANSFER*/
8,
/* SET PARITY BIT*/
/*=0 for no parity bit, =1 for odd parity bit, =2 for even parity bit*/
0,
/* SET STOP BITS BIT VALUE*/
/*=1, 1,5 or 2 stop bits*/
1,
/* SET BIT ORDER*/
/* =0 for LSB first, =1 for MSB first*/
0,
/* SET INVERTED LOGIC OR NOT*/
/*=0 for not inverted logic, =1 for all signals inverted, =2 for only data inverted*/
0,
/*SAMPLE RATE*/
Number(ScanaStudio.builder_get_sample_rate())
);
- Then, we can create a UART frame, using the builder object
var sentence = "Hello world, please enter a sentence here !" ; // enter the sentence to build
uart_builder.put_str(sentence) ;
- Next step consists of sending chunk of signals and start generating that chunk :
ScanaStudio.builder_set_repeat_count(1) ;
ScanaStudio.builder_start_chunk() ;
ScanaStudio.builder_wait_done(500) ;
Complete script
A minimalistic but fully functionnal scrips to generate a UART frame is shown below:
function on_draw_gui_pattern_generator()
{
}
function on_eval_gui_pattern_generator()
{
return "";
}
function on_pattern_generate()
{
/*ELECTRICAL SETTINGS*/
var ch_uart = 0;
ScanaStudio.builder_set_out_voltage(ch_uart, 3300);
ScanaStudio.builder_set_idle_state(ch_uart, 1);
ScanaStudio.builder_set_io(ch_uart, ScanaStudio.io_type.push_pull);
ScanaStudio.builder_add_samples(ch_uart, 1, 100);
/*Builder Object*/
var uart_builder = get_builder_object();
uart_builder.config(
/* SET CHANNEL*/
/*0 to 17*/
ch_uart,
/* SET BAUD*/
9600,
/* SET NUMBER OF BITS PER TRANSFER*/
8,
/* SET PARITY BIT*/
/*=0 for no parity bit, =1 for odd parity bit, =2 for even parity bit*/
0,
/* SET STOP BITS BIT VALUE*/
/*=1, 1,5 or 2 stop bits*/
1,
/* SET BIT ORDER*/
/* =0 for LSB first, =1 for MSB first*/
0,
/* SET INVERTED LOGIC OR NOT*/
/*=0 for not inverted logic, =1 for all signals inverted, =2 for only data inverted*/
0,
/*SAMPLE RATE*/
Number(ScanaStudio.builder_get_sample_rate())
);
/*SENTENCE TO SEND*/
var sentence = "Hello world, please enter a sentence here !" ; // enter the sentence to send
uart_builder.put_str(sentence);
/*SENDTO SP1018G*/
ScanaStudio.builder_set_repeat_count(1);
ScanaStudio.builder_start_chunk();
ScanaStudio.builder_wait_done(500);
return;
}
function get_builder_object()
{
var builder = ScanaStudio.load_builder_object("uart.js");
return builder;
}
- You can launch that script in
ScanaStudio
and add UART protocol decoder. The protocol decoder will allow you to see your sentence inView
, thenHexView
, as shown in the image below :
I2C
Following the same methodology as with the UART generator script, below is a minimalistic but fully functionnal script that shows how to generate I2C signals using the SP1000G serial logic analyzer and pattern generator.
function on_draw_gui_pattern_generator()
{
}
function on_eval_gui_pattern_generator()
{
return ""; //All good.
}
function on_pattern_generate()
{
/* ENTER A CHANNEL FOR SDA AND SCL*/
/*0 to 17*/
var ch_sda = 0 ; //for data
var ch_scl = 1 ; // for clock
/*ELECTRICAL SETTINGS*/
ScanaStudio.builder_set_out_voltage(ch_sda, 3300);
ScanaStudio.builder_set_idle_state(ch_sda, 1);
ScanaStudio.builder_set_io(ch_sda, ScanaStudio.io_type.push_pull);
ScanaStudio.builder_set_out_voltage(ch_scl, 3300);
ScanaStudio.builder_set_idle_state(ch_scl, 1);
ScanaStudio.builder_set_io(ch_scl, ScanaStudio.io_type.push_pull);
/*CONFIG BUILDER OBJECT*/
var i2c_f = ScanaStudio.builder_get_sample_rate()/2000; //clock frequency
var i2c_builder = get_builder_object();
i2c_builder.config(ch_scl,ch_sda,i2c_f);
/*I2C FRAME*/
/*I2C write*/
/*function i2C_write(i2c_builder,slave_address, register_address, tab,n_item)*/
i2C_write(i2c_builder,0x1A, 0x20, [0x10,0x11,0x12,0x13], 4);
/*I2C read*/
/*function i2C_read(i2c_builder,slave_address, register_address, tab,n_item)*/
i2C_read(i2c_builder,0x1B,0x21,2);
/*SENDING TO SP1018G*/
ScanaStudio.builder_set_repeat_count(1);
ScanaStudio.builder_start_chunk();
ScanaStudio.builder_wait_done(500);
ScanaStudio.console_info_msg("Last chunk sent");
return;
}
function get_builder_object()
{
var builder = ScanaStudio.load_builder_object("i2c.js");
return builder;
}
function i2C_write(i2c_builder,slave_address,register_address, tab,n_item)
{
i2c_builder.put_silence(5000);
i2c_builder.put_start(); //Start
i2c_builder.put_byte(slave_address<<1, 0); //Slave Adress + WRITE (Even) + ACK (so ACK=0)
//<<1 To shift a bit to the left
i2c_builder.put_byte(register_address, 0); //Register Adress + ACK (so ACK=0)
for (var i = 0; i < n_item-1; i++) //For all data except the last one
{
i2c_builder.put_byte(tab[i],0); //Data + ACK (so ACK=0)
}
i2c_builder.put_byte(tab[n_item-1],1); //Last Data + NACK (so ACK=1)
i2c_builder.put_stop(); //Stop
i2c_builder.put_silence(5000);
}
function i2C_read(i2c_builder,slave_address,register_address,n_item)
{
i2c_builder.put_silence(5000);
i2c_builder.put_start(); //Start
i2c_builder.put_byte(slave_address<<1, 0); //Slave Adress + WRITE (Even) + ACK (so ACK=0)
i2c_builder.put_byte(register_address, 0); //Register Adress + ACK (so ACK=0)
i2c_builder.put_start(); //Re-start
i2c_builder.put_byte((slave_address<<1)-1, 0); //Slave Adress + READ (Odd) + ACK (so ACK=0)
//<<1 To shift a bit to the left, -1 to make it odd
for (var i = 0; i < n_item-1; i++) //For all data except the last one
{
i2c_builder.put_byte(0xFF, 0); //Data + ACK (so ACK=0)
}
i2c_builder.put_byte(0xFF,1); //Last Data + NACK (so ACK=1)
i2c_builder.put_stop(); //Stop
i2c_builder.put_silence(5000);
}
- You can finally launch it in
ScanaStudio
and add i2c protocol :
SPI
Following the same methodology as with the UART generator script, below is a minimalistic fully functionnal script that shows how to generate SPI signals using the SP1000G serial logic analyzer and pattern generator.
function on_draw_gui_pattern_generator()
{
}
function on_eval_gui_pattern_generator()
{
return "" ; //All good.
}
function on_pattern_generate()
{
/*INITIALIZE CHANNELS*/
var ch_mosi = 0;
var ch_miso = 1;
var ch_clock = 2;
var ch_cs = 3;
/*ELECTRICAL SETTINGS*/
ScanaStudio.builder_set_out_voltage(ch_mosi, 3300) ;
ScanaStudio.builder_set_idle_state(ch_mosi, 0) ;
ScanaStudio.builder_set_io(ch_mosi, ScanaStudio.io_type.open_drain) ;
ScanaStudio.builder_set_out_voltage(ch_miso, 3300) ;
ScanaStudio.builder_set_idle_state(ch_miso, 0) ;
ScanaStudio.builder_set_io(ch_miso, ScanaStudio.io_type.open_drain) ;
ScanaStudio.builder_set_out_voltage(ch_clock, 3300) ;
ScanaStudio.builder_set_idle_state(ch_clock, 0) ;
ScanaStudio.builder_set_io(ch_clock, ScanaStudio.io_type.open_drain) ;
ScanaStudio.builder_set_out_voltage(ch_cs, 3300) ;
ScanaStudio.builder_set_idle_state(ch_cs, 1) ;
ScanaStudio.builder_set_io(ch_cs, ScanaStudio.io_type.open_drain) ;
/*Builder Object*/
var spi_builder = get_builder_object() ;
spi_builder.config(
/* SET CHANNEL (0 TO 17)*/
/*for MOSI channel*/
ch_mosi,
/*for MISO channel*/
ch_miso,
/*for CLOCK channel*/
ch_clock,
/*for CS channel*/
ch_cs,
/* SET BIT ORDER*/
/*=0 for MSB first, =1 for LSB first*/
0,
/* SET CLOCK POLARITY*/
/*=0 for clock LOW when inactive, =1 for clock HIGH when inactive*/
0,
/* SET CLOCK PHASE*/
/*=0 for data samples on leading edge, =1 for data samples on trailing edge*/
0,
/* SET NUMBER OF BITS PER WORD */
/* Index starts at 2*/
6,
/* SET CHIP SELECT*/
/*=0 for active LOW, =1 for active HIGH*/
0,
/*SAMPLING RATE*/
ScanaStudio.builder_get_sample_rate(),
/*CLOCK FREQUENCY*/
ScanaStudio.builder_get_sample_rate()/100
);
/*SIGNAL EXAMPLE*/
var tab_mosi = [0x10,0x20,0x30,0x40,0x50] ; //tab with data for MOSI
spi_write(spi_builder, tab_mosi) ;
/*SENDING TO SP1018G*/
ScanaStudio.builder_set_repeat_count(1) ;
ScanaStudio.builder_start_chunk() ;
ScanaStudio.builder_wait_done(500) ;
ScanaStudio.console_info_msg("Last chunk sent") ;
return ;
}
function get_builder_object()
{
var builder = ScanaStudio.load_builder_object("spi.js") ;
return builder ;
}
function spi_write(spi_builder, tab_mosi)
{
var n_item = tab_mosi.length -1 ; //number of data in tab
spi_builder.put_silence(1e-6);
spi_builder.put_start();
for (var i = 0 ; i< n_item+1 ; i++)
{
spi_builder.put_word(tab_mosi[i], 0xFF);
}
spi_builder.put_stop();
spi_builder.put_silence(1e-3);
}
- You can finally launch it in
ScanaStudio
and add SPI protocol :