XS07 Script Production Example: I2C protocol decoder
The next example is a simple I2C protocol decoder. The example shows, how to analyse logic signals and how to generate a struct signal.
// input: an array of all input signals // in0: primary input of type ISamplePointer,IReadableSamples // in1..: additional inputs of type ISamplePointer,IReadableSamples // out: output signal of type ??? // console: console output of type MessageConsoleStream // iter: iterator of type ISamplesIterator // progress: progess control of type IScriptProgress // input var sda <:ISamplePointer:> = input[0]; // sda input var scl <:ISamplePointer:> = input[1]; // scl input // state var states, state, started, pos, bits, address, byt, bytes, write, members, extAddressing; // init if (state == null) { // signal members = out.createMembers(6); out.createMember(members, 0, "RW", ISample.STRUCT_TYPE_ENUM, null, -1); out.createMember(members, 1, "Address", ISample.STRUCT_TYPE_INTEGER, null, ISample.FORMAT_HEXADECIMAL); out.createMember(members, 2, "Data", ISample.STRUCT_TYPE_BINARY, null, ISample.FORMAT_HEXADECIMAL); out.createMember(members, 3, "Bytes", ISample.STRUCT_TYPE_INTEGER, null, -1); out.createMember(members, 4, "Duration", ISample.STRUCT_TYPE_INTEGER, null, -1); out.createMember(members, 5, "Addressing", ISample.STRUCT_TYPE_ENUM, null, -1); // states states = { INIT: 0, STARTED: 1, GOT_ADDRESS: 2, READ_DATA: 3 }; state = states.INIT; } // iterate progress.cont(); while (iter.hasNext()) { var current <:Long:> = iter.next(out); // start condition if (sda.isEdge(-1) && scl.isHigh()) { state = states.STARTED; started = current; bytes = 0; extAddressing = null; bits = 0; continue; } // stop condition if (state != states.INIT && sda.isEdge(1) && scl.isHigh()) { state = 0; members[0].setValue(read == 1 ? "Read" : "Write"); members[1].setValue(new java.lang.Integer(address)); members[2].setValue(new java.math.BigInteger(data, 16) .toByteArray()); members[3].setValue(new java.lang.Integer(bytes)); members[4].setValue(new java.lang.Long(current - started)); members[5].setValue(extAddressing ? "10 Bit" : "7 Bit"); out.write(started, false, members); continue; } // address if (state == states.STARTED && scl.isEdge(1)) { if (bits == 0) { address = 0; data = ""; pos = current; } bits++; address = (address << 1) | (sda.isHigh() ? 1 : 0); if (bits == 7) { state = states.GOT_ADDRESS; bits = 0; } continue; } // rw / ackn if (state == states.GOT_ADDRESS && scl.isEdge(1)) { bits++; if (bits == 1) { // rw read = sda.isHigh(); } if (bits == 2) { // ackn var ackn = sda.isHigh(); state = states.READ_DATA; bits = 0; } continue; } // data if (state == states.READ_DATA && scl.isEdge(1)) { if (bits == 0) { byt = 0; pos = current; } bits++; byt = (byt << 1) | (sda.isHigh() ? 1 : 0); if (bits == 8) { if (extAddressing == null) { if ((address & 0x7c) == 0x78) { address = (address & 3) << 8 | byt; extAddressing = 1; } else { extAddressing = 0; bytes++; data += byt.toString(16); } } else { bytes++; data += byt.toString(16); } } if (bits == 9) { // ackn var ackn = sda.isHigh(); bits = 0; } continue; } }About Signal Scripts Open JavaDoc Reference