Advanced Programming¶
trgenpy offers some advanced functions and features for users need to customize the output/input signal’s behaviour.
TrgenPin¶
Each Pin on the TRGen Device has an unique PIN_ID.
This is the classification of each Port grouped by connector Type
This is the complete list of the TRGen Device’s pinout:
Neuroscan IDs The Neuroscan pinout (only for used pins) goes from 0 to 7
[NS0,NS1,NS2,NS3,NS4,NS5,NS6,NS7]
Synamps IDs The Neuroscan pinout (only for used pins) goes from 0 to 7
[SA0,SA1,SA2,SA3,SA4,SA5,SA6,SA7]
BNC I/O IDs
BNCOBNCI
GPIO IDs The GPIO pinout goes from 0 to 7
[GPIO0,GPIO1,GPIO2,GPIO3,GPIO4,GPIO5,GPIO6,GPIO7]
The definition of a specific port is done by calling an enumeration through the TrgenPin class by doing TrgenPin.PIN_ID, like this:
# defining pin
neuroscan_third_pin = TrgenPin.NS3
TrgenPort¶
The TrgenPort object defines a single trigger behaviours through its id. This is the list of supported TrgenPin values for the TRGrgen:
Instantiate the TrgenPort object with:
# a NeuroScan port is defined passing
# the TrgenPin.NS3 enum in the constructor argument
client = TrgenClient()
neuro_scan = client.createTrgenPort(TrgenPin.NS3)
# but it's also possible to store the pin variable
bnco_pin = TrgenPin.BNCO
bnco_port = client.createTrgenPort(bnco_pin)
passing as only argument the enum from TrgenPin corresponding to the real used pin.
Custom Instruction Set¶
The TrgenPort object has a memory property.
memory has to be filled with a list of 2^mtml slots, default cardinality is 32.
Each slot can contain a single instruction that will be executed in order from the first to the last.
see the “Concatenate Instructions”” section for further informations.
The supported instruction set for TRGen Device is:
Unactive For N µs
The TrgenPort stays unactive for N µs
Active For N µs
The TrgenPort stays active for N µs
Wait Positive Edge
The TrgenPort waits until there is a positive edge
Wait Negative Edge
The TrgenPort waits until there is a negative edge
Repeat from N, for X times
The TrgenPort istrRepeat the instruction sequence from the index X to the current one, for X times
End
The TrgenPort istrEnd its behaviour
Not Admissible
This instruction is ignored, but it is necessary to fill the empty instruction list. NOTE: if the space is empty, it will be filled automatically by
trgenpy
Concatenate Instructions¶
Each TrgenPortcan be programmed with a bunch of instructions.
the function setInstruction() can be use to store instructuons into memory slots.
Instructions can be added in memory slots using the static function setInstruction(i, x), that adds a specific instruction (x) in the desired memory index (i).
Index position is important since the program counter of TRGen Device execute each TrgenPort instructions from 0 to 2^mtml.
def setInstruction(self, index, instruction):
# index: 0-32 value
# instruction: instruction_code
Remember, the setInstruction(index,instruction) function does not program the TrgenPort, it just stores the instructon, that will be written using writeTrgenMemory().
Instruction Helpers¶
Since for TRGen Device all instruction are “bitmask chunks” this library offers some helper functions that do the bitmask encoding. They can be used to define easily any instruction:
Instruction |
1° Param |
2° Param |
Description |
|---|---|---|---|
µ seconds duration |
Set the unactivation time for µ seconds |
||
µ seconds duration |
Set the activation time for µ seconds |
||
TrgenPin |
Wait the positive edge for a specific TrgenPin |
||
TrgenPin |
Wait the negative edge for a specific TrgenPin |
||
Instruction address |
Time of istrRepeat |
Set the activation time for µ seconds |
|
End the behaviour |
|||
Empty instruction, it has to be placed after the |
Write Instruction on TRGenPort¶
Once the desired instruction set has be setted, the memory can be written using the function writeTrgenMemory(trgenport=TrgenPin.BNCO) to a specific TrgenPort by its TrgenPin.
After this, the program can be executed invoking the static method start()
The following script use the whole instruction set in order to program a single TrgenPort
custom instructions examples¶
client = TrgenClient()
bnco = client.createTrgenPort(TrgenPin.BNCO)
impl = client.getImplementation()
# helper function used to clear the current TrgenPort memory
# it program the desired TrgenPort with a blank program
# made of one istrEnd() and all istrNotAdmissible() instructions
client.resetTrgenPort(bnco) # it is explained in the "Helpers" paragraph
bnco.setInstruction(0, istrActiveFor(5))
bnco.setInstruction(1, istrUnactiveFor(3))
bnco.setInstruction(2, istrRepeat(0,2))
bnco.setInstruction(3, istrWaitPE(TrgenPin.NS4))
bnco.setInstruction(4, istrUnactiveFor(3))
bnco.setInstruction(5, istrWaitNE(TrgenPin.NS0))
bnco.setInstruction(6, istrEnd()) # istrEnd is mandatory!
for i in range(7,impl.memory_length): # from 7 to 31!
bnco.setInstruction(i, istrNotAdmissible()) # this also is mandatory
# write the program
# every instruction in bnco.memory
# is flashed into TRGen Device
client.writeTrgenMemory(bnco)
# execute the TRGen Device
client.start()
writeTrgenMemory() write only specific TrgenPort. This means that many custom programs can be written
bnco = client.createTrgenPort(TrgenPin.BNCO)
ns7 = client.createTrgenPort(TrgenPin.NS7)
# you can also use this helper in order to reset All TrgenPort
client.resetAllTrgenPorts()
bnco.setInstruction(0, istrActiveFor(5))
bnco.setInstruction(1, istrUnactiveFor(3))
bnco.setInstruction(2, istrEnd())
for i in range(3,31):
bnco.setInstruction(i, istrNotAdmissible())
ns7.setInstruction(0, istrWaitPE(TrgenPin.BNCO))
ns7.setInstruction(1, istrActiveFor(5))
ns7.setInstruction(2, istrUnactiveFor(3))
ns7.setInstruction(3, istrEnd())
for i in range(4,31):
ns7.setInstruction(i, istrNotAdmissible())
client.writeTrgenMemory(ns7)
client.writeTrgenMemory(bnco)
# execute the TRGen Device
client.start()