Jump to content
  • 0

Script Editor: Decoding Doesn't Extend Across Entire Waveform


Empower

Question

I have a custom serial bus that I've modified the SPI logic analyzer example script to create a nearly-working decoder.  Referring to the image below, I'd like the decoded `0x08b` to extend full width of the yellow box.  How do I do this?
image.thumb.png.ac21edf91f270d33983c1bd7168a02e4.png

Decoder

// This scripted is intended for use with Digilent Analog Discovery 3.
// Refer to https://digilent.com/reference/test-and-measurement/guides/waveforms-script-editor

// rgData: input, raw digital sample array
// rgValue: output, decoded data array
// rgFlag: output, decoded flag array

var sample_count    = rgData.length // sample_count = number of raw samples
var previous_clock  = false;        // previous cock signal level
var start_index     = 0;            // used to keep track on word start index
var sbus_bit_count  = 0;            // byte count per transmission
var bit_count       = 0;            // bit counter
var bit_value       = 0;            // bit value 
var byte_value      = 0;            // byte value
var sbus_bit        = 0;            // sbus bit
var num_sbus_bits   = 0;
var sbus_byte_count = 0;

for (var sample_index = 0; sample_index < sample_count; sample_index++) { // for each sample
    var current_sample = rgData[sample_index];      // current sample
    var clock_pin      = 1 & (current_sample >> 1); // pin1 is the clock signal
    var data_pin       = 1 & (current_sample >> 2); // pin2 is the data signal

    if (previous_clock == 0 && clock_pin != 0) { // sample on clock rising edge
        bit_value <<= 1; // serial data bit, MSBit first
        
        if (data_pin) bit_value |= 1;
        bit_count++;
        
        if (bit_count == 2) { // when got the 2nd bit of the word store it
            sbus_bit_count++;
            
            if (bit_value == 0) {
                num_sbus_bits = 1;
                sbus_bit = 0; // "S" Start
            }
            else if (bit_value == 1) {
                num_sbus_bits = 8;
                sbus_bit = 1;
            }
            else if (bit_value == 2) {
                num_sbus_bits = 8;
                sbus_bit = 0;
            }
            else {
                num_sbus_bits = 1;
                sbus_bit = 1; // "P" Stop, End of Message  
            }
            byte_value = (sbus_bit << (7 - (sbus_bit_count - 1))) | byte_value;
            if (sbus_bit_count == num_sbus_bits) { // when got the 2nd bit of the word store it
                sbus_byte_count++;
                
                // store rgValue/Flag from word start index to current sample position
                for (var j = start_index; j < sample_index; j++) {
                    // rgFlag and rgValue used in Value2Text()
                    // Flag change will be visible on plot even when data remains constant.
                    // This is useful in case we get more consecutive equal bit_values.
                    rgFlag[j] = sbus_bit_count; // If rgFlag[] is > 0, waveform is displayed
                    rgValue[j] = byte_value;
                }
                byte_value = 0;
                sbus_bit_count = 0;
            }
            bit_value = 0; // reset bit_value variable
            bit_count = 0;  // reset bit count for the next byte
            start_index = sample_index + 1; // next word might start after this sample
        }
    }
    previous_clock = clock_pin; // previous clock level
}

Value2Text

// This scripted is intended for use with Digilent Analog Discovery 3.
// Refer to https://digilent.com/reference/test-and-measurement/guides/waveforms-script-editor

// value: value sample
// flag: flag sample - the flag is the counter

function Value2Text(flag, value){
  var sbus_value = "";
  var this_flag = flag;

  switch(flag){
    case 0: return "X";
    default: 
        if (this_flag == 1) {
            if (value == 0) {
                sbus_value += "S"; // Start of message, hold
            }
            else {
                sbus_value += "P"; // End of message
            }
        }
        else {
            sbus_value += "0x" + value.toString(16).toUpperCase();
        }
    return sbus_value;
  }
}
Value2Text(1,1)

 

Sbus Pattern 20240112.dwf3patterns SBus Logic Analyzer 20240112.dwf3logic

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Hi @Empower

Move start_index = sample_index + 1;

image.png

 

like in the included example:

image.png

 

// This scripted is intended for use with Digilent Analog Discovery 3.
// Refer to https://digilent.com/reference/test-and-measurement/guides/waveforms-script-editor

// rgData: input, raw digital sample array
// rgValue: output, decoded data array
// rgFlag: output, decoded flag array

var sample_count    = rgData.length // sample_count = number of raw samples
var previous_clock  = false;        // previous cock signal level
var start_index     = 0;            // used to keep track on word start index
var sbus_bit_count  = 0;            // byte count per transmission
var bit_count       = 0;            // bit counter
var bit_value       = 0;            // bit value 
var byte_value      = 0;            // byte value
var sbus_bit        = 0;            // sbus bit
var num_sbus_bits   = 0;
var sbus_byte_count = 0;

for (var sample_index = 0; sample_index < sample_count; sample_index++) { // for each sample
    var current_sample = rgData[sample_index]; // current sample
    // var chip_select_pin = 1 & (current_sample >> 0); // pin0 is the select signal
    var clock_pin = 1 & (current_sample >> 1); // pin1 is the clock signal
    var data_pin = 1 & (current_sample >> 2); // pin2 is the data signal

    // if (chip_select_pin != 0) { // select active low
        // // while select inactive reset our counters/variables
        // start_index = sample_index+1; // select might become active with next sample
        // sbus_bit_count = 0;
        // bit_count = 0;
        // bit_value = 0;
        // previous_clock = false;
        // continue;
    // }
    if (previous_clock == 0 && clock_pin != 0) { // sample on clock rising edge
        bit_value <<= 1; // serial data bit, MSBit first
        
        if (data_pin) bit_value |= 1;
        bit_count++;
        
        if (bit_count == 2) { // when got the 2nd bit of the word store it
            sbus_bit_count++;
            
            if (bit_value == 0) {
                num_sbus_bits = 1;
                sbus_bit = 0; // "S" Start
            }
            else if (bit_value == 1) {
                num_sbus_bits = 8;
                sbus_bit = 1;
            }
            else if (bit_value == 2) {
                num_sbus_bits = 8;
                sbus_bit = 0;
            }
            else {
                num_sbus_bits = 1;
                sbus_bit = 1; // "P" Stop, End of Message  
            }
            byte_value = (sbus_bit << (7 - (sbus_bit_count - 1))) | byte_value;
            if (sbus_bit_count == num_sbus_bits) { // when got the 2nd bit of the word store it
                sbus_byte_count++;
                
                // store rgValue/Flag from word start index to current sample position
                for (var j = start_index; j < sample_index; j++) {
                    // rgFlag and rgValue used in Value2Text()
                    // Flag change will be visible on plot even when data remains constant.
                    // This is useful in case we get more consecutive equal bit_values.
                    rgFlag[j] = sbus_bit_count; // If rgFlag[] is > 0, waveform is displayed
                    rgValue[j] = byte_value;
                }
                start_index = sample_index + 1; // next word might start after this sample
                byte_value = 0;
                sbus_bit_count = 0;
            }
            bit_value = 0; // reset bit_value variable
            bit_count = 0;  // reset bit count for the next byte
        }
    }
    previous_clock = clock_pin; // previous clock level
}

 

Link to comment
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
×
×
  • Create New...